cc3100_Socket_Wifi_Server version for LPC1768

Dependencies:   mbed

Fork of cc3100_Test_Demo by David Fletcher

Committer:
artpes
Date:
Fri May 26 19:26:11 2017 +0000
Revision:
8:e79bacf664cc
Parent:
7:0687d16b9781
Fork

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