Easysetup - Change OC_LOG to OIC_LOG & warning fixes
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / src / resourcehandler.cpp
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 #include "resourcehandler.h"
22 #include "ocpayload.h"
23
24
25 /**
26  * @var ES_RH_TAG
27  * @brief Logging tag for module name.
28  */
29 #define ES_RH_TAG "ES_RH"
30 //-----------------------------------------------------------------------------
31 // Private variables
32 //-----------------------------------------------------------------------------
33
34 /**
35  * @var gProvResource
36  * @brief Structure for holding the Provisioning status and target information required to connect to the target network
37  */
38 static ProvResource gProvResource;
39
40 /**
41  * @var gNetResource
42  * @brief Structure forr holding the Provisioning status of network information
43  */
44 static NetResource gNetResource;
45
46 //-----------------------------------------------------------------------------
47 // Private internal function prototypes
48 //-----------------------------------------------------------------------------
49 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
50                                                 OCEntityHandlerRequest *ehRequest, void *callback);
51 const char *getResult(OCStackResult result);
52 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
53                                                OCRepPayload** payload);
54 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
55                                                OCRepPayload** payload);
56 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
57                                                 OCRepPayload** payload);
58 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest);
59
60 static int g_flag = 0;
61
62 ResourceEventCallback gNetworkInfoProvEventCb = NULL;
63
64 void RegisterResourceEventCallBack(ResourceEventCallback cb)
65 {
66     gNetworkInfoProvEventCb = cb;
67 }
68
69 void UnRegisterResourceEventCallBack()
70 {
71     if (gNetworkInfoProvEventCb)
72     {
73         gNetworkInfoProvEventCb = NULL;
74     }
75 }
76
77 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
78 {
79     if (name != NULL && pass != NULL)
80     {
81         sprintf(name, "%s", gProvResource.tnn);
82         sprintf(pass, "%s", gProvResource.cd);
83     }
84 }
85
86 OCStackResult CreateProvisioningResource(bool isSecured)
87 {
88     gProvResource.ps = ES_PS_NEED_PROVISIONING;
89
90     gProvResource.tnt = CT_ADAPTER_IP;
91     sprintf(gProvResource.tnn, "Unknown");
92     sprintf(gProvResource.cd, "Unknown");
93
94     OCStackResult res = OC_STACK_ERROR;
95     if (isSecured)
96     {
97         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
98                 OC_RSRVD_INTERFACE_DEFAULT,
99                 OC_RSRVD_ES_URI_PROV,
100                 OCEntityHandlerCb,
101                 NULL,
102                 OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
103     }
104     else
105     {
106         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
107                 OC_RSRVD_INTERFACE_DEFAULT,
108                 OC_RSRVD_ES_URI_PROV,
109                 OCEntityHandlerCb,
110                 NULL,
111                 OC_DISCOVERABLE | OC_OBSERVABLE);
112     }
113
114     OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
115     return res;
116 }
117
118 OCStackResult DeleteProvisioningResource()
119 {
120     OCStackResult res = OCDeleteResource(gProvResource.handle);
121     if (res != OC_STACK_OK)
122     {
123         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
124     }
125
126     return res;
127 }
128
129 #ifdef ESWIFI
130 OCStackResult CreateNetworkResource()
131 {
132     NetworkInfo netInfo;
133
134     if (getCurrentNetworkInfo(CT_ADAPTER_IP, &netInfo) != ES_OK)
135     {
136         return OC_STACK_ERROR;
137     }
138
139     if (netInfo.type != CT_ADAPTER_IP)
140     {
141         return OC_STACK_ERROR;
142     }
143
144     gNetResource.cnt = (int) netInfo.type;
145     gNetResource.ant[0] = (int) CT_ADAPTER_IP;
146
147     if(netInfo.ipaddr != NULL)
148     {
149         sprintf(gNetResource.ipaddr, "%d.%d.%d.%d", netInfo.ipaddr[0], netInfo.ipaddr[1],
150                                             netInfo.ipaddr[2], netInfo.ipaddr[3]);
151     }
152     sprintf(gNetResource.cnn, "%s", netInfo.ssid);
153
154     OIC_LOG_V(INFO, ES_RH_TAG, "SSID: %s", gNetResource.cnn);
155     OIC_LOG_V(INFO, ES_RH_TAG, "IP Address: %s", gNetResource.ipaddr);
156
157     OCStackResult res = OCCreateResource(&gNetResource.handle, "oic.r.net", OC_RSRVD_INTERFACE_DEFAULT,
158             OC_RSRVD_ES_URI_NET, OCEntityHandlerCb,NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
159
160     OIC_LOG_V(INFO, ES_RH_TAG, "Created Net resource with result: %s", getResult(res));
161
162     return res;
163 }
164
165 OCStackResult DeleteNetworkResource()
166 {
167     OCStackResult res = OCDeleteResource(gNetResource.handle);
168     if (res != OC_STACK_OK)
169     {
170         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Network resource error with result: %s",
171                                                                             getResult(res));
172     }
173
174     return res;
175 }
176 #endif
177
178 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
179                                                 OCRepPayload **payload)
180 {
181     OCEntityHandlerResult ehResult = OC_EH_ERROR;
182     if (!ehRequest)
183     {
184         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
185         return ehResult;
186     }
187     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
188     {
189         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
190         return ehResult;
191     }
192
193     OCRepPayload *getResp = constructResponse(ehRequest);
194     if (!getResp)
195     {
196         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
197         return OC_EH_ERROR;
198     }
199
200     *payload = getResp;
201     ehResult = OC_EH_OK;
202
203     return ehResult;
204 }
205
206 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
207                                                OCRepPayload** payload)
208 {
209     OIC_LOG(INFO, ES_RH_TAG, "ProcessPutRequest enter");
210     OCEntityHandlerResult ehResult = OC_EH_ERROR;
211     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
212     {
213         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
214         return ehResult;
215     }
216
217     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
218     if (!input)
219     {
220         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
221         return ehResult;
222     }
223
224     //TODO : ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
225     // A new request for provisioning means overriding existing network provisioning information.
226     // Metadata to indicate that it is override is needed. The metadata can be a new attribute
227     // should be added to the /oic/prov resource indicating to override the existing network
228     // information.
229     if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED)
230     {
231         OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed. "
232                 "This a request to override the existing the network provisioning information");
233     }
234
235     // PUT request is appropriate for provisioning information to the enrollee.
236     // When an enrollee receives the put request, the entire resource information should
237     // be overwritten.
238     sprintf(gProvResource.tnn, "%s", "");
239     sprintf(gProvResource.tnn, "%s", "");
240
241     char* tnn;
242     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
243     {
244         sprintf(gProvResource.tnn, "%s", tnn);
245         OIC_LOG(INFO, ES_RH_TAG, "got ssid");
246     }
247
248         OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.tnn %s", gProvResource.tnn);
249     char* cd;
250     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
251     {
252         sprintf(gProvResource.cd, "%s", cd);
253         OIC_LOG(INFO, ES_RH_TAG, "got password");
254     }
255         OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.cd %s", gProvResource.cd);
256     gProvResource.ps = 2;
257     OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.ps %d", gProvResource.ps);
258     g_flag = 1;
259
260     OCRepPayload *getResp = constructResponse(ehRequest);
261     if (!getResp)
262     {
263         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
264         return OC_EH_ERROR;
265     }
266
267     *payload = getResp;
268     ehResult = OC_EH_OK;
269
270     return ehResult;
271 }
272
273 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
274                                                 OCRepPayload** /*payload*/)
275 {
276     OCEntityHandlerResult ehResult = OC_EH_ERROR;
277     if (!ehRequest)
278     {
279         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
280         return ehResult;
281     }
282     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
283     {
284         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
285         return ehResult;
286     }
287
288     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
289     if (!input)
290     {
291         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
292         return ehResult;
293     }
294     char* tr;
295     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
296     {
297         // Triggering
298         ehResult = OC_EH_OK;
299     }
300     g_flag = 1;
301
302     return ehResult;
303 }
304
305 OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest)
306 {
307     OCRepPayload* payload = OCRepPayloadCreate();
308     if (!payload)
309     {
310         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
311         return NULL;
312     }
313
314     if (ehRequest->resource == gProvResource.handle)
315     {
316         OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
317         OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
318         OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS, gProvResource.ps);
319         OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, gProvResource.tnt);
320         OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, gProvResource.tnn);
321         OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, gProvResource.cd);
322     }
323     else if (ehRequest->requestHandle == gNetResource.handle)
324     {
325
326         OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET);
327         OCRepPayloadSetPropInt(payload, "ant", gNetResource.ant[0]);
328     }
329     return payload;
330 }
331
332 /**
333  * This is the entity handler for the registered resource.
334  * This is invoked by OCStack whenever it recevies a request for this resource.
335  */
336 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
337         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
338 {
339     (void) callback;
340     OCEntityHandlerResult ehRet = OC_EH_OK;
341     OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, { },{ 0 }, false };
342     OCRepPayload* payload = NULL;
343     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
344     {
345         if (OC_REST_GET == entityHandlerRequest->method)
346         {
347             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
348             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
349         }
350         else if (OC_REST_PUT == entityHandlerRequest->method)
351         {
352             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
353
354             if (gProvResource.handle != NULL && entityHandlerRequest->resource == gProvResource.handle)
355             {
356                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
357             }
358             else
359             {
360                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
361                 ehRet = OC_EH_ERROR;
362             }
363         }
364         else if (OC_REST_POST == entityHandlerRequest->method)
365         {
366             // TODO: As of now, POST request will be not received.
367             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
368             //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
369         }
370
371         if (ehRet == OC_EH_OK)
372         {
373             // Format the response.  Note this requires some info about the request
374             response.requestHandle = entityHandlerRequest->requestHandle;
375             response.resourceHandle = entityHandlerRequest->resource;
376             response.ehResult = ehRet;
377             //response uses OCPaylod while all get,put methodes use OCRepPayload
378             response.payload = (OCPayload*) (payload);
379             response.numSendVendorSpecificHeaderOptions = 0;
380             memset(response.sendVendorSpecificHeaderOptions, 0,
381                     sizeof response.sendVendorSpecificHeaderOptions);
382             memset(response.resourceUri, 0, sizeof response.resourceUri);
383             // Indicate that response is NOT in a persistent buffer
384             response.persistentBufferFlag = 0;
385
386             // Send the response
387             if (OCDoResponse(&response) != OC_STACK_OK)
388             {
389                 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
390                 ehRet = OC_EH_ERROR;
391             }
392         }
393     }
394
395     if (g_flag == 1)
396     {
397         gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
398         g_flag = 0;
399     }
400
401     return ehRet;
402 }
403
404 const char *getResult(OCStackResult result)
405 {
406     switch (result)
407     {
408         case OC_STACK_OK:
409             return "OC_STACK_OK";
410         case OC_STACK_INVALID_URI:
411             return "OC_STACK_INVALID_URI";
412         case OC_STACK_INVALID_QUERY:
413             return "OC_STACK_INVALID_QUERY";
414         case OC_STACK_INVALID_IP:
415             return "OC_STACK_INVALID_IP";
416         case OC_STACK_INVALID_PORT:
417             return "OC_STACK_INVALID_PORT";
418         case OC_STACK_INVALID_CALLBACK:
419             return "OC_STACK_INVALID_CALLBACK";
420         case OC_STACK_INVALID_METHOD:
421             return "OC_STACK_INVALID_METHOD";
422         case OC_STACK_NO_MEMORY:
423             return "OC_STACK_NO_MEMORY";
424         case OC_STACK_COMM_ERROR:
425             return "OC_STACK_COMM_ERROR";
426         case OC_STACK_INVALID_PARAM:
427             return "OC_STACK_INVALID_PARAM";
428         case OC_STACK_NOTIMPL:
429             return "OC_STACK_NOTIMPL";
430         case OC_STACK_NO_RESOURCE:
431             return "OC_STACK_NO_RESOURCE";
432         case OC_STACK_RESOURCE_ERROR:
433             return "OC_STACK_RESOURCE_ERROR";
434         case OC_STACK_SLOW_RESOURCE:
435             return "OC_STACK_SLOW_RESOURCE";
436         case OC_STACK_NO_OBSERVERS:
437             return "OC_STACK_NO_OBSERVERS";
438         case OC_STACK_ERROR:
439             return "OC_STACK_ERROR";
440         default:
441             return "UNKNOWN";
442     }
443 }
444