1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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"
35 #define TAG "SRM-PSTAT"
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;
41 // Max cbor size payload.
42 static const uint16_t CBOR_MAX_SIZE = 4400;
44 // PSTAT Map size - Number of mandatory items
45 static const uint8_t PSTAT_MAP_SIZE = 6;
47 // Number of writeable property
48 static const uint8_t WRITEABLE_PROPERTY_SIZE = 3;
50 static OicSecDpom_t gSm = SINGLE_SERVICE_CLIENT_DRIVEN;
51 static OicSecPstat_t gDefaultPstat =
54 (OicSecDpm_t)(BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
55 PROVISION_CREDENTIALS | PROVISION_ACLS), // OicSecDpm_t cm
56 (OicSecDpm_t)(TAKE_OWNER | BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
57 PROVISION_CREDENTIALS | PROVISION_ACLS), // OicSecDpm_t tm
58 {.id = {0}}, // OicUuid_t deviceID
59 SINGLE_SERVICE_CLIENT_DRIVEN, // OicSecDpom_t om */
60 1, // the number of elts in Sms
61 &gSm, // OicSecDpom_t *sm
62 0, // uint16_t commitHash
63 {.id = {0}}, // OicUuid_t rownerID
66 static OicSecPstat_t *gPstat = NULL;
68 static OCResourceHandle gPstatHandle = NULL;
71 * This method is internal method.
72 * the param roParsed is optionally used to know whether cborPayload has
73 * at least read only property value or not.
75 static OCStackResult CBORPayloadToPstatBin(const uint8_t *cborPayload, const size_t size,
76 OicSecPstat_t **secPstat, bool *roParsed);
78 void DeletePstatBinData(OicSecPstat_t* pstat)
82 //Clean 'supported modes' field
90 OCStackResult PstatToCBORPayload(const OicSecPstat_t *pstat, uint8_t **payload, size_t *size,
93 if (NULL == pstat || NULL == payload || NULL != *payload || NULL == size)
95 return OC_STACK_INVALID_PARAM;
98 size_t cborLen = *size;
107 OCStackResult ret = OC_STACK_ERROR;
108 size_t pstatMapSize = PSTAT_MAP_SIZE;
110 CborEncoder pstatMap;
111 char* strUuid = NULL;
113 int64_t cborEncoderResult = CborNoError;
115 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
116 VERIFY_NON_NULL(TAG, outPayload, ERROR);
117 cbor_encoder_init(&encoder, outPayload, cborLen, 0);
119 if (false == writableOnly)
121 pstatMapSize += WRITEABLE_PROPERTY_SIZE;
124 cborEncoderResult = cbor_encoder_create_map(&encoder, &pstatMap, pstatMapSize);
125 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pstat Map.");
127 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ISOP_NAME,
128 strlen(OIC_JSON_ISOP_NAME));
129 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ISOP Name Tag.");
130 cborEncoderResult = cbor_encode_boolean(&pstatMap, pstat->isOp);
131 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ISOP Name Value.");
133 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_CM_NAME,
134 strlen(OIC_JSON_CM_NAME));
135 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CM Name Tag.");
136 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->cm);
137 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CM Name Value.");
139 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_TM_NAME,
140 strlen(OIC_JSON_TM_NAME));
141 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding TM Name Tag.");
142 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->tm);
143 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding TM Name Value.");
145 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_OM_NAME,
146 strlen(OIC_JSON_OM_NAME));
147 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding OM Name Tag.");
148 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->om);
149 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding OM Name Value.");
151 if (false == writableOnly)
153 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_SM_NAME,
154 strlen(OIC_JSON_SM_NAME));
155 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Tag.");
156 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->sm[0]);
157 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Value.");
159 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_DEVICE_ID_NAME,
160 strlen(OIC_JSON_DEVICE_ID_NAME));
161 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Tag.");
162 ret = ConvertUuidToStr(&pstat->deviceID, &strUuid);
163 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
164 cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid));
165 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Value.");
169 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ROWNERID_NAME,
170 strlen(OIC_JSON_ROWNERID_NAME));
171 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Tag.");
172 ret = ConvertUuidToStr(&pstat->rownerID, &strUuid);
173 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
174 cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid));
175 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Value.");
182 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_RT_NAME,
183 strlen(OIC_JSON_RT_NAME));
184 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Name Tag.");
185 cborEncoderResult = cbor_encoder_create_array(&pstatMap, &rtArray, 1);
186 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Value.");
187 for (size_t i = 0; i < 1; i++)
189 cborEncoderResult = cbor_encode_text_string(&rtArray, OIC_RSRC_TYPE_SEC_PSTAT,
190 strlen(OIC_RSRC_TYPE_SEC_PSTAT));
191 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding RT Value.");
193 cborEncoderResult = cbor_encoder_close_container(&pstatMap, &rtArray);
194 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing RT.");
198 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_IF_NAME,
199 strlen(OIC_JSON_IF_NAME));
200 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Name Tag.");
201 cborEncoderResult = cbor_encoder_create_array(&pstatMap, &ifArray, 1);
202 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Value.");
203 for (size_t i = 0; i < 1; i++)
205 cborEncoderResult = cbor_encode_text_string(&ifArray, OC_RSRVD_INTERFACE_DEFAULT,
206 strlen(OC_RSRVD_INTERFACE_DEFAULT));
207 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding IF Value.");
209 cborEncoderResult = cbor_encoder_close_container(&pstatMap, &ifArray);
210 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing IF.");
212 cborEncoderResult = cbor_encoder_close_container(&encoder, &pstatMap);
213 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Closing PSTAT Map.");
215 if (CborNoError == cborEncoderResult)
217 *size = encoder.ptr - outPayload;
218 *payload = outPayload;
222 if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
224 // reallocate and try again!
226 // Since the allocated initial memory failed, double the memory.
227 cborLen += encoder.ptr - encoder.end;
228 cborEncoderResult = CborNoError;
229 ret = PstatToCBORPayload(pstat, payload, &cborLen, writableOnly);
230 if (OC_STACK_OK == ret)
236 if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
242 ret = OC_STACK_ERROR;
248 OCStackResult CBORPayloadToPstat(const uint8_t *cborPayload, const size_t size,
249 OicSecPstat_t **secPstat)
251 return CBORPayloadToPstatBin(cborPayload, size, secPstat, NULL);
254 static OCStackResult CBORPayloadToPstatBin(const uint8_t *cborPayload, const size_t size,
255 OicSecPstat_t **secPstat, bool *roParsed)
257 if (NULL == cborPayload || NULL == secPstat || NULL != *secPstat || 0 == size)
259 return OC_STACK_INVALID_PARAM;
262 OCStackResult ret = OC_STACK_ERROR;
267 CborError cborFindResult = CborNoError;
268 char *strUuid = NULL;
271 cbor_parser_init(cborPayload, size, 0, &parser, &pstatCbor);
272 CborValue pstatMap = { .parser = NULL };
274 OicSecPstat_t *pstat = NULL;
275 cborFindResult = cbor_value_enter_container(&pstatCbor, &pstatMap);
276 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Map.");
278 pstat = (OicSecPstat_t *)OICCalloc(1, sizeof(OicSecPstat_t));
279 VERIFY_NON_NULL(TAG, pstat, ERROR);
281 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ISOP_NAME, &pstatMap);
282 if (CborNoError == cborFindResult && cbor_value_is_boolean(&pstatMap))
284 cborFindResult = cbor_value_get_boolean(&pstatMap, &pstat->isOp);
285 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding isOp Value.");
289 pstat->isOp = gPstat->isOp;
290 cborFindResult = CborNoError;
293 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_DEVICE_ID_NAME, &pstatMap);
294 if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
296 cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
297 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Device Id Value.");
298 ret = ConvertStrToUuid(strUuid , &pstat->deviceID);
299 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
310 memcpy(&pstat->deviceID, &gPstat->deviceID, sizeof(OicUuid_t));
311 cborFindResult = CborNoError;
314 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_CM_NAME, &pstatMap);
315 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
317 cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->cm);
318 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CM.");
322 pstat->cm = gPstat->cm;
323 cborFindResult = CborNoError;
326 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_TM_NAME, &pstatMap);
327 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
329 cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->tm);
330 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding TM.");
334 pstat->tm = gPstat->tm;
335 cborFindResult = CborNoError;
338 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_OM_NAME, &pstatMap);
339 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
341 cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->om);
342 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding OM.");
346 pstat->om = gPstat->om;
347 cborFindResult = CborNoError;
350 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_SM_NAME, &pstatMap);
351 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
354 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
355 cborFindResult = cbor_value_get_int(&pstatMap, (int *) &pstat->sm[0]);
356 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM.");
365 VERIFY_NON_NULL(TAG, gPstat, ERROR);
366 pstat->smLen = gPstat->smLen;
367 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
368 *pstat->sm = *gPstat->sm;
369 cborFindResult = CborNoError;
372 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ROWNERID_NAME, &pstatMap);
373 if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
375 cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
376 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Id Value.");
377 ret = ConvertStrToUuid(strUuid , &pstat->rownerID);
378 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
389 VERIFY_NON_NULL(TAG, gPstat, ERROR);
390 memcpy(pstat->rownerID.id, gPstat->rownerID.id, sizeof(gPstat->rownerID.id));
391 cborFindResult = CborNoError;
398 if (CborNoError != cborFindResult)
400 OIC_LOG(ERROR, TAG, "CBORPayloadToPstat failed");
401 DeletePstatBinData(pstat);
404 ret = OC_STACK_ERROR;
411 * Function to update persistent storage
413 static bool UpdatePersistentStorage(OicSecPstat_t *pstat)
418 uint8_t *cborPayload = NULL;
419 OCStackResult ret = PstatToCBORPayload(pstat, &cborPayload, &size, false);
420 if (OC_STACK_OK == ret)
422 if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size))
426 OICFree(cborPayload);
432 static bool ValidateQuery(const char * query)
434 OIC_LOG (DEBUG, TAG, "In ValidateQuery");
440 bool bInterfaceQry = false; // does querystring contains 'if' query ?
441 bool bInterfaceMatch = false; // does 'if' query matches with oic.if.baseline ?
443 OicParseQueryIter_t parseIter = {.attrPos = NULL};
445 ParseQueryIterInit((unsigned char*)query, &parseIter);
447 while (GetNextQuery(&parseIter))
449 if (strncasecmp((char *)parseIter.attrPos, OC_RSRVD_INTERFACE, parseIter.attrLen) == 0)
451 bInterfaceQry = true;
452 if ((strncasecmp((char *)parseIter.valPos, OC_RSRVD_INTERFACE_DEFAULT, parseIter.valLen) == 0))
454 bInterfaceMatch = true;
458 return (bInterfaceQry ? bInterfaceMatch: true);
462 * The entity handler determines how to process a GET request.
464 static OCEntityHandlerResult HandlePstatGetRequest (const OCEntityHandlerRequest * ehRequest)
466 OCEntityHandlerResult ehRet = OC_EH_OK;
468 OIC_LOG(INFO, TAG, "HandlePstatGetRequest processing GET request");
470 //Checking if Get request is a query.
471 if (ehRequest->query)
473 OIC_LOG_V(DEBUG,TAG,"query:%s",ehRequest->query);
474 OIC_LOG(DEBUG, TAG, "HandlePstatGetRequest processing query");
475 if (!ValidateQuery(ehRequest->query))
482 * For GET or Valid Query request return doxm resource CBOR payload.
483 * For non-valid query return NULL json payload.
484 * A device will 'always' have a default Pstat, so PstatToCBORPayload will
485 * return valid pstat resource json.
488 uint8_t *payload = NULL;
489 if (ehRet == OC_EH_OK)
491 if(OC_STACK_OK != PstatToCBORPayload(gPstat, &payload, &size, false))
493 OIC_LOG(WARNING, TAG, "PstatToCBORPayload failed in HandlePstatGetRequest");
497 // Send response payload to request originator
498 ehRet = ((SendSRMResponse(ehRequest, ehRet, payload, size)) == OC_STACK_OK) ?
499 OC_EH_OK : OC_EH_ERROR;
505 * The entity handler determines how to process a POST request.
506 * Per the REST paradigm, POST can also be used to update representation of existing
507 * resource or create a new resource.
508 * For pstat, it updates only tm and om.
510 static OCEntityHandlerResult HandlePstatPostRequest(const OCEntityHandlerRequest *ehRequest)
512 OCEntityHandlerResult ehRet = OC_EH_ERROR;
513 OIC_LOG(INFO, TAG, "HandlePstatPostRequest processing POST request");
514 OicSecPstat_t *pstat = NULL;
515 static uint16_t prevMsgId = 0;
517 if (ehRequest->payload && NULL != gPstat)
519 uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData;
520 size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
521 VERIFY_NON_NULL(TAG, payload, ERROR);
523 bool roParsed = false;
524 OCStackResult ret = CBORPayloadToPstatBin(payload, size, &pstat, &roParsed);
525 VERIFY_NON_NULL(TAG, pstat, ERROR);
526 if (OC_STACK_OK == ret)
528 bool validReq = false;
530 if (true == roParsed)
532 OIC_LOG(ERROR, TAG, "Not acceptable request because of read-only properties");
533 ehRet = OC_EH_NOT_ACCEPTABLE;
537 //operation mode(om) should be one of supported modes(sm)
538 for(size_t i = 0; i < gPstat->smLen; i++)
540 if(gPstat->sm[i] == pstat->om)
549 OIC_LOG_V(ERROR, TAG, "%d is unsupported Operation Mode", (int) pstat->om);
550 ehRet = OC_EH_BAD_REQ;
555 //Currently, we dose not support the multiple service server driven yet.
556 if (pstat->om != MULTIPLE_SERVICE_SERVER_DRIVEN)
558 if ((pstat->cm & RESET) && false == pstat->isOp)
561 OIC_LOG(INFO, TAG, "State changed to Ready for Reset");
563 else if ((pstat->cm & TAKE_OWNER) && false == pstat->isOp)
566 OIC_LOG (INFO, TAG, "State changed to Ready for Ownership transfer");
568 else if (false == (pstat->cm & TAKE_OWNER) && false == pstat->isOp)
571 OIC_LOG(INFO, TAG, "State changed to Ready for Provisioning");
573 else if (false == (pstat->cm & TAKE_OWNER) && true == pstat->isOp)
576 OIC_LOG (INFO, TAG, "State changed to Ready for Normal Operation");
580 OIC_LOG(DEBUG, TAG, "Invalid Device provisionig state");
581 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
582 ehRet = OC_EH_BAD_REQ;
589 OIC_LOG(DEBUG, TAG, "Bad request for PSTAT");
590 ehRet = OC_EH_BAD_REQ;
594 gPstat->isOp = pstat->isOp;
595 gPstat->om = pstat->om;
596 gPstat->tm = pstat->tm;
597 gPstat->cm = pstat->cm;
599 // Convert pstat data into CBOR for update to persistent storage
600 if (UpdatePersistentStorage(gPstat))
604 if (true == (pstat->cm & RESET))
606 if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
609 OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
610 DeletePstatBinData(pstat);
613 ret = ResetSecureResourceInPS();
614 if (OC_STACK_OK == ret)
618 DeletePstatBinData(pstat);
625 if(OC_EH_OK != ehRet)
628 * If some error is occured while ownership transfer,
629 * ownership transfer related resource should be revert back to initial status.
631 const OicSecDoxm_t* doxm = GetDoxmResourceData();
634 if(!doxm->owned && prevMsgId != ehRequest->messageID)
636 RestoreDoxmToInitState();
637 RestorePstatToInitState();
642 OIC_LOG(ERROR, TAG, "Invalid DOXM resource.");
647 prevMsgId = ehRequest->messageID;
650 // Send response payload to request originator
651 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
652 OC_EH_OK : OC_EH_ERROR;
654 DeletePstatBinData(pstat);
659 * This internal method is the entity handler for pstat resources.
661 OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
662 OCEntityHandlerRequest * ehRequest,
666 OCEntityHandlerResult ehRet = OC_EH_ERROR;
667 // This method will handle REST request (GET/POST) for /oic/sec/pstat
668 if (flag & OC_REQUEST_FLAG)
670 OIC_LOG(INFO, TAG, "Flag includes OC_REQUEST_FLAG");
671 switch (ehRequest->method)
674 ehRet = HandlePstatGetRequest(ehRequest);
677 ehRet = HandlePstatPostRequest(ehRequest);
680 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
681 OC_EH_OK : OC_EH_ERROR;
689 * This internal method is used to create '/oic/sec/pstat' resource.
691 OCStackResult CreatePstatResource()
693 OCStackResult ret = OCCreateResource(&gPstatHandle,
694 OIC_RSRC_TYPE_SEC_PSTAT,
695 OC_RSRVD_INTERFACE_DEFAULT,
702 if (OC_STACK_OK != ret)
704 OIC_LOG(FATAL, TAG, "Unable to instantiate pstat resource");
705 DeInitPstatResource();
711 * Get the default value.
713 * @return the gDefaultPstat pointer.
715 static OicSecPstat_t* GetPstatDefault()
717 return &gDefaultPstat;
720 OCStackResult InitPstatResource()
722 OCStackResult ret = OC_STACK_ERROR;
724 // Read Pstat resource from PS
725 uint8_t *data = NULL;
727 OicUuid_t emptyUuid = {.id={0}};
728 ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_PSTAT_NAME, &data, &size);
729 // If database read failed
730 if (OC_STACK_OK != ret)
732 OIC_LOG (DEBUG, TAG, "ReadSVDataFromPS failed");
736 // Read ACL resource from PS
737 ret = CBORPayloadToPstat(data, size, &gPstat);
741 * If SVR database in persistent storage got corrupted or
742 * is not available for some reason, a default pstat is created
743 * which allows user to initiate pstat provisioning again.
745 if ((OC_STACK_OK != ret) || !gPstat)
747 gPstat = GetPstatDefault();
749 VERIFY_NON_NULL(TAG, gPstat, FATAL);
751 //In case of Pstat's device id is empty, fill the device id as doxm's device id.
752 if(0 == memcmp(&gPstat->deviceID, &emptyUuid, sizeof(OicUuid_t)))
754 OicUuid_t doxmUuid = {.id={0}};
755 if(OC_STACK_OK == GetDoxmDeviceID(&doxmUuid))
757 memcpy(&gPstat->deviceID, &doxmUuid, sizeof(OicUuid_t));
761 // Instantiate 'oic.sec.pstat'
762 ret = CreatePstatResource();
765 if (OC_STACK_OK != ret)
767 DeInitPstatResource();
772 OCStackResult DeInitPstatResource()
774 if (gPstat != &gDefaultPstat)
776 DeletePstatBinData(gPstat);
779 return OCDeleteResource(gPstatHandle);
783 * Function to restore pstat resurce to initial status.
784 * This function will use in case of error while ownership transfer
786 void RestorePstatToInitState()
790 OIC_LOG(INFO, TAG, "PSTAT resource will revert back to initial status.");
792 gPstat->cm = (OicSecDpm_t)(gPstat->cm | TAKE_OWNER);
793 gPstat->tm = (OicSecDpm_t)(gPstat->tm & (~TAKE_OWNER));
794 gPstat->om = SINGLE_SERVICE_CLIENT_DRIVEN;
795 if(gPstat->sm && 0 < gPstat->smLen)
797 gPstat->sm[0] = SINGLE_SERVICE_CLIENT_DRIVEN;
800 if (!UpdatePersistentStorage(gPstat))
802 OIC_LOG(ERROR, TAG, "Failed to revert PSTAT in persistent storage");
807 OCStackResult SetPstatRownerId(const OicUuid_t* newROwner)
809 OCStackResult ret = OC_STACK_ERROR;
810 uint8_t *cborPayload = NULL;
812 OicUuid_t prevId = {.id={0}};
814 if(NULL == newROwner)
816 ret = OC_STACK_INVALID_PARAM;
820 ret = OC_STACK_NO_RESOURCE;
823 if(newROwner && gPstat)
825 memcpy(prevId.id, gPstat->rownerID.id, sizeof(prevId.id));
826 memcpy(gPstat->rownerID.id, newROwner->id, sizeof(newROwner->id));
828 ret = PstatToCBORPayload(gPstat, &cborPayload, &size, false);
829 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
831 ret = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size);
832 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
834 OICFree(cborPayload);
840 OICFree(cborPayload);
841 memcpy(gPstat->rownerID.id, prevId.id, sizeof(prevId.id));
846 * This function returns the "isop" status of the device.
848 * @return true iff pstat.isop == 1, else false
855 OCStackResult GetPstatRownerId(OicUuid_t *rowneruuid)
857 OCStackResult retVal = OC_STACK_ERROR;
860 *rowneruuid = gPstat->rownerID;
861 retVal = OC_STACK_OK;