http-example.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #if (!WIN32)
  22. #include <unistd.h>
  23. #endif
  24. #include "iot_import.h"
  25. #include "iot_export.h"
  26. #if defined(TEST_HTTP_DAILY)
  27. /* Daily Environment */
  28. #define IOTX_PRODUCT_KEY "zPygj0yP3UF"
  29. #define IOTX_DEVICE_NAME "device_3"
  30. #define IOTX_DEVICE_SECRET "dTWyCJgBYOGVDcr93ukArCa5cndX3s4D"
  31. #define IOTX_DEVICE_ID "device_3"
  32. #else
  33. /* Pre-Online or Online Environment */
  34. #define IOTX_PRODUCT_KEY "yfTuLfBJTiL"
  35. #define IOTX_DEVICE_NAME "TestDeviceForDemo"
  36. #define IOTX_DEVICE_SECRET "fSCl9Ns5YPnYN8Ocg0VEel1kXFnRlV6c"
  37. #define IOTX_DEVICE_ID "IoTxHttpTestDev_001"
  38. #endif
  39. #define DEFAULT_TIMEOUT_MS 5000
  40. static int iotx_post_data_to_server(void *handle)
  41. {
  42. int ret = -1;
  43. char path[IOTX_URI_MAX_LEN + 1] = {0};
  44. char rsp_buf[1024];
  45. iotx_http_t *iotx_http_context = (iotx_http_t *)handle;
  46. iotx_device_info_t *p_devinfo = iotx_http_context->p_devinfo;
  47. iotx_http_message_param_t msg_param;
  48. msg_param.request_payload = (char *)"{\"name\":\"hello world\"}";
  49. msg_param.response_payload = rsp_buf;
  50. msg_param.timeout_ms = iotx_http_context->timeout_ms;
  51. msg_param.request_payload_len = strlen(msg_param.request_payload) + 1;
  52. msg_param.response_payload_len = 1024;
  53. msg_param.topic_path = path;
  54. HAL_Snprintf(msg_param.topic_path, IOTX_URI_MAX_LEN, "/topic/%s/%s/update",
  55. p_devinfo->product_key, p_devinfo->device_name);
  56. if (0 == (ret = IOT_HTTP_SendMessage(iotx_http_context, &msg_param))) {
  57. HAL_Printf("message response is %s\r\n", msg_param.response_payload);
  58. } else {
  59. HAL_Printf("error\r\n");
  60. }
  61. return ret;
  62. }
  63. int main(int argc, char **argv)
  64. {
  65. iotx_device_info_t device_info;
  66. iotx_http_param_t http_param;
  67. #if (!WIN32)
  68. int opt;
  69. #endif
  70. void *handle = NULL;
  71. IOT_OpenLog("http");
  72. IOT_SetLogLevel(IOT_LOG_DEBUG);
  73. /**< set device info*/
  74. HAL_SetProductKey(IOTX_PRODUCT_KEY);
  75. HAL_SetDeviceName(IOTX_DEVICE_NAME);
  76. HAL_SetDeviceSecret(IOTX_DEVICE_SECRET);
  77. /**< end*/
  78. memset(&device_info, 0, sizeof(device_info));
  79. memset(&http_param, 0, sizeof(http_param));
  80. /**< get device info*/
  81. HAL_GetProductKey(device_info.product_key);
  82. HAL_GetDeviceName(device_info.device_name);
  83. HAL_GetDeviceSecret(device_info.device_secret);
  84. HAL_GetDeviceID(device_info.device_id);
  85. /**< end*/
  86. #if (!WIN32)
  87. HAL_Printf("[HTTP-Client]: Enter HTTP Client\r\n");
  88. while ((opt = getopt(argc, argv, "lh")) != -1) {
  89. switch (opt) {
  90. case 'l':
  91. http_param.keep_alive = 1;
  92. break;
  93. case 'h':
  94. /* TODO: */
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. #endif
  101. HAL_Printf("[HTTP-Client]: keep_alive=%d\r\n", http_param.keep_alive);
  102. http_param.device_info = &device_info;
  103. http_param.timeout_ms = DEFAULT_TIMEOUT_MS;
  104. handle = IOT_HTTP_Init(&http_param);
  105. if (NULL != handle) {
  106. IOT_HTTP_DeviceNameAuth(handle);
  107. iotx_post_data_to_server(handle);
  108. HAL_Printf("IoTx HTTP Message Sent\r\n");
  109. } else {
  110. HAL_Printf("IoTx HTTP init failed\r\n");
  111. return 0;
  112. }
  113. IOT_HTTP_Disconnect(handle);
  114. IOT_HTTP_DeInit(&handle);
  115. IOT_DumpMemoryStats(IOT_LOG_DEBUG);
  116. IOT_CloseLog();
  117. return 0;
  118. }