struct things_resource_s *create_resource(struct things_server_builder_s *builder, char *uri, char *type, char *interface, int isDiscoverable, int isObserable, int isSecure) { things_resource_s *res = NULL; OCResourceHandle hd = NULL; uint8_t rsc_properties = OC_DISCOVERABLE; if (builder->handler == NULL) { THINGS_LOG_E(TAG, "Handler for serverbuilder is not registered"); return res; } if (1 != isDiscoverable) { rsc_properties = OC_ACTIVE; } if (1 == isObserable) { rsc_properties |= OC_OBSERVABLE; } if (1 == isSecure) { #ifdef __SECURED__ rsc_properties |= OC_SECURE; #else THINGS_LOG_D(TAG, "Stack is in UNSECURED Mode"); #endif } iotivity_api_lock(); OCStackResult ret = OCCreateResource(&hd, type, interface, uri, builder->handler, NULL, rsc_properties); iotivity_api_unlock(); if (ret != OC_STACK_OK) { THINGS_LOG_V(TAG, "Resource Creation Failed - ret = %d, %s", ret, uri); return NULL; } res = things_create_resource_inst(NULL, hd, NULL, NULL); if (NULL == res) { THINGS_LOG_E(TAG, "things_create_resource_inst is failed"); return res; } res->res_type = things_strdup(type); builder->gres_arr[builder->res_num++] = res; THINGS_LOG_D(TAG, "Created hd [%x], prop [0x%X] uri : %s", res->resource_handle, rsc_properties, res->uri); THINGS_LOG_D(TAG, "DISCOVERABLE : %s", (isDiscoverable == 1 ? "YES" : "NO")); THINGS_LOG_D(TAG, "OBSERABLE : %s", (isObserable == 1 ? "YES" : "NO")); THINGS_LOG_D(TAG, "SECURE : %s", (isSecure == 1 ? "YES" : "NO")); return res; }
struct things_resource_s *create_collection_resource(struct things_server_builder_s *builder, char *uri, char *type) { things_resource_s *res = NULL; OCResourceHandle hd = NULL; uint8_t rsc_properties = OC_DISCOVERABLE | OC_OBSERVABLE; if (builder->handler == NULL) { THINGS_LOG_E(TAG, "Handler for serverbuilder is not registered"); return res; } #ifdef __SECURED__ rsc_properties |= OC_SECURE; #endif //#ifdef __SECURED__ iotivity_api_lock(); OCStackResult ret = OCCreateResource(&hd, type, OIC_INTERFACE_LINKEDLIST, uri, builder->handler, NULL, rsc_properties); if (ret != OC_STACK_OK) { THINGS_LOG_V(TAG, "Resource Creation Failed - ret = %d, %s", ret, uri); iotivity_api_unlock(); return NULL; } OCBindResourceTypeToResource(hd, OIC_RTYPE_COLLECTION_WK); OCBindResourceInterfaceToResource(hd, OIC_INTERFACE_BATCH); iotivity_api_unlock(); res = things_create_resource_inst(NULL, hd, NULL, NULL); if (NULL == res) { THINGS_LOG_E(TAG, "things_create_resource_inst is failed"); return res; } res->res_type = things_strdup(type); builder->gres_arr[builder->res_num++] = res; THINGS_LOG_V(TAG, "Created hd [%x], prop [0x%X] uri : %s", res->resource_handle, rsc_properties, uri); return res; }
/** * Return : 0 (Invalid Request), 1 (Resource Supporting Interface) * Need refactoring later... */ static int verify_request(OCEntityHandlerRequest *eh_request, const char *uri, int req_type) { int result = 0; things_resource_s *pst_temp_resource = NULL; if (g_builder == NULL) { THINGS_LOG_E(TAG, "Server Builder is not registered.."); goto EXIT_VALIDATION; } things_resource_s *resource = things_create_resource_inst(eh_request->requestHandle, eh_request->resource, eh_request->query, eh_request->payload); //g_builder->get_resource(g_builder, uri); things_resource_s *child = NULL; /*! Added by st_things for memory Leak fix */ pst_temp_resource = resource; if (resource == NULL) { THINGS_LOG_E(TAG, "Resource Not found : %s ", uri); goto EXIT_VALIDATION; } THINGS_LOG_D(TAG, "Query in the Request : %s", eh_request->query); // Verify received request in valid or not.. // 1. If there's no interface type in the request .. if ((strstr(eh_request->query, "if=") == NULL) || (strstr(eh_request->query, OIC_INTERFACE_BASELINE) != NULL) || (true == resource->things_is_supporting_interface_type(resource, eh_request->query))) { result = 1; } // 2. If request type == POST(OC_REST_POST == 4), // then validate the attributes kyes in the request if (result == 1 && req_type == OC_REST_POST) { // Reseting the result value to apply // the result of additional verification result = 0; // If the given resource does not have sensor nor read interface type.. if ((resource->things_is_supporting_interface_type(resource, OIC_INTERFACE_ACTUATOR)) || (!(resource->things_is_supporting_interface_type(resource, OIC_INTERFACE_SENSOR)) && !(resource->things_is_supporting_interface_type(resource, OC_RSRVD_INTERFACE_READ)))) { // If no query or.. // query with "supporting interface types" then... if ((strstr(eh_request->query, "if=") == NULL) || ((strstr(eh_request->query, "if=") != NULL) && (resource->things_is_supporting_interface_type(resource, eh_request->query)) ) ) { // Verify request(Attributes.) with Validator func implemented const int num = resource->things_get_num_of_res_types(resource); THINGS_LOG_D(TAG, "# of RT :%d", num); if (num > 0) { for (int iter = 0; iter < num; iter++) { // if it's okey in any case then....it's valid.. result |= dm_validate_attribute_in_request(resource->things_get_res_type(resource, iter), (const void *)eh_request->payload); } } } } } EXIT_VALIDATION: /*! Added by st_things for memory Leak fix */ things_release_resource_inst(pst_temp_resource); THINGS_LOG_D(TAG, "Validation Result.(%d)", result); return result; }
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; }