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
Diff: SPWFInterface.cpp
- Revision:
- 5:c83ffd44f40a
- Parent:
- 4:d7d25616f1f7
- Child:
- 6:e7a3fca2df10
--- a/SPWFInterface.cpp Tue Apr 19 07:42:22 2016 +0000 +++ b/SPWFInterface.cpp Wed May 04 12:16:20 2016 +0000 @@ -19,9 +19,15 @@ // Various timeouts for different SPWF operations #define SPWF_CONNECT_TIMEOUT 20000 #define SPWF_SEND_TIMEOUT 500 -#define SPWF_RECV_TIMEOUT 10000 +#define SPWF_RECV_TIMEOUT 2000 #define SPWF_MISC_TIMEOUT 15000 - + +// Handle structure for socket +struct spwf_socket { + int id; + nsapi_protocol_t proto; + bool connected; +}; // SpwfSAInterface implementation SpwfSAInterface::SpwfSAInterface(PinName tx, PinName rx, PinName rst, PinName wkup, PinName rts, bool debug) @@ -35,80 +41,75 @@ { } -int32_t SpwfSAInterface::init(void) +int SpwfSAInterface::init(void) { _spwf.setTimeout(SPWF_MISC_TIMEOUT); return (_spwf.init()); } -int32_t SpwfSAInterface::connect( +int SpwfSAInterface::connect( const char *ap, const char *pass_phrase, - ns_security_t security) + nsapi_security_t security) { //initialize the device before connecting if(!isInitialized) { if(init()==0) isInitialized=true; - else return NS_ERROR_DEVICE_ERROR; + else return NSAPI_ERROR_DEVICE_ERROR; } _spwf.setTimeout(SPWF_CONNECT_TIMEOUT); - if(security == NS_SECURITY_WPA2) return NS_ERROR_DEVICE_ERROR; + if(security == NSAPI_SECURITY_WPA2) return NSAPI_ERROR_DEVICE_ERROR; WiFi_Priv_Mode mode = (WiFi_Priv_Mode)security; return (_spwf.connect((char*)ap, (char*)pass_phrase, mode));//0 on success } -int32_t SpwfSAInterface::disconnect() -{ - if(!isConnected()) return NS_ERROR_NO_CONNECTION; - +int SpwfSAInterface::disconnect() +{ return (_spwf.disconnect()); } -const char *SpwfSAInterface::getIPAddress() +const char *SpwfSAInterface::get_ip_address() { return _spwf.getIPAddress(); } -const char *SpwfSAInterface::getMACAddress() +const char *SpwfSAInterface::get_mac_address() { return _spwf.getMACAddress(); } -void SpwfSAInterface::setid(bool set, int id) -{ - if(set) - _ids[id] = true; - else - _ids[id] = false; -} - -SocketInterface *SpwfSAInterface::createSocket(ns_protocol_t proto) -{ - return new SpwfSASocket(this, &_spwf, proto); -} - -void SpwfSAInterface::destroySocket(SocketInterface *iface) + +int SpwfSAInterface::socket_open(void **handle, nsapi_protocol_t proto) { - SpwfSASocket *socket = (SpwfSASocket *)iface; - _ids[socket->_id] = false; - delete socket; + // Look for an unused socket + int id = -1; + + struct spwf_socket *socket = new struct spwf_socket; + if (!socket) { + return NSAPI_ERROR_NO_SOCKET; + } + + socket->id = id; + socket->proto = proto; + socket->connected = false; + *handle = socket; + return 0; } - + +int SpwfSAInterface::socket_connect(void *handle, const SocketAddress &addr) +{ + uint8_t sock_id = 9; + struct spwf_socket *socket = (struct spwf_socket *)handle; + + const char *proto = (socket->proto == NSAPI_UDP) ? "u" : "t";//"s" for secure socket? -// SpwfSASocket implementation -int32_t SpwfSAInterface::SpwfSASocket::open(const char *ip, uint16_t port) -{ - uint8_t sock_id = 99; - - const char *proto = (_proto == NS_UDP) ? "u" : "t";//"s" for secure socket? - - __spwf->socket_client_open((uint8_t*)ip, (uint32_t)port, (uint8_t *)proto, &sock_id); + _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 //TODO: Maintain a socket table to map socket ID to host & port //TODO: lookup on client table to see if already socket is allocated to same host/port @@ -116,50 +117,100 @@ if(sock_id <= SPWFSA_SOCKET_COUNT) { - _id = sock_id;//the socket ID of this Socket instance - _itf->setid(true, _id); - //_itf->c_table.insert(pair <char *, vector <uint16_t> > ((char*)ip, port)); + //_ids[socket->id] = false; + socket->id = sock_id;//the socket ID of this Socket instance + _ids[socket->id] = true; + socket->connected = true; } else - return NS_ERROR_NO_SOCKET; + return NSAPI_ERROR_NO_SOCKET; return 0;//0 means SUCCESS } - -int32_t SpwfSAInterface::SpwfSASocket::close() + +int SpwfSAInterface::socket_bind(void *handle, const SocketAddress &address) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +int SpwfSAInterface::socket_listen(void *handle, int backlog) { - int32_t err; + return NSAPI_ERROR_UNSUPPORTED; +} + +int SpwfSAInterface::socket_accept(void **handle, void *server) +{ + return NSAPI_ERROR_UNSUPPORTED; +} + +int SpwfSAInterface::socket_close(void *handle) +{ + struct spwf_socket *socket = (struct spwf_socket *)handle; + int err = 0; + //_spwf.setTimeout(SPWF_MISC_TIMEOUT); - _itf->setid(false, _id); - //_itf->c_table.empty(); - - err = __spwf->socket_client_close((uint8_t)_id); - + if(socket->id!=-1) + { + if (_spwf.socket_client_close(socket->id)==-1) { + err = NSAPI_ERROR_DEVICE_ERROR; + } + _ids[socket->id] = false; + } + + delete socket; return err; } - -int32_t SpwfSAInterface::SpwfSASocket::send(const void *data, uint32_t size) -{ - int32_t err; + +int SpwfSAInterface::socket_send(void *handle, const void *data, unsigned size) +{ + struct spwf_socket *socket = (struct spwf_socket *)handle; + int err; - err = __spwf->socket_client_write((uint8_t)_id, (uint16_t)size, (char*)data); + err = _spwf.socket_client_write((uint8_t)socket->id, (uint16_t)size, (char*)data); return err; } //return no of bytes read -int32_t SpwfSAInterface::SpwfSASocket::recv(void *data, uint32_t size) +int SpwfSAInterface::socket_recv(void *handle, void *data, unsigned size) { + struct spwf_socket *socket = (struct spwf_socket *)handle; int32_t recv; - __spwf->setTimeout(SPWF_RECV_TIMEOUT); + _spwf.setTimeout(SPWF_RECV_TIMEOUT); - recv = __spwf->socket_client_recv((uint8_t)_id, (uint16_t)size, (char*)data); - + recv = _spwf.socket_client_recv((uint8_t)socket->id, (uint16_t)size, (char*)data); + if (recv < 0) { + return NSAPI_ERROR_WOULD_BLOCK; + } return recv; } +int SpwfSAInterface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size) +{ + struct spwf_socket *socket = (struct spwf_socket *)handle; + if (!socket->connected) { + int err = socket_connect(socket, addr); + if (err < 0) { + return err; + } + } + + return socket_send(socket, data, size); +} + +int SpwfSAInterface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size) +{ + struct spwf_socket *socket = (struct spwf_socket *)handle; + return socket_recv(socket, data, size); +} + +void SpwfSAInterface::socket_attach(void *handle, void (*callback)(void *), void *data) +{ + //No implementation yet +} + void SpwfSAInterface::debug(const char * string) { _spwf.debug_print(string);