cc
SpwfInterface.cpp@16:c8697141ce44, 2017-01-13 (annotated)
- Committer:
- mapellil
- Date:
- Fri Jan 13 14:37:49 2017 +0000
- Revision:
- 16:c8697141ce44
- Parent:
- 15:0a075334e0e9
added get_time API
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 | 9:4cd89586b370 | 41 | #define SPWF_RECV_TIMEOUT 500 |
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; |
mapellil | 14:4ec13009341c | 52 | bool secure; |
mridup | 0:907910bfb0b5 | 53 | }; |
mridup | 0:907910bfb0b5 | 54 | |
mridup | 0:907910bfb0b5 | 55 | |
mridup | 0:907910bfb0b5 | 56 | /** |
mridup | 0:907910bfb0b5 | 57 | * @brief SpwfSAInterface constructor |
mridup | 0:907910bfb0b5 | 58 | * @param tx: Pin USART TX |
mridup | 0:907910bfb0b5 | 59 | * rx: Pin USART RX |
mridup | 0:907910bfb0b5 | 60 | * rst: reset pin for Spwf module |
mridup | 0:907910bfb0b5 | 61 | * wkup: reset pin for Spwf module |
mridup | 0:907910bfb0b5 | 62 | * rts: Pin USART RTS |
mridup | 0:907910bfb0b5 | 63 | * debug : not used |
mridup | 0:907910bfb0b5 | 64 | * @retval none |
mridup | 0:907910bfb0b5 | 65 | */ |
mapellil | 13:0368732b5b9d | 66 | SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, bool debug) |
mapellil | 13:0368732b5b9d | 67 | : _spwf(tx, rx, PC_12, PC_8, debug) |
mapellil | 13:0368732b5b9d | 68 | { |
mapellil | 13:0368732b5b9d | 69 | memset(_ids, 0, sizeof(_ids)); |
mapellil | 13:0368732b5b9d | 70 | isInitialized = false; |
mapellil | 14:4ec13009341c | 71 | isListening = false; |
mapellil | 14:4ec13009341c | 72 | isSecure = false; |
mapellil | 13:0368732b5b9d | 73 | } |
mapellil | 13:0368732b5b9d | 74 | |
mapellil | 13:0368732b5b9d | 75 | SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, PinName reset, PinName wakeup, bool debug) |
mapellil | 13:0368732b5b9d | 76 | : _spwf(tx, rx, reset, wakeup, debug) |
mridup | 0:907910bfb0b5 | 77 | { |
mridup | 0:907910bfb0b5 | 78 | memset(_ids, 0, sizeof(_ids)); |
mridup | 0:907910bfb0b5 | 79 | isInitialized = false; |
mridup | 0:907910bfb0b5 | 80 | isListening = false; |
mapellil | 14:4ec13009341c | 81 | isSecure = false; |
mapellil | 13:0368732b5b9d | 82 | } |
mridup | 0:907910bfb0b5 | 83 | |
mridup | 0:907910bfb0b5 | 84 | /** |
mridup | 0:907910bfb0b5 | 85 | * @brief SpwfSAInterface destructor |
mridup | 0:907910bfb0b5 | 86 | * @param none |
mridup | 0:907910bfb0b5 | 87 | * @retval none |
mridup | 0:907910bfb0b5 | 88 | */ |
mridup | 0:907910bfb0b5 | 89 | SpwfSAInterface::~SpwfSAInterface() |
mridup | 0:907910bfb0b5 | 90 | { |
mridup | 0:907910bfb0b5 | 91 | } |
mridup | 0:907910bfb0b5 | 92 | |
mridup | 0:907910bfb0b5 | 93 | /** |
mridup | 0:907910bfb0b5 | 94 | * @brief init function |
mridup | 0:907910bfb0b5 | 95 | initializes SPWF FW and module |
mridup | 0:907910bfb0b5 | 96 | * @param none |
mridup | 0:907910bfb0b5 | 97 | * @retval error value |
mridup | 0:907910bfb0b5 | 98 | */ |
mridup | 0:907910bfb0b5 | 99 | int SpwfSAInterface::init(void) |
mridup | 0:907910bfb0b5 | 100 | { |
mridup | 1:fd7b3df18dd7 | 101 | if(_spwf.startup(0)) { |
mridup | 1:fd7b3df18dd7 | 102 | isInitialized=true; |
mridup | 1:fd7b3df18dd7 | 103 | return true; |
mridup | 1:fd7b3df18dd7 | 104 | } |
mridup | 1:fd7b3df18dd7 | 105 | else return NSAPI_ERROR_DEVICE_ERROR; |
mridup | 0:907910bfb0b5 | 106 | } |
mridup | 0:907910bfb0b5 | 107 | |
mridup | 0:907910bfb0b5 | 108 | /** |
mridup | 0:907910bfb0b5 | 109 | * @brief network connect |
mridup | 0:907910bfb0b5 | 110 | connects to Access Point |
mridup | 0:907910bfb0b5 | 111 | * @param ap: Access Point (AP) Name String |
mridup | 0:907910bfb0b5 | 112 | * pass_phrase: Password String for AP |
mridup | 0:907910bfb0b5 | 113 | * security: type of NSAPI security supported |
mridup | 0:907910bfb0b5 | 114 | * @retval NSAPI Error Type |
mridup | 0:907910bfb0b5 | 115 | */ |
mridup | 0:907910bfb0b5 | 116 | int SpwfSAInterface::connect(const char *ap, |
mridup | 0:907910bfb0b5 | 117 | const char *pass_phrase, |
mridup | 0:907910bfb0b5 | 118 | nsapi_security_t security) |
mridup | 0:907910bfb0b5 | 119 | { |
mridup | 0:907910bfb0b5 | 120 | int mode; |
mridup | 0:907910bfb0b5 | 121 | |
mridup | 0:907910bfb0b5 | 122 | //initialize the device before connecting |
mridup | 0:907910bfb0b5 | 123 | if(!isInitialized) |
mridup | 0:907910bfb0b5 | 124 | { |
mridup | 1:fd7b3df18dd7 | 125 | if(!init()) |
mridup | 1:fd7b3df18dd7 | 126 | return NSAPI_ERROR_DEVICE_ERROR; |
mridup | 0:907910bfb0b5 | 127 | } |
mapellil | 13:0368732b5b9d | 128 | |
mridup | 0:907910bfb0b5 | 129 | switch(security) |
mridup | 0:907910bfb0b5 | 130 | { |
mridup | 0:907910bfb0b5 | 131 | case NSAPI_SECURITY_NONE: |
mridup | 0:907910bfb0b5 | 132 | mode = 0; |
mridup | 0:907910bfb0b5 | 133 | pass_phrase = NULL; |
mridup | 0:907910bfb0b5 | 134 | break; |
mridup | 0:907910bfb0b5 | 135 | case NSAPI_SECURITY_WEP: |
mridup | 0:907910bfb0b5 | 136 | mode = 1; |
mridup | 0:907910bfb0b5 | 137 | break; |
mridup | 0:907910bfb0b5 | 138 | case NSAPI_SECURITY_WPA: |
mridup | 0:907910bfb0b5 | 139 | case NSAPI_SECURITY_WPA2: |
mridup | 0:907910bfb0b5 | 140 | mode = 2; |
mridup | 0:907910bfb0b5 | 141 | break; |
mridup | 0:907910bfb0b5 | 142 | default: |
mridup | 0:907910bfb0b5 | 143 | mode = 2; |
mridup | 0:907910bfb0b5 | 144 | break; |
mridup | 0:907910bfb0b5 | 145 | } |
mridup | 0:907910bfb0b5 | 146 | return (_spwf.connect((char*)ap, (char*)pass_phrase, mode)); |
mridup | 0:907910bfb0b5 | 147 | } |
mridup | 0:907910bfb0b5 | 148 | |
mridup | 0:907910bfb0b5 | 149 | /** |
mridup | 0:907910bfb0b5 | 150 | * @brief network disconnect |
mridup | 0:907910bfb0b5 | 151 | disconnects from Access Point |
mridup | 0:907910bfb0b5 | 152 | * @param none |
mridup | 0:907910bfb0b5 | 153 | * @retval NSAPI Error Type |
mridup | 0:907910bfb0b5 | 154 | */ |
mridup | 0:907910bfb0b5 | 155 | int SpwfSAInterface::disconnect() |
mridup | 0:907910bfb0b5 | 156 | { |
mridup | 0:907910bfb0b5 | 157 | return (_spwf.disconnect()); |
mridup | 0:907910bfb0b5 | 158 | } |
mridup | 0:907910bfb0b5 | 159 | |
mapellil | 14:4ec13009341c | 160 | /** |
mapellil | 14:4ec13009341c | 161 | * @brief set UTC time on wifi module |
mapellil | 14:4ec13009341c | 162 | * @param time since epoch in UTC format |
mapellil | 14:4ec13009341c | 163 | * @retval true on succes |
mapellil | 14:4ec13009341c | 164 | */ |
mapellil | 14:4ec13009341c | 165 | int SpwfSAInterface::set_time(time_t ctTime) |
mapellil | 14:4ec13009341c | 166 | { |
mapellil | 14:4ec13009341c | 167 | return (_spwf.settime(ctTime)); |
mapellil | 14:4ec13009341c | 168 | } |
mapellil | 14:4ec13009341c | 169 | |
mapellil | 16:c8697141ce44 | 170 | /** |
mapellil | 16:c8697141ce44 | 171 | * @brief get UTC time on wifi module |
mapellil | 16:c8697141ce44 | 172 | * @param time since epoch in UTC format |
mapellil | 16:c8697141ce44 | 173 | * @retval true on succes |
mapellil | 16:c8697141ce44 | 174 | */ |
mapellil | 16:c8697141ce44 | 175 | int SpwfSAInterface::get_time(time_t *ctTime) |
mapellil | 16:c8697141ce44 | 176 | { |
mapellil | 16:c8697141ce44 | 177 | return (_spwf.gettime(ctTime)); |
mapellil | 16:c8697141ce44 | 178 | } |
mapellil | 16:c8697141ce44 | 179 | |
mapellil | 14:4ec13009341c | 180 | int SpwfSAInterface::set_TLS_certificate(char *cert, unsigned int size, CertType_t type) |
mapellil | 14:4ec13009341c | 181 | { |
mapellil | 14:4ec13009341c | 182 | return (_spwf.setTLScertificate(cert, size, type)); |
mapellil | 14:4ec13009341c | 183 | } |
mapellil | 14:4ec13009341c | 184 | |
mapellil | 14:4ec13009341c | 185 | int SpwfSAInterface::set_TLS_SRV_domain(char *domain, CertType_t type) |
mapellil | 14:4ec13009341c | 186 | { |
mapellil | 14:4ec13009341c | 187 | return (_spwf.setTLSSRVdomain(domain, type)); |
mapellil | 14:4ec13009341c | 188 | } |
mapellil | 14:4ec13009341c | 189 | |
mapellil | 14:4ec13009341c | 190 | int SpwfSAInterface::clean_TLS_certificate(CertType_t type) |
mapellil | 14:4ec13009341c | 191 | { |
mapellil | 14:4ec13009341c | 192 | return (_spwf.cleanTLScertificate(type)); |
mapellil | 14:4ec13009341c | 193 | } |
mapellil | 14:4ec13009341c | 194 | |
mridup | 0:907910bfb0b5 | 195 | /** |
mridup | 0:907910bfb0b5 | 196 | * @brief Get the local IP address |
mridup | 0:907910bfb0b5 | 197 | * @param none |
mridup | 0:907910bfb0b5 | 198 | * @retval Null-terminated representation of the local IP address |
mridup | 0:907910bfb0b5 | 199 | * or null if not yet connected |
mridup | 0:907910bfb0b5 | 200 | */ |
mridup | 0:907910bfb0b5 | 201 | const char *SpwfSAInterface::get_ip_address() |
mridup | 0:907910bfb0b5 | 202 | { |
mridup | 0:907910bfb0b5 | 203 | return _spwf.getIPAddress(); |
mridup | 0:907910bfb0b5 | 204 | } |
mridup | 0:907910bfb0b5 | 205 | |
mridup | 0:907910bfb0b5 | 206 | /** |
mridup | 0:907910bfb0b5 | 207 | * @brief Get the MAC address |
mridup | 0:907910bfb0b5 | 208 | * @param none |
mridup | 0:907910bfb0b5 | 209 | * @retval Null-terminated representation of the MAC address |
mridup | 0:907910bfb0b5 | 210 | * or null if not yet connected |
mridup | 0:907910bfb0b5 | 211 | */ |
mridup | 0:907910bfb0b5 | 212 | const char *SpwfSAInterface::get_mac_address() |
mridup | 0:907910bfb0b5 | 213 | { |
mridup | 0:907910bfb0b5 | 214 | return _spwf.getMACAddress(); |
mridup | 0:907910bfb0b5 | 215 | } |
mridup | 0:907910bfb0b5 | 216 | |
mridup | 0:907910bfb0b5 | 217 | /** |
mridup | 0:907910bfb0b5 | 218 | * @brief open a socket handle |
mridup | 0:907910bfb0b5 | 219 | * @param handle: Pointer to handle |
mridup | 0:907910bfb0b5 | 220 | * proto: TCP/UDP protocol |
mridup | 0:907910bfb0b5 | 221 | * @retval NSAPI Error Type |
mridup | 0:907910bfb0b5 | 222 | */ |
mridup | 0:907910bfb0b5 | 223 | int SpwfSAInterface::socket_open(void **handle, nsapi_protocol_t proto) |
mridup | 0:907910bfb0b5 | 224 | { |
mridup | 0:907910bfb0b5 | 225 | int id = -1; |
mridup | 0:907910bfb0b5 | 226 | |
mapellil | 13:0368732b5b9d | 227 | struct spwf_socket *socket = new struct spwf_socket; |
mridup | 0:907910bfb0b5 | 228 | if (!socket) { |
mridup | 0:907910bfb0b5 | 229 | return NSAPI_ERROR_NO_SOCKET; |
mridup | 0:907910bfb0b5 | 230 | } |
mridup | 0:907910bfb0b5 | 231 | |
mridup | 0:907910bfb0b5 | 232 | socket->id = id; |
mridup | 0:907910bfb0b5 | 233 | socket->server_port = id; |
mridup | 0:907910bfb0b5 | 234 | socket->proto = proto; |
mridup | 0:907910bfb0b5 | 235 | socket->connected = false; |
mapellil | 16:c8697141ce44 | 236 | if (isSecure_mode()) socket->secure = true; |
mapellil | 14:4ec13009341c | 237 | else socket->secure = false; |
mridup | 0:907910bfb0b5 | 238 | *handle = socket; |
mridup | 0:907910bfb0b5 | 239 | return 0; |
mridup | 0:907910bfb0b5 | 240 | } |
mridup | 0:907910bfb0b5 | 241 | |
mridup | 0:907910bfb0b5 | 242 | /** |
mapellil | 14:4ec13009341c | 243 | * @brief set secure mode for all the subsequently created sockets |
mapellil | 14:4ec13009341c | 244 | * @param none |
mapellil | 14:4ec13009341c | 245 | * @retval void |
mapellil | 14:4ec13009341c | 246 | */ |
mapellil | 15:0a075334e0e9 | 247 | void SpwfSAInterface::set_secure_mode(void) |
mapellil | 14:4ec13009341c | 248 | { |
mapellil | 14:4ec13009341c | 249 | isSecure = true; |
mapellil | 14:4ec13009341c | 250 | } |
mapellil | 14:4ec13009341c | 251 | |
mapellil | 14:4ec13009341c | 252 | /** |
mapellil | 14:4ec13009341c | 253 | * @brief set unsecure mode for all the subsequently created sockets |
mapellil | 14:4ec13009341c | 254 | * @param none |
mapellil | 14:4ec13009341c | 255 | * @retval void |
mapellil | 14:4ec13009341c | 256 | */ |
mapellil | 15:0a075334e0e9 | 257 | void SpwfSAInterface::set_unsecure_mode(void) |
mapellil | 14:4ec13009341c | 258 | { |
mapellil | 14:4ec13009341c | 259 | isSecure = false; |
mapellil | 14:4ec13009341c | 260 | } |
mapellil | 14:4ec13009341c | 261 | |
mapellil | 14:4ec13009341c | 262 | /** |
mapellil | 14:4ec13009341c | 263 | * @brief Return the interface security mode set |
mapellil | 14:4ec13009341c | 264 | * @param none |
mapellil | 14:4ec13009341c | 265 | * @retval true if secure |
mapellil | 14:4ec13009341c | 266 | */ |
mapellil | 16:c8697141ce44 | 267 | bool SpwfSAInterface::isSecure_mode(void) |
mapellil | 14:4ec13009341c | 268 | { |
mapellil | 14:4ec13009341c | 269 | return isSecure; |
mapellil | 14:4ec13009341c | 270 | } |
mapellil | 14:4ec13009341c | 271 | |
mapellil | 14:4ec13009341c | 272 | /** |
mridup | 0:907910bfb0b5 | 273 | * @brief connect to a remote socket |
mridup | 0:907910bfb0b5 | 274 | * @param handle: Pointer to socket handle |
mridup | 0:907910bfb0b5 | 275 | * addr: Address to connect to |
mridup | 0:907910bfb0b5 | 276 | * @retval NSAPI Error Type |
mridup | 0:907910bfb0b5 | 277 | */ |
mridup | 0:907910bfb0b5 | 278 | int SpwfSAInterface::socket_connect(void *handle, const SocketAddress &addr) |
mridup | 0:907910bfb0b5 | 279 | { |
mapellil | 14:4ec13009341c | 280 | // int sock_id = 99; |
mapellil | 14:4ec13009341c | 281 | int sock_id = -1; |
mapellil | 14:4ec13009341c | 282 | const char* proto; |
mridup | 0:907910bfb0b5 | 283 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
mapellil | 14:4ec13009341c | 284 | if (socket->secure) |
mapellil | 14:4ec13009341c | 285 | proto="s"; |
mapellil | 14:4ec13009341c | 286 | else |
mapellil | 14:4ec13009341c | 287 | proto=(socket->proto == NSAPI_UDP) ? "u" : "t"; |
mridup | 0:907910bfb0b5 | 288 | |
mapellil | 14:4ec13009341c | 289 | if (!_spwf.open(proto, &sock_id, addr.get_ip_address(), addr.get_port())) {//sock ID is allocated NOW |
mridup | 1:fd7b3df18dd7 | 290 | return NSAPI_ERROR_DEVICE_ERROR; |
mridup | 1:fd7b3df18dd7 | 291 | } |
mridup | 0:907910bfb0b5 | 292 | |
mridup | 0:907910bfb0b5 | 293 | //TODO: Maintain a socket table to map socket ID to host & port |
mridup | 0:907910bfb0b5 | 294 | //TODO: lookup on client table to see if already socket is allocated to same host/port |
mridup | 0:907910bfb0b5 | 295 | //multimap <char *, vector <uint16_t> > ::iterator i = c_table.find((char*)ip); |
mridup | 0:907910bfb0b5 | 296 | |
mridup | 0:907910bfb0b5 | 297 | if(sock_id <= SPWFSA_SOCKET_COUNT) |
mridup | 0:907910bfb0b5 | 298 | { |
mridup | 0:907910bfb0b5 | 299 | socket->id = sock_id;//the socket ID of this Socket instance |
mridup | 0:907910bfb0b5 | 300 | _ids[socket->id] = true; |
mridup | 0:907910bfb0b5 | 301 | socket->connected = true; |
mridup | 0:907910bfb0b5 | 302 | } |
mridup | 0:907910bfb0b5 | 303 | else |
mridup | 0:907910bfb0b5 | 304 | return NSAPI_ERROR_NO_SOCKET; |
mridup | 0:907910bfb0b5 | 305 | |
mridup | 0:907910bfb0b5 | 306 | return 0; |
mridup | 0:907910bfb0b5 | 307 | } |
mridup | 0:907910bfb0b5 | 308 | |
mridup | 0:907910bfb0b5 | 309 | /** |
mridup | 0:907910bfb0b5 | 310 | * @brief bind to a port number and address |
mridup | 0:907910bfb0b5 | 311 | * @param handle: Pointer to socket handle |
mridup | 0:907910bfb0b5 | 312 | * proto: address to bind to |
mridup | 0:907910bfb0b5 | 313 | * @retval NSAPI Error Type |
mridup | 0:907910bfb0b5 | 314 | */ |
mridup | 0:907910bfb0b5 | 315 | int SpwfSAInterface::socket_bind(void *handle, const SocketAddress &address) |
mridup | 0:907910bfb0b5 | 316 | { |
mridup | 0:907910bfb0b5 | 317 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
mridup | 0:907910bfb0b5 | 318 | socket->server_port = address.get_port(); |
mridup | 0:907910bfb0b5 | 319 | return 0; |
mridup | 0:907910bfb0b5 | 320 | } |
mridup | 0:907910bfb0b5 | 321 | |
mridup | 0:907910bfb0b5 | 322 | /** |
mridup | 0:907910bfb0b5 | 323 | * @brief start listening on a port and address |
mridup | 0:907910bfb0b5 | 324 | * @param handle: Pointer to handle |
mridup | 0:907910bfb0b5 | 325 | * backlog: not used (always value is 1) |
mridup | 0:907910bfb0b5 | 326 | * @retval NSAPI Error Type |
mridup | 0:907910bfb0b5 | 327 | */ |
mridup | 0:907910bfb0b5 | 328 | int SpwfSAInterface::socket_listen(void *handle, int backlog) |
mridup | 0:907910bfb0b5 | 329 | { |
mridup | 0:907910bfb0b5 | 330 | return NSAPI_ERROR_UNSUPPORTED; |
mridup | 0:907910bfb0b5 | 331 | } |
mridup | 0:907910bfb0b5 | 332 | |
mridup | 0:907910bfb0b5 | 333 | /** |
mridup | 0:907910bfb0b5 | 334 | * @brief accept connections from remote sockets |
mridup | 0:907910bfb0b5 | 335 | * @param handle: Pointer to handle of client socket (connecting) |
mridup | 0:907910bfb0b5 | 336 | * proto: handle of server socket which will accept connections |
mridup | 0:907910bfb0b5 | 337 | * @retval NSAPI Error Type |
mridup | 0:907910bfb0b5 | 338 | */ |
mridup | 0:907910bfb0b5 | 339 | int SpwfSAInterface::socket_accept(void **handle, void *server) |
mridup | 0:907910bfb0b5 | 340 | { |
mridup | 0:907910bfb0b5 | 341 | return NSAPI_ERROR_UNSUPPORTED; |
mridup | 0:907910bfb0b5 | 342 | } |
mridup | 0:907910bfb0b5 | 343 | |
mridup | 0:907910bfb0b5 | 344 | /** |
mridup | 0:907910bfb0b5 | 345 | * @brief close a socket |
mridup | 0:907910bfb0b5 | 346 | * @param handle: Pointer to handle |
mridup | 0:907910bfb0b5 | 347 | * @retval NSAPI Error Type |
mridup | 0:907910bfb0b5 | 348 | */ |
mridup | 0:907910bfb0b5 | 349 | int SpwfSAInterface::socket_close(void *handle) |
mridup | 0:907910bfb0b5 | 350 | { |
mridup | 0:907910bfb0b5 | 351 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
mridup | 0:907910bfb0b5 | 352 | int err = 0; |
mridup | 0:907910bfb0b5 | 353 | |
mridup | 0:907910bfb0b5 | 354 | if(socket->id!=-1) |
mridup | 0:907910bfb0b5 | 355 | { |
mridup | 1:fd7b3df18dd7 | 356 | if (_spwf.close(socket->id)) { |
mridup | 1:fd7b3df18dd7 | 357 | if(socket->id==SERVER_SOCKET_NO) |
mridup | 1:fd7b3df18dd7 | 358 | isListening = false; |
mridup | 1:fd7b3df18dd7 | 359 | else |
mridup | 1:fd7b3df18dd7 | 360 | _ids[socket->id] = false; |
mridup | 0:907910bfb0b5 | 361 | } |
mridup | 1:fd7b3df18dd7 | 362 | else err = NSAPI_ERROR_DEVICE_ERROR; |
mridup | 0:907910bfb0b5 | 363 | } |
mridup | 0:907910bfb0b5 | 364 | |
mridup | 0:907910bfb0b5 | 365 | delete socket; |
mridup | 0:907910bfb0b5 | 366 | return err; |
mridup | 0:907910bfb0b5 | 367 | } |
mridup | 0:907910bfb0b5 | 368 | |
mridup | 0:907910bfb0b5 | 369 | /** |
mridup | 0:907910bfb0b5 | 370 | * @brief write to a socket |
mridup | 0:907910bfb0b5 | 371 | * @param handle: Pointer to handle |
mridup | 0:907910bfb0b5 | 372 | * data: pointer to data |
mridup | 0:907910bfb0b5 | 373 | * size: size of data |
mridup | 0:907910bfb0b5 | 374 | * @retval no of bytes sent |
mridup | 0:907910bfb0b5 | 375 | */ |
mridup | 0:907910bfb0b5 | 376 | int SpwfSAInterface::socket_send(void *handle, const void *data, unsigned size) |
mridup | 0:907910bfb0b5 | 377 | { |
mridup | 0:907910bfb0b5 | 378 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
mridup | 3:4c2cbca19031 | 379 | //int err; |
mridup | 0:907910bfb0b5 | 380 | |
mridup | 1:fd7b3df18dd7 | 381 | /*if(socket->id==SERVER_SOCKET_NO) |
mridup | 0:907910bfb0b5 | 382 | { |
mridup | 0:907910bfb0b5 | 383 | if(socket->server_port==-1 || !isListening) |
mridup | 0:907910bfb0b5 | 384 | return NSAPI_ERROR_NO_SOCKET; //server socket not bound or not listening |
mridup | 0:907910bfb0b5 | 385 | |
mridup | 1:fd7b3df18dd7 | 386 | err = _spwf.socket_server_write((uint16_t)size, (char*)data); |
mridup | 0:907910bfb0b5 | 387 | } |
mridup | 0:907910bfb0b5 | 388 | else |
mridup | 3:4c2cbca19031 | 389 | { |
mridup | 1:fd7b3df18dd7 | 390 | err = _spwf.send(socket->id, (char*)data, (uint32_t)size); |
mridup | 3:4c2cbca19031 | 391 | }*/ |
mridup | 3:4c2cbca19031 | 392 | if (!_spwf.send(socket->id, (char*)data, (uint32_t)size)) { |
mridup | 3:4c2cbca19031 | 393 | return NSAPI_ERROR_DEVICE_ERROR; |
mridup | 3:4c2cbca19031 | 394 | } |
mridup | 3:4c2cbca19031 | 395 | |
mridup | 3:4c2cbca19031 | 396 | return size; |
mridup | 0:907910bfb0b5 | 397 | } |
mridup | 0:907910bfb0b5 | 398 | |
mridup | 0:907910bfb0b5 | 399 | /** |
mridup | 0:907910bfb0b5 | 400 | * @brief receive data on a socket |
mridup | 0:907910bfb0b5 | 401 | * @param handle: Pointer to handle |
mridup | 0:907910bfb0b5 | 402 | * data: pointer to data |
mridup | 0:907910bfb0b5 | 403 | * size: size of data |
mridup | 0:907910bfb0b5 | 404 | * @retval no of bytes read |
mridup | 0:907910bfb0b5 | 405 | */ |
mridup | 0:907910bfb0b5 | 406 | int SpwfSAInterface::socket_recv(void *handle, void *data, unsigned size) |
mridup | 0:907910bfb0b5 | 407 | { |
mridup | 0:907910bfb0b5 | 408 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
mridup | 0:907910bfb0b5 | 409 | int32_t recv; |
mridup | 0:907910bfb0b5 | 410 | |
mridup | 0:907910bfb0b5 | 411 | //CHECK:Receive for both Client and Server Sockets same? |
mridup | 1:fd7b3df18dd7 | 412 | recv = _spwf.recv(socket->id, (char*)data, (uint32_t)size); |
mridup | 1:fd7b3df18dd7 | 413 | if (recv < 0) { |
mridup | 5:8ee8698d19e3 | 414 | //wait_ms(1);//delay of 1ms <for F4>?? |
mridup | 5:8ee8698d19e3 | 415 | //printf("."); |
mridup | 5:8ee8698d19e3 | 416 | if (recv == -1) return NSAPI_ERROR_WOULD_BLOCK;//send this if we want to block call (else timeout will happen) |
mapellil | 13:0368732b5b9d | 417 | else if (recv == -2)return NSAPI_ERROR_DEVICE_ERROR; |
mapellil | 13:0368732b5b9d | 418 | else if (recv == -3)return NSAPI_ERROR_NO_CONNECTION; |
mridup | 1:fd7b3df18dd7 | 419 | } |
mapellil | 13:0368732b5b9d | 420 | return recv; |
mridup | 0:907910bfb0b5 | 421 | } |
mridup | 0:907910bfb0b5 | 422 | |
mridup | 0:907910bfb0b5 | 423 | /** |
mridup | 0:907910bfb0b5 | 424 | * @brief send data to a udp socket |
mridup | 0:907910bfb0b5 | 425 | * @param handle: Pointer to handle |
mridup | 0:907910bfb0b5 | 426 | * addr: address of udp socket |
mridup | 0:907910bfb0b5 | 427 | * data: pointer to data |
mridup | 0:907910bfb0b5 | 428 | * size: size of data |
mridup | 0:907910bfb0b5 | 429 | * @retval no of bytes sent |
mridup | 0:907910bfb0b5 | 430 | */ |
mridup | 0:907910bfb0b5 | 431 | int SpwfSAInterface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) |
mridup | 0:907910bfb0b5 | 432 | { |
mridup | 0:907910bfb0b5 | 433 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
mapellil | 13:0368732b5b9d | 434 | if (!socket->connected) { |
mridup | 0:907910bfb0b5 | 435 | int err = socket_connect(socket, addr); |
mridup | 0:907910bfb0b5 | 436 | if (err < 0) { |
mridup | 0:907910bfb0b5 | 437 | return err; |
mridup | 0:907910bfb0b5 | 438 | } |
mridup | 0:907910bfb0b5 | 439 | } |
mridup | 0:907910bfb0b5 | 440 | |
mridup | 0:907910bfb0b5 | 441 | return socket_send(socket, data, size); |
mridup | 0:907910bfb0b5 | 442 | } |
mridup | 0:907910bfb0b5 | 443 | |
mridup | 0:907910bfb0b5 | 444 | /** |
mridup | 0:907910bfb0b5 | 445 | * @brief receive data on a udp socket |
mridup | 0:907910bfb0b5 | 446 | * @param handle: Pointer to handle |
mridup | 0:907910bfb0b5 | 447 | * addr: address of udp socket |
mridup | 0:907910bfb0b5 | 448 | * data: pointer to data |
mridup | 0:907910bfb0b5 | 449 | * size: size of data |
mridup | 0:907910bfb0b5 | 450 | * @retval no of bytes read |
mridup | 0:907910bfb0b5 | 451 | */ |
mridup | 0:907910bfb0b5 | 452 | int SpwfSAInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) |
mridup | 0:907910bfb0b5 | 453 | { |
mridup | 0:907910bfb0b5 | 454 | struct spwf_socket *socket = (struct spwf_socket *)handle; |
mridup | 0:907910bfb0b5 | 455 | return socket_recv(socket, data, size); |
mridup | 0:907910bfb0b5 | 456 | } |
mridup | 0:907910bfb0b5 | 457 | |
mridup | 0:907910bfb0b5 | 458 | /** |
mridup | 0:907910bfb0b5 | 459 | * @brief attach function/callback to the socket |
mridup | 0:907910bfb0b5 | 460 | * Not used |
mridup | 0:907910bfb0b5 | 461 | * @param handle: Pointer to handle |
mridup | 0:907910bfb0b5 | 462 | * callback: callback function pointer |
mridup | 0:907910bfb0b5 | 463 | * data: pointer to data |
mridup | 0:907910bfb0b5 | 464 | * @retval none |
mridup | 0:907910bfb0b5 | 465 | */ |
mridup | 0:907910bfb0b5 | 466 | void SpwfSAInterface::socket_attach(void *handle, void (*callback)(void *), void *data) |
mridup | 0:907910bfb0b5 | 467 | { |
mridup | 0:907910bfb0b5 | 468 | //No implementation yet |
mridup | 0:907910bfb0b5 | 469 | } |
mridup | 0:907910bfb0b5 | 470 | |
mridup | 0:907910bfb0b5 | 471 | /** |
mridup | 0:907910bfb0b5 | 472 | * @brief utility debug function for printing to serial terminal |
mridup | 0:907910bfb0b5 | 473 | * @param string: Pointer to data |
mridup | 0:907910bfb0b5 | 474 | * @retval none |
mridup | 0:907910bfb0b5 | 475 | */ |
mridup | 0:907910bfb0b5 | 476 | void SpwfSAInterface::debug(const char * string) |
mridup | 0:907910bfb0b5 | 477 | { |
mridup | 0:907910bfb0b5 | 478 | //_spwf.debug_print(string); |
mridup | 0:907910bfb0b5 | 479 | } |
mapellil | 13:0368732b5b9d | 480 | |
mapellil | 13:0368732b5b9d | 481 | /** |
mapellil | 13:0368732b5b9d | 482 | * @brief Set the socket options |
mapellil | 13:0368732b5b9d | 483 | * Not used |
mapellil | 13:0368732b5b9d | 484 | * @param handle: Pointer to handle |
mapellil | 13:0368732b5b9d | 485 | * level: SOL_SOCKET |
mapellil | 13:0368732b5b9d | 486 | * optname: option name |
mapellil | 13:0368732b5b9d | 487 | * optval: pointer to option value |
mapellil | 13:0368732b5b9d | 488 | * optlen: option length |
mapellil | 13:0368732b5b9d | 489 | @retval NSAPI Error Type |
mapellil | 13:0368732b5b9d | 490 | */ |
mapellil | 13:0368732b5b9d | 491 | int SpwfSAInterface::setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen) |
mapellil | 13:0368732b5b9d | 492 | { |
mapellil | 13:0368732b5b9d | 493 | // struct spwf_socket *socket = (struct spwf_socket *)handle; |
mapellil | 13:0368732b5b9d | 494 | |
mapellil | 13:0368732b5b9d | 495 | switch (optname) { |
mapellil | 13:0368732b5b9d | 496 | case NSAPI_REUSEADDR: /*!< Allow bind to reuse local addresses */ |
mapellil | 13:0368732b5b9d | 497 | case NSAPI_KEEPALIVE: /*!< Enables sending of keepalive messages */ |
mapellil | 13:0368732b5b9d | 498 | case NSAPI_LINGER: /*!< Keeps close from returning until queues empty */ |
mapellil | 13:0368732b5b9d | 499 | case NSAPI_SNDBUF: /*!< Sets send buffer size */ |
mapellil | 13:0368732b5b9d | 500 | case NSAPI_RCVBUF: /*!< Sets recv buffer size */ |
mapellil | 13:0368732b5b9d | 501 | default: |
mapellil | 13:0368732b5b9d | 502 | printf("SpwfSAInterface::setsockopt> ERROR!!!! Unknown optname: %d \r\n", optname); |
mapellil | 13:0368732b5b9d | 503 | return -1; |
mapellil | 13:0368732b5b9d | 504 | } |
mapellil | 13:0368732b5b9d | 505 | return NSAPI_ERROR_UNSUPPORTED; |
mapellil | 13:0368732b5b9d | 506 | } |
mapellil | 13:0368732b5b9d | 507 | |
mapellil | 13:0368732b5b9d | 508 | /** |
mapellil | 13:0368732b5b9d | 509 | * @brief Get the socket options |
mapellil | 13:0368732b5b9d | 510 | * Not used |
mapellil | 13:0368732b5b9d | 511 | * @param handle: Pointer to handle |
mapellil | 13:0368732b5b9d | 512 | * level: SOL_SOCKET |
mapellil | 13:0368732b5b9d | 513 | * optname: option name |
mapellil | 13:0368732b5b9d | 514 | * optval: pointer to option value |
mapellil | 13:0368732b5b9d | 515 | * optlen: pointer to option length |
mapellil | 13:0368732b5b9d | 516 | @retval NSAPI Error Type |
mapellil | 13:0368732b5b9d | 517 | */ |
mapellil | 13:0368732b5b9d | 518 | int SpwfSAInterface::getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen) |
mapellil | 13:0368732b5b9d | 519 | { |
mapellil | 13:0368732b5b9d | 520 | return NSAPI_ERROR_UNSUPPORTED; |
mapellil | 13:0368732b5b9d | 521 | } |