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