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 "ocpayloadcbor.h"
28 #include "payload_logging.h"
29 #include "resourcemanager.h"
30 #include "pstatresource.h"
31 #include "doxmresource.h"
32 #include "psinterface.h"
33 #include "srmresourcestrings.h"
34 #include "srmutility.h"
36 #define TAG "OIC_SRM_PSTAT"
38 /** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
39 * The value of payload size is increased until reaching below max cbor size. */
40 static const uint16_t CBOR_SIZE = 512;
42 // Max cbor size payload.
43 static const uint16_t CBOR_MAX_SIZE = 4400;
45 // PSTAT Map size - Number of mandatory items
46 static const uint8_t PSTAT_MAP_SIZE = 6;
48 // Number of writeable property
49 static const uint8_t WRITEABLE_PROPERTY_SIZE = 3;
51 static OicSecDpom_t gSm = SINGLE_SERVICE_CLIENT_DRIVEN;
52 static OicSecPstat_t gDefaultPstat =
55 (OicSecDpm_t)(BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
56 PROVISION_CREDENTIALS | PROVISION_ACLS), // OicSecDpm_t cm
57 (OicSecDpm_t)(TAKE_OWNER | BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
58 PROVISION_CREDENTIALS | PROVISION_ACLS), // OicSecDpm_t tm
59 {.id = {0}}, // OicUuid_t deviceID
60 SINGLE_SERVICE_CLIENT_DRIVEN, // OicSecDpom_t om */
61 1, // the number of elts in Sms
62 &gSm, // OicSecDpom_t *sm
63 0, // uint16_t commitHash
64 {.id = {0}}, // OicUuid_t rownerID
67 static OicSecPstat_t *gPstat = NULL;
69 static OCResourceHandle gPstatHandle = NULL;
72 * This method is internal method.
73 * the param roParsed is optionally used to know whether cborPayload has
74 * at least read only property value or not.
76 static OCStackResult CBORPayloadToPstatBin(const uint8_t *cborPayload, const size_t size,
77 OicSecPstat_t **secPstat, bool *roParsed);
79 void DeletePstatBinData(OicSecPstat_t* pstat)
83 //Clean 'supported modes' field
91 OCStackResult PstatToCBORPayload(const OicSecPstat_t *pstat, uint8_t **payload, size_t *size,
94 if (NULL == pstat || NULL == payload || NULL != *payload || NULL == size)
96 return OC_STACK_INVALID_PARAM;
99 size_t cborLen = *size;
108 OCStackResult ret = OC_STACK_ERROR;
109 size_t pstatMapSize = PSTAT_MAP_SIZE;
111 CborEncoder pstatMap;
112 char* strUuid = NULL;
114 int64_t cborEncoderResult = CborNoError;
116 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
117 VERIFY_NON_NULL(TAG, outPayload, ERROR);
118 cbor_encoder_init(&encoder, outPayload, cborLen, 0);
120 if (false == writableOnly)
122 pstatMapSize += WRITEABLE_PROPERTY_SIZE;
125 cborEncoderResult = cbor_encoder_create_map(&encoder, &pstatMap, pstatMapSize);
126 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pstat Map.");
128 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ISOP_NAME,
129 strlen(OIC_JSON_ISOP_NAME));
130 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ISOP Name Tag.");
131 cborEncoderResult = cbor_encode_boolean(&pstatMap, pstat->isOp);
132 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ISOP Name Value.");
134 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_CM_NAME,
135 strlen(OIC_JSON_CM_NAME));
136 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CM Name Tag.");
137 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->cm);
138 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CM Name Value.");
140 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_TM_NAME,
141 strlen(OIC_JSON_TM_NAME));
142 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding TM Name Tag.");
143 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->tm);
144 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding TM Name Value.");
146 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_OM_NAME,
147 strlen(OIC_JSON_OM_NAME));
148 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding OM Name Tag.");
149 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->om);
150 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding OM Name Value.");
152 if (false == writableOnly)
154 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_SM_NAME,
155 strlen(OIC_JSON_SM_NAME));
156 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Tag.");
157 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->sm[0]);
158 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Value.");
160 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_DEVICE_ID_NAME,
161 strlen(OIC_JSON_DEVICE_ID_NAME));
162 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Tag.");
163 ret = ConvertUuidToStr(&pstat->deviceID, &strUuid);
164 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
165 cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid));
166 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Value.");
170 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ROWNERID_NAME,
171 strlen(OIC_JSON_ROWNERID_NAME));
172 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Tag.");
173 ret = ConvertUuidToStr(&pstat->rownerID, &strUuid);
174 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
175 cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid));
176 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Value.");
183 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_RT_NAME,
184 strlen(OIC_JSON_RT_NAME));
185 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Name Tag.");
186 cborEncoderResult = cbor_encoder_create_array(&pstatMap, &rtArray, 1);
187 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Value.");
188 for (size_t i = 0; i < 1; i++)
190 cborEncoderResult = cbor_encode_text_string(&rtArray, OIC_RSRC_TYPE_SEC_PSTAT,
191 strlen(OIC_RSRC_TYPE_SEC_PSTAT));
192 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding RT Value.");
194 cborEncoderResult = cbor_encoder_close_container(&pstatMap, &rtArray);
195 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing RT.");
199 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_IF_NAME,
200 strlen(OIC_JSON_IF_NAME));
201 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Name Tag.");
202 cborEncoderResult = cbor_encoder_create_array(&pstatMap, &ifArray, 1);
203 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Value.");
204 for (size_t i = 0; i < 1; i++)
206 cborEncoderResult = cbor_encode_text_string(&ifArray, OC_RSRVD_INTERFACE_DEFAULT,
207 strlen(OC_RSRVD_INTERFACE_DEFAULT));
208 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding IF Value.");
210 cborEncoderResult = cbor_encoder_close_container(&pstatMap, &ifArray);
211 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing IF.");
213 cborEncoderResult = cbor_encoder_close_container(&encoder, &pstatMap);
214 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Closing PSTAT Map.");
216 if (CborNoError == cborEncoderResult)
218 *size = cbor_encoder_get_buffer_size(&encoder, outPayload);
219 *payload = outPayload;
223 if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
225 // reallocate and try again!
227 // Since the allocated initial memory failed, double the memory.
228 cborLen += cbor_encoder_get_buffer_size(&encoder, encoder.end);
229 cborEncoderResult = CborNoError;
230 ret = PstatToCBORPayload(pstat, payload, &cborLen, writableOnly);
231 if (OC_STACK_OK == ret)
237 if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
243 ret = OC_STACK_ERROR;
249 OCStackResult CBORPayloadToPstat(const uint8_t *cborPayload, const size_t size,
250 OicSecPstat_t **secPstat)
252 return CBORPayloadToPstatBin(cborPayload, size, secPstat, NULL);
255 static OCStackResult CBORPayloadToPstatBin(const uint8_t *cborPayload, const size_t size,
256 OicSecPstat_t **secPstat, bool *roParsed)
258 if (NULL == cborPayload || NULL == secPstat || NULL != *secPstat || 0 == size)
260 return OC_STACK_INVALID_PARAM;
263 OCStackResult ret = OC_STACK_ERROR;
268 CborError cborFindResult = CborNoError;
269 char *strUuid = NULL;
272 cbor_parser_init(cborPayload, size, 0, &parser, &pstatCbor);
273 CborValue pstatMap = { .parser = NULL };
275 OicSecPstat_t *pstat = NULL;
276 cborFindResult = cbor_value_enter_container(&pstatCbor, &pstatMap);
277 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Map.");
279 pstat = (OicSecPstat_t *)OICCalloc(1, sizeof(OicSecPstat_t));
280 VERIFY_NON_NULL(TAG, pstat, ERROR);
282 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ISOP_NAME, &pstatMap);
283 if (CborNoError == cborFindResult && cbor_value_is_boolean(&pstatMap))
285 cborFindResult = cbor_value_get_boolean(&pstatMap, &pstat->isOp);
286 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding isOp Value.");
290 pstat->isOp = gPstat->isOp;
291 cborFindResult = CborNoError;
294 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_DEVICE_ID_NAME, &pstatMap);
295 if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
297 cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
298 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Device Id Value.");
299 ret = ConvertStrToUuid(strUuid , &pstat->deviceID);
300 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
311 memcpy(&pstat->deviceID, &gPstat->deviceID, sizeof(OicUuid_t));
312 cborFindResult = CborNoError;
315 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_CM_NAME, &pstatMap);
316 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
320 cborFindResult = cbor_value_get_int(&pstatMap, &cm);
321 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CM.");
322 pstat->cm = (OicSecDpm_t)cm;
326 pstat->cm = gPstat->cm;
327 cborFindResult = CborNoError;
330 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_TM_NAME, &pstatMap);
331 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
335 cborFindResult = cbor_value_get_int(&pstatMap, &tm);
336 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding TM.");
337 pstat->tm = (OicSecDpm_t)tm;
341 pstat->tm = gPstat->tm;
342 cborFindResult = CborNoError;
345 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_OM_NAME, &pstatMap);
346 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
350 cborFindResult = cbor_value_get_int(&pstatMap, &om);
351 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding OM.");
352 pstat->om = (OicSecDpom_t)om;
356 pstat->om = gPstat->om;
357 cborFindResult = CborNoError;
360 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_SM_NAME, &pstatMap);
361 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
366 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
367 cborFindResult = cbor_value_get_int(&pstatMap, &sm);
368 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM.");
369 pstat->sm[0] = (OicSecDpom_t)sm;
378 VERIFY_NON_NULL(TAG, gPstat, ERROR);
379 pstat->smLen = gPstat->smLen;
380 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
381 *pstat->sm = *gPstat->sm;
382 cborFindResult = CborNoError;
385 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ROWNERID_NAME, &pstatMap);
386 if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
388 cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
389 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Id Value.");
390 ret = ConvertStrToUuid(strUuid , &pstat->rownerID);
391 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
397 VERIFY_NON_NULL(TAG, gPstat, ERROR);
398 memcpy(pstat->rownerID.id, gPstat->rownerID.id, sizeof(gPstat->rownerID.id));
399 cborFindResult = CborNoError;
406 if (CborNoError != cborFindResult)
408 OIC_LOG(ERROR, TAG, "CBORPayloadToPstat failed");
409 DeletePstatBinData(pstat);
412 ret = OC_STACK_ERROR;
419 * Function to update persistent storage
421 static bool UpdatePersistentStorage(OicSecPstat_t *pstat)
426 uint8_t *cborPayload = NULL;
427 OCStackResult ret = PstatToCBORPayload(pstat, &cborPayload, &size, false);
428 if (OC_STACK_OK == ret)
430 if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size))
434 OICFree(cborPayload);
440 static bool ValidateQuery(const char * query)
442 OIC_LOG (DEBUG, TAG, "In ValidateQuery");
448 bool bInterfaceQry = false; // does querystring contains 'if' query ?
449 bool bInterfaceMatch = false; // does 'if' query matches with oic.if.baseline ?
451 OicParseQueryIter_t parseIter = {.attrPos = NULL};
453 ParseQueryIterInit((unsigned char*)query, &parseIter);
455 while (GetNextQuery(&parseIter))
457 if (strncasecmp((char *)parseIter.attrPos, OC_RSRVD_INTERFACE, parseIter.attrLen) == 0)
459 bInterfaceQry = true;
460 if ((strncasecmp((char *)parseIter.valPos, OC_RSRVD_INTERFACE_DEFAULT, parseIter.valLen) == 0))
462 bInterfaceMatch = true;
466 return (bInterfaceQry ? bInterfaceMatch: true);
470 * The entity handler determines how to process a GET request.
472 static OCEntityHandlerResult HandlePstatGetRequest (const OCEntityHandlerRequest * ehRequest)
474 OCEntityHandlerResult ehRet = OC_EH_OK;
476 OIC_LOG(INFO, TAG, "HandlePstatGetRequest processing GET request");
478 //Checking if Get request is a query.
479 if (ehRequest->query)
481 OIC_LOG_V(DEBUG,TAG,"query:%s",ehRequest->query);
482 OIC_LOG(DEBUG, TAG, "HandlePstatGetRequest processing query");
483 if (!ValidateQuery(ehRequest->query))
490 * For GET or Valid Query request return doxm resource CBOR payload.
491 * For non-valid query return NULL json payload.
492 * A device will 'always' have a default Pstat, so PstatToCBORPayload will
493 * return valid pstat resource json.
496 uint8_t *payload = NULL;
497 if (ehRet == OC_EH_OK)
499 if(OC_STACK_OK != PstatToCBORPayload(gPstat, &payload, &size, false))
501 OIC_LOG(WARNING, TAG, "PstatToCBORPayload failed in HandlePstatGetRequest");
505 // Send response payload to request originator
506 ehRet = ((SendSRMResponse(ehRequest, ehRet, payload, size)) == OC_STACK_OK) ?
507 OC_EH_OK : OC_EH_ERROR;
513 * The entity handler determines how to process a POST request.
514 * Per the REST paradigm, POST can also be used to update representation of existing
515 * resource or create a new resource.
516 * For pstat, it updates only tm and om.
518 static OCEntityHandlerResult HandlePstatPostRequest(const OCEntityHandlerRequest *ehRequest)
520 OCEntityHandlerResult ehRet = OC_EH_ERROR;
521 OIC_LOG(INFO, TAG, "HandlePstatPostRequest processing POST request");
522 OicSecPstat_t *pstat = NULL;
523 static uint16_t prevMsgId = 0;
525 if (ehRequest->payload && NULL != gPstat)
527 uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData;
528 size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
529 VERIFY_NON_NULL(TAG, payload, ERROR);
531 bool roParsed = false;
532 OCStackResult ret = CBORPayloadToPstatBin(payload, size, &pstat, &roParsed);
533 VERIFY_NON_NULL(TAG, pstat, ERROR);
534 if (OC_STACK_OK == ret)
536 bool validReq = false;
538 if (true == roParsed)
540 OIC_LOG(ERROR, TAG, "Not acceptable request because of read-only properties");
541 ehRet = OC_EH_NOT_ACCEPTABLE;
545 //operation mode(om) should be one of supported modes(sm)
546 for(size_t i = 0; i < gPstat->smLen; i++)
548 if(gPstat->sm[i] == pstat->om)
557 OIC_LOG_V(ERROR, TAG, "%d is unsupported Operation Mode", (int) pstat->om);
558 ehRet = OC_EH_BAD_REQ;
563 //Currently, IoTivity only supports Single Service Client Directed provisioning
564 if (pstat->om == SINGLE_SERVICE_CLIENT_DRIVEN)
566 if ((pstat->cm & RESET) && false == pstat->isOp)
569 OIC_LOG(INFO, TAG, "State changed to Ready for Reset");
571 else if ((pstat->cm & TAKE_OWNER) && false == pstat->isOp)
574 OIC_LOG (INFO, TAG, "State changed to Ready for Ownership transfer");
576 else if (false == (pstat->cm & TAKE_OWNER) && false == pstat->isOp)
579 OIC_LOG(INFO, TAG, "State changed to Ready for Provisioning");
581 else if (false == (pstat->cm & TAKE_OWNER) && true == pstat->isOp)
584 OIC_LOG (INFO, TAG, "State changed to Ready for Normal Operation");
588 OIC_LOG(DEBUG, TAG, "Invalid Device provisionig state");
589 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
590 ehRet = OC_EH_BAD_REQ;
597 OIC_LOG(DEBUG, TAG, "Bad request for PSTAT");
598 ehRet = OC_EH_BAD_REQ;
602 gPstat->isOp = pstat->isOp;
603 gPstat->om = pstat->om;
604 gPstat->tm = pstat->tm;
605 gPstat->cm = pstat->cm;
606 memcpy(&(gPstat->rownerID), &(pstat->rownerID), sizeof(OicUuid_t));
608 // Convert pstat data into CBOR for update to persistent storage
609 if (UpdatePersistentStorage(gPstat))
613 if (true == (pstat->cm & RESET))
615 if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
618 OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
619 DeletePstatBinData(pstat);
622 ret = ResetSecureResourceInPS();
623 if (OC_STACK_OK == ret)
627 DeletePstatBinData(pstat);
634 if(OC_EH_OK != ehRet)
637 * If some error is occured while ownership transfer,
638 * ownership transfer related resource should be revert back to initial status.
640 const OicSecDoxm_t* doxm = GetDoxmResourceData();
643 if(!doxm->owned && prevMsgId != ehRequest->messageID)
645 RestoreDoxmToInitState();
646 RestorePstatToInitState();
651 OIC_LOG(ERROR, TAG, "Invalid DOXM resource.");
656 prevMsgId = ehRequest->messageID;
659 // Send response payload to request originator
660 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
661 OC_EH_OK : OC_EH_ERROR;
663 DeletePstatBinData(pstat);
668 * This internal method is the entity handler for pstat resources.
670 OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
671 OCEntityHandlerRequest * ehRequest,
675 OCEntityHandlerResult ehRet = OC_EH_ERROR;
676 // This method will handle REST request (GET/POST) for /oic/sec/pstat
677 if (flag & OC_REQUEST_FLAG)
679 OIC_LOG(INFO, TAG, "Flag includes OC_REQUEST_FLAG");
680 switch (ehRequest->method)
683 ehRet = HandlePstatGetRequest(ehRequest);
686 ehRet = HandlePstatPostRequest(ehRequest);
689 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
690 OC_EH_OK : OC_EH_ERROR;
698 * This internal method is used to create '/oic/sec/pstat' resource.
700 OCStackResult CreatePstatResource()
702 OCStackResult ret = OCCreateResource(&gPstatHandle,
703 OIC_RSRC_TYPE_SEC_PSTAT,
704 OC_RSRVD_INTERFACE_DEFAULT,
711 if (OC_STACK_OK != ret)
713 OIC_LOG(FATAL, TAG, "Unable to instantiate pstat resource");
714 DeInitPstatResource();
720 * Get the default value.
722 * @return the gDefaultPstat pointer.
724 static OicSecPstat_t* GetPstatDefault()
726 return &gDefaultPstat;
729 OCStackResult InitPstatResource()
731 OCStackResult ret = OC_STACK_ERROR;
733 // Read Pstat resource from PS
734 uint8_t *data = NULL;
736 OicUuid_t emptyUuid = {.id={0}};
737 ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_PSTAT_NAME, &data, &size);
738 // If database read failed
739 if (OC_STACK_OK != ret)
741 OIC_LOG (DEBUG, TAG, "ReadSVDataFromPS failed");
745 // Read ACL resource from PS
746 ret = CBORPayloadToPstat(data, size, &gPstat);
750 * If SVR database in persistent storage got corrupted or
751 * is not available for some reason, a default pstat is created
752 * which allows user to initiate pstat provisioning again.
754 if ((OC_STACK_OK != ret) || !gPstat)
756 gPstat = GetPstatDefault();
758 VERIFY_NON_NULL(TAG, gPstat, FATAL);
760 //In case of Pstat's device id is empty, fill the device id as doxm's device id.
761 if(0 == memcmp(&gPstat->deviceID, &emptyUuid, sizeof(OicUuid_t)))
763 OicUuid_t doxmUuid = {.id={0}};
764 if(OC_STACK_OK == GetDoxmDeviceID(&doxmUuid))
766 memcpy(&gPstat->deviceID, &doxmUuid, sizeof(OicUuid_t));
770 // Instantiate 'oic.sec.pstat'
771 ret = CreatePstatResource();
774 if (OC_STACK_OK != ret)
776 DeInitPstatResource();
781 OCStackResult DeInitPstatResource()
783 if (gPstat != &gDefaultPstat)
785 DeletePstatBinData(gPstat);
788 return OCDeleteResource(gPstatHandle);
792 * Function to restore pstat resurce to initial status.
793 * This function will use in case of error while ownership transfer
795 void RestorePstatToInitState()
799 OIC_LOG(INFO, TAG, "PSTAT resource will revert back to initial status.");
801 gPstat->cm = (OicSecDpm_t)(gPstat->cm | TAKE_OWNER);
802 gPstat->tm = (OicSecDpm_t)(gPstat->tm & (~TAKE_OWNER));
803 gPstat->om = SINGLE_SERVICE_CLIENT_DRIVEN;
804 if(gPstat->sm && 0 < gPstat->smLen)
806 gPstat->sm[0] = SINGLE_SERVICE_CLIENT_DRIVEN;
809 if (!UpdatePersistentStorage(gPstat))
811 OIC_LOG(ERROR, TAG, "Failed to revert PSTAT in persistent storage");
816 OCStackResult SetPstatRownerId(const OicUuid_t* newROwner)
818 OCStackResult ret = OC_STACK_ERROR;
819 uint8_t *cborPayload = NULL;
821 OicUuid_t prevId = {.id={0}};
823 if(NULL == newROwner)
825 ret = OC_STACK_INVALID_PARAM;
829 ret = OC_STACK_NO_RESOURCE;
832 if(newROwner && gPstat)
834 memcpy(prevId.id, gPstat->rownerID.id, sizeof(prevId.id));
835 memcpy(gPstat->rownerID.id, newROwner->id, sizeof(newROwner->id));
837 ret = PstatToCBORPayload(gPstat, &cborPayload, &size, false);
838 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
840 ret = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size);
841 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
843 OICFree(cborPayload);
849 OICFree(cborPayload);
850 memcpy(gPstat->rownerID.id, prevId.id, sizeof(prevId.id));
855 * This function returns the "isop" status of the device.
857 * @return true iff pstat.isop == 1, else false
864 OCStackResult GetPstatRownerId(OicUuid_t *rowneruuid)
866 OCStackResult retVal = OC_STACK_ERROR;
869 *rowneruuid = gPstat->rownerID;
870 retVal = OC_STACK_OK;