smartconfig_app.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 i;
  64. int sock = -1;
  65. int udpbufsize = 2;
  66. struct sockaddr_in UDPBCAddr, UDPBCServerAddr;
  67. sock = socket(AF_INET, SOCK_DGRAM, 0);
  68. if (sock < 0)
  69. {
  70. rt_kprintf("notify create socket error!\n");
  71. goto _exit;
  72. }
  73. UDPBCAddr.sin_family = AF_INET;
  74. UDPBCAddr.sin_port = htons(10000);
  75. UDPBCAddr.sin_addr.s_addr = htonl(0xffffffff);
  76. rt_memset(&(UDPBCAddr.sin_zero), 0, sizeof(UDPBCAddr.sin_zero));
  77. UDPBCServerAddr.sin_family = AF_INET;
  78. UDPBCServerAddr.sin_port = htons(10000);
  79. UDPBCServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  80. rt_memset(&(UDPBCServerAddr.sin_zero), 0, sizeof(UDPBCServerAddr.sin_zero));
  81. if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &udpbufsize, sizeof(int)) != 0)
  82. {
  83. rt_kprintf("notify socket setsockopt error\n");
  84. goto _exit;
  85. }
  86. if (bind(sock, (struct sockaddr *)&UDPBCServerAddr, sizeof(UDPBCServerAddr)) != 0)
  87. {
  88. rt_kprintf("notify socket bind error\n");
  89. goto _exit;
  90. }
  91. for (i = 0; i <= 20; i++)
  92. {
  93. int ret = sendto(sock, (char *)&random, 1, 0, (struct sockaddr *)&UDPBCAddr, sizeof(UDPBCAddr));
  94. rt_thread_delay(10);
  95. }
  96. rt_kprintf("airkiss notification thread exit!\n");
  97. _exit:
  98. if (sock >= 0)
  99. {
  100. closesocket(sock);
  101. }
  102. }
  103. static int smartconfig_result(rt_smartconfig_type result_type, char *ssid, char *passwd, void *userdata, rt_uint8_t userdata_len)
  104. {
  105. rt_uint8_t random = *(rt_uint8_t *)userdata;
  106. rt_kprintf("type:%d\n", result_type);
  107. rt_kprintf("ssid:%s\n", ssid);
  108. rt_kprintf("passwd:%s\n", passwd);
  109. rt_kprintf("user_data:0x%02x\n", random);
  110. if (rt_wlan_device_connetct(ssid, passwd) == RT_EOK)
  111. {
  112. airkiss_send_notification(random);
  113. }
  114. return 0;
  115. }
  116. void smartconfig_demo(void)
  117. {
  118. rt_smartconfig_start(SMARTCONFIG_TYPE_AIRKISS, SMARTCONFIG_ENCRYPT_NONE, RT_NULL, smartconfig_result);
  119. }
  120. #ifdef RT_USING_FINSH
  121. #include "finsh.h"
  122. MSH_CMD_EXPORT(smartconfig_demo, smartconfig demo);
  123. #endif
  124. #endif