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);
307 memcpy(&pstat->deviceID, &gPstat->deviceID, sizeof(OicUuid_t));
308 cborFindResult = CborNoError;
311 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_CM_NAME, &pstatMap);
312 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
316 cborFindResult = cbor_value_get_int(&pstatMap, &cm);
317 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CM.");
318 pstat->cm = (OicSecDpm_t)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))
331 cborFindResult = cbor_value_get_int(&pstatMap, &tm);
332 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding TM.");
333 pstat->tm = (OicSecDpm_t)tm;
337 pstat->tm = gPstat->tm;
338 cborFindResult = CborNoError;
341 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_OM_NAME, &pstatMap);
342 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
346 cborFindResult = cbor_value_get_int(&pstatMap, &om);
347 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding OM.");
348 pstat->om = (OicSecDpom_t)om;
352 pstat->om = gPstat->om;
353 cborFindResult = CborNoError;
356 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_SM_NAME, &pstatMap);
357 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
362 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
363 cborFindResult = cbor_value_get_int(&pstatMap, &sm);
364 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM.");
365 pstat->sm[0] = (OicSecDpom_t)sm;
374 VERIFY_NON_NULL(TAG, gPstat, ERROR);
375 pstat->smLen = gPstat->smLen;
376 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
377 *pstat->sm = *gPstat->sm;
378 cborFindResult = CborNoError;
381 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ROWNERID_NAME, &pstatMap);
382 if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
384 cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
385 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Id Value.");
386 ret = ConvertStrToUuid(strUuid , &pstat->rownerID);
387 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
393 VERIFY_NON_NULL(TAG, gPstat, ERROR);
394 memcpy(pstat->rownerID.id, gPstat->rownerID.id, sizeof(gPstat->rownerID.id));
395 cborFindResult = CborNoError;
402 if (CborNoError != cborFindResult)
404 OIC_LOG(ERROR, TAG, "CBORPayloadToPstat failed");
405 DeletePstatBinData(pstat);
408 ret = OC_STACK_ERROR;
415 * Function to update persistent storage
417 static bool UpdatePersistentStorage(OicSecPstat_t *pstat)
422 uint8_t *cborPayload = NULL;
423 OCStackResult ret = PstatToCBORPayload(pstat, &cborPayload, &size, false);
424 if (OC_STACK_OK == ret)
426 if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size))
430 OICFree(cborPayload);
436 static bool ValidateQuery(const char * query)
438 OIC_LOG (DEBUG, TAG, "In ValidateQuery");
444 bool bInterfaceQry = false; // does querystring contains 'if' query ?
445 bool bInterfaceMatch = false; // does 'if' query matches with oic.if.baseline ?
447 OicParseQueryIter_t parseIter = {.attrPos = NULL};
449 ParseQueryIterInit((unsigned char*)query, &parseIter);
451 while (GetNextQuery(&parseIter))
453 if (strncasecmp((char *)parseIter.attrPos, OC_RSRVD_INTERFACE, parseIter.attrLen) == 0)
455 bInterfaceQry = true;
456 if ((strncasecmp((char *)parseIter.valPos, OC_RSRVD_INTERFACE_DEFAULT, parseIter.valLen) == 0))
458 bInterfaceMatch = true;
462 return (bInterfaceQry ? bInterfaceMatch: true);
466 * The entity handler determines how to process a GET request.
468 static OCEntityHandlerResult HandlePstatGetRequest (const OCEntityHandlerRequest * ehRequest)
470 OCEntityHandlerResult ehRet = OC_EH_OK;
472 OIC_LOG(INFO, TAG, "HandlePstatGetRequest processing GET request");
474 //Checking if Get request is a query.
475 if (ehRequest->query)
477 OIC_LOG_V(DEBUG,TAG,"query:%s",ehRequest->query);
478 OIC_LOG(DEBUG, TAG, "HandlePstatGetRequest processing query");
479 if (!ValidateQuery(ehRequest->query))
486 * For GET or Valid Query request return doxm resource CBOR payload.
487 * For non-valid query return NULL json payload.
488 * A device will 'always' have a default Pstat, so PstatToCBORPayload will
489 * return valid pstat resource json.
492 uint8_t *payload = NULL;
493 if (ehRet == OC_EH_OK)
495 if(OC_STACK_OK != PstatToCBORPayload(gPstat, &payload, &size, false))
497 OIC_LOG(WARNING, TAG, "PstatToCBORPayload failed in HandlePstatGetRequest");
501 // Send response payload to request originator
502 ehRet = ((SendSRMResponse(ehRequest, ehRet, payload, size)) == OC_STACK_OK) ?
503 OC_EH_OK : OC_EH_ERROR;
509 * The entity handler determines how to process a POST request.
510 * Per the REST paradigm, POST can also be used to update representation of existing
511 * resource or create a new resource.
512 * For pstat, it updates only tm and om.
514 static OCEntityHandlerResult HandlePstatPostRequest(const OCEntityHandlerRequest *ehRequest)
516 OCEntityHandlerResult ehRet = OC_EH_ERROR;
517 OIC_LOG(INFO, TAG, "HandlePstatPostRequest processing POST request");
518 OicSecPstat_t *pstat = NULL;
519 static uint16_t prevMsgId = 0;
521 if (ehRequest->payload && NULL != gPstat)
523 uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData;
524 size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
525 VERIFY_NON_NULL(TAG, payload, ERROR);
527 bool roParsed = false;
528 OCStackResult ret = CBORPayloadToPstatBin(payload, size, &pstat, &roParsed);
529 VERIFY_NON_NULL(TAG, pstat, ERROR);
530 if (OC_STACK_OK == ret)
532 bool validReq = false;
534 if (true == roParsed)
536 OIC_LOG(ERROR, TAG, "Not acceptable request because of read-only properties");
537 ehRet = OC_EH_NOT_ACCEPTABLE;
541 //operation mode(om) should be one of supported modes(sm)
542 for(size_t i = 0; i < gPstat->smLen; i++)
544 if(gPstat->sm[i] == pstat->om)
553 OIC_LOG_V(ERROR, TAG, "%d is unsupported Operation Mode", (int) pstat->om);
554 ehRet = OC_EH_BAD_REQ;
559 //Currently, IoTivity only supports Single Service Client Directed provisioning
560 if (pstat->om == SINGLE_SERVICE_CLIENT_DRIVEN)
562 if ((pstat->cm & RESET) && false == pstat->isOp)
565 OIC_LOG(INFO, TAG, "State changed to Ready for Reset");
567 else if ((pstat->cm & TAKE_OWNER) && false == pstat->isOp)
570 OIC_LOG (INFO, TAG, "State changed to Ready for Ownership transfer");
572 else if (false == (pstat->cm & TAKE_OWNER) && false == pstat->isOp)
575 OIC_LOG(INFO, TAG, "State changed to Ready for Provisioning");
577 else if (false == (pstat->cm & TAKE_OWNER) && true == pstat->isOp)
580 OIC_LOG (INFO, TAG, "State changed to Ready for Normal Operation");
584 OIC_LOG(DEBUG, TAG, "Invalid Device provisionig state");
585 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
586 ehRet = OC_EH_BAD_REQ;
593 OIC_LOG(DEBUG, TAG, "Bad request for PSTAT");
594 ehRet = OC_EH_BAD_REQ;
598 gPstat->isOp = pstat->isOp;
599 gPstat->om = pstat->om;
600 gPstat->tm = pstat->tm;
601 gPstat->cm = pstat->cm;
602 memcpy(&(gPstat->deviceID), &(pstat->deviceID), sizeof(OicUuid_t));
603 memcpy(&(gPstat->rownerID), &(pstat->rownerID), sizeof(OicUuid_t));
605 // Convert pstat data into CBOR for update to persistent storage
606 if (UpdatePersistentStorage(gPstat))
610 if (true == (pstat->cm & RESET))
612 if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
615 OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
616 DeletePstatBinData(pstat);
619 ret = ResetSecureResourceInPS();
620 if (OC_STACK_OK == ret)
624 DeletePstatBinData(pstat);
631 if(OC_EH_OK != ehRet)
634 * If some error is occured while ownership transfer,
635 * ownership transfer related resource should be revert back to initial status.
637 const OicSecDoxm_t* doxm = GetDoxmResourceData();
640 if(!doxm->owned && prevMsgId != ehRequest->messageID)
642 RestoreDoxmToInitState();
643 RestorePstatToInitState();
648 OIC_LOG(ERROR, TAG, "Invalid DOXM resource.");
653 prevMsgId = ehRequest->messageID;
656 // Send response payload to request originator
657 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
658 OC_EH_OK : OC_EH_ERROR;
660 DeletePstatBinData(pstat);
665 * This internal method is the entity handler for pstat resources.
667 OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
668 OCEntityHandlerRequest * ehRequest,
672 OCEntityHandlerResult ehRet = OC_EH_ERROR;
673 // This method will handle REST request (GET/POST) for /oic/sec/pstat
674 if (flag & OC_REQUEST_FLAG)
676 OIC_LOG(INFO, TAG, "Flag includes OC_REQUEST_FLAG");
677 switch (ehRequest->method)
680 ehRet = HandlePstatGetRequest(ehRequest);
683 ehRet = HandlePstatPostRequest(ehRequest);
686 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
687 OC_EH_OK : OC_EH_ERROR;
695 * This internal method is used to create '/oic/sec/pstat' resource.
697 OCStackResult CreatePstatResource()
699 OCStackResult ret = OCCreateResource(&gPstatHandle,
700 OIC_RSRC_TYPE_SEC_PSTAT,
701 OC_RSRVD_INTERFACE_DEFAULT,
708 if (OC_STACK_OK != ret)
710 OIC_LOG(FATAL, TAG, "Unable to instantiate pstat resource");
711 DeInitPstatResource();
717 * Get the default value.
719 * @return the gDefaultPstat pointer.
721 static OicSecPstat_t* GetPstatDefault()
723 return &gDefaultPstat;
726 OCStackResult InitPstatResource()
728 OCStackResult ret = OC_STACK_ERROR;
730 // Read Pstat resource from PS
731 uint8_t *data = NULL;
733 OicUuid_t emptyUuid = {.id={0}};
734 ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_PSTAT_NAME, &data, &size);
735 // If database read failed
736 if (OC_STACK_OK != ret)
738 OIC_LOG (DEBUG, TAG, "ReadSVDataFromPS failed");
742 // Read ACL resource from PS
743 ret = CBORPayloadToPstat(data, size, &gPstat);
747 * If SVR database in persistent storage got corrupted or
748 * is not available for some reason, a default pstat is created
749 * which allows user to initiate pstat provisioning again.
751 if ((OC_STACK_OK != ret) || !gPstat)
753 gPstat = GetPstatDefault();
755 VERIFY_NON_NULL(TAG, gPstat, FATAL);
757 //In case of Pstat's device id is empty, fill the device id as doxm's device id.
758 if(0 == memcmp(&gPstat->deviceID, &emptyUuid, sizeof(OicUuid_t)))
760 OicUuid_t doxmUuid = {.id={0}};
761 if(OC_STACK_OK == GetDoxmDeviceID(&doxmUuid))
763 memcpy(&gPstat->deviceID, &doxmUuid, sizeof(OicUuid_t));
767 // Instantiate 'oic.sec.pstat'
768 ret = CreatePstatResource();
771 if (OC_STACK_OK != ret)
773 DeInitPstatResource();
778 OCStackResult DeInitPstatResource()
780 if (gPstat != &gDefaultPstat)
782 DeletePstatBinData(gPstat);
785 return OCDeleteResource(gPstatHandle);
789 * Function to restore pstat resurce to initial status.
790 * This function will use in case of error while ownership transfer
792 void RestorePstatToInitState()
796 OIC_LOG(INFO, TAG, "PSTAT resource will revert back to initial status.");
798 gPstat->cm = (OicSecDpm_t)(gPstat->cm | TAKE_OWNER);
799 gPstat->tm = (OicSecDpm_t)(gPstat->tm & (~TAKE_OWNER));
800 gPstat->om = SINGLE_SERVICE_CLIENT_DRIVEN;
801 if(gPstat->sm && 0 < gPstat->smLen)
803 gPstat->sm[0] = SINGLE_SERVICE_CLIENT_DRIVEN;
806 if (!UpdatePersistentStorage(gPstat))
808 OIC_LOG(ERROR, TAG, "Failed to revert PSTAT in persistent storage");
813 OCStackResult SetPstatRownerId(const OicUuid_t* newROwner)
815 OCStackResult ret = OC_STACK_ERROR;
816 uint8_t *cborPayload = NULL;
818 OicUuid_t prevId = {.id={0}};
820 if(NULL == newROwner)
822 ret = OC_STACK_INVALID_PARAM;
826 ret = OC_STACK_NO_RESOURCE;
829 if(newROwner && gPstat)
831 memcpy(prevId.id, gPstat->rownerID.id, sizeof(prevId.id));
832 memcpy(gPstat->rownerID.id, newROwner->id, sizeof(newROwner->id));
834 ret = PstatToCBORPayload(gPstat, &cborPayload, &size, false);
835 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
837 ret = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size);
838 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
840 OICFree(cborPayload);
846 OICFree(cborPayload);
847 memcpy(gPstat->rownerID.id, prevId.id, sizeof(prevId.id));
852 * This function returns the "isop" status of the device.
854 * @return true iff pstat.isop == 1, else false
861 OCStackResult GetPstatRownerId(OicUuid_t *rowneruuid)
863 OCStackResult retVal = OC_STACK_ERROR;
866 *rowneruuid = gPstat->rownerID;
867 retVal = OC_STACK_OK;