dynamic_auth_sample.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <string.h>
  17. #include <limits.h>
  18. #include "uiot_export.h"
  19. #include "uiot_import.h"
  20. #include "lite-utils.h"
  21. #define MAX_SIZE_OF_TOPIC_CONTENT 100
  22. MQTTInitParams init_params = DEFAULT_MQTT_INIT_PARAMS;
  23. static int running_state = 0;
  24. void dynamic_event_handler(void *pclient, void *handle_context, MQTTEventMsg *msg) {
  25. LOG_DEBUG("Enter event_handler!type:%d\n",msg->event_type);
  26. }
  27. static void on_message_callback(void *pClient, MQTTMessage *message, void *userData) {
  28. if (message == NULL) {
  29. return;
  30. }
  31. LOG_DEBUG("Receive Message With topicName:%.*s, payload:%.*s",
  32. (int) message->topic_len, message->topic, (int) message->payload_len, (char *) message->payload);
  33. }
  34. static int _publish_msg(void *client)
  35. {
  36. char topicName[128] = {0};
  37. int num = 18;
  38. HAL_Snprintf(topicName, 128, "/%s/%s/upload/event", PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN);
  39. PublishParams pub_params = DEFAULT_PUB_PARAMS;
  40. char topic_content[MAX_SIZE_OF_TOPIC_CONTENT + 1] = {0};
  41. HAL_Snprintf(topic_content, MAX_SIZE_OF_TOPIC_CONTENT, "{\"test\": \"%d\"}", num);
  42. pub_params.payload = topic_content;
  43. pub_params.payload_len = strlen(topic_content);
  44. return IOT_MQTT_Publish(client, topicName, &pub_params);
  45. }
  46. static int _register_subscribe_topics(void *client)
  47. {
  48. static char topic_name[128] = {0};
  49. HAL_Snprintf(topic_name, 128, "/%s/%s/upload/event", PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN);
  50. SubscribeParams sub_params = DEFAULT_SUB_PARAMS;
  51. sub_params.on_message_handler = on_message_callback;
  52. return IOT_MQTT_Subscribe(client, topic_name, &sub_params);
  53. }
  54. /**
  55. * 设置MQTT connet初始化参数
  56. *
  57. * @param initParams MQTT connet初始化参数
  58. *
  59. * @return 0: 参数初始化成功 非0: 失败
  60. */
  61. static int _setup_connect_init_params(MQTTInitParams* initParams)
  62. {
  63. initParams->device_sn = (char *)PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN;
  64. initParams->product_sn = (char *)PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN;
  65. initParams->product_secret = (char *)PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SECRET;
  66. initParams->command_timeout = UIOT_MQTT_COMMAND_TIMEOUT;
  67. initParams->keep_alive_interval = UIOT_MQTT_KEEP_ALIVE_INTERNAL;
  68. initParams->auto_connect_enable = 1;
  69. initParams->event_handler.h_fp = dynamic_event_handler;
  70. initParams->event_handler.context = NULL;
  71. return SUCCESS_RET;
  72. }
  73. static void dynamic_test_thread(void)
  74. {
  75. int ret = 0;
  76. char secret[IOT_DEVICE_SECRET_LEN+1];
  77. ret = _setup_connect_init_params(&init_params);
  78. if (ret != SUCCESS_RET) {
  79. return;
  80. }
  81. ret = IOT_MQTT_Dynamic_Register(&init_params);
  82. if (ret != SUCCESS_RET) {
  83. return;
  84. }
  85. ret = HAL_GetDeviceSecret(secret);
  86. if(ret != SUCCESS_RET)
  87. {
  88. LOG_ERROR("get device secret fail\n");
  89. return;
  90. }
  91. init_params.device_secret = secret;
  92. LOG_DEBUG("Password:%s\n",init_params.device_secret);
  93. void *static_client = IOT_MQTT_Construct(&init_params);
  94. if(static_client == NULL)
  95. {
  96. LOG_ERROR("static_client Construct fail\n");
  97. return;
  98. }
  99. _register_subscribe_topics(static_client);
  100. IOT_MQTT_Yield(static_client, 200);
  101. _publish_msg(static_client);
  102. IOT_MQTT_Yield(static_client, 200);
  103. ret = IOT_MQTT_Destroy(&static_client);
  104. return;
  105. }
  106. static int dynamic_test_example(int argc, char **argv)
  107. {
  108. rt_thread_t tid;
  109. int stack_size = 8192;
  110. if (2 == argc)
  111. {
  112. if (!strcmp("start", argv[1]))
  113. {
  114. if (1 == running_state)
  115. {
  116. HAL_Printf("dynamic_test_example is already running\n");
  117. return 0;
  118. }
  119. }
  120. else if (!strcmp("stop", argv[1]))
  121. {
  122. if (0 == running_state)
  123. {
  124. HAL_Printf("dynamic_test_example is already stopped\n");
  125. return 0;
  126. }
  127. running_state = 0;
  128. return 0;
  129. }
  130. else
  131. {
  132. HAL_Printf("Usage: dynamic_test_example start/stop");
  133. return 0;
  134. }
  135. }
  136. else
  137. {
  138. HAL_Printf("Para err, usage: dynamic_test_example start/stop");
  139. return 0;
  140. }
  141. tid = rt_thread_create("dynamic_test", (void (*)(void *))dynamic_test_thread,
  142. NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100);
  143. if (tid != RT_NULL)
  144. {
  145. rt_thread_startup(tid);
  146. }
  147. return 0;
  148. }
  149. #ifdef RT_USING_FINSH
  150. #include <finsh.h>
  151. FINSH_FUNCTION_EXPORT(dynamic_test_example, startup mqtt dynamic example);
  152. #endif
  153. #ifdef FINSH_USING_MSH
  154. MSH_CMD_EXPORT(dynamic_test_example, startup mqtt dynamic example);
  155. #endif