Merge branch 'cloud-interface'
[platform/upstream/iotivity.git] / resource / csdk / security / src / pstatresource.c
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH 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 <stdlib.h>
22 #include <string.h>
23
24 #include "ocstack.h"
25 #include "oic_malloc.h"
26 #include "ocpayload.h"
27 #include "payload_logging.h"
28 #include "resourcemanager.h"
29 #include "pstatresource.h"
30 #include "doxmresource.h"
31 #include "psinterface.h"
32 #include "srmresourcestrings.h"
33 #include "srmutility.h"
34
35 #define TAG  "SRM-PSTAT"
36
37 /** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
38  * The value of payload size is increased until reaching below max cbor size. */
39 static const uint16_t CBOR_SIZE = 512;
40
41 // Max cbor size payload.
42 static const uint16_t CBOR_MAX_SIZE = 4400;
43
44 // PSTAT Map size - Number of mandatory items
45 static const uint8_t PSTAT_MAP_SIZE = 9;
46
47 static OicSecDpom_t gSm = SINGLE_SERVICE_CLIENT_DRIVEN;
48 static OicSecPstat_t gDefaultPstat =
49 {
50     false,                                    // bool isop
51     (OicSecDpm_t)(BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
52     PROVISION_CREDENTIALS | PROVISION_ACLS),   // OicSecDpm_t cm
53     (OicSecDpm_t)(TAKE_OWNER | BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
54     PROVISION_CREDENTIALS | PROVISION_ACLS),   // OicSecDpm_t tm
55     {.id = {0}},                              // OicUuid_t deviceID
56     SINGLE_SERVICE_CLIENT_DRIVEN,             // OicSecDpom_t om */
57     1,                                        // the number of elts in Sms
58     &gSm,                                     // OicSecDpom_t *sm
59     0,                                        // uint16_t commitHash
60     {.id = {0}},                              // OicUuid_t rownerID
61 };
62
63 static OicSecPstat_t    *gPstat = NULL;
64
65 static OCResourceHandle gPstatHandle = NULL;
66
67 void DeletePstatBinData(OicSecPstat_t* pstat)
68 {
69     if (pstat)
70     {
71         //Clean 'supported modes' field
72         OICFree(pstat->sm);
73
74         //Clean pstat itself
75         OICFree(pstat);
76     }
77 }
78
79 OCStackResult PstatToCBORPayload(const OicSecPstat_t *pstat, uint8_t **payload, size_t *size)
80 {
81     if (NULL == pstat || NULL == payload || NULL != *payload || NULL == size)
82     {
83         return OC_STACK_INVALID_PARAM;
84     }
85
86     size_t cborLen = *size;
87     if (0 == cborLen)
88     {
89         cborLen = CBOR_SIZE;
90     }
91
92     *payload = NULL;
93     *size = 0;
94
95     OCStackResult ret = OC_STACK_ERROR;
96
97     CborEncoder encoder;
98     CborEncoder pstatMap;
99     char* strUuid = NULL;
100
101     int64_t cborEncoderResult = CborNoError;
102
103     uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
104     VERIFY_NON_NULL(TAG, outPayload, ERROR);
105     cbor_encoder_init(&encoder, outPayload, cborLen, 0);
106
107     cborEncoderResult = cbor_encoder_create_map(&encoder, &pstatMap, PSTAT_MAP_SIZE);
108     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pstat Map.");
109
110     cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ISOP_NAME,
111         strlen(OIC_JSON_ISOP_NAME));
112     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ISOP Name Tag.");
113     cborEncoderResult = cbor_encode_boolean(&pstatMap, pstat->isOp);
114     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ISOP Name Value.");
115
116     cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_DEVICE_ID_NAME,
117         strlen(OIC_JSON_DEVICE_ID_NAME));
118     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Tag.");
119     ret = ConvertUuidToStr(&pstat->deviceID, &strUuid);
120     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
121     cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid));
122     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Value.");
123     OICFree(strUuid);
124     strUuid = NULL;
125
126     cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_CM_NAME,
127         strlen(OIC_JSON_CM_NAME));
128     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CM Name Tag.");
129     cborEncoderResult = cbor_encode_int(&pstatMap, pstat->cm);
130     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CM Name Value.");
131
132     cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_TM_NAME,
133         strlen(OIC_JSON_TM_NAME));
134     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding TM Name Tag.");
135     cborEncoderResult = cbor_encode_int(&pstatMap, pstat->tm);
136     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding TM Name Value.");
137
138     cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_OM_NAME,
139         strlen(OIC_JSON_OM_NAME));
140     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding OM Name Tag.");
141     cborEncoderResult = cbor_encode_int(&pstatMap, pstat->om);
142     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding OM Name Value.");
143
144     cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_SM_NAME,
145         strlen(OIC_JSON_SM_NAME));
146     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Tag.");
147     cborEncoderResult = cbor_encode_int(&pstatMap, pstat->sm[0]);
148     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Value.");
149
150     cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ROWNERID_NAME,
151         strlen(OIC_JSON_ROWNERID_NAME));
152     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Tag.");
153     ret = ConvertUuidToStr(&pstat->rownerID, &strUuid);
154     VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
155     cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid));
156     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Value.");
157     OICFree(strUuid);
158     strUuid = NULL;
159
160     //RT -- Mandatory
161     CborEncoder rtArray;
162     cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_RT_NAME,
163             strlen(OIC_JSON_RT_NAME));
164     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Name Tag.");
165     cborEncoderResult = cbor_encoder_create_array(&pstatMap, &rtArray, 1);
166     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Value.");
167     for (size_t i = 0; i < 1; i++)
168     {
169         cborEncoderResult = cbor_encode_text_string(&rtArray, OIC_RSRC_TYPE_SEC_PSTAT,
170                 strlen(OIC_RSRC_TYPE_SEC_PSTAT));
171         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding RT Value.");
172     }
173     cborEncoderResult = cbor_encoder_close_container(&pstatMap, &rtArray);
174     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing RT.");
175
176     //IF-- Mandatory
177      CborEncoder ifArray;
178      cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_IF_NAME,
179              strlen(OIC_JSON_IF_NAME));
180      VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Name Tag.");
181      cborEncoderResult = cbor_encoder_create_array(&pstatMap, &ifArray, 1);
182      VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Value.");
183     for (size_t i = 0; i < 1; i++)
184     {
185         cborEncoderResult = cbor_encode_text_string(&ifArray, OC_RSRVD_INTERFACE_DEFAULT,
186                 strlen(OC_RSRVD_INTERFACE_DEFAULT));
187         VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding IF Value.");
188     }
189     cborEncoderResult = cbor_encoder_close_container(&pstatMap, &ifArray);
190     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing IF.");
191
192     cborEncoderResult = cbor_encoder_close_container(&encoder, &pstatMap);
193     VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Closing PSTAT Map.");
194
195     if (CborNoError == cborEncoderResult)
196     {
197         *size = encoder.ptr - outPayload;
198         *payload = outPayload;
199         ret = OC_STACK_OK;
200     }
201 exit:
202     if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
203     {
204         // reallocate and try again!
205         OICFree(outPayload);
206         // Since the allocated initial memory failed, double the memory.
207         cborLen += encoder.ptr - encoder.end;
208         cborEncoderResult = CborNoError;
209         ret = PstatToCBORPayload(pstat, payload, &cborLen);
210         if (OC_STACK_OK == ret)
211         {
212             *size = cborLen;
213         }
214     }
215
216     if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
217     {
218         OICFree(outPayload);
219         outPayload = NULL;
220         *payload = NULL;
221         *size = 0;
222         ret = OC_STACK_ERROR;
223     }
224
225     return ret;
226 }
227
228 OCStackResult CBORPayloadToPstat(const uint8_t *cborPayload, const size_t size,
229                                  OicSecPstat_t **secPstat)
230 {
231     if (NULL == cborPayload || NULL == secPstat || NULL != *secPstat || 0 == size)
232     {
233         return OC_STACK_INVALID_PARAM;
234     }
235
236     OCStackResult ret = OC_STACK_ERROR;
237     *secPstat = NULL;
238
239     CborValue pstatCbor;
240     CborParser parser;
241     CborError cborFindResult = CborNoError;
242     char *strUuid = NULL;
243     size_t len = 0;
244
245     cbor_parser_init(cborPayload, size, 0, &parser, &pstatCbor);
246     CborValue pstatMap = { .parser = NULL };
247
248     OicSecPstat_t *pstat = NULL;
249     cborFindResult = cbor_value_enter_container(&pstatCbor, &pstatMap);
250     VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Map.");
251
252     pstat = (OicSecPstat_t *)OICCalloc(1, sizeof(OicSecPstat_t));
253     VERIFY_NON_NULL(TAG, pstat, ERROR);
254
255     cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ISOP_NAME, &pstatMap);
256     if (CborNoError == cborFindResult && cbor_value_is_boolean(&pstatMap))
257     {
258         cborFindResult = cbor_value_get_boolean(&pstatMap, &pstat->isOp);
259         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding isOp Value.");
260     }
261
262     cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_DEVICE_ID_NAME, &pstatMap);
263     if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
264     {
265         cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
266         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Device Id Value.");
267         ret = ConvertStrToUuid(strUuid , &pstat->deviceID);
268         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
269         OICFree(strUuid );
270         strUuid  = NULL;
271     }
272
273     cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_CM_NAME, &pstatMap);
274     if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
275     {
276         cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->cm);
277         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CM.");
278     }
279
280     cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_TM_NAME, &pstatMap);
281     if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
282     {
283         cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->tm);
284         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding TM.");
285     }
286
287     cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_OM_NAME, &pstatMap);
288     if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
289     {
290         cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->om);
291         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding OM.");
292     }
293
294     cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_SM_NAME, &pstatMap);
295     if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
296     {
297         pstat->smLen = 1;
298         pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
299         cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->sm[0]);
300         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM.");
301
302     }
303
304     cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ROWNERID_NAME, &pstatMap);
305     if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
306     {
307         cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
308         VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Id Value.");
309         ret = ConvertStrToUuid(strUuid , &pstat->rownerID);
310         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
311         OICFree(strUuid );
312         strUuid  = NULL;
313     }
314
315     *secPstat = pstat;
316     ret = OC_STACK_OK;
317
318 exit:
319     if (CborNoError != cborFindResult)
320     {
321         OIC_LOG(ERROR, TAG, "CBORPayloadToPstat failed");
322         DeletePstatBinData(pstat);
323         pstat = NULL;
324         *secPstat = NULL;
325         ret = OC_STACK_ERROR;
326     }
327
328     return ret;
329 }
330
331 /**
332  * Function to update persistent storage
333  */
334 static bool UpdatePersistentStorage(OicSecPstat_t *pstat)
335 {
336     bool bRet = false;
337
338     size_t size = 0;
339     uint8_t *cborPayload = NULL;
340     OCStackResult ret = PstatToCBORPayload(pstat, &cborPayload, &size);
341     if (OC_STACK_OK == ret)
342     {
343         if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size))
344         {
345             bRet = true;
346         }
347         OICFree(cborPayload);
348     }
349
350     return bRet;
351 }
352
353 static bool ValidateQuery(const char * query)
354 {
355     OIC_LOG (DEBUG, TAG, "In ValidateQuery");
356     if(NULL == gPstat)
357     {
358         return false;
359     }
360
361     bool bInterfaceQry = false;      // does querystring contains 'if' query ?
362     bool bInterfaceMatch = false;    // does 'if' query matches with oic.if.baseline ?
363
364     OicParseQueryIter_t parseIter = {.attrPos = NULL};
365
366     ParseQueryIterInit((unsigned char*)query, &parseIter);
367
368     while (GetNextQuery(&parseIter))
369     {
370         if (strncasecmp((char *)parseIter.attrPos, OC_RSRVD_INTERFACE, parseIter.attrLen) == 0)
371         {
372             bInterfaceQry = true;
373             if ((strncasecmp((char *)parseIter.valPos, OC_RSRVD_INTERFACE_DEFAULT, parseIter.valLen) == 0))
374             {
375                 bInterfaceMatch = true;
376             }
377         }
378     }
379     return (bInterfaceQry ? bInterfaceMatch: true);
380 }
381
382 /**
383  * The entity handler determines how to process a GET request.
384  */
385 static OCEntityHandlerResult HandlePstatGetRequest (const OCEntityHandlerRequest * ehRequest)
386 {
387     OCEntityHandlerResult ehRet = OC_EH_OK;
388
389     OIC_LOG(INFO, TAG, "HandlePstatGetRequest  processing GET request");
390
391     //Checking if Get request is a query.
392     if (ehRequest->query)
393     {
394         OIC_LOG_V(DEBUG,TAG,"query:%s",ehRequest->query);
395         OIC_LOG(DEBUG, TAG, "HandlePstatGetRequest processing query");
396         if (!ValidateQuery(ehRequest->query))
397         {
398             ehRet = OC_EH_ERROR;
399         }
400     }
401
402     /*
403      * For GET or Valid Query request return doxm resource CBOR payload.
404      * For non-valid query return NULL json payload.
405      * A device will 'always' have a default Pstat, so PstatToCBORPayload will
406      * return valid pstat resource json.
407      */
408     size_t size = 0;
409     uint8_t *payload = NULL;
410     if (ehRet == OC_EH_OK)
411     {
412         if(OC_STACK_OK != PstatToCBORPayload(gPstat, &payload, &size))
413         {
414             OIC_LOG(WARNING, TAG, "PstatToCBORPayload failed in HandlePstatGetRequest");
415         }
416     }
417
418     // Send response payload to request originator
419     if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, payload, size))
420     {
421         ehRet = OC_EH_ERROR;
422         OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandlePstatGetRequest");
423     }
424     OICFree(payload);
425     return ehRet;
426 }
427
428 /**
429  * The entity handler determines how to process a POST request.
430  * Per the REST paradigm, POST can also be used to update representation of existing
431  * resource or create a new resource.
432  * For pstat, it updates only tm and om.
433  */
434 static OCEntityHandlerResult HandlePstatPostRequest(const OCEntityHandlerRequest *ehRequest)
435 {
436     OCEntityHandlerResult ehRet = OC_EH_ERROR;
437     OIC_LOG(INFO, TAG, "HandlePstatPostRequest  processing POST request");
438     OicSecPstat_t *pstat = NULL;
439
440     if (ehRequest->payload)
441     {
442         uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData;
443         size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
444         VERIFY_NON_NULL(TAG, payload, ERROR);
445
446         OCStackResult ret = CBORPayloadToPstat(payload, size, &pstat);
447         VERIFY_NON_NULL(TAG, pstat, ERROR);
448         if (OC_STACK_OK == ret)
449         {
450             if (false == (pstat->cm & TAKE_OWNER) && false == pstat->isOp)
451             {
452                 gPstat->cm = pstat->cm;
453                 OIC_LOG (INFO, TAG, "State changed to Ready for Provisioning");
454             }
455             else if (false == (pstat->cm & TAKE_OWNER) && true == pstat->isOp)
456             {
457                 gPstat->isOp =pstat->isOp;
458                 OIC_LOG (INFO, TAG, "State changed to Ready for Normal Operation");
459             }
460             else
461             {
462                 OIC_LOG(DEBUG, TAG, "Invalid Device provisionig state");
463             }
464             if (pstat->om != MULTIPLE_SERVICE_SERVER_DRIVEN && gPstat)
465             {
466                 /*
467                  * Check if the operation mode is in the supported provisioning services
468                  * operation mode list.
469                  */
470                 for (size_t i=0; i< gPstat->smLen; i++)
471                 {
472                     if(gPstat->sm[i] == pstat->om)
473                     {
474                         gPstat->om = pstat->om;
475                         break;
476                     }
477                 }
478             }
479             // Convert pstat data into CBOR for update to persistent storage
480             if (UpdatePersistentStorage(gPstat))
481             {
482                 ehRet = OC_EH_OK;
483             }
484         }
485     }
486  exit:
487     if(OC_EH_OK != ehRet)
488     {
489         /*
490           * If some error is occured while ownership transfer,
491           * ownership transfer related resource should be revert back to initial status.
492           */
493         RestoreDoxmToInitState();
494         RestorePstatToInitState();
495     }
496
497     //Send payload to request originator
498     if(OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
499     {
500         ehRet = OC_EH_ERROR;
501         OIC_LOG (ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
502     }
503     DeletePstatBinData(pstat);
504     return ehRet;
505 }
506
507 /**
508  * This internal method is the entity handler for pstat resources.
509  */
510  OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
511                                           OCEntityHandlerRequest * ehRequest,
512                                           void *callbackParam)
513 {
514     (void)callbackParam;
515     OCEntityHandlerResult ehRet = OC_EH_ERROR;
516     // This method will handle REST request (GET/POST) for /oic/sec/pstat
517     if (flag & OC_REQUEST_FLAG)
518     {
519         OIC_LOG(INFO, TAG, "Flag includes OC_REQUEST_FLAG");
520         switch (ehRequest->method)
521         {
522             case OC_REST_GET:
523                 ehRet = HandlePstatGetRequest(ehRequest);
524                 break;
525             case OC_REST_POST:
526                 ehRet = HandlePstatPostRequest(ehRequest);
527                 break;
528             default:
529                 ehRet = OC_EH_ERROR;
530                 SendSRMResponse(ehRequest, ehRet, NULL, 0);
531                 break;
532         }
533     }
534     return ehRet;
535 }
536
537 /**
538  * This internal method is used to create '/oic/sec/pstat' resource.
539  */
540  OCStackResult CreatePstatResource()
541 {
542     OCStackResult ret = OCCreateResource(&gPstatHandle,
543                                          OIC_RSRC_TYPE_SEC_PSTAT,
544                                          OC_RSRVD_INTERFACE_DEFAULT,
545                                          OIC_RSRC_PSTAT_URI,
546                                          PstatEntityHandler,
547                                          NULL,
548                                          OC_SECURE |
549                                          OC_DISCOVERABLE);
550
551     if (OC_STACK_OK != ret)
552     {
553         OIC_LOG(FATAL, TAG, "Unable to instantiate pstat resource");
554         DeInitPstatResource();
555     }
556     return ret;
557 }
558
559 /**
560  * Get the default value.
561  *
562  * @return the gDefaultPstat pointer.
563  */
564 static OicSecPstat_t* GetPstatDefault()
565 {
566     return &gDefaultPstat;
567 }
568
569 OCStackResult InitPstatResource()
570 {
571     OCStackResult ret = OC_STACK_ERROR;
572
573     // Read Pstat resource from PS
574     uint8_t *data = NULL;
575     size_t size = 0;
576     OicUuid_t emptyUuid = {.id={0}};
577     ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_PSTAT_NAME, &data, &size);
578     // If database read failed
579     if (OC_STACK_OK != ret)
580     {
581         OIC_LOG (DEBUG, TAG, "ReadSVDataFromPS failed");
582     }
583     if (data)
584     {
585         // Read ACL resource from PS
586         ret = CBORPayloadToPstat(data, size, &gPstat);
587         OICFree(data);
588     }
589     /*
590      * If SVR database in persistent storage got corrupted or
591      * is not available for some reason, a default pstat is created
592      * which allows user to initiate pstat provisioning again.
593      */
594     if ((OC_STACK_OK != ret) || !gPstat)
595     {
596         gPstat = GetPstatDefault();
597     }
598     VERIFY_NON_NULL(TAG, gPstat, FATAL);
599
600     //In case of Pstat's device id is empty, fill the device id as doxm's device id.
601     if(0 == memcmp(&gPstat->deviceID, &emptyUuid, sizeof(OicUuid_t)))
602     {
603         OicUuid_t doxmUuid = {.id={0}};
604         if(OC_STACK_OK == GetDoxmDeviceID(&doxmUuid))
605         {
606             memcpy(&gPstat->deviceID, &doxmUuid, sizeof(OicUuid_t));
607         }
608     }
609
610     // Instantiate 'oic.sec.pstat'
611     ret = CreatePstatResource();
612
613 exit:
614     if (OC_STACK_OK != ret)
615     {
616         DeInitPstatResource();
617     }
618     return ret;
619 }
620
621 OCStackResult DeInitPstatResource()
622 {
623     if (gPstat != &gDefaultPstat)
624     {
625         DeletePstatBinData(gPstat);
626         gPstat = NULL;
627     }
628     return OCDeleteResource(gPstatHandle);
629 }
630
631 /**
632  * Function to restore pstat resurce to initial status.
633  * This function will use in case of error while ownership transfer
634  */
635 void RestorePstatToInitState()
636 {
637     if(gPstat)
638     {
639         OIC_LOG(INFO, TAG, "PSTAT resource will revert back to initial status.");
640
641         gPstat->cm = (OicSecDpm_t)(gPstat->cm | TAKE_OWNER);
642         gPstat->tm = (OicSecDpm_t)(gPstat->tm & (~TAKE_OWNER));
643         gPstat->om = SINGLE_SERVICE_CLIENT_DRIVEN;
644         if(gPstat->sm && 0 < gPstat->smLen)
645         {
646             gPstat->sm[0] = SINGLE_SERVICE_CLIENT_DRIVEN;
647         }
648
649         if (!UpdatePersistentStorage(gPstat))
650         {
651             OIC_LOG(ERROR, TAG, "Failed to revert PSTAT in persistent storage");
652         }
653     }
654 }
655
656 OCStackResult SetPstatRownerId(const OicUuid_t* newROwner)
657 {
658     OCStackResult ret = OC_STACK_ERROR;
659     uint8_t *cborPayload = NULL;
660     size_t size = 0;
661     OicUuid_t prevId = {.id={0}};
662
663     if(NULL == newROwner)
664     {
665         ret = OC_STACK_INVALID_PARAM;
666     }
667     if(NULL == gPstat)
668     {
669         ret = OC_STACK_NO_RESOURCE;
670     }
671
672     if(newROwner && gPstat)
673     {
674         memcpy(prevId.id, gPstat->rownerID.id, sizeof(prevId.id));
675         memcpy(gPstat->rownerID.id, newROwner->id, sizeof(newROwner->id));
676
677         ret = PstatToCBORPayload(gPstat, &cborPayload, &size);
678         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
679
680         ret = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size);
681         VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
682
683         OICFree(cborPayload);
684     }
685
686     return ret;
687
688 exit:
689     OICFree(cborPayload);
690     memcpy(gPstat->rownerID.id, prevId.id, sizeof(prevId.id));
691     return ret;
692 }
693
694 /**
695  * This function returns the "isop" status of the device.
696  *
697  * @return true iff pstat.isop == 1, else false
698  */
699 bool GetPstatIsop()
700 {
701     return gPstat->isOp;
702 }
703
704 OCStackResult GetPstatRownerId(OicUuid_t *rowneruuid)
705 {
706     OCStackResult retVal = OC_STACK_ERROR;
707     if (gPstat)
708     {
709         *rowneruuid = gPstat->rownerID;
710         retVal = OC_STACK_OK;
711     }
712     return retVal;
713 }