Maggie Mei / emw3162-driver

Dependents:   HelloEMW3162

Fork of emw3162-driver by Maggie Mei

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EMW3162Interface.cpp Source File

EMW3162Interface.cpp

00001 /* EMW3162 implementation of NetworkInterfaceAPI
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 #include "EMW3162Interface.h"
00019 
00020 // Various timeouts for different EMW3162 operations
00021 #define EMW3162_CONNECT_TIMEOUT 15000
00022 #define EMW3162_SEND_TIMEOUT    500
00023 #define EMW3162_RECV_TIMEOUT    0
00024 #define EMW3162_MISC_TIMEOUT    500
00025 
00026 
00027 // EMW3162Interface implementation
00028 EMW3162Interface::EMW3162Interface(PinName tx, PinName rx, bool debug)
00029     : _emw(tx, rx, debug)
00030 {
00031     memset(_ids, 0, sizeof(_ids));
00032     memset(_cbs, 0, sizeof(_cbs));
00033 
00034     _emw.attach(this, &EMW3162Interface::event);
00035 }
00036 
00037 int EMW3162Interface::connect(
00038     const char *ssid,
00039     const char *pass,
00040     nsapi_security_t security)
00041 {
00042     _emw.setTimeout(EMW3162_CONNECT_TIMEOUT);
00043 
00044     if (!_emw.startup()) {
00045         return NSAPI_ERROR_DEVICE_ERROR;
00046     }
00047 
00048     if (!_emw.dhcp(true)) {
00049         return NSAPI_ERROR_DHCP_FAILURE;
00050     }
00051 
00052     if (!_emw.connect(ssid, pass)) {
00053         return NSAPI_ERROR_NO_CONNECTION;
00054     }
00055 
00056     if (!_emw.getIPAddress()) {
00057         return NSAPI_ERROR_DHCP_FAILURE;
00058     }
00059 
00060     return 0;
00061 }
00062 
00063 int EMW3162Interface::disconnect()
00064 {
00065     _emw.setTimeout(EMW3162_MISC_TIMEOUT);
00066 
00067     if (!_emw.disconnect()) {
00068         return NSAPI_ERROR_DEVICE_ERROR;
00069     }
00070 
00071     return 0;
00072 }
00073 
00074 const char* EMW3162Interface::get_ip_address()
00075 {
00076     return _emw.getIPAddress();
00077 }
00078 
00079 const char* EMW3162Interface::get_mac_address()
00080 {
00081     return _emw.getMACAddress();
00082 }
00083 
00084 struct EMW3162_socket {
00085     int id;
00086     int socketId;
00087     nsapi_protocol_t proto;
00088     bool connected;
00089 };
00090 
00091 int EMW3162Interface::socket_open(void **handle, nsapi_protocol_t proto)
00092 {
00093     // Look for an unused socket
00094     int id = -1;
00095  
00096     for (int i = 1; i < EMW3162_SOCKET_COUNT; i++) {
00097         if (!_ids[i]) {
00098             id = i;
00099             _ids[i] = true;
00100             break;
00101         }
00102     }
00103  
00104     if (id == -1) {
00105         return NSAPI_ERROR_NO_SOCKET;
00106     }
00107     
00108     struct EMW3162_socket *socket = new struct EMW3162_socket;
00109     if (!socket) {
00110         return NSAPI_ERROR_NO_SOCKET;
00111     }
00112     
00113     socket->id = id;
00114     socket->socketId = 0;
00115     socket->proto = proto;
00116     socket->connected = false;
00117     *handle = socket;
00118     return 0;
00119 }
00120 
00121 int EMW3162Interface::socket_close(void *handle)
00122 {
00123     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
00124     int err = 0;
00125     _emw.setTimeout(EMW3162_MISC_TIMEOUT);
00126  
00127     if (!_emw.close(socket->socketId)) {
00128         err = NSAPI_ERROR_DEVICE_ERROR;
00129     }
00130 
00131     _ids[socket->id] = false;
00132     delete socket;
00133     return err;
00134 }
00135 
00136 int EMW3162Interface::socket_bind(void *handle, const SocketAddress &address)
00137 {
00138     return NSAPI_ERROR_UNSUPPORTED;
00139 }
00140 
00141 int EMW3162Interface::socket_listen(void *handle, int backlog)
00142 {
00143     return NSAPI_ERROR_UNSUPPORTED;
00144 }
00145 
00146 int EMW3162Interface::socket_connect(void *handle, const SocketAddress &addr)
00147 {
00148     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
00149     _emw.setTimeout(EMW3162_MISC_TIMEOUT);
00150 
00151     const char *proto = (socket->proto == NSAPI_UDP) ? "UNICAST" : "CLIENT";
00152     socket -> socketId = _emw.open(proto, socket->id, addr.get_ip_address(), addr.get_port());
00153     if (!(socket -> socketId)) {
00154         return NSAPI_ERROR_DEVICE_ERROR;
00155     }
00156     
00157     socket->connected = true;
00158     return 0;
00159 }
00160     
00161 int EMW3162Interface::socket_accept(void **handle, void *server)
00162 {
00163     return NSAPI_ERROR_UNSUPPORTED;
00164 }
00165 
00166 int EMW3162Interface::socket_send(void *handle, const void *data, unsigned size)
00167 {
00168     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
00169     _emw.setTimeout(EMW3162_SEND_TIMEOUT);
00170  
00171     if (!_emw.send(socket->socketId, data, size)) {
00172         return NSAPI_ERROR_DEVICE_ERROR;
00173     }
00174  
00175     return size;
00176 }
00177 
00178 int EMW3162Interface::socket_recv(void *handle, void *data, unsigned size)
00179 {
00180     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
00181     _emw.setTimeout(EMW3162_RECV_TIMEOUT);
00182  
00183     int32_t recv = _emw.recv(socket->socketId, data, size);
00184     if (recv < 0) {
00185         return NSAPI_ERROR_WOULD_BLOCK;
00186     }
00187  
00188     return recv;
00189 }
00190 
00191 int EMW3162Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
00192 {
00193     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
00194     if (!socket->connected) {
00195         int err = socket_connect(socket, addr);
00196         if (err < 0) {
00197             return err;
00198         }
00199     }
00200     
00201     return socket_send(socket, data, size);
00202 }
00203 
00204 int EMW3162Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
00205 {
00206     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;    
00207     return socket_recv(socket, data, size);
00208 }
00209 
00210 void EMW3162Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
00211 {
00212     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;    
00213     _cbs[socket->id].callback = callback;
00214     _cbs[socket->id].data = data;
00215 }
00216 
00217 void EMW3162Interface::event() {
00218     for (int i = 0; i < EMW3162_SOCKET_COUNT; i++) {
00219         if (_cbs[i].callback) {
00220             _cbs[i].callback(_cbs[i].data);
00221         }
00222     }
00223 }