easysetup.c 7.82 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

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
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;
    }
}

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

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

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

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

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

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

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

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

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

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

135 136
    gIsSecured = isSecured;

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

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

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

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

193 194
        return ES_ERROR;
    }
195

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

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

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

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

214 215 216 217
ESResult ESSetState(ESEnrolleeState esState)
{
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState IN");

218
    if(esState < ES_STATE_INIT || esState >= ES_STATE_EOF)
219 220 221 222 223 224 225
    {
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "Invalid ESEnrolleeState : %d", esState);
        return ES_ERROR;
    }

    if(SetEnrolleeState(esState) != OC_STACK_OK)
    {
226
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetState ES_ERROR");
227 228 229
        return ES_ERROR;
    }

230
    OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Set ESState succesfully : %d", esState);
231 232 233 234 235 236 237 238
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState OUT");
    return ES_OK;
}

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

239
    if(esErrCode < ES_ERRCODE_NO_ERROR || esErrCode > ES_ERRCODE_UNKNOWN)
240
    {
241
        OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "Invalid ESSetErrorCode : %d", esErrCode);
242 243 244 245 246
            return ES_ERROR;
    }

    if(SetEnrolleeErrCode(esErrCode) != OC_STACK_OK)
    {
247
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetErrorCode ES_ERROR");
248 249 250
        return ES_ERROR;
    }

251
    OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Set ESErrorCode succesfully : %d", esErrCode);
252 253 254 255
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetErrorCode OUT");
    return ES_OK;
}

256
ESResult ESTerminateEnrollee()
257
{
258 259
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESTerminateEnrollee IN");

260 261 262
    UnRegisterResourceEventCallBack();

    //Delete Prov resource
263
    if (DeleteEasySetupResources() != OC_STACK_OK)
264
    {
265
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
266 267
        return ES_ERROR;
    }
268

269
    OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");
270
    return ES_OK;
271
}
272 273 274

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

277 278 279 280 281 282 283 284
    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;
285
}