iowfehu;gdbjwHJAOPIHO?L

Fork of X_NUCLEO_IDW01M1 by ST

Files at this revision

API Documentation at this revision

Comitter:
mridup
Date:
Mon May 09 10:38:22 2016 +0000
Parent:
5:c83ffd44f40a
Child:
7:0fdd186a7d90
Commit message:
Socket Server support.

Changed in this revision

SPWFInterface.cpp Show annotated file Show diff for this revision Revisions of this file
SPWFInterface.h Show annotated file Show diff for this revision Revisions of this file
Spwf/inc/wifi_driver.h Show annotated file Show diff for this revision Revisions of this file
Spwf/wifi_driver.c Show annotated file Show diff for this revision Revisions of this file
Spwf/wifi_interface.c Show annotated file Show diff for this revision Revisions of this file
Spwf_API/SpwfSADevice.cpp Show annotated file Show diff for this revision Revisions of this file
Spwf_API/SpwfSADevice.h Show annotated file Show diff for this revision Revisions of this file
Spwf_API/utils/ItfTranslator.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SPWFInterface.cpp	Wed May 04 12:16:20 2016 +0000
+++ b/SPWFInterface.cpp	Mon May 09 10:38:22 2016 +0000
@@ -25,6 +25,7 @@
 // Handle structure for socket
 struct spwf_socket {
     int id;
+    int server_port;
     nsapi_protocol_t proto;
     bool connected;
 };
@@ -35,6 +36,7 @@
 {
     memset(_ids, 0, sizeof(_ids));
     isInitialized = false;
+    isListening = false;
 }
  
 SpwfSAInterface::~SpwfSAInterface()
@@ -96,6 +98,7 @@
     }
 
     socket->id = id;
+    socket->server_port = id;
     socket->proto = proto;
     socket->connected = false;
     *handle = socket;
@@ -104,7 +107,7 @@
 
 int SpwfSAInterface::socket_connect(void *handle, const SocketAddress &addr)
 {
-    uint8_t sock_id = 9;
+    uint8_t sock_id = 99;
     struct spwf_socket *socket = (struct spwf_socket *)handle;
     
     const char *proto = (socket->proto == NSAPI_UDP) ? "u" : "t";//"s" for secure socket?
@@ -130,19 +133,106 @@
 
 int SpwfSAInterface::socket_bind(void *handle, const SocketAddress &address)
 {
-    return NSAPI_ERROR_UNSUPPORTED;
+    struct spwf_socket *socket = (struct spwf_socket *)handle;
+    socket->id = SERVER_SOCKET_NO;//Special socket ID number for Server Socket
+    socket->server_port = address.get_port();
+    return 0;
 }
 
 int SpwfSAInterface::socket_listen(void *handle, int backlog)
 {
-    return NSAPI_ERROR_UNSUPPORTED;
+    struct spwf_socket *socket = (struct spwf_socket *)handle;
+    int err;
+    
+    if(socket->server_port==-1 || isListening) 
+        return NSAPI_ERROR_NO_SOCKET; //server socket not bound or already listening
+    
+    const char *proto = (socket->proto == NSAPI_UDP) ? "u" : "t";
+    
+    err = _spwf.socket_server_open((uint32_t)socket->server_port, (uint8_t *)proto);
+    
+    if(err==0)
+    {
+        isListening = true;
+    }
+    else 
+        return NSAPI_ERROR_DEVICE_ERROR;
+        
+    return err;
 }
 
+/** Accepts a connection on a TCP socket
+     *
+     *  The server socket must be bound and set to listen for connections.
+     *  On a new connection, creates a network socket using the specified
+     *  socket instance.
+     *
+     *  By default, accept blocks until data is sent. If socket is set to
+     *  non-blocking or times out, NSAPI_ERROR_WOULD_BLOCK is returned
+     *  immediately.
+     *
+     *  @param socket   TCPSocket instance that will handle the incoming connection.
+     *  @return         0 on success, negative error code on failure
+     */
 int SpwfSAInterface::socket_accept(void **handle, void *server)
 {
-    return NSAPI_ERROR_UNSUPPORTED;
+    char debug_str[10];
+    struct spwf_socket *server_socket = (struct spwf_socket *)server;
+    int id = -1;
+    
+    if(server_socket->server_port==-1 || !isListening) 
+        return NSAPI_ERROR_NO_SOCKET; //server socket not bound or not listening
+    
+    while(true)
+    {        
+        if(_spwf.get_wait_for_incoming_client())
+        {
+            server_socket->connected = true;
+
+            //struct spwf_socket *temp_socket;// = new struct spwf_socket;//create new network socket
+            struct spwf_socket *socket = new struct spwf_socket;//create new network socket
+            if (!socket) {
+                return NSAPI_ERROR_NO_SOCKET;
+            }
+            
+            memset(socket, 0, sizeof (struct spwf_socket));
+            /*
+            if(get_network_socket(&temp_socket)!=0)
+            {
+                return NSAPI_ERROR_NO_SOCKET;
+            }*/
+            
+            socket->id = server_socket->id;
+            socket->server_port = server_socket->server_port;
+            socket->proto = server_socket->proto;
+            socket->connected = true;
+            
+            *handle = socket; 
+            socket = 0;
+            _spwf.set_wait_for_incoming_client(false);//reset
+            wait_ms(50);//CHECK:TODO:Why do we need this?
+            break;
+        }
+    }
+    
+    return 0;
 }
 
+/*
+int SpwfSAInterface::get_network_socket(struct spwf_socket **handle)
+{
+    struct spwf_socket *__socket = new struct spwf_socket;//create new network socket
+    __socket = new struct spwf_socket;//create new network socket
+    if (!__socket) {
+        return NSAPI_ERROR_NO_SOCKET;
+    }
+    
+    memset(__socket, 0, sizeof (struct spwf_socket));
+    *handle = __socket;
+    return 0;
+}
+*/
+
 int SpwfSAInterface::socket_close(void *handle)
 {
     struct spwf_socket *socket = (struct spwf_socket *)handle;
@@ -151,10 +241,20 @@
     
     if(socket->id!=-1)
     {
-        if (_spwf.socket_client_close(socket->id)==-1) {
-            err = NSAPI_ERROR_DEVICE_ERROR;
+        if(socket->id==SERVER_SOCKET_NO)
+        {
+            if (_spwf.socket_server_close()==-1) {
+                err = NSAPI_ERROR_DEVICE_ERROR;
+            }
+            isListening = false;
         }
-        _ids[socket->id] = false;
+        else
+        {
+            if (_spwf.socket_client_close(socket->id)==-1) {
+                err = NSAPI_ERROR_DEVICE_ERROR;
+            }
+            _ids[socket->id] = false;
+        }
     }
 
     delete socket;
@@ -166,7 +266,14 @@
     struct spwf_socket *socket = (struct spwf_socket *)handle;
     int err;
     
-    err = _spwf.socket_client_write((uint8_t)socket->id, (uint16_t)size, (char*)data);
+    if(socket->id==SERVER_SOCKET_NO)
+        {
+            err = _spwf.socket_server_write((uint16_t)size, (char*)data);
+        }
+    else
+        {
+            err = _spwf.socket_client_write((uint8_t)socket->id, (uint16_t)size, (char*)data);
+        }
     
     return err;
 }
@@ -179,6 +286,7 @@
     
     _spwf.setTimeout(SPWF_RECV_TIMEOUT);
     
+    //CHECK:Receive for both Client and Server Sockets same?
     recv = _spwf.socket_client_recv((uint8_t)socket->id, (uint16_t)size, (char*)data);    
     if (recv < 0) {
         return NSAPI_ERROR_WOULD_BLOCK;
--- a/SPWFInterface.h	Wed May 04 12:16:20 2016 +0000
+++ b/SPWFInterface.h	Mon May 09 10:38:22 2016 +0000
@@ -23,6 +23,7 @@
 #include "SpwfSADevice.h"
  
 #define SPWFSA_SOCKET_COUNT 8
+#define SERVER_SOCKET_NO    9
  
 /** SpwfSAInterface class
  *  Implementation of the NetworkStack for the SPWF Device
@@ -52,9 +53,9 @@
     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_listen(void *handle, int backlog);
+    virtual     int socket_connect(void *handle, const SocketAddress &address);
+    virtual     int socket_accept(void **handle, void *server);
     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);    
@@ -62,10 +63,12 @@
     virtual     void socket_attach(void *handle, void (*callback)(void *), void *data);
            
 private:
-    int     init(void); 
+    int     init(void);
+    //int         get_network_socket(struct spwf_socket **handle);
         
     SpwfSADevice _spwf;
     bool _ids[SPWFSA_SOCKET_COUNT];
+    bool isListening;
     bool isInitialized;
     multimap <char *, vector <uint16_t> > c_table;       
     
--- a/Spwf/inc/wifi_driver.h	Wed May 04 12:16:20 2016 +0000
+++ b/Spwf/inc/wifi_driver.h	Mon May 09 10:38:22 2016 +0000
@@ -10,6 +10,8 @@
 extern WiFi_AT_CMD_Response_t WiFi_Module_State;
 extern volatile WiFi_WIND_State_TypeDef WiFi_WIND_State;
 extern volatile uint8_t wifi_connected;
+extern volatile uint32_t WIND64_count;
+extern volatile uint8_t wifi_client_connected;
 extern wifi_bool WiFi_Enabled;
 extern wifi_bool Standby_Enabled;
 extern wifi_bool Deep_Sleep_Enabled;
--- a/Spwf/wifi_driver.c	Wed May 04 12:16:20 2016 +0000
+++ b/Spwf/wifi_driver.c	Mon May 09 10:38:22 2016 +0000
@@ -185,3 +185,74 @@
   return WiFi_MODULE_SUCCESS;
 }
 
+/**
+* @brief  wifi_socket_server_write
+*         Write to a Server socket
+* @param  None
+* @retval WiFi_Status_t : return status of server socket request
+*/
+WiFi_Status_t wifi_socket_server_write(uint16_t DataLength,char * pData) 
+{
+    WiFi_Status_t status = WiFi_MODULE_SUCCESS;
+    /*Can only write if there is a client connected*/
+    if(!wifi_client_connected)
+    {
+    return WiFi_NOT_READY;
+    }
+     __disable_irq();
+    
+    status_flag.do_not_reset_push_WIFI_event = WIFI_TRUE;
+    status_flag.prevent_push_WIFI_event = WIFI_TRUE;
+    __enable_irq();
+    
+    while(status_flag.sock_read_ongoing || WIND64_count!= 0)//wait till any pending data is read
+    {
+        asm("NOP");
+    }
+    
+    wait_for_command_mode();
+    
+    /*to make sure that by default the mode is not switched to command mode from data mode*/
+    status_flag.switch_by_default_to_command_mode = WIFI_FALSE;
+    
+    /*Switch to Data Mode first*/
+    if(!status_flag.data_mode)
+    {
+    WiFi_switch_to_data_mode();//switch by default
+    while(!status_flag.data_mode)
+    {
+      //Wait till data_mode is active
+        asm("NOP");
+    }
+    }  
+    
+    callSpwfSADevice_write(spwf_dev, (const char *) pData, DataLength);
+    
+    /*Write the data on the uart*/
+    /*if(HAL_UART_Transmit(&UartWiFiHandle, (uint8_t *)pData, DataLength,1000)!= HAL_OK)
+    {
+    Error_Handler();
+    return WiFi_HAL_UART_ERROR;
+    }*/
+    //HAL_Delay(100);//Wait for tx before switching back to command mode
+    
+    /*Switch back to Command Mode*/
+    if(!status_flag.command_mode)
+    {
+    WiFi_switch_to_command_mode();//switch by default
+    while(!status_flag.command_mode)
+    {
+      //Wait till command_mode is active
+        asm("NOP");
+    }
+    }
+    
+    status_flag.switch_by_default_to_command_mode = WIFI_TRUE;  /*back to default behaviour*/
+    
+    __disable_irq();
+    status_flag.prevent_push_WIFI_event = WIFI_FALSE;
+    status_flag.do_not_reset_push_WIFI_event = WIFI_FALSE;
+    __enable_irq();
+    
+    return status;
+}
\ No newline at end of file
--- a/Spwf/wifi_interface.c	Wed May 04 12:16:20 2016 +0000
+++ b/Spwf/wifi_interface.c	Mon May 09 10:38:22 2016 +0000
@@ -624,75 +624,7 @@
 return status; 
 }
 
-/**
-* @brief  wifi_socket_server_write
-*         Write to a Server socket
-* @param  None
-* @retval WiFi_Status_t : return status of server socket request
-*/
-WiFi_Status_t wifi_socket_server_write(uint16_t DataLength,char * pData) 
-{
-WiFi_Status_t status = WiFi_MODULE_SUCCESS;
-/*Can only write if there is a client connected*/
-if(!wifi_client_connected)
-{
-return WiFi_NOT_READY;
-}
- __disable_irq();
 
-status_flag.do_not_reset_push_WIFI_event = WIFI_TRUE;
-status_flag.prevent_push_WIFI_event = WIFI_TRUE;
-__enable_irq();
-
-while(status_flag.sock_read_ongoing || WIND64_count!= 0)//wait till any pending data is read
-{
-    asm("NOP");
-}
-
-wait_for_command_mode();
-
-/*to make sure that by default the mode is not switched to command mode from data mode*/
-status_flag.switch_by_default_to_command_mode = WIFI_FALSE;
-
-/*Switch to Data Mode first*/
-if(!status_flag.data_mode)
-{
-WiFi_switch_to_data_mode();//switch by default
-while(!status_flag.data_mode)
-{
-  //Wait till data_mode is active
-    asm("NOP");
-}
-}  
-
-/*Write the data on the uart*/
-/*if(HAL_UART_Transmit(&UartWiFiHandle, (uint8_t *)pData, DataLength,1000)!= HAL_OK)
-{
-Error_Handler();
-return WiFi_HAL_UART_ERROR;
-}*/
-//HAL_Delay(100);//Wait for tx before switching back to command mode
-
-/*Switch back to Command Mode*/
-if(!status_flag.command_mode)
-{
-WiFi_switch_to_command_mode();//switch by default
-while(!status_flag.command_mode)
-{
-  //Wait till command_mode is active
-    asm("NOP");
-}
-}
-
-status_flag.switch_by_default_to_command_mode = WIFI_TRUE;  /*back to default behaviour*/
-
-__disable_irq();
-status_flag.prevent_push_WIFI_event = WIFI_FALSE;
-status_flag.do_not_reset_push_WIFI_event = WIFI_FALSE;
-__enable_irq();
-
-return status;
-}
 
 /**
 * @brief  Server Socket Close
--- a/Spwf_API/SpwfSADevice.cpp	Wed May 04 12:16:20 2016 +0000
+++ b/Spwf_API/SpwfSADevice.cpp	Mon May 09 10:38:22 2016 +0000
@@ -60,6 +60,7 @@
 {
     setSpwfSADevice(this);
     sync_wait_signal = false;
+    wait_for_incoming_client = false;
 }
 
 SpwfSADevice::~SpwfSADevice(void)
@@ -341,6 +342,48 @@
 }
 
 
+int SpwfSADevice::socket_server_open(uint32_t port_number, uint8_t * protocol)
+{
+    WiFi_Status_t status = WiFi_MODULE_SUCCESS;
+
+    status = wifi_socket_server_open(port_number, protocol);
+    //map error to enum ns_error_t 
+    if(status!=WiFi_MODULE_SUCCESS)
+    {
+        return -1;
+    }    
+    
+    return 0;
+}
+
+int SpwfSADevice::socket_server_write(uint16_t data_length,char * pdata)
+{    
+    WiFi_Status_t status = WiFi_MODULE_SUCCESS;
+
+    status = wifi_socket_server_write(data_length, pdata);
+    //map error to enum ns_error_t 
+    if(status!=WiFi_MODULE_SUCCESS)
+    {
+        return -1;
+    }    
+    
+    return 0;
+}
+    
+int SpwfSADevice::socket_server_close(void)
+{    
+    WiFi_Status_t status = WiFi_MODULE_SUCCESS;
+
+    status = wifi_socket_server_close();
+    //map error to enum ns_error_t 
+    if(status!=WiFi_MODULE_SUCCESS)
+    {
+        return -1;
+    }    
+    
+    return 0;
+}
+    
 void SpwfSADevice::spwf_attach_irq(wifi_bool attach) 
 {
     if(attach)
@@ -400,3 +443,13 @@
     term_.puts(string);
 }
 
+void SpwfSADevice::set_wait_for_incoming_client(bool set)
+{
+    wait_for_incoming_client = set;    
+}
+
+bool SpwfSADevice::get_wait_for_incoming_client()
+{
+    return wait_for_incoming_client;
+}
+
--- a/Spwf_API/SpwfSADevice.h	Wed May 04 12:16:20 2016 +0000
+++ b/Spwf_API/SpwfSADevice.h	Mon May 09 10:38:22 2016 +0000
@@ -52,22 +52,22 @@
     SpwfSADevice(PinName tx, PinName rx, PinName rst, PinName wkup, PinName rts);
     ~SpwfSADevice(void);
 
-    int     init(void);
-    int     connect(char * ssid, char * sec_key, WiFi_Priv_Mode priv_mode);
-    int     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);
 
-    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);
+    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);
-    void        socket_server_write(uint16_t DataLength,char * pData);
-    void        socket_server_close(void);
+    int         socket_server_open(uint32_t port_number, uint8_t * protocol);
+    int         socket_server_write(uint16_t data_length,char * pdata);
+    int         socket_server_close(void);
     
     void        http_get(uint8_t * hostname, uint8_t * path, uint32_t port_number);
     void        http_post(uint8_t * url_path);
@@ -85,6 +85,8 @@
     void        waitForEvent(void); 
     void        signal_data_receive(uint8_t socket_id, uint8_t * data_ptr, uint32_t message_size, uint32_t chunk_size);
     void        signal_synch_wait(WiFi_Status_t code);
+    void        set_wait_for_incoming_client(bool set);
+    bool        get_wait_for_incoming_client();
     bool        getIsInitialized(void);
     void        setTimeout(int timeout) {
                 _timeout = timeout;
@@ -103,6 +105,7 @@
     DigitalInOut                rts_;              
     wifi_config                 config;
     bool                        sync_wait_signal;
+    bool                        wait_for_incoming_client;
     uint16_t                    bytes_read;
     uint16_t                    bytes_to_read;
     uint8_t *                   recv_buff;
--- a/Spwf_API/utils/ItfTranslator.cpp	Wed May 04 12:16:20 2016 +0000
+++ b/Spwf_API/utils/ItfTranslator.cpp	Mon May 09 10:38:22 2016 +0000
@@ -134,8 +134,8 @@
 
 void ind_wifi_http_data_available(uint8_t * data_ptr, uint32_t message_size)
 {
-  //spwf->debug("\r\nrx>>\r\n");
-  //memcpy(user_buffer, data_ptr, 511);
+    //spwf->debug("\r\nrx>>\r\n");
+    //memcpy(user_buffer, data_ptr, 511);
 }
 
 void ind_wifi_socket_client_remote_server_closed(uint8_t * socket_closed_id)
@@ -144,6 +144,22 @@
     //device->signal_synch_wait(WiFi_MODULE_SUCCESS);
 }
 
+void ind_socket_server_client_joined(void)
+{
+    //device->debug_print("\r\nclient connected!\r\n");
+    device->set_wait_for_incoming_client(true);
+}
+
+void ind_socket_server_client_left(void)
+{
+    
+}
+
+void ind_wifi_socket_server_data_lost(void)
+{
+    
+}
+
 #ifdef __cplusplus
 }
 #endif