bool thethingsio_example_read_and_write(char * write_data) { bool ret = false; int err_code = 0; char ext_header[100] = {0,}; struct http_entity * entity = _thethingsio_example_http_set_default_entity(); DEBUG(DEBUG_CONF_THETHINGSIO"thethingsio_example_read_and_write"DEBUG_EOL); entity->priv_data = (void*)write_data; //sprintf(ext_header,THETHINGSIO_EXAMPLE_HTTP_CIK_EXT_HEADER, cik); err_code = http_client_send_request(&http_client_module_inst, gau8TheThingsiOHttpRWUrl,HTTP_METHOD_POST,entity, NULL); if( err_code == 0) { ret = true; tick_counter_pending_timer(); } else { DEBUG(DEBUG_CONF_THETHINGSIO"Error : thethingsio_example_read_and_write code %d"DEBUG_EOL, err_code); } return ret; }
/** * \brief Start file downloading via http connection. */ static void start_download(void) { if (!is_state_set(STORAGE_READY)) { printf("start_download: MMC storage not ready.\r\n"); return; } if (!is_state_set(WIFI_CONNECTED)) { printf("start_download: Wi-Fi is not connected.\r\n"); return; } if (is_state_set(GET_REQUESTED)) { printf("start_download: request is sent already.\r\n"); return; } if (is_state_set(DOWNLOADING)) { printf("start_download: running download already.\r\n"); return; } /* Send the HTTP request. */ printf("start_download: send http request.\r\n"); http_client_send_request(&http_client_module_inst, MAIN_HTTP_FILE_URL, HTTP_METHOD_GET, NULL, NULL); }
bool thethingsio_activate_thing() { char * activation_code = MAIN_THETHINGSIO_ACTIVATION_CODE; bool ret = false; int err_code = 0; DEBUG(DEBUG_CONF_THETHINGSIO"thethingsio_example_activiate"DEBUG_EOL); sprintf(gau8TheThingsiOActivationData,THETHINGSIO_EXAMPLE_JSON_ACTIVATE,activation_code); struct http_entity * entity = _thethingsio_example_http_set_default_entity(); entity->priv_data = (void*)gau8TheThingsiOActivationData; err_code = http_client_send_request(&http_client_module_inst, THETHINGSIO_EXAMPLE_HTTP_ACTIVATE_URL,HTTP_METHOD_POST,entity, NULL); if( err_code == 0) { ret = true; tick_counter_pending_timer(); } else { DEBUG(DEBUG_CONF_THETHINGSIO"Error : thethingsio_example_activiate code %d"DEBUG_EOL, err_code); } return ret; }
/** * \brief Callback to get the Wi-Fi status update. * * \param[in] msg_type type of Wi-Fi notification. Possible types are: * - [M2M_WIFI_RESP_CURRENT_RSSI](@ref M2M_WIFI_RESP_CURRENT_RSSI) * - [M2M_WIFI_RESP_CON_STATE_CHANGED](@ref M2M_WIFI_RESP_CON_STATE_CHANGED) * - [M2M_WIFI_RESP_CONNTION_STATE](@ref M2M_WIFI_RESP_CONNTION_STATE) * - [M2M_WIFI_RESP_SCAN_DONE](@ref M2M_WIFI_RESP_SCAN_DONE) * - [M2M_WIFI_RESP_SCAN_RESULT](@ref M2M_WIFI_RESP_SCAN_RESULT) * - [M2M_WIFI_REQ_WPS](@ref M2M_WIFI_REQ_WPS) * - [M2M_WIFI_RESP_IP_CONFIGURED](@ref M2M_WIFI_RESP_IP_CONFIGURED) * - [M2M_WIFI_RESP_IP_CONFLICT](@ref M2M_WIFI_RESP_IP_CONFLICT) * - [M2M_WIFI_RESP_P2P](@ref M2M_WIFI_RESP_P2P) * - [M2M_WIFI_RESP_AP](@ref M2M_WIFI_RESP_AP) * - [M2M_WIFI_RESP_CLIENT_INFO](@ref M2M_WIFI_RESP_CLIENT_INFO) * \param[in] pvMsg A pointer to a buffer containing the notification parameters * (if any). It should be casted to the correct data type corresponding to the * notification type. Existing types are: * - tstrM2mWifiStateChanged * - tstrM2MWPSInfo * - tstrM2MP2pResp * - tstrM2MAPResp * - tstrM2mScanDone * - tstrM2mWifiscanResult */ static void wifi_callback(uint8_t msg_type, void *msg_data) { switch (msg_type) { case M2M_WIFI_RESP_CON_STATE_CHANGED: { tstrM2mWifiStateChanged *msg_wifi_state = (tstrM2mWifiStateChanged *)msg_data; if (msg_wifi_state->u8CurrState == M2M_WIFI_CONNECTED) { /* If Wi-Fi is connected. */ printf("Wi-Fi connected\r\n"); m2m_wifi_request_dhcp_client(); } else if (msg_wifi_state->u8CurrState == M2M_WIFI_DISCONNECTED) { /* If Wi-Fi is disconnected. */ printf("Wi-Fi disconnected\r\n"); m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL); } } break; case M2M_WIFI_REQ_DHCP_CONF: { uint8 *msg_ip_addr = (uint8 *)msg_data; printf("Wi-Fi IP is %u.%u.%u.%u\r\n", msg_ip_addr[0], msg_ip_addr[1], msg_ip_addr[2], msg_ip_addr[3]); /* Send the HTTP request. */ http_client_send_request(&http_client_module_inst, MAIN_HTTP_CLIENT_TEST_URL, MAIN_HTTP_CLIENT_TEST_METHOD, NULL); } break; default: break; } }
int http_client_pool_post_request_send(struct http_client_context *context, struct vmbuf *post_data) { size_t size = vmbuf_wlocpos(post_data); vmbuf_sprintf(&context->request, "\r\nContent-Length: %zu\r\n\r\n", size); // TODO: use writev instead of copying vmbuf_memcpy(&context->request, vmbuf_data(post_data), size); if (0 > http_client_send_request(context)) { http_client_free(context); return -1; } return 0; }
int http_client_pool_get_request(struct http_client_pool *http_client_pool, struct in_addr addr, uint16_t port, const char *hostname, const char *format, ...) { struct http_client_context *cctx = http_client_pool_create_client(http_client_pool, addr, port, NULL); if (NULL == cctx) return -1; vmbuf_strcpy(&cctx->request, "GET "); va_list ap; va_start(ap, format); vmbuf_vsprintf(&cctx->request, format, ap); va_end(ap); vmbuf_sprintf(&cctx->request, " HTTP/1.1\r\nHost: %s\r\n\r\n", hostname); if (0 > http_client_send_request(cctx)) { http_client_free(http_client_pool, cctx); return -1; } return 0; }
/** * \brief Callback of the HTTP client. * * \param[in] module_inst Module instance of HTTP client module. * \param[in] type Type of event. * \param[in] data Data structure of the event. \refer http_client_data */ static void http_client_callback(struct http_client_module *module_inst, int type, union http_client_data *data) { struct json_obj json, loc; switch (type) { case HTTP_CLIENT_CALLBACK_SOCK_CONNECTED: printf("Connected\r\n"); break; case HTTP_CLIENT_CALLBACK_REQUESTED: printf("Request complete\r\n"); break; case HTTP_CLIENT_CALLBACK_RECV_RESPONSE: printf("Received response %u data size %u\r\n", (unsigned int)data->recv_response.response_code, (unsigned int)data->recv_response.content_length); if (data->recv_response.content != NULL) { if (json_create(&json, data->recv_response.content, data->recv_response.content_length) == 0 && json_find(&json, "loc", &loc) == 0) { printf("Location : %s\r\n", loc.value.s); } } break; case HTTP_CLIENT_CALLBACK_DISCONNECTED: printf("Disconnected reason:%d\r\n", data->disconnected.reason); /* If disconnect reason is equals to -ECONNRESET(-104), * It means Server was disconnected your connection by the keep alive timeout. * This is normal operation. */ if (data->disconnected.reason == -EAGAIN) { /* Server has not responded. retry it immediately. */ http_client_send_request(&http_client_module_inst, MAIN_HTTP_CLIENT_TEST_URL, MAIN_HTTP_CLIENT_TEST_METHOD, NULL); } break; } }
static int http_client_pool_post_request2( struct http_client_pool *http_client_pool, struct in_addr addr, uint16_t port, const char *hostname, const char *data, size_t size_of_data, const char *format, ...) { struct http_client_context *cctx = http_client_pool_create_client2(http_client_pool, addr, port, hostname, NULL); if (NULL == cctx) return -1; vmbuf_reset(&cctx->request); vmbuf_strcpy(&cctx->request, "POST "); va_list ap; va_start(ap, format); vmbuf_vsprintf(&cctx->request, format, ap); va_end(ap); vmbuf_sprintf(&cctx->request, " HTTP/1.1\r\nHost: %s\r\nContent-Type: application/json\r\nContent-Length: %zu\r\n\r\n", hostname, size_of_data); vmbuf_memcpy(&cctx->request, data, size_of_data); vmbuf_chrcpy(&cctx->request, '\0'); if (0 > http_client_send_request(cctx)) return http_client_free(cctx), -1; return 0; }