cc3100_Socket_Wifi_Server version for LPC1768

Dependencies:   mbed

Fork of cc3100_Test_Demo by David Fletcher

Files at this revision

API Documentation at this revision

Comitter:
artpes
Date:
Fri May 26 19:26:11 2017 +0000
Parent:
7:0687d16b9781
Commit message:
Fork

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
simplelink/cc3100_sl_common.h Show annotated file Show diff for this revision Revisions of this file
diff -r 0687d16b9781 -r e79bacf664cc main.cpp
--- a/main.cpp	Thu Sep 03 13:43:50 2015 +0000
+++ b/main.cpp	Fri May 26 19:26:11 2017 +0000
@@ -1,301 +1,59 @@
-/*
- * main.c - sample application to switch to AP mode and ping client
- *
- * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
- *
- *
- *  Redistribution and use in source and binary forms, with or without
- *  modification, are permitted provided that the following conditions
- *  are met:
- *
- *    Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- *    Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the
- *    distribution.
- *
- *    Neither the name of Texas Instruments Incorporated nor the names of
- *    its contributors may be used to endorse or promote products derived
- *    from this software without specific prior written permission.
- *
- *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-/*
- * Application Name     -   Getting started with Wi-Fi Access-Point mode
- * Application Overview -   This sample application demonstrates how
- *                          to configure CC3100 in Access-Point mode. Any
- *                          WLAN station in its range can connect/communicate
- *                          to/with it as per the standard networking protocols.
- *                          On a successful connection, the device ping's the
- *                          connected station.
- * Application Details  -   http://processors.wiki.ti.com/index.php/CC31xx_Getting_Started_with_WLAN_AP
- *                          doc\examples\getting_started_with_wlan_ap.pdf
- */
-
 #include "cc3100_simplelink.h"
 #include "cc3100_sl_common.h"
-
 #include "fPtr_func.h"
 #include "cc3100.h"
 #include "cc3100_spi.h"
 #include "myBoardInit.h"
+#include "cc3100_socket.h"
+#include "cc3100_driver.h"
+#include "cc3100_nonos.h"
+#include "mbed.h"
+//#include "ethernet.h"
+//#include "rtos.h"
+//#include "EthernetInterface.h"
 
 using namespace mbed_cc3100;
 
-#if (THIS_BOARD == MBED_BOARD_LPC1768)
-//cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC1768  irq, nHib, cs, mosi, miso, sck
+#define SL_STOP_TIMEOUT        0xFF
+
+/* IP addressed of server side socket. Should be in long format,
+ * E.g: 0xc0a8010a == 192.168.1.10 */
+//#define IP_ADDR         0xc0a82b69 //192.168.43.105
+#define PORT_NUM        50001            /* Port number to be used */
+
+#define BUF_SIZE        1400
+#define NO_OF_PACKETS   1000
+
 cc3100 _cc3100(p9, p10, p8, SPI(p11, p12, p13));//LPC1768  irq, nHib, cs, mosi, miso, sck
 Serial pc(USBTX, USBRX);//lpc1768
-#elif (THIS_BOARD == ST_MBED_NUCLEOF411)
-cc3100 _cc3100(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF411  irq, nHib, cs, mosi, miso, sck
-Serial pc(SERIAL_TX, SERIAL_RX);//nucleoF411
-#elif (THIS_BOARD == ST_MBED_NUCLEOF401)
-cc3100 _cc3100(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF401  irq, nHib, cs, mosi, miso, sck
-Serial pc(SERIAL_TX, SERIAL_RX);//nucleoF401
-#elif (THIS_BOARD == EA_MBED_LPC4088)
-cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC4088  irq, nHib, cs, mosi, miso, sck
-Serial pc(USBTX, USBRX);//EA_lpc4088
-#elif (THIS_BOARD == ST_MBED_NUCLEOF103)
-cc3100 _cc3100(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF103  irq, nHib, cs, mosi, miso, sck
-Serial pc(SERIAL_TX, SERIAL_RX);
-#elif (THIS_BOARD == Seeed_Arch_Max)
-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"
-
-/*
- * GLOBAL VARIABLES -- Start
- */
-int32_t demo = 0;
-
-/*
- * GLOBAL VARIABLES -- End
- */
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);
+DigitalOut myled4(LED4);
 
 
-/*
- * STATIC FUNCTION DEFINITIONS -- Start
- */
-
-static void displayBanner();
-/*
- * STATIC FUNCTION DEFINITIONS -- End
- */
- 
-void station_app(void);
-void AP_app(void);
-
-/*
- * Application's entry point
- */
-  
- 
-int main(void) {
-
-    pc.baud(115200);
-
-    int32_t retVal = -1;
-
-    retVal = _cc3100.initializeAppVariables();
-    ASSERT_ON_ERROR(retVal);
-
-    displayBanner();
+//GLOBAL VARIABLES -- Start
+int32_t demo = 0;
+//GLOBAL VARIABLES -- End
 
-    _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE);
-    g_PingPacketsRecv = 0;
-    
-    /*
-     * Following function configures the device to default state by cleaning
-     * the persistent settings stored in NVMEM (viz. connection profiles &
-     * policies, power policy etc)
-     *
-     * Applications may choose to skip this step if the developer is sure
-     * that the device is in its default state at start of application
-     *
-     * Note that all profiles and persistent settings that were done on the
-     * device will be lost
-     */
-    retVal = _cc3100.configureSimpleLinkToDefaultState();
-    
-    if(retVal < 0) {
-        if (DEVICE_NOT_IN_STATION_MODE == retVal)
-            printf(" Failed to configure the device to its default state \n\r");
 
-        LOOP_FOREVER();
-    }
-
-    printf(" Device is configured in it's default state \n\r");
-    
-    /*
-     * Assumption is that the device is configured in station mode already
-     * and it is in its default state
-     */
-    /* Initializing the CC3100 device */
-    
-    if(!(demo ==1)){
-        retVal = _cc3100.sl_Start(0, 0, 0);
-    
-        if ((retVal < 0) || (ROLE_STA != retVal) ){
-        printf(" Failed to start the device \n\r");
-        LOOP_FOREVER();
-        }
-
-        printf(" Device started as STATION \n\r");
-    }
-    
-    if(demo == 0 ){
-       station_app();
-    }else{
-       AP_app(); 
-    }    
-    return 0;
-}    
-
-/*!
-    \brief This function displays the application's banner
-
-    \param      None
-
-    \return     None
-*/
 static void displayBanner()
 {
-    if(!demo){
-        printf("\n\r\n\r");
-        printf(" Getting started with WLAN access-point application - Version ");
-        printf("%s",APPLICATION_VERSION);
-        printf("\n\r*******************************************************************************\n\r");
-    
-    }else{
-         printf("\n\r\n\r");
-         printf(" Getting started with station application - Version ");
-         printf(" %s", APPLICATION_VERSION);
-         printf("\n\r*******************************************************************************\n\r");
-    }
+     printf("\n\r\n\r");
+     printf("***********Getting started with station application***********");
+     printf("\n\r*******************************************************************************\n\r");
 }
 
-void AP_app(void){
-    
-    SlPingStartCommand_t PingParams = {0};
-    SlPingReport_t Report = {0};
-//    SlNetCfgIpV4Args_t ipV4 = {0};
-//    SlNetAppDhcpServerBasicOpt_t dhcpParams = {0};
 
-    uint8_t SecType = 0;
-    int32_t role = ROLE_STA;
-    int32_t retVal = -1;
-    
-    /*
-     * Assumption is that the device is configured in station mode already
-     * and it is in its default state
-     */
-    role = _cc3100.sl_Start(0, 0, 0);
-    if (ROLE_AP == role) {
-        /* If the device is in AP mode, we need to wait for this event before doing anything */
-        while(!_cc3100.IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED)) {
-            _cc3100._nonos._SlNonOsMainLoopTask();
-        }
-    } else {
-        /* Configure CC3100 to start in AP mode */
-        retVal = _cc3100._wlan.sl_WlanSetMode(ROLE_AP);
-        if(retVal < 0)
-            LOOP_FOREVER();
-            
-        /* Configure the SSID of the CC3100 */
-        retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID,
-                strlen(SSID_AP_MODE), (uint8_t *)SSID_AP_MODE);
-        if(retVal < 0)
-            LOOP_FOREVER();
-
-        SecType = SEC_TYPE_AP_MODE;
-        /* Configure the Security parameter the AP mode */
-        retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, (uint8_t *)&SecType);
-        if(retVal < 0)
-            LOOP_FOREVER();
-
-        retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(PASSWORD_AP_MODE), (uint8_t *)PASSWORD_AP_MODE);
-        if(retVal < 0)
-            LOOP_FOREVER();    
-
-        retVal = _cc3100.sl_Stop(SL_STOP_TIMEOUT);
-        if(retVal < 0)
-            LOOP_FOREVER();
-
-        _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_IP_ACQUIRED);
-
-        role = _cc3100.sl_Start(0, 0, 0);
-        if (ROLE_AP == role) {
-            /* If the device is in AP mode, we need to wait for this event before doing anything */
-            while(!_cc3100.IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED)) {
-                _cc3100._nonos._SlNonOsMainLoopTask();
-            }
-        } else {
-            printf(" Device couldn't be configured in AP mode \n\r");
-            LOOP_FOREVER();
-        }
-    }
-    printf(" Device started as Access Point\n\r");
-
-    /* Wait */
-    printf(" Waiting for clients to connect...!\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");
-
-    /* Set the ping parameters */
-    PingParams.PingIntervalTime = PING_INTERVAL;
-    PingParams.PingSize = PING_SIZE;
-    PingParams.PingRequestTimeout = PING_TIMEOUT;
-    PingParams.TotalNumberOfAttempts = PING_ATTEMPTS;
-    PingParams.Flags = 0;
-    PingParams.Ip = g_StationIP; /* Fill the station IP address connected to CC3100 */
-
-    /* Ping client connected to CC3100 */
-    retVal = _cc3100._netapp.sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&Report, &SimpleLinkPingReport);
-    if(retVal < 0)
-        LOOP_FOREVER();
-
-    /* Wait */
-    while(!_cc3100.IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) {
-        _cc3100._nonos._SlNonOsMainLoopTask();
-    }
-
-    if (0 == g_PingPacketsRecv) {
-        printf(" A STATION couldn't connect to the device \n\r");
-        //ASSERT_ON_ERROR(LAN_CONNECTION_FAILED);
-        printf(" ERROR code %d\n\r", LAN_CONNECTION_FAILED);
-    }
-
-    printf(" Device and the station are successfully connected \n\r");
-    //return SUCCESS;
-}
-
-void station_app(void){
-    
+//******************************STATION_MODE***********************************************
+//*****************************************************************************************
+void station_app()
+{
     int32_t retVal = -1;
     
     /* Connecting to WLAN AP */
     retVal = _cc3100.establishConnectionWithAP();
+    //printf("retVal 000: %d\n\r",retVal);   
     if(retVal < 0)
     {
         printf(" Failed to establish connection w/ an AP \n\r");
@@ -305,13 +63,14 @@
     
     printf(" Pinging...! \n\r");
     retVal = _cc3100.checkLanConnection();
+    //printf("retVal 001: %d\n\r",retVal);
     if(retVal < 0)
     {
         printf(" Device couldn't connect to LAN \n\r");
         LOOP_FOREVER();
     }
-    printf(" Device successfully connected to the LAN\r\n");
-    
+    printf(" Device successfully connected to the LAN\n\r");
+    /*
     retVal = _cc3100.checkInternetConnection();
     if(retVal < 0)
     {
@@ -319,7 +78,238 @@
         LOOP_FOREVER();
     }
 
-    printf(" Device successfully connected to the internet \n\r");
-}              
+    printf(" Device successfully connected to the internet \n\r");*/
+}
+//****************************END_STATION_MODE*********************************************
+//*****************************************************************************************
+void led(int result){
+    
+    if (result > 0)
+      { 
+            myled1 = 1;
+            wait(0.05);
+            myled1 = 0;
+            wait(0.05);
+            /*
+                    myled2 = 1;
+            wait(0.05);
+            myled2 = 0;
+            wait(0.05);
+                    myled3 = 1;
+            wait(0.05);
+            myled3 = 0;
+            wait(0.05);
+                    myled4 = 1;
+            wait(0.05);
+            myled4 = 0;
+            wait(0.05);
+        }
+    else 
+    { myled1 = 0;
+      myled2 = 0;
+      myled3 = 0;
+      myled4 = 0;
+    }
+*/
+}
+ }
+
+//osThreadDef(led, osPriorityNormal, DEFAULT_STACK_SIZE);
+static int32_t BsdTcpServer(uint16_t Port)
+{
+    
+    SlSockAddrIn_t  Addr;
+    SlSockAddrIn_t  LocalAddr;
+    char stringa[3];
+    //int16_t         idx = 0;
+    int16_t          AddrSize = 0;
+    int16_t          SockID = 0;
+    int32_t          Status = 0;
+    int16_t          newSockID = 0;
+    int16_t          LoopCount = 0;
+    //int16_t          SlSockAddrIn_t = 0;
+    int16_t          recvSize = 0;
+    //Thread thread;
+       
+/*
+    for (idx=0 ; idx<BUF_SIZE ; idx++)
+    {
+        uBuf.BsdBuf[idx] = (_u8)(idx % 10);
+    }
+*/
+    /*int a = 0;
+    a = ethernet();
+    printf("\nEth funzia : %d \n\r",a)*/
+    
+    LocalAddr.sin_family = SL_AF_INET;
+    LocalAddr.sin_port = _cc3100._socket.sl_Htons(PORT_NUM);
+    LocalAddr.sin_addr.s_addr = 0;
+    //int port = 1;
+    //osThreadCreate(osThread(led), (void *) port);
+    //thread.start(led(1));  
+        
+        SockID = _cc3100._socket.sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
+        if( SockID < 0 )
+        {
+            printf(" \n[TCP Server] Create socket Error \n\r");
+            ASSERT_ON_ERROR(SockID);
+        }
+        else 
+            printf("\nCreate socket \n\r");
+        
+    
+        AddrSize = sizeof(SlSockAddrIn_t);
+        Status = _cc3100._socket.sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize);
+        if( Status < 0 )
+        {
+            _cc3100._socket.sl_Close(SockID);
+            printf(" \n[TCP Server] Socket address assignment Error \n\r");
+            ASSERT_ON_ERROR(Status);
+        }
+        else
+            printf("\nSocket address assignment \n\r");
+    
+        Status = _cc3100._socket.sl_Listen(SockID, 0);
+        if( Status < 0 )
+        {
+            _cc3100._socket.sl_Close(SockID);
+            printf("\n [TCP Server] Listen Error \n\r");
+            ASSERT_ON_ERROR(Status);
+        }
+        else
+            printf("\nListen for a Connection \n\r");
+    
+        newSockID = _cc3100._socket.sl_Accept(SockID, (SlSockAddr_t *)&Addr,(SlSocklen_t*)&AddrSize);
+        if( newSockID < 0 )
+        {
+            _cc3100._socket.sl_Close(SockID);
+            printf("\n [TCP Server] Accept connection Error \n\r");
+            ASSERT_ON_ERROR(newSockID);
+        }
+        else 
+            printf("\nAccept connection \n\r");
+ 
+    while (LoopCount < NO_OF_PACKETS)
+        {   
+            //TODO : sarebbe da chiamare la funzione led() con un altro thread così da farla girare in background
+            //led(1);
+            recvSize = BUF_SIZE;
+            //printf("RecvSize = %d \n\r",recvSize);
+            do
+            {
+                Status = _cc3100._socket.sl_Recv(newSockID, &(stringa), strlen("a"), 0);
+                led(1);
+                if( Status <= 0 )
+                {
+                    _cc3100._socket.sl_Close(newSockID);
+                    _cc3100._socket.sl_Close(SockID);
+                    //led(1);
+                    printf(" \n[TCP Server] Data recv Error. Close socket %d \n\n\r",SockID);
+                    //ASSERT_ON_ERROR(TCP_RECV_ERROR);
+                    
+                    /*-----Accept new connection----------*/
+                    SockID = _cc3100._socket.sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
+                    printf("----------Create new socket---------- \n\n\r");
+                    AddrSize = sizeof(SlSockAddrIn_t);
+                    Status = _cc3100._socket.sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize);
+                    printf("----------Socket address assignment---------- \n\n\r");
+                    Status = _cc3100._socket.sl_Listen(SockID, 0);
+                    printf("----------Listen for a new Connection---------- \n\n\r");
+                    newSockID = _cc3100._socket.sl_Accept(SockID, (SlSockAddr_t *)&Addr,(SlSocklen_t*)&AddrSize);
+                    printf("----------Accept new connection---------- \n\n\r");
+                    led(1);
+                }
+                
+                //printf("Status = %d \n\r",Status);
+                recvSize -= Status;
+                //printf("RecvSize = %d \n\r",recvSize);
+                printf("%s",stringa);
+                //led(1);
+                //char thanks [] = "Data received";
+                //_cc3100._socket.sl_Send(SockID, &(thanks), strlen(thanks), 0);
+                
+            }
+            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;
+    
+}
 
 
+
+//**************************_START_APPLICATION_*********************************
+int main(void) 
+{
+    pc.baud(115200);
+    
+    
+    
+    int32_t retVal = -1;
+    
+    retVal = _cc3100.initializeAppVariables();
+    
+    displayBanner();
+
+    _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE);
+    g_PingPacketsRecv = 0;
+    //printf("\n\r FUNZIONE CLR_STATUS_BIT ESEGUITA CON SUCCESSO\n\r");
+
+    //retVal = _cc3100.configureSimpleLinkToDefaultState();
+    
+    //printf("\n\rRetval 2: %d \n\r ",retVal);
+    
+    if(retVal < 0) {
+        if (DEVICE_NOT_IN_STATION_MODE == retVal)
+            printf(" Failed to configure the device to its default state \n\r");
+
+        LOOP_FOREVER();
+    }
+
+    printf("\n\rDevice is configured in it's default state \n\r");
+    /*printf("\n\r******************************************************************************************************\n\r");
+    printf("\n\r*****_DA_QUI_IN_POI_SCELTA_TIPO_FUNZIONE_IN_BASE_AL_VALORE_demo_*****\n\r");
+    printf("\n\r******************************************************************************************************\n\r");*/
+    
+    /*
+     * Assumption is that the device is configured in station mode already
+     * and it is in its default state
+     */
+    /* Initializing the CC3100 device */
+    
+    retVal = _cc3100.sl_Start(0, 0, 0);
+    
+    if ((retVal < 0) || (ROLE_STA != retVal) )
+    {
+        printf(" Failed to start the device \n\r");
+        LOOP_FOREVER();
+    }
+
+    printf("\n\r Device started as STATION \n\r");
+    
+    
+    station_app();
+    
+    printf("Establishing connection with TCP server \n\r");
+    /*Before proceeding, please make sure to have a server waiting on PORT_NUM*/
+    retVal = BsdTcpServer(PORT_NUM);
+    if(retVal < 0)
+        printf(" Failed to establishing connection with TCP server \n\r");
+    else
+        printf(" Connection with TCP server established successfully \n\r");
+    
+    return 0;
+    
+    
+    
+    
+}    
+
diff -r 0687d16b9781 -r e79bacf664cc simplelink/cc3100_sl_common.h
--- a/simplelink/cc3100_sl_common.h	Thu Sep 03 13:43:50 2015 +0000
+++ b/simplelink/cc3100_sl_common.h	Fri May 26 19:26:11 2017 +0000
@@ -65,10 +65,10 @@
  * Values for below macros shall be modified per the access-point's (AP) properties
  * SimpleLink device will connect to following AP when the application is executed
  */
-#define SSID_NAME         "**********"
+#define SSID_NAME         "APAP"
 //#define SSID_NAME       "OpenWrt"         /* Access point name to connect to. */
 #define SEC_TYPE        SL_SEC_TYPE_WPA_WPA2    /* Security type of the Access point */
-#define PASSKEY           "**********"
+#define PASSKEY           "hansolone"
 //#define PASSKEY         "**********"                  /* Password in case of secure AP */
 #define PASSKEY_LEN     strlen(PASSKEY)  /* Password length in case of secure AP */