/* * ::accept() */ int accept(int socket, struct sockaddr *address, socklen_t *address_len) { SlSockAddr_t sl_address; SlSocklen_t sl_address_len; int result = sl_Accept(socket, &sl_address, &sl_address_len); if (address && address_len) { memcpy(address, &sl_address, *address_len); } if (result < 0) { switch (result) { default: errno = EINVAL; break; case SL_POOL_IS_EMPTY: usleep(10000); /* fall through */ case SL_EAGAIN: errno = EAGAIN; break; } return -1; } return result; }
static void telnet_wait_for_connection (void) { SlSocklen_t in_addrSize; sockaddr_in sClientAddress; // accepts a connection from a TCP client, if there is any, otherwise returns SL_EAGAIN telnet_data.n_sd = sl_Accept(telnet_data.sd, (SlSockAddr_t *)&sClientAddress, (SlSocklen_t *)&in_addrSize); if (telnet_data.n_sd == SL_EAGAIN) { return; } else { if (telnet_data.n_sd <= 0) { // error telnet_reset(); return; } // close the listening socket, we don't need it anymore servers_close_socket(&telnet_data.sd); // add the new socket to the network administration modusocket_socket_add(telnet_data.n_sd, false); // client connected, so go on telnet_data.rxWindex = 0; telnet_data.rxRindex = 0; telnet_data.txRetries = 0; telnet_data.state = E_TELNET_STE_CONNECTED; telnet_data.substate.connected = E_TELNET_STE_SUB_WELCOME; telnet_data.credentialsValid = true; telnet_data.logginRetries = 0; telnet_data.timeout = 0; } }
static int SockAccept(unsigned int slot) { if(!IS_SOCK_STARTED(slot) || IS_SOCK_CONNECTED(slot)) {RETURN_ERROR(ERROR_UNKNOWN, "Uninit fail")}; int addrsize = sizeof(SlSockAddrIn_t); int newid = sl_Accept(socket_state[slot].parent_id, (SlSockAddr_t*)&(socket_state[slot].addr_local), (SlSocklen_t *) &addrsize); if((newid != SL_EAGAIN) && (newid>=0)){ LOG(LOG_VERBOSE, "Socket %d: client connected.", slot); socket_state[slot].status |= SOCKET_CONNECTED; socket_state[slot].child_id = newid; //set to non-blocking again long nonblock = 1; int retval = sl_SetSockOpt(socket_state[slot].child_id, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &nonblock, sizeof(nonblock)); if(retval < 0){ sl_Close(socket_state[slot].child_id); RETURN_ERROR(retval, "Socket opt fail"); } if(slot == SOCKET_LOG){ mem_log_start_putchar(&ExtThread_LogSocketPutChar); serialconfig_start(&ExtThread_LogSocketGetChar); } } else if(newid != SL_EAGAIN){ sl_Close(socket_state[slot].child_id); sl_Close(socket_state[slot].parent_id); socket_state[slot].status = 0; RETURN_ERROR(newid, "Socket accept fail"); } return RET_SUCCESS; }
//--tested, working--// WiFiClient WiFiServer::available(byte* status) { // //Get a socket number from the wificlass // int clientSocketIndex = WiFiClass::getSocket(); if (clientSocketIndex == NO_SOCKET_AVAIL) { return WiFiClient(255); } // //create the client address structure to be filled in by sl_accept // SlSockAddrIn_t clientAddress = {0}; unsigned int clientAddressSize = sizeof(clientAddress); // //get the client handle, if there's a queued client. If no client, return 0 // int socketHandle = WiFiClass::_handleArray[_socketIndex]; int clientHandle = sl_Accept(socketHandle, (SlSockAddr_t*)&clientAddress, &clientAddressSize); // //We've successfully created a socket, so store everything in the wificlass //arrays used to keep track of the connected sockets, port #s, and types // if (clientHandle > 0) { WiFiClass::_handleArray[clientSocketIndex] = clientHandle; WiFiClass::_typeArray[clientSocketIndex] = TYPE_TCP_CONNECTED_CLIENT; WiFiClass::_portArray[clientSocketIndex] = sl_Htons(clientAddress.sin_port); WiFiClass::clients[clientSocketIndex] = WiFiClient(clientSocketIndex); } // //Now loop through the connected clients // uint8_t oneclient = 0; for(uint8_t i = 0; i < MAX_SOCK_NUM; i++) { if(WiFiClass::_handleArray[i] != -1 && WiFiClass::_typeArray[i] == TYPE_TCP_CONNECTED_CLIENT) { if( i == _lastServicedClient) { oneclient = 1; continue; } _lastServicedClient = i; return WiFiClass::clients[i]; } } if(oneclient) { return WiFiClass::clients[_lastServicedClient]; } _lastServicedClient = -1; return WiFiClient(255); }
//**************************************************************************** // //! \brief Opening a server side socket and receiving data //! //! This function opens a TCP socket in Listen mode and waits for an incoming //! TCP connection. If a socket connection is established then the function //! will try to read 1000 TCP packets from the connected client. //! //! \param[in] port number on which the server will be listening on //! //! \return 0 on success, -1 on Error. //! //! \note This function will wait for an incoming connection till one //! is established // //**************************************************************************** static int BsdTcpServer(UINT16 Port) { SlSockAddrIn_t Addr; SlSockAddrIn_t LocalAddr; int idx; int AddrSize; int SockID; int Status; int newSockID; long LoopCount = 0; long nonBlocking = 1; for (idx=0 ; idx<BUF_SIZE ; idx++) { uBuf.BsdBuf[idx] = (char)(idx % 10); } LocalAddr.sin_family = SL_AF_INET; LocalAddr.sin_port = sl_Htons((UINT16)Port); LocalAddr.sin_addr.s_addr = 0; SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( SockID < 0 ) { /* error */ return -1; } AddrSize = sizeof(SlSockAddrIn_t); Status = sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize); if( Status < 0 ) { /* error */ sl_Close(SockID); return -1; } Status = sl_Listen(SockID, 0); if( Status < 0 ) { sl_Close(SockID); return -1; } Status = sl_SetSockOpt(SockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlocking, sizeof(nonBlocking)); if( Status < 0 ) { return -1; } newSockID = SL_EAGAIN; while( newSockID < 0 ) { newSockID = sl_Accept(SockID, ( struct SlSockAddr_t *)&Addr, (SlSocklen_t*)&AddrSize); if( newSockID == SL_EAGAIN ) { /* Wait for 1 ms */ Delay(1); } else if( newSockID < 0 ) { sl_Close(SockID); return -1; } } while (LoopCount < TCP_PACKET_COUNT) { Status = sl_Recv(newSockID, uBuf.BsdBuf, BUF_SIZE, 0); if( Status <= 0 ) { /* error */ sl_Close(newSockID); sl_Close(SockID); return -1; } LoopCount++; } GPIO_IF_LedOn(MCU_EXECUTE_SUCCESS_IND); sl_Close(newSockID); sl_Close(SockID); return 0; }
int BsdTcpServer(unsigned short usPort) { UART_PRINT("BsdTcpServer\r\n"); while( wlanConnectStatus == 0); SlSockAddrIn_t sAddr; SlSockAddrIn_t sLocalAddr; int iCounter; int iAddrSize; int iSockID; int iStatus; int iNewSockID; unsigned long lLoopCount = 0; long lBytesSent = 0; long lNonBlocking = 0; //0 :non-blocking, int iTestBufLen; // filling the buffer for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++) { g_cBsdBuf[iCounter] = (char)(iCounter % 10) + '0'; } iTestBufLen = BUF_SIZE; //filling the TCP server socket address sLocalAddr.sin_family = SL_AF_INET; sLocalAddr.sin_port = sl_Htons((unsigned short)usPort); sLocalAddr.sin_addr.s_addr = 0; // sLocalAddr.sin_port = usPort; // sLocalAddr.sin_addr.s_addr = SL_IPV4_VAL(192,168,1,101); // creating a TCP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( iSockID < 0 ) { // UART_PRINT("error at creating a TCP socket ! \n\r"); // error return -1; } // UART_PRINT("iSockID :"); // Z_NumDispaly(iSockID, 2); iAddrSize = sizeof(SlSockAddrIn_t); // binding the TCP socket to the TCP server address iStatus = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize); if( iStatus < 0 ) { // UART_PRINT("error at binding the TCP socket to the TCP server address ! \n\r"); // error return -1; } // UART_PRINT("binding the TCP socket to the TCP server address ok! \n\r"); // putting the socket for listening to the incoming TCP connection iStatus = sl_Listen(iSockID, 0); if( iStatus < 0 ) { // UART_PRINT("error at putting the socket for listening to the incoming TCP connection ! \n\r"); return -1; } // UART_PRINT("listen end! \n\r"); // setting socket option to make the socket as non blocking iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking)); iNewSockID = SL_EAGAIN; char uttMessage[50]={0}; snprintf(uttMessage,sizeof(uttMessage),"IntTimes:%d, TickValue:%d,\n\r",IntTimes, SysTickValueGet()); UART_PRINT(uttMessage); serverCreatOK = 1; UART_PRINT(" waiting for an incoming TCP connection! \n\r"); // waiting for an incoming TCP connection while( iNewSockID < 0 ) { // accepts a connection form a TCP client, if there is any // otherwise returns SL_EAGAIN iNewSockID = sl_Accept(iSockID, ( struct SlSockAddr_t *)&sAddr, (SlSocklen_t*)&iAddrSize); if( iNewSockID == SL_EAGAIN ) { UtilsDelay(10000); char uttMessage[50]={0}; snprintf(uttMessage,sizeof(uttMessage),"IntTimes:%d, TickValue:%d,\n\r",IntTimes, SysTickValueGet()); UART_PRINT(uttMessage); // vTaskResume((xTaskHandle)&clientTaskHandle); vTaskSuspend((xTaskHandle)&ServerTaskHandle); // UART_PRINT(" iNewSockID == SL_EAGAIN! \n\r"); } /* else if( iNewSockID < 0 ) { // error UART_PRINT(" iNewSockID < 0! \n\r"); return -1; }*/ } char utttMessage[50]={0}; snprintf(utttMessage,sizeof(utttMessage),"IntTimes:%d, TickValue:%d,\n\r",IntTimes, SysTickValueGet()); UART_PRINT(utttMessage); UART_PRINT("connect succeed the new iSockID :"); Z_NumDispaly(iSockID, 5); unsigned long the_client_ip = sl_BIGtoLITTLE_l( (unsigned long)sAddr.sin_addr.s_addr ); UART_PRINT("the client ip is :"); Z_IPDispaly(&the_client_ip); unsigned short the_client_port = sl_BIGtoLITTLE_S( (unsigned short)sAddr.sin_port ); UART_PRINT("the client port is :"); Z_NumDispaly( (unsigned long)the_client_port,5); /* UART_PRINT(" waits for 1000 packets from the connected TCP client! \n\r"); // waits for 1000 packets from the connected TCP client while (lLoopCount < 1000) { iStatus = sl_Recv(iNewSockID, g_cBsdBuf, iTestBufLen, 0); if( iStatus <= 0 ) { // error return -1; } lLoopCount++; lBytesSent += iStatus; } */ /* // sending 3 packets to the TCP server while (lLoopCount < 3) { // sending packet // iStatus = sl_Send(iNewSockID, g_cBsdBuf, iTestBufLen, 0 ); char *send_buffer = "hellow i am cc3200 , welcome to wifi world !\n\r"; iStatus = sl_Send(iNewSockID, send_buffer, strlen(send_buffer), 0 ); if( iStatus <= 0 ) { UART_PRINT("error at sending packet\n\r"); Z_NumDispaly(lLoopCount,5); // error return -1; } lLoopCount++; lBytesSent += iStatus; } /* //#define SL_POLICY_CONNECTION (0x10) //#define SL_POLICY_SCAN (0x20) //#define SL_POLICY_PM (0x30) // while(1){ // Z_DelayS(1); // char OutMessage[50]={0}; // snprintf(OutMessage,sizeof(OutMessage),"IntTimes:%d, TickValue:%d,\n\r",IntTimes, SysTickValueGet()); // sl_Send(iNewSockID, OutMessage, strlen(OutMessage), 0 ); // } char *setbuffer= "\n set policy \n\r"; sl_Send(iNewSockID, setbuffer, strlen(setbuffer), 0 ); char recBuffer[2] = {0}; char numBuffer[15]={0}; while(1){ char sl_policy = '0'; char set_prar = '0'; char OutMessage[50] = {0}; char num = 0; while(1){ char temp_num = sl_Recv(iNewSockID, recBuffer, sizeof(recBuffer), 0); if(temp_num>0) num += temp_num; if(num == 1) sl_policy = recBuffer[0]-'0'; if(num == 3) set_prar = recBuffer[0]-'0'; if(num>3 && (recBuffer[0]=='\r' || recBuffer[0]=='\n')) break; if(num>3){ num = 0; char *send_buffer= "\nplease input again!!!\n\r"; sl_Send(iNewSockID, send_buffer, strlen(send_buffer), 0 );} snprintf(numBuffer,sizeof(numBuffer),"num:%d\n\r",num); UART_PRINT(numBuffer); } char *send_buffer= "\nfinished input!\n\r"; sl_Send(iNewSockID, send_buffer, strlen(send_buffer), 0 ); switch(sl_policy*16){ case SL_POLICY_CONNECTION://1 x snprintf(OutMessage,sizeof(OutMessage),"CONN= first:%d, second:%d,\n\r",sl_policy, set_prar); UART_PRINT(OutMessage); break; case SL_POLICY_SCAN://2 x snprintf(OutMessage,sizeof(OutMessage),"SCAN= first:%d, second:%d,\n\r",sl_policy, set_prar); if(set_prar == 0) sl_WlanPolicySet(SL_POLICY_SCAN,0,0,0); else sl_WlanPolicySet(SL_POLICY_SCAN,1,(unsigned char *)&set_prar,sizeof(set_prar)); UART_PRINT(OutMessage); break; case SL_POLICY_PM://3 x : x=0(normal),x=1(latency),x=2(low),x=3(always on), snprintf(OutMessage,sizeof(OutMessage),"PM= first:%d, second:%d,\n\r",sl_policy, set_prar); UART_PRINT(OutMessage); sl_WlanPolicySet(SL_POLICY_PM , set_prar, NULL,0); break; case 0x00://0 x :finished set break; case 0x40:{//4xx: hibernate char *send_buffer= "\nsl_hibernate!\n\r"; sl_Send(iNewSockID, send_buffer, strlen(send_buffer), 0 ); sl_Stop(0); break;} case 0x50:{//4xx: hibernate char *send_buffer= "\ndevice_hibernate!\n\r"; sl_Send(iNewSockID, send_buffer, strlen(send_buffer), 0 ); PRCMHibernateEnter(); break;} default: snprintf(OutMessage,sizeof(OutMessage),"Error= first:%d, second:%d,\n\r",sl_policy, set_prar); UART_PRINT(OutMessage); break; } if(sl_policy == 0) break; } Sl_WlanNetworkEntry_t netEntries[20]; char message[80]; /****no scan*********/ /* #define SL_SCAN_DISABLE 0 sl_WlanPolicySet(SL_POLICY_SCAN,SL_SCAN_DISABLE,0,0); while(1); return 0; UINT8 intervalInSeconds=1; while(1){ Z_DelayS(1); sl_WlanPolicySet(SL_POLICY_SCAN,SL_SCAN_POLICY_EN(1), (unsigned char *)&intervalInSeconds,sizeof(intervalInSeconds)); } */ /* char *noticebuffer= "\n set send message time \n\r"; sl_Send(iNewSockID, noticebuffer, strlen(noticebuffer), 0 ); while(1){ char temp_num = sl_Recv(iNewSockID, recBuffer, sizeof(recBuffer), 0); if(temp_num>0) break; } while(1){ //Get Scan Result UINT8 Index = sl_WlanGetNetworkList(0,20,&netEntries[0]); for(UINT8 i=0; i< Index; i++) { snprintf(message, 60, "%d) SSID %s RSSI %d \n\r",i,netEntries[i].ssid,netEntries[i].rssi); //UART_PRINT(message); sl_Send(iNewSockID, message, strlen(message), 0 ); } Z_DelayS(recBuffer[0]-'0'); } // close the connected socket after receiving from connected TCP client sl_Close(iNewSockID); // close the listening socket sl_Close(iSockID); return 0; */ }
void ControlServer(void *pvParameters) { char ServerBuffer[CONFIG_SERVER_BUFFER]; char MsgBuffer[100]; char *pMsgBuffer; SlSockAddrIn_t sAddr; SlSockAddrIn_t sLocalAddr; int32_t i32SockID; int32_t i32NewSockID; int32_t i32DataSize; int32_t i32NonBlocking = 1; SlSocklen_t i32AddrSize; int32_t retval; pMsgBuffer = &MsgBuffer[0]; InitVariables(); retval = ResetSimpleLink(); if (retval < 0) while (1) ; WlanConnect(); sprintf(MsgBuffer, "Connectado a rede: %s\n\r" "IP: %d.%d.%d.%d\n\r" "Gateway: %d.%d.%d.%d\n\r", SL_IPV4_BYTE(g_sSLCon.DeviceIP, 3), SL_IPV4_BYTE(g_sSLCon.DeviceIP, 2), SL_IPV4_BYTE(g_sSLCon.DeviceIP, 1), SL_IPV4_BYTE(g_sSLCon.DeviceIP, 0), SL_IPV4_BYTE(g_sSLCon.GatewayIP, 3), SL_IPV4_BYTE(g_sSLCon.GatewayIP, 2), SL_IPV4_BYTE(g_sSLCon.GatewayIP, 1), SL_IPV4_BYTE(g_sSLCon.GatewayIP, 0)); osi_MsgQWrite(&g_sUartQuee, &pMsgBuffer, OSI_NO_WAIT); sLocalAddr.sin_family = SL_AF_INET; sLocalAddr.sin_port = sl_Htons((unsigned short) g_sSLCon.PortNumber); sLocalAddr.sin_addr.s_addr = 0; i32SockID = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0); sl_Bind(i32SockID, (SlSockAddr_t *) &sLocalAddr, sizeof(SlSockAddrIn_t)); sl_Listen(i32SockID, 0); sl_SetSockOpt(i32SockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &i32NonBlocking, sizeof(i32NonBlocking)); while (1) { i32NewSockID = SL_EAGAIN; while (i32NewSockID < 0) { i32NewSockID = sl_Accept(i32SockID, (struct SlSockAddr_t *) &sAddr, (SlSocklen_t*) &i32AddrSize); if (i32NewSockID == SL_EAGAIN) { osi_Sleep(100); } else if (i32NewSockID < 0) { while (1) { } } } i32DataSize = sl_Recv(i32NewSockID, ServerBuffer, CONFIG_SERVER_BUFFER, 0); if (strcmp(ServerBuffer, "Led On") == 0) { Led_Green(LED_ON); strcpy(ServerBuffer, "OK"); i32DataSize = 3; } else if (strcmp(ServerBuffer, "Led Off") == 0) { Led_Green(LED_OFF); strcpy(ServerBuffer, "OK"); i32DataSize = 3; } else if (strcmp(ServerBuffer, "Lamp On") == 0) { MAP_GPIOPinWrite(GPIOA0_BASE, GPIO_PIN_6, GPIO_PIN_6); strcpy(ServerBuffer, "OK"); i32DataSize = 3; } else if (strcmp(ServerBuffer, "Lamp Off") == 0) { MAP_GPIOPinWrite(GPIOA0_BASE, GPIO_PIN_6, 0); strcpy(ServerBuffer, "OK"); i32DataSize = 3; } sl_Send(i32NewSockID, ServerBuffer, i32DataSize, 0); sl_Close(i32NewSockID); } }
//**************************************************************************** // //! \brief Opening a server side socket and receiving data //! //! This function opens a TCP socket in Listen mode and waits for an incoming //! TCP connection. If a socket connection is established then the function //! will try to read 1000 TCP packets from the connected client. //! //! \param[in] port number on which the server will be listening on //! //! \return 0 on success, -1 on Error. //! //! \note This function will wait for an incoming connection till one //! is established // //**************************************************************************** static int BsdTcpServer(unsigned short Port) { SlSockAddrIn_t Addr; SlSockAddrIn_t LocalAddr; int idx; int AddrSize; int SockID; int Status; int newSockID; long LoopCount = 0; long nonBlocking = 1; for (idx=0 ; idx<BUF_SIZE ; idx++) { uBuf.BsdBuf[idx] = (char)(idx % 10); } LocalAddr.sin_family = SL_AF_INET; LocalAddr.sin_port = sl_Htons((unsigned short)Port); LocalAddr.sin_addr.s_addr = 0; SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); ASSERT_ON_ERROR(SockID); AddrSize = sizeof(SlSockAddrIn_t); Status = sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, AddrSize); if( Status < 0 ) { /* error */ sl_Close(SockID); ASSERT_ON_ERROR(Status); } Status = sl_Listen(SockID, 0); if( Status < 0 ) { sl_Close(SockID); ASSERT_ON_ERROR(Status); } Status = sl_SetSockOpt(SockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlocking, sizeof(nonBlocking)); ASSERT_ON_ERROR(Status); newSockID = SL_EAGAIN; while( newSockID < 0 && IS_IP_ACQUIRED(g_ulStatus)) { newSockID = sl_Accept(SockID, ( struct SlSockAddr_t *)&Addr, (SlSocklen_t*)&AddrSize); if( newSockID == SL_EAGAIN ) { /* Wait for 1 ms */ Delay(1); } else if( newSockID < 0 ) { sl_Close(SockID); ASSERT_ON_ERROR(newSockID); } } if(! IS_IP_ACQUIRED(g_ulStatus)) { return CLIENT_DISCONNECTED; } // run 'iperf -c <device IP> -i 1 -t 10000' command on PC/Smartphone while (LoopCount < TCP_PACKET_COUNT) { Status = sl_Recv(newSockID, uBuf.BsdBuf, BUF_SIZE, 0); if( Status <= 0 ) { /* error */ ASSERT_ON_ERROR(sl_Close(newSockID)); ASSERT_ON_ERROR(sl_Close(SockID)); ASSERT_ON_ERROR(Status); } LoopCount++; } GPIO_IF_LedOn(MCU_EXECUTE_SUCCESS_IND); ASSERT_ON_ERROR(sl_Close(newSockID)); ASSERT_ON_ERROR(sl_Close(SockID)); return SUCCESS; }
//***************************************************************************** // //! This function opens a TCP socket in Listen mode and waits for an incoming //! TCP connection.. If a socket connection is established then the function //! will try to read 1000 TCP packets from the connected client. //! //! \param port number on which the server will be listening on //! //! \return 0 on success, -ve on Error. //! //***************************************************************************** static long BsdTcpServer(unsigned short Port) { SlSockAddrIn_t Addr; SlSockAddrIn_t LocalAddr; int indexCount,iAddrSize,SockID,iStatus,newSockID; long LoopCount = 0; long nonBlocking = 1; SlTimeval_t timeVal; for (indexCount=0 ; indexCount<BUF_SIZE ; indexCount++) { g_uBuf.cBsdBuf[indexCount] = (char)(indexCount % 10); } LocalAddr.sin_family = SL_AF_INET; LocalAddr.sin_port = sl_Htons((unsigned short)Port); LocalAddr.sin_addr.s_addr = 0; SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); ASSERT_ON_ERROR(SockID); iAddrSize = sizeof(SlSockAddrIn_t); iStatus = sl_Bind(SockID, (SlSockAddr_t *)&LocalAddr, iAddrSize); if( iStatus < 0 ) { // error ASSERT_ON_ERROR(sl_Close(SockID)); ASSERT_ON_ERROR(iStatus); } iStatus = sl_Listen(SockID, 0); if( iStatus < 0 ) { sl_Close(SockID); ASSERT_ON_ERROR(iStatus); } iStatus = sl_SetSockOpt(SockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, \ &nonBlocking, sizeof(nonBlocking)); newSockID = SL_EAGAIN; while( newSockID < 0 ) { newSockID = sl_Accept(SockID, ( struct SlSockAddr_t *)&Addr, (SlSocklen_t*)&iAddrSize); if( newSockID == SL_EAGAIN ) { MAP_UtilsDelay(80000); } else if( newSockID < 0 ) { ASSERT_ON_ERROR(sl_Close(SockID)); // error ASSERT_ON_ERROR(newSockID); } } timeVal.tv_sec = 1; timeVal.tv_usec = 0; if( newSockID >= 0 ) { iStatus = sl_SetSockOpt(newSockID, SL_SOL_SOCKET, SL_SO_RCVTIMEO, &timeVal, sizeof(timeVal)); ASSERT_ON_ERROR(iStatus); } while (LoopCount < PACKET_COUNT) { iStatus = sl_Recv(newSockID, g_uBuf.cBsdBuf, BUF_SIZE, 0); if( iStatus <= 0 ) { // error ASSERT_ON_ERROR(sl_Close(newSockID)); ASSERT_ON_ERROR(sl_Close(SockID)); ASSERT_ON_ERROR(iStatus); } LoopCount++; } ASSERT_ON_ERROR(sl_Close(newSockID)); ASSERT_ON_ERROR(sl_Close(SockID)); return 0; }
//**************************************************************************** // //! \brief Opening a TCP server side socket and receiving data //! //! This function opens a TCP socket in Listen mode and waits for an incoming //! TCP connection. //! If a socket connection is established then the function will try to read //! 1000 TCP packets from the connected client. //! //! \param[in] port number on which the server will be listening on //! //! \return 0 on success, -1 on error. //! //! \note This function will wait for an incoming connection till //! one is established // //**************************************************************************** int BsdTcpServer(unsigned short usPort) { SlSockAddrIn_t sAddr; SlSockAddrIn_t sLocalAddr; int iCounter; int iAddrSize; int iSockID; int iStatus; int iNewSockID; unsigned long lLoopCount = 0; long lBytesSent = 0; long lNonBlocking = 1; int iTestBufLen; // filling the buffer for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++) { g_cBsdBuf[iCounter] = (char)(iCounter % 10) + '0'; } iTestBufLen = BUF_SIZE; //filling the TCP server socket address sLocalAddr.sin_family = SL_AF_INET; sLocalAddr.sin_port = sl_Htons((unsigned short)usPort); sLocalAddr.sin_addr.s_addr = 0; // sLocalAddr.sin_port = usPort; // sLocalAddr.sin_addr.s_addr = SL_IPV4_VAL(192,168,1,101); // creating a TCP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( iSockID < 0 ) { UART_PRINT("error at creating a TCP socket ! \n\r"); // error return -1; } UART_PRINT("iSockID :"); Z_NumDispaly(iSockID, 2); iAddrSize = sizeof(SlSockAddrIn_t); // binding the TCP socket to the TCP server address iStatus = sl_Bind(iSockID, (SlSockAddr_t *)&sLocalAddr, iAddrSize); if( iStatus < 0 ) { UART_PRINT("error at binding the TCP socket to the TCP server address ! \n\r"); // error return -1; } UART_PRINT("binding the TCP socket to the TCP server address ok! \n\r"); // putting the socket for listening to the incoming TCP connection iStatus = sl_Listen(iSockID, 0); if( iStatus < 0 ) { UART_PRINT("error at putting the socket for listening to the incoming TCP connection ! \n\r"); return -1; } UART_PRINT("listen end! \n\r"); // setting socket option to make the socket as non blocking iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking)); iNewSockID = SL_EAGAIN; UART_PRINT(" waiting for an incoming TCP connection! \n\r"); // waiting for an incoming TCP connection while( iNewSockID < 0 ) { // accepts a connection form a TCP client, if there is any // otherwise returns SL_EAGAIN iNewSockID = sl_Accept(iSockID, ( struct SlSockAddr_t *)&sAddr, (SlSocklen_t*)&iAddrSize); if( iNewSockID == SL_EAGAIN ) { UtilsDelay(10000); // UART_PRINT(" iNewSockID == SL_EAGAIN! \n\r"); } else if( iNewSockID < 0 ) { // error UART_PRINT(" iNewSockID < 0! \n\r"); return -1; } } UART_PRINT("connect succeed the new iSockID :"); Z_NumDispaly(iSockID, 5); unsigned long the_client_ip = sl_BIGtoLITTLE_l( (unsigned long)sAddr.sin_addr.s_addr ); UART_PRINT("the client ip is :"); Z_IPDispaly(&the_client_ip); unsigned short the_client_port = sl_BIGtoLITTLE_S( (unsigned short)sAddr.sin_port ); UART_PRINT("the client port is :"); Z_NumDispaly( (unsigned long)the_client_port,5); /* UART_PRINT(" waits for 1000 packets from the connected TCP client! \n\r"); // waits for 1000 packets from the connected TCP client while (lLoopCount < 1000) { iStatus = sl_Recv(iNewSockID, g_cBsdBuf, iTestBufLen, 0); if( iStatus <= 0 ) { // error return -1; } lLoopCount++; lBytesSent += iStatus; } */ // sending 3 packets to the TCP server while (lLoopCount < 3) { // sending packet // iStatus = sl_Send(iNewSockID, g_cBsdBuf, iTestBufLen, 0 ); char *send_buffer = "hellow i am cc3200 , welcome to wifi world !\n\r"; iStatus = sl_Send(iNewSockID, send_buffer, strlen(send_buffer), 0 ); if( iStatus <= 0 ) { UART_PRINT("error at sending packet\n\r"); Z_NumDispaly(lLoopCount,5); // error return -1; } lLoopCount++; lBytesSent += iStatus; } Sl_WlanNetworkEntry_t netEntries[20]; char message[80]; unsigned long intervalInSeconds = 10; sl_WlanPolicySet(SL_POLICY_SCAN,SL_SCAN_POLICY_EN(1), (unsigned char *)&intervalInSeconds,sizeof(intervalInSeconds)); while(1){ //Get Scan Result UINT8 Index = sl_WlanGetNetworkList(0,20,&netEntries[0]); for(UINT8 i=0; i< Index; i++) { snprintf(message, 60, "%d) SSID %s RSSI %d \n\r",i,netEntries[i].ssid,netEntries[i].rssi); UART_PRINT(message); sl_Send(iNewSockID, message, strlen(message), 0 ); } Z_DelayS(3); } // close the connected socket after receiving from connected TCP client sl_Close(iNewSockID); // close the listening socket sl_Close(iSockID); return 0; }