oneshot_demo.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-01-25 tyx add w600
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "oneshot.h"
  13. #define NET_READY_TIME_OUT (rt_tick_from_millisecond(30 * 1000))
  14. struct join_info
  15. {
  16. char ssid[64];
  17. char passwd[64];
  18. };
  19. static int rt_wlan_device_connetct(char *ssid, char *passwd)
  20. {
  21. int result = RT_EOK;
  22. rt_uint8_t time_cnt = 0;
  23. result = rt_wlan_connect(ssid, passwd);
  24. if (result != RT_EOK)
  25. {
  26. rt_kprintf("\nconnect ssid %s error:%d!\n", ssid, result);
  27. return result;
  28. };
  29. do
  30. {
  31. rt_thread_mdelay(1000);
  32. time_cnt ++;
  33. if (rt_wlan_is_ready())
  34. {
  35. break;
  36. }
  37. }
  38. while (time_cnt <= (NET_READY_TIME_OUT / 1000));
  39. if (time_cnt <= (NET_READY_TIME_OUT / 1000))
  40. {
  41. rt_kprintf("networking ready!\n");
  42. }
  43. else
  44. {
  45. rt_kprintf("wait ip got timeout!\n");
  46. result = -RT_ETIMEOUT;
  47. }
  48. return result;
  49. }
  50. static void join_wifi_thread(void *param)
  51. {
  52. struct join_info *info = param;
  53. rt_kprintf("connect wifi:%s\n", info->ssid);
  54. if (rt_strlen(info->passwd) > 0)
  55. {
  56. rt_wlan_device_connetct(info->ssid, info->passwd);
  57. }
  58. else
  59. {
  60. rt_wlan_device_connetct(info->ssid, RT_NULL);
  61. }
  62. rt_free(info);
  63. }
  64. static void wm_oneshot_result_cb(int state, unsigned char *ssid, unsigned char *passwd)
  65. {
  66. rt_thread_t tid;
  67. struct join_info *info;
  68. if (state != 0)
  69. {
  70. rt_kprintf("Receive wifi info timeout(%d). exit!\n", state);
  71. return;
  72. }
  73. if (ssid == RT_NULL)
  74. {
  75. rt_kprintf("SSID is NULL. exit!\n");
  76. return;
  77. }
  78. rt_kprintf("Receive ssid:%s passwd:%s\n", ssid == RT_NULL ? "" : ssid, passwd == RT_NULL ? "" : passwd);
  79. info = rt_malloc(sizeof(struct join_info));
  80. if (info == RT_NULL)
  81. {
  82. return;
  83. }
  84. rt_memset(info, 0, sizeof(struct join_info));
  85. rt_strncpy(info->ssid, ssid, sizeof(info->ssid));
  86. if (passwd)
  87. {
  88. rt_strncpy(info->passwd, passwd, sizeof(info->passwd));
  89. }
  90. tid = rt_thread_create("join", join_wifi_thread, info, 2048, 22, 20);
  91. if (tid == RT_NULL)
  92. {
  93. rt_free(info);
  94. return;
  95. }
  96. rt_thread_startup(tid);
  97. }
  98. void oneshot_demo(int argc, char *argv[])
  99. {
  100. WM_ONESHOT_MODE mode = WM_UDP;
  101. if (argc > 2)
  102. {
  103. rt_kprintf("use: %s [web]", __FUNCTION__);
  104. return;
  105. }
  106. if (argc == 2)
  107. {
  108. if (rt_strcmp("web", argv[1]) == 0)
  109. {
  110. mode = WM_APWEB;
  111. }
  112. else if(rt_strcmp("udp", argv[1]) == 0)
  113. {
  114. mode = WM_UDP;
  115. }
  116. else
  117. {
  118. rt_kprintf("use: %s [web]", __FUNCTION__);
  119. return;
  120. }
  121. }
  122. if (wm_oneshot_start(mode, wm_oneshot_result_cb) == 0)
  123. {
  124. rt_kprintf("oneshot start...\n");
  125. }
  126. else
  127. {
  128. rt_kprintf("oneshot start failed\n");
  129. }
  130. }
  131. #ifdef RT_USING_FINSH
  132. #include "finsh.h"
  133. MSH_CMD_EXPORT(oneshot_demo, oneshot demo);
  134. #endif