iowfehu;gdbjwHJAOPIHO?L
Fork of X_NUCLEO_IDW01M1 by
Revision 5:c83ffd44f40a, committed 2016-05-04
- Comitter:
- mridup
- Date:
- Wed May 04 12:16:20 2016 +0000
- Parent:
- 4:d7d25616f1f7
- Child:
- 6:e7a3fca2df10
- Commit message:
- changes adapting to NetworkStack v119
Changed in this revision
--- 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);
--- a/SPWFInterface.h Tue Apr 19 07:42:22 2016 +0000 +++ b/SPWFInterface.h Wed May 04 12:16:20 2016 +0000 @@ -25,9 +25,9 @@ #define SPWFSA_SOCKET_COUNT 8 /** SpwfSAInterface class - * Implementation of the NetworkInterface for the SPWF Device + * Implementation of the NetworkStack for the SPWF Device */ -class SpwfSAInterface : public WiFiInterface +class SpwfSAInterface : public NetworkStack, public WiFiInterface { public: @@ -35,62 +35,41 @@ virtual ~SpwfSAInterface(); // Implementation of WiFiInterface - virtual int32_t connect( + virtual int connect( const char *ssid, const char *pass, - ns_security_t security = NS_SECURITY_NONE); - - virtual int32_t disconnect(); - - // Implementation of NetworkInterface - virtual const char *getIPAddress(); - virtual const char *getMACAddress(); + nsapi_security_t security = NSAPI_SECURITY_NONE); - virtual SocketInterface *createSocket(ns_protocol_t proto); - virtual void destroySocket(SocketInterface *socket); - + virtual int disconnect(); + virtual const char *get_mac_address(); void debug(const char * string); - void setid(bool set, int id); + + //Implementation of NetworkStack + virtual const char *get_ip_address(); + +protected: + //Implementation of NetworkStack + virtual int socket_open(void **handle, nsapi_protocol_t proto); + virtual int socket_close(void *handle); + virtual int socket_bind(void *handle, const SocketAddress &address); //not supported + virtual int socket_listen(void *handle, int backlog); //not supported + virtual int socket_connect(void *handle, const SocketAddress &address); + virtual int socket_accept(void **handle, void *server); //not supported + virtual int socket_send(void *handle, const void *data, unsigned size); + virtual int socket_recv(void *handle, void *data, unsigned size); + virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); + virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); + virtual void socket_attach(void *handle, void (*callback)(void *), void *data); + +private: + int init(void); -private: - SpwfSADevice _spwf; bool _ids[SPWFSA_SOCKET_COUNT]; bool isInitialized; - multimap <char *, vector <uint16_t> > c_table; - - int32_t init(void); - - // Implementation of the SocketInterface for the SpwfSA - class SpwfSASocket : public SocketInterface - { - public: + multimap <char *, vector <uint16_t> > c_table; - // SpwfSA specific details - SpwfSADevice *__spwf; - SpwfSAInterface *_itf; - ns_protocol_t _proto; - int _id; - - SpwfSASocket(SpwfSAInterface *itf, SpwfSADevice *spwf, ns_protocol_t proto) - : __spwf(spwf), _itf(itf), _proto(proto) {} - - virtual ~SpwfSASocket() { - _itf = 0; - __spwf = 0; - } - - // Implementation of SocketInterface - virtual int32_t open(const char *ip, uint16_t port); - virtual int32_t close(); - - virtual int32_t send(const void *data, uint32_t size); - virtual int32_t recv(void *data, uint32_t size); - }; - }; -/*Function to export singleton instance*/ -SpwfSAInterface *createSPWFInstance(void); #endif
--- a/Spwf/wifi_interface.c Tue Apr 19 07:42:22 2016 +0000 +++ b/Spwf/wifi_interface.c Wed May 04 12:16:20 2016 +0000 @@ -80,6 +80,7 @@ extern volatile Wifi_Status_Var status_flag; extern char UserDataBuff[MAX_BUFFER_GLOBAL]; +extern buffer_e event_buff; extern wifi_scan *wifi_scanned_list;//[15]; extern char print_msg_buff[MAX_BUFFER_GLOBAL]; extern uint8_t user_scan_number; @@ -1233,6 +1234,10 @@ { WiFi_Status_t status = WiFi_MODULE_SUCCESS; +//Wait till all queued events are executed +while(!event_empty(&event_buff)); +Wait_For_Sock_Read_To_Complete(); + /* Set wifi_mode to idle*/ status = SET_Configuration_Value(WIFI_MODE, WiFi_IDLE_MODE);
--- a/Spwf/wifi_module.c Tue Apr 19 07:42:22 2016 +0000 +++ b/Spwf/wifi_module.c Wed May 04 12:16:20 2016 +0000 @@ -484,41 +484,49 @@ break; case WIFI_CLIENT_SOCKET_OPEN_EVENT: - Reset_AT_CMD_Buffer(); - - /* AT+S.SOCKON = myserver,1234,t <cr> */ - sprintf((char*)WiFi_AT_Cmd_Buff,AT_SOCKET_OPEN,curr_hostname,(int)curr_port_number,curr_protocol); - status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); - if(status != WiFi_MODULE_SUCCESS) + + if(!open_sockets[event_pop_s->socket_id])//make sure socket is closed { - #if DEBUG_PRINT - printf("\r\n ERROR During Socket Open \r\n"); - #endif - status_flag.AT_Response_Received = WIFI_TRUE; - AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; + Reset_AT_CMD_Buffer(); + + /* AT+S.SOCKON = myserver,1234,t <cr> */ + sprintf((char*)WiFi_AT_Cmd_Buff,AT_SOCKET_OPEN,curr_hostname,(int)curr_port_number,curr_protocol); + status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); + if(status != WiFi_MODULE_SUCCESS) + { + #if DEBUG_PRINT + printf("\r\n ERROR During Socket Open \r\n"); + #endif + status_flag.AT_Response_Received = WIFI_TRUE; + AT_RESPONSE = WiFi_AT_CMD_RESP_ERROR; + } } break; case WIFI_CLIENT_SOCKET_CLOSE_EVENT: - Reset_AT_CMD_Buffer(); - - /* AT+S.SOCKC=00<cr> */ - sprintf((char*)WiFi_AT_Cmd_Buff,AT_SOCKET_CLOSE,event_pop_s->socket_id); - status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); - if(status == WiFi_MODULE_SUCCESS) + + if(open_sockets[event_pop_s->socket_id])//make sure socket is open { - AT_RESPONSE = WiFi_MODULE_SUCCESS; - status_flag.prevent_push_OK_event = WIFI_TRUE; //prevent the OK received after socket close command to be Q'ed - status_flag.stop_event_dequeue = WIFI_TRUE; - remote_socket_closed_id = event_pop_s->socket_id; - status_flag.client_socket_close_ongoing = WIFI_TRUE; //used for making changes in the value of open_sockets[sock_id] if no error is returned -// status_flag.SockON_Server_Closed_Callback = WIFI_TRUE; - } - else - { - #if DEBUG_PRINT - printf("\r\n ERROR During Socket Close \r\n"); - #endif + Reset_AT_CMD_Buffer(); + + /* AT+S.SOCKC=00<cr> */ + sprintf((char*)WiFi_AT_Cmd_Buff,AT_SOCKET_CLOSE,event_pop_s->socket_id); + status = USART_Transmit_AT_Cmd(strlen((char*)WiFi_AT_Cmd_Buff)); + if(status == WiFi_MODULE_SUCCESS) + { + AT_RESPONSE = WiFi_MODULE_SUCCESS; + status_flag.prevent_push_OK_event = WIFI_TRUE; //prevent the OK received after socket close command to be Q'ed + status_flag.stop_event_dequeue = WIFI_TRUE; + remote_socket_closed_id = event_pop_s->socket_id; + status_flag.client_socket_close_ongoing = WIFI_TRUE; //used for making changes in the value of open_sockets[sock_id] if no error is returned + // status_flag.SockON_Server_Closed_Callback = WIFI_TRUE; + } + else + { + #if DEBUG_PRINT + printf("\r\n ERROR During Socket Close \r\n"); + #endif + } } break;
--- a/Spwf_API/SpwfSADevice.cpp Tue Apr 19 07:42:22 2016 +0000 +++ b/Spwf_API/SpwfSADevice.cpp Wed May 04 12:16:20 2016 +0000 @@ -67,7 +67,7 @@ //de-constructor } -int32_t SpwfSADevice::init(void) +int SpwfSADevice::init(void) { WiFi_Status_t status = WiFi_MODULE_SUCCESS; Timer timer; @@ -114,7 +114,7 @@ return 0; } -int32_t SpwfSADevice::connect(char * ssid, char * sec_key, WiFi_Priv_Mode priv_mode) +int SpwfSADevice::connect(char * ssid, char * sec_key, WiFi_Priv_Mode priv_mode) { WiFi_Status_t status = WiFi_MODULE_SUCCESS; Timer timer; @@ -138,7 +138,7 @@ return 0; } -int32_t SpwfSADevice::disconnect() +int SpwfSADevice::disconnect() { WiFi_Status_t status = WiFi_MODULE_SUCCESS; @@ -177,7 +177,7 @@ return _mac_buffer; } -int32_t SpwfSADevice::socket_client_open(uint8_t * hostname, uint32_t port_number, uint8_t * protocol, uint8_t * sock_id) +int SpwfSADevice::socket_client_open(uint8_t * hostname, uint32_t port_number, uint8_t * protocol, uint8_t * sock_id) { WiFi_Status_t status = WiFi_MODULE_SUCCESS; @@ -193,7 +193,7 @@ } -int32_t SpwfSADevice::socket_client_write(uint8_t sock_id, uint16_t DataLength,char * pData) +int SpwfSADevice::socket_client_write(uint8_t sock_id, uint16_t DataLength,char * pData) { WiFi_Status_t status = WiFi_MODULE_SUCCESS; @@ -208,10 +208,10 @@ } -int32_t SpwfSADevice::socket_client_recv(uint8_t sock_id, uint16_t RecvLength,char * pData) +int SpwfSADevice::socket_client_recv(uint8_t sock_id, uint16_t RecvLength,char * pData) { Timer timer; - + //char debug_str[10]; timer.start(); bytes_to_read = RecvLength; @@ -220,13 +220,15 @@ sync_wait_signal = false; recv_buff = (uint8_t*)pData; __enable_irq(); - + while(!sync_wait_signal) { if (timer.read_ms() > _timeout) { //debug_print("\r\n SpwfSADevice:: Timeout!\r\n"); sync_wait_signal = true; //if(bytes_read==0) return -1;//return error if no bytes are read! + //sprintf((char*)debug_str,"bytes_read: %d\r\n",bytes_read); + //debug_print(debug_str); return bytes_read;//return amount of data arrived so far //when do we return NS_ERROR_WOULD_BLOCK?? } @@ -275,6 +277,8 @@ char debug_str[10]; //Data will be copied or returned to user only if there is a pending request //Copy data to pData + //sprintf((char*)debug_str,"sock_id: %d, size: %d\r\n",socket_id, message_size); + if(recv_buff && !sync_wait_signal) { if((bytes_read + message_size)<= bytes_to_read) @@ -322,7 +326,7 @@ } } -int32_t SpwfSADevice::socket_client_close(uint8_t sock_close_id) +int SpwfSADevice::socket_client_close(uint8_t sock_close_id) { WiFi_Status_t status = WiFi_MODULE_SUCCESS;
--- a/Spwf_API/SpwfSADevice.h Tue Apr 19 07:42:22 2016 +0000 +++ b/Spwf_API/SpwfSADevice.h Wed May 04 12:16:20 2016 +0000 @@ -52,17 +52,17 @@ SpwfSADevice(PinName tx, PinName rx, PinName rst, PinName wkup, PinName rts); ~SpwfSADevice(void); - int32_t init(void); - int32_t connect(char * ssid, char * sec_key, WiFi_Priv_Mode priv_mode); - int32_t disconnect(void); + int init(void); + int connect(char * ssid, char * sec_key, WiFi_Priv_Mode priv_mode); + int disconnect(void); const char *getIPAddress(); const char *getMACAddress(); void network_scan(wifi_scan *scan_result, uint16_t max_scan_number); - int32_t socket_client_open(uint8_t * hostname, uint32_t port_number, uint8_t * protocol, uint8_t * sock_id); - int32_t socket_client_write(uint8_t sock_id, uint16_t DataLength,char * pData); - int32_t socket_client_recv(uint8_t sock_id, uint16_t RecvLength,char * pData); - int32_t socket_client_close(uint8_t sock_close_id); + int socket_client_open(uint8_t * hostname, uint32_t port_number, uint8_t * protocol, uint8_t * sock_id); + int socket_client_write(uint8_t sock_id, uint16_t DataLength,char * pData); + int socket_client_recv(uint8_t sock_id, uint16_t RecvLength,char * pData); + int socket_client_close(uint8_t sock_close_id); void socket_client_security(uint8_t* tls_mode, uint8_t* root_ca_server, uint8_t* client_cert, uint8_t* client_key, uint8_t* client_domain, uint32_t tls_epoch_time); void socket_server_open(uint32_t port_number, uint8_t * protocol);
--- a/Spwf_API/utils/ItfTranslator.cpp Tue Apr 19 07:42:22 2016 +0000 +++ b/Spwf_API/utils/ItfTranslator.cpp Wed May 04 12:16:20 2016 +0000 @@ -140,7 +140,8 @@ void ind_wifi_socket_client_remote_server_closed(uint8_t * socket_closed_id) { - device->signal_synch_wait(WiFi_MODULE_SUCCESS); + //device->debug_print("\r\nsocket closed!\r\n"); + //device->signal_synch_wait(WiFi_MODULE_SUCCESS); } #ifdef __cplusplus