cc3100_Socket_Wifi_Server with Ethernet Interface not working

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of cc3100_Test_Demo by David Fletcher

Committer:
artpes
Date:
Fri May 26 10:03:00 2017 +0000
Revision:
9:fd9f64918306
Parent:
8:f4eb30a34ea7
Wifi Server

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 0:e89ba455dbcf 1 #include "cc3100_simplelink.h"
dflet 0:e89ba455dbcf 2 #include "cc3100_sl_common.h"
dflet 0:e89ba455dbcf 3 #include "fPtr_func.h"
dflet 3:b89198ac2efe 4 #include "cc3100.h"
dflet 0:e89ba455dbcf 5 #include "cc3100_spi.h"
dflet 0:e89ba455dbcf 6 #include "myBoardInit.h"
artpes 8:f4eb30a34ea7 7 #include "cc3100_socket.h"
artpes 8:f4eb30a34ea7 8 #include "cc3100_driver.h"
artpes 8:f4eb30a34ea7 9 #include "cc3100_nonos.h"
artpes 8:f4eb30a34ea7 10 #include "mbed.h"
artpes 8:f4eb30a34ea7 11 #include "rtos.h"
artpes 9:fd9f64918306 12 //#include "EthernetInterface.h"
dflet 0:e89ba455dbcf 13
dflet 0:e89ba455dbcf 14 using namespace mbed_cc3100;
dflet 0:e89ba455dbcf 15
artpes 8:f4eb30a34ea7 16 #define SL_STOP_TIMEOUT 0xFF
artpes 8:f4eb30a34ea7 17
artpes 8:f4eb30a34ea7 18 /* IP addressed of server side socket. Should be in long format,
artpes 8:f4eb30a34ea7 19 * E.g: 0xc0a8010a == 192.168.1.10 */
artpes 8:f4eb30a34ea7 20 //#define IP_ADDR 0xc0a82b69 //192.168.43.105
artpes 8:f4eb30a34ea7 21 #define PORT_NUM 50001 /* Port number to be used */
artpes 8:f4eb30a34ea7 22
artpes 8:f4eb30a34ea7 23 #define BUF_SIZE 1400
artpes 8:f4eb30a34ea7 24 #define NO_OF_PACKETS 1000
artpes 8:f4eb30a34ea7 25
dflet 0:e89ba455dbcf 26 cc3100 _cc3100(p9, p10, p8, SPI(p11, p12, p13));//LPC1768 irq, nHib, cs, mosi, miso, sck
dflet 0:e89ba455dbcf 27 Serial pc(USBTX, USBRX);//lpc1768
artpes 8:f4eb30a34ea7 28 DigitalOut myled1(LED1);
artpes 8:f4eb30a34ea7 29 DigitalOut myled2(LED2);
artpes 8:f4eb30a34ea7 30 DigitalOut myled3(LED3);
artpes 8:f4eb30a34ea7 31 DigitalOut myled4(LED4);
dflet 0:e89ba455dbcf 32
dflet 0:e89ba455dbcf 33
artpes 8:f4eb30a34ea7 34 //GLOBAL VARIABLES -- Start
dflet 3:b89198ac2efe 35 int32_t demo = 0;
artpes 8:f4eb30a34ea7 36 //GLOBAL VARIABLES -- End
dflet 0:e89ba455dbcf 37
dflet 0:e89ba455dbcf 38
dflet 0:e89ba455dbcf 39 static void displayBanner()
dflet 0:e89ba455dbcf 40 {
artpes 8:f4eb30a34ea7 41 printf("\n\r\n\r");
artpes 8:f4eb30a34ea7 42 printf("***********Getting started with station application***********");
artpes 8:f4eb30a34ea7 43 printf("\n\r*******************************************************************************\n\r");
artpes 8:f4eb30a34ea7 44 }
artpes 9:fd9f64918306 45
dflet 0:e89ba455dbcf 46
artpes 8:f4eb30a34ea7 47 //******************************STATION_MODE***********************************************
artpes 8:f4eb30a34ea7 48 //*****************************************************************************************
artpes 8:f4eb30a34ea7 49 void station_app()
artpes 8:f4eb30a34ea7 50 {
dflet 0:e89ba455dbcf 51 int32_t retVal = -1;
dflet 5:d3b320ebd469 52
dflet 0:e89ba455dbcf 53 /* Connecting to WLAN AP */
dflet 0:e89ba455dbcf 54 retVal = _cc3100.establishConnectionWithAP();
artpes 8:f4eb30a34ea7 55 //printf("retVal 000: %d\n\r",retVal);
dflet 0:e89ba455dbcf 56 if(retVal < 0)
dflet 0:e89ba455dbcf 57 {
dflet 0:e89ba455dbcf 58 printf(" Failed to establish connection w/ an AP \n\r");
dflet 0:e89ba455dbcf 59 LOOP_FOREVER();
dflet 5:d3b320ebd469 60 }
dflet 0:e89ba455dbcf 61 printf(" Connection established w/ AP and IP is acquired \n\r");
dflet 5:d3b320ebd469 62
dflet 0:e89ba455dbcf 63 printf(" Pinging...! \n\r");
dflet 0:e89ba455dbcf 64 retVal = _cc3100.checkLanConnection();
artpes 8:f4eb30a34ea7 65 //printf("retVal 001: %d\n\r",retVal);
dflet 0:e89ba455dbcf 66 if(retVal < 0)
dflet 0:e89ba455dbcf 67 {
dflet 0:e89ba455dbcf 68 printf(" Device couldn't connect to LAN \n\r");
dflet 0:e89ba455dbcf 69 LOOP_FOREVER();
dflet 0:e89ba455dbcf 70 }
artpes 8:f4eb30a34ea7 71 printf(" Device successfully connected to the LAN\n\r");
artpes 8:f4eb30a34ea7 72 /*
dflet 0:e89ba455dbcf 73 retVal = _cc3100.checkInternetConnection();
dflet 0:e89ba455dbcf 74 if(retVal < 0)
dflet 0:e89ba455dbcf 75 {
dflet 0:e89ba455dbcf 76 printf(" Device couldn't connect to the internet \n\r");
dflet 0:e89ba455dbcf 77 LOOP_FOREVER();
dflet 0:e89ba455dbcf 78 }
dflet 0:e89ba455dbcf 79
artpes 8:f4eb30a34ea7 80 printf(" Device successfully connected to the internet \n\r");*/
artpes 8:f4eb30a34ea7 81 }
artpes 8:f4eb30a34ea7 82 //****************************END_STATION_MODE*********************************************
artpes 8:f4eb30a34ea7 83 //*****************************************************************************************
artpes 8:f4eb30a34ea7 84 void led(int result){
artpes 8:f4eb30a34ea7 85
artpes 8:f4eb30a34ea7 86 if (result > 0)
artpes 8:f4eb30a34ea7 87 {
artpes 8:f4eb30a34ea7 88 myled1 = 1;
artpes 8:f4eb30a34ea7 89 wait(0.05);
artpes 8:f4eb30a34ea7 90 myled1 = 0;
artpes 8:f4eb30a34ea7 91 wait(0.05);
artpes 8:f4eb30a34ea7 92 myled2 = 1;
artpes 8:f4eb30a34ea7 93 wait(0.05);
artpes 8:f4eb30a34ea7 94 myled2 = 0;
artpes 8:f4eb30a34ea7 95 wait(0.05);
artpes 8:f4eb30a34ea7 96 myled3 = 1;
artpes 8:f4eb30a34ea7 97 wait(0.05);
artpes 8:f4eb30a34ea7 98 myled3 = 0;
artpes 8:f4eb30a34ea7 99 wait(0.05);
artpes 8:f4eb30a34ea7 100 myled4 = 1;
artpes 8:f4eb30a34ea7 101 wait(0.05);
artpes 8:f4eb30a34ea7 102 myled4 = 0;
artpes 8:f4eb30a34ea7 103 wait(0.05);
artpes 8:f4eb30a34ea7 104 }
artpes 8:f4eb30a34ea7 105 else
artpes 8:f4eb30a34ea7 106 { myled1 = 0;
artpes 8:f4eb30a34ea7 107 myled2 = 0;
artpes 8:f4eb30a34ea7 108 myled3 = 0;
artpes 8:f4eb30a34ea7 109 myled4 = 0;
artpes 8:f4eb30a34ea7 110 }
artpes 8:f4eb30a34ea7 111
artpes 8:f4eb30a34ea7 112 }
artpes 8:f4eb30a34ea7 113
artpes 8:f4eb30a34ea7 114 //osThreadDef(led, osPriorityNormal, DEFAULT_STACK_SIZE);
artpes 8:f4eb30a34ea7 115 static int32_t BsdTcpServer(uint16_t Port)
artpes 8:f4eb30a34ea7 116 {
artpes 8:f4eb30a34ea7 117
artpes 8:f4eb30a34ea7 118 SlSockAddrIn_t Addr;
artpes 8:f4eb30a34ea7 119 SlSockAddrIn_t LocalAddr;
artpes 8:f4eb30a34ea7 120 char stringa[100];
artpes 8:f4eb30a34ea7 121 //int16_t idx = 0;
artpes 8:f4eb30a34ea7 122 int16_t AddrSize = 0;
artpes 8:f4eb30a34ea7 123 int16_t SockID = 0;
artpes 8:f4eb30a34ea7 124 int32_t Status = 0;
artpes 8:f4eb30a34ea7 125 int16_t newSockID = 0;
artpes 8:f4eb30a34ea7 126 int16_t LoopCount = 0;
artpes 8:f4eb30a34ea7 127 //int16_t SlSockAddrIn_t = 0;
artpes 8:f4eb30a34ea7 128 int16_t recvSize = 0;
artpes 8:f4eb30a34ea7 129 //Thread thread;
artpes 8:f4eb30a34ea7 130
artpes 8:f4eb30a34ea7 131 /*
artpes 8:f4eb30a34ea7 132 for (idx=0 ; idx<BUF_SIZE ; idx++)
artpes 8:f4eb30a34ea7 133 {
artpes 8:f4eb30a34ea7 134 uBuf.BsdBuf[idx] = (_u8)(idx % 10);
artpes 8:f4eb30a34ea7 135 }
artpes 8:f4eb30a34ea7 136 */
artpes 8:f4eb30a34ea7 137
artpes 8:f4eb30a34ea7 138 LocalAddr.sin_family = SL_AF_INET;
artpes 8:f4eb30a34ea7 139 LocalAddr.sin_port = _cc3100._socket.sl_Htons(PORT_NUM);
artpes 8:f4eb30a34ea7 140 LocalAddr.sin_addr.s_addr = 0;
artpes 8:f4eb30a34ea7 141 //int port = 1;
artpes 8:f4eb30a34ea7 142 //osThreadCreate(osThread(led), (void *) port);
artpes 8:f4eb30a34ea7 143 //thread.start(led(1));
artpes 8:f4eb30a34ea7 144
artpes 8:f4eb30a34ea7 145 SockID = _cc3100._socket.sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
artpes 8:f4eb30a34ea7 146 if( SockID < 0 )
artpes 8:f4eb30a34ea7 147 {
artpes 8:f4eb30a34ea7 148 printf(" \n[TCP Server] Create socket Error \n\r");
artpes 8:f4eb30a34ea7 149 ASSERT_ON_ERROR(SockID);
artpes 8:f4eb30a34ea7 150 }
artpes 8:f4eb30a34ea7 151 else
artpes 8:f4eb30a34ea7 152 printf("\nCreate socket \n\r");
artpes 8:f4eb30a34ea7 153
artpes 8:f4eb30a34ea7 154
artpes 8:f4eb30a34ea7 155 AddrSize = sizeof(SlSockAddrIn_t);
artpes 8:f4eb30a34ea7 156 Status = _cc3100._socket.sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize);
artpes 8:f4eb30a34ea7 157 if( Status < 0 )
artpes 8:f4eb30a34ea7 158 {
artpes 8:f4eb30a34ea7 159 _cc3100._socket.sl_Close(SockID);
artpes 8:f4eb30a34ea7 160 printf(" \n[TCP Server] Socket address assignment Error \n\r");
artpes 8:f4eb30a34ea7 161 ASSERT_ON_ERROR(Status);
artpes 8:f4eb30a34ea7 162 }
artpes 8:f4eb30a34ea7 163 else
artpes 8:f4eb30a34ea7 164 printf("\nSocket address assignment \n\r");
artpes 8:f4eb30a34ea7 165
artpes 8:f4eb30a34ea7 166 Status = _cc3100._socket.sl_Listen(SockID, 0);
artpes 8:f4eb30a34ea7 167 if( Status < 0 )
artpes 8:f4eb30a34ea7 168 {
artpes 8:f4eb30a34ea7 169 _cc3100._socket.sl_Close(SockID);
artpes 8:f4eb30a34ea7 170 printf("\n [TCP Server] Listen Error \n\r");
artpes 8:f4eb30a34ea7 171 ASSERT_ON_ERROR(Status);
artpes 8:f4eb30a34ea7 172 }
artpes 8:f4eb30a34ea7 173 else
artpes 8:f4eb30a34ea7 174 printf("\nListen for a Connection \n\r");
artpes 8:f4eb30a34ea7 175
artpes 8:f4eb30a34ea7 176 newSockID = _cc3100._socket.sl_Accept(SockID, (SlSockAddr_t *)&Addr,(SlSocklen_t*)&AddrSize);
artpes 8:f4eb30a34ea7 177 if( newSockID < 0 )
artpes 8:f4eb30a34ea7 178 {
artpes 8:f4eb30a34ea7 179 _cc3100._socket.sl_Close(SockID);
artpes 8:f4eb30a34ea7 180 printf("\n [TCP Server] Accept connection Error \n\r");
artpes 8:f4eb30a34ea7 181 ASSERT_ON_ERROR(newSockID);
artpes 8:f4eb30a34ea7 182 }
artpes 8:f4eb30a34ea7 183 else
artpes 8:f4eb30a34ea7 184 printf("\nAccept connection \n\r");
artpes 9:fd9f64918306 185
artpes 8:f4eb30a34ea7 186 while (LoopCount < NO_OF_PACKETS)
artpes 8:f4eb30a34ea7 187 {
artpes 8:f4eb30a34ea7 188 //TODO : sarebbe da chiamare la funzione led() con un altro thread così da farla girare in background
artpes 8:f4eb30a34ea7 189 led(1);
artpes 8:f4eb30a34ea7 190 recvSize = BUF_SIZE;
artpes 8:f4eb30a34ea7 191 //printf("RecvSize = %d \n\r",recvSize);
artpes 8:f4eb30a34ea7 192 do
artpes 8:f4eb30a34ea7 193 {
artpes 8:f4eb30a34ea7 194 Status = _cc3100._socket.sl_Recv(newSockID, &(stringa), strlen(stringa), 0);
artpes 8:f4eb30a34ea7 195 if( Status <= 0 )
artpes 8:f4eb30a34ea7 196 {
artpes 8:f4eb30a34ea7 197 _cc3100._socket.sl_Close(newSockID);
artpes 8:f4eb30a34ea7 198 _cc3100._socket.sl_Close(SockID);
artpes 8:f4eb30a34ea7 199 led(1);
artpes 8:f4eb30a34ea7 200 printf(" \n[TCP Server] Data recv Error. Close socket %d \n\n\r",SockID);
artpes 8:f4eb30a34ea7 201 //ASSERT_ON_ERROR(TCP_RECV_ERROR);
artpes 8:f4eb30a34ea7 202
artpes 8:f4eb30a34ea7 203 /*-----Accept new connection----------*/
artpes 8:f4eb30a34ea7 204 SockID = _cc3100._socket.sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
artpes 8:f4eb30a34ea7 205 printf("----------Create new socket---------- \n\n\r");
artpes 8:f4eb30a34ea7 206 AddrSize = sizeof(SlSockAddrIn_t);
artpes 8:f4eb30a34ea7 207 Status = _cc3100._socket.sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize);
artpes 8:f4eb30a34ea7 208 printf("----------Socket address assignment---------- \n\n\r");
artpes 8:f4eb30a34ea7 209 Status = _cc3100._socket.sl_Listen(SockID, 0);
artpes 8:f4eb30a34ea7 210 printf("----------Listen for a new Connection---------- \n\n\r");
artpes 8:f4eb30a34ea7 211 newSockID = _cc3100._socket.sl_Accept(SockID, (SlSockAddr_t *)&Addr,(SlSocklen_t*)&AddrSize);
artpes 8:f4eb30a34ea7 212 printf("----------Accept new connection---------- \n\n\r");
artpes 8:f4eb30a34ea7 213 led(1);
artpes 8:f4eb30a34ea7 214 }
artpes 8:f4eb30a34ea7 215
artpes 8:f4eb30a34ea7 216 //printf("Status = %d \n\r",Status);
artpes 8:f4eb30a34ea7 217 recvSize -= Status;
artpes 8:f4eb30a34ea7 218 //printf("RecvSize = %d \n\r",recvSize);
artpes 8:f4eb30a34ea7 219 printf("%s",stringa);
artpes 8:f4eb30a34ea7 220 led(1);
artpes 8:f4eb30a34ea7 221 char thanks [] = "Data received";
artpes 8:f4eb30a34ea7 222 _cc3100._socket.sl_Send(SockID, &(thanks), strlen(thanks), 0);
artpes 8:f4eb30a34ea7 223
artpes 8:f4eb30a34ea7 224 }
artpes 8:f4eb30a34ea7 225 while(recvSize > 0);
artpes 8:f4eb30a34ea7 226
artpes 8:f4eb30a34ea7 227 LoopCount++;
artpes 8:f4eb30a34ea7 228 }
artpes 8:f4eb30a34ea7 229
artpes 8:f4eb30a34ea7 230 Status = _cc3100._socket.sl_Close(newSockID);
artpes 8:f4eb30a34ea7 231 ASSERT_ON_ERROR(Status);
artpes 8:f4eb30a34ea7 232
artpes 8:f4eb30a34ea7 233 Status = _cc3100._socket.sl_Close(SockID);
artpes 8:f4eb30a34ea7 234 ASSERT_ON_ERROR(Status);
artpes 8:f4eb30a34ea7 235
artpes 8:f4eb30a34ea7 236 return SUCCESS;
artpes 8:f4eb30a34ea7 237
artpes 8:f4eb30a34ea7 238 }
dflet 0:e89ba455dbcf 239
dflet 0:e89ba455dbcf 240
artpes 8:f4eb30a34ea7 241
artpes 8:f4eb30a34ea7 242 //**************************_START_APPLICATION_*********************************
artpes 8:f4eb30a34ea7 243 int main(void)
artpes 8:f4eb30a34ea7 244 {
artpes 8:f4eb30a34ea7 245 pc.baud(115200);
artpes 8:f4eb30a34ea7 246
artpes 8:f4eb30a34ea7 247
artpes 8:f4eb30a34ea7 248
artpes 8:f4eb30a34ea7 249 int32_t retVal = -1;
artpes 8:f4eb30a34ea7 250
artpes 8:f4eb30a34ea7 251 retVal = _cc3100.initializeAppVariables();
artpes 8:f4eb30a34ea7 252
artpes 8:f4eb30a34ea7 253 displayBanner();
artpes 8:f4eb30a34ea7 254
artpes 8:f4eb30a34ea7 255 _cc3100.CLR_STATUS_BIT(g_Status, STATUS_BIT_PING_DONE);
artpes 8:f4eb30a34ea7 256 g_PingPacketsRecv = 0;
artpes 8:f4eb30a34ea7 257 //printf("\n\r FUNZIONE CLR_STATUS_BIT ESEGUITA CON SUCCESSO\n\r");
artpes 8:f4eb30a34ea7 258
artpes 8:f4eb30a34ea7 259 //retVal = _cc3100.configureSimpleLinkToDefaultState();
artpes 8:f4eb30a34ea7 260
artpes 8:f4eb30a34ea7 261 //printf("\n\rRetval 2: %d \n\r ",retVal);
artpes 8:f4eb30a34ea7 262
artpes 8:f4eb30a34ea7 263 if(retVal < 0) {
artpes 8:f4eb30a34ea7 264 if (DEVICE_NOT_IN_STATION_MODE == retVal)
artpes 8:f4eb30a34ea7 265 printf(" Failed to configure the device to its default state \n\r");
artpes 8:f4eb30a34ea7 266
artpes 8:f4eb30a34ea7 267 LOOP_FOREVER();
artpes 8:f4eb30a34ea7 268 }
artpes 8:f4eb30a34ea7 269
artpes 8:f4eb30a34ea7 270 printf("\n\rDevice is configured in it's default state \n\r");
artpes 8:f4eb30a34ea7 271 /*printf("\n\r******************************************************************************************************\n\r");
artpes 8:f4eb30a34ea7 272 printf("\n\r*****_DA_QUI_IN_POI_SCELTA_TIPO_FUNZIONE_IN_BASE_AL_VALORE_demo_*****\n\r");
artpes 8:f4eb30a34ea7 273 printf("\n\r******************************************************************************************************\n\r");*/
artpes 8:f4eb30a34ea7 274
artpes 8:f4eb30a34ea7 275 /*
artpes 8:f4eb30a34ea7 276 * Assumption is that the device is configured in station mode already
artpes 8:f4eb30a34ea7 277 * and it is in its default state
artpes 8:f4eb30a34ea7 278 */
artpes 8:f4eb30a34ea7 279 /* Initializing the CC3100 device */
artpes 8:f4eb30a34ea7 280
artpes 8:f4eb30a34ea7 281 retVal = _cc3100.sl_Start(0, 0, 0);
artpes 8:f4eb30a34ea7 282
artpes 8:f4eb30a34ea7 283 if ((retVal < 0) || (ROLE_STA != retVal) )
artpes 8:f4eb30a34ea7 284 {
artpes 8:f4eb30a34ea7 285 printf(" Failed to start the device \n\r");
artpes 8:f4eb30a34ea7 286 LOOP_FOREVER();
artpes 8:f4eb30a34ea7 287 }
artpes 8:f4eb30a34ea7 288
artpes 8:f4eb30a34ea7 289 printf("\n\r Device started as STATION \n\r");
artpes 8:f4eb30a34ea7 290
artpes 8:f4eb30a34ea7 291
artpes 8:f4eb30a34ea7 292 station_app();
artpes 8:f4eb30a34ea7 293
artpes 8:f4eb30a34ea7 294 printf("Establishing connection with TCP server \n\r");
artpes 8:f4eb30a34ea7 295 /*Before proceeding, please make sure to have a server waiting on PORT_NUM*/
artpes 8:f4eb30a34ea7 296 retVal = BsdTcpServer(PORT_NUM);
artpes 8:f4eb30a34ea7 297 if(retVal < 0)
artpes 8:f4eb30a34ea7 298 printf(" Failed to establishing connection with TCP server \n\r");
artpes 8:f4eb30a34ea7 299 else
artpes 8:f4eb30a34ea7 300 printf(" Connection with TCP server established successfully \n\r");
artpes 8:f4eb30a34ea7 301
artpes 8:f4eb30a34ea7 302 return 0;
artpes 8:f4eb30a34ea7 303
artpes 8:f4eb30a34ea7 304
artpes 8:f4eb30a34ea7 305
artpes 8:f4eb30a34ea7 306
artpes 8:f4eb30a34ea7 307 }
artpes 8:f4eb30a34ea7 308