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
12 changes: 6 additions & 6 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 Expand Up @@ -724,7 +724,7 @@
GCC_PREPROCESSOR_DEFINITIONS = (
"$(inherited)",
HAVE_CONFIG_H,
DEBUG,
"DEBUG=1",
);
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand All @@ -735,7 +735,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = macOS/;
MACOSX_DEPLOYMENT_TARGET = 10.13;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
Expand Down Expand Up @@ -797,7 +797,7 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = macOS/;
MACOSX_DEPLOYMENT_TARGET = 10.13;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SYSTEM_HEADER_SEARCH_PATHS = ../src;
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
3 changes: 3 additions & 0 deletions src/libnyoci/coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ coap_option_key_to_cstr(
case COAP_OPTION_CONTENT_TYPE: ret = "Content-type"; break;
case COAP_OPTION_MAX_AGE: ret = "Max-age"; break;
case COAP_OPTION_ETAG: ret = "Etag"; break;
case COAP_OPTION_IF_MATCH: ret = "If-Match"; break;
case COAP_OPTION_IF_NONE_MATCH: ret = "If-None-Match"; break;
case COAP_OPTION_PROXY_URI: ret = "Proxy-uri"; break;
case COAP_OPTION_URI_HOST: ret = "URI-host"; break;
case COAP_OPTION_URI_PORT: ret = "URI-port"; break;
Expand Down Expand Up @@ -657,6 +659,7 @@ http_code_to_cstr(int x) {
break;

case HTTP_RESULT_CODE_REQUEST_TIMEOUT: return "REQUEST_TIMEOUT"; break;
case HTTP_RESULT_CODE_PRECONDITION_FAILED: return "PRECONDITION_FAILED"; break;

case HTTP_RESULT_CODE_BAD_REQUEST: return "BAD_REQUEST"; break;
case HTTP_RESULT_CODE_UNAUTHORIZED: return "UNAUTHORIZED"; break;
Expand Down
28 changes: 21 additions & 7 deletions src/libnyoci/coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ enum {
COAP_METHOD_POST = 2,
COAP_METHOD_PUT = 3,
COAP_METHOD_DELETE = 4,

// RFC 8132
COAP_METHOD_FETCH = 5,
COAP_METHOD_PATCH = 6,
COAP_METHOD_IPATCH = 7,
};

enum {
Expand Down Expand Up @@ -172,11 +177,16 @@ enum {
COAP_RESULT_504_GATEWAY_TIMEOUT = HTTP_TO_COAP_CODE(504),
COAP_RESULT_505_PROXYING_NOT_SUPPORTED = HTTP_TO_COAP_CODE(505),

// https://tools.ietf.org/html/draft-ietf-core-block-20#section-2.9
// RFC 7959, section-2.9
COAP_RESULT_231_CONTINUE = HTTP_TO_COAP_CODE(231),
COAP_RESULT_408_REQUEST_ENTITY_INCOMPLETE = HTTP_TO_COAP_CODE(408),
COAP_RESULT_413_REQUEST_ENTITY_TOO_LARGE = HTTP_TO_COAP_CODE(413),

// RFC 8132
COAP_RESULT_409_CONFLICT = HTTP_TO_COAP_CODE(409),
COAP_RESULT_422_UNPROCESSABLE_ENTITY = HTTP_TO_COAP_CODE(422),


// https://tools.ietf.org/html/draft-bormann-core-coap-sig-01
COAP_CODE_701_CSM = HTTP_TO_COAP_CODE(701),
COAP_CODE_702_PING = HTTP_TO_COAP_CODE(702),
Expand Down Expand Up @@ -205,6 +215,7 @@ enum {
HTTP_RESULT_CODE_METHOD_NOT_ALLOWED = 405,
HTTP_RESULT_CODE_NOT_ACCEPTABLE = 406,
HTTP_RESULT_CODE_REQUEST_TIMEOUT= 408,
HTTP_RESULT_CODE_CONFLICT = 409,
HTTP_RESULT_CODE_PRECONDITION_FAILED = 412,
HTTP_RESULT_CODE_REQUEST_ENTITY_TOO_LARGE = 413,
HTTP_RESULT_CODE_UNSUPPORTED_MEDIA_TYPE = 415,
Expand All @@ -222,7 +233,6 @@ enum {
HTTP_RESULT_CODE_SEE_OTHER = 303,
HTTP_RESULT_CODE_TEMPORARY_REDIRECT = 307,
HTTP_RESULT_CODE_PARTIAL_CONTENT = 206,
HTTP_RESULT_CODE_CONFLICT = 409,
HTTP_RESULT_CODE_GONE = 410,
HTTP_RESULT_CODE_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
};
Expand All @@ -247,13 +257,13 @@ typedef enum {
COAP_OPTION_URI_QUERY = 15,
COAP_OPTION_ACCEPT = 17,
COAP_OPTION_LOCATION_QUERY = 20,
COAP_OPTION_BLOCK2 = 23, /* draft-ietf-core-block-10 */
COAP_OPTION_BLOCK1 = 27, /* draft-ietf-core-block-10 */
COAP_OPTION_SIZE = 28, /* draft-ietf-core-block-10 */
COAP_OPTION_BLOCK2 = 23, /* RFC 7959 */
COAP_OPTION_BLOCK1 = 27, /* RFC 7959 */
COAP_OPTION_SIZE = 28, /* RFC 7959 */
COAP_OPTION_PROXY_URI = 35,

COAP_OPTION_SIZE2 = 28, /* draft-ietf-core-block-20 */
COAP_OPTION_SIZE1 = 60, /* draft-ietf-core-block-20 */
COAP_OPTION_SIZE2 = 28, /* RFC 7959 */
COAP_OPTION_SIZE1 = 60, /* RFC 7959 */

//////////////////////////////////////////////////////////////////////
// Experimental after this point. Experimentals start at 65000.
Expand Down Expand Up @@ -300,6 +310,10 @@ enum {
COAP_CONTENT_TYPE_APPLICATION_EXI = 47,
COAP_CONTENT_TYPE_APPLICATION_JSON = 50,

//! RFC 8132
COAP_CONTENT_TYPE_APPLICATION_JSON_PATCH = 51,
COAP_CONTENT_TYPE_APPLICATION_MERGE_PATCH = 52,

//! application/cbor rfc7049
COAP_CONTENT_TYPE_APPLICATION_CBOR = 60,

Expand Down
6 changes: 6 additions & 0 deletions src/libnyoci/libnyoci.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,12 @@ NYOCI_API_EXTERN nyoci_status_t nyoci_outbound_append_content_formatted(const ch
nyoci_outbound_append_content_formatted(fmt,__VA_ARGS__)
#endif

//! Returns a pointer to the start of the current outbound CoAP packet.
NYOCI_API_EXTERN const struct coap_header_s* nyoci_outbound_get_packet(void);

//! Returns the length of the outbound packet.
NYOCI_API_EXTERN coap_size_t nyoci_outbound_get_packet_length(void);

//! Sends the outbound packet.
/*! After calling this function, you are done for this callback. You may not
** call any other nyoci_outbound_* functions. You may only send one outbound
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
16 changes: 16 additions & 0 deletions src/libnyoci/nyoci-outbound.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,22 @@ nyoci_outbound_set_var_content_unsigned_long_int(unsigned long int v)
}


const struct coap_header_s*
nyoci_outbound_get_packet(void)
{
nyoci_t const self = nyoci_get_current_instance();
return self->outbound.packet;
}

coap_size_t
nyoci_outbound_get_packet_length(void)
{
nyoci_t const self = nyoci_get_current_instance();
coap_size_t header_len = (coap_size_t)(nyoci_outbound_get_content_ptr(NULL)-(char*)self->outbound.packet);
return header_len+self->outbound.content_len;
}


nyoci_status_t
nyoci_outbound_quick_response(coap_code_t code, const char* body)
{
Expand Down
2 changes: 1 addition & 1 deletion src/nyocictl/cmd_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ parse_link_format(char* content, coap_size_t content_length, void* context) {
type = (coap_content_type_t)strtol(value, NULL, 0);

} else if (0 == strcmp(key, "obs")) {
obs = (strtol(value, NULL, 0) != NULL);
obs = (strtol(value, NULL, 0) != 0);

} else if (0 == strcmp(key, "sh")) {
sh_url = value;
Expand Down
8 changes: 8 additions & 0 deletions src/nyocictl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ static arg_list_item_t option_list[] = {

void print_commands(void);

static void print_env_help(void) {
printf("Environment:\n"
" NYOCI_CURRENT_PATH CoAP URL to access; defaults to coap://localhost/\n"
" COAP_PROXY_URL Proxy server to use\n");
}

static int
tool_cmd_help(
nyoci_t nyoci, int argc, char* argv[]
Expand All @@ -92,6 +98,7 @@ tool_cmd_help(
return exec_command(nyoci, 2, (char**)argv2);
} else {
print_commands();
print_env_help();
}
return ERRORCODE_HELP;
}
Expand Down Expand Up @@ -766,6 +773,7 @@ main(
argv[0],
"[options] <sub-command> [args]");
print_commands();
print_env_help();
gRet = ERRORCODE_HELP;
goto bail;
}
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