-static OicSecSvc_t* JSONToSvcBin(const char * jsonStr)
-{
- OCStackResult ret = OC_STACK_ERROR;
- OicSecSvc_t * headSvc = NULL;
- OicSecSvc_t * prevSvc = NULL;
- cJSON *jsonRoot = NULL;
- cJSON *jsonSvcArray = NULL;
-
- VERIFY_NON_NULL(TAG, jsonStr, ERROR);
-
- jsonRoot = cJSON_Parse(jsonStr);
- VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
-
- jsonSvcArray = cJSON_GetObjectItem(jsonRoot, OIC_JSON_SVC_NAME);
- VERIFY_NON_NULL(TAG, jsonSvcArray, INFO);
-
- if (cJSON_Array == jsonSvcArray->type)
- {
- int numSvc = cJSON_GetArraySize(jsonSvcArray);
- int idx = 0;
-
- VERIFY_SUCCESS(TAG, numSvc > 0, INFO);
- do
- {
- cJSON *jsonSvc = cJSON_GetArrayItem(jsonSvcArray, idx);
- VERIFY_NON_NULL(TAG, jsonSvc, ERROR);
-
- OicSecSvc_t *svc = (OicSecSvc_t*)OICCalloc(1, sizeof(OicSecSvc_t));
- VERIFY_NON_NULL(TAG, svc, ERROR);
-
- headSvc = (headSvc) ? headSvc : svc;
- if (prevSvc)
- {
- prevSvc->next = svc;
- }
-
- cJSON *jsonObj = NULL;
- unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
- uint32_t outLen = 0;
- B64Result b64Ret = B64_OK;
-
- // Service Device Identity
- jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_DEVICE_ID);
- VERIFY_NON_NULL(TAG, jsonObj, ERROR);
- VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
- outLen = 0;
- b64Ret = b64Decode(jsonObj->valuestring, strlen(jsonObj->valuestring), base64Buff,
- sizeof(base64Buff), &outLen);
- VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->svcdid.id)), ERROR);
- memcpy(svc->svcdid.id, base64Buff, outLen);
-
- // Service Type
- jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_SERVICE_TYPE);
- VERIFY_NON_NULL(TAG, jsonObj, ERROR);
- VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
- svc->svct = (OicSecSvcType_t)jsonObj->valueint;
-
- // Resource Owners
- jsonObj = cJSON_GetObjectItem(jsonSvc, OIC_JSON_OWNERS_NAME);
- VERIFY_NON_NULL(TAG, jsonObj, ERROR);
- VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
-
- svc->ownersLen = (size_t)cJSON_GetArraySize(jsonObj);
- VERIFY_SUCCESS(TAG, svc->ownersLen > 0, ERROR);
- svc->owners = (OicUuid_t*)OICCalloc(svc->ownersLen, sizeof(OicUuid_t));
- VERIFY_NON_NULL(TAG, (svc->owners), ERROR);
-
- size_t idxx = 0;
- do
- {
- cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
- VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
- VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
- outLen = 0;
- b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
- sizeof(base64Buff), &outLen);
-
- VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof(svc->owners[idxx].id)),
- ERROR);
- memcpy(svc->owners[idxx].id, base64Buff, outLen);
- } while ( ++idxx < svc->ownersLen);
-
- prevSvc = svc;
- } while( ++idx < numSvc);
- }
-
- ret = OC_STACK_OK;
-
-exit:
- cJSON_Delete(jsonRoot);
- if (OC_STACK_OK != ret)
- {
- DeleteSVCList(headSvc);
- headSvc = NULL;
- }
- return headSvc;
-}
-