easysetup.c 5.14 KB
Newer Older
1 2
//******************************************************************
//
3
// Copyright 2015 Samsung Electronics All Rights Reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

21 22 23 24 25 26
/**
 * @file
 *
 * This file contains the implementation for EasySetup Enrollee device
 */

27 28
#include "easysetup.h"
#include "logger.h"
29
#include "resourcehandler.h"
30
#include "easysetupcallbacks.h"
31

32 33 34 35 36 37 38 39 40
/**
 * @var ES_ENROLLEE_TAG
 * @brief Logging tag for module name.
 */
#define ES_ENROLLEE_TAG "ES"

//-----------------------------------------------------------------------------
// Private variables
//-----------------------------------------------------------------------------
41
static bool gIsSecured = false;
42

43
static ESProvisioningCallbacks gESProvisioningCb;
44

45 46 47
void ESWiFiRsrcCallback(ESResult esResult, ESWiFiProvData *eventData)
{
    OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESWiFiRsrcCallback IN");
48

49 50 51 52 53
    if(esResult != ES_OK)
    {
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESWiFiRsrcCallback Error Occured");
        return;
    }
54

55 56 57 58 59 60 61 62 63 64
    // deliver data to ESProvisioningCallbacks
    if(gESProvisioningCb.WiFiProvCb != NULL)
    {
        gESProvisioningCb.WiFiProvCb(eventData);
    }
    else
    {
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "WiFiProvCb is NULL");
        return;
    }
65 66
}

67
void ESCloudRsrcCallback(ESResult esResult, ESCloudProvData *eventData)
68
{
69
    OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESCloudRsrcCallback IN");
70

71
    if(esResult != ES_OK)
72
    {
73 74 75
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESCloudRsrcCallback Error Occured");
        return;
    }
76

77 78 79
    if(gESProvisioningCb.CloudDataProvCb != NULL)
    {
        gESProvisioningCb.CloudDataProvCb(eventData);
80 81 82
    }
    else
    {
83 84
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "CloudDataProvCb is NULL");
        return;
85 86 87
    }
}

88
void ESDevconfRsrcallback(ESResult esResult, ESDevConfProvData *eventData)
89
{
90 91 92 93 94 95 96 97 98
    OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESDevconfRsrcallback IN");

    if(esResult != ES_OK)
    {
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESDevconfRsrcallback Error Occured");
        return;
    }

    if(gESProvisioningCb.DevConfProvCb != NULL)
99
    {
100
        gESProvisioningCb.DevConfProvCb(eventData);
101
    }
102 103
    else
    {
104 105
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb is NULL");
        return;
106 107 108
    }
}

109
ESResult ESInitEnrollee(bool isSecured, ESResourceMask resourceMask, ESProvisioningCallbacks callbacks)
110
{
111
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee IN");
112

113 114
    gIsSecured = isSecured;

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
    {
        if(callbacks.WiFiProvCb != NULL)
        {
            gESProvisioningCb.WiFiProvCb = callbacks.WiFiProvCb;
            RegisterWifiRsrcEventCallBack(ESWiFiRsrcCallback);
        }
        else
        {
            OIC_LOG(ERROR, ES_ENROLLEE_TAG, "WiFiProvCb NULL");
            return ES_ERROR;
        }
    }
    if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
    {
        if(callbacks.DevConfProvCb != NULL)
        {
            gESProvisioningCb.DevConfProvCb = callbacks.DevConfProvCb;
            RegisterDevConfRsrcEventCallBack(ESDevconfRsrcallback);
        }
        else
        {
            OIC_LOG(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb NULL");
            return ES_ERROR;
        }
    }
    if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
    {
        if(callbacks.DevConfProvCb != NULL)
        {
            gESProvisioningCb.CloudDataProvCb = callbacks.CloudDataProvCb;
            RegisterCloudRsrcEventCallBack(ESCloudRsrcCallback);
        }
        else
        {
            OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CloudDataProvCb NULL");
            return ES_ERROR;
        }
    }
154

155
    if(CreateEasySetupResources(gIsSecured, resourceMask) != OC_STACK_OK)
156
    {
157
        // TODO : Error Handling
158 159
        return ES_ERROR;
    }
160

161

162
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee OUT");
163
    return ES_OK;
164 165
}

166
ESResult ESTerminateEnrollee()
167
{
168 169 170
    UnRegisterResourceEventCallBack();

    //Delete Prov resource
171
    if (DeleteEasySetupResources() != OC_STACK_OK)
172
    {
173
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
174 175
        return ES_ERROR;
    }
176

177
    OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");
178
    return ES_OK;
179 180
}

181 182
static bool ESEnrolleeValidateParam(OCConnectivityType networkType, const char *ssid,
                                                const char *passwd, ESEnrolleeEventCallback cb)
183 184 185
{
    if (!ssid || !passwd || !cb)
    {
186
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESEnrolleeValidateParam - Invalid parameters");
187 188 189 190 191
        return false;
    }
    return true;
}