OCStackResult OCCloudSignup(const char *host, const char *deviceId, const char *authprovider, const char *authcode, OCClientResponseHandler response) { char targetUri[MAX_URI_LENGTH * 2] = { 0, }; snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, DEFAULT_AUTH_SIGNUP); OCCallbackData cbData; memset(&cbData, 0, sizeof(OCCallbackData)); cbData.cb = response; cbData.cd = NULL; cbData.context = (void *)DEFAULT_CONTEXT_VALUE; printf("Host: %s\n", targetUri); printf("Dev id: %s\n", deviceId); printf("Auth Provider: %s\n", authprovider); printf("Auth Code: %s\n", authcode); OCRepPayload *registerPayload = OCRepPayloadCreate(); OCRepPayloadSetPropString(registerPayload, "di", deviceId); OCRepPayloadSetPropString(registerPayload, "authprovider", authprovider); OCRepPayloadSetPropString(registerPayload, "authcode", authcode); return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)registerPayload, CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0); }
OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest) { OCRepPayload* payload = OCRepPayloadCreate(); if (!payload) { OC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload"); return NULL; } if (ehRequest->resource == g_prov.handle) { OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV); OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS, g_prov.ps); OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, g_prov.tnt); OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, g_prov.tnn); OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, g_prov.cd); } else if (ehRequest->requestHandle == g_net.handle) { OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET); OCRepPayloadSetPropInt(payload, "ant", g_net.ant[0]); } return payload; }
OCStackResult OCCloudSession(const char *host, const char *query, const char *uId, const char *deviceId, const char *accesstoken, bool isLogin, OCClientResponseHandler response) { char targetUri[MAX_URI_LENGTH * 2] = { 0, }; snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, query); OCCallbackData cbData; memset(&cbData, 0, sizeof(OCCallbackData)); cbData.cb = response; cbData.cd = NULL; cbData.context = (void *)DEFAULT_CONTEXT_VALUE; OCRepPayload *loginoutPayload = OCRepPayloadCreate(); if (uId != NULL) { OCRepPayloadSetPropString(loginoutPayload, "uid", uId); } if (deviceId != NULL) { OCRepPayloadSetPropString(loginoutPayload, "di", deviceId); } if (accesstoken != NULL) { OCRepPayloadSetPropString(loginoutPayload, "accesstoken", accesstoken); } OCRepPayloadSetPropBool(loginoutPayload, "login", isLogin); return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)loginoutPayload, CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0); }
OCEntityHandlerResult LcdOCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void *callbackParam) { OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = {0}; OCRepPayload* payload = OCRepPayloadCreate(); if(!payload) { OC_LOG(ERROR, TAG, ("Failed to allocate Payload")); return OC_EH_ERROR; } if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { OC_LOG (INFO, TAG, ("Flag includes OC_REQUEST_FLAG")); if(OC_REST_GET == entityHandlerRequest->method) { OCRepPayloadSetUri(payload, "/grove/lcd"); OCRepPayloadSetPropString(payload, "lcd", (const char *)lcd.str); } else if(OC_REST_PUT == entityHandlerRequest->method) { OC_LOG(INFO, TAG, ("PUT request")); OCRepPayload *rep = (OCRepPayload *)entityHandlerRequest->payload; OCRepPayloadGetPropString(rep, "lcd", &lcd.str); OC_LOG_V(INFO, TAG, "LCD string: %s", lcd.str); lcd_put(); OCRepPayloadSetPropString(payload, "lcd", (const char *)lcd.str); } if (ehRet == OC_EH_OK) { // Format the response. Note this requires some info about the request response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; response.ehResult = ehRet; response.payload = (OCPayload*) payload; response.numSendVendorSpecificHeaderOptions = 0; memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions); memset(response.resourceUri, 0, sizeof response.resourceUri); // Indicate that response is NOT in a persistent buffer response.persistentBufferFlag = 0; // Send the response if (OCDoResponse(&response) != OC_STACK_OK) { OC_LOG(ERROR, TAG, "Error sending response"); ehRet = OC_EH_ERROR; } } } if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG)) { if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, ("Received OC_OBSERVE_REGISTER from client")); gLightUnderObservation = 1; } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, ("Received OC_OBSERVE_DEREGISTER from client")); gLightUnderObservation = 0; } } OCRepPayloadDestroy(payload); return ehRet; }
void things_set_value(struct things_representation_s *rep, char *key, char *value) { #ifdef __ST_THINGS_RTOS__ if (value != NULL && key != NULL) { OCRepPayloadSetPropString(rep->payload, key, value); } #else OCRepPayloadSetPropString(rep->payload, key, value); #endif }
/** * @brief Called when a REST GET is request * * @param OCBaseResource base resource attributes * * @return result of the entityHandler */ OCEntityHandlerResult getRequest(OCBaseResourceT *resource, OCRepPayload *payload) { // Get the attributes and set them. OCAttributeT *current = resource->attribute; while(current != NULL) { // Check type switch(current->value.dataType) { case INT: OCRepPayloadSetPropInt(payload, current->name, current->value.data.i);// *((int*)current->value.data)); break; case DOUBLE: OCRepPayloadSetPropDouble(payload, current->name, current->value.data.d);// *((double*)current->value.data)); break; case STRING: OCRepPayloadSetPropString(payload, current->name, current->value.data.str);// *((char**)current->value.data)); break; case BOOL: OCRepPayloadSetPropBool(payload, current->name, current->value.data.b);// *((bool*)current->value.data)); break; } current = current->next; } return OC_EH_OK; }
NSResult NSSetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload) { NS_LOG(DEBUG, "NSSetMessagePayload - IN"); *msgPayload = msg->extraInfo != NULL ? msg->extraInfo : OCRepPayloadCreate(); if (!*msgPayload) { NS_LOG(ERROR, "Failed to allocate payload"); return NS_ERROR; } OCRepPayloadSetUri(*msgPayload, NS_COLLECTION_MESSAGE_URI); OCRepPayloadSetPropInt(*msgPayload, NS_ATTRIBUTE_MESSAGE_ID, msg->messageId); OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_PROVIDER_ID, msg->providerId); NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_TYPE, msg->type); NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_TTL, msg->ttl); NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_DATETIME, msg->dateTime); NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TITLE, msg->title); NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TEXT, msg->contentText); NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_SOURCE, msg->sourceName); NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TOPIC_NAME, msg->topic); if (msg->mediaContents) { NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_ICON_IMAGE, msg->mediaContents->iconImage); } NS_LOG(DEBUG, "NSSetMessagePayload - OUT"); return NS_OK; }
//Client should call refresh before expiresin or when receive 4.01 during sign-in OCStackResult OCCloudRefresh(const char *host, const char *query, const char *uId, const char *deviceId, const char *refreshtoken, OCClientResponseHandler response) { char targetUri[MAX_URI_LENGTH * 2] = { 0, }; snprintf(targetUri, MAX_URI_LENGTH * 2, "%s%s", host, query); OCCallbackData cbData; memset(&cbData, 0, sizeof(OCCallbackData)); cbData.cb = response; cbData.cd = NULL; cbData.context = (void *)DEFAULT_CONTEXT_VALUE; OCRepPayload *refreshPayload = OCRepPayloadCreate(); OCRepPayloadSetPropString(refreshPayload, "uid", uId); OCRepPayloadSetPropString(refreshPayload, "di", deviceId); OCRepPayloadSetPropString(refreshPayload, "granttype", "refresh_token"); OCRepPayloadSetPropString(refreshPayload, "refreshtoken", refreshtoken); return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)refreshPayload, CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0); }
OCStackResult ProvisionEnrollee(OCQualityOfService qos, const char *query, const char *resUri, OCDevAddr *destination, int pauseBeforeStart) { // This sleep is required in case of BLE provisioning due to packet drop issue. OIC_LOG_V(INFO, TAG, "Sleeping for %d seconds", pauseBeforeStart); sleep(pauseBeforeStart); OIC_LOG_V(INFO, TAG, "\n\nExecuting ProvisionEnrollee%s", __func__); OCRepPayload *payload = OCRepPayloadCreate(); OCRepPayloadSetUri(payload, resUri); OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, netProvInfo->netAddressInfo.WIFI.ssid); OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, netProvInfo->netAddressInfo.WIFI.pwd); OIC_LOG_V(DEBUG, TAG, "OCPayload ready for ProvisionEnrollee"); OCStackResult ret = InvokeOCDoResource(query, OC_REST_PUT, destination, OC_HIGH_QOS, ProvisionEnrolleeResponse, payload, NULL, 0); return ret; }
OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr, OCRepPayload** payload) { OCRepPayload *tempPayload = OCRepPayloadCreate(); if (!resourcePtr) { OCRepPayloadDestroy(tempPayload); return OC_STACK_INVALID_PARAM; } if(!tempPayload) { return OC_STACK_NO_MEMORY; } OCRepPayloadSetUri(tempPayload, resourcePtr->uri); OCResourceType *resType = resourcePtr->rsrcType; while(resType) { OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename); resType = resType->next; } OCResourceInterface *resInterface = resourcePtr->rsrcInterface; while(resInterface) { OCRepPayloadAddInterface(tempPayload, resInterface->name); resInterface = resInterface->next; } OCAttribute *resAttrib = resourcePtr->rsrcAttributes; while(resAttrib) { OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, resAttrib->attrValue); resAttrib = resAttrib->next; } if(!*payload) { *payload = tempPayload; } else { OCRepPayloadAppend(*payload, tempPayload); } return OC_STACK_OK; }
NSResult NSSetSyncPayload(NSSyncInfo *sync, OCRepPayload** syncPayload) { NS_LOG(DEBUG, "NSSetSyncPayload - IN"); *syncPayload = OCRepPayloadCreate(); if (!*syncPayload) { NS_LOG(ERROR, "Failed to allocate payload"); return NS_ERROR; } OCRepPayloadSetUri(*syncPayload, NS_COLLECTION_SYNC_URI); OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_PROVIDER_ID, sync->providerId); OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_MESSAGE_ID, sync->messageId); OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->state); NS_LOG(DEBUG, "NSSetSyncPayload - OUT"); return NS_OK; }
/** * @brief Called when a REST PUT is request * * @param OCBaseResource base resource attributes * * @return result of the entityHandler */ OCEntityHandlerResult putRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload* payload, OCBaseResourceT *resource) { // Set the new states OCRepPayload* inputPayload = (OCRepPayload*)(ehRequest->payload); OCAttributeT *current = resource->attribute; while(current != NULL) { switch(current->value.dataType) { case INT: { int64_t value(0); if(OCRepPayloadGetPropInt(inputPayload, current->name, &value)) { //OIC_LOG_V(DEBUG, TAG, "PUT: Type is int: %i", (int) value); //*((int*)current->value.data) = (int) value; current->value.data.i = value; } OCRepPayloadSetPropInt(payload, current->name, value); break; } case DOUBLE: { double value(0); if(OCRepPayloadGetPropDouble(inputPayload, current->name, &value)) { //OIC_LOG_V(DEBUG, TAG, "PUT: type is double: &d", value); //*((double*)current->value.data) = value; current->value.data.d = value; } OCRepPayloadSetPropDouble(payload, current->name, value); break; } case STRING: { char* value(""); if(OCRepPayloadGetPropString(inputPayload, current->name, &value)) { //OIC_LOG_V(DEBUG, TAG, "PUT: type is string: %s", value); //*((char**)current->value.data) = value; current->value.data.str = value; } OCRepPayloadSetPropString(payload, current->name, value); } case BOOL: { bool value(false); if(OCRepPayloadGetPropBool(inputPayload, current->name, &value)) { //OIC_LOG_V(DEBUG, TAG, "PUT: Type is bool: %s", value ? "true" : "false"); //*((bool*)current->value.data) = value; current->value.data.b = value; } OCRepPayloadSetPropBool(payload, current->name, value); break; } } current = current->next; } // Set the output pins if(resource->OCIOhandler) { OIC_LOG_V(DEBUG, TAG, "Value of underObservation is: %s", resource->underObservation ? "true" : "false"); resource->OCIOhandler(resource->attribute, OUTPUT, resource->handle, &resource->underObservation); } else { OIC_LOG(ERROR, TAG, "Resource OutputHandler has not been set"); } OIC_LOG(DEBUG, TAG, "Leaving putRequest"); return OC_EH_OK; }
static bool add_property_in_post_req_msg(st_things_set_request_message_s *req_msg, OCRepPayload *req_payload, things_attribute_info_s *prop) { RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg); RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg->rep); RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg->rep->payload); RET_FALSE_IF_PARAM_IS_NULL(TAG, req_payload); RET_FALSE_IF_PARAM_IS_NULL(TAG, prop); OCRepPayload *resp_payload = req_msg->rep->payload; THINGS_LOG_D(TAG, "Property Key is %s", prop->key); THINGS_LOG_D(TAG, "Property type is %d", prop->type); // Based on the property type, call appropriate methods to copy // the property from request payload to request representation. bool result = false; switch (prop->type) { case BOOL_ID: { bool value = false; if (OCRepPayloadGetPropBool(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropBool(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the boolean value of '%s' in request message.", prop->key); } } else { THINGS_LOG_E(TAG, "Failed to get the boolean value of '%s' for request message.", prop->key); } } break; case INT_ID: { int64_t value = 0; if (OCRepPayloadGetPropInt(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropInt(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the integer value of '%s' in request message", prop->key); } } else { THINGS_LOG_E(TAG, "Failed to get the integer value of '%s' for request message", prop->key); } } break; case DOUBLE_ID: { double value = 0.0; if (OCRepPayloadGetPropDouble(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropDouble(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the double value of '%s' in request message", prop->key); } } else { THINGS_LOG_E(TAG, "Failed to get the double value of '%s' for request message", prop->key); } } break; case STRING_ID: { char *value = NULL; if (OCRepPayloadGetPropString(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropString(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the string value of '%s' in request message", prop->key); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the string value of '%s' for request message", prop->key); } } break; case OBJECT_ID: { OCRepPayload *value = NULL; if (OCRepPayloadGetPropObject(req_payload, prop->key, &value)) { result = OCRepPayloadSetPropObject(resp_payload, prop->key, value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the object value of '%s' in request message", prop->key); } OCRepPayloadDestroy(value); } else { THINGS_LOG_E(TAG, "Failed to get the object value of '%s' for request message", prop->key); } } break; case BYTE_ID: { OCByteString byte_value; if (OCRepPayloadGetPropByteString(req_payload, prop->key, &byte_value)) { result = OCRepPayloadSetPropByteString(resp_payload, prop->key, byte_value); if (!result) { THINGS_LOG_E(TAG, "Failed to set the byte string value of '%s' in request message", prop->key); } things_free(byte_value.bytes); } else { THINGS_LOG_E(TAG, "Failed to get the byte string value of '%s' for request message", prop->key); } } break; case INT_ARRAY_ID: { int64_t *value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetIntArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetIntArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the integer array value of '%s' in request message", prop->key); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the integer array value of '%s' for request message", prop->key); } } break; case DOUBLE_ARRAY_ID: { double *value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetDoubleArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetDoubleArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the double array value of '%s' in request message", prop->key); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the double array value of '%s' for request message", prop->key); } } break; case STRING_ARRAY_ID: { char **value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetStringArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetStringArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the string array value of '%s' in request message", prop->key); } size_t len = calcDimTotal(dimensions); things_free_str_array(value, len); } else { THINGS_LOG_E(TAG, "Failed to get the string array value of '%s' for request message", prop->key); } } break; case OBJECT_ARRAY_ID: { OCRepPayload **value = NULL; size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 }; if (OCRepPayloadGetPropObjectArray(req_payload, prop->key, &value, dimensions)) { result = OCRepPayloadSetPropObjectArray(resp_payload, prop->key, value, dimensions); if (!result) { THINGS_LOG_E(TAG, "Failed to set the object array value of '%s' in request message", prop->key); } size_t len = calcDimTotal(dimensions); for (size_t index = 0; index < len; index++) { OCRepPayloadDestroy(value[index]); } things_free(value); } else { THINGS_LOG_E(TAG, "Failed to get the object array value of '%s' for request message", prop->key); } } break; default: THINGS_LOG_E(TAG, "Invalid property type (%d).", prop->type); break; } return result; }
OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, OCEntityHandlerResponse *response, OCRepPayload** payload) { OCEntityHandlerResult ehResult = OC_EH_OK; OCRepPayload *respPLPost_light = nullptr; /* * The entity handler determines how to process a POST request. * Per the REST paradigm, POST can also be used to update representation of existing * resource or create a new resource. * In the sample below, if the POST is for /a/light then a new instance of the Light * resource is created with default representation (if representation is included in * POST payload it can be used as initial values) as long as the instance is * lesser than max new instance count. Once max instance count is reached, POST on * /a/light updated the representation of /a/light (just like PUT) */ if (ehRequest->resource == Light.handle) { if (gCurrLightInstance < SAMPLE_MAX_NUM_POST_INSTANCE) { // Create new Light instance char newLightUri[URI_MAXSIZE]; snprintf(newLightUri, URI_MAXSIZE, "/a/light/%d", gCurrLightInstance); respPLPost_light = OCRepPayloadCreate(); OCRepPayloadSetUri(respPLPost_light, gResourceUri); OCRepPayloadSetPropString(respPLPost_light, "createduri", newLightUri); if (0 == createLightResource (newLightUri, &gLightInstance[gCurrLightInstance])) { OIC_LOG (INFO, TAG, "Created new Light instance\n"); gLightInstance[gCurrLightInstance].state = 0; gLightInstance[gCurrLightInstance].power = 0; gCurrLightInstance++; strncpy ((char *)response->resourceUri, newLightUri, MAX_URI_LENGTH); ehResult = OC_EH_RESOURCE_CREATED; } } else { // Update repesentation of /a/light Light.state = true; Light.power = 11; respPLPost_light = constructResponse(ehRequest); } } else { for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++) { if (ehRequest->resource == gLightInstance[i].handle) { gLightInstance[i].state = true; gLightInstance[i].power = 22; if (i == 0) { respPLPost_light = constructResponse(ehRequest); break; } else if (i == 1) { respPLPost_light = constructResponse(ehRequest); } } } } if ((respPLPost_light != NULL)) { *payload = respPLPost_light; } else { OIC_LOG(INFO, TAG, "Payload was NULL"); ehResult = OC_EH_ERROR; } return ehResult; }
OCEntityHandlerResult ProcessPostRequest (OCEntityHandlerRequest *ehRequest, OCEntityHandlerResponse *response, OCRepPayload **payload) { OCRepPayload *respPLPost_led = NULL; OCEntityHandlerResult ehResult = OC_EH_OK; /* * The entity handler determines how to process a POST request. * Per the REST paradigm, POST can also be used to update representation of existing * resource or create a new resource. * In the sample below, if the POST is for /a/led then a new instance of the LED * resource is created with default representation (if representation is included in * POST payload it can be used as initial values) as long as the instance is * lesser than max new instance count. Once max instance count is reached, POST on * /a/led updated the representation of /a/led (just like PUT) */ if (ehRequest->resource == LED.handle) { if (gCurrLedInstance < SAMPLE_MAX_NUM_POST_INSTANCE) { // Create new LED instance char newLedUri[15] = "/a/led/"; int newLedUriLength = strlen(newLedUri); snprintf (newLedUri + newLedUriLength, sizeof(newLedUri)-newLedUriLength, "%d", gCurrLedInstance); respPLPost_led = OCRepPayloadCreate(); OCRepPayloadSetUri(respPLPost_led, gResourceUri); OCRepPayloadSetPropString(respPLPost_led, "createduri", newLedUri); if (0 == createLEDResource (newLedUri, &gLedInstance[gCurrLedInstance], false, 0)) { OIC_LOG (INFO, TAG, "Created new LED instance"); gLedInstance[gCurrLedInstance].state = 0; gLedInstance[gCurrLedInstance].power = 0; gCurrLedInstance++; strncpy ((char *)response->resourceUri, newLedUri, MAX_URI_LENGTH); ehResult = OC_EH_RESOURCE_CREATED; } } else { respPLPost_led = constructResponse(ehRequest); } } else { for (int i = 0; i < SAMPLE_MAX_NUM_POST_INSTANCE; i++) { if (ehRequest->resource == gLedInstance[i].handle) { if (i == 0) { respPLPost_led = constructResponse(ehRequest); break; } else if (i == 1) { respPLPost_led = constructResponse(ehRequest); } } } } if (respPLPost_led != NULL) { *payload = respPLPost_led; ehResult = OC_EH_OK; } else { OIC_LOG_V (INFO, TAG, "Payload was NULL"); ehResult = OC_EH_ERROR; } return ehResult; }
static int _icd_state_value_from_gvariant(OCRepPayload *repr, GVariantIter *iter) { int ret; char *key; GVariant *var; const char *str_value; OCRepPayload *repr_value; struct icd_state_list_s value_list = {0}; while (g_variant_iter_loop(iter, "{sv}", &key, &var)) { if (g_variant_is_of_type(var, G_VARIANT_TYPE_BOOLEAN)) { OCRepPayloadSetPropBool(repr, key, g_variant_get_boolean(var)); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE_INT32)) { OCRepPayloadSetPropInt(repr, key, g_variant_get_int32(var)); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE_DOUBLE)) { OCRepPayloadSetPropDouble(repr, key, g_variant_get_double(var)); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE_STRING)) { str_value = g_variant_get_string(var, NULL); if (NULL == str_value) { ERR("g_variant_get_string() Fail"); _icd_payload_state_list_destroy(&value_list); return IOTCON_ERROR_OUT_OF_MEMORY; } if (IC_STR_EQUAL == strcmp(IC_STR_NULL, str_value)) OCRepPayloadSetNull(repr, key); else OCRepPayloadSetPropString(repr, key, str_value); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE("ay"))) { OCByteString byte_value; byte_value.bytes = (uint8_t*)g_variant_get_data(var); byte_value.len = g_variant_get_size(var); OCRepPayloadSetPropByteString(repr, key, byte_value); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE("a{sv}"))) { GVariantIter state_iter; repr_value = OCRepPayloadCreate(); g_variant_iter_init(&state_iter, var); ret = _icd_state_value_from_gvariant(repr_value, &state_iter); if (IOTCON_ERROR_NONE != ret) { ERR("_icd_state_value_from_gvariant() Fail(%d)", ret); _icd_payload_state_list_destroy(&value_list); OCRepPayloadDestroy(repr_value); return ret; } OCRepPayloadSetPropObjectAsOwner(repr, key, repr_value); } else if (g_variant_is_of_type(var, G_VARIANT_TYPE_ARRAY)) { memset(&value_list, 0, sizeof(struct icd_state_list_s)); ret = _icd_state_list_from_gvariant(var, &value_list, 0); if (IOTCON_ERROR_NONE != ret) { ERR("_icd_state_list_from_gvariant() Fail(%d)", ret); _icd_payload_state_list_destroy(&value_list); return ret; } ret = _icd_state_array_from_list(repr, &value_list, key); if (IOTCON_ERROR_NONE != ret) { ERR("_icd_state_array_from_list() Fail(%d)", ret); _icd_payload_state_list_destroy(&value_list); return ret; } } else { ERR("Invalid type(%s)", g_variant_get_type_string(var)); return IOTCON_ERROR_INVALID_TYPE; } } return IOTCON_ERROR_NONE; }
void CHPJsonToRepPayload(cJSON* rootJSon, OCRepPayload* payload) { cJSON* dataJson = rootJSon->child; while (dataJson) { switch (dataJson->type) { case cJSON_String: OCRepPayloadSetPropString(payload, dataJson->string, dataJson->valuestring); break; case cJSON_Number: if (dataJson->valueint == dataJson->valuedouble) { OCRepPayloadSetPropInt(payload, dataJson->string, dataJson->valueint); } else { OCRepPayloadSetPropDouble(payload, dataJson->string, dataJson->valuedouble); } break; case cJSON_False: OCRepPayloadSetPropBool(payload, dataJson->string, false); break; case cJSON_True: OCRepPayloadSetPropBool(payload, dataJson->string, true); break; case cJSON_Object: { OCRepPayload* childPayload = OCRepPayloadCreate(); CHPJsonToRepPayload(dataJson,childPayload); OCRepPayloadSetPropObject(payload, dataJson->string,childPayload ); break; } case cJSON_Array: { int size = cJSON_GetArraySize(dataJson); size_t dimensions[MAX_REP_ARRAY_DEPTH]; dimensions[0] = size; dimensions[1] = dimensions[2] = 0; int i = 0; int type = cJSON_IsReference; int numType = 0; // int:1, double:2 const int intType = 1; const int doubleType = 2; int64_t intArray[size]; double doubleArray[size]; char* strArray[size]; OCRepPayload* objPayloadArray[size]; for (; i < size ; ++i) { cJSON* subitem = cJSON_GetArrayItem(dataJson, i); if (subitem == NULL) { continue; } if ((type != cJSON_IsReference) && (type != subitem->type)) { continue; } else { type = subitem->type; switch (type) { case cJSON_Number: if (subitem->valueint == subitem->valuedouble) { numType = intType; intArray[i] = (int64_t) subitem->valueint; } else { numType = doubleType; doubleArray[i] = subitem->valuedouble; } break; case cJSON_String: strArray[i] = subitem->valuestring; break; case cJSON_Object: objPayloadArray[i] = OCRepPayloadCreate(); CHPJsonToRepPayload(subitem,objPayloadArray[i]); break; default: OIC_LOG(ERROR, TAG, "wrong ArrayType in JsonToRepPayload()"); break; } } } switch (type) { case cJSON_Number: if (numType == intType) { OCRepPayloadSetIntArray(payload, dataJson->string,(const int64_t*)intArray, dimensions); } else if (numType == doubleType) { OCRepPayloadSetDoubleArray(payload, dataJson->string, (const double*)doubleArray, dimensions); } break; case cJSON_String: OCRepPayloadSetStringArray(payload, dataJson->string, (const char**)strArray, dimensions); break; case cJSON_Object: OCRepPayloadSetPropObjectArray(payload, dataJson->string, (const OCRepPayload**)objPayloadArray, dimensions); break; default: OIC_LOG(ERROR, TAG, "wrong ArrayType in JsonToRepPayload()"); break; } break; } } dataJson = dataJson->next; } }