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; }
TEST_F(CborByteStringTest, ByteStringArraySetGetTest ) { OCRepPayloadSetUri(payload_in, "/a/quake_sensor"); OCRepPayloadSetPropInt(payload_in, "scale", 4); size_t dimensions_in[MAX_REP_ARRAY_DEPTH] = { 3, 0, 0}; uint8_t binval1[] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}; uint8_t binval2[] = {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29}; uint8_t binval3[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39}; OCByteString quakedata_in[3] = {{binval1, sizeof(binval1)}, {binval2, sizeof(binval2)}, {binval3, sizeof(binval3)}}; EXPECT_EQ(true, OCRepPayloadSetByteStringArray(payload_in, "quakedata", quakedata_in, dimensions_in)); OCByteString* quakedata_out = NULL; size_t dimensions_out[MAX_REP_ARRAY_DEPTH] = {0}; ASSERT_EQ(true, OCRepPayloadGetByteStringArray(payload_in, "quakedata", &quakedata_out, dimensions_out)); for(size_t i = 0; i < dimensions_in[0]; i++) { EXPECT_EQ(quakedata_in[i].len, quakedata_out[i].len); EXPECT_EQ(0, memcmp(quakedata_in[i].bytes, quakedata_out[i].bytes, quakedata_in[i].len)); } // Cleanup for(size_t i = 0; i < dimensions_out[0]; i++) { OICFree(quakedata_out[i].bytes); } OICFree(quakedata_out); }
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; }
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; }
static int handle_post_req_on_single_rsrc(things_resource_s *single_rsrc) { RET_VAL_IF_PARAM_IS_NULL(TAG, single_rsrc, 0); // Retrieve the request representation. This representation will hold all the input properties of post request. // Payload in this representation will be used to form the request message which will be given to the application. struct things_representation_s *req_rep = NULL; bool rep_exist = single_rsrc->things_get_representation(single_rsrc, &req_rep); if (!rep_exist || NULL == req_rep || NULL == req_rep->payload) { THINGS_LOG_E(TAG, "Empty payload in POST request."); return 0; // TODO: When a post request comes with empty payload, how do we handle? } // Setup the response representation. This representation will be handed over to the underlying stack. // The payload which is passed as a parameter will be updated with the common properties. things_representation_s *resp_rep = things_create_representation_inst(NULL); RET_VAL_IF_NULL(TAG, resp_rep, "Failed to create response representation.", 0); if (!OCRepPayloadSetUri(resp_rep->payload, single_rsrc->uri)) { THINGS_LOG_E(TAG, "Failed to set the resource uri(%s) in response payload.", single_rsrc->uri); things_release_representation_inst(resp_rep); return 0; } THINGS_LOG_D(TAG, "Resource uri(%s) is set in the response payload.", single_rsrc->uri); // Get interface type from query parameter char *if_type = NULL; if (NULL != single_rsrc->query && strlen(single_rsrc->query) > 0) { bool found = false; bool result = get_query_value_internal(single_rsrc->query, OC_RSRVD_INTERFACE, &if_type, &found); if (found && !result) { // If query is present but API returns false. THINGS_LOG_E(TAG, "Failed to get the interface type from query parameter(%s).", single_rsrc->query); things_release_representation_inst(resp_rep); return 0; } } // Set the common properties in the payload (Only for baseline interface). // The payload which is passed as a parameter will be updated with the common properties. if (NULL == if_type || !strncmp(if_type, OC_RSRVD_INTERFACE_DEFAULT, strlen(OC_RSRVD_INTERFACE_DEFAULT))) { if (!add_common_props(single_rsrc, false, resp_rep->payload)) { THINGS_LOG_E(TAG, "Failed to add the common properties in response payload."); things_release_representation_inst(resp_rep); things_free(if_type); return 0; } THINGS_LOG_D(TAG, "Added common properties in response payload."); } // Give the request properties to application and get the response back. bool res = handle_post_req_helper(single_rsrc->uri, single_rsrc->query, req_rep->payload, resp_rep->payload); if (res) { // Set the response representation in the resource. single_rsrc->things_set_representation(single_rsrc, resp_rep); } else { things_release_representation_inst(resp_rep); } things_free(if_type); return res ? 1 : 0; }
// This is the entity handler for the registered resource. // This is invoked by OCStack whenever it recevies a request for this resource. OCEntityHandlerResult SensorOCEntityHandlerCb(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) { //sensor_get(); OCRepPayloadSetUri(payload, "/grove/sensor"); OCRepPayloadSetPropDouble(payload, "temperature", sensor.temp); OCRepPayloadSetPropInt(payload, "light", sensor.light); OCRepPayloadSetPropInt(payload, "sound", sensor.sound); } else if(OC_REST_PUT == entityHandlerRequest->method) { OC_LOG(ERROR, TAG, ("Un-supported request for Sensor: PUT")); } 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; }
TEST_F(CborByteStringTest, ByteStringArrayConvertParseTest ) { OCRepPayloadSetUri(payload_in, "/a/quake_sensor"); OCRepPayloadSetPropInt(payload_in, "scale", 4); size_t dimensions_in[MAX_REP_ARRAY_DEPTH] = { 3, 0, 0}; uint8_t binval1[] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}; uint8_t binval2[] = {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29}; uint8_t binval3[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39}; OCByteString quakedata_in[3] = {{binval1, sizeof(binval1)}, {binval2, sizeof(binval2)}, {binval3, sizeof(binval3)}}; EXPECT_EQ(true, OCRepPayloadSetByteStringArray(payload_in, "quakedata", quakedata_in, dimensions_in)); // Convert OCPayload to CBOR uint8_t *payload_cbor = NULL; size_t payload_cbor_size = 0; EXPECT_EQ(OC_STACK_OK, OCConvertPayload((OCPayload*) payload_in, &payload_cbor, &payload_cbor_size)); #ifdef CBOR_BIN_STRING_DEBUG FILE *fp = fopen("binstringarr.cbor", "wb+"); if (fp) { fwrite(payload_cbor, 1, payload_cbor_size, fp); fclose(fp); } #endif //CBOR_BIN_STRING_DEBUG // Parse CBOR back to OCPayload OCPayload* payload_out = NULL; EXPECT_EQ(OC_STACK_OK, OCParsePayload(&payload_out, PAYLOAD_TYPE_REPRESENTATION, payload_cbor, payload_cbor_size)); OCByteString* quakedata_out = NULL; size_t dimensions_out[MAX_REP_ARRAY_DEPTH] = {0}; ASSERT_EQ(true, OCRepPayloadGetByteStringArray((OCRepPayload*)payload_out, "quakedata", &quakedata_out, dimensions_out)); for(size_t i = 0; i < dimensions_in[0]; i++) { EXPECT_EQ(quakedata_in[i].len, quakedata_out[i].len); EXPECT_EQ(0, memcmp(quakedata_in[i].bytes, quakedata_out[i].bytes, quakedata_in[i].len)); } // Cleanup OICFree(payload_cbor); for(size_t i = 0; i < dimensions_out[0]; i++) { OICFree(quakedata_out[i].bytes); } OICFree(quakedata_out); OCPayloadDestroy((OCPayload*)payload_out); }
//This function takes the request as an input and returns the response //in JSON format. OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest) { LEDResource *currLEDResource = &LED; OC_LOG(INFO, TAG, "Entering constructResponse"); if (ehRequest->resource == gLedInstance[0].handle) { OC_LOG(INFO, TAG, "handle 0"); currLEDResource = &gLedInstance[0]; gResourceUri = const_cast<char *>("a/led/0"); } else if (ehRequest->resource == gLedInstance[1].handle) { OC_LOG(INFO, TAG, "handle 1"); currLEDResource = &gLedInstance[1]; gResourceUri = const_cast<char *>("a/led/1"); } if(OC_REST_PUT == ehRequest->method) { if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION) { OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation")); return nullptr; } OCRepPayload *putPayload = reinterpret_cast<OCRepPayload*> (ehRequest->payload); int64_t power; bool state; if (OCRepPayloadGetPropBool(putPayload, "state", &state)) { currLEDResource->state = state; } if (OCRepPayloadGetPropInt (putPayload, "power", &power)) { currLEDResource->power = power; } } OCRepPayload *response = OCRepPayloadCreate(); if (!response) { OC_LOG_V(ERROR, TAG, "Memory allocation for response payload failed."); } OCRepPayloadSetUri (response, gResourceUri); OCRepPayloadSetPropBool(response, "state", currLEDResource->state); OCRepPayloadSetPropInt(response, "power", currLEDResource->power); return response; }
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; }
static int handle_get_req_on_single_rsrc(things_resource_s *single_rsrc) { RET_VAL_IF_PARAM_IS_NULL(TAG, single_rsrc, 0); // Setup the response representation. This representation will be handed over to the underlying stack. things_representation_s *resp_rep = things_create_representation_inst(NULL); RET_VAL_IF_NULL(TAG, resp_rep, "Failed to create response representation.", 0); bool result = OCRepPayloadSetUri(resp_rep->payload, single_rsrc->uri); if (!result) { THINGS_LOG_E(TAG, "Failed to set the resource uri(%s) in response payload.", single_rsrc->uri); things_release_representation_inst(resp_rep); return 0; } THINGS_LOG_D(TAG, "Resource uri(%s) is set in the response payload.", single_rsrc->uri); // Set the common properties in the payload (Only for baseline interface). char *if_type = NULL; if (NULL != single_rsrc->query && strlen(single_rsrc->query) > 0) { bool found = false; bool result = get_query_value_internal(single_rsrc->query, OC_RSRVD_INTERFACE, &if_type, &found); if (found && !result) { // If query is present but API returns false. THINGS_LOG_E(TAG, "Failed to get the interface type from query parameter(%s).", single_rsrc->query); things_release_representation_inst(resp_rep); return 0; } } if (NULL == if_type || strlen(if_type) < 1 || !strncmp(if_type, OC_RSRVD_INTERFACE_DEFAULT, strlen(OC_RSRVD_INTERFACE_DEFAULT))) { if (!add_common_props(single_rsrc, false, resp_rep->payload)) { THINGS_LOG_E(TAG, "Failed to add the common properties in response payload."); things_release_representation_inst(resp_rep); things_free(if_type); return 0; } THINGS_LOG_D(TAG, "Added common properties in response payload."); } // Get the resource's properties from the application. result = handle_get_req_helper(single_rsrc->uri, single_rsrc->query, resp_rep->payload); if (!result) { things_release_representation_inst(resp_rep); things_free(if_type); return 0; } // Set the response representation in the resource. single_rsrc->things_set_representation(single_rsrc, resp_rep); things_free(if_type); return 1; }
OCRepPayload* getPayload(const char* uri, int64_t brightness) { OCRepPayload* payload = OCRepPayloadCreate(); if(!payload) { OIC_LOG(ERROR, TAG, PCF("Failed to allocate Payload")); return nullptr; } OCRepPayloadSetUri(payload, uri); OCRepPayloadSetPropInt(payload, "brightness", brightness); return payload; }
OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest) { OCRepPayload* payload = OCRepPayloadCreate(); if (!payload) { OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload"); return NULL; } if (ehRequest->resource == gProvResource.handle) { OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res"); OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV); OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS, gProvResource.ps); OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, gProvResource.tnt); } else if (ehRequest->requestHandle == gNetResource.handle) { OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_NET); OCRepPayloadSetPropInt(payload, "ant", gNetResource.ant[0]); } return payload; }
OCRepPayload* getPayload(const char* uri, int64_t power, bool state) { OCRepPayload* payload = OCRepPayloadCreate(); if(!payload) { OIC_LOG(ERROR, TAG, "Failed to allocate Payload"); return NULL; } OCRepPayloadSetUri(payload, uri); OCRepPayloadSetPropBool(payload, "state", state); OCRepPayloadSetPropInt(payload, "power", power); return payload; }
TEST_F(CborByteStringTest, ByteStringConvertParseTest) { OCRepPayloadSetUri(payload_in, "/a/quake_sensor"); OCRepPayloadSetPropInt(payload_in, "scale", 4); uint8_t binval[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF}; OCByteString quakedata_in = { binval, sizeof(binval)}; // Set ByteString in Payload EXPECT_EQ(true, OCRepPayloadSetPropByteString(payload_in, "quakedata", quakedata_in)); // Convert OCPayload to CBOR uint8_t *payload_cbor = NULL; size_t payload_cbor_size = 0; EXPECT_EQ(OC_STACK_OK, OCConvertPayload((OCPayload*) payload_in, &payload_cbor, &payload_cbor_size)); #ifdef CBOR_BIN_STRING_DEBUG FILE *fp = fopen("binstring.cbor", "wb+"); if (fp) { fwrite(payload_cbor, 1, payload_cbor_size, fp); fclose(fp); } #endif //CBOR_BIN_STRING_DEBUG // Parse CBOR back to OCPayload OCPayload* payload_out = NULL; EXPECT_EQ(OC_STACK_OK, OCParsePayload(&payload_out, PAYLOAD_TYPE_REPRESENTATION, payload_cbor, payload_cbor_size)); OCByteString quakedata_out = {NULL, 0}; ASSERT_EQ(true, OCRepPayloadGetPropByteString((OCRepPayload*)payload_out, "quakedata", &quakedata_out)); // Compare input and output data EXPECT_EQ(quakedata_in.len, quakedata_out.len); EXPECT_EQ(0, memcmp(quakedata_in.bytes, quakedata_out.bytes, quakedata_in.len)); // Cleanup OICFree(payload_cbor); OICFree(quakedata_out.bytes); OCPayloadDestroy((OCPayload*)payload_out); }
TEST_F(CborByteStringTest, ByteStringSetGetTest) { OCRepPayloadSetUri(payload_in, "/a/quake_sensor"); OCRepPayloadSetPropInt(payload_in, "scale", 4); uint8_t binval[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF}; OCByteString quakedata_in = { binval, sizeof(binval)}; EXPECT_EQ(true, OCRepPayloadSetPropByteString(payload_in, "quakedata", quakedata_in)); OCByteString quakedata_out = { NULL, 0}; ASSERT_EQ(true, OCRepPayloadGetPropByteString(payload_in, "quakedata", &quakedata_out)); EXPECT_EQ(quakedata_in.len, quakedata_out.len); EXPECT_EQ(0, memcmp(quakedata_in.bytes, quakedata_out.bytes, quakedata_in.len)); // Cleanup OICFree(quakedata_out.bytes); }
OCRepPayload* icd_payload_representation_from_gvariant(GVariant *var) { int ret; GVariant *child; OCRepPayload *repr, *cur; char *uri_path, *resource_iface, *resource_type; GVariantIter *resource_types, *resource_ifaces, *repr_gvar, *children; repr = OCRepPayloadCreate(); g_variant_get(var, "(&sasasa{sv}av)", &uri_path, &resource_ifaces, &resource_types, &repr_gvar, &children); if (IC_STR_EQUAL != strcmp(IC_STR_NULL, uri_path)) OCRepPayloadSetUri(repr, uri_path); while (g_variant_iter_loop(resource_ifaces, "s", &resource_iface)) OCRepPayloadAddInterface(repr, resource_iface); while (g_variant_iter_loop(resource_types, "s", &resource_type)) OCRepPayloadAddResourceType(repr, resource_type); ret = _icd_state_value_from_gvariant(repr, repr_gvar); if (IOTCON_ERROR_NONE != ret) { ERR("_icd_state_value_from_gvariant() Fail(%d)", ret); OCRepPayloadDestroy(repr); return NULL; } cur = repr; while (g_variant_iter_loop(children, "v", &child)) { cur->next = icd_payload_representation_from_gvariant(child); if (NULL == cur->next) { ERR("icd_payload_representation_from_gvariant() Fail"); OCRepPayloadDestroy(repr); return NULL; } cur = cur->next; } return repr; }
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; }
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; }
/** * @brief Create and loads the payload * * @return the created payload */ OCRepPayload *getPayload(OCEntityHandlerRequest *ehRequest, OCBaseResourceT *resource) { OIC_LOG(DEBUG, TAG, "Getting Payload"); if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION) { OIC_LOG(ERROR, TAG, "Incoming payload not a representation"); return NULL; } OCRepPayload *payload = OCRepPayloadCreate(); if(!payload) { OIC_LOG(ERROR, TAG, "Failed to allocate Payload"); return NULL; } OCRepPayloadSetUri(payload, resource->uri); //OCRepPayloadSetPropString(payload, "name", resource->name); return payload; }
void set_uri(struct things_resource_s *res, const char *key) { //THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); if (key == NULL) { return; } if (res->uri != NULL) { things_free(res->uri); res->uri = NULL; } res->uri = (char *)things_malloc(sizeof(char) *strlen(key) + 1); memset(res->uri, 0, strlen(key) + 1); things_strncpy(res->uri, key, strlen(key) + 1); if (NULL != res->rep) { OCRepPayloadSetUri(res->rep->payload, key); } else { THINGS_LOG_E(TAG, "Set URI Failed, No Representation Yet"); } //THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); }
OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void *callbackParam) { OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = {0}; OCRepPayload* payload = OCRepPayloadCreate(); if(!payload) { //OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload")); return OC_EH_ERROR; } if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { //OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG")); if(OC_REST_GET == entityHandlerRequest->method) { OCRepPayloadSetUri(payload, "/iotar/speaker"); OCRepPayloadSetPropInt(payload, "state", speaker.state); OCRepPayloadSetPropInt(payload, "volume",speaker.volume); OCRepPayloadSetPropInt(payload, "time", speaker.time); OCRepPayloadSetPropInt(payload, "present_song",speaker.present_song); OCRepPayloadSetPropInt(payload, "next_song", speaker.next_song); } else if(OC_REST_PUT == entityHandlerRequest->method) { //OC_LOG_V(DEBUG, TAG, "put \n"); //풋 올때 데이터 어떻게 받지?? OCRepPayload* pay = (OCRepPayload*) entityHandlerRequest-> payload; int64_t order = 0; int64_t temp = 0; char in[10]; OCRepPayloadGetPropInt(pay, "order", &order); if(order == 30){ //재생 OCRepPayloadGetPropInt(pay, "temp", &temp); makeString('a', temp, (char*)&in); speaker.present_song = temp; Serial1.println(in); millis_prv = millis(); save_time = 0; speaker.state = 1; }else if(order == 31){ //볼륨 설정 OCRepPayloadGetPropInt(pay, "temp", &temp); makeString('v', temp, (char*)&in); speaker.volume = temp; Serial1.println(in); }else if(order == 32){ //다음 노래 설정 OCRepPayloadGetPropInt(pay, "temp", &temp); makeString('d', temp, (char*)&in); speaker.next_song = temp; Serial1.println(in); }else if(order == 33){ // 현재 아무일 안하고 있으면 0, 재생중 1, 일시정지 2. 로 세팅. Serial1.println('x'); }else if(order == 34) { //정지. Serial1.println('s'); }else if(order == 35) { // +1 볼륨. speaker.volume++; if(speaker.volume >= 100){ speaker.volume = 100; } makeString('v', speaker.volume, (char*)&in); Serial1.println(in); }else if(order == 36) { // -1 볼륨. speaker.volume--; if(speaker.volume < 1){ speaker.volume = 1; } makeString('v', speaker.volume, (char*)&in); Serial1.println(in); }else if(order == 37){ // 일시정지, 일시정지 풀기. Serial1.println('p'); if(speaker.state == 1){ speaker.state = 2; save_time = speaker.time+2; }else{ speaker.state = 1; millis_prv = millis(); } }else if(order == 38){ //다음노래 재생 speaker.present_song++; if(speaker.present_song > MAX_SONG){ speaker.present_song = 1; } makeString('a', speaker.present_song, (char*)&in); Serial1.println(in); millis_prv = millis(); save_time = 0; speaker.state = 1; }else if(order == 39){ // 이전 노래 재생 speaker.present_song--; if(speaker.present_song < 1){ speaker.present_song = MAX_SONG; } makeString('a', speaker.present_song, (char*)&in); Serial1.println(in); millis_prv = millis(); save_time = 0; speaker.state = 1; } OCRepPayloadSetUri(payload, "/iotar/speaker"); OCRepPayloadSetPropInt(payload, "time", speaker.time); } 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, PCF("Received OC_OBSERVE_REGISTER from client")); gLightUnderObservation = 1; } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { // OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client")); gLightUnderObservation = 0; } } OCRepPayloadDestroy(payload); return ehRet; }
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; }
// This is the entity handler for the registered resource. // This is invoked by OCStack whenever it recevies a request for this resource. OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void *callbackParam) { OCEntityHandlerResult ehRet = OC_EH_OK; OCEntityHandlerResponse response = {0}; OCRepPayload* payload = OCRepPayloadCreate(); if(!payload) { OC_LOG(ERROR, TAG, PCF("Failed to allocate Payload")); return OC_EH_ERROR; } if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) { OC_LOG (INFO, TAG, PCF("Flag includes OC_REQUEST_FLAG")); if(OC_REST_GET == entityHandlerRequest->method) { OCRepPayloadSetUri(payload, "/a/light"); OCRepPayloadSetPropBool(payload, "state", true); OCRepPayloadSetPropInt(payload, "power", 10); } else if(OC_REST_PUT == entityHandlerRequest->method) { //Do something with the 'put' payload OCRepPayloadSetUri(payload, "/a/light"); OCRepPayloadSetPropBool(payload, "state", false); OCRepPayloadSetPropInt(payload, "power", 0); } 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, PCF("Received OC_OBSERVE_REGISTER from client")); gLightUnderObservation = 1; } else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) { OC_LOG (INFO, TAG, PCF("Received OC_OBSERVE_DEREGISTER from client")); } } return ehRet; }
static OCStackResult HandleBatchInterface(OCEntityHandlerRequest *ehRequest) { if (!ehRequest) { return OC_STACK_INVALID_PARAM; } OCResource * collResource = (OCResource *) ehRequest->resource; OCRepPayload* payload = OCRepPayloadCreate(); if (!payload) { return OC_STACK_NO_MEMORY; } if (collResource) { OCRepPayloadSetUri(payload, collResource->uri); } OCEntityHandlerResponse response = {0}; response.ehResult = OC_EH_OK; response.payload = (OCPayload*)payload; response.persistentBufferFlag = 0; response.requestHandle = (OCRequestHandle) ehRequest->requestHandle; response.resourceHandle = (OCResourceHandle) collResource; OCStackResult stackRet = OCDoResponse(&response); if (stackRet == OC_STACK_OK) { for (uint8_t i = 0; i < MAX_CONTAINED_RESOURCES; i++) { OCResource* temp = collResource->rsrcResources[i]; if (temp) { // Note that all entity handlers called through a collection // will get the same pointer to ehRequest, the only difference // is ehRequest->resource ehRequest->resource = (OCResourceHandle) temp; OCEntityHandlerResult ehResult = temp->entityHandler(OC_REQUEST_FLAG, ehRequest, temp->entityHandlerCallbackParam); // The default collection handler is returning as OK if (stackRet != OC_STACK_SLOW_RESOURCE) { stackRet = OC_STACK_OK; } // if a single resource is slow, then entire response will be treated // as slow response if (ehResult == OC_EH_SLOW) { OC_LOG(INFO, TAG, "This is a slow resource"); ((OCServerRequest *)ehRequest->requestHandle)->slowFlag = 1; stackRet = EntityHandlerCodeToOCStackCode(ehResult); } } else { break; } } ehRequest->resource = (OCResourceHandle) collResource; } return stackRet; }