1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains the implementation for EasySetup Enrollee device
27 #include "easysetup.h"
29 #include "resourcehandler.h"
30 #include "oic_string.h"
33 * @var ES_ENROLLEE_TAG
34 * @brief Logging tag for module name.
36 #define ES_ENROLLEE_TAG "ES"
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 static bool gIsSecured = false;
44 static ESProvisioningCallbacks gESProvisioningCb;
45 static ESDeviceProperty gESDeviceProperty;
47 void ESWiFiRsrcCallback(ESResult esResult, ESWiFiProvData *eventData)
49 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESWiFiRsrcCallback IN");
53 OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESWiFiRsrcCallback Error Occured");
57 // deliver data to ESProvisioningCallbacks
58 if(gESProvisioningCb.WiFiProvCb != NULL)
60 gESProvisioningCb.WiFiProvCb(eventData);
64 OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "WiFiProvCb is NULL");
69 void ESCloudRsrcCallback(ESResult esResult, ESCloudProvData *eventData)
71 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESCloudRsrcCallback IN");
75 OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESCloudRsrcCallback Error Occured");
79 if(gESProvisioningCb.CloudDataProvCb != NULL)
81 gESProvisioningCb.CloudDataProvCb(eventData);
85 OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "CloudDataProvCb is NULL");
90 void ESDevconfRsrcallback(ESResult esResult, ESDevConfProvData *eventData)
92 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESDevconfRsrcallback IN");
96 OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESDevconfRsrcallback Error Occured");
100 if(gESProvisioningCb.DevConfProvCb != NULL)
102 gESProvisioningCb.DevConfProvCb(eventData);
106 OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb is NULL");
111 ESResult ESInitEnrollee(bool isSecured, ESResourceMask resourceMask, ESProvisioningCallbacks callbacks)
113 OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee IN");
115 gIsSecured = isSecured;
117 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
119 if(callbacks.WiFiProvCb != NULL)
121 gESProvisioningCb.WiFiProvCb = callbacks.WiFiProvCb;
122 RegisterWifiRsrcEventCallBack(ESWiFiRsrcCallback);
126 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "WiFiProvCb NULL");
130 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
132 if(callbacks.DevConfProvCb != NULL)
134 gESProvisioningCb.DevConfProvCb = callbacks.DevConfProvCb;
135 RegisterDevConfRsrcEventCallBack(ESDevconfRsrcallback);
139 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb NULL");
143 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
145 if(callbacks.DevConfProvCb != NULL)
147 gESProvisioningCb.CloudDataProvCb = callbacks.CloudDataProvCb;
148 RegisterCloudRsrcEventCallBack(ESCloudRsrcCallback);
152 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CloudDataProvCb NULL");
157 if(CreateEasySetupResources(gIsSecured, resourceMask) != OC_STACK_OK)
159 UnRegisterResourceEventCallBack();
161 if (DeleteEasySetupResources() != OC_STACK_OK)
163 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
170 OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee OUT");
174 ESResult ESSetDeviceProperty(ESDeviceProperty *deviceProperty)
176 OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetDeviceProperty IN");
178 if(SetDeviceProperty(deviceProperty) != OC_STACK_OK)
180 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetDeviceProperty Error");
185 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
187 (gESDeviceProperty.WiFi).mode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
188 OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "WiFi Mode : %d", (gESDeviceProperty.WiFi).mode[modeIdx]);
191 (gESDeviceProperty.WiFi).freq = (deviceProperty->WiFi).freq;
192 OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "WiFi Freq : %d", (gESDeviceProperty.WiFi).freq);
194 OICStrcpy((gESDeviceProperty.DevConf).deviceName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
195 OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Device Name : %s", (gESDeviceProperty.DevConf).deviceName);
197 OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetDeviceProperty OUT");
201 ESResult ESSetState(ESEnrolleeState esState)
203 OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState IN");
205 if(esState < ES_STATE_INIT || esState > ES_STATE_REGISTRRED_FAIL_TO_CLOUD)
207 OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "Invalid ESEnrolleeState : %d", esState);
211 if(SetEnrolleeState(esState) != OC_STACK_OK)
213 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetState ES_ERROR");
217 OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Set ESState succesfully : %d", esState);
218 OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState OUT");
222 ESResult ESSetErrorCode(ESErrorCode esErrCode)
224 OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetErrorCode IN");
226 if(esErrCode < ES_ERRCODE_NO_ERROR || esErrCode > ES_ERRCODE_UNKNOWN)
228 OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "Invalid ESSetErrorCode : %d", esErrCode);
232 if(SetEnrolleeErrCode(esErrCode) != OC_STACK_OK)
234 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetErrorCode ES_ERROR");
238 OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Set ESErrorCode succesfully : %d", esErrCode);
239 OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetErrorCode OUT");
243 ESResult ESTerminateEnrollee()
245 OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESTerminateEnrollee IN");
247 UnRegisterResourceEventCallBack();
249 //Delete Prov resource
250 if (DeleteEasySetupResources() != OC_STACK_OK)
252 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
256 OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");