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"
35 #include "aclresource.h"
36 #include "credresource.h"
37 #include "ocprovisioningmanager.h"
39 #define TAG "OIC_SRM_PSTAT"
41 /** Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
42 * The value of payload size is increased until reaching below max cbor size. */
43 static const uint16_t CBOR_SIZE = 512;
45 // Max cbor size payload.
46 static const uint16_t CBOR_MAX_SIZE = 4400;
48 // PSTAT Map size - Number of mandatory items
49 static const uint8_t PSTAT_MAP_SIZE = 6;
51 // Number of writeable property
52 static const uint8_t WRITEABLE_PROPERTY_SIZE = 3;
54 static OicSecDpom_t gSm = SINGLE_SERVICE_CLIENT_DRIVEN;
55 static OicSecPstat_t gDefaultPstat =
58 (OicSecDpm_t)(BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
59 PROVISION_CREDENTIALS | PROVISION_ACLS), // OicSecDpm_t cm
60 (OicSecDpm_t)(TAKE_OWNER | BOOTSTRAP_SERVICE | SECURITY_MANAGEMENT_SERVICES |
61 PROVISION_CREDENTIALS | PROVISION_ACLS), // OicSecDpm_t tm
62 {.id = {0}}, // OicUuid_t deviceID
63 SINGLE_SERVICE_CLIENT_DRIVEN, // OicSecDpom_t om */
64 1, // the number of elts in Sms
65 &gSm, // OicSecDpom_t *sm
66 0, // uint16_t commitHash
67 {.id = {0}}, // OicUuid_t rownerID
70 static OicSecPstat_t *gPstat = NULL;
72 static OCResourceHandle gPstatHandle = NULL;
75 * This method is internal method.
76 * the param roParsed is optionally used to know whether cborPayload has
77 * at least read only property value or not.
79 static OCStackResult CBORPayloadToPstatBin(const uint8_t *cborPayload, const size_t size,
80 OicSecPstat_t **secPstat, bool *roParsed);
82 void DeletePstatBinData(OicSecPstat_t* pstat)
86 //Clean 'supported modes' field
94 OCStackResult PstatToCBORPayload(const OicSecPstat_t *pstat, uint8_t **payload, size_t *size,
97 if (NULL == pstat || NULL == payload || NULL != *payload || NULL == size)
99 return OC_STACK_INVALID_PARAM;
102 size_t cborLen = *size;
111 OCStackResult ret = OC_STACK_ERROR;
112 size_t pstatMapSize = PSTAT_MAP_SIZE;
114 CborEncoder pstatMap;
115 char* strUuid = NULL;
117 int64_t cborEncoderResult = CborNoError;
119 uint8_t *outPayload = (uint8_t *)OICCalloc(1, cborLen);
120 VERIFY_NON_NULL(TAG, outPayload, ERROR);
121 cbor_encoder_init(&encoder, outPayload, cborLen, 0);
123 if (false == writableOnly)
125 pstatMapSize += WRITEABLE_PROPERTY_SIZE;
128 cborEncoderResult = cbor_encoder_create_map(&encoder, &pstatMap, pstatMapSize);
129 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pstat Map.");
131 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ISOP_NAME,
132 strlen(OIC_JSON_ISOP_NAME));
133 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ISOP Name Tag.");
134 cborEncoderResult = cbor_encode_boolean(&pstatMap, pstat->isOp);
135 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ISOP Name Value.");
137 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_CM_NAME,
138 strlen(OIC_JSON_CM_NAME));
139 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CM Name Tag.");
140 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->cm);
141 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CM Name Value.");
143 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_TM_NAME,
144 strlen(OIC_JSON_TM_NAME));
145 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding TM Name Tag.");
146 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->tm);
147 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding TM Name Value.");
149 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_OM_NAME,
150 strlen(OIC_JSON_OM_NAME));
151 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding OM Name Tag.");
152 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->om);
153 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding OM Name Value.");
155 if (false == writableOnly)
157 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_SM_NAME,
158 strlen(OIC_JSON_SM_NAME));
159 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Tag.");
160 cborEncoderResult = cbor_encode_int(&pstatMap, pstat->sm[0]);
161 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SM Name Value.");
163 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_DEVICE_ID_NAME,
164 strlen(OIC_JSON_DEVICE_ID_NAME));
165 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Tag.");
166 ret = ConvertUuidToStr(&pstat->deviceID, &strUuid);
167 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
168 cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid));
169 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Id Value.");
173 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_ROWNERID_NAME,
174 strlen(OIC_JSON_ROWNERID_NAME));
175 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Tag.");
176 ret = ConvertUuidToStr(&pstat->rownerID, &strUuid);
177 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret , ERROR);
178 cborEncoderResult = cbor_encode_text_string(&pstatMap, strUuid, strlen(strUuid));
179 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ROwner Id Value.");
186 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_RT_NAME,
187 strlen(OIC_JSON_RT_NAME));
188 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Name Tag.");
189 cborEncoderResult = cbor_encoder_create_array(&pstatMap, &rtArray, 1);
190 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding RT Value.");
191 for (size_t i = 0; i < 1; i++)
193 cborEncoderResult = cbor_encode_text_string(&rtArray, OIC_RSRC_TYPE_SEC_PSTAT,
194 strlen(OIC_RSRC_TYPE_SEC_PSTAT));
195 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding RT Value.");
197 cborEncoderResult = cbor_encoder_close_container(&pstatMap, &rtArray);
198 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing RT.");
202 cborEncoderResult = cbor_encode_text_string(&pstatMap, OIC_JSON_IF_NAME,
203 strlen(OIC_JSON_IF_NAME));
204 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Name Tag.");
205 cborEncoderResult = cbor_encoder_create_array(&pstatMap, &ifArray, 1);
206 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Addding IF Value.");
207 for (size_t i = 0; i < 1; i++)
209 cborEncoderResult = cbor_encode_text_string(&ifArray, OC_RSRVD_INTERFACE_DEFAULT,
210 strlen(OC_RSRVD_INTERFACE_DEFAULT));
211 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding IF Value.");
213 cborEncoderResult = cbor_encoder_close_container(&pstatMap, &ifArray);
214 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing IF.");
216 cborEncoderResult = cbor_encoder_close_container(&encoder, &pstatMap);
217 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Closing PSTAT Map.");
219 if (CborNoError == cborEncoderResult)
221 *size = cbor_encoder_get_buffer_size(&encoder, outPayload);
222 *payload = outPayload;
226 if ((CborErrorOutOfMemory == cborEncoderResult) && (cborLen < CBOR_MAX_SIZE))
228 // reallocate and try again!
231 // Since the allocated initial memory failed, double the memory.
232 cborLen += cbor_encoder_get_buffer_size(&encoder, encoder.end);
233 cborEncoderResult = CborNoError;
234 ret = PstatToCBORPayload(pstat, payload, &cborLen, writableOnly);
235 if (OC_STACK_OK == ret)
241 if ((CborNoError != cborEncoderResult) || (OC_STACK_OK != ret))
247 ret = OC_STACK_ERROR;
253 OCStackResult CBORPayloadToPstat(const uint8_t *cborPayload, const size_t size,
254 OicSecPstat_t **secPstat)
256 return CBORPayloadToPstatBin(cborPayload, size, secPstat, NULL);
259 static OCStackResult CBORPayloadToPstatBin(const uint8_t *cborPayload, const size_t size,
260 OicSecPstat_t **secPstat, bool *roParsed)
262 if (NULL == cborPayload || NULL == secPstat || NULL != *secPstat || 0 == size)
264 return OC_STACK_INVALID_PARAM;
267 OCStackResult ret = OC_STACK_ERROR;
272 CborError cborFindResult = CborNoError;
273 char *strUuid = NULL;
276 cbor_parser_init(cborPayload, size, 0, &parser, &pstatCbor);
277 CborValue pstatMap = { .parser = NULL };
279 OicSecPstat_t *pstat = NULL;
280 cborFindResult = cbor_value_enter_container(&pstatCbor, &pstatMap);
281 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Map.");
283 pstat = (OicSecPstat_t *)OICCalloc(1, sizeof(OicSecPstat_t));
284 VERIFY_NON_NULL(TAG, pstat, ERROR);
287 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ISOP_NAME, &pstatMap);
288 if (CborNoError == cborFindResult && cbor_value_is_boolean(&pstatMap))
290 cborFindResult = cbor_value_get_boolean(&pstatMap, &pstat->isOp);
291 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding isOp Value.");
295 VERIFY_NON_NULL(TAG, gPstat, ERROR);
296 pstat->isOp = gPstat->isOp;
297 cborFindResult = CborNoError;
300 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_DEVICE_ID_NAME, &pstatMap);
301 if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
303 cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
304 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Device Id Value.");
305 ret = ConvertStrToUuid(strUuid , &pstat->deviceID);
306 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
313 VERIFY_NON_NULL(TAG, gPstat, ERROR);
314 memcpy(&pstat->deviceID, &gPstat->deviceID, sizeof(OicUuid_t));
315 cborFindResult = CborNoError;
318 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_CM_NAME, &pstatMap);
319 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
323 cborFindResult = cbor_value_get_int(&pstatMap, &cm);
324 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CM.");
325 pstat->cm = (OicSecDpm_t)cm;
329 VERIFY_NON_NULL(TAG, gPstat, ERROR);
330 pstat->cm = gPstat->cm;
331 cborFindResult = CborNoError;
334 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_TM_NAME, &pstatMap);
335 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
339 cborFindResult = cbor_value_get_int(&pstatMap, &tm);
340 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding TM.");
341 pstat->tm = (OicSecDpm_t)tm;
345 VERIFY_NON_NULL(TAG, gPstat, ERROR);
346 pstat->tm = gPstat->tm;
347 cborFindResult = CborNoError;
350 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_OM_NAME, &pstatMap);
351 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
355 cborFindResult = cbor_value_get_int(&pstatMap, &om);
356 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding OM.");
357 pstat->om = (OicSecDpom_t)om;
361 VERIFY_NON_NULL(TAG, gPstat, ERROR);
362 pstat->om = gPstat->om;
363 cborFindResult = CborNoError;
366 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_SM_NAME, &pstatMap);
367 if (CborNoError == cborFindResult && cbor_value_is_integer(&pstatMap))
372 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
373 VERIFY_NON_NULL(TAG, pstat->sm, ERROR);
374 cborFindResult = cbor_value_get_int(&pstatMap, &sm);
375 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SM.");
376 pstat->sm[0] = (OicSecDpom_t)sm;
385 VERIFY_NON_NULL(TAG, gPstat, ERROR);
386 pstat->smLen = gPstat->smLen;
387 pstat->sm = (OicSecDpom_t*)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
388 VERIFY_NON_NULL(TAG, pstat->sm, ERROR);
389 *pstat->sm = *gPstat->sm;
390 cborFindResult = CborNoError;
393 cborFindResult = cbor_value_map_find_value(&pstatCbor, OIC_JSON_ROWNERID_NAME, &pstatMap);
394 if (CborNoError == cborFindResult && cbor_value_is_text_string(&pstatMap))
396 cborFindResult = cbor_value_dup_text_string(&pstatMap, &strUuid , &len, NULL);
397 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ROwner Id Value.");
398 ret = ConvertStrToUuid(strUuid , &pstat->rownerID);
399 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
405 VERIFY_NON_NULL(TAG, gPstat, ERROR);
406 memcpy(pstat->rownerID.id, gPstat->rownerID.id, sizeof(gPstat->rownerID.id));
407 cborFindResult = CborNoError;
414 if (CborNoError != cborFindResult)
416 OIC_LOG(ERROR, TAG, "CBORPayloadToPstat failed");
417 DeletePstatBinData(pstat);
420 ret = OC_STACK_ERROR;
427 * Function to update persistent storage
429 static bool UpdatePersistentStorage(OicSecPstat_t *pstat)
434 uint8_t *cborPayload = NULL;
435 OCStackResult ret = PstatToCBORPayload(pstat, &cborPayload, &size, false);
436 if (OC_STACK_OK == ret)
438 if (OC_STACK_OK == UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size))
442 OICFree(cborPayload);
448 static bool ValidateQuery(const char * query)
450 OIC_LOG (DEBUG, TAG, "In ValidateQuery");
456 bool bInterfaceQry = false; // does querystring contains 'if' query ?
457 bool bInterfaceMatch = false; // does 'if' query matches with oic.if.baseline ?
459 OicParseQueryIter_t parseIter = {.attrPos = NULL};
461 ParseQueryIterInit((unsigned char*)query, &parseIter);
463 while (GetNextQuery(&parseIter))
465 if (strncasecmp((char *)parseIter.attrPos, OC_RSRVD_INTERFACE, parseIter.attrLen) == 0)
467 bInterfaceQry = true;
468 if ((strncasecmp((char *)parseIter.valPos, OC_RSRVD_INTERFACE_DEFAULT, parseIter.valLen) == 0))
470 bInterfaceMatch = true;
474 return (bInterfaceQry ? bInterfaceMatch: true);
477 #ifdef MULTIPLE_OWNER
478 bool IsValidPstatAccessForSubOwner(const uint8_t *cborPayload, size_t size)
480 OicSecPstat_t* pstat = NULL;
481 bool isValidPstat = true;
483 OIC_LOG_BUFFER(DEBUG, TAG, cborPayload, size);
484 VERIFY_NON_NULL(TAG, cborPayload, ERROR);
485 VERIFY_SUCCESS(TAG, 0 != size, ERROR);
486 VERIFY_SUCCESS(TAG, OC_STACK_OK == CBORPayloadToPstat(cborPayload, size, &pstat), ERROR);
487 VERIFY_NON_NULL(TAG, pstat, ERROR);
489 if (RESET & pstat->cm)
491 OIC_LOG(ERROR, TAG, "SubOwner can't reset the server.");
492 isValidPstat = false;
496 DeletePstatBinData(pstat);
499 #endif //MULTIPLE_OWNER
502 * The entity handler determines how to process a GET request.
504 static OCEntityHandlerResult HandlePstatGetRequest (const OCEntityHandlerRequest * ehRequest)
506 OCEntityHandlerResult ehRet = OC_EH_OK;
508 OIC_LOG(INFO, TAG, "HandlePstatGetRequest processing GET request");
510 //Checking if Get request is a query.
511 if (ehRequest->query)
513 OIC_LOG_V(DEBUG,TAG,"query:%s",ehRequest->query);
514 OIC_LOG(DEBUG, TAG, "HandlePstatGetRequest processing query");
515 if (!ValidateQuery(ehRequest->query))
522 * For GET or Valid Query request return doxm resource CBOR payload.
523 * For non-valid query return NULL json payload.
524 * A device will 'always' have a default Pstat, so PstatToCBORPayload will
525 * return valid pstat resource json.
528 uint8_t *payload = NULL;
529 if (ehRet == OC_EH_OK)
531 if(OC_STACK_OK != PstatToCBORPayload(gPstat, &payload, &size, false))
533 OIC_LOG(WARNING, TAG, "PstatToCBORPayload failed in HandlePstatGetRequest");
537 // Send response payload to request originator
538 ehRet = ((SendSRMResponse(ehRequest, ehRet, payload, size)) == OC_STACK_OK) ?
539 OC_EH_OK : OC_EH_ERROR;
545 * Checks if device can change state to Ready for Normal Operation.
547 static OCEntityHandlerResult ValidateReadyForNOP(const OicSecPstat_t *pstat)
549 OIC_LOG_V(DEBUG, TAG, "%s: IN", __func__);
551 const OicSecDoxm_t *doxm = GetDoxmResourceData();
552 OicUuid_t rowneruuid;
556 OIC_LOG(WARNING, TAG, "DOXM is NULL");
557 return OC_EH_NOT_ACCEPTABLE;
562 OIC_LOG(WARNING, TAG, "Can't change state to Ready for Normal Operation: the device is unowned");
563 return OC_EH_NOT_ACCEPTABLE;
566 if (IsNilUuid(&doxm->owner))
568 OIC_LOG(WARNING, TAG, "Can't change state to Ready for Normal Operation: the device owner is NIL");
569 return OC_EH_INTERNAL_SERVER_ERROR;
572 if (IsNilUuid(&doxm->deviceID))
574 OIC_LOG(WARNING, TAG,
575 "Can't change state to Ready for Normal Operation: the device owner ID is NIL");
576 return OC_EH_INTERNAL_SERVER_ERROR;
579 if (IsNilUuid(&doxm->rownerID))
581 OIC_LOG(WARNING, TAG, "Can't change state to Ready for Normal Operation: the doxm rowner is NIL");
582 return OC_EH_INTERNAL_SERVER_ERROR;
586 if (IsNilUuid(&pstat->rownerID))
588 OIC_LOG(WARNING, TAG, "Can't change state to Ready for Normal Operation: the pstat rowner is NIL");
589 return OC_EH_INTERNAL_SERVER_ERROR;
592 memset(&rowneruuid, 0, sizeof(OicUuid_t));
593 if (OC_STACK_OK != GetAclRownerId(&rowneruuid))
595 OIC_LOG(WARNING, TAG, "Can't change state to Ready for Normal Operation: can't get acl");
596 return OC_EH_INTERNAL_SERVER_ERROR;
599 if (IsNilUuid(&rowneruuid))
601 OIC_LOG(WARNING, TAG, "Can't change state to Ready for Normal Operation: the acl rowner is NIL");
602 return OC_EH_INTERNAL_SERVER_ERROR;
605 memset(&rowneruuid, 0, sizeof(OicUuid_t));
606 if (OC_STACK_OK != GetCredRownerId(&rowneruuid))
608 OIC_LOG(WARNING, TAG, "Can't change state to Ready for Normal Operation: can't get cred");
609 return OC_EH_INTERNAL_SERVER_ERROR;
612 if (IsNilUuid(&rowneruuid))
614 OIC_LOG(WARNING, TAG, "Can't change state to Ready for Normal Operation: the cred rowner is NIL");
615 return OC_EH_INTERNAL_SERVER_ERROR;
618 OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
625 * The entity handler determines how to process a POST request.
626 * Per the REST paradigm, POST can also be used to update representation of existing
627 * resource or create a new resource.
628 * For pstat, it updates only tm and om.
630 static OCEntityHandlerResult HandlePstatPostRequest(OCEntityHandlerRequest *ehRequest)
632 OCEntityHandlerResult ehRet = OC_EH_ERROR;
633 OIC_LOG(INFO, TAG, "HandlePstatPostRequest processing POST request");
634 OicSecPstat_t *pstat = NULL;
635 static uint16_t previousMsgId = 0;
636 bool isDuplicatedMsg = false;
638 if (ehRequest->payload && NULL != gPstat)
640 uint8_t *payload = ((OCSecurityPayload *) ehRequest->payload)->securityData;
641 size_t size = ((OCSecurityPayload *) ehRequest->payload)->payloadSize;
642 VERIFY_NON_NULL(TAG, payload, ERROR);
644 bool roParsed = false;
645 OCStackResult ret = CBORPayloadToPstatBin(payload, size, &pstat, &roParsed);
646 VERIFY_NON_NULL(TAG, pstat, ERROR);
647 if (OC_STACK_OK == ret)
649 bool validReq = false;
652 * message ID is supported for CoAP over UDP only according to RFC 7252
653 * So we should check message ID to prevent duplicate request handling in case of OC_ADAPTER_IP.
654 * In case of other transport adapter, duplicate message check is not required.
656 if (OC_ADAPTER_IP == ehRequest->devAddr.adapter &&
657 previousMsgId == ehRequest->messageID)
659 isDuplicatedMsg = true;
662 if (true == roParsed)
664 OIC_LOG(ERROR, TAG, "Not acceptable request because of read-only properties");
665 ehRet = OC_EH_NOT_ACCEPTABLE;
669 //operation mode(om) should be one of supported modes(sm)
670 for(size_t i = 0; i < gPstat->smLen; i++)
672 if(gPstat->sm[i] == pstat->om)
681 OIC_LOG_V(ERROR, TAG, "%d is unsupported Operation Mode", (int) pstat->om);
682 ehRet = OC_EH_BAD_REQ;
687 //Currently, IoTivity only supports Single Service Client Directed provisioning
688 if (pstat->om == SINGLE_SERVICE_CLIENT_DRIVEN)
690 if ((pstat->cm & RESET) && false == pstat->isOp)
693 OIC_LOG(INFO, TAG, "State changed to Ready for Reset");
695 else if ((pstat->cm & TAKE_OWNER) && false == pstat->isOp)
698 OIC_LOG (INFO, TAG, "State changed to Ready for Ownership transfer");
700 else if (false == (pstat->cm & TAKE_OWNER) && false == pstat->isOp)
703 OIC_LOG(INFO, TAG, "State changed to Ready for Provisioning");
705 else if (false == (pstat->cm & TAKE_OWNER) && true == pstat->isOp)
707 ehRet = ValidateReadyForNOP(pstat);
708 if(OC_EH_OK != ehRet)
713 OIC_LOG (INFO, TAG, "State changed to Ready for Normal Operation");
717 OIC_LOG(DEBUG, TAG, "Invalid Device provisionig state");
718 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
719 ehRet = OC_EH_BAD_REQ;
726 OIC_LOG(DEBUG, TAG, "Bad request for PSTAT");
727 ehRet = OC_EH_BAD_REQ;
731 gPstat->isOp = pstat->isOp;
732 gPstat->om = pstat->om;
733 gPstat->tm = pstat->tm;
734 gPstat->cm = pstat->cm;
735 memcpy(&(gPstat->deviceID), &(pstat->deviceID), sizeof(OicUuid_t));
736 memcpy(&(gPstat->rownerID), &(pstat->rownerID), sizeof(OicUuid_t));
738 // Convert pstat data into CBOR for update to persistent storage
739 if (UpdatePersistentStorage(gPstat))
743 if (true == (pstat->cm & RESET))
745 if (OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL, 0))
748 OIC_LOG(ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
749 DeletePstatBinData(pstat);
752 ret = ResetSecureResourceInPS();
753 if (OC_STACK_OK == ret)
757 DeletePstatBinData(pstat);
764 if(OC_EH_OK != ehRet)
767 * If some error is occured while ownership transfer,
768 * ownership transfer related resource should be revert back to initial status.
770 const OicSecDoxm_t* doxm = GetDoxmResourceData();
771 if(doxm && !doxm->owned)
773 OIC_LOG(WARNING, TAG, "The operation failed during handle DOXM request");
775 if (!isDuplicatedMsg)
777 #if defined (__WITH_TLS__) || defined(__WITH_DTLS__)
778 InvokeOtmEventHandler(ehRequest->devAddr.addr, ehRequest->devAddr.port,
779 NULL, OIC_OTM_ERROR);
781 ResetSecureResourceInPS();
782 OIC_LOG(INFO, TAG, "DOXM will be reverted.");
787 OIC_LOG(ERROR, TAG, "Invalid DOXM resource.");
788 ResetSecureResourceInPS();
793 if(ehRequest->devAddr.adapter == OC_ADAPTER_IP)
795 previousMsgId = ehRequest->messageID;
799 // Send response payload to request originator
800 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
801 OC_EH_OK : OC_EH_ERROR;
803 DeletePstatBinData(pstat);
808 * This internal method is the entity handler for pstat resources.
810 OCEntityHandlerResult PstatEntityHandler(OCEntityHandlerFlag flag,
811 OCEntityHandlerRequest * ehRequest,
815 OCEntityHandlerResult ehRet = OC_EH_ERROR;
816 // This method will handle REST request (GET/POST) for /oic/sec/pstat
817 if (flag & OC_REQUEST_FLAG)
819 OIC_LOG(INFO, TAG, "Flag includes OC_REQUEST_FLAG");
820 switch (ehRequest->method)
823 ehRet = HandlePstatGetRequest(ehRequest);
826 ehRet = HandlePstatPostRequest(ehRequest);
829 ehRet = ((SendSRMResponse(ehRequest, ehRet, NULL, 0)) == OC_STACK_OK) ?
830 OC_EH_OK : OC_EH_ERROR;
838 * This internal method is used to create '/oic/sec/pstat' resource.
840 OCStackResult CreatePstatResource()
842 OCStackResult ret = OCCreateResource(&gPstatHandle,
843 OIC_RSRC_TYPE_SEC_PSTAT,
844 OC_RSRVD_INTERFACE_DEFAULT,
851 if (OC_STACK_OK != ret)
853 OIC_LOG(FATAL, TAG, "Unable to instantiate pstat resource");
854 DeInitPstatResource();
860 * Get the default value.
862 * @return the gDefaultPstat pointer.
864 static OicSecPstat_t* GetPstatDefault()
866 return &gDefaultPstat;
869 OCStackResult InitPstatResource()
871 OCStackResult ret = OC_STACK_ERROR;
873 // Read Pstat resource from PS
874 uint8_t *data = NULL;
876 OicUuid_t emptyUuid = {.id={0}};
877 ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_PSTAT_NAME, &data, &size);
878 // If database read failed
879 if (OC_STACK_OK != ret)
881 OIC_LOG (DEBUG, TAG, "ReadSVDataFromPS failed");
885 // Read ACL resource from PS
886 ret = CBORPayloadToPstat(data, size, &gPstat);
890 * If SVR database in persistent storage got corrupted or
891 * is not available for some reason, a default pstat is created
892 * which allows user to initiate pstat provisioning again.
894 if ((OC_STACK_OK != ret) || !gPstat)
896 gPstat = GetPstatDefault();
898 VERIFY_NON_NULL(TAG, gPstat, FATAL);
900 //In case of Pstat's device id is empty, fill the device id as doxm's device id.
901 if(0 == memcmp(&gPstat->deviceID, &emptyUuid, sizeof(OicUuid_t)))
903 OicUuid_t doxmUuid = {.id={0}};
904 if(OC_STACK_OK == GetDoxmDeviceID(&doxmUuid))
906 memcpy(&gPstat->deviceID, &doxmUuid, sizeof(OicUuid_t));
910 // Instantiate 'oic.sec.pstat'
911 ret = CreatePstatResource();
914 if (OC_STACK_OK != ret)
916 DeInitPstatResource();
921 OCStackResult DeInitPstatResource()
923 if (gPstat != &gDefaultPstat)
925 DeletePstatBinData(gPstat);
928 return OCDeleteResource(gPstatHandle);
932 * Function to restore pstat resurce to initial status.
933 * This function will use in case of error while ownership transfer
935 void RestorePstatToInitState()
939 OIC_LOG(INFO, TAG, "PSTAT resource will revert back to initial status.");
941 gPstat->cm = (OicSecDpm_t)(gPstat->cm | TAKE_OWNER);
942 gPstat->tm = (OicSecDpm_t)(gPstat->tm & (~TAKE_OWNER));
943 gPstat->om = SINGLE_SERVICE_CLIENT_DRIVEN;
944 if(gPstat->sm && 0 < gPstat->smLen)
946 gPstat->sm[0] = SINGLE_SERVICE_CLIENT_DRIVEN;
949 if (!UpdatePersistentStorage(gPstat))
951 OIC_LOG(ERROR, TAG, "Failed to revert PSTAT in persistent storage");
956 OCStackResult SetPstatRownerId(const OicUuid_t* newROwner)
958 OCStackResult ret = OC_STACK_ERROR;
959 uint8_t *cborPayload = NULL;
961 OicUuid_t prevId = {.id={0}};
963 if(NULL == newROwner)
965 ret = OC_STACK_INVALID_PARAM;
969 ret = OC_STACK_NO_RESOURCE;
972 if(newROwner && gPstat)
974 memcpy(prevId.id, gPstat->rownerID.id, sizeof(prevId.id));
975 memcpy(gPstat->rownerID.id, newROwner->id, sizeof(newROwner->id));
977 ret = PstatToCBORPayload(gPstat, &cborPayload, &size, false);
978 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
980 ret = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size);
981 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
983 OICFree(cborPayload);
989 OICFree(cborPayload);
990 memcpy(gPstat->rownerID.id, prevId.id, sizeof(prevId.id));
995 * This function returns the "isop" status of the device.
997 * @return true iff pstat.isop == 1, else false
1003 return gPstat->isOp;
1007 //In case of gPstat is NULL
1012 OCStackResult GetPstatRownerId(OicUuid_t *rowneruuid)
1014 OCStackResult retVal = OC_STACK_ERROR;
1017 *rowneruuid = gPstat->rownerID;
1018 retVal = OC_STACK_OK;
1023 OCStackResult SetPstatSelfOwnership(const OicUuid_t* newROwner)
1025 OCStackResult ret = OC_STACK_ERROR;
1026 uint8_t *cborPayload = NULL;
1031 ret = OC_STACK_NO_RESOURCE;
1035 if( newROwner && (false == gPstat->isOp) && (true == (TAKE_OWNER && gPstat->cm)) )
1037 gPstat->cm = (OicSecDpm_t)(gPstat->cm & (~TAKE_OWNER));
1038 gPstat->isOp = true;
1040 memcpy(gPstat->deviceID.id, newROwner->id, sizeof(newROwner->id));
1041 memcpy(gPstat->rownerID.id, newROwner->id, sizeof(newROwner->id));
1043 ret = PstatToCBORPayload(gPstat, &cborPayload, &size, false);
1044 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1046 ret = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, cborPayload, size);
1047 VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
1049 OICFree(cborPayload);
1053 OIC_LOG(ERROR, TAG, "The state of PSTAT is not Ready For OTM");
1059 OICFree(cborPayload);