Extending the X_NUCLEO_IDW01M1 to allow configuration of the board as an access point

Dependents:   X_NUCLEO_IDW01M1_AP_Test

Fork of X_NUCLEO_IDW01M1 by ST

Committer:
mridup
Date:
Thu Jun 02 05:52:45 2016 +0000
Revision:
16:156be7b55904
Parent:
15:225d5d3c4f84
Child:
17:2c47c2aab4a4
few comments and documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mridup 14:af04fe9ca955 1 /* SpwfSAInterface implementation of NetworkStack
mridup 0:dc55f40eb04f 2 * Copyright (c) 2015 ARM Limited
mridup 0:dc55f40eb04f 3 *
mridup 0:dc55f40eb04f 4 * Licensed under the Apache License, Version 2.0 (the "License");
mridup 0:dc55f40eb04f 5 * you may not use this file except in compliance with the License.
mridup 0:dc55f40eb04f 6 * You may obtain a copy of the License at
mridup 0:dc55f40eb04f 7 *
mridup 0:dc55f40eb04f 8 * http://www.apache.org/licenses/LICENSE-2.0
mridup 0:dc55f40eb04f 9 *
mridup 0:dc55f40eb04f 10 * Unless required by applicable law or agreed to in writing, software
mridup 0:dc55f40eb04f 11 * distributed under the License is distributed on an "AS IS" BASIS,
mridup 0:dc55f40eb04f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mridup 0:dc55f40eb04f 13 * See the License for the specific language governing permissions and
mridup 0:dc55f40eb04f 14 * limitations under the License.
mridup 0:dc55f40eb04f 15 */
mridup 0:dc55f40eb04f 16
mridup 13:cdcc13d78252 17 /**
mridup 13:cdcc13d78252 18 ******************************************************************************
mridup 13:cdcc13d78252 19 * @file SpwfInterface.cpp
mridup 13:cdcc13d78252 20 * @author STMicroelectronics
mridup 13:cdcc13d78252 21 * @brief Implementation of the NetworkStack for the SPWF Device
mridup 13:cdcc13d78252 22 ******************************************************************************
mridup 13:cdcc13d78252 23 * @copy
mridup 13:cdcc13d78252 24 *
mridup 13:cdcc13d78252 25 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
mridup 13:cdcc13d78252 26 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
mridup 13:cdcc13d78252 27 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
mridup 13:cdcc13d78252 28 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
mridup 13:cdcc13d78252 29 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
mridup 13:cdcc13d78252 30 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
mridup 13:cdcc13d78252 31 *
mridup 13:cdcc13d78252 32 * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
mridup 13:cdcc13d78252 33 ******************************************************************************
mridup 13:cdcc13d78252 34 */
mridup 13:cdcc13d78252 35
mridup 0:dc55f40eb04f 36 #include "SPWFInterface.h"
mridup 0:dc55f40eb04f 37
mridup 0:dc55f40eb04f 38 // Various timeouts for different SPWF operations
mridup 0:dc55f40eb04f 39 #define SPWF_CONNECT_TIMEOUT 20000
mridup 0:dc55f40eb04f 40 #define SPWF_SEND_TIMEOUT 500
mridup 7:0fdd186a7d90 41 #define SPWF_RECV_TIMEOUT 1000
mridup 0:dc55f40eb04f 42 #define SPWF_MISC_TIMEOUT 15000
mridup 5:c83ffd44f40a 43
mridup 16:156be7b55904 44 /** spwf_socket class
mridup 16:156be7b55904 45 * Implementation of SPWF socket structure
mridup 16:156be7b55904 46 */
mridup 5:c83ffd44f40a 47 struct spwf_socket {
mridup 5:c83ffd44f40a 48 int id;
mridup 6:e7a3fca2df10 49 int server_port;
mridup 5:c83ffd44f40a 50 nsapi_protocol_t proto;
mridup 5:c83ffd44f40a 51 bool connected;
mridup 5:c83ffd44f40a 52 };
mridup 0:dc55f40eb04f 53
mridup 11:67a8c3499caf 54
mridup 11:67a8c3499caf 55 /**
mridup 11:67a8c3499caf 56 * @brief SpwfSAInterface constructor
mridup 11:67a8c3499caf 57 * @param tx: Pin USART TX
mridup 11:67a8c3499caf 58 * rx: Pin USART RX
mridup 11:67a8c3499caf 59 * rst: reset pin for Spwf module
mridup 11:67a8c3499caf 60 * wkup: reset pin for Spwf module
mridup 11:67a8c3499caf 61 * rts: Pin USART RTS
mridup 11:67a8c3499caf 62 * debug : not used
mridup 11:67a8c3499caf 63 * @retval none
mridup 11:67a8c3499caf 64 */
mridup 0:dc55f40eb04f 65 SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, PinName rst, PinName wkup, PinName rts, bool debug)
mridup 0:dc55f40eb04f 66 : _spwf(tx, rx, rst, wkup, rts)
mridup 0:dc55f40eb04f 67 {
mridup 0:dc55f40eb04f 68 memset(_ids, 0, sizeof(_ids));
mridup 3:fd9d20c4d3f0 69 isInitialized = false;
mridup 6:e7a3fca2df10 70 isListening = false;
mridup 0:dc55f40eb04f 71 }
mridup 12:3799f8475c8a 72
mridup 12:3799f8475c8a 73 /**
mridup 12:3799f8475c8a 74 * @brief SpwfSAInterface destructor
mridup 12:3799f8475c8a 75 * @param none
mridup 12:3799f8475c8a 76 * @retval none
mridup 12:3799f8475c8a 77 */
mridup 0:dc55f40eb04f 78 SpwfSAInterface::~SpwfSAInterface()
mridup 0:dc55f40eb04f 79 {
mridup 0:dc55f40eb04f 80 }
mridup 0:dc55f40eb04f 81
mridup 12:3799f8475c8a 82 /**
mridup 12:3799f8475c8a 83 * @brief init function
mridup 12:3799f8475c8a 84 initializes SPWF FW and module
mridup 12:3799f8475c8a 85 * @param none
mridup 12:3799f8475c8a 86 * @retval error value
mridup 12:3799f8475c8a 87 */
mridup 5:c83ffd44f40a 88 int SpwfSAInterface::init(void)
mridup 0:dc55f40eb04f 89 {
mridup 0:dc55f40eb04f 90 _spwf.setTimeout(SPWF_MISC_TIMEOUT);
mridup 0:dc55f40eb04f 91 return (_spwf.init());
mridup 0:dc55f40eb04f 92 }
mridup 0:dc55f40eb04f 93
mridup 12:3799f8475c8a 94 /**
mridup 12:3799f8475c8a 95 * @brief network connect
mridup 12:3799f8475c8a 96 connects to Access Point
mridup 12:3799f8475c8a 97 * @param ap: Access Point (AP) Name String
mridup 12:3799f8475c8a 98 * pass_phrase: Password String for AP
mridup 12:3799f8475c8a 99 * security: type of NSAPI security supported
mridup 12:3799f8475c8a 100 * @retval NSAPI Error Type
mridup 12:3799f8475c8a 101 */
mridup 5:c83ffd44f40a 102 int SpwfSAInterface::connect(
mridup 0:dc55f40eb04f 103 const char *ap,
mridup 0:dc55f40eb04f 104 const char *pass_phrase,
mridup 5:c83ffd44f40a 105 nsapi_security_t security)
mridup 0:dc55f40eb04f 106 {
mridup 15:225d5d3c4f84 107 WiFi_Priv_Mode mode;
mridup 15:225d5d3c4f84 108
mridup 3:fd9d20c4d3f0 109 //initialize the device before connecting
mridup 3:fd9d20c4d3f0 110 if(!isInitialized)
mridup 3:fd9d20c4d3f0 111 {
mridup 3:fd9d20c4d3f0 112 if(init()==0)
mridup 3:fd9d20c4d3f0 113 isInitialized=true;
mridup 5:c83ffd44f40a 114 else return NSAPI_ERROR_DEVICE_ERROR;
mridup 3:fd9d20c4d3f0 115 }
mridup 3:fd9d20c4d3f0 116
mridup 15:225d5d3c4f84 117 _spwf.setTimeout(SPWF_CONNECT_TIMEOUT);
mridup 0:dc55f40eb04f 118
mridup 15:225d5d3c4f84 119 switch(security)
mridup 15:225d5d3c4f84 120 {
mridup 15:225d5d3c4f84 121 case NSAPI_SECURITY_NONE:
mridup 15:225d5d3c4f84 122 mode = (WiFi_Priv_Mode) None;
mridup 15:225d5d3c4f84 123 pass_phrase = NULL;
mridup 15:225d5d3c4f84 124 break;
mridup 15:225d5d3c4f84 125 case NSAPI_SECURITY_WEP:
mridup 15:225d5d3c4f84 126 mode = (WiFi_Priv_Mode) WEP;
mridup 15:225d5d3c4f84 127 break;
mridup 15:225d5d3c4f84 128 case NSAPI_SECURITY_WPA:
mridup 15:225d5d3c4f84 129 case NSAPI_SECURITY_WPA2:
mridup 15:225d5d3c4f84 130 mode = (WiFi_Priv_Mode) WPA_Personal;
mridup 15:225d5d3c4f84 131 break;
mridup 15:225d5d3c4f84 132 default:
mridup 15:225d5d3c4f84 133 mode = (WiFi_Priv_Mode) WPA_Personal;
mridup 15:225d5d3c4f84 134 break;
mridup 15:225d5d3c4f84 135 }
mridup 0:dc55f40eb04f 136
mridup 12:3799f8475c8a 137 return (_spwf.connect((char*)ap, (char*)pass_phrase, mode));
mridup 0:dc55f40eb04f 138 }
mridup 12:3799f8475c8a 139
mridup 12:3799f8475c8a 140 /**
mridup 12:3799f8475c8a 141 * @brief network disconnect
mridup 12:3799f8475c8a 142 disconnects from Access Point
mridup 12:3799f8475c8a 143 * @param none
mridup 12:3799f8475c8a 144 * @retval NSAPI Error Type
mridup 12:3799f8475c8a 145 */
mridup 5:c83ffd44f40a 146 int SpwfSAInterface::disconnect()
mridup 5:c83ffd44f40a 147 {
mridup 0:dc55f40eb04f 148 return (_spwf.disconnect());
mridup 0:dc55f40eb04f 149 }
mridup 12:3799f8475c8a 150
mridup 12:3799f8475c8a 151 /**
mridup 12:3799f8475c8a 152 * @brief Get the local IP address
mridup 12:3799f8475c8a 153 * @param none
mridup 12:3799f8475c8a 154 * @retval Null-terminated representation of the local IP address
mridup 12:3799f8475c8a 155 * or null if not yet connected
mridup 12:3799f8475c8a 156 */
mridup 5:c83ffd44f40a 157 const char *SpwfSAInterface::get_ip_address()
mridup 0:dc55f40eb04f 158 {
mridup 0:dc55f40eb04f 159 return _spwf.getIPAddress();
mridup 0:dc55f40eb04f 160 }
mridup 12:3799f8475c8a 161
mridup 12:3799f8475c8a 162 /**
mridup 12:3799f8475c8a 163 * @brief Get the MAC address
mridup 12:3799f8475c8a 164 * @param none
mridup 12:3799f8475c8a 165 * @retval Null-terminated representation of the MAC address
mridup 12:3799f8475c8a 166 * or null if not yet connected
mridup 12:3799f8475c8a 167 */
mridup 5:c83ffd44f40a 168 const char *SpwfSAInterface::get_mac_address()
mridup 0:dc55f40eb04f 169 {
mridup 0:dc55f40eb04f 170 return _spwf.getMACAddress();
mridup 0:dc55f40eb04f 171 }
mridup 0:dc55f40eb04f 172
mridup 12:3799f8475c8a 173 /**
mridup 12:3799f8475c8a 174 * @brief open a socket handle
mridup 12:3799f8475c8a 175 * @param handle: Pointer to handle
mridup 12:3799f8475c8a 176 * proto: TCP/UDP protocol
mridup 12:3799f8475c8a 177 * @retval NSAPI Error Type
mridup 12:3799f8475c8a 178 */
mridup 5:c83ffd44f40a 179 int SpwfSAInterface::socket_open(void **handle, nsapi_protocol_t proto)
mridup 0:dc55f40eb04f 180 {
mridup 5:c83ffd44f40a 181 int id = -1;
mridup 5:c83ffd44f40a 182
mridup 5:c83ffd44f40a 183 struct spwf_socket *socket = new struct spwf_socket;
mridup 5:c83ffd44f40a 184 if (!socket) {
mridup 5:c83ffd44f40a 185 return NSAPI_ERROR_NO_SOCKET;
mridup 5:c83ffd44f40a 186 }
mridup 5:c83ffd44f40a 187
mridup 5:c83ffd44f40a 188 socket->id = id;
mridup 6:e7a3fca2df10 189 socket->server_port = id;
mridup 5:c83ffd44f40a 190 socket->proto = proto;
mridup 5:c83ffd44f40a 191 socket->connected = false;
mridup 5:c83ffd44f40a 192 *handle = socket;
mridup 5:c83ffd44f40a 193 return 0;
mridup 0:dc55f40eb04f 194 }
mridup 5:c83ffd44f40a 195
mridup 12:3799f8475c8a 196 /**
mridup 12:3799f8475c8a 197 * @brief connect to a remote socket
mridup 12:3799f8475c8a 198 * @param handle: Pointer to socket handle
mridup 12:3799f8475c8a 199 * addr: Address to connect to
mridup 12:3799f8475c8a 200 * @retval NSAPI Error Type
mridup 12:3799f8475c8a 201 */
mridup 5:c83ffd44f40a 202 int SpwfSAInterface::socket_connect(void *handle, const SocketAddress &addr)
mridup 5:c83ffd44f40a 203 {
mridup 6:e7a3fca2df10 204 uint8_t sock_id = 99;
mridup 5:c83ffd44f40a 205 struct spwf_socket *socket = (struct spwf_socket *)handle;
mridup 5:c83ffd44f40a 206
mridup 5:c83ffd44f40a 207 const char *proto = (socket->proto == NSAPI_UDP) ? "u" : "t";//"s" for secure socket?
mridup 0:dc55f40eb04f 208
mridup 5:c83ffd44f40a 209 _spwf.socket_client_open((uint8_t*)addr.get_ip_address(), (uint32_t)addr.get_port(), (uint8_t *)proto, &sock_id);//sock ID is allocated NOW
mridup 0:dc55f40eb04f 210
mridup 0:dc55f40eb04f 211 //TODO: Maintain a socket table to map socket ID to host & port
mridup 0:dc55f40eb04f 212 //TODO: lookup on client table to see if already socket is allocated to same host/port
mridup 0:dc55f40eb04f 213 //multimap <char *, vector <uint16_t> > ::iterator i = c_table.find((char*)ip);
mridup 0:dc55f40eb04f 214
mridup 0:dc55f40eb04f 215 if(sock_id <= SPWFSA_SOCKET_COUNT)
mridup 0:dc55f40eb04f 216 {
mridup 5:c83ffd44f40a 217 socket->id = sock_id;//the socket ID of this Socket instance
mridup 5:c83ffd44f40a 218 _ids[socket->id] = true;
mridup 5:c83ffd44f40a 219 socket->connected = true;
mridup 0:dc55f40eb04f 220 }
mridup 0:dc55f40eb04f 221 else
mridup 5:c83ffd44f40a 222 return NSAPI_ERROR_NO_SOCKET;
mridup 0:dc55f40eb04f 223
mridup 12:3799f8475c8a 224 return 0;
mridup 0:dc55f40eb04f 225 }
mridup 5:c83ffd44f40a 226
mridup 12:3799f8475c8a 227 /**
mridup 12:3799f8475c8a 228 * @brief bind to a port number and address
mridup 12:3799f8475c8a 229 * @param handle: Pointer to socket handle
mridup 12:3799f8475c8a 230 * proto: address to bind to
mridup 12:3799f8475c8a 231 * @retval NSAPI Error Type
mridup 12:3799f8475c8a 232 */
mridup 5:c83ffd44f40a 233 int SpwfSAInterface::socket_bind(void *handle, const SocketAddress &address)
mridup 5:c83ffd44f40a 234 {
mridup 7:0fdd186a7d90 235 struct spwf_socket *socket = (struct spwf_socket *)handle;
mridup 6:e7a3fca2df10 236 socket->server_port = address.get_port();
mridup 6:e7a3fca2df10 237 return 0;
mridup 5:c83ffd44f40a 238 }
mridup 5:c83ffd44f40a 239
mridup 12:3799f8475c8a 240 /**
mridup 12:3799f8475c8a 241 * @brief start listening on a port and address
mridup 12:3799f8475c8a 242 * @param handle: Pointer to handle
mridup 12:3799f8475c8a 243 * backlog: not used (always value is 1)
mridup 12:3799f8475c8a 244 * @retval NSAPI Error Type
mridup 12:3799f8475c8a 245 */
mridup 5:c83ffd44f40a 246 int SpwfSAInterface::socket_listen(void *handle, int backlog)
mridup 0:dc55f40eb04f 247 {
mridup 6:e7a3fca2df10 248 struct spwf_socket *socket = (struct spwf_socket *)handle;
mridup 6:e7a3fca2df10 249 int err;
mridup 6:e7a3fca2df10 250
mridup 6:e7a3fca2df10 251 if(socket->server_port==-1 || isListening)
mridup 6:e7a3fca2df10 252 return NSAPI_ERROR_NO_SOCKET; //server socket not bound or already listening
mridup 6:e7a3fca2df10 253
mridup 6:e7a3fca2df10 254 const char *proto = (socket->proto == NSAPI_UDP) ? "u" : "t";
mridup 6:e7a3fca2df10 255
mridup 6:e7a3fca2df10 256 err = _spwf.socket_server_open((uint32_t)socket->server_port, (uint8_t *)proto);
mridup 6:e7a3fca2df10 257
mridup 6:e7a3fca2df10 258 if(err==0)
mridup 6:e7a3fca2df10 259 {
mridup 7:0fdd186a7d90 260 socket->id = SERVER_SOCKET_NO;//Special socket ID number for Server Socket
mridup 6:e7a3fca2df10 261 isListening = true;
mridup 6:e7a3fca2df10 262 }
mridup 6:e7a3fca2df10 263 else
mridup 6:e7a3fca2df10 264 return NSAPI_ERROR_DEVICE_ERROR;
mridup 6:e7a3fca2df10 265
mridup 6:e7a3fca2df10 266 return err;
mridup 5:c83ffd44f40a 267 }
mridup 5:c83ffd44f40a 268
mridup 12:3799f8475c8a 269 /**
mridup 12:3799f8475c8a 270 * @brief accept connections from remote sockets
mridup 12:3799f8475c8a 271 * @param handle: Pointer to handle of client socket (connecting)
mridup 12:3799f8475c8a 272 * proto: handle of server socket which will accept connections
mridup 12:3799f8475c8a 273 * @retval NSAPI Error Type
mridup 12:3799f8475c8a 274 */
mridup 5:c83ffd44f40a 275 int SpwfSAInterface::socket_accept(void **handle, void *server)
mridup 5:c83ffd44f40a 276 {
mridup 6:e7a3fca2df10 277 struct spwf_socket *server_socket = (struct spwf_socket *)server;
mridup 6:e7a3fca2df10 278
mridup 6:e7a3fca2df10 279 if(server_socket->server_port==-1 || !isListening)
mridup 6:e7a3fca2df10 280 return NSAPI_ERROR_NO_SOCKET; //server socket not bound or not listening
mridup 6:e7a3fca2df10 281
mridup 7:0fdd186a7d90 282 if(_spwf.get_wait_for_incoming_client())
mridup 7:0fdd186a7d90 283 {
mridup 7:0fdd186a7d90 284 server_socket->connected = true;
mridup 7:0fdd186a7d90 285 struct spwf_socket *socket = new struct spwf_socket;//create new network socket
mridup 7:0fdd186a7d90 286 if (!socket) {
mridup 7:0fdd186a7d90 287 return NSAPI_ERROR_NO_SOCKET;
mridup 7:0fdd186a7d90 288 }
mridup 7:0fdd186a7d90 289
mridup 7:0fdd186a7d90 290 memset(socket, 0, sizeof (struct spwf_socket));
mridup 11:67a8c3499caf 291
mridup 7:0fdd186a7d90 292 socket->id = server_socket->id;
mridup 7:0fdd186a7d90 293 socket->server_port = server_socket->server_port;
mridup 7:0fdd186a7d90 294 socket->proto = server_socket->proto;
mridup 7:0fdd186a7d90 295 socket->connected = true;
mridup 7:0fdd186a7d90 296
mridup 7:0fdd186a7d90 297 *handle = socket;
mridup 7:0fdd186a7d90 298 socket = 0;
mridup 12:3799f8475c8a 299 _spwf.set_wait_for_incoming_client(false);
mridup 7:0fdd186a7d90 300 wait_ms(50);//CHECK:TODO:Why do we need this?
mridup 7:0fdd186a7d90 301 return 0;
mridup 7:0fdd186a7d90 302 }
mridup 7:0fdd186a7d90 303 return NSAPI_ERROR_WOULD_BLOCK;
mridup 5:c83ffd44f40a 304 }
mridup 5:c83ffd44f40a 305
mridup 12:3799f8475c8a 306 /**
mridup 12:3799f8475c8a 307 * @brief close a socket
mridup 12:3799f8475c8a 308 * @param handle: Pointer to handle
mridup 12:3799f8475c8a 309 * @retval NSAPI Error Type
mridup 12:3799f8475c8a 310 */
mridup 5:c83ffd44f40a 311 int SpwfSAInterface::socket_close(void *handle)
mridup 5:c83ffd44f40a 312 {
mridup 5:c83ffd44f40a 313 struct spwf_socket *socket = (struct spwf_socket *)handle;
mridup 5:c83ffd44f40a 314 int err = 0;
mridup 7:0fdd186a7d90 315 _spwf.setTimeout(SPWF_MISC_TIMEOUT);
mridup 0:dc55f40eb04f 316
mridup 5:c83ffd44f40a 317 if(socket->id!=-1)
mridup 5:c83ffd44f40a 318 {
mridup 6:e7a3fca2df10 319 if(socket->id==SERVER_SOCKET_NO)
mridup 6:e7a3fca2df10 320 {
mridup 6:e7a3fca2df10 321 if (_spwf.socket_server_close()==-1) {
mridup 6:e7a3fca2df10 322 err = NSAPI_ERROR_DEVICE_ERROR;
mridup 6:e7a3fca2df10 323 }
mridup 6:e7a3fca2df10 324 isListening = false;
mridup 5:c83ffd44f40a 325 }
mridup 6:e7a3fca2df10 326 else
mridup 6:e7a3fca2df10 327 {
mridup 6:e7a3fca2df10 328 if (_spwf.socket_client_close(socket->id)==-1) {
mridup 6:e7a3fca2df10 329 err = NSAPI_ERROR_DEVICE_ERROR;
mridup 6:e7a3fca2df10 330 }
mridup 6:e7a3fca2df10 331 _ids[socket->id] = false;
mridup 6:e7a3fca2df10 332 }
mridup 5:c83ffd44f40a 333 }
mridup 5:c83ffd44f40a 334
mridup 5:c83ffd44f40a 335 delete socket;
mridup 0:dc55f40eb04f 336 return err;
mridup 0:dc55f40eb04f 337 }
mridup 5:c83ffd44f40a 338
mridup 12:3799f8475c8a 339 /**
mridup 12:3799f8475c8a 340 * @brief write to a socket
mridup 12:3799f8475c8a 341 * @param handle: Pointer to handle
mridup 12:3799f8475c8a 342 * data: pointer to data
mridup 12:3799f8475c8a 343 * size: size of data
mridup 12:3799f8475c8a 344 * @retval no of bytes sent
mridup 12:3799f8475c8a 345 */
mridup 5:c83ffd44f40a 346 int SpwfSAInterface::socket_send(void *handle, const void *data, unsigned size)
mridup 5:c83ffd44f40a 347 {
mridup 5:c83ffd44f40a 348 struct spwf_socket *socket = (struct spwf_socket *)handle;
mridup 5:c83ffd44f40a 349 int err;
mridup 0:dc55f40eb04f 350
mridup 6:e7a3fca2df10 351 if(socket->id==SERVER_SOCKET_NO)
mridup 6:e7a3fca2df10 352 {
mridup 7:0fdd186a7d90 353 if(socket->server_port==-1 || !isListening)
mridup 7:0fdd186a7d90 354 return NSAPI_ERROR_NO_SOCKET; //server socket not bound or not listening
mridup 7:0fdd186a7d90 355
mridup 6:e7a3fca2df10 356 err = _spwf.socket_server_write((uint16_t)size, (char*)data);
mridup 6:e7a3fca2df10 357 }
mridup 6:e7a3fca2df10 358 else
mridup 6:e7a3fca2df10 359 {
mridup 6:e7a3fca2df10 360 err = _spwf.socket_client_write((uint8_t)socket->id, (uint16_t)size, (char*)data);
mridup 6:e7a3fca2df10 361 }
mridup 0:dc55f40eb04f 362
mridup 0:dc55f40eb04f 363 return err;
mridup 0:dc55f40eb04f 364 }
mridup 0:dc55f40eb04f 365
mridup 12:3799f8475c8a 366 /**
mridup 12:3799f8475c8a 367 * @brief receive data on a socket
mridup 12:3799f8475c8a 368 * @param handle: Pointer to handle
mridup 12:3799f8475c8a 369 * data: pointer to data
mridup 12:3799f8475c8a 370 * size: size of data
mridup 12:3799f8475c8a 371 * @retval no of bytes read
mridup 12:3799f8475c8a 372 */
mridup 5:c83ffd44f40a 373 int SpwfSAInterface::socket_recv(void *handle, void *data, unsigned size)
mridup 0:dc55f40eb04f 374 {
mridup 5:c83ffd44f40a 375 struct spwf_socket *socket = (struct spwf_socket *)handle;
mridup 0:dc55f40eb04f 376 int32_t recv;
mridup 0:dc55f40eb04f 377
mridup 5:c83ffd44f40a 378 _spwf.setTimeout(SPWF_RECV_TIMEOUT);
mridup 0:dc55f40eb04f 379
mridup 6:e7a3fca2df10 380 //CHECK:Receive for both Client and Server Sockets same?
mridup 5:c83ffd44f40a 381 recv = _spwf.socket_client_recv((uint8_t)socket->id, (uint16_t)size, (char*)data);
mridup 5:c83ffd44f40a 382 if (recv < 0) {
mridup 5:c83ffd44f40a 383 return NSAPI_ERROR_WOULD_BLOCK;
mridup 5:c83ffd44f40a 384 }
mridup 0:dc55f40eb04f 385 return recv;
mridup 0:dc55f40eb04f 386
mridup 0:dc55f40eb04f 387 }
mridup 0:dc55f40eb04f 388
mridup 12:3799f8475c8a 389 /**
mridup 12:3799f8475c8a 390 * @brief send data to a udp socket
mridup 12:3799f8475c8a 391 * @param handle: Pointer to handle
mridup 12:3799f8475c8a 392 * addr: address of udp socket
mridup 12:3799f8475c8a 393 * data: pointer to data
mridup 12:3799f8475c8a 394 * size: size of data
mridup 12:3799f8475c8a 395 * @retval no of bytes sent
mridup 12:3799f8475c8a 396 */
mridup 5:c83ffd44f40a 397 int SpwfSAInterface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
mridup 5:c83ffd44f40a 398 {
mridup 5:c83ffd44f40a 399 struct spwf_socket *socket = (struct spwf_socket *)handle;
mridup 5:c83ffd44f40a 400 if (!socket->connected) {
mridup 5:c83ffd44f40a 401 int err = socket_connect(socket, addr);
mridup 5:c83ffd44f40a 402 if (err < 0) {
mridup 5:c83ffd44f40a 403 return err;
mridup 5:c83ffd44f40a 404 }
mridup 5:c83ffd44f40a 405 }
mridup 5:c83ffd44f40a 406
mridup 5:c83ffd44f40a 407 return socket_send(socket, data, size);
mridup 5:c83ffd44f40a 408 }
mridup 5:c83ffd44f40a 409
mridup 12:3799f8475c8a 410 /**
mridup 12:3799f8475c8a 411 * @brief receive data on a udp socket
mridup 12:3799f8475c8a 412 * @param handle: Pointer to handle
mridup 12:3799f8475c8a 413 * addr: address of udp socket
mridup 12:3799f8475c8a 414 * data: pointer to data
mridup 12:3799f8475c8a 415 * size: size of data
mridup 12:3799f8475c8a 416 * @retval no of bytes read
mridup 12:3799f8475c8a 417 */
mridup 5:c83ffd44f40a 418 int SpwfSAInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
mridup 5:c83ffd44f40a 419 {
mridup 5:c83ffd44f40a 420 struct spwf_socket *socket = (struct spwf_socket *)handle;
mridup 5:c83ffd44f40a 421 return socket_recv(socket, data, size);
mridup 5:c83ffd44f40a 422 }
mridup 5:c83ffd44f40a 423
mridup 12:3799f8475c8a 424 /**
mridup 12:3799f8475c8a 425 * @brief attach function/callback to the socket
mridup 12:3799f8475c8a 426 * Not used
mridup 12:3799f8475c8a 427 * @param handle: Pointer to handle
mridup 12:3799f8475c8a 428 * callback: callback function pointer
mridup 12:3799f8475c8a 429 * data: pointer to data
mridup 12:3799f8475c8a 430 * @retval none
mridup 12:3799f8475c8a 431 */
mridup 5:c83ffd44f40a 432 void SpwfSAInterface::socket_attach(void *handle, void (*callback)(void *), void *data)
mridup 5:c83ffd44f40a 433 {
mridup 5:c83ffd44f40a 434 //No implementation yet
mridup 5:c83ffd44f40a 435 }
mridup 5:c83ffd44f40a 436
mridup 12:3799f8475c8a 437 /**
mridup 12:3799f8475c8a 438 * @brief utility debug function for printing to serial terminal
mridup 12:3799f8475c8a 439 * @param string: Pointer to data
mridup 12:3799f8475c8a 440 * @retval none
mridup 12:3799f8475c8a 441 */
mridup 0:dc55f40eb04f 442 void SpwfSAInterface::debug(const char * string)
mridup 0:dc55f40eb04f 443 {
mridup 0:dc55f40eb04f 444 _spwf.debug_print(string);
mridup 0:dc55f40eb04f 445 }