Skip to content

Commit

Permalink
Merge pull request ARMmbed#68 from linlingao/debug_dns_2
Browse files Browse the repository at this point in the history
Fix DNS issue caused by recvfrom
  • Loading branch information
linlingao committed Sep 20, 2018
2 parents af2d40a + 944bb06 commit a43eec6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#define CONNECT_TIMEOUT_MS 2000
#define IP_SET_TIMEOUT_MS 2000

CC3220SFInterface * CC3220SFInterface::cc3200sf_wifi_instance;

CC3220SFInterface::CC3220SFInterface():
Expand Down Expand Up @@ -232,8 +233,8 @@ struct cc3200_socket {
int sd;
nsapi_protocol_t proto;
bool connected;
// SocketAddress addr;
int keepalive; // TCP
void (*cb)(void *);
void *data;
};

int CC3220SFInterface::socket_open(void **handle, nsapi_protocol_t proto)
Expand All @@ -253,7 +254,6 @@ int CC3220SFInterface::socket_open(void **handle, nsapi_protocol_t proto)
socket->sd = sd;
socket->proto = proto;
socket->connected = false;
socket->keepalive = 0;
*handle = socket;
return 0;
}
Expand Down Expand Up @@ -446,4 +446,9 @@ int CC3220SFInterface::socket_recvfrom(void *handle, SocketAddress *address, voi

void CC3220SFInterface::socket_attach(void *handle, void (*callback)(void *), void *data)
{
struct cc3200_socket *s = (struct cc3200_socket *)handle;

s->cb = callback;
s->data = data;
// TODO: invoke callback functions
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/


#include <stdlib.h>
#include "cc3200_simplelink.h"
//#include "Callback.h"
//#include "mbed_error.h"
Expand Down Expand Up @@ -48,7 +48,8 @@ CC3200_SIMPLELINK::CC3200_SIMPLELINK()
_closed(false),
_connection_status(NSAPI_STATUS_DISCONNECTED),
_connected_ssid(),
_connected_channel(0)
_connected_channel(0),
_timeout(CC3200_SIMPLELINK_MISC_TIMEOUT)
{
memset(_ip_buffer, 0, sizeof(_ip_buffer));
memset(_gateway_buffer, 0, sizeof(_gateway_buffer));
Expand Down Expand Up @@ -900,8 +901,10 @@ int32_t CC3200_SIMPLELINK::send(int sd, const void *data, uint32_t size)

int32_t CC3200_SIMPLELINK::recv(int sd, void *data, uint32_t size)
{
uint32_t rcvd_bytes = 0;
int32_t status;
uint32_t rcvd_bytes = 0;
int32_t status;

memset((void *)data, 0, size);

while(rcvd_bytes < size)
{
Expand All @@ -928,11 +931,41 @@ int32_t CC3200_SIMPLELINK::recv(int sd, void *data, uint32_t size)

int32_t CC3200_SIMPLELINK::recvfrom(uint32_t sd, bool ipv6, void * buf, uint32_t size, uint8_t *ipAddr, uint32_t portNumber)
{
sockAddr_t sAddr;
SlSockAddr_t* sa;
int32_t addrSize;
int32_t status = -1;
sockAddr_t sAddr;
SlSockAddr_t* sa;
int32_t addrSize;
int32_t status = -1;
short int nonBlocking = 1;
struct SlTimeval_t TimeVal;
uint16_t nb_counter = 0;

if (1 == nonBlocking)
{
// non-blocking
status = sl_SetSockOpt((short int)sd, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlocking, sizeof(nonBlocking));
if(status < 0)
{
printf("sl_SetSockOpt failed with %d\n\r", (int)status);
return NSAPI_ERROR_DEVICE_ERROR;
}
}
else
{
/* In case of blocking, a timeout for sl_RecvFrom will be set to TimeVal
* When timeout is expired sl_RecvFrom will return SL_ERROR_BSD_EAGAIN */
TimeVal.tv_sec = _timeout;
TimeVal.tv_usec = 0;
status = sl_SetSockOpt(sd,SL_SOL_SOCKET,SL_SO_RCVTIMEO,
(uint8_t *)&TimeVal,
sizeof(TimeVal));
if(status < 0)
{
printf ("sl_SetSockOpt failed with %d\n", (int)status);
return NSAPI_ERROR_DEVICE_ERROR;
}
}

memset((void*)buf, 0, size);
memset((void*)&sAddr, 0, sizeof(sAddr));
if (ipv6)
{
Expand All @@ -958,13 +991,27 @@ int32_t CC3200_SIMPLELINK::recvfrom(uint32_t sd, bool ipv6, void * buf, uint32_t

uint32_t rcvd_bytes = 0;

while(rcvd_bytes < size)
while(1)
{
status = sl_RecvFrom(sd, buf, size, 0, sa, (SlSocklen_t*)&addrSize);
if(status == SL_ERROR_BSD_EAGAIN)
{
wait_ms(1);
continue;
if (1 == nonBlocking)
{
wait_ms(1);
nb_counter ++;
if (nb_counter < _timeout)
{
continue;
}
printf ("Timeout expired before receiving packet\n");
return NSAPI_ERROR_TIMEOUT;
}
else
{
printf ("Timeout expired before receiving packet\n");
return NSAPI_ERROR_TIMEOUT;
}
}
else if(status <= 0)
{
Expand All @@ -974,7 +1021,13 @@ int32_t CC3200_SIMPLELINK::recvfrom(uint32_t sd, bool ipv6, void * buf, uint32_t
else
{
rcvd_bytes += status;
break;
}
}
return rcvd_bytes;
}

void CC3200_SIMPLELINK::setTimeout(uint32_t timeout_ms)
{
_timeout = timeout_ms;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,6 @@
#define MAX_SCAN_ENTRIES (15)
#define MAX_SCAN_ATTEMPTS (10)

#ifndef CC3200_SIMPLELINK_CONNECT_TIMEOUT
#define CC3200_SIMPLELINK_CONNECT_TIMEOUT 15000
#endif
#ifndef CC3200_SIMPLELINK_SEND_TIMEOUT
#define CC3200_SIMPLELINK_SEND_TIMEOUT 2000
#endif
#ifndef CC3200_SIMPLELINK_RECV_TIMEOUT
#define CC3200_SIMPLELINK_RECV_TIMEOUT 2000
#endif
#ifndef CC3200_SIMPLELINK_MISC_TIMEOUT
#define CC3200_SIMPLELINK_MISC_TIMEOUT 2000
#endif
Expand Down Expand Up @@ -326,6 +317,7 @@ class CC3200_SIMPLELINK
char _connected_ssid[SL_WLAN_SSID_MAX_LENGTH+1]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
int _connected_channel;
SlWlanNetworkEntry_t netEntries[MAX_SCAN_ENTRIES];
int _timeout;
};
#endif /* CC3200_SIMPLELINK_H_ */

Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ void PowerCC32XX_setParkState(PowerCC32XX_Pin pin, uint32_t level)
DebugP_assert(PowerCC32XX_config.numPins < PowerCC32XX_NUMPINS + 1);

/* first check if level indicates "don't park" */
if (level == ~1) {
if (level == (uint32_t)~1) {
state = PowerCC32XX_DONT_PARK;
}

Expand Down

0 comments on commit a43eec6

Please sign in to comment.