Simple LED control project using CC3100 as Access Point and socket

Dependencies:   mbed

Fork of cc3100_Test_Demo by David Fletcher

Revision:
9:30d6c10d4eab
Parent:
8:2acb25effa3a
Child:
10:d24deb1afd1b
--- a/main.cpp	Thu May 25 16:58:17 2017 +0000
+++ b/main.cpp	Fri May 26 07:25:06 2017 +0000
@@ -46,6 +46,7 @@
  *                          doc\examples\getting_started_with_wlan_ap.pdf
  */
 
+#include "mbed.h"
 #include "cc3100_simplelink.h"
 #include "cc3100_sl_common.h"
 
@@ -53,12 +54,14 @@
 #include "cc3100.h"
 #include "cc3100_spi.h"
 #include "myBoardInit.h"
+#include <string.h>
 
 using namespace mbed_cc3100;
 
 #if (THIS_BOARD == MBED_BOARD_LPC4337)
 //cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC1768  irq, nHib, cs, mosi, miso, sck
 cc3100 _cc3100(P2_2, P3_5, P1_5, SPI(P1_4, P1_3, PF_4));//LPC4337  irq, nHib, cs, mosi, miso, sck
+//cc3100_socket _csk(_c3100.;
 Serial pc(USBTX, USBRX);//lpc4337
 #elif (THIS_BOARD == MBED_BOARD_LPC1768)
 //cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC1768  irq, nHib, cs, mosi, miso, sck
@@ -80,15 +83,19 @@
 cc3100 _cc3100(PD_12, PD_13, PD_11, SPI(PB_5, PB_4, PB_3));//Seeed_Arch_Max  irq, nHib, cs, mosi, miso, sck
 Serial pc(USBTX, USBRX);
 #else
-
 #endif
 
-#define APPLICATION_VERSION "1.2.0"
+/* Indirizzo IP del server, bisogna usare il formato long esadecimale
+ * E.g: 0xc0a8010a == 192.168.1.1 */
+#define IP_ADDR         0xc0a80101
+#define PORT_NUM        5000
 
+#define BUF_SIZE        1400
+#define NO_OF_PACKETS   1000
 /*
  * GLOBAL VARIABLES -- Start
  */
-int32_t accessPoint = 0;
+int32_t accessPoint = 1;
 
 /*
  * GLOBAL VARIABLES -- End
@@ -107,13 +114,48 @@
 void station_app(void);
 void AP_app(void);
 
-/*
- * Application's entry point
- */
-  
+DigitalOut myled1(LED1);//Azzurro
+DigitalOut myled2(LED2);//Giallo
+DigitalOut myled3(LED3);//Viola
+
+void white() {
+    myled1 = 0;
+    myled2 = 0;
+    myled3 = 0;
+}
+
+void ledShow() {
+    while(1) {
+        myled1 = 1;
+        myled2 = 1;
+        myled3 = 1;
+        wait(5);
+        myled1 = 0;
+        wait(5);
+        myled1 = 1;
+        myled2 = 0;
+        wait(5);
+        myled2 = 1;
+        myled3 = 0;
+        wait(5);
+        myled2 = 0;
+        wait(5);
+        myled1 = 0;
+        myled2 = 1;
+        wait(5);
+        myled2 = 0;
+        myled3 = 1;
+        wait(5);
+        myled3 = 0;
+        wait(5);
+    }
+}
  
 int main(void) {
 
+    myled1 = 1;
+    myled2 = 1;
+    myled3 = 1;
     pc.baud(115200);
 
     int32_t retVal = -1;
@@ -198,6 +240,112 @@
     }
 }
 
+int32_t TcpServer(uint16_t Port)
+{
+    SlSockAddrIn_t  Addr;
+    SlSockAddrIn_t  LocalAddr;
+    char buf[100], tmp[100];
+    int i, len;
+
+    int16_t       AddrSize = 0;
+    int16_t       SockID = 0;
+    int32_t       Status = 0;
+    int16_t       newSockID = 0;
+    uint16_t      LoopCount = 0;
+    int16_t       recvSize = 0;
+
+    LocalAddr.sin_family = SL_AF_INET;
+    LocalAddr.sin_port = _cc3100._socket.sl_Htons((int16_t)Port);
+    LocalAddr.sin_addr.s_addr = 0;
+
+    SockID = _cc3100._socket.sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
+    if( SockID < 0 )
+    {
+        printf(" Errore durante la creazione del socket \n\r");
+        ASSERT_ON_ERROR(SockID);
+    }
+
+    AddrSize = sizeof(SlSockAddrIn_t);
+    Status = _cc3100._socket.sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize);
+    if( Status < 0 )
+    {
+        _cc3100._socket.sl_Close(SockID);
+        printf(" Errore durante il bind della porta \n\r");
+        ASSERT_ON_ERROR(Status);
+    }
+
+    Status = _cc3100._socket.sl_Listen(SockID, 0);
+    if( Status < 0 )
+    {
+        _cc3100._socket.sl_Close(SockID);
+        printf(" Errore durante la listen \n\r");
+        ASSERT_ON_ERROR(Status);
+    }
+    
+    printf("Socket in attesa di connessioni \n\r");
+
+    newSockID = _cc3100._socket.sl_Accept(SockID, (SlSockAddr_t *)&Addr,
+                              (SlSocklen_t*)&AddrSize);
+    if( newSockID < 0 )
+    {
+        _cc3100._socket.sl_Close(SockID);
+        printf(" Errore durante la accept \n\r");
+        ASSERT_ON_ERROR(newSockID);
+    }
+    
+    printf("Connessione accettata\n\r");
+
+    while (LoopCount < NO_OF_PACKETS)
+    {
+        recvSize = BUF_SIZE;
+        do
+        {
+            Status = _cc3100._socket.sl_Recv(newSockID, &(buf), recvSize, 0);
+            if( Status <= 0 )
+            {
+                _cc3100._socket.sl_Close(newSockID);
+                _cc3100._socket.sl_Close(SockID);
+                printf(" Errore in ricezione \n\r");
+                ASSERT_ON_ERROR(TCP_RECV_ERROR);
+            } else {
+                printf("\n\r Ricevuto -> %s %d \n\r", buf, strlen(buf));
+            
+                // Rimuovo i caratteri strani
+                for(i = 0; i < strlen(buf); i++) {
+                    if((buf[i] >= 65 && buf[i] <= 90) || (buf[i] >= 97 && buf[i] <= 122)){
+                        printf("%c ", buf[i]);
+                        len = i + 1;
+                    }
+                }
+                buf[len] = '\0'; 
+                
+                printf("%s %d \n\r", buf, strlen(buf));
+                sprintf(tmp, "Ricevuto -> %s \n\r", buf);
+                _cc3100._socket.sl_Send(newSockID, &(tmp), strlen(tmp),0);
+                
+                if(strcmp(buf,"Blu") != 0 || strcmp(buf,"blu") != 0) {
+                    white();
+                    myled1 = 1;
+                    myled3 = 1;
+                }
+            }
+
+            recvSize -= Status;
+        }
+        while(recvSize > 0);
+
+        LoopCount++;
+    }
+
+    Status = _cc3100._socket.sl_Close(newSockID);
+    ASSERT_ON_ERROR(Status);
+
+    Status = _cc3100._socket.sl_Close(SockID);
+    ASSERT_ON_ERROR(Status);
+
+    return SUCCESS;
+}
+
 void AP_app(void){
     
     SlPingStartCommand_t PingParams = {0};
@@ -254,19 +402,19 @@
                 _cc3100._nonos._SlNonOsMainLoopTask();
             }
         } else {
-            printf(" Device couldn't be configured in AP mode \n\r");
+            printf(" Impossibile completare l'avvio come Access Point \n\r");
             LOOP_FOREVER();
         }
     }
-    printf(" Device started as Access Point\n\r");
+    printf(" Avvio come Access Point completato \n\r");
 
     /* Wait */
-    printf(" Waiting for clients to connect...!\n\r");
+    printf(" In attesa di qualcuno che si connetta\n\r");
     while((!_cc3100.IS_IP_LEASED(g_Status,STATUS_BIT_IP_LEASED)) || (!_cc3100.IS_STA_CONNECTED(g_Status,STATUS_BIT_STA_CONNECTED))) {
         _cc3100._nonos._SlNonOsMainLoopTask();
     }
-    printf(" Client connected to the device \n\r");
-    printf(" Pinging...! \n\r");
+    printf(" Un client sta tentando di connettersi \n\r");
+    printf(" In corso test di connettivita'... \n\r");
 
     /* Set the ping parameters */
     PingParams.PingIntervalTime = PING_INTERVAL;
@@ -287,13 +435,18 @@
     }
 
     if (0 == g_PingPacketsRecv) {
-        printf(" A STATION couldn't connect to the device \n\r");
+        printf(" Impossibile connettersi correttamente al client \n\r");
         //ASSERT_ON_ERROR(LAN_CONNECTION_FAILED);
-        printf(" ERROR code %d\n\r", LAN_CONNECTION_FAILED);
+        printf(" ERRORE numero %d\n\r", LAN_CONNECTION_FAILED);
     }
 
-    printf(" Device and the station are successfully connected \n\r");
-    //return SUCCESS;
+    printf(" Client correttamente connesso \n\r");
+    printf(" Socket in apertura \n\r");
+    
+    //Funzione che gestisce e crea la socket
+    retVal = TcpServer(PORT_NUM);
+    if(retVal < 0)
+        printf(" Impossibile avviare la comunicazione \n\r");
 }
 
 int32_t pingToHost(char* host) {
@@ -341,10 +494,6 @@
     if (0 == g_PingPacketsRecv) {
         printf(" Il test non e' andato a buon fine \n\r");
         return retVal;
-    } else {
-        
-        printf("\n\r Pacchetti inviati -> %d \n\r Pacchetti ricevuti -> %d \n\r", pingReport.PacketsSent, pingReport.PacketsReceived);
-        printf("Tempo minimo -> %d s\n\r Tempo massimo -> %d s\n\r Tempo medio -> %d s\n\r Tempo di test %d s\n\r", pingReport.MinRoundTime, pingReport.MaxRoundTime, pingReport.AvgRoundTime ,pingReport.TestTime);
     }
     
     return 0;