-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMXCHIPInterface.cpp
More file actions
257 lines (215 loc) · 6.35 KB
/
MXCHIPInterface.cpp
File metadata and controls
257 lines (215 loc) · 6.35 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* MXCHIP implementation of NetworkInterfaceAPI
* Copyright (c) 2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MXCHIPInterface.h"
// Various timeouts for different MXCHIP operations
#define MXCHIP_CONNECT_TIMEOUT 15000
#define MXCHIP_SEND_TIMEOUT 500
#define MXCHIP_RECV_TIMEOUT 0
#define MXCHIP_MISC_TIMEOUT 500
// MXCHIPInterface implementation
MXCHIPInterface::MXCHIPInterface(PinName tx, PinName rx, bool debug)
: _mxchip(tx, rx, debug)
{
_channel=0;
memset(_ids, 0, sizeof(_ids));
memset(_cbs, 0, sizeof(_cbs));
_mxchip.attach(this, &MXCHIPInterface::event);
}
int MXCHIPInterface::connect(const char *ssid, const char *pass, nsapi_security_t security,uint8_t channel)
{
set_credentials(ssid, pass, security);
return connect();
}
int MXCHIPInterface::connect()
{
_mxchip.setTimeout(MXCHIP_CONNECT_TIMEOUT);
if (!_mxchip.startup()) {
return NSAPI_ERROR_DEVICE_ERROR;
}
if (!_mxchip.connect(ap_ssid, ap_pass)) {
return NSAPI_ERROR_NO_CONNECTION;
}
for(int id=0; id<MXCHIP_SOCKET_COUNT; id++){
if(!network_reconnect(false, id))
return false;
}
if (!_mxchip.getIPAddress()) {
return NSAPI_ERROR_DHCP_FAILURE;
}
return NSAPI_ERROR_OK;
}
int MXCHIPInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
{
memset(ap_ssid, 0, sizeof(ap_ssid));
strncpy(ap_ssid, ssid, sizeof(ap_ssid));
memset(ap_pass, 0, sizeof(ap_pass));
strncpy(ap_pass, pass, sizeof(ap_pass));
ap_sec = security;
return 0;
}
int MXCHIPInterface::set_channel(uint8_t channel)
{
return NSAPI_ERROR_UNSUPPORTED;
}
int MXCHIPInterface::disconnect()
{
_mxchip.setTimeout(MXCHIP_MISC_TIMEOUT);
if (!_mxchip.disconnect()) {
return NSAPI_ERROR_DEVICE_ERROR;
}
return 0;
}
const char* MXCHIPInterface::get_ip_address()
{
return _mxchip.getIPAddress();
}
const char* MXCHIPInterface::get_mac_address()
{
return _mxchip.getMACAddress();
}
const char* MXCHIPInterface::get_gateway()
{
return NULL;
}
const char* MXCHIPInterface::get_netmask()
{
return NULL;
}
int8_t MXCHIPInterface::get_rssi()
{
return _mxchip.getRSSI();
}
int MXCHIPInterface::scan(WiFiAccessPoint *ap, unsigned count)
{
return NSAPI_ERROR_UNSUPPORTED;
}
bool MXCHIPInterface::network_reconnect(bool ENABLE, uint8_t id)
{
return _mxchip.NetworkReconnect(ENABLE, id);
}
struct MXCHIP_socket {
int id;
nsapi_protocol_t proto;
bool connected;
SocketAddress addr;
};
int MXCHIPInterface::socket_open(void **handle, nsapi_protocol_t proto)
{
// Look for an unused socket
int id = -1;
for (int i = 0; i < MXCHIP_SOCKET_COUNT; i++) {
if (!_ids[i]) {
id = i;
_ids[i] = true;
break;
}
}
if (id == -1) {
return NSAPI_ERROR_NO_SOCKET;
}
struct MXCHIP_socket *socket = new struct MXCHIP_socket;
if (!socket) {
return NSAPI_ERROR_NO_SOCKET;
}
socket->id = id;
socket->proto = proto;
socket->connected = false;
*handle = socket;
return 0;
}
int MXCHIPInterface::socket_close(void *handle)
{
struct MXCHIP_socket *socket = (struct MXCHIP_socket *)handle;
int err = 0;
_mxchip.setTimeout(MXCHIP_MISC_TIMEOUT);
if (socket->connected && !_mxchip.close(socket->id)) {
err = NSAPI_ERROR_DEVICE_ERROR;
}
socket->connected = false;
_ids[socket->id] = false;
delete socket;
return err;
}
int MXCHIPInterface::socket_bind(void *handle, const SocketAddress &address)
{
return NSAPI_ERROR_UNSUPPORTED;
}
int MXCHIPInterface::socket_listen(void *handle, int backlog)
{
return NSAPI_ERROR_UNSUPPORTED;
}
int MXCHIPInterface::socket_connect(void *handle, const SocketAddress &addr)
{
struct MXCHIP_socket *socket = (struct MXCHIP_socket *)handle;
_mxchip.setTimeout(MXCHIP_MISC_TIMEOUT);
const char *proto = ((socket->proto == NSAPI_TCP) ? "tcp_client":"udp_unicast");
if(!_mxchip.open(proto, socket->id, addr.get_ip_address(), addr.get_port()))
return NSAPI_ERROR_DEVICE_ERROR;
socket->connected = true;
return 0;
}
int MXCHIPInterface::socket_accept(void *server, void **handle, SocketAddress *addr )
{
return NSAPI_ERROR_UNSUPPORTED;
}
int MXCHIPInterface::socket_send(void *handle, const void *data, unsigned size)
{
struct MXCHIP_socket *socket = (struct MXCHIP_socket *)handle;
_mxchip.setTimeout(MXCHIP_SEND_TIMEOUT);
if (!_mxchip.send(socket->id, data, size)) {
return NSAPI_ERROR_DEVICE_ERROR;
}
return size;
}
int MXCHIPInterface::socket_recv(void *handle, void *data, unsigned size)
{
struct MXCHIP_socket *socket = (struct MXCHIP_socket *)handle;
_mxchip.setTimeout(MXCHIP_RECV_TIMEOUT);
int32_t recv = _mxchip.recv(socket->id, data, size);
if (recv < 0) {
return NSAPI_ERROR_WOULD_BLOCK;
}
return recv;
}
int MXCHIPInterface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
{
struct MXCHIP_socket *socket = (struct MXCHIP_socket *)handle;
if (!socket->connected ) {
int err = socket_connect( socket, addr );
if(err < 0)
return err;
}
return socket_send(socket, data, size);
}
int MXCHIPInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
{
struct MXCHIP_socket *socket = (struct MXCHIP_socket *)handle;
return socket_recv(socket, data, size);
}
void MXCHIPInterface::socket_attach(void *handle, void (*callback)(void *), void *data)
{
struct MXCHIP_socket *socket = (struct MXCHIP_socket *)handle;
_cbs[socket->id].callback = callback;
_cbs[socket->id].data = data;
}
void MXCHIPInterface::event()
{
for (int i = 0; i < MXCHIP_SOCKET_COUNT; i++) {
if (_cbs[i].callback) {
_cbs[i].callback(_cbs[i].data);
}
}
}