#include #include #include #include // TI-Driver includes #include "ti_drivers_config.h" #include "pthread.h" #include "FreeRTOS.h" #include "Task.h" #define APPLICATION_NAME ("UDP STA Configuration...") #define APPLICATION_VERSION ("1.0.0.0") #define DEVICE_ERROR ("Device error, please refer \"DEVICE ERRORS CODES\" section in errors.h") #define WLAN_ERROR ("WLAN error, please refer \"WLAN ERRORS CODES\" section in errors.h") #define SL_STOP_TIMEOUT (200) #define UDPPORT (1000) #define SPAWN_TASK_PRIORITY (9) #define TASK_STACK_SIZE (2048) #define SLNET_IF_WIFI_PRIO (5) #define SLNET_IF_WIFI_NAME "CC32xx" #define SSID_NAME "HCTRIVEDI_AP1" // AP SSID to which the STA is supposed to get connected #define SECURITY_TYPE SL_WLAN_SEC_TYPE_WPA_WPA2 // Security type could be SL_WLAN_SEC_TYPE_OPEN #define SECURITY_TYPE_printf "SL_WLAN_SEC_TYPE_WPA_WPA2" // Security type could be SL_WLAN_SEC_TYPE_OPEN #define SECURITY_KEY "HarshAPConnect" // Password of the secured AP pthread_t udpThread = (pthread_t)NULL; pthread_t spawn_thread = (pthread_t)NULL; int32_t mode; Display_Handle display; unsigned char flag_SlWlanEvent = 0; unsigned char flag_config_nw_parameters = 0; unsigned char ping_status=0; unsigned char flag_second_ping = 0; unsigned char status = 0; char msg1[26], msg2[6], msg3[6]; extern void echoFxn(uint32_t arg0, uint32_t arg1); extern int32_t ti_net_SlNet_initConfig(); /* * ======== printError ======== */ void printError(char *errString, int code) { Display_printf(display, 0, 0, "Error! code = %d, Description = %s\n", code, errString); while(1); } void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent) { // unused in this application... } void SimpleLinkFatalErrorEventHandler(SlDeviceFatal_t *slFatalErrorEvent) { /* Unused in this application */ } void SimpleLinkNetAppRequestMemFreeEventHandler(uint8_t *buffer) { /* Unused in this application */ } void SimpleLinkNetAppRequestEventHandler(SlNetAppRequest_t *pNetAppRequest, SlNetAppResponse_t *pNetAppResponse) { /* Unused in this application */ } void SimpleLinkHttpServerEventHandler(SlNetAppHttpServerEvent_t *pHttpEvent, SlNetAppHttpServerResponse_t *pHttpResponse) { /* Unused in this application */ } void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent) { if(pWlanEvent == NULL) { return; } switch(pWlanEvent->Id) { case SL_WLAN_EVENT_CONNECT: printf("\n\n"); printf("********************"); printf("\n\n"); printf("Connected to AP with following parameters:"); printf("\n AP SSID: %s",pWlanEvent->Data.Connect.SsidName); printf("\n Communication Channel: %s",pWlanEvent->Data.Connect.Channel); printf("********************"); flag_SlWlanEvent = 1; break; case SL_WLAN_EVENT_DISCONNECT: printf("\n\n"); printf("********************"); printf("\n\n"); printf("Disconnected from AP with following parameters:"); printf("\n AP SSID: %s",pWlanEvent->Data.Disconnect.SsidName); printf("\n Reason for Disconnection: %s",pWlanEvent->Data.Disconnect.ReasonCode); printf("\n\n"); printf("********************"); break; } } void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent) { /* Unused in this application */ } void SimpleLinkSockEventHandler(SlSockEvent_t *pSock) { /* Unused in this application */ } void Connect(void) { SlWlanSecParams_t secParams = {0}; int16_t ret = 0; secParams.Key = (signed char*)SECURITY_KEY; secParams.KeyLen = strlen(SECURITY_KEY); secParams.Type = SECURITY_TYPE; printf("Connecting to : %s.\r\n",SSID_NAME); ret = sl_WlanConnect((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0); if (ret) { printError("Connection failed", ret); } else { printf("\n"); printf("sl_WlanConnect function did not throw any error!! We shall wait for the SL_WLAN_EVENT_CONNECT though!!"); printf("\n"); } } void check_device_role(void) { if(mode == ROLE_STA) { printf("The device is working in STA Mode ..."); printf("\n"); } else { printError("Failed to configure device to STA Role", mode); } } void config_STA_parameters(void) { _u8 status = 0; // setup a variable to hold return values... // set STA Tx power level... _u8 stapower = 5; _u8 maxpower = 15; status = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, SL_WLAN_GENERAL_PARAM_OPT_STA_TX_POWER,1,(_u8 *)&stapower); if (status != 0) { Display_printf(display, 0, 0,"\n\r[line:%d, error code:%d] %s\n\r", __LINE__, status, WLAN_ERROR); } else { printf("STA Transmit power level configured successfully..."); printf("\n"); printf("STA Transmit power level = %d dBm", (maxpower - stapower)); printf("\n"); } // set STA Country Code... _u8* str = "US"; // string of 2 characters. i.e. - "US" status = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, SL_WLAN_GENERAL_PARAM_OPT_COUNTRY_CODE, 2, str); if (status != 0) { Display_printf(display, 0, 0,"\n\r[line:%d, error code:%d] %s\n\r", __LINE__, status, WLAN_ERROR); } else { printf("STA country code configured successfully..."); printf("\n"); printf("STA country code = %s", str); printf("\n"); } // For changes to take affect, we restart the NWP status = sl_Stop(SL_STOP_TIMEOUT); if (status < 0) { Display_printf(display, 0, 0,"\n\r[line:%d, error code:%d] %s\n\r", __LINE__, status, DEVICE_ERROR); } mode = sl_Start(0, 0, 0); if (mode < 0) { Display_printf(display, 0, 0,"\n\r[line:%d, error code:%d] %s\n\r", __LINE__, mode, DEVICE_ERROR); } } void config_nw_parameters(void) { int status_config_nw_parameters = 0; SlNetCfgIpV4Args_t ipV4; ipV4.Ip = (_u32)SL_IPV4_VAL(10,1,1,201); // _u32 IP address ipV4.IpMask = (_u32)SL_IPV4_VAL(255,255,255,0); // _u32 Subnet mask for this STA ipV4.IpGateway = (_u32)SL_IPV4_VAL(10,1,1,1); // _u32 Default gateway address ipV4.IpDnsServer = (_u32)SL_IPV4_VAL(8,16,32,64); // _u32 DNS server address status_config_nw_parameters = sl_NetCfgSet(SL_NETCFG_IPV4_STA_ADDR_MODE,SL_NETCFG_ADDR_STATIC,sizeof(SlNetCfgIpV4Args_t),(_u8 *)&ipV4); if(status_config_nw_parameters >= 0) { printf("\n Network parameters configured successfully and are as follows..."); printf("\n"); printf("IP Address: %d.%d.%d.%d",SL_IPV4_BYTE(ipV4.Ip,3),SL_IPV4_BYTE(ipV4.Ip,2),SL_IPV4_BYTE(ipV4.Ip,1),SL_IPV4_BYTE(ipV4.Ip,0)); printf("\n"); printf("Subnet Mask: %d.%d.%d.%d", SL_IPV4_BYTE(ipV4.IpMask,3),SL_IPV4_BYTE(ipV4.IpMask,2),SL_IPV4_BYTE(ipV4.IpMask,1),SL_IPV4_BYTE(ipV4.IpMask,0)); printf("\n"); printf("Gateway: %d.%d.%d.%d", SL_IPV4_BYTE(ipV4.IpGateway,3),SL_IPV4_BYTE(ipV4.IpGateway,2),SL_IPV4_BYTE(ipV4.IpGateway,1),SL_IPV4_BYTE(ipV4.IpGateway,0)); printf("\n"); printf("DNS Server: %d.%d.%d.%d", SL_IPV4_BYTE(ipV4.IpDnsServer,3),SL_IPV4_BYTE(ipV4.IpDnsServer,2),SL_IPV4_BYTE(ipV4.IpDnsServer,1),SL_IPV4_BYTE(ipV4.IpDnsServer,0)); printf("\n"); } sl_Stop(0); sl_Start(NULL,NULL,NULL); flag_config_nw_parameters = 1; } void first_ping_attempt(void) { SlNetAppPingReport_t ping1_report; SlNetAppPingCommand_t pingCommand; pingCommand.Ip = SL_IPV4_VAL(10,1,1,200); // destination IP address is 10.1.1.200 pingCommand.PingSize = 150; // size of ping, in bytes pingCommand.PingIntervalTime = 100; // delay between pings, in milliseconds pingCommand.PingRequestTimeout = 1000; // timeout for every ping in milliseconds pingCommand.TotalNumberOfAttempts = 20; // max number of ping requests. 0 - forever pingCommand.Flags = 2; // report at the end of first successful ping ping_status = sl_NetAppPing( &pingCommand, SL_AF_INET, &ping1_report, NULL ); if(ping_status) { printf("\n\r ~~~~~ Ping Failure ~~~~~"); } printf("\n"); // while(ping1_report.PacketsReceived !=1){} } void second_ping_attempt(void) { SlNetAppPingReport_t ping2_report; SlNetAppPingCommand_t pingCommand; pingCommand.Ip = SL_IPV4_VAL(10,1,1,200); // destination IP address is 10.1.1.200 pingCommand.PingSize = 150; // size of ping, in bytes pingCommand.PingIntervalTime = 100; // delay between pings, in milliseconds pingCommand.PingRequestTimeout = 1000; // timeout for every ping in milliseconds pingCommand.TotalNumberOfAttempts = 50; // max number of ping requests. 0 - forever pingCommand.Flags = 0; // report at the end of first successful ping ping_status = sl_NetAppPing( &pingCommand, SL_AF_INET, &ping2_report, NULL ); if(ping_status) { printf("\n\r ~~~~~ Ping Failure ~~~~~"); } printf("\n"); if(ping2_report.PacketsReceived >=45) { printf("\n\n"); printf("90%% of the ping response was positive..."); printf("Connection can be assumed rock solid..."); printf("Socket creation and data transfer can be initiated..."); } flag_second_ping = 1; } void socket_task(void *pvParameters) { long counter1 = 0; long counter2 = 0; _i16 Sd; SlSockAddrIn_t Addr; _i8 SendBuf[] = "Hello from STA !!!"; _i8 RecvBuf[1460] ={0}; Sd = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, 0); // IPv4 socket (UDP, TCP, etc), UDP Packets, Null if( Sd < 0 ) { printf("Socket opening failed!!"); } Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons(5001); Addr.sin_addr.s_addr = SL_INADDR_ANY; // bind to any address // Addr.sin_addr.s_addr = sl_Htonl(SL_IPV4_VAL(192,168,1,31)); // bind to this specific address only. status = sl_Bind(Sd, ( SlSockAddr_t *)&Addr, sizeof(SlSockAddrIn_t)); if(status) { printf("\n\n\n Socket Binding failed"); } else { printf("\n socket binding completed \n"); } Addr.sin_addr.s_addr = sl_Htonl(SL_IPV4_VAL(10,1,1,200)); // connect with this specific address only. status = sl_Connect(Sd, ( SlSockAddr_t *)&Addr, sizeof(SlSockAddrIn_t)); // address specified in this statement is the same from which the data is to be received and to which the data is to be sent. if(status) { printf("\n\n\n Socket Connection failed"); } while(1) { strcpy(msg1, "STA Message # "); ltoa(counter1, msg2, 10); ltoa(counter2, msg3, 10); strcat(msg1, msg2); strcat(msg1, " & "); strcat(msg1, msg3); status = sl_Send(Sd, msg1, strlen(msg1), 0); // SL_WLAN_RATE_1M if( strlen(msg1) != status ) { printf("\n\n\n All packets could not be sent"); } status = sl_Recv(Sd, RecvBuf, 1460, 0); if (status < 0) { printf("\n\n\nPacket reception failed!!"); } else { printf("\n\n\n number of packets received = %d", status); printf(" \n received messages is: %s", RecvBuf); // printf(" \n "); // printf(" \n "); } counter1++; if(counter1 == 50000) { // printf(" \n received messages is: %s", RecvBuf); // printf(" \n "); counter2++; counter1 = 0; } } printf("\n\n Out of infinite loop!!!"); printf("\n\n"); } void mainThread(void *pvParameters) { int32_t status = 0; pthread_attr_t pAttrs_spawn; struct sched_param priParam; SPI_init(); Display_init(); display = Display_open(Display_Type_UART, NULL); if (display == NULL) { /* Failed to open display driver */ while(1); } /* Start the SimpleLink Host */ pthread_attr_init(&pAttrs_spawn); priParam.sched_priority = SPAWN_TASK_PRIORITY; status = pthread_attr_setschedparam(&pAttrs_spawn, &priParam); status |= pthread_attr_setstacksize(&pAttrs_spawn, TASK_STACK_SIZE); status = pthread_create(&spawn_thread, &pAttrs_spawn, sl_Task, NULL); if(status) { printError("Task create failed", status); } /* Turn NWP on - initialize the device*/ mode = sl_Start(0, 0, 0); if (mode < 0) { Display_printf(display, 0, 0,"\n\r[line:%d, error code:%d] %s\n\r", __LINE__, mode, DEVICE_ERROR); } switch(mode) { case 0: printf("\n"); printf("Role = STA i.e. Station"); printf("\n"); break; case 1: printf("Role = Reserved"); printf("\n"); break; case 2: printf("Role = AP i.e. Access Point"); printf("\n"); break; case 3: printf("Role = P2P i.e. WiFi Direct"); printf("\n"); break; case 4: printf("Role = Role_Tag i.e. Mane pan nathi khabar aa to..."); printf("\n"); break; default: printf("Somthing went wrong... No such role defined at SimpleLink Level"); printf("\n"); } if(mode != ROLE_STA) { /* Set NWP role as STA */ mode = sl_WlanSetMode(ROLE_STA); if (mode != 0) { Display_printf(display, 0, 0,"\n\r[line:%d, error code:%d] %s\n\r", __LINE__, mode, WLAN_ERROR); } /* For changes to take affect, we restart the NWP */ status = sl_Stop(SL_STOP_TIMEOUT); if (status < 0) { Display_printf(display, 0, 0,"\n\r[line:%d, error code:%d] %s\n\r", __LINE__, status, DEVICE_ERROR); } mode = sl_Start(0, 0, 0); if (mode < 0) { Display_printf(display, 0, 0,"\n\r[line:%d, error code:%d] %s\n\r", __LINE__, mode, DEVICE_ERROR); } } check_device_role(); // check device role after configuration.. config_STA_parameters(); // configure STA parameters and restart the device at the end of configuration for the changes to take place check_device_role(); // check device role after configuration... config_nw_parameters(); // configure network parameters like IP Address, Gateway, Submetmask etc... Connect(); printf("returned after connection attempt!!"); printf("\n"); while(flag_SlWlanEvent != 1) { unsigned int timer1 = 0; printf("Waiting for STA to get connected to proposed AP"); while(timer1 != 64000) { timer1++; } printf("\n"); } printf("\n\n"); printf("********************"); printf("\n\n"); printf("Connection done successfully... Attempting Ping to AP..."); printf("********************"); printf("\n\n"); first_ping_attempt(); second_ping_attempt(); do { printf("\n Second ping confirmation received... Awaiting socket creation..."); }while(flag_second_ping != 1); unsigned int retc1 = 0; retc1 = xTaskCreate( socket_task, /* Pointer to the function that implements the task. */ "Socket Task",/* Text name for the task. This is to facilitate debugging only. */ 1024, /* Stack depth - small microcontrollers will use much less stack than this. */ NULL, /* This example does not use the task parameter. */ 4, // This task will run at Maximum priority. NULL ); // This example does not use the task handle. if(retc1 != pdPASS) { printf("\n\n"); printf("Socket Task Creation failed!!!"); printf("\n\n"); while(1) { ; } } if(retc1 == pdPASS) { printf("\n\n"); printf("Socket Task Creation Succeeded !!!"); printf("\n\n"); } while(1); }