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