Skip to content

Commit

Permalink
pre-commit: Include re-styled src/coap_[t-z].c files to checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdeep1 committed Jul 14, 2023
1 parent 755845c commit 7a5d13a
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 361 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ repos:
rev: v0.9.0
hooks:
- id: astyle_py
files: '^(coap_config|man|include|src/oscore|src/coap_[a-s]|src/coap_oscore|examples|tests).*\.(c|cpp|cxx|h|h.in|h.riot|h.riot.in|h.windows|h.windows.in|h.contiki|hpp|inc)$'
files: '^.*\.(c|cpp|cxx|h|h.in|h.riot|h.riot.in|h.windows|h.windows.in|h.contiki|hpp|inc)$'
exclude: '^.*/(coap_uthash_internal.h|coap_utlist_internal.h)$'
args: ['--style=google',
'--align-pointer=name',
Expand Down
102 changes: 48 additions & 54 deletions src/coap_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# define OPTVAL_GT(t) (t)
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#include <sys/ioctl.h>
#endif
#ifdef HAVE_WS2TCPIP_H
#include <ws2tcpip.h>
Expand Down Expand Up @@ -54,15 +54,14 @@ coap_socket_connect_tcp1(coap_socket_t *sock,
u_long u_on = 1;
#endif
coap_address_t connect_addr;
coap_address_copy( &connect_addr, server );
coap_address_copy(&connect_addr, server);

sock->flags &= ~COAP_SOCKET_CONNECTED;
sock->fd = socket(server->addr.sa.sa_family, SOCK_STREAM, 0);

if (sock->fd == COAP_INVALID_SOCKET) {
coap_log_warn(
"coap_socket_connect_tcp1: socket: %s\n",
coap_socket_strerror());
coap_log_warn("coap_socket_connect_tcp1: socket: %s\n",
coap_socket_strerror());
goto error;
}

Expand All @@ -72,9 +71,8 @@ coap_socket_connect_tcp1(coap_socket_t *sock,
#else
if (ioctl(sock->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) {
#endif
coap_log_warn(
"coap_socket_connect_tcp1: ioctl FIONBIO: %s\n",
coap_socket_strerror());
coap_log_warn("coap_socket_connect_tcp1: ioctl FIONBIO: %s\n",
coap_socket_strerror());
}
#endif /* RIOT_VERSION */

Expand All @@ -91,10 +89,10 @@ coap_socket_connect_tcp1(coap_socket_t *sock,
connect_addr.addr.sin6.sin6_port = htons(default_port);
#ifndef RIOT_VERSION
/* Configure the socket as dual-stacked */
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off), sizeof(off)) == COAP_SOCKET_ERROR)
coap_log_warn(
"coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
coap_socket_strerror());
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) == COAP_SOCKET_ERROR)
coap_log_warn("coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n",
coap_socket_strerror());
#endif /* RIOT_VERSION */
break;
#endif /* COAP_IPV6_SUPPORT */
Expand All @@ -110,17 +108,16 @@ coap_socket_connect_tcp1(coap_socket_t *sock,
if (local_if && local_if->addr.sa.sa_family) {
coap_address_copy(local_addr, local_if);
if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on), sizeof(on)) == COAP_SOCKET_ERROR)
coap_log_warn(
"coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
coap_socket_strerror());
coap_log_warn("coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n",
coap_socket_strerror());
if (bind(sock->fd, &local_if->addr.sa,
#if COAP_IPV4_SUPPORT
local_if->addr.sa.sa_family == AF_INET ?
(socklen_t)sizeof(struct sockaddr_in) :
(socklen_t)sizeof(struct sockaddr_in) :
#endif /* COAP_IPV4_SUPPORT */
(socklen_t)local_if->size) == COAP_SOCKET_ERROR) {
(socklen_t)local_if->size) == COAP_SOCKET_ERROR) {
coap_log_warn("coap_socket_connect_tcp1: bind: %s\n",
coap_socket_strerror());
coap_socket_strerror());
goto error;
}
} else {
Expand All @@ -142,18 +139,18 @@ coap_socket_connect_tcp1(coap_socket_t *sock,
return 1;
}
coap_log_warn("coap_socket_connect_tcp1: connect: %s\n",
coap_socket_strerror());
coap_socket_strerror());
goto error;
}

if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) {
coap_log_warn("coap_socket_connect_tcp1: getsockname: %s\n",
coap_socket_strerror());
coap_socket_strerror());
}

if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) {
coap_log_warn("coap_socket_connect_tcp1: getpeername: %s\n",
coap_socket_strerror());
coap_socket_strerror());
}

sock->flags |= COAP_SOCKET_CONNECTED;
Expand All @@ -170,35 +167,34 @@ coap_socket_connect_tcp2(coap_socket_t *sock,
coap_address_t *remote_addr) {
int error = 0;
#ifdef _WIN32
int optlen = (int)sizeof( error );
int optlen = (int)sizeof(error);
#else
socklen_t optlen = (socklen_t)sizeof( error );
socklen_t optlen = (socklen_t)sizeof(error);
#endif

sock->flags &= ~(COAP_SOCKET_WANT_CONNECT | COAP_SOCKET_CAN_CONNECT);

if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, OPTVAL_GT(&error),
&optlen) == COAP_SOCKET_ERROR) {
&optlen) == COAP_SOCKET_ERROR) {
coap_log_warn("coap_socket_finish_connect_tcp: getsockopt: %s\n",
coap_socket_strerror());
coap_socket_strerror());
}

if (error) {
coap_log_warn(
"coap_socket_finish_connect_tcp: connect failed: %s\n",
coap_socket_format_errno(error));
coap_log_warn("coap_socket_finish_connect_tcp: connect failed: %s\n",
coap_socket_format_errno(error));
coap_socket_close(sock);
return 0;
}

if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) {
coap_log_warn("coap_socket_connect_tcp: getsockname: %s\n",
coap_socket_strerror());
coap_socket_strerror());
}

if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) {
coap_log_warn("coap_socket_connect_tcp: getpeername: %s\n",
coap_socket_strerror());
coap_socket_strerror());
}

return 1;
Expand All @@ -220,7 +216,7 @@ coap_socket_bind_tcp(coap_socket_t *sock,

if (sock->fd == COAP_INVALID_SOCKET) {
coap_log_warn("coap_socket_bind_tcp: socket: %s\n",
coap_socket_strerror());
coap_socket_strerror());
goto error;
}

Expand All @@ -231,20 +227,18 @@ coap_socket_bind_tcp(coap_socket_t *sock,
if (ioctl(sock->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) {
#endif
coap_log_warn("coap_socket_bind_tcp: ioctl FIONBIO: %s\n",
coap_socket_strerror());
coap_socket_strerror());
}
#endif /* RIOT_VERSION */
if (setsockopt (sock->fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on),
sizeof (on)) == COAP_SOCKET_ERROR)
coap_log_warn(
"coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n",
coap_socket_strerror());
if (setsockopt(sock->fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on),
sizeof(on)) == COAP_SOCKET_ERROR)
coap_log_warn("coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n",
coap_socket_strerror());

if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on),
sizeof(on)) == COAP_SOCKET_ERROR)
coap_log_warn(
"coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n",
coap_socket_strerror());
coap_log_warn("coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n",
coap_socket_strerror());

switch (listen_addr->addr.sa.sa_family) {
#if COAP_IPV4_SUPPORT
Expand All @@ -255,10 +249,10 @@ coap_socket_bind_tcp(coap_socket_t *sock,
case AF_INET6:
#ifndef RIOT_VERSION
/* Configure the socket as dual-stacked */
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off), sizeof(off)) == COAP_SOCKET_ERROR)
coap_log_alert(
"coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
coap_socket_strerror());
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off),
sizeof(off)) == COAP_SOCKET_ERROR)
coap_log_alert("coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n",
coap_socket_strerror());
#endif /* RIOT_VERSION */
break;
#endif /* COAP_IPV6_SUPPORT */
Expand All @@ -273,24 +267,24 @@ coap_socket_bind_tcp(coap_socket_t *sock,
if (bind(sock->fd, &listen_addr->addr.sa,
#if COAP_IPV4_SUPPORT
listen_addr->addr.sa.sa_family == AF_INET ?
(socklen_t)sizeof(struct sockaddr_in) :
(socklen_t)sizeof(struct sockaddr_in) :
#endif /* COAP_IPV4_SUPPORT */
(socklen_t)listen_addr->size) == COAP_SOCKET_ERROR) {
(socklen_t)listen_addr->size) == COAP_SOCKET_ERROR) {
coap_log_alert("coap_socket_bind_tcp: bind: %s\n",
coap_socket_strerror());
coap_socket_strerror());
goto error;
}

bound_addr->size = (socklen_t)sizeof(*bound_addr);
if (getsockname(sock->fd, &bound_addr->addr.sa, &bound_addr->size) < 0) {
coap_log_warn("coap_socket_bind_tcp: getsockname: %s\n",
coap_socket_strerror());
coap_socket_strerror());
goto error;
}

if (listen(sock->fd, 5) == COAP_SOCKET_ERROR) {
coap_log_alert("coap_socket_bind_tcp: listen: %s\n",
coap_socket_strerror());
coap_socket_strerror());
goto error;
}

Expand Down Expand Up @@ -320,22 +314,22 @@ coap_socket_accept_tcp(coap_socket_t *server,
&remote_addr->size);
if (new_client->fd == COAP_INVALID_SOCKET) {
coap_log_warn("coap_socket_accept_tcp: accept: %s\n",
coap_socket_strerror());
coap_socket_strerror());
return 0;
}

if (getsockname( new_client->fd, &local_addr->addr.sa, &local_addr->size) < 0)
if (getsockname(new_client->fd, &local_addr->addr.sa, &local_addr->size) < 0)
coap_log_warn("coap_socket_accept_tcp: getsockname: %s\n",
coap_socket_strerror());
coap_socket_strerror());

#ifndef RIOT_VERSION
#ifdef _WIN32
#ifdef _WIN32
if (ioctlsocket(new_client->fd, FIONBIO, &u_on) == COAP_SOCKET_ERROR) {
#else
if (ioctl(new_client->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) {
#endif
coap_log_warn("coap_socket_accept_tcp: ioctl FIONBIO: %s\n",
coap_socket_strerror());
coap_socket_strerror());
}
#endif /* RIOT_VERSION */
return 1;
Expand Down
18 changes: 10 additions & 8 deletions src/coap_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
static coap_tick_t coap_clock_offset = 0;

#if _POSIX_TIMERS && !defined(__APPLE__)
/* _POSIX_TIMERS is > 0 when clock_gettime() is available */
/* _POSIX_TIMERS is > 0 when clock_gettime() is available */

/* Use real-time clock for correct timestamps in coap_log(). */
/* Use real-time clock for correct timestamps in coap_log(). */
#define COAP_CLOCK CLOCK_REALTIME
#endif

Expand All @@ -47,10 +47,10 @@ gettimeofday(struct timeval *tp, TIME_ZONE_INFORMATION *tzp) {
ULARGE_INTEGER time;
uint64_t tUsSinceUnicEpoch;

GetSystemTimeAsFileTime( &file_time );
GetSystemTimeAsFileTime(&file_time);
time.LowPart = file_time.dwLowDateTime;
time.HighPart = file_time.dwHighDateTime;
tUsSinceUnicEpoch = ( time.QuadPart - s_tUnixEpoch ) / 10;
tUsSinceUnicEpoch = (time.QuadPart - s_tUnixEpoch) / 10;

tp->tv_sec = (long)(tUsSinceUnicEpoch / 1000000);
tp->tv_usec = (long)(tUsSinceUnicEpoch % 1000000);
Expand Down Expand Up @@ -118,11 +118,13 @@ coap_ticks_to_rt(coap_tick_t t) {
return coap_clock_offset + (t / COAP_TICKS_PER_SECOND);
}

uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
uint64_t
coap_ticks_to_rt_us(coap_tick_t t) {
return (uint64_t)coap_clock_offset * 1000000 + (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
}

coap_tick_t coap_ticks_from_rt_us(uint64_t t) {
coap_tick_t
coap_ticks_from_rt_us(uint64_t t) {
return (coap_tick_t)((t - (uint64_t)coap_clock_offset * 1000000) * COAP_TICKS_PER_SECOND / 1000000);
}

Expand All @@ -133,8 +135,8 @@ coap_tick_t coap_ticks_from_rt_us(uint64_t t) {
#else /* HAVE_TIME_H */

/* make compilers happy that do not like empty modules */
COAP_STATIC_INLINE void dummy(void)
{
COAP_STATIC_INLINE void
dummy(void) {
}

#endif /* not HAVE_TIME_H */
Loading

0 comments on commit 7a5d13a

Please sign in to comment.