int webclient_init_request(char *url, struct http_client_request_t *request) { int ret = -1; memset(request, 0, sizeof(struct http_client_request_t)); request->method = WGET_MODE_GET; request->url = (char *)things_malloc(strlen(url) + 1); if (!request->url) { return ret; } strncpy(request->url, url, strlen(url)); request->url[strlen(url)] = '\0'; #ifdef CONFIG_NET_SECURITY_TLS if (!strncmp(request->url, "https", 5)) { g_https = 1; } else #endif if (!strncmp(request->url, "http", 4)) { g_https = 0; } else { return ret; } request->buflen = FOTA_WEBCLIENT_BUF_SIZE; ret = 0; return ret; }
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; } }
// Getter should be refactored to deliver the request value as its return // not through the inserted param. void get_uri(struct things_resource_s *res, char **value) { if (res->uri != NULL) { *value = (char *)things_malloc(sizeof(char) *strlen(res->uri) + 1); memset(*value, 0, strlen(res->uri) + 1); things_strncpy(*value, res->uri, strlen(res->uri) + 1); } }
void set_dev_addr(struct things_resource_s *p_res, void *dev_addr) { if (dev_addr == NULL) { p_res->dev_addr = NULL; return; } (p_res->dev_addr) = (OCDevAddr *) things_malloc(sizeof(OCDevAddr)); if (NULL != p_res->dev_addr) { memcpy((p_res->dev_addr), dev_addr, sizeof(OCDevAddr)); } // p_res->dev_addr = dev_addr; }
struct things_request_handler_s *get_handler_instance() { struct things_request_handler_s *handler = (things_request_handler_s *) things_malloc(sizeof(things_request_handler_s)); if (handler == NULL) { THINGS_LOG_E(TAG, "Not Enough Memory"); return NULL; } else { handler->entity_handler = &entity_handler; handler->init_module = &init_handler; handler->deinit_module = &deinit_handler; handler->notify_things_observers = ¬ify_things_observers; return handler; } }
things_representation_s *things_create_representation_inst(void *rep_payload) { things_representation_s *rep = (things_representation_s *) things_malloc(sizeof(things_representation_s)); if (NULL == rep) { THINGS_LOG_E(TAG, THINGS_MEMORY_ERROR); return NULL; } rep->things_set_value = &things_set_value; rep->things_set_bool_value = &things_set_bool_value; rep->things_set_double_value = &things_set_double_value; rep->things_set_int_value = &things_set_int_value; rep->things_set_byte_value = &things_set_byte_value; rep->things_set_object_value = &things_set_object_value; rep->things_set_arrayvalue = &things_set_arrayvalue; rep->things_get_arrayvalue = &things_get_arrayvalue; rep->things_set_string_arrayvalue = &things_set_string_arrayvalue; rep->things_get_string_arrayvalue = &things_get_string_arrayvalue; rep->things_set_double_arrayvalue = &things_set_double_arrayvalue; rep->things_get_double_arrayvalue = &things_get_double_arrayvalue; rep->things_set_int_arrayvalue = &things_set_int_arrayvalue; rep->things_get_int_arrayvalue = &things_get_int_arrayvalue; rep->things_get_value = &things_get_value; rep->things_get_bool_value = &things_get_bool_value; rep->things_get_int_value = &things_get_int_value; rep->things_get_double_value = &things_get_double_value; rep->things_get_byte_value = &things_get_byte_value; rep->things_get_object_value = &things_get_object_value; rep->payload = NULL; rep->children_payload = NULL; rep->children = NULL; rep->num_children = 0; if (rep_payload != NULL) { rep->payload = OCRepPayloadClone((OCRepPayload *) rep_payload); } else { rep->payload = OCRepPayloadCreate(); } return rep; }
static st_things_get_request_message_s *create_req_msg_inst_for_get(const char *res_uri, const char *query) { st_things_get_request_message_s *req_msg = (st_things_get_request_message_s *) things_malloc(sizeof(st_things_get_request_message_s)); RET_VAL_IF_NULL(TAG, req_msg, "Failed to allocate memory for GET request message.", NULL); req_msg->resource_uri = things_clone_string(res_uri); RET_VAL_IF_NULL(TAG, req_msg->resource_uri, "Failed to clone the resource uri.", NULL); if (NULL != query && strlen(query) > 1) { req_msg->query = things_clone_string(query); } else { req_msg->query = NULL; } req_msg->property_key = NULL; req_msg->get_query_value = &get_query_value_for_get_req; req_msg->has_property_key = &is_property_key_exist; return req_msg; }
bool things_get_byte_value(struct things_representation_s *rep, char *key, uint8_t **value, size_t *size) { OCByteString b_val = { NULL, 0 }; bool ret = OCRepPayloadGetPropByteString((OCRepPayload *) rep->payload, key, &b_val); if (true == ret) { (*size) = b_val.len; (*value) = (uint8_t *) things_malloc((*size) + 1); memcpy((*value), b_val.bytes, b_val.len); if (b_val.bytes != NULL) { things_free(b_val.bytes); b_val.bytes = NULL; } } return ret; }
void *things_realloc(void *ptr, size_t size) { // Override realloc() behavior for NULL pointer which normally would // work as per malloc(), however we suppress the behavior of possibly // returning a non-null unique pointer. if (ptr == NULL) { return things_malloc(size); } // Otherwise leave the behavior up to realloc() itself: #ifdef ENABLE_MALLOC_DEBUG void *newptr = realloc(ptr, size); THINGS_LOG_D(TAG, "realloc: ptr=%p, newptr=%p, size=%u", ptr, newptr, size); // Very important to return the correct pointer here, as it only *somtimes* // differs and thus can be hard to notice/test: return newptr; #else return realloc(ptr, size); #endif }
bool things_get_arrayvalue(struct things_representation_s *mother, char *key, int *length, struct things_representation_s ***children) { //THINGS_LOG_E(TAG, "NOt Supported This Function Yet"); bool find_value = false; THINGS_LOG_D(TAG, "There're (%d) Number of children resources in the Payload : %d", mother->num_children); // if( OCRepPayloadGetPropInt((OCRepPayload*)(mother->payload), SEC_ATTRIBUTE_LENGTH, &(mother->num_children) ) ) OCRepPayloadValue *payload_value = things_rep_payload_find_values((OCRepPayload *)(mother->payload), key); if (NULL != payload_value) { OCRepPayload **payload_values = NULL; size_t dimension_size = calcDimTotal(payload_value->arr.dimensions); size_t dimensions[3] = { dimension_size, 0, 0 }; THINGS_LOG_D(TAG, "Dimension size in the Payload : %d", dimension_size); // This is testing code only... will be removed... find_value = OCRepPayloadGetPropObjectArray((OCRepPayload *)(mother->payload), key, &payload_values, dimensions); THINGS_LOG_D(TAG, "Find Value : %d", find_value); if (find_value) { *children = (things_representation_s **) things_malloc(sizeof(things_representation_s *) *dimension_size); for (int iter = 0; iter < dimension_size; iter++) { (*children)[iter] = things_create_representation_inst(payload_values[iter]); /*! Added by st_things for memory Leak fix */ OCRepPayloadDestroy(payload_values[iter]); } /*! Added by st_things for memory Leak fix */ things_free(payload_values); *length = mother->num_children = dimension_size; } } else { THINGS_LOG_E(TAG, "DATA NOT EXIST~!!!!"); } return find_value; }
static st_things_set_request_message_s *create_req_msg_inst_for_post(const char *res_uri, const char *query, OCRepPayload *req_payload) { st_things_set_request_message_s *req_msg = (st_things_set_request_message_s *) things_malloc(sizeof(st_things_set_request_message_s)); RET_VAL_IF_NULL(TAG, req_msg, "Failed to allocate memory for SET request message.", NULL); req_msg->resource_uri = things_clone_string(res_uri); RET_VAL_IF_NULL(TAG, req_msg->resource_uri, "Failed to clone the resource uri.", NULL); if (NULL != query && strlen(query) > 1) { req_msg->query = things_clone_string(query); } else { req_msg->query = NULL; } req_msg->rep = create_representation_inst_internal(req_payload); if (NULL == req_msg->rep) { THINGS_LOG_E(TAG, "Failed to create representation for SET request message."); destroy_req_msg_inst_for_post(req_msg, false); return NULL; } req_msg->get_query_value = &get_query_value_for_post_req; return req_msg; }
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); }
int fmwup_http_download_file(const char *download_url) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); download_state = FOTA_DOWNLOAD_STATE_JSON; json_str = (char *)things_malloc(FOTA_REC_JSON_SIZE); recv_size = 0; is_link_fail = false; // parsing json if (wget_from_url(download_url) < 0) { THINGS_LOG_E(TAG, "wget_from_url error"); things_free(json_str); download_state = FOTA_DOWNLOAD_STATE_NONE; return -1; } json_str[recv_size] = 0; if (is_link_fail) { things_free(json_str); download_state = FOTA_DOWNLOAD_STATE_NONE; return -1; } THINGS_LOG_D(TAG, "[recv:JSON] state : %d / recv_size : %u / total size : %u / json = %s", download_state, recv_size, total_size, json_str); if (recv_size != total_size) { THINGS_LOG_E(TAG, "[recv:JSON] file size error"); things_free(json_str); download_state = FOTA_DOWNLOAD_STATE_NONE; return -1; } fotahal_handle = fotahal_open(); if (fotahal_handle == NULL) { fotahal_close(fotahal_handle); things_free(json_str); download_state = FOTA_DOWNLOAD_STATE_NONE; return -1; } download_state = FOTA_DOWNLOAD_STATE_BINARY; cJSON *root = cJSON_Parse((const char *)json_str); cJSON *url = cJSON_GetObjectItem(root, KEY_URL); recv_size = 0; is_link_fail = false; if (wget_from_url(url->valuestring) < 0) { THINGS_LOG_E(TAG, "wget_from_url error"); if (root != NULL) { cJSON_Delete(root); } fotahal_erase(fotahal_handle); fotahal_close(fotahal_handle); things_free(json_str); download_state = FOTA_DOWNLOAD_STATE_NONE; return -1; } if (is_link_fail) { things_free(json_str); fotahal_erase(fotahal_handle); fotahal_close(fotahal_handle); download_state = FOTA_DOWNLOAD_STATE_NONE; return -1; } THINGS_LOG_D(TAG, "[recv:BINARY] state : %d / recv_size : %u / total size : %u", download_state, recv_size, total_size, total_size); if (recv_size != total_size) { THINGS_LOG_E(TAG, "[recv:BINARY] file size error"); fotahal_erase(fotahal_handle); fotahal_close(fotahal_handle); things_free(json_str); download_state = FOTA_DOWNLOAD_STATE_NONE; return -1; } if (root != NULL) { cJSON_Delete(root); } things_free(json_str); download_state = FOTA_DOWNLOAD_STATE_DONE; fotahal_close(fotahal_handle); return 0; }
bool get_query_value_internal(const char *query, const char *key, char **value, bool *found) { RET_FALSE_IF_EXPR_IS_TRUE(TAG, (NULL == query || strlen(query) < 1), "Invalid query."); RET_FALSE_IF_EXPR_IS_TRUE(TAG, (NULL == key || strlen(key) < 1), "Invalid key."); RET_FALSE_IF_PARAM_IS_NULL(TAG, value); *value = NULL; if (NULL != found) { *found = false; } int query_len = strlen(query); int key_len = strlen(key); char *p_buff = NULL; char *p_origin = NULL; char *p_ptr = NULL; p_origin = p_buff = (char *)things_malloc(query_len + 1); if (NULL == p_origin) { THINGS_LOG_E(TAG, "Failed to allocate memory to get a specific value from query."); return false; } memset(p_buff, 0, query_len + 1); memcpy(p_buff, query, query_len); p_ptr = strtok(p_buff, QUERY_DELIMITER); if (NULL == p_ptr) { THINGS_LOG_E(TAG, "Failed to tokenize the query."); things_free(p_origin); return false; } bool res = false; while (p_ptr != NULL) { if (strncmp(p_ptr, key, key_len) == 0) { THINGS_LOG_D(TAG, "Key(%s) exists in query parameter(%s).", key, query); if (NULL != found) { *found = true; } *value = things_clone_string(p_ptr + key_len + 1); if (NULL == *value) { THINGS_LOG_E(TAG, "Failed to clone the query value."); things_free(p_origin); return false; } else { res = true; } break; } p_ptr = strtok(NULL, QUERY_DELIMITER); } if (NULL == p_ptr) { THINGS_LOG_D(TAG, "Key(%s) doesn't exist in query(%s).", key, query); } things_free(p_origin); return res; }
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; }
bool get_query(struct things_resource_s *res, char *key, char **value) { THINGS_LOG_D(TAG, THINGS_FUNC_ENTRY); if (NULL == key) { return 0; } else if (NULL == res) { return 0; } else if (NULL == res->query) { return 0; } else if (strlen(res->query) < 1) { return 0; } THINGS_LOG_D(TAG, "Query is %s.", res->query); *value = NULL; char *p_buff = NULL, *p_origin = NULL; char *p_ptr = NULL; char *p_ptr2 = NULL; p_origin = p_buff = (char *)things_malloc(strlen(res->query) + 1); if (NULL == p_buff || NULL == p_origin) { return 0; } memset(p_buff, 0, strlen(res->query) + 1); THINGS_LOG_D(TAG, "p_buff is initialized by 0."); memcpy(p_buff, res->query, strlen(res->query) + 1); THINGS_LOG_D(TAG, "p_buff is Initialized as", res->query); p_ptr = strtok(p_buff, QUERY_DELIMITER); if (p_ptr != NULL) { p_ptr2 = p_ptr; } else { things_free(p_origin); return 0; } //while (p_ptr != NULL) while (p_ptr2 != NULL) { if (strncmp(p_ptr2, key, strlen(key)) == 0) { THINGS_LOG_D(TAG, "\tFind Query : %s", p_ptr2 + strlen(key) + 1); things_string_duplicate(p_ptr2 + strlen(key) + 1, value); if (NULL == *value) { things_free(p_origin); return 1; } THINGS_LOG_D(TAG, "\tRESULT : %s", *value); break; } p_ptr2 = strtok(NULL, QUERY_DELIMITER); } things_free(p_origin); THINGS_LOG_D(TAG, THINGS_FUNC_EXIT); return 1; }