diff --git a/examples/bootstrap_server/bootstrap_server.c b/examples/bootstrap_server/bootstrap_server.c index 8d59824..c3eeafb 100644 --- a/examples/bootstrap_server/bootstrap_server.c +++ b/examples/bootstrap_server/bootstrap_server.c @@ -35,9 +35,9 @@ #include #include -#include "commandline.h" -#include "connection.h" #include "bootstrap_info.h" +#include "commandline.h" +#include "udp/connection.h" #define CMD_STATUS_NEW 0 #define CMD_STATUS_SENT 1 diff --git a/examples/client/lwm2mclient.c b/examples/client/lwm2mclient.c index b540a0e..4c77d71 100644 --- a/examples/client/lwm2mclient.c +++ b/examples/client/lwm2mclient.c @@ -62,7 +62,7 @@ #ifdef WITH_TINYDTLS #include "dtlsconnection.h" #else -#include "connection.h" +#include "udp/connection.h" #endif #include diff --git a/examples/lightclient/lightclient.c b/examples/lightclient/lightclient.c index 9a4d222..5af4b67 100644 --- a/examples/lightclient/lightclient.c +++ b/examples/lightclient/lightclient.c @@ -56,7 +56,7 @@ */ #include "liblwm2m.h" -#include "connection.h" +#include "udp/connection.h" #include #include diff --git a/examples/server/lwm2mserver.c b/examples/server/lwm2mserver.c index b1e2b59..1a85ec0 100644 --- a/examples/server/lwm2mserver.c +++ b/examples/server/lwm2mserver.c @@ -72,7 +72,7 @@ #include #include "commandline.h" -#include "connection.h" +#include "udp/connection.h" #define MAX_PACKET_SIZE 2048 diff --git a/examples/shared/connection.c b/transport/udp/connection.c similarity index 59% rename from examples/shared/connection.c rename to transport/udp/connection.c index 289134f..355a22f 100644 --- a/examples/shared/connection.c +++ b/transport/udp/connection.c @@ -16,18 +16,17 @@ * *******************************************************************************/ +#include "udp/connection.h" +#include "commandline.h" +#include #include #include -#include -#include "connection.h" -#include "commandline.h" +#include #include #include -#include -int create_socket(const char * portStr, int addressFamily) -{ +int create_socket(const char *portStr, int addressFamily) { int s = -1; struct addrinfo hints; struct addrinfo *res; @@ -38,18 +37,14 @@ int create_socket(const char * portStr, int addressFamily) hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_PASSIVE; - if (0 != getaddrinfo(NULL, portStr, &hints, &res)) - { + if (0 != getaddrinfo(NULL, portStr, &hints, &res)) { return -1; } - for(p = res ; p != NULL && s == -1 ; p = p->ai_next) - { + for (p = res; p != NULL && s == -1; p = p->ai_next) { s = socket(p->ai_family, p->ai_socktype, p->ai_protocol); - if (s >= 0) - { - if (-1 == bind(s, p->ai_addr, p->ai_addrlen)) - { + if (s >= 0) { + if (-1 == bind(s, p->ai_addr, p->ai_addrlen)) { close(s); s = -1; } @@ -61,18 +56,12 @@ int create_socket(const char * portStr, int addressFamily) return s; } -connection_t * connection_find(connection_t * connList, - struct sockaddr_storage * addr, - size_t addrLen) -{ - connection_t * connP; +connection_t *connection_find(connection_t *connList, struct sockaddr_storage *addr, size_t addrLen) { + connection_t *connP; connP = connList; - while (connP != NULL) - { - if ((connP->addrLen == addrLen) - && (memcmp(&(connP->addr), addr, addrLen) == 0)) - { + while (connP != NULL) { + if ((connP->addrLen == addrLen) && (memcmp(&(connP->addr), addr, addrLen) == 0)) { return connP; } connP = connP->next; @@ -81,16 +70,11 @@ connection_t * connection_find(connection_t * connList, return connP; } -connection_t * connection_new_incoming(connection_t * connList, - int sock, - struct sockaddr * addr, - size_t addrLen) -{ - connection_t * connP; +connection_t *connection_new_incoming(connection_t *connList, int sock, struct sockaddr *addr, size_t addrLen) { + connection_t *connP; connP = (connection_t *)lwm2m_malloc(sizeof(connection_t)); - if (connP != NULL) - { + if (connP != NULL) { connP->sock = sock; memcpy(&(connP->addr), addr, addrLen); connP->addrLen = addrLen; @@ -100,44 +84,36 @@ connection_t * connection_new_incoming(connection_t * connList, return connP; } -connection_t * connection_create(connection_t * connList, - int sock, - char * host, - char * port, - int addressFamily) -{ +connection_t *connection_create(connection_t *connList, int sock, char *host, char *port, int addressFamily) { struct addrinfo hints; struct addrinfo *servinfo = NULL; struct addrinfo *p; int s; struct sockaddr *sa; socklen_t sl; - connection_t * connP = NULL; + connection_t *connP = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_family = addressFamily; hints.ai_socktype = SOCK_DGRAM; - if (0 != getaddrinfo(host, port, &hints, &servinfo) || servinfo == NULL) return NULL; + if (0 != getaddrinfo(host, port, &hints, &servinfo) || servinfo == NULL) + return NULL; // we test the various addresses s = -1; - for(p = servinfo ; p != NULL && s == -1 ; p = p->ai_next) - { + for (p = servinfo; p != NULL && s == -1; p = p->ai_next) { s = socket(p->ai_family, p->ai_socktype, p->ai_protocol); - if (s >= 0) - { + if (s >= 0) { sa = p->ai_addr; sl = p->ai_addrlen; - if (-1 == connect(s, p->ai_addr, p->ai_addrlen)) - { + if (-1 == connect(s, p->ai_addr, p->ai_addrlen)) { close(s); s = -1; } } } - if (s >= 0) - { + if (s >= 0) { connP = connection_new_incoming(connList, sock, sa, sl); close(s); } @@ -148,11 +124,9 @@ connection_t * connection_create(connection_t * connList, return connP; } -void connection_free(connection_t * connList) -{ - while (connList != NULL) - { - connection_t * nextP; +void connection_free(connection_t *connList) { + while (connList != NULL) { + connection_t *nextP; nextP = connList->next; lwm2m_free(connList); @@ -161,10 +135,7 @@ void connection_free(connection_t * connList) } } -int connection_send(connection_t *connP, - uint8_t * buffer, - size_t length) -{ +int connection_send(connection_t *connP, uint8_t *buffer, size_t length) { int nbSent; size_t offset; @@ -174,14 +145,11 @@ int connection_send(connection_t *connP, s[0] = 0; - if (AF_INET == connP->addr.sin6_family) - { + if (AF_INET == connP->addr.sin6_family) { struct sockaddr_in *saddr = (struct sockaddr_in *)&connP->addr; inet_ntop(saddr->sin_family, &saddr->sin_addr, s, INET6_ADDRSTRLEN); port = saddr->sin_port; - } - else if (AF_INET6 == connP->addr.sin6_family) - { + } else if (AF_INET6 == connP->addr.sin6_family) { struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)&connP->addr; inet_ntop(saddr->sin6_family, &saddr->sin6_addr, s, INET6_ADDRSTRLEN); port = saddr->sin6_port; @@ -195,43 +163,35 @@ int connection_send(connection_t *connP, #endif offset = 0; - while (offset != length) - { - nbSent = sendto(connP->sock, buffer + offset, length - offset, 0, (struct sockaddr *)&(connP->addr), connP->addrLen); - if (nbSent == -1) return -1; + while (offset != length) { + nbSent = + sendto(connP->sock, buffer + offset, length - offset, 0, (struct sockaddr *)&(connP->addr), connP->addrLen); + if (nbSent == -1) + return -1; offset += nbSent; } return 0; } -uint8_t lwm2m_buffer_send(void * sessionH, - uint8_t * buffer, - size_t length, - void * userdata) -{ - connection_t * connP = (connection_t*) sessionH; +uint8_t lwm2m_buffer_send(void *sessionH, uint8_t *buffer, size_t length, void *userdata) { + connection_t *connP = (connection_t *)sessionH; (void)userdata; /* unused */ - if (connP == NULL) - { + if (connP == NULL) { fprintf(stderr, "#> failed sending %zu bytes, missing connection\r\n", length); - return COAP_500_INTERNAL_SERVER_ERROR ; + return COAP_500_INTERNAL_SERVER_ERROR; } - if (-1 == connection_send(connP, buffer, length)) - { + if (-1 == connection_send(connP, buffer, length)) { fprintf(stderr, "#> failed sending %zu bytes\r\n", length); - return COAP_500_INTERNAL_SERVER_ERROR ; + return COAP_500_INTERNAL_SERVER_ERROR; } return COAP_NO_ERROR; } -bool lwm2m_session_is_equal(void * session1, - void * session2, - void * userData) -{ +bool lwm2m_session_is_equal(void *session1, void *session2, void *userData) { (void)userData; /* unused */ return (session1 == session2); diff --git a/examples/shared/connection.h b/transport/udp/include/udp/connection.h similarity index 56% rename from examples/shared/connection.h rename to transport/udp/include/udp/connection.h index 6e8d02f..644ee03 100644 --- a/examples/shared/connection.h +++ b/transport/udp/include/udp/connection.h @@ -18,38 +18,37 @@ #ifndef CONNECTION_H_ #define CONNECTION_H_ -#include -#include -#include #include +#include #include +#include +#include #include #include -#include +#include #define LWM2M_STANDARD_PORT_STR "5683" -#define LWM2M_STANDARD_PORT 5683 -#define LWM2M_DTLS_PORT_STR "5684" -#define LWM2M_DTLS_PORT 5684 +#define LWM2M_STANDARD_PORT 5683 +#define LWM2M_DTLS_PORT_STR "5684" +#define LWM2M_DTLS_PORT 5684 #define LWM2M_BSSERVER_PORT_STR "5685" -#define LWM2M_BSSERVER_PORT 5685 +#define LWM2M_BSSERVER_PORT 5685 -typedef struct _connection_t -{ - struct _connection_t * next; - int sock; - struct sockaddr_in6 addr; - size_t addrLen; +typedef struct _connection_t { + struct _connection_t *next; + int sock; + struct sockaddr_in6 addr; + size_t addrLen; } connection_t; -int create_socket(const char * portStr, int ai_family); +int create_socket(const char *portStr, int ai_family); -connection_t * connection_find(connection_t * connList, struct sockaddr_storage * addr, size_t addrLen); -connection_t * connection_new_incoming(connection_t * connList, int sock, struct sockaddr * addr, size_t addrLen); -connection_t * connection_create(connection_t * connList, int sock, char * host, char * port, int addressFamily); +connection_t *connection_find(connection_t *connList, struct sockaddr_storage *addr, size_t addrLen); +connection_t *connection_new_incoming(connection_t *connList, int sock, struct sockaddr *addr, size_t addrLen); +connection_t *connection_create(connection_t *connList, int sock, char *host, char *port, int addressFamily); -void connection_free(connection_t * connList); +void connection_free(connection_t *connList); -int connection_send(connection_t *connP, uint8_t * buffer, size_t length); +int connection_send(connection_t *connP, uint8_t *buffer, size_t length); #endif diff --git a/wakaama.cmake b/wakaama.cmake index 9662025..0f8fdf8 100644 --- a/wakaama.cmake +++ b/wakaama.cmake @@ -242,7 +242,8 @@ function(target_sources_shared target) set_defines(${target}) if(NOT TARGET_PROPERTY_CONN_IMPL) - target_sources(${target} PRIVATE ${WAKAAMA_EXAMPLE_SHARED_DIRECTORY}/connection.c) + target_sources(${target} PRIVATE ${WAKAAMA_TOP_LEVEL_DIRECTORY}/transport/udp/connection.c) + target_include_directories(${target} PUBLIC ${WAKAAMA_TOP_LEVEL_DIRECTORY}/transport/udp/include) elseif(TARGET_PROPERTY_CONN_IMPL MATCHES "tinydtls") include(${WAKAAMA_EXAMPLE_SHARED_DIRECTORY}/tinydtls.cmake) target_sources(${target} PRIVATE ${WAKAAMA_EXAMPLE_SHARED_DIRECTORY}/dtlsconnection.c)