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:
Wed Apr 13 11:34:07 2016 +0000
Revision:
0:dc55f40eb04f
Child:
1:bd9db471d47d
First Version of mbed X_NUCLEO_IDW01M1 library

Who changed what in which revision?

UserRevisionLine numberNew 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 * The singleton which represents the SpwfSAInterface.
mridup 0:dc55f40eb04f 27 *
mridup 0:dc55f40eb04f 28 */
mridup 0:dc55f40eb04f 29 static SpwfSAInterface spwf_device(PA_9, PA_10, PC_12, PC_8, PA_12, true);
mridup 0:dc55f40eb04f 30
mridup 0:dc55f40eb04f 31 /**
mridup 0:dc55f40eb04f 32 * Export the instance to the user.
mridup 0:dc55f40eb04f 33 */
mridup 0:dc55f40eb04f 34 SpwfSAInterface *createSPWFInstance(void)
mridup 0:dc55f40eb04f 35 {
mridup 0:dc55f40eb04f 36 return (&spwf_device);
mridup 0:dc55f40eb04f 37 }
mridup 0:dc55f40eb04f 38
mridup 0:dc55f40eb04f 39 // SpwfSAInterface implementation
mridup 0:dc55f40eb04f 40 SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, PinName rst, PinName wkup, PinName rts, bool debug)
mridup 0:dc55f40eb04f 41 : _spwf(tx, rx, rst, wkup, rts)
mridup 0:dc55f40eb04f 42 {
mridup 0:dc55f40eb04f 43 memset(_ids, 0, sizeof(_ids));
mridup 0:dc55f40eb04f 44 }
mridup 0:dc55f40eb04f 45
mridup 0:dc55f40eb04f 46 SpwfSAInterface::~SpwfSAInterface()
mridup 0:dc55f40eb04f 47 {
mridup 0:dc55f40eb04f 48 }
mridup 0:dc55f40eb04f 49
mridup 0:dc55f40eb04f 50 int32_t SpwfSAInterface::init(void)
mridup 0:dc55f40eb04f 51 {
mridup 0:dc55f40eb04f 52 _spwf.setTimeout(SPWF_MISC_TIMEOUT);
mridup 0:dc55f40eb04f 53 return (_spwf.init());
mridup 0:dc55f40eb04f 54 }
mridup 0:dc55f40eb04f 55
mridup 0:dc55f40eb04f 56 int32_t SpwfSAInterface::connect(
mridup 0:dc55f40eb04f 57 const char *ap,
mridup 0:dc55f40eb04f 58 const char *pass_phrase,
mridup 0:dc55f40eb04f 59 ns_security_t security)
mridup 0:dc55f40eb04f 60 {
mridup 0:dc55f40eb04f 61 _spwf.setTimeout(SPWF_CONNECT_TIMEOUT);
mridup 0:dc55f40eb04f 62
mridup 0:dc55f40eb04f 63 if(security == NS_SECURITY_WPA2) return NS_ERROR_DEVICE_ERROR;
mridup 0:dc55f40eb04f 64
mridup 0:dc55f40eb04f 65 WiFi_Priv_Mode mode = (WiFi_Priv_Mode)security;
mridup 0:dc55f40eb04f 66
mridup 0:dc55f40eb04f 67 return (_spwf.connect((char*)ap, (char*)pass_phrase, mode));//0 on success
mridup 0:dc55f40eb04f 68 }
mridup 0:dc55f40eb04f 69
mridup 0:dc55f40eb04f 70 int32_t SpwfSAInterface::disconnect()
mridup 0:dc55f40eb04f 71 {
mridup 0:dc55f40eb04f 72 //_spwf.setTimeout(SPWF_SEND_TIMEOUT);
mridup 0:dc55f40eb04f 73
mridup 0:dc55f40eb04f 74 return (_spwf.disconnect());
mridup 0:dc55f40eb04f 75 }
mridup 0:dc55f40eb04f 76
mridup 0:dc55f40eb04f 77 const char *SpwfSAInterface::getIPAddress()
mridup 0:dc55f40eb04f 78 {
mridup 0:dc55f40eb04f 79 return _spwf.getIPAddress();
mridup 0:dc55f40eb04f 80 }
mridup 0:dc55f40eb04f 81
mridup 0:dc55f40eb04f 82 const char *SpwfSAInterface::getMACAddress()
mridup 0:dc55f40eb04f 83 {
mridup 0:dc55f40eb04f 84 return _spwf.getMACAddress();
mridup 0:dc55f40eb04f 85 }
mridup 0:dc55f40eb04f 86
mridup 0:dc55f40eb04f 87 void SpwfSAInterface::setid(bool set, int id)
mridup 0:dc55f40eb04f 88 {
mridup 0:dc55f40eb04f 89 if(set)
mridup 0:dc55f40eb04f 90 _ids[id] = true;
mridup 0:dc55f40eb04f 91 else
mridup 0:dc55f40eb04f 92 _ids[id] = false;
mridup 0:dc55f40eb04f 93 }
mridup 0:dc55f40eb04f 94
mridup 0:dc55f40eb04f 95 SocketInterface *SpwfSAInterface::createSocket(ns_protocol_t proto)
mridup 0:dc55f40eb04f 96 {
mridup 0:dc55f40eb04f 97 return new SpwfSASocket(this, &_spwf, proto);
mridup 0:dc55f40eb04f 98 }
mridup 0:dc55f40eb04f 99
mridup 0:dc55f40eb04f 100 void SpwfSAInterface::destroySocket(SocketInterface *iface)
mridup 0:dc55f40eb04f 101 {
mridup 0:dc55f40eb04f 102 SpwfSASocket *socket = (SpwfSASocket *)iface;
mridup 0:dc55f40eb04f 103 _ids[socket->_id] = false;
mridup 0:dc55f40eb04f 104 delete socket;
mridup 0:dc55f40eb04f 105 }
mridup 0:dc55f40eb04f 106
mridup 0:dc55f40eb04f 107
mridup 0:dc55f40eb04f 108 // SpwfSASocket implementation
mridup 0:dc55f40eb04f 109 int32_t SpwfSAInterface::SpwfSASocket::open(const char *ip, uint16_t port)
mridup 0:dc55f40eb04f 110 {
mridup 0:dc55f40eb04f 111 uint8_t sock_id = -1;
mridup 0:dc55f40eb04f 112
mridup 0:dc55f40eb04f 113 //__spwf->setTimeout(SPWF_SEND_TIMEOUT);
mridup 0:dc55f40eb04f 114
mridup 0:dc55f40eb04f 115 const char *proto = (_proto == NS_UDP) ? "u" : "t";//"s" for secure socket?
mridup 0:dc55f40eb04f 116
mridup 0:dc55f40eb04f 117 __spwf->socket_client_open((uint8_t*)ip, (uint32_t)port, (uint8_t *)proto, &sock_id);
mridup 0:dc55f40eb04f 118
mridup 0:dc55f40eb04f 119 //TODO: Maintain a socket table to map socket ID to host & port
mridup 0:dc55f40eb04f 120 //TODO: lookup on client table to see if already socket is allocated to same host/port
mridup 0:dc55f40eb04f 121 //multimap <char *, vector <uint16_t> > ::iterator i = c_table.find((char*)ip);
mridup 0:dc55f40eb04f 122
mridup 0:dc55f40eb04f 123 if(sock_id <= SPWFSA_SOCKET_COUNT)
mridup 0:dc55f40eb04f 124 {
mridup 0:dc55f40eb04f 125 _id = sock_id;//the socket ID of this Socket instance
mridup 0:dc55f40eb04f 126 _itf->setid(true, _id);
mridup 0:dc55f40eb04f 127 //_itf->c_table.insert(pair <char *, vector <uint16_t> > ((char*)ip, port));
mridup 0:dc55f40eb04f 128 }
mridup 0:dc55f40eb04f 129 else
mridup 0:dc55f40eb04f 130 return NS_ERROR_NO_SOCKET;
mridup 0:dc55f40eb04f 131
mridup 0:dc55f40eb04f 132 return 0;//0 means SUCCESS
mridup 0:dc55f40eb04f 133 }
mridup 0:dc55f40eb04f 134
mridup 0:dc55f40eb04f 135 int32_t SpwfSAInterface::SpwfSASocket::close()
mridup 0:dc55f40eb04f 136 {
mridup 0:dc55f40eb04f 137 int32_t err;
mridup 0:dc55f40eb04f 138
mridup 0:dc55f40eb04f 139 //__spwf->setTimeout(SPWF_SEND_TIMEOUT);
mridup 0:dc55f40eb04f 140
mridup 0:dc55f40eb04f 141 _itf->setid(false, _id);
mridup 0:dc55f40eb04f 142 //_itf->c_table.empty();
mridup 0:dc55f40eb04f 143
mridup 0:dc55f40eb04f 144 err = __spwf->socket_client_close((uint8_t)_id);
mridup 0:dc55f40eb04f 145
mridup 0:dc55f40eb04f 146 return err;
mridup 0:dc55f40eb04f 147 }
mridup 0:dc55f40eb04f 148
mridup 0:dc55f40eb04f 149 int32_t SpwfSAInterface::SpwfSASocket::send(const void *data, uint32_t size)
mridup 0:dc55f40eb04f 150 {
mridup 0:dc55f40eb04f 151 int32_t err;
mridup 0:dc55f40eb04f 152
mridup 0:dc55f40eb04f 153 //__spwf->setTimeout(SPWF_SEND_TIMEOUT);
mridup 0:dc55f40eb04f 154
mridup 0:dc55f40eb04f 155 err = __spwf->socket_client_write((uint8_t)_id, (uint16_t)size, (char*)data);
mridup 0:dc55f40eb04f 156
mridup 0:dc55f40eb04f 157 return err;
mridup 0:dc55f40eb04f 158 }
mridup 0:dc55f40eb04f 159
mridup 0:dc55f40eb04f 160 //return no of bytes read
mridup 0:dc55f40eb04f 161 int32_t SpwfSAInterface::SpwfSASocket::recv(void *data, uint32_t size)
mridup 0:dc55f40eb04f 162 {
mridup 0:dc55f40eb04f 163 int32_t recv;
mridup 0:dc55f40eb04f 164
mridup 0:dc55f40eb04f 165 __spwf->setTimeout(SPWF_RECV_TIMEOUT);
mridup 0:dc55f40eb04f 166
mridup 0:dc55f40eb04f 167 recv = __spwf->socket_client_recv((uint8_t)_id, (uint16_t)size, (char*)data);
mridup 0:dc55f40eb04f 168
mridup 0:dc55f40eb04f 169
mridup 0:dc55f40eb04f 170 return recv;
mridup 0:dc55f40eb04f 171
mridup 0:dc55f40eb04f 172 }
mridup 0:dc55f40eb04f 173
mridup 0:dc55f40eb04f 174 void SpwfSAInterface::debug(const char * string)
mridup 0:dc55f40eb04f 175 {
mridup 0:dc55f40eb04f 176 _spwf.debug_print(string);
mridup 0:dc55f40eb04f 177 }