Skip to content
This repository has been archived by the owner on Jun 10, 2022. It is now read-only.

Compatibility with lwIP and ESP-IDF #13

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions Xcode/nyoci.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@
277A052920AC959F00970354 /* help.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = help.h; sourceTree = "<group>"; };
277A052A20AC959F00970354 /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
277A053620AC959F00970354 /* nyocictl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = nyocictl.h; sourceTree = "<group>"; };
277A054520AC959F00970354 /* nyoci-plat-net-internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "nyoci-plat-net-internal.h"; sourceTree = "<group>"; };
277A054620AC959F00970354 /* nyoci-plat-net.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "nyoci-plat-net.c"; sourceTree = "<group>"; };
277A054720AC959F00970354 /* nyoci-plat-net.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "nyoci-plat-net.h"; sourceTree = "<group>"; };
277A054520AC959F00970354 /* nyoci-plat-net-internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "nyoci-plat-net-internal.h"; sourceTree = "<group>"; usesTabs = 0; };
277A054620AC959F00970354 /* nyoci-plat-net.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "nyoci-plat-net.c"; sourceTree = "<group>"; usesTabs = 0; };
277A054720AC959F00970354 /* nyoci-plat-net.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "nyoci-plat-net.h"; sourceTree = "<group>"; usesTabs = 0; };
277A055820AC959F00970354 /* nyoci-plat-tls.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "nyoci-plat-tls.c"; sourceTree = "<group>"; };
277A055920AC959F00970354 /* nyoci-plat-tls.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "nyoci-plat-tls.h"; sourceTree = "<group>"; };
277A056220AC959F00970354 /* main-client.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "main-client.c"; sourceTree = "<group>"; };
Expand Down
11 changes: 11 additions & 0 deletions src/libnyoci/assert-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#if !DEBUG || ASSERT_MACROS_SQUELCH
#define assert_printf(fmt, ...) do { } while(0)
#define check_string(c, s) do { } while(0)
#define check_string_errno(c, s) do { } while(0)
#define require_action_string(c, l, a, s) \
do { if(!(c)) { \
a; goto l; \
Expand All @@ -79,6 +80,12 @@
PSTR(__FILE__ ":%d: "fmt"\n"), \
__LINE__, \
__VA_ARGS__)
#elif ESP_PLATFORM
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like all of the changes in this file, including the errno stuff. May be worth spinning out.

#define assert_printf(fmt, ...) \
ESP_LOGE("nyoci", \
__FILE__ ":%d: "fmt"\n", \
__LINE__, \
__VA_ARGS__)
#else
#if ASSERT_MACROS_USE_SYSLOG
#define assert_printf(fmt, ...) \
Expand All @@ -102,6 +109,9 @@
#define check_string(c, s) \
do { if(!(c)) assert_printf("Check Failed (%s)", \
s); } while(0)
#define check_string_errno(c, s) \
do { if(!(c)) assert_printf("Check Failed (%s), errno=%d", \
s, errno); } while(0)
#define require_action_string(c, l, a, s) \
do { if(!(c)) { \
assert_printf("Requirement Failed (%s)", \
Expand All @@ -122,6 +132,7 @@
#endif

#define check(c) check_string(c, # c)
#define check_errno(c) check_string_errno(c, # c)
#define check_noerr(c) check((c) == 0)
#define check_noerr_string(c, s) check_string((c) == 0, s)
#define require_quiet(c, l) do { if(!(c)) goto l; } while(0)
Expand Down
4 changes: 4 additions & 0 deletions src/libnyoci/nyoci-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
((uint32_t)random_rand() ^ \
((uint32_t)random_rand() << 16))
#define NYOCI_RANDOM_MAX RAND_MAX
#elif ESP_PLATFORM
#include <sodium/randombytes.h>
#define NYOCI_FUNC_RANDOM_UINT32() randombytes_random()
#define NYOCI_RANDOM_MAX (uint32_t)(0xFFFFFFFF)
#else
#define NYOCI_FUNC_RANDOM_UINT32() \
((uint32_t)random() ^ \
Expand Down
58 changes: 32 additions & 26 deletions src/libnyoci/nyoci-logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,46 @@

#if !VERBOSE_DEBUG

#define CSTR(x) (x)
#define CSTR(x) (x)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather significant changes in the whitespace not be combined with material additions or changes to a file, unless it is unavoidable. It makes the diffs harder to read.

That aside, I'm having trouble getting my brain to not be weirded out by the use of full indents, though. That's largely why I used single-space indents in assert-macros.h: indented preprocessor macros look weird to me after 20+ years of not indenting them at all. I found the single-space indentation improved readability for assert-macros.h without looking too weird to my eyes, but maybe I'm just being stubborn and tabs are really the way to go.


#ifndef DEBUG_PRINTF
#define DEBUG_PRINTF(...) do { } while(0)
#endif
#define NYOCI_DEBUG_OUT_FILE stdout
#ifndef DEBUG_PRINTF
#define DEBUG_PRINTF(...) do { } while(0)
#endif
#define NYOCI_DEBUG_OUT_FILE stdout

#elif defined(__AVR__)
#define NYOCI_DEBUG_OUT_FILE stdout
#define NYOCI_DEBUG_OUT_FILE stdout

#include <stdio.h>
#include <avr/pgmspace.h>
#define CSTR(x) PSTR(x)
#define DEBUG_PRINTF(...) \
#include <stdio.h>
#include <avr/pgmspace.h>
#define CSTR(x) PSTR(x)
#define DEBUG_PRINTF(...) \
do { fprintf_P(NYOCI_DEBUG_OUT_FILE, __VA_ARGS__); fputc( \
'\n', \
NYOCI_DEBUG_OUT_FILE); } while(0)
'\n', \
NYOCI_DEBUG_OUT_FILE); } while(0)

#else // __AVR__
#define NYOCI_DEBUG_OUT_FILE stderr
#elif defined(ESP_PLATFORM)
#include <esp_log.h>
#define DEBUG_PRINTF(FMT, ...) ESP_LOGI("nyoci", FMT, ##__VA_ARGS__)
#define NYOCI_DEBUG_OUT_FILE stderr
#define CSTR(x) x

#include <stdio.h>
#define CSTR(x) (x)
#if ASSERT_MACROS_USES_SYSLOG
#include <syslog.h>
#define DEBUG_PRINTF(...) syslog(7, __VA_ARGS__)
#elif ASSERT_MACROS_USE_VANILLA_PRINTF
#define DEBUG_PRINTF(...) \
do { printf(__VA_ARGS__); printf("\n"); } while(0)
#else
#define DEBUG_PRINTF(...) \
do { fprintf(NYOCI_DEBUG_OUT_FILE, __VA_ARGS__); fputc('\n', \
NYOCI_DEBUG_OUT_FILE); } while(0)
#endif
#define NYOCI_DEBUG_OUT_FILE stderr

#include <stdio.h>
#define CSTR(x) (x)
#if ASSERT_MACROS_USES_SYSLOG
#include <syslog.h>
#define DEBUG_PRINTF(...) syslog(7, __VA_ARGS__)
#elif ASSERT_MACROS_USE_VANILLA_PRINTF
#define DEBUG_PRINTF(...) \
do { printf(__VA_ARGS__); printf("\n"); } while(0)
#else
#define DEBUG_PRINTF(...) \
do { fprintf(NYOCI_DEBUG_OUT_FILE, __VA_ARGS__); fputc('\n', \
NYOCI_DEBUG_OUT_FILE); } while(0)
#endif

#endif

Expand Down
2 changes: 1 addition & 1 deletion src/libnyoci/nyoci-outbound.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ nyoci_outbound_add_options_up_to_key_(

#if NYOCI_CONF_TRANS_ENABLE_OBSERVING
if ( (self->current_transaction != NULL)
&& (self->current_transaction->flags & NYOCI_TRANSACTION_OBSERVE == NYOCI_TRANSACTION_OBSERVE)
&& ((self->current_transaction->flags & NYOCI_TRANSACTION_OBSERVE) == NYOCI_TRANSACTION_OBSERVE)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Mind pulling this commit out into it's own pull request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't claim credit -- GCC found it and warned me (but I can't remember which warning flag it was.)
I'll submit a separate PR when I have a chance.

&& (self->outbound.last_option_key < COAP_OPTION_OBSERVE)
&& (key > COAP_OPTION_OBSERVE)
) {
Expand Down
25 changes: 20 additions & 5 deletions src/plat-net/posix/nyoci-plat-net-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
#define NYOCI_PLAT_NET_POSIX_FAMILY AF_INET6
#endif

#ifndef NYOCI_LWIP
#ifdef ESP_PLATFORM
#define NYOCI_LWIP
#endif
#endif

#if NYOCI_SINGLETON
#define nyoci_internal_multicast_joinleave(self,...) nyoci_internal_multicast_joinleave(__VA_ARGS__)
#endif
Expand All @@ -53,6 +59,15 @@
#define NYOCI_ADDR_NTOP(str, len, addr) inet_ntop(NYOCI_PLAT_NET_POSIX_FAMILY, addr , str, len-1)

#if NYOCI_PLAT_NET_POSIX_FAMILY == AF_INET6
#ifndef IN6_IS_ADDR_V4MAPPED
// IN6_IS_ADDR_V4MAPPED is not defined in lwIP. I copied this macro from macOS 10.13's in6.h. --snej
#define IN6_IS_ADDR_V4MAPPED(a) \
((*(const __uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) && \
(*(const __uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) && \
(*(const __uint32_t *)(const void *)(&(a)->s6_addr[8]) == \
ntohl(0x0000ffff)))
#endif

#define ___nyoci_len sin6_len
#define ___nyoci_family sin6_family
#define NYOCI_IS_ADDR_MULTICAST(addrptr) (IN6_IS_ADDR_MULTICAST(addrptr) || (IN6_IS_ADDR_V4MAPPED(addrptr) && ((addrptr)->s6_addr[12] & 0xF0)==0xE0))
Expand All @@ -66,6 +81,7 @@
#endif
#ifdef IPV6_PKTINFO
#define NYOCI_PKTINFO IPV6_PKTINFO
#define nyoci_pktinfo in6_pktinfo
#endif
#define NYOCI_IPPROTO IPPROTO_IPV6

Expand All @@ -77,6 +93,7 @@
#endif
#ifdef IP_PKTINFO
#define NYOCI_PKTINFO IP_PKTINFO
#define nyoci_pktinfo in_pktinfo
#endif
#define NYOCI_IPPROTO IPPROTO_IPV4

Expand All @@ -90,7 +107,7 @@
#endif // NYOCI_PLAT_NET_POSIX_FAMILY

NYOCI_BEGIN_C_DECLS

struct nyoci_plat_s {
int mcfd_v6; //!< For multicast
int mcfd_v4; //!< For multicast
Expand All @@ -109,10 +126,8 @@ struct nyoci_plat_s {
nyoci_sockaddr_t sockaddr_remote;
nyoci_session_type_t session_type;

#if NYOCI_PLAT_NET_POSIX_FAMILY==AF_INET6
struct in6_pktinfo pktinfo;
#elif NYOCI_PLAT_NET_POSIX_FAMILY==AF_INET
struct in_pktinfo pktinfo;
#ifdef NYOCI_PKTINFO
struct nyoci_pktinfo pktinfo;
#endif

char outbound_packet_bytes[NYOCI_MAX_PACKET_LENGTH+1];
Expand Down
Loading