smartconfig_app.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * File : smartconfig_demo.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-09-13 Bernard the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #ifdef RT_USING_SMARTCONFIG_LIB
  27. #include <sys/socket.h>
  28. #include "smartconfig.h"
  29. #define NET_READY_TIME_OUT (rt_tick_from_millisecond(30 * 1000))
  30. static int rt_wlan_device_connetct(char *ssid, char *passwd)
  31. {
  32. int result = RT_EOK;
  33. rt_uint8_t time_cnt = 0;
  34. result = rt_wlan_connect(ssid, passwd);
  35. if (result != RT_EOK)
  36. {
  37. rt_kprintf("\nconnect ssid %s error:%d!\n", ssid, result);
  38. return result;
  39. };
  40. do
  41. {
  42. rt_thread_mdelay(1000);
  43. time_cnt ++;
  44. if (rt_wlan_is_ready())
  45. {
  46. break;
  47. }
  48. }
  49. while (time_cnt <= (NET_READY_TIME_OUT / 1000));
  50. if (time_cnt <= (NET_READY_TIME_OUT / 1000))
  51. {
  52. rt_kprintf("networking ready!\n");
  53. }
  54. else
  55. {
  56. rt_kprintf("wait ip got timeout!\n");
  57. result = -RT_ETIMEOUT;
  58. }
  59. return result;
  60. }
  61. static void airkiss_send_notification(uint8_t random)
  62. {
  63. int sock = -1;
  64. int udpbufsize = 2;
  65. struct sockaddr_in UDPBCAddr, UDPBCServerAddr;
  66. sock = socket(AF_INET, SOCK_DGRAM, 0);
  67. if (sock < 0)
  68. {
  69. rt_kprintf("notify create socket error!\n");
  70. goto _exit;
  71. }
  72. UDPBCAddr.sin_family = AF_INET;
  73. UDPBCAddr.sin_port = htons(10000);
  74. UDPBCAddr.sin_addr.s_addr = htonl(0xffffffff);
  75. rt_memset(&(UDPBCAddr.sin_zero), 0, sizeof(UDPBCAddr.sin_zero));
  76. UDPBCServerAddr.sin_family = AF_INET;
  77. UDPBCServerAddr.sin_port = htons(10000);
  78. UDPBCServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  79. rt_memset(&(UDPBCServerAddr.sin_zero), 0, sizeof(UDPBCServerAddr.sin_zero));
  80. if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &udpbufsize, sizeof(int)) != 0)
  81. {
  82. rt_kprintf("notify socket setsockopt error\n");
  83. goto _exit;
  84. }
  85. if (bind(sock, (struct sockaddr *)&UDPBCServerAddr, sizeof(UDPBCServerAddr)) != 0)
  86. {
  87. rt_kprintf("notify socket bind error\n");
  88. goto _exit;
  89. }
  90. for (int i = 0; i <= 20; i++)
  91. {
  92. int ret = sendto(sock, (char *)&random, 1, 0, (struct sockaddr *)&UDPBCAddr, sizeof(UDPBCAddr));
  93. rt_thread_delay(10);
  94. }
  95. rt_kprintf("airkiss notification thread exit!\n");
  96. _exit:
  97. if (sock >= 0)
  98. {
  99. closesocket(sock);
  100. }
  101. }
  102. static int smartconfig_result(rt_smartconfig_type result_type, char *ssid, char *passwd, void *userdata, rt_uint8_t userdata_len)
  103. {
  104. rt_uint8_t random = *(rt_uint8_t *)userdata;
  105. rt_kprintf("type:%d\n", result_type);
  106. rt_kprintf("ssid:%s\n", ssid);
  107. rt_kprintf("passwd:%s\n", passwd);
  108. rt_kprintf("user_data:0x%02x\n", random);
  109. if (rt_wlan_device_connetct(ssid, passwd) == RT_EOK)
  110. {
  111. airkiss_send_notification(random);
  112. }
  113. return 0;
  114. }
  115. void smartconfig_demo(void)
  116. {
  117. rt_smartconfig_start(SMARTCONFIG_TYPE_AIRKISS, SMARTCONFIG_ENCRYPT_NONE, RT_NULL, smartconfig_result);
  118. }
  119. #ifdef RT_USING_FINSH
  120. #include "finsh.h"
  121. MSH_CMD_EXPORT(smartconfig_demo, smartconfig demo);
  122. #endif
  123. #endif