Skip to content

Commit

Permalink
Time support w/out NTP, UDP Write events:
Browse files Browse the repository at this point in the history
- Added support for time outside the NTP client.
- forcing UDP Write events when there is enough space in the socket buffer (solves integration
issues with latest cesanta/fossa).
  • Loading branch information
Daniele Lacamera committed Jul 3, 2015
1 parent 89fb667 commit 1a03910
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
34 changes: 31 additions & 3 deletions pico_bsd_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Author: Maxime Vincent, Daniele Lacamera
#include "pico_config.h" /* for zalloc and free */
#include "pico_bsd_sockets.h"
#include "pico_osal.h"
#ifdef PICO_SUPPORT_SNTP_CLIENT
#include "pico_sntp_client.h"
#endif

#include <errno.h> /* should be there in C99 */

Expand Down Expand Up @@ -1252,7 +1254,6 @@ int pico_gettimeofday(struct timeval *tv, struct timezone *tz)
{
int ret;
(void)tz;

struct pico_timeval ptv;

ret= pico_sntp_gettimeofday(&ptv);
Expand All @@ -1261,6 +1262,29 @@ int pico_gettimeofday(struct timeval *tv, struct timezone *tz)
tv->tv_usec= ptv.tv_msec * 1000; /* pico_timeval uses milliseconds instead of microseconds */
return ret;
}
#else

static struct pico_timeval ptv = {0u,0u};

int pico_gettimeofday(struct timeval *tv, struct timezone *tz)
{
int ret;
(void)tz;

tv->tv_sec = ptv.tv_sec;
tv->tv_usec= ptv.tv_msec * 1000; /* pico_timeval uses milliseconds instead of microseconds */
return 0;
}

int pico_settimeofday(struct timeval *tv, struct timezone *tz)
{
int ret;
(void)tz;

ptv.tv_sec = tv->tv_sec;
ptv.tv_msec= tv->tv_usec / 1000; /* pico_timeval uses milliseconds instead of microseconds */
return 0;
}

long XTIME(void) {
struct timeval t;
Expand Down Expand Up @@ -1348,8 +1372,12 @@ int pico_pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
PICO_FD_SET(i, &readfds_out);
}

/* WRITE event needed and available? */
if (writefds && PICO_FD_ISSET(i,writefds) && (ep->revents & (PICO_SOCK_EV_WR)))
/* Force write events on empty udp sockets */
if ((ep->proto == PICO_PROTO_UDP) && (ep->s->q_out.size < ep->s->q_out.max_size))
ep->revents |= PICO_SOCK_EV_WR;

/* WRITE event needed? and available? */
if (writefds && PICO_FD_ISSET(i,writefds) && (ep->revents & (PICO_SOCK_EV_WR)))
{
bsd_dbg_select("- WRITE_EV - ");
nfds_out++;
Expand Down
16 changes: 12 additions & 4 deletions pico_bsd_sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,20 @@ int pico_poll (struct pollfd *pfd, nfds_t npfd, int timeout);
int pico_ppoll (struct pollfd *pfd, nfds_t npfd, const struct timespec *timeout_ts, const sigset_t *sigmask);


#ifdef PICO_SUPPORT_SNTP_CLIENT
int pico_gettimeofday(struct timeval *tv, struct timezone *tz);
long XTIME(void);
#define XGMTIME gmtime
#ifndef PICO_SUPPORT_SNTP_CLIENT
struct pico_timeval
{
pico_time tv_sec;
pico_time tv_msec;
};
int pico_settimeofday(struct timeval *tv, struct timezone *tz);
#endif


int pico_gettimeofday(struct timeval *tv, struct timezone *tz);
long XTIME(void);
#define XGMTIME gmtime

/* arpa/inet.h */
const char *pico_inet_ntop (int af, const void *src, char *dst, socklen_t size);
char *pico_inet_ntoa (struct in_addr in);
Expand Down
8 changes: 6 additions & 2 deletions pico_bsd_syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,16 @@ static inline int ppoll(struct pollfd *pfd, nfds_t npfd, const struct timespec *
return pico_ppoll(pfd, npfd, timeout_ts, sigmask);
}

#ifdef PICO_SUPPORT_SNTP_CLIENT
static int gettimeofday(struct timeval *tv, struct timezone *tz)
{
return pico_gettimeofday(tv, tz);
}
#endif

static int settimeofday(struct timeval *tv, struct timezone *tz)
{
return pico_settimeofday(tv, tz);
}


static inline const char *inet_ntop(int af, const void *src, char *dst, socklen_t size)
{
Expand Down

0 comments on commit 1a03910

Please sign in to comment.