// //--tested, working--// //bit of a misnomer. This waits to receive a packet and then stores it in a buffer //it is important that this is called before any of the read or available commands //are called. This does the actual work. Read, peek, etc. are just organizational. // int WiFiUDP::parsePacket() { // //make sure we actually have a socket // if (_socketIndex == NO_SOCKET_AVAIL) { return 0; } // //the sl_select command blocks until something interesting happens or //it times out (current timeout set for 10 ms, the minimum) // SlTimeval_t timeout; timeout.tv_sec = 0; timeout.tv_usec = 10000; int socketHandle = WiFiClass::_handleArray[_socketIndex]; SlFdSet_t readSocketHandles, errorSocketHandles; SL_FD_ZERO(&readSocketHandles); SL_FD_ZERO(&errorSocketHandles); SL_FD_SET(socketHandle, &readSocketHandles); SL_FD_SET(socketHandle, &errorSocketHandles); int iRet = sl_Select(socketHandle+1, &readSocketHandles, NULL, &errorSocketHandles, &timeout); if (iRet <= 0) { return 0; } // //Since we've reached this point, the sl_select command has indicated //that either we're going to get an error, or an immediate read // SlSockAddrIn_t address = {0}; int AddrSize = sizeof(address); int bytes = sl_RecvFrom(socketHandle, rx_buf, UDP_RX_PACKET_MAX_SIZE, NULL, (SlSockAddr_t*)&address, (SlSocklen_t*)&AddrSize); // //store the sender's address (sl_HtonX reorders bits to processor order) //!! Although this follows some examples (upd_socket), it goes against the //!! API documentation. The API maintains that the 5th arg to RecvFrom is not in/out // _remoteIP = address.sin_addr.s_addr; _remotePort = sl_Htons(address.sin_port); // //If an error occured, return 0, otherwise return the byte length of the packet //and reset the buffer index counter and fill level variables // if (bytes < 0) { rx_fillLevel = 0; return 0; } else { rx_currentIndex = 0; rx_fillLevel = bytes; return bytes; } }
void modusocket_enter_sleep (void) { fd_set socketset; int16_t maxfd = 0; for (int i = 0; i < MOD_NETWORK_MAX_SOCKETS; i++) { int16_t sd; if ((sd = modusocket_sockets[i].sd) >= 0) { FD_SET(sd, &socketset); maxfd = (maxfd > sd) ? maxfd : sd; } } // wait for any of the sockets to become ready... sl_Select(maxfd + 1, &socketset, NULL, NULL, NULL); }
int cc3200_write(Network* n, unsigned char* buffer, int len, int timeout_ms) { SlTimeval_t timeVal; SlFdSet_t fdset; int rc = 0; int readySock; SL_FD_ZERO(&fdset); SL_FD_SET(n->my_socket, &fdset); timeVal.tv_sec = 0; timeVal.tv_usec = timeout_ms * 1000; do { readySock = sl_Select(n->my_socket + 1, NULL, &fdset, NULL, &timeVal); } while(readySock != 1); rc = sl_Send(n->my_socket, buffer, len, 0); return rc; }
int cc3200_read(Network* n, unsigned char* buffer, int len, int timeout_ms) { SlTimeval_t timeVal; SlFdSet_t fdset; int rc = 0; int recvLen = 0; SL_FD_ZERO(&fdset); SL_FD_SET(n->my_socket, &fdset); timeVal.tv_sec = 0; timeVal.tv_usec = timeout_ms * 1000; if (sl_Select(n->my_socket + 1, &fdset, NULL, NULL, &timeVal) == 1) { do { rc = sl_Recv(n->my_socket, buffer + recvLen, len - recvLen, 0); recvLen += rc; } while(recvLen < len); } return recvLen; }