From f65ffde2527631c1cfcbd8dbb50318986bcf4dfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danyi=20D=C3=A1vid?= Date: Tue, 17 Oct 2017 21:46:31 +0200 Subject: [PATCH] * initial commit --- library.properties | 10 +++ src/arduino-base.cmake | 8 +++ src/connection.cpp | 140 +++++++++++++++++++++++++++++++++++++++++ src/connection.h | 61 ++++++++++++++++++ src/platform.c | 67 ++++++++++++++++++++ src/shared.cmake | 29 +++++++++ 6 files changed, 315 insertions(+) create mode 100644 library.properties create mode 100644 src/arduino-base.cmake create mode 100644 src/connection.cpp create mode 100644 src/connection.h create mode 100644 src/platform.c create mode 100644 src/shared.cmake diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..e3e4a5c --- /dev/null +++ b/library.properties @@ -0,0 +1,10 @@ +name=Wakaama connection SAMD +version=0.1.1 +author=DavidDanyi,Ericsson +maintainer=David Danyi +sentence=Eclipse Wakaama connection specific implementation +paragraph= +category=Communication +url= +architectures=samd +includes= diff --git a/src/arduino-base.cmake b/src/arduino-base.cmake new file mode 100644 index 0000000..c3cdf33 --- /dev/null +++ b/src/arduino-base.cmake @@ -0,0 +1,8 @@ +set(ARDUINO_BASE_LIBDIR /opt/arduino-1.8.5) +set(ARDUINO_SAMD_DIR $ENV{HOME}/.arduino15/packages/arduino/hardware/samd/1.6.16) + +set(ARDUINO_INCLUDE_DIRS + ${ARDUINO_SAMD_DIR}/cores/arduino + ${ARDUINO_SAMD_DIR}/libraries/SPI + ${ARDUINO_BASE_LIBDIR}/libraries/Ethernet/src + ) diff --git a/src/connection.cpp b/src/connection.cpp new file mode 100644 index 0000000..6763665 --- /dev/null +++ b/src/connection.cpp @@ -0,0 +1,140 @@ +/******************************************************************************* + * + * Copyright (c) 2013, 2014 Intel Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * David Navarro, Intel Corporation - initial API and implementation + * Pascal Rieux - Please refer to git log + * + *******************************************************************************/ + +#include +#include +#include +#include +#include "connection.h" + + +connection_t * connection_find(connection_t * connList, + EthernetUDP * udpConnection) +{ + connection_t * connP; + + connP = connList; + while (connP != nullptr) + { + if (connP->udpConnection == udpConnection) + { + return connP; + } + connP = connP->next; + } + + return connP; +} + + +connection_t * connection_new_incoming(connection_t * connList, + EthernetUDP * udpConnection, + IPAddress * remoteIp, + uint16_t port) +{ + connection_t * connP; + + connP = (connection_t *)malloc(sizeof(connection_t)); + if (connP != nullptr) + { + connP->udpConnection = udpConnection; + connP->remoteIp = remoteIp; + connP->port = port; + connP->next = connList; + } + + return connP; +} + +/** + * @todo add some sort of sanity checking to see if its possible to connect + * @param connList + * @param udpConnection + * @param remoteIp + * @param port + * @return + */ +connection_t * connection_create(connection_t * connList, + EthernetUDP * udpConnection, + IPAddress * remoteIp, + uint16_t port) +{ + connection_t * connP = nullptr; + + if(*remoteIp != 0) { + connP = connection_new_incoming(connList, udpConnection, remoteIp, port); + } + return connP; +} + + +void connection_free(connection_t * connList) +{ + while (connList != nullptr) + { + connection_t * nextP; + + nextP = connList->next; + free(connList); + + connList = nextP; + } +} + + +int connection_send(connection_t *connP, + uint8_t * buffer, + size_t length) +{ + SerialOut.println(F("connection_send() start")); + connP->udpConnection->beginPacket(*(connP->remoteIp), connP->port); + connP->udpConnection->write(buffer, length); + connP->udpConnection->endPacket(); + return 0; +} + + +uint8_t lwm2m_buffer_send(void * sessionH, + uint8_t * buffer, + size_t length, + void * userdata) +{ + auto * connP = (connection_t*) sessionH; + + if (connP == nullptr) + { +// fprintf(stderr, "#> failed sending %lu bytes, missing connection\r\n", length); + return COAP_500_INTERNAL_SERVER_ERROR ; + } + + if (-1 == connection_send(connP, buffer, length)) + { +// fprintf(stderr, "#> failed sending %lu bytes\r\n", length); + return COAP_500_INTERNAL_SERVER_ERROR ; + } + + return COAP_NO_ERROR; +} + + +bool lwm2m_session_is_equal(void * session1, + void * session2, + void * userData) +{ + return (session1 == session2); +} diff --git a/src/connection.h b/src/connection.h new file mode 100644 index 0000000..d6b337f --- /dev/null +++ b/src/connection.h @@ -0,0 +1,61 @@ +/******************************************************************************* + * + * Copyright (c) 2013, 2014 Intel Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * David Navarro, Intel Corporation - initial API and implementation + * + *******************************************************************************/ + +#ifndef CONNECTION_H_ +#define CONNECTION_H_ + +#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_BSSERVER_PORT_STR "5685" +#define LWM2M_BSSERVER_PORT 5685 + +typedef struct _connection_t +{ + struct _connection_t * next; + EthernetUDP * udpConnection; + IPAddress * remoteIp; + uint16_t port; +} connection_t; + +//int create_socket(const char * portStr, int ai_family); + +connection_t * connection_find(connection_t * connList, + EthernetUDP * udpConnection); +connection_t * connection_new_incoming(connection_t * connList, + EthernetUDP * udpConnection, + IPAddress * remoteIp, + uint16_t port); +connection_t * connection_create(connection_t * connList, + EthernetUDP * udpConnection, + IPAddress * remoteIp, + uint16_t port); + +void connection_free(connection_t * connList); + +int connection_send(connection_t *connP, uint8_t * buffer, size_t length); + +#endif diff --git a/src/platform.c b/src/platform.c new file mode 100644 index 0000000..a41f134 --- /dev/null +++ b/src/platform.c @@ -0,0 +1,67 @@ +/******************************************************************************* + * + * Copyright (c) 2013, 2014, 2015 Intel Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * The Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * David Navarro, Intel Corporation - initial API and implementation + *******************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef LWM2M_MEMORY_TRACE + +void * lwm2m_malloc(size_t s) +{ + return malloc(s); +} + +void lwm2m_free(void * p) +{ + return free(p); +} + +char * lwm2m_strdup(const char * str) +{ + return strdup(str); +} + +#endif + +int lwm2m_strncmp(const char * s1, + const char * s2, + size_t n) +{ + return strncmp(s1, s2, n); +} + +time_t lwm2m_gettime(void) +{ + time_t seconds = floor(millis() / 1000); + return seconds; +} + +//void lwm2m_printf(const char * format, ...) +//{ +// va_list ap; +// +// va_start(ap, format); +// +// SerialUSB.println(format, ap); +// +// va_end(ap); +//} diff --git a/src/shared.cmake b/src/shared.cmake new file mode 100644 index 0000000..c20d3fd --- /dev/null +++ b/src/shared.cmake @@ -0,0 +1,29 @@ +# Provides SHARED_SOURCES_DIR, SHARED_SOURCES, SHARED_INCLUDE_DIRS and SHARED_DEFINITIONS variables + +set(SHARED_SOURCES_DIR ${CMAKE_CURRENT_LIST_DIR}) + +set(SHARED_SOURCES + ${SHARED_SOURCES_DIR}/platform.c) + +if(DTLS) + include(${CMAKE_CURRENT_LIST_DIR}/tinydtls.cmake) + + set(SHARED_SOURCES + ${SHARED_SOURCES} + ${TINYDTLS_SOURCES} + ${SHARED_SOURCES_DIR}/dtlsconnection.c) + + set(SHARED_INCLUDE_DIRS + ${SHARED_SOURCES_DIR} + ${TINYDTLS_SOURCES_DIR}) + + set(SHARED_DEFINITIONS -DWITH_TINYDTLS) +else() + set(SHARED_SOURCES + ${SHARED_SOURCES} + ${SHARED_SOURCES_DIR}/connection.cpp) + + set(SHARED_INCLUDE_DIRS ${SHARED_SOURCES_DIR}) +endif() + +