[Enrollee] Implement ESSetDeviceProperty() API
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / src / easysetup.c
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /**
22  * @file
23  *
24  * This file contains the implementation for EasySetup Enrollee device
25  */
26
27 #include "easysetup.h"
28 #include "logger.h"
29 #include "resourcehandler.h"
30 #include "oic_string.h"
31
32 /**
33  * @var ES_ENROLLEE_TAG
34  * @brief Logging tag for module name.
35  */
36 #define ES_ENROLLEE_TAG "ES"
37
38 //-----------------------------------------------------------------------------
39 // Private variables
40 //-----------------------------------------------------------------------------
41
42 /**
43  * @var gTargetSsid
44  * @brief Target SSID of the Soft Access point to which the device has to connect
45  */
46 // static char gTargetSsid[MAXSSIDLEN];
47
48 /**
49  * @var gTargetPass
50  * @brief Password of the target access point to which the device has to connect
51  */
52 // static char gTargetPass[MAXNETCREDLEN];
53
54 /**
55  * @var gEnrolleeStatusCb
56  * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
57  */
58 // static ESEnrolleeEventCallback gEnrolleeStatusCb = NULL;
59
60 /**
61  * @var gIsSecured
62  * @brief Variable to check if secure mode is enabled or not.
63  */
64 static bool gIsSecured = false;
65
66 static ESProvisioningCallbacks gESProvisioningCb;
67 static ESDeviceProperty gESDeviceProperty;
68
69 void ESWiFiRsrcCallback(ESResult esResult, ESWiFiProvData *eventData)
70 {
71     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESWiFiRsrcCallback IN");
72
73     if(esResult != ES_OK)
74     {
75         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESWiFiRsrcCallback Error Occured");
76         return;
77     }
78
79     // deliver data to ESProvisioningCallbacks
80     if(gESProvisioningCb.WiFiProvCb != NULL)
81     {
82         gESProvisioningCb.WiFiProvCb(eventData);
83     }
84     else
85     {
86         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "WiFiProvCb is NULL");
87         return;
88     }
89 }
90
91 void ESCloudRsrcCallback(ESResult esResult, ESCloudProvData *eventData)
92 {
93     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESCloudRsrcCallback IN");
94
95     if(esResult != ES_OK)
96     {
97         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESCloudRsrcCallback Error Occured");
98         return;
99     }
100
101     if(gESProvisioningCb.CloudDataProvCb != NULL)
102     {
103         gESProvisioningCb.CloudDataProvCb(eventData);
104     }
105     else
106     {
107         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "CloudDataProvCb is NULL");
108         return;
109     }
110 }
111
112 void ESDevconfRsrcallback(ESResult esResult, ESDevConfProvData *eventData)
113 {
114     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESDevconfRsrcallback IN");
115
116     if(esResult != ES_OK)
117     {
118         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESDevconfRsrcallback Error Occured");
119         return;
120     }
121
122     if(gESProvisioningCb.DevConfProvCb != NULL)
123     {
124         gESProvisioningCb.DevConfProvCb(eventData);
125     }
126     else
127     {
128         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb is NULL");
129         return;
130     }
131 }
132
133 ESResult ESInitEnrollee(bool isSecured, ESResourceMask resourceMask, ESProvisioningCallbacks callbacks)
134 {
135     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee IN");
136
137     gIsSecured = isSecured;
138
139     if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
140     {
141         if(callbacks.WiFiProvCb != NULL)
142         {
143             gESProvisioningCb.WiFiProvCb = callbacks.WiFiProvCb;
144             RegisterWifiRsrcEventCallBack(ESWiFiRsrcCallback);
145         }
146         else
147         {
148             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "WiFiProvCb NULL");
149             return ES_ERROR;
150         }
151     }
152     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
153     {
154         if(callbacks.DevConfProvCb != NULL)
155         {
156             gESProvisioningCb.DevConfProvCb = callbacks.DevConfProvCb;
157             RegisterDevConfRsrcEventCallBack(ESDevconfRsrcallback);
158         }
159         else
160         {
161             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb NULL");
162             return ES_ERROR;
163         }
164     }
165     if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
166     {
167         if(callbacks.DevConfProvCb != NULL)
168         {
169             gESProvisioningCb.CloudDataProvCb = callbacks.CloudDataProvCb;
170             RegisterCloudRsrcEventCallBack(ESCloudRsrcCallback);
171         }
172         else
173         {
174             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CloudDataProvCb NULL");
175             return ES_ERROR;
176         }
177     }
178
179     if(CreateEasySetupResources(gIsSecured, resourceMask) != OC_STACK_OK)
180     {
181         // TODO : Error Handling
182         return ES_ERROR;
183     }
184
185
186     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee OUT");
187     return ES_OK;
188 }
189
190 ESResult ESSetDeviceProperty(ESDeviceProperty *deviceProperty)
191 {
192     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetDeviceProperty IN");
193
194     if(SetDeviceProperty(deviceProperty) != OC_STACK_OK)
195     {
196         return ES_ERROR;
197     }
198
199     int modeIdx = 0;
200     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
201     {
202         (gESDeviceProperty.WiFi).mode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
203         OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "WiFi Mode : %d", (gESDeviceProperty.WiFi).mode[modeIdx]);
204         modeIdx ++;
205     }
206     (gESDeviceProperty.WiFi).freq = (deviceProperty->WiFi).freq;
207     OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "WiFi Freq : %d", (gESDeviceProperty.WiFi).freq);
208
209     OICStrcpy((gESDeviceProperty.DevConf).deviceName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
210     OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Device Name : %s", (gESDeviceProperty.DevConf).deviceName);
211
212
213     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetDeviceProperty OUT");
214     return ES_OK;
215 }
216
217 ESResult ESTerminateEnrollee()
218 {
219     UnRegisterResourceEventCallBack();
220
221     //Delete Prov resource
222     if (DeleteEasySetupResources() != OC_STACK_OK)
223     {
224         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
225         return ES_ERROR;
226     }
227
228     OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");
229     return ES_OK;
230 }