Update resource handler for POST request with batch interface
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / src / resourcehandler.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 #include "resourcehandler.h"
22
23 #include <stdio.h>
24
25 #include "ocpayload.h"
26 #include "oic_string.h"
27
28 /**
29  * @var ES_RH_TAG
30  * @brief Logging tag for module name.
31  */
32 #define ES_RH_TAG "ES_RH"
33 //-----------------------------------------------------------------------------
34 // Private variables
35 //-----------------------------------------------------------------------------
36
37 /**
38  * @var gProvResource
39  * @brief Structure for holding the Provisioning status and target information required to
40  * connect to the target network
41  */
42 static ProvResource gProvResource;
43 static WiFiResource gWiFiResource;
44 static CloudResource gCloudResource;
45 static DevConfResource gDevConfResource;
46
47 //-----------------------------------------------------------------------------
48 // Private internal function prototypes
49 //-----------------------------------------------------------------------------
50 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
51         void *callback);
52 const char *getResult(OCStackResult result);
53 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
54 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
55 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
56 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
57 void updateWiFiResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
58 void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
59 void updateDevConfResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
60
61 ESEnrolleeResourceEventCallback gNetworkInfoProvEventCb = NULL;
62
63 void RegisterResourceEventCallBack(ESEnrolleeResourceEventCallback cb)
64 {
65     gNetworkInfoProvEventCb = cb;
66 }
67
68 void UnRegisterResourceEventCallBack()
69 {
70     if (gNetworkInfoProvEventCb)
71     {
72         gNetworkInfoProvEventCb = NULL;
73     }
74 }
75
76 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
77 {
78     if (name != NULL && pass != NULL)
79     {
80         OICStrcpy(name, MAXSSIDLEN, gWiFiResource.ssid);
81         OICStrcpy(pass, MAXNETCREDLEN, gWiFiResource.cred);
82     }
83 }
84
85 OCStackResult initProvResource(bool isSecured)
86 {
87     gProvResource.status = NO_PROVISION;
88     gProvResource.trigger = false;
89
90     OCStackResult res = OC_STACK_ERROR;
91     if (isSecured)
92     {
93         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
94         OC_RSRVD_INTERFACE_DEFAULT,
95         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
96         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
97     }else
98     {
99         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
100         OC_RSRVD_INTERFACE_DEFAULT,
101         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
102         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
103     }
104     if(res)
105     {
106         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
107         return res;
108     }
109
110     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
111     if(res)
112     {
113         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
114         return res;
115     }
116     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
117     if(res)
118     {
119         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
120         return res;
121     }
122
123     OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
124     return res;
125 }
126
127 OCStackResult initWiFiResource(bool isSecured)
128 {
129     OCStackResult res = OC_STACK_ERROR;
130
131     gWiFiResource.supportedFreq = WiFi_BOTH;
132     gWiFiResource.supportedMode[0] = WiFi_11A;
133     gWiFiResource.supportedMode[1] = WiFi_11B;
134     gWiFiResource.supportedMode[2] = WiFi_11G;
135     gWiFiResource.supportedMode[3] = WiFi_11N;
136     gWiFiResource.numMode = 4;
137     gWiFiResource.authType = NONE_AUTH;
138     gWiFiResource.encType = NONE_ENC;
139     OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
140     OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
141
142     if (isSecured)
143     {
144         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
145         OC_RSRVD_INTERFACE_DEFAULT,
146         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
147         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
148     }else
149     {
150         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
151         OC_RSRVD_INTERFACE_DEFAULT,
152         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
153         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
154     }
155
156     OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
157     return res;
158
159 }
160
161 OCStackResult initCloudServerResource(bool isSecured)
162 {
163     OCStackResult res = OC_STACK_ERROR;
164
165     OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
166     OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
167     OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
168
169     if (isSecured)
170     {
171         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
172         OC_RSRVD_INTERFACE_DEFAULT,
173         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
174         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
175     }else
176     {
177         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
178         OC_RSRVD_INTERFACE_DEFAULT,
179         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
180         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
181     }
182
183     OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
184     return res;
185
186 }
187
188 OCStackResult initDevConfResource(bool isSecured)
189 {
190     OCStackResult res = OC_STACK_ERROR;
191
192     OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
193     OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
194     OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
195
196     if (isSecured)
197     {
198         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
199         OC_RSRVD_INTERFACE_DEFAULT,
200         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
201         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
202     }else
203     {
204         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
205         OC_RSRVD_INTERFACE_DEFAULT,
206         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
207         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
208     }
209
210     OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
211     return res;
212
213 }
214
215 void updateProvResource(OCEntityHandlerRequest *ehRequest, OCRepPayload* input)
216 {
217     OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %lld", gProvResource.status);
218     bool trigger;
219     if (OCRepPayloadGetPropBool(input, OC_RSRVD_ES_TRIGGER, &trigger))
220     {
221         // Triggering
222         gProvResource.trigger = trigger;
223     }
224
225     if(ehRequest->query)
226     {
227         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
228         {// When Provisioning resource has a POST with BatchInterface
229             updateCloudResource(ehRequest, input);
230             updateWiFiResource(ehRequest, input);
231             updateDevConfResource(ehRequest, input);
232         }
233     }
234 }
235
236 void updateWiFiResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
237 {
238     (void) ehRequest;
239     char* ssid;
240     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
241     {
242         OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
243         OIC_LOG(INFO, ES_RH_TAG, "got ssid");
244     }
245
246     char* cred;
247     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
248     {
249         OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
250         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
251     }
252
253     int64_t authType;
254     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
255     {
256         gWiFiResource.authType = authType;
257         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
258     }
259
260     int64_t encType;
261     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
262     {
263         gWiFiResource.encType = encType;
264         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
265     }
266 }
267 void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
268 {
269     (void) ehRequest;
270     char *authCode;
271     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
272     {
273         OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
274         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
275     }
276
277     char *authProvider;
278     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
279     {
280         OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
281         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
282     }
283
284     char *ciServer;
285     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
286     {
287         OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
288         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
289     }
290 }
291
292 void updateDevConfResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
293 {
294     (void) ehRequest;
295     char *country;
296     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &country))
297     {
298         OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
299         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
300     }
301
302     char *language;
303     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &language))
304     {
305         OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
306         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
307     }
308 }
309
310 OCRepPayload* constructResponseOfWiFi(OCEntityHandlerRequest *ehRequest)
311 {
312     (void) ehRequest;
313     OCRepPayload* payload = OCRepPayloadCreate();
314     if (!payload)
315     {
316         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
317         return NULL;
318     }
319
320     OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
321     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
322
323     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
324     OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, gWiFiResource.supportedMode, dimensions);
325
326     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
327     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
328     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
329     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, gWiFiResource.authType);
330     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, gWiFiResource.encType);
331
332     printf("%s\n", gWiFiResource.ssid);
333
334     return payload;
335 }
336
337 OCRepPayload* constructResponseOfCloud(OCEntityHandlerRequest *ehRequest)
338 {
339     (void) ehRequest;
340     OCRepPayload* payload = OCRepPayloadCreate();
341     if (!payload)
342     {
343         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
344         return NULL;
345     }
346
347     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
348     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
349     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
350     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
351     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
352
353     return payload;
354 }
355
356 OCRepPayload* constructResponseOfDevConf(OCEntityHandlerRequest *ehRequest)
357 {
358     (void) ehRequest;
359     OCRepPayload* payload = OCRepPayloadCreate();
360     if (!payload)
361     {
362         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
363         return NULL;
364     }
365
366     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
367     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
368     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
369     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
370     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
371
372     return payload;
373 }
374
375 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
376 {
377     OCRepPayload* payload = OCRepPayloadCreate();
378     if (!payload)
379     {
380         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
381         return NULL;
382     }
383
384     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
385     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
386     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
387     OCRepPayloadSetPropBool(payload, OC_RSRVD_ES_TRIGGER, gProvResource.trigger);
388
389     if(ehRequest->query)
390     {
391         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
392         {// When Provisioning resource has a GET with BatchInterface
393             payload->next = constructResponseOfWiFi(ehRequest);
394
395             if(payload->next)
396                 payload->next->next = constructResponseOfCloud(ehRequest);
397             else
398                 return payload;
399
400             if(payload->next->next)
401                 payload->next->next->next = constructResponseOfDevConf(ehRequest);
402             else
403                 return payload;
404         }
405     }
406
407     return payload;
408 }
409
410
411 OCStackResult CreateEasySetupResources(bool isSecured)
412 {
413     OCStackResult res = OC_STACK_ERROR;
414
415     res = initProvResource(isSecured);
416     if(res)
417     {
418         // TODO: destroy logic will be added
419         return res;
420     }
421
422     res = initWiFiResource(isSecured);
423     if(res)
424     {
425         // TODO: destroy logic will be added
426         return res;
427     }
428
429     res = initCloudServerResource(isSecured);
430     if(res)
431     {
432         // TODO: destroy logic will be added
433         return res;
434     }
435
436     res = initDevConfResource(isSecured);
437     if(res)
438     {
439         // TODO: destroy logic will be added
440         return res;
441     }
442
443     OCBindResource(gProvResource.handle, gWiFiResource.handle);
444     OCBindResource(gProvResource.handle, gCloudResource.handle);
445     OCBindResource(gProvResource.handle, gDevConfResource.handle);
446
447     OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
448     return res;
449 }
450
451 OCStackResult DeleteProvisioningResource()
452 {
453     OCStackResult res = OCDeleteResource(gProvResource.handle);
454     if (res != OC_STACK_OK)
455     {
456         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
457     }
458
459     return res;
460 }
461
462 OCStackResult DeleteEasySetupResources()
463 {
464     OCStackResult res = OCDeleteResource(gProvResource.handle);
465     if (res != OC_STACK_OK)
466     {
467         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
468     }
469     res = OCDeleteResource(gWiFiResource.handle);
470     if (res != OC_STACK_OK)
471     {
472         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
473     }
474     res = OCDeleteResource(gCloudResource.handle);
475     if (res != OC_STACK_OK)
476     {
477         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
478     }
479     res = OCDeleteResource(gDevConfResource.handle);
480     if (res != OC_STACK_OK)
481     {
482         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
483     }
484
485     return res;
486 }
487
488 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
489 {
490     OCEntityHandlerResult ehResult = OC_EH_ERROR;
491     if (!ehRequest)
492     {
493         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
494         return ehResult;
495     }
496     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
497     {
498         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
499         return ehResult;
500     }
501
502     OCRepPayload *getResp = NULL;
503
504     if(ehRequest->resource == gProvResource.handle)
505         getResp = constructResponseOfProv(ehRequest);
506     else if(ehRequest->resource == gWiFiResource.handle)
507         getResp = constructResponseOfWiFi(ehRequest);
508     else if(ehRequest->resource == gCloudResource.handle)
509         getResp = constructResponseOfCloud(ehRequest);
510     else if(ehRequest->resource == gDevConfResource.handle)
511         getResp = constructResponseOfDevConf(ehRequest);
512
513     if (!getResp)
514     {
515         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
516         return OC_EH_ERROR;
517     }
518
519     *payload = getResp;
520     ehResult = OC_EH_OK;
521
522     return ehResult;
523 }
524
525 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
526 {
527     OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
528     OCEntityHandlerResult ehResult = OC_EH_ERROR;
529     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
530     {
531         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
532         return ehResult;
533     }
534
535     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
536     if (!input)
537     {
538         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
539         return ehResult;
540     }
541
542     if(ehRequest->resource == gProvResource.handle)
543         updateProvResource(ehRequest, input);
544     else if(ehRequest->resource == gWiFiResource.handle)
545         updateWiFiResource(ehRequest, input);
546     else if(ehRequest->resource == gCloudResource.handle)
547         updateCloudResource(ehRequest, input);
548     else if(ehRequest->resource == gDevConfResource.handle)
549         updateDevConfResource(ehRequest, input);
550
551     // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
552     // A new request for provisioning means overriding existing network provisioning information.
553     if (gProvResource.trigger)
554     {
555         OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed."
556                 "Tiggering the network connection");
557
558         if (gNetworkInfoProvEventCb)
559         {
560             gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
561             ehResult = OC_EH_OK;
562         }
563         else
564         {
565             OIC_LOG(ERROR, ES_RH_TAG, "gNetworkInfoProvEventCb is NULL."
566                     "Network handler not registered. Failed to connect to the network");
567             ehResult = OC_EH_ERROR;
568             return ehResult;
569         }
570     }
571     else
572     {
573         OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning the network information to the Enrollee.");
574     }
575
576     OCRepPayload *getResp = NULL;
577     if(ehRequest->resource == gProvResource.handle)
578         getResp = constructResponseOfProv(ehRequest);
579     else if(ehRequest->resource == gWiFiResource.handle)
580         getResp = constructResponseOfWiFi(ehRequest);
581     else if(ehRequest->resource == gCloudResource.handle)
582         getResp = constructResponseOfCloud(ehRequest);
583     else if(ehRequest->resource == gDevConfResource.handle)
584         getResp = constructResponseOfDevConf(ehRequest);
585
586     if (gProvResource.trigger)// Trigger false should be restored after executed
587         gProvResource.trigger = false;
588
589     if (!getResp)
590     {
591         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
592         return OC_EH_ERROR;
593     }
594
595     *payload = getResp;
596     ehResult = OC_EH_OK;
597
598     return ehResult;
599 }
600
601 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
602         OCRepPayload** payload)
603 {
604     (void) ehRequest;
605     (void) payload;
606     OCEntityHandlerResult ehResult = OC_EH_ERROR;
607
608     return ehResult;
609 }
610 /**
611  * This is the entity handler for the registered resource.
612  * This is invoked by OCStack whenever it recevies a request for this resource.
613  */
614 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
615         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
616 {
617     (void) callback;
618     OCEntityHandlerResult ehRet = OC_EH_OK;
619     OCEntityHandlerResponse response =
620     { 0, 0, OC_EH_ERROR, 0, 0,
621     { },
622     { 0 }, false };
623     OCRepPayload* payload = NULL;
624
625     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
626     {
627         if (OC_REST_GET == entityHandlerRequest->method)
628         {
629             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
630             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
631         }
632         else if (OC_REST_PUT == entityHandlerRequest->method)
633         {
634             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
635
636             //PUT request will be handled in the internal implementation
637             if (gProvResource.handle != NULL)
638             {
639                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
640             }
641             else
642             {
643                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
644                 ehRet = OC_EH_ERROR;
645             }
646         }
647         else if (OC_REST_POST == entityHandlerRequest->method)
648         {
649             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
650             if (gProvResource.handle != NULL)
651             {
652                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
653             }
654             else
655             {
656                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
657                 ehRet = OC_EH_ERROR;
658             }
659         }
660
661         if (ehRet == OC_EH_OK)
662         {
663             // Format the response.  Note this requires some info about the request
664             response.requestHandle = entityHandlerRequest->requestHandle;
665             response.resourceHandle = entityHandlerRequest->resource;
666             response.ehResult = ehRet;
667             //response uses OCPaylod while all get,put methodes use OCRepPayload
668             response.payload = (OCPayload*) (payload);
669             response.numSendVendorSpecificHeaderOptions = 0;
670             memset(response.sendVendorSpecificHeaderOptions, 0,
671                     sizeof(response.sendVendorSpecificHeaderOptions));
672             memset(response.resourceUri, 0, sizeof(response.resourceUri));
673             // Indicate that response is NOT in a persistent buffer
674             response.persistentBufferFlag = 0;
675
676             // Send the response
677             if (OCDoResponse(&response) != OC_STACK_OK)
678             {
679                 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
680                 ehRet = OC_EH_ERROR;
681             }
682         }
683     }
684
685     return ehRet;
686 }
687
688 const char *getResult(OCStackResult result)
689 {
690     switch (result)
691     {
692         case OC_STACK_OK:
693             return "OC_STACK_OK";
694         case OC_STACK_INVALID_URI:
695             return "OC_STACK_INVALID_URI";
696         case OC_STACK_INVALID_QUERY:
697             return "OC_STACK_INVALID_QUERY";
698         case OC_STACK_INVALID_IP:
699             return "OC_STACK_INVALID_IP";
700         case OC_STACK_INVALID_PORT:
701             return "OC_STACK_INVALID_PORT";
702         case OC_STACK_INVALID_CALLBACK:
703             return "OC_STACK_INVALID_CALLBACK";
704         case OC_STACK_INVALID_METHOD:
705             return "OC_STACK_INVALID_METHOD";
706         case OC_STACK_NO_MEMORY:
707             return "OC_STACK_NO_MEMORY";
708         case OC_STACK_COMM_ERROR:
709             return "OC_STACK_COMM_ERROR";
710         case OC_STACK_INVALID_PARAM:
711             return "OC_STACK_INVALID_PARAM";
712         case OC_STACK_NOTIMPL:
713             return "OC_STACK_NOTIMPL";
714         case OC_STACK_NO_RESOURCE:
715             return "OC_STACK_NO_RESOURCE";
716         case OC_STACK_RESOURCE_ERROR:
717             return "OC_STACK_RESOURCE_ERROR";
718         case OC_STACK_SLOW_RESOURCE:
719             return "OC_STACK_SLOW_RESOURCE";
720         case OC_STACK_NO_OBSERVERS:
721             return "OC_STACK_NO_OBSERVERS";
722         case OC_STACK_ERROR:
723             return "OC_STACK_ERROR";
724         default:
725             return "UNKNOWN";
726     }
727 }
728