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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "eos.h"
#include "event.h"
#include "dev/net.h"
#include "wifi.h"
static eos_evt_handler_t evt_handler[EOS_WIFI_MAX_MTYPE];
static void wifi_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) {
unsigned char mtype;
if ((buffer == NULL) || (len < 1)) {
eos_net_bad_handler(type, buffer, len);
return;
}
mtype = buffer[0];
if ((mtype < EOS_WIFI_MAX_MTYPE) && evt_handler[mtype]) {
evt_handler[mtype](mtype, buffer, len);
} else {
eos_net_bad_handler(type, buffer, len);
}
}
void eos_wifi_init(void) {
int i;
for (i=0; i<EOS_WIFI_MAX_MTYPE; i++) {
evt_handler[i] = NULL;
}
eos_net_set_handler(EOS_NET_MTYPE_WIFI, wifi_handle_msg);
}
void eos_wifi_set_handler(unsigned char mtype, eos_evt_handler_t handler) {
if (mtype < EOS_WIFI_MAX_MTYPE) evt_handler[mtype] = handler;
}
eos_evt_handler_t eos_wifi_get_handler(unsigned char mtype) {
if (mtype < EOS_WIFI_MAX_MTYPE) return evt_handler[mtype];
return NULL;
}
int eos_wifi_status_parse(unsigned char *buffer, uint16_t len, uint8_t *status, uint8_t ip_addr[], char *ssid) {
if (len < 2) return EOS_ERR_SIZE;
if (buffer[0] != EOS_WIFI_MTYPE_STATUS) return EOS_ERR_NET;
buffer++;
len -= 1;
*status = buffer[0];
buffer++;
len -= 1;
switch (*status) {
case EOS_WIFI_STATUS_GOT_IP:
if (len < sizeof(uint32_t)) return EOS_ERR_SIZE;
if (ip_addr) memcpy(ip_addr, buffer, sizeof(uint32_t));
buffer += sizeof(uint32_t);
len -= sizeof(uint32_t);
case EOS_WIFI_STATUS_CONNECTED:
if ((len == 0) || (len > EOS_WIFI_SIZE_SSID)) return EOS_ERR_SIZE;
buffer[len - 1] = '\0';
if (ssid) strcpy(ssid, buffer);
break;
}
return EOS_OK;
}
int eos_wifi_status(uint8_t *status, uint8_t ip_addr[], char *ssid, unsigned char *buffer) {
unsigned char type;
uint16_t len;
int do_release;
int rv;
do_release = 0;
if (buffer == NULL) {
buffer = eos_net_alloc();
do_release = 1;
}
type = EOS_NET_MTYPE_WIFI;
len = 1;
buffer[0] = EOS_WIFI_MTYPE_STATUS;
rv = eos_net_xchg(&type, buffer, &len);
if (rv) goto wifi_status_fin;
if (type != EOS_NET_MTYPE_WIFI) {
rv = EOS_ERR_NET;
goto wifi_status_fin;
}
rv = eos_wifi_status_parse(buffer, len, status, ip_addr, ssid);
wifi_status_fin:
if (do_release) eos_net_free(buffer, 1);
return rv;
}
int eos_wifi_start(unsigned char *buffer, int sync) {
int async;
if (buffer == NULL) {
buffer = eos_net_alloc();
async = 1;
} else {
async = !sync;
}
buffer[0] = EOS_WIFI_MTYPE_START;
return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1);
}
int eos_wifi_stop(unsigned char *buffer, int sync) {
int async;
if (buffer == NULL) {
buffer = eos_net_alloc();
async = 1;
} else {
async = !sync;
}
buffer[0] = EOS_WIFI_MTYPE_STOP;
return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1);
}
int eos_wifi_scan(unsigned char *buffer, int sync) {
int async;
if (buffer == NULL) {
buffer = eos_net_alloc();
async = 1;
} else {
async = !sync;
}
buffer[0] = EOS_WIFI_MTYPE_SCAN;
return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1);
}
int eos_wifi_connect(char *ssid, char *pwd, unsigned char *buffer, int sync) {
unsigned char *buf;
size_t ssid_len, pwd_len;
int async;
ssid_len = strlen(ssid);
pwd_len = strlen(pwd);
if (ssid_len > EOS_WIFI_SIZE_SSID - 1) return EOS_ERR_SIZE;
if (pwd_len > EOS_WIFI_SIZE_PWD - 1) return EOS_ERR_SIZE;
if (buffer == NULL) {
buffer = eos_net_alloc();
async = 1;
} else {
async = !sync;
}
buf = buffer;
buf[0] = EOS_WIFI_MTYPE_CONNECT;
buf++;
strcpy(buf, ssid);
buf += ssid_len + 1;
strcpy(buf, pwd);
buf += pwd_len + 1;
return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 3 + ssid_len + pwd_len, async, 1);
}
int eos_wifi_disconnect(unsigned char *buffer, int sync) {
int async;
if (buffer == NULL) {
buffer = eos_net_alloc();
async = 1;
} else {
async = !sync;
}
buffer[0] = EOS_WIFI_MTYPE_DISCONNECT;
return _eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, async, 1);
}
|