Simple LED control project using CC3100 as Access Point and socket

Dependencies:   mbed

Fork of cc3100_Test_Demo by David Fletcher

Committer:
ArcN00b
Date:
Fri May 26 07:25:06 2017 +0000
Revision:
9:30d6c10d4eab
Parent:
8:2acb25effa3a
Child:
10:d24deb1afd1b
Utilizzo del socket per colorare di blu il led con successo

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
ArcN00b 9:30d6c10d4eab 49 #include "mbed.h"
dflet 0:e89ba455dbcf 50 #include "cc3100_simplelink.h"
dflet 0:e89ba455dbcf 51 #include "cc3100_sl_common.h"
dflet 0:e89ba455dbcf 52
dflet 0:e89ba455dbcf 53 #include "fPtr_func.h"
dflet 3:b89198ac2efe 54 #include "cc3100.h"
dflet 0:e89ba455dbcf 55 #include "cc3100_spi.h"
dflet 0:e89ba455dbcf 56 #include "myBoardInit.h"
ArcN00b 9:30d6c10d4eab 57 #include <string.h>
dflet 0:e89ba455dbcf 58
dflet 0:e89ba455dbcf 59 using namespace mbed_cc3100;
dflet 0:e89ba455dbcf 60
ArcN00b 8:2acb25effa3a 61 #if (THIS_BOARD == MBED_BOARD_LPC4337)
ArcN00b 8:2acb25effa3a 62 //cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC1768 irq, nHib, cs, mosi, miso, sck
ArcN00b 8:2acb25effa3a 63 cc3100 _cc3100(P2_2, P3_5, P1_5, SPI(P1_4, P1_3, PF_4));//LPC4337 irq, nHib, cs, mosi, miso, sck
ArcN00b 9:30d6c10d4eab 64 //cc3100_socket _csk(_c3100.;
ArcN00b 8:2acb25effa3a 65 Serial pc(USBTX, USBRX);//lpc4337
ArcN00b 8:2acb25effa3a 66 #elif (THIS_BOARD == MBED_BOARD_LPC1768)
dflet 0:e89ba455dbcf 67 //cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC1768 irq, nHib, cs, mosi, miso, sck
dflet 0:e89ba455dbcf 68 cc3100 _cc3100(p9, p10, p8, SPI(p11, p12, p13));//LPC1768 irq, nHib, cs, mosi, miso, sck
dflet 0:e89ba455dbcf 69 Serial pc(USBTX, USBRX);//lpc1768
dflet 0:e89ba455dbcf 70 #elif (THIS_BOARD == ST_MBED_NUCLEOF411)
dflet 0:e89ba455dbcf 71 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 72 Serial pc(SERIAL_TX, SERIAL_RX);//nucleoF411
dflet 0:e89ba455dbcf 73 #elif (THIS_BOARD == ST_MBED_NUCLEOF401)
dflet 0:e89ba455dbcf 74 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 75 Serial pc(SERIAL_TX, SERIAL_RX);//nucleoF401
dflet 0:e89ba455dbcf 76 #elif (THIS_BOARD == EA_MBED_LPC4088)
dflet 0:e89ba455dbcf 77 cc3100 _cc3100(p9, p10, p8, SPI(p5, p6, p7));//LPC4088 irq, nHib, cs, mosi, miso, sck
dflet 0:e89ba455dbcf 78 Serial pc(USBTX, USBRX);//EA_lpc4088
dflet 0:e89ba455dbcf 79 #elif (THIS_BOARD == ST_MBED_NUCLEOF103)
dflet 0:e89ba455dbcf 80 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 81 Serial pc(SERIAL_TX, SERIAL_RX);
dflet 7:0687d16b9781 82 #elif (THIS_BOARD == Seeed_Arch_Max)
dflet 7:0687d16b9781 83 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 84 Serial pc(USBTX, USBRX);
dflet 0:e89ba455dbcf 85 #else
dflet 0:e89ba455dbcf 86 #endif
dflet 0:e89ba455dbcf 87
ArcN00b 9:30d6c10d4eab 88 /* Indirizzo IP del server, bisogna usare il formato long esadecimale
ArcN00b 9:30d6c10d4eab 89 * E.g: 0xc0a8010a == 192.168.1.1 */
ArcN00b 9:30d6c10d4eab 90 #define IP_ADDR 0xc0a80101
ArcN00b 9:30d6c10d4eab 91 #define PORT_NUM 5000
dflet 0:e89ba455dbcf 92
ArcN00b 9:30d6c10d4eab 93 #define BUF_SIZE 1400
ArcN00b 9:30d6c10d4eab 94 #define NO_OF_PACKETS 1000
dflet 0:e89ba455dbcf 95 /*
dflet 0:e89ba455dbcf 96 * GLOBAL VARIABLES -- Start
dflet 0:e89ba455dbcf 97 */
ArcN00b 9:30d6c10d4eab 98 int32_t accessPoint = 1;
dflet 0:e89ba455dbcf 99
dflet 0:e89ba455dbcf 100 /*
dflet 0:e89ba455dbcf 101 * GLOBAL VARIABLES -- End
dflet 0:e89ba455dbcf 102 */
dflet 0:e89ba455dbcf 103
dflet 0:e89ba455dbcf 104
dflet 0:e89ba455dbcf 105 /*
dflet 0:e89ba455dbcf 106 * STATIC FUNCTION DEFINITIONS -- Start
dflet 0:e89ba455dbcf 107 */
dflet 0:e89ba455dbcf 108
dflet 0:e89ba455dbcf 109 static void displayBanner();
dflet 0:e89ba455dbcf 110 /*
dflet 0:e89ba455dbcf 111 * STATIC FUNCTION DEFINITIONS -- End
dflet 0:e89ba455dbcf 112 */
dflet 0:e89ba455dbcf 113
dflet 0:e89ba455dbcf 114 void station_app(void);
dflet 0:e89ba455dbcf 115 void AP_app(void);
dflet 0:e89ba455dbcf 116
ArcN00b 9:30d6c10d4eab 117 DigitalOut myled1(LED1);//Azzurro
ArcN00b 9:30d6c10d4eab 118 DigitalOut myled2(LED2);//Giallo
ArcN00b 9:30d6c10d4eab 119 DigitalOut myled3(LED3);//Viola
ArcN00b 9:30d6c10d4eab 120
ArcN00b 9:30d6c10d4eab 121 void white() {
ArcN00b 9:30d6c10d4eab 122 myled1 = 0;
ArcN00b 9:30d6c10d4eab 123 myled2 = 0;
ArcN00b 9:30d6c10d4eab 124 myled3 = 0;
ArcN00b 9:30d6c10d4eab 125 }
ArcN00b 9:30d6c10d4eab 126
ArcN00b 9:30d6c10d4eab 127 void ledShow() {
ArcN00b 9:30d6c10d4eab 128 while(1) {
ArcN00b 9:30d6c10d4eab 129 myled1 = 1;
ArcN00b 9:30d6c10d4eab 130 myled2 = 1;
ArcN00b 9:30d6c10d4eab 131 myled3 = 1;
ArcN00b 9:30d6c10d4eab 132 wait(5);
ArcN00b 9:30d6c10d4eab 133 myled1 = 0;
ArcN00b 9:30d6c10d4eab 134 wait(5);
ArcN00b 9:30d6c10d4eab 135 myled1 = 1;
ArcN00b 9:30d6c10d4eab 136 myled2 = 0;
ArcN00b 9:30d6c10d4eab 137 wait(5);
ArcN00b 9:30d6c10d4eab 138 myled2 = 1;
ArcN00b 9:30d6c10d4eab 139 myled3 = 0;
ArcN00b 9:30d6c10d4eab 140 wait(5);
ArcN00b 9:30d6c10d4eab 141 myled2 = 0;
ArcN00b 9:30d6c10d4eab 142 wait(5);
ArcN00b 9:30d6c10d4eab 143 myled1 = 0;
ArcN00b 9:30d6c10d4eab 144 myled2 = 1;
ArcN00b 9:30d6c10d4eab 145 wait(5);
ArcN00b 9:30d6c10d4eab 146 myled2 = 0;
ArcN00b 9:30d6c10d4eab 147 myled3 = 1;
ArcN00b 9:30d6c10d4eab 148 wait(5);
ArcN00b 9:30d6c10d4eab 149 myled3 = 0;
ArcN00b 9:30d6c10d4eab 150 wait(5);
ArcN00b 9:30d6c10d4eab 151 }
ArcN00b 9:30d6c10d4eab 152 }
dflet 0:e89ba455dbcf 153
dflet 0:e89ba455dbcf 154 int main(void) {
dflet 0:e89ba455dbcf 155
ArcN00b 9:30d6c10d4eab 156 myled1 = 1;
ArcN00b 9:30d6c10d4eab 157 myled2 = 1;
ArcN00b 9:30d6c10d4eab 158 myled3 = 1;
dflet 0:e89ba455dbcf 159 pc.baud(115200);
dflet 0:e89ba455dbcf 160
dflet 0:e89ba455dbcf 161 int32_t retVal = -1;
dflet 0:e89ba455dbcf 162
dflet 0:e89ba455dbcf 163 retVal = _cc3100.initializeAppVariables();
dflet 0:e89ba455dbcf 164 ASSERT_ON_ERROR(retVal);
dflet 0:e89ba455dbcf 165
dflet 0:e89ba455dbcf 166 displayBanner();
dflet 0:e89ba455dbcf 167
dflet 0:e89ba455dbcf 168 _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE);
dflet 0:e89ba455dbcf 169 g_PingPacketsRecv = 0;
dflet 3:b89198ac2efe 170
dflet 0:e89ba455dbcf 171 /*
dflet 0:e89ba455dbcf 172 * Following function configures the device to default state by cleaning
dflet 0:e89ba455dbcf 173 * the persistent settings stored in NVMEM (viz. connection profiles &
dflet 0:e89ba455dbcf 174 * policies, power policy etc)
dflet 0:e89ba455dbcf 175 *
dflet 0:e89ba455dbcf 176 * Applications may choose to skip this step if the developer is sure
dflet 0:e89ba455dbcf 177 * that the device is in its default state at start of application
dflet 0:e89ba455dbcf 178 *
dflet 0:e89ba455dbcf 179 * Note that all profiles and persistent settings that were done on the
dflet 0:e89ba455dbcf 180 * device will be lost
dflet 0:e89ba455dbcf 181 */
dflet 0:e89ba455dbcf 182 retVal = _cc3100.configureSimpleLinkToDefaultState();
dflet 0:e89ba455dbcf 183
dflet 0:e89ba455dbcf 184 if(retVal < 0) {
dflet 0:e89ba455dbcf 185 if (DEVICE_NOT_IN_STATION_MODE == retVal)
ArcN00b 8:2acb25effa3a 186 printf(" Impossibile completare la configurazione della CC3100 \n\r");
dflet 0:e89ba455dbcf 187
dflet 0:e89ba455dbcf 188 LOOP_FOREVER();
dflet 0:e89ba455dbcf 189 }
dflet 0:e89ba455dbcf 190
ArcN00b 8:2acb25effa3a 191 printf(" CC3100 configurata nel suo stato di default \n\r");
dflet 0:e89ba455dbcf 192
dflet 0:e89ba455dbcf 193 /*
dflet 0:e89ba455dbcf 194 * Assumption is that the device is configured in station mode already
dflet 0:e89ba455dbcf 195 * and it is in its default state
dflet 0:e89ba455dbcf 196 */
dflet 0:e89ba455dbcf 197 /* Initializing the CC3100 device */
dflet 0:e89ba455dbcf 198
ArcN00b 8:2acb25effa3a 199 if(!accessPoint){
dflet 0:e89ba455dbcf 200 retVal = _cc3100.sl_Start(0, 0, 0);
dflet 0:e89ba455dbcf 201
dflet 0:e89ba455dbcf 202 if ((retVal < 0) || (ROLE_STA != retVal) ){
ArcN00b 8:2acb25effa3a 203 printf(" Impossibile avviare la CC3100 nella modalita' richiesta \n\r");
dflet 0:e89ba455dbcf 204 LOOP_FOREVER();
dflet 0:e89ba455dbcf 205 }
dflet 0:e89ba455dbcf 206
ArcN00b 8:2acb25effa3a 207 printf(" CC3100 avviata come scheda di rete \n\r");
dflet 0:e89ba455dbcf 208 }
dflet 0:e89ba455dbcf 209
ArcN00b 8:2acb25effa3a 210 if(accessPoint == 0 ){
dflet 0:e89ba455dbcf 211 station_app();
dflet 0:e89ba455dbcf 212 }else{
dflet 0:e89ba455dbcf 213 AP_app();
dflet 0:e89ba455dbcf 214 }
dflet 0:e89ba455dbcf 215 return 0;
dflet 0:e89ba455dbcf 216 }
dflet 0:e89ba455dbcf 217
dflet 0:e89ba455dbcf 218 /*!
dflet 0:e89ba455dbcf 219 \brief This function displays the application's banner
dflet 0:e89ba455dbcf 220
dflet 0:e89ba455dbcf 221 \param None
dflet 0:e89ba455dbcf 222
dflet 0:e89ba455dbcf 223 \return None
dflet 0:e89ba455dbcf 224 */
dflet 0:e89ba455dbcf 225 static void displayBanner()
dflet 0:e89ba455dbcf 226 {
ArcN00b 8:2acb25effa3a 227 //int choose;
ArcN00b 8:2acb25effa3a 228
ArcN00b 8:2acb25effa3a 229 if(accessPoint){
dflet 0:e89ba455dbcf 230 printf("\n\r\n\r");
ArcN00b 8:2acb25effa3a 231 printf(" CC3100 in avvio come Access Point ");
dflet 0:e89ba455dbcf 232 printf("\n\r*******************************************************************************\n\r");
ArcN00b 8:2acb25effa3a 233 //scanf("%s", &choose);
ArcN00b 8:2acb25effa3a 234 //printf("\n\r%d\n\r", choose);
dflet 0:e89ba455dbcf 235
dflet 0:e89ba455dbcf 236 }else{
dflet 0:e89ba455dbcf 237 printf("\n\r\n\r");
ArcN00b 8:2acb25effa3a 238 printf(" CC3100 in avvio come scheda di rete ");
dflet 0:e89ba455dbcf 239 printf("\n\r*******************************************************************************\n\r");
dflet 0:e89ba455dbcf 240 }
dflet 0:e89ba455dbcf 241 }
dflet 0:e89ba455dbcf 242
ArcN00b 9:30d6c10d4eab 243 int32_t TcpServer(uint16_t Port)
ArcN00b 9:30d6c10d4eab 244 {
ArcN00b 9:30d6c10d4eab 245 SlSockAddrIn_t Addr;
ArcN00b 9:30d6c10d4eab 246 SlSockAddrIn_t LocalAddr;
ArcN00b 9:30d6c10d4eab 247 char buf[100], tmp[100];
ArcN00b 9:30d6c10d4eab 248 int i, len;
ArcN00b 9:30d6c10d4eab 249
ArcN00b 9:30d6c10d4eab 250 int16_t AddrSize = 0;
ArcN00b 9:30d6c10d4eab 251 int16_t SockID = 0;
ArcN00b 9:30d6c10d4eab 252 int32_t Status = 0;
ArcN00b 9:30d6c10d4eab 253 int16_t newSockID = 0;
ArcN00b 9:30d6c10d4eab 254 uint16_t LoopCount = 0;
ArcN00b 9:30d6c10d4eab 255 int16_t recvSize = 0;
ArcN00b 9:30d6c10d4eab 256
ArcN00b 9:30d6c10d4eab 257 LocalAddr.sin_family = SL_AF_INET;
ArcN00b 9:30d6c10d4eab 258 LocalAddr.sin_port = _cc3100._socket.sl_Htons((int16_t)Port);
ArcN00b 9:30d6c10d4eab 259 LocalAddr.sin_addr.s_addr = 0;
ArcN00b 9:30d6c10d4eab 260
ArcN00b 9:30d6c10d4eab 261 SockID = _cc3100._socket.sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
ArcN00b 9:30d6c10d4eab 262 if( SockID < 0 )
ArcN00b 9:30d6c10d4eab 263 {
ArcN00b 9:30d6c10d4eab 264 printf(" Errore durante la creazione del socket \n\r");
ArcN00b 9:30d6c10d4eab 265 ASSERT_ON_ERROR(SockID);
ArcN00b 9:30d6c10d4eab 266 }
ArcN00b 9:30d6c10d4eab 267
ArcN00b 9:30d6c10d4eab 268 AddrSize = sizeof(SlSockAddrIn_t);
ArcN00b 9:30d6c10d4eab 269 Status = _cc3100._socket.sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize);
ArcN00b 9:30d6c10d4eab 270 if( Status < 0 )
ArcN00b 9:30d6c10d4eab 271 {
ArcN00b 9:30d6c10d4eab 272 _cc3100._socket.sl_Close(SockID);
ArcN00b 9:30d6c10d4eab 273 printf(" Errore durante il bind della porta \n\r");
ArcN00b 9:30d6c10d4eab 274 ASSERT_ON_ERROR(Status);
ArcN00b 9:30d6c10d4eab 275 }
ArcN00b 9:30d6c10d4eab 276
ArcN00b 9:30d6c10d4eab 277 Status = _cc3100._socket.sl_Listen(SockID, 0);
ArcN00b 9:30d6c10d4eab 278 if( Status < 0 )
ArcN00b 9:30d6c10d4eab 279 {
ArcN00b 9:30d6c10d4eab 280 _cc3100._socket.sl_Close(SockID);
ArcN00b 9:30d6c10d4eab 281 printf(" Errore durante la listen \n\r");
ArcN00b 9:30d6c10d4eab 282 ASSERT_ON_ERROR(Status);
ArcN00b 9:30d6c10d4eab 283 }
ArcN00b 9:30d6c10d4eab 284
ArcN00b 9:30d6c10d4eab 285 printf("Socket in attesa di connessioni \n\r");
ArcN00b 9:30d6c10d4eab 286
ArcN00b 9:30d6c10d4eab 287 newSockID = _cc3100._socket.sl_Accept(SockID, (SlSockAddr_t *)&Addr,
ArcN00b 9:30d6c10d4eab 288 (SlSocklen_t*)&AddrSize);
ArcN00b 9:30d6c10d4eab 289 if( newSockID < 0 )
ArcN00b 9:30d6c10d4eab 290 {
ArcN00b 9:30d6c10d4eab 291 _cc3100._socket.sl_Close(SockID);
ArcN00b 9:30d6c10d4eab 292 printf(" Errore durante la accept \n\r");
ArcN00b 9:30d6c10d4eab 293 ASSERT_ON_ERROR(newSockID);
ArcN00b 9:30d6c10d4eab 294 }
ArcN00b 9:30d6c10d4eab 295
ArcN00b 9:30d6c10d4eab 296 printf("Connessione accettata\n\r");
ArcN00b 9:30d6c10d4eab 297
ArcN00b 9:30d6c10d4eab 298 while (LoopCount < NO_OF_PACKETS)
ArcN00b 9:30d6c10d4eab 299 {
ArcN00b 9:30d6c10d4eab 300 recvSize = BUF_SIZE;
ArcN00b 9:30d6c10d4eab 301 do
ArcN00b 9:30d6c10d4eab 302 {
ArcN00b 9:30d6c10d4eab 303 Status = _cc3100._socket.sl_Recv(newSockID, &(buf), recvSize, 0);
ArcN00b 9:30d6c10d4eab 304 if( Status <= 0 )
ArcN00b 9:30d6c10d4eab 305 {
ArcN00b 9:30d6c10d4eab 306 _cc3100._socket.sl_Close(newSockID);
ArcN00b 9:30d6c10d4eab 307 _cc3100._socket.sl_Close(SockID);
ArcN00b 9:30d6c10d4eab 308 printf(" Errore in ricezione \n\r");
ArcN00b 9:30d6c10d4eab 309 ASSERT_ON_ERROR(TCP_RECV_ERROR);
ArcN00b 9:30d6c10d4eab 310 } else {
ArcN00b 9:30d6c10d4eab 311 printf("\n\r Ricevuto -> %s %d \n\r", buf, strlen(buf));
ArcN00b 9:30d6c10d4eab 312
ArcN00b 9:30d6c10d4eab 313 // Rimuovo i caratteri strani
ArcN00b 9:30d6c10d4eab 314 for(i = 0; i < strlen(buf); i++) {
ArcN00b 9:30d6c10d4eab 315 if((buf[i] >= 65 && buf[i] <= 90) || (buf[i] >= 97 && buf[i] <= 122)){
ArcN00b 9:30d6c10d4eab 316 printf("%c ", buf[i]);
ArcN00b 9:30d6c10d4eab 317 len = i + 1;
ArcN00b 9:30d6c10d4eab 318 }
ArcN00b 9:30d6c10d4eab 319 }
ArcN00b 9:30d6c10d4eab 320 buf[len] = '\0';
ArcN00b 9:30d6c10d4eab 321
ArcN00b 9:30d6c10d4eab 322 printf("%s %d \n\r", buf, strlen(buf));
ArcN00b 9:30d6c10d4eab 323 sprintf(tmp, "Ricevuto -> %s \n\r", buf);
ArcN00b 9:30d6c10d4eab 324 _cc3100._socket.sl_Send(newSockID, &(tmp), strlen(tmp),0);
ArcN00b 9:30d6c10d4eab 325
ArcN00b 9:30d6c10d4eab 326 if(strcmp(buf,"Blu") != 0 || strcmp(buf,"blu") != 0) {
ArcN00b 9:30d6c10d4eab 327 white();
ArcN00b 9:30d6c10d4eab 328 myled1 = 1;
ArcN00b 9:30d6c10d4eab 329 myled3 = 1;
ArcN00b 9:30d6c10d4eab 330 }
ArcN00b 9:30d6c10d4eab 331 }
ArcN00b 9:30d6c10d4eab 332
ArcN00b 9:30d6c10d4eab 333 recvSize -= Status;
ArcN00b 9:30d6c10d4eab 334 }
ArcN00b 9:30d6c10d4eab 335 while(recvSize > 0);
ArcN00b 9:30d6c10d4eab 336
ArcN00b 9:30d6c10d4eab 337 LoopCount++;
ArcN00b 9:30d6c10d4eab 338 }
ArcN00b 9:30d6c10d4eab 339
ArcN00b 9:30d6c10d4eab 340 Status = _cc3100._socket.sl_Close(newSockID);
ArcN00b 9:30d6c10d4eab 341 ASSERT_ON_ERROR(Status);
ArcN00b 9:30d6c10d4eab 342
ArcN00b 9:30d6c10d4eab 343 Status = _cc3100._socket.sl_Close(SockID);
ArcN00b 9:30d6c10d4eab 344 ASSERT_ON_ERROR(Status);
ArcN00b 9:30d6c10d4eab 345
ArcN00b 9:30d6c10d4eab 346 return SUCCESS;
ArcN00b 9:30d6c10d4eab 347 }
ArcN00b 9:30d6c10d4eab 348
dflet 0:e89ba455dbcf 349 void AP_app(void){
dflet 0:e89ba455dbcf 350
dflet 0:e89ba455dbcf 351 SlPingStartCommand_t PingParams = {0};
dflet 0:e89ba455dbcf 352 SlPingReport_t Report = {0};
dflet 5:d3b320ebd469 353 // SlNetCfgIpV4Args_t ipV4 = {0};
dflet 5:d3b320ebd469 354 // SlNetAppDhcpServerBasicOpt_t dhcpParams = {0};
dflet 0:e89ba455dbcf 355
dflet 0:e89ba455dbcf 356 uint8_t SecType = 0;
dflet 0:e89ba455dbcf 357 int32_t role = ROLE_STA;
dflet 0:e89ba455dbcf 358 int32_t retVal = -1;
dflet 0:e89ba455dbcf 359
dflet 0:e89ba455dbcf 360 /*
dflet 0:e89ba455dbcf 361 * Assumption is that the device is configured in station mode already
dflet 0:e89ba455dbcf 362 * and it is in its default state
dflet 0:e89ba455dbcf 363 */
dflet 0:e89ba455dbcf 364 role = _cc3100.sl_Start(0, 0, 0);
dflet 0:e89ba455dbcf 365 if (ROLE_AP == role) {
dflet 0:e89ba455dbcf 366 /* If the device is in AP mode, we need to wait for this event before doing anything */
dflet 0:e89ba455dbcf 367 while(!_cc3100.IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED)) {
dflet 3:b89198ac2efe 368 _cc3100._nonos._SlNonOsMainLoopTask();
dflet 0:e89ba455dbcf 369 }
dflet 0:e89ba455dbcf 370 } else {
dflet 0:e89ba455dbcf 371 /* Configure CC3100 to start in AP mode */
dflet 3:b89198ac2efe 372 retVal = _cc3100._wlan.sl_WlanSetMode(ROLE_AP);
dflet 0:e89ba455dbcf 373 if(retVal < 0)
dflet 0:e89ba455dbcf 374 LOOP_FOREVER();
dflet 5:d3b320ebd469 375
dflet 5:d3b320ebd469 376 /* Configure the SSID of the CC3100 */
dflet 5:d3b320ebd469 377 retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID,
dflet 5:d3b320ebd469 378 strlen(SSID_AP_MODE), (uint8_t *)SSID_AP_MODE);
dflet 5:d3b320ebd469 379 if(retVal < 0)
dflet 5:d3b320ebd469 380 LOOP_FOREVER();
dflet 5:d3b320ebd469 381
dflet 5:d3b320ebd469 382 SecType = SEC_TYPE_AP_MODE;
dflet 5:d3b320ebd469 383 /* Configure the Security parameter the AP mode */
dflet 5:d3b320ebd469 384 retVal = _cc3100._wlan.sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, (uint8_t *)&SecType);
dflet 5:d3b320ebd469 385 if(retVal < 0)
dflet 5:d3b320ebd469 386 LOOP_FOREVER();
dflet 5:d3b320ebd469 387
dflet 5:d3b320ebd469 388 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 389 if(retVal < 0)
dflet 5:d3b320ebd469 390 LOOP_FOREVER();
dflet 0:e89ba455dbcf 391
dflet 0:e89ba455dbcf 392 retVal = _cc3100.sl_Stop(SL_STOP_TIMEOUT);
dflet 0:e89ba455dbcf 393 if(retVal < 0)
dflet 0:e89ba455dbcf 394 LOOP_FOREVER();
dflet 0:e89ba455dbcf 395
dflet 0:e89ba455dbcf 396 _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_IP_ACQUIRED);
dflet 0:e89ba455dbcf 397
dflet 0:e89ba455dbcf 398 role = _cc3100.sl_Start(0, 0, 0);
dflet 0:e89ba455dbcf 399 if (ROLE_AP == role) {
dflet 0:e89ba455dbcf 400 /* If the device is in AP mode, we need to wait for this event before doing anything */
dflet 0:e89ba455dbcf 401 while(!_cc3100.IS_IP_ACQUIRED(g_Status,STATUS_BIT_IP_ACQUIRED)) {
dflet 3:b89198ac2efe 402 _cc3100._nonos._SlNonOsMainLoopTask();
dflet 0:e89ba455dbcf 403 }
dflet 0:e89ba455dbcf 404 } else {
ArcN00b 9:30d6c10d4eab 405 printf(" Impossibile completare l'avvio come Access Point \n\r");
dflet 0:e89ba455dbcf 406 LOOP_FOREVER();
dflet 0:e89ba455dbcf 407 }
dflet 0:e89ba455dbcf 408 }
ArcN00b 9:30d6c10d4eab 409 printf(" Avvio come Access Point completato \n\r");
dflet 0:e89ba455dbcf 410
dflet 0:e89ba455dbcf 411 /* Wait */
ArcN00b 9:30d6c10d4eab 412 printf(" In attesa di qualcuno che si connetta\n\r");
dflet 0:e89ba455dbcf 413 while((!_cc3100.IS_IP_LEASED(g_Status,STATUS_BIT_IP_LEASED)) || (!_cc3100.IS_STA_CONNECTED(g_Status,STATUS_BIT_STA_CONNECTED))) {
dflet 3:b89198ac2efe 414 _cc3100._nonos._SlNonOsMainLoopTask();
dflet 0:e89ba455dbcf 415 }
ArcN00b 9:30d6c10d4eab 416 printf(" Un client sta tentando di connettersi \n\r");
ArcN00b 9:30d6c10d4eab 417 printf(" In corso test di connettivita'... \n\r");
dflet 0:e89ba455dbcf 418
dflet 0:e89ba455dbcf 419 /* Set the ping parameters */
dflet 0:e89ba455dbcf 420 PingParams.PingIntervalTime = PING_INTERVAL;
dflet 0:e89ba455dbcf 421 PingParams.PingSize = PING_SIZE;
dflet 0:e89ba455dbcf 422 PingParams.PingRequestTimeout = PING_TIMEOUT;
dflet 0:e89ba455dbcf 423 PingParams.TotalNumberOfAttempts = PING_ATTEMPTS;
dflet 0:e89ba455dbcf 424 PingParams.Flags = 0;
dflet 0:e89ba455dbcf 425 PingParams.Ip = g_StationIP; /* Fill the station IP address connected to CC3100 */
dflet 0:e89ba455dbcf 426
dflet 0:e89ba455dbcf 427 /* Ping client connected to CC3100 */
dflet 3:b89198ac2efe 428 retVal = _cc3100._netapp.sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&Report, &SimpleLinkPingReport);
dflet 0:e89ba455dbcf 429 if(retVal < 0)
dflet 0:e89ba455dbcf 430 LOOP_FOREVER();
dflet 0:e89ba455dbcf 431
dflet 0:e89ba455dbcf 432 /* Wait */
dflet 0:e89ba455dbcf 433 while(!_cc3100.IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) {
dflet 3:b89198ac2efe 434 _cc3100._nonos._SlNonOsMainLoopTask();
dflet 0:e89ba455dbcf 435 }
dflet 0:e89ba455dbcf 436
dflet 0:e89ba455dbcf 437 if (0 == g_PingPacketsRecv) {
ArcN00b 9:30d6c10d4eab 438 printf(" Impossibile connettersi correttamente al client \n\r");
dflet 0:e89ba455dbcf 439 //ASSERT_ON_ERROR(LAN_CONNECTION_FAILED);
ArcN00b 9:30d6c10d4eab 440 printf(" ERRORE numero %d\n\r", LAN_CONNECTION_FAILED);
dflet 0:e89ba455dbcf 441 }
dflet 0:e89ba455dbcf 442
ArcN00b 9:30d6c10d4eab 443 printf(" Client correttamente connesso \n\r");
ArcN00b 9:30d6c10d4eab 444 printf(" Socket in apertura \n\r");
ArcN00b 9:30d6c10d4eab 445
ArcN00b 9:30d6c10d4eab 446 //Funzione che gestisce e crea la socket
ArcN00b 9:30d6c10d4eab 447 retVal = TcpServer(PORT_NUM);
ArcN00b 9:30d6c10d4eab 448 if(retVal < 0)
ArcN00b 9:30d6c10d4eab 449 printf(" Impossibile avviare la comunicazione \n\r");
dflet 0:e89ba455dbcf 450 }
dflet 0:e89ba455dbcf 451
ArcN00b 8:2acb25effa3a 452 int32_t pingToHost(char* host) {
ArcN00b 8:2acb25effa3a 453
ArcN00b 8:2acb25effa3a 454 SlPingStartCommand_t pingParams = {0};
ArcN00b 8:2acb25effa3a 455 SlPingReport_t pingReport = {0};
ArcN00b 8:2acb25effa3a 456
ArcN00b 8:2acb25effa3a 457 uint32_t ipAddr = 0;
ArcN00b 8:2acb25effa3a 458
ArcN00b 8:2acb25effa3a 459 int32_t retVal = -1;
ArcN00b 8:2acb25effa3a 460
ArcN00b 8:2acb25effa3a 461 _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE);
ArcN00b 8:2acb25effa3a 462 g_PingPacketsRecv = 0;
ArcN00b 8:2acb25effa3a 463
ArcN00b 8:2acb25effa3a 464 /* Set the ping parameters */
ArcN00b 8:2acb25effa3a 465 pingParams.PingIntervalTime = PING_INTERVAL;
ArcN00b 8:2acb25effa3a 466 pingParams.PingSize = PING_PKT_SIZE;
ArcN00b 8:2acb25effa3a 467 pingParams.PingRequestTimeout = PING_TIMEOUT;
ArcN00b 8:2acb25effa3a 468 pingParams.TotalNumberOfAttempts = PING_ATTEMPTS;
ArcN00b 8:2acb25effa3a 469 pingParams.Flags = 0;
ArcN00b 8:2acb25effa3a 470 pingParams.Ip = g_GatewayIP;
ArcN00b 8:2acb25effa3a 471
ArcN00b 8:2acb25effa3a 472 /* Check for Internet connection */
ArcN00b 8:2acb25effa3a 473 retVal = _cc3100._netapp.sl_NetAppDnsGetHostByName((unsigned char *)host, strlen(host), &ipAddr, SL_AF_INET);
ArcN00b 8:2acb25effa3a 474 if(retVal < 0) {
ArcN00b 8:2acb25effa3a 475 printf(" Impossibile trovare l'host \n\r");
ArcN00b 8:2acb25effa3a 476 return retVal;
ArcN00b 8:2acb25effa3a 477 } else
ArcN00b 8:2acb25effa3a 478 printf(" Avvio ping\n");
ArcN00b 8:2acb25effa3a 479
ArcN00b 8:2acb25effa3a 480 /* Replace the ping address to match HOST_NAME's IP address */
ArcN00b 8:2acb25effa3a 481 pingParams.Ip = ipAddr;
ArcN00b 8:2acb25effa3a 482
ArcN00b 8:2acb25effa3a 483 /* Try to ping HOST_NAME */
ArcN00b 8:2acb25effa3a 484 retVal = _cc3100._netapp.sl_NetAppPingStart( (SlPingStartCommand_t*)&pingParams, SL_AF_INET,
ArcN00b 8:2acb25effa3a 485 (SlPingReport_t*)&pingReport, SimpleLinkPingReport);
ArcN00b 8:2acb25effa3a 486 if(retVal < 0) {
ArcN00b 8:2acb25effa3a 487 printf(" Impossibile connettersi all'host \n\r");
ArcN00b 8:2acb25effa3a 488 return retVal;
ArcN00b 8:2acb25effa3a 489 }
ArcN00b 8:2acb25effa3a 490
ArcN00b 8:2acb25effa3a 491 /* Wait */
ArcN00b 8:2acb25effa3a 492 while(!_cc3100.IS_PING_DONE(g_Status,STATUS_BIT_PING_DONE)) { _cc3100._nonos._SlNonOsMainLoopTask(); }
ArcN00b 8:2acb25effa3a 493
ArcN00b 8:2acb25effa3a 494 if (0 == g_PingPacketsRecv) {
ArcN00b 8:2acb25effa3a 495 printf(" Il test non e' andato a buon fine \n\r");
ArcN00b 8:2acb25effa3a 496 return retVal;
ArcN00b 8:2acb25effa3a 497 }
ArcN00b 8:2acb25effa3a 498
ArcN00b 8:2acb25effa3a 499 return 0;
ArcN00b 8:2acb25effa3a 500
ArcN00b 8:2acb25effa3a 501 }
ArcN00b 8:2acb25effa3a 502
ArcN00b 8:2acb25effa3a 503 void station_app(void) {
dflet 0:e89ba455dbcf 504
dflet 0:e89ba455dbcf 505 int32_t retVal = -1;
ArcN00b 8:2acb25effa3a 506 char host[100];
dflet 5:d3b320ebd469 507
dflet 0:e89ba455dbcf 508 /* Connecting to WLAN AP */
dflet 0:e89ba455dbcf 509 retVal = _cc3100.establishConnectionWithAP();
dflet 0:e89ba455dbcf 510 if(retVal < 0)
dflet 0:e89ba455dbcf 511 {
ArcN00b 8:2acb25effa3a 512 printf(" Impossibile connettersi \n\r");
dflet 0:e89ba455dbcf 513 LOOP_FOREVER();
dflet 5:d3b320ebd469 514 }
ArcN00b 8:2acb25effa3a 515 printf(" Connessione stabilita con l'Access Point \n\r");
dflet 5:d3b320ebd469 516
ArcN00b 8:2acb25effa3a 517 printf(" Test di connettivita' in corso... \n\r");
dflet 0:e89ba455dbcf 518 retVal = _cc3100.checkLanConnection();
ArcN00b 8:2acb25effa3a 519 if(retVal < 0) {
dflet 0:e89ba455dbcf 520 printf(" Device couldn't connect to LAN \n\r");
dflet 0:e89ba455dbcf 521 LOOP_FOREVER();
dflet 0:e89ba455dbcf 522 }
ArcN00b 8:2acb25effa3a 523 printf(" Connesso alla rete locale con successo \r\n");
dflet 5:d3b320ebd469 524
dflet 0:e89ba455dbcf 525 retVal = _cc3100.checkInternetConnection();
dflet 0:e89ba455dbcf 526 if(retVal < 0)
dflet 0:e89ba455dbcf 527 {
dflet 0:e89ba455dbcf 528 printf(" Device couldn't connect to the internet \n\r");
dflet 0:e89ba455dbcf 529 LOOP_FOREVER();
dflet 0:e89ba455dbcf 530 }
dflet 0:e89ba455dbcf 531
ArcN00b 8:2acb25effa3a 532 printf(" Connesso ad internet con successo");
ArcN00b 8:2acb25effa3a 533
ArcN00b 8:2acb25effa3a 534 // Esegue ping finchè connesso
ArcN00b 8:2acb25effa3a 535 while(1) {
ArcN00b 8:2acb25effa3a 536 printf(" \n\rInserire un host a cui fare il ping \n\r");
ArcN00b 8:2acb25effa3a 537 scanf("%s", host);
ArcN00b 8:2acb25effa3a 538
ArcN00b 8:2acb25effa3a 539 if(pingToHost(host) == 0)
ArcN00b 8:2acb25effa3a 540 printf("\nTest completato senza errori\n\r");
ArcN00b 8:2acb25effa3a 541
ArcN00b 8:2acb25effa3a 542 }
dflet 0:e89ba455dbcf 543 }