| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /*
- * Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
- * License-Identifier: Apache-2.0
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #if (!WIN32)
- #include <unistd.h>
- #endif
- #include "iot_import.h"
- #include "iot_export.h"
- #if defined(TEST_HTTP_DAILY)
- /* Daily Environment */
- #define IOTX_PRODUCT_KEY "zPygj0yP3UF"
- #define IOTX_DEVICE_NAME "device_3"
- #define IOTX_DEVICE_SECRET "dTWyCJgBYOGVDcr93ukArCa5cndX3s4D"
- #define IOTX_DEVICE_ID "device_3"
- #else
- /* Pre-Online or Online Environment */
- #define IOTX_PRODUCT_KEY "yfTuLfBJTiL"
- #define IOTX_DEVICE_NAME "TestDeviceForDemo"
- #define IOTX_DEVICE_SECRET "fSCl9Ns5YPnYN8Ocg0VEel1kXFnRlV6c"
- #define IOTX_DEVICE_ID "IoTxHttpTestDev_001"
- #endif
- #define DEFAULT_TIMEOUT_MS 5000
- static int iotx_post_data_to_server(void *handle)
- {
- int ret = -1;
- char path[IOTX_URI_MAX_LEN + 1] = {0};
- char rsp_buf[1024];
- iotx_http_t *iotx_http_context = (iotx_http_t *)handle;
- iotx_device_info_t *p_devinfo = iotx_http_context->p_devinfo;
- iotx_http_message_param_t msg_param;
- msg_param.request_payload = (char *)"{\"name\":\"hello world\"}";
- msg_param.response_payload = rsp_buf;
- msg_param.timeout_ms = iotx_http_context->timeout_ms;
- msg_param.request_payload_len = strlen(msg_param.request_payload) + 1;
- msg_param.response_payload_len = 1024;
- msg_param.topic_path = path;
- HAL_Snprintf(msg_param.topic_path, IOTX_URI_MAX_LEN, "/topic/%s/%s/update",
- p_devinfo->product_key, p_devinfo->device_name);
- if (0 == (ret = IOT_HTTP_SendMessage(iotx_http_context, &msg_param))) {
- HAL_Printf("message response is %s\r\n", msg_param.response_payload);
- } else {
- HAL_Printf("error\r\n");
- }
- return ret;
- }
- int main(int argc, char **argv)
- {
- iotx_device_info_t device_info;
- iotx_http_param_t http_param;
- #if (!WIN32)
- int opt;
- #endif
- void *handle = NULL;
- IOT_OpenLog("http");
- IOT_SetLogLevel(IOT_LOG_DEBUG);
- /**< set device info*/
- HAL_SetProductKey(IOTX_PRODUCT_KEY);
- HAL_SetDeviceName(IOTX_DEVICE_NAME);
- HAL_SetDeviceSecret(IOTX_DEVICE_SECRET);
- /**< end*/
- memset(&device_info, 0, sizeof(device_info));
- memset(&http_param, 0, sizeof(http_param));
- /**< get device info*/
- HAL_GetProductKey(device_info.product_key);
- HAL_GetDeviceName(device_info.device_name);
- HAL_GetDeviceSecret(device_info.device_secret);
- HAL_GetDeviceID(device_info.device_id);
- /**< end*/
- #if (!WIN32)
- HAL_Printf("[HTTP-Client]: Enter HTTP Client\r\n");
- while ((opt = getopt(argc, argv, "lh")) != -1) {
- switch (opt) {
- case 'l':
- http_param.keep_alive = 1;
- break;
- case 'h':
- /* TODO: */
- break;
- default:
- break;
- }
- }
- #endif
- HAL_Printf("[HTTP-Client]: keep_alive=%d\r\n", http_param.keep_alive);
- http_param.device_info = &device_info;
- http_param.timeout_ms = DEFAULT_TIMEOUT_MS;
- handle = IOT_HTTP_Init(&http_param);
- if (NULL != handle) {
- IOT_HTTP_DeviceNameAuth(handle);
- iotx_post_data_to_server(handle);
- HAL_Printf("IoTx HTTP Message Sent\r\n");
- } else {
- HAL_Printf("IoTx HTTP init failed\r\n");
- return 0;
- }
- IOT_HTTP_Disconnect(handle);
- IOT_HTTP_DeInit(&handle);
- IOT_DumpMemoryStats(IOT_LOG_DEBUG);
- IOT_CloseLog();
- return 0;
- }
|