struct things_resource_s *get_resource(things_server_builder_s *builder, const char *uri) { things_resource_s *ret = NULL; for (size_t iter = 0; iter < builder->res_num; iter++) { iotivity_api_lock(); const char *rURI = OCGetResourceUri((OCResourceHandle)(builder->gres_arr[iter]->resource_handle)); iotivity_api_unlock(); if (things_string_compare(rURI, uri) == 0) { THINGS_LOG_D(TAG, "URI Compare : %s , %s", uri, rURI); ret = builder->gres_arr[iter]; break; } } return ret; }
OCRDPayload *OCRDPublishPayloadCreate(OCResourceHandle resourceHandles[], uint8_t nHandles, uint64_t ttl) { OCTagsPayload *tagsPayload = NULL; OCLinksPayload *linksPayload = NULL; OCStringLL *rt = NULL; OCStringLL *itf = NULL; OCStringLL *mt = NULL; OCResourceProperty p = OC_RES_PROP_NONE; uint8_t ins = 0; OCRDPayload *rdPayload = OCRDPayloadCreate(); if (!rdPayload) { return NULL; } const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString(); tagsPayload = OCCopyTagsResources(NULL, id, ttl); if (!tagsPayload) { goto exit; } for (uint8_t j = 0; j < nHandles; j++) { OCResourceHandle handle = resourceHandles[j]; if (handle) { rt = NULL; itf = NULL; mt = NULL; ins = 0; const char *uri = OCGetResourceUri(handle); uint8_t numElement = 0; if (OC_STACK_OK == OCGetNumberOfResourceTypes(handle, &numElement)) { OCStackResult res = CreateStringLL(numElement, handle, OCGetResourceTypeName, &rt); if (OC_STACK_OK != res || !rt) { goto exit; } } if (OC_STACK_OK == OCGetNumberOfResourceInterfaces(handle, &numElement)) { OCStackResult res = CreateStringLL(numElement, handle, OCGetResourceInterfaceName, &itf); if (OC_STACK_OK != res || !itf) { goto exit; } } p = OCGetResourceProperties(handle); p = (OCResourceProperty) ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)); OCStackResult res = OCGetResourceIns(handle, &ins); if (OC_STACK_OK != res) { goto exit; } mt = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL)); if (!mt) { goto exit; } mt->value = OICStrdup(DEFAULT_MESSAGE_TYPE); if (!mt->value) { goto exit; } if (!linksPayload) { linksPayload = OCCopyLinksResources(uri, NULL, rt, itf, p, NULL, NULL, ins, ttl, mt);; if (!linksPayload) { goto exit; } } else { OCLinksPayload *temp = linksPayload; while (temp->next) { temp = temp->next; } temp->next = OCCopyLinksResources(uri, NULL, rt, itf, p, NULL, NULL, ins, ttl, mt); if (!temp->next) { goto exit; } } OCFreeOCStringLL(rt); OCFreeOCStringLL(itf); OCFreeOCStringLL(mt); } } rdPayload->rdPublish = OCCopyCollectionResource(tagsPayload, linksPayload); if (!rdPayload->rdPublish) { goto exit; } return rdPayload; exit: if (rt) { OCFreeOCStringLL(rt); } if (itf) { OCFreeOCStringLL(itf); } if (mt) { OCFreeOCStringLL(mt); } if (tagsPayload) { OCFreeTagsResource(tagsPayload); } if (linksPayload) { OCFreeLinksResource(linksPayload); } OCRDPayloadDestroy(rdPayload); return NULL; }
OCEntityHandlerResult entity_handler(OCEntityHandlerFlag flag, OCEntityHandlerRequest *entity_handler_request, void *callback) { THINGS_LOG_V(TAG, THINGS_FUNC_ENTRY); OCEntityHandlerResult eh_result = OC_EH_ERROR; // Validate pointer if (!entity_handler_request) { THINGS_LOG_E(TAG, "Invalid request pointer"); return eh_result; } const char *uri = OCGetResourceUri(entity_handler_request->resource); // Observe Request Handling if (flag & OC_OBSERVE_FLAG) { if (OC_OBSERVE_REGISTER == entity_handler_request->obsInfo.action) { THINGS_LOG_V(TAG, "Observe Requset on : %s ", uri); // 1. Check whether it's Observe request on the Collection Resource if (NULL != strstr(uri, URI_DEVICE_COL)) { //2. Check whether the query carriese the if=oic.if.b if ((strstr(entity_handler_request->query, OIC_INTERFACE_BATCH) == NULL)) { //3. If not batch then error THINGS_LOG_E(TAG, "Collection Resource Requires BATCH for Observing : %s", entity_handler_request->query); eh_result = OC_EH_BAD_REQ; goto RESPONSE_ERROR; } else { THINGS_LOG_E(TAG, "Receiving Observe Request Collection Resource"); } } } else if (OC_OBSERVE_DEREGISTER == entity_handler_request->obsInfo.action) { THINGS_LOG_V(TAG, "CancelObserve Request on : %s", uri); } } // Get/Post Request Handling if (flag & OC_REQUEST_FLAG) { if (things_get_reset_mask(RST_CONTROL_MODULE_DISABLE) == true) { THINGS_LOG_V(TAG, "Control Module Disable."); eh_result = OC_EH_NOT_ACCEPTABLE; } else if ((OC_REST_GET == entity_handler_request->method) || (OC_REST_POST == entity_handler_request->method)) { THINGS_LOG_V(TAG, "Request Handle : %x, Resource Handle : %x", entity_handler_request->requestHandle, entity_handler_request->resource); if (verify_request(entity_handler_request, uri, (int)entity_handler_request->method) > 0) { // IoTivity Stack Destroy the payload after receving result from this function // Therefore, we need to copy/clone the payload for the later use.. things_resource_s *resource = things_create_resource_inst(entity_handler_request->requestHandle, entity_handler_request->resource, entity_handler_request->query, entity_handler_request->payload); resource->things_set_dev_addr(resource, &(entity_handler_request->devAddr)); resource->req_type = entity_handler_request->method; eh_result = handle_message(resource); } else { THINGS_LOG_E(TAG, "Invalid Query in the Request : %s", entity_handler_request->query); } } else if (OC_REST_DELETE == entity_handler_request->method || OC_REST_PUT == entity_handler_request->method) { THINGS_LOG_E(TAG, "Delete/PUT Req. Handling is not supported Yet"); } else { THINGS_LOG_D(TAG, "Received unsupported method from client"); } } RESPONSE_ERROR: if (eh_result != OC_EH_SLOW && eh_result != OC_EH_OK) { // If the result is OC_EH_ERROR, the stack will remove the // received request in the stack. // If the reusult is OC_EH_SLOW, then the request will be // stored in the stack till the response goes out eh_result = send_response(entity_handler_request->requestHandle, entity_handler_request->resource, eh_result, uri, NULL); // Currently this code will not do anything... // Need to refactor later.. } THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return eh_result; }
things_resource_s *create_resource_inst_impl(void *requesthd, void *resourcehd, void *query, void *rep_payload) { things_resource_s *res = (things_resource_s *) things_malloc(sizeof(things_resource_s)); if (NULL == res) { THINGS_LOG_E(TAG, THINGS_MEMORY_ERROR); return NULL; } res->error = 0; // OC_EH_OK; res->dev_addr = NULL; res->things_set_dev_addr = &set_dev_addr; res->things_get_dev_addr = &get_dev_addr; res->things_set_uri = &set_uri; res->things_set_error = &set_error; res->things_set_command_id = &set_command_id; res->things_add_child = &add_child; res->things_get_children = &get_children; res->things_get_uri = &get_uri; res->things_get_res_type = &get_res_type; res->things_get_num_of_res_types = &get_num_of_res_types; res->things_get_inf_type = &get_inf_type; res->things_get_num_of_inf_types = &get_num_of_inf_types; res->things_set_representation = &set_representation; res->things_get_representation = &get_representation; res->things_get_query = &get_query; res->things_get_rep_payload = &get_rep_payload; res->things_create_payload = &create_payload; res->things_is_supporting_interface_type = &is_supporting_interface_type; res->things_is_supporting_resource_type = &is_supporting_resource_type; res->next = NULL; res->things_get_next = &get_next; res->resource_handle = resourcehd; res->request_handle = requesthd; res->uri = NULL; const char *uri = OCGetResourceUri(resourcehd); if (uri != NULL && strlen(uri) > 0) { res->uri = (char *)things_malloc(sizeof(char) * strlen(uri) + 1); memset(res->uri, 0, strlen(uri) + 1); things_strncpy(res->uri, uri, strlen(uri)); } res->res_type = NULL; res->interface_type = NULL; res->req_type = 0; res->cmd_id = NULL; res->rep = NULL; res->query = NULL; res->size = 1; if (NULL != query) { things_string_duplicate((char *)query, &(res->query)); } if (NULL != rep_payload) { res->rep = things_create_representation_inst(rep_payload); } // else // { // THINGS_LOG_D(TAG, "Representation not created!!"); // } return res; }