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