easysetup.c 8.29 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 218 219 220 221 222 223 224
#if defined(__GNUC__)
#pragma GCC diagnostic push
/*
 * Prevent reporting esState < ES_STATE_INT and esErrCode < ES_ERRCODE_NO_ERROR as type-limits
 * The type used for an enum is decided by the compiler the only way to know that the value
 * passed in by the user is within the limits is to check.
 * Note this warning is seems to be limited to older compilers.
 */
#pragma GCC diagnostic ignored "-Wtype-limits"
#endif

225 226 227 228
ESResult ESSetState(ESEnrolleeState esState)
{
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState IN");

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

    if(SetEnrolleeState(esState) != OC_STACK_OK)
    {
237
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetState ES_ERROR");
238 239 240
        return ES_ERROR;
    }

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

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

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

    if(SetEnrolleeErrCode(esErrCode) != OC_STACK_OK)
    {
258
        OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetErrorCode ES_ERROR");
259 260 261
        return ES_ERROR;
    }

262
    OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Set ESErrorCode succesfully : %d", esErrCode);
263 264 265 266
    OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetErrorCode OUT");
    return ES_OK;
}

267 268 269 270
#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif

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

275 276 277
    UnRegisterResourceEventCallBack();

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

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

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

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;
300
}