Sarah Marsh / emw3162-driver

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     : _esp(tx, rx, debug)
00030 {
00031     memset(_ids, 0, sizeof(_ids));
00032     memset(_cbs, 0, sizeof(_cbs));
00033 
00034     _esp.attach(this, &EMW3162Interface::event);
00035 }
00036 
00037 int EMW3162Interface::connect()
00038 {
00039     _esp.setTimeout(EMW3162_CONNECT_TIMEOUT);
00040 
00041     if (!_esp.startup()) {
00042         return NSAPI_ERROR_DEVICE_ERROR;
00043     }
00044 
00045     if (!_esp.dhcp(true)) {
00046         return NSAPI_ERROR_DHCP_FAILURE;
00047     }
00048 
00049     if (!_esp.connect(ap_ssid, ap_pass)) {
00050         return NSAPI_ERROR_NO_CONNECTION;
00051     }
00052 
00053     if (!_esp.getIPAddress()) {
00054         return NSAPI_ERROR_DHCP_FAILURE;
00055     }
00056 
00057     return 0;
00058 }
00059 
00060 int EMW3162Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
00061                                         uint8_t channel)
00062 {
00063     if (channel != 0) {
00064         return NSAPI_ERROR_UNSUPPORTED;
00065     }
00066 
00067     set_credentials(ssid, pass, security);
00068     return connect();
00069 }
00070 
00071 int EMW3162Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
00072 {
00073     memset(ap_ssid, 0, sizeof(ap_ssid));
00074     strncpy(ap_ssid, ssid, sizeof(ap_ssid));
00075 
00076     memset(ap_pass, 0, sizeof(ap_pass));
00077     strncpy(ap_pass, pass, sizeof(ap_pass));
00078 
00079     ap_sec = security;
00080 
00081     return 0;
00082 }
00083 
00084 int EMW3162Interface::set_channel(uint8_t channel)
00085 {
00086     return NSAPI_ERROR_UNSUPPORTED;
00087 }
00088 
00089 int EMW3162Interface::disconnect()
00090 {
00091     _esp.setTimeout(EMW3162_MISC_TIMEOUT);
00092 
00093     if (!_esp.disconnect()) {
00094         return NSAPI_ERROR_DEVICE_ERROR;
00095     }
00096 
00097     return 0;
00098 }
00099 
00100 const char* EMW3162Interface::get_ip_address()
00101 {
00102     return _esp.getIPAddress();
00103 }
00104 
00105 const char* EMW3162Interface::get_mac_address()
00106 {
00107     return _esp.getMACAddress();
00108 }
00109 
00110 int EMW3162Interface::scan(WiFiAccessPoint *res, unsigned count){
00111     return NSAPI_ERROR_UNSUPPORTED;
00112 }
00113 
00114 int8_t EMW3162Interface::get_rssi()
00115 {
00116     return NSAPI_ERROR_UNSUPPORTED;
00117 }
00118 
00119 struct EMW3162_socket {
00120     int id;
00121     int socketId;
00122     nsapi_protocol_t proto;
00123     bool connected;
00124 };
00125 
00126 int EMW3162Interface::socket_open(void **handle, nsapi_protocol_t proto)
00127 {
00128     // Look for an unused socket
00129     int id = -1;
00130  
00131     for (int i = 1; i < EMW3162_SOCKET_COUNT; i++) {
00132         if (!_ids[i]) {
00133             id = i;
00134             _ids[i] = true;
00135             break;
00136         }
00137     }
00138  
00139     if (id == -1) {
00140         return NSAPI_ERROR_NO_SOCKET;
00141     }
00142     
00143     struct EMW3162_socket *socket = new struct EMW3162_socket;
00144     if (!socket) {
00145         return NSAPI_ERROR_NO_SOCKET;
00146     }
00147     
00148     socket->id = id;
00149     socket->socketId = 0;
00150     socket->proto = proto;
00151     socket->connected = false;
00152     *handle = socket;
00153     return 0;
00154 }
00155 
00156 int EMW3162Interface::socket_close(void *handle)
00157 {
00158     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
00159     int err = 0;
00160     _esp.setTimeout(EMW3162_MISC_TIMEOUT);
00161  
00162     if (!_esp.close(socket->socketId)) {
00163         err = NSAPI_ERROR_DEVICE_ERROR;
00164     }
00165 
00166     _ids[socket->id] = false;
00167     delete socket;
00168     return err;
00169 }
00170 
00171 int EMW3162Interface::socket_bind(void *handle, const SocketAddress &address)
00172 {
00173     return NSAPI_ERROR_UNSUPPORTED;
00174 }
00175 
00176 int EMW3162Interface::socket_listen(void *handle, int backlog)
00177 {
00178     return NSAPI_ERROR_UNSUPPORTED;
00179 }
00180 
00181 int EMW3162Interface::socket_connect(void *handle, const SocketAddress &addr)
00182 {
00183     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
00184     _esp.setTimeout(EMW3162_MISC_TIMEOUT);
00185 
00186     const char *proto = (socket->proto == NSAPI_UDP) ? "UNICAST" : "CLIENT";
00187     socket -> socketId = _esp.open(proto, socket->id, addr.get_ip_address(), addr.get_port());
00188     if (!(socket -> socketId)) {
00189         return NSAPI_ERROR_DEVICE_ERROR;
00190     }
00191     
00192     socket->connected = true;
00193     return 0;
00194 }
00195     
00196 int EMW3162Interface::socket_accept(void *handle, void **socket, SocketAddress *address)
00197 {
00198     return NSAPI_ERROR_UNSUPPORTED;
00199 }
00200 
00201 int EMW3162Interface::socket_send(void *handle, const void *data, unsigned size)
00202 {
00203     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
00204     _esp.setTimeout(EMW3162_SEND_TIMEOUT);
00205  
00206     if (!_esp.send(socket->socketId, data, size)) {
00207         return NSAPI_ERROR_DEVICE_ERROR;
00208     }
00209  
00210     return size;
00211 }
00212 
00213 int EMW3162Interface::socket_recv(void *handle, void *data, unsigned size)
00214 {
00215     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
00216     _esp.setTimeout(EMW3162_RECV_TIMEOUT);
00217  
00218     int32_t recv = _esp.recv(socket->socketId, data, size);
00219     if (recv < 0) {
00220         return NSAPI_ERROR_WOULD_BLOCK;
00221     }
00222  
00223     return recv;
00224 }
00225 
00226 int EMW3162Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
00227 {
00228     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;
00229     if (!socket->connected) {
00230         int err = socket_connect(socket, addr);
00231         if (err < 0) {
00232             return err;
00233         }
00234     }
00235     
00236     return socket_send(socket, data, size);
00237 }
00238 
00239 int EMW3162Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
00240 {
00241     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;    
00242     return socket_recv(socket, data, size);
00243 }
00244 
00245 void EMW3162Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
00246 {
00247     struct EMW3162_socket *socket = (struct EMW3162_socket *)handle;    
00248     _cbs[socket->id].callback = callback;
00249     _cbs[socket->id].data = data;
00250 }
00251 
00252 void EMW3162Interface::event() {
00253     for (int i = 0; i < EMW3162_SOCKET_COUNT; i++) {
00254         if (_cbs[i].callback) {
00255             _cbs[i].callback(_cbs[i].data);
00256         }
00257     }
00258 }