ThingPlug Test

Dependents:   WizFi310_ThingPlug_Test WizFi310_ThingPlug_Test_P

Fork of WizFi310Interface by WIZnet

Committer:
jehoon
Date:
Mon Jun 26 00:17:10 2017 +0000
Revision:
5:72212beb817c
Parent:
4:176b6f3addd6
modify message parsing in isr

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jehoon 0:df571f8f8c03 1 /* WizFi310 implementation of NetworkInterfaceAPI
jehoon 0:df571f8f8c03 2 * Copyright (c) 2015 ARM Limited
jehoon 0:df571f8f8c03 3 *
jehoon 0:df571f8f8c03 4 * Licensed under the Apache License, Version 2.0 (the "License");
jehoon 0:df571f8f8c03 5 * you may not use this file except in compliance with the License.
jehoon 0:df571f8f8c03 6 * You may obtain a copy of the License at
jehoon 0:df571f8f8c03 7 *
jehoon 0:df571f8f8c03 8 * http://www.apache.org/licenses/LICENSE-2.0
jehoon 0:df571f8f8c03 9 *
jehoon 0:df571f8f8c03 10 * Unless required by applicable law or agreed to in writing, software
jehoon 0:df571f8f8c03 11 * distributed under the License is distributed on an "AS IS" BASIS,
jehoon 0:df571f8f8c03 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jehoon 0:df571f8f8c03 13 * See the License for the specific language governing permissions and
jehoon 0:df571f8f8c03 14 * limitations under the License.
jehoon 0:df571f8f8c03 15 */
jehoon 0:df571f8f8c03 16
jehoon 0:df571f8f8c03 17
jehoon 0:df571f8f8c03 18 #include "WizFi310Interface.h"
jehoon 0:df571f8f8c03 19
jehoon 0:df571f8f8c03 20 // Various timeouts for different WizFi310 operations
jehoon 0:df571f8f8c03 21 #define WizFi310_CONNECT_TIMEOUT 15000
jehoon 0:df571f8f8c03 22 #define WizFi310_SEND_TIMEOUT 500
jehoon 0:df571f8f8c03 23 #define WizFi310_RECV_TIMEOUT 0
jehoon 0:df571f8f8c03 24 #define WizFi310_MISC_TIMEOUT 500
jehoon 0:df571f8f8c03 25
jehoon 0:df571f8f8c03 26 #define WizFi310_DELAY_MS 300
jehoon 0:df571f8f8c03 27
jehoon 0:df571f8f8c03 28
jehoon 0:df571f8f8c03 29 // WizFi310Interface implementation
jehoon 0:df571f8f8c03 30 WizFi310Interface::WizFi310Interface(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud)
jehoon 0:df571f8f8c03 31 : _wizfi310(tx, rx, cts, rts, reset, alarm, baud)
jehoon 0:df571f8f8c03 32 {
jehoon 0:df571f8f8c03 33 memset(_ids, 0, sizeof(_ids));
jehoon 0:df571f8f8c03 34 }
jehoon 0:df571f8f8c03 35
jehoon 0:df571f8f8c03 36 int WizFi310Interface::connect(
jehoon 0:df571f8f8c03 37 const char *ssid,
jehoon 0:df571f8f8c03 38 const char *pass,
jehoon 0:df571f8f8c03 39 nsapi_security_t security)
jehoon 0:df571f8f8c03 40 {
jehoon 0:df571f8f8c03 41 if (!_wizfi310.startup())
jehoon 0:df571f8f8c03 42 {
jehoon 0:df571f8f8c03 43 return NSAPI_ERROR_DEVICE_ERROR;
jehoon 0:df571f8f8c03 44 }
jehoon 0:df571f8f8c03 45
jehoon 0:df571f8f8c03 46 _wizfi310.setSsid(ssid);
jehoon 0:df571f8f8c03 47 _wizfi310.setSec(security, pass);
jehoon 0:df571f8f8c03 48 _wizfi310.setAddress("");
jehoon 0:df571f8f8c03 49
jehoon 0:df571f8f8c03 50 if( _wizfi310.join(WizFi310::WM_STATION) == -1)
jehoon 0:df571f8f8c03 51 {
jehoon 0:df571f8f8c03 52 return NSAPI_ERROR_NO_CONNECTION;
jehoon 0:df571f8f8c03 53 }
jehoon 0:df571f8f8c03 54
jehoon 0:df571f8f8c03 55 return 0;
jehoon 0:df571f8f8c03 56 }
jehoon 0:df571f8f8c03 57
stkim92 3:dae9a0924a73 58 int WizFi310Interface::connectAP(
stkim92 3:dae9a0924a73 59 const char *ssid,
stkim92 3:dae9a0924a73 60 const char *pass,
stkim92 3:dae9a0924a73 61 nsapi_security_t security)
stkim92 3:dae9a0924a73 62 {
stkim92 3:dae9a0924a73 63 if (!_wizfi310.startup())
stkim92 3:dae9a0924a73 64 {
stkim92 3:dae9a0924a73 65 return NSAPI_ERROR_DEVICE_ERROR;
stkim92 3:dae9a0924a73 66 }
stkim92 3:dae9a0924a73 67
stkim92 3:dae9a0924a73 68 _wizfi310.setSsid(ssid);
stkim92 3:dae9a0924a73 69 _wizfi310.setSec(security, pass);
stkim92 3:dae9a0924a73 70 //_wizfi310.setAddress("192.168.1.1");
stkim92 3:dae9a0924a73 71 _wizfi310.setAddress("192.168.100.1","255.255.255.0","192.168.100.1");
stkim92 3:dae9a0924a73 72
stkim92 3:dae9a0924a73 73 if( _wizfi310.join(WizFi310::WM_AP) == -1)
stkim92 3:dae9a0924a73 74 {
stkim92 3:dae9a0924a73 75 return NSAPI_ERROR_NO_CONNECTION;
stkim92 3:dae9a0924a73 76 }
stkim92 3:dae9a0924a73 77
stkim92 3:dae9a0924a73 78 return 0;
stkim92 3:dae9a0924a73 79 }
stkim92 3:dae9a0924a73 80
stkim92 3:dae9a0924a73 81
jehoon 0:df571f8f8c03 82 int WizFi310Interface::disconnect()
jehoon 0:df571f8f8c03 83 {
jehoon 0:df571f8f8c03 84 if ( _wizfi310.cmdWLEAVE() == -1 ) return NSAPI_ERROR_DEVICE_ERROR;
jehoon 0:df571f8f8c03 85
jehoon 0:df571f8f8c03 86 return 0;
jehoon 0:df571f8f8c03 87 }
jehoon 0:df571f8f8c03 88
jehoon 0:df571f8f8c03 89 const char *WizFi310Interface::get_ip_address()
jehoon 0:df571f8f8c03 90 {
jehoon 0:df571f8f8c03 91 return _wizfi310.getIPAddress();
jehoon 0:df571f8f8c03 92 }
jehoon 0:df571f8f8c03 93
jehoon 0:df571f8f8c03 94 const char *WizFi310Interface::get_mac_address()
jehoon 0:df571f8f8c03 95 {
jehoon 0:df571f8f8c03 96 return _wizfi310.getMACAddress();
jehoon 0:df571f8f8c03 97 }
jehoon 0:df571f8f8c03 98
jehoon 0:df571f8f8c03 99 struct wizfi310_socket {
jehoon 0:df571f8f8c03 100 int id;
jehoon 0:df571f8f8c03 101 nsapi_protocol_t proto;
jehoon 0:df571f8f8c03 102 bool connected;
kaizen 4:176b6f3addd6 103 uint16_t port;
jehoon 0:df571f8f8c03 104 };
jehoon 0:df571f8f8c03 105
jehoon 0:df571f8f8c03 106 int WizFi310Interface::socket_open(void **handle, nsapi_protocol_t proto)
jehoon 0:df571f8f8c03 107 {
jehoon 0:df571f8f8c03 108 // Look for an unused socket
jehoon 0:df571f8f8c03 109
jehoon 0:df571f8f8c03 110 /*
jehoon 0:df571f8f8c03 111 int id = -1;
jehoon 0:df571f8f8c03 112
jehoon 0:df571f8f8c03 113 for (int i = 0; i < WIZFI310_SOCKET_COUNT; i++) {
jehoon 0:df571f8f8c03 114 if (_ids[i] == false) {
jehoon 0:df571f8f8c03 115 id = i;
jehoon 0:df571f8f8c03 116 _ids[i] = true;
jehoon 0:df571f8f8c03 117 break;
jehoon 0:df571f8f8c03 118 }
jehoon 0:df571f8f8c03 119 }
jehoon 0:df571f8f8c03 120
jehoon 0:df571f8f8c03 121 if (id == -1) {
jehoon 0:df571f8f8c03 122 return NSAPI_ERROR_NO_SOCKET;
jehoon 0:df571f8f8c03 123 }
jehoon 0:df571f8f8c03 124 */
jehoon 0:df571f8f8c03 125
jehoon 0:df571f8f8c03 126 struct wizfi310_socket *socket = new struct wizfi310_socket;
jehoon 0:df571f8f8c03 127 if (!socket) {
jehoon 5:72212beb817c 128 delete socket;
jehoon 0:df571f8f8c03 129 return NSAPI_ERROR_NO_SOCKET;
jehoon 0:df571f8f8c03 130 }
jehoon 0:df571f8f8c03 131
jehoon 0:df571f8f8c03 132 socket->id = -1;
jehoon 0:df571f8f8c03 133 socket->proto = proto;
jehoon 0:df571f8f8c03 134 socket->connected = false;
jehoon 0:df571f8f8c03 135 *handle = socket;
jehoon 0:df571f8f8c03 136 return 0;
jehoon 0:df571f8f8c03 137 }
jehoon 0:df571f8f8c03 138
jehoon 0:df571f8f8c03 139 int WizFi310Interface::socket_close(void *handle)
jehoon 0:df571f8f8c03 140 {
jehoon 0:df571f8f8c03 141 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
jehoon 0:df571f8f8c03 142 int err = 0;
jehoon 0:df571f8f8c03 143
jehoon 0:df571f8f8c03 144 if(socket->id == -1){
jehoon 0:df571f8f8c03 145 err = NSAPI_ERROR_NO_SOCKET;
jehoon 0:df571f8f8c03 146 }
jehoon 0:df571f8f8c03 147 else if (_wizfi310.close(socket->id) == -1) {
jehoon 0:df571f8f8c03 148 err = NSAPI_ERROR_DEVICE_ERROR;
jehoon 0:df571f8f8c03 149 }
jehoon 0:df571f8f8c03 150
jehoon 0:df571f8f8c03 151 _ids[socket->id] = false;
jehoon 0:df571f8f8c03 152 wait_ms(WizFi310_DELAY_MS);
jehoon 0:df571f8f8c03 153 delete socket;
jehoon 0:df571f8f8c03 154 return err;
jehoon 0:df571f8f8c03 155 }
jehoon 0:df571f8f8c03 156
jehoon 0:df571f8f8c03 157 int WizFi310Interface::socket_bind(void *handle, const SocketAddress &address)
jehoon 0:df571f8f8c03 158 {
kaizen 4:176b6f3addd6 159 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
kaizen 4:176b6f3addd6 160 socket->port = address.get_port();
kaizen 4:176b6f3addd6 161
kaizen 4:176b6f3addd6 162 return 0;
jehoon 0:df571f8f8c03 163 }
jehoon 0:df571f8f8c03 164
jehoon 0:df571f8f8c03 165 int WizFi310Interface::socket_listen(void *handle, int backlog)
jehoon 0:df571f8f8c03 166 {
kaizen 4:176b6f3addd6 167 int cid=-1;
kaizen 4:176b6f3addd6 168 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
kaizen 4:176b6f3addd6 169
kaizen 4:176b6f3addd6 170 if((cid = _wizfi310.listen(WizFi310::PROTO_TCP, socket->port)) == -1 )
kaizen 4:176b6f3addd6 171 {
kaizen 4:176b6f3addd6 172 return NSAPI_ERROR_DEVICE_ERROR;
kaizen 4:176b6f3addd6 173 }
kaizen 4:176b6f3addd6 174 if(cid >= WIZFI310_SOCKET_COUNT)
kaizen 4:176b6f3addd6 175 {
kaizen 4:176b6f3addd6 176 return NSAPI_ERROR_NO_SOCKET;
kaizen 4:176b6f3addd6 177 }
kaizen 4:176b6f3addd6 178 _ids[cid] = true;
kaizen 4:176b6f3addd6 179 socket->id = cid;
kaizen 4:176b6f3addd6 180 socket->connected = false;
kaizen 4:176b6f3addd6 181 return 0;
jehoon 0:df571f8f8c03 182 }
jehoon 0:df571f8f8c03 183
jehoon 0:df571f8f8c03 184 int WizFi310Interface::socket_connect(void *handle, const SocketAddress &addr)
jehoon 0:df571f8f8c03 185 {
jehoon 0:df571f8f8c03 186 int cid=-1;
jehoon 0:df571f8f8c03 187 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
jehoon 0:df571f8f8c03 188
jehoon 0:df571f8f8c03 189 WizFi310::Protocol proto = (socket->proto == NSAPI_UDP) ? WizFi310::PROTO_UDP : WizFi310::PROTO_TCP;
jehoon 5:72212beb817c 190
jehoon 0:df571f8f8c03 191 if((cid = _wizfi310.open(proto, addr.get_ip_address(), addr.get_port())) == -1 )
jehoon 0:df571f8f8c03 192 {
jehoon 0:df571f8f8c03 193 return NSAPI_ERROR_DEVICE_ERROR;
jehoon 0:df571f8f8c03 194 }
jehoon 0:df571f8f8c03 195
jehoon 0:df571f8f8c03 196 if(cid >= WIZFI310_SOCKET_COUNT)
jehoon 0:df571f8f8c03 197 {
jehoon 0:df571f8f8c03 198 return NSAPI_ERROR_NO_SOCKET;
jehoon 0:df571f8f8c03 199 }
jehoon 0:df571f8f8c03 200
jehoon 0:df571f8f8c03 201 _ids[cid] = true;
jehoon 0:df571f8f8c03 202 socket->id = cid;
jehoon 0:df571f8f8c03 203 socket->connected = true;
jehoon 0:df571f8f8c03 204 wait_ms(WizFi310_DELAY_MS);
jehoon 0:df571f8f8c03 205 return 0;
jehoon 0:df571f8f8c03 206 }
jehoon 0:df571f8f8c03 207
jehoon 0:df571f8f8c03 208 int WizFi310Interface::socket_accept(void **handle, void *server)
jehoon 0:df571f8f8c03 209 {
kaizen 4:176b6f3addd6 210 struct wizfi310_socket *new_socket = new struct wizfi310_socket;
jehoon 5:72212beb817c 211
kaizen 4:176b6f3addd6 212 if( !new_socket )
kaizen 4:176b6f3addd6 213 {
kaizen 4:176b6f3addd6 214 return NSAPI_ERROR_NO_SOCKET;
kaizen 4:176b6f3addd6 215 }
kaizen 4:176b6f3addd6 216
kaizen 4:176b6f3addd6 217 memset(new_socket, 0, sizeof(new_socket));
jehoon 5:72212beb817c 218
kaizen 4:176b6f3addd6 219 for(int cid=0; cid<WIZFI310_SOCKET_COUNT; cid++)
kaizen 4:176b6f3addd6 220 {
kaizen 4:176b6f3addd6 221 if( _wizfi310.accept(cid) != -1 )
kaizen 4:176b6f3addd6 222 {
kaizen 4:176b6f3addd6 223 _ids[cid] = true;
kaizen 4:176b6f3addd6 224 new_socket->id = cid;
kaizen 4:176b6f3addd6 225 new_socket->connected = true;
kaizen 4:176b6f3addd6 226
kaizen 4:176b6f3addd6 227 *handle = new_socket;
jehoon 5:72212beb817c 228
jehoon 5:72212beb817c 229
kaizen 4:176b6f3addd6 230 return 0;
kaizen 4:176b6f3addd6 231 }
kaizen 4:176b6f3addd6 232 }
jehoon 5:72212beb817c 233 delete new_socket;
kaizen 4:176b6f3addd6 234 return NSAPI_ERROR_WOULD_BLOCK;
jehoon 0:df571f8f8c03 235 }
jehoon 0:df571f8f8c03 236
jehoon 0:df571f8f8c03 237 int WizFi310Interface::socket_send(void *handle, const void *data, unsigned size)
jehoon 0:df571f8f8c03 238 {
jehoon 0:df571f8f8c03 239 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
jehoon 0:df571f8f8c03 240
jehoon 0:df571f8f8c03 241 if ( _wizfi310.send(socket->id, (const char*)data, size) == -1 ) {
jehoon 0:df571f8f8c03 242 return NSAPI_ERROR_DEVICE_ERROR;
jehoon 0:df571f8f8c03 243 }
jehoon 0:df571f8f8c03 244
jehoon 0:df571f8f8c03 245 return size;
jehoon 0:df571f8f8c03 246 }
jehoon 0:df571f8f8c03 247
jehoon 0:df571f8f8c03 248 int WizFi310Interface::socket_recv(void *handle, void *data, unsigned size)
jehoon 0:df571f8f8c03 249 {
jehoon 0:df571f8f8c03 250 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
jehoon 0:df571f8f8c03 251
jehoon 0:df571f8f8c03 252 int32_t recv = _wizfi310.recv(socket->id, (char*)data, size);
kaizen 4:176b6f3addd6 253 if (recv == 0) {
jehoon 0:df571f8f8c03 254 return NSAPI_ERROR_WOULD_BLOCK;
jehoon 0:df571f8f8c03 255 }
kaizen 4:176b6f3addd6 256 else if(recv == -1){
kaizen 4:176b6f3addd6 257 return NSAPI_ERROR_NO_SOCKET;
kaizen 4:176b6f3addd6 258 }
jehoon 0:df571f8f8c03 259
jehoon 0:df571f8f8c03 260 return recv;
jehoon 0:df571f8f8c03 261 }
jehoon 0:df571f8f8c03 262
jehoon 0:df571f8f8c03 263 int WizFi310Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
jehoon 0:df571f8f8c03 264 {
jehoon 0:df571f8f8c03 265 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
jehoon 0:df571f8f8c03 266 if (!socket->connected) {
jehoon 0:df571f8f8c03 267 int err = socket_connect(socket, addr);
jehoon 0:df571f8f8c03 268 if (err < 0) {
jehoon 0:df571f8f8c03 269 return err;
jehoon 0:df571f8f8c03 270 }
jehoon 0:df571f8f8c03 271 }
jehoon 0:df571f8f8c03 272
jehoon 0:df571f8f8c03 273 return socket_send(socket, data, size);
jehoon 0:df571f8f8c03 274 }
jehoon 0:df571f8f8c03 275
jehoon 0:df571f8f8c03 276 int WizFi310Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
jehoon 0:df571f8f8c03 277 {
jehoon 0:df571f8f8c03 278 struct wizfi310_socket *socket = (struct wizfi310_socket *)handle;
jehoon 0:df571f8f8c03 279 return socket_recv(socket, data, size);
jehoon 0:df571f8f8c03 280 }
jehoon 0:df571f8f8c03 281
jehoon 0:df571f8f8c03 282 void WizFi310Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
jehoon 0:df571f8f8c03 283 {
jehoon 0:df571f8f8c03 284 }