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
SPWFInterface.cpp@4:d7d25616f1f7, 2016-04-19 (annotated)
- Committer:
- mridup
- Date:
- Tue Apr 19 07:42:22 2016 +0000
- Revision:
- 4:d7d25616f1f7
- Parent:
- 3:fd9d20c4d3f0
- Child:
- 5:c83ffd44f40a
removing isConnected() check in Socket APIs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 0:dc55f40eb04f | 1 | /* SpwfSAInterface implementation of NetworkInterfaceAPI |
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 | 0:dc55f40eb04f | 17 | #include "SPWFInterface.h" |
mridup | 0:dc55f40eb04f | 18 | |
mridup | 0:dc55f40eb04f | 19 | // Various timeouts for different SPWF operations |
mridup | 0:dc55f40eb04f | 20 | #define SPWF_CONNECT_TIMEOUT 20000 |
mridup | 0:dc55f40eb04f | 21 | #define SPWF_SEND_TIMEOUT 500 |
mridup | 0:dc55f40eb04f | 22 | #define SPWF_RECV_TIMEOUT 10000 |
mridup | 0:dc55f40eb04f | 23 | #define SPWF_MISC_TIMEOUT 15000 |
mridup | 0:dc55f40eb04f | 24 | |
mridup | 0:dc55f40eb04f | 25 | |
mridup | 0:dc55f40eb04f | 26 | // SpwfSAInterface implementation |
mridup | 0:dc55f40eb04f | 27 | SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, PinName rst, PinName wkup, PinName rts, bool debug) |
mridup | 0:dc55f40eb04f | 28 | : _spwf(tx, rx, rst, wkup, rts) |
mridup | 0:dc55f40eb04f | 29 | { |
mridup | 0:dc55f40eb04f | 30 | memset(_ids, 0, sizeof(_ids)); |
mridup | 3:fd9d20c4d3f0 | 31 | isInitialized = false; |
mridup | 0:dc55f40eb04f | 32 | } |
mridup | 0:dc55f40eb04f | 33 | |
mridup | 0:dc55f40eb04f | 34 | SpwfSAInterface::~SpwfSAInterface() |
mridup | 0:dc55f40eb04f | 35 | { |
mridup | 0:dc55f40eb04f | 36 | } |
mridup | 0:dc55f40eb04f | 37 | |
mridup | 0:dc55f40eb04f | 38 | int32_t SpwfSAInterface::init(void) |
mridup | 0:dc55f40eb04f | 39 | { |
mridup | 0:dc55f40eb04f | 40 | _spwf.setTimeout(SPWF_MISC_TIMEOUT); |
mridup | 0:dc55f40eb04f | 41 | return (_spwf.init()); |
mridup | 0:dc55f40eb04f | 42 | } |
mridup | 0:dc55f40eb04f | 43 | |
mridup | 0:dc55f40eb04f | 44 | int32_t SpwfSAInterface::connect( |
mridup | 0:dc55f40eb04f | 45 | const char *ap, |
mridup | 0:dc55f40eb04f | 46 | const char *pass_phrase, |
mridup | 0:dc55f40eb04f | 47 | ns_security_t security) |
mridup | 0:dc55f40eb04f | 48 | { |
mridup | 3:fd9d20c4d3f0 | 49 | //initialize the device before connecting |
mridup | 3:fd9d20c4d3f0 | 50 | if(!isInitialized) |
mridup | 3:fd9d20c4d3f0 | 51 | { |
mridup | 3:fd9d20c4d3f0 | 52 | if(init()==0) |
mridup | 3:fd9d20c4d3f0 | 53 | isInitialized=true; |
mridup | 3:fd9d20c4d3f0 | 54 | else return NS_ERROR_DEVICE_ERROR; |
mridup | 3:fd9d20c4d3f0 | 55 | } |
mridup | 3:fd9d20c4d3f0 | 56 | |
mridup | 0:dc55f40eb04f | 57 | _spwf.setTimeout(SPWF_CONNECT_TIMEOUT); |
mridup | 0:dc55f40eb04f | 58 | |
mridup | 0:dc55f40eb04f | 59 | if(security == NS_SECURITY_WPA2) return NS_ERROR_DEVICE_ERROR; |
mridup | 0:dc55f40eb04f | 60 | |
mridup | 0:dc55f40eb04f | 61 | WiFi_Priv_Mode mode = (WiFi_Priv_Mode)security; |
mridup | 0:dc55f40eb04f | 62 | |
mridup | 0:dc55f40eb04f | 63 | return (_spwf.connect((char*)ap, (char*)pass_phrase, mode));//0 on success |
mridup | 0:dc55f40eb04f | 64 | } |
mridup | 0:dc55f40eb04f | 65 | |
mridup | 0:dc55f40eb04f | 66 | int32_t SpwfSAInterface::disconnect() |
mridup | 0:dc55f40eb04f | 67 | { |
mridup | 3:fd9d20c4d3f0 | 68 | if(!isConnected()) return NS_ERROR_NO_CONNECTION; |
mridup | 0:dc55f40eb04f | 69 | |
mridup | 0:dc55f40eb04f | 70 | return (_spwf.disconnect()); |
mridup | 0:dc55f40eb04f | 71 | } |
mridup | 0:dc55f40eb04f | 72 | |
mridup | 0:dc55f40eb04f | 73 | const char *SpwfSAInterface::getIPAddress() |
mridup | 0:dc55f40eb04f | 74 | { |
mridup | 0:dc55f40eb04f | 75 | return _spwf.getIPAddress(); |
mridup | 0:dc55f40eb04f | 76 | } |
mridup | 0:dc55f40eb04f | 77 | |
mridup | 0:dc55f40eb04f | 78 | const char *SpwfSAInterface::getMACAddress() |
mridup | 0:dc55f40eb04f | 79 | { |
mridup | 0:dc55f40eb04f | 80 | return _spwf.getMACAddress(); |
mridup | 0:dc55f40eb04f | 81 | } |
mridup | 0:dc55f40eb04f | 82 | |
mridup | 0:dc55f40eb04f | 83 | void SpwfSAInterface::setid(bool set, int id) |
mridup | 0:dc55f40eb04f | 84 | { |
mridup | 0:dc55f40eb04f | 85 | if(set) |
mridup | 0:dc55f40eb04f | 86 | _ids[id] = true; |
mridup | 0:dc55f40eb04f | 87 | else |
mridup | 0:dc55f40eb04f | 88 | _ids[id] = false; |
mridup | 0:dc55f40eb04f | 89 | } |
mridup | 0:dc55f40eb04f | 90 | |
mridup | 0:dc55f40eb04f | 91 | SocketInterface *SpwfSAInterface::createSocket(ns_protocol_t proto) |
mridup | 0:dc55f40eb04f | 92 | { |
mridup | 0:dc55f40eb04f | 93 | return new SpwfSASocket(this, &_spwf, proto); |
mridup | 0:dc55f40eb04f | 94 | } |
mridup | 0:dc55f40eb04f | 95 | |
mridup | 0:dc55f40eb04f | 96 | void SpwfSAInterface::destroySocket(SocketInterface *iface) |
mridup | 0:dc55f40eb04f | 97 | { |
mridup | 0:dc55f40eb04f | 98 | SpwfSASocket *socket = (SpwfSASocket *)iface; |
mridup | 0:dc55f40eb04f | 99 | _ids[socket->_id] = false; |
mridup | 0:dc55f40eb04f | 100 | delete socket; |
mridup | 0:dc55f40eb04f | 101 | } |
mridup | 0:dc55f40eb04f | 102 | |
mridup | 0:dc55f40eb04f | 103 | |
mridup | 0:dc55f40eb04f | 104 | // SpwfSASocket implementation |
mridup | 0:dc55f40eb04f | 105 | int32_t SpwfSAInterface::SpwfSASocket::open(const char *ip, uint16_t port) |
mridup | 0:dc55f40eb04f | 106 | { |
mridup | 1:bd9db471d47d | 107 | uint8_t sock_id = 99; |
mridup | 0:dc55f40eb04f | 108 | |
mridup | 0:dc55f40eb04f | 109 | const char *proto = (_proto == NS_UDP) ? "u" : "t";//"s" for secure socket? |
mridup | 0:dc55f40eb04f | 110 | |
mridup | 0:dc55f40eb04f | 111 | __spwf->socket_client_open((uint8_t*)ip, (uint32_t)port, (uint8_t *)proto, &sock_id); |
mridup | 0:dc55f40eb04f | 112 | |
mridup | 0:dc55f40eb04f | 113 | //TODO: Maintain a socket table to map socket ID to host & port |
mridup | 0:dc55f40eb04f | 114 | //TODO: lookup on client table to see if already socket is allocated to same host/port |
mridup | 0:dc55f40eb04f | 115 | //multimap <char *, vector <uint16_t> > ::iterator i = c_table.find((char*)ip); |
mridup | 0:dc55f40eb04f | 116 | |
mridup | 0:dc55f40eb04f | 117 | if(sock_id <= SPWFSA_SOCKET_COUNT) |
mridup | 0:dc55f40eb04f | 118 | { |
mridup | 0:dc55f40eb04f | 119 | _id = sock_id;//the socket ID of this Socket instance |
mridup | 0:dc55f40eb04f | 120 | _itf->setid(true, _id); |
mridup | 0:dc55f40eb04f | 121 | //_itf->c_table.insert(pair <char *, vector <uint16_t> > ((char*)ip, port)); |
mridup | 0:dc55f40eb04f | 122 | } |
mridup | 0:dc55f40eb04f | 123 | else |
mridup | 0:dc55f40eb04f | 124 | return NS_ERROR_NO_SOCKET; |
mridup | 0:dc55f40eb04f | 125 | |
mridup | 0:dc55f40eb04f | 126 | return 0;//0 means SUCCESS |
mridup | 0:dc55f40eb04f | 127 | } |
mridup | 0:dc55f40eb04f | 128 | |
mridup | 0:dc55f40eb04f | 129 | int32_t SpwfSAInterface::SpwfSASocket::close() |
mridup | 0:dc55f40eb04f | 130 | { |
mridup | 0:dc55f40eb04f | 131 | int32_t err; |
mridup | 0:dc55f40eb04f | 132 | |
mridup | 0:dc55f40eb04f | 133 | _itf->setid(false, _id); |
mridup | 0:dc55f40eb04f | 134 | //_itf->c_table.empty(); |
mridup | 0:dc55f40eb04f | 135 | |
mridup | 0:dc55f40eb04f | 136 | err = __spwf->socket_client_close((uint8_t)_id); |
mridup | 0:dc55f40eb04f | 137 | |
mridup | 0:dc55f40eb04f | 138 | return err; |
mridup | 0:dc55f40eb04f | 139 | } |
mridup | 0:dc55f40eb04f | 140 | |
mridup | 0:dc55f40eb04f | 141 | int32_t SpwfSAInterface::SpwfSASocket::send(const void *data, uint32_t size) |
mridup | 0:dc55f40eb04f | 142 | { |
mridup | 0:dc55f40eb04f | 143 | int32_t err; |
mridup | 0:dc55f40eb04f | 144 | |
mridup | 0:dc55f40eb04f | 145 | err = __spwf->socket_client_write((uint8_t)_id, (uint16_t)size, (char*)data); |
mridup | 0:dc55f40eb04f | 146 | |
mridup | 0:dc55f40eb04f | 147 | return err; |
mridup | 0:dc55f40eb04f | 148 | } |
mridup | 0:dc55f40eb04f | 149 | |
mridup | 0:dc55f40eb04f | 150 | //return no of bytes read |
mridup | 0:dc55f40eb04f | 151 | int32_t SpwfSAInterface::SpwfSASocket::recv(void *data, uint32_t size) |
mridup | 0:dc55f40eb04f | 152 | { |
mridup | 0:dc55f40eb04f | 153 | int32_t recv; |
mridup | 0:dc55f40eb04f | 154 | |
mridup | 0:dc55f40eb04f | 155 | __spwf->setTimeout(SPWF_RECV_TIMEOUT); |
mridup | 0:dc55f40eb04f | 156 | |
mridup | 4:d7d25616f1f7 | 157 | recv = __spwf->socket_client_recv((uint8_t)_id, (uint16_t)size, (char*)data); |
mridup | 0:dc55f40eb04f | 158 | |
mridup | 0:dc55f40eb04f | 159 | return recv; |
mridup | 0:dc55f40eb04f | 160 | |
mridup | 0:dc55f40eb04f | 161 | } |
mridup | 0:dc55f40eb04f | 162 | |
mridup | 0:dc55f40eb04f | 163 | void SpwfSAInterface::debug(const char * string) |
mridup | 0:dc55f40eb04f | 164 | { |
mridup | 0:dc55f40eb04f | 165 | _spwf.debug_print(string); |
mridup | 0:dc55f40eb04f | 166 | } |