easysetup.c 8.54 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 "oic_string.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

42
static bool gIsSecured = false;
43

44
static ESProvisioningCallbacks gESProvisioningCb;
45
static ESDeviceProperty gESDeviceProperty;
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
void ESConnectRequestCallback(ESResult esResult, ESConnectRequest *eventData)
{
    OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESConnectRequestCallback IN");

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

    if(gESProvisioningCb.ConnectRequestCb != NULL)
    {
        gESProvisioningCb.ConnectRequestCb(eventData);
    }
    else
    {
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ConnectRequestCb is NULL");
        return;
    }
}

68
void ESWiFiConfRsrcCallback(ESResult esResult, ESWiFiConfData *eventData)
69
{
70
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESWiFiConfRsrcCallback IN");
71

72 73
    if(esResult != ES_OK)
    {
74
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESWiFiConfRsrcCallback Error Occured");
75 76
        return;
    }
77

78
    // deliver data to ESProvisioningCallbacks
79
    if(gESProvisioningCb.WiFiConfProvCb != NULL)
80
    {
81
        gESProvisioningCb.WiFiConfProvCb(eventData);
82 83 84
    }
    else
    {
85
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "WiFiConfProvCb is NULL");
86 87
        return;
    }
88 89
}

90
void ESCoapCloudConfRsrcCallback(ESResult esResult, ESCoapCloudConfData *eventData)
91
{
92
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESCoapCloudConfRsrcCallback IN");
93

94
    if(esResult != ES_OK)
95
    {
96
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESCoapCloudConfRsrcCallback Error Occured");
97 98
        return;
    }
99

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

111
void ESDevConfRsrcallback(ESResult esResult, ESDevConfData *eventData)
112
{
113
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESDevConfRsrcallback IN");
114 115 116

    if(esResult != ES_OK)
    {
117
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESDevConfRsrcallback Error Occured");
118 119 120 121
        return;
    }

    if(gESProvisioningCb.DevConfProvCb != NULL)
122
    {
123
        gESProvisioningCb.DevConfProvCb(eventData);
124
    }
125 126
    else
    {
127
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb is NULL");
128
        return;
129 130 131
    }
}

132
ESResult ESInitEnrollee(bool isSecured, ESResourceMask resourceMask, ESProvisioningCallbacks callbacks)
133
{
134
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee IN");
135

136 137
    gIsSecured = isSecured;

138
    if((resourceMask & ES_WIFICONF_RESOURCE) == ES_WIFICONF_RESOURCE)
139
    {
140
        if(callbacks.WiFiConfProvCb != NULL)
141
        {
142 143
            gESProvisioningCb.WiFiConfProvCb = callbacks.WiFiConfProvCb;
            RegisterWifiRsrcEventCallBack(ESWiFiConfRsrcCallback);
144 145 146
        }
        else
        {
147
            OIC_LOG(ERROR, ES_ENROLLEE_TAG, "WiFiConfProvCb NULL");
148 149 150 151 152 153 154 155
            return ES_ERROR;
        }
    }
    if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
    {
        if(callbacks.DevConfProvCb != NULL)
        {
            gESProvisioningCb.DevConfProvCb = callbacks.DevConfProvCb;
156
            RegisterDevConfRsrcEventCallBack(ESDevConfRsrcallback);
157 158 159 160 161 162 163
        }
        else
        {
            OIC_LOG(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb NULL");
            return ES_ERROR;
        }
    }
164
    if((resourceMask & ES_COAPCLOUDCONF_RESOURCE) == ES_COAPCLOUDCONF_RESOURCE)
165
    {
166
        if(callbacks.CoapCloudConfProvCb != NULL)
167
        {
168 169
            gESProvisioningCb.CoapCloudConfProvCb = callbacks.CoapCloudConfProvCb;
            RegisterCloudRsrcEventCallBack(ESCoapCloudConfRsrcCallback);
170 171 172
        }
        else
        {
173
            OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CoapCloudConfProvCb NULL");
174 175 176
            return ES_ERROR;
        }
    }
177

178 179 180 181 182 183 184
    // TODO: if EasySetupProvCb is NULL, we should return an error at this moment.
    if(callbacks.ConnectRequestCb != NULL)
    {
        gESProvisioningCb.ConnectRequestCb = callbacks.ConnectRequestCb;
        RegisterConnectRequestEventCallBack(ESConnectRequestCallback);
    }

185
    if(CreateEasySetupResources(gIsSecured, resourceMask) != OC_STACK_OK)
186
    {
187 188 189 190 191 192 193
        UnRegisterResourceEventCallBack();

        if (DeleteEasySetupResources() != OC_STACK_OK)
        {
            OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
        }

194 195
        return ES_ERROR;
    }
196

197
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee OUT");
198
    return ES_OK;
199 200
}

201 202 203 204 205 206
ESResult ESSetDeviceProperty(ESDeviceProperty *deviceProperty)
{
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetDeviceProperty IN");

    if(SetDeviceProperty(deviceProperty) != OC_STACK_OK)
    {
207
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetDeviceProperty Error");
208 209 210 211 212 213 214
        return ES_ERROR;
    }

    int modeIdx = 0;
    while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
    {
        (gESDeviceProperty.WiFi).mode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
215
        OIC_LOG_V(INFO_PRIVATE, ES_ENROLLEE_TAG, "WiFi Mode : %d", (gESDeviceProperty.WiFi).mode[modeIdx]);
216 217 218
        modeIdx ++;
    }
    (gESDeviceProperty.WiFi).freq = (deviceProperty->WiFi).freq;
219
    OIC_LOG_V(INFO_PRIVATE, ES_ENROLLEE_TAG, "WiFi Freq : %d", (gESDeviceProperty.WiFi).freq);
220

221
    OICStrcpy((gESDeviceProperty.DevConf).deviceName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
222
    OIC_LOG_V(INFO_PRIVATE, ES_ENROLLEE_TAG, "Device Name : %s", (gESDeviceProperty.DevConf).deviceName);
223 224 225 226 227

    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetDeviceProperty OUT");
    return ES_OK;
}

228 229 230 231
ESResult ESSetState(ESEnrolleeState esState)
{
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState IN");

232
    if(esState < ES_STATE_INIT || esState >= ES_STATE_EOF)
233 234 235 236 237 238 239
    {
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "Invalid ESEnrolleeState : %d", esState);
        return ES_ERROR;
    }

    if(SetEnrolleeState(esState) != OC_STACK_OK)
    {
240
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetState ES_ERROR");
241 242 243
        return ES_ERROR;
    }

244
    OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Set ESState succesfully : %d", esState);
245 246 247 248 249 250 251 252
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState OUT");
    return ES_OK;
}

ESResult ESSetErrorCode(ESErrorCode esErrCode)
{
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetErrorCode IN");

253
    if(esErrCode < ES_ERRCODE_NO_ERROR || esErrCode > ES_ERRCODE_UNKNOWN)
254
    {
255
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "Invalid ESSetErrorCode : %d", esErrCode);
256 257 258 259 260
            return ES_ERROR;
    }

    if(SetEnrolleeErrCode(esErrCode) != OC_STACK_OK)
    {
261
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetErrorCode ES_ERROR");
262 263 264
        return ES_ERROR;
    }

265
    OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Set ESErrorCode succesfully : %d", esErrCode);
266 267 268 269
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetErrorCode OUT");
    return ES_OK;
}

270
ESResult ESTerminateEnrollee()
271
{
272 273
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESTerminateEnrollee IN");

274 275 276
    UnRegisterResourceEventCallBack();

    //Delete Prov resource
277
    if (DeleteEasySetupResources() != OC_STACK_OK)
278
    {
279
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
280 281
        return ES_ERROR;
    }
282

283
    OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");
284
    return ES_OK;
285
}
286 287 288

ESResult ESSetCallbackForUserdata(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
{
289 290
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetCallbackForUserdata IN");

291 292 293 294 295 296 297 298 299
    if(!readCb && !writeCb)
    {
        OIC_LOG(INFO, ES_ENROLLEE_TAG, "Both of callbacks for user data are null");
        return ES_ERROR;
    }

    SetCallbackForUserData(readCb, writeCb);
    return ES_OK;
}