http_client_sample.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <limits.h>
  19. #include <stdbool.h>
  20. #include <string.h>
  21. #include <signal.h>
  22. #include "uiot_import.h"
  23. #include "ca.h"
  24. #include "utils_httpc.h"
  25. #include "uiot_export_http.h"
  26. #define UIOT_PUBLISH_TOPIC "%s/%s/upload/event"
  27. static int running_state = 0;
  28. static void http_publish_test_thread(void)
  29. {
  30. char *token = (char *)HAL_Malloc(1024);
  31. memset(token, 0, 1024);
  32. int ret = SUCCESS_RET;
  33. char *topic = (char *)HAL_Malloc(256);
  34. memset(topic, 0, 256);
  35. HAL_Snprintf((char *)topic, 256, UIOT_PUBLISH_TOPIC,PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN);
  36. char *data = "{\"test\": \"18\"}";
  37. ret = IOT_HTTP_Get_Token(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SECRET, token);
  38. if(SUCCESS_RET != ret)
  39. {
  40. HAL_Printf("get Token fail,ret:%d\r\n", ret);
  41. return;
  42. }
  43. HAL_Printf("get token:%s\n", token);
  44. HAL_Printf("topic:%s\n", topic);
  45. ret = IOT_HTTP_Publish(token, topic, data, 5000);
  46. if(SUCCESS_RET != ret)
  47. {
  48. HAL_Printf("Publish fail,ret:%d\r\n", ret);
  49. return;
  50. }
  51. HAL_Printf("Publish success\n");
  52. HAL_Free(token);
  53. HAL_Free(topic);
  54. return;
  55. }
  56. static int http_publish_test_example(int argc, char **argv)
  57. {
  58. rt_thread_t tid;
  59. int stack_size = 8192;
  60. if (2 == argc)
  61. {
  62. if (!strcmp("start", argv[1]))
  63. {
  64. if (1 == running_state)
  65. {
  66. HAL_Printf("http_publish_test_example is already running\n");
  67. return 0;
  68. }
  69. }
  70. else if (!strcmp("stop", argv[1]))
  71. {
  72. if (0 == running_state)
  73. {
  74. HAL_Printf("http_publish_test_example is already stopped\n");
  75. return 0;
  76. }
  77. running_state = 0;
  78. return 0;
  79. }
  80. else
  81. {
  82. HAL_Printf("Usage: http_publish_test_example start/stop");
  83. return 0;
  84. }
  85. }
  86. else
  87. {
  88. HAL_Printf("Para err, usage: http_publish_test_example start/stop");
  89. return 0;
  90. }
  91. tid = rt_thread_create("http_publish_test", (void (*)(void *))http_publish_test_thread,
  92. NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100);
  93. if (tid != RT_NULL)
  94. {
  95. rt_thread_startup(tid);
  96. }
  97. return 0;
  98. }
  99. #ifdef RT_USING_FINSH
  100. #include <finsh.h>
  101. FINSH_FUNCTION_EXPORT(http_publish_test_example, startup http publish example);
  102. #endif
  103. #ifdef FINSH_USING_MSH
  104. MSH_CMD_EXPORT(http_publish_test_example, startup http publish example);
  105. #endif