/** * DA Stack invokes this callback for providing ownership transfer states. */ void ownership_transfer_state_cb(const char *addr, uint16_t port, const char *uuid, int event) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); THINGS_LOG_D(TAG, "Received event for ownership-transfer state change (%d)", event); (void)addr; (void)port; (void)uuid; if (NULL == g_handle_things_status_change_cb) { THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return; } switch (event) { case 1: // OTM Started g_handle_things_status_change_cb(ST_THINGS_STATUS_ES_STARTED); break; case 2: // OTM Done g_handle_things_status_change_cb(ST_THINGS_STATUS_ES_DONE); break; case 3: // OTM Error g_handle_things_status_change_cb(ST_THINGS_STATUS_ES_FAILED_ON_OWNERSHIP_TRANSFER); break; } THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); }
things_resource_s *things_create_resource_inst(OCRequestHandle requesthd, OCResourceHandle resourcehd, void *query, void *rep_payload) { things_resource_s *res = create_resource_inst_impl(requesthd, resourcehd, query, rep_payload); if (NULL != rep_payload) { THINGS_LOG_D(TAG, "Representation Inserted to Clone~!!!"); OCRepPayload *pl = ((OCRepPayload *) rep_payload)->next; things_resource_s *pTempRes = NULL; while (pl) { if (NULL != pl->uri) { pTempRes = create_resource_inst_impl(requesthd, resourcehd, NULL, pl); THINGS_LOG_D(TAG, "Child Resource URI : %s", pl->uri); pTempRes->things_set_uri(pTempRes, pl->uri); res->things_add_child(res, pTempRes); //res->size++; } else { THINGS_LOG_D(TAG, "No Uri in this payload for child, this payload was ignored."); } pl = pl->next; } THINGS_LOG_D(TAG, "\t Child Count : %d", res->size); } else { THINGS_LOG_D(TAG, "No Representation to Clone : %x", rep_payload); } return res; }
int handle_set_request_cb(struct things_resource_s *resource) { THINGS_LOG_D(TAG, "Received a SET request callback"); RET_VAL_IF_PARAM_IS_NULL(TAG, resource, 0); RET_VAL_IF_NULL(TAG, g_handle_set_req_cb, "Request callback for SET is NULL", 0); RET_VAL_IF_EXPR_IS_TRUE(TAG, (NULL == resource->uri || strlen(resource->uri) < 1), "Resource URI is invalid", 0); int result = 0; #ifdef CONFIG_ST_THINGS_COLLECTION bool collection = things_is_collection_resource(resource->uri); #else bool collection = false; #endif THINGS_LOG_D(TAG, "Resource URI : %s (%s resource)", resource->uri, collection ? "collection" : "single"); THINGS_LOG_D(TAG, "Query Parameter: %s", resource->query); if (collection) { #ifdef CONFIG_ST_THINGS_COLLECTION result = handle_post_req_on_collection_rsrc(resource); #endif } else { result = handle_post_req_on_single_rsrc(resource); } if (result) { THINGS_LOG_D(TAG, "Handled SET request for %s resource successfully.", collection ? "collection" : "single"); } else { THINGS_LOG_D(TAG, "Failed to handle the SET request for %s resource.", collection ? "collection" : "single"); } return result; }
things_resource_s *clone_resource_inst(things_resource_s *pori) { if (pori == NULL) { return NULL; } things_resource_s *pclone = create_resource_inst_impl(pori->request_handle, pori->resource_handle, pori->query, (pori->rep == NULL) ? NULL : pori->rep->payload); pclone->things_set_uri(pclone, pori->uri); THINGS_LOG_D(TAG, "@@@@@@@@@@@@@@@ URI %s", pclone->uri); things_resource_s *p_temp = pori->next; while (p_temp) { things_resource_s *p_new = create_resource_inst_impl(p_temp->request_handle, p_temp->resource_handle, p_temp->query, ((p_temp->rep == NULL) ? NULL : p_temp->rep->payload)); p_new->things_set_uri(p_new, p_temp->uri); p_temp->things_add_child(p_temp, p_new); p_temp = p_temp->next; } THINGS_LOG_D(TAG, "@@@@@@@@@@@@@@@ Address of cloned %x", pclone); return pclone; }
bool is_supporting_interface_type(struct things_resource_s *res, char *query) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); bool result = false; uint8_t number_of_interfaces = 0; OCGetNumberOfResourceInterfaces((OCResourceHandle) res->resource_handle, &number_of_interfaces); THINGS_LOG_D(TAG, "@@ IF # : %d ", number_of_interfaces); //2.a Find interface type(s) & insert it/them into payload for (uint8_t index = 0; index < number_of_interfaces; index++) { const char *interface = OCGetResourceInterfaceName((OCResourceHandle) res->resource_handle, index); // THINGS_LOG_D(TAG, "Supporting Interface :: %s(%s)", query, interface); if (NULL != query) { if (strstr(query, interface) != NULL) { // THINGS_LOG_D(TAG, "Confirm Interface."); result = true; break; } } } THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return result; }
int st_things_reset(void) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); if (STACK_STARTED != g_stack_status) { int ret_val = ST_THINGS_ERROR_OPERATION_FAILED; switch (g_stack_status) { case STACK_NOT_INITIALIZED: THINGS_LOG_E(TAG, "Stack is not initialized. Before reset, stack should be initialized and started."); ret_val = ST_THINGS_ERROR_STACK_NOT_INITIALIZED; break; case STACK_INITIALIZED: THINGS_LOG_E(TAG, "Stack is initialized but not started."); ret_val = ST_THINGS_ERROR_STACK_NOT_STARTED; break; default: THINGS_LOG_E(TAG, "Invalid stack state: %d.", g_stack_status); break; } THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return ret_val; } int result = 0; if (1 != (result = things_reset(NULL, RST_AUTO_RESET))) { THINGS_LOG_E(TAG, "things_reset failed (result:%d)", result); THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return ST_THINGS_ERROR_OPERATION_FAILED; } THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return ST_THINGS_ERROR_NONE; }
bool things_get_string_arrayvalue(struct things_representation_s *mother, char *key, int *length, char ***array) { THINGS_LOG_D(TAG, "There're (%d) Number of children resources in the Payload : %d", mother->num_children); bool find_value = false; OCRepPayloadValue *payload_value = NULL; payload_value = things_rep_payload_find_values((OCRepPayload *)(mother->payload), key); if (payload_value != NULL) { size_t dimension_size = calcDimTotal(payload_value->arr.dimensions); size_t dimensions[3] = { dimension_size, 0, 0 }; find_value = OCRepPayloadGetStringArray((OCRepPayload *)(mother->payload), key, array, dimensions); THINGS_LOG_D(TAG, "Find Value : %d", find_value); if (find_value) { *length = dimension_size; } } else { THINGS_LOG_E(TAG, "DATA NOT EXIST~!!!!"); } return find_value; }
st_things_representation_s *st_things_create_representation_inst() { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); st_things_representation_s *rep = create_representation_inst(); THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return rep; }
void notify_result_of_reset(things_resource_s *target_resource, bool result) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); OCEntityHandlerResult eh_result = OC_EH_ERROR; if (target_resource == NULL) { THINGS_LOG_D(TAG, "Not exist remote-client."); return; } if (result == true) { // reset Success. OCRepPayload *rep_payload = target_resource->things_get_rep_payload(target_resource); eh_result = send_response(target_resource->request_handle, // reqInfo->reqHandle, target_resource->resource_handle, // reqInfo->resHandle, target_resource->error, target_resource->uri, rep_payload); OCPayloadDestroy((OCPayload *) rep_payload); rep_payload = NULL; } else { THINGS_LOG_E(TAG, "Handing Request Failed, Sending ERROR Response"); send_response(target_resource->request_handle, target_resource->resource_handle, eh_result, target_resource->uri, NULL); } things_release_resource_inst(target_resource); THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); }
void set_device_info(things_server_builder_s *builder, char *device_name, char *device_type) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); THINGS_LOG_D(TAG, "[/oic/d] name :%s", device_name); THINGS_LOG_D(TAG, "[/oic/d] type :%s", device_type); OCDeviceInfo device_info; device_info.deviceName = NULL; device_info.types = NULL; device_info.specVersion = NULL; device_info.dataModelVersions = NULL; things_string_duplicate(device_name, &device_info.deviceName); things_string_duplicate(OC_SPEC_VERSION, &device_info.specVersion); device_info.dataModelVersions = OCCreateOCStringLL(DEFAULT_DATA_MODEL_VERSIONS); device_info.types = OCCreateOCStringLL(device_type); OCResourcePayloadAddStringLL(&device_info.types, OC_RSRVD_RESOURCE_TYPE_DEVICE); iotivity_api_lock(); OCSetDeviceInfo(device_info); iotivity_api_unlock(); // OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, KEY_ATTR_DEVICE_NAME, device_name); things_free(device_info.deviceName); things_free(device_info.specVersion); OCFreeOCStringLL(device_info.dataModelVersions); OCFreeOCStringLL(device_info.types); THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); }
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; }
/** * Callback for getting the pin for random pin based ownership transfer. */ void generated_pin_cb(char *pin_data, size_t pin_size) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); if (NULL != g_handle_pin_generated_cb) { g_handle_pin_generated_cb(pin_data, pin_size); } THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); }
/** * Callback for getting the close pin display request after random pin based ownership transfer. */ void close_pin_display_cb(void) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); if (NULL != g_handle_pin_display_close_cb) { g_handle_pin_display_close_cb(); } THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); }
const char *get_inf_type(struct things_resource_s *res, int index) { const char *interface_type = OCGetResourceInterfaceName((OCResourceHandle) res->resource_handle, index); THINGS_LOG_D(TAG, "=====> RH : %x cnt : %d", res->resource_handle, index); THINGS_LOG_D(TAG, "=====> IT : %s ", interface_type); return interface_type; }
const char *get_res_type(struct things_resource_s *res, int index) { const char *resourcType = OCGetResourceTypeName((OCResourceHandle) res->resource_handle, index); THINGS_LOG_D(TAG, "=====> RH : %x cnt : %d", res->resource_handle, index); THINGS_LOG_D(TAG, "=====> RT : %s ", resourcType); return resourcType; }
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 callback will be invoked by DA Stack with the result of reset. * Result will be passed to the application through its registered callback. */ void get_reset_result_cb(int result) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); THINGS_LOG_D(TAG, "Result of reset is %d", result); bool res = (result == 1) ? true : false; if (NULL != g_handle_reset_result_cb) { g_handle_reset_result_cb(res); } THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); }
int st_things_register_request_cb(st_things_get_request_cb get_cb, st_things_set_request_cb set_cb) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); if (NULL == get_cb || NULL == set_cb) { THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return ST_THINGS_ERROR_INVALID_PARAMETER; } THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return register_request_handler_cb(get_cb, set_cb); }
int st_things_register_things_status_change_cb(st_things_status_change_cb status_cb) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); if (NULL == status_cb) { THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return ST_THINGS_ERROR_INVALID_PARAMETER; } g_handle_things_status_change_cb = status_cb; THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return ST_THINGS_ERROR_NONE; }
int st_things_register_user_confirm_cb(st_things_user_confirm_cb confirm_cb) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); if (NULL == confirm_cb) { THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return ST_THINGS_ERROR_INVALID_PARAMETER; } g_handle_user_confirm_cb = confirm_cb; THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return ST_THINGS_ERROR_NONE; }
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; }
int st_things_register_pin_handling_cb(st_things_pin_generated_cb generated_cb, st_things_pin_display_close_cb close_cb) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); if (NULL == generated_cb || NULL == close_cb) { THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return ST_THINGS_ERROR_INVALID_PARAMETER; } g_handle_pin_generated_cb = generated_cb; g_handle_pin_display_close_cb = close_cb; THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return ST_THINGS_ERROR_NONE; }
/** * Callback for getting users confirmation for MUTUAL VERIFICATION BASED JUST WORK Ownership transfer */ int get_user_confirm_cb(void) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); if (NULL == g_handle_user_confirm_cb) { THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return 0; } bool confirm = g_handle_user_confirm_cb(); THINGS_LOG_D(TAG, "User's confirmation for MUTUAL VERIFICATION BASED JUST WORK Ownership-transfer : %s", confirm ? "true" : "false"); THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return confirm ? 1 : 0; }
int notify_things_observers(const char *uri, const char *query) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); int res = 0; THINGS_LOG_D(TAG, "uri = %s", uri); if (NULL != uri) { int remainLen = MAX_RESOURCE_LEN - 1; char tempUri[MAX_RESOURCE_LEN] = { 0 }; if (NULL != g_get_notify_obs_uri) { char *temp = g_get_notify_obs_uri(uri, query); if (NULL != temp) { things_strncpy(tempUri, temp, remainLen); remainLen -= strnlen(tempUri, MAX_RESOURCE_LEN - 1); things_free(temp); } } if (strnlen(tempUri, MAX_RESOURCE_LEN - 1) < 1) { things_strncpy(tempUri, uri, remainLen); remainLen -= strnlen(tempUri, MAX_RESOURCE_LEN - 1); } THINGS_LOG_D(TAG, "%s resource notifies to observers.", tempUri); for (int iter = 0; iter < g_builder->res_num; iter++) { if (strstr(g_builder->gres_arr[iter]->uri, tempUri) != NULL) { OCStackResult ret2 = OCNotifyAllObservers((OCResourceHandle) g_builder->gres_arr[iter]->resource_handle, OC_MEDIUM_QOS); THINGS_LOG_D(TAG, "%s resource has notified to observers.", g_builder->gres_arr[iter]->uri); if (OC_STACK_OK == ret2) { THINGS_LOG_V(TAG, "Success: Sent notification to Observers"); } else { THINGS_LOG_V(TAG, "Failed: No Observers to notify : %d ", ret2); } res = 1; break; } } } THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return res; }
static OCEntityHandlerResult process_get_request(things_resource_s *target_resource) { OCEntityHandlerResult eh_result = OC_EH_ERROR; char *device_id = NULL; device_id = strrchr(target_resource->uri, '/') + 1; THINGS_LOG_D(TAG, "Get Device ID in Resources URI = %s", device_id); things_representation_s *rep = things_create_representation_inst(NULL); target_resource->things_set_representation(target_resource, rep); if (strstr(target_resource->uri, URI_SEC) != NULL && strstr(target_resource->uri, URI_PROVINFO) != NULL) { // 1. Get request for the Access point list eh_result = get_provisioning_info(target_resource); #ifdef CONFIG_ST_THINGS_FOTA } else if (strstr(target_resource->uri, URI_FIRMWARE) != NULL) { eh_result = fmwup_get_data(target_resource); #endif } else { if (g_handle_request_get_cb != NULL) { int ret = g_handle_request_get_cb(target_resource); if (1 == ret) { eh_result = OC_EH_OK; } else { THINGS_LOG_E(TAG, "Handled as ERROR from App."); } } else { THINGS_LOG_E(TAG, "g_handle_request_get_cb is not registered"); } } return eh_result; }
void add_child(things_resource_s *root, things_resource_s *child) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); things_resource_s *p_node = NULL; p_node = root; while (NULL != p_node->things_get_next(p_node)) { p_node = p_node->things_get_next(p_node); } p_node->next = child; root->size++; THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); }
bool is_property_key_exist(struct _st_things_get_request_message *req_msg, const char *key) { RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg); RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg->property_key); RET_FALSE_IF_EXPR_IS_TRUE(TAG, strlen(req_msg->property_key) < 1, "Property key in request message is empty."); RET_FALSE_IF_PARAM_IS_NULL(TAG, key); RET_FALSE_IF_EXPR_IS_TRUE(TAG, strlen(key) < 1, "Key is empty."); int key_len = strlen(key); bool exist = false; char *key_ptr = req_msg->property_key; while (NULL != key_ptr && !exist) { key_ptr = strstr(key_ptr, key); if (NULL == key_ptr) { break; } // The following logic ensures that the key is a complete key and not a substring of another key. // Check whether a delimeter immediately follows the key. if (PROPERTY_KEY_DELIMITER != key_ptr[key_len]) { key_ptr += key_len; continue; } // If key is not at the begining, then check whether there is a delimiter immediately before the key. if (key_ptr != req_msg->property_key && PROPERTY_KEY_DELIMITER != key_ptr[-1]) { key_ptr += key_len; continue; } exist = true; } THINGS_LOG_D(TAG, "Key(%s) exist?: %s.", key, exist ? "Yes" : "No"); return exist; }
things_server_builder_s *get_builder_instance() { if (g_builder == NULL) { g_builder = (things_server_builder_s *) things_malloc(sizeof(things_server_builder_s)); if (g_builder != NULL) { g_builder->init_module = &init_builder; g_builder->deinit_module = &deinit_builder; g_builder->set_device_info = &set_device_info; g_builder->set_platform_info = &set_platform_info; g_builder->create_resource = &create_resource; // g_builder->CreateActiveResource = &CreateActiveResource; #ifdef CONFIG_ST_THINGS_COLLECTION g_builder->create_collection_resource = &create_collection_resource; #endif g_builder->get_resource = &get_resource; g_builder->delete_resource = &delete_resource; g_builder->add_interface_type = &add_interface_type; g_builder->add_resource_type = &add_resource_type; g_builder->bind = &things_bind; g_builder->bind_all = &bind_all; g_builder->res_num = 0; g_builder->handler = NULL; return g_builder; } else { THINGS_LOG_E(TAG, "Not enough Memory for Builder Instance"); return NULL; } } else { THINGS_LOG_D(TAG, "Builder Instance Already Created"); return g_builder; } }
OCEntityHandlerResult handle_message(things_resource_s *target_resource) { OCEntityHandlerResult eh_result = OC_EH_ERROR; if (NULL == target_resource) { THINGS_LOG_D(TAG, "Request Item is NULL."); return OC_EH_ERROR; } THINGS_LOG_D(TAG, "Request Handle : %x, Resource Handle : %x", target_resource->request_handle, target_resource->resource_handle); if (target_resource->req_type == OC_REST_GET) { THINGS_LOG_V(TAG, "\t\tReq. : GET on %s", target_resource->uri); THINGS_LOG_V(TAG, "\t\tQuery: %s", target_resource->query); eh_result = process_get_request(target_resource); } else if (target_resource->req_type == OC_REST_POST) { THINGS_LOG_V(TAG, "\t\tReq. : POST on %s", target_resource->uri); THINGS_LOG_V(TAG, "\t\tQuery: %s", target_resource->query); eh_result = process_post_request(&target_resource); } else { THINGS_LOG_E(TAG, " Invalid Request Received : %d", target_resource->req_type); } THINGS_LOG_D(TAG, " @@@@@ target_resource ->size : %d", target_resource->size); if (is_can_not_response_case(target_resource, target_resource->req_type, eh_result) == false) { if (eh_result != OC_EH_OK && eh_result != OC_EH_SLOW) { THINGS_LOG_E(TAG, "Handing Request Failed, Sending ERROR Response"); send_response(target_resource->request_handle, target_resource->resource_handle, eh_result, target_resource->uri, NULL); eh_result = OC_EH_OK; } else { OCRepPayload *rep_payload = target_resource->things_get_rep_payload(target_resource); eh_result = send_response(target_resource->request_handle, // reqInfo->reqHandle, target_resource->resource_handle, // reqInfo->resHandle, target_resource->error, target_resource->uri, rep_payload); OCPayloadDestroy((OCPayload *) rep_payload); rep_payload = NULL; } } // Need to design How to release memory allocated for the things_resource_s list. things_release_resource_inst(target_resource); return eh_result; }
void /*OCRepPayload */ *create_payload(struct things_resource_s *res, char *query) { // To provide identical pattern for handling the result // allocating new memory for the payload to delete later OCRepPayload *payload = OCRepPayloadClone((OCRepPayload *)(res->rep->payload)); THINGS_LOG_D(TAG, "Query : %s", query); if (query == NULL || (query != NULL && strlen(query) < 1) || strstr(query, OIC_INTERFACE_ACTUATOR) != NULL || strstr(query, OIC_INTERFACE_SENSOR) != NULL) { THINGS_LOG_D(TAG, "Including properties & its values only"); // Do nothing.. } else if (strstr(query, OIC_INTERFACE_BASELINE)) { THINGS_LOG_D(TAG, "Including all the properties & its values"); uint8_t index = 0; uint8_t number_of_interfaces = 0; uint8_t number_of_resource_type = 0; OCGetNumberOfResourceInterfaces((OCResourceHandle) res->resource_handle, &number_of_interfaces); THINGS_LOG_D(TAG, "@@ IF # : %d ", number_of_interfaces); //2.a Find interface type(s) & insert it/them into payload for (index = 0; index < number_of_interfaces; index++) { const char *interface = OCGetResourceInterfaceName((OCResourceHandle) res->resource_handle, index); THINGS_LOG_D(TAG, "=====> IF : %s ", interface); OCRepPayloadAddInterface(payload, interface); } //3.a Find resource type & insert it into payload OCGetNumberOfResourceTypes((OCResourceHandle) res->resource_handle, &number_of_resource_type); THINGS_LOG_D(TAG, "@@ RT # : %d ", number_of_resource_type); for (index = 0; index < number_of_resource_type; index++) { const char *rt = OCGetResourceTypeName((OCResourceHandle) res->resource_handle, index); THINGS_LOG_D(TAG, "=====> RT : %s ", rt); OCRepPayloadAddResourceType(payload, rt); } } else if (strstr(query, OC_RSRVD_INTERFACE_BATCH)) { THINGS_LOG_D(TAG, "Batch only supported by Collection Resource"); } else if (strstr(query, OC_RSRVD_INTERFACE_LL)) { THINGS_LOG_D(TAG, "Link-List only supported by Collection Resource"); } else { THINGS_LOG_D(TAG, "Not supporting Interface type : %s", query); } return payload; }