Simple LED control project using CC3100 as Access Point and socket

Dependencies:   mbed

Fork of cc3100_Test_Demo by David Fletcher

Committer:
ArcN00b
Date:
Thu May 25 16:58:17 2017 +0000
Revision:
8:2acb25effa3a
Parent:
7:0687d16b9781
Child:
9:30d6c10d4eab
Procedura ping testata

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 0:e89ba455dbcf 1 /*
dflet 0:e89ba455dbcf 2 * main.c - sample application to switch to AP mode and ping client
dflet 0:e89ba455dbcf 3 *
dflet 0:e89ba455dbcf 4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
dflet 0:e89ba455dbcf 5 *
dflet 0:e89ba455dbcf 6 *
dflet 0:e89ba455dbcf 7 * Redistribution and use in source and binary forms, with or without
dflet 0:e89ba455dbcf 8 * modification, are permitted provided that the following conditions
dflet 0:e89ba455dbcf 9 * are met:
dflet 0:e89ba455dbcf 10 *
dflet 0:e89ba455dbcf 11 * Redistributions of source code must retain the above copyright
dflet 0:e89ba455dbcf 12 * notice, this list of conditions and the following disclaimer.
dflet 0:e89ba455dbcf 13 *
dflet 0:e89ba455dbcf 14 * Redistributions in binary form must reproduce the above copyright
dflet 0:e89ba455dbcf 15 * notice, this list of conditions and the following disclaimer in the
dflet 0:e89ba455dbcf 16 * documentation and/or other materials provided with the
dflet 0:e89ba455dbcf 17 * distribution.
dflet 0:e89ba455dbcf 18 *
dflet 0:e89ba455dbcf 19 * Neither the name of Texas Instruments Incorporated nor the names of
dflet 0:e89ba455dbcf 20 * its contributors may be used to endorse or promote products derived
dflet 0:e89ba455dbcf 21 * from this software without specific prior written permission.
dflet 0:e89ba455dbcf 22 *
dflet 0:e89ba455dbcf 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
dflet 0:e89ba455dbcf 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
dflet 0:e89ba455dbcf 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
dflet 0:e89ba455dbcf 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
dflet 0:e89ba455dbcf 27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
dflet 0:e89ba455dbcf 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
dflet 0:e89ba455dbcf 29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
dflet 0:e89ba455dbcf 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
dflet 0:e89ba455dbcf 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
dflet 0:e89ba455dbcf 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dflet 0:e89ba455dbcf 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dflet 0:e89ba455dbcf 34 *
dflet 0:e89ba455dbcf 35 */
dflet 0:e89ba455dbcf 36
dflet 0:e89ba455dbcf 37 /*
dflet 0:e89ba455dbcf 38 * Application Name - Getting started with Wi-Fi Access-Point mode
ArcN00b 8:2acb25effa3a 39 * Application Overview - This sample application accessPointnstrates how
dflet 0:e89ba455dbcf 40 * to configure CC3100 in Access-Point mode. Any
dflet 0:e89ba455dbcf 41 * WLAN station in its range can connect/communicate
dflet 0:e89ba455dbcf 42 * to/with it as per the standard networking protocols.
dflet 0:e89ba455dbcf 43 * On a successful connection, the device ping's the
dflet 0:e89ba455dbcf 44 * connected station.
dflet 0:e89ba455dbcf 45 * Application Details - http://processors.wiki.ti.com/index.php/CC31xx_Getting_Started_with_WLAN_AP
dflet 0:e89ba455dbcf 46 * doc\examples\getting_started_with_wlan_ap.pdf
dflet 0:e89ba455dbcf 47 */
dflet 0:e89ba455dbcf 48
dflet 0:e89ba455dbcf 49 #include "cc3100_simplelink.h"
dflet 0:e89ba455dbcf 50 #include "cc3100_sl_common.h"
dflet 0:e89ba455dbcf 51
dflet 0:e89ba455dbcf 52 #include "fPtr_func.h"
dflet 3:b89198ac2efe 53 #include "cc3100.h"
dflet 0:e89ba455dbcf 54 #include "cc3100_spi.h"
dflet 0:e89ba455dbcf 55 #include "myBoardInit.h"
dflet 0:e89ba455dbcf 56
dflet 0:e89ba455dbcf 57 using namespace mbed_cc3100;
dflet 0:e89ba455dbcf 58
ArcN00b 8:2acb25effa3a 59 #if (THIS_BOARD == MBED_BOARD_LPC4337)
ArcN00b 8:2acb25effa3a 60 //cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC1768 irq, nHib, cs, mosi, miso, sck
ArcN00b 8:2acb25effa3a 61 cc3100 _cc3100(P2_2, P3_5, P1_5, SPI(P1_4, P1_3, PF_4));//LPC4337 irq, nHib, cs, mosi, miso, sck
ArcN00b 8:2acb25effa3a 62 Serial pc(USBTX, USBRX);//lpc4337
ArcN00b 8:2acb25effa3a 63 #elif (THIS_BOARD == MBED_BOARD_LPC1768)
dflet 0:e89ba455dbcf 64 //cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC1768 irq, nHib, cs, mosi, miso, sck
dflet 0:e89ba455dbcf 65 cc3100 _cc3100(p9, p10, p8, SPI(p11, p12, p13));//LPC1768 irq, nHib, cs, mosi, miso, sck
dflet 0:e89ba455dbcf 66 Serial pc(USBTX, USBRX);//lpc1768
dflet 0:e89ba455dbcf 67 #elif (THIS_BOARD == ST_MBED_NUCLEOF411)
dflet 0:e89ba455dbcf 68 cc3100 _cc3100(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF411 irq, nHib, cs, mosi, miso, sck
dflet 0:e89ba455dbcf 69 Serial pc(SERIAL_TX, SERIAL_RX);//nucleoF411
dflet 0:e89ba455dbcf 70 #elif (THIS_BOARD == ST_MBED_NUCLEOF401)
dflet 0:e89ba455dbcf 71 cc3100 _cc3100(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF401 irq, nHib, cs, mosi, miso, sck
dflet 0:e89ba455dbcf 72 Serial pc(SERIAL_TX, SERIAL_RX);//nucleoF401
dflet 0:e89ba455dbcf 73 #elif (THIS_BOARD == EA_MBED_LPC4088)
dflet 0:e89ba455dbcf 74 cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC4088 irq, nHib, cs, mosi, miso, sck
dflet 0:e89ba455dbcf 75 Serial pc(USBTX, USBRX);//EA_lpc4088
dflet 0:e89ba455dbcf 76 #elif (THIS_BOARD == ST_MBED_NUCLEOF103)
dflet 0:e89ba455dbcf 77 cc3100 _cc3100(PA_9, PC_7, PB_6, SPI(PA_7, PA_6, PA_5));//nucleoF103 irq, nHib, cs, mosi, miso, sck
dflet 0:e89ba455dbcf 78 Serial pc(SERIAL_TX, SERIAL_RX);
dflet 7:0687d16b9781 79 #elif (THIS_BOARD == Seeed_Arch_Max)
dflet 7:0687d16b9781 80 cc3100 _cc3100(PD_12, PD_13, PD_11, SPI(PB_5, PB_4, PB_3));//Seeed_Arch_Max irq, nHib, cs, mosi, miso, sck
dflet 5:d3b320ebd469 81 Serial pc(USBTX, USBRX);
dflet 0:e89ba455dbcf 82 #else
dflet 0:e89ba455dbcf 83
dflet 0:e89ba455dbcf 84 #endif
dflet 0:e89ba455dbcf 85
dflet 5:d3b320ebd469 86 #define APPLICATION_VERSION "1.2.0"
dflet 0:e89ba455dbcf 87
dflet 0:e89ba455dbcf 88 /*
dflet 0:e89ba455dbcf 89 * GLOBAL VARIABLES -- Start
dflet 0:e89ba455dbcf 90 */
ArcN00b 8:2acb25effa3a 91 int32_t accessPoint = 0;
dflet 0:e89ba455dbcf 92
dflet 0:e89ba455dbcf 93 /*
dflet 0:e89ba455dbcf 94 * GLOBAL VARIABLES -- End
dflet 0:e89ba455dbcf 95 */
dflet 0:e89ba455dbcf 96
dflet 0:e89ba455dbcf 97
dflet 0:e89ba455dbcf 98 /*
dflet 0:e89ba455dbcf 99 * STATIC FUNCTION DEFINITIONS -- Start
dflet 0:e89ba455dbcf 100 */
dflet 0:e89ba455dbcf 101
dflet 0:e89ba455dbcf 102 static void displayBanner();
dflet 0:e89ba455dbcf 103 /*
dflet 0:e89ba455dbcf 104 * STATIC FUNCTION DEFINITIONS -- End
dflet 0:e89ba455dbcf 105 */
dflet 0:e89ba455dbcf 106
dflet 0:e89ba455dbcf 107 void station_app(void);
dflet 0:e89ba455dbcf 108 void AP_app(void);
dflet 0:e89ba455dbcf 109
dflet 0:e89ba455dbcf 110 /*
dflet 0:e89ba455dbcf 111 * Application's entry point
dflet 0:e89ba455dbcf 112 */
dflet 0:e89ba455dbcf 113
dflet 0:e89ba455dbcf 114
dflet 0:e89ba455dbcf 115 int main(void) {
dflet 0:e89ba455dbcf 116
dflet 0:e89ba455dbcf 117 pc.baud(115200);
dflet 0:e89ba455dbcf 118
dflet 0:e89ba455dbcf 119 int32_t retVal = -1;
dflet 0:e89ba455dbcf 120
dflet 0:e89ba455dbcf 121 retVal = _cc3100.initializeAppVariables();
dflet 0:e89ba455dbcf 122 ASSERT_ON_ERROR(retVal);
dflet 0:e89ba455dbcf 123
dflet 0:e89ba455dbcf 124 displayBanner();
dflet 0:e89ba455dbcf 125
dflet 0:e89ba455dbcf 126 _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE);
dflet 0:e89ba455dbcf 127 g_PingPacketsRecv = 0;
dflet 3:b89198ac2efe 128
dflet 0:e89ba455dbcf 129 /*
dflet 0:e89ba455dbcf 130 * Following function configures the device to default state by cleaning
dflet 0:e89ba455dbcf 131 * the persistent settings stored in NVMEM (viz. connection profiles &
dflet 0:e89ba455dbcf 132 * policies, power policy etc)
dflet 0:e89ba455dbcf 133 *
dflet 0:e89ba455dbcf 134 * Applications may choose to skip this step if the developer is sure
dflet 0:e89ba455dbcf 135 * that the device is in its default state at start of application
dflet 0:e89ba455dbcf 136 *
dflet 0:e89ba455dbcf 137 * Note that all profiles and persistent settings that were done on the
dflet 0:e89ba455dbcf 138 * device will be lost
dflet 0:e89ba455dbcf 139 */
dflet 0:e89ba455dbcf 140 retVal = _cc3100.configureSimpleLinkToDefaultState();
dflet 0:e89ba455dbcf 141
dflet 0:e89ba455dbcf 142 if(retVal < 0) {
dflet 0:e89ba455dbcf 143 if (DEVICE_NOT_IN_STATION_MODE == retVal)
ArcN00b 8:2acb25effa3a 144 printf(" Impossibile completare la configurazione della CC3100 \n\r");
dflet 0:e89ba455dbcf 145
dflet 0:e89ba455dbcf 146 LOOP_FOREVER();
dflet 0:e89ba455dbcf 147 }
dflet 0:e89ba455dbcf 148
ArcN00b 8:2acb25effa3a 149 printf(" CC3100 configurata nel suo stato di default \n\r");
dflet 0:e89ba455dbcf 150
dflet 0:e89ba455dbcf 151 /*
dflet 0:e89ba455dbcf 152 * Assumption is that the device is configured in station mode already
dflet 0:e89ba455dbcf 153 * and it is in its default state
dflet 0:e89ba455dbcf 154 */
dflet 0:e89ba455dbcf 155 /* Initializing the CC3100 device */
dflet 0:e89ba455dbcf 156
ArcN00b 8:2acb25effa3a 157 if(!accessPoint){
dflet 0:e89ba455dbcf 158 retVal = _cc3100.sl_Start(0, 0, 0);
dflet 0:e89ba455dbcf 159
dflet 0:e89ba455dbcf 160 if ((retVal < 0) || (ROLE_STA != retVal) ){
ArcN00b 8:2acb25effa3a 161 printf(" Impossibile avviare la CC3100 nella modalita' richiesta \n\r");
dflet 0:e89ba455dbcf 162 LOOP_FOREVER();
dflet 0:e89ba455dbcf 163 }
dflet 0:e89ba455dbcf 164
ArcN00b 8:2acb25effa3a 165 printf(" CC3100 avviata come scheda di rete \n\r");
dflet 0:e89ba455dbcf 166 }
dflet 0:e89ba455dbcf 167
ArcN00b 8:2acb25effa3a 168 if(accessPoint == 0 ){
dflet 0:e89ba455dbcf 169 station_app();
dflet 0:e89ba455dbcf 170 }else{
dflet 0:e89ba455dbcf 171 AP_app();
dflet 0:e89ba455dbcf 172 }
dflet 0:e89ba455dbcf 173 return 0;
dflet 0:e89ba455dbcf 174 }
dflet 0:e89ba455dbcf 175
dflet 0:e89ba455dbcf 176 /*!
dflet 0:e89ba455dbcf 177 \brief This function displays the application's banner
dflet 0:e89ba455dbcf 178
dflet 0:e89ba455dbcf 179 \param None
dflet 0:e89ba455dbcf 180
dflet 0:e89ba455dbcf 181 \return None
dflet 0:e89ba455dbcf 182 */
dflet 0:e89ba455dbcf 183 static void displayBanner()
dflet 0:e89ba455dbcf 184 {
ArcN00b 8:2acb25effa3a 185 //int choose;
ArcN00b 8:2acb25effa3a 186
ArcN00b 8:2acb25effa3a 187 if(accessPoint){
dflet 0:e89ba455dbcf 188 printf("\n\r\n\r");
ArcN00b 8:2acb25effa3a 189 printf(" CC3100 in avvio come Access Point ");
dflet 0:e89ba455dbcf 190 printf("\n\r*******************************************************************************\n\r");
ArcN00b 8:2acb25effa3a 191 //scanf("%s", &choose);
ArcN00b 8:2acb25effa3a 192 //printf("\n\r%d\n\r", choose);
dflet 0:e89ba455dbcf 193
dflet 0:e89ba455dbcf 194 }else{
dflet 0:e89ba455dbcf 195 printf("\n\r\n\r");
ArcN00b 8:2acb25effa3a 196 printf(" CC3100 in avvio come scheda di rete ");
dflet 0:e89ba455dbcf 197 printf("\n\r*******************************************************************************\n\r");
dflet 0:e89ba455dbcf 198 }
dflet 0:e89ba455dbcf 199 }
dflet 0:e89ba455dbcf 200
dflet 0:e89ba455dbcf 201 void AP_app(void){
dflet 0:e89ba455dbcf 202
dflet 0:e89ba455dbcf 203 SlPingStartCommand_t PingParams = {0};
dflet 0:e89ba455dbcf 204 SlPingReport_t Report = {0};
dflet 5:d3b320ebd469 205 // SlNetCfgIpV4Args_t ipV4 = {0};
dflet 5:d3b320ebd469 206 // SlNetAppDhcpServerBasicOpt_t dhcpParams = {0};
dflet 0:e89ba455dbcf 207
dflet 0:e89ba455dbcf 208 uint8_t SecType = 0;
dflet 0:e89ba455dbcf 209 int32_t role = ROLE_STA;
dflet 0:e89ba455dbcf 210 int32_t retVal = -1;
dflet 0:e89ba455dbcf 211
dflet 0:e89ba455dbcf 212 /*
dflet 0:e89ba455dbcf 213 * Assumption is that the device is configured in station mode already
dflet 0:e89ba455dbcf 214 * and it is in its default state
dflet 0:e89ba455dbcf 215 */
dflet 0:e89ba455dbcf 216 role = _cc3100.sl_Start(0, 0, 0);
dflet 0:e89ba455dbcf 217 if (ROLE_AP == role) {
dflet 0:e89ba455dbcf 218 /* If the device is in AP mode, we need to wait for this event before doing anything */
dflet 0:e89ba455dbcf 219 while(!_cc3100.IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED)) {
dflet 3:b89198ac2efe 220 _cc3100._nonos._SlNonOsMainLoopTask();
dflet 0:e89ba455dbcf 221 }
dflet 0:e89ba455dbcf 222 } else {
dflet 0:e89ba455dbcf 223 /* Configure CC3100 to start in AP mode */
dflet 3:b89198ac2efe 224 retVal = _cc3100._wlan.sl_WlanSetMode(ROLE_AP);
dflet 0:e89ba455dbcf 225 if(retVal < 0)
dflet 0:e89ba455dbcf 226 LOOP_FOREVER();
dflet 5:d3b320ebd469 227
dflet 5:d3b320ebd469 228 /* Configure the SSID of the CC3100 */
dflet 5:d3b320ebd469 229 retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID,
dflet 5:d3b320ebd469 230 strlen(SSID_AP_MODE), (uint8_t *)SSID_AP_MODE);
dflet 5:d3b320ebd469 231 if(retVal < 0)
dflet 5:d3b320ebd469 232 LOOP_FOREVER();
dflet 5:d3b320ebd469 233
dflet 5:d3b320ebd469 234 SecType = SEC_TYPE_AP_MODE;
dflet 5:d3b320ebd469 235 /* Configure the Security parameter the AP mode */
dflet 5:d3b320ebd469 236 retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, (uint8_t *)&SecType);
dflet 5:d3b320ebd469 237 if(retVal < 0)
dflet 5:d3b320ebd469 238 LOOP_FOREVER();
dflet 5:d3b320ebd469 239
dflet 5:d3b320ebd469 240 retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(PASSWORD_AP_MODE), (uint8_t *)PASSWORD_AP_MODE);
dflet 5:d3b320ebd469 241 if(retVal < 0)
dflet 5:d3b320ebd469 242 LOOP_FOREVER();
dflet 0:e89ba455dbcf 243
dflet 0:e89ba455dbcf 244 retVal = _cc3100.sl_Stop(SL_STOP_TIMEOUT);
dflet 0:e89ba455dbcf 245 if(retVal < 0)
dflet 0:e89ba455dbcf 246 LOOP_FOREVER();
dflet 0:e89ba455dbcf 247
dflet 0:e89ba455dbcf 248 _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_IP_ACQUIRED);
dflet 0:e89ba455dbcf 249
dflet 0:e89ba455dbcf 250 role = _cc3100.sl_Start(0, 0, 0);
dflet 0:e89ba455dbcf 251 if (ROLE_AP == role) {
dflet 0:e89ba455dbcf 252 /* If the device is in AP mode, we need to wait for this event before doing anything */
dflet 0:e89ba455dbcf 253 while(!_cc3100.IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED)) {
dflet 3:b89198ac2efe 254 _cc3100._nonos._SlNonOsMainLoopTask();
dflet 0:e89ba455dbcf 255 }
dflet 0:e89ba455dbcf 256 } else {
dflet 0:e89ba455dbcf 257 printf(" Device couldn't be configured in AP mode \n\r");
dflet 0:e89ba455dbcf 258 LOOP_FOREVER();
dflet 0:e89ba455dbcf 259 }
dflet 0:e89ba455dbcf 260 }
dflet 0:e89ba455dbcf 261 printf(" Device started as Access Point\n\r");
dflet 0:e89ba455dbcf 262
dflet 0:e89ba455dbcf 263 /* Wait */
dflet 0:e89ba455dbcf 264 printf(" Waiting for clients to connect...!\n\r");
dflet 0:e89ba455dbcf 265 while((!_cc3100.IS_IP_LEASED(g_Status,STATUS_BIT_IP_LEASED)) || (!_cc3100.IS_STA_CONNECTED(g_Status,STATUS_BIT_STA_CONNECTED))) {
dflet 3:b89198ac2efe 266 _cc3100._nonos._SlNonOsMainLoopTask();
dflet 0:e89ba455dbcf 267 }
dflet 0:e89ba455dbcf 268 printf(" Client connected to the device \n\r");
dflet 0:e89ba455dbcf 269 printf(" Pinging...! \n\r");
dflet 0:e89ba455dbcf 270
dflet 0:e89ba455dbcf 271 /* Set the ping parameters */
dflet 0:e89ba455dbcf 272 PingParams.PingIntervalTime = PING_INTERVAL;
dflet 0:e89ba455dbcf 273 PingParams.PingSize = PING_SIZE;
dflet 0:e89ba455dbcf 274 PingParams.PingRequestTimeout = PING_TIMEOUT;
dflet 0:e89ba455dbcf 275 PingParams.TotalNumberOfAttempts = PING_ATTEMPTS;
dflet 0:e89ba455dbcf 276 PingParams.Flags = 0;
dflet 0:e89ba455dbcf 277 PingParams.Ip = g_StationIP; /* Fill the station IP address connected to CC3100 */
dflet 0:e89ba455dbcf 278
dflet 0:e89ba455dbcf 279 /* Ping client connected to CC3100 */
dflet 3:b89198ac2efe 280 retVal = _cc3100._netapp.sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&Report, &SimpleLinkPingReport);
dflet 0:e89ba455dbcf 281 if(retVal < 0)
dflet 0:e89ba455dbcf 282 LOOP_FOREVER();
dflet 0:e89ba455dbcf 283
dflet 0:e89ba455dbcf 284 /* Wait */
dflet 0:e89ba455dbcf 285 while(!_cc3100.IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) {
dflet 3:b89198ac2efe 286 _cc3100._nonos._SlNonOsMainLoopTask();
dflet 0:e89ba455dbcf 287 }
dflet 0:e89ba455dbcf 288
dflet 0:e89ba455dbcf 289 if (0 == g_PingPacketsRecv) {
dflet 0:e89ba455dbcf 290 printf(" A STATION couldn't connect to the device \n\r");
dflet 0:e89ba455dbcf 291 //ASSERT_ON_ERROR(LAN_CONNECTION_FAILED);
dflet 0:e89ba455dbcf 292 printf(" ERROR code %d\n\r", LAN_CONNECTION_FAILED);
dflet 0:e89ba455dbcf 293 }
dflet 0:e89ba455dbcf 294
dflet 0:e89ba455dbcf 295 printf(" Device and the station are successfully connected \n\r");
dflet 0:e89ba455dbcf 296 //return SUCCESS;
dflet 0:e89ba455dbcf 297 }
dflet 0:e89ba455dbcf 298
ArcN00b 8:2acb25effa3a 299 int32_t pingToHost(char* host) {
ArcN00b 8:2acb25effa3a 300
ArcN00b 8:2acb25effa3a 301 SlPingStartCommand_t pingParams = {0};
ArcN00b 8:2acb25effa3a 302 SlPingReport_t pingReport = {0};
ArcN00b 8:2acb25effa3a 303
ArcN00b 8:2acb25effa3a 304 uint32_t ipAddr = 0;
ArcN00b 8:2acb25effa3a 305
ArcN00b 8:2acb25effa3a 306 int32_t retVal = -1;
ArcN00b 8:2acb25effa3a 307
ArcN00b 8:2acb25effa3a 308 _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE);
ArcN00b 8:2acb25effa3a 309 g_PingPacketsRecv = 0;
ArcN00b 8:2acb25effa3a 310
ArcN00b 8:2acb25effa3a 311 /* Set the ping parameters */
ArcN00b 8:2acb25effa3a 312 pingParams.PingIntervalTime = PING_INTERVAL;
ArcN00b 8:2acb25effa3a 313 pingParams.PingSize = PING_PKT_SIZE;
ArcN00b 8:2acb25effa3a 314 pingParams.PingRequestTimeout = PING_TIMEOUT;
ArcN00b 8:2acb25effa3a 315 pingParams.TotalNumberOfAttempts = PING_ATTEMPTS;
ArcN00b 8:2acb25effa3a 316 pingParams.Flags = 0;
ArcN00b 8:2acb25effa3a 317 pingParams.Ip = g_GatewayIP;
ArcN00b 8:2acb25effa3a 318
ArcN00b 8:2acb25effa3a 319 /* Check for Internet connection */
ArcN00b 8:2acb25effa3a 320 retVal = _cc3100._netapp.sl_NetAppDnsGetHostByName((unsigned char *)host, strlen(host), &ipAddr, SL_AF_INET);
ArcN00b 8:2acb25effa3a 321 if(retVal < 0) {
ArcN00b 8:2acb25effa3a 322 printf(" Impossibile trovare l'host \n\r");
ArcN00b 8:2acb25effa3a 323 return retVal;
ArcN00b 8:2acb25effa3a 324 } else
ArcN00b 8:2acb25effa3a 325 printf(" Avvio ping\n");
ArcN00b 8:2acb25effa3a 326
ArcN00b 8:2acb25effa3a 327 /* Replace the ping address to match HOST_NAME's IP address */
ArcN00b 8:2acb25effa3a 328 pingParams.Ip = ipAddr;
ArcN00b 8:2acb25effa3a 329
ArcN00b 8:2acb25effa3a 330 /* Try to ping HOST_NAME */
ArcN00b 8:2acb25effa3a 331 retVal = _cc3100._netapp.sl_NetAppPingStart( (SlPingStartCommand_t*)&pingParams, SL_AF_INET,
ArcN00b 8:2acb25effa3a 332 (SlPingReport_t*)&pingReport, SimpleLinkPingReport);
ArcN00b 8:2acb25effa3a 333 if(retVal < 0) {
ArcN00b 8:2acb25effa3a 334 printf(" Impossibile connettersi all'host \n\r");
ArcN00b 8:2acb25effa3a 335 return retVal;
ArcN00b 8:2acb25effa3a 336 }
ArcN00b 8:2acb25effa3a 337
ArcN00b 8:2acb25effa3a 338 /* Wait */
ArcN00b 8:2acb25effa3a 339 while(!_cc3100.IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) { _cc3100._nonos._SlNonOsMainLoopTask(); }
ArcN00b 8:2acb25effa3a 340
ArcN00b 8:2acb25effa3a 341 if (0 == g_PingPacketsRecv) {
ArcN00b 8:2acb25effa3a 342 printf(" Il test non e' andato a buon fine \n\r");
ArcN00b 8:2acb25effa3a 343 return retVal;
ArcN00b 8:2acb25effa3a 344 } else {
ArcN00b 8:2acb25effa3a 345
ArcN00b 8:2acb25effa3a 346 printf("\n\r Pacchetti inviati -> %d \n\r Pacchetti ricevuti -> %d \n\r", pingReport.PacketsSent, pingReport.PacketsReceived);
ArcN00b 8:2acb25effa3a 347 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);
ArcN00b 8:2acb25effa3a 348 }
ArcN00b 8:2acb25effa3a 349
ArcN00b 8:2acb25effa3a 350 return 0;
ArcN00b 8:2acb25effa3a 351
ArcN00b 8:2acb25effa3a 352 }
ArcN00b 8:2acb25effa3a 353
ArcN00b 8:2acb25effa3a 354 void station_app(void) {
dflet 0:e89ba455dbcf 355
dflet 0:e89ba455dbcf 356 int32_t retVal = -1;
ArcN00b 8:2acb25effa3a 357 char host[100];
dflet 5:d3b320ebd469 358
dflet 0:e89ba455dbcf 359 /* Connecting to WLAN AP */
dflet 0:e89ba455dbcf 360 retVal = _cc3100.establishConnectionWithAP();
dflet 0:e89ba455dbcf 361 if(retVal < 0)
dflet 0:e89ba455dbcf 362 {
ArcN00b 8:2acb25effa3a 363 printf(" Impossibile connettersi \n\r");
dflet 0:e89ba455dbcf 364 LOOP_FOREVER();
dflet 5:d3b320ebd469 365 }
ArcN00b 8:2acb25effa3a 366 printf(" Connessione stabilita con l'Access Point \n\r");
dflet 5:d3b320ebd469 367
ArcN00b 8:2acb25effa3a 368 printf(" Test di connettivita' in corso... \n\r");
dflet 0:e89ba455dbcf 369 retVal = _cc3100.checkLanConnection();
ArcN00b 8:2acb25effa3a 370 if(retVal < 0) {
dflet 0:e89ba455dbcf 371 printf(" Device couldn't connect to LAN \n\r");
dflet 0:e89ba455dbcf 372 LOOP_FOREVER();
dflet 0:e89ba455dbcf 373 }
ArcN00b 8:2acb25effa3a 374 printf(" Connesso alla rete locale con successo \r\n");
dflet 5:d3b320ebd469 375
dflet 0:e89ba455dbcf 376 retVal = _cc3100.checkInternetConnection();
dflet 0:e89ba455dbcf 377 if(retVal < 0)
dflet 0:e89ba455dbcf 378 {
dflet 0:e89ba455dbcf 379 printf(" Device couldn't connect to the internet \n\r");
dflet 0:e89ba455dbcf 380 LOOP_FOREVER();
dflet 0:e89ba455dbcf 381 }
dflet 0:e89ba455dbcf 382
ArcN00b 8:2acb25effa3a 383 printf(" Connesso ad internet con successo");
ArcN00b 8:2acb25effa3a 384
ArcN00b 8:2acb25effa3a 385 // Esegue ping finchè connesso
ArcN00b 8:2acb25effa3a 386 while(1) {
ArcN00b 8:2acb25effa3a 387 printf(" \n\rInserire un host a cui fare il ping \n\r");
ArcN00b 8:2acb25effa3a 388 scanf("%s", host);
ArcN00b 8:2acb25effa3a 389
ArcN00b 8:2acb25effa3a 390 if(pingToHost(host) == 0)
ArcN00b 8:2acb25effa3a 391 printf("\nTest completato senza errori\n\r");
ArcN00b 8:2acb25effa3a 392
ArcN00b 8:2acb25effa3a 393 }
dflet 0:e89ba455dbcf 394 }