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