Simple LED control project using CC3100 as Access Point and socket
Fork of cc3100_Test_Demo by
main.cpp
- Committer:
- ArcN00b
- Date:
- 2017-05-25
- Revision:
- 8:2acb25effa3a
- Parent:
- 7:0687d16b9781
- Child:
- 9:30d6c10d4eab
File content as of revision 8:2acb25effa3a:
/* * 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 accessPointnstrates 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" 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 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 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 accessPoint = 0; /* * GLOBAL VARIABLES -- End */ /* * 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(); _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(" Impossibile completare la configurazione della CC3100 \n\r"); LOOP_FOREVER(); } printf(" CC3100 configurata nel suo stato di default \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(!accessPoint){ retVal = _cc3100.sl_Start(0, 0, 0); if ((retVal < 0) || (ROLE_STA != retVal) ){ printf(" Impossibile avviare la CC3100 nella modalita' richiesta \n\r"); LOOP_FOREVER(); } printf(" CC3100 avviata come scheda di rete \n\r"); } if(accessPoint == 0 ){ station_app(); }else{ AP_app(); } return 0; } /*! \brief This function displays the application's banner \param None \return None */ static void displayBanner() { //int choose; if(accessPoint){ printf("\n\r\n\r"); printf(" CC3100 in avvio come Access Point "); printf("\n\r*******************************************************************************\n\r"); //scanf("%s", &choose); //printf("\n\r%d\n\r", choose); }else{ printf("\n\r\n\r"); printf(" CC3100 in avvio come scheda di rete "); 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; } int32_t pingToHost(char* host) { SlPingStartCommand_t pingParams = {0}; SlPingReport_t pingReport = {0}; uint32_t ipAddr = 0; int32_t retVal = -1; _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE); g_PingPacketsRecv = 0; /* Set the ping parameters */ pingParams.PingIntervalTime = PING_INTERVAL; pingParams.PingSize = PING_PKT_SIZE; pingParams.PingRequestTimeout = PING_TIMEOUT; pingParams.TotalNumberOfAttempts = PING_ATTEMPTS; pingParams.Flags = 0; pingParams.Ip = g_GatewayIP; /* Check for Internet connection */ retVal = _cc3100._netapp.sl_NetAppDnsGetHostByName((unsigned char *)host, strlen(host), &ipAddr, SL_AF_INET); if(retVal < 0) { printf(" Impossibile trovare l'host \n\r"); return retVal; } else printf(" Avvio ping\n"); /* Replace the ping address to match HOST_NAME's IP address */ pingParams.Ip = ipAddr; /* Try to ping HOST_NAME */ retVal = _cc3100._netapp.sl_NetAppPingStart( (SlPingStartCommand_t*)&pingParams, SL_AF_INET, (SlPingReport_t*)&pingReport, SimpleLinkPingReport); if(retVal < 0) { printf(" Impossibile connettersi all'host \n\r"); return retVal; } /* Wait */ while(!_cc3100.IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) { _cc3100._nonos._SlNonOsMainLoopTask(); } 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; } void station_app(void) { int32_t retVal = -1; char host[100]; /* Connecting to WLAN AP */ retVal = _cc3100.establishConnectionWithAP(); if(retVal < 0) { printf(" Impossibile connettersi \n\r"); LOOP_FOREVER(); } printf(" Connessione stabilita con l'Access Point \n\r"); printf(" Test di connettivita' in corso... \n\r"); retVal = _cc3100.checkLanConnection(); if(retVal < 0) { printf(" Device couldn't connect to LAN \n\r"); LOOP_FOREVER(); } printf(" Connesso alla rete locale con successo \r\n"); retVal = _cc3100.checkInternetConnection(); if(retVal < 0) { printf(" Device couldn't connect to the internet \n\r"); LOOP_FOREVER(); } printf(" Connesso ad internet con successo"); // Esegue ping finchè connesso while(1) { printf(" \n\rInserire un host a cui fare il ping \n\r"); scanf("%s", host); if(pingToHost(host) == 0) printf("\nTest completato senza errori\n\r"); } }