smartconfig_app.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * 2018-09-13 Bernard the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #ifdef RT_USING_SMARTCONFIG_LIB
  13. #include <sys/socket.h>
  14. #include "smartconfig.h"
  15. #define NET_READY_TIME_OUT (rt_tick_from_millisecond(30 * 1000))
  16. static int rt_wlan_device_connetct(char *ssid, char *passwd)
  17. {
  18. int result = RT_EOK;
  19. rt_uint8_t time_cnt = 0;
  20. result = rt_wlan_connect(ssid, passwd);
  21. if (result != RT_EOK)
  22. {
  23. rt_kprintf("\nconnect ssid %s error:%d!\n", ssid, result);
  24. return result;
  25. };
  26. do
  27. {
  28. rt_thread_mdelay(1000);
  29. time_cnt ++;
  30. if (rt_wlan_is_ready())
  31. {
  32. break;
  33. }
  34. }
  35. while (time_cnt <= (NET_READY_TIME_OUT / 1000));
  36. if (time_cnt <= (NET_READY_TIME_OUT / 1000))
  37. {
  38. rt_kprintf("networking ready!\n");
  39. }
  40. else
  41. {
  42. rt_kprintf("wait ip got timeout!\n");
  43. result = -RT_ETIMEOUT;
  44. }
  45. return result;
  46. }
  47. static void airkiss_send_notification(uint8_t random)
  48. {
  49. int i;
  50. int sock = -1;
  51. int udpbufsize = 2;
  52. struct sockaddr_in UDPBCAddr, UDPBCServerAddr;
  53. sock = socket(AF_INET, SOCK_DGRAM, 0);
  54. if (sock < 0)
  55. {
  56. rt_kprintf("notify create socket error!\n");
  57. goto _exit;
  58. }
  59. UDPBCAddr.sin_family = AF_INET;
  60. UDPBCAddr.sin_port = htons(10000);
  61. UDPBCAddr.sin_addr.s_addr = htonl(0xffffffff);
  62. rt_memset(&(UDPBCAddr.sin_zero), 0, sizeof(UDPBCAddr.sin_zero));
  63. UDPBCServerAddr.sin_family = AF_INET;
  64. UDPBCServerAddr.sin_port = htons(10000);
  65. UDPBCServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  66. rt_memset(&(UDPBCServerAddr.sin_zero), 0, sizeof(UDPBCServerAddr.sin_zero));
  67. if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &udpbufsize, sizeof(int)) != 0)
  68. {
  69. rt_kprintf("notify socket setsockopt error\n");
  70. goto _exit;
  71. }
  72. if (bind(sock, (struct sockaddr *)&UDPBCServerAddr, sizeof(UDPBCServerAddr)) != 0)
  73. {
  74. rt_kprintf("notify socket bind error\n");
  75. goto _exit;
  76. }
  77. for (i = 0; i <= 20; i++)
  78. {
  79. int ret = sendto(sock, (char *)&random, 1, 0, (struct sockaddr *)&UDPBCAddr, sizeof(UDPBCAddr));
  80. rt_thread_delay(10);
  81. }
  82. rt_kprintf("airkiss notification thread exit!\n");
  83. _exit:
  84. if (sock >= 0)
  85. {
  86. closesocket(sock);
  87. }
  88. }
  89. static int smartconfig_result(rt_smartconfig_type result_type, char *ssid, char *passwd, void *userdata, rt_uint8_t userdata_len)
  90. {
  91. rt_uint8_t random = *(rt_uint8_t *)userdata;
  92. rt_kprintf("type:%d\n", result_type);
  93. rt_kprintf("ssid:%s\n", ssid);
  94. rt_kprintf("passwd:%s\n", passwd);
  95. rt_kprintf("user_data:0x%02x\n", random);
  96. if (rt_wlan_device_connetct(ssid, passwd) == RT_EOK)
  97. {
  98. airkiss_send_notification(random);
  99. }
  100. return 0;
  101. }
  102. void smartconfig_demo(void)
  103. {
  104. rt_smartconfig_start(SMARTCONFIG_TYPE_AIRKISS, SMARTCONFIG_ENCRYPT_NONE, RT_NULL, smartconfig_result);
  105. }
  106. #ifdef RT_USING_FINSH
  107. #include "finsh.h"
  108. MSH_CMD_EXPORT(smartconfig_demo, smartconfig demo);
  109. #endif
  110. #endif