diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..758543d7f --- /dev/null +++ b/.clang-format @@ -0,0 +1,22 @@ +BasedOnStyle: llvm +AccessModifierOffset: -4 +AlignEscapedNewlinesLeft: true +AlignOperands: true +AllowShortFunctionsOnASingleLine: Empty +AlwaysBreakTemplateDeclarations: true +AvoidMisleadingControlStatementContinuationIndent: true +BinPackParameters: false +BinPackArguments: true +BreakBeforeBinaryOperators: NonAssignment +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 8 +ContinuationIndentWidth: 8 +Cpp11BracedListStyle: false +IndentWidth: 4 +PenaltyBreakBeforeFirstCallParameter: 40 +SpaceAfterCStyleCast: true +TypenameMacros: ['AVS_LIST', 'AVS_RBTREE', 'AVS_RBTREE_ELEM', 'AVS_VECTOR'] +BreakDesignatedInitializers: true +IndentPPDirectives: AfterHash +CommentPragmas: "AVSYSTEM_ANJAY_COMMERCIAL.*" +ExperimentalAutoDetectBinPacking: true diff --git a/.gitignore b/.gitignore index 8259238d5..2547e9791 100644 --- a/.gitignore +++ b/.gitignore @@ -61,6 +61,7 @@ compile_commands.json /doc/sphinx/build /doc/sphinx/html /doc/doxygen +/doc/sphinx/source/.doctrees !/doc/sphinx/Makefile # built tutorials diff --git a/CMakeLists.txt b/CMakeLists.txt index c8d60be1f..971f8236e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ cmake_minimum_required(VERSION 2.8.12) project(anjay C) -set(ANJAY_VERSION "1.11.0" CACHE STRING "Anjay library version") +set(ANJAY_VERSION "1.12.0" CACHE STRING "Anjay library version") set(ANJAY_BINARY_VERSION 1.0.0) set(ANJAY_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}") @@ -75,9 +75,6 @@ endif() include(${CMAKE_CURRENT_LIST_DIR}/cmake/PosixFeatures.cmake) option(WITH_LIBRARY_SHARED "Compile Anjay as shared library" "${UNIX}") -cmake_dependent_option(WITH_STATIC_DEPS_LINKED - "Directly link shared library with its static dependencies such as avs_commons (e.g. for interpreted language bindings)" - OFF WITH_LIBRARY_SHARED OFF) option(WITH_ACCESS_CONTROL "Enable core support for Access Control mechanism" ON) option(WITH_ATTR_STORAGE "Enable Attribute storage module" ON) @@ -93,7 +90,7 @@ if(WITH_OBSERVE) endif() option(WITH_LEGACY_CONTENT_FORMAT_SUPPORT "Enable support for pre-LwM2M 1.0 CoAP Content-Format values (1541-1543)" OFF) -option(WITH_JSON "Enable support for JSON content format (output only)" OFF) +option(WITH_JSON "Enable support for JSON content format (output only)" ON) option(WITH_AVS_PERSISTENCE "Enable support for persisting objects data" ON) @@ -187,11 +184,12 @@ set(CORE_SOURCES src/raw_buffer.c src/sched.c src/servers/activate.c - src/servers/connection_info.c + src/servers/connections.c src/servers/connection_udp.c src/servers/offline.c src/servers/register_internal.c src/servers/reload.c + src/servers/server_connections.c src/servers/servers_internal.c src/servers_utils.c src/utils_core.c) @@ -227,7 +225,7 @@ if(WITH_OBSERVE) src/observe/observe_core.c src/observe/observe_io.c) endif() -if(WITH_JSON) +if(WITH_JSON OR WITH_SENML_JSON) set(CORE_SOURCES ${CORE_SOURCES} src/io/json_out.c) endif() @@ -268,10 +266,11 @@ set(CORE_PRIVATE_HEADERS src/sched_internal.h src/servers.h src/servers/activate.h - src/servers/connection_info.h + src/servers/connections.h src/servers/connections_internal.h src/servers/register_internal.h src/servers/reload.h + src/servers/server_connections.h src/servers/servers_internal.h src/servers_utils.h src/utils_core.h) @@ -441,8 +440,6 @@ else() add_subdirectory(avs_commons/git) endif() -set(DEPS_INCLUDE_DIRS ${DEPS_INCLUDE_DIRS} ${avs_commons_INCLUDE_DIRS}) - if(WITH_AVS_LOG) set(DEPS_LIBRARIES_WEAK ${DEPS_LIBRARIES_WEAK} avs_log) endif() @@ -452,7 +449,6 @@ endif() set(PUBLIC_INCLUDE_DIRS ${PUBLIC_INCLUDE_DIRS} "${CMAKE_CURRENT_SOURCE_DIR}/include_public") set(MODULES_INCLUDE_DIRS ${MODULES_INCLUDE_DIRS} "${CMAKE_CURRENT_SOURCE_DIR}/include_modules") -include_directories(SYSTEM ${DEPS_INCLUDE_DIRS}) include_directories(${PUBLIC_INCLUDE_DIRS} ${MODULES_INCLUDE_DIRS}) @@ -505,16 +501,11 @@ include_directories("${CMAKE_CURRENT_BINARY_DIR}/config") if(WITH_LIBRARY_SHARED) add_library(${PROJECT_NAME} SHARED ${ABSOLUTE_SOURCES}) - if(WITH_STATIC_DEPS_LINKED) - target_link_libraries(${PROJECT_NAME} ${DEPS_LIBRARIES} ${DEPS_LIBRARIES_WEAK}) - else() - target_link_libraries(${PROJECT_NAME} ${DEPS_LIBRARIES}) - target_link_libraries(${PROJECT_NAME} LINK_INTERFACE_LIBRARIES ${DEPS_LIBRARIES_WEAK}) - anjay_emit_deps(${PROJECT_NAME} ${DEPS_LIBRARIES_WEAK}) - endif() + target_link_libraries(${PROJECT_NAME} PRIVATE ${DEPS_LIBRARIES}) + target_link_libraries(${PROJECT_NAME} PUBLIC ${DEPS_LIBRARIES_WEAK}) set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${ANJAY_BINARY_VERSION}) - if(HAVE_VISIBILITY AND NOT WITH_STATIC_DEPS_LINKED) + if(HAVE_VISIBILITY) get_property(LINK_FLAGS TARGET ${PROJECT_NAME} PROPERTY LINK_FLAGS) set_property(TARGET ${PROJECT_NAME} PROPERTY LINK_FLAGS "${LINK_FLAGS} -Wl,--exclude-libs,ALL") endif() @@ -659,7 +650,7 @@ if(WITH_TEST) # Symbol validation add_custom_target(symbols_check COMMAND ${CMAKE_CTEST_COMMAND} -R "'^test_.*_symbols$$'" --output-on-failure) - if(WITH_LIBRARY_SHARED AND NOT WITH_STATIC_DEPS_LINKED) + if(WITH_LIBRARY_SHARED) add_test(NAME test_so_symbols COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test_symbols.sh $ anjay_ ANJAY_) add_dependencies(symbols_check ${PROJECT_NAME}) endif() diff --git a/README.md b/README.md index 94602e53d..8b2ea795f 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ More details about OMA LwM2M: [Brief introduction to LwM2M](https://AVSystem.git sudo apt-get install git build-essential cmake libmbedtls-dev zlib1g-dev # Optionally for tests: sudo apt-get install libpython3-dev libssl-dev python3 python3-cryptography python3-jinja2 python3-sphinx python3-requests clang valgrind clang-tools +pip3 install grequests ``` #### CentOS 7 @@ -129,7 +130,7 @@ sudo yum install -y valgrind valgrind-devel openssl openssl-devel python35u pyth # Use update-alternatives to create a /usr/bin/python3 symlink with priority 0 # (lowest possible) sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 0 -sudo python3 -m pip install cryptography jinja2 requests +sudo python3 -m pip install cryptography jinja2 requests grequests ``` #### macOS Sierra with [Homebrew](https://brew.sh/) @@ -138,7 +139,7 @@ sudo python3 -m pip install cryptography jinja2 requests brew install cmake mbedtls # Optionally for tests: brew install python3 openssl llvm -pip3 install cryptography sphinx sphinx_rtd_theme requests +pip3 install cryptography sphinx sphinx_rtd_theme requests grequests ``` #### Windows diff --git a/avs_commons/git b/avs_commons/git index bb6d36e5f..9e09c24ba 160000 --- a/avs_commons/git +++ b/avs_commons/git @@ -1 +1 @@ -Subproject commit bb6d36e5fcf50ba5f00cff15f8b5c2937f26ab93 +Subproject commit 9e09c24ba8aed5eae7180efcdc13e7a4ef384ee5 diff --git a/demo/demo.c b/demo/demo.c index 828783f42..02b6d4651 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -17,17 +17,17 @@ #include "demo.h" #include "demo_args.h" #include "demo_cmds.h" +#include "demo_utils.h" #include "firmware_update.h" #include "iosched.h" #include "objects.h" -#include "demo_utils.h" -#include #include -#include +#include #include +#include #include -#include +#include #include @@ -64,10 +64,13 @@ static int security_object_reload(anjay_demo_t *demo) { * anjay_security_object_add_instance will make a deep copy by itself. */ instance.server_uri = server->uri; - instance.public_cert_or_psk_identity = args->public_cert_or_psk_identity; - instance.public_cert_or_psk_identity_size = args->public_cert_or_psk_identity_size; + instance.public_cert_or_psk_identity = + args->public_cert_or_psk_identity; + instance.public_cert_or_psk_identity_size = + args->public_cert_or_psk_identity_size; instance.private_cert_or_psk_key = args->private_cert_or_psk_key; - instance.private_cert_or_psk_key_size = args->private_cert_or_psk_key_size; + instance.private_cert_or_psk_key_size = + args->private_cert_or_psk_key_size; instance.server_public_key = args->server_public_key; instance.server_public_key_size = args->server_public_key_size; @@ -118,8 +121,7 @@ const anjay_dm_object_def_t **demo_find_object(anjay_demo_t *demo, } void demo_reload_servers(anjay_demo_t *demo) { - if (security_object_reload(demo) - || server_object_reload(demo)) { + if (security_object_reload(demo) || server_object_reload(demo)) { demo_log(ERROR, "Error while adding new server objects"); exit(-1); } @@ -196,17 +198,18 @@ static int add_default_access_entries(anjay_demo_t *demo) { } void *cookie = NULL; anjay_iid_t iid; - while (!(result = (*object->obj_ptr)->handlers.instance_it( - demo->anjay, object->obj_ptr, &iid, &cookie)) - && iid != ANJAY_IID_INVALID) { + while (!(result = (*object->obj_ptr) + ->handlers.instance_it(demo->anjay, + object->obj_ptr, &iid, + &cookie)) + && iid != ANJAY_IID_INVALID) { if (anjay_access_control_set_acl( - demo->anjay, - (*object->obj_ptr)->oid, - iid, - ANJAY_SSID_ANY, - ANJAY_ACCESS_MASK_READ - | ANJAY_ACCESS_MASK_WRITE - | ANJAY_ACCESS_MASK_EXECUTE)) { + demo->anjay, + (*object->obj_ptr)->oid, + iid, + ANJAY_SSID_ANY, + ANJAY_ACCESS_MASK_READ | ANJAY_ACCESS_MASK_WRITE + | ANJAY_ACCESS_MASK_EXECUTE)) { return -1; } } @@ -219,8 +222,8 @@ static int add_access_entries(anjay_demo_t *demo, const cmdline_args_t *cmdline_args) { const AVS_LIST(access_entry_t) it; AVS_LIST_FOREACH(it, cmdline_args->access_entries) { - if (anjay_access_control_set_acl(demo->anjay, - it->oid, ANJAY_IID_INVALID, it->ssid, + if (anjay_access_control_set_acl(demo->anjay, it->oid, + ANJAY_IID_INVALID, it->ssid, ANJAY_ACCESS_MASK_CREATE)) { return -1; } @@ -259,8 +262,7 @@ install_object(anjay_demo_t *demo, } -static int demo_init(anjay_demo_t *demo, - cmdline_args_t *cmdline_args) { +static int demo_init(anjay_demo_t *demo, cmdline_args_t *cmdline_args) { anjay_configuration_t config = { .endpoint_name = cmdline_args->endpoint_name, @@ -278,6 +280,8 @@ static int demo_init(anjay_demo_t *demo, .max_icmp_failures = &cmdline_args->max_icmp_failures, .disable_server_initiated_bootstrap = cmdline_args->disable_server_initiated_bootstrap, + .udp_tx_params = &cmdline_args->tx_params, + .udp_dtls_hs_tx_params = &cmdline_args->dtls_hs_tx_params }; const avs_net_security_info_t *fw_security_info_ptr = NULL; @@ -291,21 +295,23 @@ static int demo_init(anjay_demo_t *demo, demo->anjay = anjay_new(&config); } demo->iosched = iosched_create(); - if (!demo->anjay - || !demo->iosched + if (!demo->anjay || !demo->iosched || anjay_attr_storage_install(demo->anjay) || anjay_access_control_install(demo->anjay) || firmware_update_install(demo->anjay, &demo->fw_update, cmdline_args->fw_updated_marker_path, - fw_security_info_ptr) + fw_security_info_ptr, + cmdline_args->fwu_tx_params_modified + ? &cmdline_args->fwu_tx_params + : NULL) #ifndef _WIN32 || !iosched_poll_entry_new(demo->iosched, STDIN_FILENO, - POLLIN | POLLHUP, - demo_command_dispatch, demo, NULL) + POLLIN | POLLHUP, demo_command_dispatch, + demo, NULL) #else // _WIN32 -#warning "TODO: Support stdin somehow on Windows" +# warning "TODO: Support stdin somehow on Windows" #endif // _WIN32 - ) { + ) { return -1; } @@ -321,8 +327,8 @@ static int demo_init(anjay_demo_t *demo, || install_object(demo, cm_object_create(), cm_notify_time_dependent, cm_object_release) || install_object(demo, cs_object_create(), NULL, cs_object_release) - || install_object(demo, download_diagnostics_object_create(), - NULL, download_diagnostics_object_release) + || install_object(demo, download_diagnostics_object_create(), NULL, + download_diagnostics_object_release) || install_object(demo, device_object_create(demo->iosched, cmdline_args->endpoint_name), @@ -338,10 +344,9 @@ static int demo_init(anjay_demo_t *demo, ip_ping_object_release) #endif // _WIN32 || install_object(demo, test_object_create(), - test_notify_time_dependent, - test_object_release) - || install_object(demo, portfolio_object_create(), - NULL, portfolio_object_release)) { + test_notify_time_dependent, test_object_release) + || install_object(demo, portfolio_object_create(), NULL, + portfolio_object_release)) { return -1; } @@ -375,7 +380,7 @@ static int demo_init(anjay_demo_t *demo, } static anjay_demo_t *demo_new(cmdline_args_t *cmdline_args) { - anjay_demo_t *demo = (anjay_demo_t*) avs_calloc(1, sizeof(anjay_demo_t)); + anjay_demo_t *demo = (anjay_demo_t *) avs_calloc(1, sizeof(anjay_demo_t)); if (!demo) { return NULL; } @@ -423,9 +428,9 @@ static socket_entry_t *create_socket_entry(anjay_demo_t *demo, *(const demo_fd_t *) avs_net_socket_get_system(socket); entry->demo = demo; entry->socket = socket; - entry->iosched_entry = iosched_poll_entry_new(demo->iosched, sys_socket, - POLLIN, socket_dispatch, - entry, NULL); + entry->iosched_entry = + iosched_poll_entry_new(demo->iosched, sys_socket, POLLIN, + socket_dispatch, entry, NULL); if (!entry->iosched_entry) { demo_log(ERROR, "cannot add iosched entry"); AVS_LIST_DELETE(&entry); @@ -484,7 +489,7 @@ static void serve(anjay_demo_t *demo) { int waitms = anjay_sched_calculate_wait_time_ms( demo->anjay, (int) ((1000500000 - current_time.since_real_epoch.nanoseconds) - / 1000000)); + / 1000000)); demo_log(TRACE, "wait time: %d ms", waitms); // +1 to prevent annoying annoying looping in case of @@ -501,11 +506,10 @@ static void serve(anjay_demo_t *demo) { } } -static void log_handler(avs_log_level_t level, - const char *module, - const char *message) { - (void)level; - (void)module; +static void +log_handler(avs_log_level_t level, const char *module, const char *message) { + (void) level; + (void) module; char timebuf[128]; struct timeval now; @@ -516,8 +520,7 @@ static void log_handler(avs_log_level_t level, assert(now_tm); strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", now_tm); - fprintf(stderr, "%s.%06d %s\n", - timebuf, (int)now.tv_usec, message); + fprintf(stderr, "%s.%06d %s\n", timebuf, (int) now.tv_usec, message); } static void cmdline_args_cleanup(cmdline_args_t *cmdline_args) { @@ -529,9 +532,22 @@ static void cmdline_args_cleanup(cmdline_args_t *cmdline_args) { int main(int argc, char *argv[]) { #ifndef _WIN32 - // 0 ~ stdin, 1 ~ stdout, 2 ~ stderr; close everything else - for (int fd = 3, maxfd = (int) sysconf(_SC_OPEN_MAX); - fd < maxfd; ++fd) { + /* + * The demo application implements mock firmware update with execv() call + * on the new LwM2M client application. As a direct consequence, all file + * descriptors from the original process are inherited, even though we will + * never use most of them. To free resources associated with these + * descriptors and avoid weird behavior caused by multiple sockets bound to + * the same local port (*), we close all unknown descriptors before + * continuing. Only 0 (stdin), 1 (stdout) and 2 (stderr) are left open. + * + * (*) For example, Linux does load-balancing between UDP sockets that + * reuse the same local address and port. See `man 7 socket` or + * http://man7.org/linux/man-pages/man7/socket.7.html . + * https://stackoverflow.com/a/14388707/2339636 contains more detailed + * info on SO_REUSEADDR/SO_REUSEPORT behavior on various systems. + */ + for (int fd = 3, maxfd = (int) sysconf(_SC_OPEN_MAX); fd < maxfd; ++fd) { close(fd); } #endif // WIN32 diff --git a/demo/demo.h b/demo/demo.h index 95851d857..79d4474f1 100644 --- a/demo/demo.h +++ b/demo/demo.h @@ -17,12 +17,12 @@ #ifndef DEMO_H #define DEMO_H -#include "objects.h" #include "firmware_update.h" +#include "objects.h" #include -#include #include +#include #include #include diff --git a/demo/demo_args.c b/demo/demo_args.c index d346336cf..3acca8315 100644 --- a/demo/demo_args.c +++ b/demo/demo_args.c @@ -18,14 +18,15 @@ #include #include +#include #include -#include #include +#include #include #define DEFAULT_PSK_IDENTITY "sesame" -#define DEFAULT_PSK_KEY "password" +#define DEFAULT_PSK_KEY "password" static const cmdline_args_t DEFAULT_CMDLINE_ARGS = { .endpoint_name = "urn:dev:os:0023C7-000001", @@ -54,6 +55,10 @@ static const cmdline_args_t DEFAULT_CMDLINE_ARGS = { }, .attr_storage_file = NULL, .disable_server_initiated_bootstrap = false, + .tx_params = ANJAY_COAP_DEFAULT_UDP_TX_PARAMS, + .dtls_hs_tx_params = ANJAY_DTLS_DEFAULT_UDP_HS_TX_PARAMS, + .fwu_tx_params_modified = false, + .fwu_tx_params = ANJAY_COAP_DEFAULT_UDP_TX_PARAMS }; static int parse_security_mode(const char *mode_string, @@ -66,10 +71,12 @@ static int parse_security_mode(const char *mode_string, const char *name; anjay_udp_security_mode_t value; } MODES[] = { + // clang-format off { "psk", ANJAY_UDP_SECURITY_PSK }, { "rpk", ANJAY_UDP_SECURITY_RPK }, { "cert", ANJAY_UDP_SECURITY_CERTIFICATE }, { "nosec", ANJAY_UDP_SECURITY_NOSEC }, + // clang-format on }; for (size_t i = 0; i < ARRAY_SIZE(MODES); ++i) { @@ -82,17 +89,16 @@ static int parse_security_mode(const char *mode_string, char allowed_modes[64]; size_t offset = 0; for (size_t i = 0; i < ARRAY_SIZE(MODES); ++i) { - ssize_t written = snprintf(allowed_modes + offset, - sizeof(allowed_modes) - offset, - " %s", MODES[i].name); - if (written < 0 - || (size_t)written >= sizeof(allowed_modes) - offset) { + ssize_t written = + snprintf(allowed_modes + offset, sizeof(allowed_modes) - offset, + " %s", MODES[i].name); + if (written < 0 || (size_t) written >= sizeof(allowed_modes) - offset) { demo_log(ERROR, "could not enumerate available security modes"); allowed_modes[0] = '\0'; break; } - offset += (size_t)written; + offset += (size_t) written; } demo_log(ERROR, "unrecognized security mode %s (expected one of:%s)", @@ -121,6 +127,7 @@ static void print_option_help(const struct option *opt) { const char *default_value; const char *help; } HELP_INFO[] = { + // clang-format off { 'a', "OBJECT_ID SHORT_SERVER_ID", NULL, "allow Short Server ID to instantiate Object ID." }, { 'b', "client-initiated-only", NULL, @@ -193,6 +200,27 @@ static void print_option_help(const struct option *opt) { { 6, "PERSISTENCE_FILE", NULL, "File to load attribute storage data from at startup, and " "store it at shutdown" }, + { 12, "ACK_RANDOM_FACTOR", "1.5", + "Configures ACK_RANDOM_FACTOR (defined in RFC7252)" }, + { 13, "ACK_TIMEOUT", "2.0", + "Configures ACK_TIMEOUT (defined in RFC7252) in seconds" }, + { 14, "MAX_RETRANSMIT", "4", + "Configures MAX_RETRANSMIT (defined in RFC7252)" }, + { 15, "DTLS_HS_RETRY_WAIT_MIN", "1", + "Configures minimum period of time to wait before sending first " + "DTLS HS retransmission" }, + { 16, "DTLS_HS_RETRY_WAIT_MAX", "60", + "Configures maximum period of time to wait (after last " + "retransmission) before giving up on handshake completely" }, + { 17, "ACK_RANDOM_FACTOR", "1.5", + "Configures ACK_RANDOM_FACTOR (defined in RFC7252) for firmware " + "update" }, + { 18, "ACK_TIMEOUT", "2.0", + "Configures ACK_TIMEOUT (defined in RFC7252) in seconds for firmware " + "update" }, + { 19, "MAX_RETRANSMIT", "4", + "Configures MAX_RETRANSMIT (defined in RFC7252) for firmware update" }, + // clang-format on }; int description_offset = 25; @@ -219,9 +247,8 @@ static void print_option_help(const struct option *opt) { arg_suffix = "]"; } padding -= (int) (strlen(arg_prefix) + strlen(args)); - fprintf(stderr, "%s%s%*s - %s", - arg_prefix, args, padding > 0 ? -padding : 0, arg_suffix, - HELP_INFO[i].help); + fprintf(stderr, "%s%s%*s - %s", arg_prefix, args, + padding > 0 ? -padding : 0, arg_suffix, HELP_INFO[i].help); if (HELP_INFO[i].default_value) { fprintf(stderr, " (default: %s)", HELP_INFO[i].default_value); } @@ -230,14 +257,13 @@ static void print_option_help(const struct option *opt) { } } - fprintf(stderr, "%*s - [NO DESCRIPTION]\n", - padding > 0 ? -padding : 0, help_arg_list(opt)); + fprintf(stderr, "%*s - [NO DESCRIPTION]\n", padding > 0 ? -padding : 0, + help_arg_list(opt)); } static int parse_i32(const char *str, int32_t *out_value) { long long_value; - if (demo_parse_long(str, &long_value) - || long_value < INT32_MIN + if (demo_parse_long(str, &long_value) || long_value < INT32_MIN || long_value > INT32_MAX) { demo_log(ERROR, "value out of range: %s", str); return -1; @@ -249,8 +275,7 @@ static int parse_i32(const char *str, int32_t *out_value) { static int parse_u16(const char *str, uint16_t *out_value) { long long_value; - if (demo_parse_long(str, &long_value) - || long_value < 0 + if (demo_parse_long(str, &long_value) || long_value < 0 || long_value > UINT16_MAX) { demo_log(ERROR, "value out of range: %s", str); return -1; @@ -260,6 +285,16 @@ static int parse_u16(const char *str, uint16_t *out_value) { return 0; } +static int parse_double(const char *str, double *out_value) { + errno = 0; + char *endptr = NULL; + *out_value = strtod(str, &endptr); + if (!*str || isspace((unsigned char) *str) || errno || !endptr || *endptr) { + return -1; + } + return 0; +} + static int parse_hexstring(const char *str, uint8_t **out, size_t *out_size) { if (!str) { return -1; @@ -301,13 +336,13 @@ static void build_getopt_string(const struct option *options, memset(buffer, 0, buffer_size); while (curr_opt->val != 0) { - assert(getopt_string_ptr - buffer < (ssize_t)buffer_size - 1); - *getopt_string_ptr++ = (char)curr_opt->val; + assert(getopt_string_ptr - buffer < (ssize_t) buffer_size - 1); + *getopt_string_ptr++ = (char) curr_opt->val; int colons = curr_opt->has_arg; assert(colons >= 0 && colons <= 2); // 2 colons signify optional arg while (colons-- > 0) { - assert(getopt_string_ptr - buffer < (ssize_t)buffer_size - 1); + assert(getopt_string_ptr - buffer < (ssize_t) buffer_size - 1); *getopt_string_ptr++ = ':'; } @@ -315,8 +350,10 @@ static void build_getopt_string(const struct option *options, } } -static int clone_buffer(uint8_t **out, size_t *out_size, - const void *src, size_t src_size) { +static int clone_buffer(uint8_t **out, + size_t *out_size, + const void *src, + size_t src_size) { *out = (uint8_t *) avs_malloc(src_size); if (!*out) { return -1; @@ -326,8 +363,8 @@ static int clone_buffer(uint8_t **out, size_t *out_size, return 0; } -static int load_buffer_from_file(uint8_t **out, size_t *out_size, - const char *filename) { +static int +load_buffer_from_file(uint8_t **out, size_t *out_size, const char *filename) { FILE *f = fopen(filename, "rb"); if (!f) { return -1; @@ -338,8 +375,7 @@ static int load_buffer_from_file(uint8_t **out, size_t *out_size, goto finish; } size = ftell(f); - if (size < 0 || (unsigned long) size > SIZE_MAX - || fseek(f, 0, SEEK_SET)) { + if (size < 0 || (unsigned long) size > SIZE_MAX || fseek(f, 0, SEEK_SET)) { goto finish; } *out_size = (size_t) size; @@ -367,6 +403,7 @@ int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char *argv[]) { int num_servers = 0; const struct option options[] = { + // clang-format off { "access-entry", required_argument, 0, 'a' }, { "bootstrap", optional_argument, 0, 'b' }, { "bootstrap-holdoff", required_argument, 0, 'H' }, @@ -397,17 +434,26 @@ int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char *argv[]) { { "fw-psk-identity", required_argument, 0, 4 }, { "fw-psk-key", required_argument, 0, 5 }, { "attribute-storage-persistence-file", required_argument, 0, 6 }, + { "ack-random-factor", required_argument, 0, 12 }, + { "ack-timeout", required_argument, 0, 13 }, + { "max-retransmit", required_argument, 0, 14 }, + { "dtls-hs-retry-wait-min", required_argument, 0, 15 }, + { "dtls-hs-retry-wait-max", required_argument, 0, 16 }, + { "fwu-ack-random-factor", required_argument, 0, 17 }, + { "fwu-ack-timeout", required_argument, 0, 18 }, + { "fwu-max-retransmit", required_argument, 0, 19 }, { 0, 0, 0, 0 } + // clang-format on }; int retval = -1; *parsed_args = DEFAULT_CMDLINE_ARGS; - char *default_cert_path = (char *) avs_malloc( - arg0_prefix_length + sizeof(DEFAULT_CERT_FILE)); - char *default_key_path = (char *) avs_malloc( - arg0_prefix_length + sizeof(DEFAULT_KEY_FILE)); + char *default_cert_path = + (char *) avs_malloc(arg0_prefix_length + sizeof(DEFAULT_CERT_FILE)); + char *default_key_path = + (char *) avs_malloc(arg0_prefix_length + sizeof(DEFAULT_KEY_FILE)); const char *cert_path = default_cert_path; const char *key_path = default_key_path; @@ -439,8 +485,7 @@ int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char *argv[]) { /* optind is the index of the next argument to be processed, which means that argv[optind-1] is the current one. We shall fail if more than one free argument is provided */ - if (*argv[optind-1] == '-' - || optind == argc + if (*argv[optind - 1] == '-' || optind == argc || *argv[optind] == '-' || (optind + 1 < argc && *argv[optind + 1] != '-')) { demo_log(ERROR, "invalid pair OID SSID"); @@ -448,14 +493,14 @@ int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char *argv[]) { } AVS_LIST(access_entry_t) entry = - AVS_LIST_NEW_ELEMENT(access_entry_t); + AVS_LIST_NEW_ELEMENT(access_entry_t); if (!entry) { goto finish; } // We ignore the fact, that someone passes bad input, and silently // truncate it to zero. - long oid = strtol(argv[optind-1], NULL, 10); + long oid = strtol(argv[optind - 1], NULL, 10); long ssid = strtol(argv[optind], NULL, 10); entry->oid = (anjay_oid_t) oid; entry->ssid = (anjay_ssid_t) ssid; @@ -505,36 +550,33 @@ int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char *argv[]) { case 'c': parsed_args->location_csv = optarg; break; - case 'f': - { - long freq; - if (demo_parse_long(optarg, &freq) - || freq <= 0 - || freq > INT32_MAX) { - demo_log(ERROR, "invalid location update frequency: %s", - optarg); - goto finish; - } - - parsed_args->location_update_frequency_s = (time_t)freq; + case 'f': { + long freq; + if (demo_parse_long(optarg, &freq) || freq <= 0 + || freq > INT32_MAX) { + demo_log(ERROR, "invalid location update frequency: %s", + optarg); + goto finish; } - break; - case 'p': - { - long port; - if (demo_parse_long(optarg, &port) - || port <= 0 - || port > UINT16_MAX) { - demo_log(ERROR, "invalid UDP port number: %s", optarg); - goto finish; - } - parsed_args->udp_listen_port = (uint16_t)port; + parsed_args->location_update_frequency_s = (time_t) freq; + break; + } + case 'p': { + long port; + if (demo_parse_long(optarg, &port) || port <= 0 + || port > UINT16_MAX) { + demo_log(ERROR, "invalid UDP port number: %s", optarg); + goto finish; } + + parsed_args->udp_listen_port = (uint16_t) port; break; + } case 'i': - if (parse_hexstring(optarg, &parsed_args->connection_args - .public_cert_or_psk_identity, + if (parse_hexstring(optarg, + &parsed_args->connection_args + .public_cert_or_psk_identity, &parsed_args->connection_args .public_cert_or_psk_identity_size)) { demo_log(ERROR, "Invalid identity"); @@ -567,38 +609,38 @@ int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char *argv[]) { } break; case 'D': { - int idx = num_servers == 0 ? 0 : num_servers - 1; - if (parse_u16(optarg, - &parsed_args->connection_args.servers[idx] - .security_iid)) { - goto finish; - } + int idx = num_servers == 0 ? 0 : num_servers - 1; + if (parse_u16(optarg, + &parsed_args->connection_args.servers[idx] + .security_iid)) { + goto finish; } break; + } case 's': if (parse_security_mode( - optarg, &parsed_args->connection_args.security_mode)) { + optarg, &parsed_args->connection_args.security_mode)) { goto finish; } break; case 'd': { - int idx = num_servers == 0 ? 0 : num_servers - 1; - if (parse_u16(optarg, - &parsed_args->connection_args.servers[idx] - .server_iid)) { - goto finish; - } + int idx = num_servers == 0 ? 0 : num_servers - 1; + if (parse_u16(optarg, + &parsed_args->connection_args.servers[idx] + .server_iid)) { + goto finish; } break; + } case 'u': { - AVS_ASSERT(num_servers < MAX_SERVERS, "Too many servers"); - server_entry_t *entry = - &parsed_args->connection_args.servers[num_servers++]; - entry->uri = optarg; - entry->security_iid = ANJAY_IID_INVALID; - entry->server_iid = ANJAY_IID_INVALID; - } + AVS_ASSERT(num_servers < MAX_SERVERS, "Too many servers"); + server_entry_t *entry = + &parsed_args->connection_args.servers[num_servers++]; + entry->uri = optarg; + entry->security_iid = ANJAY_IID_INVALID; + entry->server_iid = ANJAY_IID_INVALID; break; + } case 'I': if (parse_i32(optarg, &parsed_args->inbuf_size) || parsed_args->inbuf_size <= 0) { @@ -626,46 +668,47 @@ int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char *argv[]) { || max_icmp_failures < 0) { goto finish; } - parsed_args->max_icmp_failures = (uint32_t)max_icmp_failures; + parsed_args->max_icmp_failures = (uint32_t) max_icmp_failures; break; } case 1: parsed_args->fw_updated_marker_path = optarg; break; - case 2: + case 2: { if (parsed_args->fw_security_info.mode != (avs_net_security_mode_t) -1) { demo_log(ERROR, "Multiple incompatible security information " "specified for firmware upgrade"); goto finish; } + + const avs_net_certificate_info_t cert_info = { + .server_cert_validation = true, + .trusted_certs = avs_net_trusted_cert_info_from_file(optarg) + }; parsed_args->fw_security_info = - avs_net_security_info_from_certificates( - (avs_net_certificate_info_t) { - .server_cert_validation = true, - .trusted_certs = - avs_net_trusted_cert_info_from_file(optarg) - }); - break; - case 3: + avs_net_security_info_from_certificates(cert_info); + break; + } + case 3: { if (parsed_args->fw_security_info.mode != (avs_net_security_mode_t) -1) { demo_log(ERROR, "Multiple incompatible security information " "specified for firmware upgrade"); goto finish; } + const avs_net_certificate_info_t cert_info = { + .server_cert_validation = true, + .trusted_certs = avs_net_trusted_cert_info_from_path(optarg) + }; parsed_args->fw_security_info = - avs_net_security_info_from_certificates( - (avs_net_certificate_info_t) { - .server_cert_validation = true, - .trusted_certs = - avs_net_trusted_cert_info_from_path(optarg) - }); + avs_net_security_info_from_certificates(cert_info); break; + } case 4: if (parsed_args->fw_security_info.mode != AVS_NET_SECURITY_PSK && parsed_args->fw_security_info.mode - != (avs_net_security_mode_t) -1) { + != (avs_net_security_mode_t) -1) { demo_log(ERROR, "Multiple incompatible security information " "specified for firmware upgrade"); goto finish; @@ -675,20 +718,20 @@ int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char *argv[]) { demo_log(ERROR, "--fw-psk-identity specified more than once"); goto finish; } - if (parse_hexstring( - optarg, - (uint8_t **) (intptr_t) - &parsed_args->fw_security_info.data.psk.identity, - &parsed_args->fw_security_info.data.psk.identity_size)) { + if (parse_hexstring(optarg, + (uint8_t **) (intptr_t) &parsed_args + ->fw_security_info.data.psk.identity, + &parsed_args->fw_security_info.data.psk + .identity_size)) { demo_log(ERROR, "Invalid PSK identity for firmware upgrade"); goto finish; } parsed_args->fw_security_info.mode = AVS_NET_SECURITY_PSK; break; - case 5: + case 5: { if (parsed_args->fw_security_info.mode != AVS_NET_SECURITY_PSK && parsed_args->fw_security_info.mode - != (avs_net_security_mode_t) -1) { + != (avs_net_security_mode_t) -1) { demo_log(ERROR, "Multiple incompatible security information " "specified for firmware upgrade"); goto finish; @@ -698,19 +741,103 @@ int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char *argv[]) { demo_log(ERROR, "--fw-psk-key specified more than once"); goto finish; } + + uint8_t **psk_ptr = (uint8_t **) (intptr_t) &parsed_args + ->fw_security_info.data.psk.psk; if (parse_hexstring( - optarg, - (uint8_t **) (intptr_t) - &parsed_args->fw_security_info.data.psk.psk, - &parsed_args->fw_security_info.data.psk.psk_size)) { + optarg, psk_ptr, + &parsed_args->fw_security_info.data.psk.psk_size)) { demo_log(ERROR, "Invalid pre-shared key for firmware upgrade"); goto finish; } parsed_args->fw_security_info.mode = AVS_NET_SECURITY_PSK; break; + } case 6: parsed_args->attr_storage_file = optarg; break; + case 12: + if (parse_double(optarg, + &parsed_args->tx_params.ack_random_factor)) { + demo_log(ERROR, "Expected ACK_RANDOM_FACTOR to be a floating " + "point number"); + goto finish; + } + break; + case 13: { + double ack_timeout_s; + if (parse_double(optarg, &ack_timeout_s)) { + demo_log(ERROR, + "Expected ACK_TIMEOUT to be a floating point number"); + goto finish; + } + parsed_args->tx_params.ack_timeout = + avs_time_duration_from_fscalar(ack_timeout_s, AVS_TIME_S); + break; + } + case 14: { + int32_t max_retransmit; + if (parse_i32(optarg, &max_retransmit) || max_retransmit < 0) { + demo_log(ERROR, "Expected MAX_RETRANSMIT to be an unsigned " + "integer"); + goto finish; + } + parsed_args->tx_params.max_retransmit = (unsigned) max_retransmit; + break; + } + case 15: { + double min_wait_s; + if (parse_double(optarg, &min_wait_s) || min_wait_s <= 0) { + demo_log(ERROR, "Expected DTLS_HS_RETRY_WAIT_MIN > 0"); + goto finish; + } + parsed_args->dtls_hs_tx_params.min = + avs_time_duration_from_fscalar(min_wait_s, AVS_TIME_S); + break; + } + case 16: { + double max_wait_s; + if (parse_double(optarg, &max_wait_s) || max_wait_s <= 0) { + demo_log(ERROR, "Expected DTLS_HS_RETRY_WAIT_MAX > 0"); + goto finish; + } + parsed_args->dtls_hs_tx_params.max = + avs_time_duration_from_fscalar(max_wait_s, AVS_TIME_S); + break; + } + case 17: + if (parse_double(optarg, + &parsed_args->fwu_tx_params.ack_random_factor)) { + demo_log(ERROR, "Expected ACK_RANDOM_FACTOR to be a floating " + "point number"); + goto finish; + } + parsed_args->fwu_tx_params_modified = true; + break; + case 18: { + double ack_timeout_s; + if (parse_double(optarg, &ack_timeout_s)) { + demo_log(ERROR, + "Expected ACK_TIMEOUT to be a floating point number"); + goto finish; + } + parsed_args->fwu_tx_params.ack_timeout = + avs_time_duration_from_fscalar(ack_timeout_s, AVS_TIME_S); + parsed_args->fwu_tx_params_modified = true; + break; + } + case 19: { + int32_t max_retransmit; + if (parse_i32(optarg, &max_retransmit) || max_retransmit < 0) { + demo_log(ERROR, "Expected MAX_RETRANSMIT to be an unsigned " + "integer"); + goto finish; + } + parsed_args->fwu_tx_params.max_retransmit = + (unsigned) max_retransmit; + parsed_args->fwu_tx_params_modified = true; + break; + } case 0: goto process; } @@ -737,52 +864,52 @@ int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char *argv[]) { } if (parsed_args->connection_args.security_mode == ANJAY_UDP_SECURITY_PSK) { if (!identity_set - && clone_buffer(&parsed_args->connection_args - .public_cert_or_psk_identity, - &parsed_args->connection_args - .public_cert_or_psk_identity_size, - DEFAULT_PSK_IDENTITY, - sizeof(DEFAULT_PSK_IDENTITY) - 1)) { + && clone_buffer(&parsed_args->connection_args + .public_cert_or_psk_identity, + &parsed_args->connection_args + .public_cert_or_psk_identity_size, + DEFAULT_PSK_IDENTITY, + sizeof(DEFAULT_PSK_IDENTITY) - 1)) { goto finish; } if (!key_set - && clone_buffer( - &parsed_args->connection_args.private_cert_or_psk_key, - &parsed_args->connection_args - .private_cert_or_psk_key_size, - DEFAULT_PSK_KEY, sizeof(DEFAULT_PSK_KEY) - 1)) { + && clone_buffer(&parsed_args->connection_args + .private_cert_or_psk_key, + &parsed_args->connection_args + .private_cert_or_psk_key_size, + DEFAULT_PSK_KEY, sizeof(DEFAULT_PSK_KEY) - 1)) { goto finish; } } else if (parsed_args->connection_args.security_mode - == ANJAY_UDP_SECURITY_CERTIFICATE) { + == ANJAY_UDP_SECURITY_CERTIFICATE) { if (identity_set ^ key_set) { demo_log(ERROR, "Setting public cert but not private cert (and " "other way around) makes little sense"); goto finish; } else if (!identity_set) { - if (load_buffer_from_file(&parsed_args->connection_args - .public_cert_or_psk_identity, - &parsed_args->connection_args - .public_cert_or_psk_identity_size, - cert_path)) { + if (load_buffer_from_file( + &parsed_args->connection_args + .public_cert_or_psk_identity, + &parsed_args->connection_args + .public_cert_or_psk_identity_size, + cert_path)) { demo_log(ERROR, "Could not load certificate from %s", cert_path); goto finish; } - if (load_buffer_from_file(&parsed_args->connection_args - .private_cert_or_psk_key, - &parsed_args->connection_args - .private_cert_or_psk_key_size, - key_path)) { - demo_log(ERROR, "Could not load private key from %s", - key_path); + if (load_buffer_from_file( + &parsed_args->connection_args.private_cert_or_psk_key, + &parsed_args->connection_args + .private_cert_or_psk_key_size, + key_path)) { + demo_log(ERROR, "Could not load private key from %s", key_path); goto finish; } } } if (parsed_args->fw_security_info.mode == AVS_NET_SECURITY_PSK && (!parsed_args->fw_security_info.data.psk.identity - || !parsed_args->fw_security_info.data.psk.psk)) { + || !parsed_args->fw_security_info.data.psk.psk)) { demo_log(ERROR, "Both identity and key must be provided when using PSK " "for firmware upgrade security"); goto finish; diff --git a/demo/demo_args.h b/demo/demo_args.h index c2f6cbe4e..a816b8ac0 100644 --- a/demo/demo_args.h +++ b/demo/demo_args.h @@ -17,8 +17,8 @@ #ifndef DEMO_ARGS_H #define DEMO_ARGS_H -#include "objects.h" #include "demo_utils.h" +#include "objects.h" #include #include @@ -44,6 +44,16 @@ typedef struct cmdline_args { avs_net_security_info_t fw_security_info; const char *attr_storage_file; bool disable_server_initiated_bootstrap; + avs_coap_tx_params_t tx_params; + avs_net_dtls_handshake_timeouts_t dtls_hs_tx_params; + /** + * This flag allows to enable callback providing tx_params for firmware + * update only if some of parameters were changed by passing proper command + * line argument to demo. Otherwise tx_params should be inherited from + * Anjay. + */ + bool fwu_tx_params_modified; + avs_coap_tx_params_t fwu_tx_params; } cmdline_args_t; int demo_parse_argv(cmdline_args_t *parsed_args, int argc, char **argv); diff --git a/demo/demo_cmds.c b/demo/demo_cmds.c index 7ba5f2765..570b89ea5 100644 --- a/demo/demo_cmds.c +++ b/demo/demo_cmds.c @@ -14,34 +14,31 @@ * limitations under the License. */ -#include "demo.h" #include "demo_cmds.h" +#include "demo.h" #include "demo_utils.h" #include "firmware_update.h" #include -#include #include +#include #include #include #include -static int parse_ssid(const char *text, - anjay_ssid_t *out_ssid) { +static int parse_ssid(const char *text, anjay_ssid_t *out_ssid) { unsigned id; - if (sscanf(text, "%u", &id) < 1 - || id > UINT16_MAX) { + if (sscanf(text, "%u", &id) < 1 || id > UINT16_MAX) { demo_log(ERROR, "invalid Short Server ID: %s", text); return -1; } - *out_ssid = (uint16_t)id; + *out_ssid = (uint16_t) id; return 0; } -static void cmd_send_update(anjay_demo_t *demo, - const char *args_string) { +static void cmd_send_update(anjay_demo_t *demo, const char *args_string) { anjay_ssid_t ssid = ANJAY_SSID_ANY; if (*args_string && parse_ssid(args_string, &ssid)) { return; @@ -56,9 +53,8 @@ static void cmd_send_update(anjay_demo_t *demo, } } -static void cmd_reconnect(anjay_demo_t *demo, - const char *args_string) { - (void)args_string; +static void cmd_reconnect(anjay_demo_t *demo, const char *args_string) { + (void) args_string; if (anjay_schedule_reconnect(demo->anjay)) { demo_log(ERROR, "could not schedule reconnect"); @@ -163,7 +159,7 @@ static void cmd_trim_servers(anjay_demo_t *demo, const char *args_string) { static void cmd_socket_count(anjay_demo_t *demo, const char *args_string) { (void) args_string; printf("SOCKET_COUNT==%lu\n", - (unsigned long)AVS_LIST_SIZE(anjay_get_sockets(demo->anjay))); + (unsigned long) AVS_LIST_SIZE(anjay_get_sockets(demo->anjay))); } static void cmd_get_port(anjay_demo_t *demo, const char *args_string) { @@ -180,8 +176,8 @@ static void cmd_get_port(anjay_demo_t *demo, const char *args_string) { index = num_sockets + index; } if (index < 0 || index >= num_sockets) { - demo_log(ERROR, "Index out of range: %d; num_sockets == %d", - index, num_sockets); + demo_log(ERROR, "Index out of range: %d; num_sockets == %d", index, + num_sockets); } char port[16] = "0"; AVS_LIST(avs_net_abstract_socket_t *const) socket = @@ -206,8 +202,8 @@ static void cmd_get_transport(anjay_demo_t *demo, const char *args_string) { index = num_sockets + index; } if (index < 0 || index >= num_sockets) { - demo_log(ERROR, "Index out of range: %d; num_sockets == %d", - index, num_sockets); + demo_log(ERROR, "Index out of range: %d; num_sockets == %d", index, + num_sockets); } AVS_LIST(const anjay_socket_entry_t) entry = AVS_LIST_NTH(entries, (size_t) index); @@ -263,8 +259,8 @@ static void cmd_notify(anjay_demo_t *demo, const char *args_string) { (void) anjay_notify_instances_changed(demo->anjay, oid); } else { demo_log(WARNING, "notify usage:\n" - "1. notify /OID\n" - "2. notify /OID/IID/RID"); + "1. notify /OID\n" + "2. notify /OID/IID/RID"); return; } } @@ -310,11 +306,9 @@ static int dl_write_next_block(anjay_t *anjay, return 0; } -static void dl_finished(anjay_t *anjay, - int result, - void *user_data) { +static void dl_finished(anjay_t *anjay, int result, void *user_data) { (void) anjay; - fclose((FILE *)user_data); + fclose((FILE *) user_data); demo_log(INFO, "download finished, result == %d", result); } @@ -325,7 +319,8 @@ static void cmd_download(anjay_demo_t *demo, const char *args_string) { char psk_key[256] = ""; if (sscanf(args_string, "%255s %255s %255s %255s", url, target_file, - psk_identity, psk_key) < 2) { + psk_identity, psk_key) + < 2) { demo_log(ERROR, "invalid URL or target file in: %s", args_string); return; } @@ -429,8 +424,7 @@ static void cmd_enable_server(anjay_demo_t *demo, const char *args_string) { } if (anjay_enable_server(demo->anjay, ssid)) { - demo_log(ERROR, "could not enable server with SSID %" PRIu16, - ssid); + demo_log(ERROR, "could not enable server with SSID %" PRIu16, ssid); return; } } @@ -440,14 +434,15 @@ static void cmd_help(anjay_demo_t *demo, const char *args_string); struct cmd_handler_def { const char *cmd_name; size_t cmd_name_length; - void (*handler)(anjay_demo_t*, const char*); + void (*handler)(anjay_demo_t *, const char *); const char *help_args; const char *help_descr; }; #define CMD_HANDLER(name, args, func, help) \ -{ (name), sizeof(name) - 1, (func), (args), (help) } + { (name), sizeof(name) - 1, (func), (args), (help) } static const struct cmd_handler_def COMMAND_HANDLERS[] = { + // clang-format off CMD_HANDLER("send-update", "[ssid=0]", cmd_send_update, "Sends Update messages to LwM2M servers"), CMD_HANDLER("reconnect", "", cmd_reconnect, @@ -489,6 +484,7 @@ static const struct cmd_handler_def COMMAND_HANDLERS[] = { CMD_HANDLER("enable-server", "ssid", cmd_enable_server, "Enables a server with given SSID."), CMD_HANDLER("help", "", cmd_help, "Prints this message") + // clang-format on }; #undef CMD_HANDLER @@ -534,8 +530,8 @@ static void print_with_indent(const char *text) { } static void cmd_help(anjay_demo_t *demo, const char *args_string) { - (void)demo; - (void)args_string; + (void) demo; + (void) args_string; puts("---"); puts("LwM2M Demo client"); @@ -548,8 +544,7 @@ static void cmd_help(anjay_demo_t *demo, const char *args_string) { puts("---"); } -static void handle_command(anjay_demo_t *demo, - const char *buf) { +static void handle_command(anjay_demo_t *demo, const char *buf) { size_t cmdIdx = 0; static const size_t num_command_handlers = sizeof(COMMAND_HANDLERS) / sizeof(COMMAND_HANDLERS[0]); @@ -569,7 +564,6 @@ static void handle_command(anjay_demo_t *demo, fflush(stdout); } - void demo_command_dispatch(short revents, void *demo_) { anjay_demo_t *demo = (anjay_demo_t *) demo_; if (revents & POLLHUP) { @@ -578,7 +572,7 @@ void demo_command_dispatch(short revents, void *demo_) { if (revents & POLLIN) { static char buf[500] = ""; - if (fgets(buf, sizeof (buf), stdin) && buf[0]) { + if (fgets(buf, sizeof(buf), stdin) && buf[0]) { buf[strlen(buf) - 1] = 0; handle_command(demo, buf); } diff --git a/demo/demo_utils.c b/demo/demo_utils.c index fbe6805e5..231045741 100644 --- a/demo/demo_utils.c +++ b/demo/demo_utils.c @@ -15,28 +15,30 @@ */ #if !defined(_POSIX_C_SOURCE) && !defined(__APPLE__) -#define _POSIX_C_SOURCE 200809L +# define _POSIX_C_SOURCE 200809L #endif -#include +#include #include #include +#include #include -#include #include -#include #include +#include #include "demo_utils.h" -static double geo_distance_m_with_radians(double lat1, double lon1, - double lat2, double lon2) { +static double geo_distance_m_with_radians(double lat1, + double lon1, + double lat2, + double lon2) { static const double MEAN_EARTH_PERIMETER_M = 12742017.6; // Haversine formula // code heavily inspired from http://stackoverflow.com/a/21623206 double a = 0.5 - 0.5 * cos(lat2 - lat1) - + cos(lat1) * cos(lat2) * 0.5 * (1.0 - cos(lon2 - lon1)); + + cos(lat1) * cos(lat2) * 0.5 * (1.0 - cos(lon2 - lon1)); return MEAN_EARTH_PERIMETER_M * asin(sqrt(a)); } @@ -45,8 +47,7 @@ double geo_distance_m(double lat1, double lon1, double lat2, double lon2) { deg2rad(lat2), deg2rad(lon2)); } -int demo_parse_long(const char *str, - long *out_value) { +int demo_parse_long(const char *str, long *out_value) { if (!str) { return -1; } @@ -56,11 +57,8 @@ int demo_parse_long(const char *str, errno = 0; long value = strtol(str, &endptr, 10); - if ((errno == ERANGE - && (value == LONG_MAX || value == LONG_MIN)) - || (errno != 0 && value == 0) - || endptr == str - || !endptr + if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) + || (errno != 0 && value == 0) || endptr == str || !endptr || *endptr != '\0') { demo_log(ERROR, "could not parse number: %s", str); return -1; @@ -105,8 +103,8 @@ int fetch_bytes(anjay_input_ctx_t *ctx, void **buffer, size_t *out_size) { } int open_temporary_file(char *path) { - mode_t old_umask = (mode_t) umask( - S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH); + mode_t old_umask = (mode_t) umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH + | S_IWOTH | S_IXOTH); int fd = mkstemp(path); umask(old_umask); return fd; diff --git a/demo/demo_utils.h b/demo/demo_utils.h index 1d590120c..19f0aa30b 100644 --- a/demo/demo_utils.h +++ b/demo/demo_utils.h @@ -28,15 +28,15 @@ #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) -#define STATIC_ASSERT(condition, message) \ - struct message { \ +#define STATIC_ASSERT(condition, message) \ + struct message { \ char message[(condition) ? 1 : -1]; \ } #define demo_log(level, ...) avs_log(demo, level, __VA_ARGS__) #define container_of(Ptr, Type, Member) \ - ((Type*)(intptr_t)((const char*)(Ptr) - offsetof(Type, Member))) + ((Type *) (intptr_t) ((const char *) (Ptr) -offsetof(Type, Member))) extern char **saved_argv; @@ -76,8 +76,7 @@ static inline bool velocity_bearing_deg_cw_n_valid(double value) { double geo_distance_m(double lat1, double lon1, double lat2, double lon2); -int demo_parse_long(const char *str, - long *out_value); +int demo_parse_long(const char *str, long *out_value); int fetch_bytes(anjay_input_ctx_t *ctx, void **buffer, size_t *out_size); diff --git a/demo/firmware_update.c b/demo/firmware_update.c index 36ca39b2f..d3874a32e 100644 --- a/demo/firmware_update.c +++ b/demo/firmware_update.c @@ -14,24 +14,24 @@ * limitations under the License. */ +#include "firmware_update.h" #include "demo.h" #include "demo_utils.h" -#include "firmware_update.h" #include #include #include #include -#include #include #include +#include #include +#include #include #include -#include #define FORCE_ERROR_OUT_OF_MEMORY 1 #define FORCE_ERROR_FAILED_UPDATE 2 @@ -99,8 +99,7 @@ static void fix_fw_meta_endianness(fw_metadata_t *meta) { meta->crc = ntohl(meta->crc); } -static int read_fw_meta_from_file(FILE *f, - fw_metadata_t *out_metadata) { +static int read_fw_meta_from_file(FILE *f, fw_metadata_t *out_metadata) { fw_metadata_t m; memset(&m, 0, sizeof(m)); @@ -117,8 +116,7 @@ static int read_fw_meta_from_file(FILE *f, return 0; } -static int copy_file_contents(FILE *dst, - FILE *src) { +static int copy_file_contents(FILE *dst, FILE *src) { while (!feof(src)) { char buf[4096]; @@ -160,8 +158,8 @@ static int unpack_fw_to_file(const char *fw_pkg_path, } result = copy_file_contents(tmp, fw); if (result) { - demo_log(ERROR, "could not copy firmware from %s to %s", - fw_pkg_path, target_path); + demo_log(ERROR, "could not copy firmware from %s to %s", fw_pkg_path, + target_path); goto cleanup; } @@ -183,15 +181,15 @@ static int unpack_firmware_in_place(fw_update_logic_t *fw) { return -1; } - int result = unpack_fw_to_file(fw->next_target_path, tmp_path, - &fw->metadata); + int result = + unpack_fw_to_file(fw->next_target_path, tmp_path, &fw->metadata); if (result) { goto cleanup; } if ((result = rename(tmp_path, fw->next_target_path)) == -1) { - demo_log(ERROR, "could not rename %s to %s: %s", - tmp_path, fw->next_target_path, strerror(errno)); + demo_log(ERROR, "could not rename %s to %s: %s", tmp_path, + fw->next_target_path, strerror(errno)); goto cleanup; } if ((result = chmod(fw->next_target_path, 0700)) == -1) { @@ -243,12 +241,11 @@ static void crc32(uint32_t *inout_crc, const uint8_t *data, size_t size) { for (size_t i = 0; i < size; ++i) { *inout_crc = LOOKUP_TABLE[data[i] ^ (uint8_t) *inout_crc] - ^ (*inout_crc >> 8); + ^ (*inout_crc >> 8); } } -static int get_file_crc32(const char *filename, - uint32_t *out_crc) { +static int get_file_crc32(const char *filename, uint32_t *out_crc) { FILE *f = fopen(filename, "rb"); if (!f) { demo_log(ERROR, "could not open %s", filename); @@ -262,8 +259,8 @@ static int get_file_crc32(const char *filename, while (!feof(f)) { size_t bytes_read = fread(buf, 1, sizeof(buf), f); if (bytes_read == 0 && ferror(f)) { - demo_log(ERROR, "could not read from %s: %s", - filename, strerror(errno)); + demo_log(ERROR, "could not read from %s: %s", filename, + strerror(errno)); goto cleanup; } @@ -307,10 +304,10 @@ static int validate_firmware(fw_update_logic_t *fw) { } switch (fw->metadata.force_error_case) { - case FORCE_ERROR_OUT_OF_MEMORY: - return ANJAY_FW_UPDATE_ERR_OUT_OF_MEMORY; - default: - break; + case FORCE_ERROR_OUT_OF_MEMORY: + return ANJAY_FW_UPDATE_ERR_OUT_OF_MEMORY; + default: + break; } return 0; @@ -334,8 +331,8 @@ static int store_etag(avs_persistence_context_t *ctx, uint16_t size16 = (etag ? etag->size : UINT16_MAX); int result = avs_persistence_u16(ctx, &size16); if (!result && etag) { - result = avs_persistence_bytes( - ctx, (uint8_t *) (intptr_t) etag->value, etag->size); + result = avs_persistence_bytes(ctx, (uint8_t *) (intptr_t) etag->value, + etag->size); } return result; } @@ -391,7 +388,8 @@ static void fw_reset(void *fw_) { static int fw_stream_open(void *fw_, const char *package_uri, const struct anjay_etag *package_etag) { - (void) package_uri; (void) package_etag; + (void) package_uri; + (void) package_etag; fw_update_logic_t *fw = (fw_update_logic_t *) fw_; assert(!fw->stream); @@ -415,11 +413,10 @@ static int fw_stream_open(void *fw_, avs_free(fw->package_uri); fw->package_uri = uri; - if (write_persistence_file(fw->persistence_file, - ANJAY_FW_UPDATE_INITIAL_DOWNLOADING, package_uri, - fw->next_target_path, - !!fw->administratively_set_target_path, - package_etag)) { + if (write_persistence_file( + fw->persistence_file, ANJAY_FW_UPDATE_INITIAL_DOWNLOADING, + package_uri, fw->next_target_path, + !!fw->administratively_set_target_path, package_etag)) { fw_reset(fw_); return -1; } @@ -433,11 +430,12 @@ static int fw_stream_write(void *fw_, const void *data, size_t length) { demo_log(ERROR, "stream not open"); return -1; } - if (length && (fwrite(data, length, 1, fw->stream) != 1 - // Firmware update integration tests measure download - // progress by checking file size, so avoiding buffering - // is required. - || fflush(fw->stream) != 0)) { + if (length + && (fwrite(data, length, 1, fw->stream) != 1 + // Firmware update integration tests measure download + // progress by checking file size, so avoiding buffering + // is required. + || fflush(fw->stream) != 0)) { demo_log(ERROR, "fwrite or fflush failed: %s", strerror(errno)); return ANJAY_FW_UPDATE_ERR_NOT_ENOUGH_SPACE; } @@ -457,9 +455,10 @@ static int fw_stream_finish(void *fw_) { int result; if ((result = preprocess_firmware(fw)) || (result = write_persistence_file( - fw->persistence_file, ANJAY_FW_UPDATE_INITIAL_DOWNLOADED, - fw->package_uri, fw->next_target_path, - !!fw->administratively_set_target_path, NULL))) { + fw->persistence_file, + ANJAY_FW_UPDATE_INITIAL_DOWNLOADED, fw->package_uri, + fw->next_target_path, + !!fw->administratively_set_target_path, NULL))) { fw_reset(fw); } return result; @@ -508,6 +507,15 @@ static int fw_get_security_info(void *fw_, return 0; } +static avs_coap_tx_params_t g_tx_params; + +static avs_coap_tx_params_t fw_get_coap_tx_params(void *user_ptr, + const char *download_uri) { + (void) user_ptr; + (void) download_uri; + return g_tx_params; +} + static anjay_fw_update_handlers_t FW_UPDATE_HANDLERS = { .stream_open = fw_stream_open, .stream_write = fw_stream_write, @@ -515,11 +523,11 @@ static anjay_fw_update_handlers_t FW_UPDATE_HANDLERS = { .reset = fw_reset, .get_name = fw_get_name, .get_version = fw_get_version, - .perform_upgrade = fw_perform_upgrade + .perform_upgrade = fw_perform_upgrade, + .get_coap_tx_params = fw_get_coap_tx_params }; -static int restore_etag(avs_persistence_context_t *ctx, - anjay_etag_t **etag) { +static int restore_etag(avs_persistence_context_t *ctx, anjay_etag_t **etag) { assert(etag && !*etag); uint16_t size16; int result = avs_persistence_u16(ctx, &size16); @@ -570,8 +578,7 @@ static persistence_file_data_t read_persistence_file(const char *path) { // invalid or empty but existing file still signifies success result8 = (int8_t) ANJAY_FW_UPDATE_INITIAL_SUCCESS; } - if (!stream - || !(ctx = avs_persistence_restore_context_new(stream)) + if (!stream || !(ctx = avs_persistence_restore_context_new(stream)) || avs_persistence_bytes(ctx, (uint8_t *) &result8, 1) || !is_valid_result(result8) || avs_persistence_string(ctx, &data.uri) @@ -597,7 +604,8 @@ static persistence_file_data_t read_persistence_file(const char *path) { int firmware_update_install(anjay_t *anjay, fw_update_logic_t *fw, const char *persistence_file, - const avs_net_security_info_t *security_info) { + const avs_net_security_info_t *security_info, + const avs_coap_tx_params_t *tx_params) { fw->persistence_file = persistence_file; if (security_info) { memcpy(&fw->security_info, security_info, sizeof(fw->security_info)); @@ -606,6 +614,13 @@ int firmware_update_install(anjay_t *anjay, FW_UPDATE_HANDLERS.get_security_info = NULL; } + if (tx_params) { + g_tx_params = *tx_params; + FW_UPDATE_HANDLERS.get_coap_tx_params = fw_get_coap_tx_params; + } else { + FW_UPDATE_HANDLERS.get_coap_tx_params = NULL; + } + persistence_file_data_t data = read_persistence_file(persistence_file); delete_persistence_file(fw); demo_log(INFO, "Initial firmware upgrade state result: %d", @@ -613,7 +628,7 @@ int firmware_update_install(anjay_t *anjay, if ((fw->next_target_path = data.download_file) && data.filename_administratively_set && !(fw->administratively_set_target_path = - avs_strdup(data.download_file))) { + avs_strdup(data.download_file))) { demo_log(WARNING, "Could not administratively set firmware path"); } anjay_fw_update_initial_state_t state = { @@ -643,8 +658,8 @@ int firmware_update_install(anjay_t *anjay, maybe_delete_firmware_file(fw); } - int result = anjay_fw_update_install(anjay, &FW_UPDATE_HANDLERS, fw, - &state); + int result = + anjay_fw_update_install(anjay, &FW_UPDATE_HANDLERS, fw, &state); avs_free(data.uri); avs_free(data.etag); if (result) { diff --git a/demo/firmware_update.h b/demo/firmware_update.h index c0b3fe1cd..2118b7471 100644 --- a/demo/firmware_update.h +++ b/demo/firmware_update.h @@ -18,6 +18,7 @@ #define FIRMWARE_UPDATE_H #include +#include #include @@ -41,7 +42,8 @@ typedef struct { int firmware_update_install(anjay_t *anjay, fw_update_logic_t *fw, const char *persistence_file, - const avs_net_security_info_t *security_info); + const avs_net_security_info_t *security_info, + const avs_coap_tx_params_t *tx_params); void firmware_update_destroy(fw_update_logic_t *fw_update); @@ -49,4 +51,3 @@ void firmware_update_set_package_path(fw_update_logic_t *fw_update, const char *path); #endif /* FIRMWARE_UPDATE_H */ - diff --git a/demo/iosched.c b/demo/iosched.c index 2165d7780..cc190d9c0 100644 --- a/demo/iosched.c +++ b/demo/iosched.c @@ -14,13 +14,13 @@ * limitations under the License. */ +#include "iosched.h" #include "demo.h" #include "demo_utils.h" -#include "iosched.h" +#include #include #include -#include #include #include @@ -71,8 +71,7 @@ void iosched_release(iosched_t *sched) { } } -static void insert_entry(iosched_t *sched, - AVS_LIST(iosched_entry_t) entry) { +static void insert_entry(iosched_t *sched, AVS_LIST(iosched_entry_t) entry) { AVS_LIST(iosched_entry_t) *it; AVS_LIST_FOREACH_PTR(it, &sched->entries) { if ((*it)->type >= entry->type) { @@ -126,8 +125,7 @@ const iosched_entry_t *iosched_instant_entry_new(iosched_t *sched, return entry; } -void iosched_entry_remove(iosched_t *sched, - const iosched_entry_t *entry) { +void iosched_entry_remove(iosched_t *sched, const iosched_entry_t *entry) { AVS_LIST(iosched_entry_t) *entry_ptr = AVS_LIST_FIND_PTR(&sched->entries, entry); @@ -155,9 +153,8 @@ static void handle_instant_entries(iosched_t *sched) { } } -static nfds_t get_poll_fds(iosched_t *sched, - struct pollfd *poll_fds, - size_t max_poll_fds) { +static nfds_t +get_poll_fds(iosched_t *sched, struct pollfd *poll_fds, size_t max_poll_fds) { iosched_entry_t *entry; nfds_t count = 0; @@ -175,8 +172,7 @@ static nfds_t get_poll_fds(iosched_t *sched, return count; } -static int handle_poll_entries(iosched_t *sched, - int poll_timeout_ms) { +static int handle_poll_entries(iosched_t *sched, int poll_timeout_ms) { size_t num_entries = AVS_LIST_SIZE(sched->entries); struct pollfd *poll_fds = (struct pollfd *) avs_malloc(sizeof(struct pollfd) * num_entries); @@ -195,8 +191,7 @@ static int handle_poll_entries(iosched_t *sched, AVS_LIST(iosched_entry_t) helper; AVS_LIST_DELETABLE_FOREACH_PTR(entry, helper, &sched->entries) { - while (i < poll_fds_count - && poll_fds[i].fd != (*entry)->data.poll.fd) { + while (i < poll_fds_count && poll_fds[i].fd != (*entry)->data.poll.fd) { ++i; } diff --git a/demo/iosched.h b/demo/iosched.h index 74dc3644a..e1411f042 100644 --- a/demo/iosched.h +++ b/demo/iosched.h @@ -19,29 +19,30 @@ #ifdef _WIN32 -# if defined(_WINDOWS_) || defined(_WIN32_WINNT) -# error "iosched.h needs to be included before windows.h or _mingw.h" -# endif +# if defined(_WINDOWS_) || defined(_WIN32_WINNT) +# error "iosched.h needs to be included before windows.h or _mingw.h" +# endif -# define WIN32_LEAN_AND_MEAN -# define _WIN32_WINNT 0x600 // minimum requirement: Windows NT 6.0 a.k.a. Vista -# include +# define WIN32_LEAN_AND_MEAN +# define _WIN32_WINNT \ + 0x600 // minimum requirement: Windows NT 6.0 a.k.a. Vista +# include -# ifdef ERROR +# ifdef ERROR // Windows headers are REALLY weird. winsock2.h includes windows.h, which // includes wingdi.h, even with WIN32_LEAN_AND_MEAN. And wingdi.h defines // a macro called ERROR, which conflicts with avs_log() usage. -# undef ERROR -# endif -# define poll WSAPoll +# undef ERROR +# endif +# define poll WSAPoll typedef UINT nfds_t; typedef SOCKET demo_fd_t; #else // _WIN32 -# include -# include -# include +# include +# include +# include typedef int demo_fd_t; @@ -95,8 +96,7 @@ const iosched_entry_t *iosched_instant_entry_new(iosched_t *sched, * Cancels a job represented by the @p entry and releases its arg using the * free_arg handler. If called with an invalid @p entry, does nothing. */ -void iosched_entry_remove(iosched_t *sched, - const iosched_entry_t *entry); +void iosched_entry_remove(iosched_t *sched, const iosched_entry_t *entry); int iosched_run(iosched_t *sched, int timeout_ms); diff --git a/demo/objects.h b/demo/objects.h index 22448c3dc..3dc18e4eb 100644 --- a/demo/objects.h +++ b/demo/objects.h @@ -17,35 +17,35 @@ #ifndef DEMO_OBJECTS_H #define DEMO_OBJECTS_H -#include "iosched.h" #include "demo_utils.h" +#include "iosched.h" -#include #include +#include #include #include #include -#include #include +#include typedef struct anjay_demo_struct anjay_demo_t; -#define DEMO_OID_SECURITY 0 -#define DEMO_OID_SERVER 1 -#define DEMO_OID_DEVICE 3 -#define DEMO_OID_CONN_MONITORING 4 -#define DEMO_OID_FIRMWARE_UPDATE 5 -#define DEMO_OID_LOCATION 6 -#define DEMO_OID_CONN_STATISTICS 7 +#define DEMO_OID_SECURITY 0 +#define DEMO_OID_SERVER 1 +#define DEMO_OID_DEVICE 3 +#define DEMO_OID_CONN_MONITORING 4 +#define DEMO_OID_FIRMWARE_UPDATE 5 +#define DEMO_OID_LOCATION 6 +#define DEMO_OID_CONN_STATISTICS 7 #define DEMO_OID_CELL_CONNECTIVITY 10 -#define DEMO_OID_APN_CONN_PROFILE 11 -#define DEMO_OID_TEST 1337 -#define DEMO_OID_EXT_DEV_INFO 11111 -#define DEMO_OID_IP_PING 12359 -#define DEMO_OID_GEOPOINTS 12360 -#define DEMO_OID_DOWNLOAD_DIAG 12361 +#define DEMO_OID_APN_CONN_PROFILE 11 +#define DEMO_OID_TEST 1337 +#define DEMO_OID_EXT_DEV_INFO 11111 +#define DEMO_OID_IP_PING 12359 +#define DEMO_OID_GEOPOINTS 12360 +#define DEMO_OID_DOWNLOAD_DIAG 12361 const anjay_dm_object_def_t **device_object_create(iosched_t *iosched, const char *endpoint_name); @@ -78,10 +78,10 @@ typedef struct { size_t server_public_key_size; } server_connection_args_t; -#define DEMO_FOREACH_SERVER_ENTRY(It, ConnArgs) \ - for ((It) = &(ConnArgs)->servers[0]; \ - (It) < &(ConnArgs)->servers[MAX_SERVERS] && (It)->uri; \ - ++(It)) +#define DEMO_FOREACH_SERVER_ENTRY(It, ConnArgs) \ + for ((It) = &(ConnArgs)->servers[0]; \ + (It) < &(ConnArgs)->servers[MAX_SERVERS] && (It)->uri; \ + ++(It)) #define UNDEFINED_LIFETIME -1 @@ -106,8 +106,7 @@ void ext_dev_info_object_release(const anjay_dm_object_def_t **def); void ext_dev_info_notify_time_dependent(anjay_t *anjay, const anjay_dm_object_def_t **def); -const anjay_dm_object_def_t ** -ip_ping_object_create(iosched_t *iosched); +const anjay_dm_object_def_t **ip_ping_object_create(iosched_t *iosched); void ip_ping_object_release(const anjay_dm_object_def_t **def); const anjay_dm_object_def_t **apn_conn_profile_object_create(void); @@ -125,9 +124,11 @@ void location_object_release(const anjay_dm_object_def_t **def); void location_notify_time_dependent(anjay_t *anjay, const anjay_dm_object_def_t **def); void location_get(const anjay_dm_object_def_t **def, - double *out_latitude, double *out_longitude); + double *out_latitude, + double *out_longitude); int location_open_csv(const anjay_dm_object_def_t **def, - const char *file_name, time_t frequency_s); + const char *file_name, + time_t frequency_s); const anjay_dm_object_def_t **geopoints_object_create(anjay_demo_t *demo); void geopoints_object_release(const anjay_dm_object_def_t **def); diff --git a/demo/objects/apn_conn_profile.c b/demo/objects/apn_conn_profile.c index caa604856..b94960073 100644 --- a/demo/objects/apn_conn_profile.c +++ b/demo/objects/apn_conn_profile.c @@ -14,35 +14,35 @@ * limitations under the License. */ -#include "../objects.h" #include "../demo_utils.h" +#include "../objects.h" -#include #include +#include -#define APNCP_RES_PROFILE_NAME 0 // string -#define APNCP_RES_APN 1 // string -#define APNCP_RES_AUTO_SELECT_APN_BY_DEVICE 2 // bool -#define APNCP_RES_ENABLE_STATUS 3 // bool -#define APNCP_RES_AUTHENTICATION_TYPE 4 // int -#define APNCP_RES_USER_NAME 5 // string -#define APNCP_RES_SECRET 6 // string -#define APNCP_RES_RECONNECT_SCHEDULE 7 // string -#define APNCP_RES_VALIDITY 8 // string -#define APNCP_RES_CONNECTION_ESTABLISHMENT_TIME 9 // time -#define APNCP_RES_CONNECTION_ESTABLISHMENT_RESULT 10 // int -#define APNCP_RES_CONNECTION_ESTABLISHMENT_REJECT_CAUSE 11 // int[0:111] -#define APNCP_RES_CONNECTION_END_TIME 12 // time -#define APNCP_RES_TOTAL_BYTES_SENT 13 // int -#define APNCP_RES_TOTAL_BYTES_RECEIVED 14 // int -#define APNCP_RES_IP_ADDRESS 15 // string -#define APNCP_RES_PREFIX_LENGTH 16 // string -#define APNCP_RES_SUBNET_MASK 17 // string -#define APNCP_RES_GATEWAY 18 // string -#define APNCP_RES_PRIMARY_DNS_ADDRESS 19 // string -#define APNCP_RES_SECONDARY_DNS_ADDRESS 20 // string -#define APNCP_RES_QCI 21 // int[1:9] -#define APNCP_RES_VENDOR_SPECIFIC_EXTENSIONS 22 // objlnk +#define APNCP_RES_PROFILE_NAME 0 // string +#define APNCP_RES_APN 1 // string +#define APNCP_RES_AUTO_SELECT_APN_BY_DEVICE 2 // bool +#define APNCP_RES_ENABLE_STATUS 3 // bool +#define APNCP_RES_AUTHENTICATION_TYPE 4 // int +#define APNCP_RES_USER_NAME 5 // string +#define APNCP_RES_SECRET 6 // string +#define APNCP_RES_RECONNECT_SCHEDULE 7 // string +#define APNCP_RES_VALIDITY 8 // string +#define APNCP_RES_CONNECTION_ESTABLISHMENT_TIME 9 // time +#define APNCP_RES_CONNECTION_ESTABLISHMENT_RESULT 10 // int +#define APNCP_RES_CONNECTION_ESTABLISHMENT_REJECT_CAUSE 11 // int[0:111] +#define APNCP_RES_CONNECTION_END_TIME 12 // time +#define APNCP_RES_TOTAL_BYTES_SENT 13 // int +#define APNCP_RES_TOTAL_BYTES_RECEIVED 14 // int +#define APNCP_RES_IP_ADDRESS 15 // string +#define APNCP_RES_PREFIX_LENGTH 16 // string +#define APNCP_RES_SUBNET_MASK 17 // string +#define APNCP_RES_GATEWAY 18 // string +#define APNCP_RES_PRIMARY_DNS_ADDRESS 19 // string +#define APNCP_RES_SECONDARY_DNS_ADDRESS 20 // string +#define APNCP_RES_QCI 21 // int[1:9] +#define APNCP_RES_VENDOR_SPECIFIC_EXTENSIONS 22 // objlnk typedef enum { AUTH_PAP = 0, @@ -91,9 +91,9 @@ static int apncp_instance_it(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t *out, void **cookie) { - (void)anjay; + (void) anjay; - AVS_LIST(apn_conn_profile_t) inst = (AVS_LIST(apn_conn_profile_t))*cookie; + AVS_LIST(apn_conn_profile_t) inst = (AVS_LIST(apn_conn_profile_t)) *cookie; if (!inst) { inst = get_apncp(obj_ptr)->instances; @@ -109,7 +109,7 @@ static int apncp_instance_it(anjay_t *anjay, static int apncp_instance_present(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid) { - (void)anjay; + (void) anjay; return find_instance(get_apncp(obj_ptr), iid) != NULL; } @@ -132,7 +132,7 @@ static int apncp_instance_create(anjay_t *anjay, anjay_ssid_t ssid) { (void) anjay; (void) ssid; - apn_conn_profile_repr_t* repr = get_apncp(obj_ptr); + apn_conn_profile_repr_t *repr = get_apncp(obj_ptr); AVS_LIST(apn_conn_profile_t) created = AVS_LIST_NEW_ELEMENT(apn_conn_profile_t); @@ -164,8 +164,8 @@ static int apncp_instance_create(anjay_t *anjay, static int apncp_instance_remove(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid) { - (void)anjay; - apn_conn_profile_repr_t *repr = get_apncp(obj_ptr); + (void) anjay; + apn_conn_profile_repr_t *repr = get_apncp(obj_ptr); AVS_LIST(apn_conn_profile_t) *it; AVS_LIST_FOREACH_PTR(it, &repr->instances) { @@ -184,7 +184,9 @@ static int apncp_resource_read(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void) anjay; (void) obj_ptr; (void) iid; + (void) anjay; + (void) obj_ptr; + (void) iid; apn_conn_profile_t *inst = find_instance(get_apncp(obj_ptr), iid); assert(inst); @@ -206,44 +208,44 @@ static int apncp_resource_write(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_input_ctx_t *ctx) { - (void) anjay; (void) obj_ptr; (void) iid; + (void) anjay; + (void) obj_ptr; + (void) iid; apn_conn_profile_t *inst = find_instance(get_apncp(obj_ptr), iid); assert(inst); switch (rid) { - case APNCP_RES_PROFILE_NAME: - { - char buf[sizeof(inst->profile_name)]; - if (anjay_get_string(ctx, buf, sizeof(buf)) < 0) { - return ANJAY_ERR_INTERNAL; - } - - ssize_t result = snprintf(inst->profile_name, - sizeof(inst->profile_name), "%s", buf); - if (result < 0 || result >= (ssize_t)sizeof(inst->profile_name)) { - return ANJAY_ERR_INTERNAL; - } - inst->has_profile_name = true; - return 0; + case APNCP_RES_PROFILE_NAME: { + char buf[sizeof(inst->profile_name)]; + if (anjay_get_string(ctx, buf, sizeof(buf)) < 0) { + return ANJAY_ERR_INTERNAL; + } + + ssize_t result = snprintf(inst->profile_name, + sizeof(inst->profile_name), "%s", buf); + if (result < 0 || result >= (ssize_t) sizeof(inst->profile_name)) { + return ANJAY_ERR_INTERNAL; } + inst->has_profile_name = true; + return 0; + } case APNCP_RES_ENABLE_STATUS: return anjay_get_bool(ctx, &inst->enabled); - case APNCP_RES_AUTHENTICATION_TYPE: - { - int new_val = 0; - if (anjay_get_i32(ctx, &new_val)) { - return ANJAY_ERR_INTERNAL; - } - - if (new_val < 0 || new_val >= AUTH_END_) { - return ANJAY_ERR_BAD_REQUEST; - } - - inst->auth_type = (apn_auth_type_t)new_val; - inst->has_auth_type = true; - return 0; + case APNCP_RES_AUTHENTICATION_TYPE: { + int new_val = 0; + if (anjay_get_i32(ctx, &new_val)) { + return ANJAY_ERR_INTERNAL; } + + if (new_val < 0 || new_val >= AUTH_END_) { + return ANJAY_ERR_BAD_REQUEST; + } + + inst->auth_type = (apn_auth_type_t) new_val; + inst->has_auth_type = true; + return 0; + } default: return ANJAY_ERR_NOT_FOUND; } @@ -297,10 +299,9 @@ apncp_transaction_rollback(anjay_t *anjay, return 0; } -static int -apncp_instance_reset(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid) { +static int apncp_instance_reset(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid) { (void) anjay; apn_conn_profile_t *inst = find_instance(get_apncp(obj_ptr), iid); AVS_ASSERT(inst, "could not find instance"); @@ -312,10 +313,9 @@ apncp_instance_reset(anjay_t *anjay, static const anjay_dm_object_def_t apn_conn_profile = { .oid = DEMO_OID_APN_CONN_PROFILE, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - APNCP_RES_PROFILE_NAME, - APNCP_RES_ENABLE_STATUS, - APNCP_RES_AUTHENTICATION_TYPE), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(APNCP_RES_PROFILE_NAME, + APNCP_RES_ENABLE_STATUS, + APNCP_RES_AUTHENTICATION_TYPE), .handlers = { .instance_it = apncp_instance_it, .instance_present = apncp_instance_present, @@ -333,8 +333,8 @@ static const anjay_dm_object_def_t apn_conn_profile = { }; const anjay_dm_object_def_t **apn_conn_profile_object_create(void) { - apn_conn_profile_repr_t *repr = (apn_conn_profile_repr_t *) - avs_calloc(1, sizeof(apn_conn_profile_repr_t)); + apn_conn_profile_repr_t *repr = (apn_conn_profile_repr_t *) avs_calloc( + 1, sizeof(apn_conn_profile_repr_t)); if (!repr) { return NULL; } diff --git a/demo/objects/cell_connectivity.c b/demo/objects/cell_connectivity.c index 375580ae6..53f77c032 100644 --- a/demo/objects/cell_connectivity.c +++ b/demo/objects/cell_connectivity.c @@ -15,8 +15,8 @@ */ #include "../demo.h" -#include "../objects.h" #include "../demo_utils.h" +#include "../objects.h" #include #include @@ -26,9 +26,9 @@ #define CELL_RES_MODULE_ACTIVATION_CODE 2 // string #define CELL_RES_VENDOR_SPECIFIC_EXTENSIONS 3 // objlnk -#define CELL_RES_SERVING_PLMN_RATE_CONTROL 6 // int +#define CELL_RES_SERVING_PLMN_RATE_CONTROL 6 // int -#define CELL_RES_ACTIVATED_PROFILE_NAMES 11 // objlnk[] +#define CELL_RES_ACTIVATED_PROFILE_NAMES 11 // objlnk[] #define CELL_RES_POWER_SAVING_MODES 13 // int16_t #define CELL_RES_ACTIVE_POWER_SAVING_MODES 14 // int16_t @@ -56,7 +56,8 @@ get_cell(const anjay_dm_object_def_t *const *obj_ptr) { static int cell_instance_reset(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; get_cell(obj_ptr)->active_power_saving_modes = 0; @@ -68,45 +69,45 @@ static int cell_resource_read(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; cell_connectivity_repr_t *cell = get_cell(obj_ptr); switch (rid) { case CELL_RES_SERVING_PLMN_RATE_CONTROL: return anjay_ret_i32(ctx, 0); - case CELL_RES_ACTIVATED_PROFILE_NAMES: - { - int result = ANJAY_ERR_INTERNAL; - AVS_LIST(anjay_iid_t) profile_iids = NULL; - AVS_LIST(anjay_iid_t) iid = NULL; - anjay_output_ctx_t *array = NULL; - - const anjay_dm_object_def_t **apn_conn_profile = - demo_find_object(cell->demo, DEMO_OID_APN_CONN_PROFILE); - if (!apn_conn_profile) { - goto cleanup; - } + case CELL_RES_ACTIVATED_PROFILE_NAMES: { + int result = ANJAY_ERR_INTERNAL; + AVS_LIST(anjay_iid_t) profile_iids = NULL; + AVS_LIST(anjay_iid_t) iid = NULL; + anjay_output_ctx_t *array = NULL; + + const anjay_dm_object_def_t **apn_conn_profile = + demo_find_object(cell->demo, DEMO_OID_APN_CONN_PROFILE); + if (!apn_conn_profile) { + goto cleanup; + } - profile_iids = apn_conn_profile_list_activated(apn_conn_profile); + profile_iids = apn_conn_profile_list_activated(apn_conn_profile); - array = anjay_ret_array_start(ctx); - if (!array) { - goto cleanup; - } + array = anjay_ret_array_start(ctx); + if (!array) { + goto cleanup; + } - AVS_LIST_FOREACH(iid, profile_iids) { - if (anjay_ret_array_index(array, *iid) - || anjay_ret_objlnk(array, - DEMO_OID_APN_CONN_PROFILE, *iid)) { - goto cleanup; - } + AVS_LIST_FOREACH(iid, profile_iids) { + if (anjay_ret_array_index(array, *iid) + || anjay_ret_objlnk(array, DEMO_OID_APN_CONN_PROFILE, + *iid)) { + goto cleanup; } - - result = anjay_ret_array_finish(array); -cleanup: - AVS_LIST_CLEAR(&profile_iids); - return result; } + + result = anjay_ret_array_finish(array); + cleanup: + AVS_LIST_CLEAR(&profile_iids); + return result; + } case CELL_RES_POWER_SAVING_MODES: return anjay_ret_i32(ctx, PS_ALL_AVILABLE_MODES); case CELL_RES_ACTIVE_POWER_SAVING_MODES: @@ -121,7 +122,8 @@ static int cell_resource_write(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_input_ctx_t *ctx) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; cell_connectivity_repr_t *cell = get_cell(obj_ptr); @@ -131,19 +133,19 @@ static int cell_resource_write(anjay_t *anjay, case CELL_RES_POWER_SAVING_MODES: return ANJAY_ERR_METHOD_NOT_ALLOWED; case CELL_RES_ACTIVE_POWER_SAVING_MODES: { - int32_t i32_val; - int result = anjay_get_i32(ctx, &i32_val); - if (result) { - return result; - } - if ((uint16_t)i32_val != i32_val - || (uint16_t)i32_val & ~PS_ALL_AVILABLE_MODES) { - return ANJAY_ERR_BAD_REQUEST; - } - - cell->active_power_saving_modes = (uint16_t)i32_val; - return 0; + int32_t i32_val; + int result = anjay_get_i32(ctx, &i32_val); + if (result) { + return result; } + if ((uint16_t) i32_val != i32_val + || (uint16_t) i32_val & ~PS_ALL_AVILABLE_MODES) { + return ANJAY_ERR_BAD_REQUEST; + } + + cell->active_power_saving_modes = (uint16_t) i32_val; + return 0; + } default: return ANJAY_ERR_NOT_FOUND; } @@ -153,25 +155,26 @@ static int cell_resource_dim(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid, anjay_rid_t rid) { - (void) anjay; (void) obj_ptr; (void) iid; + (void) anjay; + (void) obj_ptr; + (void) iid; cell_connectivity_repr_t *cell = get_cell(obj_ptr); switch (rid) { - case CELL_RES_ACTIVATED_PROFILE_NAMES: - { - const anjay_dm_object_def_t **apn_conn_profile = - demo_find_object(cell->demo, DEMO_OID_APN_CONN_PROFILE); - if (!apn_conn_profile) { - return ANJAY_ERR_INTERNAL; - } + case CELL_RES_ACTIVATED_PROFILE_NAMES: { + const anjay_dm_object_def_t **apn_conn_profile = + demo_find_object(cell->demo, DEMO_OID_APN_CONN_PROFILE); + if (!apn_conn_profile) { + return ANJAY_ERR_INTERNAL; + } - AVS_LIST(anjay_iid_t) profile_iids = - apn_conn_profile_list_activated(apn_conn_profile); - size_t size = AVS_LIST_SIZE(profile_iids); - AVS_LIST_CLEAR(&profile_iids); + AVS_LIST(anjay_iid_t) profile_iids = + apn_conn_profile_list_activated(apn_conn_profile); + size_t size = AVS_LIST_SIZE(profile_iids); + AVS_LIST_CLEAR(&profile_iids); - return (int)size; - } + return (int) size; + } default: return ANJAY_DM_DIM_INVALID; } @@ -201,11 +204,11 @@ cell_transaction_rollback(anjay_t *anjay, static const anjay_dm_object_def_t cell_connectivity = { .oid = DEMO_OID_CELL_CONNECTIVITY, .version = "1.1", - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - CELL_RES_SERVING_PLMN_RATE_CONTROL, - CELL_RES_ACTIVATED_PROFILE_NAMES, - CELL_RES_POWER_SAVING_MODES, - CELL_RES_ACTIVE_POWER_SAVING_MODES), + .supported_rids = + ANJAY_DM_SUPPORTED_RIDS(CELL_RES_SERVING_PLMN_RATE_CONTROL, + CELL_RES_ACTIVATED_PROFILE_NAMES, + CELL_RES_POWER_SAVING_MODES, + CELL_RES_ACTIVE_POWER_SAVING_MODES), .handlers = { .instance_it = anjay_dm_instance_it_SINGLE, .instance_present = anjay_dm_instance_present_SINGLE, @@ -223,8 +226,8 @@ static const anjay_dm_object_def_t cell_connectivity = { const anjay_dm_object_def_t ** cell_connectivity_object_create(anjay_demo_t *demo) { - cell_connectivity_repr_t *repr = (cell_connectivity_repr_t *) - avs_calloc(1, sizeof(cell_connectivity_repr_t)); + cell_connectivity_repr_t *repr = (cell_connectivity_repr_t *) avs_calloc( + 1, sizeof(cell_connectivity_repr_t)); if (!repr) { return NULL; } diff --git a/demo/objects/conn_monitoring.c b/demo/objects/conn_monitoring.c index 3cfc536d6..d89bd4688 100644 --- a/demo/objects/conn_monitoring.c +++ b/demo/objects/conn_monitoring.c @@ -14,22 +14,22 @@ * limitations under the License. */ -#include "../objects.h" #include "../demo_utils.h" +#include "../objects.h" #include -#define CM_RES_NETWORK_BEARER 0 /* int */ +#define CM_RES_NETWORK_BEARER 0 /* int */ #define CM_RES_AVAILABLE_NETWORK_BEARER 1 /* array */ -#define CM_RES_RADIO_SIGNAL_STRENGTH 2 /* int */ -#define CM_RES_LINK_QUALITY 3 /* int */ -#define CM_RES_IP_ADDRESSES 4 /* array */ -#define CM_RES_ROUTER_IP_ADDRESSES 5 /* array */ -#define CM_RES_LINK_UTILIZATION 6 /* int */ -#define CM_RES_APN 7 /* array */ -#define CM_RES_CELL_ID 8 /* int */ -#define CM_RES_SMNC 9 /* int */ -#define CM_RES_SMCC 10 /* int */ +#define CM_RES_RADIO_SIGNAL_STRENGTH 2 /* int */ +#define CM_RES_LINK_QUALITY 3 /* int */ +#define CM_RES_IP_ADDRESSES 4 /* array */ +#define CM_RES_ROUTER_IP_ADDRESSES 5 /* array */ +#define CM_RES_LINK_UTILIZATION 6 /* int */ +#define CM_RES_APN 7 /* array */ +#define CM_RES_CELL_ID 8 /* int */ +#define CM_RES_SMNC 9 /* int */ +#define CM_RES_SMCC 10 /* int */ typedef struct { const anjay_dm_object_def_t *def; @@ -52,7 +52,9 @@ static int cm_resource_read(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void) anjay; (void) obj_ptr; (void) iid; + (void) anjay; + (void) obj_ptr; + (void) iid; enum { NB_CELLULAR_GSM = 0, @@ -89,7 +91,8 @@ static int cm_resource_read(anjay_t *anjay, || anjay_ret_array_index(array, 4) || anjay_ret_i32(array, NB_WIRELESS_BLUETOOTH) || anjay_ret_array_finish(array)) - ? -1 : 0; + ? -1 + : 0; case CM_RES_RADIO_SIGNAL_STRENGTH: return anjay_ret_i32(ctx, signal_strength_dbm()); case CM_RES_LINK_QUALITY: @@ -99,13 +102,15 @@ static int cm_resource_read(anjay_t *anjay, || anjay_ret_array_index(array, 0) || anjay_ret_string(array, "10.10.53.53") || anjay_ret_array_finish(array)) - ? -1 : 0; + ? -1 + : 0; case CM_RES_ROUTER_IP_ADDRESSES: return (!(array = anjay_ret_array_start(ctx)) || anjay_ret_array_index(array, 0) || anjay_ret_string(array, "10.10.0.1") || anjay_ret_array_finish(array)) - ? -1 : 0; + ? -1 + : 0; case CM_RES_LINK_UTILIZATION: return anjay_ret_i32(ctx, 50); case CM_RES_APN: @@ -113,7 +118,8 @@ static int cm_resource_read(anjay_t *anjay, || anjay_ret_array_index(array, 0) || anjay_ret_string(array, "internet") || anjay_ret_array_finish(array)) - ? -1 : 0; + ? -1 + : 0; case CM_RES_CELL_ID: return anjay_ret_i32(ctx, 12345); case CM_RES_SMNC: @@ -129,7 +135,9 @@ static int cm_resource_dim(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid, anjay_rid_t rid) { - (void) anjay; (void) obj_ptr; (void) iid; + (void) anjay; + (void) obj_ptr; + (void) iid; switch (rid) { case CM_RES_AVAILABLE_NETWORK_BEARER: @@ -145,18 +153,17 @@ static int cm_resource_dim(anjay_t *anjay, static const anjay_dm_object_def_t CONN_MONITORING = { .oid = DEMO_OID_CONN_MONITORING, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - CM_RES_NETWORK_BEARER, - CM_RES_AVAILABLE_NETWORK_BEARER, - CM_RES_RADIO_SIGNAL_STRENGTH, - CM_RES_LINK_QUALITY, - CM_RES_IP_ADDRESSES, - CM_RES_ROUTER_IP_ADDRESSES, - CM_RES_LINK_UTILIZATION, - CM_RES_APN, - CM_RES_CELL_ID, - CM_RES_SMNC, - CM_RES_SMCC), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(CM_RES_NETWORK_BEARER, + CM_RES_AVAILABLE_NETWORK_BEARER, + CM_RES_RADIO_SIGNAL_STRENGTH, + CM_RES_LINK_QUALITY, + CM_RES_IP_ADDRESSES, + CM_RES_ROUTER_IP_ADDRESSES, + CM_RES_LINK_UTILIZATION, + CM_RES_APN, + CM_RES_CELL_ID, + CM_RES_SMNC, + CM_RES_SMCC), .handlers = { .instance_it = anjay_dm_instance_it_SINGLE, .instance_present = anjay_dm_instance_present_SINGLE, @@ -167,8 +174,8 @@ static const anjay_dm_object_def_t CONN_MONITORING = { }; const anjay_dm_object_def_t **cm_object_create(void) { - conn_monitoring_repr_t *repr = (conn_monitoring_repr_t *) - avs_calloc(1, sizeof(conn_monitoring_repr_t)); + conn_monitoring_repr_t *repr = (conn_monitoring_repr_t *) avs_calloc( + 1, sizeof(conn_monitoring_repr_t)); if (!repr) { return NULL; } diff --git a/demo/objects/conn_statistics.c b/demo/objects/conn_statistics.c index 331e18b7a..ffe3e956c 100644 --- a/demo/objects/conn_statistics.c +++ b/demo/objects/conn_statistics.c @@ -14,23 +14,23 @@ * limitations under the License. */ -#include "../objects.h" #include "../demo_utils.h" +#include "../objects.h" #include #include #include typedef enum { - CS_SMS_TX_COUNTER = 0, - CS_SMS_RX_COUNTER = 1, - CS_TX_KB = 2, - CS_RX_KB = 3, - CS_MAX_MSG_SIZE = 4, - CS_AVG_MSG_SIZE = 5, - CS_START = 6, - CS_STOP = 7, - CS_COLLECTION_PERIOD = 8, + CS_SMS_TX_COUNTER = 0, + CS_SMS_RX_COUNTER = 1, + CS_TX_KB = 2, + CS_RX_KB = 3, + CS_MAX_MSG_SIZE = 4, + CS_AVG_MSG_SIZE = 5, + CS_START = 6, + CS_STOP = 7, + CS_COLLECTION_PERIOD = 8, } conn_stats_res_t; typedef struct { @@ -70,18 +70,18 @@ static int ifname_for_first_socket(anjay_t *anjay, return avs_net_socket_interface_name(*first, if_name); } -static const char* RX_STATS = "rx_bytes"; -static const char* TX_STATS = "tx_bytes"; +static const char *RX_STATS = "rx_bytes"; +static const char *TX_STATS = "tx_bytes"; static int stats_getter(avs_net_socket_interface_name_t *if_name, uint64_t *bytes, - const char* stats) { + const char *stats) { char file_name[128]; sprintf(file_name, "/sys/class/net/%s/statistics/%s", *if_name, stats); return read_uint64_from_file(bytes, file_name); } -static uint64_t first_socket_stats(anjay_t *anjay, const char* stats) { +static uint64_t first_socket_stats(anjay_t *anjay, const char *stats) { avs_net_socket_interface_name_t if_name; memset(&if_name, 0, sizeof(if_name)); if (ifname_for_first_socket(anjay, &if_name)) { @@ -152,9 +152,9 @@ static int cs_resource_execute(anjay_t *anjay, return ANJAY_ERR_BAD_REQUEST; } repr->last_tx_bytes = - first_socket_stats(anjay, TX_STATS) - repr->last_tx_bytes; + first_socket_stats(anjay, TX_STATS) - repr->last_tx_bytes; repr->last_rx_bytes = - first_socket_stats(anjay, RX_STATS) - repr->last_rx_bytes; + first_socket_stats(anjay, RX_STATS) - repr->last_rx_bytes; repr->is_collecting = false; break; default: @@ -198,17 +198,17 @@ static int cs_resource_write(anjay_t *anjay, conn_stats_repr_t *repr = get_cs(obj_ptr); switch (rid) { case CS_COLLECTION_PERIOD: { - int32_t val; - int result; - if ((result = anjay_get_i32(ctx, &val))) { - return result; - } else if (val < 0) { - return ANJAY_ERR_BAD_REQUEST; - } - - repr->collection_period = (uint32_t) val; - return 0; + int32_t val; + int result; + if ((result = anjay_get_i32(ctx, &val))) { + return result; + } else if (val < 0) { + return ANJAY_ERR_BAD_REQUEST; } + + repr->collection_period = (uint32_t) val; + return 0; + } case CS_MAX_MSG_SIZE: case CS_AVG_MSG_SIZE: case CS_SMS_TX_COUNTER: @@ -224,16 +224,15 @@ static int cs_resource_write(anjay_t *anjay, static const anjay_dm_object_def_t CONN_STATISTICS = { .oid = DEMO_OID_CONN_STATISTICS, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - CS_SMS_TX_COUNTER, - CS_SMS_RX_COUNTER, - CS_TX_KB, - CS_RX_KB, - CS_MAX_MSG_SIZE, - CS_AVG_MSG_SIZE, - CS_START, - CS_STOP, - CS_COLLECTION_PERIOD), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(CS_SMS_TX_COUNTER, + CS_SMS_RX_COUNTER, + CS_TX_KB, + CS_RX_KB, + CS_MAX_MSG_SIZE, + CS_AVG_MSG_SIZE, + CS_START, + CS_STOP, + CS_COLLECTION_PERIOD), .handlers = { .instance_it = anjay_dm_instance_it_SINGLE, .instance_present = anjay_dm_instance_present_SINGLE, @@ -250,8 +249,8 @@ static const anjay_dm_object_def_t CONN_STATISTICS = { }; const anjay_dm_object_def_t **cs_object_create(void) { - conn_stats_repr_t *repr = (conn_stats_repr_t *) - avs_calloc(1, sizeof(conn_stats_repr_t)); + conn_stats_repr_t *repr = + (conn_stats_repr_t *) avs_calloc(1, sizeof(conn_stats_repr_t)); if (!repr) { return NULL; } diff --git a/demo/objects/device.c b/demo/objects/device.c index 7bfdb4fa1..aa4d61174 100644 --- a/demo/objects/device.c +++ b/demo/objects/device.c @@ -14,8 +14,8 @@ * limitations under the License. */ -#include "../objects.h" #include "../demo_utils.h" +#include "../objects.h" #include @@ -25,29 +25,29 @@ #include -#define DEV_RES_MANUFACTURER 0 // string -#define DEV_RES_MODEL_NUMBER 1 // string -#define DEV_RES_SERIAL_NUMBER 2 // string -#define DEV_RES_FIRMWARE_VERSION 3 // string +#define DEV_RES_MANUFACTURER 0 // string +#define DEV_RES_MODEL_NUMBER 1 // string +#define DEV_RES_SERIAL_NUMBER 2 // string +#define DEV_RES_FIRMWARE_VERSION 3 // string #define DEV_RES_REBOOT 4 #define DEV_RES_FACTORY_RESET 5 -#define DEV_RES_AVAILABLE_POWER_SOURCES 6 // array -#define DEV_RES_POWER_SOURCE_VOLTAGE 7 // array -#define DEV_RES_POWER_SOURCE_CURRENT 8 // array -#define DEV_RES_BATTERY_LEVEL 9 // int -#define DEV_RES_MEMORY_FREE 10 // int -#define DEV_RES_ERROR_CODE 11 // int +#define DEV_RES_AVAILABLE_POWER_SOURCES 6 // array +#define DEV_RES_POWER_SOURCE_VOLTAGE 7 // array +#define DEV_RES_POWER_SOURCE_CURRENT 8 // array +#define DEV_RES_BATTERY_LEVEL 9 // int +#define DEV_RES_MEMORY_FREE 10 // int +#define DEV_RES_ERROR_CODE 11 // int #define DEV_RES_RESET_ERROR_CODE 12 -#define DEV_RES_CURRENT_TIME 13 // time -#define DEV_RES_UTC_OFFSET 14 // string -#define DEV_RES_TIMEZONE 15 // string -#define DEV_RES_SUPPORTED_BINDING_AND_MODES 16 // string -#define DEV_RES_DEVICE_TYPE 17 // string -#define DEV_RES_HARDWARE_VERSION 18 // string -#define DEV_RES_SOFTWARE_VERSION 19 // string -#define DEV_RES_BATTERY_STATUS 20 // int -#define DEV_RES_MEMORY_TOTAL 21 // int -#define DEV_RES_EXTDEVINFO 22 // objlnk +#define DEV_RES_CURRENT_TIME 13 // time +#define DEV_RES_UTC_OFFSET 14 // string +#define DEV_RES_TIMEZONE 15 // string +#define DEV_RES_SUPPORTED_BINDING_AND_MODES 16 // string +#define DEV_RES_DEVICE_TYPE 17 // string +#define DEV_RES_HARDWARE_VERSION 18 // string +#define DEV_RES_SOFTWARE_VERSION 19 // string +#define DEV_RES_BATTERY_STATUS 20 // int +#define DEV_RES_MEMORY_TOTAL 21 // int +#define DEV_RES_EXTDEVINFO 22 // objlnk typedef enum { DEV_ERR_NO_ERROR = 0, @@ -89,23 +89,20 @@ static inline dev_repr_t *get_dev(const anjay_dm_object_def_t *const *obj_ptr) { return container_of(obj_ptr, dev_repr_t, def); } -static int32_t randint_from_range(int32_t min_value, - int32_t max_value) { +static int32_t randint_from_range(int32_t min_value, int32_t max_value) { assert(min_value <= max_value); // RNG-like predictable generation in range [min_value, max_value] int32_t diff = max_value - min_value; - return min_value + (int32_t)time_to_rand() % (diff + 1); + return min_value + (int32_t) time_to_rand() % (diff + 1); } static int32_t get_dc_voltage_mv(void) { - return randint_from_range(32 * 1000 - 500, - 32 * 1000 + 500); + return randint_from_range(32 * 1000 - 500, 32 * 1000 + 500); } static int32_t get_dc_current_ma(void) { - return randint_from_range(10 - 1, - 10 + 1); + return randint_from_range(10 - 1, 10 + 1); } static int dev_read(anjay_t *anjay, @@ -113,7 +110,8 @@ static int dev_read(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; dev_repr_t *dev = get_dev(obj_ptr); @@ -126,49 +124,41 @@ static int dev_read(anjay_t *anjay, return anjay_ret_string(ctx, dev->serial_number); case DEV_RES_FIRMWARE_VERSION: return anjay_ret_string(ctx, anjay_get_version()); - case DEV_RES_AVAILABLE_POWER_SOURCES: - { - anjay_output_ctx_t *array = anjay_ret_array_start(ctx); - if (!array - || anjay_ret_array_index(array, 0) - || anjay_ret_i32(array, (int32_t)POWER_SOURCE_DC)) { - return ANJAY_ERR_INTERNAL; - } - return anjay_ret_array_finish(array); + case DEV_RES_AVAILABLE_POWER_SOURCES: { + anjay_output_ctx_t *array = anjay_ret_array_start(ctx); + if (!array || anjay_ret_array_index(array, 0) + || anjay_ret_i32(array, (int32_t) POWER_SOURCE_DC)) { + return ANJAY_ERR_INTERNAL; } - case DEV_RES_POWER_SOURCE_VOLTAGE: - { - anjay_output_ctx_t *array = anjay_ret_array_start(ctx); - if (!array - || anjay_ret_array_index(array, 0) - || anjay_ret_i32(array, get_dc_voltage_mv())) { - return ANJAY_ERR_INTERNAL; - } - return anjay_ret_array_finish(array); + return anjay_ret_array_finish(array); + } + case DEV_RES_POWER_SOURCE_VOLTAGE: { + anjay_output_ctx_t *array = anjay_ret_array_start(ctx); + if (!array || anjay_ret_array_index(array, 0) + || anjay_ret_i32(array, get_dc_voltage_mv())) { + return ANJAY_ERR_INTERNAL; } - case DEV_RES_POWER_SOURCE_CURRENT: - { - anjay_output_ctx_t *array = anjay_ret_array_start(ctx); - if (!array - || anjay_ret_array_index(array, 0) - || anjay_ret_i32(array, get_dc_current_ma())) { - return ANJAY_ERR_INTERNAL; - } - return anjay_ret_array_finish(array); + return anjay_ret_array_finish(array); + } + case DEV_RES_POWER_SOURCE_CURRENT: { + anjay_output_ctx_t *array = anjay_ret_array_start(ctx); + if (!array || anjay_ret_array_index(array, 0) + || anjay_ret_i32(array, get_dc_current_ma())) { + return ANJAY_ERR_INTERNAL; } + return anjay_ret_array_finish(array); + } case DEV_RES_BATTERY_LEVEL: case DEV_RES_MEMORY_FREE: return anjay_ret_i32(ctx, 0); - case DEV_RES_ERROR_CODE: - { - anjay_output_ctx_t *array = anjay_ret_array_start(ctx); - if (!array - || anjay_ret_array_index(array, 0) - || anjay_ret_i32(array, (int32_t) dev->last_error)) { - return ANJAY_ERR_INTERNAL; - } - return anjay_ret_array_finish(array); + case DEV_RES_ERROR_CODE: { + anjay_output_ctx_t *array = anjay_ret_array_start(ctx); + if (!array || anjay_ret_array_index(array, 0) + || anjay_ret_i32(array, (int32_t) dev->last_error)) { + return ANJAY_ERR_INTERNAL; } + return anjay_ret_array_finish(array); + } case DEV_RES_CURRENT_TIME: return anjay_ret_i64(ctx, avs_time_real_now().since_real_epoch.seconds + dev->current_time_offset); @@ -180,16 +170,14 @@ static int dev_read(anjay_t *anjay, return anjay_ret_string(ctx, ""); case DEV_RES_SUPPORTED_BINDING_AND_MODES: return anjay_ret_string(ctx, "UQ"); - case DEV_RES_EXTDEVINFO: - { - anjay_output_ctx_t *array = anjay_ret_array_start(ctx); - if (!array - || anjay_ret_array_index(array, 0) - || anjay_ret_objlnk(array, DEMO_OID_EXT_DEV_INFO, 0)) { - return ANJAY_ERR_INTERNAL; - } - return anjay_ret_array_finish(array); + case DEV_RES_EXTDEVINFO: { + anjay_output_ctx_t *array = anjay_ret_array_start(ctx); + if (!array || anjay_ret_array_index(array, 0) + || anjay_ret_objlnk(array, DEMO_OID_EXT_DEV_INFO, 0)) { + return ANJAY_ERR_INTERNAL; } + return anjay_ret_array_finish(array); + } case DEV_RES_HARDWARE_VERSION: case DEV_RES_SOFTWARE_VERSION: return anjay_ret_string(ctx, ""); @@ -204,9 +192,8 @@ static int dev_read(anjay_t *anjay, } } -static int read_string(anjay_input_ctx_t *ctx, - char *buffer, - size_t buffer_size) { +static int +read_string(anjay_input_ctx_t *ctx, char *buffer, size_t buffer_size) { char *tmp = (char *) avs_malloc(buffer_size); if (!tmp) { demo_log(ERROR, "Out of memory"); @@ -233,23 +220,23 @@ static int dev_write(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_input_ctx_t *ctx) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; dev_repr_t *dev = get_dev(obj_ptr); switch (rid) { - case DEV_RES_CURRENT_TIME: - { - int64_t new_time; - int result = anjay_get_i64(ctx, &new_time); - if (result) { - return result; - } - - time_t now = time(NULL); - dev->current_time_offset = (time_t)new_time - now; - return 0; + case DEV_RES_CURRENT_TIME: { + int64_t new_time; + int result = anjay_get_i64(ctx, &new_time); + if (result) { + return result; } + + time_t now = time(NULL); + dev->current_time_offset = (time_t) new_time - now; + return 0; + } case DEV_RES_UTC_OFFSET: return read_string(ctx, dev->utc_offset, sizeof(dev->utc_offset)); case DEV_RES_TIMEZONE: @@ -268,7 +255,8 @@ static void dev_instance_reset_impl(dev_repr_t *dev) { static int dev_instance_reset(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; dev_instance_reset_impl(get_dev(obj_ptr)); return 0; @@ -299,15 +287,17 @@ static int dev_execute(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_execute_ctx_t *ctx) { - (void) anjay; (void) iid; (void) ctx; + (void) anjay; + (void) iid; + (void) ctx; dev_repr_t *dev = get_dev(obj_ptr); switch (rid) { case DEV_RES_REBOOT: case DEV_RES_FACTORY_RESET: - if (!iosched_instant_entry_new(dev->iosched, - perform_reboot, NULL, NULL)) { + if (!iosched_instant_entry_new(dev->iosched, perform_reboot, NULL, + NULL)) { return ANJAY_ERR_INTERNAL; } @@ -321,7 +311,9 @@ static int dev_dim(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid, anjay_rid_t rid) { - (void) anjay; (void) obj_ptr; (void) iid; + (void) anjay; + (void) obj_ptr; + (void) iid; switch (rid) { case DEV_RES_AVAILABLE_POWER_SOURCES: @@ -337,29 +329,29 @@ static int dev_dim(anjay_t *anjay, static const anjay_dm_object_def_t DEVICE = { .oid = DEMO_OID_DEVICE, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - DEV_RES_MANUFACTURER, - DEV_RES_MODEL_NUMBER, - DEV_RES_SERIAL_NUMBER, - DEV_RES_FIRMWARE_VERSION, - DEV_RES_REBOOT, - DEV_RES_FACTORY_RESET, - DEV_RES_AVAILABLE_POWER_SOURCES, - DEV_RES_POWER_SOURCE_VOLTAGE, - DEV_RES_POWER_SOURCE_CURRENT, - DEV_RES_BATTERY_LEVEL, - DEV_RES_MEMORY_FREE, - DEV_RES_ERROR_CODE, - DEV_RES_CURRENT_TIME, - DEV_RES_UTC_OFFSET, - DEV_RES_TIMEZONE, - DEV_RES_SUPPORTED_BINDING_AND_MODES, - DEV_RES_DEVICE_TYPE, - DEV_RES_HARDWARE_VERSION, - DEV_RES_SOFTWARE_VERSION, - DEV_RES_BATTERY_STATUS, - DEV_RES_MEMORY_TOTAL, - DEV_RES_EXTDEVINFO), + .supported_rids = + ANJAY_DM_SUPPORTED_RIDS(DEV_RES_MANUFACTURER, + DEV_RES_MODEL_NUMBER, + DEV_RES_SERIAL_NUMBER, + DEV_RES_FIRMWARE_VERSION, + DEV_RES_REBOOT, + DEV_RES_FACTORY_RESET, + DEV_RES_AVAILABLE_POWER_SOURCES, + DEV_RES_POWER_SOURCE_VOLTAGE, + DEV_RES_POWER_SOURCE_CURRENT, + DEV_RES_BATTERY_LEVEL, + DEV_RES_MEMORY_FREE, + DEV_RES_ERROR_CODE, + DEV_RES_CURRENT_TIME, + DEV_RES_UTC_OFFSET, + DEV_RES_TIMEZONE, + DEV_RES_SUPPORTED_BINDING_AND_MODES, + DEV_RES_DEVICE_TYPE, + DEV_RES_HARDWARE_VERSION, + DEV_RES_SOFTWARE_VERSION, + DEV_RES_BATTERY_STATUS, + DEV_RES_MEMORY_TOTAL, + DEV_RES_EXTDEVINFO), .handlers = { .instance_it = anjay_dm_instance_it_SINGLE, .instance_present = anjay_dm_instance_present_SINGLE, @@ -393,10 +385,10 @@ static void extract_device_info(const char *endpoint_name, demo_log(WARNING, "empty manufacturer part of endpoint name"); strncpy(out_manufacturer, "Anjay", sizeof("Anjay")); } else { - AVS_ASSERT((size_t)(dash - at) < manufacturer_size, + AVS_ASSERT((size_t) (dash - at) < manufacturer_size, "manufacturer part of endpoint name too long"); (void) manufacturer_size; - strncpy(out_manufacturer, at, (size_t)(dash - at)); + strncpy(out_manufacturer, at, (size_t) (dash - at)); at = dash + 1; } @@ -412,13 +404,13 @@ static void extract_device_info(const char *endpoint_name, strncpy(out_serial, at, serial_length); } - demo_log(DEBUG, "manufacturer: %s; serial number: %s", - out_manufacturer, out_serial); + demo_log(DEBUG, "manufacturer: %s; serial number: %s", out_manufacturer, + out_serial); } const anjay_dm_object_def_t **device_object_create(iosched_t *iosched, const char *endpoint_name) { - dev_repr_t *repr = (dev_repr_t*)avs_calloc(1, sizeof(dev_repr_t)); + dev_repr_t *repr = (dev_repr_t *) avs_calloc(1, sizeof(dev_repr_t)); if (!repr) { return NULL; } @@ -428,9 +420,9 @@ const anjay_dm_object_def_t **device_object_create(iosched_t *iosched, repr->last_error = DEV_ERR_NO_ERROR; dev_instance_reset_impl(repr); - extract_device_info(endpoint_name, - repr->manufacturer, sizeof(repr->manufacturer), - repr->serial_number, sizeof(repr->serial_number)); + extract_device_info(endpoint_name, repr->manufacturer, + sizeof(repr->manufacturer), repr->serial_number, + sizeof(repr->serial_number)); return &repr->def; } diff --git a/demo/objects/download_diagnostics.c b/demo/objects/download_diagnostics.c index 41c59aac2..00c9152b4 100644 --- a/demo/objects/download_diagnostics.c +++ b/demo/objects/download_diagnostics.c @@ -14,32 +14,32 @@ * limitations under the License. */ -#include "../objects.h" #include "../demo_utils.h" +#include "../objects.h" #include -#include #include -#include +#include #include +#include typedef enum { - DOWNLOAD_DIAG_STATE = 0, - DOWNLOAD_DIAG_URL = 1, - DOWNLOAD_DIAG_ROM_TIME_US = 2, + DOWNLOAD_DIAG_STATE = 0, + DOWNLOAD_DIAG_URL = 1, + DOWNLOAD_DIAG_ROM_TIME_US = 2, /* begin of transmission time */ - DOWNLOAD_DIAG_BOM_TIME_US = 3, + DOWNLOAD_DIAG_BOM_TIME_US = 3, /* end of transmission time */ - DOWNLOAD_DIAG_EOM_TIME_US = 4, + DOWNLOAD_DIAG_EOM_TIME_US = 4, /* total number of bytes transmitted between BOM_TIME and EOM_TIME */ - DOWNLOAD_DIAG_TOTAL_BYTES = 5, - DOWNLOAD_DIAG_RUN = 6 + DOWNLOAD_DIAG_TOTAL_BYTES = 5, + DOWNLOAD_DIAG_RUN = 6 } download_diag_res_t; typedef enum { - DIAG_STATE_NONE = 0, - DIAG_STATE_REQUESTED = 1, - DIAG_STATE_COMPLETED = 2, - DIAG_STATE_TRANSFER_FAILED = 3 + DIAG_STATE_NONE = 0, + DIAG_STATE_REQUESTED = 1, + DIAG_STATE_COMPLETED = 2, + DIAG_STATE_TRANSFER_FAILED = 3 } download_diag_state_t; typedef struct { @@ -56,7 +56,8 @@ typedef struct { download_diag_state_t state; } download_diag_repr_t; -static download_diag_repr_t *get_diag(const anjay_dm_object_def_t *const *obj_ptr) { +static download_diag_repr_t * +get_diag(const anjay_dm_object_def_t *const *obj_ptr) { assert(obj_ptr); return container_of(obj_ptr, download_diag_repr_t, def); } @@ -89,16 +90,16 @@ static int dl_block_callback(anjay_t *anjay, size_t data_size, const anjay_etag_t *etag, void *user_data) { - (void) anjay; (void) data; (void) etag; + (void) anjay; + (void) data; + (void) etag; download_diag_repr_t *repr = (download_diag_repr_t *) user_data; repr->stats.bytes_received += data_size; update_times(repr); return 0; } -static void dl_finish_callback(anjay_t *anjay, - int result, - void *user_data) { +static void dl_finish_callback(anjay_t *anjay, int result, void *user_data) { download_diag_repr_t *repr = (download_diag_repr_t *) user_data; update_times(repr); if (result) { @@ -108,8 +109,7 @@ static void dl_finish_callback(anjay_t *anjay, } } -static int diag_download_run(anjay_t *anjay, - download_diag_repr_t *repr) { +static int diag_download_run(anjay_t *anjay, download_diag_repr_t *repr) { if (repr->dl_handle) { demo_log(ERROR, "download diagnostic already in progress"); return -1; @@ -240,22 +240,21 @@ static int diag_resource_write(anjay_t *anjay, (void) ctx; download_diag_repr_t *repr = get_diag(obj_ptr); switch ((download_diag_res_t) rid) { - case DOWNLOAD_DIAG_URL: - { - if (repr->state == DIAG_STATE_REQUESTED) { - demo_log(ERROR, - "Cancelling a diagnostic in progress is not supported"); - return ANJAY_ERR_BAD_REQUEST; - } + case DOWNLOAD_DIAG_URL: { + if (repr->state == DIAG_STATE_REQUESTED) { + demo_log(ERROR, + "Cancelling a diagnostic in progress is not supported"); + return ANJAY_ERR_BAD_REQUEST; + } + reset_diagnostic(repr); + int result = anjay_get_string(ctx, repr->download_url, + sizeof(repr->download_url)); + if (result < 0 || result == ANJAY_BUFFER_TOO_SHORT) { reset_diagnostic(repr); - int result = anjay_get_string(ctx, repr->download_url, - sizeof(repr->download_url)); - if (result < 0 || result == ANJAY_BUFFER_TOO_SHORT) { - reset_diagnostic(repr); - result = result < 0 ? result : ANJAY_ERR_BAD_REQUEST; - } - return result; + result = result < 0 ? result : ANJAY_ERR_BAD_REQUEST; } + return result; + } default: return ANJAY_ERR_METHOD_NOT_ALLOWED; } @@ -264,14 +263,13 @@ static int diag_resource_write(anjay_t *anjay, static const anjay_dm_object_def_t DOWNLOAD_DIAG = { .oid = DEMO_OID_DOWNLOAD_DIAG, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - DOWNLOAD_DIAG_STATE, - DOWNLOAD_DIAG_URL, - DOWNLOAD_DIAG_ROM_TIME_US, - DOWNLOAD_DIAG_BOM_TIME_US, - DOWNLOAD_DIAG_EOM_TIME_US, - DOWNLOAD_DIAG_TOTAL_BYTES, - DOWNLOAD_DIAG_RUN), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(DOWNLOAD_DIAG_STATE, + DOWNLOAD_DIAG_URL, + DOWNLOAD_DIAG_ROM_TIME_US, + DOWNLOAD_DIAG_BOM_TIME_US, + DOWNLOAD_DIAG_EOM_TIME_US, + DOWNLOAD_DIAG_TOTAL_BYTES, + DOWNLOAD_DIAG_RUN), .handlers = { .instance_it = anjay_dm_instance_it_SINGLE, .instance_present = anjay_dm_instance_present_SINGLE, @@ -287,8 +285,9 @@ static const anjay_dm_object_def_t DOWNLOAD_DIAG = { }; const anjay_dm_object_def_t **download_diagnostics_object_create(void) { - download_diag_repr_t *repr = (download_diag_repr_t *) - avs_calloc(1, sizeof(download_diag_repr_t)); + download_diag_repr_t *repr = + (download_diag_repr_t *) avs_calloc(1, + sizeof(download_diag_repr_t)); if (!repr) { return NULL; } diff --git a/demo/objects/ext_dev_info.c b/demo/objects/ext_dev_info.c index a6728c37f..22ff63d7f 100644 --- a/demo/objects/ext_dev_info.c +++ b/demo/objects/ext_dev_info.c @@ -14,37 +14,38 @@ * limitations under the License. */ -#include "../objects.h" #include "../demo_utils.h" +#include "../objects.h" #include #include #include -#define EXT_DEV_RES_OBU_ID 0 // string -#define EXT_DEV_RES_PLATE_NUMBER 1 // string -#define EXT_DEV_RES_IMEI 2 // string -#define EXT_DEV_RES_IMSI 3 // string -#define EXT_DEV_RES_ICCID 4 // string -#define EXT_DEV_RES_GPRS_RSSI 5 // int -#define EXT_DEV_RES_GPRS_PLMN 6 // int -#define EXT_DEV_RES_GPRS_ULMODULATION 7 // string -#define EXT_DEV_RES_GPRS_DLMODULATION 8 // string -#define EXT_DEV_RES_GPRS_ULFREQUENCY 9 // int -#define EXT_DEV_RES_GPRS_DLFREQUENCY 10 // int -#define EXT_DEV_RES_RX_BYTES 11 // uint64 -#define EXT_DEV_RES_TX_BYTES 12 // uint64 +#define EXT_DEV_RES_OBU_ID 0 // string +#define EXT_DEV_RES_PLATE_NUMBER 1 // string +#define EXT_DEV_RES_IMEI 2 // string +#define EXT_DEV_RES_IMSI 3 // string +#define EXT_DEV_RES_ICCID 4 // string +#define EXT_DEV_RES_GPRS_RSSI 5 // int +#define EXT_DEV_RES_GPRS_PLMN 6 // int +#define EXT_DEV_RES_GPRS_ULMODULATION 7 // string +#define EXT_DEV_RES_GPRS_DLMODULATION 8 // string +#define EXT_DEV_RES_GPRS_ULFREQUENCY 9 // int +#define EXT_DEV_RES_GPRS_DLFREQUENCY 10 // int +#define EXT_DEV_RES_RX_BYTES 11 // uint64 +#define EXT_DEV_RES_TX_BYTES 12 // uint64 #define EXT_DEV_RES_NUM_INCOMING_RETRANSMISSIONS 13 // uint64 #define EXT_DEV_RES_NUM_OUTGOING_RETRANSMISSIONS 14 // uint64 -#define EXT_DEV_RES_UPTIME 15 // double +#define EXT_DEV_RES_UPTIME 15 // double typedef struct { const anjay_dm_object_def_t *def; avs_time_monotonic_t init_time; } extdev_repr_t; -static inline extdev_repr_t *get_extdev(const anjay_dm_object_def_t *const *obj_ptr) { +static inline extdev_repr_t * +get_extdev(const anjay_dm_object_def_t *const *obj_ptr) { assert(obj_ptr); return container_of(obj_ptr, extdev_repr_t, def); } @@ -58,7 +59,8 @@ static int dev_read(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; switch (rid) { case EXT_DEV_RES_OBU_ID: @@ -94,8 +96,9 @@ static int dev_read(anjay_t *anjay, return anjay_ret_i64( ctx, (int64_t) anjay_get_num_outgoing_retransmissions(anjay)); case EXT_DEV_RES_UPTIME: { - avs_time_duration_t diff = avs_time_monotonic_diff( - avs_time_monotonic_now(), get_extdev(obj_ptr)->init_time); + avs_time_duration_t diff = + avs_time_monotonic_diff(avs_time_monotonic_now(), + get_extdev(obj_ptr)->init_time); return anjay_ret_double(ctx, (double) diff.seconds + (double) diff.nanoseconds / 1e9); @@ -107,23 +110,23 @@ static int dev_read(anjay_t *anjay, static const anjay_dm_object_def_t EXT_DEV_INFO = { .oid = DEMO_OID_EXT_DEV_INFO, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - EXT_DEV_RES_OBU_ID, - EXT_DEV_RES_PLATE_NUMBER, - EXT_DEV_RES_IMEI, - EXT_DEV_RES_IMSI, - EXT_DEV_RES_ICCID, - EXT_DEV_RES_GPRS_RSSI, - EXT_DEV_RES_GPRS_PLMN, - EXT_DEV_RES_GPRS_ULMODULATION, - EXT_DEV_RES_GPRS_DLMODULATION, - EXT_DEV_RES_GPRS_ULFREQUENCY, - EXT_DEV_RES_GPRS_DLFREQUENCY, - EXT_DEV_RES_RX_BYTES, - EXT_DEV_RES_TX_BYTES, - EXT_DEV_RES_NUM_INCOMING_RETRANSMISSIONS, - EXT_DEV_RES_NUM_OUTGOING_RETRANSMISSIONS, - EXT_DEV_RES_UPTIME), + .supported_rids = + ANJAY_DM_SUPPORTED_RIDS(EXT_DEV_RES_OBU_ID, + EXT_DEV_RES_PLATE_NUMBER, + EXT_DEV_RES_IMEI, + EXT_DEV_RES_IMSI, + EXT_DEV_RES_ICCID, + EXT_DEV_RES_GPRS_RSSI, + EXT_DEV_RES_GPRS_PLMN, + EXT_DEV_RES_GPRS_ULMODULATION, + EXT_DEV_RES_GPRS_DLMODULATION, + EXT_DEV_RES_GPRS_ULFREQUENCY, + EXT_DEV_RES_GPRS_DLFREQUENCY, + EXT_DEV_RES_RX_BYTES, + EXT_DEV_RES_TX_BYTES, + EXT_DEV_RES_NUM_INCOMING_RETRANSMISSIONS, + EXT_DEV_RES_NUM_OUTGOING_RETRANSMISSIONS, + EXT_DEV_RES_UPTIME), .handlers = { .instance_it = anjay_dm_instance_it_SINGLE, .instance_present = anjay_dm_instance_present_SINGLE, @@ -133,7 +136,8 @@ static const anjay_dm_object_def_t EXT_DEV_INFO = { }; const anjay_dm_object_def_t **ext_dev_info_object_create(void) { - extdev_repr_t *repr = (extdev_repr_t *) avs_calloc(1, sizeof(extdev_repr_t)); + extdev_repr_t *repr = + (extdev_repr_t *) avs_calloc(1, sizeof(extdev_repr_t)); if (!repr) { return NULL; } diff --git a/demo/objects/geopoints.c b/demo/objects/geopoints.c index f6b306665..d6fec4e76 100644 --- a/demo/objects/geopoints.c +++ b/demo/objects/geopoints.c @@ -15,19 +15,19 @@ */ #include "../demo.h" -#include "../objects.h" #include "../demo_utils.h" +#include "../objects.h" +#include #include #include #include -#include -#define GEOPOINTS_LATITUDE 0 // double, degrees -#define GEOPOINTS_LONGITUDE 1 // double, degrees -#define GEOPOINTS_RADIUS 2 // double, meters +#define GEOPOINTS_LATITUDE 0 // double, degrees +#define GEOPOINTS_LONGITUDE 1 // double, degrees +#define GEOPOINTS_RADIUS 2 // double, meters #define GEOPOINTS_DESCRIPTION 3 // string -#define GEOPOINTS_INSIDE 4 // bool +#define GEOPOINTS_INSIDE 4 // bool typedef struct { anjay_iid_t iid; @@ -56,8 +56,7 @@ get_geopoints(const anjay_dm_object_def_t *const *obj_ptr) { return container_of(obj_ptr, geopoints_t, def); } -static geopoint_t *find_instance(const geopoints_t *repr, - anjay_iid_t iid) { +static geopoint_t *find_instance(const geopoints_t *repr, anjay_iid_t iid) { AVS_LIST(geopoint_t) it; AVS_LIST_FOREACH(it, repr->instances) { if (it->iid == iid) { @@ -74,7 +73,7 @@ static int geopoints_instance_it(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t *out, void **cookie) { - (void)anjay; + (void) anjay; AVS_LIST(geopoint_t) inst = (AVS_LIST(geopoint_t)) *cookie; @@ -89,10 +88,11 @@ static int geopoints_instance_it(anjay_t *anjay, return 0; } -static int geopoints_instance_present(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid) { - (void)anjay; +static int +geopoints_instance_present(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid) { + (void) anjay; return find_instance(get_geopoints(obj_ptr), iid) != NULL; } @@ -109,10 +109,11 @@ static anjay_iid_t get_new_iid(AVS_LIST(geopoint_t) instances) { return iid; } -static int geopoints_instance_create(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t *inout_iid, - anjay_ssid_t ssid) { +static int +geopoints_instance_create(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t *inout_iid, + anjay_ssid_t ssid) { (void) anjay; (void) ssid; geopoints_t *repr = get_geopoints(obj_ptr); @@ -143,10 +144,11 @@ static int geopoints_instance_create(anjay_t *anjay, return 0; } -static int geopoints_instance_remove(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid) { - (void)anjay; +static int +geopoints_instance_remove(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid) { + (void) anjay; geopoints_t *repr = get_geopoints(obj_ptr); AVS_LIST(geopoint_t) *it; @@ -231,8 +233,7 @@ static int geopoints_resource_write(anjay_t *anjay, inst->radius_m = value; inst->has_radius_m = true; return 0; - case GEOPOINTS_DESCRIPTION: - { + case GEOPOINTS_DESCRIPTION: { char buf[sizeof(inst->description)]; result = anjay_get_string(ctx, buf, sizeof(buf)); if (result) { @@ -295,10 +296,9 @@ geopoints_transaction_rollback(anjay_t *anjay, return 0; } -static int -geopoints_instance_reset(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid) { +static int geopoints_instance_reset(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid) { (void) anjay; geopoint_t *inst = find_instance(get_geopoints(obj_ptr), iid); AVS_ASSERT(inst, "could not find instance"); @@ -309,12 +309,11 @@ geopoints_instance_reset(anjay_t *anjay, static const anjay_dm_object_def_t GEOPOINTS = { .oid = DEMO_OID_GEOPOINTS, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - GEOPOINTS_LATITUDE, - GEOPOINTS_LONGITUDE, - GEOPOINTS_RADIUS, - GEOPOINTS_DESCRIPTION, - GEOPOINTS_INSIDE), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(GEOPOINTS_LATITUDE, + GEOPOINTS_LONGITUDE, + GEOPOINTS_RADIUS, + GEOPOINTS_DESCRIPTION, + GEOPOINTS_INSIDE), .handlers = { .instance_it = geopoints_instance_it, .instance_present = geopoints_instance_present, @@ -331,8 +330,7 @@ static const anjay_dm_object_def_t GEOPOINTS = { } }; -const anjay_dm_object_def_t ** -geopoints_object_create(anjay_demo_t *demo) { +const anjay_dm_object_def_t **geopoints_object_create(anjay_demo_t *demo) { geopoints_t *repr = (geopoints_t *) avs_calloc(1, sizeof(geopoints_t)); if (!repr) { return NULL; @@ -369,13 +367,13 @@ void geopoints_notify_time_dependent(anjay_t *anjay, AVS_LIST(geopoint_t) point; AVS_LIST_FOREACH(point, geopoints->instances) { - bool inside = (geo_distance_m(latitude, longitude, - point->latitude, - point->longitude) < point->radius_m); + bool inside = (geo_distance_m(latitude, longitude, point->latitude, + point->longitude) + < point->radius_m); if (inside != point->inside) { point->inside = inside; - anjay_notify_changed(anjay, - (*def)->oid, point->iid, GEOPOINTS_INSIDE); + anjay_notify_changed(anjay, (*def)->oid, point->iid, + GEOPOINTS_INSIDE); } } } diff --git a/demo/objects/ip_ping.c b/demo/objects/ip_ping.c index 8e186de61..1a3adabab 100644 --- a/demo/objects/ip_ping.c +++ b/demo/objects/ip_ping.c @@ -15,7 +15,7 @@ */ #if !defined(_POSIX_C_SOURCE) && !defined(__APPLE__) -#define _POSIX_C_SOURCE 200809L +# define _POSIX_C_SOURCE 200809L #endif #include "../demo.h" @@ -29,18 +29,18 @@ #include -#define IP_PING_HOSTNAME 0 -#define IP_PING_REPETITIONS 1 -#define IP_PING_TIMEOUT_MS 2 -#define IP_PING_BLOCK_SIZE 3 -#define IP_PING_DSCP 4 -#define IP_PING_RUN 5 -#define IP_PING_STATE 6 -#define IP_PING_SUCCESS_COUNT 7 -#define IP_PING_ERROR_COUNT 8 -#define IP_PING_AVG_TIME_MS 9 -#define IP_PING_MIN_TIME_MS 10 -#define IP_PING_MAX_TIME_MS 11 +#define IP_PING_HOSTNAME 0 +#define IP_PING_REPETITIONS 1 +#define IP_PING_TIMEOUT_MS 2 +#define IP_PING_BLOCK_SIZE 3 +#define IP_PING_DSCP 4 +#define IP_PING_RUN 5 +#define IP_PING_STATE 6 +#define IP_PING_SUCCESS_COUNT 7 +#define IP_PING_ERROR_COUNT 8 +#define IP_PING_AVG_TIME_MS 9 +#define IP_PING_MIN_TIME_MS 10 +#define IP_PING_MAX_TIME_MS 11 #define IP_PING_TIME_STDEV_US 12 typedef enum { @@ -105,7 +105,8 @@ static int ip_ping_resource_read(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; ip_ping_t *ping = get_ip_ping(obj_ptr); switch (rid) { @@ -151,20 +152,20 @@ static int ip_ping_reset_diagnostic_state(anjay_t *anjay, ip_ping_t *ipping) { return 0; } -#define DECLARE_GET_NUM(Type, Base) \ -static int get_##Type (anjay_input_ctx_t *ctx, Type##_t *out, \ - Type##_t min, Type##_t max) {\ - int##Base##_t base; \ - int result = anjay_get_i##Base (ctx, &base); \ - if (result) { \ - return result; \ - } \ - if (base < (int##Base##_t) min || base > (int##Base##_t) max) { \ - return ANJAY_ERR_BAD_REQUEST; \ - } \ - *out = (Type##_t) base; \ - return 0; \ -} +#define DECLARE_GET_NUM(Type, Base) \ + static int get_##Type(anjay_input_ctx_t *ctx, Type##_t *out, Type##_t min, \ + Type##_t max) { \ + int##Base##_t base; \ + int result = anjay_get_i##Base(ctx, &base); \ + if (result) { \ + return result; \ + } \ + if (base < (int##Base##_t) min || base > (int##Base##_t) max) { \ + return ANJAY_ERR_BAD_REQUEST; \ + } \ + *out = (Type##_t) base; \ + return 0; \ + } DECLARE_GET_NUM(uint8, 32) DECLARE_GET_NUM(uint16, 32) @@ -175,15 +176,17 @@ static int ip_ping_resource_write(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_input_ctx_t *ctx) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; ip_ping_t *ping = get_ip_ping(obj_ptr); int result; switch (rid) { case IP_PING_HOSTNAME: (void) ((result = ip_ping_reset_diagnostic_state(anjay, ping)) - || (result = anjay_get_string(ctx, ping->configuration.hostname, - sizeof(ping->configuration.hostname)))); + || (result = anjay_get_string( + ctx, ping->configuration.hostname, + sizeof(ping->configuration.hostname)))); return result; case IP_PING_REPETITIONS: (void) ((result = ip_ping_reset_diagnostic_state(anjay, ping)) @@ -192,13 +195,13 @@ static int ip_ping_resource_write(anjay_t *anjay, return result; case IP_PING_TIMEOUT_MS: (void) ((result = ip_ping_reset_diagnostic_state(anjay, ping)) - || (result = get_uint32(ctx, &ping->configuration.ms_timeout, - 1, UINT32_MAX))); + || (result = get_uint32(ctx, &ping->configuration.ms_timeout, 1, + UINT32_MAX))); return result; case IP_PING_BLOCK_SIZE: (void) ((result = ip_ping_reset_diagnostic_state(anjay, ping)) - || (result = get_uint16(ctx, &ping->configuration.block_size, - 1, UINT16_MAX))); + || (result = get_uint16(ctx, &ping->configuration.block_size, 1, + UINT16_MAX))); return result; case IP_PING_DSCP: (void) ((result = ip_ping_reset_diagnostic_state(anjay, ping)) @@ -210,7 +213,9 @@ static int ip_ping_resource_write(anjay_t *anjay, } static void update_response_times(ip_ping_t *ping, - unsigned min, unsigned max, unsigned avg, + unsigned min, + unsigned max, + unsigned avg, unsigned mdev_us) { ping->stats.min_response_time = min; anjay_notify_changed(ping->command_state.anjay, ping->def->oid, 0, @@ -262,8 +267,7 @@ static void ip_ping_handler(short revents, void *ping_) { case IP_PING_HANDLER_SKIP2: default: return; - case IP_PING_HANDLER_COUNTS: - { + case IP_PING_HANDLER_COUNTS: { unsigned total, success; if (sscanf(line, "%u %*s %*s %u", &total, &success) != 2) { goto finish; @@ -281,8 +285,7 @@ static void ip_ping_handler(short revents, void *ping_) { } return; } - case IP_PING_HANDLER_RTT: - { + case IP_PING_HANDLER_RTT: { const char *ptr = strstr(line, "="); if (!ptr) { demo_log(ERROR, "Invalid output format of ping."); @@ -307,14 +310,12 @@ static void ip_ping_handler(short revents, void *ping_) { if (ping->stats.state == IP_PING_STATE_IN_PROGRESS) { ping->stats.state = IP_PING_STATE_ERROR_INTERNAL; } - anjay_notify_changed(ping->command_state.anjay, - ping->def->oid, 0, IP_PING_STATE); + anjay_notify_changed(ping->command_state.anjay, ping->def->oid, 0, + IP_PING_STATE); } -static ip_ping_state_t start_ip_ping(anjay_t *anjay, - ip_ping_t *ping) { - if (!ping->configuration.repetitions - || !ping->configuration.ms_timeout +static ip_ping_state_t start_ip_ping(anjay_t *anjay, ip_ping_t *ping) { + if (!ping->configuration.repetitions || !ping->configuration.ms_timeout || !ping->configuration.block_size || !ping->configuration.hostname[0]) { return IP_PING_STATE_ERROR_OTHER; @@ -332,7 +333,7 @@ static ip_ping_state_t start_ip_ping(anjay_t *anjay, ping->configuration.dscp << 2, timeout_s, ping->configuration.block_size, ping->configuration.hostname) - < 0) { + < 0) { demo_log(ERROR, "Cannot prepare ping command"); return IP_PING_STATE_ERROR_INTERNAL; } @@ -399,20 +400,19 @@ ip_ping_transaction_rollback(anjay_t *anjay, static const anjay_dm_object_def_t IP_PING = { .oid = DEMO_OID_IP_PING, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - IP_PING_HOSTNAME, - IP_PING_REPETITIONS, - IP_PING_TIMEOUT_MS, - IP_PING_BLOCK_SIZE, - IP_PING_DSCP, - IP_PING_RUN, - IP_PING_STATE, - IP_PING_SUCCESS_COUNT, - IP_PING_ERROR_COUNT, - IP_PING_AVG_TIME_MS, - IP_PING_MIN_TIME_MS, - IP_PING_MAX_TIME_MS, - IP_PING_TIME_STDEV_US), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(IP_PING_HOSTNAME, + IP_PING_REPETITIONS, + IP_PING_TIMEOUT_MS, + IP_PING_BLOCK_SIZE, + IP_PING_DSCP, + IP_PING_RUN, + IP_PING_STATE, + IP_PING_SUCCESS_COUNT, + IP_PING_ERROR_COUNT, + IP_PING_AVG_TIME_MS, + IP_PING_MIN_TIME_MS, + IP_PING_MAX_TIME_MS, + IP_PING_TIME_STDEV_US), .handlers = { .instance_it = anjay_dm_instance_it_SINGLE, .instance_present = anjay_dm_instance_present_SINGLE, diff --git a/demo/objects/location.c b/demo/objects/location.c index 7ec28e207..8bfd656f9 100644 --- a/demo/objects/location.c +++ b/demo/objects/location.c @@ -15,11 +15,11 @@ */ #if !defined(_POSIX_C_SOURCE) && !defined(__APPLE__) -#define _POSIX_C_SOURCE 200809L +# define _POSIX_C_SOURCE 200809L #endif -#include "../objects.h" #include "../demo_utils.h" +#include "../objects.h" #include #include @@ -28,13 +28,13 @@ #include -#define LOCATION_LATITUDE 0 -#define LOCATION_LONGITUDE 1 -#define LOCATION_ALTITUDE 2 -#define LOCATION_RADIUS 3 -#define LOCATION_VELOCITY 4 -#define LOCATION_TIMESTAMP 5 -#define LOCATION_SPEED 6 +#define LOCATION_LATITUDE 0 +#define LOCATION_LONGITUDE 1 +#define LOCATION_ALTITUDE 2 +#define LOCATION_RADIUS 3 +#define LOCATION_VELOCITY 4 +#define LOCATION_TIMESTAMP 5 +#define LOCATION_SPEED 6 typedef struct { double value_mps; @@ -87,7 +87,9 @@ static int location_resource_read(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void) anjay; (void) obj_ptr; (void) iid; + (void) anjay; + (void) obj_ptr; + (void) iid; location_t *location = get_location(obj_ptr); switch (rid) { @@ -149,15 +151,11 @@ static void get_meters_per_degree(double *out_m_per_deg_lat, // The formulas come from // https://en.wikipedia.org/wiki/Geographic_coordinate_system#Expressing_latitude_and_longitude_as_linear_units // (retrieved 2016-01-12) - *out_m_per_deg_lat = - 111132.92 - - 559.82 * cos(2.0 * lat_rad) - + 1.175 * cos(4.0 * lat_rad) - - 0.0023 * cos(6.0 * lat_rad); - *out_m_per_deg_lon = - 111412.84 * cos(lat_rad) - - 93.5 * cos(3.0 * lat_rad) - - 0.118 * cos(5.0 * lat_rad); + *out_m_per_deg_lat = 111132.92 - 559.82 * cos(2.0 * lat_rad) + + 1.175 * cos(4.0 * lat_rad) + - 0.0023 * cos(6.0 * lat_rad); + *out_m_per_deg_lon = 111412.84 * cos(lat_rad) - 93.5 * cos(3.0 * lat_rad) + - 0.118 * cos(5.0 * lat_rad); } static double rand_double(unsigned *seed, double min, double max) { @@ -165,8 +163,10 @@ static double rand_double(unsigned *seed, double min, double max) { } static void calculate_velocity(velocity_t *out, - double lat1, double lon1, - double lat2, double lon2, + double lat1, + double lon1, + double lat2, + double lon2, double time_change_s) { out->value_mps = geo_distance_m(lat1, lon1, lat2, lon2) / time_change_s; @@ -187,10 +187,10 @@ static int update_location_random(location_t *location) { get_meters_per_degree(&m_per_deg_lat, &m_per_deg_lon, location->latitude); // random movement of at most 1 m in each direction - double lat_change = rand_double(&location->rand_seed, - -1.0/m_per_deg_lat, 1.0/m_per_deg_lat); - double lon_change = rand_double(&location->rand_seed, - -1.0/m_per_deg_lon, 1.0/m_per_deg_lon); + double lat_change = rand_double(&location->rand_seed, -1.0 / m_per_deg_lat, + 1.0 / m_per_deg_lat); + double lon_change = rand_double(&location->rand_seed, -1.0 / m_per_deg_lon, + 1.0 / m_per_deg_lon); double old_lat = location->latitude; double old_lon = location->longitude; @@ -203,9 +203,8 @@ static int update_location_random(location_t *location) { return 1; } -static int try_parse_location_line(location_t *location, - char *line, - size_t line_length) { +static int +try_parse_location_line(location_t *location, char *line, size_t line_length) { if (line_length > 1 && line[line_length - 1] == '\n') { line[--line_length] = '\0'; } @@ -213,20 +212,18 @@ static int try_parse_location_line(location_t *location, double latitude = 0.0, longitude = 0.0; double vel_mps = 0.0, vel_bearing_deg_cw_n = 0.0; int chars_read = 0; - int scanf_result = sscanf(line, " %lf , %lf %n", - &latitude, &longitude, &chars_read); - if (scanf_result < 2 - || !latitude_valid(latitude) || !longitude_valid(longitude)) { + int scanf_result = + sscanf(line, " %lf , %lf %n", &latitude, &longitude, &chars_read); + if (scanf_result < 2 || !latitude_valid(latitude) + || !longitude_valid(longitude)) { goto invalid; } if ((size_t) chars_read < line_length) { int more_chars_read; - scanf_result = sscanf(line + chars_read, ", %lf , %lf %n", - &vel_mps, &vel_bearing_deg_cw_n, - &more_chars_read); - if (scanf_result < 2 - || !velocity_mps_valid(vel_mps) + scanf_result = sscanf(line + chars_read, ", %lf , %lf %n", &vel_mps, + &vel_bearing_deg_cw_n, &more_chars_read); + if (scanf_result < 2 || !velocity_mps_valid(vel_mps) || !velocity_bearing_deg_cw_n_valid(vel_bearing_deg_cw_n) || (size_t) (chars_read + more_chars_read) != line_length) { goto invalid; @@ -234,9 +231,8 @@ static int try_parse_location_line(location_t *location, location->velocity.value_mps = vel_mps; location->velocity.bearing_deg_cw_n = vel_bearing_deg_cw_n; } else { - calculate_velocity(&location->velocity, - location->latitude, location->longitude, - latitude, longitude, + calculate_velocity(&location->velocity, location->latitude, + location->longitude, latitude, longitude, (double) location->csv_frequency); } location->latitude = latitude; @@ -287,13 +283,12 @@ static bool update_location(location_t *location) { static const anjay_dm_object_def_t LOCATION = { .oid = DEMO_OID_LOCATION, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - LOCATION_LATITUDE, - LOCATION_LONGITUDE, - LOCATION_ALTITUDE, - LOCATION_RADIUS, - LOCATION_VELOCITY, - LOCATION_TIMESTAMP), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(LOCATION_LATITUDE, + LOCATION_LONGITUDE, + LOCATION_ALTITUDE, + LOCATION_RADIUS, + LOCATION_VELOCITY, + LOCATION_TIMESTAMP), .handlers = { .instance_it = anjay_dm_instance_it_SINGLE, .instance_present = anjay_dm_instance_present_SINGLE, @@ -344,14 +339,16 @@ void location_notify_time_dependent(anjay_t *anjay, } void location_get(const anjay_dm_object_def_t **def, - double *out_latitude, double *out_longitude) { + double *out_latitude, + double *out_longitude) { location_t *repr = get_location(def); *out_latitude = repr->latitude; *out_longitude = repr->longitude; } int location_open_csv(const anjay_dm_object_def_t **def, - const char *file_name, time_t frequency_s) { + const char *file_name, + time_t frequency_s) { location_t *location = get_location(def); if (frequency_s <= 0) { demo_log(ERROR, "Invalid CSV time frequency: %ld", (long) frequency_s); @@ -367,6 +364,7 @@ int location_open_csv(const anjay_dm_object_def_t **def, } location->csv = csv; location->csv_frequency = frequency_s; - demo_log(INFO, "CSV loaded: %s (frequency_s = %ld)", file_name, frequency_s); + demo_log(INFO, "CSV loaded: %s (frequency_s = %ld)", file_name, + frequency_s); return 0; } diff --git a/demo/objects/portfolio.c b/demo/objects/portfolio.c index 7adbf1b9e..690bacabf 100644 --- a/demo/objects/portfolio.c +++ b/demo/objects/portfolio.c @@ -44,7 +44,8 @@ * 2: Host Device Model * 3: Host Device Software Version, * - * This Resource contains data that the GetAuthData executable Resource can work with. + * This Resource contains data that the GetAuthData executable Resource can + * work with. */ #define RID_IDENTITY 0 @@ -68,7 +69,8 @@ /** * AuthStatus: R, Single, Optional * type: integer, range: [0-2], unit: N/A - * This Resource contains the state related to the process triggered by GetAuthData request. + * This Resource contains the state related to the process triggered by + * GetAuthData request. * 0 : IDLE_STATE : AuthData doesn’t contain any valid data * 1 : DATA_AVAIL_STATE : AuthData contains a valid data * 2 : ERROR_STATE : an error occurred @@ -107,9 +109,8 @@ get_obj(const anjay_dm_object_def_t *const *obj_ptr) { return AVS_CONTAINER_OF(obj_ptr, portfolio_t, def); } -static portfolio_instance_t * -find_instance(const portfolio_t *obj, - anjay_iid_t iid) { +static portfolio_instance_t *find_instance(const portfolio_t *obj, + anjay_iid_t iid) { AVS_LIST(portfolio_instance_t) it; AVS_LIST_FOREACH(it, obj->instances) { if (it->iid == iid) { @@ -125,7 +126,7 @@ find_instance(const portfolio_t *obj, static int instance_present(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid) { - (void)anjay; + (void) anjay; return find_instance(get_obj(obj_ptr), iid) != NULL; } @@ -133,9 +134,10 @@ static int instance_it(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t *out, void **cookie) { - (void)anjay; + (void) anjay; - AVS_LIST(portfolio_instance_t) curr = (AVS_LIST(portfolio_instance_t))*cookie; + AVS_LIST(portfolio_instance_t) curr = + (AVS_LIST(portfolio_instance_t)) *cookie; if (!curr) { curr = get_obj(obj_ptr)->instances; } else { @@ -164,11 +166,13 @@ static int instance_create(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t *inout_iid, anjay_ssid_t ssid) { - (void) anjay; (void) ssid; + (void) anjay; + (void) ssid; portfolio_t *obj = get_obj(obj_ptr); assert(obj); - AVS_LIST(portfolio_instance_t) created = AVS_LIST_NEW_ELEMENT(portfolio_instance_t); + AVS_LIST(portfolio_instance_t) created = + AVS_LIST_NEW_ELEMENT(portfolio_instance_t); if (!created) { return ANJAY_ERR_INTERNAL; } @@ -198,7 +202,7 @@ static int instance_create(anjay_t *anjay, static int instance_remove(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid) { - (void)anjay; + (void) anjay; portfolio_t *obj = get_obj(obj_ptr); assert(obj); @@ -234,7 +238,7 @@ static int resource_read(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void)anjay; + (void) anjay; portfolio_t *obj = get_obj(obj_ptr); assert(obj); @@ -242,28 +246,28 @@ static int resource_read(anjay_t *anjay, assert(inst); switch (rid) { - case RID_IDENTITY: - { - anjay_output_ctx_t *array = anjay_ret_array_start(ctx); - if (!array) { - return ANJAY_ERR_INTERNAL; + case RID_IDENTITY: { + anjay_output_ctx_t *array = anjay_ret_array_start(ctx); + if (!array) { + return ANJAY_ERR_INTERNAL; + } + int result = 0; + + AVS_STATIC_ASSERT(_MAX_IDENTITY_TYPE <= UINT16_MAX, + identity_type_too_big); + for (int32_t i = 0; i < _MAX_IDENTITY_TYPE; ++i) { + if (!inst->has_identity[i]) { + continue; } - int result = 0; - - AVS_STATIC_ASSERT(_MAX_IDENTITY_TYPE <= UINT16_MAX, - identity_type_too_big); - for (int32_t i = 0; i < _MAX_IDENTITY_TYPE; ++i) { - if (!inst->has_identity[i]) { - continue; - } - - if ((result = anjay_ret_array_index(array, (anjay_riid_t) i)) - || (result = anjay_ret_string(array, inst->identity_value[i]))) { - return result; - } + + if ((result = anjay_ret_array_index(array, (anjay_riid_t) i)) + || (result = anjay_ret_string(array, + inst->identity_value[i]))) { + return result; } - return anjay_ret_array_finish(array); } + return anjay_ret_array_finish(array); + } default: return ANJAY_ERR_METHOD_NOT_ALLOWED; } @@ -274,7 +278,7 @@ static int resource_write(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_input_ctx_t *ctx) { - (void)anjay; + (void) anjay; portfolio_t *obj = get_obj(obj_ptr); assert(obj); @@ -282,32 +286,31 @@ static int resource_write(anjay_t *anjay, assert(inst); switch (rid) { - case RID_IDENTITY: - { - anjay_input_ctx_t *array = anjay_get_array(ctx); - if (!array) { - return ANJAY_ERR_INTERNAL; - } - - anjay_riid_t riid; - int result = 0; - char value[MAX_IDENTITY_VALUE_SIZE]; - memset(inst->has_identity, 0, sizeof(inst->has_identity)); - while (!result && !(result = anjay_get_array_index(array, &riid))) { - result = anjay_get_string(array, value, sizeof(value)); + case RID_IDENTITY: { + anjay_input_ctx_t *array = anjay_get_array(ctx); + if (!array) { + return ANJAY_ERR_INTERNAL; + } - if (riid >= _MAX_IDENTITY_TYPE) { - return ANJAY_ERR_BAD_REQUEST; - } + anjay_riid_t riid; + int result = 0; + char value[MAX_IDENTITY_VALUE_SIZE]; + memset(inst->has_identity, 0, sizeof(inst->has_identity)); + while (!result && !(result = anjay_get_array_index(array, &riid))) { + result = anjay_get_string(array, value, sizeof(value)); - inst->has_identity[riid] = true; - strcpy(inst->identity_value[riid], value); - } - if (result && result != ANJAY_GET_INDEX_END) { + if (riid >= _MAX_IDENTITY_TYPE) { return ANJAY_ERR_BAD_REQUEST; } - return 0; + + inst->has_identity[riid] = true; + strcpy(inst->identity_value[riid], value); } + if (result && result != ANJAY_GET_INDEX_END) { + return ANJAY_ERR_BAD_REQUEST; + } + return 0; + } default: return ANJAY_ERR_METHOD_NOT_ALLOWED; @@ -326,8 +329,7 @@ static int resource_dim(anjay_t *anjay, assert(inst); switch (rid) { - case RID_IDENTITY: - { + case RID_IDENTITY: { int dim = 0; for (int32_t i = 0; i < _MAX_IDENTITY_TYPE; ++i) { dim += !!inst->has_identity[i]; @@ -341,7 +343,7 @@ static int resource_dim(anjay_t *anjay, static int transaction_begin(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr) { - (void)anjay; + (void) anjay; portfolio_t *obj = get_obj(obj_ptr); assert(obj); assert(!obj->backup); @@ -354,7 +356,7 @@ static int transaction_begin(anjay_t *anjay, static int transaction_commit(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr) { - (void)anjay; + (void) anjay; portfolio_t *obj = get_obj(obj_ptr); assert(obj); AVS_LIST_CLEAR(&obj->backup); @@ -363,7 +365,7 @@ static int transaction_commit(anjay_t *anjay, static int transaction_rollback(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr) { - (void)anjay; + (void) anjay; portfolio_t *obj = get_obj(obj_ptr); assert(obj); AVS_LIST_CLEAR(&obj->instances); @@ -374,9 +376,7 @@ static int transaction_rollback(anjay_t *anjay, static const anjay_dm_object_def_t OBJ_DEF = { .oid = 16, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - RID_IDENTITY, - ), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(RID_IDENTITY, ), .handlers = { .instance_it = instance_it, .instance_present = instance_present, diff --git a/demo/objects/test.c b/demo/objects/test.c index d105e805a..05ff7e569 100644 --- a/demo/objects/test.c +++ b/demo/objects/test.c @@ -18,30 +18,30 @@ #include "../demo_utils.h" #include "../objects.h" -#include #include +#include #include #include -#define TEST_RES_TIMESTAMP 0 -#define TEST_RES_COUNTER 1 -#define TEST_RES_INCREMENT_COUNTER 2 -#define TEST_RES_INT_ARRAY 3 +#define TEST_RES_TIMESTAMP 0 +#define TEST_RES_COUNTER 1 +#define TEST_RES_INCREMENT_COUNTER 2 +#define TEST_RES_INT_ARRAY 3 #define TEST_RES_LAST_EXEC_ARGS_ARRAY 4 -#define TEST_RES_BYTES 5 -#define TEST_RES_BYTES_SIZE 6 -#define TEST_RES_BYTES_BURST 7 -#define TEST_RES_EMPTY 8 -#define TEST_RES_INIT_INT_ARRAY 9 -#define TEST_RES_RAW_BYTES 10 -#define TEST_RES_OPAQUE_ARRAY 11 -#define TEST_RES_INT 12 -#define TEST_RES_BOOL 13 -#define TEST_RES_FLOAT 14 -#define TEST_RES_STRING 15 -#define TEST_RES_OBJLNK 16 -#define TEST_RES_BYTES_ZERO_BEGIN 17 +#define TEST_RES_BYTES 5 +#define TEST_RES_BYTES_SIZE 6 +#define TEST_RES_BYTES_BURST 7 +#define TEST_RES_EMPTY 8 +#define TEST_RES_INIT_INT_ARRAY 9 +#define TEST_RES_RAW_BYTES 10 +#define TEST_RES_OPAQUE_ARRAY 11 +#define TEST_RES_INT 12 +#define TEST_RES_BOOL 13 +#define TEST_RES_FLOAT 14 +#define TEST_RES_STRING 15 +#define TEST_RES_OBJLNK 16 +#define TEST_RES_BYTES_ZERO_BEGIN 17 typedef struct test_array_entry_struct { anjay_riid_t index; @@ -101,7 +101,7 @@ static test_instance_t *find_instance(const test_repr_t *repr, static int test_instance_present(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid) { - (void)anjay; + (void) anjay; return find_instance(get_test(obj_ptr), iid) != NULL; } @@ -109,10 +109,10 @@ static int test_instance_it(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t *out, void **cookie) { - (void)anjay; + (void) anjay; - test_repr_t* test = get_test(obj_ptr); - AVS_LIST(test_instance_t) curr = (AVS_LIST(test_instance_t))*cookie; + test_repr_t *test = get_test(obj_ptr); + AVS_LIST(test_instance_t) curr = (AVS_LIST(test_instance_t)) *cookie; if (!curr) { curr = test->instances; @@ -142,8 +142,9 @@ static int test_instance_create(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t *inout_iid, anjay_ssid_t ssid) { - (void) anjay; (void) ssid; - test_repr_t* test = get_test(obj_ptr); + (void) anjay; + (void) ssid; + test_repr_t *test = get_test(obj_ptr); AVS_LIST(test_instance_t) created = AVS_LIST_NEW_ELEMENT(test_instance_t); if (!created) { @@ -187,8 +188,8 @@ static void release_instance(test_instance_t *inst) { static int test_instance_remove(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid) { - (void)anjay; - test_repr_t* test = get_test(obj_ptr); + (void) anjay; + test_repr_t *test = get_test(obj_ptr); AVS_LIST(test_instance_t) *it; AVS_LIST_FOREACH_PTR(it, &test->instances) { @@ -219,9 +220,9 @@ static int test_resource_read(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void)anjay; + (void) anjay; - test_repr_t* test = get_test(obj_ptr); + test_repr_t *test = get_test(obj_ptr); test_instance_t *inst = find_instance(test, iid); assert(inst); @@ -240,7 +241,7 @@ static int test_resource_read(anjay_t *anjay, test_array_entry_t *it; AVS_LIST_FOREACH(it, inst->array) { if (anjay_ret_array_index(array, it->index) - || anjay_ret_i32(array, it->value)) { + || anjay_ret_i32(array, it->value)) { return ANJAY_ERR_INTERNAL; } } @@ -256,8 +257,9 @@ static int test_resource_read(anjay_t *anjay, if (inst->last_exec_args) { test_exec_arg_t *it; AVS_LIST_FOREACH(it, inst->last_exec_args) { - if (anjay_ret_array_index(array, (anjay_riid_t)it->number) - || anjay_ret_string(array, it->value ? it->value : "")) { + if (anjay_ret_array_index(array, (anjay_riid_t) it->number) + || anjay_ret_string(array, + it->value ? it->value : "")) { return ANJAY_ERR_INTERNAL; } } @@ -281,9 +283,8 @@ static int test_resource_read(anjay_t *anjay, return -1; } int32_t counter = 0; - for (int32_t offset = 0; - offset < inst->bytes_size; - offset += inst->bytes_burst) { + for (int32_t offset = 0; offset < inst->bytes_size; + offset += inst->bytes_burst) { int32_t bytes_to_write = inst->bytes_size - offset; if (bytes_to_write > inst->bytes_burst) { bytes_to_write = inst->bytes_burst; @@ -331,7 +332,7 @@ static int test_resource_read(anjay_t *anjay, AVS_LIST_FOREACH(it, inst->array) { const uint32_t value = htonl((uint32_t) it->value); if (anjay_ret_array_index(array, it->index) - || anjay_ret_bytes(array, &value, sizeof(value))) { + || anjay_ret_bytes(array, &value, sizeof(value))) { return ANJAY_ERR_INTERNAL; } } @@ -359,8 +360,9 @@ static int test_resource_read(anjay_t *anjay, } } -static int test_resource_write_to_array(AVS_LIST(test_array_entry_t) *inst_array, - anjay_input_ctx_t *input_array) { +static int +test_resource_write_to_array(AVS_LIST(test_array_entry_t) *inst_array, + anjay_input_ctx_t *input_array) { assert(inst_array); assert(input_array); int result; @@ -385,7 +387,7 @@ static int test_resource_write_to_array(AVS_LIST(test_array_entry_t) *inst_array if (!value_updated) { AVS_LIST(test_array_entry_t) list_entry = - AVS_LIST_NEW_ELEMENT(test_array_entry_t); + AVS_LIST_NEW_ELEMENT(test_array_entry_t); if (!list_entry) { demo_log(ERROR, "out of memory"); return ANJAY_ERR_INTERNAL; @@ -406,9 +408,9 @@ static int test_resource_write(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_input_ctx_t *ctx) { - (void)anjay; + (void) anjay; - test_repr_t* test = get_test(obj_ptr); + test_repr_t *test = get_test(obj_ptr); test_instance_t *inst = find_instance(test, iid); assert(inst); @@ -487,7 +489,7 @@ static int read_exec_arg_value(anjay_execute_ctx_t *arg_ctx, while (true) { size_t new_value_size = bytes_read + VALUE_CHUNK_SIZE; - char *new_string = (char*)avs_realloc(*out_string, new_value_size); + char *new_string = (char *) avs_realloc(*out_string, new_value_size); if (!new_string) { demo_log(ERROR, "out of memory"); result = ANJAY_ERR_INTERNAL; @@ -497,18 +499,18 @@ static int read_exec_arg_value(anjay_execute_ctx_t *arg_ctx, *out_string = new_string; result = anjay_execute_get_arg_value(arg_ctx, new_string + bytes_read, - (ssize_t)VALUE_CHUNK_SIZE); + (ssize_t) VALUE_CHUNK_SIZE); if (result < 0) { - demo_log(ERROR, "could not read arg value: %d", (int)result); + demo_log(ERROR, "could not read arg value: %d", (int) result); goto fail; - } else if ((size_t)result != VALUE_CHUNK_SIZE - 1) { + } else if ((size_t) result != VALUE_CHUNK_SIZE - 1) { // nothing more to read, we're done break; } // incomplete read; bigger buffer required - bytes_read += (size_t)result; + bytes_read += (size_t) result; } return 0; @@ -516,7 +518,7 @@ static int read_exec_arg_value(anjay_execute_ctx_t *arg_ctx, fail: avs_free(*out_string); *out_string = NULL; - return (int)result; + return (int) result; } static int read_exec_arg(anjay_execute_ctx_t *arg_ctx, @@ -579,28 +581,28 @@ static int init_int_array_read_element(test_array_entry_t *out_entry, int arg_number; bool has_value; - ssize_t result = anjay_execute_get_next_arg(arg_ctx, &arg_number, &has_value); + ssize_t result = + anjay_execute_get_next_arg(arg_ctx, &arg_number, &has_value); if (result) { - return (int)result; + return (int) result; } char value_buf[16]; result = anjay_execute_get_arg_value(arg_ctx, value_buf, sizeof(value_buf)); if (result < 0) { - return (int)result; + return (int) result; } long value; - if (demo_parse_long(value_buf, &value) - || value < INT32_MIN + if (demo_parse_long(value_buf, &value) || value < INT32_MIN || value > INT32_MAX) { demo_log(WARNING, "invalid resource %d value", arg_number); return ANJAY_ERR_BAD_REQUEST; } assert(0 <= arg_number && arg_number <= UINT16_MAX); - out_entry->index = (anjay_riid_t)arg_number; - out_entry->value = (int32_t)value; + out_entry->index = (anjay_riid_t) arg_number; + out_entry->value = (int32_t) value; return 0; } @@ -642,9 +644,9 @@ static int test_resource_execute(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_execute_ctx_t *arg_ctx) { - (void)arg_ctx; + (void) arg_ctx; - test_repr_t* test = get_test(obj_ptr); + test_repr_t *test = get_test(obj_ptr); test_instance_t *inst = find_instance(test, iid); assert(inst); @@ -652,34 +654,30 @@ static int test_resource_execute(anjay_t *anjay, case TEST_RES_TIMESTAMP: case TEST_RES_COUNTER: return ANJAY_ERR_METHOD_NOT_ALLOWED; - case TEST_RES_INCREMENT_COUNTER: - { - int result = read_exec_args(arg_ctx, &inst->last_exec_args); - if (result) { - demo_log(ERROR, "could not save Execute arguments"); - return result; - } - - ++inst->execute_counter; - anjay_notify_changed(anjay, (*obj_ptr)->oid, iid, - TEST_RES_COUNTER); - anjay_notify_changed(anjay, (*obj_ptr)->oid, iid, - TEST_RES_LAST_EXEC_ARGS_ARRAY); - return 0; + case TEST_RES_INCREMENT_COUNTER: { + int result = read_exec_args(arg_ctx, &inst->last_exec_args); + if (result) { + demo_log(ERROR, "could not save Execute arguments"); + return result; } - case TEST_RES_INIT_INT_ARRAY: - { - int result = init_int_array(&inst->array, arg_ctx); - if (result) { - return result; - } - anjay_notify_changed(anjay, (*obj_ptr)->oid, iid, - TEST_RES_INT_ARRAY); - anjay_notify_changed(anjay, (*obj_ptr)->oid, iid, - TEST_RES_OPAQUE_ARRAY); - return 0; + ++inst->execute_counter; + anjay_notify_changed(anjay, (*obj_ptr)->oid, iid, TEST_RES_COUNTER); + anjay_notify_changed(anjay, (*obj_ptr)->oid, iid, + TEST_RES_LAST_EXEC_ARGS_ARRAY); + return 0; + } + case TEST_RES_INIT_INT_ARRAY: { + int result = init_int_array(&inst->array, arg_ctx); + if (result) { + return result; } + + anjay_notify_changed(anjay, (*obj_ptr)->oid, iid, TEST_RES_INT_ARRAY); + anjay_notify_changed(anjay, (*obj_ptr)->oid, iid, + TEST_RES_OPAQUE_ARRAY); + return 0; + } default: return ANJAY_ERR_NOT_FOUND; } @@ -707,25 +705,24 @@ static int test_resource_dim(anjay_t *anjay, const anjay_dm_object_def_t TEST_OBJECT = { .oid = DEMO_OID_TEST, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - TEST_RES_TIMESTAMP, - TEST_RES_COUNTER, - TEST_RES_INCREMENT_COUNTER, - TEST_RES_INT_ARRAY, - TEST_RES_LAST_EXEC_ARGS_ARRAY, - TEST_RES_BYTES, - TEST_RES_BYTES_SIZE, - TEST_RES_BYTES_BURST, - TEST_RES_EMPTY, - TEST_RES_INIT_INT_ARRAY, - TEST_RES_RAW_BYTES, - TEST_RES_OPAQUE_ARRAY, - TEST_RES_INT, - TEST_RES_BOOL, - TEST_RES_FLOAT, - TEST_RES_STRING, - TEST_RES_OBJLNK, - TEST_RES_BYTES_ZERO_BEGIN), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(TEST_RES_TIMESTAMP, + TEST_RES_COUNTER, + TEST_RES_INCREMENT_COUNTER, + TEST_RES_INT_ARRAY, + TEST_RES_LAST_EXEC_ARGS_ARRAY, + TEST_RES_BYTES, + TEST_RES_BYTES_SIZE, + TEST_RES_BYTES_BURST, + TEST_RES_EMPTY, + TEST_RES_INIT_INT_ARRAY, + TEST_RES_RAW_BYTES, + TEST_RES_OPAQUE_ARRAY, + TEST_RES_INT, + TEST_RES_BOOL, + TEST_RES_FLOAT, + TEST_RES_STRING, + TEST_RES_OBJLNK, + TEST_RES_BYTES_ZERO_BEGIN), .handlers = { .instance_it = test_instance_it, .instance_present = test_instance_present, @@ -745,7 +742,7 @@ const anjay_dm_object_def_t TEST_OBJECT = { }; const anjay_dm_object_def_t **test_object_create(void) { - test_repr_t *repr = (test_repr_t*)avs_calloc(1, sizeof(test_repr_t)); + test_repr_t *repr = (test_repr_t *) avs_calloc(1, sizeof(test_repr_t)); if (!repr) { return NULL; } diff --git a/devconfig b/devconfig index 2622a59fd..38ceccb46 100755 --- a/devconfig +++ b/devconfig @@ -130,7 +130,6 @@ ${CMAKE_COMMAND} \ -D WITH_EXTRA_WARNINGS=ON \ -D WITH_CON_ATTR=ON \ -D WITH_HTTP_DOWNLOAD=ON \ - -D WITH_JSON=ON \ -D WITH_VALGRIND=${WITH_VALGRIND} \ -D WITH_INTEGRATION_TESTS=ON \ -D WITH_DOC_CHECK=ON \ diff --git a/doc/sphinx/source/BasicTutorial/BT4.rst b/doc/sphinx/source/BasicTutorial/BT4.rst index 33f0c3af9..4a7d039e4 100644 --- a/doc/sphinx/source/BasicTutorial/BT4.rst +++ b/doc/sphinx/source/BasicTutorial/BT4.rst @@ -16,29 +16,77 @@ A few notes on general usage ============================ -Pros and cons of being single threaded -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.. _single-request-single-function-call: -To start, we first need to establish that Anjay is a single-threaded -library. It relies on the user calling ``anjay_serve()`` whenever some -packet is receieved and ``anjay_sched_run()`` regularly, as described in the -:doc:`previous chapter `. Both of the methods block until the incoming -message or (respectively) a job is processed. +Single request - single function call +------------------------------------- -.. note:: +To start, we first need to establish that Anjay LwM2M client API assumes a +single-threaded mode of operation. It relies on the user calling +``anjay_serve()`` whenever some packet is receieved and ``anjay_sched_run()`` +regularly, as described in the :doc:`previous chapter `. Both of the +methods block until the incoming message or (respectively) a job is processed. + +An incoming message may either be fully contained in a single packet, or split +between multiple packets, however ``anjay_serve()`` is called just for the +first one. Other parts (if any) are fetched internally by the library. In other +words: ``anjay_serve()`` blocks until the message gets handled completely. + +.. figure:: _images/anjay-server-request.svg + :width: 100% + + Handling a single LwM2M request. The application gets notified a packet + was received on a socket using ``poll()``/``select()`` and calls + ``anjay_serve()`` on that socket, allowing the library to interpret the + request and create a response. + + +When considering a request from the LwM2M server, the block-wise transfer may +be initiated either by server (e.g. a big Write request) or by the client +(e.g. a large response to a Read request). + +.. figure:: _images/anjay-server-block-request.svg + :width: 100% + + Handling a block-wise request from the LwM2M server. The server realizes + its request is too big to fit in a single packet and explicitly initiates + block-wise transfer by adding CoAP BLOCK option to the request. A single + call to ``anjay_serve()`` blocks the client until the request is completely + handled. + +.. figure:: _images/anjay-block-response-to-server-request.svg + :width: 100% + + A block-wise response to a non-block request from the LwM2M server. In + this case, the server performs a simple Read request. The client realizes + that returned data is too big, and adds a BLOCK option to the response. + The server then requests further blocks of a response. The call to + ``anjay_serve()`` only returns after the last block of a response is sent. + + +Similar situation arises when the client attempts to send a Register or Update +LwM2M request to the server with a large list of available Object Instances, +or a big Notify message. The difference is that the client sends its own +requests from within ``anjay_sched_run()`` call instead of ``anjay_serve()``. + +.. figure:: _images/anjay-client-request.svg + :width: 100% + + A simple request from the LwM2M client. + +.. figure:: _images/anjay-block-client-request.svg + :width: 100% + + A block-wise request from the LwM2M client. ``anjay_sched_run()`` call + blocks until the full transfer is complete. - The incoming message may be split between few packets (e.g. in case of - blockwise transfers), however ``anjay_serve()`` is called just for the - first one. Other parts (if any) are fetched internally by Anjay. In other - words: ``anjay_serve()`` blocks until the message gets handled completely. Because ``anjay_serve()`` blocks after a packet arrives, the library can handle at most one LwM2M Server at time, which makes its usage convenient, -as one does not have to worry about :ref:`data model ` -being accessed or modified by multiple LwM2M Servers at the same time. -Unfortunately it may happen to be a problem, as during blockwise transfers -the library is unable to respond to other LwM2M Servers with anything else -than 5.03 Service Unavailable. +as one does not have to worry about data model being accessed or modified +by multiple LwM2M Servers at the same time. Unfortunately it may happen to +be a problem, as during blockwise transfers the library is unable to respond +to other LwM2M Servers with anything else than 5.03 Service Unavailable. Before getting worried about it too much, one shall realize that the above behavior happens only when a blockwise transfer is issued on some part of @@ -47,15 +95,14 @@ and transfer big amounts of data regularly through LwM2M which, in context of resource constrained environments targeted by the LwM2M protocol might not be the best fit. -We believe that pretty much all transfers of bigger amounts of data happen -due to firmware update of the Client. In this case one may mitigate described -problem quite easily by either spawning a child process which would download -the firmware in the background, or by using our :ref:`asynchronous CoAP -downloader `. Both methods will leave the library in -an operational state during the transfer. +.. note:: + + The blocking behavior does not apply to firmware downloaded using the PULL + method. See :ref:`firmware-transfer` for details. + Transactions and ``anjay_serve()`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +---------------------------------- Our data model supports transactional operations. They are here to ensure that whenever something goes wrong during a transaction, all changes applied since @@ -70,15 +117,64 @@ operation no other LwM2M Server can see the LwM2M Client being in partially consistent state. Things work a bit different during the Bootstrap Sequence though. When the -Client/Server initiated Bootstrap begins, Anjay fires transaction handlers -for all data model entities. At the same time, it enters the state where -requests originated from Bootstrap Server only are handled - there may -be more than one such request, and so ``anjay_serve()`` could get called -multiple times. This again does not hurt consistency in any way, because -according to the LwM2M Specification, the LwM2M Client may ignore other -servers during that special time, and Anjay is doing just that - meaning +Client/Server initiated Bootstrap begins, the library fires transaction +handlers for all data model entities. At the same time, it enters the state +where requests originated from Bootstrap Server only are handled - there may be +more than one such request, and so ``anjay_serve()`` could get called multiple +times. This again does not hurt consistency in any way, because according to +the LwM2M Specification, the LwM2M Client may ignore other servers during that +special time, and the library is doing just that - meaning that they won't be able to observe intermediate initialization state. -After the Bootstrap Sequence finishes Anjay checks that the data model is +After the Bootstrap Sequence finishes the library checks that the data model is valid, and if it isn't the previous correct state will be restored, which proves the point. + + +.. _firmware-transfer: + +Firmware transfer +----------------- + +In LwM2M, the firmware may be transferred to a client using two possible +methods: + +- **PUSH** - the download is initiated by the server, performing a Write + operation on the /5/0/0 (Package) Resource. This usually means a block-wise + transfer, which for Anjay based clients will prevent handling other + LwM2M requests until the Write completes, as described in + :ref:`single-request-single-function-call`. + +- **PULL** - the LwM2M server indicates where should the client download + firmware package from with Write on /5/0/1 (Package URI) Resource. The client + then performs the download asynchronously, while still being able to handle + LwM2M requests. + +.. figure:: _images/anjay-firmware-download-coap-pull.svg + :width: 100% + + Anjay generally handles asynchronous CoAP downloads within + ``anjay_serve()`` calls. The ``anjay_sched_run()`` may initiate the + download or retransmit requests in case of packet loss. Downloaded firmware + is passed to the application through callbacks configured using + ``fw_update`` field of the ``anjay_configuration_t`` struct passed to + ``anjay_new()``. For more details, see API reference. + + +Notifications +------------- + +Anjay uses its scheduler to track pending notifications. Whenever +a notification has to be sent, it is done from within ``anjay_sched_run()`` +function. + +.. note:: + + Calling ``anjay_notify_changed()`` or ``anjay_notify_instances_changed()`` + does not send notifications immediately - they use the scheduler instead. + + +.. figure:: _images/anjay-notification.svg + :width: 100% + + Sending a LwM2M Notify message. diff --git a/doc/sphinx/source/BasicTutorial/BT5_CustomObject.rst b/doc/sphinx/source/BasicTutorial/BT5_CustomObject.rst index 48fa2738f..7492d4861 100644 --- a/doc/sphinx/source/BasicTutorial/BT5_CustomObject.rst +++ b/doc/sphinx/source/BasicTutorial/BT5_CustomObject.rst @@ -28,9 +28,14 @@ which holds: /** A struct containing pointers to Object handlers. */ typedef struct { - /** Get default Object attributes, @ref anjay_dm_object_read_default_attrs_t */ + /** + * Get default Object attributes, @ref anjay_dm_object_read_default_attrs_t + */ anjay_dm_object_read_default_attrs_t *object_read_default_attrs; - /** Set default Object attributes, @ref anjay_dm_object_write_default_attrs_t */ + /** + * Set default Object attributes, + * @ref anjay_dm_object_write_default_attrs_t + */ anjay_dm_object_write_default_attrs_t *object_write_default_attrs; /** Enumerate available Object Instances, @ref anjay_dm_instance_it_t */ @@ -45,24 +50,40 @@ which holds: /** Delete an Object Instance, @ref anjay_dm_instance_remove_t */ anjay_dm_instance_remove_t *instance_remove; - /** Get default Object Instance attributes, @ref anjay_dm_instance_read_default_attrs_t */ + /** + * Get default Object Instance attributes, + * @ref anjay_dm_instance_read_default_attrs_t + */ anjay_dm_instance_read_default_attrs_t *instance_read_default_attrs; - /** Set default Object Instance attributes, @ref anjay_dm_instance_write_default_attrs_t */ + /** + * Set default Object Instance attributes, + * @ref anjay_dm_instance_write_default_attrs_t + */ anjay_dm_instance_write_default_attrs_t *instance_write_default_attrs; - /** Check if a Resource is present in given Object Instance, @ref anjay_dm_resource_present_t */ + /** + * Check if a Resource is present in given Object Instance, + * @ref anjay_dm_resource_present_t + */ anjay_dm_resource_present_t *resource_present; - /** Returns a mask of supported operations on a given Resource, @ref anjay_dm_resource_operations_t */ + /** + * Returns a mask of supported operations on a given Resource, + * @ref anjay_dm_resource_operations_t + */ anjay_dm_resource_operations_t *resource_operations; /** Get Resource value, @ref anjay_dm_resource_read_t */ anjay_dm_resource_read_t *resource_read; /** Set Resource value, @ref anjay_dm_resource_write_t */ anjay_dm_resource_write_t *resource_write; - /** Perform Execute action on a Resource, @ref anjay_dm_resource_execute_t */ + /** + * Perform Execute action on a Resource, @ref anjay_dm_resource_execute_t + */ anjay_dm_resource_execute_t *resource_execute; - /** Get number of Multiple Resource instances, @ref anjay_dm_resource_dim_t */ + /** + * Get number of Multiple Resource instances, @ref anjay_dm_resource_dim_t + */ anjay_dm_resource_dim_t *resource_dim; /** Get Resource attributes, @ref anjay_dm_resource_read_attrs_t */ anjay_dm_resource_read_attrs_t *resource_read_attrs; @@ -71,11 +92,19 @@ which holds: /** Begin a transaction on this Object, @ref anjay_dm_transaction_begin_t */ anjay_dm_transaction_begin_t *transaction_begin; - /** Validate whether a transaction on this Object can be cleanly committed. See @ref anjay_dm_transaction_validate_t */ + /** + * Validate whether a transaction on this Object can be cleanly committed. + * See @ref anjay_dm_transaction_validate_t + */ anjay_dm_transaction_validate_t *transaction_validate; - /** Commit changes made in a transaction, @ref anjay_dm_transaction_commit_t */ + /** + * Commit changes made in a transaction, @ref anjay_dm_transaction_commit_t + */ anjay_dm_transaction_commit_t *transaction_commit; - /** Rollback changes made in a transaction, @ref anjay_dm_transaction_rollback_t */ + /** + * Rollback changes made in a transaction, + * @ref anjay_dm_transaction_rollback_t + */ anjay_dm_transaction_rollback_t *transaction_rollback; } anjay_dm_handlers_t; @@ -83,10 +112,12 @@ which holds: typedef struct { /** Number of element in the array */ size_t count; - /** Pointer to an array of Resource IDs supported by the object. A Resource + /** + * Pointer to an array of Resource IDs supported by the object. A Resource * is considered SUPPORTED if it may ever be present within the Object. The * array MUST be exactly count elements long and sorted in strictly - * ascending order. */ + * ascending order. + */ const uint16_t *rids; } anjay_dm_supported_rids_t; @@ -100,7 +131,7 @@ which holds: * supported_rids field of @ref anjay_dm_object_def_t. The count * field will be automatically calculated. */ - #define ANJAY_DM_SUPPORTED_RIDS(...) \ + # define ANJAY_DM_SUPPORTED_RIDS(...) \ // ... /** A struct defining an LwM2M Object. */ @@ -108,15 +139,19 @@ which holds: /** Object ID */ anjay_oid_t oid; - /** Object version: a string with static lifetime, containing two digits + /** + * Object version: a string with static lifetime, containing two digits * separated by a dot (for example: "1.1"). * If left NULL, client will not include the "ver=" attribute in Register - * and Discover messages, which implies version 1.0. */ + * and Discover messages, which implies version 1.0. + */ const char *version; - /** List of Resource IDs supported by the object. The + /** + * List of Resource IDs supported by the object. The * @ref ANJAY_DM_SUPPORTED_RIDS macro is the preferred way of initializing - * it. */ + * it. + */ anjay_dm_supported_rids_t supported_rids; /** Handler callbacks for this object. */ diff --git a/doc/sphinx/source/BasicTutorial/BT5_CustomObject/BT_CO5_MultiInstanceDynamic.rst b/doc/sphinx/source/BasicTutorial/BT5_CustomObject/BT_CO5_MultiInstanceDynamic.rst index 98d18c2c3..e2d39159e 100644 --- a/doc/sphinx/source/BasicTutorial/BT5_CustomObject/BT_CO5_MultiInstanceDynamic.rst +++ b/doc/sphinx/source/BasicTutorial/BT5_CustomObject/BT_CO5_MultiInstanceDynamic.rst @@ -301,10 +301,11 @@ Let's have a look on ``anjay_dm_instance_create_t`` handler type signature: .. snippet-source:: include_public/anjay/dm.h - typedef int anjay_dm_instance_create_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t *inout_iid, - anjay_ssid_t ssid); + typedef int + anjay_dm_instance_create_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t *inout_iid, + anjay_ssid_t ssid); The ``inout_iid`` parameter is the most important for us at the moment, as if the instantiation succeeds we MUST tell the library the id of the newly diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-block-client-request.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-block-client-request.svg new file mode 100644 index 000000000..7a6ecdfa6 --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-block-client-request.svg @@ -0,0 +1,2 @@ + +
LwM2M client
event loop
[Not supported by viewer]
Anjay
Anjay
LwM2M server
LwM2M server
anjay_sched_run()
anjay_sched_run()
LwM2M request (e.g. Register, Update), BLOCK 0
LwM2M request (e.g. Register, Update), BLOCK 0
2.31 Continue
2.31 Continue
LwM2M request (e.g. Register), BLOCK 1
LwM2M request (e.g. Register), BLOCK 1
2.31 Continue
2.31 Continue
LwM2M request (e.g. Register), BLOCK 2, final
LwM2M request (e.g. Register), BLOCK 2, final
LwM2M response (e.g. Created)
LwM2M response (e.g. Created)
\ No newline at end of file diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-block-response-to-server-request.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-block-response-to-server-request.svg new file mode 100644 index 000000000..57ab5179f --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-block-response-to-server-request.svg @@ -0,0 +1,2 @@ + +
LwM2M client
event loop
[Not supported by viewer]
Anjay
Anjay
Network stack
(OS or e.g. LwIP)
[Not supported by viewer]
LwM2M request
LwM2M request
recv(), blocking call
recv(), blocking call
LwM2M request (e.g. Read), BLOCK 2
LwM2M request (e.g. Read), BLOCK 2
LwM2M request, BLOCK 2
LwM2M request, BLOCK 2
send()
send()
LwM2M response, BLOCK2, final
LwM2M response, BLOCK2, final
LwM2M server
LwM2M server
poll()/select()
poll()/select()
notification: message received
notification: message received
LwM2M request (e.g. Read)
LwM2M request (e.g. Read)
anjay_serve()
anjay_serve()
recv()
recv()
send()
send()
LwM2M response, BLOCK 0
LwM2M response, BLOCK 0
recv(), blocking call
recv(), blocking call
LwM2M request (e.g. Read), BLOCK 1
LwM2M request (e.g. Read), BLOCK 1
LwM2M request, BLOCK 1
LwM2M request, BLOCK 1
send()
send()
LwM2M response, BLOCK 1
LwM2M response, BLOCK 1
Client realizes
the response is too large to fit in a single message, initiates BLOCK transfer
[Not supported by viewer]
After receiving a repsonse with BLOCK2 CoAP option, server sends repeated requests with increasing BLOCK numbers to retrieve full response
[Not supported by viewer]
\ No newline at end of file diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-client-request.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-client-request.svg new file mode 100644 index 000000000..2831bc6a7 --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-client-request.svg @@ -0,0 +1,2 @@ + +
LwM2M client
event loop
[Not supported by viewer]
Anjay
Anjay
LwM2M server
LwM2M server
anjay_sched_run()
anjay_sched_run()
LwM2M request (e.g. Register, Update)
LwM2M request (e.g. Register, Update)
LwM2M response
LwM2M response
\ No newline at end of file diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-firmware-download-coap-pull.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-firmware-download-coap-pull.svg new file mode 100644 index 000000000..db846075a --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-firmware-download-coap-pull.svg @@ -0,0 +1,2 @@ + +
LwM2M client
event loop
[Not supported by viewer]
Anjay
Anjay
CoAP Content, BLOCK 1
CoAP Content, BLOCK 1
anjay_serve()
anjay_serve()
anjay_sched_run()
anjay_sched_run()
CoAP Read, BLOCK 2
CoAP Read, BLOCK 2
If a request is lost, retransmissions are handled by the scheduler
If a request is lost, retransmissions are handled by the scheduler
LwM2M server
LwM2M server
anjay_sched_run()
anjay_sched_run()
CoAP Read, BLOCK 0
CoAP Read, BLOCK 0
CoAP Content, BLOCK 0
CoAP Content, BLOCK 0
anjay_serve()
anjay_serve()
Returned firmware is passed
to anjay_fw_update_handlers_t callbacks
Returned firmware is passed<br>to anjay_fw_update_handlers_t callbacks
CoAP Read, BLOCK 1
CoAP Read, BLOCK 1
CoAP Content, BLOCK 2, final
CoAP Content, BLOCK 2, final
anjay_serve()
anjay_serve()
CoAP Read, BLOCK 2 (lost)
CoAP Read, BLOCK 2 (lost)
\ No newline at end of file diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-firmware-handling.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-firmware-handling.svg new file mode 100644 index 000000000..006545d60 --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-firmware-handling.svg @@ -0,0 +1,2 @@ + +
Anjay
Anjay
LwM2M Execute: Update
LwM2M Execute: Update
Firmware update handlers
Firmware update handlers
stream_write()
stream_write()
stream_finish()
stream_finish()
perform_upgrade()
perform_upgrade()
LwM2M server
LwM2M server
Request firmware part
Request firmware part
Last firmware part
Last firmware part
LwM2M Write: firmware URI
LwM2M Write: firmware URI
reset()
reset()
stream_open()
stream_open()
Request firmware part
Request firmware part
Firmware part
Firmware part
stream_write()
stream_write()
perform_upgrade() handler may or may not return, see API documentation
[Not supported by viewer]
\ No newline at end of file diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-notification.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-notification.svg new file mode 100644 index 000000000..00c7da0f2 --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-notification.svg @@ -0,0 +1,2 @@ + +
LwM2M client
event loop
[Not supported by viewer]
Anjay
Anjay
LwM2M server
LwM2M server
anjay_sched_run()
anjay_sched_run()
LwM2M Notify (Non-confirmable)
LwM2M Notify (Non-confirmable)
\ No newline at end of file diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-read.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-read.svg new file mode 100644 index 000000000..fa70adac0 --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-read.svg @@ -0,0 +1,2 @@ + +
Anjay
Anjay
Object /3 handlers
Object /3 handlers
resource_operations(IID = 0, RID = 0)
resource_operations(IID = 0, RID = 0)
R
R
LwM2M server
LwM2M server
LwM2M Read /3/0/0
LwM2M Read /3/0/0
2.05 Content: 42
2.05 Content: 42
resource_read()
resource_read()
anjay_ret_i32(ctx, 42)
anjay_ret_i32(ctx, 42)
instance_present(IID = 0)
instance_present(IID = 0)
1
1
resource_present(IID = 0, RID = 0)
resource_present(IID = 0, RID = 0)
1
1
\ No newline at end of file diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-server-block-request.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-server-block-request.svg new file mode 100644 index 000000000..1a9213d02 --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-server-block-request.svg @@ -0,0 +1,2 @@ + +
LwM2M client
event loop
[Not supported by viewer]
Anjay
Anjay
Network stack
(OS or e.g. LwIP)
[Not supported by viewer]
LwM2M request, BLOCK 0
LwM2M request, BLOCK 0
recv(), blocking call
recv(), blocking call
LwM2M request (e.g. Write), BLOCK 2, final
LwM2M request (e.g. Write), BLOCK 2, final
LwM2M request, BLOCK 2, final
LwM2M request, BLOCK 2, final
send()
send()
LwM2M response
LwM2M response
LwM2M server
LwM2M server
poll()/select()
poll()/select()
notification: message received
notification: message received
LwM2M request (e.g. Write), BLOCK 0
LwM2M request (e.g. Write), BLOCK 0
anjay_serve()
anjay_serve()
recv()
recv()
send()
send()
2.31 Continue
2.31 Continue
recv(), blocking call
recv(), blocking call
LwM2M request (e.g. Write), BLOCK 1
LwM2M request (e.g. Write), BLOCK 1
LwM2M request, BLOCK 1
LwM2M request, BLOCK 1
send()
send()
2.31 Continue
2.31 Continue
\ No newline at end of file diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-server-request.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-server-request.svg new file mode 100644 index 000000000..a99ca2f84 --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-server-request.svg @@ -0,0 +1,2 @@ + +
LwM2M client
event loop
[Not supported by viewer]
Anjay
Anjay
Network stack
(OS or e.g. LwIP)
[Not supported by viewer]
LwM2M request
LwM2M request
LwM2M server
LwM2M server
poll()/select()
poll()/select()
notification: message received
notification: message received
LwM2M request (e.g. Read)
LwM2M request (e.g. Read)
anjay_serve()
anjay_serve()
recv()
recv()
send()
send()
LwM2M response
LwM2M response
\ No newline at end of file diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-write-replace-instance.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-write-replace-instance.svg new file mode 100644 index 000000000..c2bab2a6d --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-write-replace-instance.svg @@ -0,0 +1,2 @@ + +
Anjay
Anjay
Object /1 handlers
Object /1 handlers
LwM2M server
LwM2M server
LwM2M Write (replace) /1/0
RID 1: 3600
RID 7: "U"
LwM2M Write (replace) /1/0<br>RID 1: 3600<br>RID 7: "U"
transaction_begin()
transaction_begin()
instance_reset(IID = 0)
instance_reset(IID = 0)
2.04 Changed
2.04 Changed
transaction_commit()
transaction_commit()
transaction_validate()
transaction_validate()
instance_present(IID = 0)
instance_present(IID = 0)
1
1
Resource 1
Resource 1
resource_operations(IID = 0, RID = 1)
resource_operations(IID = 0, RID = 1)
RW
RW
anjay_get_string()
anjay_get_string()
"U"
"U"
resource_operations(IID = 0, RID = 7)
resource_operations(IID = 0, RID = 7)
RW
RW
resource_write(RID = 7)
resource_write(RID = 7)
resource_write(RID = 1)
resource_write(RID = 1)
anjay_get_i32()
anjay_get_i32()
3600
3600
One call sequence for each Resource in request payload:
- resource_operations
- resource_write
[Not supported by viewer]
Resource 7
Resource 7
\ No newline at end of file diff --git a/doc/sphinx/source/BasicTutorial/_images/anjay-write-resource.svg b/doc/sphinx/source/BasicTutorial/_images/anjay-write-resource.svg new file mode 100644 index 000000000..08c6ee635 --- /dev/null +++ b/doc/sphinx/source/BasicTutorial/_images/anjay-write-resource.svg @@ -0,0 +1,2 @@ + +
Anjay
Anjay
Object /5
handlers
[Not supported by viewer]
LwM2M server
LwM2M server
LwM2M Write /5/0/1: coap://example.com
LwM2M Write /5/0/1: coap://example.com
transaction_begin()
transaction_begin()
resource_write()
resource_write()
anjay_get_string()
anjay_get_string()
"coap://example.com"
"coap://example.com"
resource_operations(IID = 0, RID = 0)
resource_operations(IID = 0, RID = 0)
RW
RW
instance_present(IID = 0)
instance_present(IID = 0)
1
1
2.04 Changed
2.04 Changed
transaction_validate()
transaction_validate()
transaction_commit()
transaction_commit()
\ No newline at end of file diff --git a/examples/test_examples.md5 b/examples/test_examples.md5 index 57c9ee979..bb4f489fc 100644 --- a/examples/test_examples.md5 +++ b/examples/test_examples.md5 @@ -14,9 +14,9 @@ b043782e2aa5444e713b0e15e618133c examples/tutorial/custom-object/notifications/s 6a1027758af849100e7af177140a5c30 examples/tutorial/custom-object/read-only/src/main.c 962be6487df5009e2d30d959b5c3fb9d examples/tutorial/custom-object/writable-multiple-fixed-transactional/src/main.c 4e0c4110e9a82f9f0093944bc3d67bb6 examples/tutorial/custom-object/writable-multiple-fixed/src/main.c -7d3f4d2e29a0ab544b44b63bc3f1d458 include_public/anjay/dm.h -fe5935f76a8ad616a504c7ffc7236dc3 include_public/anjay/io.h +913716c3e53f2b01f8616e0314d6ad71 include_public/anjay/dm.h +7f9ac7902906576933cea4296cc999cb include_public/anjay/io.h abf21d7ece894fa3ea0b3497431b4d03 modules/access_control/include_public/anjay/access_control.h 8cc6b3ca19fb67d9a2501107c4f404b1 modules/attr_storage/include_public/anjay/attr_storage.h -91353a41f4fefae8fd3fea07a8589534 modules/security/include_public/anjay/security.h -b1193049de993beaca2c0125a902d1df modules/server/include_public/anjay/server.h +c497f239f7ad86941952af82f71323c4 modules/security/include_public/anjay/security.h +c78ab3c174f175d9692df6290a52bfd1 modules/server/include_public/anjay/server.h diff --git a/include_modules/anjay_modules/dm/attributes.h b/include_modules/anjay_modules/dm/attributes.h index faf249c37..f79c2630b 100644 --- a/include_modules/anjay_modules/dm/attributes.h +++ b/include_modules/anjay_modules/dm/attributes.h @@ -22,7 +22,7 @@ VISIBILITY_PRIVATE_HEADER_BEGIN #if defined(WITH_CON_ATTR) // || defined(...) -#define WITH_CUSTOM_ATTRIBUTES +# define WITH_CUSTOM_ATTRIBUTES #endif typedef enum { @@ -33,15 +33,15 @@ typedef enum { #ifdef WITH_CUSTOM_ATTRIBUTES typedef struct { -#ifdef WITH_CON_ATTR +# ifdef WITH_CON_ATTR anjay_dm_con_attr_t con; -#endif +# endif } anjay_dm_custom_attrs_t; typedef struct { -#ifdef WITH_CON_ATTR +# ifdef WITH_CON_ATTR bool has_con; -#endif +# endif } anjay_dm_custom_request_attribute_flags_t; /* @@ -129,13 +129,15 @@ typedef struct { */ typedef union { anjay_dm_custom_attrs_t data; - char padding[offsetof(struct { + char padding[offsetof( + struct { anjay_dm_custom_attrs_t custom; union { anjay_dm_attributes_t common; anjay_dm_resource_attributes_t res; } standard; - }, standard)]; + }, + standard)]; } anjay_dm_custom_attrs_storage_t; #endif // WITH_CUSTOM_ATTRIBUTES @@ -178,51 +180,49 @@ _anjay_dm_get_internal_res_attrs(anjay_dm_resource_attributes_t *attrs) { static inline const anjay_dm_internal_res_attrs_t * _anjay_dm_get_internal_res_attrs_const( const anjay_dm_resource_attributes_t *attrs) { - return AVS_CONTAINER_OF(attrs, - const anjay_dm_internal_res_attrs_t, standard); + return AVS_CONTAINER_OF(attrs, const anjay_dm_internal_res_attrs_t, + standard); } #ifdef WITH_CUSTOM_ATTRIBUTES -#ifdef WITH_CON_ATTR -#define _ANJAY_DM_CUSTOM_CON_ATTR_INITIALIZER \ - .con = ANJAY_DM_CON_ATTR_DEFAULT, -#else // WITH_CON_ATTR -#define _ANJAY_DM_CUSTOM_CON_ATTR_INITIALIZER -#endif // WITH_CON_ATTR +# ifdef WITH_CON_ATTR +# define _ANJAY_DM_CUSTOM_CON_ATTR_INITIALIZER \ + .con = ANJAY_DM_CON_ATTR_DEFAULT, +# else // WITH_CON_ATTR +# define _ANJAY_DM_CUSTOM_CON_ATTR_INITIALIZER +# endif // WITH_CON_ATTR -#define _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER \ - .custom = { \ - .data = { \ - _ANJAY_DM_CUSTOM_CON_ATTR_INITIALIZER \ - } \ - }, +# define _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER \ + .custom = { \ + .data = { _ANJAY_DM_CUSTOM_CON_ATTR_INITIALIZER } \ + }, #else // WITH_CUSTOM_ATTRIBUTES -#define _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER +# define _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER #endif // WITH_CUSTOM_ATTRIBUTES -#define _ANJAY_DM_ATTRIBS_EMPTY { \ +#define _ANJAY_DM_ATTRIBS_EMPTY \ + { \ .min_period = ANJAY_ATTRIB_PERIOD_NONE, \ - .max_period = ANJAY_ATTRIB_PERIOD_NONE \ + .max_period = ANJAY_ATTRIB_PERIOD_NONE \ } -#define _ANJAY_DM_INTERNAL_ATTRS_EMPTY { \ - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER \ - .standard = _ANJAY_DM_ATTRIBS_EMPTY \ - } +#define _ANJAY_DM_INTERNAL_ATTRS_EMPTY \ + { _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = _ANJAY_DM_ATTRIBS_EMPTY } -#define _ANJAY_RES_ATTRIBS_EMPTY { \ - .common = _ANJAY_DM_ATTRIBS_EMPTY, \ +// clang-format off +#define _ANJAY_RES_ATTRIBS_EMPTY \ + { \ + .common = _ANJAY_DM_ATTRIBS_EMPTY, \ .greater_than = ANJAY_ATTRIB_VALUE_NONE, \ - .less_than = ANJAY_ATTRIB_VALUE_NONE, \ - .step = ANJAY_ATTRIB_VALUE_NONE \ + .less_than = ANJAY_ATTRIB_VALUE_NONE, \ + .step = ANJAY_ATTRIB_VALUE_NONE \ } +// clang-format on -#define _ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY { \ - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER \ - .standard = _ANJAY_RES_ATTRIBS_EMPTY \ - } +#define _ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY \ + { _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = _ANJAY_RES_ATTRIBS_EMPTY } extern const anjay_dm_internal_attrs_t ANJAY_DM_INTERNAL_ATTRS_EMPTY; extern const anjay_dm_internal_res_attrs_t ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY; diff --git a/include_modules/anjay_modules/dm/execute.h b/include_modules/anjay_modules/dm/execute.h index 19ebda48d..86e2182b5 100644 --- a/include_modules/anjay_modules/dm/execute.h +++ b/include_modules/anjay_modules/dm/execute.h @@ -27,4 +27,3 @@ void _anjay_execute_ctx_destroy(anjay_execute_ctx_t **ctx); VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_INCLUDE_ANJAY_MODULES_DM_EXECUTE_H */ - diff --git a/include_modules/anjay_modules/dm/modules.h b/include_modules/anjay_modules/dm/modules.h index b48765a5e..262e83fea 100644 --- a/include_modules/anjay_modules/dm/modules.h +++ b/include_modules/anjay_modules/dm/modules.h @@ -101,4 +101,3 @@ void *_anjay_dm_module_get_arg(anjay_t *anjay, const anjay_dm_module_t *module); VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_INCLUDE_ANJAY_MODULES_DM_MODULES_H */ - diff --git a/include_modules/anjay_modules/dm_utils.h b/include_modules/anjay_modules/dm_utils.h index 553ea2b55..2561c94bd 100644 --- a/include_modules/anjay_modules/dm_utils.h +++ b/include_modules/anjay_modules/dm_utils.h @@ -53,19 +53,24 @@ static inline bool _anjay_uri_path_has_oid(const anjay_uri_path_t *path) { static inline bool _anjay_uri_path_equal(const anjay_uri_path_t *left, const anjay_uri_path_t *right) { - return (_anjay_uri_path_has_oid(left) - ? (_anjay_uri_path_has_oid(right) && left->oid == right->oid) - : !_anjay_uri_path_has_oid(right)) - && (_anjay_uri_path_has_iid(left) - ? (_anjay_uri_path_has_iid(right) && left->iid == right->iid) - : !_anjay_uri_path_has_iid(right)) - && (_anjay_uri_path_has_rid(left) - ? (_anjay_uri_path_has_rid(right) && left->rid == right->rid) - : !_anjay_uri_path_has_rid(right)); + if (left->type == right->type) { + switch (left->type) { + case ANJAY_PATH_ROOT: + return true; + case ANJAY_PATH_OBJECT: + return left->oid == right->oid; + case ANJAY_PATH_INSTANCE: + return left->oid == right->oid && left->iid == right->iid; + case ANJAY_PATH_RESOURCE: + return left->oid == right->oid && left->iid == right->iid + && left->rid == right->rid; + } + AVS_UNREACHABLE("switch above is supposed to be exhaustive"); + } + return false; } -#define ASSERT_RESOURCE_PATH(uri) \ - assert((uri).type == ANJAY_PATH_RESOURCE) +#define ASSERT_RESOURCE_PATH(uri) assert((uri).type == ANJAY_PATH_RESOURCE) #define MAKE_OBJECT_PATH(Oid) \ (anjay_uri_path_t) { \ @@ -73,21 +78,20 @@ static inline bool _anjay_uri_path_equal(const anjay_uri_path_t *left, .type = ANJAY_PATH_OBJECT \ } -#define MAKE_INSTANCE_PATH(Oid, Iid) \ - (anjay_uri_path_t) { \ - .oid = (Oid), \ - .iid = (Iid), \ - .type = ANJAY_PATH_INSTANCE \ +#define MAKE_INSTANCE_PATH(Oid, Iid) \ + (anjay_uri_path_t) { \ + .oid = (Oid), \ + .iid = (Iid), \ + .type = ANJAY_PATH_INSTANCE \ } -#define MAKE_INSTANCE_OR_RESOURCE_PATH(Oid, Iid, Rid) \ - (anjay_uri_path_t) { \ - .oid = (Oid), \ - .iid = (Iid), \ - .rid = (anjay_rid_t)(Rid), \ - .type = (0 <= (Rid) && (Rid) < UINT16_MAX) \ - ? ANJAY_PATH_RESOURCE \ - : ANJAY_PATH_INSTANCE \ +#define MAKE_INSTANCE_OR_RESOURCE_PATH(Oid, Iid, Rid) \ + (anjay_uri_path_t) { \ + .oid = (Oid), \ + .iid = (Iid), \ + .rid = (anjay_rid_t) (Rid), \ + .type = (0 <= (Rid) && (Rid) < UINT16_MAX) ? ANJAY_PATH_RESOURCE \ + : ANJAY_PATH_INSTANCE \ } #define MAKE_RESOURCE_PATH(Oid, Iid, Rid) \ @@ -157,10 +161,8 @@ static inline int _anjay_dm_res_read_bool(anjay_t *anjay, typedef struct anjay_dm anjay_dm_t; -typedef int -anjay_dm_foreach_object_handler_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj, - void *data); +typedef int anjay_dm_foreach_object_handler_t( + anjay_t *anjay, const anjay_dm_object_def_t *const *obj, void *data); int _anjay_dm_foreach_object(anjay_t *anjay, anjay_dm_foreach_object_handler_t *handler, @@ -213,16 +215,18 @@ bool _anjay_dm_handler_implemented(anjay_t *anjay, const anjay_dm_module_t *current_module, size_t handler_offset); -int _anjay_dm_object_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - anjay_dm_internal_attrs_t *out, - const anjay_dm_module_t *current_module); -int _anjay_dm_object_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - const anjay_dm_internal_attrs_t *attrs, - const anjay_dm_module_t *current_module); +int _anjay_dm_object_read_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + anjay_dm_internal_attrs_t *out, + const anjay_dm_module_t *current_module); +int _anjay_dm_object_write_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + const anjay_dm_internal_attrs_t *attrs, + const anjay_dm_module_t *current_module); int _anjay_dm_instance_it(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t *out, @@ -245,18 +249,20 @@ int _anjay_dm_instance_remove(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid, const anjay_dm_module_t *current_module); -int _anjay_dm_instance_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - anjay_dm_internal_attrs_t *out, - const anjay_dm_module_t *current_module); -int _anjay_dm_instance_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - const anjay_dm_internal_attrs_t *attrs, - const anjay_dm_module_t *current_module); +int _anjay_dm_instance_read_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + anjay_dm_internal_attrs_t *out, + const anjay_dm_module_t *current_module); +int _anjay_dm_instance_write_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + const anjay_dm_internal_attrs_t *attrs, + const anjay_dm_module_t *current_module); int _anjay_dm_resource_present(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid, @@ -265,12 +271,12 @@ int _anjay_dm_resource_present(anjay_t *anjay, bool _anjay_dm_resource_supported(const anjay_dm_object_def_t *const *obj_ptr, anjay_rid_t rid); -int -_anjay_dm_resource_supported_and_present(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - const anjay_dm_module_t *current_module); +int _anjay_dm_resource_supported_and_present( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + const anjay_dm_module_t *current_module); int _anjay_dm_resource_operations(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, @@ -316,18 +322,22 @@ int _anjay_dm_resource_write_attrs(anjay_t *anjay, const anjay_dm_internal_res_attrs_t *attrs, const anjay_dm_module_t *current_module); -int _anjay_dm_delegate_transaction_begin(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - const anjay_dm_module_t *current_module); -int _anjay_dm_delegate_transaction_validate(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - const anjay_dm_module_t *current_module); -int _anjay_dm_delegate_transaction_commit(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - const anjay_dm_module_t *current_module); -int _anjay_dm_delegate_transaction_rollback(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - const anjay_dm_module_t *current_module); +int _anjay_dm_delegate_transaction_begin( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + const anjay_dm_module_t *current_module); +int _anjay_dm_delegate_transaction_validate( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + const anjay_dm_module_t *current_module); +int _anjay_dm_delegate_transaction_commit( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + const anjay_dm_module_t *current_module); +int _anjay_dm_delegate_transaction_rollback( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + const anjay_dm_module_t *current_module); /** * Starts a transaction on the data model. If a transaction is already in @@ -430,4 +440,3 @@ anjay_ssid_t _anjay_dm_current_ssid(anjay_t *anjay); VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_INCLUDE_ANJAY_MODULES_DM_H */ - diff --git a/include_modules/anjay_modules/notify.h b/include_modules/anjay_modules/notify.h index a96cc6e3b..8a9d4e509 100644 --- a/include_modules/anjay_modules/notify.h +++ b/include_modules/anjay_modules/notify.h @@ -53,15 +53,13 @@ typedef AVS_LIST(anjay_notify_queue_object_entry_t) anjay_notify_queue_t; * Object require knowing which server (if any) performed the changes. * @ref _anjay_dm_current_ssid will be called to determine it. */ -int _anjay_notify_perform(anjay_t *anjay, - anjay_notify_queue_t queue); +int _anjay_notify_perform(anjay_t *anjay, anjay_notify_queue_t queue); /** * Calls @ref _anjay_notify_perform and @ref _anjay_notify_clear_queue * afterwards (regardless of success or failure). */ -int _anjay_notify_flush(anjay_t *anjay, - anjay_notify_queue_t *queue_ptr); +int _anjay_notify_flush(anjay_t *anjay, anjay_notify_queue_t *queue_ptr); int _anjay_notify_queue_instance_created(anjay_notify_queue_t *out_queue, anjay_oid_t oid, @@ -72,8 +70,7 @@ int _anjay_notify_queue_instance_removed(anjay_notify_queue_t *out_queue, anjay_iid_t iid); int _anjay_notify_queue_instance_set_unknown_change( - anjay_notify_queue_t *out_queue, - anjay_oid_t oid); + anjay_notify_queue_t *out_queue, anjay_oid_t oid); /** * Adds a notification about the change of value of the data model resource @@ -90,9 +87,8 @@ int _anjay_notify_instance_created(anjay_t *anjay, anjay_oid_t oid, anjay_iid_t iid); -typedef int anjay_notify_callback_t(anjay_t *anjay, - anjay_notify_queue_t queue, - void *data); +typedef int +anjay_notify_callback_t(anjay_t *anjay, anjay_notify_queue_t queue, void *data); VISIBILITY_PRIVATE_HEADER_END diff --git a/include_modules/anjay_modules/observe.h b/include_modules/anjay_modules/observe.h index 16c482a61..ba517cb06 100644 --- a/include_modules/anjay_modules/observe.h +++ b/include_modules/anjay_modules/observe.h @@ -26,11 +26,10 @@ void _anjay_observe_gc(anjay_t *anjay); #else // WITH_OBSERVE -#define _anjay_observe_gc(...) ((void) 0) +# define _anjay_observe_gc(...) ((void) 0) #endif // WITH_OBSERVE VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_INCLUDE_ANJAY_MODULES_OBSERVE_H */ - diff --git a/include_modules/anjay_modules/raw_buffer.h b/include_modules/anjay_modules/raw_buffer.h index d51a8d4cb..cf4b0469e 100644 --- a/include_modules/anjay_modules/raw_buffer.h +++ b/include_modules/anjay_modules/raw_buffer.h @@ -33,18 +33,18 @@ typedef struct { size_t capacity; } anjay_raw_buffer_t; -#define ANJAY_RAW_BUFFER_ON_STACK(Capacity) \ - (anjay_raw_buffer_t) { \ - .data = &(uint8_t[Capacity]){0}[0], \ - .size = 0, \ - .capacity = Capacity \ +#define ANJAY_RAW_BUFFER_ON_STACK(Capacity) \ + (anjay_raw_buffer_t) { \ + .data = &(uint8_t[Capacity]){ 0 }[0], \ + .size = 0, \ + .capacity = Capacity \ } #define ANJAY_RAW_BUFFER_EMPTY \ - (anjay_raw_buffer_t) { \ - .data = NULL, \ - .size = 0, \ - .capacity = 0 \ + (anjay_raw_buffer_t) { \ + .data = NULL, \ + .size = 0, \ + .capacity = 0 \ } /** @@ -76,4 +76,3 @@ int _anjay_raw_buffer_from_data(anjay_raw_buffer_t *dst, VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_INCLUDE_ANJAY_MODULES_RAW_BUFFER_H */ - diff --git a/include_modules/anjay_modules/sched.h b/include_modules/anjay_modules/sched.h index 0c0cbeb1f..f3f5cc3b2 100644 --- a/include_modules/anjay_modules/sched.h +++ b/include_modules/anjay_modules/sched.h @@ -84,11 +84,10 @@ static inline int _anjay_sched_now(anjay_sched_t *sched, anjay_sched_clb_t clb, const void *clb_data, size_t clb_data_size) { - return _anjay_sched(sched, out_handle, AVS_TIME_DURATION_ZERO, - clb, clb_data, clb_data_size); + return _anjay_sched(sched, out_handle, AVS_TIME_DURATION_ZERO, clb, + clb_data, clb_data_size); } VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_INCLUDE_ANJAY_MODULES_SCHED_H */ - diff --git a/include_modules/anjay_modules/servers.h b/include_modules/anjay_modules/servers.h index 5d96c8d19..21a4e8008 100644 --- a/include_modules/anjay_modules/servers.h +++ b/include_modules/anjay_modules/servers.h @@ -24,13 +24,15 @@ VISIBILITY_PRIVATE_HEADER_BEGIN typedef enum { - ANJAY_CONNECTION_UNSET = 0, - ANJAY_CONNECTION_FIRST_VALID_, - ANJAY_CONNECTION_UDP = ANJAY_CONNECTION_FIRST_VALID_, - ANJAY_CONNECTION_SMS, + ANJAY_CONNECTION_UNSET = -1, + ANJAY_CONNECTION_UDP = 0, ANJAY_CONNECTION_LIMIT_ } anjay_connection_type_t; +#define ANJAY_CONNECTION_TYPE_FOREACH(Var) \ + for ((Var) = (anjay_connection_type_t) 0; (Var) < ANJAY_CONNECTION_LIMIT_; \ + (Var) = (anjay_connection_type_t) ((Var) + 1)) + typedef struct { char pk_or_identity[ANJAY_MAX_PK_OR_IDENTITY_SIZE]; size_t pk_or_identity_size; diff --git a/include_modules/anjay_modules/time_defs.h b/include_modules/anjay_modules/time_defs.h index b453e40e4..f7c1870fc 100644 --- a/include_modules/anjay_modules/time_defs.h +++ b/include_modules/anjay_modules/time_defs.h @@ -17,9 +17,9 @@ #ifndef ANJAY_INCLUDE_ANJAY_MODULES_TIME_H #define ANJAY_INCLUDE_ANJAY_MODULES_TIME_H +#include #include #include -#include #include @@ -31,4 +31,3 @@ VISIBILITY_PRIVATE_HEADER_BEGIN VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_INCLUDE_ANJAY_MODULES_TIME_H */ - diff --git a/include_modules/anjay_modules/utils_core.h b/include_modules/anjay_modules/utils_core.h index b173cd6bd..c18377881 100644 --- a/include_modules/anjay_modules/utils_core.h +++ b/include_modules/anjay_modules/utils_core.h @@ -20,10 +20,10 @@ #include #ifdef WITH_AVS_LOG -#include -#define _anjay_log(...) avs_log(__VA_ARGS__) +# include +# define _anjay_log(...) avs_log(__VA_ARGS__) #else -#define _anjay_log(...) ((void) 0) +# define _anjay_log(...) ((void) 0) #endif VISIBILITY_PRIVATE_HEADER_BEGIN @@ -33,7 +33,10 @@ typedef struct anjay_string { } anjay_string_t; #define ANJAY_MAX_URL_RAW_LENGTH 256 -#define ANJAY_MAX_URL_HOSTNAME_SIZE (ANJAY_MAX_URL_RAW_LENGTH - sizeof("coaps://" ":0")) +#define ANJAY_MAX_URL_HOSTNAME_SIZE \ + (ANJAY_MAX_URL_RAW_LENGTH \ + - sizeof("coaps://" \ + ":0")) #define ANJAY_MAX_URL_PORT_SIZE sizeof("65535") typedef enum { @@ -49,9 +52,10 @@ typedef struct anjay_url { AVS_LIST(const anjay_string_t) uri_query; } anjay_url_t; -#define ANJAY_URL_EMPTY \ - (anjay_url_t) { \ - .uri_path = NULL, .uri_query = NULL \ +#define ANJAY_URL_EMPTY \ + (anjay_url_t) { \ + .uri_path = NULL, \ + .uri_query = NULL \ } #define ANJAY_FOREACH_BREAK INT_MIN diff --git a/include_public/anjay/anjay.h b/include_public/anjay/anjay.h index 0e3c83658..f7b1ee357 100644 --- a/include_public/anjay/anjay.h +++ b/include_public/anjay/anjay.h @@ -19,7 +19,7 @@ #include #include -#include #include +#include #endif /*ANJAY_INCLUDE_ANJAY_ANJAY_H*/ diff --git a/include_public/anjay/core.h b/include_public/anjay/core.h index 2c424c6a5..8ef559644 100644 --- a/include_public/anjay/core.h +++ b/include_public/anjay/core.h @@ -67,12 +67,27 @@ void anjay_smsdrv_cleanup(anjay_smsdrv_t **smsdrv_ptr); /** * Default transmission params recommended by the CoAP specification (RFC 7252). */ -#define ANJAY_COAP_DEFAULT_UDP_TX_PARAMS \ - { \ - /* .ack_timeout_ms = */ { 2, 0 }, \ - /* .ack_random_factor = */ 1.5, \ - /* .max_retransmit = */ 4 \ +// clang-format off +#define ANJAY_COAP_DEFAULT_UDP_TX_PARAMS \ + { \ + /* .ack_timeout = */ { 2, 0 }, \ + /* .ack_random_factor = */ 1.5, \ + /* .max_retransmit = */ 4 \ } +// clang-format on + +/** + * Default handshake retransmission params recommended by the DTLS specification + * (RFC 6347), i.e: 1s for the initial response, growing exponentially (with + * each retransmission) up to maximum of 60s. + */ +// clang-format off +#define ANJAY_DTLS_DEFAULT_UDP_HS_TX_PARAMS \ + { \ + /* .min = */ { 1, 0 }, \ + /* .max = */ { 60, 0 } \ + } +// clang-format on /** * Default transmission params for SMS connections. @@ -80,12 +95,14 @@ void anjay_smsdrv_cleanup(anjay_smsdrv_t **smsdrv_ptr); * This set of parameters ensures MAX_TRANSMIT_WAIT is equal to the default * (i.e. CoAP specified) MAX_TRANSMIT_WAIT, while disabling retransmissions. */ -#define ANJAY_COAP_DEFAULT_SMS_TX_PARAMS \ - { \ - /* .ack_timeout_ms = */ { 62, 0 }, \ - /* .ack_random_factor = */ 1.5, \ - /* .max_retransmit = */ 0 \ +// clang-format off +#define ANJAY_COAP_DEFAULT_SMS_TX_PARAMS \ + { \ + /* .ack_timeout = */ { 62, 0 }, \ + /* .ack_random_factor = */ 1.5, \ + /* .max_retransmit = */ 0 \ } +// clang-format on typedef struct anjay_configuration { /** @@ -155,6 +172,42 @@ typedef struct anjay_configuration { */ const avs_coap_tx_params_t *udp_tx_params; + /** + * Configuration of the DTLS handshake retransmission timeouts for UDP + * connection. + * + * If NULL, the default configuration + * @ref ANJAY_DTLS_DEFAULT_UDP_HS_TX_PARAMS will be selected. + * + * NOTE: Parameters are copied during @ref anjay_new() and cannot be + * modified later on. + * + * IMPORTANT: In case of a need to adjust DTLS retransmission params to + * match the CoAP retransmission params, the @ref udp_dtls_hs_tx_params + * shall be initialized as `dtls_hs_params` is in the following code + * snippet: + * @code + * const avs_coap_tx_params_t coap_tx_params = { + * // ... some initialization + * }; + * + * // Without ACK_RANDOM_FACTOR = 1.0, it is impossible to create a DTLS HS + * // configuration that matches CoAP retransmission configuration + * // perfectly. + * assert(coap_tx_params.ack_random_factor == 1.0); + * + * const avs_net_dtls_handshake_timeouts_t dtls_hs_tx_params = { + * .min = avs_time_duration_fmul(coap_tx_params.ack_timeout, + * coap_tx_params.ack_random_factor), + * .max = avs_time_duration_fmul( + * coap_tx_params.ack_timeout, + * (1 << coap_tx_params.max_retransmit) + * * coap_tx_params.ack_random_factor) + * }; + * @endcode + */ + const avs_net_dtls_handshake_timeouts_t *udp_dtls_hs_tx_params; + /** * Configuration of the CoAP transmission params for SMS connection, as per * RFC 7252. @@ -363,8 +416,7 @@ AVS_LIST(const anjay_socket_entry_t) anjay_get_socket_entries(anjay_t *anjay); * @returns 0 on success, a negative value in case of error. Note that it * includes non-fatal errors, such as receiving a malformed packet. */ -int anjay_serve(anjay_t *anjay, - avs_net_abstract_socket_t *ready_socket); +int anjay_serve(anjay_t *anjay, avs_net_abstract_socket_t *ready_socket); /** Object ID */ typedef uint16_t anjay_oid_t; @@ -386,37 +438,53 @@ typedef uint16_t anjay_riid_t; #define ANJAY_COAP_STATUS(Maj, Min) ((uint8_t) ((Maj << 5) | (Min & 0x1F))) /** Error values that may be returned from data model handlers. @{ */ -/** Request sent by the LwM2M Server was malformed or contained an invalid - * value. */ -#define ANJAY_ERR_BAD_REQUEST (-ANJAY_COAP_STATUS(4, 0)) -/** LwM2M Server is not allowed to perform the operation due to lack of - * necessary access rights. */ -#define ANJAY_ERR_UNAUTHORIZED (-ANJAY_COAP_STATUS(4, 1)) -/** Low-level CoAP error code; used internally by Anjay when CoAP option values - * were invalid. */ -#define ANJAY_ERR_BAD_OPTION (-ANJAY_COAP_STATUS(4, 2)) -#define ANJAY_ERR_FORBIDDEN (-ANJAY_COAP_STATUS(4, 3)) +/** + * Request sent by the LwM2M Server was malformed or contained an invalid + * value. + */ +#define ANJAY_ERR_BAD_REQUEST (-ANJAY_COAP_STATUS(4, 0)) +/** + * LwM2M Server is not allowed to perform the operation due to lack of + * necessary access rights. + */ +#define ANJAY_ERR_UNAUTHORIZED (-ANJAY_COAP_STATUS(4, 1)) +/** + * Low-level CoAP error code; used internally by Anjay when CoAP option values + * were invalid. + */ +#define ANJAY_ERR_BAD_OPTION (-ANJAY_COAP_STATUS(4, 2)) +#define ANJAY_ERR_FORBIDDEN (-ANJAY_COAP_STATUS(4, 3)) /** Target of the operation (Object/Instance/Resource) does not exist. */ -#define ANJAY_ERR_NOT_FOUND (-ANJAY_COAP_STATUS(4, 4)) -/** Operation is not allowed in current device state or the attempted operation - * is invalid for this target (Object/Instance/Resource) */ -#define ANJAY_ERR_METHOD_NOT_ALLOWED (-ANJAY_COAP_STATUS(4, 5)) -/** Low-level CoAP error code; used internally by Anjay when the client is - * unable to encode response in requested content format. */ -#define ANJAY_ERR_NOT_ACCEPTABLE (-ANJAY_COAP_STATUS(4, 6)) -/** Low-level CoAP error code; used internally by Anjay in case of unrecoverable - * problems during block-wise transfer. */ -#define ANJAY_ERR_REQUEST_ENTITY_INCOMPLETE (-ANJAY_COAP_STATUS(4, 8)) -/** The server requested operation has a Content Format option that is unsupported - * by Anjay. */ +#define ANJAY_ERR_NOT_FOUND (-ANJAY_COAP_STATUS(4, 4)) +/** + * Operation is not allowed in current device state or the attempted operation + * is invalid for this target (Object/Instance/Resource) + */ +#define ANJAY_ERR_METHOD_NOT_ALLOWED (-ANJAY_COAP_STATUS(4, 5)) +/** + * Low-level CoAP error code; used internally by Anjay when the client is + * unable to encode response in requested content format. + */ +#define ANJAY_ERR_NOT_ACCEPTABLE (-ANJAY_COAP_STATUS(4, 6)) +/** + * Low-level CoAP error code; used internally by Anjay in case of unrecoverable + * problems during block-wise transfer. + */ +#define ANJAY_ERR_REQUEST_ENTITY_INCOMPLETE (-ANJAY_COAP_STATUS(4, 8)) +/** + * The server requested operation has a Content Format option that is + * unsupported by Anjay. + */ #define ANJAY_ERR_UNSUPPORTED_CONTENT_FORMAT (-ANJAY_COAP_STATUS(4, 15)) /** Unspecified error, no other error code was suitable. */ -#define ANJAY_ERR_INTERNAL (-ANJAY_COAP_STATUS(5, 0)) +#define ANJAY_ERR_INTERNAL (-ANJAY_COAP_STATUS(5, 0)) /** Operation is not implemented by the LwM2M Client. */ -#define ANJAY_ERR_NOT_IMPLEMENTED (-ANJAY_COAP_STATUS(5, 1)) -/** LwM2M Client is busy processing some other request; LwM2M Server may retry - * sending the same request after some delay. */ -#define ANJAY_ERR_SERVICE_UNAVAILABLE (-ANJAY_COAP_STATUS(5, 3)) +#define ANJAY_ERR_NOT_IMPLEMENTED (-ANJAY_COAP_STATUS(5, 1)) +/** + * LwM2M Client is busy processing some other request; LwM2M Server may retry + * sending the same request after some delay. + */ +#define ANJAY_ERR_SERVICE_UNAVAILABLE (-ANJAY_COAP_STATUS(5, 3)) /** @} */ /** @@ -430,8 +498,7 @@ typedef uint16_t anjay_riid_t; * * @returns 0 on success, or a negative value if no tasks are scheduled. */ -int anjay_sched_time_to_next(anjay_t *anjay, - avs_time_duration_t *out_delay); +int anjay_sched_time_to_next(anjay_t *anjay, avs_time_duration_t *out_delay); /** * Determines time of next scheduled task in milliseconds. @@ -494,8 +561,7 @@ int anjay_sched_run(anjay_t *anjay); * * @returns 0 on success, a negative value in case of error. */ -int anjay_schedule_registration_update(anjay_t *anjay, - anjay_ssid_t ssid); +int anjay_schedule_registration_update(anjay_t *anjay, anjay_ssid_t ssid); /** * Reconnects sockets associated with all connected servers and ongoing @@ -572,8 +638,7 @@ int anjay_disable_server_with_timeout(anjay_t *anjay, * * @returns 0 on success, a negative value in case of error. */ -int anjay_enable_server(anjay_t *anjay, - anjay_ssid_t ssid); +int anjay_enable_server(anjay_t *anjay, anjay_ssid_t ssid); /** * Checks whether anjay is currently in offline state. diff --git a/include_public/anjay/dm.h b/include_public/anjay/dm.h index e8f2a717f..6673d9202 100644 --- a/include_public/anjay/dm.h +++ b/include_public/anjay/dm.h @@ -23,9 +23,9 @@ #include #ifdef __cplusplus -#if __cplusplus >= 201103L -#include // for ANJAY_DM_SUPPORTED_RIDS -#endif +# if __cplusplus >= 201103L +# include // for ANJAY_DM_SUPPORTED_RIDS +# endif extern "C" { #endif @@ -40,10 +40,14 @@ typedef struct { /** Resource attributes. */ typedef struct { - anjay_dm_attributes_t common; //< Attributes shared with Objects/Object Instances - double greater_than; //< Greater Than attribute as defined by LwM2M spec - double less_than; //< Less Than attribute as defined by LwM2M spec - double step; //< Step attribute as defined by LwM2M spec + /** Attributes shared with Objects/Object Instances */ + anjay_dm_attributes_t common; + /** Greater Than attribute as defined by LwM2M spec */ + double greater_than; + /** Less Than attribute as defined by LwM2M spec */ + double less_than; + /** Step attribute as defined by LwM2M spec */ + double step; } anjay_dm_resource_attributes_t; /** A value indicating that the Min/Max Period attribute is not set */ @@ -77,10 +81,11 @@ extern const anjay_dm_resource_attributes_t ANJAY_RES_ATTRIBS_EMPTY; * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_object_read_default_attrs_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - anjay_dm_attributes_t *out); +typedef int anjay_dm_object_read_default_attrs_t( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + anjay_dm_attributes_t *out); /** * A handler that sets default attribute values for the Object. @@ -98,10 +103,11 @@ typedef int anjay_dm_object_read_default_attrs_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_object_write_default_attrs_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - const anjay_dm_attributes_t *attrs); +typedef int anjay_dm_object_write_default_attrs_t( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + const anjay_dm_attributes_t *attrs); /** * A handler that enumerates all Object Instances for the Object. @@ -186,9 +192,10 @@ int anjay_dm_instance_it_SINGLE(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_instance_present_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid); +typedef int +anjay_dm_instance_present_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid); /** * Convenience function to use as the instance_present handler in Single @@ -196,9 +203,10 @@ typedef int anjay_dm_instance_present_t(anjay_t *anjay, * * @returns 1 (true) if iid == 0, 0 (false) otherwise. */ -int anjay_dm_instance_present_SINGLE(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid); +int anjay_dm_instance_present_SINGLE( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid); /** * A handler that shall reset Object Instance to its default (after creational) @@ -219,9 +227,10 @@ int anjay_dm_instance_present_SINGLE(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_instance_reset_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid); +typedef int +anjay_dm_instance_reset_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid); /** * A handler that removes an Object Instance with given Instance ID. @@ -242,9 +251,10 @@ typedef int anjay_dm_instance_reset_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_instance_remove_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid); +typedef int +anjay_dm_instance_remove_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid); /** * A handler that creates an Object Instance. @@ -271,10 +281,11 @@ typedef int anjay_dm_instance_remove_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_instance_create_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t *inout_iid, - anjay_ssid_t ssid); +typedef int +anjay_dm_instance_create_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t *inout_iid, + anjay_ssid_t ssid); /** * A handler that returns default attributes set for the Object Instance. @@ -293,11 +304,12 @@ typedef int anjay_dm_instance_create_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_instance_read_default_attrs_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - anjay_dm_attributes_t *out); +typedef int anjay_dm_instance_read_default_attrs_t( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + anjay_dm_attributes_t *out); /** * A handler that sets default attributes for the Object Instance. @@ -316,11 +328,12 @@ typedef int anjay_dm_instance_read_default_attrs_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_instance_write_default_attrs_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - const anjay_dm_attributes_t *attrs); +typedef int anjay_dm_instance_write_default_attrs_t( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + const anjay_dm_attributes_t *attrs); /** * A handler that checks if a Resource has been instantiated in Object Instance, @@ -340,10 +353,11 @@ typedef int anjay_dm_instance_write_default_attrs_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_resource_present_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid); +typedef int +anjay_dm_resource_present_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid); /** * Convenience function to use as the resource_present handler in objects that @@ -420,11 +434,12 @@ anjay_dm_resource_operations_t(anjay_t *anjay, * ANJAY_ERR_ constant, the normal fallback response is * 5.00 Internal Server Error. */ -typedef int anjay_dm_resource_read_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_output_ctx_t *ctx); +typedef int +anjay_dm_resource_read_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_output_ctx_t *ctx); /** * A handler that writes the Resource value. @@ -444,11 +459,12 @@ typedef int anjay_dm_resource_read_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_resource_write_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_input_ctx_t *ctx); +typedef int +anjay_dm_resource_write_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_input_ctx_t *ctx); /** * A handler that performs the Execute action on given Resource. @@ -458,8 +474,8 @@ typedef int anjay_dm_resource_write_t(anjay_t *anjay, * @ref anjay_register_object . * @param iid Object Instance ID. * @param rid Resource ID. - * @param ctx Execute context to read the execution arguments from, using the - * anjay_execute_get_* function family. + * @param ctx Execute context to read the execution arguments from, using + * the anjay_execute_get_* function family. * * @returns This handler should return: * - 0 on success, @@ -468,11 +484,12 @@ typedef int anjay_dm_resource_write_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_resource_execute_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_execute_ctx_t *ctx); +typedef int +anjay_dm_resource_execute_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_execute_ctx_t *ctx); #define ANJAY_DM_DIM_INVALID ANJAY_ERR_NOT_IMPLEMENTED @@ -518,12 +535,13 @@ typedef int anjay_dm_resource_dim_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_resource_read_attrs_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_ssid_t ssid, - anjay_dm_resource_attributes_t *out); +typedef int +anjay_dm_resource_read_attrs_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_ssid_t ssid, + anjay_dm_resource_attributes_t *out); /** * A handler that sets attributes for given Resource. @@ -543,12 +561,13 @@ typedef int anjay_dm_resource_read_attrs_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_resource_write_attrs_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_ssid_t ssid, - const anjay_dm_resource_attributes_t *attrs); +typedef int +anjay_dm_resource_write_attrs_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_ssid_t ssid, + const anjay_dm_resource_attributes_t *attrs); /** * A handler that is called when there is a request that might modify an Object @@ -577,8 +596,9 @@ typedef int anjay_dm_resource_write_attrs_t(anjay_t *anjay, * - 0 on success * - a negative value in case of error */ -typedef int anjay_dm_transaction_begin_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr); +typedef int +anjay_dm_transaction_begin_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr); /** * A handler that is called after transaction is finished, but before @@ -606,8 +626,9 @@ typedef int anjay_dm_transaction_begin_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_transaction_validate_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr); +typedef int +anjay_dm_transaction_validate_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr); /** * A handler that is called after transaction is finished. If it fails then @@ -630,8 +651,9 @@ typedef int anjay_dm_transaction_validate_t(anjay_t *anjay, * code. Otherwise, the device will respond with an unspecified (but valid) * error code. */ -typedef int anjay_dm_transaction_commit_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr); +typedef int +anjay_dm_transaction_commit_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr); /** * Stub handler that can be substituted for any transaction operation. Does @@ -653,14 +675,20 @@ int anjay_dm_transaction_NOOP(anjay_t *anjay, * - 0 on success * - a negative value in case of error. */ -typedef int anjay_dm_transaction_rollback_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr); +typedef int +anjay_dm_transaction_rollback_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr); /** A struct containing pointers to Object handlers. */ typedef struct { - /** Get default Object attributes, @ref anjay_dm_object_read_default_attrs_t */ + /** + * Get default Object attributes, @ref anjay_dm_object_read_default_attrs_t + */ anjay_dm_object_read_default_attrs_t *object_read_default_attrs; - /** Set default Object attributes, @ref anjay_dm_object_write_default_attrs_t */ + /** + * Set default Object attributes, + * @ref anjay_dm_object_write_default_attrs_t + */ anjay_dm_object_write_default_attrs_t *object_write_default_attrs; /** Enumerate available Object Instances, @ref anjay_dm_instance_it_t */ @@ -675,24 +703,40 @@ typedef struct { /** Delete an Object Instance, @ref anjay_dm_instance_remove_t */ anjay_dm_instance_remove_t *instance_remove; - /** Get default Object Instance attributes, @ref anjay_dm_instance_read_default_attrs_t */ + /** + * Get default Object Instance attributes, + * @ref anjay_dm_instance_read_default_attrs_t + */ anjay_dm_instance_read_default_attrs_t *instance_read_default_attrs; - /** Set default Object Instance attributes, @ref anjay_dm_instance_write_default_attrs_t */ + /** + * Set default Object Instance attributes, + * @ref anjay_dm_instance_write_default_attrs_t + */ anjay_dm_instance_write_default_attrs_t *instance_write_default_attrs; - /** Check if a Resource is present in given Object Instance, @ref anjay_dm_resource_present_t */ + /** + * Check if a Resource is present in given Object Instance, + * @ref anjay_dm_resource_present_t + */ anjay_dm_resource_present_t *resource_present; - /** Returns a mask of supported operations on a given Resource, @ref anjay_dm_resource_operations_t */ + /** + * Returns a mask of supported operations on a given Resource, + * @ref anjay_dm_resource_operations_t + */ anjay_dm_resource_operations_t *resource_operations; /** Get Resource value, @ref anjay_dm_resource_read_t */ anjay_dm_resource_read_t *resource_read; /** Set Resource value, @ref anjay_dm_resource_write_t */ anjay_dm_resource_write_t *resource_write; - /** Perform Execute action on a Resource, @ref anjay_dm_resource_execute_t */ + /** + * Perform Execute action on a Resource, @ref anjay_dm_resource_execute_t + */ anjay_dm_resource_execute_t *resource_execute; - /** Get number of Multiple Resource instances, @ref anjay_dm_resource_dim_t */ + /** + * Get number of Multiple Resource instances, @ref anjay_dm_resource_dim_t + */ anjay_dm_resource_dim_t *resource_dim; /** Get Resource attributes, @ref anjay_dm_resource_read_attrs_t */ anjay_dm_resource_read_attrs_t *resource_read_attrs; @@ -701,11 +745,19 @@ typedef struct { /** Begin a transaction on this Object, @ref anjay_dm_transaction_begin_t */ anjay_dm_transaction_begin_t *transaction_begin; - /** Validate whether a transaction on this Object can be cleanly committed. See @ref anjay_dm_transaction_validate_t */ + /** + * Validate whether a transaction on this Object can be cleanly committed. + * See @ref anjay_dm_transaction_validate_t + */ anjay_dm_transaction_validate_t *transaction_validate; - /** Commit changes made in a transaction, @ref anjay_dm_transaction_commit_t */ + /** + * Commit changes made in a transaction, @ref anjay_dm_transaction_commit_t + */ anjay_dm_transaction_commit_t *transaction_commit; - /** Rollback changes made in a transaction, @ref anjay_dm_transaction_rollback_t */ + /** + * Rollback changes made in a transaction, + * @ref anjay_dm_transaction_rollback_t + */ anjay_dm_transaction_rollback_t *transaction_rollback; } anjay_dm_handlers_t; @@ -713,22 +765,25 @@ typedef struct { typedef struct { /** Number of element in the array */ size_t count; - /** Pointer to an array of Resource IDs supported by the object. A Resource + /** + * Pointer to an array of Resource IDs supported by the object. A Resource * is considered SUPPORTED if it may ever be present within the Object. The * array MUST be exactly count elements long and sorted in strictly - * ascending order. */ + * ascending order. + */ const uint16_t *rids; } anjay_dm_supported_rids_t; #if defined(__cplusplus) && __cplusplus >= 201103L -#define ANJAY_DM_SUPPORTED_RIDS(...) \ - { \ - ::std::tuple_size::value,\ - []() -> const uint16_t * { \ - static const uint16_t rids[] = { __VA_ARGS__ }; \ - return rids; \ - }() \ - } +# define ANJAY_DM_SUPPORTED_RIDS(...) \ + { \ + ::std::tuple_size::value, \ + []() -> const uint16_t * { \ + static const uint16_t rids[] = { __VA_ARGS__ }; \ + return rids; \ + }() \ + } #else // __cplusplus /** * Convenience macro for initializing @ref anjay_dm_supported_rids_t objects. @@ -739,10 +794,12 @@ typedef struct { * supported_rids field of @ref anjay_dm_object_def_t. The count * field will be automatically calculated. */ -#define ANJAY_DM_SUPPORTED_RIDS(...) \ - { \ +# define ANJAY_DM_SUPPORTED_RIDS(...) \ + { \ sizeof((const uint16_t[]) { __VA_ARGS__ }) / sizeof(uint16_t), \ - (const uint16_t[]) { __VA_ARGS__ } \ + (const uint16_t[]) { \ + __VA_ARGS__ \ + } \ } #endif // __cplusplus @@ -751,15 +808,19 @@ struct anjay_dm_object_def_struct { /** Object ID */ anjay_oid_t oid; - /** Object version: a string with static lifetime, containing two digits + /** + * Object version: a string with static lifetime, containing two digits * separated by a dot (for example: "1.1"). * If left NULL, client will not include the "ver=" attribute in Register - * and Discover messages, which implies version 1.0. */ + * and Discover messages, which implies version 1.0. + */ const char *version; - /** List of Resource IDs supported by the object. The + /** + * List of Resource IDs supported by the object. The * @ref ANJAY_DM_SUPPORTED_RIDS macro is the preferred way of initializing - * it. */ + * it. + */ anjay_dm_supported_rids_t supported_rids; /** Handler callbacks for this object. */ @@ -859,10 +920,10 @@ bool anjay_binding_mode_valid(const char *binding_mode); * Object definition. */ typedef enum { - ANJAY_UDP_SECURITY_PSK = 0, //< Pre-Shared Key mode - ANJAY_UDP_SECURITY_RPK = 1, //< Raw Public Key mode + ANJAY_UDP_SECURITY_PSK = 0, //< Pre-Shared Key mode + ANJAY_UDP_SECURITY_RPK = 1, //< Raw Public Key mode ANJAY_UDP_SECURITY_CERTIFICATE = 2, //< Certificate mode - ANJAY_UDP_SECURITY_NOSEC = 3 //< NoSec mode + ANJAY_UDP_SECURITY_NOSEC = 3 //< NoSec mode } anjay_udp_security_mode_t; /** @@ -870,22 +931,25 @@ typedef enum { * Security Object definition. */ typedef enum { - ANJAY_SMS_SECURITY_DTLS_PSK = 1, //< DTLS in PSK mode + ANJAY_SMS_SECURITY_DTLS_PSK = 1, //< DTLS in PSK mode ANJAY_SMS_SECURITY_SECURE_PACKET = 2, //< Secure Packet Structure - ANJAY_SMS_SECURITY_NOSEC = 3 //< NoSec mode + ANJAY_SMS_SECURITY_NOSEC = 3 //< NoSec mode } anjay_sms_security_mode_t; -#define ANJAY_ACCESS_MASK_READ (1U << 0) -#define ANJAY_ACCESS_MASK_WRITE (1U << 1) -#define ANJAY_ACCESS_MASK_EXECUTE (1U << 2) -#define ANJAY_ACCESS_MASK_DELETE (1U << 3) -#define ANJAY_ACCESS_MASK_CREATE (1U << 4) -#define ANJAY_ACCESS_MASK_FULL (ANJAY_ACCESS_MASK_READ | \ - ANJAY_ACCESS_MASK_WRITE | \ - ANJAY_ACCESS_MASK_DELETE | \ - ANJAY_ACCESS_MASK_EXECUTE | \ - ANJAY_ACCESS_MASK_CREATE) -#define ANJAY_ACCESS_MASK_NONE 0 +#define ANJAY_ACCESS_MASK_READ (1U << 0) +#define ANJAY_ACCESS_MASK_WRITE (1U << 1) +#define ANJAY_ACCESS_MASK_EXECUTE (1U << 2) +#define ANJAY_ACCESS_MASK_DELETE (1U << 3) +#define ANJAY_ACCESS_MASK_CREATE (1U << 4) +// clang-format off +#define ANJAY_ACCESS_MASK_FULL \ + (ANJAY_ACCESS_MASK_READ \ + | ANJAY_ACCESS_MASK_WRITE \ + | ANJAY_ACCESS_MASK_DELETE \ + | ANJAY_ACCESS_MASK_EXECUTE \ + | ANJAY_ACCESS_MASK_CREATE) +// clang-format on +#define ANJAY_ACCESS_MASK_NONE 0 #define ANJAY_ACCESS_LIST_OWNER_BOOTSTRAP UINT16_MAX typedef uint16_t anjay_access_mask_t; diff --git a/include_public/anjay/download.h b/include_public/anjay/download.h index 9ab2905de..506038a15 100644 --- a/include_public/anjay/download.h +++ b/include_public/anjay/download.h @@ -105,9 +105,8 @@ typedef enum anjay_download_result { * @param user_data Value of @ref anjay_download_config_t#user_data passed * to @ref anjay_download . */ -typedef void anjay_download_finished_handler_t(anjay_t *anjay, - int result, - void *user_data); +typedef void +anjay_download_finished_handler_t(anjay_t *anjay, int result, void *user_data); typedef struct anjay_download_config { /** Required. coap:// or coaps:// URL */ @@ -140,6 +139,12 @@ typedef struct anjay_download_config { * ignored for coap:// transfers. */ avs_net_security_info_t security_info; + + /** + * Pointer to CoAP transmission parameters object. If NULL, downloader will + * inherit parameters from Anjay. + */ + avs_coap_tx_params_t *coap_tx_params; } anjay_download_config_t; typedef void *anjay_download_handle_t; @@ -177,8 +182,7 @@ anjay_download_handle_t anjay_download(anjay_t *anjay, * @param dl_handle Download handle previously returned by * @ref anjay_download. */ -void anjay_download_abort(anjay_t *anjay, - anjay_download_handle_t dl_handle); +void anjay_download_abort(anjay_t *anjay, anjay_download_handle_t dl_handle); #ifdef __cplusplus } /* extern "C" */ diff --git a/include_public/anjay/io.h b/include_public/anjay/io.h index ae527bfd2..436b75b4b 100644 --- a/include_public/anjay/io.h +++ b/include_public/anjay/io.h @@ -17,7 +17,6 @@ #ifndef ANJAY_INCLUDE_ANJAY_IO_H #define ANJAY_INCLUDE_ANJAY_IO_H - #include #ifdef __cplusplus @@ -353,9 +352,9 @@ int anjay_get_bytes(anjay_input_ctx_t *ctx, * @param[out] out_buf Buffer to read data into. * @param buf_size Number of bytes available in @p out_buf . * - * @returns 0 on success, a negative value in case of error, ANJAY_BUFFER_TOO_SHORT - * if the buffer is not big enough to contain whole message content + - * terminating nullbyte. + * @returns 0 on success, a negative value in case of error, + * ANJAY_BUFFER_TOO_SHORT if the buffer is not big enough to contain + * whole message content + terminating nullbyte. */ int anjay_get_string(anjay_input_ctx_t *ctx, char *out_buf, size_t buf_size); @@ -428,7 +427,8 @@ int anjay_get_bool(anjay_input_ctx_t *ctx, bool *out); * untouched. */ int anjay_get_objlnk(anjay_input_ctx_t *ctx, - anjay_oid_t *out_oid, anjay_iid_t *out_iid); + anjay_oid_t *out_oid, + anjay_iid_t *out_iid); /** * Begins reading an array of values (also known as Multiple Resource Instances) diff --git a/include_public/anjay/persistence.h b/include_public/anjay/persistence.h index 4179374ed..892a531a6 100644 --- a/include_public/anjay/persistence.h +++ b/include_public/anjay/persistence.h @@ -24,19 +24,21 @@ extern "C" { #endif #ifdef __GNUC__ -#warning " is deprecated. Please use instead." +# warning " is deprecated. Please use " \ + " instead." #endif typedef avs_persistence_context_t anjay_persistence_context_t; typedef avs_persistence_handler_collection_element_t -anjay_persistence_handler_collection_element_t; + anjay_persistence_handler_collection_element_t; typedef avs_persistence_cleanup_collection_element_t -anjay_persistence_cleanup_collection_element_t; + anjay_persistence_cleanup_collection_element_t; #define anjay_persistence_store_context_new avs_persistence_store_context_new -#define anjay_persistence_restore_context_new avs_persistence_restore_context_new +#define anjay_persistence_restore_context_new \ + avs_persistence_restore_context_new #define anjay_persistence_ignore_context_new avs_persistence_ignore_context_new #define anjay_persistence_context_delete avs_persistence_context_delete diff --git a/modules/access_control/src/access_control_handlers.c b/modules/access_control/src/access_control_handlers.c index 27820d692..0103bbae2 100644 --- a/modules/access_control/src/access_control_handlers.c +++ b/modules/access_control/src/access_control_handlers.c @@ -53,7 +53,7 @@ static int ac_instance_it(anjay_t *anjay, return ANJAY_ERR_INTERNAL; } AVS_LIST(access_control_instance_t) curr = - (AVS_LIST(access_control_instance_t)) *cookie; + (AVS_LIST(access_control_instance_t)) *cookie; if (!curr) { curr = access_control->current.instances; @@ -66,17 +66,15 @@ static int ac_instance_it(anjay_t *anjay, return 0; } -static int ac_instance_present(anjay_t *anjay, - obj_ptr_t obj_ptr, - anjay_iid_t iid) { +static int +ac_instance_present(anjay_t *anjay, obj_ptr_t obj_ptr, anjay_iid_t iid) { (void) anjay; - return find_instance(_anjay_access_control_from_obj_ptr(obj_ptr), - iid) != NULL; + return find_instance(_anjay_access_control_from_obj_ptr(obj_ptr), iid) + != NULL; } -static int ac_instance_reset(anjay_t *anjay, - obj_ptr_t obj_ptr, - anjay_iid_t iid) { +static int +ac_instance_reset(anjay_t *anjay, obj_ptr_t obj_ptr, anjay_iid_t iid) { (void) anjay; access_control_t *access_control = _anjay_access_control_from_obj_ptr(obj_ptr); @@ -111,9 +109,9 @@ static int ac_instance_create(anjay_t *anjay, .oid = 0, .iid = -1 }, - .owner = ssid, + .owner = ssid, .has_acl = false, - .acl = NULL + .acl = NULL }; int retval = _anjay_access_control_add_instance(access_control, new_instance, NULL); @@ -209,21 +207,20 @@ static int ac_resource_read(anjay_t *anjay, return anjay_ret_i32(ctx, (int32_t) inst->target.oid); case ANJAY_DM_RID_ACCESS_CONTROL_OIID: return anjay_ret_i32(ctx, (int32_t) inst->target.iid); - case ANJAY_DM_RID_ACCESS_CONTROL_ACL: - { - anjay_output_ctx_t *array = anjay_ret_array_start(ctx); - if (!array) { - return ANJAY_ERR_INTERNAL; - } - acl_entry_t *it; - AVS_LIST_FOREACH(it, inst->acl) { - if (anjay_ret_array_index(array, it->ssid) + case ANJAY_DM_RID_ACCESS_CONTROL_ACL: { + anjay_output_ctx_t *array = anjay_ret_array_start(ctx); + if (!array) { + return ANJAY_ERR_INTERNAL; + } + acl_entry_t *it; + AVS_LIST_FOREACH(it, inst->acl) { + if (anjay_ret_array_index(array, it->ssid) || anjay_ret_i32(array, it->mask)) { - return ANJAY_ERR_INTERNAL; - } + return ANJAY_ERR_INTERNAL; } - return anjay_ret_array_finish(array); } + return anjay_ret_array_finish(array); + } case ANJAY_DM_RID_ACCESS_CONTROL_OWNER: return anjay_ret_i32(ctx, (int32_t) inst->owner); default: @@ -250,8 +247,7 @@ static int write_to_acl_array(AVS_LIST(acl_entry_t) *acl, } if (!*it) { - AVS_LIST(acl_entry_t) new_entry = - AVS_LIST_NEW_ELEMENT(acl_entry_t); + AVS_LIST(acl_entry_t) new_entry = AVS_LIST_NEW_ELEMENT(acl_entry_t); if (!new_entry) { return ANJAY_ERR_INTERNAL; } @@ -356,14 +352,14 @@ static void what_changed(anjay_ssid_t origin_ssid, if (!it->instance_set_changes.instance_set_changed) { continue; } - if (it->oid == ANJAY_DM_OID_SECURITY - || it->oid == ANJAY_DM_OID_SERVER + if (it->oid == ANJAY_DM_OID_SECURITY || it->oid == ANJAY_DM_OID_SERVER || it->oid == ANJAY_DM_OID_ACCESS_CONTROL) { *out_might_caused_orphaned_ac_instances = true; } // NOTE: This makes it possible for BOOTSTRAP DELETE to leave // "lingering" Access Control instances without valid targets; - // Relevant: https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/192 + // Relevant: + // https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues/192 // // Quoting Thierry's response: // > Regarding the orphan AC Object Instances, [...] it could be let @@ -377,7 +373,7 @@ static void what_changed(anjay_ssid_t origin_ssid, if (origin_ssid != ANJAY_SSID_BOOTSTRAP && it->oid != ANJAY_DM_OID_ACCESS_CONTROL && (it->instance_set_changes.known_removed_iids - || it->instance_set_changes.known_added_iids)) { + || it->instance_set_changes.known_added_iids)) { *out_have_adds_or_removes = true; } if (*out_might_caused_orphaned_ac_instances @@ -401,8 +397,8 @@ static int remove_ac_instance_by_target(anjay_t *anjay, && (*it)->target.iid == target_iid) { if (_anjay_dm_transaction_include_object(anjay, &ac->obj_def) || _anjay_notify_queue_instance_removed( - notify_queue, - ANJAY_DM_OID_ACCESS_CONTROL, (*it)->iid)) { + notify_queue, ANJAY_DM_OID_ACCESS_CONTROL, + (*it)->iid)) { return -1; } AVS_LIST_CLEAR(&(*it)->acl); @@ -417,13 +413,13 @@ static int perform_adds_and_removes(anjay_t *anjay, anjay_notify_queue_t incoming_queue, anjay_notify_queue_t *local_queue_ptr) { assert(_anjay_dm_find_object_by_oid(anjay, ANJAY_DM_OID_ACCESS_CONTROL) - == &ac->obj_def); + == &ac->obj_def); const anjay_ssid_t origin_ssid = _anjay_dm_current_ssid(anjay); AVS_LIST(access_control_instance_t) acs_to_insert = NULL; - AVS_LIST(access_control_instance_t) *acs_to_insert_append_ptr - = &acs_to_insert; + AVS_LIST(access_control_instance_t) *acs_to_insert_append_ptr = + &acs_to_insert; AVS_LIST(anjay_notify_queue_object_entry_t) it; AVS_LIST_FOREACH(it, incoming_queue) { @@ -459,8 +455,9 @@ static int perform_adds_and_removes(anjay_t *anjay, AVS_LIST_ADVANCE_PTR(&acs_to_insert_append_ptr); } } - int result = _anjay_access_control_add_instances_without_iids( - ac, &acs_to_insert, local_queue_ptr); + int result = + _anjay_access_control_add_instances_without_iids(ac, &acs_to_insert, + local_queue_ptr); AVS_LIST_CLEAR(&acs_to_insert) { AVS_LIST_CLEAR(&acs_to_insert->acl); } @@ -492,8 +489,8 @@ static int sync_on_notify(anjay_t *anjay, &local_queue); } if (!result && have_adds_or_removes) { - result = perform_adds_and_removes(anjay, ac, - incoming_queue, &local_queue); + result = perform_adds_and_removes(anjay, ac, incoming_queue, + &local_queue); } if (!result) { result = _anjay_notify_flush(anjay, &local_queue); @@ -528,19 +525,21 @@ static int acl_target_cmp(const void *left_, const void *right_) { static int validate_inst_ref(anjay_t *anjay, AVS_RBTREE(acl_target_t) encountered_refs, const acl_target_t *target) { - ac_log(TRACE, "Validating: /%" PRIu16 "/%" PRId32, - target->oid, target->iid); + ac_log(TRACE, "Validating: /%" PRIu16 "/%" PRId32, target->oid, + target->iid); if (!_anjay_access_control_target_oid_valid(target->oid) || !_anjay_access_control_target_iid_valid(target->iid)) { - ac_log(ERROR, "Validation failed: invalid target: " - "/%" PRIu16 "/%" PRId32 ": invalid IDs", + ac_log(ERROR, + "Validation failed: invalid target: /%" PRIu16 "/%" PRId32 + ": invalid IDs", target->oid, target->iid); return -1; } obj_ptr_t obj = _anjay_dm_find_object_by_oid(anjay, target->oid); if (!obj) { - ac_log(ERROR, "Validation failed: invalid target: " - "/%" PRIu16 "/%" PRId32 ": no such object", + ac_log(ERROR, + "Validation failed: invalid target: /%" PRIu16 "/%" PRId32 + ": no such object", target->oid, target->iid); return -1; } @@ -553,16 +552,18 @@ static int validate_inst_ref(anjay_t *anjay, if (AVS_RBTREE_INSERT(encountered_refs, ref) != ref) { // duplicate AVS_RBTREE_ELEM_DELETE_DETACHED(&ref); - ac_log(ERROR, "Validation failed: duplicate target: " - "/%" PRIu16 "/%" PRId32, target->oid, target->iid); + ac_log(ERROR, + "Validation failed: duplicate target: /%" PRIu16 "/%" PRId32, + target->oid, target->iid); return -1; } if (target->iid != ANJAY_IID_INVALID // ACL targeting object are always OK - && _anjay_dm_instance_present(anjay, obj, - (anjay_iid_t) target->iid, - NULL) <= 0) { - ac_log(ERROR, "Validation failed: invalid target: " - "/%" PRIu16 "/%" PRId32 ": no such instance", + && _anjay_dm_instance_present(anjay, obj, (anjay_iid_t) target->iid, + NULL) + <= 0) { + ac_log(ERROR, + "Validation failed: invalid target: /%" PRIu16 "/%" PRId32 + ": no such instance", target->oid, target->iid); return -1; } @@ -596,13 +597,12 @@ static int add_ssid(AVS_RBTREE(anjay_ssid_t) ssids_list, anjay_ssid_t ssid) { */ int _anjay_access_control_validate_ssid(anjay_t *anjay, anjay_ssid_t ssid) { return (ssid != ANJAY_SSID_BOOTSTRAP - && (ssid == ANJAY_SSID_ANY - || _anjay_dm_ssid_exists(anjay, ssid))) - ? 0 : -1; + && (ssid == ANJAY_SSID_ANY || _anjay_dm_ssid_exists(anjay, ssid))) + ? 0 + : -1; } -static int -ac_transaction_validate(anjay_t *anjay, obj_ptr_t obj_ptr) { +static int ac_transaction_validate(anjay_t *anjay, obj_ptr_t obj_ptr) { access_control_t *access_control = _anjay_access_control_from_obj_ptr(obj_ptr); int result = 0; @@ -610,8 +610,8 @@ ac_transaction_validate(anjay_t *anjay, obj_ptr_t obj_ptr) { AVS_RBTREE(anjay_ssid_t) ssids_used = NULL; if (access_control->needs_validation) { if (!(encountered_refs = AVS_RBTREE_NEW(acl_target_t, acl_target_cmp)) - || !(ssids_used = AVS_RBTREE_NEW(anjay_ssid_t, - anjay_ssid_cmp))) { + || !(ssids_used = + AVS_RBTREE_NEW(anjay_ssid_t, anjay_ssid_cmp))) { ac_log(ERROR, "Out of memory"); goto finish; } @@ -620,7 +620,7 @@ ac_transaction_validate(anjay_t *anjay, obj_ptr_t obj_ptr) { AVS_LIST_FOREACH(inst, access_control->current.instances) { if (validate_inst_ref(anjay, encountered_refs, &inst->target) || (inst->owner != ANJAY_SSID_BOOTSTRAP - && add_ssid(ssids_used, inst->owner))) { + && add_ssid(ssids_used, inst->owner))) { goto finish; } acl_entry_t *acl; @@ -666,8 +666,7 @@ static int ac_transaction_rollback(anjay_t *anjay, obj_ptr_t obj_ptr) { static void ac_delete(anjay_t *anjay, void *access_control_) { (void) anjay; - access_control_t *access_control = - (access_control_t *) access_control_; + access_control_t *access_control = (access_control_t *) access_control_; _anjay_access_control_clear_state(&access_control->current); _anjay_access_control_clear_state(&access_control->saved_state); avs_free(access_control); @@ -680,8 +679,8 @@ void anjay_access_control_purge(anjay_t *anjay) { _anjay_access_control_mark_modified(ac); ac->needs_validation = false; if (anjay_notify_instances_changed(anjay, ANJAY_DM_OID_ACCESS_CONTROL)) { - ac_log(WARNING, "Could not schedule access control instance " - "changes notifications"); + ac_log(WARNING, "Could not schedule access control instance changes " + "notifications"); } } @@ -697,11 +696,11 @@ static const anjay_dm_module_t ACCESS_CONTROL_MODULE = { static const anjay_dm_object_def_t ACCESS_CONTROL = { .oid = ANJAY_DM_OID_ACCESS_CONTROL, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - ANJAY_DM_RID_ACCESS_CONTROL_OID, - ANJAY_DM_RID_ACCESS_CONTROL_OIID, - ANJAY_DM_RID_ACCESS_CONTROL_ACL, - ANJAY_DM_RID_ACCESS_CONTROL_OWNER), + .supported_rids = + ANJAY_DM_SUPPORTED_RIDS(ANJAY_DM_RID_ACCESS_CONTROL_OID, + ANJAY_DM_RID_ACCESS_CONTROL_OIID, + ANJAY_DM_RID_ACCESS_CONTROL_ACL, + ANJAY_DM_RID_ACCESS_CONTROL_OWNER), .handlers = { .instance_it = ac_instance_it, .instance_present = ac_instance_present, @@ -725,7 +724,7 @@ int anjay_access_control_install(anjay_t *anjay) { return -1; } access_control_t *access_control = - (access_control_t *) avs_calloc(1, sizeof(access_control_t)); + (access_control_t *) avs_calloc(1, sizeof(access_control_t)); if (!access_control) { return -1; } @@ -746,6 +745,6 @@ int anjay_access_control_install(anjay_t *anjay) { } access_control_t *_anjay_access_control_get(anjay_t *anjay) { - return (access_control_t *) - _anjay_dm_module_get_arg(anjay, &ACCESS_CONTROL_MODULE); + return (access_control_t *) _anjay_dm_module_get_arg( + anjay, &ACCESS_CONTROL_MODULE); } diff --git a/modules/access_control/src/access_control_persistence.c b/modules/access_control/src/access_control_persistence.c index 50ae85afe..f2a41c94c 100644 --- a/modules/access_control/src/access_control_persistence.c +++ b/modules/access_control/src/access_control_persistence.c @@ -17,7 +17,7 @@ #include #ifdef WITH_AVS_PERSISTENCE -#include +# include #endif // WITH_AVS_PERSISTENCE #include @@ -37,7 +37,7 @@ static int handle_acl_entry(avs_persistence_context_t *ctx, acl_entry_t *element = (acl_entry_t *) element_; int retval; (void) ((retval = avs_persistence_u16(ctx, &element->mask)) - || (retval = avs_persistence_u16(ctx, &element->ssid))); + || (retval = avs_persistence_u16(ctx, &element->ssid))); return retval; } @@ -68,16 +68,16 @@ static int persist_instance(avs_persistence_context_t *ctx, } int retval; (void) ((retval = avs_persistence_u16(ctx, &element->target.oid)) - || (retval = avs_persistence_u16(ctx, &element->iid)) - || (retval = avs_persistence_u16(ctx, &target_iid)) - || (retval = avs_persistence_u16(ctx, &element->owner)) - || (retval = handle_acl(ctx, element))); + || (retval = avs_persistence_u16(ctx, &element->iid)) + || (retval = avs_persistence_u16(ctx, &target_iid)) + || (retval = avs_persistence_u16(ctx, &element->owner)) + || (retval = handle_acl(ctx, element))); return retval; } static bool is_object_registered(anjay_t *anjay, anjay_oid_t oid) { return oid != ANJAY_DM_OID_SECURITY - && _anjay_dm_find_object_by_oid(anjay, oid) != NULL; + && _anjay_dm_find_object_by_oid(anjay, oid) != NULL; } static int restore_instance(access_control_instance_t *out_instance, @@ -134,9 +134,8 @@ static int restore_instances(anjay_t *anjay, return 0; } -static int restore(anjay_t *anjay, - access_control_t *ac, - avs_stream_abstract_t *in) { +static int +restore(anjay_t *anjay, access_control_t *ac, avs_stream_abstract_t *in) { avs_persistence_context_t *restore_ctx = avs_persistence_restore_context_new(in); avs_persistence_context_t *ignore_ctx = @@ -147,8 +146,8 @@ static int restore(anjay_t *anjay, goto finish; } - if ((retval = restore_instances(anjay, &state.instances, - restore_ctx, ignore_ctx))) { + if ((retval = restore_instances(anjay, &state.instances, restore_ctx, + ignore_ctx))) { _anjay_access_control_clear_state(&state); goto finish; } @@ -162,8 +161,7 @@ static int restore(anjay_t *anjay, static const char MAGIC[] = { 'A', 'C', 'O', '\1' }; -int anjay_access_control_persist(anjay_t *anjay, - avs_stream_abstract_t *out) { +int anjay_access_control_persist(anjay_t *anjay, avs_stream_abstract_t *out) { access_control_t *ac = _anjay_access_control_get(anjay); if (!ac) { ac_log(ERROR, "Access Control not installed in this Anjay object"); @@ -179,10 +177,10 @@ int anjay_access_control_persist(anjay_t *anjay, ac_log(ERROR, "Out of memory"); return -1; } - retval = avs_persistence_list(ctx, - (AVS_LIST(void) *) &ac->current.instances, - sizeof(*ac->current.instances), - persist_instance, NULL, NULL); + retval = + avs_persistence_list(ctx, (AVS_LIST(void) *) &ac->current.instances, + sizeof(*ac->current.instances), + persist_instance, NULL, NULL); if (!retval) { ac_log(INFO, "Access Control state persisted"); _anjay_access_control_clear_modified(ac); @@ -199,8 +197,8 @@ int anjay_access_control_restore(anjay_t *anjay, avs_stream_abstract_t *in) { } char magic_header[sizeof(MAGIC)]; - int retval = avs_stream_read_reliably(in, - magic_header, sizeof(magic_header)); + int retval = + avs_stream_read_reliably(in, magic_header, sizeof(magic_header)); if (retval) { ac_log(ERROR, "magic constant not found"); return retval; @@ -217,21 +215,22 @@ int anjay_access_control_restore(anjay_t *anjay, avs_stream_abstract_t *in) { return retval; } -#ifdef ANJAY_TEST -#include "test/persistence.c" -#endif // ANJAY_TEST +# ifdef ANJAY_TEST +# include "test/persistence.c" +# endif // ANJAY_TEST #else // WITH_AVS_PERSISTENCE -int anjay_access_control_persist(anjay_t *anjay, - avs_stream_abstract_t *out) { - (void) anjay; (void) out; +int anjay_access_control_persist(anjay_t *anjay, avs_stream_abstract_t *out) { + (void) anjay; + (void) out; ac_log(ERROR, "Persistence not compiled in"); return -1; } int anjay_access_control_restore(anjay_t *anjay, avs_stream_abstract_t *in) { - (void) anjay; (void) in; + (void) anjay; + (void) in; ac_log(ERROR, "Persistence not compiled in"); return -1; } diff --git a/modules/access_control/src/mod_access_control.c b/modules/access_control/src/mod_access_control.c index 1c5217942..e6073413d 100644 --- a/modules/access_control/src/mod_access_control.c +++ b/modules/access_control/src/mod_access_control.c @@ -25,8 +25,7 @@ VISIBILITY_SOURCE_BEGIN //// HELPERS /////////////////////////////////////////////////////////////////// -access_control_t * -_anjay_access_control_from_obj_ptr(obj_ptr_t obj_ptr) { +access_control_t *_anjay_access_control_from_obj_ptr(obj_ptr_t obj_ptr) { if (!obj_ptr) { return NULL; } @@ -85,20 +84,18 @@ has_instance_multiple_owners(AVS_LIST(access_control_instance_t) it) { return false; } -static int -remove_referred_instance(anjay_t *anjay, - AVS_LIST(access_control_instance_t) it) { +static int remove_referred_instance(anjay_t *anjay, + AVS_LIST(access_control_instance_t) it) { // We do not fail if either of the following is true: // - the target Object does not exist // - the target Instance is not set // - the target Instance does not exist int result = 0; obj_ptr_t obj = _anjay_dm_find_object_by_oid(anjay, it->target.oid); - if (obj - && _anjay_access_control_target_iid_valid(it->target.iid) + if (obj && _anjay_access_control_target_iid_valid(it->target.iid) && _anjay_dm_instance_present(anjay, obj, - (anjay_iid_t) it->target.iid, - NULL) > 0) { + (anjay_iid_t) it->target.iid, NULL) + > 0) { result = _anjay_dm_instance_remove(anjay, obj, (anjay_iid_t) it->target.iid, NULL); } @@ -122,8 +119,8 @@ static anjay_ssid_t elect_instance_owner(AVS_LIST(acl_entry_t) acl) { AVS_LIST(acl_entry_t) entry; AVS_LIST_FOREACH(entry, acl) { - size_t sum = (entry->mask & ANJAY_ACCESS_MASK_WRITE) * write_weight + - (entry->mask & ANJAY_ACCESS_MASK_DELETE) * delete_weight; + size_t sum = (entry->mask & ANJAY_ACCESS_MASK_WRITE) * write_weight + + (entry->mask & ANJAY_ACCESS_MASK_DELETE) * delete_weight; if (sum >= highest_sum) { highest_sum = sum; new_owner = entry->ssid; @@ -144,8 +141,8 @@ int _anjay_access_control_add_instances_without_iids( if (!*insert_ptr || proposed_iid < (*insert_ptr)->iid) { if (out_dm_changes) { int result = _anjay_notify_queue_instance_created( - out_dm_changes, - ANJAY_DM_OID_ACCESS_CONTROL, proposed_iid); + out_dm_changes, ANJAY_DM_OID_ACCESS_CONTROL, + proposed_iid); if (result) { return result; } @@ -233,7 +230,7 @@ int _anjay_access_control_remove_orphaned_instances( access_control_t *access_control, anjay_notify_queue_t *out_dm_changes) { assert(_anjay_dm_find_object_by_oid(anjay, ANJAY_DM_OID_ACCESS_CONTROL) - == &access_control->obj_def); + == &access_control->obj_def); AVS_LIST(access_control_instance_t) *curr; AVS_LIST(access_control_instance_t) helper; AVS_LIST_DELETABLE_FOREACH_PTR(curr, helper, @@ -243,8 +240,9 @@ int _anjay_access_control_remove_orphaned_instances( (*curr)->owner)) { continue; } - int result = _anjay_dm_transaction_include_object( - anjay, &access_control->obj_def); + int result = + _anjay_dm_transaction_include_object(anjay, + &access_control->obj_def); if (result) { return result; } @@ -252,8 +250,8 @@ int _anjay_access_control_remove_orphaned_instances( /* Try to remove referred Instance according to Appendix E.1.3 */ if ((result = remove_referred_instance(anjay, *curr)) || (result = _anjay_notify_queue_instance_removed( - out_dm_changes, ANJAY_DM_OID_ACCESS_CONTROL, - (*curr)->iid))) { + out_dm_changes, ANJAY_DM_OID_ACCESS_CONTROL, + (*curr)->iid))) { return result; } AVS_LIST_CLEAR(&(*curr)->acl); @@ -268,11 +266,12 @@ int _anjay_access_control_remove_orphaned_instances( } (*curr)->owner = elect_instance_owner((*curr)->acl); if ((result = _anjay_notify_queue_resource_change( - out_dm_changes, ANJAY_DM_OID_ACCESS_CONTROL, - (*curr)->iid, ANJAY_DM_RID_ACCESS_CONTROL_ACL)) + out_dm_changes, ANJAY_DM_OID_ACCESS_CONTROL, + (*curr)->iid, ANJAY_DM_RID_ACCESS_CONTROL_ACL)) || (result = _anjay_notify_queue_resource_change( - out_dm_changes, ANJAY_DM_OID_ACCESS_CONTROL, - (*curr)->iid, ANJAY_DM_RID_ACCESS_CONTROL_OWNER))) { + out_dm_changes, ANJAY_DM_OID_ACCESS_CONTROL, + (*curr)->iid, + ANJAY_DM_RID_ACCESS_CONTROL_OWNER))) { return result; } } @@ -280,9 +279,8 @@ int _anjay_access_control_remove_orphaned_instances( return 0; } -static access_control_instance_t *find_ac_instance(access_control_t *ac, - anjay_oid_t oid, - anjay_iid_t iid) { +static access_control_instance_t * +find_ac_instance(access_control_t *ac, anjay_oid_t oid, anjay_iid_t iid) { AVS_LIST(access_control_instance_t) it; AVS_LIST_FOREACH(it, ac->current.instances) { if (it->target.oid == oid && it->target.iid == iid) { @@ -293,9 +291,8 @@ static access_control_instance_t *find_ac_instance(access_control_t *ac, return NULL; } -static bool target_instance_reachable(anjay_t *anjay, - anjay_oid_t oid, - anjay_iid_t iid) { +static bool +target_instance_reachable(anjay_t *anjay, anjay_oid_t oid, anjay_iid_t iid) { if (!_anjay_access_control_target_oid_valid(oid) || !_anjay_access_control_target_iid_valid(iid)) { return false; @@ -305,7 +302,7 @@ static bool target_instance_reachable(anjay_t *anjay, return false; } return iid == UINT16_MAX - || _anjay_dm_instance_present(anjay, target_obj, iid, NULL) > 0; + || _anjay_dm_instance_present(anjay, target_obj, iid, NULL) > 0; } static int set_acl_in_instance(anjay_t *anjay, @@ -321,8 +318,10 @@ static int set_acl_in_instance(anjay_t *anjay, if (!entry) { if (_anjay_access_control_validate_ssid(anjay, ssid)) { - ac_log(ERROR, "cannot set ACL: Server with SSID==%" PRIu16 " does " - "not exist", ssid); + ac_log(ERROR, + "cannot set ACL: Server with SSID==%" PRIu16 + " does not exist", + ssid); return -1; } @@ -352,15 +351,19 @@ static int set_acl(anjay_t *anjay, find_ac_instance(ac, oid, iid); if (!ac_instance) { if (!target_instance_reachable(anjay, oid, iid)) { - ac_log(ERROR, "cannot set ACL: object instance " - "/%" PRIu16 "/%" PRIu16 " does not exist", oid, iid); + ac_log(ERROR, + "cannot set ACL: object instance /%" PRIu16 "/%" PRIu16 + " does not exist", + oid, iid); return -1; } ac_instance = _anjay_access_control_create_missing_ac_instance( ANJAY_SSID_BOOTSTRAP, &(const acl_target_t) { oid, iid }); if (!ac_instance) { - ac_log(ERROR, "cannot set ACL: Access Control instance for /%u/%u " - "does not exist and it could not be created", oid, iid); + ac_log(ERROR, + "cannot set ACL: Access Control instance for /%u/%u does " + "not exist and it could not be created", + oid, iid); return -1; } ac_instance_needs_inserting = true; @@ -376,7 +379,7 @@ static int set_acl(anjay_t *anjay, if (!result && (result = anjay_notify_instances_changed( - anjay, ANJAY_DM_OID_ACCESS_CONTROL))) { + anjay, ANJAY_DM_OID_ACCESS_CONTROL))) { ac_log(ERROR, "error while calling anjay_notify_instances_changed()"); } anjay_notify_queue_t dm_changes = NULL; @@ -385,7 +388,7 @@ static int set_acl(anjay_t *anjay, &dm_changes))) { assert(AVS_LIST_SIZE(dm_changes) == 1); assert(AVS_LIST_SIZE(dm_changes->instance_set_changes.known_added_iids) - == 1); + == 1); assert(!dm_changes->instance_set_changes.known_removed_iids); assert(!dm_changes->resources_changed); _anjay_access_control_mark_modified(ac); @@ -423,13 +426,13 @@ int anjay_access_control_set_acl(anjay_t *anjay, } if (iid != UINT16_MAX && (access_mask & ANJAY_ACCESS_MASK_CREATE)) { ac_log(ERROR, "cannot set ACL: Create permission makes no sense for " - "Object Instances"); + "Object Instances"); return -1; } if (iid == UINT16_MAX && (access_mask & ANJAY_ACCESS_MASK_CREATE) != access_mask) { ac_log(ERROR, "cannot set ACL: only Create permission makes sense for " - "creation instance"); + "creation instance"); return -1; } @@ -437,5 +440,5 @@ int anjay_access_control_set_acl(anjay_t *anjay, } #ifdef ANJAY_TEST -#include "test/access_control.c" +# include "test/access_control.c" #endif // ANJAY_TEST diff --git a/modules/access_control/src/mod_access_control.h b/modules/access_control/src/mod_access_control.h index 7d105e33e..ba61bab95 100644 --- a/modules/access_control/src/mod_access_control.h +++ b/modules/access_control/src/mod_access_control.h @@ -83,9 +83,8 @@ void _anjay_access_control_clear_state(access_control_state_t *state); int _anjay_access_control_clone_state(access_control_state_t *dest, const access_control_state_t *src); -int -_anjay_access_control_remove_instance(access_control_t *access_control, - anjay_iid_t iid); +int _anjay_access_control_remove_instance(access_control_t *access_control, + anjay_iid_t iid); int _anjay_access_control_remove_orphaned_instances( anjay_t *anjay, diff --git a/modules/access_control/src/test/access_control.c b/modules/access_control/src/test/access_control.c index cbb06a14c..45499d01e 100644 --- a/modules/access_control/src/test/access_control.c +++ b/modules/access_control/src/test/access_control.c @@ -30,21 +30,22 @@ #include "../mod_access_control.h" #define TEST_OID 0x100 -static const anjay_dm_object_def_t *const TEST = &(const anjay_dm_object_def_t) { - .oid = TEST_OID, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS(0, 1, 2, 3, 4, 5, 6), - .handlers = { - .instance_it = _anjay_mock_dm_instance_it, - .instance_present = _anjay_mock_dm_instance_present, - .instance_create = _anjay_mock_dm_instance_create, - .instance_remove = _anjay_mock_dm_instance_remove, - .resource_present = _anjay_mock_dm_resource_present, - .resource_read = _anjay_mock_dm_resource_read, - .resource_write = _anjay_mock_dm_resource_write, - .resource_execute = _anjay_mock_dm_resource_execute, - .resource_dim = _anjay_mock_dm_resource_dim - } -}; +static const anjay_dm_object_def_t *const TEST = + &(const anjay_dm_object_def_t) { + .oid = TEST_OID, + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(0, 1, 2, 3, 4, 5, 6), + .handlers = { + .instance_it = _anjay_mock_dm_instance_it, + .instance_present = _anjay_mock_dm_instance_present, + .instance_create = _anjay_mock_dm_instance_create, + .instance_remove = _anjay_mock_dm_instance_remove, + .resource_present = _anjay_mock_dm_resource_present, + .resource_read = _anjay_mock_dm_resource_read, + .resource_write = _anjay_mock_dm_resource_write, + .resource_execute = _anjay_mock_dm_resource_execute, + .resource_dim = _anjay_mock_dm_resource_dim + } + }; AVS_UNIT_TEST(access_control, set_acl) { DM_TEST_INIT_WITH_OBJECTS(&FAKE_SECURITY, &TEST); @@ -66,37 +67,32 @@ AVS_UNIT_TEST(access_control, set_acl) { } // NULL AC object ptr - AVS_UNIT_ASSERT_FAILED( - anjay_access_control_set_acl(NULL, TEST->oid, iid, ssid, - ANJAY_ACCESS_MASK_NONE)); + AVS_UNIT_ASSERT_FAILED(anjay_access_control_set_acl( + NULL, TEST->oid, iid, ssid, ANJAY_ACCESS_MASK_NONE)); // unknown Object ID AVS_UNIT_ASSERT_FAILED( - anjay_access_control_set_acl(anjay, (anjay_oid_t)(TEST->oid + 1), + anjay_access_control_set_acl(anjay, (anjay_oid_t) (TEST->oid + 1), iid, ssid, ANJAY_ACCESS_MASK_NONE)); // uknown Object instance ID - _anjay_mock_dm_expect_instance_present(anjay, &TEST, (anjay_iid_t)(iid + 1), - 0); - AVS_UNIT_ASSERT_FAILED( - anjay_access_control_set_acl(anjay, TEST->oid, - (anjay_iid_t)(iid + 1), ssid, - ANJAY_ACCESS_MASK_NONE)); + _anjay_mock_dm_expect_instance_present(anjay, &TEST, + (anjay_iid_t) (iid + 1), 0); + AVS_UNIT_ASSERT_FAILED(anjay_access_control_set_acl( + anjay, TEST->oid, (anjay_iid_t) (iid + 1), ssid, + ANJAY_ACCESS_MASK_NONE)); // Create flag in access mask - AVS_UNIT_ASSERT_FAILED( - anjay_access_control_set_acl(anjay, TEST->oid, iid, ssid, - ANJAY_ACCESS_MASK_CREATE)); - AVS_UNIT_ASSERT_FAILED( - anjay_access_control_set_acl(anjay, TEST->oid, iid, ssid, - ANJAY_ACCESS_MASK_FULL)); + AVS_UNIT_ASSERT_FAILED(anjay_access_control_set_acl( + anjay, TEST->oid, iid, ssid, ANJAY_ACCESS_MASK_CREATE)); + AVS_UNIT_ASSERT_FAILED(anjay_access_control_set_acl( + anjay, TEST->oid, iid, ssid, ANJAY_ACCESS_MASK_FULL)); { // valid call - anjay_access_mask_t mask = ANJAY_ACCESS_MASK_READ - | ANJAY_ACCESS_MASK_WRITE - | ANJAY_ACCESS_MASK_EXECUTE - | ANJAY_ACCESS_MASK_DELETE; + anjay_access_mask_t mask = + ANJAY_ACCESS_MASK_READ | ANJAY_ACCESS_MASK_WRITE + | ANJAY_ACCESS_MASK_EXECUTE | ANJAY_ACCESS_MASK_DELETE; AVS_UNIT_ASSERT_SUCCESS(anjay_access_control_set_acl(anjay, TEST->oid, iid, ssid, mask)); diff --git a/modules/access_control/src/test/persistence.c b/modules/access_control/src/test/persistence.c index 3fa8923f3..5cfc43082 100644 --- a/modules/access_control/src/test/persistence.c +++ b/modules/access_control/src/test/persistence.c @@ -17,8 +17,8 @@ #include #include -#include #include +#include #include #include @@ -41,7 +41,8 @@ static const anjay_dm_supported_rids_t MOCK_SUPPORTED_RIDS = static anjay_dm_object_def_t *make_mock_object(anjay_oid_t oid) { anjay_dm_object_def_t *obj = - (anjay_dm_object_def_t *) avs_calloc(1, sizeof(anjay_dm_object_def_t)); + (anjay_dm_object_def_t *) avs_calloc(1, + sizeof(anjay_dm_object_def_t)); if (obj) { obj->oid = oid; obj->supported_rids = MOCK_SUPPORTED_RIDS; @@ -52,9 +53,8 @@ static anjay_dm_object_def_t *make_mock_object(anjay_oid_t oid) { typedef bool comparator_t(const void *a, const void *b); -static bool lists_equal(AVS_LIST(void) a, - AVS_LIST(void) b, - comparator_t *equals) { +static bool +lists_equal(AVS_LIST(void) a, AVS_LIST(void) b, comparator_t *equals) { AVS_LIST(void) p = a; AVS_LIST(void) q = b; while (p && q) { @@ -76,11 +76,10 @@ static bool acl_entry_equal(const void *a, const void *b) { static bool instances_equal(const void *a, const void *b) { const access_control_instance_t *p = (const access_control_instance_t *) a; const access_control_instance_t *q = (const access_control_instance_t *) b; - return (p == q) || (p->iid == q->iid && - p->target.oid == q->target.oid && - p->target.iid == q->target.iid && - p->owner == q->owner && - lists_equal(p->acl, q->acl, acl_entry_equal)); + return (p == q) + || (p->iid == q->iid && p->target.oid == q->target.oid + && p->target.iid == q->target.iid && p->owner == q->owner + && lists_equal(p->acl, q->acl, acl_entry_equal)); } static bool aco_equal(access_control_t *a, access_control_t *b) { @@ -104,8 +103,10 @@ typedef struct { } storage_ctx_t; static void init_context(storage_ctx_t *ctx) { - memcpy(&ctx->in, &AVS_STREAM_INBUF_STATIC_INITIALIZER, sizeof(avs_stream_inbuf_t)); - memcpy(&ctx->out, &AVS_STREAM_OUTBUF_STATIC_INITIALIZER, sizeof(avs_stream_outbuf_t)); + memcpy(&ctx->in, &AVS_STREAM_INBUF_STATIC_INITIALIZER, + sizeof(avs_stream_inbuf_t)); + memcpy(&ctx->out, &AVS_STREAM_OUTBUF_STATIC_INITIALIZER, + sizeof(avs_stream_outbuf_t)); ctx->out.buffer = ctx->buffer; ctx->out.buffer_size = sizeof(ctx->buffer); ctx->in.buffer = ctx->buffer; @@ -115,7 +116,9 @@ AVS_UNIT_TEST(access_control_persistence, empty_aco) { anjay_t *anjay1 = ac_test_create_fake_anjay(); anjay_t *anjay2 = ac_test_create_fake_anjay(); - storage_ctx_t ctx = { .buffer = {} }; + storage_ctx_t ctx = { + .buffer = {} + }; init_context(&ctx); AVS_UNIT_ASSERT_SUCCESS(anjay_access_control_install(anjay1)); AVS_UNIT_ASSERT_SUCCESS(anjay_access_control_install(anjay2)); @@ -137,7 +140,9 @@ AVS_UNIT_TEST(access_control_persistence, normal_usage) { anjay_t *anjay1 = ac_test_create_fake_anjay(); anjay_t *anjay2 = ac_test_create_fake_anjay(); - storage_ctx_t ctx = { .buffer = {} }; + storage_ctx_t ctx = { + .buffer = {} + }; init_context(&ctx); AVS_UNIT_ASSERT_SUCCESS(anjay_access_control_install(anjay1)); @@ -193,9 +198,15 @@ AVS_UNIT_TEST(access_control_persistence, normal_usage) { AVS_LIST(acl_entry_t) acl1 = NULL; AVS_LIST_APPEND(&acl1, AVS_LIST_NEW_ELEMENT(acl_entry_t)); - *acl1 = (acl_entry_t) { .mask = 0xFFFF, .ssid = 1 }; + *acl1 = (acl_entry_t) { + .mask = 0xFFFF, + .ssid = 1 + }; AVS_LIST_INSERT(&acl1, AVS_LIST_NEW_ELEMENT(acl_entry_t)); - *acl1 = (acl_entry_t) { .mask = 0xDEAD, .ssid = 0xBABE }; + *acl1 = (acl_entry_t) { + .mask = 0xDEAD, + .ssid = 0xBABE + }; access_control_instance_t instance1 = (access_control_instance_t) { .target = { .oid = 32, diff --git a/modules/attr_storage/src/attr_storage_persistence.c b/modules/attr_storage/src/attr_storage_persistence.c index 5537859b9..15a13d1ea 100644 --- a/modules/attr_storage/src/attr_storage_persistence.c +++ b/modules/attr_storage/src/attr_storage_persistence.c @@ -29,9 +29,9 @@ VISIBILITY_SOURCE_BEGIN -#define HANDLE_LIST(Type, Ctx, ListPtr, UserPtr) \ - avs_persistence_list((Ctx), (AVS_LIST(void) *) (ListPtr), \ - sizeof(**(ListPtr)), handle_##Type, UserPtr, NULL) +#define HANDLE_LIST(Type, Ctx, ListPtr, UserPtr) \ + avs_persistence_list((Ctx), (AVS_LIST(void) *) (ListPtr), \ + sizeof(**(ListPtr)), handle_##Type, UserPtr, NULL) //// DATA STRUCTURE HANDLERS /////////////////////////////////////////////////// @@ -97,8 +97,9 @@ static int handle_internal_res_attrs(avs_persistence_context_t *ctx, int retval; (void) ((retval = handle_resource_attributes(ctx, &attrs->standard)) || (retval = handle_custom_attributes( - ctx, _anjay_dm_get_internal_attrs(&attrs->standard.common), - version))); + ctx, + _anjay_dm_get_internal_attrs(&attrs->standard.common), + version))); return retval; } @@ -109,7 +110,7 @@ static int handle_default_attrs(avs_persistence_context_t *ctx, int retval; (void) ((retval = avs_persistence_u16(ctx, &attrs->ssid)) || (retval = handle_internal_attrs( - ctx, &attrs->attrs, (int) (intptr_t) version_as_ptr))); + ctx, &attrs->attrs, (int) (intptr_t) version_as_ptr))); return retval; } @@ -120,7 +121,7 @@ static int handle_resource_attrs(avs_persistence_context_t *ctx, int retval; (void) ((retval = avs_persistence_u16(ctx, &attrs->ssid)) || (retval = handle_internal_res_attrs( - ctx, &attrs->attrs, (int) (intptr_t) version_as_ptr))); + ctx, &attrs->attrs, (int) (intptr_t) version_as_ptr))); return retval; } @@ -143,8 +144,8 @@ static int handle_instance_entry(avs_persistence_context_t *ctx, (void) ((retval = avs_persistence_u16(ctx, &instance->iid)) || (retval = HANDLE_LIST(default_attrs, ctx, &instance->default_attrs, version_as_ptr)) - || (retval = HANDLE_LIST(resource_entry, ctx, - &instance->resources, version_as_ptr))); + || (retval = HANDLE_LIST(resource_entry, ctx, &instance->resources, + version_as_ptr))); return retval; } @@ -154,8 +155,8 @@ static int handle_object(avs_persistence_context_t *ctx, fas_object_entry_t *object = (fas_object_entry_t *) object_; int retval; (void) ((retval = avs_persistence_u16(ctx, &object->oid)) - || (retval = HANDLE_LIST(default_attrs, ctx, - &object->default_attrs, version_as_ptr)) + || (retval = HANDLE_LIST(default_attrs, ctx, &object->default_attrs, + version_as_ptr)) || (retval = HANDLE_LIST(instance_entry, ctx, &object->instances, version_as_ptr))); return retval; @@ -177,11 +178,7 @@ typedef char fas_magic_t[4]; static const fas_magic_t MAGIC_V0 = { 'F', 'A', 'S', '\0' }; static const fas_magic_t MAGIC_V2 = { 'F', 'A', 'S', '\2' }; -typedef enum { - RM_SUCCESS, - RM_ERROR, - RM_EOF -} read_magic_result_t; +typedef enum { RM_SUCCESS, RM_ERROR, RM_EOF } read_magic_result_t; static int read_magic_or_eof(avs_stream_abstract_t *stream, fas_magic_t *out) { size_t bytes_read = 0; @@ -260,7 +257,7 @@ static bool is_object_sane(fas_object_entry_t *object) { return is_attrs_list_sane(object->default_attrs, offsetof(fas_default_attrs_t, attrs), default_attrs_empty) - && is_instances_list_sane(object->instances); + && is_instances_list_sane(object->instances); } static bool is_attr_storage_sane(anjay_attr_storage_t *fas) { @@ -424,8 +421,7 @@ int _anjay_attr_storage_restore_inner(anjay_t *anjay, (void) ((retval = HANDLE_LIST(object, ctx, &attr_storage->objects, (void *) version)) || (retval = (is_attr_storage_sane(attr_storage) ? 0 : -1)) - || (retval = clear_nonexistent_entries(anjay, - attr_storage))); + || (retval = clear_nonexistent_entries(anjay, attr_storage))); avs_persistence_context_delete(ctx); } if (retval) { @@ -465,5 +461,5 @@ int anjay_attr_storage_restore(anjay_t *anjay, avs_stream_abstract_t *in) { } #ifdef ANJAY_TEST -#include "test/persistence.c" +# include "test/persistence.c" #endif // ANJAY_TEST diff --git a/modules/attr_storage/src/mod_attr_storage.c b/modules/attr_storage/src/mod_attr_storage.c index a07d9a21c..d9bd8be90 100644 --- a/modules/attr_storage/src/mod_attr_storage.c +++ b/modules/attr_storage/src/mod_attr_storage.c @@ -80,14 +80,15 @@ int anjay_attr_storage_install(anjay_t *anjay) { return -1; } anjay_attr_storage_t *fas = - (anjay_attr_storage_t *) avs_calloc(1, sizeof(anjay_attr_storage_t)); + (anjay_attr_storage_t *) avs_calloc(1, + sizeof(anjay_attr_storage_t)); if (!fas) { fas_log(ERROR, "out of memory"); return -1; } if (!(fas->saved_state.persist_data = avs_stream_membuf_create()) - || _anjay_dm_module_install(anjay, - &_anjay_attr_storage_MODULE, fas)) { + || _anjay_dm_module_install(anjay, &_anjay_attr_storage_MODULE, + fas)) { avs_stream_cleanup(&fas->saved_state.persist_data); avs_free(fas); return -1; @@ -130,42 +131,35 @@ void anjay_attr_storage_purge(anjay_t *anjay) { //// HELPERS /////////////////////////////////////////////////////////////////// static bool implements_any_object_default_attrs_handlers( - anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr) { - return _anjay_dm_handler_implemented(anjay, obj_ptr, - &_anjay_attr_storage_MODULE, - offsetof(anjay_dm_handlers_t, - object_read_default_attrs)) - || _anjay_dm_handler_implemented(anjay, obj_ptr, - &_anjay_attr_storage_MODULE, - offsetof(anjay_dm_handlers_t, - object_write_default_attrs)); + anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr) { + return _anjay_dm_handler_implemented( + anjay, obj_ptr, &_anjay_attr_storage_MODULE, + offsetof(anjay_dm_handlers_t, object_read_default_attrs)) + || _anjay_dm_handler_implemented( + anjay, obj_ptr, &_anjay_attr_storage_MODULE, + offsetof(anjay_dm_handlers_t, + object_write_default_attrs)); } static bool implements_any_instance_default_attrs_handlers( - anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr) { - return _anjay_dm_handler_implemented(anjay, obj_ptr, - &_anjay_attr_storage_MODULE, - offsetof(anjay_dm_handlers_t, - instance_read_default_attrs)) - || _anjay_dm_handler_implemented(anjay, obj_ptr, - &_anjay_attr_storage_MODULE, - offsetof(anjay_dm_handlers_t, - instance_write_default_attrs)); + anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr) { + return _anjay_dm_handler_implemented( + anjay, obj_ptr, &_anjay_attr_storage_MODULE, + offsetof(anjay_dm_handlers_t, instance_read_default_attrs)) + || _anjay_dm_handler_implemented( + anjay, obj_ptr, &_anjay_attr_storage_MODULE, + offsetof(anjay_dm_handlers_t, + instance_write_default_attrs)); } static bool implements_any_resource_attrs_handlers( - anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr) { - return _anjay_dm_handler_implemented(anjay, obj_ptr, - &_anjay_attr_storage_MODULE, - offsetof(anjay_dm_handlers_t, - resource_read_attrs)) - || _anjay_dm_handler_implemented(anjay, obj_ptr, - &_anjay_attr_storage_MODULE, - offsetof(anjay_dm_handlers_t, - resource_write_attrs)); + anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr) { + return _anjay_dm_handler_implemented( + anjay, obj_ptr, &_anjay_attr_storage_MODULE, + offsetof(anjay_dm_handlers_t, resource_read_attrs)) + || _anjay_dm_handler_implemented( + anjay, obj_ptr, &_anjay_attr_storage_MODULE, + offsetof(anjay_dm_handlers_t, resource_write_attrs)); } static void @@ -176,8 +170,8 @@ remove_resource_if_empty(AVS_LIST(fas_resource_entry_t) *entry_ptr) { } anjay_attr_storage_t *_anjay_attr_storage_get(anjay_t *anjay) { - return (anjay_attr_storage_t *) - _anjay_dm_module_get_arg(anjay, &_anjay_attr_storage_MODULE); + return (anjay_attr_storage_t *) _anjay_dm_module_get_arg( + anjay, &_anjay_attr_storage_MODULE); } static anjay_attr_storage_t *get_fas(anjay_t *anjay) { @@ -187,12 +181,9 @@ static anjay_attr_storage_t *get_fas(anjay_t *anjay) { return (anjay_attr_storage_t *) fas; } -AVS_STATIC_ASSERT(offsetof(fas_object_entry_t, oid) == 0, - object_id_offset); -AVS_STATIC_ASSERT(offsetof(fas_instance_entry_t, iid) == 0, - instance_id_offset); -AVS_STATIC_ASSERT(offsetof(fas_resource_entry_t, rid) == 0, - resource_id_offset); +AVS_STATIC_ASSERT(offsetof(fas_object_entry_t, oid) == 0, object_id_offset); +AVS_STATIC_ASSERT(offsetof(fas_instance_entry_t, iid) == 0, instance_id_offset); +AVS_STATIC_ASSERT(offsetof(fas_resource_entry_t, rid) == 0, resource_id_offset); static AVS_LIST(void) * find_or_create_entry_impl(AVS_LIST(void) *children_list_ptr, @@ -221,58 +212,53 @@ find_or_create_entry_impl(AVS_LIST(void) *children_list_ptr, return entry_ptr; } -static inline -AVS_LIST(fas_object_entry_t) *find_object(anjay_attr_storage_t *parent, - anjay_oid_t id) { - return (AVS_LIST(fas_object_entry_t) *) - find_or_create_entry_impl((AVS_LIST(void) *) &parent->objects, - sizeof(fas_object_entry_t), id, false); +static inline AVS_LIST(fas_object_entry_t) * +find_object(anjay_attr_storage_t *parent, anjay_oid_t id) { + return (AVS_LIST(fas_object_entry_t) *) find_or_create_entry_impl( + (AVS_LIST(void) *) &parent->objects, sizeof(fas_object_entry_t), id, + false); } -static inline -AVS_LIST(fas_object_entry_t) *find_or_create_object(anjay_attr_storage_t *parent, - anjay_oid_t id) { - return (AVS_LIST(fas_object_entry_t) *) - find_or_create_entry_impl((AVS_LIST(void) *) &parent->objects, - sizeof(fas_object_entry_t), id, true); +static inline AVS_LIST(fas_object_entry_t) * +find_or_create_object(anjay_attr_storage_t *parent, anjay_oid_t id) { + return (AVS_LIST(fas_object_entry_t) *) find_or_create_entry_impl( + (AVS_LIST(void) *) &parent->objects, sizeof(fas_object_entry_t), id, + true); } -static inline -AVS_LIST(fas_instance_entry_t) *find_instance(fas_object_entry_t *parent, - anjay_iid_t id) { - return (AVS_LIST(fas_instance_entry_t) *) - find_or_create_entry_impl((AVS_LIST(void) *) &parent->instances, - sizeof(fas_instance_entry_t), id, false); +static inline AVS_LIST(fas_instance_entry_t) * +find_instance(fas_object_entry_t *parent, anjay_iid_t id) { + return (AVS_LIST(fas_instance_entry_t) *) find_or_create_entry_impl( + (AVS_LIST(void) *) &parent->instances, sizeof(fas_instance_entry_t), + id, false); } -static inline -AVS_LIST(fas_instance_entry_t) *find_or_create_instance(fas_object_entry_t *parent, - anjay_iid_t id) { - return (AVS_LIST(fas_instance_entry_t) *) - find_or_create_entry_impl((AVS_LIST(void) *) &parent->instances, - sizeof(fas_instance_entry_t), id, true); +static inline AVS_LIST(fas_instance_entry_t) * +find_or_create_instance(fas_object_entry_t *parent, anjay_iid_t id) { + return (AVS_LIST(fas_instance_entry_t) *) find_or_create_entry_impl( + (AVS_LIST(void) *) &parent->instances, sizeof(fas_instance_entry_t), + id, true); } -static inline -AVS_LIST(fas_resource_entry_t) *find_resource(fas_instance_entry_t *parent, - anjay_rid_t id) { - return (AVS_LIST(fas_resource_entry_t) *) - find_or_create_entry_impl((AVS_LIST(void) *) &parent->resources, - sizeof(fas_resource_entry_t), id, false); +static inline AVS_LIST(fas_resource_entry_t) * +find_resource(fas_instance_entry_t *parent, anjay_rid_t id) { + return (AVS_LIST(fas_resource_entry_t) *) find_or_create_entry_impl( + (AVS_LIST(void) *) &parent->resources, sizeof(fas_resource_entry_t), + id, false); } static inline AVS_LIST(fas_resource_entry_t) * find_or_create_resource(fas_instance_entry_t *parent, anjay_rid_t id) { - return (AVS_LIST(fas_resource_entry_t) *) - find_or_create_entry_impl((AVS_LIST(void) *) &parent->resources, - sizeof(fas_resource_entry_t), id, true); + return (AVS_LIST(fas_resource_entry_t) *) find_or_create_entry_impl( + (AVS_LIST(void) *) &parent->resources, sizeof(fas_resource_entry_t), + id, true); } static void remove_instance(anjay_attr_storage_t *fas, AVS_LIST(fas_object_entry_t) *object_ptr, anjay_iid_t iid) { - AVS_LIST(fas_instance_entry_t) *instance_ptr = find_instance(*object_ptr, - iid); + AVS_LIST(fas_instance_entry_t) *instance_ptr = + find_instance(*object_ptr, iid); if (instance_ptr && *instance_ptr) { remove_instance_entry(fas, instance_ptr); } @@ -283,8 +269,8 @@ static void remove_resource(anjay_attr_storage_t *fas, AVS_LIST(fas_object_entry_t) *object_ptr, AVS_LIST(fas_instance_entry_t) *instance_ptr, anjay_rid_t rid) { - AVS_LIST(fas_resource_entry_t) *resource_ptr = find_resource(*instance_ptr, - rid); + AVS_LIST(fas_resource_entry_t) *resource_ptr = + find_resource(*instance_ptr, rid); if (resource_ptr) { remove_resource_entry(fas, resource_ptr); } @@ -293,8 +279,7 @@ static void remove_resource(anjay_attr_storage_t *fas, } static inline bool is_ssid_reference_object(anjay_oid_t oid) { - return oid == ANJAY_DM_OID_SECURITY - || oid == ANJAY_DM_OID_SERVER; + return oid == ANJAY_DM_OID_SECURITY || oid == ANJAY_DM_OID_SERVER; } static inline anjay_rid_t ssid_rid(anjay_oid_t oid) { @@ -338,7 +323,7 @@ static void remove_attrs_for_server(anjay_attr_storage_t *fas, AVS_LIST_ITERATE_PTR(attrs_ptr) { assert(!*AVS_LIST_NEXT_PTR(attrs_ptr) || *get_ssid_ptr(*attrs_ptr) - < *get_ssid_ptr(*AVS_LIST_NEXT_PTR(attrs_ptr))); + < *get_ssid_ptr(*AVS_LIST_NEXT_PTR(attrs_ptr))); if (*get_ssid_ptr(*attrs_ptr) == ssid) { remove_attrs_entry(fas, attrs_ptr); assert(!*attrs_ptr || ssid < *get_ssid_ptr(*attrs_ptr)); @@ -386,7 +371,7 @@ static void remove_servers(anjay_attr_storage_t *fas, &(*object_ptr)->instances) { remove_attrs_func( fas, (AVS_LIST(void) *) &(*instance_ptr)->default_attrs, - ssid_ref); + ssid_ref); AVS_LIST(fas_resource_entry_t) *res_ptr; AVS_LIST(fas_resource_entry_t) res_helper; AVS_LIST_DELETABLE_FOREACH_PTR(res_ptr, res_helper, @@ -401,16 +386,16 @@ static void remove_servers(anjay_attr_storage_t *fas, } } -int _anjay_attr_storage_compare_u16ids(const void *a, const void *b, +int _anjay_attr_storage_compare_u16ids(const void *a, + const void *b, size_t element_size) { assert(element_size == sizeof(uint16_t)); (void) element_size; return *(const uint16_t *) a - *(const uint16_t *) b; } -static int -remove_servers_after_iteration(anjay_t *anjay, - anjay_attr_storage_t *fas) { +static int remove_servers_after_iteration(anjay_t *anjay, + anjay_attr_storage_t *fas) { AVS_LIST(anjay_ssid_t) ssids = NULL; AVS_LIST(anjay_iid_t) iid; AVS_LIST_FOREACH(iid, fas->iteration.iids) { @@ -537,9 +522,9 @@ static int write_attrs_impl(anjay_attr_storage_t *fas, return 0; } -#define WRITE_ATTRS(Fas, OutAttrs, IsEmptyFunc, Ssid, Attrs) \ - write_attrs_impl( \ - (Fas), (AVS_LIST(void) *) (OutAttrs), sizeof(**(OutAttrs)), \ +#define WRITE_ATTRS(Fas, OutAttrs, IsEmptyFunc, Ssid, Attrs) \ + write_attrs_impl( \ + (Fas), (AVS_LIST(void) *) (OutAttrs), sizeof(**(OutAttrs)), \ (size_t) ((char *) &(*(OutAttrs))->attrs - (char *) *(OutAttrs)), \ sizeof((*(OutAttrs))->attrs), (IsEmptyFunc), (Ssid), (Attrs)) @@ -623,8 +608,8 @@ static int write_resource_attrs(anjay_t *anjay, result = -1; } if (!result) { - result = WRITE_ATTRS(fas, &(*resource_ptr)->attrs, - resource_attrs_empty, ssid, attrs); + result = WRITE_ATTRS(fas, &(*resource_ptr)->attrs, resource_attrs_empty, + ssid, attrs); } if (resource_ptr) { remove_resource_if_empty(resource_ptr); @@ -636,51 +621,53 @@ static int write_resource_attrs(anjay_t *anjay, return result; } - //// ATTRIBUTE HANDLERS //////////////////////////////////////////////////////// -static int object_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - anjay_dm_attributes_t *out_) { +static int +object_read_default_attrs(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + anjay_dm_attributes_t *out_) { anjay_dm_internal_attrs_t *out = _anjay_dm_get_internal_attrs(out_); if (implements_any_object_default_attrs_handlers(anjay, obj_ptr)) { return _anjay_dm_object_read_default_attrs(anjay, obj_ptr, ssid, out, &_anjay_attr_storage_MODULE); } - AVS_LIST(fas_object_entry_t) *object_ptr = find_object(get_fas(anjay), - (*obj_ptr)->oid); - read_default_attrs(object_ptr ? (*object_ptr)->default_attrs : NULL, - ssid, out); + AVS_LIST(fas_object_entry_t) *object_ptr = + find_object(get_fas(anjay), (*obj_ptr)->oid); + read_default_attrs(object_ptr ? (*object_ptr)->default_attrs : NULL, ssid, + out); return 0; } -static int object_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - const anjay_dm_attributes_t *attrs_) { +static int +object_write_default_attrs(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + const anjay_dm_attributes_t *attrs_) { const anjay_dm_internal_attrs_t *attrs = _anjay_dm_get_internal_attrs_const(attrs_); if (implements_any_object_default_attrs_handlers(anjay, obj_ptr)) { - return _anjay_dm_object_write_default_attrs(anjay, obj_ptr, ssid, attrs, - &_anjay_attr_storage_MODULE); + return _anjay_dm_object_write_default_attrs( + anjay, obj_ptr, ssid, attrs, &_anjay_attr_storage_MODULE); } return write_object_attrs(anjay, ssid, obj_ptr, attrs) ? ANJAY_ERR_INTERNAL : 0; } -static int instance_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - anjay_dm_attributes_t *out_) { +static int +instance_read_default_attrs(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + anjay_dm_attributes_t *out_) { anjay_dm_internal_attrs_t *out = _anjay_dm_get_internal_attrs(out_); if (implements_any_instance_default_attrs_handlers(anjay, obj_ptr)) { return _anjay_dm_instance_read_default_attrs( anjay, obj_ptr, iid, ssid, out, &_anjay_attr_storage_MODULE); } - AVS_LIST(fas_object_entry_t) *object_ptr = find_object(get_fas(anjay), - (*obj_ptr)->oid); + AVS_LIST(fas_object_entry_t) *object_ptr = + find_object(get_fas(anjay), (*obj_ptr)->oid); AVS_LIST(fas_instance_entry_t) *instance_ptr = object_ptr ? find_instance(*object_ptr, iid) : NULL; read_default_attrs(instance_ptr ? (*instance_ptr)->default_attrs : NULL, @@ -688,11 +675,12 @@ static int instance_read_default_attrs(anjay_t *anjay, return 0; } -static int instance_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - const anjay_dm_attributes_t *attrs_) { +static int +instance_write_default_attrs(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + const anjay_dm_attributes_t *attrs_) { const anjay_dm_internal_attrs_t *attrs = _anjay_dm_get_internal_attrs_const(attrs_); if (implements_any_instance_default_attrs_handlers(anjay, obj_ptr)) { @@ -715,8 +703,8 @@ static int resource_read_attrs(anjay_t *anjay, return _anjay_dm_resource_read_attrs(anjay, obj_ptr, iid, rid, ssid, out, &_anjay_attr_storage_MODULE); } - AVS_LIST(fas_object_entry_t) *object_ptr = find_object(get_fas(anjay), - (*obj_ptr)->oid); + AVS_LIST(fas_object_entry_t) *object_ptr = + find_object(get_fas(anjay), (*obj_ptr)->oid); AVS_LIST(fas_instance_entry_t) *instance_ptr = object_ptr ? find_instance(*object_ptr, iid) : NULL; AVS_LIST(fas_resource_entry_t) *res_ptr = @@ -767,8 +755,7 @@ static int instance_it(anjay_t *anjay, } int result = _anjay_dm_instance_it(anjay, obj_ptr, out, cookie, &_anjay_attr_storage_MODULE); - if (result - || fas->iteration.oid != (*obj_ptr)->oid + if (result || fas->iteration.oid != (*obj_ptr)->oid || fas->iteration.last_cookie != orig_cookie) { reset_it_state(&fas->iteration); } else { @@ -794,8 +781,8 @@ static int instance_present(anjay_t *anjay, &_anjay_attr_storage_MODULE); if (result == 0) { anjay_attr_storage_t *fas = get_fas(anjay); - AVS_LIST(fas_object_entry_t) *object_ptr = find_object(fas, - (*obj_ptr)->oid); + AVS_LIST(fas_object_entry_t) *object_ptr = + find_object(fas, (*obj_ptr)->oid); if (object_ptr) { remove_instance(fas, object_ptr, iid); } @@ -811,8 +798,8 @@ static int instance_remove(anjay_t *anjay, &_anjay_attr_storage_MODULE); if (result == 0) { anjay_attr_storage_t *fas = get_fas(anjay); - AVS_LIST(fas_object_entry_t) *object_ptr = find_object(fas, - (*obj_ptr)->oid); + AVS_LIST(fas_object_entry_t) *object_ptr = + find_object(fas, (*obj_ptr)->oid); if (object_ptr) { remove_instance(fas, object_ptr, iid); } @@ -831,8 +818,8 @@ static int resource_present(anjay_t *anjay, &_anjay_attr_storage_MODULE); if (result == 0) { anjay_attr_storage_t *fas = get_fas(anjay); - AVS_LIST(fas_object_entry_t) *object_ptr = find_object(fas, - (*obj_ptr)->oid); + AVS_LIST(fas_object_entry_t) *object_ptr = + find_object(fas, (*obj_ptr)->oid); AVS_LIST(fas_instance_entry_t) *instance_ptr = object_ptr ? find_instance(*object_ptr, iid) : NULL; if (instance_ptr) { @@ -853,8 +840,7 @@ static int saved_state_save(anjay_attr_storage_t *fas) { fas->saved_state.persist_data); } -static int saved_state_restore(anjay_t *anjay, - anjay_attr_storage_t *fas) { +static int saved_state_restore(anjay_t *anjay, anjay_attr_storage_t *fas) { int result = _anjay_attr_storage_restore_inner(anjay, fas, fas->saved_state.persist_data); @@ -872,8 +858,9 @@ static int transaction_begin(anjay_t *anjay, return ANJAY_ERR_INTERNAL; } } - int result = _anjay_dm_delegate_transaction_begin( - anjay, obj_ptr, &_anjay_attr_storage_MODULE); + int result = + _anjay_dm_delegate_transaction_begin(anjay, obj_ptr, + &_anjay_attr_storage_MODULE); if (result) { saved_state_reset(fas); } @@ -883,8 +870,9 @@ static int transaction_begin(anjay_t *anjay, static int transaction_commit(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr) { anjay_attr_storage_t *fas = get_fas(anjay); - int result = _anjay_dm_delegate_transaction_commit( - anjay, obj_ptr, &_anjay_attr_storage_MODULE); + int result = + _anjay_dm_delegate_transaction_commit(anjay, obj_ptr, + &_anjay_attr_storage_MODULE); if (--fas->saved_state.depth == 0) { if (result && saved_state_restore(anjay, fas)) { result = ANJAY_ERR_INTERNAL; @@ -955,7 +943,7 @@ int anjay_attr_storage_set_object_attrs(anjay_t *anjay, #ifdef WITH_CUSTOM_ATTRIBUTES _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER #endif // WITH_CUSTOM_ATTRIBUTES - .standard = *attrs + .standard = *attrs }; int result; @@ -993,7 +981,7 @@ int anjay_attr_storage_set_instance_attrs(anjay_t *anjay, #ifdef WITH_CUSTOM_ATTRIBUTES _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER #endif // WITH_CUSTOM_ATTRIBUTES - .standard = *attrs + .standard = *attrs }; int result; @@ -1029,10 +1017,12 @@ int anjay_attr_storage_set_resource_attrs( fas_log(ERROR, ERR_INSTANCE_PRESENCE_CHECK, oid, iid); return -1; } - if (_anjay_dm_resource_supported_and_present(anjay, obj, iid, rid, NULL) <= 0) { - fas_log(ERROR, "resource /%" PRIu16 "/%" PRIu16 "/%" PRIu16 - "does not exist or an error occurred during querying " - "its presence", + if (_anjay_dm_resource_supported_and_present(anjay, obj, iid, rid, NULL) + <= 0) { + fas_log(ERROR, + "resource /%" PRIu16 "/%" PRIu16 "/%" PRIu16 + "does not exist or an error occurred during querying " + "its presence", oid, iid, rid); return -1; } @@ -1040,7 +1030,7 @@ int anjay_attr_storage_set_resource_attrs( #ifdef WITH_CUSTOM_ATTRIBUTES _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER #endif // WITH_CUSTOM_ATTRIBUTES - .standard = *attrs + .standard = *attrs }; int result; if (!(result = write_resource_attrs(anjay, ssid, obj, iid, rid, @@ -1051,5 +1041,5 @@ int anjay_attr_storage_set_resource_attrs( } #ifdef ANJAY_TEST -#include "test/attr_storage.c" +# include "test/attr_storage.c" #endif // ANJAY_TEST diff --git a/modules/attr_storage/src/mod_attr_storage.h b/modules/attr_storage/src/mod_attr_storage.h index e3363d40b..2f6b7d582 100644 --- a/modules/attr_storage/src/mod_attr_storage.h +++ b/modules/attr_storage/src/mod_attr_storage.h @@ -122,8 +122,7 @@ remove_instance_if_empty(AVS_LIST(fas_instance_entry_t) *entry_ptr) { } } -static void -remove_object_if_empty(AVS_LIST(fas_object_entry_t) *entry_ptr) { +static void remove_object_if_empty(AVS_LIST(fas_object_entry_t) *entry_ptr) { if (!(*entry_ptr)->default_attrs && !(*entry_ptr)->instances) { AVS_LIST_DELETE(entry_ptr); } @@ -154,7 +153,8 @@ static bool resource_attrs_empty(const void *attrs) { (const anjay_dm_internal_res_attrs_t *) attrs); } -int _anjay_attr_storage_compare_u16ids(const void *a, const void *b, +int _anjay_attr_storage_compare_u16ids(const void *a, + const void *b, size_t element_size); int _anjay_attr_storage_persist_inner(anjay_attr_storage_t *attr_storage, diff --git a/modules/attr_storage/src/test/attr_storage.c b/modules/attr_storage/src/test/attr_storage.c index ba347611e..e77501e76 100644 --- a/modules/attr_storage/src/test/attr_storage.c +++ b/modules/attr_storage/src/test/attr_storage.c @@ -46,23 +46,24 @@ static const anjay_dm_object_def_t *const OBJ2 = } }; -#define DM_ATTR_STORAGE_TEST_INIT \ - DM_TEST_INIT_WITH_OBJECTS(&OBJ, &OBJ2, &FAKE_SECURITY2, &FAKE_SERVER); \ - _anjay_dm_transaction_begin(anjay); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_install(anjay)) - -#define DM_ATTR_STORAGE_TEST_FINISH do { \ - (void) mocksocks; \ - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_transaction_finish(anjay, 0)); \ - DM_TEST_FINISH; \ -} while (0) +#define DM_ATTR_STORAGE_TEST_INIT \ + DM_TEST_INIT_WITH_OBJECTS(&OBJ, &OBJ2, &FAKE_SECURITY2, &FAKE_SERVER); \ + _anjay_dm_transaction_begin(anjay); \ + AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_install(anjay)) + +#define DM_ATTR_STORAGE_TEST_FINISH \ + do { \ + (void) mocksocks; \ + AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_transaction_finish(anjay, 0)); \ + DM_TEST_FINISH; \ + } while (0) AVS_UNIT_TEST(attr_storage, instance_create) { DM_ATTR_STORAGE_TEST_INIT; anjay_iid_t iid = 42; _anjay_mock_dm_expect_instance_create(anjay, &OBJ, 42, 1, 0, 42); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_create(anjay, &OBJ, &iid, 1, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_create(anjay, &OBJ, &iid, 1, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 42); iid = 0; _anjay_mock_dm_expect_instance_create(anjay, &OBJ, 0, 1, -42, 69); @@ -77,12 +78,12 @@ AVS_UNIT_TEST(attr_storage, resource_read) { DM_ATTR_STORAGE_TEST_INIT; _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 514, 42, 0, ANJAY_MOCK_DM_NONE); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_read(anjay, &OBJ, 514, 42, NULL, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_resource_read(anjay, &OBJ, 514, 42, NULL, NULL)); _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 14, -7, ANJAY_MOCK_DM_NONE); - AVS_UNIT_ASSERT_EQUAL(_anjay_dm_resource_read(anjay, &OBJ, 69, 14, NULL, - NULL), -7); + AVS_UNIT_ASSERT_EQUAL( + _anjay_dm_resource_read(anjay, &OBJ, 69, 14, NULL, NULL), -7); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); DM_ATTR_STORAGE_TEST_FINISH; } @@ -91,12 +92,12 @@ AVS_UNIT_TEST(attr_storage, resource_write) { DM_ATTR_STORAGE_TEST_INIT; _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 514, 42, ANJAY_MOCK_DM_NONE, 0); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write(anjay, &OBJ, 514, 42, NULL, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_resource_write(anjay, &OBJ, 514, 42, NULL, NULL)); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 14, ANJAY_MOCK_DM_NONE, -7); - AVS_UNIT_ASSERT_EQUAL(_anjay_dm_resource_write(anjay, &OBJ, 69, 14, NULL, - NULL), -7); + AVS_UNIT_ASSERT_EQUAL( + _anjay_dm_resource_write(anjay, &OBJ, 69, 14, NULL, NULL), -7); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); DM_ATTR_STORAGE_TEST_FINISH; } @@ -107,12 +108,12 @@ AVS_UNIT_TEST(attr_storage, resource_execute) { AVS_UNIT_ASSERT_NOT_NULL(ctx); _anjay_mock_dm_expect_resource_execute(anjay, &OBJ, 514, 42, ANJAY_MOCK_DM_NONE, 0); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_execute(anjay, &OBJ, - 514, 42, ctx, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_resource_execute(anjay, &OBJ, 514, 42, ctx, NULL)); _anjay_mock_dm_expect_resource_execute(anjay, &OBJ, 69, 14, ANJAY_MOCK_DM_NONE, -7); - AVS_UNIT_ASSERT_EQUAL(_anjay_dm_resource_execute(anjay, &OBJ, 69, 14, ctx, - NULL), -7); + AVS_UNIT_ASSERT_EQUAL( + _anjay_dm_resource_execute(anjay, &OBJ, 69, 14, ctx, NULL), -7); _anjay_execute_ctx_destroy(&ctx); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); DM_ATTR_STORAGE_TEST_FINISH; @@ -138,7 +139,8 @@ AVS_UNIT_TEST(attr_storage, instance_it) { void *cookie = NULL; // prepare initial state - AVS_LIST_APPEND(&get_fas(anjay)->objects, + AVS_LIST_APPEND( + &get_fas(anjay)->objects, test_object_entry( 42, NULL, @@ -196,28 +198,28 @@ AVS_UNIT_TEST(attr_storage, instance_it) { NULL)); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 0, 0, 7); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 7); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 1, 0, 2); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 2); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 2, 0, 13); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 13); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 3, 0, 3); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 3); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 4, 0, 42); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 42); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 5, 0, ANJAY_IID_INVALID); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, ANJAY_IID_INVALID); AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(get_fas(anjay)->objects), 1); @@ -252,8 +254,8 @@ AVS_UNIT_TEST(attr_storage, instance_it) { get_fas(anjay)->modified_since_persist = false; cookie = NULL; _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 0, -11, 7); - AVS_UNIT_ASSERT_EQUAL(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, - NULL), -11); + AVS_UNIT_ASSERT_EQUAL( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie, NULL), -11); AVS_UNIT_ASSERT_EQUAL(iid, 7); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); DM_ATTR_STORAGE_TEST_FINISH; @@ -263,37 +265,31 @@ AVS_UNIT_TEST(attr_storage, instance_present) { DM_ATTR_STORAGE_TEST_INIT; // prepare initial state - AVS_LIST_APPEND(&get_fas(anjay)->objects, + AVS_LIST_APPEND( + &get_fas(anjay)->objects, test_object_entry( 42, NULL, - test_instance_entry( - 4, NULL, - test_resource_entry(33, NULL), - test_resource_entry(69, NULL), - NULL), - test_instance_entry( - 7, NULL, - test_resource_entry(11, NULL), - NULL), - test_instance_entry( - 21, NULL, - test_resource_entry(22, NULL), - NULL), - test_instance_entry( - 42, NULL, - test_resource_entry(17, NULL), - NULL), + test_instance_entry(4, NULL, test_resource_entry(33, NULL), + test_resource_entry(69, NULL), NULL), + test_instance_entry(7, NULL, test_resource_entry(11, NULL), + NULL), + test_instance_entry(21, NULL, test_resource_entry(22, NULL), + NULL), + test_instance_entry(42, NULL, test_resource_entry(17, NULL), + NULL), NULL)); // tests _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 42, 1); AVS_UNIT_ASSERT_EQUAL(_anjay_dm_instance_present(anjay, &OBJ, 42, NULL), 1); - AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE((*find_object(get_fas(anjay), 42))->instances), 4); + AVS_UNIT_ASSERT_EQUAL( + AVS_LIST_SIZE((*find_object(get_fas(anjay), 42))->instances), 4); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 21, -1); AVS_UNIT_ASSERT_EQUAL(_anjay_dm_instance_present(anjay, &OBJ, 21, NULL), -1); - AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE((*find_object(get_fas(anjay), 42))->instances), 4); + AVS_UNIT_ASSERT_EQUAL( + AVS_LIST_SIZE((*find_object(get_fas(anjay), 42))->instances), 4); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 4, 0); AVS_UNIT_ASSERT_EQUAL(_anjay_dm_instance_present(anjay, &OBJ, 4, NULL), 0); @@ -304,18 +300,12 @@ AVS_UNIT_TEST(attr_storage, instance_present) { get_fas(anjay)->objects, test_object_entry( 42, NULL, - test_instance_entry( - 7, NULL, - test_resource_entry(11, NULL), - NULL), - test_instance_entry( - 21, NULL, - test_resource_entry(22, NULL), - NULL), - test_instance_entry( - 42, NULL, - test_resource_entry(17, NULL), - NULL), + test_instance_entry(7, NULL, test_resource_entry(11, NULL), + NULL), + test_instance_entry(21, NULL, test_resource_entry(22, NULL), + NULL), + test_instance_entry(42, NULL, test_resource_entry(17, NULL), + NULL), NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); DM_ATTR_STORAGE_TEST_FINISH; @@ -325,33 +315,29 @@ AVS_UNIT_TEST(attr_storage, instance_remove) { DM_ATTR_STORAGE_TEST_INIT; // prepare initial state - AVS_LIST_APPEND(&get_fas(anjay)->objects, + AVS_LIST_APPEND( + &get_fas(anjay)->objects, test_object_entry( 42, NULL, - test_instance_entry( - 4, NULL, - test_resource_entry(33, NULL), - test_resource_entry(69, NULL), - NULL), - test_instance_entry( - 7, NULL, - test_resource_entry(11, NULL), - NULL), - test_instance_entry( - 42, NULL, - test_resource_entry(17, NULL), - NULL), + test_instance_entry(4, NULL, test_resource_entry(33, NULL), + test_resource_entry(69, NULL), NULL), + test_instance_entry(7, NULL, test_resource_entry(11, NULL), + NULL), + test_instance_entry(42, NULL, test_resource_entry(17, NULL), + NULL), NULL)); // tests _anjay_mock_dm_expect_instance_remove(anjay, &OBJ, 42, 0); AVS_UNIT_ASSERT_EQUAL(_anjay_dm_instance_remove(anjay, &OBJ, 42, NULL), 0); - AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE((*find_object(get_fas(anjay), 42))->instances), 2); + AVS_UNIT_ASSERT_EQUAL( + AVS_LIST_SIZE((*find_object(get_fas(anjay), 42))->instances), 2); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; _anjay_mock_dm_expect_instance_remove(anjay, &OBJ, 2, 0); AVS_UNIT_ASSERT_EQUAL(_anjay_dm_instance_remove(anjay, &OBJ, 2, NULL), 0); - AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE((*find_object(get_fas(anjay), 42))->instances), 2); + AVS_UNIT_ASSERT_EQUAL( + AVS_LIST_SIZE((*find_object(get_fas(anjay), 42))->instances), 2); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); _anjay_mock_dm_expect_instance_remove(anjay, &OBJ, 7, -44); AVS_UNIT_ASSERT_EQUAL(_anjay_dm_instance_remove(anjay, &OBJ, 7, NULL), -44); @@ -362,15 +348,10 @@ AVS_UNIT_TEST(attr_storage, instance_remove) { get_fas(anjay)->objects, test_object_entry( 42, NULL, - test_instance_entry( - 4, NULL, - test_resource_entry(33, NULL), - test_resource_entry(69, NULL), - NULL), - test_instance_entry( - 7, NULL, - test_resource_entry(11, NULL), - NULL), + test_instance_entry(4, NULL, test_resource_entry(33, NULL), + test_resource_entry(69, NULL), NULL), + test_instance_entry(7, NULL, test_resource_entry(11, NULL), + NULL), NULL)); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); DM_ATTR_STORAGE_TEST_FINISH; @@ -380,30 +361,19 @@ AVS_UNIT_TEST(attr_storage, resource_present) { DM_ATTR_STORAGE_TEST_INIT; // prepare initial state - AVS_LIST_APPEND(&get_fas(anjay)->objects, + AVS_LIST_APPEND( + &get_fas(anjay)->objects, test_object_entry( 42, NULL, - test_instance_entry( - 4, NULL, - test_resource_entry(11, NULL), - test_resource_entry(33, NULL), - test_resource_entry(69, NULL), - NULL), - test_instance_entry( - 7, NULL, - test_resource_entry(11, NULL), - test_resource_entry(42, NULL), - NULL), - test_instance_entry( - 21, NULL, - test_resource_entry(22, NULL), - test_resource_entry(33, NULL), - NULL), - test_instance_entry( - 42, NULL, - test_resource_entry(17, NULL), - test_resource_entry(69, NULL), - NULL), + test_instance_entry(4, NULL, test_resource_entry(11, NULL), + test_resource_entry(33, NULL), + test_resource_entry(69, NULL), NULL), + test_instance_entry(7, NULL, test_resource_entry(11, NULL), + test_resource_entry(42, NULL), NULL), + test_instance_entry(21, NULL, test_resource_entry(22, NULL), + test_resource_entry(33, NULL), NULL), + test_instance_entry(42, NULL, test_resource_entry(17, NULL), + test_resource_entry(69, NULL), NULL), NULL)); // tests @@ -443,20 +413,12 @@ AVS_UNIT_TEST(attr_storage, resource_present) { get_fas(anjay)->objects, test_object_entry( 42, NULL, - test_instance_entry( - 4, NULL, - test_resource_entry(11, NULL), - test_resource_entry(69, NULL), - NULL), - test_instance_entry( - 21, NULL, - test_resource_entry(22, NULL), - test_resource_entry(33, NULL), - NULL), - test_instance_entry( - 42, NULL, - test_resource_entry(17, NULL), - NULL), + test_instance_entry(4, NULL, test_resource_entry(11, NULL), + test_resource_entry(69, NULL), NULL), + test_instance_entry(21, NULL, test_resource_entry(22, NULL), + test_resource_entry(33, NULL), NULL), + test_instance_entry(42, NULL, test_resource_entry(17, NULL), + NULL), NULL)); DM_ATTR_STORAGE_TEST_FINISH; } @@ -469,14 +431,14 @@ AVS_UNIT_TEST(attr_storage, read_object_default_attrs_proxy) { anjay_dm_internal_attrs_t attrs; _anjay_mock_dm_expect_object_read_default_attrs( anjay, &OBJ, 4, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_read_default_attrs( - anjay, &OBJ, 4, &attrs, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_object_read_default_attrs(anjay, &OBJ, 4, &attrs, NULL)); assert_attrs_equal(&attrs, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); _anjay_mock_dm_expect_object_read_default_attrs( anjay, &OBJ, 42, -413, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); - AVS_UNIT_ASSERT_EQUAL(_anjay_dm_object_read_default_attrs( - anjay, &OBJ, 42, &attrs, NULL), + AVS_UNIT_ASSERT_EQUAL(_anjay_dm_object_read_default_attrs(anjay, &OBJ, 42, + &attrs, NULL), -413); _anjay_mock_dm_expect_object_read_default_attrs( @@ -487,8 +449,8 @@ AVS_UNIT_TEST(attr_storage, read_object_default_attrs_proxy) { .max_period = 77 } }); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_read_default_attrs( - anjay, &OBJ, 7, &attrs, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_object_read_default_attrs(anjay, &OBJ, 7, &attrs, NULL)); assert_attrs_equal(&attrs, &(const anjay_dm_internal_attrs_t) { .standard = { @@ -509,7 +471,8 @@ AVS_UNIT_TEST(attr_storage, write_object_default_attrs_proxy) { .min_period = 43, .max_period = ANJAY_ATTRIB_PERIOD_NONE } - }, 0); + }, + 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_write_default_attrs( anjay, &OBJ, 42, &(const anjay_dm_internal_attrs_t) { @@ -517,7 +480,8 @@ AVS_UNIT_TEST(attr_storage, write_object_default_attrs_proxy) { .min_period = 43, .max_period = ANJAY_ATTRIB_PERIOD_NONE } - }, NULL)); + }, + NULL)); _anjay_mock_dm_expect_object_write_default_attrs( anjay, &OBJ, 7, @@ -526,7 +490,8 @@ AVS_UNIT_TEST(attr_storage, write_object_default_attrs_proxy) { .min_period = ANJAY_ATTRIB_PERIOD_NONE, .max_period = 77 } - }, 0); + }, + 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_write_default_attrs( anjay, &OBJ, 7, &(const anjay_dm_internal_attrs_t) { @@ -534,7 +499,8 @@ AVS_UNIT_TEST(attr_storage, write_object_default_attrs_proxy) { .min_period = ANJAY_ATTRIB_PERIOD_NONE, .max_period = 77 } - }, NULL)); + }, + NULL)); _anjay_mock_dm_expect_object_write_default_attrs( anjay, &OBJ, 8, @@ -543,15 +509,18 @@ AVS_UNIT_TEST(attr_storage, write_object_default_attrs_proxy) { .min_period = 88, .max_period = 888 } - }, -8888); + }, + -8888); AVS_UNIT_ASSERT_EQUAL(_anjay_dm_object_write_default_attrs( - anjay, &OBJ, 8, - &(const anjay_dm_internal_attrs_t) { - .standard = { - .min_period = 88, - .max_period = 888 - } - }, NULL), -8888); + anjay, &OBJ, 8, + &(const anjay_dm_internal_attrs_t) { + .standard = { + .min_period = 88, + .max_period = 888 + } + }, + NULL), + -8888); _anjay_mock_dm_expect_object_write_default_attrs( anjay, &OBJ, 9, @@ -560,7 +529,8 @@ AVS_UNIT_TEST(attr_storage, write_object_default_attrs_proxy) { .min_period = 4, .max_period = 99 } - }, 0); + }, + 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_write_default_attrs( anjay, &OBJ, 9, &(const anjay_dm_internal_attrs_t) { @@ -568,7 +538,8 @@ AVS_UNIT_TEST(attr_storage, write_object_default_attrs_proxy) { .min_period = 4, .max_period = 99 } - }, NULL)); + }, + NULL)); _anjay_mock_dm_expect_object_write_default_attrs( anjay, &OBJ, 9, &ANJAY_DM_INTERNAL_ATTRS_EMPTY, 0); @@ -593,23 +564,21 @@ AVS_UNIT_TEST(attr_storage, object_default_attrs) { AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_write_default_attrs( anjay, &OBJ2, 42, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 43, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 43, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_write_default_attrs( anjay, &OBJ2, 7, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = ANJAY_ATTRIB_PERIOD_NONE, - .max_period = 77 - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = 77 + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_write_default_attrs( @@ -619,12 +588,11 @@ AVS_UNIT_TEST(attr_storage, object_default_attrs) { AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_write_default_attrs( anjay, &OBJ2, 9, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 4, - .max_period = 99 - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 4, + .max_period = 99 + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_write_default_attrs( @@ -649,29 +617,25 @@ AVS_UNIT_TEST(attr_storage, object_default_attrs) { NULL)); anjay_dm_internal_attrs_t attrs; - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_read_default_attrs( - anjay, &OBJ2, 4, &attrs, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_object_read_default_attrs(anjay, &OBJ2, 4, &attrs, NULL)); assert_attrs_equal(&attrs, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_read_default_attrs( anjay, &OBJ2, 42, &attrs, NULL)); assert_attrs_equal(&attrs, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 43, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - } - }); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_object_read_default_attrs( - anjay, &OBJ2, 7, &attrs, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 43, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + } }); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_object_read_default_attrs(anjay, &OBJ2, 7, &attrs, NULL)); assert_attrs_equal(&attrs, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = ANJAY_ATTRIB_PERIOD_NONE, - .max_period = 77 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = 77 + } }); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); DM_ATTR_STORAGE_TEST_FINISH; } @@ -689,7 +653,8 @@ AVS_UNIT_TEST(attr_storage, read_instance_default_attrs_proxy) { _anjay_mock_dm_expect_instance_read_default_attrs( anjay, &OBJ, 5, 42, -413, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); AVS_UNIT_ASSERT_EQUAL(_anjay_dm_instance_read_default_attrs( - anjay, &OBJ, 5, 42, &attrs, NULL), -413); + anjay, &OBJ, 5, 42, &attrs, NULL), + -413); _anjay_mock_dm_expect_instance_read_default_attrs( anjay, &OBJ, 7, 4, 0, @@ -704,8 +669,7 @@ AVS_UNIT_TEST(attr_storage, read_instance_default_attrs_proxy) { assert_attrs_equal(&attrs, &(const anjay_dm_internal_attrs_t) { .standard = { - .min_period = - ANJAY_ATTRIB_PERIOD_NONE, + .min_period = ANJAY_ATTRIB_PERIOD_NONE, .max_period = 77 } }); @@ -723,7 +687,8 @@ AVS_UNIT_TEST(attr_storage, write_instance_default_attrs_proxy) { .min_period = 43, .max_period = ANJAY_ATTRIB_PERIOD_NONE } - }, 0); + }, + 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( anjay, &OBJ, 4, 42, &(const anjay_dm_internal_attrs_t) { @@ -731,7 +696,8 @@ AVS_UNIT_TEST(attr_storage, write_instance_default_attrs_proxy) { .min_period = 43, .max_period = ANJAY_ATTRIB_PERIOD_NONE } - }, NULL)); + }, + NULL)); _anjay_mock_dm_expect_instance_write_default_attrs( anjay, &OBJ, 4, 7, @@ -740,7 +706,8 @@ AVS_UNIT_TEST(attr_storage, write_instance_default_attrs_proxy) { .min_period = ANJAY_ATTRIB_PERIOD_NONE, .max_period = 77 } - }, 0); + }, + 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( anjay, &OBJ, 4, 7, &(const anjay_dm_internal_attrs_t) { @@ -748,7 +715,8 @@ AVS_UNIT_TEST(attr_storage, write_instance_default_attrs_proxy) { .min_period = ANJAY_ATTRIB_PERIOD_NONE, .max_period = 77 } - }, NULL)); + }, + NULL)); _anjay_mock_dm_expect_instance_write_default_attrs( anjay, &OBJ, 8, 7, @@ -757,15 +725,18 @@ AVS_UNIT_TEST(attr_storage, write_instance_default_attrs_proxy) { .min_period = 88, .max_period = 888 } - }, -8888); + }, + -8888); AVS_UNIT_ASSERT_EQUAL(_anjay_dm_instance_write_default_attrs( - anjay, &OBJ, 8, 7, - &(const anjay_dm_internal_attrs_t) { - .standard = { - .min_period = 88, - .max_period = 888 - } - }, NULL), -8888); + anjay, &OBJ, 8, 7, + &(const anjay_dm_internal_attrs_t) { + .standard = { + .min_period = 88, + .max_period = 888 + } + }, + NULL), + -8888); _anjay_mock_dm_expect_instance_write_default_attrs( anjay, &OBJ, 9, 4, @@ -774,7 +745,8 @@ AVS_UNIT_TEST(attr_storage, write_instance_default_attrs_proxy) { .min_period = 4, .max_period = 99 } - }, 0); + }, + 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( anjay, &OBJ, 9, 4, &(const anjay_dm_internal_attrs_t) { @@ -782,7 +754,8 @@ AVS_UNIT_TEST(attr_storage, write_instance_default_attrs_proxy) { .min_period = 4, .max_period = 99 } - }, NULL)); + }, + NULL)); _anjay_mock_dm_expect_instance_write_default_attrs( anjay, &OBJ, 9, 4, &ANJAY_DM_INTERNAL_ATTRS_EMPTY, 0); @@ -792,8 +765,7 @@ AVS_UNIT_TEST(attr_storage, write_instance_default_attrs_proxy) { _anjay_mock_dm_expect_instance_write_default_attrs( anjay, &OBJ, 11, 11, &ANJAY_DM_INTERNAL_ATTRS_EMPTY, 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( - anjay, &OBJ, 11, 11, &ANJAY_DM_INTERNAL_ATTRS_EMPTY, - NULL)); + anjay, &OBJ, 11, 11, &ANJAY_DM_INTERNAL_ATTRS_EMPTY, NULL)); AVS_UNIT_ASSERT_NULL(get_fas(anjay)->objects); @@ -806,53 +778,48 @@ AVS_UNIT_TEST(attr_storage, instance_default_attrs) { AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( - anjay, &OBJ2, 42, 2, &ANJAY_DM_INTERNAL_ATTRS_EMPTY, - NULL)); + anjay, &OBJ2, 42, 2, &ANJAY_DM_INTERNAL_ATTRS_EMPTY, NULL)); // nothing actually changed AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); AVS_UNIT_ASSERT_NULL(get_fas(anjay)->objects); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( anjay, &OBJ2, 3, 2, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 4, - .max_period = 9 - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 4, + .max_period = 9 + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( anjay, &OBJ2, 3, 5, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 7, - .max_period = 15 - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 7, + .max_period = 15 + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( anjay, &OBJ2, 9, 5, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 1, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 1, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( anjay, &OBJ2, 14, 5, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = ANJAY_ATTRIB_PERIOD_NONE, - .max_period = 10 - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = 10 + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( @@ -893,22 +860,18 @@ AVS_UNIT_TEST(attr_storage, instance_default_attrs) { anjay, &OBJ2, 3, 2, &attrs, NULL)); assert_attrs_equal(&attrs, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 4, - .max_period = 9 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 4, + .max_period = 9 + } }); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_read_default_attrs( anjay, &OBJ2, 3, 5, &attrs, NULL)); assert_attrs_equal(&attrs, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 7, - .max_period = 15 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 7, + .max_period = 15 + } }); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_read_default_attrs( anjay, &OBJ2, 9, 5, &attrs, NULL)); assert_attrs_equal(&attrs, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); @@ -916,13 +879,10 @@ AVS_UNIT_TEST(attr_storage, instance_default_attrs) { anjay, &OBJ2, 14, 5, &attrs, NULL)); assert_attrs_equal(&attrs, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = - ANJAY_ATTRIB_PERIOD_NONE, - .max_period = 10 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = 10 + } }); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); DM_ATTR_STORAGE_TEST_FINISH; @@ -934,14 +894,15 @@ AVS_UNIT_TEST(attr_storage, read_resource_attrs_proxy) { anjay_dm_internal_res_attrs_t attrs; _anjay_mock_dm_expect_resource_read_attrs( anjay, &OBJ, 5, 6, 4, 0, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_read_attrs( - anjay, &OBJ, 5, 6, 4, &attrs, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_resource_read_attrs(anjay, &OBJ, 5, 6, 4, &attrs, NULL)); assert_res_attrs_equal(&attrs, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); _anjay_mock_dm_expect_resource_read_attrs( anjay, &OBJ, 5, 7, 42, -413, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); - AVS_UNIT_ASSERT_EQUAL(_anjay_dm_resource_read_attrs( - anjay, &OBJ, 5, 7, 42, &attrs, NULL), -413); + AVS_UNIT_ASSERT_EQUAL(_anjay_dm_resource_read_attrs(anjay, &OBJ, 5, 7, 42, + &attrs, NULL), + -413); _anjay_mock_dm_expect_resource_read_attrs( anjay, &OBJ, 7, 17, 4, 0, @@ -956,8 +917,8 @@ AVS_UNIT_TEST(attr_storage, read_resource_attrs_proxy) { .step = .5 } }); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_read_attrs( - anjay, &OBJ, 7, 17, 4, &attrs, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_resource_read_attrs(anjay, &OBJ, 7, 17, 4, &attrs, NULL)); assert_res_attrs_equal(&attrs, &(const anjay_dm_internal_res_attrs_t) { .standard = { @@ -989,7 +950,8 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs_proxy) { .less_than = ANJAY_ATTRIB_VALUE_NONE, .step = ANJAY_ATTRIB_VALUE_NONE } - }, 0); + }, + 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( anjay, &OBJ, 4, 9, 42, &(const anjay_dm_internal_res_attrs_t) { @@ -1002,7 +964,8 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs_proxy) { .less_than = ANJAY_ATTRIB_VALUE_NONE, .step = ANJAY_ATTRIB_VALUE_NONE } - }, NULL)); + }, + NULL)); _anjay_mock_dm_expect_resource_write_attrs( anjay, &OBJ, 4, 111, 7, @@ -1016,7 +979,8 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs_proxy) { .less_than = ANJAY_ATTRIB_VALUE_NONE, .step = ANJAY_ATTRIB_VALUE_NONE } - }, 0); + }, + 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( anjay, &OBJ, 4, 111, 7, &(const anjay_dm_internal_res_attrs_t) { @@ -1029,7 +993,8 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs_proxy) { .less_than = ANJAY_ATTRIB_VALUE_NONE, .step = ANJAY_ATTRIB_VALUE_NONE } - }, NULL)); + }, + NULL)); _anjay_mock_dm_expect_resource_write_attrs( anjay, &OBJ, 8, 9, 7, @@ -1043,20 +1008,24 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs_proxy) { .less_than = 8.8, .step = 88.8 } - }, -8888); - AVS_UNIT_ASSERT_EQUAL(_anjay_dm_resource_write_attrs( - anjay, &OBJ, 8, 9, 7, - &(const anjay_dm_internal_res_attrs_t) { - .standard = { - .common = { - .min_period = ANJAY_ATTRIB_PERIOD_NONE, - .max_period = ANJAY_ATTRIB_PERIOD_NONE + }, + -8888); + AVS_UNIT_ASSERT_EQUAL( + _anjay_dm_resource_write_attrs( + anjay, &OBJ, 8, 9, 7, + &(const anjay_dm_internal_res_attrs_t) { + .standard = { + .common = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + }, + .greater_than = 0.8, + .less_than = 8.8, + .step = 88.8 + } }, - .greater_than = 0.8, - .less_than = 8.8, - .step = 88.8 - } - }, NULL), -8888); + NULL), + -8888); _anjay_mock_dm_expect_resource_write_attrs( anjay, &OBJ, 9, 23, 4, @@ -1070,7 +1039,8 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs_proxy) { .less_than = ANJAY_ATTRIB_VALUE_NONE, .step = ANJAY_ATTRIB_VALUE_NONE, } - }, 0); + }, + 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( anjay, &OBJ, 9, 23, 4, &(const anjay_dm_internal_res_attrs_t) { @@ -1083,19 +1053,18 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs_proxy) { .less_than = ANJAY_ATTRIB_VALUE_NONE, .step = ANJAY_ATTRIB_VALUE_NONE, } - }, NULL)); + }, + NULL)); _anjay_mock_dm_expect_resource_write_attrs( anjay, &OBJ, 9, 23, 4, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( - anjay, &OBJ, 9, 23, 4, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, - NULL)); + anjay, &OBJ, 9, 23, 4, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, NULL)); _anjay_mock_dm_expect_resource_write_attrs( anjay, &OBJ, 11, 11, 11, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, 0); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( - anjay, &OBJ, 11, 11, 11, - &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, NULL)); + anjay, &OBJ, 11, 11, 11, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, NULL)); AVS_UNIT_ASSERT_NULL(get_fas(anjay)->objects); @@ -1107,43 +1076,42 @@ AVS_UNIT_TEST(attr_storage, read_resource_attrs) { DM_ATTR_STORAGE_TEST_INIT; AVS_LIST_APPEND(&get_fas(anjay)->objects, - test_object_entry( - 69, NULL, - test_instance_entry( - 3, NULL, - test_resource_entry( - 1, - test_resource_attrs( - 42, 1, 2, 3.0, 4.0, 5.0, - ANJAY_DM_CON_ATTR_DEFAULT), + test_object_entry( + 69, NULL, + test_instance_entry( + 3, NULL, + test_resource_entry( + 1, + test_resource_attrs( + 42, 1, 2, 3.0, 4.0, 5.0, + ANJAY_DM_CON_ATTR_DEFAULT), + NULL), NULL), - NULL), - NULL)); + NULL)); anjay_dm_internal_res_attrs_t attrs; - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_read_attrs( - anjay, &OBJ2, 3, 1, 42, &attrs, NULL)); - assert_res_attrs_equal(&attrs, - &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = 1, - .max_period = 2 - }, - .greater_than = 3.0, - .less_than = 4.0, - .step = 5.0 - } - }); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_read_attrs( - anjay, &OBJ2, 3, 1, 4, &attrs, NULL)); + AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_read_attrs(anjay, &OBJ2, 3, 1, + 42, &attrs, NULL)); + assert_res_attrs_equal( + &attrs, + &(const anjay_dm_internal_res_attrs_t) { + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = 1, + .max_period = 2 + }, + .greater_than = 3.0, + .less_than = 4.0, + .step = 5.0 + } }); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_resource_read_attrs(anjay, &OBJ2, 3, 1, 4, &attrs, NULL)); assert_res_attrs_equal(&attrs, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_read_attrs( - anjay, &OBJ2, 3, 2, 4, &attrs, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_resource_read_attrs(anjay, &OBJ2, 3, 2, 4, &attrs, NULL)); assert_res_attrs_equal(&attrs, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_read_attrs( - anjay, &OBJ2, 2, 2, 4, &attrs, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_resource_read_attrs(anjay, &OBJ2, 2, 2, 4, &attrs, NULL)); assert_res_attrs_equal(&attrs, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); @@ -1155,25 +1123,23 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs) { AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( - anjay, &OBJ2, 2, 5, 3, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, - NULL)); + anjay, &OBJ2, 2, 5, 3, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, NULL)); // nothing actually changed AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); AVS_UNIT_ASSERT_NULL(get_fas(anjay)->objects); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( anjay, &OBJ2, 2, 3, 1, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = 1, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - }, - .greater_than = 34.0, - .less_than = ANJAY_ATTRIB_VALUE_NONE, - .step = ANJAY_ATTRIB_VALUE_NONE - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = 1, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + }, + .greater_than = 34.0, + .less_than = ANJAY_ATTRIB_VALUE_NONE, + .step = ANJAY_ATTRIB_VALUE_NONE + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; @@ -1202,33 +1168,31 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs) { AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( anjay, &OBJ2, 2, 5, 3, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = 4, - .max_period = 5 - }, - .greater_than = 6.0, - .less_than = 7.0, - .step = 8.0 - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = 4, + .max_period = 5 + }, + .greater_than = 6.0, + .less_than = 7.0, + .step = 8.0 + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( anjay, &OBJ2, 2, 3, 5, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = 9, - .max_period = 10 - }, - .greater_than = 11.0, - .less_than = 22.0, - .step = 33.0 - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = 9, + .max_period = 10 + }, + .greater_than = 11.0, + .less_than = 22.0, + .step = 33.0 + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; @@ -1266,22 +1230,19 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs) { AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( anjay, &OBJ2, 2, 4, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 4, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 4, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( - anjay, &OBJ2, 2, 3, 5, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, - NULL)); + anjay, &OBJ2, 2, 3, 5, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( - anjay, &OBJ2, 2, 3, 1, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, - NULL)); + anjay, &OBJ2, 2, 3, 1, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; @@ -1307,8 +1268,7 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs) { NULL)); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( - anjay, &OBJ2, 2, 5, 3, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, - NULL)); + anjay, &OBJ2, 2, 5, 3, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; @@ -1330,17 +1290,16 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs) { AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( anjay, &OBJ2, 2, 3, 5, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = 9, - .max_period = 10 - }, - .greater_than = 11.0, - .less_than = 22.0, - .step = 33.0 - } - }, NULL)); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = 9, + .max_period = 10 + }, + .greater_than = 11.0, + .less_than = 22.0, + .step = 33.0 + } }, + NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_write_default_attrs( @@ -1366,8 +1325,7 @@ AVS_UNIT_TEST(attr_storage, write_resource_attrs) { NULL)); AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( - anjay, &OBJ2, 2, 3, 5, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, - NULL)); + anjay, &OBJ2, 2, 3, 5, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY, NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_NULL(get_fas(anjay)->objects); @@ -1391,7 +1349,8 @@ AVS_UNIT_TEST(attr_storage, ssid_it) { // /1/10/0 == 2 // /1/11/0 == -5 (invalid) - AVS_LIST_APPEND(&get_fas(anjay)->objects, + AVS_LIST_APPEND( + &get_fas(anjay)->objects, test_object_entry( 42, test_default_attrlist( @@ -1457,20 +1416,20 @@ AVS_UNIT_TEST(attr_storage, ssid_it) { anjay_iid_t iid; void *cookie = NULL; _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 0, 0, 514); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it( - anjay, &FAKE_SECURITY2, &iid, &cookie, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &FAKE_SECURITY2, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 514); _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 1, 0, 7); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it( - anjay, &FAKE_SECURITY2, &iid, &cookie, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &FAKE_SECURITY2, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 7); _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 2, 0, 42); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it( - anjay, &FAKE_SECURITY2, &iid, &cookie, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &FAKE_SECURITY2, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 42); _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 3, 0, 4); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it( - anjay, &FAKE_SECURITY2, &iid, &cookie, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &FAKE_SECURITY2, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 4); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 4, 0, @@ -1487,8 +1446,8 @@ AVS_UNIT_TEST(attr_storage, ssid_it) { _anjay_mock_dm_expect_resource_present(anjay, &FAKE_SECURITY2, 514, 10, 1); _anjay_mock_dm_expect_resource_read(anjay, &FAKE_SECURITY2, 514, 10, 0, ANJAY_MOCK_DM_INT(0, -4)); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it( - anjay, &FAKE_SECURITY2, &iid, &cookie, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &FAKE_SECURITY2, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_EQUAL(iid, ANJAY_IID_INVALID); @@ -1520,16 +1479,16 @@ AVS_UNIT_TEST(attr_storage, ssid_it) { cookie = NULL; _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SERVER, 0, 0, 11); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it( - anjay, &FAKE_SERVER, &iid, &cookie, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &FAKE_SERVER, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 11); _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SERVER, 1, 0, 9); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it( - anjay, &FAKE_SERVER, &iid, &cookie, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &FAKE_SERVER, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 9); _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SERVER, 2, 0, 10); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it( - anjay, &FAKE_SERVER, &iid, &cookie, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &FAKE_SERVER, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); AVS_UNIT_ASSERT_EQUAL(iid, 10); _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SERVER, 3, 0, @@ -1543,8 +1502,8 @@ AVS_UNIT_TEST(attr_storage, ssid_it) { _anjay_mock_dm_expect_resource_present(anjay, &FAKE_SERVER, 11, 0, 1); _anjay_mock_dm_expect_resource_read(anjay, &FAKE_SERVER, 11, 0, 0, ANJAY_MOCK_DM_INT(0, -5)); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it( - anjay, &FAKE_SERVER, &iid, &cookie, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &FAKE_SERVER, &iid, &cookie, NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; AVS_UNIT_ASSERT_EQUAL(iid, ANJAY_IID_INVALID); @@ -1574,7 +1533,8 @@ AVS_UNIT_TEST(attr_storage, ssid_it) { AVS_UNIT_TEST(attr_storage, ssid_remove) { DM_ATTR_STORAGE_TEST_INIT; - AVS_LIST_APPEND(&get_fas(anjay)->objects, + AVS_LIST_APPEND( + &get_fas(anjay)->objects, test_object_entry( 42, test_default_attrlist( @@ -1642,8 +1602,8 @@ AVS_UNIT_TEST(attr_storage, ssid_remove) { ANJAY_MOCK_DM_INT(0, 2)); _anjay_mock_dm_expect_instance_remove(anjay, &FAKE_SECURITY2, 7, 0); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_remove( - anjay, &FAKE_SECURITY2, 7, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_remove(anjay, &FAKE_SECURITY2, 7, NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; @@ -1715,8 +1675,8 @@ AVS_UNIT_TEST(attr_storage, ssid_remove) { ANJAY_MOCK_DM_INT(0, 42)); _anjay_mock_dm_expect_instance_remove(anjay, &FAKE_SERVER, 19, 0); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_remove( - anjay, &FAKE_SERVER, 19, NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_remove(anjay, &FAKE_SERVER, 19, NULL)); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; @@ -1782,63 +1742,58 @@ AVS_UNIT_TEST(attr_storage, nested_iterations) { anjay_iid_t iid; // prepare initial state - AVS_LIST_APPEND(&get_fas(anjay)->objects, - test_object_entry( - 42, NULL, - test_instance_entry(1, NULL, NULL), - test_instance_entry(2, NULL, NULL), - test_instance_entry(3, NULL, NULL), - test_instance_entry(4, NULL, NULL), - test_instance_entry(5, NULL, NULL), - NULL)); + AVS_LIST_APPEND( + &get_fas(anjay)->objects, + test_object_entry(42, NULL, test_instance_entry(1, NULL, NULL), + test_instance_entry(2, NULL, NULL), + test_instance_entry(3, NULL, NULL), + test_instance_entry(4, NULL, NULL), + test_instance_entry(5, NULL, NULL), NULL)); void *cookie1 = NULL; void *cookie2 = NULL; _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 0, 0, 1); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 1); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 1, 0, 2); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 2); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 0, 0, 1); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 1); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 1, 0, 2); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 2); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 2, 0, 3); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 3); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 3, 0, ANJAY_IID_INVALID); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, ANJAY_IID_INVALID); AVS_UNIT_ASSERT_TRUE(anjay_attr_storage_is_modified(anjay)); get_fas(anjay)->modified_since_persist = false; _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 2, 0, 3); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 3); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 3, 0, ANJAY_IID_INVALID); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, ANJAY_IID_INVALID); AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(get_fas(anjay)->objects), 1); assert_object_equal( get_fas(anjay)->objects, - test_object_entry( - 42, NULL, - test_instance_entry(1, NULL, NULL), - test_instance_entry(2, NULL, NULL), - test_instance_entry(3, NULL, NULL), - NULL)); + test_object_entry(42, NULL, test_instance_entry(1, NULL, NULL), + test_instance_entry(2, NULL, NULL), + test_instance_entry(3, NULL, NULL), NULL)); DM_ATTR_STORAGE_TEST_FINISH; } @@ -1848,62 +1803,57 @@ AVS_UNIT_TEST(attr_storage, parallel_iterations) { anjay_iid_t iid; // prepare initial state - AVS_LIST_APPEND(&get_fas(anjay)->objects, - test_object_entry( - 42, NULL, - test_instance_entry(1, NULL, NULL), - test_instance_entry(2, NULL, NULL), - test_instance_entry(3, NULL, NULL), - test_instance_entry(4, NULL, NULL), - test_instance_entry(5, NULL, NULL), - NULL)); + AVS_LIST_APPEND( + &get_fas(anjay)->objects, + test_object_entry(42, NULL, test_instance_entry(1, NULL, NULL), + test_instance_entry(2, NULL, NULL), + test_instance_entry(3, NULL, NULL), + test_instance_entry(4, NULL, NULL), + test_instance_entry(5, NULL, NULL), NULL)); void *cookie1 = NULL; void *cookie2 = NULL; _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 0, 0, 1); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 1); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 0, 0, 1); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 1); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 1, 0, 2); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 2); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 1, 0, 2); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 2); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 2, 0, 3); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 3); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 2, 0, 3); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, 3); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 3, 0, ANJAY_IID_INVALID); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie1, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, ANJAY_IID_INVALID); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 3, 0, ANJAY_IID_INVALID); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, - NULL)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_dm_instance_it(anjay, &OBJ, &iid, &cookie2, NULL)); AVS_UNIT_ASSERT_EQUAL(iid, ANJAY_IID_INVALID); AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(get_fas(anjay)->objects), 1); assert_object_equal( get_fas(anjay)->objects, - test_object_entry( - 42, NULL, - test_instance_entry(1, NULL, NULL), - test_instance_entry(2, NULL, NULL), - test_instance_entry(3, NULL, NULL), - test_instance_entry(4, NULL, NULL), - test_instance_entry(5, NULL, NULL), - NULL)); + test_object_entry(42, NULL, test_instance_entry(1, NULL, NULL), + test_instance_entry(2, NULL, NULL), + test_instance_entry(3, NULL, NULL), + test_instance_entry(4, NULL, NULL), + test_instance_entry(5, NULL, NULL), NULL)); AVS_UNIT_ASSERT_FALSE(anjay_attr_storage_is_modified(anjay)); DM_ATTR_STORAGE_TEST_FINISH; @@ -1913,8 +1863,8 @@ AVS_UNIT_TEST(set_attribs, fail_on_null_attribs) { DM_TEST_INIT_WITH_OBJECTS(&OBJ_NOATTRS, &FAKE_SECURITY2); AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_install(anjay)); - AVS_UNIT_ASSERT_FAILED( - anjay_attr_storage_set_object_attrs(anjay, 1, OBJ_NOATTRS->oid, NULL)); + AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_set_object_attrs( + anjay, 1, OBJ_NOATTRS->oid, NULL)); AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_set_instance_attrs( anjay, 1, OBJ_NOATTRS->oid, 30, NULL)); AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_set_resource_attrs( @@ -1931,11 +1881,8 @@ AVS_UNIT_TEST(set_attribs, fail_on_invalid_ssid) { DM_TEST_INIT_WITH_OBJECTS(&OBJ_NOATTRS, &FAKE_SECURITY2); AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_install(anjay)); - const anjay_ssid_t SSIDS_TO_TEST[] = { - ANJAY_SSID_ANY, - ANJAY_SSID_BOOTSTRAP, - 341 - }; + const anjay_ssid_t SSIDS_TO_TEST[] = { ANJAY_SSID_ANY, ANJAY_SSID_BOOTSTRAP, + 341 }; // Assumming no Security Instances for (int i = 0; i < (int) AVS_ARRAY_SIZE(SSIDS_TO_TEST); ++i) { // object @@ -1974,15 +1921,16 @@ AVS_UNIT_TEST(set_attribs, fail_on_invalid_ssid) { if (SSIDS_TO_TEST[i] != ANJAY_SSID_BOOTSTRAP) { _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 0, 0, 1); _anjay_mock_dm_expect_resource_present( - anjay, &FAKE_SECURITY2, 1, ANJAY_DM_RID_SECURITY_BOOTSTRAP, 1); + anjay, &FAKE_SECURITY2, 1, ANJAY_DM_RID_SECURITY_BOOTSTRAP, + 1); _anjay_mock_dm_expect_resource_read(anjay, &FAKE_SECURITY2, 1, - ANJAY_DM_RID_SECURITY_BOOTSTRAP, 0, - ANJAY_MOCK_DM_BOOL(0, true)); + ANJAY_DM_RID_SECURITY_BOOTSTRAP, + 0, ANJAY_MOCK_DM_BOOL(0, true)); _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 1, 0, ANJAY_IID_INVALID); - _anjay_mock_dm_expect_resource_present(anjay, &FAKE_SECURITY2, 1, - ANJAY_DM_RID_SECURITY_SSID, 0); + _anjay_mock_dm_expect_resource_present( + anjay, &FAKE_SECURITY2, 1, ANJAY_DM_RID_SECURITY_SSID, 0); } AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_set_object_attrs( anjay, SSIDS_TO_TEST[i], OBJ_NOATTRS->oid, FAKE_DM_ATTRS)); @@ -1992,15 +1940,16 @@ AVS_UNIT_TEST(set_attribs, fail_on_invalid_ssid) { if (SSIDS_TO_TEST[i] != ANJAY_SSID_BOOTSTRAP) { _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 0, 0, 1); _anjay_mock_dm_expect_resource_present( - anjay, &FAKE_SECURITY2, 1, ANJAY_DM_RID_SECURITY_BOOTSTRAP, 1); + anjay, &FAKE_SECURITY2, 1, ANJAY_DM_RID_SECURITY_BOOTSTRAP, + 1); _anjay_mock_dm_expect_resource_read(anjay, &FAKE_SECURITY2, 1, - ANJAY_DM_RID_SECURITY_BOOTSTRAP, 0, - ANJAY_MOCK_DM_BOOL(0, true)); + ANJAY_DM_RID_SECURITY_BOOTSTRAP, + 0, ANJAY_MOCK_DM_BOOL(0, true)); _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 1, 0, ANJAY_IID_INVALID); - _anjay_mock_dm_expect_resource_present(anjay, &FAKE_SECURITY2, 1, - ANJAY_DM_RID_SECURITY_SSID, 0); + _anjay_mock_dm_expect_resource_present( + anjay, &FAKE_SECURITY2, 1, ANJAY_DM_RID_SECURITY_SSID, 0); } AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_set_instance_attrs( anjay, SSIDS_TO_TEST[i], OBJ_NOATTRS->oid, 0, FAKE_DM_ATTRS)); @@ -2010,15 +1959,16 @@ AVS_UNIT_TEST(set_attribs, fail_on_invalid_ssid) { if (SSIDS_TO_TEST[i] != ANJAY_SSID_BOOTSTRAP) { _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 0, 0, 1); _anjay_mock_dm_expect_resource_present( - anjay, &FAKE_SECURITY2, 1, ANJAY_DM_RID_SECURITY_BOOTSTRAP, 1); + anjay, &FAKE_SECURITY2, 1, ANJAY_DM_RID_SECURITY_BOOTSTRAP, + 1); _anjay_mock_dm_expect_resource_read(anjay, &FAKE_SECURITY2, 1, - ANJAY_DM_RID_SECURITY_BOOTSTRAP, 0, - ANJAY_MOCK_DM_BOOL(0, true)); + ANJAY_DM_RID_SECURITY_BOOTSTRAP, + 0, ANJAY_MOCK_DM_BOOL(0, true)); _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SECURITY2, 1, 0, ANJAY_IID_INVALID); - _anjay_mock_dm_expect_resource_present(anjay, &FAKE_SECURITY2, 1, - ANJAY_DM_RID_SECURITY_SSID, 0); + _anjay_mock_dm_expect_resource_present( + anjay, &FAKE_SECURITY2, 1, ANJAY_DM_RID_SECURITY_SSID, 0); } AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_set_resource_attrs( anjay, SSIDS_TO_TEST[i], OBJ_NOATTRS->oid, 0, 0, @@ -2076,8 +2026,9 @@ AVS_UNIT_TEST(set_attribs, fail_on_invalid_iid) { ANJAY_DM_RID_SECURITY_SSID, 0, ANJAY_MOCK_DM_INT(0, 1)); - AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_set_resource_attrs( - anjay, 1, OBJ_NOATTRS->oid, ANJAY_IID_INVALID, 1, FAKE_DM_RES_ATTRS)); + AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_set_resource_attrs( + anjay, 1, OBJ_NOATTRS->oid, ANJAY_IID_INVALID, 1, + FAKE_DM_RES_ATTRS)); DM_TEST_FINISH; } diff --git a/modules/attr_storage/src/test/attr_storage_test.h b/modules/attr_storage/src/test/attr_storage_test.h index 5c0b93b96..4148ce3b3 100644 --- a/modules/attr_storage/src/test/attr_storage_test.h +++ b/modules/attr_storage/src/test/attr_storage_test.h @@ -47,7 +47,8 @@ static fas_resource_attrs_t *test_resource_attrs(anjay_ssid_t ssid, } /* Using anjay_rid_t instead of int / unsigned in va_start is UB */ -static fas_resource_entry_t *test_resource_entry(unsigned /*anjay_rid_t*/ rid, ...) { +static fas_resource_entry_t *test_resource_entry(unsigned /*anjay_rid_t*/ rid, + ...) { assert(rid <= UINT16_MAX); fas_resource_entry_t *resource = AVS_LIST_NEW_ELEMENT(fas_resource_entry_t); AVS_UNIT_ASSERT_NOT_NULL(resource); @@ -90,10 +91,8 @@ test_default_attrlist(fas_default_attrs_t *entry, ...) { return attrlist; } -static fas_instance_entry_t * -test_instance_entry(anjay_iid_t iid, - AVS_LIST(fas_default_attrs_t) default_attrs, - ...) { +static fas_instance_entry_t *test_instance_entry( + anjay_iid_t iid, AVS_LIST(fas_default_attrs_t) default_attrs, ...) { fas_instance_entry_t *instance = AVS_LIST_NEW_ELEMENT(fas_instance_entry_t); AVS_UNIT_ASSERT_NOT_NULL(instance); instance->iid = iid; @@ -108,10 +107,8 @@ test_instance_entry(anjay_iid_t iid, return instance; } -static fas_object_entry_t * -test_object_entry(anjay_oid_t oid, - AVS_LIST(fas_default_attrs_t) default_attrs, - ...) { +static fas_object_entry_t *test_object_entry( + anjay_oid_t oid, AVS_LIST(fas_default_attrs_t) default_attrs, ...) { fas_object_entry_t *object = AVS_LIST_NEW_ELEMENT(fas_object_entry_t); AVS_UNIT_ASSERT_NOT_NULL(object); object->oid = oid; @@ -231,4 +228,3 @@ static void assert_object_equal(fas_object_entry_t *actual, } #endif /* ATTR_STORAGE_TEST_H */ - diff --git a/modules/attr_storage/src/test/persistence.c b/modules/attr_storage/src/test/persistence.c index 2d4b008c0..62e914fd3 100644 --- a/modules/attr_storage/src/test/persistence.c +++ b/modules/attr_storage/src/test/persistence.c @@ -17,8 +17,8 @@ #include #include -#include #include +#include #include #include @@ -28,27 +28,27 @@ #include "../mod_attr_storage.h" #include "attr_storage_test.h" -#define PERSIST_TEST_INIT(Size) \ - char buf[Size]; \ - avs_stream_outbuf_t outbuf = AVS_STREAM_OUTBUF_STATIC_INITIALIZER; \ - avs_stream_outbuf_set_buffer(&outbuf, buf, sizeof(buf)); \ - anjay_t *anjay = _anjay_test_dm_init(DM_TEST_CONFIGURATION()); \ - AVS_UNIT_ASSERT_NOT_NULL(anjay); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_install(anjay)) - -#define PERSISTENCE_TEST_FINISH \ - do { \ - _anjay_mock_dm_expect_clean(); \ - _anjay_test_dm_finish(anjay); \ - } while (0) - -#define PERSIST_TEST_CHECK(Data) \ - do { \ - AVS_UNIT_ASSERT_EQUAL(sizeof(Data) - 1, \ - avs_stream_outbuf_offset(&outbuf)); \ - AVS_UNIT_ASSERT_EQUAL_BYTES_SIZED(Data, buf, sizeof(Data) - 1); \ - PERSISTENCE_TEST_FINISH; \ - } while (0) +#define PERSIST_TEST_INIT(Size) \ + char buf[Size]; \ + avs_stream_outbuf_t outbuf = AVS_STREAM_OUTBUF_STATIC_INITIALIZER; \ + avs_stream_outbuf_set_buffer(&outbuf, buf, sizeof(buf)); \ + anjay_t *anjay = _anjay_test_dm_init(DM_TEST_CONFIGURATION()); \ + AVS_UNIT_ASSERT_NOT_NULL(anjay); \ + AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_install(anjay)) + +#define PERSISTENCE_TEST_FINISH \ + do { \ + _anjay_mock_dm_expect_clean(); \ + _anjay_test_dm_finish(anjay); \ + } while (0) + +#define PERSIST_TEST_CHECK(Data) \ + do { \ + AVS_UNIT_ASSERT_EQUAL(sizeof(Data) - 1, \ + avs_stream_outbuf_offset(&outbuf)); \ + AVS_UNIT_ASSERT_EQUAL_BYTES_SIZED(Data, buf, sizeof(Data) - 1); \ + PERSISTENCE_TEST_FINISH; \ + } while (0) #define MAGIC_HEADER_V0 "FAS\0" #define MAGIC_HEADER_V2 "FAS\2" @@ -60,16 +60,14 @@ AVS_UNIT_TEST(attr_storage_persistence, persist_empty) { PERSIST_TEST_CHECK(MAGIC_HEADER_V2 "\x00\x00\x00\x00"); } -#define INSTALL_FAKE_OBJECT(Oid, ...) \ - const anjay_dm_object_def_t *const OBJ##Oid = \ - &(const anjay_dm_object_def_t) { \ - .oid = Oid, \ - .supported_rids = ANJAY_DM_SUPPORTED_RIDS(__VA_ARGS__), \ - .handlers = { \ - ANJAY_MOCK_DM_HANDLERS_NOATTRS \ - } \ - }; \ - AVS_UNIT_ASSERT_SUCCESS(anjay_register_object(anjay, &OBJ##Oid)) +#define INSTALL_FAKE_OBJECT(Oid, ...) \ + const anjay_dm_object_def_t *const OBJ##Oid = \ + &(const anjay_dm_object_def_t) { \ + .oid = Oid, \ + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(__VA_ARGS__), \ + .handlers = { ANJAY_MOCK_DM_HANDLERS_NOATTRS } \ + }; \ + AVS_UNIT_ASSERT_SUCCESS(anjay_register_object(anjay, &OBJ##Oid)) static void write_obj_attrs(anjay_t *anjay, anjay_oid_t oid, @@ -103,65 +101,64 @@ static void write_res_attrs(anjay_t *anjay, const anjay_dm_object_def_t *const *obj = _anjay_dm_find_object_by_oid(anjay, oid); AVS_UNIT_ASSERT_NOT_NULL(obj); - AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs( - anjay, obj, iid, rid, ssid, attrs, NULL)); + AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_resource_write_attrs(anjay, obj, iid, rid, + ssid, attrs, NULL)); } static const char PERSIST_TEST_DATA[] = - MAGIC_HEADER_V2 - "\x00\x00\x00\x03" // 3 objects - "\x00\x04" // OID 4 - "\x00\x00\x00\x02" // 2 object-level default attrs - "\x00\x0E" // SSID 14 + MAGIC_HEADER_V2 "\x00\x00\x00\x03" // 3 objects + "\x00\x04" // OID 4 + "\x00\x00\x00\x02" // 2 object-level default attrs + "\x00\x0E" // SSID 14 "\xFF\xFF\xFF\xFF" // min period "\x00\x00\x00\x03" // max period - "\xFF" // confirmable - "\x00\x21" // SSID 33 + "\xFF" // confirmable + "\x00\x21" // SSID 33 "\x00\x00\x00\x2A" // min period "\xFF\xFF\xFF\xFF" // max period - "\x00" // confirmable - "\x00\x00\x00\x00" // 0 instance entries - "\x00\x2A" // OID 42 - "\x00\x00\x00\x00" // 0 object-level default attrs - "\x00\x00\x00\x01" // 1 instance entry - "\x00\x01" // IID 1 + "\x00" // confirmable + "\x00\x00\x00\x00" // 0 instance entries + "\x00\x2A" // OID 42 + "\x00\x00\x00\x00" // 0 object-level default attrs + "\x00\x00\x00\x01" // 1 instance entry + "\x00\x01" // IID 1 "\x00\x00\x00\x01" // 1 instance-level default attr - "\x00\x02" // SSID 2 - "\x00\x00\x00\x07" // min period - "\x00\x00\x00\x0D" // max period - "\xFF" // confirmable + "\x00\x02" // SSID 2 + "\x00\x00\x00\x07" // min period + "\x00\x00\x00\x0D" // max period + "\xFF" // confirmable "\x00\x00\x00\x01" // 1 resource entry - "\x00\x03" // RID 3 - "\x00\x00\x00\x02" // 2 attr entries - "\x00\x02" // SSID 2 - "\xFF\xFF\xFF\xFF" // min period - "\xFF\xFF\xFF\xFF" // max period - /* greater than */ "\x3F\xF0\x00\x00\x00\x00\x00\x00" - /* less than */ "\xBF\xF0\x00\x00\x00\x00\x00\x00" - /* step */ "\x7F\xF8\x00\x00\x00\x00\x00\x00" - "\x01" // confirmable - "\x00\x07" // SSID 7 - "\x00\x00\x00\x01" // min period - "\x00\x00\x00\x0E" // max period - /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* step */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - "\xFF" // confirmable - "\x02\x05" // OID 517 - "\x00\x00\x00\x00" // 0 object-level default attrs - "\x00\x00\x00\x01" // 1 instance entry - "\x02\x04" // IID 516 + "\x00\x03" // RID 3 + "\x00\x00\x00\x02" // 2 attr entries + "\x00\x02" // SSID 2 + "\xFF\xFF\xFF\xFF" // min period + "\xFF\xFF\xFF\xFF" // max period + /* greater than */ "\x3F\xF0\x00\x00\x00\x00\x00\x00" + /* less than */ "\xBF\xF0\x00\x00\x00\x00\x00\x00" + /* step */ "\x7F\xF8\x00\x00\x00\x00\x00\x00" + "\x01" // confirmable + "\x00\x07" // SSID 7 + "\x00\x00\x00\x01" // min period + "\x00\x00\x00\x0E" // max period + /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* step */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + "\xFF" // confirmable + "\x02\x05" // OID 517 + "\x00\x00\x00\x00" // 0 object-level default attrs + "\x00\x00\x00\x01" // 1 instance entry + "\x02\x04" // IID 516 "\x00\x00\x00\x00" // 0 instance-level default attrs "\x00\x00\x00\x01" // 1 resource entry - "\x02\x03" // RID 515 - "\x00\x00\x00\x01" // 1 attr entry - "\x02\x02" // SSID 514 - "\x00\x00\x00\x21" // min period - "\xFF\xFF\xFF\xFF" // max period - /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* step */ "\x40\x45\x00\x00\x00\x00\x00\x00" - "\xFF"; // confirmable + "\x02\x03" // RID 515 + "\x00\x00\x00\x01" // 1 attr entry + "\x02\x02" // SSID 514 + "\x00\x00\x00\x21" // min period + "\xFF\xFF\xFF\xFF" // max period + /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* step */ "\x40\x45\x00\x00\x00\x00\x00\x00" + "\xFF"; // confirmable #ifdef WITH_CUSTOM_ATTRIBUTES static void persist_test_fill(anjay_t *anjay) { @@ -179,20 +176,16 @@ static void persist_test_fill(anjay_t *anjay) { }); write_obj_attrs(anjay, 4, 14, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = ANJAY_ATTRIB_PERIOD_NONE, - .max_period = 3 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = 3 + } }); write_inst_attrs(anjay, 42, 1, 2, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 7, - .max_period = 13 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 7, + .max_period = 13 + } }); write_res_attrs(anjay, 42, 1, 3, 2, &(const anjay_dm_internal_res_attrs_t) { .custom = { @@ -212,30 +205,26 @@ static void persist_test_fill(anjay_t *anjay) { }); write_res_attrs(anjay, 42, 1, 3, 7, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = 1, - .max_period = 14 - }, - .greater_than = ANJAY_ATTRIB_VALUE_NONE, - .less_than = ANJAY_ATTRIB_VALUE_NONE, - .step = ANJAY_ATTRIB_VALUE_NONE - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = 1, + .max_period = 14 + }, + .greater_than = ANJAY_ATTRIB_VALUE_NONE, + .less_than = ANJAY_ATTRIB_VALUE_NONE, + .step = ANJAY_ATTRIB_VALUE_NONE + } }); write_res_attrs(anjay, 517, 516, 515, 514, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = 33, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - }, - .greater_than = ANJAY_ATTRIB_VALUE_NONE, - .less_than = ANJAY_ATTRIB_VALUE_NONE, - .step = 42.0 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = 33, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + }, + .greater_than = ANJAY_ATTRIB_VALUE_NONE, + .less_than = ANJAY_ATTRIB_VALUE_NONE, + .step = 42.0 + } }); } AVS_UNIT_TEST(attr_storage_persistence, persist_full) { @@ -261,12 +250,12 @@ AVS_UNIT_TEST(attr_storage_persistence, persist_not_enough_space) { } #endif // WITH_CUSTOM_ATTRIBUTES -#define RESTORE_TEST_INIT(Data) \ - avs_stream_inbuf_t inbuf = AVS_STREAM_INBUF_STATIC_INITIALIZER; \ - avs_stream_inbuf_set_buffer(&inbuf, (Data), sizeof(Data) - 1); \ - anjay_t *anjay = _anjay_test_dm_init(DM_TEST_CONFIGURATION()); \ - AVS_UNIT_ASSERT_NOT_NULL(anjay); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_install(anjay)) +#define RESTORE_TEST_INIT(Data) \ + avs_stream_inbuf_t inbuf = AVS_STREAM_INBUF_STATIC_INITIALIZER; \ + avs_stream_inbuf_set_buffer(&inbuf, (Data), sizeof(Data) - 1); \ + anjay_t *anjay = _anjay_test_dm_init(DM_TEST_CONFIGURATION()); \ + AVS_UNIT_ASSERT_NOT_NULL(anjay); \ + AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_install(anjay)) AVS_UNIT_TEST(attr_storage_persistence, restore_empty) { RESTORE_TEST_INIT(""); @@ -288,15 +277,15 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_one_object) { INSTALL_FAKE_OBJECT(42, 3); _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 0, 0, 1); - _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 1, 0, - ANJAY_IID_INVALID); + _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 1, 0, ANJAY_IID_INVALID); _anjay_mock_dm_expect_resource_present(anjay, &OBJ42, 1, 3, 1); AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_restore( anjay, (avs_stream_abstract_t *) &inbuf)); AVS_UNIT_ASSERT_EQUAL( AVS_LIST_SIZE(_anjay_attr_storage_get(anjay)->objects), 1); - assert_object_equal(_anjay_attr_storage_get(anjay)->objects, + assert_object_equal( + _anjay_attr_storage_get(anjay)->objects, test_object_entry( 42, NULL, test_instance_entry( @@ -341,22 +330,17 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_all_objects) { // this will be cleared write_inst_attrs(anjay, 69, 68, 67, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 66, - .max_period = 65 - } - }); - - _anjay_mock_dm_expect_instance_it(anjay, &OBJ4, 0, 0, - ANJAY_IID_INVALID); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 66, + .max_period = 65 + } }); + + _anjay_mock_dm_expect_instance_it(anjay, &OBJ4, 0, 0, ANJAY_IID_INVALID); _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 0, 0, 1); - _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 1, 0, - ANJAY_IID_INVALID); + _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 1, 0, ANJAY_IID_INVALID); _anjay_mock_dm_expect_resource_present(anjay, &OBJ42, 1, 3, 1); _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 0, 0, 516); - _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 1, 0, - ANJAY_IID_INVALID); + _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 1, 0, ANJAY_IID_INVALID); _anjay_mock_dm_expect_resource_present(anjay, &OBJ517, 516, 515, 1); AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_restore( anjay, (avs_stream_abstract_t *) &inbuf)); @@ -365,7 +349,8 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_all_objects) { AVS_LIST_SIZE(_anjay_attr_storage_get(anjay)->objects), 3); // object 4 - assert_object_equal(_anjay_attr_storage_get(anjay)->objects, + assert_object_equal( + _anjay_attr_storage_get(anjay)->objects, test_object_entry( 4, test_default_attrlist( @@ -377,7 +362,8 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_all_objects) { NULL)); // object 42 - assert_object_equal(AVS_LIST_NEXT(_anjay_attr_storage_get(anjay)->objects), + assert_object_equal( + AVS_LIST_NEXT(_anjay_attr_storage_get(anjay)->objects), test_object_entry( 42, NULL, test_instance_entry( @@ -434,52 +420,49 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_all_objects) { } static const char CLEARING_TEST_DATA[] = - MAGIC_HEADER_V0 - "\x00\x00\x00\x02" // 2 objects - "\x00\x2A" // OID 42 - "\x00\x00\x00\x00" // 0 object-level default attrs - "\x00\x00\x00\x01" // 1 instance entry - "\x00\x01" // IID 1 + MAGIC_HEADER_V0 "\x00\x00\x00\x02" // 2 objects + "\x00\x2A" // OID 42 + "\x00\x00\x00\x00" // 0 object-level default attrs + "\x00\x00\x00\x01" // 1 instance entry + "\x00\x01" // IID 1 "\x00\x00\x00\x00" // 0 instance-level default attr "\x00\x00\x00\x01" // 1 resource entry - "\x00\x03" // RID 3 - "\x00\x00\x00\x02" // 2 attr entries - "\x00\x02" // SSID 2 - "\xFF\xFF\xFF\xFF" // min period - "\xFF\xFF\xFF\xFF" // max period - /* greater than */ "\x3F\xF0\x00\x00\x00\x00\x00\x00" - /* less than */ "\xBF\xF0\x00\x00\x00\x00\x00\x00" - /* step */ "\x7F\xF8\x00\x00\x00\x00\x00\x00" - "\x00\x07" // SSID 7 - "\x00\x00\x00\x01" // min period - "\x00\x00\x00\x0E" // max period - /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* step */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - "\x02\x05" // OID 517 - "\x00\x00\x00\x00" // 0 object-level default attrs - "\x00\x00\x00\x01" // 1 instance entry - "\x02\x04" // IID 516 + "\x00\x03" // RID 3 + "\x00\x00\x00\x02" // 2 attr entries + "\x00\x02" // SSID 2 + "\xFF\xFF\xFF\xFF" // min period + "\xFF\xFF\xFF\xFF" // max period + /* greater than */ "\x3F\xF0\x00\x00\x00\x00\x00\x00" + /* less than */ "\xBF\xF0\x00\x00\x00\x00\x00\x00" + /* step */ "\x7F\xF8\x00\x00\x00\x00\x00\x00" + "\x00\x07" // SSID 7 + "\x00\x00\x00\x01" // min period + "\x00\x00\x00\x0E" // max period + /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* step */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + "\x02\x05" // OID 517 + "\x00\x00\x00\x00" // 0 object-level default attrs + "\x00\x00\x00\x01" // 1 instance entry + "\x02\x04" // IID 516 "\x00\x00\x00\x00" // 0 instance-level default attrs "\x00\x00\x00\x01" // 1 resource entry - "\x02\x03" // RID 515 - "\x00\x00\x00\x01" // 1 attr entry - "\x02\x02" // SSID 514 - "\x00\x00\x00\x21" // min period - "\xFF\xFF\xFF\xFF" // max period - /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* step */ "\x40\x45\x00\x00\x00\x00\x00\x00"; + "\x02\x03" // RID 515 + "\x00\x00\x00\x01" // 1 attr entry + "\x02\x02" // SSID 514 + "\x00\x00\x00\x21" // min period + "\xFF\xFF\xFF\xFF" // max period + /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* step */ "\x40\x45\x00\x00\x00\x00\x00\x00"; AVS_UNIT_TEST(attr_storage_persistence, restore_no_instances) { RESTORE_TEST_INIT(CLEARING_TEST_DATA); INSTALL_FAKE_OBJECT(42, 3); INSTALL_FAKE_OBJECT(517, 3, 515); - _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 0, 0, - ANJAY_IID_INVALID); - _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 0, 0, - ANJAY_IID_INVALID); + _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 0, 0, ANJAY_IID_INVALID); + _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 0, 0, ANJAY_IID_INVALID); AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_restore( anjay, (avs_stream_abstract_t *) &inbuf)); AVS_UNIT_ASSERT_NULL(_anjay_attr_storage_get(anjay)->objects); @@ -492,11 +475,9 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_no_supported_resources) { INSTALL_FAKE_OBJECT(517, 0); _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 0, 0, 1); - _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 1, 0, - ANJAY_IID_INVALID); + _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 1, 0, ANJAY_IID_INVALID); _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 0, 0, 516); - _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 1, 0, - ANJAY_IID_INVALID); + _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 1, 0, ANJAY_IID_INVALID); AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_restore( anjay, (avs_stream_abstract_t *) &inbuf)); AVS_UNIT_ASSERT_NULL(_anjay_attr_storage_get(anjay)->objects); @@ -509,12 +490,10 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_no_present_resources) { INSTALL_FAKE_OBJECT(517, 515); _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 0, 0, 1); - _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 1, 0, - ANJAY_IID_INVALID); + _anjay_mock_dm_expect_instance_it(anjay, &OBJ42, 1, 0, ANJAY_IID_INVALID); _anjay_mock_dm_expect_resource_present(anjay, &OBJ42, 1, 3, 0); _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 0, 0, 516); - _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 1, 0, - ANJAY_IID_INVALID); + _anjay_mock_dm_expect_instance_it(anjay, &OBJ517, 1, 0, ANJAY_IID_INVALID); _anjay_mock_dm_expect_resource_present(anjay, &OBJ517, 516, 515, 0); AVS_UNIT_ASSERT_SUCCESS(anjay_attr_storage_restore( anjay, (avs_stream_abstract_t *) &inbuf)); @@ -523,43 +502,42 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_no_present_resources) { } static const char RESTORE_BROKEN_DATA[] = - MAGIC_HEADER_V0 - "\x00\x00\x00\x03" // 3 objects - "\x00\x04" // OID 4 - "\x00\x00\x00\x02" // 2 object-level default attrs - "\x00\x0E" // SSID 14 + MAGIC_HEADER_V0 "\x00\x00\x00\x03" // 3 objects + "\x00\x04" // OID 4 + "\x00\x00\x00\x02" // 2 object-level default attrs + "\x00\x0E" // SSID 14 "\xFF\xFF\xFF\xFF" // min period "\x00\x00\x00\x03" // max period "\x7f\xf8\x00\x00\x00\x00\x00\x00" // greater than "\x7f\xf8\x00\x00\x00\x00\x00\x00" // less than "\x7f\xf8\x00\x00\x00\x00\x00\x00" // step - "\x00\x21" // SSID 33 - "\x00\x00\x00\x2A" // min period - "\xFF\xFF\xFF\xFF" // max period + "\x00\x21" // SSID 33 + "\x00\x00\x00\x2A" // min period + "\xFF\xFF\xFF\xFF" // max period "\x7f\xf8\x00\x00\x00\x00\x00\x00" // greater than "\x7f\xf8\x00\x00\x00\x00\x00\x00" // less than "\x7f\xf8\x00\x00\x00\x00\x00\x00" // step - "\x00\x00\x00\x00" // 0 instance entries - "\x00\x2A" // OID 42 - "\x00\x00\x00\x00" // 0 object-level default attrs - "\x00\x00\x00\x01" // 1 instance entry - "\x00\x01" // IID 1 + "\x00\x00\x00\x00" // 0 instance entries + "\x00\x2A" // OID 42 + "\x00\x00\x00\x00" // 0 object-level default attrs + "\x00\x00\x00\x01" // 1 instance entry + "\x00\x01" // IID 1 "\x00\x00\x00\x01" // 1 instance-level default attr - "\x00\x02" // SSID 2 - "\x00\x00\x00\x07" // min period - "\x00\x00\x00\x0D" // max period - "\x7f\xf8\x00\x00\x00\x00\x00\x00" // greater than - "\x7f\xf8\x00\x00\x00\x00\x00\x00" // less than - "\x7f\xf8\x00\x00\x00\x00\x00\x00" // step - "\x00\x00\x00\x01" // 1 resource entry - "\x00\x03" // RID 3 - "\x00\x00\x00\x02" // 2 attr entries - "\x00\x02" // SSID 2 - "\xFF\xFF\xFF\xFF" // min period - "\xFF\xFF\xFF\xFF" // max period - /* greater than */ "\x3F\xF0\x00\x00\x00\x00\x00\x00" - /* less than */ "\xBF\xF0\x00\x00\x00\x00\x00\x00" - "\x7f"; /* premature end of data */ + "\x00\x02" // SSID 2 + "\x00\x00\x00\x07" // min period + "\x00\x00\x00\x0D" // max period + "\x7f\xf8\x00\x00\x00\x00\x00\x00" // greater than + "\x7f\xf8\x00\x00\x00\x00\x00\x00" // less than + "\x7f\xf8\x00\x00\x00\x00\x00\x00" // step + "\x00\x00\x00\x01" // 1 resource entry + "\x00\x03" // RID 3 + "\x00\x00\x00\x02" // 2 attr entries + "\x00\x02" // SSID 2 + "\xFF\xFF\xFF\xFF" // min period + "\xFF\xFF\xFF\xFF" // max period + /* greater than */ "\x3F\xF0\x00\x00\x00\x00\x00\x00" + /* less than */ "\xBF\xF0\x00\x00\x00\x00\x00\x00" + "\x7f"; /* premature end of data */ AVS_UNIT_TEST(attr_storage_persistence, restore_broken_stream) { RESTORE_TEST_INIT(RESTORE_BROKEN_DATA); @@ -570,12 +548,10 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_broken_stream) { // this will be cleared write_inst_attrs(anjay, 517, 518, 519, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 520, - .max_period = 521, - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 520, + .max_period = 521, + } }); AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_restore( anjay, (avs_stream_abstract_t *) &inbuf)); @@ -585,56 +561,55 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_broken_stream) { } static const char INSANE_TEST_DATA[] = - MAGIC_HEADER_V0 - "\x00\x00\x00\x03" // 3 objects - "\x00\x04" // OID 4 - "\x00\x00\x00\x02" // 2 object-level default attrs - "\x00\x0E" // SSID 14 + MAGIC_HEADER_V0 "\x00\x00\x00\x03" // 3 objects + "\x00\x04" // OID 4 + "\x00\x00\x00\x02" // 2 object-level default attrs + "\x00\x0E" // SSID 14 "\xFF\xFF\xFF\xFF" // min period "\x00\x00\x00\x03" // max period - "\x00\x21" // SSID 33 + "\x00\x21" // SSID 33 "\x00\x00\x00\x2A" // min period "\xFF\xFF\xFF\xFF" // max period - "\x00\x00\x00\x00" // 0 instance entries - "\x00\x2A" // OID 42 - "\x00\x00\x00\x00" // 0 object-level default attrs - "\x00\x00\x00\x01" // 1 instance entry - "\x00\x01" // IID 1 + "\x00\x00\x00\x00" // 0 instance entries + "\x00\x2A" // OID 42 + "\x00\x00\x00\x00" // 0 object-level default attrs + "\x00\x00\x00\x01" // 1 instance entry + "\x00\x01" // IID 1 "\x00\x00\x00\x01" // 1 instance-level default attr - "\x00\x02" // SSID 2 - "\x00\x00\x00\x07" // min period - "\x00\x00\x00\x0D" // max period + "\x00\x02" // SSID 2 + "\x00\x00\x00\x07" // min period + "\x00\x00\x00\x0D" // max period "\x00\x00\x00\x01" // 1 resource entry - "\x00\x03" // RID 3 - "\x00\x00\x00\x02" // 2 attr entries - /*********** INVALID SSID ORDER FOLLOW ***********/ - "\x00\x07" // SSID 7 - "\x00\x00\x00\x01" // min period - "\x00\x00\x00\x0E" // max period - /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* step */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - "\x00\x02" // SSID 2 - "\xFF\xFF\xFF\xFF" // min period - "\xFF\xFF\xFF\xFF" // max period - /* greater than */ "\x3F\xF0\x00\x00\x00\x00\x00\x00" - /* less than */ "\xBF\xF0\x00\x00\x00\x00\x00\x00" - /* step */ "\x7F\xF8\x00\x00\x00\x00\x00\x00" - /************ INVALID SSID ORDER END ************/ - "\x02\x05" // OID 517 - "\x00\x00\x00\x00" // 0 object-level default attrs - "\x00\x00\x00\x01" // 1 instance entry - "\x02\x04" // IID 516 + "\x00\x03" // RID 3 + "\x00\x00\x00\x02" // 2 attr entries + /*********** INVALID SSID ORDER FOLLOW ***********/ + "\x00\x07" // SSID 7 + "\x00\x00\x00\x01" // min period + "\x00\x00\x00\x0E" // max period + /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* step */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + "\x00\x02" // SSID 2 + "\xFF\xFF\xFF\xFF" // min period + "\xFF\xFF\xFF\xFF" // max period + /* greater than */ "\x3F\xF0\x00\x00\x00\x00\x00\x00" + /* less than */ "\xBF\xF0\x00\x00\x00\x00\x00\x00" + /* step */ "\x7F\xF8\x00\x00\x00\x00\x00\x00" + /************ INVALID SSID ORDER END ************/ + "\x02\x05" // OID 517 + "\x00\x00\x00\x00" // 0 object-level default attrs + "\x00\x00\x00\x01" // 1 instance entry + "\x02\x04" // IID 516 "\x00\x00\x00\x00" // 0 instance-level default attrs "\x00\x00\x00\x01" // 1 resource entry - "\x02\x03" // RID 515 - "\x00\x00\x00\x01" // 1 attr entry - "\x02\x02" // SSID 514 - "\x00\x00\x00\x21" // min period - "\xFF\xFF\xFF\xFF" // max period - /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* step */ "\x40\x45\x00\x00\x00\x00\x00\x00"; + "\x02\x03" // RID 515 + "\x00\x00\x00\x01" // 1 attr entry + "\x02\x02" // SSID 514 + "\x00\x00\x00\x21" // min period + "\xFF\xFF\xFF\xFF" // max period + /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* step */ "\x40\x45\x00\x00\x00\x00\x00\x00"; AVS_UNIT_TEST(attr_storage_persistence, restore_insane_data) { RESTORE_TEST_INIT(INSANE_TEST_DATA); @@ -645,12 +620,10 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_insane_data) { // this will be cleared write_inst_attrs(anjay, 517, 518, 519, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 520, - .max_period = 521 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 520, + .max_period = 521 + } }); AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_restore( anjay, (avs_stream_abstract_t *) &inbuf)); @@ -660,77 +633,74 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_insane_data) { } static const char TEST_DATA_WITH_EMPTY_OID_ATTRS[] = - MAGIC_HEADER_V0 - "\x00\x00\x00\x01" // 3 objects - "\x00\x04" // OID 4 - "\x00\x00\x00\x02" // 2 object-level default attrs - "\x00\x0E" // SSID 14 + MAGIC_HEADER_V0 "\x00\x00\x00\x01" // 3 objects + "\x00\x04" // OID 4 + "\x00\x00\x00\x02" // 2 object-level default attrs + "\x00\x0E" // SSID 14 "\xFF\xFF\xFF\xFF" // min period "\x00\x00\x00\x03" // max period - "\x00\x21" // SSID 33 - /********* EMPTY ATTRIBUTES FOLLOW *********/ + "\x00\x21" // SSID 33 + /********* EMPTY ATTRIBUTES FOLLOW *********/ "\xFF\xFF\xFF\xFF" // min period "\xFF\xFF\xFF\xFF" // max period - /*********** EMPTY ATTRIBUTES END ***********/ - "\x00\x00\x00\x00"; // 0 instance entries - + /*********** EMPTY ATTRIBUTES END ***********/ + "\x00\x00\x00\x00"; // 0 instance entries static const char TEST_DATA_WITH_EMPTY_IID_ATTRS[] = - MAGIC_HEADER_V0 - "\x00\x00\x00\x01" // 3 objects - "\x00\x2A" // OID 42 - "\x00\x00\x00\x00" // 0 object-level default attrs - "\x00\x00\x00\x01" // 1 instance entry - "\x00\x01" // IID 1 + MAGIC_HEADER_V0 "\x00\x00\x00\x01" // 3 objects + "\x00\x2A" // OID 42 + "\x00\x00\x00\x00" // 0 object-level default attrs + "\x00\x00\x00\x01" // 1 instance entry + "\x00\x01" // IID 1 "\x00\x00\x00\x01" // 1 instance-level default attr - "\x00\x02" // SSID 2 + "\x00\x02" // SSID 2 /********* EMPTY ATTRIBUTES FOLLOW *********/ - "\xFF\xFF\xFF\xFF" // min period - "\xFF\xFF\xFF\xFF" // max period + "\xFF\xFF\xFF\xFF" // min period + "\xFF\xFF\xFF\xFF" // max period /*********** EMPTY ATTRIBUTES END ***********/ "\x00\x00\x00\x01" // 1 resource entry - "\x00\x03" // RID 3 - "\x00\x00\x00\x01" // 2 attr entries - "\x00\x02" // SSID 2 - "\x00\x00\x00\x01" // min period - "\x00\x00\x00\x0E" // max period - /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* step */ "\x7f\xf8\x00\x00\x00\x00\x00\x00"; + "\x00\x03" // RID 3 + "\x00\x00\x00\x01" // 2 attr entries + "\x00\x02" // SSID 2 + "\x00\x00\x00\x01" // min period + "\x00\x00\x00\x0E" // max period + /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* step */ "\x7f\xf8\x00\x00\x00\x00\x00\x00"; static const char TEST_DATA_WITH_EMPTY_RID_ATTRS[] = - MAGIC_HEADER_V0 - "\x00\x00\x00\x01" // 3 objects - "\x02\x05" // OID 517 - "\x00\x00\x00\x00" // 0 object-level default attrs - "\x00\x00\x00\x01" // 1 instance entry - "\x02\x04" // IID 516 + MAGIC_HEADER_V0 "\x00\x00\x00\x01" // 3 objects + "\x02\x05" // OID 517 + "\x00\x00\x00\x00" // 0 object-level default attrs + "\x00\x00\x00\x01" // 1 instance entry + "\x02\x04" // IID 516 "\x00\x00\x00\x00" // 0 instance-level default attrs "\x00\x00\x00\x01" // 1 resource entry - "\x02\x03" // RID 515 - "\x00\x00\x00\x01" // 1 attr entry - "\x02\x02" // SSID 514 - /********* EMPTY ATTRIBUTES FOLLOW *********/ - "\xFF\xFF\xFF\xFF" // min period - "\xFF\xFF\xFF\xFF" // max period - /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" - /* step */ "\x7f\xf8\x00\x00\x00\x00\x00\x00"; - /*********** EMPTY ATTRIBUTES END ***********/ - -#define DEFINE_UNIT_TEST_RESTORE_DATA_WITH_EMPTY(Suffix) \ -AVS_UNIT_TEST(attr_storage_persistence, restore_data_with_empty_##Suffix) { \ - RESTORE_TEST_INIT(TEST_DATA_WITH_EMPTY_##Suffix); \ - INSTALL_FAKE_OBJECT(4, 3); \ - INSTALL_FAKE_OBJECT(42, 3); \ - INSTALL_FAKE_OBJECT(517, 3, 515); \ - \ - AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_restore( \ - anjay, (avs_stream_abstract_t *) &inbuf)); \ - \ - AVS_UNIT_ASSERT_NULL(_anjay_attr_storage_get(anjay)->objects); \ - PERSISTENCE_TEST_FINISH; \ -} + "\x02\x03" // RID 515 + "\x00\x00\x00\x01" // 1 attr entry + "\x02\x02" // SSID 514 + /********* EMPTY ATTRIBUTES FOLLOW *********/ + "\xFF\xFF\xFF\xFF" // min period + "\xFF\xFF\xFF\xFF" // max period + /* greater than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* less than */ "\x7f\xf8\x00\x00\x00\x00\x00\x00" + /* step */ "\x7f\xf8\x00\x00\x00\x00\x00\x00"; +/*********** EMPTY ATTRIBUTES END ***********/ + +#define DEFINE_UNIT_TEST_RESTORE_DATA_WITH_EMPTY(Suffix) \ + AVS_UNIT_TEST(attr_storage_persistence, \ + restore_data_with_empty_##Suffix) { \ + RESTORE_TEST_INIT(TEST_DATA_WITH_EMPTY_##Suffix); \ + INSTALL_FAKE_OBJECT(4, 3); \ + INSTALL_FAKE_OBJECT(42, 3); \ + INSTALL_FAKE_OBJECT(517, 3, 515); \ + \ + AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_restore( \ + anjay, (avs_stream_abstract_t *) &inbuf)); \ + \ + AVS_UNIT_ASSERT_NULL(_anjay_attr_storage_get(anjay)->objects); \ + PERSISTENCE_TEST_FINISH; \ + } DEFINE_UNIT_TEST_RESTORE_DATA_WITH_EMPTY(OID_ATTRS) DEFINE_UNIT_TEST_RESTORE_DATA_WITH_EMPTY(IID_ATTRS) @@ -752,20 +722,19 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_data_with_bad_magic) { } static const char TEST_DATA_DUPLICATE_OID[] = - MAGIC_HEADER_V0 - "\x00\x00\x00\x02" // 2 objects - "\x00\x04" // OID 4 - "\x00\x00\x00\x01" // 1 object-level default attr - "\x00\x0E" // SSID 14 - "\xFF\xFF\xFF\xFF" // min period - "\x00\x00\x00\x03" // max period - "\x00\x00\x00\x00" // 0 instance entries - "\x00\x04" // OID 4 - "\x00\x00\x00\x01" // 1 object-level default attr - "\x00\x07" // SSID 7 - "\xFF\xFF\xFF\xFF" // min period - "\x00\x00\x00\x03" // max period - "\x00\x00\x00\x00"; // 0 instance entries + MAGIC_HEADER_V0 "\x00\x00\x00\x02" // 2 objects + "\x00\x04" // OID 4 + "\x00\x00\x00\x01" // 1 object-level default attr + "\x00\x0E" // SSID 14 + "\xFF\xFF\xFF\xFF" // min period + "\x00\x00\x00\x03" // max period + "\x00\x00\x00\x00" // 0 instance entries + "\x00\x04" // OID 4 + "\x00\x00\x00\x01" // 1 object-level default attr + "\x00\x07" // SSID 7 + "\xFF\xFF\xFF\xFF" // min period + "\x00\x00\x00\x03" // max period + "\x00\x00\x00\x00"; // 0 instance entries AVS_UNIT_TEST(attr_storage_persistence, restore_duplicate_oid) { RESTORE_TEST_INIT(TEST_DATA_DUPLICATE_OID); @@ -774,12 +743,10 @@ AVS_UNIT_TEST(attr_storage_persistence, restore_duplicate_oid) { // this will be cleared write_inst_attrs(anjay, 4, 5, 6, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 7, - .max_period = 8 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 7, + .max_period = 8 + } }); AVS_UNIT_ASSERT_FAILED(anjay_attr_storage_restore( anjay, (avs_stream_abstract_t *) &inbuf)); diff --git a/modules/fw_update/include_public/anjay/fw_update.h b/modules/fw_update/include_public/anjay/fw_update.h index a6c6e440a..f51e945bf 100644 --- a/modules/fw_update/include_public/anjay/fw_update.h +++ b/modules/fw_update/include_public/anjay/fw_update.h @@ -23,7 +23,8 @@ extern "C" { #endif -/** @{ +/** @name Firmware update result codes + * @{ * The following result codes may be returned from * @ref anjay_fw_update_stream_write_t, @ref anjay_fw_update_stream_finish_t or * @ref anjay_fw_update_perform_upgrade_t to control the value of the Update @@ -33,9 +34,9 @@ extern "C" { * attempting to use other negated value will be checked and cause a fall-back * to a value default for a given handler. */ -#define ANJAY_FW_UPDATE_ERR_NOT_ENOUGH_SPACE (-2) -#define ANJAY_FW_UPDATE_ERR_OUT_OF_MEMORY (-3) -#define ANJAY_FW_UPDATE_ERR_INTEGRITY_FAILURE (-5) +#define ANJAY_FW_UPDATE_ERR_NOT_ENOUGH_SPACE (-2) +#define ANJAY_FW_UPDATE_ERR_OUT_OF_MEMORY (-3) +#define ANJAY_FW_UPDATE_ERR_INTEGRITY_FAILURE (-5) #define ANJAY_FW_UPDATE_ERR_UNSUPPORTED_PACKAGE_TYPE (-6) /** @} */ @@ -209,9 +210,8 @@ anjay_fw_update_stream_open_t(void *user_ptr, * returned, an equivalent value will be set in the Update Result * Resource. */ -typedef int anjay_fw_update_stream_write_t(void *user_ptr, - const void *data, - size_t length); +typedef int +anjay_fw_update_stream_write_t(void *user_ptr, const void *data, size_t length); /** * Closes the download stream and prepares the firmware package to be flashed. @@ -383,6 +383,22 @@ anjay_fw_update_get_security_info_t(void *user_ptr, avs_net_security_info_t *out_security_info, const char *download_uri); +/** + * Returns tx_params used to override default ones. + * + * If this handler is not implemented at all (with the corresponding field set + * to NULL), udp_tx_params from anjay_t object are used. + * + * @param user_ptr Opaque pointer to user data, as passed to + * @ref anjay_fw_update_install . + * + * @param download_uri Target firmware URI. + * + * @returns Object with CoAP transmission parameters. + */ +typedef avs_coap_tx_params_t +anjay_fw_update_get_coap_tx_params_t(void *user_ptr, const char *download_uri); + /** * Handler callbacks that shall implement the platform-specific part of firmware * update process. @@ -456,6 +472,10 @@ typedef struct { /** Queries security information that shall be used for an encrypted * connection; @ref anjay_fw_update_get_security_info_t */ anjay_fw_update_get_security_info_t *get_security_info; + + /** Queries CoAP transmission parameters to be used during firmware + * update. */ + anjay_fw_update_get_coap_tx_params_t *get_coap_tx_params; } anjay_fw_update_handlers_t; /** @@ -485,11 +505,11 @@ typedef struct { * * @returns 0 on success, or a negative value in case of error. */ -int -anjay_fw_update_install(anjay_t *anjay, - const anjay_fw_update_handlers_t *handlers, - void *user_arg, - const anjay_fw_update_initial_state_t *initial_state); +int anjay_fw_update_install( + anjay_t *anjay, + const anjay_fw_update_handlers_t *handlers, + void *user_arg, + const anjay_fw_update_initial_state_t *initial_state); /** * Helper function that is used as a default implementation of security @@ -517,8 +537,8 @@ anjay_fw_update_install(anjay_t *anjay, * heap-allocated; to release all memory allocated for it, it is enough * to call avs_free() on the returned pointer. */ -avs_net_security_info_t * -anjay_fw_update_load_security_from_dm(anjay_t *anjay, const char *uri); +avs_net_security_info_t *anjay_fw_update_load_security_from_dm(anjay_t *anjay, + const char *uri); #ifdef __cplusplus } diff --git a/modules/fw_update/src/fw_dm_security.c b/modules/fw_update/src/fw_dm_security.c index 25e82c3af..27aa5a6c1 100644 --- a/modules/fw_update/src/fw_dm_security.c +++ b/modules/fw_update/src/fw_dm_security.c @@ -70,18 +70,20 @@ static int try_security_instance(anjay_t *anjay, MAKE_RESOURCE_PATH(ANJAY_DM_OID_SECURITY, security_iid, ANJAY_DM_RID_SECURITY_SERVER_URI); - if (_anjay_dm_res_read_string(anjay, &path, - raw_server_url, sizeof(raw_server_url))) { - fw_log(WARNING, "could not read LwM2M server URI from " - "/%" PRIu16 "/%" PRIu16 "/%" PRIu16, + if (_anjay_dm_res_read_string(anjay, &path, raw_server_url, + sizeof(raw_server_url))) { + fw_log(WARNING, + "could not read LwM2M server URI from /%" PRIu16 "/%" PRIu16 + "/%" PRIu16, path.oid, path.iid, path.rid); return ANJAY_FOREACH_CONTINUE; } avs_url_t *server_url = avs_url_parse(raw_server_url); if (!server_url) { - fw_log(WARNING, "Could not parse URL from " - "/%" PRIu16 "/%" PRIu16 "/%" PRIu16 ": %s", + fw_log(WARNING, + "Could not parse URL from /%" PRIu16 "/%" PRIu16 "/%" PRIu16 + ": %s", path.oid, path.iid, path.rid, raw_server_url); return ANJAY_FOREACH_CONTINUE; } @@ -96,14 +98,16 @@ static int try_security_instance(anjay_t *anjay, anjay, &new_result->security_info, &new_result->dtls_keys, security_iid, ANJAY_CONNECTION_UDP); if (get_result) { - fw_log(WARNING, "Could not read security information for server" - " /%" PRIu16 "/%" PRIu16, + fw_log(WARNING, + "Could not read security information for server " + "/%" PRIu16 "/%" PRIu16, ANJAY_DM_OID_SECURITY, security_iid); } else if (!new_result->dtls_keys.pk_or_identity_size - && !new_result->dtls_keys.server_pk_or_identity_size - && !new_result->dtls_keys.secret_key_size) { - fw_log(DEBUG, "Server /%" PRIu16 "/%" PRIu16 " does not use " - "encrypted connection, ignoring", + && !new_result->dtls_keys.server_pk_or_identity_size + && !new_result->dtls_keys.secret_key_size) { + fw_log(DEBUG, + "Server /%" PRIu16 "/%" PRIu16 + " does not use encrypted connection, ignoring", ANJAY_DM_OID_SECURITY, security_iid); } else { avs_free(args->result); @@ -144,13 +148,15 @@ anjay_fw_update_load_security_from_dm(anjay_t *anjay, const char *raw_url) { .result = NULL, .url = url }; - _anjay_dm_foreach_instance(anjay, security_obj, - try_security_instance, &args); + _anjay_dm_foreach_instance(anjay, security_obj, try_security_instance, + &args); avs_url_free(url); if (!args.result) { - fw_log(WARNING, "Matching security information not found in data model " - "for URL: %s", raw_url); + fw_log(WARNING, + "Matching security information not found in data model for URL: " + "%s", + raw_url); } return args.result; } diff --git a/modules/fw_update/src/fw_update.c b/modules/fw_update/src/fw_update.c index aea0aa2ed..67b6f9d57 100644 --- a/modules/fw_update/src/fw_update.c +++ b/modules/fw_update/src/fw_update.c @@ -36,15 +36,15 @@ VISIBILITY_SOURCE_BEGIN #define FW_OID 5 -#define FW_RES_PACKAGE 0 -#define FW_RES_PACKAGE_URI 1 -#define FW_RES_UPDATE 2 -#define FW_RES_STATE 3 -#define FW_RES_UPDATE_RESULT 5 -#define FW_RES_PKG_NAME 6 -#define FW_RES_PKG_VERSION 7 -#define FW_RES_UPDATE_PROTOCOL_SUPPORT 8 -#define FW_RES_UPDATE_DELIVERY_METHOD 9 +#define FW_RES_PACKAGE 0 +#define FW_RES_PACKAGE_URI 1 +#define FW_RES_UPDATE 2 +#define FW_RES_STATE 3 +#define FW_RES_UPDATE_RESULT 5 +#define FW_RES_PKG_NAME 6 +#define FW_RES_PKG_VERSION 7 +#define FW_RES_UPDATE_PROTOCOL_SUPPORT 8 +#define FW_RES_UPDATE_DELIVERY_METHOD 9 typedef enum { UPDATE_STATE_IDLE = 0, @@ -98,8 +98,8 @@ user_state_ensure_stream_open(fw_user_state_t *user, return 0; } assert(user->state == UPDATE_STATE_IDLE); - int result = user->handlers->stream_open(user->arg, - package_uri, package_etag); + int result = + user->handlers->stream_open(user->arg, package_uri, package_etag); if (!result) { user->state = UPDATE_STATE_DOWNLOADING; } @@ -107,14 +107,14 @@ user_state_ensure_stream_open(fw_user_state_t *user, } static int user_state_stream_write(fw_user_state_t *user, - const void *data, size_t length) { + const void *data, + size_t length) { assert(user->state == UPDATE_STATE_DOWNLOADING); return user->handlers->stream_write(user->arg, data, length); } static const char *user_state_get_name(fw_user_state_t *user) { - if (!user->handlers->get_name - || user->state != UPDATE_STATE_DOWNLOADED) { + if (!user->handlers->get_name || user->state != UPDATE_STATE_DOWNLOADED) { return NULL; } return user->handlers->get_name(user->arg); @@ -167,7 +167,7 @@ static int get_security_info(anjay_t *anjay, } else { assert(!fw->security_from_dm); if (!(fw->security_from_dm = anjay_fw_update_load_security_from_dm( - anjay, fw->package_uri))) { + anjay, fw->package_uri))) { return -1; } *out_security_info = *fw->security_from_dm; @@ -175,6 +175,17 @@ static int get_security_info(anjay_t *anjay, } } +static int get_coap_tx_params(fw_repr_t *fw, + avs_coap_tx_params_t *out_tx_params) { + if (fw->user_state.handlers->get_coap_tx_params) { + *out_tx_params = + fw->user_state.handlers->get_coap_tx_params(fw->user_state.arg, + fw->package_uri); + return 0; + } + return -1; +} + static void set_update_result(anjay_t *anjay, fw_repr_t *fw, fw_update_result_t new_result) { @@ -184,9 +195,8 @@ static void set_update_result(anjay_t *anjay, } } -static void set_state(anjay_t *anjay, - fw_repr_t *fw, - fw_update_state_t new_state) { +static void +set_state(anjay_t *anjay, fw_repr_t *fw, fw_update_state_t new_state) { if (fw->state != new_state) { fw->state = new_state; anjay_notify_changed(anjay, FW_OID, 0, FW_RES_STATE); @@ -224,7 +234,8 @@ static int fw_res_present(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid, anjay_rid_t rid) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; fw_repr_t *fw = get_fw(obj_ptr); switch (rid) { case FW_RES_PKG_NAME: @@ -241,7 +252,8 @@ static int fw_read(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void) anjay; (void) iid; + (void) anjay; + (void) iid; fw_repr_t *fw = get_fw(obj_ptr); switch (rid) { case FW_RES_PACKAGE_URI: @@ -275,11 +287,11 @@ static int fw_read(anjay_t *anjay, #ifdef WITH_BLOCK_DOWNLOAD 0, /* CoAP */ 1, /* CoAPS */ -#endif // WITH_BLOCK_DOWNLOAD +#endif // WITH_BLOCK_DOWNLOAD #ifdef WITH_HTTP_DOWNLOAD 2, /* HTTP 1.1 */ 3, /* HTTPS 1.1 */ -#endif // WITH_HTTP_DOWNLOAD +#endif // WITH_HTTP_DOWNLOAD -1 }; size_t index = 0; @@ -295,18 +307,18 @@ static int fw_read(anjay_t *anjay, } case FW_RES_UPDATE_DELIVERY_METHOD: #ifdef WITH_BLOCK_RECEIVE -#ifdef WITH_DOWNLOADER +# ifdef WITH_DOWNLOADER // 2 -> pull && push return anjay_ret_i32(ctx, 2); -#else // WITH_DOWNLOADER - // 1 -> push only +# else // WITH_DOWNLOADER + // 1 -> push only return anjay_ret_i32(ctx, 1); -#endif // WITH_DOWNLOADER +# endif // WITH_DOWNLOADER #elif defined(WITH_DOWNLOADER) // WITH_BLOCK_RECEIVE // 0 -> pull only return anjay_ret_i32(ctx, 0); -#else // WITH_DOWNLOADER -#error "Firmware Update requires at least WITH_DOWNLOADER or WITH_BLOCK_RECEIVE" +#else // WITH_DOWNLOADER +# error "Firmware Update requires at least WITH_DOWNLOADER or WITH_BLOCK_RECEIVE" #endif // WITH_DOWNLOADER case FW_RES_PACKAGE: case FW_RES_UPDATE: @@ -337,8 +349,8 @@ static int download_write_block(anjay_t *anjay, (void) etag; fw_repr_t *fw = (fw_repr_t *) fw_; - int result = user_state_ensure_stream_open(&fw->user_state, - fw->package_uri, etag); + int result = user_state_ensure_stream_open(&fw->user_state, fw->package_uri, + etag); if (!result && data_size > 0) { result = user_state_stream_write(&fw->user_state, data, data_size); } @@ -357,9 +369,7 @@ static int schedule_background_anjay_download(anjay_t *anjay, size_t start_offset, const anjay_etag_t *etag); -static void download_finished(anjay_t *anjay, - int result, - void *fw_) { +static void download_finished(anjay_t *anjay, int result, void *fw_) { (void) anjay; fw_repr_t *fw = (fw_repr_t *) fw_; @@ -380,8 +390,10 @@ static void download_finished(anjay_t *anjay, reset_user_state(fw); if (fw->retry_download_on_expired && result == ANJAY_DOWNLOAD_ERR_EXPIRED) { - fw_log(INFO, "Could not resume firmware download (result = %d), " - "retrying from the beginning", result); + fw_log(INFO, + "Could not resume firmware download (result = %d), " + "retrying from the beginning", + result); if (schedule_background_anjay_download(anjay, fw, 0, NULL)) { fw_log(WARNING, "Could not retry firmware download"); set_state(anjay, fw, UPDATE_STATE_IDLE); @@ -393,7 +405,7 @@ static void download_finished(anjay_t *anjay, } } else if ((result = user_state_ensure_stream_open(&fw->user_state, fw->package_uri, NULL)) - || (result = finish_user_stream(fw))) { + || (result = finish_user_stream(fw))) { handle_err_result(anjay, fw, UPDATE_STATE_IDLE, result, UPDATE_RESULT_NOT_ENOUGH_SPACE); } else { @@ -425,6 +437,11 @@ static int schedule_background_anjay_download(anjay_t *anjay, } } + avs_coap_tx_params_t tx_params; + if (!get_coap_tx_params(fw, &tx_params)) { + cfg.coap_tx_params = &tx_params; + } + anjay_download_handle_t handle = anjay_download(anjay, &cfg); if (!handle) { fw_update_result_t update_result; @@ -464,7 +481,7 @@ static int write_firmware_to_stream(anjay_t *anjay, size_t bytes_read; char buffer[1024]; if ((result = anjay_get_bytes(ctx, &bytes_read, &finished, buffer, - sizeof(buffer)))) { + sizeof(buffer)))) { fw_log(ERROR, "anjay_get_bytes() failed"); set_state(anjay, fw, UPDATE_STATE_IDLE); @@ -476,8 +493,8 @@ static int write_firmware_to_stream(anjay_t *anjay, if (first_byte == EOF) { first_byte = (unsigned char) buffer[0]; } - result = user_state_stream_write(&fw->user_state, - buffer, bytes_read); + result = user_state_stream_write(&fw->user_state, buffer, + bytes_read); } if (result) { handle_err_result(anjay, fw, UPDATE_STATE_IDLE, result, @@ -549,71 +566,70 @@ static int fw_write(anjay_t *anjay, fw_repr_t *fw = get_fw(obj_ptr); switch (rid) { - case FW_RES_PACKAGE: - { - int result = 0; - if (fw->state == UPDATE_STATE_DOWNLOADED) { - result = expect_single_nullbyte(ctx); - if (!result) { - reset(anjay, fw); - } - } else { - bool is_reset_request = false; - result = write_firmware(anjay, fw, ctx, &is_reset_request); - if (!result && is_reset_request) { - reset(anjay, fw); - } + case FW_RES_PACKAGE: { + int result = 0; + if (fw->state == UPDATE_STATE_DOWNLOADED) { + result = expect_single_nullbyte(ctx); + if (!result) { + reset(anjay, fw); + } + } else { + bool is_reset_request = false; + result = write_firmware(anjay, fw, ctx, &is_reset_request); + if (!result && is_reset_request) { + reset(anjay, fw); } - return result; } + return result; + } case FW_RES_PACKAGE_URI: #ifdef WITH_DOWNLOADER - { - if (fw->state == UPDATE_STATE_DOWNLOADING) { - fw_log(ERROR, - "cannot set Package Uri resource while downloading"); - return ANJAY_ERR_METHOD_NOT_ALLOWED; - } + { + if (fw->state == UPDATE_STATE_DOWNLOADING) { + fw_log(ERROR, "cannot set Package Uri resource while downloading"); + return ANJAY_ERR_METHOD_NOT_ALLOWED; + } - char *new_uri = NULL; - int result = _anjay_io_fetch_string(ctx, &new_uri); - size_t len = (new_uri ? strlen(new_uri) : 0); + char *new_uri = NULL; + int result = _anjay_io_fetch_string(ctx, &new_uri); + size_t len = (new_uri ? strlen(new_uri) : 0); - if (!result && len > 0 && fw->state != UPDATE_STATE_IDLE) { - result = ANJAY_ERR_BAD_REQUEST; - } + if (!result && len > 0 && fw->state != UPDATE_STATE_IDLE) { + result = ANJAY_ERR_BAD_REQUEST; + } - if (!result && len > 0 && classify_protocol(new_uri) - == ANJAY_DOWNLOADER_PROTO_UNSUPPORTED) { - fw_log(ERROR, - "unsupported download protocol required for uri %s", - new_uri); - set_update_result(anjay, fw, - UPDATE_RESULT_UNSUPPORTED_PROTOCOL); - result = ANJAY_ERR_BAD_REQUEST; - } + if (!result && len > 0 + && classify_protocol(new_uri) + == ANJAY_DOWNLOADER_PROTO_UNSUPPORTED) { + fw_log(ERROR, + "unsupported download protocol required for uri %s", + new_uri); + set_update_result(anjay, fw, UPDATE_RESULT_UNSUPPORTED_PROTOCOL); + result = ANJAY_ERR_BAD_REQUEST; + } - if (!result) { - avs_free((void *) (intptr_t) fw->package_uri); - fw->package_uri = new_uri; - - if (len == 0) { - reset(anjay, fw); - } else { - int dl_res = schedule_background_anjay_download(anjay, fw, - 0, NULL); - if (dl_res) { - fw_log(WARNING, "schedule_download_in_background " - "failed: %d", dl_res); - } - // write itself succeeded; do not propagate error - } + if (!result) { + avs_free((void *) (intptr_t) fw->package_uri); + fw->package_uri = new_uri; + + if (len == 0) { + reset(anjay, fw); } else { - avs_free(new_uri); + int dl_res = + schedule_background_anjay_download(anjay, fw, 0, NULL); + if (dl_res) { + fw_log(WARNING, + "schedule_download_in_background failed: %d", + dl_res); + } + // write itself succeeded; do not propagate error } - - return result; + } else { + avs_free(new_uri); } + + return result; + } #endif // WITH_DOWNLOADER default: return ANJAY_ERR_METHOD_NOT_ALLOWED; @@ -636,23 +652,25 @@ static int fw_execute(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_execute_ctx_t *ctx) { - (void) iid; (void) ctx; + (void) iid; + (void) ctx; fw_repr_t *fw = get_fw(obj_ptr); switch (rid) { - case FW_RES_UPDATE: - { - if (fw->state != UPDATE_STATE_DOWNLOADED) { - fw_log(WARNING, "Firmware Update requested, but firmware not " - "yet downloaded (state = %d)", fw->state); - return ANJAY_ERR_METHOD_NOT_ALLOWED; - } - - set_state(anjay, fw, UPDATE_STATE_UPDATING); - set_update_result(anjay, fw, UPDATE_RESULT_INITIAL); - // update process will be continued in fw_on_notify - return 0; + case FW_RES_UPDATE: { + if (fw->state != UPDATE_STATE_DOWNLOADED) { + fw_log(WARNING, + "Firmware Update requested, but firmware not yet downloaded " + "(state = %d)", + fw->state); + return ANJAY_ERR_METHOD_NOT_ALLOWED; } + + set_state(anjay, fw, UPDATE_STATE_UPDATING); + set_update_result(anjay, fw, UPDATE_RESULT_INITIAL); + // update process will be continued in fw_on_notify + return 0; + } default: return ANJAY_ERR_METHOD_NOT_ALLOWED; } @@ -660,16 +678,15 @@ static int fw_execute(anjay_t *anjay, static const anjay_dm_object_def_t FIRMWARE_UPDATE = { .oid = FW_OID, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - FW_RES_PACKAGE, - FW_RES_PACKAGE_URI, - FW_RES_UPDATE, - FW_RES_STATE, - FW_RES_UPDATE_RESULT, - FW_RES_PKG_NAME, - FW_RES_PKG_VERSION, - FW_RES_UPDATE_PROTOCOL_SUPPORT, - FW_RES_UPDATE_DELIVERY_METHOD), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(FW_RES_PACKAGE, + FW_RES_PACKAGE_URI, + FW_RES_UPDATE, + FW_RES_STATE, + FW_RES_UPDATE_RESULT, + FW_RES_PKG_NAME, + FW_RES_PKG_VERSION, + FW_RES_UPDATE_PROTOCOL_SUPPORT, + FW_RES_UPDATE_DELIVERY_METHOD), .handlers = { .instance_it = anjay_dm_instance_it_SINGLE, .instance_present = anjay_dm_instance_present_SINGLE, @@ -684,13 +701,11 @@ static const anjay_dm_object_def_t FIRMWARE_UPDATE = { } }; -static int fw_on_notify(anjay_t *anjay, - anjay_notify_queue_t incoming_queue, - void *fw_) { +static int +fw_on_notify(anjay_t *anjay, anjay_notify_queue_t incoming_queue, void *fw_) { fw_repr_t *fw = (fw_repr_t *) fw_; (void) incoming_queue; - if (!fw->update_job - && fw->state == UPDATE_STATE_UPDATING + if (!fw->update_job && fw->state == UPDATE_STATE_UPDATING && _anjay_sched_now(_anjay_sched_get(anjay), &fw->update_job, perform_upgrade, &fw, sizeof(fw))) { // we don't need to reschedule notifying, @@ -726,7 +741,7 @@ initialize_fw_repr(anjay_t *anjay, case ANJAY_FW_UPDATE_INITIAL_DOWNLOADED: if (initial_state->persisted_uri && !(repr->package_uri = - avs_strdup(initial_state->persisted_uri))) { + avs_strdup(initial_state->persisted_uri))) { fw_log(WARNING, "Could not copy the persisted Package URI"); } repr->user_state.state = UPDATE_STATE_DOWNLOADED; @@ -743,22 +758,23 @@ initialize_fw_repr(anjay_t *anjay, } if (!initial_state->persisted_uri || !(repr->package_uri = - avs_strdup(initial_state->persisted_uri))) { - fw_log(WARNING, "Could not copy the persisted Package URI, " - "not resuming firmware download"); + avs_strdup(initial_state->persisted_uri))) { + fw_log(WARNING, "Could not copy the persisted Package URI, not " + "resuming firmware download"); reset_user_state(repr); } else if (schedule_background_anjay_download( - anjay, repr, resume_offset, initial_state->resume_etag)) { + anjay, repr, resume_offset, + initial_state->resume_etag)) { fw_log(WARNING, "Could not resume firmware download"); reset_user_state(repr); if (repr->result == UPDATE_RESULT_CONNECTION_LOST && initial_state->resume_etag - && schedule_background_anjay_download(anjay, repr, - 0, NULL)) { + && schedule_background_anjay_download(anjay, repr, 0, + NULL)) { fw_log(WARNING, "Could not retry firmware download"); } } -#else // WITH_DOWNLOADER +#else // WITH_DOWNLOADER fw_log(WARNING, "Unable to resume download: PULL download not supported"); #endif // WITH_DOWNLOADER @@ -776,11 +792,11 @@ initialize_fw_repr(anjay_t *anjay, } } -int -anjay_fw_update_install(anjay_t *anjay, - const anjay_fw_update_handlers_t *handlers, - void *user_arg, - const anjay_fw_update_initial_state_t *initial_state) { +int anjay_fw_update_install( + anjay_t *anjay, + const anjay_fw_update_handlers_t *handlers, + void *user_arg, + const anjay_fw_update_initial_state_t *initial_state) { assert(anjay); fw_repr_t *repr = (fw_repr_t *) avs_calloc(1, sizeof(fw_repr_t)); diff --git a/modules/security/include_public/anjay/security.h b/modules/security/include_public/anjay/security.h index 5d6480bdb..79f550847 100644 --- a/modules/security/include_public/anjay/security.h +++ b/modules/security/include_public/anjay/security.h @@ -81,10 +81,10 @@ typedef struct { * @return 0 on success, negative value in case of an error or if the instance * of specified id already exists. */ -int anjay_security_object_add_instance(anjay_t *anjay, - const anjay_security_instance_t *instance, - anjay_iid_t *inout_iid); - +int anjay_security_object_add_instance( + anjay_t *anjay, + const anjay_security_instance_t *instance, + anjay_iid_t *inout_iid); /** * Purges instances of Security Object leaving it in an empty state. diff --git a/modules/security/src/mod_security.c b/modules/security/src/mod_security.c index cac3557f0..643060cc5 100644 --- a/modules/security/src/mod_security.c +++ b/modules/security/src/mod_security.c @@ -16,14 +16,14 @@ #include +#include #include #include -#include #include -#include #include +#include #include "mod_security.h" #include "security_transaction.h" @@ -31,8 +31,7 @@ VISIBILITY_SOURCE_BEGIN -static inline sec_instance_t * -find_instance(sec_repr_t *repr, anjay_iid_t iid) { +static inline sec_instance_t *find_instance(sec_repr_t *repr, anjay_iid_t iid) { if (!repr) { return NULL; } @@ -78,7 +77,8 @@ static int add_instance(sec_repr_t *repr, } else if (find_instance(repr, *inout_iid)) { return -1; } - AVS_LIST(sec_instance_t) new_instance = AVS_LIST_NEW_ELEMENT(sec_instance_t); + AVS_LIST(sec_instance_t) new_instance = + AVS_LIST_NEW_ELEMENT(sec_instance_t); if (!new_instance) { security_log(ERROR, "Out of memory"); return -1; @@ -94,41 +94,45 @@ static int add_instance(sec_repr_t *repr, new_instance->bs_timeout_s = instance->bootstrap_timeout_s; if (_anjay_raw_buffer_clone( &new_instance->public_cert_or_psk_identity, - &(const anjay_raw_buffer_t){ - .data = (void *) (intptr_t) - instance->public_cert_or_psk_identity, - .size = instance->public_cert_or_psk_identity_size })) { + &(const anjay_raw_buffer_t) { + .data = (void *) (intptr_t) + instance->public_cert_or_psk_identity, + .size = instance->public_cert_or_psk_identity_size + })) { goto error; } if (_anjay_raw_buffer_clone( &new_instance->private_cert_or_psk_key, - &(const anjay_raw_buffer_t){ - .data = (void *) (intptr_t) - instance->private_cert_or_psk_key, - .size = instance->private_cert_or_psk_key_size })) { + &(const anjay_raw_buffer_t) { + .data = (void *) (intptr_t) + instance->private_cert_or_psk_key, + .size = instance->private_cert_or_psk_key_size + })) { goto error; } if (_anjay_raw_buffer_clone( &new_instance->server_public_key, - &(const anjay_raw_buffer_t){ - .data = (void *) (intptr_t) instance->server_public_key, - .size = instance->server_public_key_size })) { + &(const anjay_raw_buffer_t) { + .data = (void *) (intptr_t) instance->server_public_key, + .size = instance->server_public_key_size + })) { goto error; } new_instance->sms_security_mode = instance->sms_security_mode; if (_anjay_raw_buffer_clone( &new_instance->sms_key_params, - &(const anjay_raw_buffer_t){ - .data = (void *) (intptr_t) - instance->sms_key_parameters, - .size = instance->sms_key_parameters_size })) { + &(const anjay_raw_buffer_t) { + .data = (void *) (intptr_t) instance->sms_key_parameters, + .size = instance->sms_key_parameters_size + })) { goto error; } if (_anjay_raw_buffer_clone( &new_instance->sms_secret_key, - &(const anjay_raw_buffer_t){ - .data = (void *) (intptr_t) instance->sms_secret_key, - .size = instance->sms_secret_key_size })) { + &(const anjay_raw_buffer_t) { + .data = (void *) (intptr_t) instance->sms_secret_key, + .size = instance->sms_secret_key_size + })) { goto error; } if (instance->server_sms_number) { @@ -158,11 +162,11 @@ static int add_instance(sec_repr_t *repr, AVS_LIST_INSERT(ptr, new_instance); if (instance->bootstrap_server) { - security_log(INFO, "Added instance %u (bootstrap, URI: %s)", - *inout_iid, instance->server_uri); + security_log(INFO, "Added instance %u (bootstrap, URI: %s)", *inout_iid, + instance->server_uri); } else { - security_log(INFO, "Added instance %u (SSID: %u, URI: %s)", - *inout_iid, instance->ssid, instance->server_uri); + security_log(INFO, "Added instance %u (SSID: %u, URI: %s)", *inout_iid, + instance->ssid, instance->server_uri); } _anjay_sec_mark_modified(repr); @@ -199,10 +203,11 @@ static int sec_resource_operations(anjay_t *anjay, return 0; } -static inline int sec_resource_present(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid) { +static inline int +sec_resource_present(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid) { (void) anjay; (void) iid; const sec_instance_t *inst = find_instance(_anjay_sec_get(obj_ptr), iid); @@ -299,7 +304,7 @@ static int sec_write(anjay_t *anjay, return retval; case SEC_RES_UDP_SECURITY_MODE: if (!(retval = _anjay_sec_fetch_udp_security_mode( - ctx, &inst->udp_security_mode))) { + ctx, &inst->udp_security_mode))) { inst->has_udp_security_mode = true; } return retval; @@ -311,7 +316,7 @@ static int sec_write(anjay_t *anjay, return _anjay_io_fetch_bytes(ctx, &inst->private_cert_or_psk_key); case SEC_RES_SMS_SECURITY_MODE: if (!(retval = _anjay_sec_fetch_sms_security_mode( - ctx, &inst->sms_security_mode))) { + ctx, &inst->sms_security_mode))) { inst->has_sms_security_mode = true; } return retval; @@ -372,7 +377,8 @@ static int sec_instance_create(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t *inout_iid, anjay_ssid_t ssid) { - (void) anjay; (void) ssid; + (void) anjay; + (void) ssid; sec_repr_t *repr = _anjay_sec_get(obj_ptr); if (*inout_iid == ANJAY_IID_INVALID && assign_iid(repr, inout_iid)) { return ANJAY_ERR_INTERNAL; @@ -405,16 +411,14 @@ static int sec_instance_remove(anjay_t *anjay, return del_instance(_anjay_sec_get(obj_ptr), iid); } -static int -sec_transaction_begin(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr) { +static int sec_transaction_begin(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr) { (void) anjay; return _anjay_sec_transaction_begin_impl(_anjay_sec_get(obj_ptr)); } -static int -sec_transaction_commit(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr) { +static int sec_transaction_commit(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr) { (void) anjay; return _anjay_sec_transaction_commit_impl(_anjay_sec_get(obj_ptr)); } @@ -433,10 +437,9 @@ sec_transaction_rollback(anjay_t *anjay, return _anjay_sec_transaction_rollback_impl(_anjay_sec_get(obj_ptr)); } -static int -sec_instance_reset(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid) { +static int sec_instance_reset(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid) { (void) anjay; sec_instance_t *inst = find_instance(_anjay_sec_get(obj_ptr), iid); assert(inst); @@ -449,20 +452,19 @@ sec_instance_reset(anjay_t *anjay, static const anjay_dm_object_def_t SECURITY = { .oid = ANJAY_DM_OID_SECURITY, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - SEC_RES_LWM2M_SERVER_URI, - SEC_RES_BOOTSTRAP_SERVER, - SEC_RES_UDP_SECURITY_MODE, - SEC_RES_PK_OR_IDENTITY, - SEC_RES_SERVER_PK, - SEC_RES_SECRET_KEY, - SEC_RES_SMS_SECURITY_MODE, - SEC_RES_SMS_BINDING_KEY_PARAMS, - SEC_RES_SMS_BINDING_SECRET_KEYS, - SEC_RES_SERVER_SMS_NUMBER, - SEC_RES_SHORT_SERVER_ID, - SEC_RES_CLIENT_HOLD_OFF_TIME, - SEC_RES_BOOTSTRAP_TIMEOUT), + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(SEC_RES_LWM2M_SERVER_URI, + SEC_RES_BOOTSTRAP_SERVER, + SEC_RES_UDP_SECURITY_MODE, + SEC_RES_PK_OR_IDENTITY, + SEC_RES_SERVER_PK, + SEC_RES_SECRET_KEY, + SEC_RES_SMS_SECURITY_MODE, + SEC_RES_SMS_BINDING_KEY_PARAMS, + SEC_RES_SMS_BINDING_SECRET_KEYS, + SEC_RES_SERVER_SMS_NUMBER, + SEC_RES_SHORT_SERVER_ID, + SEC_RES_CLIENT_HOLD_OFF_TIME, + SEC_RES_BOOTSTRAP_TIMEOUT), .handlers = { .instance_it = sec_instance_it, .instance_present = sec_instance_present, @@ -587,5 +589,5 @@ int anjay_security_object_install(anjay_t *anjay) { } #ifdef ANJAY_TEST -#include "test/api.c" +# include "test/api.c" #endif diff --git a/modules/security/src/mod_security.h b/modules/security/src/mod_security.h index c3c29b495..2ba1bba4a 100644 --- a/modules/security/src/mod_security.h +++ b/modules/security/src/mod_security.h @@ -30,19 +30,19 @@ VISIBILITY_PRIVATE_HEADER_BEGIN typedef enum { - SEC_RES_LWM2M_SERVER_URI = 0, - SEC_RES_BOOTSTRAP_SERVER = 1, - SEC_RES_UDP_SECURITY_MODE = 2, - SEC_RES_PK_OR_IDENTITY = 3, - SEC_RES_SERVER_PK = 4, - SEC_RES_SECRET_KEY = 5, - SEC_RES_SMS_SECURITY_MODE = 6, - SEC_RES_SMS_BINDING_KEY_PARAMS = 7, + SEC_RES_LWM2M_SERVER_URI = 0, + SEC_RES_BOOTSTRAP_SERVER = 1, + SEC_RES_UDP_SECURITY_MODE = 2, + SEC_RES_PK_OR_IDENTITY = 3, + SEC_RES_SERVER_PK = 4, + SEC_RES_SECRET_KEY = 5, + SEC_RES_SMS_SECURITY_MODE = 6, + SEC_RES_SMS_BINDING_KEY_PARAMS = 7, SEC_RES_SMS_BINDING_SECRET_KEYS = 8, - SEC_RES_SERVER_SMS_NUMBER = 9, - SEC_RES_SHORT_SERVER_ID = 10, - SEC_RES_CLIENT_HOLD_OFF_TIME = 11, - SEC_RES_BOOTSTRAP_TIMEOUT = 12, + SEC_RES_SERVER_SMS_NUMBER = 9, + SEC_RES_SHORT_SERVER_ID = 10, + SEC_RES_CLIENT_HOLD_OFF_TIME = 11, + SEC_RES_BOOTSTRAP_TIMEOUT = 12, } security_resource_t; typedef struct { diff --git a/modules/security/src/security_persistence.c b/modules/security/src/security_persistence.c index aea9c2c1e..aefd10f9a 100644 --- a/modules/security/src/security_persistence.c +++ b/modules/security/src/security_persistence.c @@ -17,13 +17,13 @@ #include #ifdef WITH_AVS_PERSISTENCE -#include +# include #endif // WITH_AVS_PERSISTENCE #include -#include #include +#include #include "mod_security.h" #include "security_transaction.h" @@ -52,17 +52,17 @@ static int handle_sized_v0_fields(avs_persistence_context_t *ctx, || (retval = avs_persistence_u32(ctx, (uint32_t *) &element->holdoff_s)) || (retval = avs_persistence_u32( - ctx, (uint32_t *) &element->bs_timeout_s))); + ctx, (uint32_t *) &element->bs_timeout_s))); return retval; } static int handle_sized_v1_fields(avs_persistence_context_t *ctx, sec_instance_t *element) { int retval; - (void) ((retval = avs_persistence_bool(ctx, - &element->has_sms_security_mode)) - || (retval = avs_persistence_bool(ctx, - &element->has_sms_key_params)) + (void) ((retval = + avs_persistence_bool(ctx, &element->has_sms_security_mode)) + || (retval = + avs_persistence_bool(ctx, &element->has_sms_key_params)) || (retval = avs_persistence_bool(ctx, &element->has_sms_secret_key))); return retval; @@ -70,8 +70,8 @@ static int handle_sized_v1_fields(avs_persistence_context_t *ctx, static int handle_raw_buffer(avs_persistence_context_t *ctx, anjay_raw_buffer_t *buffer) { - int retval = avs_persistence_sized_buffer(ctx, - &buffer->data, &buffer->size); + int retval = + avs_persistence_sized_buffer(ctx, &buffer->data, &buffer->size); if (!buffer->capacity) { buffer->capacity = buffer->size; } @@ -89,7 +89,7 @@ static int handle_instance(avs_persistence_context_t *ctx, || (retval = avs_persistence_u16(ctx, &udp_security_mode)) || (retval = avs_persistence_string(ctx, &element->server_uri)) || (retval = handle_raw_buffer( - ctx, &element->public_cert_or_psk_identity)) + ctx, &element->public_cert_or_psk_identity)) || (retval = handle_raw_buffer(ctx, &element->private_cert_or_psk_key)) || (retval = handle_raw_buffer(ctx, &element->server_public_key))) { @@ -133,8 +133,8 @@ int anjay_security_object_persist(anjay_t *anjay, return -1; } retval = avs_persistence_list(ctx, (AVS_LIST(void) *) &repr->instances, - sizeof(sec_instance_t), - handle_instance, (void *) (intptr_t) 1, NULL); + sizeof(sec_instance_t), handle_instance, + (void *) (intptr_t) 1, NULL); avs_persistence_context_delete(ctx); if (!retval) { _anjay_sec_clear_modified(repr); @@ -198,22 +198,24 @@ int anjay_security_object_restore(anjay_t *anjay, return retval; } -#ifdef ANJAY_TEST -#include "test/persistence.c" -#endif +# ifdef ANJAY_TEST +# include "test/persistence.c" +# endif #else // WITH_AVS_PERSISTENCE int anjay_security_object_persist(anjay_t *anjay, avs_stream_abstract_t *out_stream) { - (void) anjay; (void) out_stream; + (void) anjay; + (void) out_stream; persistence_log(ERROR, "Persistence not compiled in"); return -1; } int anjay_security_object_restore(anjay_t *anjay, avs_stream_abstract_t *in_stream) { - (void) anjay; (void) in_stream; + (void) anjay; + (void) in_stream; persistence_log(ERROR, "Persistence not compiled in"); return -1; } diff --git a/modules/security/src/security_transaction.c b/modules/security/src/security_transaction.c index 3bcdf3ffd..a0f425116 100644 --- a/modules/security/src/security_transaction.c +++ b/modules/security/src/security_transaction.c @@ -32,8 +32,8 @@ static int ssid_cmp(const void *a, const void *b, size_t element_size) { static bool uri_protocol_matching(anjay_udp_security_mode_t security_mode, const char *uri) { - const char *expected_prefix = (security_mode == ANJAY_UDP_SECURITY_NOSEC) - ? "coap:" : "coaps:"; + const char *expected_prefix = + (security_mode == ANJAY_UDP_SECURITY_NOSEC) ? "coap:" : "coaps:"; return strncmp(uri, expected_prefix, strlen(expected_prefix)) == 0; } @@ -73,7 +73,7 @@ static int validate_instance(sec_instance_t *it) { return -1; } if (_anjay_sec_validate_udp_security_mode( - (int32_t) it->udp_security_mode)) { + (int32_t) it->udp_security_mode)) { LOG_VALIDATION_FAILED(it, "UDP Security mode %d not supported", (int) it->udp_security_mode); return -1; @@ -98,14 +98,14 @@ static int validate_instance(sec_instance_t *it) { } if (it->has_sms_security_mode) { if (_anjay_sec_validate_sms_security_mode( - (int32_t) it->sms_security_mode)) { + (int32_t) it->sms_security_mode)) { LOG_VALIDATION_FAILED(it, "SMS Security mode %d not supported", (int) it->sms_security_mode); return -1; } if ((it->sms_security_mode == ANJAY_SMS_SECURITY_DTLS_PSK - || it->sms_security_mode == ANJAY_SMS_SECURITY_SECURE_PACKET) - && (!it->sms_key_params.data || !it->sms_secret_key.data)) { + || it->sms_security_mode == ANJAY_SMS_SECURITY_SECURE_PACKET) + && (!it->sms_key_params.data || !it->sms_secret_key.data)) { LOG_VALIDATION_FAILED( it, "SMS security credentials not fully configured"); return -1; diff --git a/modules/security/src/test/api.c b/modules/security/src/test/api.c index 489c22e3c..4bc3854ed 100644 --- a/modules/security/src/test/api.c +++ b/modules/security/src/test/api.c @@ -26,12 +26,11 @@ typedef struct { anjay_t *anjay; } security_test_env_t; -#define SCOPED_SERVER_TEST_ENV(Name) \ +#define SCOPED_SERVER_TEST_ENV(Name) \ SCOPED_PTR(security_test_env_t, security_test_env_destroy) \ Name = security_test_env_create(); -static security_test_env_t * -security_test_env_create(void) { +static security_test_env_t *security_test_env_create(void) { security_test_env_t *env = (__typeof__(env)) avs_calloc(1, sizeof(*env)); AVS_UNIT_ASSERT_NOT_NULL(env); env->anjay = anjay_new(&CONFIG); diff --git a/modules/security/src/test/persistence.c b/modules/security/src/test/persistence.c index fdcaabb45..889995836 100644 --- a/modules/security/src/test/persistence.c +++ b/modules/security/src/test/persistence.c @@ -52,8 +52,10 @@ security_persistence_test_env_create(void) { AVS_UNIT_ASSERT_SUCCESS(anjay_security_object_install(env->anjay_restored)); env->stream = avs_stream_membuf_create(); AVS_UNIT_ASSERT_NOT_NULL(env->stream); - env->stored = _anjay_dm_find_object_by_oid(env->anjay_stored, ANJAY_DM_OID_SECURITY); - env->restored = _anjay_dm_find_object_by_oid(env->anjay_restored, ANJAY_DM_OID_SECURITY); + env->stored = _anjay_dm_find_object_by_oid(env->anjay_stored, + ANJAY_DM_OID_SECURITY); + env->restored = _anjay_dm_find_object_by_oid(env->anjay_restored, + ANJAY_DM_OID_SECURITY); env->stored_repr = _anjay_sec_get(env->stored); env->restored_repr = _anjay_sec_get(env->restored); return env; @@ -70,8 +72,10 @@ security_persistence_test_env_destroy(security_persistence_test_env_t **env) { AVS_UNIT_TEST(security_persistence, empty_store_restore) { SCOPED_SECURITY_PERSISTENCE_TEST_ENV(env); AVS_UNIT_ASSERT_EQUAL(0, AVS_LIST_SIZE(env->stored_repr->instances)); - AVS_UNIT_ASSERT_SUCCESS(anjay_security_object_persist(env->anjay_stored, env->stream)); - AVS_UNIT_ASSERT_SUCCESS(anjay_security_object_restore(env->anjay_restored, env->stream)); + AVS_UNIT_ASSERT_SUCCESS( + anjay_security_object_persist(env->anjay_stored, env->stream)); + AVS_UNIT_ASSERT_SUCCESS( + anjay_security_object_restore(env->anjay_restored, env->stream)); AVS_UNIT_ASSERT_EQUAL(0, AVS_LIST_SIZE(env->restored_repr->instances)); } @@ -113,14 +117,11 @@ static void assert_instances_equal(const sec_instance_t *a, &b->public_cert_or_psk_identity); assert_raw_buffers_equal(&a->private_cert_or_psk_key, &b->private_cert_or_psk_key); - assert_raw_buffers_equal(&a->server_public_key, - &b->server_public_key); + assert_raw_buffers_equal(&a->server_public_key, &b->server_public_key); AVS_UNIT_ASSERT_EQUAL((uint32_t) a->sms_security_mode, (uint32_t) b->sms_security_mode); - assert_raw_buffers_equal(&a->sms_key_params, - &b->sms_key_params); - assert_raw_buffers_equal(&a->sms_secret_key, - &b->sms_secret_key); + assert_raw_buffers_equal(&a->sms_key_params, &b->sms_key_params); + assert_raw_buffers_equal(&a->sms_secret_key, &b->sms_secret_key); AVS_UNIT_ASSERT_EQUAL_STRING(a->sms_number, b->sms_number); AVS_UNIT_ASSERT_EQUAL(a->ssid, b->ssid); AVS_UNIT_ASSERT_EQUAL(a->holdoff_s, b->holdoff_s); @@ -134,11 +135,11 @@ static void assert_instances_equal(const sec_instance_t *a, AVS_UNIT_ASSERT_EQUAL(a->has_ssid, b->has_ssid); } -static void assert_objects_equal(const sec_repr_t *a, - const sec_repr_t *b) { +static void assert_objects_equal(const sec_repr_t *a, const sec_repr_t *b) { AVS_LIST(sec_instance_t) a_it = a->instances; AVS_LIST(sec_instance_t) b_it = b->instances; - AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(a->instances), AVS_LIST_SIZE(b->instances)); + AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(a->instances), + AVS_LIST_SIZE(b->instances)); while (a_it && b_it) { assert_instances_equal(a_it, b_it); a_it = AVS_LIST_NEXT(a_it); @@ -151,9 +152,8 @@ static void assert_objects_equal(const sec_repr_t *a, AVS_UNIT_TEST(security_persistence, basic_store_restore) { SCOPED_SECURITY_PERSISTENCE_TEST_ENV(env); anjay_iid_t iid = ANJAY_IID_INVALID; - AVS_UNIT_ASSERT_SUCCESS( - anjay_security_object_add_instance(env->anjay_stored, - &BOOTSTRAP_INSTANCE, &iid)); + AVS_UNIT_ASSERT_SUCCESS(anjay_security_object_add_instance( + env->anjay_stored, &BOOTSTRAP_INSTANCE, &iid)); AVS_UNIT_ASSERT_TRUE(anjay_security_object_is_modified(env->anjay_stored)); AVS_UNIT_ASSERT_SUCCESS( anjay_security_object_persist(env->anjay_stored, env->stream)); @@ -184,10 +184,12 @@ AVS_UNIT_TEST(security_persistence, invalid_object_to_restore) { AVS_UNIT_ASSERT_SUCCESS( anjay_security_object_persist(env->anjay_stored, env->stream)); - AVS_UNIT_ASSERT_FALSE(anjay_security_object_is_modified(env->anjay_restored)); + AVS_UNIT_ASSERT_FALSE( + anjay_security_object_is_modified(env->anjay_restored)); AVS_UNIT_ASSERT_FAILED( anjay_security_object_restore(env->anjay_restored, env->stream)); - AVS_UNIT_ASSERT_FALSE(anjay_security_object_is_modified(env->anjay_restored)); + AVS_UNIT_ASSERT_FALSE( + anjay_security_object_is_modified(env->anjay_restored)); /* Restored Object remains untouched */ AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(env->restored_repr->instances), @@ -205,15 +207,13 @@ AVS_UNIT_TEST(security_persistence, modification_flag_add_instance) { const anjay_security_instance_t invalid_instance = { .server_uri = "" }; - AVS_UNIT_ASSERT_FAILED(anjay_security_object_add_instance(env->anjay_stored, - &invalid_instance, - &iid)); + AVS_UNIT_ASSERT_FAILED(anjay_security_object_add_instance( + env->anjay_stored, &invalid_instance, &iid)); AVS_UNIT_ASSERT_FALSE(anjay_security_object_is_modified(env->anjay_stored)); /* Same thing applies if the flag already was set to true */ _anjay_sec_mark_modified(_anjay_sec_get(env->stored)); - AVS_UNIT_ASSERT_FAILED(anjay_security_object_add_instance(env->anjay_stored, - &invalid_instance, - &iid)); + AVS_UNIT_ASSERT_FAILED(anjay_security_object_add_instance( + env->anjay_stored, &invalid_instance, &iid)); AVS_UNIT_ASSERT_TRUE(anjay_security_object_is_modified(env->anjay_stored)); _anjay_sec_clear_modified(_anjay_sec_get(env->stored)); diff --git a/modules/server/include_public/anjay/server.h b/modules/server/include_public/anjay/server.h index 736e95d4f..337d5d158 100644 --- a/modules/server/include_public/anjay/server.h +++ b/modules/server/include_public/anjay/server.h @@ -30,9 +30,11 @@ typedef struct { anjay_ssid_t ssid; /** Resource: Lifetime */ int32_t lifetime; - /** Resource: Default Minimum Period - or a negative value to disable presence */ + /** Resource: Default Minimum Period - or a negative value to disable + * presence */ int32_t default_min_period; - /** Resource: Default Maximum Period - or a negative value to disable presence */ + /** Resource: Default Maximum Period - or a negative value to disable + * presence */ int32_t default_max_period; /** Resource: Disable Timeout - or a negative value to disable presence */ int32_t disable_timeout; @@ -65,7 +67,6 @@ int anjay_server_object_add_instance(anjay_t *anjay, const anjay_server_instance_t *instance, anjay_iid_t *inout_iid); - /** * Removes all instances of Server Object leaving it in an empty state. * diff --git a/modules/server/src/mod_server.c b/modules/server/src/mod_server.c index ac58861f8..e6b12ac22 100644 --- a/modules/server/src/mod_server.c +++ b/modules/server/src/mod_server.c @@ -28,9 +28,8 @@ VISIBILITY_SOURCE_BEGIN -static inline server_instance_t * -find_instance(server_repr_t *repr, - anjay_iid_t iid) { +static inline server_instance_t *find_instance(server_repr_t *repr, + anjay_iid_t iid) { if (!repr) { return NULL; } @@ -100,8 +99,9 @@ static int add_instance(server_repr_t *repr, if (instance->binding) { if (!anjay_binding_mode_valid(instance->binding) || avs_simple_snprintf(new_instance->binding_buf, - sizeof(new_instance->binding_buf), - "%s", instance->binding) < 0) { + sizeof(new_instance->binding_buf), "%s", + instance->binding) + < 0) { server_log(ERROR, "Unsupported binding mode: %s", instance->binding); return -1; @@ -116,8 +116,8 @@ static int add_instance(server_repr_t *repr, AVS_LIST_CLEAR(&new_instance); return -1; } - server_log(INFO, "Added instance %u (SSID: %u)", - *inout_iid, instance->ssid); + server_log(INFO, "Added instance %u (SSID: %u)", *inout_iid, + instance->ssid); return 0; } @@ -179,7 +179,8 @@ static int serv_instance_create(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t *inout_iid, anjay_ssid_t ssid) { - (void) anjay; (void) ssid; + (void) anjay; + (void) ssid; server_repr_t *repr = _anjay_serv_get(obj_ptr); if (*inout_iid == ANJAY_IID_INVALID && assign_iid(repr, inout_iid)) { server_log(ERROR, "Cannot assign new Instance id"); @@ -376,22 +377,23 @@ static int serv_execute(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_execute_ctx_t *ctx) { - (void) anjay; (void) obj_ptr; (void) ctx; + (void) anjay; + (void) obj_ptr; + (void) ctx; server_instance_t *inst = find_instance(_anjay_serv_get(obj_ptr), iid); assert(inst); - switch ((server_rid_t)rid) { - case SERV_RES_DISABLE: - { - avs_time_duration_t disable_timeout = - inst->data.disable_timeout < 0 + switch ((server_rid_t) rid) { + case SERV_RES_DISABLE: { + avs_time_duration_t disable_timeout = + inst->data.disable_timeout < 0 ? AVS_TIME_DURATION_INVALID : avs_time_duration_from_scalar( - inst->data.disable_timeout, AVS_TIME_S); + inst->data.disable_timeout, AVS_TIME_S); - return anjay_disable_server_with_timeout(anjay, inst->data.ssid, - disable_timeout); - } + return anjay_disable_server_with_timeout(anjay, inst->data.ssid, + disable_timeout); + } case SERV_RES_REGISTRATION_UPDATE_TRIGGER: return anjay_schedule_registration_update(anjay, inst->data.ssid); @@ -473,10 +475,9 @@ server_repr_t *_anjay_serv_get(const anjay_dm_object_def_t *const *obj_ptr) { return AVS_CONTAINER_OF(obj_ptr, server_repr_t, def); } -int anjay_server_object_add_instance( - anjay_t *anjay, - const anjay_server_instance_t *instance, - anjay_iid_t *inout_iid) { +int anjay_server_object_add_instance(anjay_t *anjay, + const anjay_server_instance_t *instance, + anjay_iid_t *inout_iid) { assert(anjay); const anjay_dm_object_def_t *const *obj_ptr = @@ -512,7 +513,7 @@ static void server_purge(server_repr_t *repr) { static void server_delete(anjay_t *anjay, void *repr) { (void) anjay; - server_purge((server_repr_t*) repr); + server_purge((server_repr_t *) repr); avs_free(repr); } @@ -545,7 +546,8 @@ static const anjay_dm_module_t SERVER_MODULE = { int anjay_server_object_install(anjay_t *anjay) { assert(anjay); - server_repr_t *repr = (server_repr_t *) avs_calloc(1, sizeof(server_repr_t)); + server_repr_t *repr = + (server_repr_t *) avs_calloc(1, sizeof(server_repr_t)); if (!repr) { server_log(ERROR, "Out of memory"); return -1; @@ -570,5 +572,5 @@ int anjay_server_object_install(anjay_t *anjay) { } #ifdef ANJAY_TEST -#include "test/api.c" +# include "test/api.c" #endif diff --git a/modules/server/src/server_persistence.c b/modules/server/src/server_persistence.c index 9fa060e10..ed5acda94 100644 --- a/modules/server/src/server_persistence.c +++ b/modules/server/src/server_persistence.c @@ -17,15 +17,15 @@ #include #ifdef WITH_AVS_PERSISTENCE -#include +# include #endif // WITH_AVS_PERSISTENCE #include #include #include -#include #include +#include #include "mod_server.h" #include "server_transaction.h" @@ -40,8 +40,7 @@ VISIBILITY_SOURCE_BEGIN static const char MAGIC[] = { 'S', 'R', 'V', '\0' }; -static int handle_sized_fields(avs_persistence_context_t *ctx, - void *element_) { +static int handle_sized_fields(avs_persistence_context_t *ctx, void *element_) { server_instance_t *element = (server_instance_t *) element_; bool has_binding; int retval = 0; @@ -150,8 +149,9 @@ static int restore_instance(avs_persistence_context_t *ctx, } if (!retval && avs_simple_snprintf(element->binding_buf, - sizeof(element->binding_buf), - "%s", binding_str) < 0) { + sizeof(element->binding_buf), "%s", + binding_str) + < 0) { persistence_log(ERROR, "Could not restore binding: %s", binding_str); retval = -1; @@ -181,8 +181,8 @@ int anjay_server_object_persist(anjay_t *anjay, return -1; } retval = avs_persistence_list(ctx, (AVS_LIST(void) *) &repr->instances, - sizeof(server_instance_t), - persist_instance, NULL, NULL); + sizeof(server_instance_t), persist_instance, + NULL, NULL); avs_persistence_context_delete(ctx); if (!retval) { _anjay_serv_clear_modified(repr); @@ -224,8 +224,8 @@ int anjay_server_object_restore(anjay_t *anjay, repr->instances = NULL; retval = avs_persistence_list(restore_ctx, (AVS_LIST(void) *) &repr->instances, - sizeof(server_instance_t), - restore_instance, NULL, NULL); + sizeof(server_instance_t), restore_instance, + NULL, NULL); if (retval || (retval = _anjay_serv_object_validate(repr))) { _anjay_serv_destroy_instances(&repr->instances); repr->instances = backup.instances; @@ -240,22 +240,24 @@ int anjay_server_object_restore(anjay_t *anjay, return retval; } -#ifdef ANJAY_TEST -#include "test/persistence.c" -#endif +# ifdef ANJAY_TEST +# include "test/persistence.c" +# endif #else // WITH_AVS_PERSISTENCE int anjay_server_object_persist(anjay_t *anjay, avs_stream_abstract_t *out_stream) { - (void) anjay; (void) out_stream; + (void) anjay; + (void) out_stream; persistence_log(ERROR, "Persistence not compiled in"); return -1; } int anjay_server_object_restore(anjay_t *anjay, avs_stream_abstract_t *in_stream) { - (void) anjay; (void) in_stream; + (void) anjay; + (void) in_stream; persistence_log(ERROR, "Persistence not compiled in"); return -1; } diff --git a/modules/server/src/server_utils.c b/modules/server/src/server_utils.c index a924c406a..94910dd08 100644 --- a/modules/server/src/server_utils.c +++ b/modules/server/src/server_utils.c @@ -58,4 +58,3 @@ _anjay_serv_clone_instances(const server_repr_t *repr) { void _anjay_serv_destroy_instances(AVS_LIST(server_instance_t) *instances) { AVS_LIST_CLEAR(instances); } - diff --git a/modules/server/src/server_utils.h b/modules/server/src/server_utils.h index 686897ad5..3c0c4624d 100644 --- a/modules/server/src/server_utils.h +++ b/modules/server/src/server_utils.h @@ -40,7 +40,7 @@ int _anjay_serv_fetch_binding(anjay_input_ctx_t *ctx, AVS_LIST(server_instance_t) _anjay_serv_clone_instances(const server_repr_t *repr); -void _anjay_serv_destroy_instances(AVS_LIST(server_instance_t) * instances); +void _anjay_serv_destroy_instances(AVS_LIST(server_instance_t) *instances); VISIBILITY_PRIVATE_HEADER_END diff --git a/modules/server/src/test/api.c b/modules/server/src/test/api.c index 6eabcfdd5..fefc98fa7 100644 --- a/modules/server/src/test/api.c +++ b/modules/server/src/test/api.c @@ -30,8 +30,7 @@ typedef struct { SCOPED_PTR(server_test_env_t, server_test_env_destroy) \ Name = server_test_env_create(); -static server_test_env_t * -server_test_env_create(void) { +static server_test_env_t *server_test_env_create(void) { server_test_env_t *env = (__typeof__(env)) avs_calloc(1, sizeof(*env)); AVS_UNIT_ASSERT_NOT_NULL(env); env->anjay = anjay_new(&CONFIG); diff --git a/modules/server/src/test/persistence.c b/modules/server/src/test/persistence.c index dd24361d4..3674e1792 100644 --- a/modules/server/src/test/persistence.c +++ b/modules/server/src/test/persistence.c @@ -39,8 +39,7 @@ typedef struct { server_persistence_test_env_destroy) \ Name = server_persistence_test_env_create(); -static server_persistence_test_env_t * -server_persistence_test_env_create(void) { +static server_persistence_test_env_t *server_persistence_test_env_create(void) { server_persistence_test_env_t *env = (__typeof__(env)) avs_calloc(1, sizeof(*env)); AVS_UNIT_ASSERT_NOT_NULL(env); @@ -81,13 +80,16 @@ static void assert_instances_equal(const server_instance_t *a, if (a->has_lifetime) { AVS_UNIT_ASSERT_EQUAL(a->data.lifetime, b->data.lifetime); } - AVS_UNIT_ASSERT_EQUAL(a->has_notification_storing, b->has_notification_storing); + AVS_UNIT_ASSERT_EQUAL(a->has_notification_storing, + b->has_notification_storing); if (a->has_notification_storing) { - AVS_UNIT_ASSERT_EQUAL( - a->data.notification_storing, b->data.notification_storing); + AVS_UNIT_ASSERT_EQUAL(a->data.notification_storing, + b->data.notification_storing); } - AVS_UNIT_ASSERT_EQUAL(a->data.default_min_period, b->data.default_min_period); - AVS_UNIT_ASSERT_EQUAL(a->data.default_max_period, b->data.default_max_period); + AVS_UNIT_ASSERT_EQUAL(a->data.default_min_period, + b->data.default_min_period); + AVS_UNIT_ASSERT_EQUAL(a->data.default_max_period, + b->data.default_max_period); AVS_UNIT_ASSERT_EQUAL(a->data.disable_timeout, b->data.disable_timeout); } @@ -113,8 +115,8 @@ AVS_UNIT_TEST(server_persistence, nonempty_store_restore) { .notification_storing = true }; anjay_iid_t iid = 1; - AVS_UNIT_ASSERT_SUCCESS( - anjay_server_object_add_instance(env->anjay_stored, &instance, &iid)); + AVS_UNIT_ASSERT_SUCCESS(anjay_server_object_add_instance( + env->anjay_stored, &instance, &iid)); AVS_UNIT_ASSERT_SUCCESS( anjay_server_object_persist(env->anjay_stored, env->stream)); AVS_UNIT_ASSERT_SUCCESS( @@ -127,7 +129,8 @@ AVS_UNIT_TEST(server_persistence, nonempty_store_restore) { .has_lifetime = true, .has_notification_storing = true }; - assert_instances_equal(&expected_server_instance, env->restored_repr->instances); + assert_instances_equal(&expected_server_instance, + env->restored_repr->instances); } AVS_UNIT_TEST(server_persistence, modification_flag_add_instance) { @@ -159,8 +162,8 @@ AVS_UNIT_TEST(server_persistence, modification_flag_add_instance) { .notification_storing = true }; /* And valid instance does change the flag */ - AVS_UNIT_ASSERT_SUCCESS( - anjay_server_object_add_instance(env->anjay_stored, &instance, &iid)); + AVS_UNIT_ASSERT_SUCCESS(anjay_server_object_add_instance( + env->anjay_stored, &instance, &iid)); AVS_UNIT_ASSERT_TRUE(anjay_server_object_is_modified(env->anjay_stored)); } @@ -180,8 +183,8 @@ AVS_UNIT_TEST(server_persistence, modification_flag_purge) { .binding = "U", .notification_storing = true }; - AVS_UNIT_ASSERT_SUCCESS( - anjay_server_object_add_instance(env->anjay_stored, &instance, &iid)); + AVS_UNIT_ASSERT_SUCCESS(anjay_server_object_add_instance( + env->anjay_stored, &instance, &iid)); AVS_UNIT_ASSERT_TRUE(anjay_server_object_is_modified(env->anjay_stored)); /* Simulate persistence operation. */ _anjay_serv_clear_modified(_anjay_serv_get(env->stored)); diff --git a/src/access_control_utils.c b/src/access_control_utils.c index 1cc14ddd2..a0a998937 100644 --- a/src/access_control_utils.c +++ b/src/access_control_utils.c @@ -29,10 +29,8 @@ get_access_control(anjay_t *anjay) { return _anjay_dm_find_object_by_oid(anjay, ANJAY_DM_OID_ACCESS_CONTROL); } -static int read_u32(anjay_t *anjay, - anjay_iid_t iid, - anjay_rid_t rid, - uint32_t *out) { +static int +read_u32(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, uint32_t *out) { int64_t ret; const anjay_uri_path_t uri = MAKE_RESOURCE_PATH(ANJAY_DM_OID_ACCESS_CONTROL, iid, rid); @@ -62,11 +60,11 @@ static int read_resources(anjay_t *anjay, uint32_t owner, oid, oiid; int ret; if ((ret = read_u32(anjay, access_control_iid, - ANJAY_DM_RID_ACCESS_CONTROL_OID, &oid)) - || (ret = read_u32(anjay, access_control_iid, - ANJAY_DM_RID_ACCESS_CONTROL_OIID, &oiid)) - || (ret = read_u32(anjay, access_control_iid, - ANJAY_DM_RID_ACCESS_CONTROL_OWNER, &owner))) { + ANJAY_DM_RID_ACCESS_CONTROL_OID, &oid)) + || (ret = read_u32(anjay, access_control_iid, + ANJAY_DM_RID_ACCESS_CONTROL_OIID, &oiid)) + || (ret = read_u32(anjay, access_control_iid, + ANJAY_DM_RID_ACCESS_CONTROL_OWNER, &owner))) { return ret; } *out_oid = (anjay_oid_t) oid; @@ -90,7 +88,7 @@ static int get_mask_from_ctx(anjay_input_ctx_t *ctx, int32_t current_mask; *out_mask = ANJAY_ACCESS_MASK_NONE; while (!(result = anjay_get_array_index(array_ctx, ¤t_ssid)) - && !(result = anjay_get_i32(array_ctx, ¤t_mask))) { + && !(result = anjay_get_i32(array_ctx, ¤t_mask))) { if (current_ssid == ssid_lookup || current_ssid == 0) { // Found an entry for the given ssid or the default ACL entry *inout_ssid = current_ssid; @@ -164,8 +162,7 @@ static int get_mask(anjay_t *anjay, } static anjay_access_mask_t -access_control_mask(anjay_t *anjay, - const anjay_action_info_t *info) { +access_control_mask(anjay_t *anjay, const anjay_action_info_t *info) { get_mask_data_t data = { .oid = info->oid, .oiid = info->iid, @@ -181,8 +178,7 @@ access_control_mask(anjay_t *anjay, return data.result; } -static bool can_instantiate(anjay_t *anjay, - const anjay_action_info_t *info) { +static bool can_instantiate(anjay_t *anjay, const anjay_action_info_t *info) { get_mask_data_t data = { .oid = info->oid, .oiid = ANJAY_IID_INVALID, @@ -204,9 +200,8 @@ typedef struct { anjay_ssid_t owner; } get_owner_data_t; -static int count_non_bootstrap_clb(anjay_t *anjay, - anjay_ssid_t ssid, - void *counter_ptr) { +static int +count_non_bootstrap_clb(anjay_t *anjay, anjay_ssid_t ssid, void *counter_ptr) { (void) anjay; if (ssid != ANJAY_SSID_BOOTSTRAP) { ++*((size_t *) counter_ptr); @@ -227,7 +222,8 @@ bool _anjay_access_control_action_allowed(anjay_t *anjay, const anjay_action_info_t *info) { if (info->oid == ANJAY_DM_OID_SECURITY) { return false; - } else if (!get_access_control(anjay) || is_single_ssid_environment(anjay)) { + } else if (!get_access_control(anjay) + || is_single_ssid_environment(anjay)) { return true; } @@ -235,7 +231,7 @@ bool _anjay_access_control_action_allowed(anjay_t *anjay, if (info->action == ANJAY_ACTION_READ) { return true; } else if (info->action == ANJAY_ACTION_CREATE - || info->action == ANJAY_ACTION_DELETE) { + || info->action == ANJAY_ACTION_DELETE) { return false; } uint32_t owner; diff --git a/src/access_control_utils.h b/src/access_control_utils.h index a54cc9c70..56694a198 100644 --- a/src/access_control_utils.h +++ b/src/access_control_utils.h @@ -32,11 +32,12 @@ typedef struct { #ifdef WITH_ACCESS_CONTROL bool _anjay_access_control_action_allowed(anjay_t *anjay, - const anjay_action_info_t* info); + const anjay_action_info_t *info); #else -#define _anjay_access_control_action_allowed(anjay, info) ((void) (info), true) +# define _anjay_access_control_action_allowed(anjay, info) \ + ((void) (info), true) #endif diff --git a/src/anjay_core.c b/src/anjay_core.c index c78c6142a..dc0484849 100644 --- a/src/anjay_core.c +++ b/src/anjay_core.c @@ -37,21 +37,20 @@ #include #include "anjay_core.h" +#include "coap/coap_stream.h" +#include "coap/content_format.h" +#include "coap/id_source/auto.h" #include "dm_core.h" #include "downloader.h" +#include "interface/bootstrap_core.h" +#include "interface/register.h" #include "io_core.h" #include "servers_utils.h" #include "utils_core.h" -#include "coap/content_format.h" -#include "coap/coap_stream.h" -#include "coap/id_source/auto.h" -#include "interface/bootstrap_core.h" -#include "interface/register.h" VISIBILITY_SOURCE_BEGIN -static int init(anjay_t *anjay, - const anjay_configuration_t *config) { +static int init(anjay_t *anjay, const anjay_configuration_t *config) { anjay->dtls_version = config->dtls_version; if (anjay->dtls_version == AVS_NET_SSL_VERSION_DEFAULT) { anjay->dtls_version = AVS_NET_SSL_VERSION_TLSv1_2; @@ -65,6 +64,7 @@ static int init(anjay_t *anjay, anjay->udp_socket_config = config->udp_socket_config; anjay->udp_listen_port = config->udp_listen_port; + anjay->current_connection.conn_type = ANJAY_CONNECTION_UNSET; const char *error_msg; if (config->udp_tx_params) { @@ -79,6 +79,19 @@ static int init(anjay_t *anjay, (avs_coap_tx_params_t) ANJAY_COAP_DEFAULT_UDP_TX_PARAMS; } + if (config->udp_dtls_hs_tx_params) { + if (!avs_time_duration_less(config->udp_dtls_hs_tx_params->min, + config->udp_dtls_hs_tx_params->max)) { + anjay_log(ERROR, "UDP DTLS Handshake transmission parameters are " + "invalid: min >= max"); + return -1; + } + anjay->udp_dtls_hs_tx_params = *config->udp_dtls_hs_tx_params; + } else { + anjay->udp_dtls_hs_tx_params = (avs_net_dtls_handshake_timeouts_t) + ANJAY_DTLS_DEFAULT_UDP_HS_TX_PARAMS; + } + anjay->servers = _anjay_servers_create(); if (!anjay->servers) { @@ -133,7 +146,7 @@ static int init(anjay_t *anjay, coap_id_source_t *id_source = NULL; #ifdef WITH_BLOCK_DOWNLOAD if (!(id_source = _anjay_coap_id_source_auto_new( - 0, AVS_COAP_MAX_TOKEN_LENGTH))) { + 0, AVS_COAP_MAX_TOKEN_LENGTH))) { anjay_log(ERROR, "Out of memory"); return -1; } @@ -172,7 +185,8 @@ anjay_t *anjay_new(const anjay_configuration_t *config) { } void _anjay_release_server_stream_without_scheduling_queue(anjay_t *anjay) { - memset(&anjay->current_connection, 0, sizeof(anjay->current_connection)); + anjay->current_connection.server = NULL; + anjay->current_connection.conn_type = ANJAY_CONNECTION_UNSET; avs_stream_reset(anjay->comm_stream); if (avs_stream_net_setsock(anjay->comm_stream, NULL)) { anjay_log(ERROR, "could not set stream socket to NULL"); @@ -218,9 +232,8 @@ void anjay_delete(anjay_t *anjay) { anjay_delete_impl(anjay, true); } -static void split_query_string(char *query, - const char **out_key, - const char **out_value) { +static void +split_query_string(char *query, const char **out_key, const char **out_value) { char *eq = strchr(query, '='); *out_key = query; @@ -249,7 +262,7 @@ static int parse_nullable_period(const char *key_str, return -1; } else { *out_present = true; - *out_value = (int32_t)num; + *out_value = (int32_t) num; return 0; } } @@ -341,8 +354,9 @@ static int parse_attributes(const avs_coap_msg_t *msg, int result; avs_coap_opt_iterator_t it = AVS_COAP_OPT_ITERATOR_EMPTY; while ((result = avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_URI_QUERY, &it, - &attr_size, buffer, sizeof(buffer) - 1)) == 0) { + msg, AVS_COAP_OPT_URI_QUERY, &it, &attr_size, buffer, + sizeof(buffer) - 1)) + == 0) { const char *key; const char *value; @@ -365,16 +379,26 @@ static int parse_attributes(const avs_coap_msg_t *msg, static const char *action_to_string(anjay_request_action_t action) { switch (action) { - case ANJAY_ACTION_READ: return "Read"; - case ANJAY_ACTION_DISCOVER: return "Discover"; - case ANJAY_ACTION_WRITE: return "Write"; - case ANJAY_ACTION_WRITE_UPDATE: return "Write (Update)"; - case ANJAY_ACTION_WRITE_ATTRIBUTES: return "Write Attributes"; - case ANJAY_ACTION_EXECUTE: return "Execute"; - case ANJAY_ACTION_CREATE: return "Create"; - case ANJAY_ACTION_DELETE: return "Delete"; - case ANJAY_ACTION_CANCEL_OBSERVE: return "Cancel Observe"; - case ANJAY_ACTION_BOOTSTRAP_FINISH: return "Bootstrap Finish"; + case ANJAY_ACTION_READ: + return "Read"; + case ANJAY_ACTION_DISCOVER: + return "Discover"; + case ANJAY_ACTION_WRITE: + return "Write"; + case ANJAY_ACTION_WRITE_UPDATE: + return "Write (Update)"; + case ANJAY_ACTION_WRITE_ATTRIBUTES: + return "Write Attributes"; + case ANJAY_ACTION_EXECUTE: + return "Execute"; + case ANJAY_ACTION_CREATE: + return "Create"; + case ANJAY_ACTION_DELETE: + return "Delete"; + case ANJAY_ACTION_CANCEL_OBSERVE: + return "Cancel Observe"; + case ANJAY_ACTION_BOOTSTRAP_FINISH: + return "Bootstrap Finish"; } AVS_UNREACHABLE("invalid enum value"); return ""; @@ -458,9 +482,9 @@ static int get_msg_action(avs_coap_msg_type_t msg_type, static int parse_action(const avs_coap_msg_t *msg, anjay_request_t *inout_request) { - if (avs_coap_msg_get_option_uint( - msg, AVS_COAP_OPT_ACCEPT, &inout_request->requested_format, - sizeof(inout_request->requested_format))) { + if (avs_coap_msg_get_option_uint(msg, AVS_COAP_OPT_ACCEPT, + &inout_request->requested_format, + sizeof(inout_request->requested_format))) { inout_request->requested_format = AVS_COAP_FORMAT_NONE; } @@ -469,8 +493,7 @@ static int parse_action(const avs_coap_msg_t *msg, inout_request->requested_format, inout_request->is_bs_uri, inout_request->uri.type, - inout_request->content_format - != AVS_COAP_FORMAT_NONE, + inout_request->content_format != AVS_COAP_FORMAT_NONE, &inout_request->action); } @@ -478,14 +501,12 @@ static int parse_request_uri_segment(const char *uri, uint16_t *out_id, uint16_t max_valid_id) { long long num; - if (_anjay_safe_strtoll(uri, &num) - || num < 0 - || num > max_valid_id) { + if (_anjay_safe_strtoll(uri, &num) || num < 0 || num > max_valid_id) { anjay_log(ERROR, "invalid Uri-Path segment: %s", uri); return -1; } - *out_id = (uint16_t)num; + *out_id = (uint16_t) num; return 0; } @@ -496,17 +517,17 @@ static int parse_bs_uri(const avs_coap_msg_t *msg, bool *out_is_bs) { *out_is_bs = false; avs_coap_opt_iterator_t optit = AVS_COAP_OPT_ITERATOR_EMPTY; - int result = avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_URI_PATH, &optit, - &uri_size, uri, sizeof(uri) - 1); + int result = avs_coap_msg_get_option_string_it(msg, AVS_COAP_OPT_URI_PATH, + &optit, &uri_size, uri, + sizeof(uri) - 1); if (result) { return (result == AVS_COAP_OPTION_MISSING) ? 0 : result; } if (strcmp(uri, "bs") == 0) { - result = avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_URI_PATH, &optit, - &uri_size, uri, sizeof(uri) - 1); + result = avs_coap_msg_get_option_string_it(msg, AVS_COAP_OPT_URI_PATH, + &optit, &uri_size, uri, + sizeof(uri) - 1); if (result == AVS_COAP_OPTION_MISSING) { *out_is_bs = true; return 0; @@ -516,8 +537,7 @@ static int parse_bs_uri(const avs_coap_msg_t *msg, bool *out_is_bs) { return result; } -static int parse_dm_uri(const avs_coap_msg_t *msg, - anjay_uri_path_t *out_uri) { +static int parse_dm_uri(const avs_coap_msg_t *msg, anjay_uri_path_t *out_uri) { char uri[ANJAY_MAX_URI_SEGMENT_SIZE] = ""; size_t uri_size; @@ -527,17 +547,16 @@ static int parse_dm_uri(const avs_coap_msg_t *msg, uint16_t *id; anjay_uri_path_type_t type; uint16_t max_valid_value; - } ids[] = { - { &out_uri->oid, ANJAY_PATH_OBJECT, UINT16_MAX }, - { &out_uri->iid, ANJAY_PATH_INSTANCE, UINT16_MAX - 1 }, - { &out_uri->rid, ANJAY_PATH_RESOURCE, UINT16_MAX } - }; + } ids[] = { { &out_uri->oid, ANJAY_PATH_OBJECT, UINT16_MAX }, + { &out_uri->iid, ANJAY_PATH_INSTANCE, UINT16_MAX - 1 }, + { &out_uri->rid, ANJAY_PATH_RESOURCE, UINT16_MAX } }; avs_coap_opt_iterator_t optit = AVS_COAP_OPT_ITERATOR_EMPTY; for (size_t i = 0; i < AVS_ARRAY_SIZE(ids); ++i) { - int result = avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_URI_PATH, &optit, - &uri_size, uri, sizeof(uri) - 1); + int result = + avs_coap_msg_get_option_string_it(msg, AVS_COAP_OPT_URI_PATH, + &optit, &uri_size, uri, + sizeof(uri) - 1); if (result == AVS_COAP_OPTION_MISSING) { return 0; } else if (result) { @@ -551,9 +570,8 @@ static int parse_dm_uri(const avs_coap_msg_t *msg, } // 3 or more segments... - if (avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_URI_PATH, &optit, - &uri_size, uri, sizeof(uri) - 1) + if (avs_coap_msg_get_option_string_it(msg, AVS_COAP_OPT_URI_PATH, &optit, + &uri_size, uri, sizeof(uri) - 1) != AVS_COAP_OPTION_MISSING) { anjay_log(ERROR, "prefixed Uri-Path are not supported"); return -1; @@ -576,11 +594,10 @@ static int parse_request_uri(const avs_coap_msg_t *msg, } } -static int parse_observe(const avs_coap_msg_t *msg, - anjay_coap_observe_t *out) { +static int parse_observe(const avs_coap_msg_t *msg, anjay_coap_observe_t *out) { uint32_t raw_value; - int retval = avs_coap_msg_get_option_uint( - msg, AVS_COAP_OPT_OBSERVE, &raw_value, sizeof(raw_value)); + int retval = avs_coap_msg_get_option_uint(msg, AVS_COAP_OPT_OBSERVE, + &raw_value, sizeof(raw_value)); if (retval == AVS_COAP_OPTION_MISSING) { *out = ANJAY_COAP_OBSERVE_NONE; return 0; @@ -606,26 +623,30 @@ static int parse_request(const avs_coap_msg_t *msg, out_request->msg_type = avs_coap_msg_get_type(msg); out_request->request_code = avs_coap_msg_get_code(msg); if (parse_observe(msg, &out_request->observe) - || parse_request_uri(msg, &out_request->is_bs_uri, &out_request->uri) - || parse_attributes(msg, &out_request->attributes) - || avs_coap_msg_get_content_format(msg, &out_request->content_format) - || parse_action(msg, out_request)) { + || parse_request_uri(msg, &out_request->is_bs_uri, + &out_request->uri) + || parse_attributes(msg, &out_request->attributes) + || avs_coap_msg_get_content_format(msg, + &out_request->content_format) + || parse_action(msg, out_request)) { return -1; } return 0; } uint8_t _anjay_make_error_response_code(int handler_result) { - uint8_t handler_code = (uint8_t)(-handler_result); + uint8_t handler_code = (uint8_t) (-handler_result); int cls = avs_coap_msg_code_get_class(handler_code); if (cls == 4 || cls == 5) { return handler_code; - } else switch (handler_result) { - case ANJAY_OUTCTXERR_FORMAT_MISMATCH: - return AVS_COAP_CODE_NOT_ACCEPTABLE; - default: - anjay_log(ERROR, "invalid error code: %d", handler_result); - return -ANJAY_ERR_INTERNAL; + } else { + switch (handler_result) { + case ANJAY_OUTCTXERR_FORMAT_MISMATCH: + return AVS_COAP_CODE_NOT_ACCEPTABLE; + default: + anjay_log(ERROR, "invalid error code: %d", handler_result); + return -ANJAY_ERR_INTERNAL; + } } } @@ -633,12 +654,11 @@ static bool critical_option_validator(uint8_t msg_code, uint32_t optnum) { /* Note: BLOCK Options are handled inside stream.c */ switch (msg_code) { case AVS_COAP_CODE_GET: - return optnum == AVS_COAP_OPT_URI_PATH - || optnum == AVS_COAP_OPT_ACCEPT; + return optnum == AVS_COAP_OPT_URI_PATH || optnum == AVS_COAP_OPT_ACCEPT; case AVS_COAP_CODE_PUT: case AVS_COAP_CODE_POST: return optnum == AVS_COAP_OPT_URI_PATH - || optnum == AVS_COAP_OPT_URI_QUERY; + || optnum == AVS_COAP_OPT_URI_QUERY; case AVS_COAP_CODE_DELETE: return optnum == AVS_COAP_OPT_URI_PATH; default: @@ -651,8 +671,7 @@ static int block_request_equality_validator(const avs_coap_msg_t *msg, const anjay_request_t *orig_request = (const anjay_request_t *) orig_request_; anjay_request_t block_request; - if (avs_coap_msg_validate_critical_options(msg, - critical_option_validator) + if (avs_coap_msg_validate_critical_options(msg, critical_option_validator) || parse_request(msg, &block_request) || !_anjay_request_equal(&block_request, orig_request)) { return -1; @@ -729,8 +748,8 @@ static int handle_incoming_message(anjay_t *anjay) { anjay_request_t request; if (_anjay_coap_stream_get_request_identity(anjay->comm_stream, &request_identity) - || avs_coap_msg_validate_critical_options( - request_msg, critical_option_validator) + || avs_coap_msg_validate_critical_options(request_msg, + critical_option_validator) || parse_request(request_msg, &request)) { if (avs_coap_msg_code_is_request(avs_coap_msg_get_code(request_msg))) { if (_anjay_coap_stream_set_error(anjay->comm_stream, @@ -768,8 +787,8 @@ int _anjay_bind_server_stream(anjay_t *anjay, anjay_connection_ref_t ref) { } if (avs_stream_net_setsock(anjay->comm_stream, socket) || _anjay_coap_stream_set_tx_params( - anjay->comm_stream, - _anjay_tx_params_for_conn_type(anjay, ref.conn_type))) { + anjay->comm_stream, + _anjay_tx_params_for_conn_type(anjay, ref.conn_type))) { anjay_log(ERROR, "could not set stream socket"); return -1; } @@ -785,14 +804,12 @@ void _anjay_release_server_stream(anjay_t *anjay) { _anjay_release_server_stream_without_scheduling_queue(anjay); } -static int udp_serve(anjay_t *anjay, - avs_net_abstract_socket_t *ready_socket) { +static int udp_serve(anjay_t *anjay, avs_net_abstract_socket_t *ready_socket) { anjay_connection_ref_t connection = { .server = _anjay_servers_find_by_udp_socket(anjay, ready_socket), .conn_type = ANJAY_CONNECTION_UDP }; - if (!connection.server - || _anjay_bind_server_stream(anjay, connection)) { + if (!connection.server || _anjay_bind_server_stream(anjay, connection)) { return -1; } @@ -802,8 +819,7 @@ static int udp_serve(anjay_t *anjay, } -int anjay_serve(anjay_t *anjay, - avs_net_abstract_socket_t *ready_socket) { +int anjay_serve(anjay_t *anjay, avs_net_abstract_socket_t *ready_socket) { #ifdef WITH_DOWNLOADER if (!_anjay_downloader_handle_packet(&anjay->downloader, ready_socket)) { return 0; @@ -813,8 +829,7 @@ int anjay_serve(anjay_t *anjay, return udp_serve(anjay, ready_socket); } -int anjay_sched_time_to_next(anjay_t *anjay, - avs_time_duration_t *out_delay) { +int anjay_sched_time_to_next(anjay_t *anjay, avs_time_duration_t *out_delay) { return _anjay_sched_time_to_next(anjay->sched, out_delay); } @@ -857,7 +872,7 @@ anjay_download_handle_t anjay_download(anjay_t *anjay, anjay_download_handle_t result = NULL; errno = -_anjay_downloader_download(&anjay->downloader, &result, config); return result; -#else // WITH_DOWNLOADER +#else // WITH_DOWNLOADER (void) anjay; (void) config; anjay_log(ERROR, "CoAP download support disabled"); @@ -866,11 +881,10 @@ anjay_download_handle_t anjay_download(anjay_t *anjay, #endif // WITH_DOWNLOADER } -void anjay_download_abort(anjay_t *anjay, - anjay_download_handle_t handle) { +void anjay_download_abort(anjay_t *anjay, anjay_download_handle_t handle) { #ifdef WITH_DOWNLOADER _anjay_downloader_abort(&anjay->downloader, handle); -#else // WITH_DOWNLOADER +#else // WITH_DOWNLOADER (void) anjay; (void) handle; anjay_log(ERROR, "CoAP download support disabled"); @@ -921,5 +935,5 @@ uint64_t anjay_get_num_outgoing_retransmissions(anjay_t *anjay) { #ifdef ANJAY_TEST -#include "test/anjay.c" +# include "test/anjay.c" #endif // ANJAY_TEST diff --git a/src/anjay_core.h b/src/anjay_core.h index eba5aaab9..94bead365 100644 --- a/src/anjay_core.h +++ b/src/anjay_core.h @@ -18,16 +18,16 @@ #define ANJAY_CORE_H #include -#include #include +#include #include "dm_core.h" #include "observe/observe_core.h" -#include "servers.h" -#include "utils_core.h" #include "downloader.h" #include "interface/bootstrap_core.h" +#include "servers.h" +#include "utils_core.h" VISIBILITY_PRIVATE_HEADER_BEGIN @@ -57,6 +57,7 @@ struct anjay_struct { anjay_bootstrap_t bootstrap; #endif avs_coap_tx_params_t udp_tx_params; + avs_net_dtls_handshake_timeouts_t udp_dtls_hs_tx_params; avs_coap_ctx_t *coap_ctx; avs_stream_abstract_t *comm_stream; anjay_connection_ref_t current_connection; @@ -78,9 +79,9 @@ struct anjay_struct { #define ANJAY_DM_DEFAULT_PMIN_VALUE 1 -#define _anjay_sms_router(Anjay) NULL -#define _anjay_local_msisdn(Anjay) NULL -#define _anjay_sms_poll_socket(Anjay) NULL +# define _anjay_sms_router(Anjay) NULL +# define _anjay_local_msisdn(Anjay) NULL +# define _anjay_sms_poll_socket(Anjay) NULL uint8_t _anjay_make_error_response_code(int handler_result); @@ -105,4 +106,3 @@ anjay_sched_t *_anjay_sched_new(anjay_t *anjay); VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_CORE_H */ - diff --git a/src/coap/block/request.c b/src/coap/block/request.c index 1a3051703..8baf818b3 100644 --- a/src/coap/block/request.c +++ b/src/coap/block/request.c @@ -16,18 +16,18 @@ #include -#include #include +#include #define ANJAY_COAP_STREAM_INTERNALS #include "../coap_log.h" +#include "../id_source/auto.h" #include "../stream/common.h" #include "../stream/out.h" -#include "../id_source/auto.h" -#include "transfer_impl.h" #include "request.h" +#include "transfer_impl.h" VISIBILITY_SOURCE_BEGIN @@ -36,8 +36,8 @@ static bool is_separate_ack(const avs_coap_msg_t *msg, avs_coap_msg_type_t type = avs_coap_msg_get_type(msg); return type == AVS_COAP_MSG_ACKNOWLEDGEMENT - && avs_coap_msg_get_code(msg) == AVS_COAP_CODE_EMPTY - && avs_coap_msg_get_id(request) == avs_coap_msg_get_id(msg); + && avs_coap_msg_get_code(msg) == AVS_COAP_CODE_EMPTY + && avs_coap_msg_get_id(request) == avs_coap_msg_get_id(msg); } static bool response_token_matches(const avs_coap_msg_t *request, @@ -73,8 +73,9 @@ static bool is_matching_response(const avs_coap_msg_t *msg, return true; } -static int block_request_update_block_option(coap_block_transfer_ctx_t *ctx, - const avs_coap_block_info_t *block) { +static int +block_request_update_block_option(coap_block_transfer_ctx_t *ctx, + const avs_coap_block_info_t *block) { if (block->size == ctx->block.size) { ++ctx->block.seq_num; return 0; @@ -84,13 +85,15 @@ static int block_request_update_block_option(coap_block_transfer_ctx_t *ctx, if (block->seq_num != 0) { coap_log(WARNING, "server requested block size change in the middle of " - "a transfer"); + "a transfer"); return -1; } if (block->size > ctx->block.size) { - coap_log(WARNING, "server requested block size bigger than original" - "(%u, was %u)", block->size, ctx->block.size); + coap_log(WARNING, + "server requested block size bigger than original" + "(%u, was %u)", + block->size, ctx->block.size); return -1; } @@ -106,20 +109,21 @@ static int handle_block_options(const avs_coap_msg_t *msg, if (avs_coap_get_block_info(msg, AVS_COAP_BLOCK1, &block1) || !block1.valid) { coap_log(DEBUG, "BLOCK1 missing or invalid in response to block-wise " - "request"); + "request"); return -1; } avs_coap_block_info_t block2; if (avs_coap_get_block_info(msg, AVS_COAP_BLOCK2, &block2) || block2.valid) { coap_log(DEBUG, "block-wise responses to block-wise requests are not " - "supported"); + "supported"); return -1; } if (block1.seq_num != ctx->block.seq_num) { - coap_log(DEBUG, "mismatched block number: got %" PRIu32 ", expected %" - PRIu32, block1.seq_num, ctx->block.seq_num); + coap_log(DEBUG, + "mismatched block number: got %" PRIu32 ", expected %" PRIu32, + block1.seq_num, ctx->block.seq_num); return -1; } diff --git a/src/coap/block/response.c b/src/coap/block/response.c index 3e0d9115d..6259333e7 100644 --- a/src/coap/block/response.c +++ b/src/coap/block/response.c @@ -21,26 +21,29 @@ #include "../coap_log.h" -#include "transfer_impl.h" -#include "response.h" -#include "../stream/common.h" #include "../id_source/static.h" +#include "../stream/common.h" +#include "response.h" +#include "transfer_impl.h" VISIBILITY_SOURCE_BEGIN -static int handle_block_size_renegotiation(coap_block_transfer_ctx_t *ctx, - const avs_coap_block_info_t *block2) { +static int +handle_block_size_renegotiation(coap_block_transfer_ctx_t *ctx, + const avs_coap_block_info_t *block2) { assert(block2->size >= AVS_COAP_MSG_BLOCK_MIN_SIZE - && block2->size <= AVS_COAP_MSG_BLOCK_MAX_SIZE); + && block2->size <= AVS_COAP_MSG_BLOCK_MAX_SIZE); if (block2->size > ctx->block.size) { - coap_log(WARNING, "client attempted to increase block size from %u to " - "%u B", ctx->block.size, block2->size); + coap_log(WARNING, + "client attempted to increase block size from %u to " + "%u B", + ctx->block.size, block2->size); return -1; } else if (block2->size < ctx->block.size) { if (block2->seq_num != 0 || ctx->num_sent_blocks != 0) { coap_log(ERROR, "client changed block size in the middle of block " - "transfer"); + "transfer"); return -1; } else { coap_log(TRACE, "lowering block size to %u B on client request", @@ -59,15 +62,14 @@ static int block_recv_handler(void *validator_ctx_, bool *out_wait_for_next, uint8_t *out_error_code) { anjay_coap_block_request_validator_ctx_t *validator_ctx = - (anjay_coap_block_request_validator_ctx_t *)validator_ctx_; + (anjay_coap_block_request_validator_ctx_t *) validator_ctx_; (void) last_response; *out_wait_for_next = false; avs_coap_msg_identity_t id = avs_coap_msg_get_identity(msg); - avs_coap_msg_identity_t prev_id = - avs_coap_msg_get_identity(last_response); + avs_coap_msg_identity_t prev_id = avs_coap_msg_get_identity(last_response); // Message identity matches last response, it means it must be a duplicate // of the previous request. @@ -97,9 +99,9 @@ static int block_recv_handler(void *validator_ctx_, *out_error_code = -ANJAY_ERR_BAD_REQUEST; return -1; } else if (!block2.valid // no BLOCK2 option - must be an unrelated request - || (validator_ctx && validator_ctx->validator - && validator_ctx->validator( - msg, validator_ctx->validator_arg))) { + || (validator_ctx && validator_ctx->validator + && validator_ctx->validator(msg, + validator_ctx->validator_arg))) { *out_wait_for_next = true; *out_error_code = -ANJAY_ERR_SERVICE_UNAVAILABLE; return -1; @@ -125,8 +127,7 @@ static int block_recv_handler(void *validator_ctx_, return BLOCK_TRANSFER_RESULT_OK; } -coap_block_transfer_ctx_t * -_anjay_coap_block_response_new( +coap_block_transfer_ctx_t *_anjay_coap_block_response_new( uint16_t max_block_size, coap_stream_common_t *stream_data, coap_id_source_t *id_source, diff --git a/src/coap/block/response.h b/src/coap/block/response.h index cbbe7761f..50969e5e9 100644 --- a/src/coap/block/response.h +++ b/src/coap/block/response.h @@ -21,15 +21,15 @@ #include #include -#include #include +#include #include "transfer.h" +#include "../id_source/id_source.h" #include "../stream/common.h" #include "../stream/in.h" #include "../stream/out.h" -#include "../id_source/id_source.h" VISIBILITY_PRIVATE_HEADER_BEGIN @@ -56,8 +56,7 @@ typedef struct { * * @returns Created block_response object on success, NULL on failure. */ -coap_block_transfer_ctx_t * -_anjay_coap_block_response_new( +coap_block_transfer_ctx_t *_anjay_coap_block_response_new( uint16_t max_block_size, coap_stream_common_t *stream_data, coap_id_source_t *id_source, @@ -68,9 +67,11 @@ _anjay_coap_block_response_last_request_id(coap_block_transfer_ctx_t *ctx); #else -#define _anjay_coap_block_response_last_request_id(...) \ - (AVS_UNREACHABLE("should never happen"), \ - (avs_coap_msg_identity_t) { .msg_id = 0 }) +# define _anjay_coap_block_response_last_request_id(...) \ + (AVS_UNREACHABLE("should never happen"), \ + (avs_coap_msg_identity_t) { \ + .msg_id = 0 \ + }) #endif diff --git a/src/coap/block/transfer.c b/src/coap/block/transfer.c index 16417da32..95c781d87 100644 --- a/src/coap/block/transfer.c +++ b/src/coap/block/transfer.c @@ -16,15 +16,15 @@ #include -#include #include +#include #define ANJAY_COAP_STREAM_INTERNALS #include "../coap_log.h" -#include #include +#include #include "../stream/common.h" #include "../stream/out.h" @@ -36,11 +36,10 @@ VISIBILITY_SOURCE_BEGIN static size_t mtu_enforced_payload_capacity(const coap_output_buffer_t *out) { size_t headers_overhead = - avs_coap_msg_info_get_storage_size(&out->info) - - offsetof(avs_coap_msg_t, content) - /* assume the header does not contain the BLOCK option */ - + AVS_COAP_OPT_BLOCK_MAX_SIZE - + sizeof(AVS_COAP_PAYLOAD_MARKER); + avs_coap_msg_info_get_storage_size(&out->info) + - offsetof(avs_coap_msg_t, content) + /* assume the header does not contain the BLOCK option */ + + AVS_COAP_OPT_BLOCK_MAX_SIZE + sizeof(AVS_COAP_PAYLOAD_MARKER); if (headers_overhead < out->dgram_layer_mtu) { return out->dgram_layer_mtu - headers_overhead; @@ -51,24 +50,24 @@ static size_t mtu_enforced_payload_capacity(const coap_output_buffer_t *out) { static size_t buffer_size_enforced_payload_capacity(const coap_output_buffer_t *out) { - /* The flow assumes that the last block is only sent from finish_response. - * Because of that, we MUST NOT flush the last block of a transfer even if - * it's ready - we must wait for either finish_response call or another byte - * (that would make current block not-the-last-one). - * The max_block_size < payload_capacity condition causes that -1. */ + /* The flow assumes that the last block is only sent from finish_response. + * Because of that, we MUST NOT flush the last block of a transfer even if + * it's ready - we must wait for either finish_response call or another byte + * (that would make current block not-the-last-one). + * The max_block_size < payload_capacity condition causes that -1. */ return out->buffer_capacity < 1 ? 0 : out->buffer_capacity - 1; } static uint16_t calculate_proposed_block_size(uint16_t original_block_size, const coap_output_buffer_t *out) { - size_t payload_capacity_considering_mtu = AVS_MIN( - mtu_enforced_payload_capacity(out), - buffer_size_enforced_payload_capacity(out)); + size_t payload_capacity_considering_mtu = + AVS_MIN(mtu_enforced_payload_capacity(out), + buffer_size_enforced_payload_capacity(out)); size_t max_block_size = (payload_capacity_considering_mtu > 0) - ? _anjay_max_power_of_2_not_greater_than( - payload_capacity_considering_mtu) - : 0; + ? _anjay_max_power_of_2_not_greater_than( + payload_capacity_considering_mtu) + : 0; if (max_block_size < AVS_COAP_MSG_BLOCK_MIN_SIZE) { coap_log(ERROR, "MTU is too low to send block response"); @@ -77,8 +76,9 @@ static uint16_t calculate_proposed_block_size(uint16_t original_block_size, if (max_block_size < original_block_size) { assert(max_block_size <= AVS_COAP_MSG_BLOCK_MAX_SIZE); - coap_log(INFO, "Lowering proposed block size to %u due to buffer size " - "or MTU constraints", + coap_log(INFO, + "Lowering proposed block size to %u due to buffer size " + "or MTU constraints", (unsigned) max_block_size); return (uint16_t) max_block_size; } @@ -103,14 +103,14 @@ _anjay_coap_block_transfer_new(uint16_t max_block_size, return NULL; } - coap_block_transfer_ctx_t *ctx = (coap_block_transfer_ctx_t *) - avs_calloc(1, sizeof(coap_block_transfer_ctx_t)); + coap_block_transfer_ctx_t *ctx = (coap_block_transfer_ctx_t *) avs_calloc( + 1, sizeof(coap_block_transfer_ctx_t)); if (!ctx) { return NULL; } - *ctx = (coap_block_transfer_ctx_t){ + *ctx = (coap_block_transfer_ctx_t) { .timed_out = false, .num_sent_blocks = 0, .coap_ctx = stream_data->coap_ctx, @@ -150,20 +150,23 @@ static int block_recv(const avs_coap_msg_t *msg, void *data_, bool *out_wait_for_next, uint8_t *out_error_code) { - block_recv_data_t *data = (block_recv_data_t *)data_; - return data->ctx->block_recv_handler(data->ctx->block_recv_handler_arg, - msg, data->sent_msg, data->ctx, + block_recv_data_t *data = (block_recv_data_t *) data_; + return data->ctx->block_recv_handler(data->ctx->block_recv_handler_arg, msg, + data->sent_msg, data->ctx, out_wait_for_next, out_error_code); } - static bool should_wait_for_response(coap_block_transfer_ctx_t *ctx) { - /* for intermediate blocks, transfer direction does not matter - we need - * to wait until we receive a response*/ + /* + * For intermediate blocks, transfer direction does not matter - we need + * to wait until we receive a response + */ return ctx->block.has_more - /* For the last response block, we do not expect more requests. - * In case of requests, we still need to receive an actual response. */ - || ctx->block.type == AVS_COAP_BLOCK1; + /* + * For the last response block, we do not expect more requests. + * In case of requests, we still need to receive an actual response. + */ + || ctx->block.type == AVS_COAP_BLOCK1; } static int accept_response_with_timeout(coap_block_transfer_ctx_t *ctx, @@ -181,8 +184,8 @@ static int accept_response_with_timeout(coap_block_transfer_ctx_t *ctx, int handler_retval; int result = _anjay_coap_common_recv_msg_with_timeout( - ctx->coap_ctx, ctx->socket, ctx->in, &recv_timeout, - block_recv, &block_recv_data, &handler_retval); + ctx->coap_ctx, ctx->socket, ctx->in, &recv_timeout, block_recv, + &block_recv_data, &handler_retval); if (result == AVS_COAP_CTX_ERR_TIMEOUT) { ctx->timed_out = true; @@ -193,9 +196,11 @@ static int accept_response_with_timeout(coap_block_transfer_ctx_t *ctx, static int send_block_msg(coap_block_transfer_ctx_t *ctx, const avs_coap_msg_t *msg) { - coap_log(TRACE, "sending block %" PRIu32 " (size %" PRIu16 ", payload size " - "%lu), has_more=%d\n", ctx->block.seq_num, ctx->block.size, - (unsigned long)avs_coap_msg_payload_length(msg), + coap_log(TRACE, + "sending block %" PRIu32 " (size %" PRIu16 ", payload size " + "%lu), has_more=%d\n", + ctx->block.seq_num, ctx->block.size, + (unsigned long) avs_coap_msg_payload_length(msg), ctx->block.has_more); avs_coap_tx_params_t tx_params = avs_coap_ctx_get_tx_params(ctx->coap_ctx); @@ -255,9 +260,9 @@ static int prepare_block(coap_block_transfer_ctx_t *ctx, return result; } - *out_msg = avs_coap_block_builder_build(&ctx->block_builder, &ctx->info, - ctx->block.size, - buffer, buffer_size); + *out_msg = + avs_coap_block_builder_build(&ctx->block_builder, &ctx->info, + ctx->block.size, buffer, buffer_size); return 0; } @@ -337,8 +342,8 @@ static int get_block_packet_total_size(avs_coap_msg_info_t *info, static int flush_blocks(coap_block_transfer_ctx_t *ctx, final_block_action_t final_block_action) { size_t storage_size; - int result = get_block_packet_total_size(&ctx->info, &ctx->block, - &storage_size); + int result = + get_block_packet_total_size(&ctx->info, &ctx->block, &storage_size); if (result) { return result; } @@ -348,9 +353,9 @@ static int flush_blocks(coap_block_transfer_ctx_t *ctx, coap_log(ERROR, "out of memory"); return -1; } - result = flush_blocks_with_buffer( - ctx, avs_coap_ensure_aligned_buffer(storage), - storage_size, final_block_action); + result = flush_blocks_with_buffer(ctx, + avs_coap_ensure_aligned_buffer(storage), + storage_size, final_block_action); avs_free(storage); return result; } @@ -363,7 +368,7 @@ int _anjay_coap_block_transfer_write(coap_block_transfer_ctx_t *ctx, while (!ctx->timed_out) { bytes_written += avs_coap_block_builder_append_payload( &ctx->block_builder, - (const uint8_t*)data + bytes_written, + (const uint8_t *) data + bytes_written, data_length - bytes_written); if (bytes_written >= data_length) { diff --git a/src/coap/block/transfer.h b/src/coap/block/transfer.h index 8438cc2d3..74eda20c5 100644 --- a/src/coap/block/transfer.h +++ b/src/coap/block/transfer.h @@ -37,9 +37,9 @@ int _anjay_coap_block_transfer_finish(coap_block_transfer_ctx_t *ctx); #else -#define _anjay_coap_block_transfer_delete(ctx) ((void) 0) +# define _anjay_coap_block_transfer_delete(ctx) ((void) 0) -#define _anjay_coap_block_transfer_finish(ctx) \ +# define _anjay_coap_block_transfer_finish(ctx) \ (AVS_UNREACHABLE("should never happen"), -1) #endif diff --git a/src/coap/block/transfer_impl.h b/src/coap/block/transfer_impl.h index 3eb149a32..f9e3d2111 100644 --- a/src/coap/block/transfer_impl.h +++ b/src/coap/block/transfer_impl.h @@ -34,7 +34,7 @@ VISIBILITY_PRIVATE_HEADER_BEGIN -#define BLOCK_TRANSFER_RESULT_OK 0 +#define BLOCK_TRANSFER_RESULT_OK 0 #define BLOCK_TRANSFER_RESULT_RETRY 1 /** @@ -61,7 +61,7 @@ VISIBILITY_PRIVATE_HEADER_BEGIN * continuation, * @li BLOCK_TRANSFER_RESULT_RETRY after receiving retransmission of the * last correct message, -* @li a negative value in case of error. + * @li a negative value in case of error. * NOTE: returning a negative value from this handler is NOT equivalent * to aborting the transfer. For that, use @p out_wait_for_next . */ diff --git a/src/coap/coap_stream.h b/src/coap/coap_stream.h index cbe7d6152..eab3e9a07 100644 --- a/src/coap/coap_stream.h +++ b/src/coap/coap_stream.h @@ -17,9 +17,9 @@ #ifndef ANJAY_COAP_STREAM_H #define ANJAY_COAP_STREAM_H -#include #include #include +#include #include "../utils_core.h" @@ -45,9 +45,11 @@ typedef struct anjay_msg_details { uint8_t msg_code; uint16_t format; bool observe_serial; - AVS_LIST(const anjay_string_t) uri_path; // target URI path + /* target URI path */ + AVS_LIST(const anjay_string_t) uri_path; AVS_LIST(const anjay_string_t) uri_query; - AVS_LIST(const anjay_string_t) location_path; // path of the resource created using Create RPC + /* path of the resource created using Create RPC */ + AVS_LIST(const anjay_string_t) location_path; } anjay_msg_details_t; typedef int @@ -64,20 +66,17 @@ typedef struct anjay_coap_stream_ext { int _anjay_coap_stream_get_tx_params(avs_stream_abstract_t *stream, avs_coap_tx_params_t *out_tx_params); -int _anjay_coap_stream_set_tx_params( - avs_stream_abstract_t *stream, - const avs_coap_tx_params_t *tx_params); +int _anjay_coap_stream_set_tx_params(avs_stream_abstract_t *stream, + const avs_coap_tx_params_t *tx_params); int _anjay_coap_stream_setup_response(avs_stream_abstract_t *stream, const anjay_msg_details_t *details); -int _anjay_coap_stream_setup_request( - avs_stream_abstract_t *stream, - const anjay_msg_details_t *details, - const avs_coap_token_t *token); +int _anjay_coap_stream_setup_request(avs_stream_abstract_t *stream, + const anjay_msg_details_t *details, + const avs_coap_token_t *token); -int _anjay_coap_stream_set_error(avs_stream_abstract_t *stream, - uint8_t code); +int _anjay_coap_stream_set_error(avs_stream_abstract_t *stream, uint8_t code); /** NOTE: Pointer acquired with this function is only valid until receiving next * CoAP packet. Note that this might mean invalidation during the same stream @@ -86,8 +85,7 @@ int _anjay_coap_stream_get_incoming_msg(avs_stream_abstract_t *stream, const avs_coap_msg_t **out_msg); int _anjay_coap_stream_get_request_identity( - avs_stream_abstract_t *stream, - avs_coap_msg_identity_t *out_identity); + avs_stream_abstract_t *stream, avs_coap_msg_identity_t *out_identity); void _anjay_coap_stream_set_block_request_validator( avs_stream_abstract_t *stream, diff --git a/src/coap/content_format.h b/src/coap/content_format.h index 3748440e0..53738c6cc 100644 --- a/src/coap/content_format.h +++ b/src/coap/content_format.h @@ -32,10 +32,10 @@ VISIBILITY_PRIVATE_HEADER_BEGIN #define ANJAY_COAP_FORMAT_JSON 11543 #ifdef WITH_LEGACY_CONTENT_FORMAT_SUPPORT -#define ANJAY_COAP_FORMAT_LEGACY_PLAINTEXT 1541 -#define ANJAY_COAP_FORMAT_LEGACY_TLV 1542 -#define ANJAY_COAP_FORMAT_LEGACY_JSON 1543 -#define ANJAY_COAP_FORMAT_LEGACY_OPAQUE 1544 +# define ANJAY_COAP_FORMAT_LEGACY_PLAINTEXT 1541 +# define ANJAY_COAP_FORMAT_LEGACY_TLV 1542 +# define ANJAY_COAP_FORMAT_LEGACY_JSON 1543 +# define ANJAY_COAP_FORMAT_LEGACY_OPAQUE 1544 #endif // WITH_LEGACY_CONTENT_FORMAT_SUPPORT VISIBILITY_PRIVATE_HEADER_END diff --git a/src/coap/id_source/auto.c b/src/coap/id_source/auto.c index 82c653511..d7ad2c620 100644 --- a/src/coap/id_source/auto.c +++ b/src/coap/id_source/auto.c @@ -32,7 +32,7 @@ typedef struct coap_default_id_src { } coap_default_id_src_t; static avs_coap_msg_identity_t id_src_seq_get(coap_id_source_t *self_) { - coap_default_id_src_t *self = (coap_default_id_src_t *)self_; + coap_default_id_src_t *self = (coap_default_id_src_t *) self_; avs_coap_msg_identity_t id = { .msg_id = self->next_msg_id, @@ -49,20 +49,20 @@ static avs_coap_msg_identity_t id_src_seq_get(coap_id_source_t *self_) { } static const coap_id_source_vt_t *const ID_SRC_SEQ_VTABLE = - &(coap_id_source_vt_t){ - .get = id_src_seq_get - }; + &(coap_id_source_vt_t) { + .get = id_src_seq_get + }; coap_id_source_t *_anjay_coap_id_source_auto_new(anjay_rand_seed_t rand_seed, size_t token_size) { assert(token_size <= AVS_COAP_MAX_TOKEN_LENGTH); - coap_default_id_src_t *src = (coap_default_id_src_t *) - avs_malloc(sizeof(coap_default_id_src_t)); + coap_default_id_src_t *src = + (coap_default_id_src_t *) avs_malloc(sizeof(coap_default_id_src_t)); if (!src) { return NULL; } - memcpy((void*)(intptr_t)&src->base.vtable, &ID_SRC_SEQ_VTABLE, + memcpy((void *) (intptr_t) &src->base.vtable, &ID_SRC_SEQ_VTABLE, sizeof(ID_SRC_SEQ_VTABLE)); src->rand_seed = rand_seed; src->next_msg_id = (uint16_t) _anjay_rand32(&src->rand_seed); diff --git a/src/coap/id_source/auto.h b/src/coap/id_source/auto.h index 542f72fbc..f34271eee 100644 --- a/src/coap/id_source/auto.h +++ b/src/coap/id_source/auto.h @@ -17,8 +17,8 @@ #ifndef ANJAY_COAP_IDSOURCE_AUTO_H #define ANJAY_COAP_IDSOURCE_AUTO_H -#include "id_source.h" #include "../../utils_core.h" +#include "id_source.h" VISIBILITY_PRIVATE_HEADER_BEGIN diff --git a/src/coap/id_source/id_source.h b/src/coap/id_source/id_source.h index 2bc75845e..97bee8812 100644 --- a/src/coap/id_source/id_source.h +++ b/src/coap/id_source/id_source.h @@ -36,8 +36,7 @@ struct coap_id_source { const coap_id_source_vt_t *const vtable; }; -static inline void -_anjay_coap_id_source_release(coap_id_source_t **src) { +static inline void _anjay_coap_id_source_release(coap_id_source_t **src) { if (src && *src) { avs_free(*src); *src = NULL; diff --git a/src/coap/id_source/static.c b/src/coap/id_source/static.c index 70c0971b6..ce1ca3ada 100644 --- a/src/coap/id_source/static.c +++ b/src/coap/id_source/static.c @@ -29,37 +29,35 @@ typedef struct coap_static_id_src { avs_coap_msg_identity_t id; } coap_static_id_src_t; -static avs_coap_msg_identity_t -id_src_static_get(coap_id_source_t *self_) { - coap_static_id_src_t *self = (coap_static_id_src_t *)self_; +static avs_coap_msg_identity_t id_src_static_get(coap_id_source_t *self_) { + coap_static_id_src_t *self = (coap_static_id_src_t *) self_; return self->id; } static const coap_id_source_vt_t *const ID_SRC_STATIC_VTABLE = - &(coap_id_source_vt_t){ - .get = id_src_static_get - }; + &(coap_id_source_vt_t) { + .get = id_src_static_get + }; coap_id_source_t * _anjay_coap_id_source_new_static(const avs_coap_msg_identity_t *id) { - coap_static_id_src_t *src = (coap_static_id_src_t *) - avs_malloc(sizeof(coap_static_id_src_t)); + coap_static_id_src_t *src = + (coap_static_id_src_t *) avs_malloc(sizeof(coap_static_id_src_t)); if (!src) { return NULL; } - memcpy((void*)(intptr_t)&src->base.vtable, &ID_SRC_STATIC_VTABLE, + memcpy((void *) (intptr_t) &src->base.vtable, &ID_SRC_STATIC_VTABLE, sizeof(ID_SRC_STATIC_VTABLE)); src->id = *id; return &src->base; } -void -_anjay_coap_id_source_static_reset(coap_id_source_t *self_, - const avs_coap_msg_identity_t *new_id) { +void _anjay_coap_id_source_static_reset(coap_id_source_t *self_, + const avs_coap_msg_identity_t *new_id) { assert(self_->vtable == ID_SRC_STATIC_VTABLE); - coap_static_id_src_t *self = (coap_static_id_src_t *)self_; + coap_static_id_src_t *self = (coap_static_id_src_t *) self_; self->id = *new_id; } diff --git a/src/coap/id_source/static.h b/src/coap/id_source/static.h index ef02962e8..0eec674c4 100644 --- a/src/coap/id_source/static.h +++ b/src/coap/id_source/static.h @@ -24,9 +24,8 @@ VISIBILITY_PRIVATE_HEADER_BEGIN coap_id_source_t * _anjay_coap_id_source_new_static(const avs_coap_msg_identity_t *id); -void -_anjay_coap_id_source_static_reset(coap_id_source_t *self_, - const avs_coap_msg_identity_t *new_id); +void _anjay_coap_id_source_static_reset(coap_id_source_t *self_, + const avs_coap_msg_identity_t *new_id); VISIBILITY_PRIVATE_HEADER_END diff --git a/src/coap/stream/client_internal.c b/src/coap/stream/client_internal.c index 3c06b9a15..27651f94e 100644 --- a/src/coap/stream/client_internal.c +++ b/src/coap/stream/client_internal.c @@ -22,11 +22,11 @@ #include -#include #include +#include -#include "../coap_log.h" #include "../block/request.h" +#include "../coap_log.h" #include "common.h" #include @@ -34,9 +34,9 @@ VISIBILITY_SOURCE_BEGIN #ifdef WITH_BLOCK_SEND -#define has_block_ctx(client) ((client)->block_ctx) +# define has_block_ctx(client) ((client)->block_ctx) #else -#define has_block_ctx(client) (false) +# define has_block_ctx(client) (false) #endif const avs_coap_msg_identity_t * @@ -69,8 +69,8 @@ int _anjay_coap_client_setup_request(coap_client_t *client, assert(_anjay_coap_out_is_reset(&client->common.out)); _anjay_coap_out_setup_mtu(&client->common.out, client->common.socket); - int result = _anjay_coap_out_setup_msg(&client->common.out, - identity, details, NULL); + int result = _anjay_coap_out_setup_msg(&client->common.out, identity, + details, NULL); if (result) { _anjay_coap_client_reset(client); _anjay_coap_out_reset(&client->common.out); @@ -115,7 +115,7 @@ req_sent_process_response(coap_client_t *client, client->state = COAP_CLIENT_STATE_HAS_SEPARATE_ACK; return CHECK_OK; } else if (!avs_coap_msg_token_matches( - response, &client->last_request_identity)) { + response, &client->last_request_identity)) { coap_log(DEBUG, "invalid response: token mismatch"); return CHECK_INVALID_RESPONSE; } @@ -133,7 +133,7 @@ static check_result_t process_separate_response(coap_client_t *client, const avs_coap_msg_t *response) { assert(client->state == COAP_CLIENT_STATE_REQUEST_SENT - || client->state == COAP_CLIENT_STATE_HAS_SEPARATE_ACK); + || client->state == COAP_CLIENT_STATE_HAS_SEPARATE_ACK); avs_coap_msg_type_t type = avs_coap_msg_get_type(response); @@ -157,19 +157,18 @@ process_separate_response(coap_client_t *client, static check_result_t check_response(coap_client_t *client, const avs_coap_msg_t *response) { assert(client->state == COAP_CLIENT_STATE_REQUEST_SENT - || client->state == COAP_CLIENT_STATE_HAS_SEPARATE_ACK); + || client->state == COAP_CLIENT_STATE_HAS_SEPARATE_ACK); switch (client->state) { - case COAP_CLIENT_STATE_REQUEST_SENT: - { - uint16_t msg_id = avs_coap_msg_get_id(response); - if (msg_id != client->last_request_identity.msg_id) { - // this may still be a Separate Response if Separate ACK - // got lost - return process_separate_response(client, response); - } + case COAP_CLIENT_STATE_REQUEST_SENT: { + uint16_t msg_id = avs_coap_msg_get_id(response); + if (msg_id != client->last_request_identity.msg_id) { + // this may still be a Separate Response if Separate ACK + // got lost + return process_separate_response(client, response); } return req_sent_process_response(client, response); + } case COAP_CLIENT_STATE_HAS_SEPARATE_ACK: return process_separate_response(client, response); @@ -188,7 +187,7 @@ static int process_received(const avs_coap_msg_t *response, (void) out_error_code; assert(response); - coap_client_t *client = (coap_client_t *)client_; + coap_client_t *client = (coap_client_t *) client_; *out_wait_for_next = true; check_result_t result = check_response(client, response); @@ -211,13 +210,13 @@ static int process_received(const avs_coap_msg_t *response, AVS_UNREACHABLE("invalid enum value"); } - return (int)result; + return (int) result; } static int accept_response_with_timeout(coap_client_t *client, avs_time_duration_t timeout) { assert(client->state == COAP_CLIENT_STATE_REQUEST_SENT - || client->state == COAP_CLIENT_STATE_HAS_SEPARATE_ACK); + || client->state == COAP_CLIENT_STATE_HAS_SEPARATE_ACK); int recv_result = -1; int result = _anjay_coap_common_recv_msg_with_timeout( @@ -228,26 +227,25 @@ static int accept_response_with_timeout(coap_client_t *client, } assert(client->state == COAP_CLIENT_STATE_REQUEST_SENT - || client->state == COAP_CLIENT_STATE_HAS_SEPARATE_ACK - || client->state == COAP_CLIENT_STATE_HAS_RESPONSE_CONTENT); + || client->state == COAP_CLIENT_STATE_HAS_SEPARATE_ACK + || client->state == COAP_CLIENT_STATE_HAS_RESPONSE_CONTENT); assert(recv_result >= _CHECK_FIRST && recv_result <= _CHECK_LAST); switch (recv_result) { case CHECK_RESET: return COAP_CLIENT_RECEIVE_RESET; - case CHECK_NEEDS_ACK: - { - coap_log(TRACE, "Separate response received; sending ACK"); - - const avs_coap_msg_t *msg = - _anjay_coap_in_get_message(&client->common.in); - avs_coap_ctx_send_empty(client->common.coap_ctx, - client->common.socket, - AVS_COAP_MSG_ACKNOWLEDGEMENT, - avs_coap_msg_get_id(msg)); - } + case CHECK_NEEDS_ACK: { + coap_log(TRACE, "Separate response received; sending ACK"); + + const avs_coap_msg_t *msg = + _anjay_coap_in_get_message(&client->common.in); + avs_coap_ctx_send_empty(client->common.coap_ctx, + client->common.socket, + AVS_COAP_MSG_ACKNOWLEDGEMENT, + avs_coap_msg_get_id(msg)); return 0; + } case CHECK_OK: return 0; @@ -296,8 +294,9 @@ static int send_confirmable_with_retry(coap_client_t *client, coap_log(DEBUG, "timeout reached, next: %" PRId64 ".09%" PRId32 " s", retry_state.recv_timeout.seconds, retry_state.recv_timeout.nanoseconds); - } while (retry_state.retry_count <= avs_coap_ctx_get_tx_params( - client->common.coap_ctx).max_retransmit); + } while (retry_state.retry_count + <= avs_coap_ctx_get_tx_params(client->common.coap_ctx) + .max_retransmit); assert(result <= 0 || result == COAP_CLIENT_RECEIVE_RESET); if (result != 0) { @@ -382,8 +381,8 @@ static int block_write(coap_client_t *client, size_t data_length) { if (!client->block_ctx) { client->block_ctx = - _anjay_coap_block_request_new(AVS_COAP_MSG_BLOCK_MAX_SIZE, - &client->common, id_source); + _anjay_coap_block_request_new(AVS_COAP_MSG_BLOCK_MAX_SIZE, + &client->common, id_source); if (!client->block_ctx) { return -1; } @@ -396,7 +395,7 @@ static int block_write(coap_client_t *client, return result; } #else -#define block_write(...) \ +# define block_write(...) \ (coap_log(ERROR, "sending blockwise requests not supported"), -1) #endif @@ -404,20 +403,22 @@ int _anjay_coap_client_write(coap_client_t *client, coap_id_source_t *id_source, const void *data, size_t data_length) { - (void) client; (void) id_source; + (void) client; + (void) id_source; size_t bytes_written = 0; if (!has_block_ctx(client)) { - bytes_written = _anjay_coap_out_write(&client->common.out, - data, data_length); + bytes_written = + _anjay_coap_out_write(&client->common.out, data, data_length); if (bytes_written == data_length) { return 0; } else { coap_log(TRACE, "response payload does not fit in the buffer " - "- initiating block-wise transfer"); + "- initiating block-wise transfer"); } } - return block_write(client, id_source, (const uint8_t*) data + bytes_written, + return block_write(client, id_source, + (const uint8_t *) data + bytes_written, data_length - bytes_written); } diff --git a/src/coap/stream/client_internal.h b/src/coap/stream/client_internal.h index a9b869f2a..88483b4c3 100644 --- a/src/coap/stream/client_internal.h +++ b/src/coap/stream/client_internal.h @@ -30,7 +30,7 @@ #include "out.h" #ifndef ANJAY_COAP_STREAM_INTERNALS -#error "Headers from coap/stream are not meant to be included from outside" +# error "Headers from coap/stream are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN @@ -85,10 +85,9 @@ _anjay_coap_client_get_request_identity(const coap_client_t *client); * - a negative value in case of error (e.g. if the @p client is not reset or * the token is invalid). */ -int -_anjay_coap_client_setup_request(coap_client_t *client, - const anjay_msg_details_t *details, - const avs_coap_msg_identity_t *identity); +int _anjay_coap_client_setup_request(coap_client_t *client, + const anjay_msg_details_t *details, + const avs_coap_msg_identity_t *identity); #define COAP_CLIENT_RECEIVE_RESET 1 diff --git a/src/coap/stream/common.c b/src/coap/stream/common.c index c3a844a5a..7cd642f75 100644 --- a/src/coap/stream/common.c +++ b/src/coap/stream/common.c @@ -22,8 +22,8 @@ #include -#include "common.h" #include "../coap_log.h" +#include "common.h" VISIBILITY_SOURCE_BEGIN @@ -46,11 +46,10 @@ uint32_t _anjay_coap_common_timestamp(void) { // 32.768 us, wrapping every 512 seconds. // Should satisfy the requirements given in OBSERVE 3.4 and 4.4 return (uint32_t) ((now.since_monotonic_epoch.seconds & 0x1FF) << 15) - | (uint32_t) (now.since_monotonic_epoch.nanoseconds >> 15); + | (uint32_t) (now.since_monotonic_epoch.nanoseconds >> 15); } -static int add_observe_option(avs_coap_msg_info_t *info, - bool observe) { +static int add_observe_option(avs_coap_msg_info_t *info, bool observe) { if (observe) { return avs_coap_msg_info_opt_u32(info, AVS_COAP_OPT_OBSERVE, _anjay_coap_common_timestamp()); diff --git a/src/coap/stream/common.h b/src/coap/stream/common.h index 8f9631004..ca5fd0d12 100644 --- a/src/coap/stream/common.h +++ b/src/coap/stream/common.h @@ -24,7 +24,7 @@ #include "out.h" #ifndef ANJAY_COAP_STREAM_INTERNALS -#error "Headers from coap/stream are not meant to be included from outside" +# error "Headers from coap/stream are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN @@ -54,8 +54,9 @@ int _anjay_coap_common_fill_msg_info(avs_coap_msg_info_t *info, * true (default), the handler will be called * again with next message * @param[out] out_error_code Allows to specify custom error code that will - * be send instead of RESET. Not setting it (or setting - * to 0) will result in usual RESET message. + * be send instead of RESET. Not setting it (or + * setting to 0) will result in usual RESET + * message. * * @return If @p out_wait_for_next is not set to true, the return value of this * function will be propagated up by the diff --git a/src/coap/stream/in.c b/src/coap/stream/in.c index ff13fe3dc..b7dddeaa3 100644 --- a/src/coap/stream/in.c +++ b/src/coap/stream/in.c @@ -20,13 +20,13 @@ #define ANJAY_COAP_STREAM_INTERNALS -#include "in.h" #include "common.h" +#include "in.h" -#include +#include #include +#include #include -#include #include "../coap_log.h" @@ -50,7 +50,7 @@ int _anjay_coap_in_get_next_message(coap_input_buffer_t *in, const avs_coap_msg_t *msg = _anjay_coap_in_get_message(in); in->payload_off = 0; - in->payload = (const uint8_t *)avs_coap_msg_payload(msg); + in->payload = (const uint8_t *) avs_coap_msg_payload(msg); in->payload_size = avs_coap_msg_payload_length(msg); return 0; @@ -69,4 +69,3 @@ void _anjay_coap_in_read(coap_input_buffer_t *in, *out_bytes_read = bytes_to_copy; *out_message_finished = (in->payload_off >= in->payload_size); } - diff --git a/src/coap/stream/in.h b/src/coap/stream/in.h index 35bd99a6e..ecf41977a 100644 --- a/src/coap/stream/in.h +++ b/src/coap/stream/in.h @@ -17,16 +17,16 @@ #ifndef SRC_COAP_STREAM_IN_H #define SRC_COAP_STREAM_IN_H -#include #include +#include #include "../../utils_core.h" -#include #include +#include #ifndef ANJAY_COAP_STREAM_INTERNALS -#error "Headers from coap/stream are not meant to be included from outside" +# error "Headers from coap/stream are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN @@ -51,7 +51,7 @@ static inline bool _anjay_coap_in_is_reset(const coap_input_buffer_t *in) { static inline const avs_coap_msg_t * _anjay_coap_in_get_message(const coap_input_buffer_t *in) { - return (const avs_coap_msg_t *)in->buffer; + return (const avs_coap_msg_t *) in->buffer; } static inline size_t @@ -62,8 +62,7 @@ _anjay_coap_in_get_bytes_available(const coap_input_buffer_t *in) { static inline size_t _anjay_coap_in_get_payload_size(const coap_input_buffer_t *in) { - return avs_coap_msg_payload_length( - _anjay_coap_in_get_message(in)); + return avs_coap_msg_payload_length(_anjay_coap_in_get_message(in)); } /** diff --git a/src/coap/stream/out.c b/src/coap/stream/out.c index 105317fbe..ab3d5d486 100644 --- a/src/coap/stream/out.c +++ b/src/coap/stream/out.c @@ -24,8 +24,8 @@ #include "out.h" #include -#include #include +#include #include "../coap_log.h" @@ -33,7 +33,7 @@ VISIBILITY_SOURCE_BEGIN coap_output_buffer_t _anjay_coap_out_init(uint8_t *buffer, size_t buffer_capacity) { - return (coap_output_buffer_t){ + return (coap_output_buffer_t) { .buffer = buffer, .buffer_capacity = buffer_capacity, .dgram_layer_mtu = buffer_capacity, @@ -101,8 +101,7 @@ int _anjay_coap_out_setup_msg(coap_output_buffer_t *out, details->location_path) || add_string_options(&out->info, AVS_COAP_OPT_URI_PATH, details->uri_path) - || avs_coap_msg_info_opt_content_format(&out->info, - details->format) + || avs_coap_msg_info_opt_content_format(&out->info, details->format) || add_string_options(&out->info, AVS_COAP_OPT_URI_QUERY, details->uri_query) || (block && avs_coap_msg_info_opt_block(&out->info, block))) { diff --git a/src/coap/stream/out.h b/src/coap/stream/out.h index bc96716c1..0478627ba 100644 --- a/src/coap/stream/out.h +++ b/src/coap/stream/out.h @@ -17,29 +17,29 @@ #ifndef SRC_COAP_STREAM_OUT_H #define SRC_COAP_STREAM_OUT_H -#include #include +#include #include +#include #include #include -#include #include "../coap_stream.h" #ifndef ANJAY_COAP_STREAM_INTERNALS -#error "Headers from coap/stream are not meant to be included from outside" +# error "Headers from coap/stream are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN typedef enum msg_state { - MSG_STATE_RESET = 0, - MSG_STATE_HAS_DETAILS = (1 << 0), - MSG_STATE_HAS_ID = (1 << 1), - MSG_STATE_HAS_TOKEN = (1 << 2), + MSG_STATE_RESET = 0, + MSG_STATE_HAS_DETAILS = (1 << 0), + MSG_STATE_HAS_ID = (1 << 1), + MSG_STATE_HAS_TOKEN = (1 << 2), MSG_STATE_HAS_IDENTITY = MSG_STATE_HAS_ID | MSG_STATE_HAS_TOKEN, - MSG_STATE_FINISHED = (1 << 3), + MSG_STATE_FINISHED = (1 << 3), _MSG_STATE_MASK = (1 << 4) - 1 } msg_state_t; diff --git a/src/coap/stream/server_internal.c b/src/coap/stream/server_internal.c index ab4eb94a5..c0d0514b4 100644 --- a/src/coap/stream/server_internal.c +++ b/src/coap/stream/server_internal.c @@ -36,9 +36,9 @@ VISIBILITY_SOURCE_BEGIN #ifdef WITH_BLOCK_SEND -#define has_block_ctx(server) ((server)->block_ctx) +# define has_block_ctx(server) ((server)->block_ctx) #else -#define has_block_ctx(server) (false) +# define has_block_ctx(server) (false) #endif static inline bool has_error(coap_server_t *server) { @@ -85,7 +85,7 @@ _anjay_coap_server_get_request_identity(const coap_server_t *server) { static bool is_block1_transfer(coap_server_t *server) { return server->state == COAP_SERVER_STATE_HAS_BLOCK1_REQUEST - || server->state == COAP_SERVER_STATE_NEEDS_NEXT_BLOCK; + || server->state == COAP_SERVER_STATE_NEEDS_NEXT_BLOCK; } static bool is_success_response(uint8_t msg_code) { @@ -99,8 +99,7 @@ int _anjay_coap_server_setup_response(coap_server_t *server, return -1; } - AVS_ASSERT(!has_error(server), - "setup_response called with unsent error"); + AVS_ASSERT(!has_error(server), "setup_response called with unsent error"); clear_error(server); if (!_anjay_coap_out_is_reset(&server->common.out)) { @@ -109,8 +108,7 @@ int _anjay_coap_server_setup_response(coap_server_t *server, } const avs_coap_block_info_t *block = NULL; - if (is_block1_transfer(server) - && is_success_response(details->msg_code)) { + if (is_block1_transfer(server) && is_success_response(details->msg_code)) { block = &server->curr_block; } @@ -144,7 +142,7 @@ static void setup_error_response(coap_server_t *server) { int result = _anjay_coap_server_setup_response(server, &details); assert(result == 0); - (void)result; + (void) result; } int _anjay_coap_server_finish_response(coap_server_t *server) { @@ -163,9 +161,9 @@ int _anjay_coap_server_finish_response(coap_server_t *server) { int result = 0; if (is_block1_transfer(server)) { - result = _anjay_coap_out_update_msg_header( - &server->common.out, - &server->request_identity, &server->curr_block); + result = _anjay_coap_out_update_msg_header(&server->common.out, + &server->request_identity, + &server->curr_block); } if (!result) { @@ -188,7 +186,8 @@ static int block_store_critical_options(AVS_LIST(coap_block_optbuf_t) *out, assert(!*outptr); for (avs_coap_opt_iterator_t optit = avs_coap_opt_begin(msg); - !avs_coap_opt_end(&optit); avs_coap_opt_next(&optit)) { + !avs_coap_opt_end(&optit); + avs_coap_opt_next(&optit)) { uint32_t optnum = avs_coap_opt_number(&optit); if (optnum == optnum_to_ignore || !is_opt_critical(optnum)) { continue; @@ -201,8 +200,7 @@ static int block_store_critical_options(AVS_LIST(coap_block_optbuf_t) *out, } (*outptr)->optnum = optnum; (*outptr)->length = length; - memcpy((*outptr)->content, avs_coap_opt_value(optit.curr_opt), - length); + memcpy((*outptr)->content, avs_coap_opt_value(optit.curr_opt), length); AVS_LIST_ADVANCE_PTR(&outptr); } return 0; @@ -250,7 +248,8 @@ static process_result_t process_initial_request(coap_server_t *server, } /** * CoAP supports bidirectional block communication, but LwM2M does not have - * any operation for which it would be useful. Therefore it's not implemented. + * any operation for which it would be useful. Therefore it's not + * implemented. */ if (block1.valid && block2.valid) { _anjay_coap_server_set_error(server, -ANJAY_ERR_BAD_OPTION); @@ -291,9 +290,8 @@ static process_result_t process_initial_request(coap_server_t *server, } static int receive_request(coap_server_t *server) { - int result = _anjay_coap_in_get_next_message(&server->common.in, - server->common.coap_ctx, - server->common.socket); + int result = _anjay_coap_in_get_next_message( + &server->common.in, server->common.coap_ctx, server->common.socket); if (result == AVS_COAP_CTX_ERR_MSG_TOO_LONG) { const avs_coap_msg_t *partial_msg = (avs_coap_msg_t *) server->common.in.buffer; @@ -322,8 +320,8 @@ static int receive_request(coap_server_t *server) { } } else { avs_coap_ctx_send_error(server->common.coap_ctx, - server->common.socket, - msg, server->last_error_code); + server->common.socket, msg, + server->last_error_code); } return -1; case PROCESS_INITIAL_OK: @@ -355,19 +353,20 @@ static bool blocks_equal(const avs_coap_block_info_t *a, assert(a->valid); assert(b->valid); - return a->size == b->size - && a->has_more == b->has_more - && a->seq_num == b->seq_num; + return a->size == b->size && a->has_more == b->has_more + && a->seq_num == b->seq_num; } static int block_validate_critical_options(AVS_LIST(coap_block_optbuf_t) opts, const avs_coap_msg_t *msg, uint32_t optnum_to_ignore) { -#define BVCO_LOG_MSG "critical options mismatch when receiving BLOCK request; " -#define BVCO_LOG_OPT "%" PRIu32 " length %" PRIu32 +# define BVCO_LOG_MSG \ + "critical options mismatch when receiving BLOCK request; " +# define BVCO_LOG_OPT "%" PRIu32 " length %" PRIu32 AVS_LIST(coap_block_optbuf_t) optbuf = opts; for (avs_coap_opt_iterator_t optit = avs_coap_opt_begin(msg); - !avs_coap_opt_end(&optit); avs_coap_opt_next(&optit)) { + !avs_coap_opt_end(&optit); + avs_coap_opt_next(&optit)) { uint32_t optnum = avs_coap_opt_number(&optit); if (optnum == optnum_to_ignore || !is_opt_critical(optnum)) { continue; @@ -378,12 +377,13 @@ static int block_validate_critical_options(AVS_LIST(coap_block_optbuf_t) opts, optnum, length); return -1; } - if (optnum != optbuf->optnum - || length != optbuf->length - || memcmp(avs_coap_opt_value(optit.curr_opt), - optbuf->content, optbuf->length) != 0) { - anjay_log(DEBUG, BVCO_LOG_MSG - "expected " BVCO_LOG_OPT "; got " BVCO_LOG_OPT, + if (optnum != optbuf->optnum || length != optbuf->length + || memcmp(avs_coap_opt_value(optit.curr_opt), optbuf->content, + optbuf->length) + != 0) { + anjay_log(DEBUG, + BVCO_LOG_MSG "expected " BVCO_LOG_OPT + "; got " BVCO_LOG_OPT, optbuf->optnum, optbuf->length, optnum, length); return -1; } @@ -395,8 +395,8 @@ static int block_validate_critical_options(AVS_LIST(coap_block_optbuf_t) opts, return -1; } return 0; -#undef BVCO_LOG_OPT -#undef BVCO_LOG_MSG +# undef BVCO_LOG_OPT +# undef BVCO_LOG_MSG } typedef enum process_block_result { @@ -514,8 +514,7 @@ static int send_continue(coap_server_t *server, } msg = avs_coap_msg_build_without_payload( - avs_coap_ensure_aligned_buffer(storage), - storage_size, &info); + avs_coap_ensure_aligned_buffer(storage), storage_size, &info); if (msg) { result = avs_coap_ctx_send(server->common.coap_ctx, server->common.socket, msg); @@ -533,13 +532,13 @@ static int receive_next_block(const avs_coap_msg_t *msg, uint8_t *out_error_code) { assert(msg); - coap_server_t *server = (coap_server_t *)server_; + coap_server_t *server = (coap_server_t *) server_; assert(server->state == COAP_SERVER_STATE_NEEDS_NEXT_BLOCK); assert(server->curr_block.valid); - process_block_result_t result = process_next_block(server, msg, - out_error_code); + process_block_result_t result = + process_next_block(server, msg, out_error_code); switch (result) { case PROCESS_BLOCK_REJECT_CONTINUE: @@ -553,7 +552,7 @@ static int receive_next_block(const avs_coap_msg_t *msg, break; } - return (int)result; + return (int) result; } static int receive_next_block_with_timeout(coap_server_t *server) { @@ -594,8 +593,9 @@ static int receive_next_block_with_timeout(coap_server_t *server) { return recv_result; } } - coap_log(DEBUG, "timeout reached while waiting for block (offset = %" - PRIu32 ")", get_block_offset(&server->curr_block)); + coap_log(DEBUG, + "timeout reached while waiting for block (offset = %" PRIu32 ")", + get_block_offset(&server->curr_block)); return -1; } #endif // WITH_BLOCK_RECEIVE @@ -655,13 +655,12 @@ int _anjay_coap_server_read(coap_server_t *server, } #ifdef WITH_BLOCK_SEND -static int block_write(coap_server_t *server, - const void *data, - size_t data_length) { +static int +block_write(coap_server_t *server, const void *data, size_t data_length) { if (!server->block_ctx) { uint16_t block_size = server->curr_block.valid - ? server->curr_block.size - : AVS_COAP_MSG_BLOCK_MAX_SIZE; + ? server->curr_block.size + : AVS_COAP_MSG_BLOCK_MAX_SIZE; server->static_id_source = _anjay_coap_id_source_new_static( _anjay_coap_server_get_request_identity(server)); @@ -676,7 +675,6 @@ static int block_write(coap_server_t *server, _anjay_coap_id_source_release(&server->static_id_source); return -1; } - } int result = _anjay_coap_block_transfer_write(server->block_ctx, data, data_length); @@ -689,13 +687,13 @@ static int block_write(coap_server_t *server, return result; } #else -#define block_write(...) \ +# define block_write(...) \ (coap_log(ERROR, "sending blockwise responses not supported"), -1) #endif static bool block_response_requested(coap_server_t *server) { return server->curr_block.valid - && server->curr_block.type == AVS_COAP_BLOCK2; + && server->curr_block.type == AVS_COAP_BLOCK2; } int _anjay_coap_server_write(coap_server_t *server, @@ -703,16 +701,16 @@ int _anjay_coap_server_write(coap_server_t *server, size_t data_length) { size_t bytes_written = 0; if (!has_block_ctx(server) && !block_response_requested(server)) { - bytes_written = _anjay_coap_out_write(&server->common.out, - data, data_length); + bytes_written = + _anjay_coap_out_write(&server->common.out, data, data_length); if (bytes_written == data_length) { return 0; } else { coap_log(TRACE, "response payload does not fit in the buffer " - "- initiating block-wise transfer"); + "- initiating block-wise transfer"); } } - return block_write(server, (const uint8_t*) data + bytes_written, + return block_write(server, (const uint8_t *) data + bytes_written, data_length - bytes_written); } diff --git a/src/coap/stream/server_internal.h b/src/coap/stream/server_internal.h index 837de4150..6867e96f7 100644 --- a/src/coap/stream/server_internal.h +++ b/src/coap/stream/server_internal.h @@ -17,18 +17,18 @@ #ifndef ANJAY_COAP_STREAM_SERVER_H #define ANJAY_COAP_STREAM_SERVER_H -#include #include +#include -#include "../coap_stream.h" #include "../block/response.h" +#include "../coap_stream.h" #include "../id_source/id_source.h" #include "common.h" #include "in.h" #include "out.h" #ifndef ANJAY_COAP_STREAM_INTERNALS -#error "Headers from coap/stream are not meant to be included from outside" +# error "Headers from coap/stream are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN @@ -89,8 +89,8 @@ void _anjay_coap_server_set_block_request_relation_validator( anjay_coap_block_request_validator_t *validator, void *validator_arg); #else // WITH_BLOCK_SEND -#define _anjay_coap_server_set_block_request_relation_validator( \ - Server, Validator, Arg) \ +# define _anjay_coap_server_set_block_request_relation_validator( \ + Server, Validator, Arg) \ ((void) (Server), (void) (Validator), (void) (Arg)) #endif // WITH_BLOCK_SEND diff --git a/src/coap/stream/stream_internal.c b/src/coap/stream/stream_internal.c index 5d656cbd6..a51fc53d4 100644 --- a/src/coap/stream/stream_internal.c +++ b/src/coap/stream/stream_internal.c @@ -23,8 +23,8 @@ #include #include -#include "../content_format.h" #include "../coap_log.h" +#include "../content_format.h" #include "../id_source/auto.h" @@ -122,7 +122,7 @@ static int get_or_receive_msg(coap_stream_t *stream, static int setup_response(avs_stream_abstract_t *stream_, const anjay_msg_details_t *details) { - coap_stream_t *stream = (coap_stream_t*)stream_; + coap_stream_t *stream = (coap_stream_t *) stream_; if (stream->state != STREAM_STATE_SERVER) { coap_log(ERROR, "no request to respond to"); @@ -143,14 +143,14 @@ static const anjay_coap_stream_ext_t COAP_STREAM_EXT_VTABLE = { static int coap_getsock(avs_stream_abstract_t *stream_, avs_net_abstract_socket_t **out_sock) { - coap_stream_t *stream = (coap_stream_t*)stream_; + coap_stream_t *stream = (coap_stream_t *) stream_; *out_sock = stream->data.common.socket; return *out_sock == NULL ? -1 : 0; } static int coap_setsock(avs_stream_abstract_t *stream_, avs_net_abstract_socket_t *sock) { - coap_stream_t *stream = (coap_stream_t*)stream_; + coap_stream_t *stream = (coap_stream_t *) stream_; if (!is_reset(stream)) { return -1; } @@ -165,8 +165,7 @@ static int coap_setsock(avs_stream_abstract_t *stream_, } static const avs_stream_v_table_extension_net_t NET_EXT_VTABLE = { - coap_getsock, - coap_setsock + coap_getsock, coap_setsock }; static const avs_stream_v_table_extension_t COAP_STREAM_EXT[] = { @@ -178,12 +177,12 @@ static const avs_stream_v_table_extension_t COAP_STREAM_EXT[] = { static int coap_write(avs_stream_abstract_t *stream_, const void *data, size_t *data_length) { - coap_stream_t *stream = (coap_stream_t *)stream_; + coap_stream_t *stream = (coap_stream_t *) stream_; switch (stream->state) { case STREAM_STATE_CLIENT: - return _anjay_coap_client_write(get_client(stream), - stream->id_source, data, *data_length); + return _anjay_coap_client_write(get_client(stream), stream->id_source, + data, *data_length); case STREAM_STATE_SERVER: return _anjay_coap_server_write(get_server(stream), data, *data_length); default: @@ -193,11 +192,11 @@ static int coap_write(avs_stream_abstract_t *stream_, } static int coap_finish_message(avs_stream_abstract_t *stream_) { - coap_stream_t *stream = (coap_stream_t*)stream_; + coap_stream_t *stream = (coap_stream_t *) stream_; switch (stream->state) { case STREAM_STATE_CLIENT: - return _anjay_coap_client_finish_request(get_client(stream)); + return _anjay_coap_client_finish_request(get_client(stream)); case STREAM_STATE_SERVER: return _anjay_coap_server_finish_response(get_server(stream)); @@ -213,7 +212,7 @@ static int coap_read(avs_stream_abstract_t *stream_, char *out_message_finished, void *buffer, size_t buffer_length) { - coap_stream_t *stream = (coap_stream_t *)stream_; + coap_stream_t *stream = (coap_stream_t *) stream_; assert(stream->data.common.in.buffer); const avs_coap_msg_t *msg; @@ -228,13 +227,13 @@ static int coap_read(avs_stream_abstract_t *stream_, break; case STREAM_STATE_SERVER: result = _anjay_coap_server_read(get_server(stream), out_bytes_read, - out_message_finished, - buffer, buffer_length); + out_message_finished, buffer, + buffer_length); break; case STREAM_STATE_CLIENT: result = _anjay_coap_client_read(get_client(stream), out_bytes_read, - out_message_finished, - buffer, buffer_length); + out_message_finished, buffer, + buffer_length); break; } @@ -246,12 +245,12 @@ static int coap_read(avs_stream_abstract_t *stream_, } static int coap_reset(avs_stream_abstract_t *stream_) { - reset((coap_stream_t *)stream_); + reset((coap_stream_t *) stream_); return 0; } static int coap_close(avs_stream_abstract_t *stream_) { - coap_stream_t *stream = (coap_stream_t *)stream_; + coap_stream_t *stream = (coap_stream_t *) stream_; reset(stream); @@ -292,7 +291,8 @@ int _anjay_coap_stream_create(avs_stream_abstract_t **stream_, size_t in_buffer_size, uint8_t *out_buffer, size_t out_buffer_size) { - coap_stream_t *stream = (coap_stream_t *)avs_calloc(1, sizeof(coap_stream_t)); + coap_stream_t *stream = + (coap_stream_t *) avs_calloc(1, sizeof(coap_stream_t)); if (!stream) { coap_log(ERROR, "Out of memory"); return -1; @@ -314,8 +314,7 @@ int _anjay_coap_stream_create(avs_stream_abstract_t **stream_, (anjay_rand_seed_t) avs_time_real_now().since_real_epoch.seconds, 8); - if (!stream->data.common.in.buffer - || !stream->data.common.out.buffer + if (!stream->data.common.in.buffer || !stream->data.common.out.buffer || !stream->id_source) { coap_log(ERROR, "Out of memory"); coap_close((avs_stream_abstract_t *) stream); @@ -324,23 +323,21 @@ int _anjay_coap_stream_create(avs_stream_abstract_t **stream_, } reset(stream); - *stream_ = (avs_stream_abstract_t *)stream; + *stream_ = (avs_stream_abstract_t *) stream; return 0; } -int _anjay_coap_stream_get_tx_params( - avs_stream_abstract_t *stream_, - avs_coap_tx_params_t *out_tx_params) { - coap_stream_t *stream = (coap_stream_t*) stream_; +int _anjay_coap_stream_get_tx_params(avs_stream_abstract_t *stream_, + avs_coap_tx_params_t *out_tx_params) { + coap_stream_t *stream = (coap_stream_t *) stream_; assert(stream->vtable == &COAP_STREAM_VTABLE); *out_tx_params = avs_coap_ctx_get_tx_params(stream->data.common.coap_ctx); return 0; } -int _anjay_coap_stream_set_tx_params( - avs_stream_abstract_t *stream_, - const avs_coap_tx_params_t *tx_params) { - coap_stream_t *stream = (coap_stream_t*) stream_; +int _anjay_coap_stream_set_tx_params(avs_stream_abstract_t *stream_, + const avs_coap_tx_params_t *tx_params) { + coap_stream_t *stream = (coap_stream_t *) stream_; assert(stream->vtable == &COAP_STREAM_VTABLE); assert(avs_coap_tx_params_valid(tx_params, NULL)); avs_coap_ctx_set_tx_params(stream->data.common.coap_ctx, tx_params); @@ -349,9 +346,9 @@ int _anjay_coap_stream_set_tx_params( int _anjay_coap_stream_setup_response(avs_stream_abstract_t *stream, const anjay_msg_details_t *details) { - const anjay_coap_stream_ext_t *coap = (const anjay_coap_stream_ext_t *) - avs_stream_v_table_find_extension(stream, - ANJAY_COAP_STREAM_EXTENSION); + const anjay_coap_stream_ext_t *coap = + (const anjay_coap_stream_ext_t *) avs_stream_v_table_find_extension( + stream, ANJAY_COAP_STREAM_EXTENSION); if (coap) { return coap->setup_response(stream, details); } @@ -359,11 +356,10 @@ int _anjay_coap_stream_setup_response(avs_stream_abstract_t *stream, return -1; } -int _anjay_coap_stream_setup_request( - avs_stream_abstract_t *stream_, - const anjay_msg_details_t *details, - const avs_coap_token_t *token) { - coap_stream_t *stream = (coap_stream_t*)stream_; +int _anjay_coap_stream_setup_request(avs_stream_abstract_t *stream_, + const anjay_msg_details_t *details, + const avs_coap_token_t *token) { + coap_stream_t *stream = (coap_stream_t *) stream_; assert(stream->vtable == &COAP_STREAM_VTABLE); switch (stream->state) { case STREAM_STATE_SERVER: @@ -388,16 +384,15 @@ int _anjay_coap_stream_setup_request( } int result; - if ((result = _anjay_coap_client_setup_request(get_client(stream), - details, &identity))) { + if ((result = _anjay_coap_client_setup_request(get_client(stream), details, + &identity))) { reset(stream); } return result; } -int _anjay_coap_stream_set_error(avs_stream_abstract_t *stream_, - uint8_t code) { - coap_stream_t *stream = (coap_stream_t*)stream_; +int _anjay_coap_stream_set_error(avs_stream_abstract_t *stream_, uint8_t code) { + coap_stream_t *stream = (coap_stream_t *) stream_; assert(stream->vtable == &COAP_STREAM_VTABLE); if (stream->state != STREAM_STATE_SERVER) { @@ -411,7 +406,7 @@ int _anjay_coap_stream_set_error(avs_stream_abstract_t *stream_, int _anjay_coap_stream_get_incoming_msg(avs_stream_abstract_t *stream_, const avs_coap_msg_t **out_msg) { - coap_stream_t *stream = (coap_stream_t*)stream_; + coap_stream_t *stream = (coap_stream_t *) stream_; assert(stream->vtable == &COAP_STREAM_VTABLE); int result = get_or_receive_msg(stream, out_msg); @@ -425,7 +420,7 @@ int _anjay_coap_stream_get_incoming_msg(avs_stream_abstract_t *stream_, int _anjay_coap_stream_get_request_identity(avs_stream_abstract_t *stream_, avs_coap_msg_identity_t *out_id) { - coap_stream_t *stream = (coap_stream_t*)stream_; + coap_stream_t *stream = (coap_stream_t *) stream_; assert(stream->vtable == &COAP_STREAM_VTABLE); const avs_coap_msg_identity_t *id = NULL; @@ -450,7 +445,7 @@ void _anjay_coap_stream_set_block_request_validator( avs_stream_abstract_t *stream_, anjay_coap_block_request_validator_t *validator, void *validator_arg) { - coap_stream_t *stream = (coap_stream_t*) stream_; + coap_stream_t *stream = (coap_stream_t *) stream_; assert(stream->vtable == &COAP_STREAM_VTABLE); _anjay_coap_server_set_block_request_relation_validator( diff --git a/src/coap/stream/stream_internal.h b/src/coap/stream/stream_internal.h index f75312118..e7152aa13 100644 --- a/src/coap/stream/stream_internal.h +++ b/src/coap/stream/stream_internal.h @@ -26,7 +26,7 @@ #include "../id_source/id_source.h" #ifndef ANJAY_COAP_STREAM_INTERNALS -#error "Headers from coap/stream are not meant to be included from outside" +# error "Headers from coap/stream are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN diff --git a/src/coap/test/block_response.c b/src/coap/test/block_response.c index 67f98de6b..3655573a3 100644 --- a/src/coap/test/block_response.c +++ b/src/coap/test/block_response.c @@ -19,18 +19,18 @@ #include #include -#include #include +#include #define ANJAY_COAP_STREAM_INTERNALS #include -#include "../content_format.h" -#include "../coap_stream.h" -#include "../stream/stream_internal.h" #include "../block/response.h" #include "../block/transfer_impl.h" +#include "../coap_stream.h" +#include "../content_format.h" +#include "../stream/stream_internal.h" typedef struct test_ctx { avs_net_abstract_socket_t *mocksock; @@ -38,8 +38,7 @@ typedef struct test_ctx { anjay_mock_coap_stream_ctx_t mock_stream; } test_ctx_t; -static test_ctx_t setup(size_t in_buffer_size, - size_t out_buffer_size) { +static test_ctx_t setup(size_t in_buffer_size, size_t out_buffer_size) { test_ctx_t ctx; memset(&ctx, 0, sizeof(ctx)); @@ -80,7 +79,7 @@ static size_t block_size_for_buffer_size_and_mtu(size_t out_buffer_size, test_ctx_t test = setup(4096, out_buffer_size - MSG_LENGTH_SIZE); - avs_unit_mocksock_enable_inner_mtu_getopt(test.mocksock, (int)mtu); + avs_unit_mocksock_enable_inner_mtu_getopt(test.mocksock, (int) mtu); _anjay_coap_out_setup_mtu(&coap_stream(&test)->data.common.out, coap_stream(&test)->data.common.socket); @@ -101,17 +100,13 @@ static size_t block_size_for_buffer_size_and_mtu(size_t out_buffer_size, // block-wise transfer. Library should account for that, adjusting block // size so that any token size can be safely handled. #define EXPECTED_HEADER_BYTES \ - (AVS_COAP_MAX_HEADER_SIZE \ - + AVS_COAP_MAX_TOKEN_LENGTH \ - + 0 + 1) + (AVS_COAP_MAX_HEADER_SIZE + AVS_COAP_MAX_TOKEN_LENGTH + 0 + 1) // header size + max possible BLOCK option size #define EXPECTED_HEADER_BYTES_WITH_BLOCK \ - (EXPECTED_HEADER_BYTES + AVS_COAP_OPT_BLOCK_MAX_SIZE) - + (EXPECTED_HEADER_BYTES + AVS_COAP_OPT_BLOCK_MAX_SIZE) - AVS_UNIT_ASSERT_SUCCESS( - _anjay_coap_out_setup_msg(&coap_stream(&test)->data.common.out, - &id, &details, NULL)); + AVS_UNIT_ASSERT_SUCCESS(_anjay_coap_out_setup_msg( + &coap_stream(&test)->data.common.out, &id, &details, NULL)); static coap_id_source_t *FAKE_ID_SOURCE = (coap_id_source_t *) -1; coap_block_transfer_ctx_t *ctx = _anjay_coap_block_response_new(AVS_COAP_MSG_BLOCK_MAX_SIZE, @@ -130,35 +125,31 @@ static size_t block_size_for_buffer_size_and_mtu(size_t out_buffer_size, AVS_UNIT_TEST(block_response, considers_mtu) { // block size: minimum possible - AVS_UNIT_ASSERT_EQUAL( - block_size_for_buffer_size_and_mtu( - 4096, - EXPECTED_HEADER_BYTES_WITH_BLOCK - + AVS_COAP_MSG_BLOCK_MIN_SIZE), - AVS_COAP_MSG_BLOCK_MIN_SIZE); + AVS_UNIT_ASSERT_EQUAL(block_size_for_buffer_size_and_mtu( + 4096, + EXPECTED_HEADER_BYTES_WITH_BLOCK + + AVS_COAP_MSG_BLOCK_MIN_SIZE), + AVS_COAP_MSG_BLOCK_MIN_SIZE); // not quite enough for bigger block size - AVS_UNIT_ASSERT_EQUAL( - block_size_for_buffer_size_and_mtu( - 4096, - EXPECTED_HEADER_BYTES_WITH_BLOCK - + AVS_COAP_MSG_BLOCK_MIN_SIZE * 2 - 1), - AVS_COAP_MSG_BLOCK_MIN_SIZE); + AVS_UNIT_ASSERT_EQUAL(block_size_for_buffer_size_and_mtu( + 4096, + EXPECTED_HEADER_BYTES_WITH_BLOCK + + AVS_COAP_MSG_BLOCK_MIN_SIZE * 2 + - 1), + AVS_COAP_MSG_BLOCK_MIN_SIZE); // enough for bigger block size - AVS_UNIT_ASSERT_EQUAL( - block_size_for_buffer_size_and_mtu( - 4096, - EXPECTED_HEADER_BYTES_WITH_BLOCK - + AVS_COAP_MSG_BLOCK_MIN_SIZE * 2), - AVS_COAP_MSG_BLOCK_MIN_SIZE * 2); + AVS_UNIT_ASSERT_EQUAL(block_size_for_buffer_size_and_mtu( + 4096, + EXPECTED_HEADER_BYTES_WITH_BLOCK + + AVS_COAP_MSG_BLOCK_MIN_SIZE * 2), + AVS_COAP_MSG_BLOCK_MIN_SIZE * 2); // MTU too low - should fail - AVS_UNIT_ASSERT_EQUAL( - block_size_for_buffer_size_and_mtu( - 4096, - AVS_COAP_MSG_BLOCK_MIN_SIZE - 1), - 0); + AVS_UNIT_ASSERT_EQUAL(block_size_for_buffer_size_and_mtu( + 4096, AVS_COAP_MSG_BLOCK_MIN_SIZE - 1), + 0); } AVS_UNIT_TEST(block_response, block_size_range) { @@ -176,22 +167,15 @@ AVS_UNIT_TEST(block_response, considers_buffer_size) { // MTU > buffer size, 1 byte short from enough for 64B of payload AVS_UNIT_ASSERT_EQUAL( - block_size_for_buffer_size_and_mtu( - EXTRA_SPACE + 64 - 1, - 4096), - 32); + block_size_for_buffer_size_and_mtu(EXTRA_SPACE + 64 - 1, 4096), 32); // MTU > buffer size, enough for 64B of payload AVS_UNIT_ASSERT_EQUAL( - block_size_for_buffer_size_and_mtu( - EXTRA_SPACE + 64, - 4096), - 64); + block_size_for_buffer_size_and_mtu(EXTRA_SPACE + 64, 4096), 64); // output buffer too small - should fail - AVS_UNIT_ASSERT_EQUAL( - block_size_for_buffer_size_and_mtu( - EXTRA_SPACE + AVS_COAP_MSG_BLOCK_MIN_SIZE - 1, - 4096), - 0); + AVS_UNIT_ASSERT_EQUAL(block_size_for_buffer_size_and_mtu( + EXTRA_SPACE + AVS_COAP_MSG_BLOCK_MIN_SIZE - 1, + 4096), + 0); } diff --git a/src/coap/test/servers.c b/src/coap/test/servers.c index b84662b94..d876c3fe4 100644 --- a/src/coap/test/servers.c +++ b/src/coap/test/servers.c @@ -15,41 +15,40 @@ */ #if !defined(_POSIX_C_SOURCE) && !defined(__APPLE__) -#define _POSIX_C_SOURCE 200809L +# define _POSIX_C_SOURCE 200809L #endif #include #ifdef ANJAY_TEST -#include "servers.h" +# include "servers.h" -#include -#include -#include -#include -#include +# include +# include +# include +# include -#include -#include -#include -#include -#include +# include +# include +# include +# include +# include -#if __linux__ -#include -#endif +# if __linux__ +# include +# endif -#include -#include -#include +# include +# include +# include -#include +# include -#include "../../utils_core.h" +# include "../../utils_core.h" -#include -#include "../coap_log.h" +# include "../coap_log.h" +# include typedef struct response_state { bool has_more_responses; @@ -92,11 +91,10 @@ static void wait_for_child(void) { sigset_t set; AVS_UNIT_ASSERT_SUCCESS(sigemptyset(&set)); AVS_UNIT_ASSERT_SUCCESS(sigaddset(&set, SIGUSR1)); - sigwait(&set, &(int){ -1 }); + sigwait(&set, &(int) { -1 }); } -static void udp_serve(uint16_t port, - make_response_func_t *make_response) { +static void udp_serve(uint16_t port, make_response_func_t *make_response) { int sock = socket(AF_INET, SOCK_DGRAM, 0); struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); @@ -104,9 +102,9 @@ static void udp_serve(uint16_t port, addr.sin_port = htons(port); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); - if (bind(sock, (struct sockaddr*)&addr, sizeof(addr))) { - coap_log(ERROR, "UDP server (127.0.0.1:%u) bind failed: %s", - port, strerror(errno)); + if (bind(sock, (struct sockaddr *) &addr, sizeof(addr))) { + coap_log(ERROR, "UDP server (127.0.0.1:%u) bind failed: %s", port, + strerror(errno)); goto cleanup; } @@ -125,8 +123,8 @@ static void udp_serve(uint16_t port, while (true) { if (poll(&sock_pollfd, 1, -1) < 0) { - coap_log(ERROR, "UDP server (127.0.0.1:%u) poll failed: %s", - port, strerror(errno)); + coap_log(ERROR, "UDP server (127.0.0.1:%u) poll failed: %s", port, + strerror(errno)); goto cleanup; } @@ -134,9 +132,9 @@ static void udp_serve(uint16_t port, memset(&remote_addr, 0, sizeof(remote_addr)); socklen_t remote_addr_len = sizeof(remote_addr); - ssize_t bytes_recv = recvfrom(sock, in_buffer, sizeof(in_buffer), 0, - (struct sockaddr*)&remote_addr, - &remote_addr_len); + ssize_t bytes_recv = + recvfrom(sock, in_buffer, sizeof(in_buffer), 0, + (struct sockaddr *) &remote_addr, &remote_addr_len); if (bytes_recv < 0) { coap_log(ERROR, "UDP server (127.0.0.1:%u) recvfrom failed: %s", port, strerror(errno)); @@ -146,17 +144,19 @@ static void udp_serve(uint16_t port, do { state.has_more_responses = false; - ssize_t bytes_to_send = make_response(&state, - in_buffer, (size_t)bytes_recv, - out_buffer, sizeof(out_buffer)); + ssize_t bytes_to_send = + make_response(&state, in_buffer, (size_t) bytes_recv, + out_buffer, sizeof(out_buffer)); if (bytes_to_send < 0) { - coap_log(ERROR, "UDP server (127.0.0.1:%u) make_response failed", + coap_log(ERROR, + "UDP server (127.0.0.1:%u) make_response failed", port); goto cleanup; } - if (sendto(sock, out_buffer, (size_t)bytes_to_send, 0, - (struct sockaddr*)&remote_addr, remote_addr_len) != bytes_to_send) { + if (sendto(sock, out_buffer, (size_t) bytes_to_send, 0, + (struct sockaddr *) &remote_addr, remote_addr_len) + != bytes_to_send) { coap_log(ERROR, "UDP server (127.0.0.1:%u) sendto failed: %s", port, strerror(errno)); goto cleanup; @@ -189,16 +189,16 @@ static void spawn_udp_server(uint16_t port, int pid = -1; switch (pid = fork()) { case 0: -#if __linux__ +# if __linux__ if (prctl(PR_SET_PDEATHSIG, SIGHUP)) { coap_log(WARNING, "prctl failed: %s", strerror(errno)); } -#endif // __linux__ +# endif // __linux__ udp_serve(port, make_response); // fall-through case -1: - coap_log(ERROR, "could not start UDP server on port %u: %s", - port, strerror(errno)); + coap_log(ERROR, "could not start UDP server on port %u: %s", port, + strerror(errno)); abort(); default: break; @@ -236,7 +236,8 @@ setup_socket(uint16_t port, make_response_func_t *make_response) { // this doesn't actually do anything, // but ensures that bind() and connect() can be used together AVS_UNIT_ASSERT_SUCCESS(avs_net_socket_bind(socket, NULL, NULL)); - AVS_UNIT_ASSERT_SUCCESS(avs_net_socket_connect(socket, "localhost", port_str)); + AVS_UNIT_ASSERT_SUCCESS( + avs_net_socket_connect(socket, "localhost", port_str)); return socket; } @@ -246,18 +247,19 @@ static ssize_t echo(response_state_t *state, size_t in_size, char *out, size_t out_size) { - (void)state; + (void) state; if (in_size > out_size) { return -1; } memcpy(out, in, in_size); - return (ssize_t)in_size; + return (ssize_t) in_size; } -#define VTTL(version, type, token_length) \ - (uint8_t)((((version) & 0x03) << 6) | (((type) & 0x03) << 4) | ((token_length) & 0x0f)) +# define VTTL(version, type, token_length) \ + (uint8_t)((((version) &0x03) << 6) | (((type) &0x03) << 4) \ + | ((token_length) &0x0f)) static ssize_t fill_header_with_token_and_id(avs_coap_msg_type_t type, const char *in, @@ -268,22 +270,21 @@ static ssize_t fill_header_with_token_and_id(avs_coap_msg_type_t type, return -1; } - uint8_t token_length = *(const uint8_t*)in & 0x0F; + uint8_t token_length = *(const uint8_t *) in & 0x0F; if (token_length > AVS_COAP_MAX_TOKEN_LENGTH || in_size < AVS_COAP_MSG_MIN_SIZE + token_length || out_size < AVS_COAP_MSG_MIN_SIZE + token_length) { return -1; } - out[0] = (char)VTTL(1, type, token_length); - out[1] = (char)AVS_COAP_CODE_CONTENT, - out[2] = (char)in[2]; - out[3] = (char)in[3]; + out[0] = (char) VTTL(1, type, token_length); + out[1] = (char) AVS_COAP_CODE_CONTENT, out[2] = (char) in[2]; + out[3] = (char) in[3]; - memcpy(out + AVS_COAP_MSG_MIN_SIZE, - in + AVS_COAP_MSG_MIN_SIZE, token_length); + memcpy(out + AVS_COAP_MSG_MIN_SIZE, in + AVS_COAP_MSG_MIN_SIZE, + token_length); - return (ssize_t)(AVS_COAP_MSG_MIN_SIZE + token_length); + return (ssize_t) (AVS_COAP_MSG_MIN_SIZE + token_length); } static ssize_t ack_echo(response_state_t *state, @@ -291,18 +292,19 @@ static ssize_t ack_echo(response_state_t *state, size_t in_size, char *out, size_t out_size) { - (void)state; + (void) state; if (out_size < in_size) { return -1; } memcpy(out, in, in_size); - if (fill_header_with_token_and_id(AVS_COAP_MSG_ACKNOWLEDGEMENT, - in, in_size, out, out_size) < 0) { + if (fill_header_with_token_and_id(AVS_COAP_MSG_ACKNOWLEDGEMENT, in, in_size, + out, out_size) + < 0) { return -1; } - return (ssize_t)in_size; + return (ssize_t) in_size; } static ssize_t reset(response_state_t *state, @@ -310,9 +312,9 @@ static ssize_t reset(response_state_t *state, size_t in_size, char *out, size_t out_size) { - (void)state; - return fill_header_with_token_and_id(AVS_COAP_MSG_RESET, in, in_size, - out, out_size); + (void) state; + return fill_header_with_token_and_id(AVS_COAP_MSG_RESET, in, in_size, out, + out_size); } static ssize_t mismatched_ack_then_reset(response_state_t *state, @@ -330,8 +332,8 @@ static ssize_t mismatched_ack_then_reset(response_state_t *state, out[3] = (char) ~out[3]; } } else { - retval = fill_header_with_token_and_id(AVS_COAP_MSG_RESET, - in, in_size, out, out_size); + retval = fill_header_with_token_and_id(AVS_COAP_MSG_RESET, in, in_size, + out, out_size); } return retval; @@ -346,8 +348,8 @@ static ssize_t mismatched_reset_then_ack(response_state_t *state, ssize_t retval; if (mismatched) { - retval = fill_header_with_token_and_id(AVS_COAP_MSG_RESET, - in, in_size, out, out_size); + retval = fill_header_with_token_and_id(AVS_COAP_MSG_RESET, in, in_size, + out, out_size); if (retval >= 4) { out[2] = (char) ~out[2]; out[3] = (char) ~out[3]; @@ -359,11 +361,10 @@ static ssize_t mismatched_reset_then_ack(response_state_t *state, return retval; } -static ssize_t fill_garbage(char *out, - size_t out_size) { - ssize_t msg_size = AVS_MIN(1024, (ssize_t)out_size); +static ssize_t fill_garbage(char *out, size_t out_size) { + ssize_t msg_size = AVS_MIN(1024, (ssize_t) out_size); for (ssize_t i = 0; i < msg_size; ++i) { - out[i] = (char)rand(); + out[i] = (char) rand(); } return msg_size; } @@ -388,9 +389,9 @@ static ssize_t garbage(response_state_t *state, size_t in_size, char *out, size_t out_size) { - (void)state; - (void)in; - (void)in_size; + (void) state; + (void) in; + (void) in_size; return fill_garbage(out, out_size); } @@ -417,7 +418,7 @@ static ssize_t long_separate(response_state_t *state, char *out, size_t out_size) { static char SAVED_DATA[2048] = { 0 }; - static avs_coap_msg_t *SAVED_MSG = (avs_coap_msg_t *)SAVED_DATA; + static avs_coap_msg_t *SAVED_MSG = (avs_coap_msg_t *) SAVED_DATA; assert(in_size < sizeof(SAVED_DATA)); assert(in_size >= 4); assert(out_size >= 4); @@ -427,7 +428,7 @@ static ssize_t long_separate(response_state_t *state, switch (state->response_counter % 3) { case 0: // empty ACK - SAVED_MSG->length = (uint32_t)in_size; + SAVED_MSG->length = (uint32_t) in_size; memcpy(SAVED_MSG->content, in, in_size); state->has_more_responses = true; return ack(NULL, in, in_size, out, out_size); @@ -467,7 +468,8 @@ avs_net_abstract_socket_t *_anjay_test_setup_udp_echo_socket(uint16_t port) { return setup_socket(port, echo); } -avs_net_abstract_socket_t *_anjay_test_setup_udp_ack_echo_socket(uint16_t port) { +avs_net_abstract_socket_t * +_anjay_test_setup_udp_ack_echo_socket(uint16_t port) { return setup_socket(port, ack_echo); } @@ -475,7 +477,8 @@ avs_net_abstract_socket_t *_anjay_test_setup_udp_reset_socket(uint16_t port) { return setup_socket(port, reset); } -avs_net_abstract_socket_t *_anjay_test_setup_udp_mismatched_ack_then_reset_socket(uint16_t port) { +avs_net_abstract_socket_t * +_anjay_test_setup_udp_mismatched_ack_then_reset_socket(uint16_t port) { return setup_socket(port, mismatched_ack_then_reset); } @@ -483,15 +486,18 @@ avs_net_abstract_socket_t *_anjay_test_setup_udp_garbage_socket(uint16_t port) { return setup_socket(port, garbage); } -avs_net_abstract_socket_t *_anjay_test_setup_udp_mismatched_reset_then_ack_socket(uint16_t port) { +avs_net_abstract_socket_t * +_anjay_test_setup_udp_mismatched_reset_then_ack_socket(uint16_t port) { return setup_socket(port, mismatched_reset_then_ack); } -avs_net_abstract_socket_t *_anjay_test_setup_udp_garbage_then_ack_socket(uint16_t port) { +avs_net_abstract_socket_t * +_anjay_test_setup_udp_garbage_then_ack_socket(uint16_t port) { return setup_socket(port, garbage_then_ack); } -avs_net_abstract_socket_t *_anjay_test_setup_udp_long_separate_socket(uint16_t port) { +avs_net_abstract_socket_t * +_anjay_test_setup_udp_long_separate_socket(uint16_t port) { return setup_socket(port, long_separate); } diff --git a/src/coap/test/servers.h b/src/coap/test/servers.h index d08600238..2f3891c86 100644 --- a/src/coap/test/servers.h +++ b/src/coap/test/servers.h @@ -31,18 +31,23 @@ avs_net_abstract_socket_t *_anjay_test_setup_udp_ack_echo_socket(uint16_t port); avs_net_abstract_socket_t *_anjay_test_setup_udp_reset_socket(uint16_t port); // responds with 2 packets: ACK with mismatched ID and then a correct Reset -avs_net_abstract_socket_t *_anjay_test_setup_udp_mismatched_ack_then_reset_socket(uint16_t port); +avs_net_abstract_socket_t * +_anjay_test_setup_udp_mismatched_ack_then_reset_socket(uint16_t port); // always responds with random data avs_net_abstract_socket_t *_anjay_test_setup_udp_garbage_socket(uint16_t port); // responds with 2 packets: Reset with mismatched ID and then a correct ACK -avs_net_abstract_socket_t *_anjay_test_setup_udp_mismatched_reset_then_ack_socket(uint16_t port); +avs_net_abstract_socket_t * +_anjay_test_setup_udp_mismatched_reset_then_ack_socket(uint16_t port); // responds with 2 packets: random data and then a correct ACK -avs_net_abstract_socket_t *_anjay_test_setup_udp_garbage_then_ack_socket(uint16_t port); +avs_net_abstract_socket_t * +_anjay_test_setup_udp_garbage_then_ack_socket(uint16_t port); -// responds with 3 packets: bare ACK, mismatched NON with garbage and NON with echo -avs_net_abstract_socket_t *_anjay_test_setup_udp_long_separate_socket(uint16_t port); +// responds with 3 packets: bare ACK, mismatched NON with garbage and NON with +// echo +avs_net_abstract_socket_t * +_anjay_test_setup_udp_long_separate_socket(uint16_t port); #endif // ANJAY_COAP_TEST_SERVERS_H diff --git a/src/coap/test/stream.c b/src/coap/test/stream.c index c07149e2e..ae0f665cf 100644 --- a/src/coap/test/stream.c +++ b/src/coap/test/stream.c @@ -22,15 +22,15 @@ #include #include -#include -#include #include +#include +#include #include #include "servers.h" -#include "../content_format.h" #include "../coap_stream.h" +#include "../content_format.h" #include "utils.h" #define TEST_PORT_UDP_ECHO 4322 @@ -77,8 +77,8 @@ AVS_UNIT_TEST(coap_stream, udp_read_write) { avs_coap_opt_iterator_t optit = AVS_COAP_OPT_ITERATOR_EMPTY; AVS_UNIT_ASSERT_SUCCESS(avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_URI_PATH, &optit, &bytes_read, - buffer, sizeof(buffer))); + msg, AVS_COAP_OPT_URI_PATH, &optit, &bytes_read, buffer, + sizeof(buffer))); AVS_UNIT_ASSERT_EQUAL( bytes_read, strlen((*AVS_LIST_NTH_PTR(&details.uri_path, 0))->c_str) + 1); @@ -86,8 +86,8 @@ AVS_UNIT_TEST(coap_stream, udp_read_write) { buffer, (*AVS_LIST_NTH_PTR(&details.uri_path, 0))->c_str); AVS_UNIT_ASSERT_SUCCESS(avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_URI_PATH, &optit, &bytes_read, - buffer, sizeof(buffer))); + msg, AVS_COAP_OPT_URI_PATH, &optit, &bytes_read, buffer, + sizeof(buffer))); AVS_UNIT_ASSERT_EQUAL( bytes_read, strlen((*AVS_LIST_NTH_PTR(&details.uri_path, 1))->c_str) + 1); @@ -95,8 +95,8 @@ AVS_UNIT_TEST(coap_stream, udp_read_write) { buffer, (*AVS_LIST_NTH_PTR(&details.uri_path, 1))->c_str); AVS_UNIT_ASSERT_SUCCESS(avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_URI_PATH, &optit, &bytes_read, - buffer, sizeof(buffer))); + msg, AVS_COAP_OPT_URI_PATH, &optit, &bytes_read, buffer, + sizeof(buffer))); AVS_UNIT_ASSERT_EQUAL( bytes_read, strlen((*AVS_LIST_NTH_PTR(&details.uri_path, 2))->c_str) + 1); @@ -110,8 +110,8 @@ AVS_UNIT_TEST(coap_stream, udp_read_write) { memset(&optit, 0, sizeof(optit)); AVS_UNIT_ASSERT_SUCCESS(avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_URI_QUERY, &optit, &bytes_read, - buffer, sizeof(buffer))); + msg, AVS_COAP_OPT_URI_QUERY, &optit, &bytes_read, buffer, + sizeof(buffer))); AVS_UNIT_ASSERT_EQUAL( bytes_read, strlen((*AVS_LIST_NTH_PTR(&details.uri_query, 0))->c_str) + 1); @@ -119,8 +119,8 @@ AVS_UNIT_TEST(coap_stream, udp_read_write) { buffer, (*AVS_LIST_NTH_PTR(&details.uri_query, 0))->c_str); AVS_UNIT_ASSERT_SUCCESS(avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_URI_QUERY, &optit, &bytes_read, - buffer, sizeof(buffer))); + msg, AVS_COAP_OPT_URI_QUERY, &optit, &bytes_read, buffer, + sizeof(buffer))); AVS_UNIT_ASSERT_EQUAL( bytes_read, strlen((*AVS_LIST_NTH_PTR(&details.uri_query, 1))->c_str) + 1); @@ -148,8 +148,10 @@ AVS_UNIT_TEST(coap_stream, no_payload) { _anjay_test_setup_udp_echo_socket(TEST_PORT_UDP_ECHO); avs_stream_abstract_t *stream = NULL; - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_GET, - .msg_type = AVS_COAP_MSG_NON_CONFIRMABLE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_GET, + .msg_type = AVS_COAP_MSG_NON_CONFIRMABLE + }; SCOPED_MOCK_COAP_STREAM(ctx) = _anjay_mock_coap_stream_create(&stream, socket, 4096, 4096); @@ -185,9 +187,11 @@ AVS_UNIT_TEST(coap_stream, msg_id) { SCOPED_MOCK_COAP_STREAM(ctx) = _anjay_mock_coap_stream_create(&stream, mocksock, 4096, 4096); - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_CONTENT, - .msg_type = AVS_COAP_MSG_NON_CONFIRMABLE, - .format = AVS_COAP_FORMAT_NONE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_CONTENT, + .msg_type = AVS_COAP_MSG_NON_CONFIRMABLE, + .format = AVS_COAP_FORMAT_NONE + }; { AVS_UNIT_ASSERT_SUCCESS( @@ -199,8 +203,8 @@ AVS_UNIT_TEST(coap_stream, msg_id) { AVS_UNIT_ASSERT_EQUAL(id.token.size, 0); AVS_UNIT_ASSERT_EQUAL(id.msg_id, 0x69ED); - const avs_coap_msg_t *response = COAP_MSG(NON, CONTENT, ID(0x69ED), - NO_PAYLOAD); + const avs_coap_msg_t *response = + COAP_MSG(NON, CONTENT, ID(0x69ED), NO_PAYLOAD); avs_unit_mocksock_expect_output(mocksock, response->content, response->length); AVS_UNIT_ASSERT_SUCCESS(avs_stream_finish_message(stream)); @@ -225,8 +229,8 @@ AVS_UNIT_TEST(coap_stream, msg_id) { AVS_UNIT_ASSERT_EQUAL(id.token.size, 0); AVS_UNIT_ASSERT_EQUAL(id.msg_id, 0x69EE); - const avs_coap_msg_t *response = COAP_MSG(NON, CONTENT, ID(0x69EE), - NO_PAYLOAD); + const avs_coap_msg_t *response = + COAP_MSG(NON, CONTENT, ID(0x69EE), NO_PAYLOAD); avs_unit_mocksock_expect_output(mocksock, response->content, response->length); AVS_UNIT_ASSERT_SUCCESS(avs_stream_finish_message(stream)); @@ -234,11 +238,13 @@ AVS_UNIT_TEST(coap_stream, msg_id) { { #define TOKEN "AYY LMAO" static const uint8_t TOKEN_SIZE = sizeof(TOKEN) - 1; - avs_coap_token_t token = { .size = TOKEN_SIZE }; + avs_coap_token_t token = { + .size = TOKEN_SIZE + }; memcpy(token.bytes, TOKEN, TOKEN_SIZE); - AVS_UNIT_ASSERT_SUCCESS(_anjay_coap_stream_setup_request( - stream, &details, &token)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_coap_stream_setup_request(stream, &details, &token)); avs_coap_msg_identity_t id; AVS_UNIT_ASSERT_SUCCESS( @@ -247,9 +253,8 @@ AVS_UNIT_TEST(coap_stream, msg_id) { AVS_UNIT_ASSERT_EQUAL_BYTES(&id.token.bytes, TOKEN); AVS_UNIT_ASSERT_EQUAL(id.msg_id, 0x69EF); - const avs_coap_msg_t *response = COAP_MSG(NON, CONTENT, - ID(0x69EF, TOKEN), - NO_PAYLOAD); + const avs_coap_msg_t *response = + COAP_MSG(NON, CONTENT, ID(0x69EF, TOKEN), NO_PAYLOAD); avs_unit_mocksock_expect_output(mocksock, response->content, response->length); AVS_UNIT_ASSERT_SUCCESS(avs_stream_finish_message(stream)); @@ -265,8 +270,8 @@ AVS_UNIT_TEST(coap_stream, msg_id) { AVS_UNIT_ASSERT_EQUAL(id.token.size, 0); AVS_UNIT_ASSERT_EQUAL(id.msg_id, 0x69F0); - const avs_coap_msg_t *response = COAP_MSG(NON, CONTENT, ID(0x69F0), - NO_PAYLOAD); + const avs_coap_msg_t *response = + COAP_MSG(NON, CONTENT, ID(0x69F0), NO_PAYLOAD); avs_unit_mocksock_expect_output(mocksock, response->content, response->length); AVS_UNIT_ASSERT_SUCCESS(avs_stream_finish_message(stream)); @@ -280,8 +285,10 @@ AVS_UNIT_TEST(coap_stream, read_some) { _anjay_test_setup_udp_ack_echo_socket(TEST_PORT_UDP_ACK); avs_stream_abstract_t *stream = NULL; - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_CONTENT, - .msg_type = AVS_COAP_MSG_CONFIRMABLE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_CONTENT, + .msg_type = AVS_COAP_MSG_CONFIRMABLE + }; SCOPED_MOCK_COAP_STREAM(ctx) = _anjay_mock_coap_stream_create(&stream, socket, 4096, 4096); @@ -320,8 +327,10 @@ AVS_UNIT_TEST(coap_stream, confirmable) { _anjay_test_setup_udp_ack_echo_socket(TEST_PORT_UDP_ACK); avs_stream_abstract_t *stream = NULL; - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_CONTENT, - .msg_type = AVS_COAP_MSG_CONFIRMABLE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_CONTENT, + .msg_type = AVS_COAP_MSG_CONFIRMABLE + }; SCOPED_MOCK_COAP_STREAM(ctx) = _anjay_mock_coap_stream_create(&stream, socket, 4096, 4096); @@ -356,8 +365,10 @@ AVS_UNIT_TEST(coap_stream, reset_when_sending) { _anjay_test_setup_udp_reset_socket(TEST_PORT_UDP_RESET); avs_stream_abstract_t *stream = NULL; - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_CONTENT, - .msg_type = AVS_COAP_MSG_CONFIRMABLE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_CONTENT, + .msg_type = AVS_COAP_MSG_CONFIRMABLE + }; SCOPED_MOCK_COAP_STREAM(ctx) = _anjay_mock_coap_stream_create(&stream, socket, 4096, 4096); @@ -375,8 +386,10 @@ AVS_UNIT_TEST(coap_stream, mismatched_reset) { TEST_PORT_UDP_MISMATCHED_RESET); avs_stream_abstract_t *stream = NULL; - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_CONTENT, - .msg_type = AVS_COAP_MSG_CONFIRMABLE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_CONTENT, + .msg_type = AVS_COAP_MSG_CONFIRMABLE + }; SCOPED_MOCK_COAP_STREAM(ctx) = _anjay_mock_coap_stream_create(&stream, socket, 4096, 4096); @@ -394,8 +407,10 @@ AVS_UNIT_TEST(coap_stream, garbage_response_when_waiting_for_ack) { TEST_PORT_UDP_GARBAGE_ACK); avs_stream_abstract_t *stream = NULL; - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_CONTENT, - .msg_type = AVS_COAP_MSG_CONFIRMABLE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_CONTENT, + .msg_type = AVS_COAP_MSG_CONFIRMABLE + }; SCOPED_MOCK_COAP_STREAM(ctx) = _anjay_mock_coap_stream_create(&stream, socket, 4096, 4096); @@ -413,8 +428,10 @@ AVS_UNIT_TEST(coap_stream, ack_with_mismatched_id) { TEST_PORT_UDP_MISMATCHED); avs_stream_abstract_t *stream = NULL; - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_CONTENT, - .msg_type = AVS_COAP_MSG_CONFIRMABLE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_CONTENT, + .msg_type = AVS_COAP_MSG_CONFIRMABLE + }; SCOPED_MOCK_COAP_STREAM(ctx) = _anjay_mock_coap_stream_create(&stream, socket, 4096, 4096); @@ -432,8 +449,10 @@ AVS_UNIT_TEST(coap_stream, long_separate) { TEST_PORT_UDP_LONG_SEPARATE); avs_stream_abstract_t *stream = NULL; - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_CONTENT, - .msg_type = AVS_COAP_MSG_CONFIRMABLE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_CONTENT, + .msg_type = AVS_COAP_MSG_CONFIRMABLE + }; char out_data[] = "follow the white rabbit"; @@ -465,8 +484,10 @@ AVS_UNIT_TEST(coap_stream, receive_garbage) { TEST_PORT_UDP_GARBAGE); avs_stream_abstract_t *stream = NULL; - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_CONTENT, - .msg_type = AVS_COAP_MSG_NON_CONFIRMABLE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_CONTENT, + .msg_type = AVS_COAP_MSG_NON_CONFIRMABLE + }; SCOPED_MOCK_COAP_STREAM(ctx) = _anjay_mock_coap_stream_create(&stream, socket, 4096, 4096); @@ -496,18 +517,20 @@ AVS_UNIT_TEST(coap_stream, add_observe_option) { SCOPED_MOCK_COAP_STREAM(ctx) = _anjay_mock_coap_stream_create(&stream, mocksock, 4096, 4096); - anjay_msg_details_t details = {.msg_code = AVS_COAP_CODE_CONTENT, - .msg_type = AVS_COAP_MSG_NON_CONFIRMABLE, - .format = AVS_COAP_FORMAT_NONE }; + anjay_msg_details_t details = { + .msg_code = AVS_COAP_CODE_CONTENT, + .msg_type = AVS_COAP_MSG_NON_CONFIRMABLE, + .format = AVS_COAP_FORMAT_NONE + }; { details.observe_serial = true; _anjay_mock_clock_start((avs_time_monotonic_t) { { 514, 777 << 15 } }); AVS_UNIT_ASSERT_SUCCESS( _anjay_coap_stream_setup_request(stream, &details, NULL)); - const avs_coap_msg_t *response = COAP_MSG(NON, CONTENT, - ID(0x69ED), OBSERVE(0x010309), - NO_PAYLOAD); + const avs_coap_msg_t *response = + COAP_MSG(NON, CONTENT, ID(0x69ED), OBSERVE(0x010309), + NO_PAYLOAD); avs_unit_mocksock_expect_output(mocksock, response->content, response->length); AVS_UNIT_ASSERT_SUCCESS(avs_stream_finish_message(stream)); @@ -518,9 +541,9 @@ AVS_UNIT_TEST(coap_stream, add_observe_option) { _anjay_mock_clock_start((avs_time_monotonic_t) { { 777, 514 << 15 } }); AVS_UNIT_ASSERT_SUCCESS( _anjay_coap_stream_setup_request(stream, &details, NULL)); - const avs_coap_msg_t *response = COAP_MSG(NON, CONTENT, - ID(0x69EE), OBSERVE(0x848202), - NO_PAYLOAD); + const avs_coap_msg_t *response = + COAP_MSG(NON, CONTENT, ID(0x69EE), OBSERVE(0x848202), + NO_PAYLOAD); avs_unit_mocksock_expect_output(mocksock, response->content, response->length); AVS_UNIT_ASSERT_SUCCESS(avs_stream_finish_message(stream)); @@ -537,8 +560,10 @@ typedef struct test_data { } test_data_t; static test_data_t setup_test(void) { - test_data_t data = {.mock_socket = NULL, - .stream = NULL }; + test_data_t data = { + .mock_socket = NULL, + .stream = NULL + }; _anjay_mocksock_create(&data.mock_socket, 1252, 1252); avs_unit_mocksock_expect_connect(data.mock_socket, "", ""); @@ -565,8 +590,8 @@ static void mock_receive_request(test_data_t *test, avs_unit_mocksock_input(test->mock_socket, request, request_size); const avs_coap_msg_t *msg; - AVS_UNIT_ASSERT_SUCCESS(_anjay_coap_stream_get_incoming_msg(test->stream, - &msg)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_coap_stream_get_incoming_msg(test->stream, &msg)); } AVS_UNIT_TEST(coap_stream, response_empty) { @@ -575,15 +600,16 @@ AVS_UNIT_TEST(coap_stream, response_empty) { const avs_coap_msg_t *request = COAP_MSG(CON, PUT, ID(0x0001)); mock_receive_request(&test, request->content, request->length); - const avs_coap_msg_t *response = COAP_MSG(ACK, CHANGED, ID(0x0001), - NO_PAYLOAD); + const avs_coap_msg_t *response = + COAP_MSG(ACK, CHANGED, ID(0x0001), NO_PAYLOAD); avs_unit_mocksock_expect_output(test.mock_socket, response->content, response->length); - const anjay_msg_details_t details = {.msg_type = - AVS_COAP_MSG_ACKNOWLEDGEMENT, - .msg_code = AVS_COAP_CODE_CHANGED, - .format = AVS_COAP_FORMAT_NONE }; + const anjay_msg_details_t details = { + .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, + .msg_code = AVS_COAP_CODE_CHANGED, + .format = AVS_COAP_FORMAT_NONE + }; AVS_UNIT_ASSERT_SUCCESS( _anjay_coap_stream_setup_response(test.stream, &details)); AVS_UNIT_ASSERT_SUCCESS(avs_stream_finish_message(test.stream)); @@ -599,16 +625,17 @@ AVS_UNIT_TEST(coap_stream, response_token) { const avs_coap_msg_t *request = COAP_MSG(CON, PUT, ID(0x0001, TOKEN)); mock_receive_request(&test, request->content, request->length); - const avs_coap_msg_t *response = COAP_MSG(ACK, CHANGED, ID(0x0001, TOKEN), - NO_PAYLOAD); + const avs_coap_msg_t *response = + COAP_MSG(ACK, CHANGED, ID(0x0001, TOKEN), NO_PAYLOAD); avs_unit_mocksock_expect_output(test.mock_socket, response->content, response->length); #undef TOKEN - const anjay_msg_details_t details = {.msg_type = - AVS_COAP_MSG_ACKNOWLEDGEMENT, - .msg_code = AVS_COAP_CODE_CHANGED, - .format = AVS_COAP_FORMAT_NONE }; + const anjay_msg_details_t details = { + .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, + .msg_code = AVS_COAP_CODE_CHANGED, + .format = AVS_COAP_FORMAT_NONE + }; AVS_UNIT_ASSERT_SUCCESS( _anjay_coap_stream_setup_response(test.stream, &details)); AVS_UNIT_ASSERT_SUCCESS(avs_stream_finish_message(test.stream)); @@ -625,15 +652,16 @@ AVS_UNIT_TEST(coap_stream, response_content) { const avs_coap_msg_t *request = COAP_MSG(CON, PUT, ID(0x0001)); mock_receive_request(&test, request->content, request->length); - const avs_coap_msg_t *response = COAP_MSG(ACK, CHANGED, ID(0x0001), - PAYLOAD(CONTENT)); + const avs_coap_msg_t *response = + COAP_MSG(ACK, CHANGED, ID(0x0001), PAYLOAD(CONTENT)); avs_unit_mocksock_expect_output(test.mock_socket, response->content, response->length); - const anjay_msg_details_t details = {.msg_type = - AVS_COAP_MSG_ACKNOWLEDGEMENT, - .msg_code = AVS_COAP_CODE_CHANGED, - .format = AVS_COAP_FORMAT_NONE }; + const anjay_msg_details_t details = { + .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, + .msg_code = AVS_COAP_CODE_CHANGED, + .format = AVS_COAP_FORMAT_NONE + }; AVS_UNIT_ASSERT_SUCCESS( _anjay_coap_stream_setup_response(test.stream, &details)); AVS_UNIT_ASSERT_SUCCESS( @@ -651,12 +679,10 @@ AVS_UNIT_TEST(coap_stream, response_options) { const avs_coap_msg_t *request = COAP_MSG(CON, PUT, ID(0x0001)); mock_receive_request(&test, request->content, request->length); - const avs_coap_msg_t *response = COAP_MSG(ACK, CHANGED, ID(0x0001), - LOCATION_PATH("slychac", "trzask", - "bylo", "zalozyc", - "kask"), - PATH("w", "ryj", "z", "kopa"), - QUERY("albo=lepiej", "w=jadra")); + const avs_coap_msg_t *response = COAP_MSG( + ACK, CHANGED, ID(0x0001), + LOCATION_PATH("slychac", "trzask", "bylo", "zalozyc", "kask"), + PATH("w", "ryj", "z", "kopa"), QUERY("albo=lepiej", "w=jadra")); avs_unit_mocksock_expect_output(test.mock_socket, response->content, response->length); @@ -675,17 +701,19 @@ AVS_UNIT_TEST(coap_stream, response_options) { AVS_LIST_CLEAR((AVS_LIST(anjay_string_t) *) (intptr_t) &details.uri_path); AVS_LIST_CLEAR((AVS_LIST(anjay_string_t) *) (intptr_t) &details.uri_query); - AVS_LIST_CLEAR((AVS_LIST(anjay_string_t) *) (intptr_t) &details.location_path); + AVS_LIST_CLEAR( + (AVS_LIST(anjay_string_t) *) (intptr_t) &details.location_path); teardown_test(&test); } AVS_UNIT_TEST(coap_stream, response_no_request) { test_data_t test = setup_test(); - const anjay_msg_details_t details = {.msg_type = - AVS_COAP_MSG_ACKNOWLEDGEMENT, - .msg_code = AVS_COAP_CODE_CHANGED, - .format = AVS_COAP_FORMAT_NONE }; + const anjay_msg_details_t details = { + .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, + .msg_code = AVS_COAP_CODE_CHANGED, + .format = AVS_COAP_FORMAT_NONE + }; AVS_UNIT_ASSERT_FAILED( _anjay_coap_stream_setup_response(test.stream, &details)); @@ -707,8 +735,8 @@ AVS_UNIT_TEST(coap_stream, fuzz_1_invalid_block_size) { "\xd1\x0e" // delta = 13 + 14, length = 1 "\x07"; // seq_num = 0, has_more = 0, block_size = 2048 - const avs_coap_msg_t *bad_option_res = COAP_MSG(ACK, BAD_REQUEST, - ID(0x0001)); + const avs_coap_msg_t *bad_option_res = + COAP_MSG(ACK, BAD_REQUEST, ID(0x0001)); avs_net_abstract_socket_t *mocksock = NULL; _anjay_mocksock_create(&mocksock, 1252, 1252); diff --git a/src/coap/test/utils.h b/src/coap/test/utils.h index f572feb8e..50809176a 100644 --- a/src/coap/test/utils.h +++ b/src/coap/test/utils.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#include #include +#include #include #include @@ -69,7 +69,8 @@ coap_msg__(avs_coap_aligned_msg_buffer_t *buf, .identity = args->id, }; - memcpy(info.identity.token.bytes, args->token_as_string__, args->id.token.size); + memcpy(info.identity.token.bytes, args->token_as_string__, + args->id.token.size); if (args->block1.valid) { AVS_UNIT_ASSERT_SUCCESS( @@ -80,18 +81,16 @@ coap_msg__(avs_coap_aligned_msg_buffer_t *buf, avs_coap_msg_info_opt_block(&info, &args->block2)); } if (args->etag) { - AVS_UNIT_ASSERT_SUCCESS( - avs_coap_msg_info_opt_opaque(&info, AVS_COAP_OPT_ETAG, - args->etag->value, - args->etag->size)); + AVS_UNIT_ASSERT_SUCCESS(avs_coap_msg_info_opt_opaque( + &info, AVS_COAP_OPT_ETAG, args->etag->value, args->etag->size)); } - AVS_UNIT_ASSERT_SUCCESS(add_string_options(&info, AVS_COAP_OPT_LOCATION_PATH, - args->location_path)); - AVS_UNIT_ASSERT_SUCCESS(add_string_options(&info, AVS_COAP_OPT_URI_PATH, - args->uri_path)); - AVS_UNIT_ASSERT_SUCCESS(add_string_options(&info, AVS_COAP_OPT_URI_QUERY, - args->uri_query)); + AVS_UNIT_ASSERT_SUCCESS(add_string_options( + &info, AVS_COAP_OPT_LOCATION_PATH, args->location_path)); + AVS_UNIT_ASSERT_SUCCESS( + add_string_options(&info, AVS_COAP_OPT_URI_PATH, args->uri_path)); + AVS_UNIT_ASSERT_SUCCESS( + add_string_options(&info, AVS_COAP_OPT_URI_QUERY, args->uri_query)); if (args->content_format) { AVS_UNIT_ASSERT_SUCCESS(avs_coap_msg_info_opt_u16( &info, AVS_COAP_OPT_CONTENT_FORMAT, *args->content_format)); @@ -108,10 +107,9 @@ coap_msg__(avs_coap_aligned_msg_buffer_t *buf, AVS_UNIT_ASSERT_SUCCESS( avs_coap_msg_builder_init(&builder, buf, buf_size, &info)); - AVS_UNIT_ASSERT_EQUAL( - args->payload_size, - avs_coap_msg_builder_payload(&builder, args->payload, - args->payload_size)); + AVS_UNIT_ASSERT_EQUAL(args->payload_size, + avs_coap_msg_builder_payload(&builder, args->payload, + args->payload_size)); AVS_LIST_CLEAR(&args->location_path); AVS_LIST_CLEAR(&args->uri_path); @@ -151,26 +149,29 @@ coap_msg__(avs_coap_aligned_msg_buffer_t *buf, * BLOCK2(0, 16, "full_payload")); * @endcode */ -#define COAP_MSG(Type, Code, Id, .../* Payload, Opts... */) \ - coap_msg__(avs_coap_ensure_aligned_buffer(&(uint8_t[65536]){0}), 65536, \ - &(struct coap_msg_args){ \ - .type = (Type), \ - .code = CODE__(Code), \ - Id, __VA_ARGS__ \ - }) +#define COAP_MSG(Type, Code, Id, ... /* Payload, Opts... */) \ + coap_msg__(avs_coap_ensure_aligned_buffer(&(uint8_t[65536]){ 0 }), 65536, \ + &(struct coap_msg_args) { \ + .type = (Type), \ + .code = CODE__(Code), \ + Id, \ + __VA_ARGS__ \ + }) /* Used in COAP_MSG() to define message identity. */ -#define ID(MsgId, .../* Token */) \ - .id = (avs_coap_msg_identity_t){ \ - (uint16_t)(MsgId), \ - (avs_coap_token_t){sizeof(""__VA_ARGS__) - 1, ""} }, \ +#define ID(MsgId, ... /* Token */) \ + .id = \ + (avs_coap_msg_identity_t) { \ + (uint16_t) (MsgId), \ + (avs_coap_token_t) { sizeof(""__VA_ARGS__) - 1, "" } \ + }, \ .token_as_string__ = ""__VA_ARGS__ /* Used in COAP_MSG() to specify ETag option value. */ -#define ETAG(Tag) \ - .etag = (const anjay_etag_t *) &(anjay_coap_etag_t){ \ - .size = sizeof(Tag) - 1, \ - .value = (Tag) \ +#define ETAG(Tag) \ + .etag = (const anjay_etag_t *) &(anjay_coap_etag_t) { \ + .size = sizeof(Tag) - 1, \ + .value = (Tag) \ } /* Used in COAP_MSG() to specify a list of Location-Path options. */ @@ -178,8 +179,7 @@ coap_msg__(avs_coap_aligned_msg_buffer_t *buf, .location_path = ANJAY_MAKE_STRING_LIST(__VA_ARGS__) /* Used in COAP_MSG() to specify a list of Uri-Path options. */ -#define PATH(... /* Segments */) \ - .uri_path = ANJAY_MAKE_STRING_LIST(__VA_ARGS__) +#define PATH(... /* Segments */) .uri_path = ANJAY_MAKE_STRING_LIST(__VA_ARGS__) /* Used in COAP_MSG() to specify a list of Uri-Query options. */ #define QUERY(... /* Segments */) \ @@ -187,34 +187,40 @@ coap_msg__(avs_coap_aligned_msg_buffer_t *buf, /* Used in COAP_MSG() to specify the Content-Format option even with * unsupported value. */ -#define CONTENT_FORMAT_VALUE(Format) \ - .content_format = (const uint16_t[1]) { (Format) } +#define CONTENT_FORMAT_VALUE(Format) \ + .content_format = (const uint16_t[1]) { \ + (Format) \ + } /* Used in COAP_MSG() to specify the Content-Format option using predefined * constants. */ #define CONTENT_FORMAT(Format) CONTENT_FORMAT_VALUE(FORMAT__(Format)) /* Used in COAP_MSG() to specify the Accept option. */ -#define ACCEPT(Format) \ - .accept = (const uint16_t[1]) { (Format) } +#define ACCEPT(Format) \ + .accept = (const uint16_t[1]) { \ + (Format) \ + } /* Used in COAP_MSG() to specify the Observe option. */ -#define OBSERVE(Value) \ - .observe = (const uint32_t[1]) { (Value) } +#define OBSERVE(Value) \ + .observe = (const uint32_t[1]) { \ + (Value) \ + } /* Used in COAP_MSG() to define a message with no payload or BLOCK options. */ -#define NO_PAYLOAD \ - .block1 = {}, \ - .block2 = {}, \ +#define NO_PAYLOAD \ + .block1 = {}, \ + .block2 = {}, \ .payload = NULL, \ .payload_size = 0 /* Used in COAP_MSG() to define a non-block message payload from external * variable (not only string literal). */ #define PAYLOAD_EXTERNAL(Payload, PayloadSize) \ - .block1 = {}, \ - .block2 = {}, \ - .payload = (Payload), \ + .block1 = {}, \ + .block2 = {}, \ + .payload = (Payload), \ .payload_size = PayloadSize, /* Used in COAP_MSG() to define a non-block message payload (string literal). @@ -230,19 +236,20 @@ coap_msg__(avs_coap_aligned_msg_buffer_t *buf, * of it based on Seq and Size. Terminating nullbyte is not * considered part of the payload. */ -#define BLOCK2(Seq, Size, ... /* Payload */) \ - .block1 = {}, \ - .block2 = { \ - .type = AVS_COAP_BLOCK2, \ - .valid = true, \ - .seq_num = (assert((Seq) < (1 << 23)), (uint32_t)(Seq)), \ - .size = (assert((Size) < (1 << 15)), (uint16_t)(Size)), \ - .has_more = ((Seq + 1) * (Size) + 1 < sizeof("" __VA_ARGS__)) \ - }, \ - .payload = ((const uint8_t*)("" __VA_ARGS__)) + (Seq) * (Size), \ - .payload_size = sizeof("" __VA_ARGS__) == sizeof("") \ - ? 0 \ - : ((((Seq) + 1) * (Size) + 1 < sizeof("" __VA_ARGS__)) \ - ? (Size) \ - : (sizeof("" __VA_ARGS__) - 1 - (Seq) * (Size))) - +#define BLOCK2(Seq, Size, ... /* Payload */) \ + .block1 = {}, \ + .block2 = { \ + .type = AVS_COAP_BLOCK2, \ + .valid = true, \ + .seq_num = (assert((Seq) < (1 << 23)), (uint32_t) (Seq)), \ + .size = (assert((Size) < (1 << 15)), (uint16_t) (Size)), \ + .has_more = ((Seq + 1) * (Size) + 1 < sizeof("" __VA_ARGS__)) \ + }, \ + .payload = ((const uint8_t *) ("" __VA_ARGS__)) + (Seq) * (Size), \ + .payload_size = \ + sizeof("" __VA_ARGS__) == sizeof("") \ + ? 0 \ + : ((((Seq) + 1) * (Size) + 1 < sizeof("" __VA_ARGS__)) \ + ? (Size) \ + : (sizeof("" __VA_ARGS__) - 1 \ + - (Seq) * (Size))) diff --git a/src/dm/discover.c b/src/dm/discover.c index a04d23afb..1477d352a 100644 --- a/src/dm/discover.c +++ b/src/dm/discover.c @@ -23,14 +23,13 @@ #include "discover.h" #include "query.h" -#include "../dm_core.h" #include "../anjay_core.h" +#include "../dm_core.h" VISIBILITY_SOURCE_BEGIN -static int print_period_attr(avs_stream_abstract_t *stream, - const char *name, - int32_t t) { +static int +print_period_attr(avs_stream_abstract_t *stream, const char *name, int32_t t) { if (t < 0) { return 0; } @@ -47,7 +46,7 @@ static int print_con_attr(avs_stream_abstract_t *stream, (int) value); } #else // WITH_CON_ATTR -#define print_con_attr(...) 0 +# define print_con_attr(...) 0 #endif // WITH_CON_ATTR static int print_double_attr(avs_stream_abstract_t *stream, @@ -100,7 +99,8 @@ static int print_discovered_object(avs_stream_abstract_t *stream, return retval; } if ((*obj)->version) { - if ((retval = avs_stream_write_f(stream, ";ver=\"%s\"", (*obj)->version))) { + if ((retval = avs_stream_write_f(stream, ";ver=\"%s\"", + (*obj)->version))) { return retval; } } @@ -119,12 +119,13 @@ static int print_discovered_instance(avs_stream_abstract_t *stream, return print_attrs(stream, attrs); } -static int print_discovered_resource(avs_stream_abstract_t *stream, - const anjay_dm_object_def_t *const *obj, - anjay_iid_t iid, - anjay_rid_t rid, - int32_t resource_dim, - const anjay_dm_internal_res_attrs_t *attrs) { +static int +print_discovered_resource(avs_stream_abstract_t *stream, + const anjay_dm_object_def_t *const *obj, + anjay_iid_t iid, + anjay_rid_t rid, + int32_t resource_dim, + const anjay_dm_internal_res_attrs_t *attrs) { int retval = avs_stream_write_f(stream, "", (*obj)->oid, iid, rid); @@ -162,8 +163,7 @@ static int read_resource_dim(anjay_t *anjay, anjay_rid_t rid, int32_t *out_dim) { int result = _anjay_dm_resource_dim(anjay, obj, iid, rid, NULL); - if (result == ANJAY_DM_DIM_INVALID - || result == ANJAY_ERR_METHOD_NOT_ALLOWED + if (result == ANJAY_DM_DIM_INVALID || result == ANJAY_ERR_METHOD_NOT_ALLOWED || result == ANJAY_ERR_NOT_IMPLEMENTED) { *out_dim = -1; } else if (result < 0) { @@ -195,12 +195,12 @@ static int discover_resource(anjay_t *anjay, result = read_resource_dim(anjay, obj, iid, rid, &resource_dim); } - anjay_dm_internal_res_attrs_t resource_attributes - = ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY; + anjay_dm_internal_res_attrs_t resource_attributes = + ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY; if (hint == WITH_RESOURCE_ATTRIBS) { - result = _anjay_dm_resource_read_attrs( - anjay, obj, iid, rid, _anjay_dm_current_ssid(anjay), - &resource_attributes, NULL); + result = _anjay_dm_resource_read_attrs(anjay, obj, iid, rid, + _anjay_dm_current_ssid(anjay), + &resource_attributes, NULL); } else if (hint == WITH_INHERITED_ATTRIBS) { anjay_dm_attrs_query_details_t details = (anjay_dm_attrs_query_details_t) { @@ -230,9 +230,8 @@ static int discover_instance_resources(anjay_t *anjay, discover_resource_hint_t hint) { int result = 0; for (size_t i = 0; i < (*obj)->supported_rids.count; ++i) { - result = _anjay_dm_resource_present(anjay, obj, iid, - (*obj)->supported_rids.rids[i], - NULL); + result = _anjay_dm_resource_present( + anjay, obj, iid, (*obj)->supported_rids.rids[i], NULL); if (result <= 0) { continue; } @@ -253,8 +252,8 @@ static int discover_object_instance(anjay_t *anjay, int result = 0; (void) ((result = print_separator(anjay->comm_stream)) || (result = print_discovered_instance( - anjay->comm_stream, obj, iid, - &ANJAY_DM_INTERNAL_ATTRS_EMPTY)) + anjay->comm_stream, obj, iid, + &ANJAY_DM_INTERNAL_ATTRS_EMPTY)) || (result = discover_instance_resources(anjay, obj, iid, NO_ATTRIBS))); return result; @@ -316,8 +315,8 @@ bootstrap_discover_object_instance(anjay_t *anjay, int result = 0; (void) ((result = print_separator(anjay->comm_stream)) || (result = print_discovered_instance( - anjay->comm_stream, obj, iid, - &ANJAY_DM_INTERNAL_ATTRS_EMPTY))); + anjay->comm_stream, obj, iid, + &ANJAY_DM_INTERNAL_ATTRS_EMPTY))); if (result) { return result; } @@ -345,8 +344,8 @@ int _anjay_bootstrap_discover_object(anjay_t *anjay, if (result) { return result; } - return _anjay_dm_foreach_instance( - anjay, obj, bootstrap_discover_object_instance, NULL); + return _anjay_dm_foreach_instance(anjay, obj, + bootstrap_discover_object_instance, NULL); } static int bootstrap_discover_object(anjay_t *anjay, diff --git a/src/dm/discover.h b/src/dm/discover.h index 6fd32e45f..0ef678c91 100644 --- a/src/dm/discover.h +++ b/src/dm/discover.h @@ -18,8 +18,8 @@ #define ANJAY_DM_DISCOVER_H #include -#include #include +#include VISIBILITY_PRIVATE_HEADER_BEGIN @@ -67,7 +67,7 @@ int _anjay_discover_resource(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid); -#ifdef WITH_BOOTSTRAP +# ifdef WITH_BOOTSTRAP /** * Performs LwM2M Bootstrap Discover operation on the specified Object @p obj. * @@ -84,7 +84,7 @@ int _anjay_bootstrap_discover_object(anjay_t *anjay, * @retrurn 0 on success, negative value in case of an error. */ int _anjay_bootstrap_discover(anjay_t *anjay); -#endif // WITH_BOOTSTRAP +# endif // WITH_BOOTSTRAP #endif // WITH_DISCOVER diff --git a/src/dm/dm_attributes.c b/src/dm/dm_attributes.c index 5cb923afd..0a219cc61 100644 --- a/src/dm/dm_attributes.c +++ b/src/dm/dm_attributes.c @@ -16,8 +16,8 @@ #include -#include #include +#include #include "../anjay_core.h" #include "../utils_core.h" @@ -103,9 +103,9 @@ int _anjay_dm_read_combined_server_attrs(anjay_t *anjay, if ((result = read_combined_period(anjay, server_iid, ANJAY_DM_RID_SERVER_DEFAULT_PMIN, &out->standard.min_period)) - || (result = read_combined_period(anjay, server_iid, - ANJAY_DM_RID_SERVER_DEFAULT_PMAX, - &out->standard.max_period))) { + || (result = read_combined_period( + anjay, server_iid, ANJAY_DM_RID_SERVER_DEFAULT_PMAX, + &out->standard.max_period))) { return result; } } @@ -123,8 +123,9 @@ int _anjay_dm_read_combined_instance_attrs( anjay_dm_internal_attrs_t *out) { if (!_anjay_dm_attributes_full(out)) { anjay_dm_internal_attrs_t instattrs = ANJAY_DM_INTERNAL_ATTRS_EMPTY; - int result = _anjay_dm_instance_read_default_attrs( - anjay, obj, iid, ssid, &instattrs, NULL); + int result = + _anjay_dm_instance_read_default_attrs(anjay, obj, iid, ssid, + &instattrs, NULL); if (result) { return result; } @@ -140,8 +141,8 @@ int _anjay_dm_read_combined_object_attrs( anjay_dm_internal_attrs_t *out) { if (!_anjay_dm_attributes_full(out)) { anjay_dm_internal_attrs_t objattrs = ANJAY_DM_INTERNAL_ATTRS_EMPTY; - int result = _anjay_dm_object_read_default_attrs( - anjay, obj, ssid, &objattrs, NULL); + int result = _anjay_dm_object_read_default_attrs(anjay, obj, ssid, + &objattrs, NULL); if (result) { return result; } @@ -151,10 +152,9 @@ int _anjay_dm_read_combined_object_attrs( } bool _anjay_dm_attributes_empty(const anjay_dm_internal_attrs_t *attrs) { - return attrs->standard.min_period < 0 - && attrs->standard.max_period < 0 + return attrs->standard.min_period < 0 && attrs->standard.max_period < 0 #ifdef WITH_CON_ATTR - && attrs->custom.data.con < 0 + && attrs->custom.data.con < 0 #endif ; } @@ -162,17 +162,15 @@ bool _anjay_dm_attributes_empty(const anjay_dm_internal_attrs_t *attrs) { bool _anjay_dm_resource_attributes_empty( const anjay_dm_internal_res_attrs_t *attrs) { return _anjay_dm_attributes_empty( - _anjay_dm_get_internal_attrs_const(&attrs->standard.common)) - && isnan(attrs->standard.greater_than) - && isnan(attrs->standard.less_than) - && isnan(attrs->standard.step); + _anjay_dm_get_internal_attrs_const(&attrs->standard.common)) + && isnan(attrs->standard.greater_than) + && isnan(attrs->standard.less_than) && isnan(attrs->standard.step); } bool _anjay_dm_attributes_full(const anjay_dm_internal_attrs_t *attrs) { - return attrs->standard.min_period >= 0 - && attrs->standard.max_period >= 0 + return attrs->standard.min_period >= 0 && attrs->standard.max_period >= 0 #ifdef WITH_CON_ATTR - && attrs->custom.data.con >= 0 + && attrs->custom.data.con >= 0 #endif ; } @@ -187,9 +185,10 @@ int _anjay_dm_effective_attrs(anjay_t *anjay, if (query->obj && *query->obj) { if (query->rid >= 0) { - result = _anjay_dm_resource_read_attrs( - anjay, query->obj, query->iid, (anjay_rid_t) query->rid, - query->ssid, out, NULL); + result = + _anjay_dm_resource_read_attrs(anjay, query->obj, query->iid, + (anjay_rid_t) query->rid, + query->ssid, out, NULL); if (result) { return result; } diff --git a/src/dm/dm_attributes.h b/src/dm/dm_attributes.h index 726773ea1..236ccb73c 100644 --- a/src/dm/dm_attributes.h +++ b/src/dm/dm_attributes.h @@ -41,7 +41,10 @@ typedef struct { int32_t rid; /** Server, for which Attributes shall be obtained. */ anjay_ssid_t ssid; - /** true if no matter what we are interested in inherited Server level attributes. */ + /** + * true if no matter what we are interested in inherited Server level + * attributes. + */ bool with_server_level_attrs; } anjay_dm_attrs_query_details_t; @@ -53,18 +56,22 @@ typedef struct { * Resource and/or Instance is present - caller must ensure that it is indeed * the case. * - * Attribute inheritance logic (assuming Resource and Instance ids are provided): + * Attribute inheritance logic (assuming Resource and Instance ids are + * provided): + * * 0. Set *out to ANJAY_DM_ATTRIBS_EMPTY. * 1. Read Resource attributes and combine them with *out attributes. * 2. Read Instance attributes and combine them with *out attributes. * 3. Read Object attributes and combine them with *out attributes. - * 4. (If with_server_level_attrs is set) Read Server attributes and combine them - * with *out attributes. + * 4. (If with_server_level_attrs is set) Read Server attributes and combine + * them with *out attributes. * * Additional information: - * If any step from above fails, then the function returns negative value. - * If @p query->rid is negative, then attributes of the Resource are not queried. - * If @p query->iid is ANJAY_IID_INVALID, then attributes of the Instance are not queried. + * - If any step from above fails, then the function returns negative value. + * - If @p query->rid is negative, then attributes of the Resource are not + * queried. + * - If @p query->iid is ANJAY_IID_INVALID, then attributes of the Instance + * are not queried. * * @param anjay ANJAY object to operate on. * @param query Query details. diff --git a/src/dm/dm_execute.c b/src/dm/dm_execute.c index 215a4e660..2de6c780e 100644 --- a/src/dm/dm_execute.c +++ b/src/dm/dm_execute.c @@ -15,17 +15,18 @@ */ #include -#include -#include -#include #include +#include +#include +#include #include "dm_execute.h" VISIBILITY_SOURCE_BEGIN -static anjay_execute_state_t state_read_value(anjay_execute_ctx_t* ctx, int ch); -static anjay_execute_state_t state_read_argument(anjay_execute_ctx_t* ctx, int ch); +static anjay_execute_state_t state_read_value(anjay_execute_ctx_t *ctx, int ch); +static anjay_execute_state_t state_read_argument(anjay_execute_ctx_t *ctx, + int ch); static int next_char(anjay_execute_ctx_t *ctx) { if (ctx->end_of_message) { @@ -39,7 +40,7 @@ static int next_char(anjay_execute_ctx_t *ctx) { } else if (result == 0) { ctx->end_of_message = true; } - return (int)(uint8_t)buf[0]; + return (int) (uint8_t) buf[0]; } static bool is_arg_separator(int byte) { @@ -52,10 +53,12 @@ static bool is_value_delimiter(int byte) { static bool is_value(int byte) { /* See OMA Specification Execute section, for more details. */ + // clang-format off return byte == '!' || (byte >= 0x23 && byte <= 0x26) || (byte >= 0x28 && byte <= 0x5b) || (byte >= 0x5d && byte <= 0x7e); + // clang-format on } static bool is_value_assignment(int byte) { @@ -128,12 +131,12 @@ ssize_t anjay_execute_get_arg_value(anjay_execute_ctx_t *ctx, } ssize_t read_bytes = 0; - while ((size_t) read_bytes < buf_size-1) { + while ((size_t) read_bytes < buf_size - 1) { int ch = next_char(ctx); ctx->state = state_read_value(ctx, ch); if (ctx->state == STATE_READ_VALUE) { - out_buf[read_bytes++] = (char)ch; + out_buf[read_bytes++] = (char) ch; } else { break; } @@ -166,7 +169,7 @@ void _anjay_execute_ctx_destroy(anjay_execute_ctx_t **ctx) { static anjay_execute_state_t expect_separator_or_eof(anjay_execute_ctx_t *ctx, int ch) { - (void)ctx; + (void) ctx; if (is_arg_separator(ch)) { return STATE_FINISHED_READING_ARGUMENT; } else if (ch == EOF) { @@ -186,7 +189,7 @@ static anjay_execute_state_t state_read_value(anjay_execute_ctx_t *ctx, } static anjay_execute_state_t expect_value(anjay_execute_ctx_t *ctx, int ch) { - (void)ctx; + (void) ctx; if (is_value_delimiter(ch)) { return STATE_READ_VALUE; } else { diff --git a/src/dm/dm_handlers.c b/src/dm/dm_handlers.c index 32263856a..25944be6f 100644 --- a/src/dm/dm_handlers.c +++ b/src/dm/dm_handlers.c @@ -18,15 +18,14 @@ #include -#include "../utils_core.h" #include "../anjay_core.h" +#include "../utils_core.h" VISIBILITY_SOURCE_BEGIN #define dm_log(...) _anjay_log(anjay_dm, __VA_ARGS__) -static bool has_handler(const anjay_dm_handlers_t *def, - size_t handler_offset) { +static bool has_handler(const anjay_dm_handlers_t *def, size_t handler_offset) { typedef void (*func_ptr_t)(void); return *(AVS_APPLY_OFFSET(func_ptr_t, def, handler_offset)); } @@ -92,40 +91,42 @@ bool _anjay_dm_handler_implemented(anjay_t *anjay, return get_handler(anjay, obj_ptr, current_module, handler_offset) != NULL; } -#define CHECKED_TAIL_CALL_HANDLER(Anjay, ObjPtr, Current, HandlerName, ...) \ - do { \ - const anjay_dm_handlers_t *handler = \ - get_handler((Anjay), (ObjPtr), (Current), \ - offsetof(anjay_dm_handlers_t, HandlerName)); \ - if (handler) { \ - return handler->HandlerName(__VA_ARGS__); \ - } else { \ +#define CHECKED_TAIL_CALL_HANDLER(Anjay, ObjPtr, Current, HandlerName, ...) \ + do { \ + const anjay_dm_handlers_t *handler = \ + get_handler((Anjay), (ObjPtr), (Current), \ + offsetof(anjay_dm_handlers_t, HandlerName)); \ + if (handler) { \ + return handler->HandlerName(__VA_ARGS__); \ + } else { \ anjay_log(ERROR, #HandlerName " handler not set for object /%u", \ - (*(ObjPtr))->oid); \ - return ANJAY_ERR_METHOD_NOT_ALLOWED; \ - } \ + (*(ObjPtr))->oid); \ + return ANJAY_ERR_METHOD_NOT_ALLOWED; \ + } \ } while (0) -int _anjay_dm_object_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - anjay_dm_internal_attrs_t *out, - const anjay_dm_module_t *current_module) { +int _anjay_dm_object_read_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + anjay_dm_internal_attrs_t *out, + const anjay_dm_module_t *current_module) { dm_log(TRACE, "object_read_default_attrs /%u", (*obj_ptr)->oid); CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - object_read_default_attrs, - anjay, obj_ptr, ssid, &out->standard); + object_read_default_attrs, anjay, obj_ptr, ssid, + &out->standard); } -int _anjay_dm_object_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - const anjay_dm_internal_attrs_t *attrs, - const anjay_dm_module_t *current_module) { +int _anjay_dm_object_write_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + const anjay_dm_internal_attrs_t *attrs, + const anjay_dm_module_t *current_module) { dm_log(TRACE, "object_write_default_attrs /%u", (*obj_ptr)->oid); CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - object_write_default_attrs, - anjay, obj_ptr, ssid, &attrs->standard); + object_write_default_attrs, anjay, obj_ptr, ssid, + &attrs->standard); } int _anjay_dm_instance_it(anjay_t *anjay, @@ -134,8 +135,8 @@ int _anjay_dm_instance_it(anjay_t *anjay, void **cookie, const anjay_dm_module_t *current_module) { dm_log(TRACE, "instance_it /%u", (*obj_ptr)->oid); - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - instance_it, anjay, obj_ptr, out, cookie); + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, instance_it, + anjay, obj_ptr, out, cookie); } int _anjay_dm_instance_reset(anjay_t *anjay, @@ -147,8 +148,8 @@ int _anjay_dm_instance_reset(anjay_t *anjay, if (result) { return result; } - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - instance_reset, anjay, obj_ptr, iid); + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, instance_reset, + anjay, obj_ptr, iid); } int _anjay_dm_instance_present(anjay_t *anjay, @@ -156,8 +157,8 @@ int _anjay_dm_instance_present(anjay_t *anjay, anjay_iid_t iid, const anjay_dm_module_t *current_module) { dm_log(TRACE, "instance_present /%u/%u", (*obj_ptr)->oid, iid); - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - instance_present, anjay, obj_ptr, iid); + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, instance_present, + anjay, obj_ptr, iid); } int _anjay_dm_instance_create(anjay_t *anjay, @@ -170,8 +171,8 @@ int _anjay_dm_instance_create(anjay_t *anjay, if (result) { return result; } - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - instance_create, anjay, obj_ptr, inout_iid, ssid); + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, instance_create, + anjay, obj_ptr, inout_iid, ssid); } int _anjay_dm_instance_remove(anjay_t *anjay, @@ -183,40 +184,42 @@ int _anjay_dm_instance_remove(anjay_t *anjay, if (result) { return result; } - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - instance_remove, anjay, obj_ptr, iid); + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, instance_remove, + anjay, obj_ptr, iid); } -int _anjay_dm_instance_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - anjay_dm_internal_attrs_t *out, - const anjay_dm_module_t *current_module) { +int _anjay_dm_instance_read_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + anjay_dm_internal_attrs_t *out, + const anjay_dm_module_t *current_module) { dm_log(TRACE, "instance_read_default_attrs /%u/%u", (*obj_ptr)->oid, iid); CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - instance_read_default_attrs, - anjay, obj_ptr, iid, ssid, &out->standard); + instance_read_default_attrs, anjay, obj_ptr, iid, + ssid, &out->standard); } -int _anjay_dm_instance_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - const anjay_dm_internal_attrs_t *attrs, - const anjay_dm_module_t *current_module) { +int _anjay_dm_instance_write_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + const anjay_dm_internal_attrs_t *attrs, + const anjay_dm_module_t *current_module) { dm_log(TRACE, "instance_write_default_attrs /%u/%u", (*obj_ptr)->oid, iid); CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - instance_write_default_attrs, - anjay, obj_ptr, iid, ssid, &attrs->standard); + instance_write_default_attrs, anjay, obj_ptr, iid, + ssid, &attrs->standard); } -int -_anjay_dm_resource_supported_and_present(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - const anjay_dm_module_t *current_module) { +int _anjay_dm_resource_supported_and_present( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + const anjay_dm_module_t *current_module) { if (_anjay_dm_resource_supported(obj_ptr, rid)) { return _anjay_dm_resource_present(anjay, obj_ptr, iid, rid, current_module); @@ -230,8 +233,8 @@ int _anjay_dm_resource_present(anjay_t *anjay, anjay_rid_t rid, const anjay_dm_module_t *current_module) { anjay_log(TRACE, "resource_present /%u/%u/%u", (*obj_ptr)->oid, iid, rid); - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - resource_present, anjay, obj_ptr, iid, rid); + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, resource_present, + anjay, obj_ptr, iid, rid); } bool _anjay_dm_resource_supported(const anjay_dm_object_def_t *const *obj_ptr, @@ -261,12 +264,12 @@ int _anjay_dm_resource_operations(anjay_t *anjay, if (!_anjay_dm_handler_implemented(anjay, obj_ptr, current_module, offsetof(anjay_dm_handlers_t, resource_operations))) { - anjay_log(TRACE, "resource_operations for /%u not implemented - " - "assumed all operations supported", + anjay_log(TRACE, + "resource_operations for /%u not implemented - " + "assumed all operations supported", (*obj_ptr)->oid); - *out = ANJAY_DM_RESOURCE_OP_BIT_R - | ANJAY_DM_RESOURCE_OP_BIT_W - | ANJAY_DM_RESOURCE_OP_BIT_E; + *out = ANJAY_DM_RESOURCE_OP_BIT_R | ANJAY_DM_RESOURCE_OP_BIT_W + | ANJAY_DM_RESOURCE_OP_BIT_E; return 0; } *out = ANJAY_DM_RESOURCE_OP_NONE; @@ -281,8 +284,8 @@ int _anjay_dm_resource_read(anjay_t *anjay, anjay_output_ctx_t *ctx, const anjay_dm_module_t *current_module) { anjay_log(TRACE, "resource_read /%u/%u/%u", (*obj_ptr)->oid, iid, rid); - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - resource_read, anjay, obj_ptr, iid, rid, ctx); + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, resource_read, + anjay, obj_ptr, iid, rid, ctx); } int _anjay_dm_resource_write(anjay_t *anjay, @@ -296,8 +299,8 @@ int _anjay_dm_resource_write(anjay_t *anjay, if (result) { return result; } - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - resource_write, anjay, obj_ptr, iid, rid, ctx); + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, resource_write, + anjay, obj_ptr, iid, rid, ctx); } int _anjay_dm_resource_execute(anjay_t *anjay, @@ -307,8 +310,7 @@ int _anjay_dm_resource_execute(anjay_t *anjay, anjay_execute_ctx_t *execute_ctx, const anjay_dm_module_t *current_module) { anjay_log(TRACE, "resource_execute /%u/%u/%u", (*obj_ptr)->oid, iid, rid); - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - resource_execute, + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, resource_execute, anjay, obj_ptr, iid, rid, execute_ctx); } @@ -325,8 +327,8 @@ int _anjay_dm_resource_dim(anjay_t *anjay, (*obj_ptr)->oid); return ANJAY_ERR_METHOD_NOT_ALLOWED; } - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - resource_dim, anjay, obj_ptr, iid, rid); + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, resource_dim, + anjay, obj_ptr, iid, rid); } int _anjay_dm_resource_read_attrs(anjay_t *anjay, @@ -336,10 +338,11 @@ int _anjay_dm_resource_read_attrs(anjay_t *anjay, anjay_ssid_t ssid, anjay_dm_internal_res_attrs_t *out, const anjay_dm_module_t *current_module) { - anjay_log(TRACE, "resource_read_attrs /%u/%u/%u", (*obj_ptr)->oid, iid, rid); + anjay_log(TRACE, "resource_read_attrs /%u/%u/%u", (*obj_ptr)->oid, iid, + rid); CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - resource_read_attrs, - anjay, obj_ptr, iid, rid, ssid, &out->standard); + resource_read_attrs, anjay, obj_ptr, iid, rid, + ssid, &out->standard); } int _anjay_dm_resource_write_attrs(anjay_t *anjay, @@ -349,38 +352,42 @@ int _anjay_dm_resource_write_attrs(anjay_t *anjay, anjay_ssid_t ssid, const anjay_dm_internal_res_attrs_t *attrs, const anjay_dm_module_t *current_module) { - anjay_log(TRACE, "resource_write_attrs /%u/%u/%u", (*obj_ptr)->oid, iid, rid); + anjay_log(TRACE, "resource_write_attrs /%u/%u/%u", (*obj_ptr)->oid, iid, + rid); CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - resource_write_attrs, - anjay, obj_ptr, iid, rid, ssid, &attrs->standard); + resource_write_attrs, anjay, obj_ptr, iid, rid, + ssid, &attrs->standard); } static int call_transaction_begin(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, const anjay_dm_module_t *current_module) { anjay_log(TRACE, "begin_object_transaction /%u", (*obj_ptr)->oid); - CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, - transaction_begin, anjay, obj_ptr); + CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, transaction_begin, + anjay, obj_ptr); } -int _anjay_dm_delegate_transaction_begin(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - const anjay_dm_module_t *current_module) { +int _anjay_dm_delegate_transaction_begin( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + const anjay_dm_module_t *current_module) { assert(current_module); return call_transaction_begin(anjay, obj_ptr, current_module); } -static int call_transaction_validate(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - const anjay_dm_module_t *current_module) { +static int +call_transaction_validate(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + const anjay_dm_module_t *current_module) { anjay_log(TRACE, "validate_object /%u", (*obj_ptr)->oid); CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, transaction_validate, anjay, obj_ptr); } -int _anjay_dm_delegate_transaction_validate(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - const anjay_dm_module_t *current_module) { +int _anjay_dm_delegate_transaction_validate( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + const anjay_dm_module_t *current_module) { assert(current_module); return call_transaction_validate(anjay, obj_ptr, current_module); } @@ -393,24 +400,27 @@ static int call_transaction_commit(anjay_t *anjay, transaction_commit, anjay, obj_ptr); } -int _anjay_dm_delegate_transaction_commit(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - const anjay_dm_module_t *current_module) { +int _anjay_dm_delegate_transaction_commit( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + const anjay_dm_module_t *current_module) { assert(current_module); return call_transaction_commit(anjay, obj_ptr, current_module); } -static int call_transaction_rollback(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - const anjay_dm_module_t *current_module) { +static int +call_transaction_rollback(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + const anjay_dm_module_t *current_module) { anjay_log(TRACE, "rollback_object /%u", (*obj_ptr)->oid); CHECKED_TAIL_CALL_HANDLER(anjay, obj_ptr, current_module, transaction_rollback, anjay, obj_ptr); } -int _anjay_dm_delegate_transaction_rollback(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - const anjay_dm_module_t *current_module) { +int _anjay_dm_delegate_transaction_rollback( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + const anjay_dm_module_t *current_module) { assert(current_module); return call_transaction_rollback(anjay, obj_ptr, current_module); } @@ -461,14 +471,14 @@ static int commit_or_rollback_object(anjay_t *anjay, int result; if (predicate) { if ((result = call_transaction_rollback(anjay, obj, NULL))) { - anjay_log(ERROR, "cannot rollback transaction on /%u, " - "object may be left in undefined state", + anjay_log(ERROR, + "cannot rollback transaction on /%u, " + "object may be left in undefined state", (*obj)->oid); return result; } } else if ((result = call_transaction_commit(anjay, obj, NULL))) { - anjay_log(ERROR, "cannot commit transaction on /%u", - (*obj)->oid); + anjay_log(ERROR, "cannot commit transaction on /%u", (*obj)->oid); predicate = result; } return predicate; @@ -488,8 +498,8 @@ int _anjay_dm_transaction_validate(anjay_t *anjay) { return 0; } -int -_anjay_dm_transaction_finish_without_validation(anjay_t *anjay, int result) { +int _anjay_dm_transaction_finish_without_validation(anjay_t *anjay, + int result) { anjay_log(TRACE, "transaction_finish"); assert(anjay->transaction_state.depth > 0); if (--anjay->transaction_state.depth != 0) { @@ -498,9 +508,7 @@ _anjay_dm_transaction_finish_without_validation(anjay_t *anjay, int result) { int final_result = result; AVS_LIST_CLEAR(&anjay->transaction_state.objs_in_transaction) { int commit_result = commit_or_rollback_object( - anjay, - *anjay->transaction_state.objs_in_transaction, - result); + anjay, *anjay->transaction_state.objs_in_transaction, result); if (!final_result && commit_result) { final_result = commit_result; } @@ -529,10 +537,12 @@ int anjay_dm_instance_it_SINGLE(anjay_t *anjay, return 0; } -int anjay_dm_instance_present_SINGLE(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid) { - (void) anjay; (void) obj_ptr; +int anjay_dm_instance_present_SINGLE( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid) { + (void) anjay; + (void) obj_ptr; return (iid == 0); } @@ -540,7 +550,9 @@ int anjay_dm_resource_present_TRUE(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid, anjay_rid_t rid) { - (void) anjay; (void) obj_ptr; (void) iid, (void) rid; + (void) anjay; + (void) obj_ptr; + (void) iid, (void) rid; return 1; } diff --git a/src/dm/query.c b/src/dm/query.c index d0ecac461..8bfd3ee78 100644 --- a/src/dm/query.c +++ b/src/dm/query.c @@ -20,8 +20,8 @@ #include "query.h" -#include "../dm_core.h" #include "../anjay_core.h" +#include "../dm_core.h" VISIBILITY_SOURCE_BEGIN @@ -35,7 +35,7 @@ static int find_server_iid_handler(anjay_t *anjay, anjay_iid_t iid, void *args_) { (void) obj; - find_iid_args_t *args = (find_iid_args_t*)args_; + find_iid_args_t *args = (find_iid_args_t *) args_; int64_t ssid; const anjay_uri_path_t ssid_path = MAKE_RESOURCE_PATH(ANJAY_DM_OID_SERVER, iid, @@ -61,10 +61,9 @@ int _anjay_find_server_iid(anjay_t *anjay, const anjay_dm_object_def_t *const *obj = _anjay_dm_find_object_by_oid(anjay, ANJAY_DM_OID_SERVER); - if (ssid == ANJAY_SSID_ANY - || ssid == ANJAY_SSID_BOOTSTRAP - || _anjay_dm_foreach_instance(anjay, obj, - find_server_iid_handler, &args) + if (ssid == ANJAY_SSID_ANY || ssid == ANJAY_SSID_BOOTSTRAP + || _anjay_dm_foreach_instance(anjay, obj, find_server_iid_handler, + &args) || args.out_iid == ANJAY_IID_INVALID) { return -1; } @@ -138,13 +137,13 @@ int _anjay_ssid_from_security_iid(anjay_t *anjay, MAKE_RESOURCE_PATH(ANJAY_DM_OID_SECURITY, security_iid, ANJAY_DM_RID_SECURITY_SSID); - if (_anjay_dm_res_read_i64(anjay, &path, &_ssid) - || _ssid <= 0 || _ssid > UINT16_MAX) { + if (_anjay_dm_res_read_i64(anjay, &path, &_ssid) || _ssid <= 0 + || _ssid > UINT16_MAX) { anjay_log(ERROR, "could not get Short Server ID"); return -1; } - *out_ssid = (uint16_t)_ssid; + *out_ssid = (uint16_t) _ssid; return 0; } @@ -165,8 +164,7 @@ bool _anjay_is_bootstrap_security_instance(anjay_t *anjay, #endif avs_time_duration_t -_anjay_disable_timeout_from_server_iid(anjay_t *anjay, - anjay_iid_t server_iid) { +_anjay_disable_timeout_from_server_iid(anjay_t *anjay, anjay_iid_t server_iid) { static const int32_t DEFAULT_DISABLE_TIMEOUT_S = NUM_SECONDS_IN_A_DAY; int64_t timeout_s; diff --git a/src/dm/query.h b/src/dm/query.h index d3edaca96..1b56082bd 100644 --- a/src/dm/query.h +++ b/src/dm/query.h @@ -41,12 +41,11 @@ int _anjay_ssid_from_security_iid(anjay_t *anjay, bool _anjay_is_bootstrap_security_instance(anjay_t *anjay, anjay_iid_t security_iid); #else -#define _anjay_is_bootstrap_security_instance(...) (false) +# define _anjay_is_bootstrap_security_instance(...) (false) #endif avs_time_duration_t -_anjay_disable_timeout_from_server_iid(anjay_t *anjay, - anjay_iid_t server_iid); +_anjay_disable_timeout_from_server_iid(anjay_t *anjay, anjay_iid_t server_iid); VISIBILITY_PRIVATE_HEADER_END diff --git a/src/dm_core.c b/src/dm_core.c index e5050d104..92fba3006 100644 --- a/src/dm_core.c +++ b/src/dm_core.c @@ -19,8 +19,8 @@ #include #include #include -#include #include +#include #include #include @@ -34,22 +34,24 @@ #include "coap/content_format.h" -#include "dm_core.h" +#include "access_control_utils.h" +#include "anjay_core.h" #include "dm/discover.h" #include "dm/dm_execute.h" #include "dm/query.h" +#include "dm_core.h" #include "io_core.h" #include "observe/observe_core.h" #include "utils_core.h" -#include "anjay_core.h" -#include "access_control_utils.h" VISIBILITY_SOURCE_BEGIN static int validate_supported_rids(const anjay_dm_object_def_t *obj_def) { if (obj_def->supported_rids.count != 0 && !obj_def->supported_rids.rids) { - anjay_log(ERROR, "/%u: supported_rids.count is nonzero, but " - "supported_rids.rids in is NULL", obj_def->oid); + anjay_log(ERROR, + "/%u: supported_rids.count is nonzero, but " + "supported_rids.rids in is NULL", + obj_def->oid); return -1; } @@ -57,7 +59,7 @@ static int validate_supported_rids(const anjay_dm_object_def_t *obj_def) { if (obj_def->supported_rids.rids[i] <= obj_def->supported_rids.rids[i - 1]) { anjay_log(ERROR, "supported_rids in /%u is not strictly ascending", - obj_def->oid); + obj_def->oid); return -1; } } @@ -74,7 +76,8 @@ static int validate_version(const anjay_dm_object_def_t *obj_def) { unsigned major, minor; char dummy; if (sscanf(obj_def->version, "%u.%u%c", &major, &minor, &dummy) != 2) { - anjay_log(ERROR, "invalid Object /%u version format (expected X.Y, " + anjay_log(ERROR, + "invalid Object /%u version format (expected X.Y, " "where X and Y are unsigned integers): %s", (unsigned) obj_def->oid, obj_def->version); return -1; @@ -109,8 +112,7 @@ int anjay_register_object(anjay_t *anjay, return -1; } - if (validate_supported_rids(*def_ptr) - || validate_version(*def_ptr)) { + if (validate_supported_rids(*def_ptr) || validate_version(*def_ptr)) { return -1; } @@ -173,8 +175,9 @@ int anjay_unregister_object(anjay_t *anjay, return -1; } if (**obj_iter != def_ptr) { - anjay_log(ERROR, "object %" PRIu16 " that is registered is not " - "the same as the object passed for unregister", + anjay_log(ERROR, + "object %" PRIu16 " that is registered is not " + "the same as the object passed for unregister", (*def_ptr)->oid); return -1; } @@ -186,8 +189,10 @@ int anjay_unregister_object(anjay_t *anjay, if (_anjay_notify_queue_instance_set_unknown_change(¬ify, (*def_ptr)->oid) || _anjay_notify_flush(anjay, ¬ify)) { - anjay_log(WARNING, "could not perform notifications about " - "removed object %" PRIu16, (*def_ptr)->oid); + anjay_log(WARNING, + "could not perform notifications about " + "removed object %" PRIu16, + (*def_ptr)->oid); } remove_oid_from_notify_queue(&anjay->scheduled_notify.queue, @@ -244,17 +249,26 @@ input_ctx_for_action(anjay_request_action_t action) { static uint8_t make_success_response_code(anjay_request_action_t action) { switch (action) { - case ANJAY_ACTION_READ: return AVS_COAP_CODE_CONTENT; - case ANJAY_ACTION_DISCOVER: return AVS_COAP_CODE_CONTENT; - case ANJAY_ACTION_WRITE: return AVS_COAP_CODE_CHANGED; - case ANJAY_ACTION_WRITE_UPDATE: return AVS_COAP_CODE_CHANGED; - case ANJAY_ACTION_WRITE_ATTRIBUTES: return AVS_COAP_CODE_CHANGED; - case ANJAY_ACTION_EXECUTE: return AVS_COAP_CODE_CHANGED; - case ANJAY_ACTION_CREATE: return AVS_COAP_CODE_CREATED; - case ANJAY_ACTION_DELETE: return AVS_COAP_CODE_DELETED; - default: break; - } - return (uint8_t)(-ANJAY_ERR_INTERNAL); + case ANJAY_ACTION_READ: + return AVS_COAP_CODE_CONTENT; + case ANJAY_ACTION_DISCOVER: + return AVS_COAP_CODE_CONTENT; + case ANJAY_ACTION_WRITE: + return AVS_COAP_CODE_CHANGED; + case ANJAY_ACTION_WRITE_UPDATE: + return AVS_COAP_CODE_CHANGED; + case ANJAY_ACTION_WRITE_ATTRIBUTES: + return AVS_COAP_CODE_CHANGED; + case ANJAY_ACTION_EXECUTE: + return AVS_COAP_CODE_CHANGED; + case ANJAY_ACTION_CREATE: + return AVS_COAP_CODE_CREATED; + case ANJAY_ACTION_DELETE: + return AVS_COAP_CODE_DELETED; + default: + break; + } + return (uint8_t) (-ANJAY_ERR_INTERNAL); } static int prepare_input_context(avs_stream_abstract_t *stream, @@ -278,33 +292,27 @@ const char *_anjay_debug_make_path__(char *buffer, size_t buffer_size, const anjay_uri_path_t *uri) { assert(uri); - - const uint16_t *ids[] = { - _anjay_uri_path_has_oid(uri) ? &uri->oid : NULL, - _anjay_uri_path_has_iid(uri) ? &uri->iid : NULL, - _anjay_uri_path_has_rid(uri) ? &uri->rid : NULL, - NULL - }; - const uint16_t **id_ptr = ids; - - size_t offset = 0; - do { - ssize_t result; - if (*id_ptr) { - result = avs_simple_snprintf(buffer + offset, buffer_size - offset, - "/%u", **id_ptr); - } else { - result = avs_simple_snprintf(buffer, buffer_size, "%s", "/"); - } - if (result < 0) { - AVS_UNREACHABLE("should never happen"); - return ""; - } - - offset += (size_t) result; - ++id_ptr; - } while (*id_ptr); - + ssize_t result; + switch (uri->type) { + case ANJAY_PATH_ROOT: + result = avs_simple_snprintf(buffer, buffer_size, "/"); + break; + case ANJAY_PATH_OBJECT: + result = avs_simple_snprintf(buffer, buffer_size, "/%u", uri->oid); + break; + case ANJAY_PATH_INSTANCE: + result = avs_simple_snprintf(buffer, buffer_size, "/%u/%u", uri->oid, + uri->iid); + break; + case ANJAY_PATH_RESOURCE: + result = avs_simple_snprintf(buffer, buffer_size, "/%u/%u/%u", uri->oid, + uri->iid, uri->rid); + break; + } + if (result < 0) { + AVS_UNREACHABLE("should never happen"); + return ""; + } return buffer; } @@ -392,12 +400,10 @@ static int read_instance(anjay_t *anjay, int result = ensure_resource_present(anjay, obj, iid, (*obj)->supported_rids.rids[i]); if (!result) { - result = read_present_resource(anjay, obj, iid, - (*obj)->supported_rids.rids[i], - out_ctx); + result = read_present_resource( + anjay, obj, iid, (*obj)->supported_rids.rids[i], out_ctx); } - if (result - && result != ANJAY_ERR_METHOD_NOT_ALLOWED + if (result && result != ANJAY_ERR_METHOD_NOT_ALLOWED && result != ANJAY_ERR_NOT_FOUND) { return result; } @@ -438,9 +444,8 @@ static int read_object(anjay_t *anjay, }; while (!result - && !(result = _anjay_dm_instance_it(anjay, obj, &iid, &cookie, - NULL)) - && iid != ANJAY_IID_INVALID) { + && !(result = _anjay_dm_instance_it(anjay, obj, &iid, &cookie, NULL)) + && iid != ANJAY_IID_INVALID) { info.iid = iid; if (!_anjay_access_control_action_allowed(anjay, &info)) { continue; @@ -504,8 +509,8 @@ static int dm_read(anjay_t *anjay, if (!_anjay_access_control_action_allowed(anjay, &info)) { result = ANJAY_ERR_UNAUTHORIZED; } else if (_anjay_uri_path_has_rid(&details->uri)) { - result = read_resource(anjay, obj, details->uri.iid, details->uri.rid, - out_ctx); + result = read_resource(anjay, obj, details->uri.iid, + details->uri.rid, out_ctx); } else { result = read_instance(anjay, obj, details->uri.iid, out_ctx); } @@ -519,7 +524,8 @@ static int dm_read(anjay_t *anjay, if (result) { return result; } else if (finish_result == ANJAY_OUTCTXERR_ANJAY_RET_NOT_CALLED) { - anjay_log(ERROR, "unable to determine resource type: anjay_ret_* not " + anjay_log(ERROR, + "unable to determine resource type: anjay_ret_* not " "called during successful resource_read handler call for %s", ANJAY_DEBUG_MAKE_PATH(&details->uri)); return ANJAY_ERR_INTERNAL; @@ -535,12 +541,10 @@ static void build_observe_key(anjay_t *anjay, result->connection.ssid = _anjay_dm_current_ssid(anjay); result->connection.type = anjay->current_connection.conn_type; result->oid = request->uri.oid; - result->iid = _anjay_uri_path_has_iid(&request->uri) - ? request->uri.iid - : ANJAY_IID_INVALID; - result->rid = _anjay_uri_path_has_rid(&request->uri) - ? request->uri.rid - : ANJAY_RID_EMPTY; + result->iid = _anjay_uri_path_has_iid(&request->uri) ? request->uri.iid + : ANJAY_IID_INVALID; + result->rid = _anjay_uri_path_has_rid(&request->uri) ? request->uri.rid + : ANJAY_RID_EMPTY; result->format = request->requested_format; } @@ -571,8 +575,8 @@ ssize_t _anjay_dm_read_for_observe(anjay_t *anjay, avs_stream_outbuf_set_buffer(&out.outbuf, buffer, size); int out_ctx_errno = 0; anjay_output_ctx_t *out_ctx = - dm_observe_spawn_ctx((avs_stream_abstract_t *) &out, - &out_ctx_errno, details, out_numeric); + dm_observe_spawn_ctx((avs_stream_abstract_t *) &out, &out_ctx_errno, + details, out_numeric); if (!out_ctx) { return out_ctx_errno ? out_ctx_errno : ANJAY_ERR_INTERNAL; } @@ -602,9 +606,10 @@ static int dm_observe(anjay_t *anjay, } anjay_observe_key_t key; build_observe_key(anjay, &key, request); - int put_entry_result = _anjay_observe_put_entry( - anjay, &key, &observe_details, request_identity, numeric, - buf, (size_t) size); + int put_entry_result = + _anjay_observe_put_entry(anjay, &key, &observe_details, + request_identity, numeric, buf, + (size_t) size); if (put_entry_result) { // we are unable to create the observation entry, but we can still // process the request as usual; compare RFC 7641, section 4.1 @@ -613,8 +618,8 @@ static int dm_observe(anjay_t *anjay, int result; if ((result = _anjay_coap_stream_setup_response(anjay->comm_stream, &observe_details)) - || (result = avs_stream_write(anjay->comm_stream, - buf, (size_t) size))) { + || (result = avs_stream_write(anjay->comm_stream, buf, + (size_t) size))) { if (!put_entry_result) { _anjay_observe_remove_entry(anjay, &key); } @@ -622,8 +627,8 @@ static int dm_observe(anjay_t *anjay, return result; } #else // WITH_OBSERVE -#define dm_observe(...) (anjay_log(ERROR, "Observe support disabled"), \ - ANJAY_ERR_BAD_OPTION) +# define dm_observe(...) \ + (anjay_log(ERROR, "Observe support disabled"), ANJAY_ERR_BAD_OPTION) #endif // WITH_OBSERVE static int dm_read_or_observe(anjay_t *anjay, @@ -643,8 +648,9 @@ static int dm_read_or_observe(anjay_t *anjay, const anjay_dm_read_args_t read_args = REQUEST_TO_DM_READ_ARGS(anjay, request); int out_ctx_errno = 0; - anjay_output_ctx_t *out_ctx = dm_read_spawn_ctx( - anjay->comm_stream, &out_ctx_errno, &read_args); + anjay_output_ctx_t *out_ctx = + dm_read_spawn_ctx(anjay->comm_stream, &out_ctx_errno, + &read_args); if (!out_ctx) { return out_ctx_errno ? out_ctx_errno : ANJAY_ERR_INTERNAL; } @@ -657,21 +663,19 @@ static int dm_read_or_observe(anjay_t *anjay, } } -static inline bool resource_specific_request_attrs_empty( - const anjay_request_attributes_t *attrs) { - return !attrs->has_greater_than - && !attrs->has_less_than - && !attrs->has_step; +static inline bool +resource_specific_request_attrs_empty(const anjay_request_attributes_t *attrs) { + return !attrs->has_greater_than && !attrs->has_less_than + && !attrs->has_step; } static inline bool request_attrs_empty(const anjay_request_attributes_t *attrs) { - return !attrs->has_min_period - && !attrs->has_max_period + return !attrs->has_min_period && !attrs->has_max_period #ifdef WITH_CON_ATTR - && !attrs->custom.has_con + && !attrs->custom.has_con #endif - && resource_specific_request_attrs_empty(attrs); + && resource_specific_request_attrs_empty(attrs); } #ifdef WITH_DISCOVER @@ -681,7 +685,8 @@ static int dm_discover(anjay_t *anjay, anjay_log(DEBUG, "Discover %s", ANJAY_DEBUG_MAKE_PATH(&request->uri)); /* Access Control check is omitted here, because dm_discover is always * allowed. */ - int result = _anjay_coap_stream_setup_response(anjay->comm_stream, + int result = _anjay_coap_stream_setup_response( + anjay->comm_stream, &(anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = make_success_response_code(ANJAY_ACTION_DISCOVER), @@ -697,7 +702,8 @@ static int dm_discover(anjay_t *anjay, if (!(result = ensure_instance_present(anjay, obj, request->uri.iid))) { if (_anjay_uri_path_has_rid(&request->uri)) { if (!(result = ensure_resource_supported_and_present( - anjay, obj, request->uri.iid, request->uri.rid))) { + anjay, obj, request->uri.iid, + request->uri.rid))) { result = _anjay_discover_resource( anjay, obj, request->uri.iid, request->uri.rid); } @@ -716,19 +722,18 @@ static int dm_discover(anjay_t *anjay, return result; } #else // WITH_DISCOVER -#define dm_discover(anjay, obj, details) \ - (anjay_log(ERROR, "Not supported: Discover %s", \ +# define dm_discover(anjay, obj, details) \ + (anjay_log(ERROR, "Not supported: Discover %s", \ ANJAY_DEBUG_MAKE_PATH(&details->uri)), \ - ANJAY_ERR_NOT_IMPLEMENTED) + ANJAY_ERR_NOT_IMPLEMENTED) #endif // WITH_DISCOVER -static int -write_present_resource(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_input_ctx_t *in_ctx, - anjay_notify_queue_t *notify_queue) { +static int write_present_resource(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_input_ctx_t *in_ctx, + anjay_notify_queue_t *notify_queue) { if (!has_resource_operation_bit(anjay, obj, rid, ANJAY_DM_RESOURCE_OP_BIT_W)) { anjay_log(ERROR, "Write /%u/*/%u is not supported", (*obj)->oid, rid); @@ -736,8 +741,8 @@ write_present_resource(anjay_t *anjay, } int result = _anjay_dm_resource_write(anjay, obj, iid, rid, in_ctx, NULL); if (!result && notify_queue) { - result = _anjay_notify_queue_resource_change(notify_queue, - (*obj)->oid, iid, rid); + result = _anjay_notify_queue_resource_change(notify_queue, (*obj)->oid, + iid, rid); } return result; } @@ -776,8 +781,9 @@ static int write_instance_impl(anjay_t *anjay, if (!supported && hint == WRITE_INSTANCE_FAIL_ON_UNSUPPORTED) { return ANJAY_ERR_NOT_FOUND; } - if ((supported && (retval = write_present_resource(anjay, obj, iid, id, - in_ctx, notify))) + if ((supported + && (retval = write_present_resource(anjay, obj, iid, id, in_ctx, + notify))) || (retval = _anjay_input_next_entry(in_ctx))) { return retval; } @@ -799,9 +805,10 @@ static int write_instance(anjay_t *anjay, } if (type == ANJAY_ID_IID) { if (id != iid) { - anjay_log(WARNING, "Attempted Write on /%" PRIu16 " with " - "IID==%" PRIu16 " in CoAP Options but " - "IID==%" PRIu16 " in content header", + anjay_log(WARNING, + "Attempted Write on /%" PRIu16 " with " + "IID==%" PRIu16 " in CoAP Options but " + "IID==%" PRIu16 " in content header", (*obj)->oid, iid, id); return ANJAY_ERR_BAD_REQUEST; } @@ -813,7 +820,7 @@ static int write_instance(anjay_t *anjay, hint)) || (retval = _anjay_input_next_entry(in_ctx)) || (retval = _anjay_input_get_id(in_ctx, &type, &id)) - != ANJAY_GET_INDEX_END) { + != ANJAY_GET_INDEX_END) { return retval; } return 0; @@ -856,16 +863,16 @@ static int dm_write(anjay_t *anjay, } if (!retval) { - retval = write_resource(anjay, obj, uri->iid, uri->rid, - in_ctx, ¬ify_queue); + retval = write_resource(anjay, obj, uri->iid, uri->rid, in_ctx, + ¬ify_queue); } } else { if (action != ANJAY_ACTION_WRITE_UPDATE) { retval = _anjay_dm_instance_reset(anjay, obj, uri->iid, NULL); } if (!retval) { - retval = write_instance(anjay, obj, uri->iid, - in_ctx, ¬ify_queue, + retval = write_instance(anjay, obj, uri->iid, in_ctx, + ¬ify_queue, WRITE_INSTANCE_FAIL_ON_UNSUPPORTED); } } @@ -917,7 +924,7 @@ static bool resource_attrs_valid(const anjay_dm_internal_res_attrs_t *attrs) { if (!isnan(attrs->standard.less_than) && !isnan(attrs->standard.greater_than) && attrs->standard.less_than + 2 * step - >= attrs->standard.greater_than) { + >= attrs->standard.greater_than) { anjay_log(DEBUG, "Attempted to set attributes that fail the " "'lt + 2*st < gt' precondition"); return false; @@ -925,18 +932,19 @@ static bool resource_attrs_valid(const anjay_dm_internal_res_attrs_t *attrs) { return true; } -static int dm_write_resource_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj, - anjay_iid_t iid, - anjay_rid_t rid, - const anjay_request_attributes_t *attributes) { +static int +dm_write_resource_attrs(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj, + anjay_iid_t iid, + anjay_rid_t rid, + const anjay_request_attributes_t *attributes) { anjay_dm_internal_res_attrs_t attrs = ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY; int result = ensure_resource_supported_and_present(anjay, obj, iid, rid); if (!result) { - result = _anjay_dm_resource_read_attrs( - anjay, obj, iid, rid, _anjay_dm_current_ssid(anjay), - &attrs, NULL); + result = _anjay_dm_resource_read_attrs(anjay, obj, iid, rid, + _anjay_dm_current_ssid(anjay), + &attrs, NULL); } if (!result) { update_attrs(&attrs, attributes); @@ -944,17 +952,18 @@ static int dm_write_resource_attrs(anjay_t *anjay, result = ANJAY_ERR_BAD_REQUEST; } else { result = _anjay_dm_resource_write_attrs( - anjay, obj, iid, rid, _anjay_dm_current_ssid(anjay), - &attrs, NULL); + anjay, obj, iid, rid, _anjay_dm_current_ssid(anjay), &attrs, + NULL); } } return result; } -static int dm_write_instance_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj, - anjay_iid_t iid, - const anjay_request_attributes_t *attributes) { +static int +dm_write_instance_attrs(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj, + anjay_iid_t iid, + const anjay_request_attributes_t *attributes) { anjay_dm_internal_res_attrs_t attrs = ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY; int result = _anjay_dm_read_combined_instance_attrs( anjay, obj, iid, _anjay_dm_current_ssid(anjay), @@ -1048,12 +1057,12 @@ static int dm_execute(anjay_t *anjay, int retval = ensure_instance_present(anjay, obj, request->uri.iid); if (!retval) { - retval = ensure_resource_supported_and_present(anjay, obj, - request->uri.iid, request->uri.rid); + retval = ensure_resource_supported_and_present( + anjay, obj, request->uri.iid, request->uri.rid); } if (!retval) { if (!_anjay_access_control_action_allowed( - anjay, &REQUEST_TO_ACTION_INFO(anjay, request))) { + anjay, &REQUEST_TO_ACTION_INFO(anjay, request))) { return ANJAY_ERR_UNAUTHORIZED; } @@ -1064,9 +1073,10 @@ static int dm_execute(anjay_t *anjay, return ANJAY_ERR_METHOD_NOT_ALLOWED; } - anjay_execute_ctx_t* execute_ctx = _anjay_execute_ctx_create(in_ctx); - retval = _anjay_dm_resource_execute(anjay, obj, request->uri.iid, - request->uri.rid, execute_ctx, NULL); + anjay_execute_ctx_t *execute_ctx = _anjay_execute_ctx_create(in_ctx); + retval = + _anjay_dm_resource_execute(anjay, obj, request->uri.iid, + request->uri.rid, execute_ctx, NULL); _anjay_execute_ctx_destroy(&execute_ctx); } return retval; @@ -1110,24 +1120,27 @@ static int dm_create_inner(anjay_t *anjay, anjay_iid_t *new_iid_ptr, anjay_input_ctx_t *in_ctx) { anjay_iid_t proposed_iid = *new_iid_ptr; - int result = _anjay_dm_instance_create( - anjay, obj, new_iid_ptr, _anjay_dm_current_ssid(anjay), NULL); + int result = _anjay_dm_instance_create(anjay, obj, new_iid_ptr, + _anjay_dm_current_ssid(anjay), NULL); if (result || *new_iid_ptr == ANJAY_IID_INVALID) { - anjay_log(DEBUG, "Instance Create handler for object %" PRIu16 - " failed", (*obj)->oid); + anjay_log(DEBUG, + "Instance Create handler for object %" PRIu16 " failed", + (*obj)->oid); return result ? result : ANJAY_ERR_INTERNAL; } else if (proposed_iid != ANJAY_IID_INVALID - && *new_iid_ptr != proposed_iid) { - anjay_log(DEBUG, "Instance Create handler for object %" PRIu16 - " returned Instance %" PRIu16 " while %" PRIu16 - " was expected; removing", + && *new_iid_ptr != proposed_iid) { + anjay_log(DEBUG, + "Instance Create handler for object %" PRIu16 + " returned Instance %" PRIu16 " while %" PRIu16 + " was expected; removing", (*obj)->oid, *new_iid_ptr, proposed_iid); result = ANJAY_ERR_INTERNAL; } else if ((result = write_instance_impl( - anjay, obj, *new_iid_ptr, in_ctx, NULL, - WRITE_INSTANCE_IGNORE_UNSUPPORTED))) { - anjay_log(DEBUG, "Writing Resources for newly created " - "/%" PRIu16 "/%" PRIu16 "; removing", + anjay, obj, *new_iid_ptr, in_ctx, NULL, + WRITE_INSTANCE_IGNORE_UNSUPPORTED))) { + anjay_log(DEBUG, + "Writing Resources for newly created " + "/%" PRIu16 "/%" PRIu16 "; removing", (*obj)->oid, *new_iid_ptr); } return result; @@ -1146,8 +1159,10 @@ static int dm_create_with_explicit_iid(anjay_t *anjay, (*obj)->oid, *new_iid_ptr); return ANJAY_ERR_BAD_REQUEST; } else if (result) { - anjay_log(DEBUG, "Instance Present handler for /%" PRIu16 "/%" PRIu16 - " failed", (*obj)->oid, *new_iid_ptr); + anjay_log(DEBUG, + "Instance Present handler for /%" PRIu16 "/%" PRIu16 + " failed", + (*obj)->oid, *new_iid_ptr); return result; } anjay_input_ctx_t *nested_ctx = _anjay_input_nested_ctx(in_ctx); @@ -1176,15 +1191,15 @@ static int dm_create(anjay_t *anjay, assert(request->uri.type == ANJAY_PATH_OBJECT); if (!_anjay_access_control_action_allowed( - anjay, &REQUEST_TO_ACTION_INFO(anjay, request))) { + anjay, &REQUEST_TO_ACTION_INFO(anjay, request))) { return ANJAY_ERR_UNAUTHORIZED; } anjay_iid_t new_iid = ANJAY_IID_INVALID; anjay_id_type_t stream_first_id_type; uint16_t stream_first_id; - int result = _anjay_input_get_id(in_ctx, - &stream_first_id_type, &stream_first_id); + int result = _anjay_input_get_id(in_ctx, &stream_first_id_type, + &stream_first_id); if (!result && stream_first_id_type == ANJAY_ID_IID) { new_iid = stream_first_id; result = dm_create_with_explicit_iid(anjay, obj, &new_iid, in_ctx); @@ -1200,9 +1215,8 @@ static int dm_create(anjay_t *anjay, } if (!result) { anjay_notify_queue_t notify_queue = NULL; - (void) ((result = _anjay_notify_queue_instance_created(¬ify_queue, - request->uri.oid, - new_iid)) + (void) ((result = _anjay_notify_queue_instance_created( + ¬ify_queue, request->uri.oid, new_iid)) || (result = _anjay_notify_flush(anjay, ¬ify_queue))); } return result; @@ -1219,7 +1233,7 @@ static int dm_delete(anjay_t *anjay, int retval = ensure_instance_present(anjay, obj, request->uri.iid); if (!retval) { if (!_anjay_access_control_action_allowed( - anjay, &REQUEST_TO_ACTION_INFO(anjay, request))) { + anjay, &REQUEST_TO_ACTION_INFO(anjay, request))) { return ANJAY_ERR_UNAUTHORIZED; } retval = _anjay_dm_instance_remove(anjay, obj, request->uri.iid, NULL); @@ -1227,15 +1241,14 @@ static int dm_delete(anjay_t *anjay, if (!retval) { anjay_notify_queue_t notify_queue = NULL; (void) ((retval = _anjay_notify_queue_instance_removed( - ¬ify_queue, request->uri.oid, request->uri.iid)) + ¬ify_queue, request->uri.oid, request->uri.iid)) || (retval = _anjay_notify_flush(anjay, ¬ify_queue))); } return retval; } -static int -dm_cancel_observe(anjay_t *anjay, - const avs_coap_msg_identity_t *request_identity) { +static int dm_cancel_observe(anjay_t *anjay, + const avs_coap_msg_identity_t *request_identity) { (void) anjay; anjay_log(DEBUG, "Cancel Observe %04" PRIX16, request_identity->msg_id); #ifdef WITH_OBSERVE @@ -1383,7 +1396,7 @@ int _anjay_dm_foreach_instance(anjay_t *anjay, anjay_iid_t iid = 0; while (!(result = _anjay_dm_instance_it(anjay, obj, &iid, &cookie, NULL)) - && iid != ANJAY_IID_INVALID) { + && iid != ANJAY_IID_INVALID) { result = handler(anjay, obj, iid, data); if (result == ANJAY_FOREACH_BREAK) { anjay_log(TRACE, "foreach_instance: break on /%u/%u", (*obj)->oid, @@ -1417,14 +1430,13 @@ int _anjay_dm_res_read(anjay_t *anjay, return -1; } - avs_stream_outbuf_t stream = - AVS_STREAM_OUTBUF_STATIC_INITIALIZER; + avs_stream_outbuf_t stream = AVS_STREAM_OUTBUF_STATIC_INITIALIZER; avs_stream_outbuf_set_buffer(&stream, buffer, buffer_size); anjay_output_buf_ctx_t ctx = _anjay_output_buf_ctx_init(&stream); - int result = ensure_resource_supported_and_present( - anjay, obj, path->iid, path->rid); + int result = ensure_resource_supported_and_present(anjay, obj, path->iid, + path->rid); if (result) { return result; } @@ -1440,10 +1452,10 @@ struct anjay_dm_multires_read_ctx_struct { anjay_input_ctx_t *input_ctx; }; -static avs_stream_abstract_t * -read_tlv_to_membuf(anjay_t *anjay, const anjay_uri_path_t *path) { +static avs_stream_abstract_t *read_tlv_to_membuf(anjay_t *anjay, + const anjay_uri_path_t *path) { ASSERT_RESOURCE_PATH(*path); - const anjay_dm_object_def_t * const *obj = + const anjay_dm_object_def_t *const *obj = _anjay_dm_find_object_by_oid(anjay, path->oid); if (!obj) { anjay_log(ERROR, "unregistered Object ID: %u", path->oid); @@ -1461,8 +1473,8 @@ read_tlv_to_membuf(anjay_t *anjay, const anjay_uri_path_t *path) { return membuf; } -anjay_input_ctx_t * -_anjay_dm_read_as_input_ctx(anjay_t *anjay, const anjay_uri_path_t *path) { +anjay_input_ctx_t *_anjay_dm_read_as_input_ctx(anjay_t *anjay, + const anjay_uri_path_t *path) { ASSERT_RESOURCE_PATH(*path); avs_stream_abstract_t *membuf = read_tlv_to_membuf(anjay, path); if (!membuf) { @@ -1480,10 +1492,10 @@ _anjay_dm_read_as_input_ctx(anjay_t *anjay, const anjay_uri_path_t *path) { anjay_ssid_t _anjay_dm_current_ssid(anjay_t *anjay) { return anjay->current_connection.server - ? _anjay_server_ssid(anjay->current_connection.server) - : ANJAY_SSID_BOOTSTRAP; + ? _anjay_server_ssid(anjay->current_connection.server) + : ANJAY_SSID_BOOTSTRAP; } #ifdef ANJAY_TEST -#include "test/dm.c" +# include "test/dm.c" #endif // ANJAY_TEST diff --git a/src/dm_core.h b/src/dm_core.h index 6dc15e0e0..96eaac869 100644 --- a/src/dm_core.h +++ b/src/dm_core.h @@ -26,8 +26,8 @@ #include #include "coap/coap_stream.h" -#include "observe/observe_core.h" #include "dm/dm_attributes.h" +#include "observe/observe_core.h" VISIBILITY_PRIVATE_HEADER_BEGIN @@ -64,38 +64,38 @@ _anjay_request_attributes_equal(const anjay_request_attributes_t *left, const anjay_request_attributes_t *right) { return (left->has_min_period ? (right->has_min_period - && left->values.standard.common.min_period - == right->values.standard.common.min_period) + && left->values.standard.common.min_period + == right->values.standard.common.min_period) : !right->has_min_period) - && (left->has_max_period - ? (right->has_max_period - && left->values.standard.common.max_period - == right->values.standard.common.max_period) - : !right->has_max_period) - && (left->has_greater_than - ? (right->has_greater_than - && _anjay_double_attr_equal( - left->values.standard.greater_than, - right->values.standard.greater_than)) - : !right->has_greater_than) - && (left->has_less_than - ? (right->has_less_than - && _anjay_double_attr_equal( - left->values.standard.less_than, - right->values.standard.less_than)) - : !right->has_less_than) - && (left->has_step - ? (right->has_step - && _anjay_double_attr_equal( - left->values.standard.step, - right->values.standard.step)) - : !right->has_step) + && (left->has_max_period + ? (right->has_max_period + && left->values.standard.common.max_period + == right->values.standard.common + .max_period) + : !right->has_max_period) + && (left->has_greater_than + ? (right->has_greater_than + && _anjay_double_attr_equal( + left->values.standard.greater_than, + right->values.standard.greater_than)) + : !right->has_greater_than) + && (left->has_less_than + ? (right->has_less_than + && _anjay_double_attr_equal( + left->values.standard.less_than, + right->values.standard.less_than)) + : !right->has_less_than) + && (left->has_step ? (right->has_step + && _anjay_double_attr_equal( + left->values.standard.step, + right->values.standard.step)) + : !right->has_step) #ifdef WITH_CON_ATTR - && (left->custom.has_con - ? (right->custom.has_con - && left->values.custom.data.con - == right->values.custom.data.con) - : !right->custom.has_con) + && (left->custom.has_con + ? (right->custom.has_con + && left->values.custom.data.con + == right->values.custom.data.con) + : !right->custom.has_con) #endif // WITH_CON_ATTR ; } @@ -119,15 +119,15 @@ typedef struct { static inline bool _anjay_request_equal(const anjay_request_t *left, const anjay_request_t *right) { return left->msg_type == right->msg_type - && left->request_code == right->request_code - && left->is_bs_uri == right->is_bs_uri - && _anjay_uri_path_equal(&left->uri, &right->uri) - && left->action == right->action - && left->content_format == right->content_format - && left->requested_format == right->requested_format - && left->observe == right->observe - && _anjay_request_attributes_equal(&left->attributes, - &right->attributes); + && left->request_code == right->request_code + && left->is_bs_uri == right->is_bs_uri + && _anjay_uri_path_equal(&left->uri, &right->uri) + && left->action == right->action + && left->content_format == right->content_format + && left->requested_format == right->requested_format + && left->observe == right->observe + && _anjay_request_attributes_equal(&left->attributes, + &right->attributes); } typedef struct { @@ -140,22 +140,21 @@ typedef struct { bool observe_serial; } anjay_dm_read_args_t; -#define REQUEST_TO_DM_READ_ARGS(Anjay, Request) \ - (const anjay_dm_read_args_t) { \ - .ssid = _anjay_dm_current_ssid(Anjay), \ - .uri = (Request)->uri, \ - .requested_format = (Request)->requested_format, \ - .observe_serial = \ - ((Request)->observe == ANJAY_COAP_OBSERVE_REGISTER) \ - } +#define REQUEST_TO_DM_READ_ARGS(Anjay, Request) \ + (const anjay_dm_read_args_t) { \ + .ssid = _anjay_dm_current_ssid(Anjay), \ + .uri = (Request)->uri, \ + .requested_format = (Request)->requested_format, \ + .observe_serial = ((Request)->observe == ANJAY_COAP_OBSERVE_REGISTER) \ + } #define REQUEST_TO_ACTION_INFO(Anjay, Request) \ - (const anjay_action_info_t) { \ - .oid = (Request)->uri.oid, \ - .iid = (Request)->uri.iid, \ - .ssid = _anjay_dm_current_ssid(Anjay), \ - .action = (Request)->action \ - } + (const anjay_action_info_t) { \ + .oid = (Request)->uri.oid, \ + .iid = (Request)->uri.iid, \ + .ssid = _anjay_dm_current_ssid(Anjay), \ + .action = (Request)->action \ + } int _anjay_dm_transaction_validate(anjay_t *anjay); int _anjay_dm_transaction_finish_without_validation(anjay_t *anjay, int result); @@ -187,7 +186,7 @@ const char *_anjay_debug_make_path__(char *buffer, const anjay_uri_path_t *uri); #define ANJAY_DEBUG_MAKE_PATH(path) \ - (_anjay_debug_make_path__(&(char[32]){0}[0], 32, (path))) + (_anjay_debug_make_path__(&(char[32]){ 0 }[0], 32, (path))) static inline int _anjay_dm_map_present_result(int result) { if (!result) { diff --git a/src/downloader.h b/src/downloader.h index 5c241f891..9f3c9837b 100644 --- a/src/downloader.h +++ b/src/downloader.h @@ -18,13 +18,13 @@ #define ANJAY_DOWNLOADER_H #include -#include #include +#include #include -#include "utils_core.h" #include "coap/id_source/id_source.h" +#include "utils_core.h" VISIBILITY_PRIVATE_HEADER_BEGIN @@ -112,4 +112,3 @@ int _anjay_downloader_sched_reconnect_all(anjay_downloader_t *dl); VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_DOWNLOADER_H */ - diff --git a/src/downloader/coap.c b/src/downloader/coap.c index 95caba057..5e5241a7d 100644 --- a/src/downloader/coap.c +++ b/src/downloader/coap.c @@ -64,6 +64,7 @@ typedef struct { */ anjay_sched_handle_t sched_job; avs_coap_retry_state_t retry_state; + avs_coap_tx_params_t tx_params; } anjay_coap_download_ctx_t; static void cleanup_coap_transfer(anjay_downloader_t *dl, @@ -100,8 +101,8 @@ static int fill_coap_request_info(avs_coap_msg_info_t *req_info, avs_coap_block_info_t block2 = { .type = AVS_COAP_BLOCK2, .valid = true, - .seq_num = (uint32_t)(ctx->bytes_downloaded / ctx->block_size), - .size = (uint16_t)ctx->block_size, + .seq_num = (uint32_t) (ctx->bytes_downloaded / ctx->block_size), + .size = (uint16_t) ctx->block_size, .has_more = false }; if (avs_coap_msg_info_opt_block(req_info, &block2)) { @@ -113,12 +114,11 @@ static int fill_coap_request_info(avs_coap_msg_info_t *req_info, static void request_coap_block_job(anjay_t *anjay, const void *id_ptr); -static int -schedule_coap_retransmission(anjay_downloader_t *dl, - anjay_coap_download_ctx_t *ctx) { +static int schedule_coap_retransmission(anjay_downloader_t *dl, + anjay_coap_download_ctx_t *ctx) { anjay_t *anjay = _anjay_downloader_get_anjay(dl); - avs_coap_update_retry_state(&ctx->retry_state, &anjay->udp_tx_params, + avs_coap_update_retry_state(&ctx->retry_state, &ctx->tx_params, &dl->rand_seed); _anjay_sched_del(anjay->sched, &ctx->sched_job); return _anjay_sched(anjay->sched, &ctx->sched_job, @@ -140,15 +140,16 @@ static int request_coap_block(anjay_downloader_t *dl, required_storage_size = avs_coap_msg_info_get_packet_storage_size(&info, 0); if (required_storage_size > anjay->out_buffer_size) { - dl_log(ERROR, "CoAP output buffer too small to hold download request " - "(at least %lu bytes is needed)", + dl_log(ERROR, + "CoAP output buffer too small to hold download request " + "(at least %lu bytes is needed)", (unsigned long) required_storage_size); goto finish; } avs_coap_msg_builder_t builder; - avs_coap_msg_builder_init( - &builder,avs_coap_ensure_aligned_buffer(anjay->out_buffer), - anjay->out_buffer_size, &info); + avs_coap_msg_builder_init(&builder, + avs_coap_ensure_aligned_buffer(anjay->out_buffer), + anjay->out_buffer_size, &info); msg = avs_coap_msg_builder_get_msg(&builder); @@ -174,16 +175,20 @@ static void request_coap_block_job(anjay_t *anjay, const void *id_ptr) { } anjay_coap_download_ctx_t *ctx = (anjay_coap_download_ctx_t *) *ctx_ptr; - if (ctx->retry_state.retry_count > anjay->udp_tx_params.max_retransmit) { - dl_log(ERROR, "Limit of retransmissions reached, aborting download " - "id = %" PRIuPTR, id); + if (ctx->retry_state.retry_count > ctx->tx_params.max_retransmit) { + dl_log(ERROR, + "Limit of retransmissions reached, aborting download " + "id = %" PRIuPTR, + id); _anjay_downloader_abort_transfer(&anjay->downloader, ctx_ptr, ANJAY_DOWNLOAD_ERR_FAILED, ETIMEDOUT); } else { request_coap_block(&anjay->downloader, ctx); if (schedule_coap_retransmission(&anjay->downloader, ctx)) { - dl_log(WARNING, "could not schedule retransmission for download " - "id = %" PRIuPTR, ctx->common.id); + dl_log(WARNING, + "could not schedule retransmission for download " + "id = %" PRIuPTR, + ctx->common.id); _anjay_downloader_abort_transfer(&anjay->downloader, ctx_ptr, ANJAY_DOWNLOAD_ERR_FAILED, ENOMEM); } @@ -210,8 +215,9 @@ static int request_next_coap_block(anjay_downloader_t *dl, int result; if ((result = request_coap_block(dl, ctx)) || (result = schedule_coap_retransmission(dl, ctx))) { - dl_log(WARNING, "could not request block starting at %lu " - "for download id = %" PRIuPTR, + dl_log(WARNING, + "could not request block starting at %lu " + "for download id = %" PRIuPTR, (unsigned long) ctx->bytes_downloaded, ctx->common.id); _anjay_downloader_abort_transfer(dl, ctx_ptr, ANJAY_DOWNLOAD_ERR_FAILED, map_coap_ctx_err_to_errno(result)); @@ -232,9 +238,8 @@ static void request_next_coap_block_job(anjay_t *anjay, const void *id_ptr) { } } -static inline const char *etag_to_string(char *buf, - size_t buf_size, - const anjay_coap_etag_t *etag) { +static inline const char * +etag_to_string(char *buf, size_t buf_size, const anjay_coap_etag_t *etag) { AVS_ASSERT(buf_size >= sizeof(etag->value) * 3 + 1, "buffer too small to hold ETag"); @@ -245,13 +250,12 @@ static inline const char *etag_to_string(char *buf, return buf; } -#define ETAG_STR(EtagPtr) etag_to_string(&(char[32]){0}[0], 32, (EtagPtr)) +#define ETAG_STR(EtagPtr) etag_to_string(&(char[32]){ 0 }[0], 32, (EtagPtr)) -static int read_etag(const avs_coap_msg_t *msg, - anjay_coap_etag_t *out_etag) { +static int read_etag(const avs_coap_msg_t *msg, anjay_coap_etag_t *out_etag) { const avs_coap_opt_t *etag_opt = NULL; - int result = avs_coap_msg_find_unique_opt(msg, AVS_COAP_OPT_ETAG, - &etag_opt); + int result = + avs_coap_msg_find_unique_opt(msg, AVS_COAP_OPT_ETAG, &etag_opt); if (!etag_opt) { dl_log(TRACE, "no ETag option"); out_etag->size = 0; @@ -269,7 +273,7 @@ static int read_etag(const avs_coap_msg_t *msg, return -1; } - out_etag->size = (uint8_t)etag_size; + out_etag->size = (uint8_t) etag_size; memcpy(out_etag->value, avs_coap_opt_value(etag_opt), out_etag->size); dl_log(TRACE, "ETag: %s", ETAG_STR(out_etag)); @@ -303,11 +307,10 @@ static int parse_coap_response(const avs_coap_msg_t *msg, if (out_block2->has_more && out_block2->size != avs_coap_msg_payload_length(msg)) { dl_log(DEBUG, "malformed response: mismatched size of intermediate " - "packet"); + "packet"); return -1; } - const size_t requested_seq_num = ctx->bytes_downloaded / ctx->block_size; const size_t expected_offset = requested_seq_num * ctx->block_size; const size_t obtained_offset = out_block2->seq_num * out_block2->size; @@ -353,8 +356,8 @@ static void handle_coap_response(const avs_coap_msg_t *msg, avs_coap_block_info_t block2; anjay_coap_etag_t etag; if (parse_coap_response(msg, ctx, &block2, &etag)) { - _anjay_downloader_abort_transfer(dl, ctx_ptr, - ANJAY_DOWNLOAD_ERR_FAILED, EINVAL); + _anjay_downloader_abort_transfer(dl, ctx_ptr, ANJAY_DOWNLOAD_ERR_FAILED, + EINVAL); return; } @@ -362,9 +365,8 @@ static void handle_coap_response(const avs_coap_msg_t *msg, ctx->etag = etag; } else if (!etag_matches(&etag, &ctx->etag)) { dl_log(DEBUG, "remote resource expired, aborting download"); - _anjay_downloader_abort_transfer(dl, ctx_ptr, - ANJAY_DOWNLOAD_ERR_EXPIRED, - ECONNABORTED); + _anjay_downloader_abort_transfer( + dl, ctx_ptr, ANJAY_DOWNLOAD_ERR_EXPIRED, ECONNABORTED); return; } @@ -382,8 +384,8 @@ static void handle_coap_response(const avs_coap_msg_t *msg, (const uint8_t *) payload, payload_size, (const anjay_etag_t *) &etag, ctx->common.user_data)) { - _anjay_downloader_abort_transfer(dl, ctx_ptr, - ANJAY_DOWNLOAD_ERR_FAILED, errno); + _anjay_downloader_abort_transfer(dl, ctx_ptr, ANJAY_DOWNLOAD_ERR_FAILED, + errno); return; } @@ -403,8 +405,8 @@ static void abort_transfer_job(anjay_t *anjay, const void *ctx_) { AVS_LIST(anjay_download_ctx_t) *ctx_ptr = // IAR compiler does not support typeof, so AVS_LIST_FIND_PTR // returns void**, which is not implicitly-convertible - (AVS_LIST(anjay_download_ctx_t) *) - AVS_LIST_FIND_PTR(&anjay->downloader.downloads, ctx); + (AVS_LIST(anjay_download_ctx_t) *) AVS_LIST_FIND_PTR( + &anjay->downloader.downloads, ctx); if (!ctx_ptr) { anjay_log(WARNING, "transfer already aborted"); @@ -421,11 +423,11 @@ static void handle_coap_message(anjay_downloader_t *dl, assert(ctx_ptr); assert(*ctx_ptr); - avs_coap_msg_t *msg = (avs_coap_msg_t *) avs_coap_ensure_aligned_buffer( - anjay->in_buffer); + avs_coap_msg_t *msg = + (avs_coap_msg_t *) avs_coap_ensure_aligned_buffer(anjay->in_buffer); anjay_coap_download_ctx_t *ctx = (anjay_coap_download_ctx_t *) *ctx_ptr; - avs_coap_ctx_set_tx_params(anjay->coap_ctx, &anjay->udp_tx_params); + int result = avs_coap_ctx_recv(anjay->coap_ctx, ctx->socket, msg, anjay->in_buffer_size); @@ -444,7 +446,7 @@ static void handle_coap_message(anjay_downloader_t *dl, msg_id_must_match = false; // Separate Response break; case AVS_COAP_MSG_NON_CONFIRMABLE: - dl_log(DEBUG, "unexpected msg type: %d, ignoring", (int)type); + dl_log(DEBUG, "unexpected msg type: %d, ignoring", (int) type); return; } @@ -460,16 +462,16 @@ static void handle_coap_message(anjay_downloader_t *dl, return; } else if (type == AVS_COAP_MSG_RESET) { dl_log(DEBUG, "Reset response, aborting transfer"); - _anjay_downloader_abort_transfer(dl, ctx_ptr, - ANJAY_DOWNLOAD_ERR_FAILED, - ECONNREFUSED); + _anjay_downloader_abort_transfer( + dl, ctx_ptr, ANJAY_DOWNLOAD_ERR_FAILED, ECONNREFUSED); return; } else if (type == AVS_COAP_MSG_ACKNOWLEDGEMENT && avs_coap_msg_get_code(msg) == AVS_COAP_CODE_EMPTY) { - avs_time_duration_t abort_delay = avs_coap_exchange_lifetime( - &anjay->udp_tx_params); - dl_log(DEBUG, "Separate ACK received, waiting " - "%" PRId64 ".%09" PRId32 " for response", + avs_time_duration_t abort_delay = + avs_coap_exchange_lifetime(&ctx->tx_params); + dl_log(DEBUG, + "Separate ACK received, waiting " + "%" PRId64 ".%09" PRId32 " for response", abort_delay.seconds, abort_delay.nanoseconds); _anjay_sched_del(anjay->sched, &ctx->sched_job); @@ -501,10 +503,8 @@ static int get_coap_socket(anjay_downloader_t *dl, static size_t get_max_acceptable_block_size(size_t in_buffer_size) { size_t estimated_response_header_size = - AVS_COAP_MAX_HEADER_SIZE - + AVS_COAP_MAX_TOKEN_LENGTH - + AVS_COAP_OPT_ETAG_MAX_SIZE - + AVS_COAP_OPT_BLOCK_MAX_SIZE + AVS_COAP_MAX_HEADER_SIZE + AVS_COAP_MAX_TOKEN_LENGTH + + AVS_COAP_OPT_ETAG_MAX_SIZE + AVS_COAP_OPT_BLOCK_MAX_SIZE + 1; // payload marker size_t payload_capacity = in_buffer_size - estimated_response_header_size; size_t block_size = @@ -524,22 +524,26 @@ static int reconnect_coap_transfer(anjay_downloader_t *dl, anjay_coap_download_ctx_t *ctx = (anjay_coap_download_ctx_t *) *ctx_ptr; char hostname[ANJAY_MAX_URL_HOSTNAME_SIZE]; char port[ANJAY_MAX_URL_PORT_SIZE]; - if (avs_net_socket_get_remote_hostname(ctx->socket, - hostname, sizeof(hostname)) + if (avs_net_socket_get_remote_hostname(ctx->socket, hostname, + sizeof(hostname)) || avs_net_socket_get_remote_port(ctx->socket, port, sizeof(port)) || avs_net_socket_close(ctx->socket) || avs_net_socket_connect(ctx->socket, hostname, port)) { - dl_log(WARNING, "could not reconnect socket for download " - "id = %" PRIuPTR, ctx->common.id); + dl_log(WARNING, + "could not reconnect socket for download " + "id = %" PRIuPTR, + ctx->common.id); return -avs_net_socket_errno(ctx->socket); } else { anjay_t *anjay = _anjay_downloader_get_anjay(dl); _anjay_sched_del(anjay->sched, &ctx->sched_job); if (_anjay_sched_now(anjay->sched, &ctx->sched_job, - request_next_coap_block_job, - &ctx->common.id, sizeof(ctx->common.id))) { - dl_log(WARNING, "could not schedule resumption for download " - "id = %" PRIuPTR, ctx->common.id); + request_next_coap_block_job, &ctx->common.id, + sizeof(ctx->common.id))) { + dl_log(WARNING, + "could not schedule resumption for download " + "id = %" PRIuPTR, + ctx->common.id); return -ENOMEM; } } @@ -547,7 +551,7 @@ static int reconnect_coap_transfer(anjay_downloader_t *dl, } #ifdef ANJAY_TEST -#include "test/downloader_mock.h" +# include "test/downloader_mock.h" #endif // ANJAY_TEST int _anjay_downloader_coap_ctx_new(anjay_downloader_t *dl, @@ -644,9 +648,23 @@ int _anjay_downloader_coap_ctx_new(anjay_downloader_t *dl, memcpy(ctx->etag.value, cfg->etag->value, ctx->etag.size); } - if (_anjay_sched_now(anjay->sched, &ctx->sched_job, + if (!cfg->coap_tx_params) { + ctx->tx_params = anjay->udp_tx_params; + } else { + const char *error_string = NULL; + if (avs_coap_tx_params_valid(cfg->coap_tx_params, &error_string)) { + ctx->tx_params = *cfg->coap_tx_params; + } else { + dl_log(ERROR, "invalid tx_params: %s", error_string); + goto error; + } + } + + if (_anjay_sched_now(anjay->sched, + &ctx->sched_job, request_next_coap_block_job, - &ctx->common.id, sizeof(ctx->common.id))) { + &ctx->common.id, + sizeof(ctx->common.id))) { dl_log(ERROR, "could not schedule download job"); result = -ENOMEM; goto error; @@ -660,5 +678,5 @@ int _anjay_downloader_coap_ctx_new(anjay_downloader_t *dl, } #ifdef ANJAY_TEST -#include "test/downloader.c" +# include "test/downloader.c" #endif // ANJAY_TEST diff --git a/src/downloader/downloader.c b/src/downloader/downloader.c index 6dace4062..28c8adeec 100644 --- a/src/downloader/downloader.c +++ b/src/downloader/downloader.c @@ -120,8 +120,8 @@ static int get_ctx_socket(anjay_downloader_t *dl, assert(dl); assert(ctx); assert(ctx->common.vtable); - int result = ctx->common.vtable->get_socket(dl, ctx, - out_socket, out_transport); + int result = + ctx->common.vtable->get_socket(dl, ctx, out_socket, out_transport); if (!result) { assert(*out_socket); } @@ -136,8 +136,7 @@ find_ctx_ptr_by_socket(anjay_downloader_t *dl, avs_net_abstract_socket_t *ctx_socket = NULL; if (!get_ctx_socket(dl, *ctx, &ctx_socket, &(anjay_socket_transport_t) { - (anjay_socket_transport_t) 0 - }) + (anjay_socket_transport_t) 0 }) && ctx_socket == socket) { return ctx; } @@ -175,8 +174,7 @@ int _anjay_downloader_get_sockets(anjay_downloader_t *dl, } AVS_LIST(anjay_download_ctx_t) * -_anjay_downloader_find_ctx_ptr_by_id(anjay_downloader_t *dl, - uintptr_t id) { +_anjay_downloader_find_ctx_ptr_by_id(anjay_downloader_t *dl, uintptr_t id) { AVS_LIST(anjay_download_ctx_t) *ctx; AVS_LIST_FOREACH_PTR(ctx, &dl->downloads) { if ((*ctx)->common.id == id) { @@ -191,8 +189,7 @@ int _anjay_downloader_handle_packet(anjay_downloader_t *dl, avs_net_abstract_socket_t *socket) { assert(&_anjay_downloader_get_anjay(dl)->downloader == dl); - AVS_LIST(anjay_download_ctx_t) *ctx = - find_ctx_ptr_by_socket(dl, socket); + AVS_LIST(anjay_download_ctx_t) *ctx = find_ctx_ptr_by_socket(dl, socket); if (!ctx) { // unknown socket return -1; @@ -217,7 +214,7 @@ static uintptr_t find_free_id(anjay_downloader_t *dl) { do { id = dl->next_id++; } while (id == INVALID_DOWNLOAD_ID - || _anjay_downloader_find_ctx_ptr_by_id(dl, id) != NULL); + || _anjay_downloader_find_ctx_ptr_by_id(dl, id) != NULL); return id; } @@ -260,14 +257,14 @@ int _anjay_downloader_download(anjay_downloader_t *dl, int result = -EPROTONOSUPPORT; #ifdef WITH_BLOCK_DOWNLOAD if (starts_with(config->url, "coap")) { - result = _anjay_downloader_coap_ctx_new(dl, &dl_ctx, - config, find_free_id(dl)); + result = _anjay_downloader_coap_ctx_new(dl, &dl_ctx, config, + find_free_id(dl)); } else #endif // WITH_BLOCK_DOWNLOAD #ifdef WITH_HTTP_DOWNLOAD - if (starts_with(config->url, "http")) { - result = _anjay_downloader_http_ctx_new(dl, &dl_ctx, - config, find_free_id(dl)); + if (starts_with(config->url, "http")) { + result = _anjay_downloader_http_ctx_new(dl, &dl_ctx, config, + find_free_id(dl)); } else #endif // WITH_HTTP_DOWNLOAD { @@ -287,15 +284,15 @@ int _anjay_downloader_download(anjay_downloader_t *dl, void _anjay_downloader_abort(anjay_downloader_t *dl, anjay_download_handle_t handle) { - uintptr_t id = (uintptr_t)handle; + uintptr_t id = (uintptr_t) handle; AVS_LIST(anjay_download_ctx_t) *ctx = _anjay_downloader_find_ctx_ptr_by_id(dl, id); if (!ctx) { dl_log(DEBUG, "download id = %" PRIuPTR " not found (expired?)", id); } else { - _anjay_downloader_abort_transfer(dl, ctx, - ANJAY_DOWNLOAD_ERR_ABORTED, EINTR); + _anjay_downloader_abort_transfer(dl, ctx, ANJAY_DOWNLOAD_ERR_ABORTED, + EINTR); } } @@ -315,6 +312,6 @@ int _anjay_downloader_sched_reconnect_all(anjay_downloader_t *dl) { return 0; } return _anjay_sched_now(_anjay_downloader_get_anjay(dl)->sched, - &dl->reconnect_job_handle, - reconnect_all_job, NULL, 0); + &dl->reconnect_job_handle, reconnect_all_job, NULL, + 0); } diff --git a/src/downloader/http.c b/src/downloader/http.c index c9c09761b..96c932c16 100644 --- a/src/downloader/http.c +++ b/src/downloader/http.c @@ -56,25 +56,28 @@ static int read_start_byte_from_content_range(const char *content_range, int after_slash = 0; if (avs_match_token(&content_range, "bytes", AVS_SPACES) || sscanf(content_range, "%" SCNu64 "-%" SCNu64 "/%n", - out_start_byte, &end_byte, &after_slash) < 2 + out_start_byte, &end_byte, &after_slash) + < 2 || after_slash <= 0) { return -1; } return (strcmp(&content_range[after_slash], "*") == 0 || (!_anjay_safe_strtoll(&content_range[after_slash], &complete_length) - && complete_length >= 1 - && (uint64_t) (complete_length - 1) == end_byte)) ? 0 : -1; + && complete_length >= 1 + && (uint64_t) (complete_length - 1) == end_byte)) + ? 0 + : -1; } static anjay_etag_t *read_etag(const char *text) { size_t len = strlen(text); - if (len < 2 || len > UINT8_MAX + 2 - || text[0] != '"' || text[len - 1] != '"') { + if (len < 2 || len > UINT8_MAX + 2 || text[0] != '"' + || text[len - 1] != '"') { return NULL; } - anjay_etag_t *result = (anjay_etag_t *) - avs_malloc(offsetof(anjay_etag_t, value) + (len - 2)); + anjay_etag_t *result = (anjay_etag_t *) avs_malloc( + offsetof(anjay_etag_t, value) + (len - 2)); if (result) { result->size = (uint8_t) (len - 2); memcpy(result->value, &text[1], result->size); @@ -82,12 +85,11 @@ static anjay_etag_t *read_etag(const char *text) { return result; } -static inline bool etag_matches(const anjay_etag_t *etag, - const char *text) { +static inline bool etag_matches(const anjay_etag_t *etag, const char *text) { size_t len = strlen(text); - return len == (size_t) (etag->size + 2) - && text[0] == '"' && text[len - 1] == '"' - && memcmp(etag->value, &text[1], etag->size) == 0; + return len == (size_t) (etag->size + 2) && text[0] == '"' + && text[len - 1] == '"' + && memcmp(etag->value, &text[1], etag->size) == 0; } static void handle_http_packet(anjay_downloader_t *dl, @@ -109,14 +111,13 @@ static void handle_http_packet(anjay_downloader_t *dl, if (bytes_read) { assert(ctx->bytes_written >= ctx->bytes_downloaded); if (ctx->bytes_downloaded + bytes_read > ctx->bytes_written) { - size_t bytes_to_write = ctx->bytes_downloaded + bytes_read - - ctx->bytes_written; + size_t bytes_to_write = + ctx->bytes_downloaded + bytes_read - ctx->bytes_written; assert(bytes_read >= bytes_to_write); if (ctx->common.on_next_block( anjay, &anjay->in_buffer[bytes_read - bytes_to_write], - bytes_to_write, ctx->etag, - ctx->common.user_data)) { + bytes_to_write, ctx->etag, ctx->common.user_data)) { _anjay_downloader_abort_transfer( dl, ctx_ptr, ANJAY_DOWNLOAD_ERR_FAILED, errno); return; @@ -152,8 +153,8 @@ static void send_request(anjay_t *anjay, const void *id_ptr) { AVS_LIST(const avs_http_header_t) received_headers = NULL; anjay_http_download_ctx_t *ctx = (anjay_http_download_ctx_t *) *ctx_ptr; - int result = avs_http_open_stream(&ctx->stream, ctx->client, - AVS_HTTP_GET, AVS_HTTP_CONTENT_IDENTITY, + int result = avs_http_open_stream(&ctx->stream, ctx->client, AVS_HTTP_GET, + AVS_HTTP_CONTENT_IDENTITY, ctx->parsed_url, NULL, NULL); if (result || !ctx->stream) { goto error; @@ -164,7 +165,8 @@ static void send_request(anjay_t *anjay, const void *id_ptr) { char ifmatch[258]; if (ctx->etag) { if (avs_simple_snprintf(ifmatch, sizeof(ifmatch), "\"%.*s\"", - (int) ctx->etag->size, ctx->etag->value) < 0 + (int) ctx->etag->size, ctx->etag->value) + < 0 || avs_http_add_header(ctx->stream, "If-Match", ifmatch)) { dl_log(ERROR, "Could not send If-Match header"); goto error; @@ -175,7 +177,8 @@ static void send_request(anjay_t *anjay, const void *id_ptr) { char range[sizeof("bytes=-") + (12 * sizeof(size_t)) / 5 + 1]; if (ctx->bytes_written > 0) { if (avs_simple_snprintf(range, sizeof(range), "bytes=%lu-", - (unsigned long) ctx->bytes_written) < 0 + (unsigned long) ctx->bytes_written) + < 0 || avs_http_add_header(ctx->stream, "Range", range)) { dl_log(ERROR, "Could not resume HTTP download: " "could not send Range header"); @@ -200,8 +203,10 @@ static void send_request(anjay_t *anjay, const void *id_ptr) { uint64_t bytes_downloaded; if (read_start_byte_from_content_range(it->value, &bytes_downloaded) || bytes_downloaded > ctx->bytes_written) { - dl_log(ERROR, "Could not resume HTTP download: " - "invalid Content-Range: %s", it->value); + dl_log(ERROR, + "Could not resume HTTP download: " + "invalid Content-Range: %s", + it->value); goto error; } ctx->bytes_downloaded = (size_t) bytes_downloaded; @@ -245,8 +250,8 @@ static void send_request(anjay_t *anjay, const void *id_ptr) { } return; error: - _anjay_downloader_abort_transfer(&anjay->downloader, ctx_ptr, - error_code, result); + _anjay_downloader_abort_transfer(&anjay->downloader, ctx_ptr, error_code, + result); } static int get_http_socket(anjay_downloader_t *dl, @@ -255,7 +260,7 @@ static int get_http_socket(anjay_downloader_t *dl, anjay_socket_transport_t *out_transport) { (void) dl; if (!(*out_socket = avs_stream_net_getsock( - ((anjay_http_download_ctx_t *) ctx)->stream))) { + ((anjay_http_download_ctx_t *) ctx)->stream))) { return -1; } *out_transport = ANJAY_SOCKET_TRANSPORT_TCP; @@ -345,8 +350,8 @@ int _anjay_downloader_http_ctx_new(anjay_downloader_t *dl, } if (_anjay_sched_now(_anjay_downloader_get_anjay(dl)->sched, - &ctx->send_request_job, send_request, - &ctx->common.id, sizeof(ctx->common.id))) { + &ctx->send_request_job, send_request, &ctx->common.id, + sizeof(ctx->common.id))) { dl_log(ERROR, "could not schedule download job"); result = -ENOMEM; goto error; diff --git a/src/downloader/private.h b/src/downloader/private.h index 44a139f62..accb74dd4 100644 --- a/src/downloader/private.h +++ b/src/downloader/private.h @@ -21,7 +21,7 @@ #include "../downloader.h" #ifndef ANJAY_DOWNLOADER_INTERNALS -#error "downloader/private.h is not meant to be included from outside" +# error "downloader/private.h is not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN @@ -56,8 +56,7 @@ static inline anjay_t *_anjay_downloader_get_anjay(anjay_downloader_t *dl) { } AVS_LIST(anjay_download_ctx_t) * -_anjay_downloader_find_ctx_ptr_by_id(anjay_downloader_t *dl, - uintptr_t id); +_anjay_downloader_find_ctx_ptr_by_id(anjay_downloader_t *dl, uintptr_t id); void _anjay_downloader_abort_transfer(anjay_downloader_t *dl, AVS_LIST(anjay_download_ctx_t) *ctx, diff --git a/src/downloader/test/downloader.c b/src/downloader/test/downloader.c index 09e9fb90d..221435066 100644 --- a/src/downloader/test/downloader.c +++ b/src/downloader/test/downloader.c @@ -24,10 +24,10 @@ #include "../../coap/id_source/auto.h" #include "../../coap/test/utils.h" -#define DIV_CEIL(a, b) (((a) + (b) - 1) / (b)) +#define DIV_CEIL(a, b) (((a) + (b) -1) / (b)) #define _ENSURE_0_OR_1_ARGS(a) -#define _ASSERT_ALMOST_EQ(a, b, epsilon, ...) \ +#define _ASSERT_ALMOST_EQ(a, b, epsilon, ...) \ AVS_UNIT_ASSERT_TRUE(fabs((a) - (b)) < (epsilon)) \ _ENSURE_0_OR_1_ARGS(__VA_ARGS__) @@ -47,7 +47,9 @@ static int allocate_mocksock(avs_net_abstract_socket_t **out, const void *socket_config, const anjay_socket_bind_config_t *bind_conf, const anjay_url_t *uri) { - (void) type; (void) socket_config; (void) bind_conf; + (void) type; + (void) socket_config; + (void) bind_conf; AVS_UNIT_ASSERT_TRUE(ENV.num_mocksocks < AVS_ARRAY_SIZE(ENV.mocksock)); *out = ENV.mocksock[ENV.num_mocksocks++]; @@ -73,7 +75,8 @@ static void setup(void) { AVS_UNIT_ASSERT_SUCCESS(avs_coap_ctx_create(&ENV.anjay.coap_ctx, 0)); // this particular seed ensures generated message IDs start from 0 - coap_id_source_t *id_source = _anjay_coap_id_source_auto_new(4235699843U, 0); + coap_id_source_t *id_source = + _anjay_coap_id_source_auto_new(4235699843U, 0); _anjay_downloader_init(&ENV.anjay.downloader, &ENV.anjay, &id_source); _anjay_mock_clock_start(avs_time_monotonic_from_scalar(1, AVS_TIME_S)); @@ -163,9 +166,7 @@ static int on_next_block(anjay_t *anjay, return result; } -static void on_download_finished(anjay_t *anjay, - int result, - void *user_data) { +static void on_download_finished(anjay_t *anjay, int result, void *user_data) { handler_data_t *hd = (handler_data_t *) user_data; AVS_UNIT_ASSERT_TRUE(anjay == hd->anjay); @@ -179,7 +180,8 @@ typedef struct { dl_test_env_t *base; handler_data_t data; anjay_download_config_t cfg; - avs_net_abstract_socket_t *mocksock; // alias for SIMPLE_ENV.base->mocksock[0] + avs_net_abstract_socket_t + *mocksock; // alias for SIMPLE_ENV.base->mocksock[0] } dl_simple_test_env_t; dl_simple_test_env_t SIMPLE_ENV; @@ -188,8 +190,10 @@ static void setup_simple(const char *url) { memset(&SIMPLE_ENV, 0, sizeof(SIMPLE_ENV)); setup(); SIMPLE_ENV.base = &ENV; - SIMPLE_ENV.data = (handler_data_t){ .anjay = &SIMPLE_ENV.base->anjay }; - SIMPLE_ENV.cfg = (anjay_download_config_t){ + SIMPLE_ENV.data = (handler_data_t) { + .anjay = &SIMPLE_ENV.base->anjay + }; + SIMPLE_ENV.cfg = (anjay_download_config_t) { .url = url, .on_next_block = on_next_block, .on_download_finished = on_download_finished, @@ -237,8 +241,8 @@ AVS_UNIT_TEST(downloader, empty_has_no_sockets) { setup(); AVS_LIST(anjay_socket_entry_t) socks = NULL; - AVS_UNIT_ASSERT_SUCCESS(_anjay_downloader_get_sockets(&ENV.anjay.downloader, - &socks)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_downloader_get_sockets(&ENV.anjay.downloader, &socks)); AVS_UNIT_ASSERT_NULL(socks); teardown(); @@ -284,7 +288,7 @@ AVS_UNIT_TEST(downloader, cannot_download_without_handlers) { teardown_simple(); } -#define DESPAIR \ +#define DESPAIR \ "Despair is when you're debugging a kernel driver and you look " \ "at a memory dump and you see that a pointer has a value of 7." @@ -293,20 +297,20 @@ AVS_UNIT_TEST(downloader, coap_download_single_block) { // expect packets const avs_coap_msg_t *req = COAP_MSG(CON, GET, ID(0), BLOCK2(0, 1024)); - const avs_coap_msg_t *res = COAP_MSG(ACK, CONTENT, ID(0), - BLOCK2(0, 128, DESPAIR)); + const avs_coap_msg_t *res = + COAP_MSG(ACK, CONTENT, ID(0), BLOCK2(0, 128, DESPAIR)); avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, - &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); // expect handler calls - expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t){ - .data = DESPAIR, - .data_size = sizeof(DESPAIR) - 1, - .result = 0 - }); + expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t) { + .data = DESPAIR, + .data_size = sizeof(DESPAIR) - 1, + .result = 0 + }); expect_download_finished(&SIMPLE_ENV.data, 0); perform_simple_download(); @@ -324,14 +328,16 @@ AVS_UNIT_TEST(downloader, coap_download_multiple_blocks) { size_t num_blocks = DIV_CEIL(sizeof(DESPAIR) - 1, BLOCK_SIZE); for (size_t i = 0; i < num_blocks; ++i) { - const avs_coap_msg_t *req = COAP_MSG(CON, GET, ID(i), - BLOCK2(i, i == 0 ? 1024 : BLOCK_SIZE)); - const avs_coap_msg_t *res = COAP_MSG(ACK, CONTENT, ID(i), - BLOCK2(i, BLOCK_SIZE, DESPAIR)); + const avs_coap_msg_t *req = + COAP_MSG(CON, GET, ID(i), + BLOCK2(i, i == 0 ? 1024 : BLOCK_SIZE)); + const avs_coap_msg_t *res = + COAP_MSG(ACK, CONTENT, ID(i), BLOCK2(i, BLOCK_SIZE, DESPAIR)); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, - &req->content, req->length); - avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); + avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, + res->length); bool is_last_block = (i + 1) * BLOCK_SIZE >= sizeof(DESPAIR) - 1; size_t size = is_last_block ? sizeof(DESPAIR) - 1 - i * BLOCK_SIZE @@ -378,7 +384,8 @@ AVS_UNIT_TEST(downloader, download_abort_on_reset_response) { const avs_coap_msg_t *res = COAP_MSG(RST, EMPTY, ID(0), NO_PAYLOAD); avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); // expect handler calls @@ -405,8 +412,8 @@ AVS_UNIT_TEST(downloader, unsupported_protocol) { AVS_UNIT_TEST(downloader, unrelated_socket) { setup(); - AVS_UNIT_ASSERT_FAILED(_anjay_downloader_handle_packet(&ENV.anjay.downloader, - ENV.mocksock[0])); + AVS_UNIT_ASSERT_FAILED(_anjay_downloader_handle_packet( + &ENV.anjay.downloader, ENV.mocksock[0])); teardown(); } @@ -416,22 +423,23 @@ AVS_UNIT_TEST(downloader, coap_download_separate_response) { // expect packets const avs_coap_msg_t *req = COAP_MSG(CON, GET, ID(0), BLOCK2(0, 1024)); - const avs_coap_msg_t *res = COAP_MSG(CON, CONTENT, ID(1), - BLOCK2(0, 128, DESPAIR)); + const avs_coap_msg_t *res = + COAP_MSG(CON, CONTENT, ID(1), BLOCK2(0, 128, DESPAIR)); const avs_coap_msg_t *res_res = COAP_MSG(ACK, EMPTY, ID(1), NO_PAYLOAD); avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, - &res_res->content, res_res->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &res_res->content, + res_res->length); // expect handler calls - expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t){ - .data = DESPAIR, - .data_size = sizeof(DESPAIR) - 1, - .result = 0 - }); + expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t) { + .data = DESPAIR, + .data_size = sizeof(DESPAIR) - 1, + .result = 0 + }); expect_download_finished(&SIMPLE_ENV.data, 0); perform_simple_download(); @@ -446,21 +454,22 @@ AVS_UNIT_TEST(downloader, coap_download_unexpected_packet) { const avs_coap_msg_t *req = COAP_MSG(CON, GET, ID(0), BLOCK2(0, 1024)); const avs_coap_msg_t *unk1 = COAP_MSG(RST, CONTENT, ID(1), NO_PAYLOAD); const avs_coap_msg_t *unk2 = COAP_MSG(NON, CONTENT, ID(2), NO_PAYLOAD); - const avs_coap_msg_t *res = COAP_MSG(ACK, CONTENT, ID(0), - BLOCK2(0, 128, DESPAIR)); + const avs_coap_msg_t *res = + COAP_MSG(ACK, CONTENT, ID(0), BLOCK2(0, 128, DESPAIR)); avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &unk1->content, unk1->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &unk2->content, unk2->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); // expect handler calls - expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t){ - .data = DESPAIR, - .data_size = sizeof(DESPAIR) - 1, - .result = 0 - }); + expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t) { + .data = DESPAIR, + .data_size = sizeof(DESPAIR) - 1, + .result = 0 + }); expect_download_finished(&SIMPLE_ENV.data, 0); perform_simple_download(); @@ -473,19 +482,20 @@ AVS_UNIT_TEST(downloader, coap_download_abort_from_handler) { // expect packets const avs_coap_msg_t *req = COAP_MSG(CON, GET, ID(0), BLOCK2(0, 1024)); - const avs_coap_msg_t *res = COAP_MSG(ACK, CONTENT, ID(0), - BLOCK2(0, 128, DESPAIR)); + const avs_coap_msg_t *res = + COAP_MSG(ACK, CONTENT, ID(0), BLOCK2(0, 128, DESPAIR)); avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); // expect handler calls - expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t){ - .data = DESPAIR, - .data_size = sizeof(DESPAIR) - 1, - .result = -1 // request abort - }); + expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t) { + .data = DESPAIR, + .data_size = sizeof(DESPAIR) - 1, + .result = -1 // request abort + }); expect_download_finished(&SIMPLE_ENV.data, ANJAY_DOWNLOAD_ERR_FAILED); perform_simple_download(); @@ -498,22 +508,28 @@ AVS_UNIT_TEST(downloader, coap_download_expired) { // expect packets const avs_coap_msg_t *req1 = COAP_MSG(CON, GET, ID(0), BLOCK2(0, 1024)); - const avs_coap_msg_t *res1 = COAP_MSG(ACK, CONTENT, ID(0), ETAG("tag"), - BLOCK2(0, 64, DESPAIR)); + const avs_coap_msg_t *res1 = + COAP_MSG(ACK, CONTENT, ID(0), ETAG("tag"), BLOCK2(0, 64, DESPAIR)); const avs_coap_msg_t *req2 = COAP_MSG(CON, GET, ID(1), BLOCK2(1, 64)); - const avs_coap_msg_t *res2 = COAP_MSG(ACK, CONTENT, ID(1), ETAG("nje"), - BLOCK2(1, 64, DESPAIR)); + const avs_coap_msg_t *res2 = + COAP_MSG(ACK, CONTENT, ID(1), ETAG("nje"), BLOCK2(1, 64, DESPAIR)); avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req1->content, req1->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req1->content, + req1->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res1->content, res1->length); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req2->content, req2->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req2->content, + req2->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res2->content, res2->length); - static const anjay_coap_etag_t etag = { .size = 3, .value = "tag" }; + static const anjay_coap_etag_t etag = { + .size = 3, + .value = "tag" + }; // expect handler calls - expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t){ + expect_next_block(&SIMPLE_ENV.data, + (on_next_block_args_t) { .data = DESPAIR, .data_size = 64, .etag = (const anjay_etag_t *) &etag, @@ -547,8 +563,8 @@ AVS_UNIT_TEST(downloader, retry) { setup_simple("coap://127.0.0.1:5683"); const avs_coap_msg_t *req = COAP_MSG(CON, GET, ID(0), BLOCK2(0, 1024)); - const avs_coap_msg_t *res = COAP_MSG(ACK, CONTENT, ID(0), ETAG("tag"), - BLOCK2(0, 128, DESPAIR)); + const avs_coap_msg_t *res = + COAP_MSG(ACK, CONTENT, ID(0), ETAG("tag"), BLOCK2(0, 128, DESPAIR)); avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); @@ -558,7 +574,8 @@ AVS_UNIT_TEST(downloader, retry) { AVS_UNIT_ASSERT_NOT_NULL(handle); // initial request - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); _anjay_sched_run(SIMPLE_ENV.base->anjay.sched); // request retransmissions @@ -566,21 +583,20 @@ AVS_UNIT_TEST(downloader, retry) { for (size_t i = 0; i < 4; ++i) { // make sure there's a retransmission job scheduled avs_time_duration_t time_to_next; - AVS_UNIT_ASSERT_SUCCESS( - _anjay_sched_time_to_next(SIMPLE_ENV.base->anjay.sched, - &time_to_next)); + AVS_UNIT_ASSERT_SUCCESS(_anjay_sched_time_to_next( + SIMPLE_ENV.base->anjay.sched, &time_to_next)); _anjay_mock_clock_advance(time_to_next); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, - &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); _anjay_sched_run(SIMPLE_ENV.base->anjay.sched); // ...and it's roughly exponential backoff if (avs_time_duration_valid(last_time_to_next)) { - double ratio = avs_time_duration_to_fscalar(time_to_next, - AVS_TIME_S) - / avs_time_duration_to_fscalar(last_time_to_next, - AVS_TIME_S); + double ratio = + avs_time_duration_to_fscalar(time_to_next, AVS_TIME_S) + / avs_time_duration_to_fscalar(last_time_to_next, + AVS_TIME_S); ASSERT_ALMOST_EQ(ratio, 2.0); } last_time_to_next = time_to_next; @@ -589,8 +605,12 @@ AVS_UNIT_TEST(downloader, retry) { // handle response avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); - static const anjay_coap_etag_t etag = { .size = 3, .value = "tag" }; - expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t){ + static const anjay_coap_etag_t etag = { + .size = 3, + .value = "tag" + }; + expect_next_block(&SIMPLE_ENV.data, + (on_next_block_args_t) { .data = DESPAIR, .data_size = sizeof(DESPAIR) - 1, .etag = (const anjay_etag_t *) &etag, @@ -623,18 +643,20 @@ AVS_UNIT_TEST(downloader, missing_separate_response) { AVS_UNIT_ASSERT_NOT_NULL(handle); // initial request - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); _anjay_sched_run(SIMPLE_ENV.base->anjay.sched); // retransmission job should be scheduled avs_time_duration_t time_to_next; AVS_UNIT_ASSERT_SUCCESS(_anjay_sched_time_to_next( SIMPLE_ENV.base->anjay.sched, &time_to_next)); - AVS_UNIT_ASSERT_TRUE(avs_time_duration_to_fscalar(time_to_next, - AVS_TIME_S) < 5.0); + AVS_UNIT_ASSERT_TRUE(avs_time_duration_to_fscalar(time_to_next, AVS_TIME_S) + < 5.0); // separate ACK - avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &req_ack->content, req_ack->length); + avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &req_ack->content, + req_ack->length); AVS_UNIT_ASSERT_SUCCESS(handle_packet()); // abort job should be scheduled to run after EXCHANGE_LIFETIME @@ -643,10 +665,10 @@ AVS_UNIT_TEST(downloader, missing_separate_response) { static const avs_coap_tx_params_t tx_params = ANJAY_COAP_DEFAULT_UDP_TX_PARAMS; - ASSERT_ALMOST_EQ(avs_time_duration_to_fscalar(time_to_next, AVS_TIME_S), - avs_time_duration_to_fscalar( - avs_coap_exchange_lifetime(&tx_params), - AVS_TIME_S)); + ASSERT_ALMOST_EQ( + avs_time_duration_to_fscalar(time_to_next, AVS_TIME_S), + avs_time_duration_to_fscalar(avs_coap_exchange_lifetime(&tx_params), + AVS_TIME_S)); // no separate response should abort the transfer after EXCHANGE_LIFETIME expect_download_finished(&SIMPLE_ENV.data, ANJAY_DOWNLOAD_ERR_FAILED); @@ -697,24 +719,23 @@ AVS_UNIT_TEST(downloader, uri_path_query) { setup_simple("coap://127.0.0.1:5683/uri/path?query=string&another"); // expect packets - const avs_coap_msg_t *req = COAP_MSG(CON, GET, ID(0), - PATH("uri", "path"), - QUERY("query=string", "another"), - BLOCK2(0, 1024)); - const avs_coap_msg_t *res = COAP_MSG(ACK, CONTENT, ID(0), - BLOCK2(0, 128, DESPAIR)); + const avs_coap_msg_t *req = + COAP_MSG(CON, GET, ID(0), PATH("uri", "path"), + QUERY("query=string", "another"), BLOCK2(0, 1024)); + const avs_coap_msg_t *res = + COAP_MSG(ACK, CONTENT, ID(0), BLOCK2(0, 128, DESPAIR)); avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, - &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); // expect handler calls - expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t){ - .data = DESPAIR, - .data_size = sizeof(DESPAIR) - 1, - .result = 0 - }); + expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t) { + .data = DESPAIR, + .data_size = sizeof(DESPAIR) - 1, + .result = 0 + }); expect_download_finished(&SIMPLE_ENV.data, 0); perform_simple_download(); @@ -731,20 +752,20 @@ AVS_UNIT_TEST(downloader, in_buffer_size_enforces_smaller_initial_block_size) { // expect packets const avs_coap_msg_t *req = COAP_MSG(CON, GET, ID(0), BLOCK2(0, 128)); - const avs_coap_msg_t *res = COAP_MSG(ACK, CONTENT, ID(0), - BLOCK2(0, 128, DESPAIR)); + const avs_coap_msg_t *res = + COAP_MSG(ACK, CONTENT, ID(0), BLOCK2(0, 128, DESPAIR)); avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, - &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); // expect handler calls - expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t){ - .data = DESPAIR, - .data_size = sizeof(DESPAIR) - 1, - .result = 0 - }); + expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t) { + .data = DESPAIR, + .data_size = sizeof(DESPAIR) - 1, + .result = 0 + }); expect_download_finished(&SIMPLE_ENV.data, 0); perform_simple_download(); @@ -764,16 +785,16 @@ AVS_UNIT_TEST(downloader, renegotiation_while_requesting_more_than_available) { COAP_MSG(ACK, CONTENT, ID(0), BLOCK2(0, 128, DESPAIR)); avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, - &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); // expect handler calls expect_next_block(&SIMPLE_ENV.data, (on_next_block_args_t) { - .data = DESPAIR, - .data_size = sizeof(DESPAIR) - 1, - .result = 0 - }); + .data = DESPAIR, + .data_size = sizeof(DESPAIR) - 1, + .result = 0 + }); expect_download_finished(&SIMPLE_ENV.data, 0); perform_simple_download(); @@ -789,14 +810,16 @@ AVS_UNIT_TEST(downloader, renegotiation_after_first_packet) { on_next_block_args_t args; memset(&args, 0, sizeof(args)); - // We request as much as we can (i.e. 64 bytes due to limit of in_buffer_size) + // We request as much as we can (i.e. 64 bytes due to limit of + // in_buffer_size) SIMPLE_ENV.base->anjay.in_buffer_size = 128; const avs_coap_msg_t *req = COAP_MSG(CON, GET, ID(0), BLOCK2(0, 64)); // The server responds with 64 bytes of the first block const avs_coap_msg_t *res = COAP_MSG(ACK, CONTENT, ID(0), BLOCK2(0, 64, DESPAIR)); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); memset(args.data, 0, sizeof(args.data)); @@ -810,7 +833,8 @@ AVS_UNIT_TEST(downloader, renegotiation_after_first_packet) { // with a different seq-num that is however valid in terms of offset, i.e. // it has seq_num=2 which corresponds to the data past the first 64 bytes res = COAP_MSG(ACK, CONTENT, ID(1), BLOCK2(2, 32, DESPAIR)); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); memset(args.data, 0, sizeof(args.data)); @@ -821,7 +845,8 @@ AVS_UNIT_TEST(downloader, renegotiation_after_first_packet) { // Last block - no surprises this time. req = COAP_MSG(CON, GET, ID(2), BLOCK2(3, 32)); res = COAP_MSG(ACK, CONTENT, ID(2), BLOCK2(3, 32, DESPAIR)); - avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, req->length); + avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, + req->length); avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); memset(args.data, 0, sizeof(args.data)); @@ -840,7 +865,8 @@ AVS_UNIT_TEST(downloader, resumption_at_some_offset) { for (size_t offset = 0; offset < sizeof(DESPAIR); ++offset) { setup_simple("coap://127.0.0.1:5683"); - avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", "5683"); + avs_unit_mocksock_expect_connect(SIMPLE_ENV.mocksock, "127.0.0.1", + "5683"); on_next_block_args_t args; memset(&args, 0, sizeof(args)); @@ -852,20 +878,21 @@ AVS_UNIT_TEST(downloader, resumption_at_some_offset) { size_t msg_id = 0; while (sizeof(DESPAIR) - current_offset > 0) { size_t seq_num = current_offset / BLOCK_SIZE; - const avs_coap_msg_t *req = COAP_MSG(CON, GET, ID(msg_id), - BLOCK2(seq_num, BLOCK_SIZE)); + const avs_coap_msg_t *req = + COAP_MSG(CON, GET, ID(msg_id), BLOCK2(seq_num, BLOCK_SIZE)); const avs_coap_msg_t *res = COAP_MSG(ACK, CONTENT, ID(msg_id), BLOCK2(seq_num, BLOCK_SIZE, DESPAIR)); avs_unit_mocksock_expect_output(SIMPLE_ENV.mocksock, &req->content, req->length); - avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, res->length); + avs_unit_mocksock_input(SIMPLE_ENV.mocksock, &res->content, + res->length); // Copy contents from the current_offset till the end of the // enclosing block. const size_t bytes_till_block_end = AVS_MIN((seq_num + 1) * BLOCK_SIZE - current_offset, - sizeof(DESPAIR) - current_offset); + sizeof(DESPAIR) - current_offset); memset(args.data, 0, sizeof(args.data)); // User handler gets the data from a specified offset, even if it is diff --git a/src/downloader/test/downloader_mock.h b/src/downloader/test/downloader_mock.h index 582494b3a..6b16dde60 100644 --- a/src/downloader/test/downloader_mock.h +++ b/src/downloader/test/downloader_mock.h @@ -21,6 +21,6 @@ AVS_UNIT_MOCK_CREATE(_anjay_create_connected_udp_socket) #define _anjay_create_connected_udp_socket(...) \ - AVS_UNIT_MOCK_WRAPPER(_anjay_create_connected_udp_socket)(__VA_ARGS__) + AVS_UNIT_MOCK_WRAPPER(_anjay_create_connected_udp_socket)(__VA_ARGS__) #endif /* ANJAY_TEST_DOWNLOADER_MOCK_H */ diff --git a/src/interface/bootstrap_core.c b/src/interface/bootstrap_core.c index 1a7e58b42..c509521f4 100644 --- a/src/interface/bootstrap_core.c +++ b/src/interface/bootstrap_core.c @@ -18,20 +18,20 @@ #include -#include #include +#include #include -#include "bootstrap_core.h" -#include "../servers_utils.h" -#include "../coap/content_format.h" #include "../anjay_core.h" -#include "../io_core.h" +#include "../coap/content_format.h" #include "../dm/discover.h" #include "../dm/query.h" +#include "../io_core.h" +#include "../servers_utils.h" +#include "bootstrap_core.h" #ifdef ANJAY_TEST -#include "test/bootstrap_mock.h" +# include "test/bootstrap_mock.h" #endif // ANJAY_TEST VISIBILITY_SOURCE_BEGIN @@ -44,7 +44,8 @@ static void cancel_client_initiated_bootstrap(anjay_t *anjay) { static int suspend_nonbootstrap_server(anjay_t *anjay, anjay_server_info_t *server, void *data) { - (void) anjay; (void) data; + (void) anjay; + (void) data; if (_anjay_server_ssid(server) != ANJAY_SSID_BOOTSTRAP) { anjay_connection_ref_t ref = { .server = server, @@ -119,20 +120,26 @@ static void bootstrap_remove_notify_changed(anjay_t *anjay, static uint8_t make_success_response_code(anjay_request_action_t action) { switch (action) { - case ANJAY_ACTION_WRITE: return AVS_COAP_CODE_CHANGED; - case ANJAY_ACTION_DELETE: return AVS_COAP_CODE_DELETED; - case ANJAY_ACTION_DISCOVER: return AVS_COAP_CODE_CONTENT; - case ANJAY_ACTION_BOOTSTRAP_FINISH: return AVS_COAP_CODE_CHANGED; - default: break; + case ANJAY_ACTION_WRITE: + return AVS_COAP_CODE_CHANGED; + case ANJAY_ACTION_DELETE: + return AVS_COAP_CODE_DELETED; + case ANJAY_ACTION_DISCOVER: + return AVS_COAP_CODE_CONTENT; + case ANJAY_ACTION_BOOTSTRAP_FINISH: + return AVS_COAP_CODE_CHANGED; + default: + break; } - return (uint8_t)(-ANJAY_ERR_INTERNAL); + return (uint8_t) (-ANJAY_ERR_INTERNAL); } -typedef int with_instance_on_demand_cb_t(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj, - anjay_iid_t iid, - anjay_input_ctx_t *in_ctx, - void *arg); +typedef int +with_instance_on_demand_cb_t(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj, + anjay_iid_t iid, + anjay_input_ctx_t *in_ctx, + void *arg); static int write_resource(anjay_t *anjay, const anjay_dm_object_def_t *const *obj, @@ -166,15 +173,15 @@ static int write_instance_inner(anjay_t *anjay, } retval = write_resource(anjay, obj, iid, in_ctx, (void *) (uintptr_t) id); - if (!retval - || retval == ANJAY_ERR_NOT_FOUND + if (!retval || retval == ANJAY_ERR_NOT_FOUND || retval == ANJAY_ERR_NOT_IMPLEMENTED) { // LwM2M spec, 5.2.7.1 BOOTSTRAP WRITE: // "When the 'Write' operation targets an Object or an Object // Instance, the LwM2M Client MUST ignore optional resources it does // not support in the payload." - so, continue on these errors. if (retval) { - anjay_log(WARNING, "Ignoring error during BOOTSTRAP WRITE to " + anjay_log(WARNING, + "Ignoring error during BOOTSTRAP WRITE to " "/%" PRIu16 "/%" PRIu16 "/%" PRIu16 ": %d", (*obj)->oid, iid, id, retval); } @@ -202,13 +209,15 @@ static int with_instance_on_demand(anjay_t *anjay, result = _anjay_dm_instance_create(anjay, obj, &new_iid, ANJAY_SSID_BOOTSTRAP, NULL); if (result) { - anjay_log(DEBUG, "Instance Create handler for object %" PRIu16 - " failed", (*obj)->oid); + anjay_log(DEBUG, + "Instance Create handler for object %" PRIu16 " failed", + (*obj)->oid); return result; } else if (iid != new_iid) { - anjay_log(DEBUG, "Instance Create handler for object %" PRIu16 - " returned Instance %" PRIu16 " while %" PRIu16 - " was expected;", + anjay_log(DEBUG, + "Instance Create handler for object %" PRIu16 + " returned Instance %" PRIu16 " while %" PRIu16 + " was expected;", (*obj)->oid, new_iid, iid); result = ANJAY_ERR_INTERNAL; } @@ -255,10 +264,11 @@ static int write_object(anjay_t *anjay, return (retval == ANJAY_GET_INDEX_END) ? 0 : retval; } -static int security_object_valid_handler(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj, - anjay_iid_t iid, - void *bootstrap_instances) { +static int +security_object_valid_handler(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj, + anjay_iid_t iid, + void *bootstrap_instances) { (void) obj; if (!_anjay_is_bootstrap_security_instance(anjay, iid)) { return 0; @@ -327,7 +337,8 @@ static int append_iid(anjay_t *anjay, const anjay_dm_object_def_t *const *obj, anjay_iid_t iid, void *iids_append_ptr_) { - (void) anjay; (void) obj; + (void) anjay; + (void) obj; AVS_LIST(anjay_iid_t) **iids_append_ptr = (AVS_LIST(anjay_iid_t) **) iids_append_ptr_; AVS_LIST(anjay_iid_t) new_element = AVS_LIST_NEW_ELEMENT(anjay_iid_t); @@ -392,8 +403,7 @@ static int delete_object(anjay_t *anjay, return 0; } -static int bootstrap_delete(anjay_t *anjay, - const anjay_request_t *request) { +static int bootstrap_delete(anjay_t *anjay, const anjay_request_t *request) { anjay_log(DEBUG, "Bootstrap Delete %s", ANJAY_DEBUG_MAKE_PATH(&request->uri)); cancel_client_initiated_bootstrap(anjay); @@ -435,8 +445,7 @@ static int bootstrap_delete(anjay_t *anjay, } #ifdef WITH_DISCOVER -static int bootstrap_discover(anjay_t *anjay, - const anjay_request_t *request) { +static int bootstrap_discover(anjay_t *anjay, const anjay_request_t *request) { if (_anjay_uri_path_has_iid(&request->uri)) { return ANJAY_ERR_BAD_REQUEST; } @@ -452,10 +461,10 @@ static int bootstrap_discover(anjay_t *anjay, return _anjay_bootstrap_discover(anjay); } #else // WITH_DISCOVER -#define bootstrap_discover(anjay, details) \ +# define bootstrap_discover(anjay, details) \ (anjay_log(ERROR, "Not supported: Bootstrap Discover %s", \ - ANJAY_DEBUG_MAKE_PATH(&details->uri)), \ - ANJAY_ERR_NOT_IMPLEMENTED) + ANJAY_DEBUG_MAKE_PATH(&details->uri)), \ + ANJAY_ERR_NOT_IMPLEMENTED) #endif // WITH_DISCOVER static void purge_bootstrap(anjay_t *anjay, const void *dummy) { @@ -473,7 +482,7 @@ static void purge_bootstrap(anjay_t *anjay, const void *dummy) { anjay_notify_queue_t notification = NULL; (void) ((retval = _anjay_dm_instance_remove(anjay, obj, iid, NULL)) || (retval = _anjay_notify_queue_instance_removed( - ¬ification, (*obj)->oid, iid)) + ¬ification, (*obj)->oid, iid)) || (retval = _anjay_notify_flush(anjay, ¬ification))); retval = _anjay_dm_transaction_finish(anjay, retval); } @@ -501,12 +510,15 @@ static int schedule_bootstrap_timeout(anjay_t *anjay) { /* This function is called on each Bootstrap Finish -- i.e. we might * have already scheduled a purge. For this reason, we need to release * the purge job handle first. */ - _anjay_sched_del(anjay->sched, &anjay->bootstrap.purge_bootstrap_handle); + _anjay_sched_del(anjay->sched, + &anjay->bootstrap.purge_bootstrap_handle); if (_anjay_sched(anjay->sched, &anjay->bootstrap.purge_bootstrap_handle, avs_time_duration_from_scalar(timeout, AVS_TIME_S), purge_bootstrap, NULL, 0)) { - anjay_log(ERROR, "Could not schedule purge of " - "Bootstrap Server Account %" PRIu16, iid); + anjay_log(ERROR, + "Could not schedule purge of " + "Bootstrap Server Account %" PRIu16, + iid); return -1; } } @@ -583,8 +595,7 @@ int _anjay_bootstrap_object_write(anjay_t *anjay, return bootstrap_write(anjay, &uri, in_ctx); } -static int invoke_action(anjay_t *anjay, - const anjay_request_t *request) { +static int invoke_action(anjay_t *anjay, const anjay_request_t *request) { anjay_input_ctx_t *in_ctx = NULL; const uint16_t format = _anjay_translate_legacy_content_format(request->content_format); @@ -597,7 +608,8 @@ static int invoke_action(anjay_t *anjay, return result; } - if (format == ANJAY_COAP_FORMAT_TLV && _anjay_uri_path_has_rid(&request->uri)) { + if (format == ANJAY_COAP_FORMAT_TLV + && _anjay_uri_path_has_rid(&request->uri)) { result = _anjay_dm_check_if_tlv_rid_matches_uri_rid( in_ctx, request->uri.rid); } @@ -631,8 +643,8 @@ int _anjay_bootstrap_perform_action(anjay_t *anjay, : AVS_COAP_FORMAT_NONE }; - int result = _anjay_coap_stream_setup_response(anjay->comm_stream, - &msg_details); + int result = + _anjay_coap_stream_setup_response(anjay->comm_stream, &msg_details); if (result) { return result; } @@ -671,12 +683,11 @@ static int send_request_bootstrap(anjay_t *anjay) { if (_anjay_copy_string_list(&details.uri_path, server_uri->uri_path) || _anjay_copy_string_list(&details.uri_query, server_uri->uri_query) - || !AVS_LIST_APPEND(&details.uri_path, - ANJAY_MAKE_STRING_LIST("bs")) - || !AVS_LIST_APPEND(&details.uri_query, - _anjay_make_query_string_list( - NULL, anjay->endpoint_name, NULL, NULL, - NULL))) { + || !AVS_LIST_APPEND(&details.uri_path, ANJAY_MAKE_STRING_LIST("bs")) + || !AVS_LIST_APPEND( + &details.uri_query, + _anjay_make_query_string_list(NULL, anjay->endpoint_name, + NULL, NULL, NULL))) { anjay_log(ERROR, "could not initialize request headers"); goto cleanup; } @@ -684,8 +695,8 @@ static int send_request_bootstrap(anjay_t *anjay) { if ((result = _anjay_coap_stream_setup_request(anjay->comm_stream, &details, NULL)) || (result = avs_stream_finish_message(anjay->comm_stream)) - || (result = check_request_bootstrap_response( - anjay->comm_stream))) { + || (result = + check_request_bootstrap_response(anjay->comm_stream))) { anjay_log(ERROR, "could not request bootstrap"); } else { anjay_log(INFO, "Request Bootstrap sent"); @@ -703,11 +714,11 @@ static void request_bootstrap(anjay_t *anjay, const void *dummy); static int schedule_request_bootstrap(anjay_t *anjay) { avs_time_monotonic_t now = avs_time_monotonic_now(); if (!avs_time_monotonic_valid( - anjay->bootstrap.client_initiated_bootstrap_last_attempt)) { + anjay->bootstrap.client_initiated_bootstrap_last_attempt)) { anjay->bootstrap.client_initiated_bootstrap_last_attempt = now; } if (!avs_time_duration_valid( - anjay->bootstrap.client_initiated_bootstrap_holdoff)) { + anjay->bootstrap.client_initiated_bootstrap_holdoff)) { anjay->bootstrap.client_initiated_bootstrap_holdoff = AVS_TIME_DURATION_ZERO; } @@ -732,10 +743,12 @@ static int schedule_request_bootstrap(anjay_t *anjay) { anjay->bootstrap.client_initiated_bootstrap_holdoff = avs_time_duration_mul( anjay->bootstrap.client_initiated_bootstrap_holdoff, 2); if (avs_time_duration_less( - anjay->bootstrap.client_initiated_bootstrap_holdoff, MIN_HOLDOFF)) { + anjay->bootstrap.client_initiated_bootstrap_holdoff, + MIN_HOLDOFF)) { anjay->bootstrap.client_initiated_bootstrap_holdoff = MIN_HOLDOFF; } else if (avs_time_duration_less( - MAX_HOLDOFF, anjay->bootstrap.client_initiated_bootstrap_holdoff)) { + MAX_HOLDOFF, + anjay->bootstrap.client_initiated_bootstrap_holdoff)) { anjay->bootstrap.client_initiated_bootstrap_holdoff = MAX_HOLDOFF; } return 0; @@ -751,9 +764,7 @@ static int find_connected_non_bootstrap(anjay_t *anjay, anjay_connection_ref_t ref = { .server = server }; - for (ref.conn_type = ANJAY_CONNECTION_FIRST_VALID_; - ref.conn_type < ANJAY_CONNECTION_LIMIT_; - ref.conn_type = (anjay_connection_type_t) (ref.conn_type + 1)) { + ANJAY_CONNECTION_TYPE_FOREACH(ref.conn_type) { if (_anjay_connection_get_online_socket(ref)) { *(bool *) flag_ptr = true; return ANJAY_FOREACH_BREAK; @@ -787,13 +798,15 @@ static void request_bootstrap(anjay_t *anjay, const void *dummy) { anjay_server_info_t *server = _anjay_servers_find_active(anjay, ANJAY_SSID_BOOTSTRAP); anjay_connection_ref_t connection; - if (!server || _anjay_server_setup_primary_connection(server)) { + if (!server) { goto finish; } connection.server = server; connection.conn_type = _anjay_server_primary_conn_type(server); - if (!connection.server - || _anjay_bind_server_stream(anjay, connection)) { + if (connection.conn_type == ANJAY_CONNECTION_UNSET) { + goto finish; + } + if (_anjay_bind_server_stream(anjay, connection)) { anjay_log(ERROR, "could not get stream for bootstrap server"); goto finish; } @@ -842,7 +855,7 @@ int _anjay_bootstrap_account_prepare(anjay_t *anjay) { } if (!avs_time_monotonic_valid( - anjay->bootstrap.client_initiated_bootstrap_last_attempt)) { + anjay->bootstrap.client_initiated_bootstrap_last_attempt)) { int64_t holdoff_s = client_hold_off_time_s(anjay); if (holdoff_s < 0) { anjay_log(INFO, "Client Hold Off Time not set or invalid, " @@ -882,5 +895,5 @@ void _anjay_bootstrap_cleanup(anjay_t *anjay) { } #ifdef ANJAY_TEST -#include "test/bootstrap.c" +# include "test/bootstrap.c" #endif // ANJAY_TEST diff --git a/src/interface/bootstrap_core.h b/src/interface/bootstrap_core.h index 05c12d9f9..f6d740418 100644 --- a/src/interface/bootstrap_core.h +++ b/src/interface/bootstrap_core.h @@ -56,23 +56,23 @@ void _anjay_bootstrap_cleanup(anjay_t *anjay); #else -#define _anjay_bootstrap_notify_regular_connection_available(anjay) ((void) 0) +# define _anjay_bootstrap_notify_regular_connection_available(anjay) \ + ((void) 0) -#define _anjay_bootstrap_server_initiated_allowed(...) (false) +# define _anjay_bootstrap_server_initiated_allowed(...) (false) -#define _anjay_bootstrap_perform_action(...) (-1) +# define _anjay_bootstrap_perform_action(...) (-1) -#define _anjay_bootstrap_account_prepare(anjay) (-1) +# define _anjay_bootstrap_account_prepare(anjay) (-1) -#define _anjay_bootstrap_update_reconnected(anjay) (-1) +# define _anjay_bootstrap_update_reconnected(anjay) (-1) -#define _anjay_bootstrap_init(...) ((void) 0) +# define _anjay_bootstrap_init(...) ((void) 0) -#define _anjay_bootstrap_cleanup(anjay) ((void) 0) +# define _anjay_bootstrap_cleanup(anjay) ((void) 0) #endif VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_INTERFACE_BOOTSTRAP_H */ - diff --git a/src/interface/register.c b/src/interface/register.c index 5a993b471..9f74a20dd 100644 --- a/src/interface/register.c +++ b/src/interface/register.c @@ -25,14 +25,14 @@ #include -#include "register.h" -#include "../dm_core.h" #include "../dm/query.h" +#include "../dm_core.h" #include "../servers_utils.h" #include "../utils_core.h" +#include "register.h" -#include "../coap/content_format.h" #include "../coap/coap_stream.h" +#include "../coap/content_format.h" VISIBILITY_SOURCE_BEGIN @@ -46,12 +46,13 @@ static int get_endpoint_path(AVS_LIST(const anjay_string_t) *out_path, avs_coap_opt_iterator_t it = AVS_COAP_OPT_ITERATOR_EMPTY; while ((result = avs_coap_msg_get_option_string_it( - msg, AVS_COAP_OPT_LOCATION_PATH, &it, - &attr_size, buffer, sizeof(buffer) - 1)) == 0) { + msg, AVS_COAP_OPT_LOCATION_PATH, &it, &attr_size, buffer, + sizeof(buffer) - 1)) + == 0) { buffer[attr_size] = '\0'; AVS_LIST(anjay_string_t) segment = - (AVS_LIST(anjay_string_t))AVS_LIST_NEW_BUFFER(attr_size + 1); + (AVS_LIST(anjay_string_t)) AVS_LIST_NEW_BUFFER(attr_size + 1); if (!segment) { anjay_log(ERROR, "out of memory"); goto fail; @@ -81,7 +82,7 @@ static const char *assemble_endpoint_path(char *buffer, if (result < 0) { return ""; } - off += (size_t)result; + off += (size_t) result; } return buffer; @@ -95,9 +96,9 @@ static int send_objects_list(avs_stream_abstract_t *stream, anjay_dm_cache_object_t *object; AVS_LIST_FOREACH(object, dm) { if (*object->version || !object->instances) { - int result = avs_stream_write_f(stream, "%s", - is_first_path ? "" : ",", - object->oid); + int result = + avs_stream_write_f(stream, "%s", + is_first_path ? "" : ",", object->oid); if (result) { return result; } @@ -128,9 +129,8 @@ static int send_objects_list(avs_stream_abstract_t *stream, return 0; } -static int get_server_lifetime(anjay_t *anjay, - anjay_ssid_t ssid, - int64_t *out_lifetime) { +static int +get_server_lifetime(anjay_t *anjay, anjay_ssid_t ssid, int64_t *out_lifetime) { anjay_iid_t server_iid; if (_anjay_find_server_iid(anjay, ssid, &server_iid)) { return -1; @@ -168,8 +168,7 @@ static int send_register(anjay_t *anjay, if (_anjay_copy_string_list(&details.uri_path, server_uri->uri_path) || _anjay_copy_string_list(&details.uri_query, server_uri->uri_query) - || !AVS_LIST_APPEND(&details.uri_path, - ANJAY_MAKE_STRING_LIST("rd")) + || !AVS_LIST_APPEND(&details.uri_path, ANJAY_MAKE_STRING_LIST("rd")) || !AVS_LIST_APPEND(&details.uri_query, _anjay_make_query_string_list( ANJAY_SUPPORTED_ENABLER_VERSION, @@ -238,7 +237,8 @@ static int query_dm_instance(anjay_t *anjay, const anjay_dm_object_def_t *const *obj, anjay_iid_t iid, void *cache_instance_insert_ptr_) { - (void) anjay; (void) obj; + (void) anjay; + (void) obj; AVS_LIST(anjay_iid_t) **cache_instance_insert_ptr = (AVS_LIST(anjay_iid_t) **) cache_instance_insert_ptr_; @@ -291,8 +291,9 @@ static int query_dm_object(anjay_t *anjay, new_object->oid = (*obj)->oid; if ((*obj)->version && avs_simple_snprintf(new_object->version, - sizeof(new_object->version), - "%s", (*obj)->version) < 0) { + sizeof(new_object->version), "%s", + (*obj)->version) + < 0) { return -1; } AVS_LIST(anjay_iid_t) *instance_insert_ptr = &new_object->instances; @@ -330,9 +331,7 @@ static int init_update_parameters(anjay_t *anjay, &out_params->lifetime_s)) { goto error; } - if (_anjay_server_actual_binding_mode(&out_params->binding_mode, server)) { - goto error; - } + _anjay_server_actual_binding_mode(&out_params->binding_mode, server); return 0; error: _anjay_update_parameters_cleanup(out_params); @@ -391,8 +390,8 @@ int _anjay_register(anjay_registration_update_ctx_t *ctx) { } _anjay_server_update_registration_info( - ctx->anjay->current_connection.server, - &endpoint_path, &ctx->new_params); + ctx->anjay->current_connection.server, &endpoint_path, + &ctx->new_params); assert(!endpoint_path); finish: @@ -438,7 +437,8 @@ static int send_update(anjay_t *anjay, const char *binding_mode = (strcmp(old_params->binding_mode, new_params->binding_mode) == 0) - ? NULL : new_params->binding_mode; + ? NULL + : new_params->binding_mode; bool dm_changed_since_last_update = !dm_caches_equal(old_params->dm, new_params->dm); @@ -446,8 +446,8 @@ static int send_update(anjay_t *anjay, .msg_type = AVS_COAP_MSG_CONFIRMABLE, .msg_code = AVS_COAP_CODE_POST, .format = dm_changed_since_last_update - ? ANJAY_COAP_FORMAT_APPLICATION_LINK - : AVS_COAP_FORMAT_NONE, + ? ANJAY_COAP_FORMAT_APPLICATION_LINK + : AVS_COAP_FORMAT_NONE, .uri_path = endpoint_path, .uri_query = _anjay_make_query_string_list(NULL, NULL, lifetime_s_ptr, binding_mode, NULL) @@ -494,8 +494,7 @@ static int check_update_response(avs_stream_abstract_t *stream) { * In the first case, the correct response is to Register again. * Otherwise, we might as well do the same, as server is required to * replace client registration information in such case. */ - anjay_log(DEBUG, "Update rejected: %s", - AVS_COAP_CODE_STRING(code)); + anjay_log(DEBUG, "Update rejected: %s", AVS_COAP_CODE_STRING(code)); return ANJAY_REGISTRATION_UPDATE_REJECTED; } else { /* Any other response is either an 5.xx (server error), in which case @@ -515,8 +514,8 @@ bool _anjay_needs_registration_update(anjay_registration_update_ctx_t *ctx) { _anjay_server_registration_info(ctx->server); const anjay_update_parameters_t *old_params = &info->last_update_params; return old_params->lifetime_s != ctx->new_params.lifetime_s - || strcmp(old_params->binding_mode, ctx->new_params.binding_mode) - || !dm_caches_equal(old_params->dm, ctx->new_params.dm); + || strcmp(old_params->binding_mode, ctx->new_params.binding_mode) + || !dm_caches_equal(old_params->dm, ctx->new_params.dm); } int _anjay_update_registration(anjay_registration_update_ctx_t *ctx) { @@ -541,8 +540,8 @@ int _anjay_update_registration(anjay_registration_update_ctx_t *ctx) { return retval; } -void -_anjay_registration_update_ctx_release(anjay_registration_update_ctx_t *ctx) { +void _anjay_registration_update_ctx_release( + anjay_registration_update_ctx_t *ctx) { _anjay_update_parameters_cleanup(&ctx->new_params); } diff --git a/src/interface/register.h b/src/interface/register.h index ee5676c2f..65d8fae58 100644 --- a/src/interface/register.h +++ b/src/interface/register.h @@ -51,8 +51,8 @@ bool _anjay_needs_registration_update(anjay_registration_update_ctx_t *ctx); */ int _anjay_update_registration(anjay_registration_update_ctx_t *ctx); -void -_anjay_registration_update_ctx_release(anjay_registration_update_ctx_t *ctx); +void _anjay_registration_update_ctx_release( + anjay_registration_update_ctx_t *ctx); int _anjay_deregister(anjay_t *anjay, AVS_LIST(const anjay_string_t) endpoint_path); diff --git a/src/interface/test/bootstrap.c b/src/interface/test/bootstrap.c index 4adf3e4a1..f20aa17f5 100644 --- a/src/interface/test/bootstrap.c +++ b/src/interface/test/bootstrap.c @@ -22,9 +22,9 @@ #include +#include "../../coap/test/utils.h" #include "../../sched_internal.h" #include "../../servers/servers_internal.h" -#include "../../coap/test/utils.h" AVS_UNIT_TEST(bootstrap_write, resource) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); @@ -93,7 +93,7 @@ AVS_UNIT_TEST(bootstrap_write, resource_with_create_invalid) { AVS_UNIT_TEST(bootstrap_write, resource_error) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "514", "7"), - CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Hello")); + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 514, 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, NOT_FOUND, ID(0xFA3E), NO_PAYLOAD); @@ -104,8 +104,10 @@ AVS_UNIT_TEST(bootstrap_write, resource_error) { AVS_UNIT_TEST(bootstrap_write, resource_with_mismatched_tlv_rid) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "514", "4"), - CONTENT_FORMAT(TLV), PAYLOAD("\xc5\x05" "Hello")); - // mismatched resource id, RID Uri-Path was 4 but in the payload it is 5 + CONTENT_FORMAT(TLV), + PAYLOAD("\xc5\x05" + "Hello")); + // mismatched resource id, RID Uri-Path was 4 but in the payload it is 5 DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, BAD_REQUEST, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); @@ -130,7 +132,8 @@ AVS_UNIT_TEST(bootstrap_write, instance) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), CONTENT_FORMAT(TLV), - PAYLOAD("\xc1\x00\x0d\xc5\x06" "Hello")); + PAYLOAD("\xc1\x00\x0d\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 0, ANJAY_MOCK_DM_INT(0, 13), 0); @@ -145,8 +148,9 @@ AVS_UNIT_TEST(bootstrap_write, instance_with_redundant_tlv_header) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), CONTENT_FORMAT(TLV), - PAYLOAD("\x08\x45\x08\xc6\x0a" "DDDDDD")); - // Redundant \x08\x45 + PAYLOAD("\x08\x45\x08\xc6\x0a" + "DDDDDD")); + // Redundant \x08\x45 _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, BAD_REQUEST, ID(0xFA3E), NO_PAYLOAD); @@ -154,12 +158,14 @@ AVS_UNIT_TEST(bootstrap_write, instance_with_redundant_tlv_header) { DM_TEST_FINISH; } -AVS_UNIT_TEST(bootstrap_write, instance_with_redundant_and_incorrect_tlv_header) { +AVS_UNIT_TEST(bootstrap_write, + instance_with_redundant_and_incorrect_tlv_header) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), CONTENT_FORMAT(TLV), - PAYLOAD("\x08\x01\x08\xc6\x0a" "DDDDDD")); - // IID is 69 but TLV payload contains IID 1 + PAYLOAD("\x08\x01\x08\xc6\x0a" + "DDDDDD")); + // IID is 69 but TLV payload contains IID 1 _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, BAD_REQUEST, ID(0xFA3E), NO_PAYLOAD); @@ -171,7 +177,9 @@ AVS_UNIT_TEST(bootstrap_write, instance_wrong_type) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), CONTENT_FORMAT(TLV), - PAYLOAD("\xc1\x00\x0d" "\x05\x06" "Hello")); + PAYLOAD("\xc1\x00\x0d" + "\x05\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 0, ANJAY_MOCK_DM_INT(0, 13), 0); @@ -185,7 +193,9 @@ AVS_UNIT_TEST(bootstrap_write, instance_error) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), CONTENT_FORMAT(TLV), - PAYLOAD("\xc1\x00\x0d" "\xc5\x06" "Hello")); + PAYLOAD("\xc1\x00\x0d" + "\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 0, ANJAY_MOCK_DM_INT(0, 13), -1); @@ -199,7 +209,9 @@ AVS_UNIT_TEST(bootstrap_write, instance_some_unsupported) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), CONTENT_FORMAT(TLV), - PAYLOAD("\xc1\x00\x0d" "\xc5\x07" "Hello")); + PAYLOAD("\xc1\x00\x0d" + "\xc5\x07" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 0, ANJAY_MOCK_DM_INT(0, 13), 0); @@ -265,9 +277,8 @@ AVS_UNIT_TEST(bootstrap_write, object_error_index_end) { _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 0, ANJAY_MOCK_DM_INT(0, 42), 0); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 42, 1); - _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 42, 3, - ANJAY_MOCK_DM_INT(0, 69), - ANJAY_GET_INDEX_END); + _anjay_mock_dm_expect_resource_write( + anjay, &OBJ, 42, 3, ANJAY_MOCK_DM_INT(0, 69), ANJAY_GET_INDEX_END); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, INTERNAL_SERVER_ERROR, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_FAILED(anjay_serve(anjay, mocksocks[0])); @@ -309,8 +320,7 @@ AVS_UNIT_TEST(bootstrap_write, object_not_found) { AVS_UNIT_TEST(bootstrap_write, object_missing) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); - DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), - CONTENT_FORMAT(TLV), + DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), CONTENT_FORMAT(TLV), PAYLOAD("\x08\x45\x03" // IID == 69 "\xc1\x00\x2a" // RID == 0 "\x08\x2a\x03" // IID == 42 @@ -419,8 +429,8 @@ AVS_UNIT_TEST(bootstrap_delete, everything) { ANJAY_IID_INVALID); _anjay_mock_dm_expect_instance_remove(anjay, &FAKE_SERVER, 2, 0); _anjay_mock_dm_expect_instance_remove(anjay, &FAKE_SERVER, 3, 0); - _anjay_mock_dm_expect_instance_it(anjay, &OBJ_WITH_RESET, 0, - 0, ANJAY_IID_INVALID); + _anjay_mock_dm_expect_instance_it(anjay, &OBJ_WITH_RESET, 0, 0, + ANJAY_IID_INVALID); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 0, 0, 34); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 1, 0, 69); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 2, 0, 514); @@ -429,9 +439,8 @@ AVS_UNIT_TEST(bootstrap_delete, everything) { _anjay_mock_dm_expect_instance_remove(anjay, &OBJ, 69, 0); _anjay_mock_dm_expect_instance_remove(anjay, &OBJ, 514, 0); _anjay_mock_dm_expect_instance_it( - anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, - 0, 0, ANJAY_IID_INVALID); + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 0, 0, + ANJAY_IID_INVALID); _anjay_mock_dm_expect_instance_it(anjay, &OBJ_WITH_RES_OPS, 0, 0, ANJAY_IID_INVALID); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, DELETED, ID(0xfa3e), NO_PAYLOAD); @@ -458,9 +467,9 @@ AVS_UNIT_TEST(bootstrap_delete, bs) { DM_TEST_FINISH; } -static int fail_notify_perform(anjay_t *anjay, - anjay_notify_queue_t queue) { - (void) anjay; (void) queue; +static int fail_notify_perform(anjay_t *anjay, anjay_notify_queue_t queue) { + (void) anjay; + (void) queue; return -1; } @@ -469,7 +478,7 @@ AVS_UNIT_TEST(bootstrap_finish, error) { DM_TEST_INIT_WITH_SSIDS(ANJAY_SSID_BOOTSTRAP); // do some Write first to call notifications - DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "514", "4"), + DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "514", "4"), CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 514, 0); _anjay_mock_dm_expect_instance_create(anjay, &OBJ, 514, @@ -482,18 +491,18 @@ AVS_UNIT_TEST(bootstrap_finish, error) { // Bootstrap Finish DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("bs")); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, INTERNAL_SERVER_ERROR, - ID(0xFA3E), NO_PAYLOAD); + ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_FAILED(anjay_serve(anjay, mocksocks[0])); - AVS_UNIT_ASSERT_EQUAL( - AVS_UNIT_MOCK_INVOCATIONS(_anjay_dm_instance_remove), 0); + AVS_UNIT_ASSERT_EQUAL(AVS_UNIT_MOCK_INVOCATIONS(_anjay_dm_instance_remove), + 0); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); - AVS_UNIT_ASSERT_EQUAL( - AVS_UNIT_MOCK_INVOCATIONS(_anjay_dm_instance_remove), 0); + AVS_UNIT_ASSERT_EQUAL(AVS_UNIT_MOCK_INVOCATIONS(_anjay_dm_instance_remove), + 0); _anjay_mock_clock_advance(avs_time_duration_from_scalar(1, AVS_TIME_S)); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); // still not removing - AVS_UNIT_ASSERT_EQUAL( - AVS_UNIT_MOCK_INVOCATIONS(_anjay_dm_instance_remove), 0); + AVS_UNIT_ASSERT_EQUAL(AVS_UNIT_MOCK_INVOCATIONS(_anjay_dm_instance_remove), + 0); DM_TEST_FINISH; } @@ -510,8 +519,8 @@ AVS_UNIT_TEST(bootstrap_invalid, invalid) { static int64_t duration_to_ns(avs_time_duration_t ts) { int64_t result; - AVS_UNIT_ASSERT_SUCCESS(avs_time_duration_to_scalar(&result, AVS_TIME_NS, - ts)); + AVS_UNIT_ASSERT_SUCCESS( + avs_time_duration_to_scalar(&result, AVS_TIME_NS, ts)); return result; } @@ -531,10 +540,11 @@ AVS_UNIT_TEST(bootstrap_backoff, backoff) { avs_unit_mocksock_expect_errno(mocksocks[0], ETIMEDOUT); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); - AVS_UNIT_ASSERT_SUCCESS(anjay_sched_time_to_next(anjay, - &sched_job_delay)); - AVS_UNIT_ASSERT_TRUE(llabs(duration_to_ns(sched_job_delay) - - duration_to_ns(backoff)) < 10); + AVS_UNIT_ASSERT_SUCCESS( + anjay_sched_time_to_next(anjay, &sched_job_delay)); + AVS_UNIT_ASSERT_TRUE( + llabs(duration_to_ns(sched_job_delay) - duration_to_ns(backoff)) + < 10); _anjay_mock_clock_advance(sched_job_delay); backoff = avs_time_duration_mul(backoff, 2); @@ -546,8 +556,9 @@ AVS_UNIT_TEST(bootstrap_backoff, backoff) { AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_time_to_next(anjay, &sched_job_delay)); - AVS_UNIT_ASSERT_TRUE(llabs(duration_to_ns(sched_job_delay) - - duration_to_ns(max_backoff)) < 10); + AVS_UNIT_ASSERT_TRUE( + llabs(duration_to_ns(sched_job_delay) - duration_to_ns(max_backoff)) + < 10); DM_TEST_FINISH; } @@ -562,7 +573,7 @@ AVS_UNIT_TEST(bootstrap_reconnect, reconnect) { // mocking reconnect is rather hard, so let's just check it's scheduled... AVS_UNIT_ASSERT_TRUE(anjay->sched->entries - == anjay->servers->servers->next_action_handle); + == anjay->servers->servers->next_action_handle); AVS_UNIT_ASSERT_EQUAL(*(anjay_ssid_t *) &anjay->sched->entries->clb_data, ANJAY_SSID_BOOTSTRAP); _anjay_sched_del(anjay->sched, @@ -574,18 +585,18 @@ AVS_UNIT_TEST(bootstrap_reconnect, reconnect) { AVS_UNIT_ASSERT_SUCCESS(avs_net_socket_connect(mocksocks[0], "", "")); int sched_job_delay_ms; - AVS_UNIT_ASSERT_SUCCESS(anjay_sched_time_to_next_ms(anjay, - &sched_job_delay_ms)); + AVS_UNIT_ASSERT_SUCCESS( + anjay_sched_time_to_next_ms(anjay, &sched_job_delay_ms)); AVS_UNIT_ASSERT_EQUAL(sched_job_delay_ms, 2999); _anjay_mock_clock_advance(avs_time_duration_from_scalar(3, AVS_TIME_S)); - const avs_coap_msg_t *request = COAP_MSG(AVS_COAP_MSG_CONFIRMABLE, POST, - ID(0x69EE), PATH("bs"), - QUERY("ep=urn:dev:os:anjay-test")); + const avs_coap_msg_t *request = + COAP_MSG(AVS_COAP_MSG_CONFIRMABLE, POST, ID(0x69EE), PATH("bs"), + QUERY("ep=urn:dev:os:anjay-test")); avs_unit_mocksock_expect_output(mocksocks[0], request->content, request->length); const avs_coap_msg_t *response = COAP_MSG(AVS_COAP_MSG_ACKNOWLEDGEMENT, - CREATED, ID(0x69EE), NO_PAYLOAD); + CREATED, ID(0x69EE), NO_PAYLOAD); avs_unit_mocksock_input(mocksocks[0], response->content, response->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); diff --git a/src/interface/test/bootstrap_mock.h b/src/interface/test/bootstrap_mock.h index d8995c560..b0d84477c 100644 --- a/src/interface/test/bootstrap_mock.h +++ b/src/interface/test/bootstrap_mock.h @@ -20,9 +20,11 @@ #include AVS_UNIT_MOCK_CREATE(_anjay_notify_perform) -#define _anjay_notify_perform(...) AVS_UNIT_MOCK_WRAPPER(_anjay_notify_perform)(__VA_ARGS__) +#define _anjay_notify_perform(...) \ + AVS_UNIT_MOCK_WRAPPER(_anjay_notify_perform)(__VA_ARGS__) AVS_UNIT_MOCK_CREATE(_anjay_dm_instance_remove) -#define _anjay_dm_instance_remove(...) AVS_UNIT_MOCK_WRAPPER(_anjay_dm_instance_remove)(__VA_ARGS__) +#define _anjay_dm_instance_remove(...) \ + AVS_UNIT_MOCK_WRAPPER(_anjay_dm_instance_remove)(__VA_ARGS__) #endif /* BOOTSTRAP_MOCK_H */ diff --git a/src/io/base64_out.c b/src/io/base64_out.c index cb138324d..8572f1152 100644 --- a/src/io/base64_out.c +++ b/src/io/base64_out.c @@ -15,8 +15,8 @@ */ #include -#include #include +#include #include @@ -61,7 +61,7 @@ static int base64_ret_bytes_flush(base64_ret_bytes_ctx_t *ctx, memcpy(chunk, ctx->bytes_cached, ctx->num_bytes_cached); size_t new_bytes_written = AVS_MIN(TEXT_CHUNK_SIZE - ctx->num_bytes_cached, - bytes_to_write); + bytes_to_write); assert(new_bytes_written <= TEXT_CHUNK_SIZE); memcpy(&chunk[ctx->num_bytes_cached], *dataptr, new_bytes_written); *dataptr += new_bytes_written; @@ -98,8 +98,7 @@ static int base64_ret_bytes_append(anjay_ret_bytes_ctx_t *ctx_, if (retval) { return retval; } - assert(ctx->num_bytes_cached + bytes_to_store - <= sizeof(ctx->bytes_cached)); + assert(ctx->num_bytes_cached + bytes_to_store <= sizeof(ctx->bytes_cached)); memcpy(&ctx->bytes_cached[ctx->num_bytes_cached], dataptr, bytes_to_store); ctx->num_bytes_cached += bytes_to_store; ctx->num_bytes_left -= bytes_to_store; @@ -111,11 +110,9 @@ static const anjay_ret_bytes_ctx_vtable_t BASE64_OUT_BYTES_VTABLE = { }; anjay_ret_bytes_ctx_t * -_anjay_base64_ret_bytes_ctx_new(avs_stream_abstract_t *stream, - size_t length) { - base64_ret_bytes_ctx_t *ctx = - (base64_ret_bytes_ctx_t *) avs_calloc(1, - sizeof(base64_ret_bytes_ctx_t)); +_anjay_base64_ret_bytes_ctx_new(avs_stream_abstract_t *stream, size_t length) { + base64_ret_bytes_ctx_t *ctx = (base64_ret_bytes_ctx_t *) avs_calloc( + 1, sizeof(base64_ret_bytes_ctx_t)); if (ctx) { ctx->vtable = &BASE64_OUT_BYTES_VTABLE; ctx->stream = stream; @@ -134,8 +131,7 @@ int _anjay_base64_ret_bytes_ctx_close(anjay_ret_bytes_ctx_t *ctx_) { ctx->num_bytes_cached); } -void -_anjay_base64_ret_bytes_ctx_delete(anjay_ret_bytes_ctx_t **ctx_) { +void _anjay_base64_ret_bytes_ctx_delete(anjay_ret_bytes_ctx_t **ctx_) { if (!ctx_ || !*ctx_) { return; } diff --git a/src/io/base64_out.h b/src/io/base64_out.h index 4514d11dd..09d6096fe 100644 --- a/src/io/base64_out.h +++ b/src/io/base64_out.h @@ -24,13 +24,10 @@ VISIBILITY_PRIVATE_HEADER_BEGIN anjay_ret_bytes_ctx_t * -_anjay_base64_ret_bytes_ctx_new(avs_stream_abstract_t *stream, - size_t length); -int -_anjay_base64_ret_bytes_ctx_close(anjay_ret_bytes_ctx_t *ctx); +_anjay_base64_ret_bytes_ctx_new(avs_stream_abstract_t *stream, size_t length); +int _anjay_base64_ret_bytes_ctx_close(anjay_ret_bytes_ctx_t *ctx); -void -_anjay_base64_ret_bytes_ctx_delete(anjay_ret_bytes_ctx_t **ctx); +void _anjay_base64_ret_bytes_ctx_delete(anjay_ret_bytes_ctx_t **ctx); VISIBILITY_PRIVATE_HEADER_END diff --git a/src/io/dynamic.c b/src/io/dynamic.c index c8f6ba9f0..3843e7004 100644 --- a/src/io/dynamic.c +++ b/src/io/dynamic.c @@ -48,18 +48,19 @@ static int *dynamic_errno_ptr(anjay_output_ctx_t *ctx) { } static anjay_output_ctx_t *spawn_opaque(dynamic_out_t *ctx) { - return _anjay_output_opaque_create( - ctx->stream, ctx->errno_ptr, &ctx->details); + return _anjay_output_opaque_create(ctx->stream, ctx->errno_ptr, + &ctx->details); } static anjay_output_ctx_t *spawn_text(dynamic_out_t *ctx) { - return _anjay_output_text_create( - ctx->stream, ctx->errno_ptr, &ctx->details); + return _anjay_output_text_create(ctx->stream, ctx->errno_ptr, + &ctx->details); } static anjay_output_ctx_t *spawn_tlv(dynamic_out_t *ctx) { - anjay_output_ctx_t *result = _anjay_output_tlv_create( - ctx->stream, ctx->errno_ptr, &ctx->details); + anjay_output_ctx_t *result = + _anjay_output_tlv_create(ctx->stream, ctx->errno_ptr, + &ctx->details); if (result && ctx->id >= 0 && _anjay_output_set_id(result, ctx->id_type, (uint16_t) ctx->id)) { _anjay_output_ctx_destroy(&result); @@ -67,11 +68,11 @@ static anjay_output_ctx_t *spawn_tlv(dynamic_out_t *ctx) { return result; } -#ifdef WITH_JSON -static anjay_output_ctx_t *spawn_json(dynamic_out_t *ctx) { +#if defined(WITH_JSON) || defined(WITH_SENML_JSON) +static anjay_output_ctx_t *spawn_json(dynamic_out_t *ctx, uint16_t format) { anjay_output_ctx_t *result = _anjay_output_json_create(ctx->stream, ctx->errno_ptr, - &ctx->details, &ctx->uri); + &ctx->details, &ctx->uri, format); if (result && ctx->id >= 0 && _anjay_output_set_id(result, ctx->id_type, (uint16_t) ctx->id)) { _anjay_output_ctx_destroy(&result); @@ -90,7 +91,7 @@ static anjay_output_ctx_t *spawn_backend(dynamic_out_t *ctx, uint16_t format) { return spawn_tlv(ctx); #ifdef WITH_JSON case ANJAY_COAP_FORMAT_JSON: - return spawn_json(ctx); + return spawn_json(ctx, ANJAY_COAP_FORMAT_JSON); #endif default: anjay_log(ERROR, "Unsupported output format: %" PRIu16, format); @@ -99,8 +100,7 @@ static anjay_output_ctx_t *spawn_backend(dynamic_out_t *ctx, uint16_t format) { } } -static anjay_output_ctx_t *ensure_backend(dynamic_out_t *ctx, - uint16_t format) { +static anjay_output_ctx_t *ensure_backend(dynamic_out_t *ctx, uint16_t format) { if (!ctx->backend) { ctx->backend = spawn_backend(ctx, format); } @@ -129,8 +129,8 @@ static void adjust_errno(dynamic_out_t *ctx, const char *function) { ctx->past_first_call = true; } -static inline int process_errno(dynamic_out_t *ctx, const char *function, - int result) { +static inline int +process_errno(dynamic_out_t *ctx, const char *function, int result) { adjust_errno(ctx, function); return result; } @@ -201,8 +201,8 @@ static int dynamic_ret_bool(anjay_output_ctx_t *ctx_, bool value) { return -1; } -static int dynamic_ret_objlnk(anjay_output_ctx_t *ctx_, - anjay_oid_t oid, anjay_iid_t iid) { +static int +dynamic_ret_objlnk(anjay_output_ctx_t *ctx_, anjay_oid_t oid, anjay_iid_t iid) { dynamic_out_t *ctx = (dynamic_out_t *) ctx_; if (ensure_backend(ctx, ANJAY_COAP_FORMAT_PLAINTEXT)) { return process_errno(ctx, "ret_objlnk", @@ -231,8 +231,8 @@ static anjay_output_ctx_t *dynamic_ret_object_start(anjay_output_ctx_t *ctx_) { return NULL; } -static int dynamic_set_id(anjay_output_ctx_t *ctx_, - anjay_id_type_t type, uint16_t id) { +static int +dynamic_set_id(anjay_output_ctx_t *ctx_, anjay_id_type_t type, uint16_t id) { dynamic_out_t *ctx = (dynamic_out_t *) ctx_; if (ctx->backend) { int result = _anjay_output_set_id(ctx->backend, type, id); @@ -326,5 +326,5 @@ int _anjay_input_dynamic_create(anjay_input_ctx_t **out, } #ifdef ANJAY_TEST -#include "test/dynamic.c" +# include "test/dynamic.c" #endif diff --git a/src/io/json_out.c b/src/io/json_out.c index a3658b2ad..3c1707c92 100644 --- a/src/io/json_out.c +++ b/src/io/json_out.c @@ -17,9 +17,8 @@ #include #include -#include #include - +#include #include #include @@ -33,6 +32,11 @@ #define json_log(level, ...) _anjay_log(json, level, __VA_ARGS__) +#define FORMAT_ERROR_MSG "unsupported JSON format" + +# define ASSERT_FORMAT_SUPPORTED(format) \ + AVS_ASSERT((format) == ANJAY_COAP_FORMAT_JSON, FORMAT_ERROR_MSG) + VISIBILITY_SOURCE_BEGIN typedef struct { @@ -47,36 +51,31 @@ typedef enum { JSON_DATA_I32, JSON_DATA_I64, JSON_DATA_BOOL, + JSON_DATA_OPAQUE, JSON_DATA_OBJLNK, JSON_DATA_STRING } json_data_type_t; -static const char *data_type_to_string(json_data_type_t type) { - switch(type) { +static const char *data_type_to_string(json_data_type_t type, uint16_t format) { + ASSERT_FORMAT_SUPPORTED(format); + switch (type) { case JSON_DATA_F32: case JSON_DATA_F64: case JSON_DATA_I32: case JSON_DATA_I64: return "v"; case JSON_DATA_BOOL: - return "bv"; + return format == ANJAY_COAP_FORMAT_JSON ? "bv" : "vb"; + case JSON_DATA_OPAQUE: + return format == ANJAY_COAP_FORMAT_JSON ? "sv" : "vd"; case JSON_DATA_OBJLNK: - return "ov"; + return format == ANJAY_COAP_FORMAT_JSON ? "ov" : "vlo"; default: - return "sv"; + return format == ANJAY_COAP_FORMAT_JSON ? "sv" : "vs"; } } -typedef struct { - json_id_t id; - json_data_type_t type; - const void *value; -} json_entry_t; - -typedef enum { - EXPECT_INDEX, - EXPECT_VALUE -} json_expected_write_t; +typedef enum { EXPECT_INDEX, EXPECT_VALUE } json_expected_write_t; typedef struct { json_expected_write_t expected_write; @@ -102,6 +101,7 @@ typedef struct json_out_struct { bool returning_array; anjay_ret_bytes_ctx_t *bytes; json_id_t next_id; + uint16_t format; } json_out_t; static json_id_t *last_path_elem(json_out_t *ctx) { @@ -111,8 +111,7 @@ static json_id_t *last_path_elem(json_out_t *ctx) { return &ctx->path[ctx->num_path_elems - 1]; } -static void -push_path_elem(json_out_t *ctx, anjay_id_type_t type, uint16_t id) { +static void push_path_elem(json_out_t *ctx, anjay_id_type_t type, uint16_t id) { const size_t max_num_elems = sizeof(ctx->path) / sizeof(*ctx->path); if (ctx->num_path_elems >= max_num_elems) { @@ -161,30 +160,20 @@ update_node_path(json_out_t *ctx, anjay_id_type_t type, uint16_t id) { } } -#define MAX_CHILD_PATH_LEN sizeof("/65535/65535/65535") +#define MAX_PATH_LEN sizeof("/65535/65535/65535/65535") -static size_t -count_child_path_elems(json_out_t *ctx) { - return ctx->num_path_elems - ctx->num_base_path_elems; -} - -static ssize_t child_path_to_string(json_out_t *ctx, char *dest, size_t size) { - switch (count_child_path_elems(ctx)) { - case 1: - return avs_simple_snprintf(dest, size, "/%"PRId32, - ctx->path[ctx->num_base_path_elems].id); - case 2: - return avs_simple_snprintf(dest, size, "/%"PRId32"/%"PRId32, - ctx->path[ctx->num_base_path_elems].id, - ctx->path[ctx->num_base_path_elems + 1].id); - case 3: - return avs_simple_snprintf(dest, size, "/%"PRId32"/%"PRId32"/%"PRId32, - ctx->path[ctx->num_base_path_elems].id, - ctx->path[ctx->num_base_path_elems + 1].id, - ctx->path[ctx->num_base_path_elems + 2].id); - default: - return -1; +static int +path_to_string(json_out_t *ctx, size_t start_index, char *dest, size_t size) { + for (size_t i = start_index; i < ctx->num_path_elems; i++) { + int written_chars = + avs_simple_snprintf(dest, size, "/%" PRId32, ctx->path[i].id); + if (written_chars < 0) { + return -1; + } + dest += written_chars; + size -= (size_t) written_chars; } + return 0; } typedef struct { @@ -227,10 +216,9 @@ static int write_quoted_string(avs_stream_abstract_t *stream, } else if (value[i] == '\t') { retval = avs_stream_write(stream, "\\t", 2); } else if ((uint8_t) value[i] < 0x20) { - const int nibble0 = ((uint8_t)value[i] >> 4) & 0xF; - const int nibble1 = ((uint8_t)value[i]) & 0xF; - retval = - avs_stream_write_f(stream, "\\u00%x%x", nibble0, nibble1); + const int nibble0 = ((uint8_t) value[i] >> 4) & 0xF; + const int nibble1 = ((uint8_t) value[i]) & 0xF; + retval = avs_stream_write_f(stream, "\\u00%x%x", nibble0, nibble1); } else { retval = avs_stream_write(stream, &value[i], 1); } @@ -243,9 +231,10 @@ static int write_quoted_string(avs_stream_abstract_t *stream, static int write_variable(avs_stream_abstract_t *stream, json_data_type_t type, + uint16_t json_format, const void *value) { - int retval = - avs_stream_write_f(stream, "\"%s\":", data_type_to_string(type)); + int retval = avs_stream_write_f( + stream, "\"%s\":", data_type_to_string(type, json_format)); if (retval) { return retval; } @@ -262,12 +251,11 @@ static int write_variable(avs_stream_abstract_t *stream, case JSON_DATA_BOOL: return avs_stream_write_f(stream, "%s", (*(const bool *) value) ? "true" : "false"); - case JSON_DATA_OBJLNK: - { - const packed_objlnk_t objlnk = *(const packed_objlnk_t *) value; - return avs_stream_write_f(stream, "\"%" PRIu16 ":%" PRIu16 "\"", - objlnk.oid, objlnk.iid); - } + case JSON_DATA_OBJLNK: { + const packed_objlnk_t objlnk = *(const packed_objlnk_t *) value; + return avs_stream_write_f(stream, "\"%" PRIu16 ":%" PRIu16 "\"", + objlnk.oid, objlnk.iid); + } case JSON_DATA_STRING: return write_quoted_string(stream, (const char *) value); default: @@ -279,20 +267,24 @@ static int write_variable(avs_stream_abstract_t *stream, static int write_uri(avs_stream_abstract_t *stream, const anjay_uri_path_t *path) { int retval = avs_stream_write_f(stream, "/%d", path->oid); - if (!retval && _anjay_uri_path_has_iid(path)) { + if (!retval && _anjay_uri_path_has_iid(path)) { retval = avs_stream_write_f(stream, "/%d", path->iid); } - if (!retval && _anjay_uri_path_has_rid(path)) { + if (!retval && _anjay_uri_path_has_rid(path)) { retval = avs_stream_write_f(stream, "/%d", path->rid); } return retval; } static int write_element_name(json_out_t *ctx) { + ASSERT_FORMAT_SUPPORTED(ctx->format); const char *name = NULL; - char buf[MAX_CHILD_PATH_LEN]; - if (count_child_path_elems(ctx)) { - if (child_path_to_string(ctx, buf, sizeof(buf)) < 0) { + char buf[MAX_PATH_LEN]; + if (ctx->num_path_elems - ctx->num_base_path_elems) { + size_t start_index = ctx->format == ANJAY_COAP_FORMAT_JSON + ? ctx->num_base_path_elems + : 0; + if (path_to_string(ctx, start_index, buf, sizeof(buf))) { return -1; } name = buf; @@ -312,14 +304,13 @@ static int write_response_element(json_out_t *ctx, const void *value) { int retval; (void) ((retval = write_element_name(ctx)) - || (retval = write_variable(ctx->stream, type, value)) + || (retval = write_variable(ctx->stream, type, ctx->format, value)) || (retval = avs_stream_write(ctx->stream, "}", 1))); return retval; } -static int process_array_value(json_out_t *ctx, - json_data_type_t type, - const void *value) { +static int +process_array_value(json_out_t *ctx, json_data_type_t type, const void *value) { if (ctx->array_ctx.expected_write != EXPECT_VALUE) { json_log(ERROR, "expected array index, but got a value instead"); return -1; @@ -352,8 +343,7 @@ static int finish_ret_bytes(json_out_t *ctx) { } static int maybe_write_separator(json_out_t *ctx) { - if (ctx->needs_separator - && avs_stream_write(ctx->stream, ",", 1)) { + if (ctx->needs_separator && avs_stream_write(ctx->stream, ",", 1)) { return -1; } ctx->needs_separator = true; @@ -389,9 +379,9 @@ static anjay_ret_bytes_ctx_t *json_ret_bytes(anjay_output_ctx_t *ctx_, int retval; (void) ((retval = maybe_write_separator(ctx)) || (retval = write_element_name(ctx)) - || (retval = avs_stream_write_f(ctx->stream, "\"%s\":\"", - data_type_to_string( - JSON_DATA_STRING)))); + || (retval = avs_stream_write_f( + ctx->stream, "\"%s\":\"", + data_type_to_string(JSON_DATA_OPAQUE, ctx->format)))); if (retval) { return NULL; } @@ -498,9 +488,8 @@ static int json_ret_object_finish(anjay_output_ctx_t *ctx) { return 0; } -static int json_set_id(anjay_output_ctx_t *ctx_, - anjay_id_type_t type, - uint16_t id) { +static int +json_set_id(anjay_output_ctx_t *ctx_, anjay_id_type_t type, uint16_t id) { if (type == ANJAY_ID_RIID) { return json_ret_array_index(ctx_, id); } @@ -519,8 +508,15 @@ static int json_set_id(anjay_output_ctx_t *ctx_, return 0; } -static int write_response_finish(avs_stream_abstract_t *stream) { - return avs_stream_write(stream, "]}", 2); +static int write_response_finish(avs_stream_abstract_t *stream, + uint16_t json_format) { + switch (json_format) { + case ANJAY_COAP_FORMAT_JSON: + return avs_stream_write(stream, "]}", 2); + default: + AVS_UNREACHABLE(FORMAT_ERROR_MSG); + return -1; + } } static int json_output_close(anjay_output_ctx_t *ctx_) { @@ -531,33 +527,41 @@ static int json_output_close(anjay_output_ctx_t *ctx_) { return result; } } - return write_response_finish(ctx->stream); + return write_response_finish(ctx->stream, ctx->format); } static const anjay_output_ctx_vtable_t JSON_OUT_VTABLE = { - json_errno_ptr, - json_ret_bytes, - json_ret_string, - json_ret_i32, - json_ret_i64, - json_ret_float, - json_ret_double, - json_ret_bool, - json_ret_objlnk, - json_ret_array_start, - json_ret_array_finish, - json_ret_object_start, - json_ret_object_finish, - json_set_id, - json_output_close + .errno_ptr = json_errno_ptr, + .bytes_begin = json_ret_bytes, + .string = json_ret_string, + .i32 = json_ret_i32, + .i64 = json_ret_i64, + .f32 = json_ret_float, + .f64 = json_ret_double, + .boolean = json_ret_bool, + .objlnk = json_ret_objlnk, + .array_start = json_ret_array_start, + .array_finish = json_ret_array_finish, + .object_start = json_ret_object_start, + .object_finish = json_ret_object_finish, + .set_id = json_set_id, + .close = json_output_close }; static int write_response_preamble(avs_stream_abstract_t *stream, + uint16_t json_format, const anjay_uri_path_t *base) { int retval; - (void) ((retval = avs_stream_write_f(stream, "%s", "{\"bn\":\"")) - || (retval = write_uri(stream, base)) - || (retval = avs_stream_write_f(stream, "%s", "\",\"e\":["))); + switch (json_format) { + case ANJAY_COAP_FORMAT_JSON: + (void) ((retval = avs_stream_write_f(stream, "%s", "{\"bn\":\"")) + || (retval = write_uri(stream, base)) + || (retval = avs_stream_write_f(stream, "%s", "\",\"e\":["))); + break; + default: + AVS_UNREACHABLE(FORMAT_ERROR_MSG); + retval = -1; + } return retval; } @@ -565,12 +569,15 @@ anjay_output_ctx_t * _anjay_output_json_create(avs_stream_abstract_t *stream, int *errno_ptr, anjay_msg_details_t *inout_details, - const anjay_uri_path_t *uri) { + const anjay_uri_path_t *uri, + uint16_t format) { + ASSERT_FORMAT_SUPPORTED(format); json_out_t *ctx = (json_out_t *) avs_calloc(1, sizeof(json_out_t)); if (ctx) { ctx->vtable = &JSON_OUT_VTABLE; ctx->errno_ptr = errno_ptr; ctx->stream = stream; + ctx->format = format; if (_anjay_uri_path_has_oid(uri)) { update_node_path(ctx, ANJAY_ID_OID, uri->oid); ++ctx->num_base_path_elems; @@ -584,12 +591,12 @@ _anjay_output_json_create(avs_stream_abstract_t *stream, ++ctx->num_base_path_elems; } - if ((*errno_ptr = _anjay_handle_requested_format( - &inout_details->format, ANJAY_COAP_FORMAT_JSON)) - || _anjay_coap_stream_setup_response(stream, inout_details)) { + if ((*errno_ptr = _anjay_handle_requested_format(&inout_details->format, + format)) + || _anjay_coap_stream_setup_response(stream, inout_details)) { goto error; } - if (write_response_preamble(stream, uri)) { + if (write_response_preamble(stream, format, uri)) { json_log(ERROR, "cannot write response preamble"); goto error; } diff --git a/src/io/opaque.c b/src/io/opaque.c index a3258b1a0..a33331c35 100644 --- a/src/io/opaque.c +++ b/src/io/opaque.c @@ -83,9 +83,10 @@ _anjay_output_opaque_create(avs_stream_abstract_t *stream, int *errno_ptr, anjay_msg_details_t *inout_details) { opaque_out_t *ctx = (opaque_out_t *) avs_calloc(1, sizeof(opaque_out_t)); - if (ctx && ((*errno_ptr = _anjay_handle_requested_format( - &inout_details->format, ANJAY_COAP_FORMAT_OPAQUE)) - || _anjay_coap_stream_setup_response(stream, inout_details))) { + if (ctx + && ((*errno_ptr = _anjay_handle_requested_format( + &inout_details->format, ANJAY_COAP_FORMAT_OPAQUE)) + || _anjay_coap_stream_setup_response(stream, inout_details))) { avs_free(ctx); return NULL; } diff --git a/src/io/output_buf.c b/src/io/output_buf.c index f69264e3a..928c9dc31 100644 --- a/src/io/output_buf.c +++ b/src/io/output_buf.c @@ -20,12 +20,11 @@ VISIBILITY_SOURCE_BEGIN -static int output_buf_set_id(anjay_output_ctx_t *ctx, - anjay_id_type_t type, - uint16_t id) { - (void)ctx; - (void)type; - (void)id; +static int +output_buf_set_id(anjay_output_ctx_t *ctx, anjay_id_type_t type, uint16_t id) { + (void) ctx; + (void) type; + (void) id; return 0; } @@ -33,46 +32,44 @@ static int output_buf_ret_bytes(anjay_output_ctx_t *ctx_, const void *data, size_t data_size) { anjay_output_buf_ctx_t *ctx = (anjay_output_buf_ctx_t *) ctx_; - return avs_stream_write((avs_stream_abstract_t*)ctx->stream, - data, data_size); + return avs_stream_write((avs_stream_abstract_t *) ctx->stream, data, + data_size); } -static int output_buf_ret_string(anjay_output_ctx_t *ctx, - const char *str) { +static int output_buf_ret_string(anjay_output_ctx_t *ctx, const char *str) { return output_buf_ret_bytes(ctx, str, strlen(str)); } -#define DEFINE_RET_HANDLER(Suffix, Type) \ - static int output_buf_ret_##Suffix(anjay_output_ctx_t *ctx, \ - Type value) { \ - return output_buf_ret_bytes(ctx, &value, sizeof(value)); \ +#define DEFINE_RET_HANDLER(Suffix, Type) \ + static int output_buf_ret_##Suffix(anjay_output_ctx_t *ctx, Type value) { \ + return output_buf_ret_bytes(ctx, &value, sizeof(value)); \ } DEFINE_RET_HANDLER(i64, int64_t) // output_buf_ret_i64 DEFINE_RET_HANDLER(double, double) // output_buf_ret_double DEFINE_RET_HANDLER(bool, bool) // output_buf_ret_bool -#define DEFINE_FORWARD_HANDLER(Suffix, Type, Backend) \ +#define DEFINE_FORWARD_HANDLER(Suffix, Type, Backend) \ static int output_buf_ret_##Suffix(anjay_output_ctx_t *ctx, Type value) { \ - return output_buf_ret_##Backend(ctx, value); \ + return output_buf_ret_##Backend(ctx, value); \ } DEFINE_FORWARD_HANDLER(i32, int32_t, i64) // output_buf_ret_i32 DEFINE_FORWARD_HANDLER(float, float, double) // output_buf_ret_float static anjay_ret_bytes_ctx_t * -output_buf_ret_bytes_begin(anjay_output_ctx_t *ctx, - size_t length) { - (void)length; - return (anjay_ret_bytes_ctx_t*) - &((anjay_output_buf_ctx_t*)ctx)->ret_bytes_vtable; +output_buf_ret_bytes_begin(anjay_output_ctx_t *ctx, size_t length) { + (void) length; + return (anjay_ret_bytes_ctx_t *) &((anjay_output_buf_ctx_t *) ctx) + ->ret_bytes_vtable; } static int output_buf_ret_bytes_append(anjay_ret_bytes_ctx_t *ctx, const void *data, size_t size) { - return output_buf_ret_bytes((anjay_output_ctx_t*) - AVS_CONTAINER_OF(ctx, anjay_output_buf_ctx_t, ret_bytes_vtable), + return output_buf_ret_bytes( + (anjay_output_ctx_t *) AVS_CONTAINER_OF(ctx, anjay_output_buf_ctx_t, + ret_bytes_vtable), data, size); } diff --git a/src/io/test/bigdata.h b/src/io/test/bigdata.h index 2512bddd4..9046f5680 100644 --- a/src/io/test/bigdata.h +++ b/src/io/test/bigdata.h @@ -17,6 +17,7 @@ #ifndef ANJAY_IO_TEST_BIGDATA_H #define ANJAY_IO_TEST_BIGDATA_H +// clang-format off #define DATA10B "0123456789" #define DATA100B \ DATA10B DATA10B DATA10B DATA10B DATA10B \ @@ -37,6 +38,6 @@ DATA1MB DATA1MB DATA1MB DATA1MB DATA1MB \ DATA1MB DATA1MB DATA1MB DATA1MB DATA1MB #define DATA20MB DATA10MB DATA10MB +// clang-format on #endif /* ANJAY_IO_TEST_BIGDATA_H */ - diff --git a/src/io/test/dynamic.c b/src/io/test/dynamic.c index 1bb2244f9..3e8f85708 100644 --- a/src/io/test/dynamic.c +++ b/src/io/test/dynamic.c @@ -21,8 +21,8 @@ #include #include -#include #include +#include #include @@ -42,7 +42,6 @@ static const anjay_coap_stream_ext_t COAPIZATION = { .setup_response = test_setup_for_sending, }; - static const avs_stream_v_table_extension_t COAPIZED_VTABLE_EXT[] = { { ANJAY_COAP_STREAM_EXTENSION, &COAPIZATION }, AVS_STREAM_V_TABLE_EXTENSION_NULL @@ -58,34 +57,36 @@ AVS_UNIT_SUITE_INIT(dynamic_out, verbose) { COAPIZED_VTABLE.extension_list = COAPIZED_VTABLE_EXT; } -#define DETAILS_TEMPLATE(Format) { \ - .msg_type = AVS_COAP_MSG_NON_CONFIRMABLE, \ - .format = Format \ -} - -static const avs_stream_outbuf_t COAPIZED_OUTBUF - = {&COAPIZED_VTABLE, NULL, 0, 0, 0}; - -#define TEST_ENV_WITH_FORMAT(Size, Format) \ - char buf[Size]; \ - anjay_msg_details_t details = DETAILS_TEMPLATE(Format); \ - avs_stream_outbuf_t outbuf = COAPIZED_OUTBUF; \ - avs_stream_outbuf_set_buffer(&outbuf, buf, sizeof(buf)); \ - COAP_FORMAT = -1; \ - int outctx_errno = 0; \ - anjay_uri_path_t no_uri; \ - memset(&no_uri, 0, sizeof(no_uri)); \ - anjay_output_ctx_t *out = \ +#define DETAILS_TEMPLATE(Format) \ + { \ + .msg_type = AVS_COAP_MSG_NON_CONFIRMABLE, \ + .format = Format \ + } + +static const avs_stream_outbuf_t COAPIZED_OUTBUF = { &COAPIZED_VTABLE, NULL, 0, + 0, 0 }; + +#define TEST_ENV_WITH_FORMAT(Size, Format) \ + char buf[Size]; \ + anjay_msg_details_t details = DETAILS_TEMPLATE(Format); \ + avs_stream_outbuf_t outbuf = COAPIZED_OUTBUF; \ + avs_stream_outbuf_set_buffer(&outbuf, buf, sizeof(buf)); \ + COAP_FORMAT = -1; \ + int outctx_errno = 0; \ + anjay_uri_path_t no_uri; \ + memset(&no_uri, 0, sizeof(no_uri)); \ + anjay_output_ctx_t *out = \ _anjay_output_dynamic_create((avs_stream_abstract_t *) &outbuf, \ &outctx_errno, &details, &no_uri) #define TEST_ENV(Size) TEST_ENV_WITH_FORMAT(Size, AVS_COAP_FORMAT_NONE) -#define VERIFY_BYTES(Data) do { \ - AVS_UNIT_ASSERT_EQUAL(avs_stream_outbuf_offset(&outbuf), \ - sizeof(Data) - 1); \ - AVS_UNIT_ASSERT_EQUAL_BYTES(buf, Data); \ -} while (0) +#define VERIFY_BYTES(Data) \ + do { \ + AVS_UNIT_ASSERT_EQUAL(avs_stream_outbuf_offset(&outbuf), \ + sizeof(Data) - 1); \ + AVS_UNIT_ASSERT_EQUAL_BYTES(buf, Data); \ + } while (0) AVS_UNIT_TEST(dynamic_out, bytes) { TEST_ENV(512); @@ -196,11 +197,11 @@ AVS_UNIT_TEST(dynamic_out, array) { AVS_UNIT_ASSERT_SUCCESS(anjay_ret_array_finish(array)); AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); - VERIFY_BYTES( - "\x88\x2A\x13" // array - "\x41\x05\x2A" // first entry - "\x48\x45\x0D" "Hello, world!" //second entry - ); + VERIFY_BYTES("\x88\x2A\x13" // array + "\x41\x05\x2A" // first entry + "\x48\x45\x0D" + "Hello, world!" // second entry + ); AVS_UNIT_ASSERT_EQUAL(COAP_FORMAT, ANJAY_COAP_FORMAT_TLV); } @@ -215,10 +216,9 @@ AVS_UNIT_TEST(dynamic_out, object) { AVS_UNIT_ASSERT_SUCCESS(_anjay_output_object_finish(obj)); AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); - VERIFY_BYTES( - "\x04\x2A" // object - "\xC2\x45\x02\x02" // entry - ); + VERIFY_BYTES("\x04\x2A" // object + "\xC2\x45\x02\x02" // entry + ); AVS_UNIT_ASSERT_EQUAL(COAP_FORMAT, ANJAY_COAP_FORMAT_TLV); } @@ -254,32 +254,32 @@ AVS_UNIT_TEST(dynamic_out, format_mismatch) { /////////////////////////////////////////////////////////////////////// DECODING -#define TEST_ENV_COMMON(Data) \ - avs_net_abstract_socket_t *mocksock; \ - _anjay_mocksock_create(&mocksock, 1252, 1252); \ - avs_unit_mocksock_expect_connect(mocksock, "", ""); \ +#define TEST_ENV_COMMON(Data) \ + avs_net_abstract_socket_t *mocksock; \ + _anjay_mocksock_create(&mocksock, 1252, 1252); \ + avs_unit_mocksock_expect_connect(mocksock, "", ""); \ AVS_UNIT_ASSERT_SUCCESS(avs_net_socket_connect(mocksock, "", "")); \ - avs_stream_abstract_t *coap = NULL; \ - SCOPED_MOCK_COAP_STREAM(mock_coap_stream_ctx) = \ + avs_stream_abstract_t *coap = NULL; \ + SCOPED_MOCK_COAP_STREAM(mock_coap_stream_ctx) = \ _anjay_mock_coap_stream_create(&coap, mocksock, 256, 256); \ avs_unit_mocksock_input(mocksock, Data, sizeof(Data) - 1) -#define TEST_ENV(Data) \ - TEST_ENV_COMMON(Data); \ - anjay_input_ctx_t *ctx; \ +#define TEST_ENV(Data) \ + TEST_ENV_COMMON(Data); \ + anjay_input_ctx_t *ctx; \ AVS_UNIT_ASSERT_SUCCESS(_anjay_input_dynamic_create(&ctx, &coap, true)); \ AVS_UNIT_ASSERT_NOT_NULL(ctx) #define TEST_TEARDOWN _anjay_input_ctx_destroy(&ctx) #define COAP_HEADER(ContentFormatFirstOpt) \ - "\x50\x01\x00\x00" ContentFormatFirstOpt "\xFF" + "\x50\x01\x00\x00" ContentFormatFirstOpt "\xFF" #define LITERAL_COAP_FORMAT_FIRSTOPT_PLAINTEXT "\xC0" -#define LITERAL_COAP_FORMAT_FIRSTOPT_TLV "\xC2\x2d\x16" -#define LITERAL_COAP_FORMAT_FIRSTOPT_JSON "\xC2\x2d\x17" -#define LITERAL_COAP_FORMAT_FIRSTOPT_OPAQUE "\xC1\x2A" -#define LITERAL_COAP_FORMAT_FIRSTOPT_UNKNOWN "\xC2\x69\x69" +#define LITERAL_COAP_FORMAT_FIRSTOPT_TLV "\xC2\x2d\x16" +#define LITERAL_COAP_FORMAT_FIRSTOPT_JSON "\xC2\x2d\x17" +#define LITERAL_COAP_FORMAT_FIRSTOPT_OPAQUE "\xC1\x2A" +#define LITERAL_COAP_FORMAT_FIRSTOPT_UNKNOWN "\xC2\x69\x69" AVS_UNIT_TEST(dynamic_in, plain) { TEST_ENV(COAP_HEADER(LITERAL_COAP_FORMAT_FIRSTOPT_PLAINTEXT) "NDI="); @@ -302,7 +302,8 @@ AVS_UNIT_TEST(dynamic_in, plain) { } AVS_UNIT_TEST(dynamic_in, no_content_format) { - TEST_ENV_COMMON("\x50\x01\x00\x00\xFF" "514"); + TEST_ENV_COMMON("\x50\x01\x00\x00\xFF" + "514"); anjay_input_ctx_t *ctx; AVS_UNIT_ASSERT_SUCCESS(_anjay_input_dynamic_create(&ctx, &coap, true)); _anjay_input_ctx_destroy(&ctx); diff --git a/src/io/test/text.c b/src/io/test/text.c index 0eecb4dae..26848e9f9 100644 --- a/src/io/test/text.c +++ b/src/io/test/text.c @@ -30,33 +30,29 @@ static void text_out_destroy(anjay_ret_bytes_ctx_t ***ctx) { } } -#define TEST_ENV(Size) \ - char buf[Size]; \ +#define TEST_ENV(Size) \ + char buf[Size]; \ avs_stream_outbuf_t outbuf = AVS_STREAM_OUTBUF_STATIC_INITIALIZER; \ - int outctx_errno; \ - text_out_t out = { \ - &TEXT_OUT_VTABLE, \ - NULL, \ - &outctx_errno, \ - (avs_stream_abstract_t *) &outbuf, \ - false \ - }; \ - SCOPED_PTR(anjay_ret_bytes_ctx_t *, text_out_destroy) _ret_bytes = &out.bytes; \ - (void) _ret_bytes; \ - avs_stream_outbuf_set_buffer(&outbuf, buf, sizeof(buf)) \ + int outctx_errno; \ + text_out_t out = { &TEXT_OUT_VTABLE, NULL, &outctx_errno, \ + (avs_stream_abstract_t *) &outbuf, false }; \ + SCOPED_PTR(anjay_ret_bytes_ctx_t *, text_out_destroy) \ + _ret_bytes = &out.bytes; \ + (void) _ret_bytes; \ + avs_stream_outbuf_set_buffer(&outbuf, buf, sizeof(buf)) static void stringify_buf(avs_stream_outbuf_t *outbuf) { outbuf->message_finished = 0; - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write((avs_stream_abstract_t *) outbuf, - "", 1)); + AVS_UNIT_ASSERT_SUCCESS( + avs_stream_write((avs_stream_abstract_t *) outbuf, "", 1)); } AVS_UNIT_TEST(text_out, string) { TEST_ENV(512); static const char TEST_STRING[] = "Hello, world!"; - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_string((anjay_output_ctx_t *) &out, - TEST_STRING)); + AVS_UNIT_ASSERT_SUCCESS( + anjay_ret_string((anjay_output_ctx_t *) &out, TEST_STRING)); stringify_buf(&outbuf); AVS_UNIT_ASSERT_EQUAL_STRING(buf, TEST_STRING); } @@ -65,16 +61,18 @@ AVS_UNIT_TEST(text_out, string_err) { TEST_ENV(8); static const char TEST_STRING[] = "Hello, world!"; - AVS_UNIT_ASSERT_FAILED(anjay_ret_string((anjay_output_ctx_t *) &out, - TEST_STRING)); + AVS_UNIT_ASSERT_FAILED( + anjay_ret_string((anjay_output_ctx_t *) &out, TEST_STRING)); } -#define TEST_i32(Val) do { \ - TEST_ENV(512); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_i32((anjay_output_ctx_t *) &out, Val)); \ - stringify_buf(&outbuf); \ - AVS_UNIT_ASSERT_EQUAL_STRING(buf, #Val); \ -} while (false) +#define TEST_i32(Val) \ + do { \ + TEST_ENV(512); \ + AVS_UNIT_ASSERT_SUCCESS( \ + anjay_ret_i32((anjay_output_ctx_t *) &out, Val)); \ + stringify_buf(&outbuf); \ + AVS_UNIT_ASSERT_EQUAL_STRING(buf, #Val); \ + } while (false) AVS_UNIT_TEST(text_out, i32) { TEST_i32(514); @@ -86,13 +84,14 @@ AVS_UNIT_TEST(text_out, i32) { #undef TEST_i32 -#define TEST_i64(Val) do { \ - TEST_ENV(512); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_i64((anjay_output_ctx_t *) &out, \ - Val##LL)); \ - stringify_buf(&outbuf); \ - AVS_UNIT_ASSERT_EQUAL_STRING(buf, #Val); \ -} while (false) +#define TEST_i64(Val) \ + do { \ + TEST_ENV(512); \ + AVS_UNIT_ASSERT_SUCCESS( \ + anjay_ret_i64((anjay_output_ctx_t *) &out, Val##LL)); \ + stringify_buf(&outbuf); \ + AVS_UNIT_ASSERT_EQUAL_STRING(buf, #Val); \ + } while (false) AVS_UNIT_TEST(text_out, i64) { TEST_i64(-1000000000000000000); @@ -101,13 +100,14 @@ AVS_UNIT_TEST(text_out, i64) { #undef TEST_i64 -#define TEST_FLOAT(Val) do { \ - TEST_ENV(512); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_float((anjay_output_ctx_t *) &out, \ - (float) (Val))); \ - stringify_buf(&outbuf); \ - AVS_UNIT_ASSERT_EQUAL_STRING(buf, #Val); \ -} while (false) +#define TEST_FLOAT(Val) \ + do { \ + TEST_ENV(512); \ + AVS_UNIT_ASSERT_SUCCESS( \ + anjay_ret_float((anjay_output_ctx_t *) &out, (float) (Val))); \ + stringify_buf(&outbuf); \ + AVS_UNIT_ASSERT_EQUAL_STRING(buf, #Val); \ + } while (false) AVS_UNIT_TEST(text_out, f32) { TEST_FLOAT(0); @@ -119,12 +119,14 @@ AVS_UNIT_TEST(text_out, f32) { #undef TEST_FLOAT -#define TEST_DOUBLE(Val) do { \ - TEST_ENV(512); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_double((anjay_output_ctx_t *) &out, Val)); \ - stringify_buf(&outbuf); \ - AVS_UNIT_ASSERT_EQUAL_STRING(buf, #Val); \ -} while (false) +#define TEST_DOUBLE(Val) \ + do { \ + TEST_ENV(512); \ + AVS_UNIT_ASSERT_SUCCESS( \ + anjay_ret_double((anjay_output_ctx_t *) &out, Val)); \ + stringify_buf(&outbuf); \ + AVS_UNIT_ASSERT_EQUAL_STRING(buf, #Val); \ + } while (false) AVS_UNIT_TEST(text_out, f64) { TEST_DOUBLE(0); @@ -136,12 +138,14 @@ AVS_UNIT_TEST(text_out, f64) { #undef TEST_DOUBLE -#define TEST_BOOL(Val) do { \ - TEST_ENV(512); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_bool((anjay_output_ctx_t *) &out, Val)); \ - stringify_buf(&outbuf); \ - AVS_UNIT_ASSERT_EQUAL_STRING(buf, Val ? "1" : "0"); \ -} while (false) +#define TEST_BOOL(Val) \ + do { \ + TEST_ENV(512); \ + AVS_UNIT_ASSERT_SUCCESS( \ + anjay_ret_bool((anjay_output_ctx_t *) &out, Val)); \ + stringify_buf(&outbuf); \ + AVS_UNIT_ASSERT_EQUAL_STRING(buf, Val ? "1" : "0"); \ + } while (false) AVS_UNIT_TEST(text_out, boolean) { TEST_BOOL(true); @@ -153,13 +157,14 @@ AVS_UNIT_TEST(text_out, boolean) { #undef TEST_BOOL -#define TEST_OBJLNK(Oid, Iid) do { \ - TEST_ENV(512); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_objlnk((anjay_output_ctx_t *) &out, \ - Oid, Iid)); \ - stringify_buf(&outbuf); \ - AVS_UNIT_ASSERT_EQUAL_STRING(buf, #Oid ":" #Iid); \ -} while (false) +#define TEST_OBJLNK(Oid, Iid) \ + do { \ + TEST_ENV(512); \ + AVS_UNIT_ASSERT_SUCCESS( \ + anjay_ret_objlnk((anjay_output_ctx_t *) &out, Oid, Iid)); \ + stringify_buf(&outbuf); \ + AVS_UNIT_ASSERT_EQUAL_STRING(buf, #Oid ":" #Iid); \ + } while (false) AVS_UNIT_TEST(text_out, objlnk) { TEST_OBJLNK(0, 0); @@ -174,7 +179,8 @@ AVS_UNIT_TEST(text_out, objlnk) { AVS_UNIT_TEST(text_out, unimplemented) { TEST_ENV(512); - AVS_UNIT_ASSERT_NOT_NULL(anjay_ret_bytes_begin((anjay_output_ctx_t *) &out, 3)); + AVS_UNIT_ASSERT_NOT_NULL( + anjay_ret_bytes_begin((anjay_output_ctx_t *) &out, 3)); AVS_UNIT_ASSERT_NULL(anjay_ret_array_start((anjay_output_ctx_t *) &out)); AVS_UNIT_ASSERT_FAILED( anjay_ret_array_index((anjay_output_ctx_t *) &out, 1)); @@ -185,23 +191,24 @@ AVS_UNIT_TEST(text_out, unimplemented) { /////////////////////////////////////////////////////////////////////// DECODING -#define TEST_ENV(Size) \ - avs_stream_abstract_t *stream = NULL; \ +#define TEST_ENV(Size) \ + avs_stream_abstract_t *stream = NULL; \ AVS_UNIT_ASSERT_SUCCESS(avs_unit_memstream_alloc(&stream, Size)); \ - anjay_input_ctx_t *in; \ + anjay_input_ctx_t *in; \ AVS_UNIT_ASSERT_SUCCESS(_anjay_input_text_create(&in, &stream, false)); -#define TEST_TEARDOWN do { \ - _anjay_input_ctx_destroy(&in); \ - avs_stream_cleanup(&stream); \ -} while (0) +#define TEST_TEARDOWN \ + do { \ + _anjay_input_ctx_destroy(&in); \ + avs_stream_cleanup(&stream); \ + } while (0) AVS_UNIT_TEST(text_in, string) { TEST_ENV(64); static const char TEST_STRING[] = "Hello, world!"; - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, - TEST_STRING, strlen(TEST_STRING))); + AVS_UNIT_ASSERT_SUCCESS( + avs_stream_write(stream, TEST_STRING, strlen(TEST_STRING))); char buf[64]; AVS_UNIT_ASSERT_SUCCESS(anjay_get_string(in, buf, sizeof(buf))); @@ -214,8 +221,8 @@ AVS_UNIT_TEST(text_in, string_too_long) { TEST_ENV(16); static const char TEST_STRING[] = "Hello, world!"; - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, - TEST_STRING, strlen(TEST_STRING))); + AVS_UNIT_ASSERT_SUCCESS( + avs_stream_write(stream, TEST_STRING, strlen(TEST_STRING))); char buf[8]; AVS_UNIT_ASSERT_FAILED(anjay_get_string(in, buf, sizeof(buf))); @@ -224,24 +231,26 @@ AVS_UNIT_TEST(text_in, string_too_long) { TEST_TEARDOWN; } -#define TEST_NUM_COMMON(Val, ...) do { \ - TEST_ENV(32); \ - \ - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, #Val, sizeof(#Val) - 1)); \ - \ - __VA_ARGS__; \ - \ - TEST_TEARDOWN; \ -} while (false) - -#define TEST_NUM_FAIL(Type, Getter, Val) TEST_NUM_COMMON(Val, \ - Type result; \ - AVS_UNIT_ASSERT_FAILED(anjay_get_##Getter (in, &result))) - -#define TEST_INT(Bits, Val) TEST_NUM_COMMON(Val, \ - int##Bits##_t result; \ - AVS_UNIT_ASSERT_SUCCESS(anjay_get_i##Bits (in, &result)); \ - AVS_UNIT_ASSERT_EQUAL(result, (int##Bits##_t) (Val##ULL))) +#define TEST_NUM_COMMON(Val, ...) \ + do { \ + TEST_ENV(32); \ + \ + AVS_UNIT_ASSERT_SUCCESS( \ + avs_stream_write(stream, #Val, sizeof(#Val) - 1)); \ + \ + __VA_ARGS__; \ + \ + TEST_TEARDOWN; \ + } while (false) + +#define TEST_NUM_FAIL(Type, Getter, Val) \ + TEST_NUM_COMMON(Val, Type result; \ + AVS_UNIT_ASSERT_FAILED(anjay_get_##Getter(in, &result))) + +#define TEST_INT(Bits, Val) \ + TEST_NUM_COMMON(Val, int##Bits##_t result; \ + AVS_UNIT_ASSERT_SUCCESS(anjay_get_i##Bits(in, &result)); \ + AVS_UNIT_ASSERT_EQUAL(result, (int##Bits##_t)(Val##ULL))) #define TEST_INT_FAIL(Bits, Val) TEST_NUM_FAIL(int##Bits##_t, i##Bits, Val) @@ -277,10 +286,10 @@ AVS_UNIT_TEST(text_in, i64) { #undef TEST_INT_FAIL #undef TEST_INT -#define TEST_FLOAT(Type, Val) TEST_NUM_COMMON(Val, \ - Type result; \ - AVS_UNIT_ASSERT_SUCCESS(anjay_get_##Type (in, &result)); \ - AVS_UNIT_ASSERT_EQUAL(result, (Type) Val)) +#define TEST_FLOAT(Type, Val) \ + TEST_NUM_COMMON(Val, Type result; \ + AVS_UNIT_ASSERT_SUCCESS(anjay_get_##Type(in, &result)); \ + AVS_UNIT_ASSERT_EQUAL(result, (Type) Val)) #define TEST_FLOAT_FAIL(Type, Val) TEST_NUM_FAIL(Type, Type, Val) @@ -314,18 +323,20 @@ AVS_UNIT_TEST(text_in, f64) { #undef TEST_FLOAT_FAIL #undef TEST_FLOAT -#define TEST_BOOL(Val) TEST_NUM_COMMON(Val, \ - bool result; \ - AVS_UNIT_ASSERT_SUCCESS(anjay_get_bool(in, &result)); \ - AVS_UNIT_ASSERT_EQUAL(result, Val)) - -#define TEST_BOOL_FAIL(Str) do { \ - TEST_ENV(32); \ - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, Str, sizeof(Str) - 1)); \ - bool result; \ - AVS_UNIT_ASSERT_FAILED(anjay_get_bool(in, &result)); \ - TEST_TEARDOWN; \ -} while (false); +#define TEST_BOOL(Val) \ + TEST_NUM_COMMON(Val, bool result; \ + AVS_UNIT_ASSERT_SUCCESS(anjay_get_bool(in, &result)); \ + AVS_UNIT_ASSERT_EQUAL(result, Val)) + +#define TEST_BOOL_FAIL(Str) \ + do { \ + TEST_ENV(32); \ + AVS_UNIT_ASSERT_SUCCESS( \ + avs_stream_write(stream, Str, sizeof(Str) - 1)); \ + bool result; \ + AVS_UNIT_ASSERT_FAILED(anjay_get_bool(in, &result)); \ + TEST_TEARDOWN; \ + } while (false); AVS_UNIT_TEST(text_in, boolean) { TEST_BOOL(0); @@ -340,22 +351,26 @@ AVS_UNIT_TEST(text_in, boolean) { #undef TEST_NUM_FAIL #undef TEST_NUM_COMMON -#define TEST_OBJLNK_COMMON(Str, ...) do { \ - TEST_ENV(64); \ - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, Str, sizeof(Str) - 1)); \ - anjay_oid_t oid; \ - anjay_iid_t iid; \ - __VA_ARGS__; \ - TEST_TEARDOWN; \ -} while (false) - -#define TEST_OBJLNK(Oid, Iid) TEST_OBJLNK_COMMON(#Oid ":" #Iid, \ - AVS_UNIT_ASSERT_SUCCESS(anjay_get_objlnk(in, &oid, &iid)); \ - AVS_UNIT_ASSERT_EQUAL(oid, Oid); \ - AVS_UNIT_ASSERT_EQUAL(iid, Iid)) - -#define TEST_OBJLNK_FAIL(Str) TEST_OBJLNK_COMMON(Str, \ - AVS_UNIT_ASSERT_FAILED(anjay_get_objlnk(in, &oid, &iid))) +#define TEST_OBJLNK_COMMON(Str, ...) \ + do { \ + TEST_ENV(64); \ + AVS_UNIT_ASSERT_SUCCESS( \ + avs_stream_write(stream, Str, sizeof(Str) - 1)); \ + anjay_oid_t oid; \ + anjay_iid_t iid; \ + __VA_ARGS__; \ + TEST_TEARDOWN; \ + } while (false) + +#define TEST_OBJLNK(Oid, Iid) \ + TEST_OBJLNK_COMMON( \ + #Oid ":" #Iid, \ + AVS_UNIT_ASSERT_SUCCESS(anjay_get_objlnk(in, &oid, &iid)); \ + AVS_UNIT_ASSERT_EQUAL(oid, Oid); AVS_UNIT_ASSERT_EQUAL(iid, Iid)) + +#define TEST_OBJLNK_FAIL(Str) \ + TEST_OBJLNK_COMMON(Str, AVS_UNIT_ASSERT_FAILED( \ + anjay_get_objlnk(in, &oid, &iid))) AVS_UNIT_TEST(text_in, objlnk) { TEST_OBJLNK(0, 0); diff --git a/src/io/test/tlv_in.c b/src/io/test/tlv_in.c index 83f4ea291..d223d5a4c 100644 --- a/src/io/test/tlv_in.c +++ b/src/io/test/tlv_in.c @@ -16,76 +16,81 @@ #include +#include #include #include -#include #include "bigdata.h" #include -#define TEST_ENV(Size) \ - avs_stream_abstract_t *stream = NULL; \ +#define TEST_ENV(Size) \ + avs_stream_abstract_t *stream = NULL; \ AVS_UNIT_ASSERT_SUCCESS(avs_unit_memstream_alloc(&stream, Size)); \ - anjay_input_ctx_t *in; \ + anjay_input_ctx_t *in; \ AVS_UNIT_ASSERT_SUCCESS(_anjay_input_tlv_create(&in, &stream, false)); -#define TEST_TEARDOWN do { \ - _anjay_input_ctx_destroy(&in); \ - avs_stream_cleanup(&stream); \ -} while (0) - -#define TLV_BYTES_TEST_DATA(Header, Data) do { \ - char *buf = (char *) avs_malloc(sizeof(Data) + sizeof(Header)); \ - size_t bytes_read; \ - bool message_finished; \ - AVS_UNIT_ASSERT_SUCCESS( \ - anjay_get_bytes(in, &bytes_read, &message_finished, \ - buf, sizeof(Data) + sizeof(Header))); \ - AVS_UNIT_ASSERT_EQUAL(bytes_read, sizeof(Data) - 1); \ - AVS_UNIT_ASSERT_TRUE(message_finished); \ - AVS_UNIT_ASSERT_EQUAL_BYTES(buf, Data); \ - avs_free(buf); \ -} while (0) - -#define TLV_BYTES_TEST_ID(IdType, Id) do { \ - anjay_id_type_t type; \ - uint16_t id; \ - AVS_UNIT_ASSERT_SUCCESS(_anjay_input_get_id(in, &type, &id)); \ - AVS_UNIT_ASSERT_EQUAL(type, IdType); \ - AVS_UNIT_ASSERT_EQUAL(id, Id); \ -} while (0) - -#define TLV_BYTES_TEST(Name, IdType, Id, Header, Data) \ -AVS_UNIT_TEST(tlv_in_bytes, Name) { \ - TEST_ENV(sizeof(Data) + sizeof(Header)); \ - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, Header Data, \ - sizeof(Header Data) - 1)); \ - TLV_BYTES_TEST_DATA(Header, Data); \ - TEST_TEARDOWN; \ -} \ -\ -AVS_UNIT_TEST(tlv_in_bytes, Name##_with_id) { \ - TEST_ENV(sizeof(Data) + sizeof(Header)); \ - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, Header Data, \ - sizeof(Header Data) - 1)); \ - TLV_BYTES_TEST_ID(IdType, Id); \ - TLV_BYTES_TEST_ID(IdType, Id); \ - TLV_BYTES_TEST_DATA(Header, Data); \ - TEST_TEARDOWN; \ -} +#define TEST_TEARDOWN \ + do { \ + _anjay_input_ctx_destroy(&in); \ + avs_stream_cleanup(&stream); \ + } while (0) + +#define TLV_BYTES_TEST_DATA(Header, Data) \ + do { \ + char *buf = (char *) avs_malloc(sizeof(Data) + sizeof(Header)); \ + size_t bytes_read; \ + bool message_finished; \ + AVS_UNIT_ASSERT_SUCCESS( \ + anjay_get_bytes(in, &bytes_read, &message_finished, buf, \ + sizeof(Data) + sizeof(Header))); \ + AVS_UNIT_ASSERT_EQUAL(bytes_read, sizeof(Data) - 1); \ + AVS_UNIT_ASSERT_TRUE(message_finished); \ + AVS_UNIT_ASSERT_EQUAL_BYTES(buf, Data); \ + avs_free(buf); \ + } while (0) + +#define TLV_BYTES_TEST_ID(IdType, Id) \ + do { \ + anjay_id_type_t type; \ + uint16_t id; \ + AVS_UNIT_ASSERT_SUCCESS(_anjay_input_get_id(in, &type, &id)); \ + AVS_UNIT_ASSERT_EQUAL(type, IdType); \ + AVS_UNIT_ASSERT_EQUAL(id, Id); \ + } while (0) + +#define TLV_BYTES_TEST(Name, IdType, Id, Header, Data) \ + AVS_UNIT_TEST(tlv_in_bytes, Name) { \ + TEST_ENV(sizeof(Data) + sizeof(Header)); \ + AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, Header Data, \ + sizeof(Header Data) - 1)); \ + TLV_BYTES_TEST_DATA(Header, Data); \ + TEST_TEARDOWN; \ + } \ + \ + AVS_UNIT_TEST(tlv_in_bytes, Name##_with_id) { \ + TEST_ENV(sizeof(Data) + sizeof(Header)); \ + AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, Header Data, \ + sizeof(Header Data) - 1)); \ + TLV_BYTES_TEST_ID(IdType, Id); \ + TLV_BYTES_TEST_ID(IdType, Id); \ + TLV_BYTES_TEST_DATA(Header, Data); \ + TEST_TEARDOWN; \ + } // 3 bits for length - <=7 -TLV_BYTES_TEST(len3b_id8b, ANJAY_ID_RID, 0, "\xC7\x00", "1234567") -TLV_BYTES_TEST(len3b_id16b, ANJAY_ID_RID, 42000, "\xE7\xA4\x10", "1234567") +TLV_BYTES_TEST(len3b_id8b, ANJAY_ID_RID, 0, "\xC7\x00", "1234567") +TLV_BYTES_TEST(len3b_id16b, ANJAY_ID_RID, 42000, "\xE7\xA4\x10", "1234567") -TLV_BYTES_TEST(len8b_id8b, ANJAY_ID_RID, 255, "\xC8\xFF\x08", "12345678") -TLV_BYTES_TEST(len8b_id16b, ANJAY_ID_RID, 65534, "\xE8\xFF\xFE\x08", "12345678") +TLV_BYTES_TEST(len8b_id8b, ANJAY_ID_RID, 255, "\xC8\xFF\x08", "12345678") +TLV_BYTES_TEST(len8b_id16b, ANJAY_ID_RID, 65534, "\xE8\xFF\xFE\x08", "12345678") -TLV_BYTES_TEST(len16b_id8b, ANJAY_ID_RID, 42, "\xD0\x2A\x03\xE8", DATA1kB) -TLV_BYTES_TEST(len16b_id16b, ANJAY_ID_RID, 42420, "\xF0\xA5\xB4\x03\xE8", DATA1kB) +TLV_BYTES_TEST(len16b_id8b, ANJAY_ID_RID, 42, "\xD0\x2A\x03\xE8", DATA1kB) +TLV_BYTES_TEST( + len16b_id16b, ANJAY_ID_RID, 42420, "\xF0\xA5\xB4\x03\xE8", DATA1kB) -TLV_BYTES_TEST(len24b_id8b, ANJAY_ID_RID, 69, "\xD8\x45\x01\x86\xA0", DATA100kB) -TLV_BYTES_TEST(len24b_id16b, ANJAY_ID_RID, 258, "\xF8\x01\x02\x01\x86\xA0", DATA100kB) +TLV_BYTES_TEST(len24b_id8b, ANJAY_ID_RID, 69, "\xD8\x45\x01\x86\xA0", DATA100kB) +TLV_BYTES_TEST( + len24b_id16b, ANJAY_ID_RID, 258, "\xF8\x01\x02\x01\x86\xA0", DATA100kB) #undef TLV_BYTES_TEST #undef TLV_BYTES_TEST_DATA @@ -107,8 +112,8 @@ AVS_UNIT_TEST(tlv_in_bytes, id_too_short) { AVS_UNIT_TEST(tlv_in_bytes, length_too_short) { TEST_ENV(64); - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, - "\xF8\x01\x02\x01\x86", 5)); + AVS_UNIT_ASSERT_SUCCESS( + avs_stream_write(stream, "\xF8\x01\x02\x01\x86", 5)); char buf[64]; size_t bytes_read; @@ -121,7 +126,8 @@ AVS_UNIT_TEST(tlv_in_bytes, length_too_short) { AVS_UNIT_TEST(tlv_in_bytes, partial_read) { TEST_ENV(16); - static const char DATA[] = "\xC7\x2A" "0123456"; + static const char DATA[] = "\xC7\x2A" + "0123456"; AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, DATA, sizeof(DATA) - 1)); for (size_t i = 0; i < 7; ++i) { @@ -146,9 +152,15 @@ AVS_UNIT_TEST(tlv_in_bytes, partial_read) { AVS_UNIT_TEST(tlv_in_bytes, short_read_get_id) { TEST_ENV(64); - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write_f(stream, "%s", "\xC4\x2A" "0123")); - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write_f(stream, "%s", "\xC7\x45" "0123456")); - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write_f(stream, "%s", "\xC5\x16" "01234")); + AVS_UNIT_ASSERT_SUCCESS(avs_stream_write_f(stream, "%s", + "\xC4\x2A" + "0123")); + AVS_UNIT_ASSERT_SUCCESS(avs_stream_write_f(stream, "%s", + "\xC7\x45" + "0123456")); + AVS_UNIT_ASSERT_SUCCESS(avs_stream_write_f(stream, "%s", + "\xC5\x16" + "01234")); TLV_BYTES_TEST_ID(ANJAY_ID_RID, 42); TLV_BYTES_TEST_ID(ANJAY_ID_RID, 42); @@ -182,15 +194,15 @@ AVS_UNIT_TEST(tlv_in_bytes, short_read_get_id) { AVS_UNIT_TEST(tlv_in_bytes, premature_end) { TEST_ENV(16); - static const char DATA[] = "\xC7\x2A" "012"; + static const char DATA[] = "\xC7\x2A" + "012"; AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, DATA, sizeof(DATA) - 1)); char buf[16]; size_t bytes_read; bool message_finished; - AVS_UNIT_ASSERT_FAILED( - anjay_get_bytes(in, &bytes_read, &message_finished, - buf, sizeof(buf))); + AVS_UNIT_ASSERT_FAILED(anjay_get_bytes(in, &bytes_read, &message_finished, + buf, sizeof(buf))); TEST_TEARDOWN; } @@ -203,9 +215,8 @@ AVS_UNIT_TEST(tlv_in_bytes, no_data) { char buf[16] = { init }; size_t bytes_read; bool message_finished; - AVS_UNIT_ASSERT_SUCCESS( - anjay_get_bytes(in, &bytes_read, &message_finished, - buf, sizeof(buf))); + AVS_UNIT_ASSERT_SUCCESS(anjay_get_bytes(in, &bytes_read, &message_finished, + buf, sizeof(buf))); /* buffer untouched, read 0 bytes */ AVS_UNIT_ASSERT_EQUAL(buf[0], init); @@ -221,22 +232,24 @@ typedef struct { const char *ptr; } fake_tlv_in_t; -#define TEST_ENV(Data) \ - avs_stream_abstract_t *test_stream = NULL; \ - AVS_UNIT_ASSERT_SUCCESS(avs_unit_memstream_alloc(&test_stream, \ - sizeof(Data))); \ - AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(test_stream, \ - Data, sizeof(Data) - 1)); \ - anjay_input_ctx_t *in = (anjay_input_ctx_t *) \ - &(tlv_in_t []) { { \ - .vtable = &TLV_IN_VTABLE, \ - .stream = { \ - .vtable = &TLV_SINGLE_MSG_STREAM_WRAPPER_VTABLE, \ - .backend = test_stream \ - }, \ - .id = 0, \ - .length = sizeof(Data) - 1 \ - } }; +#define TEST_ENV(Data) \ + avs_stream_abstract_t *test_stream = NULL; \ + AVS_UNIT_ASSERT_SUCCESS( \ + avs_unit_memstream_alloc(&test_stream, sizeof(Data))); \ + AVS_UNIT_ASSERT_SUCCESS( \ + avs_stream_write(test_stream, Data, sizeof(Data) - 1)); \ + anjay_input_ctx_t *in = (anjay_input_ctx_t *) &(tlv_in_t[]) \ + { \ + { \ + .vtable = &TLV_IN_VTABLE, \ + .stream = { \ + .vtable = &TLV_SINGLE_MSG_STREAM_WRAPPER_VTABLE, \ + .backend = test_stream \ + }, \ + .id = 0, \ + .length = sizeof(Data) - 1 \ + } \ + }; #define TEST_TEARDOWN avs_stream_cleanup(&test_stream) @@ -274,145 +287,145 @@ AVS_UNIT_TEST(tlv_in_types, string_overflow) { TEST_TEARDOWN; } -#define TEST_NUM_IMPL(Name, Type, Suffix, Num, Data) \ -AVS_UNIT_TEST(tlv_in_types, Name) { \ - TEST_ENV(Data); \ - Type value; \ - AVS_UNIT_ASSERT_SUCCESS(anjay_get_##Suffix (in, &value)); \ - AVS_UNIT_ASSERT_EQUAL(value, (Type) Num); \ - TEST_TEARDOWN; \ -} +#define TEST_NUM_IMPL(Name, Type, Suffix, Num, Data) \ + AVS_UNIT_TEST(tlv_in_types, Name) { \ + TEST_ENV(Data); \ + Type value; \ + AVS_UNIT_ASSERT_SUCCESS(anjay_get_##Suffix(in, &value)); \ + AVS_UNIT_ASSERT_EQUAL(value, (Type) Num); \ + TEST_TEARDOWN; \ + } -#define TEST_NUM_FAIL_IMPL(Name, Type, Suffix, Data) \ -AVS_UNIT_TEST(tlv_in_types, Name) { \ - TEST_ENV(Data); \ - Type value; \ - AVS_UNIT_ASSERT_FAILED(anjay_get_##Suffix (in, &value)); \ - TEST_TEARDOWN; \ -} +#define TEST_NUM_FAIL_IMPL(Name, Type, Suffix, Data) \ + AVS_UNIT_TEST(tlv_in_types, Name) { \ + TEST_ENV(Data); \ + Type value; \ + AVS_UNIT_ASSERT_FAILED(anjay_get_##Suffix(in, &value)); \ + TEST_TEARDOWN; \ + } #define TEST_NUM(Type, Suffix, Num, Data) \ - TEST_NUM_IMPL(AVS_CONCAT(Suffix##_, __LINE__), Type, Suffix, Num, Data) + TEST_NUM_IMPL(AVS_CONCAT(Suffix##_, __LINE__), Type, Suffix, Num, Data) #define TEST_NUM_FAIL(Type, Suffix, Data) \ - TEST_NUM_FAIL_IMPL(AVS_CONCAT(Suffix##fail_, __LINE__), Type, Suffix, Data) + TEST_NUM_FAIL_IMPL(AVS_CONCAT(Suffix##fail_, __LINE__), Type, Suffix, Data) #define TEST_INT32(...) TEST_NUM(int32_t, i32, __VA_ARGS__) -#define TEST_INT64(Num, Data) TEST_NUM(int64_t, i64, Num ## LL, Data) -#define TEST_INT3264(...) \ +#define TEST_INT64(Num, Data) TEST_NUM(int64_t, i64, Num##LL, Data) +#define TEST_INT3264(...) \ TEST_INT32(__VA_ARGS__) \ TEST_INT64(__VA_ARGS__) -#define TEST_INT3264_FAIL(...) \ +#define TEST_INT3264_FAIL(...) \ TEST_NUM_FAIL(int32_t, i32, __VA_ARGS__) \ TEST_NUM_FAIL(int64_t, i64, __VA_ARGS__) -#define TEST_INT64ONLY(Num, Data) \ +#define TEST_INT64ONLY(Num, Data) \ TEST_NUM_FAIL(int32_t, i32, Data) \ TEST_INT64(Num, Data) -TEST_INT3264_FAIL( "") -TEST_INT3264( 42, "\x2A") -TEST_INT3264( 4242, "\x10\x92") -TEST_INT3264_FAIL( "\x06\x79\x32") -TEST_INT3264( 424242, "\x00\x06\x79\x32") -TEST_INT3264( 42424242, "\x02\x87\x57\xB2") -TEST_INT3264((int32_t) 4242424242, "\xFC\xDE\x41\xB2") -TEST_INT64ONLY( 4242424242, "\x00\x00\x00\x00\xFC\xDE\x41\xB2") -TEST_INT3264_FAIL( "\x62\xC6\xD1\xA9\xB2") -TEST_INT64ONLY( 424242424242, "\x00\x00\x00\x62\xC6\xD1\xA9\xB2") -TEST_INT3264_FAIL( "\x26\x95\xA9\xE6\x49\xB2") -TEST_INT64ONLY( 42424242424242, "\x00\x00\x26\x95\xA9\xE6\x49\xB2") -TEST_INT3264_FAIL( "\x0F\x12\x76\x5D\xF4\xC9\xB2") -TEST_INT64ONLY( 4242424242424242, "\x00\x0F\x12\x76\x5D\xF4\xC9\xB2") +TEST_INT3264_FAIL("") +TEST_INT3264(42, "\x2A") +TEST_INT3264(4242, "\x10\x92") +TEST_INT3264_FAIL("\x06\x79\x32") +TEST_INT3264(424242, "\x00\x06\x79\x32") +TEST_INT3264(42424242, "\x02\x87\x57\xB2") +TEST_INT3264((int32_t) 4242424242, "\xFC\xDE\x41\xB2") +TEST_INT64ONLY(4242424242, "\x00\x00\x00\x00\xFC\xDE\x41\xB2") +TEST_INT3264_FAIL("\x62\xC6\xD1\xA9\xB2") +TEST_INT64ONLY(424242424242, "\x00\x00\x00\x62\xC6\xD1\xA9\xB2") +TEST_INT3264_FAIL("\x26\x95\xA9\xE6\x49\xB2") +TEST_INT64ONLY(42424242424242, "\x00\x00\x26\x95\xA9\xE6\x49\xB2") +TEST_INT3264_FAIL("\x0F\x12\x76\x5D\xF4\xC9\xB2") +TEST_INT64ONLY(4242424242424242, "\x00\x0F\x12\x76\x5D\xF4\xC9\xB2") TEST_INT64ONLY(424242424242424242, "\x05\xE3\x36\x3C\xB3\x9E\xC9\xB2") -TEST_INT3264_FAIL( "\x00\x05\xE3\x36\x3C\xB3\x9E\xC9\xB2") +TEST_INT3264_FAIL("\x00\x05\xE3\x36\x3C\xB3\x9E\xC9\xB2") -#define TEST_FLOAT(Num, Data) \ - TEST_NUM(float, float, Num, Data) \ - TEST_NUM(double, double, Num, Data) +#define TEST_FLOAT(Num, Data) \ + TEST_NUM(float, float, Num, Data) \ + TEST_NUM(double, double, Num, Data) -#define TEST_FLOAT_FAIL(Data) \ - TEST_NUM_FAIL(float, float, Data) \ - TEST_NUM_FAIL(double, double, Data) +#define TEST_FLOAT_FAIL(Data) \ + TEST_NUM_FAIL(float, float, Data) \ + TEST_NUM_FAIL(double, double, Data) -TEST_FLOAT_FAIL( "") -TEST_FLOAT_FAIL( "\x3F") -TEST_FLOAT_FAIL( "\x3F\x80") -TEST_FLOAT_FAIL( "\x3F\x80\x00") -TEST_FLOAT( 1.0, "\x3F\x80\x00\x00") +TEST_FLOAT_FAIL("") +TEST_FLOAT_FAIL("\x3F") +TEST_FLOAT_FAIL("\x3F\x80") +TEST_FLOAT_FAIL("\x3F\x80\x00") +TEST_FLOAT(1.0, "\x3F\x80\x00\x00") TEST_FLOAT(-42.0e3, "\xC7\x24\x10\x00") -TEST_FLOAT_FAIL( "\x3F\xF0\x00\x00\x00") -TEST_FLOAT_FAIL( "\x3F\xF0\x00\x00\x00\x00") -TEST_FLOAT_FAIL( "\x3F\xF0\x00\x00\x00\x00\x00") -TEST_FLOAT( 1.0, "\x3F\xF0\x00\x00\x00\x00\x00\x00") -TEST_FLOAT( 1.1, "\x3F\xF1\x99\x99\x99\x99\x99\x9A") +TEST_FLOAT_FAIL("\x3F\xF0\x00\x00\x00") +TEST_FLOAT_FAIL("\x3F\xF0\x00\x00\x00\x00") +TEST_FLOAT_FAIL("\x3F\xF0\x00\x00\x00\x00\x00") +TEST_FLOAT(1.0, "\x3F\xF0\x00\x00\x00\x00\x00\x00") +TEST_FLOAT(1.1, "\x3F\xF1\x99\x99\x99\x99\x99\x9A") TEST_FLOAT(-42.0e3, "\xC0\xE4\x82\x00\x00\x00\x00\x00") -TEST_FLOAT_FAIL( "\xC0\xE4\x82\x00\x00\x00\x00\x00\x00") - -#define TEST_BOOL_IMPL(Name, Value, Data) \ -AVS_UNIT_TEST(tlv_in_types, Name) { \ - TEST_ENV(Data); \ - bool value; \ - AVS_UNIT_ASSERT_SUCCESS(anjay_get_bool(in, &value)); \ - AVS_UNIT_ASSERT_EQUAL(!!(Value), value); \ - TEST_TEARDOWN; \ -} +TEST_FLOAT_FAIL("\xC0\xE4\x82\x00\x00\x00\x00\x00\x00") + +#define TEST_BOOL_IMPL(Name, Value, Data) \ + AVS_UNIT_TEST(tlv_in_types, Name) { \ + TEST_ENV(Data); \ + bool value; \ + AVS_UNIT_ASSERT_SUCCESS(anjay_get_bool(in, &value)); \ + AVS_UNIT_ASSERT_EQUAL(!!(Value), value); \ + TEST_TEARDOWN; \ + } -#define TEST_BOOL_FAIL_IMPL(Name, Data) \ -AVS_UNIT_TEST(tlv_in_types, Name) { \ - TEST_ENV(Data); \ - bool value; \ - AVS_UNIT_ASSERT_FAILED(anjay_get_bool(in, &value)); \ - TEST_TEARDOWN; \ -} +#define TEST_BOOL_FAIL_IMPL(Name, Data) \ + AVS_UNIT_TEST(tlv_in_types, Name) { \ + TEST_ENV(Data); \ + bool value; \ + AVS_UNIT_ASSERT_FAILED(anjay_get_bool(in, &value)); \ + TEST_TEARDOWN; \ + } #define TEST_BOOL(Value, Data) \ - TEST_BOOL_IMPL(AVS_CONCAT(bool_, __LINE__), Value, Data) + TEST_BOOL_IMPL(AVS_CONCAT(bool_, __LINE__), Value, Data) #define TEST_BOOL_FAIL(Data) \ - TEST_BOOL_FAIL_IMPL(AVS_CONCAT(bool_, __LINE__), Data) + TEST_BOOL_FAIL_IMPL(AVS_CONCAT(bool_, __LINE__), Data) -TEST_BOOL_FAIL( "") +TEST_BOOL_FAIL("") TEST_BOOL(false, "\0") -TEST_BOOL(true, "\1") -TEST_BOOL_FAIL( "\2") +TEST_BOOL(true, "\1") +TEST_BOOL_FAIL("\2") TEST_BOOL_FAIL("\0\0") -#define TEST_OBJLNK_IMPL(Name, Oid, Iid, Data) \ -AVS_UNIT_TEST(tlv_in_types, Name) { \ - TEST_ENV(Data); \ - anjay_oid_t oid; \ - anjay_iid_t iid; \ - AVS_UNIT_ASSERT_SUCCESS(anjay_get_objlnk(in, &oid, &iid)); \ - AVS_UNIT_ASSERT_EQUAL(oid, Oid); \ - AVS_UNIT_ASSERT_EQUAL(iid, Iid); \ - TEST_TEARDOWN; \ -} +#define TEST_OBJLNK_IMPL(Name, Oid, Iid, Data) \ + AVS_UNIT_TEST(tlv_in_types, Name) { \ + TEST_ENV(Data); \ + anjay_oid_t oid; \ + anjay_iid_t iid; \ + AVS_UNIT_ASSERT_SUCCESS(anjay_get_objlnk(in, &oid, &iid)); \ + AVS_UNIT_ASSERT_EQUAL(oid, Oid); \ + AVS_UNIT_ASSERT_EQUAL(iid, Iid); \ + TEST_TEARDOWN; \ + } -#define TEST_OBJLNK_FAIL_IMPL(Name, Data) \ -AVS_UNIT_TEST(tlv_in_types, Name) { \ - TEST_ENV(Data); \ - anjay_oid_t oid; \ - anjay_iid_t iid; \ - AVS_UNIT_ASSERT_FAILED(anjay_get_objlnk(in, &oid, &iid)); \ - TEST_TEARDOWN; \ -} +#define TEST_OBJLNK_FAIL_IMPL(Name, Data) \ + AVS_UNIT_TEST(tlv_in_types, Name) { \ + TEST_ENV(Data); \ + anjay_oid_t oid; \ + anjay_iid_t iid; \ + AVS_UNIT_ASSERT_FAILED(anjay_get_objlnk(in, &oid, &iid)); \ + TEST_TEARDOWN; \ + } #define TEST_OBJLNK(...) \ - TEST_OBJLNK_IMPL(AVS_CONCAT(objlnk_, __LINE__), __VA_ARGS__) + TEST_OBJLNK_IMPL(AVS_CONCAT(objlnk_, __LINE__), __VA_ARGS__) #define TEST_OBJLNK_FAIL(...) \ - TEST_OBJLNK_FAIL_IMPL(AVS_CONCAT(objlnk_, __LINE__), __VA_ARGS__) - -TEST_OBJLNK_FAIL( "") -TEST_OBJLNK_FAIL( "\x00") -TEST_OBJLNK_FAIL( "\x00\x00") -TEST_OBJLNK_FAIL( "\x00\x00\x00") -TEST_OBJLNK( 0, 0, "\x00\x00\x00\x00") -TEST_OBJLNK( 1, 0, "\x00\x01\x00\x00") -TEST_OBJLNK( 0, 1, "\x00\x00\x00\x01") -TEST_OBJLNK( 1, 65535, "\x00\x01\xFF\xFF") -TEST_OBJLNK(65535, 1, "\xFF\xFF\x00\x01") + TEST_OBJLNK_FAIL_IMPL(AVS_CONCAT(objlnk_, __LINE__), __VA_ARGS__) + +TEST_OBJLNK_FAIL("") +TEST_OBJLNK_FAIL("\x00") +TEST_OBJLNK_FAIL("\x00\x00") +TEST_OBJLNK_FAIL("\x00\x00\x00") +TEST_OBJLNK(0, 0, "\x00\x00\x00\x00") +TEST_OBJLNK(1, 0, "\x00\x01\x00\x00") +TEST_OBJLNK(0, 1, "\x00\x00\x00\x01") +TEST_OBJLNK(1, 65535, "\x00\x01\xFF\xFF") +TEST_OBJLNK(65535, 1, "\xFF\xFF\x00\x01") TEST_OBJLNK(65535, 65535, "\xFF\xFF\xFF\xFF") -TEST_OBJLNK_FAIL( "\xFF\xFF\xFF\xFF\xFF") +TEST_OBJLNK_FAIL("\xFF\xFF\xFF\xFF\xFF") AVS_UNIT_TEST(tlv_in_types, invalid_read) { TEST_ENV("\xC3\x00\x00\x00\x2A"); // bytes that contain an int afterward @@ -420,8 +433,8 @@ AVS_UNIT_TEST(tlv_in_types, invalid_read) { size_t bytes_read; bool message_finished; char ch; - AVS_UNIT_ASSERT_SUCCESS(anjay_get_bytes(in, &bytes_read, &message_finished, - &ch, 1)); + AVS_UNIT_ASSERT_SUCCESS( + anjay_get_bytes(in, &bytes_read, &message_finished, &ch, 1)); int32_t value; AVS_UNIT_ASSERT_FAILED(anjay_get_i32(in, &value)); diff --git a/src/io/test/tlv_out.c b/src/io/test/tlv_out.c index 8af920aac..8907d2936 100644 --- a/src/io/test/tlv_out.c +++ b/src/io/test/tlv_out.c @@ -16,8 +16,8 @@ #include -#include #include +#include #include "bigdata.h" @@ -33,20 +33,25 @@ static anjay_output_ctx_t *new_tlv_out(avs_stream_abstract_t *stream) { return (anjay_output_ctx_t *) out; } -#define TEST_ENV_COMMON(Size) \ +#define TEST_ENV_COMMON(Size) \ avs_stream_outbuf_t outbuf = AVS_STREAM_OUTBUF_STATIC_INITIALIZER; \ - avs_stream_outbuf_set_buffer(&outbuf, buf, sizeof(buf)); \ + avs_stream_outbuf_set_buffer(&outbuf, buf, sizeof(buf)); \ anjay_output_ctx_t *out = new_tlv_out((avs_stream_abstract_t *) &outbuf) -#define TEST_ENV(Size) char buf[Size]; TEST_ENV_COMMON(Size) +#define TEST_ENV(Size) \ + char buf[Size]; \ + TEST_ENV_COMMON(Size) -#define TEST_ENV_HEAP(Size) \ - char *buf = (char *) avs_malloc(Size); TEST_ENV_COMMON(Size) +#define TEST_ENV_HEAP(Size) \ + char *buf = (char *) avs_malloc(Size); \ + TEST_ENV_COMMON(Size) -#define VERIFY_BYTES(Data) do { \ - AVS_UNIT_ASSERT_EQUAL(avs_stream_outbuf_offset(&outbuf), sizeof(Data) - 1);\ - AVS_UNIT_ASSERT_EQUAL_BYTES(buf, Data); \ -} while (0) +#define VERIFY_BYTES(Data) \ + do { \ + AVS_UNIT_ASSERT_EQUAL(avs_stream_outbuf_offset(&outbuf), \ + sizeof(Data) - 1); \ + AVS_UNIT_ASSERT_EQUAL_BYTES(buf, Data); \ + } while (0) // 3 bits for length - <=7 AVS_UNIT_TEST(tlv_out, bytes_3blen_8bid) { @@ -56,7 +61,8 @@ AVS_UNIT_TEST(tlv_out, bytes_3blen_8bid) { AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 0)); AVS_UNIT_ASSERT_SUCCESS(anjay_ret_string(out, DATA)); AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); - VERIFY_BYTES("\xC7\x00" "1234567"); + VERIFY_BYTES("\xC7\x00" + "1234567"); } AVS_UNIT_TEST(tlv_out, bytes_3blen_16bid) { @@ -67,7 +73,8 @@ AVS_UNIT_TEST(tlv_out, bytes_3blen_16bid) { AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 42000)); AVS_UNIT_ASSERT_SUCCESS(anjay_ret_string(out, DATA)); AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); - VERIFY_BYTES("\xE7\xA4\x10" "1234567"); + VERIFY_BYTES("\xE7\xA4\x10" + "1234567"); } AVS_UNIT_TEST(tlv_out, bytes_8blen_8bid) { @@ -77,7 +84,8 @@ AVS_UNIT_TEST(tlv_out, bytes_8blen_8bid) { AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 255)); AVS_UNIT_ASSERT_SUCCESS(anjay_ret_string(out, DATA)); AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); - VERIFY_BYTES("\xC8\xFF\x08" "12345678"); + VERIFY_BYTES("\xC8\xFF\x08" + "12345678"); } AVS_UNIT_TEST(tlv_out, bytes_8blen_16bid) { @@ -87,7 +95,8 @@ AVS_UNIT_TEST(tlv_out, bytes_8blen_16bid) { AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 65534)); AVS_UNIT_ASSERT_SUCCESS(anjay_ret_string(out, DATA)); AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); - VERIFY_BYTES("\xE8\xFF\xFE\x08" "12345678"); + VERIFY_BYTES("\xE8\xFF\xFE\x08" + "12345678"); } AVS_UNIT_TEST(tlv_out, bytes_16blen_8bid) { @@ -150,54 +159,64 @@ AVS_UNIT_TEST(tlv_out, no_id) { AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); } -#define TEST_INT_IMPL(Name, Bits, Num, Data) \ -AVS_UNIT_TEST(tlv_out, Name) { \ - TEST_ENV(32); \ - \ - AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 1)); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_i##Bits (out, Num)); \ - AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); \ - VERIFY_BYTES(Data); \ -} +#define TEST_INT_IMPL(Name, Bits, Num, Data) \ + AVS_UNIT_TEST(tlv_out, Name) { \ + TEST_ENV(32); \ + \ + AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 1)); \ + AVS_UNIT_ASSERT_SUCCESS(anjay_ret_i##Bits(out, Num)); \ + AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); \ + VERIFY_BYTES(Data); \ + } #define TEST_INT(Bits, Num, Data) \ - TEST_INT_IMPL(AVS_CONCAT(i##Bits##_, __LINE__), Bits, Num, Data) + TEST_INT_IMPL(AVS_CONCAT(i##Bits##_, __LINE__), Bits, Num, Data) #define TEST_INT32(...) TEST_INT(32, __VA_ARGS__) -#define TEST_INT64(Num, Data) TEST_INT(64, Num ## LL, Data) -#define TEST_INT3264(...) \ +#define TEST_INT64(Num, Data) TEST_INT(64, Num##LL, Data) +#define TEST_INT3264(...) \ TEST_INT32(__VA_ARGS__) \ TEST_INT64(__VA_ARGS__) -TEST_INT3264( 42, "\xC1\x01" "\x2A") -TEST_INT3264( 4242, "\xC2\x01" "\x10\x92") -TEST_INT3264( 424242, "\xC4\x01" "\x00\x06\x79\x32") -TEST_INT3264( 42424242, "\xC4\x01" "\x02\x87\x57\xB2") -TEST_INT3264((int32_t) 4242424242, "\xC4\x01" "\xFC\xDE\x41\xB2") -TEST_INT64( 4242424242, "\xC8\x01\x08\x00\x00\x00\x00\xFC\xDE\x41\xB2") -TEST_INT64( 424242424242, "\xC8\x01\x08\x00\x00\x00\x62\xC6\xD1\xA9\xB2") -TEST_INT64( 42424242424242, "\xC8\x01\x08\x00\x00\x26\x95\xA9\xE6\x49\xB2") -TEST_INT64( 4242424242424242, "\xC8\x01\x08\x00\x0F\x12\x76\x5D\xF4\xC9\xB2") -TEST_INT64( 424242424242424242, "\xC8\x01\x08\x05\xE3\x36\x3C\xB3\x9E\xC9\xB2") - -#define TEST_FLOAT_IMPL(Name, Type, Num, Data) \ -AVS_UNIT_TEST(tlv_out, Name) { \ - TEST_ENV(32); \ - \ - AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 1)); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_##Type (out, Num)); \ - AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); \ - VERIFY_BYTES(Data); \ -} +TEST_INT3264(42, + "\xC1\x01" + "\x2A") +TEST_INT3264(4242, + "\xC2\x01" + "\x10\x92") +TEST_INT3264(424242, + "\xC4\x01" + "\x00\x06\x79\x32") +TEST_INT3264(42424242, + "\xC4\x01" + "\x02\x87\x57\xB2") +TEST_INT3264((int32_t) 4242424242, + "\xC4\x01" + "\xFC\xDE\x41\xB2") +TEST_INT64(4242424242, "\xC8\x01\x08\x00\x00\x00\x00\xFC\xDE\x41\xB2") +TEST_INT64(424242424242, "\xC8\x01\x08\x00\x00\x00\x62\xC6\xD1\xA9\xB2") +TEST_INT64(42424242424242, "\xC8\x01\x08\x00\x00\x26\x95\xA9\xE6\x49\xB2") +TEST_INT64(4242424242424242, "\xC8\x01\x08\x00\x0F\x12\x76\x5D\xF4\xC9\xB2") +TEST_INT64(424242424242424242, "\xC8\x01\x08\x05\xE3\x36\x3C\xB3\x9E\xC9\xB2") + +#define TEST_FLOAT_IMPL(Name, Type, Num, Data) \ + AVS_UNIT_TEST(tlv_out, Name) { \ + TEST_ENV(32); \ + \ + AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 1)); \ + AVS_UNIT_ASSERT_SUCCESS(anjay_ret_##Type(out, Num)); \ + AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); \ + VERIFY_BYTES(Data); \ + } #define TEST_FLOAT(Num, Data) \ - TEST_FLOAT_IMPL(AVS_CONCAT(float, __LINE__), float, Num, Data) + TEST_FLOAT_IMPL(AVS_CONCAT(float, __LINE__), float, Num, Data) -TEST_FLOAT( 1.0, "\xC4\x01\x3F\x80\x00\x00") +TEST_FLOAT(1.0, "\xC4\x01\x3F\x80\x00\x00") TEST_FLOAT(-42.0e3, "\xC4\x01\xC7\x24\x10\x00") #define TEST_DOUBLE(Num, Data) \ - TEST_FLOAT_IMPL(AVS_CONCAT(double, __LINE__), double, Num, Data) + TEST_FLOAT_IMPL(AVS_CONCAT(double, __LINE__), double, Num, Data) // rounds exactly to float TEST_DOUBLE(1.0, "\xC4\x01\x3F\x80\x00\x00") @@ -205,37 +224,37 @@ TEST_DOUBLE(1.0, "\xC4\x01\x3F\x80\x00\x00") // using double increases precision TEST_DOUBLE(1.1, "\xC8\x01\x08\x3F\xF1\x99\x99\x99\x99\x99\x9A") -#define TEST_BOOL(Val, Data) \ -AVS_UNIT_TEST(tlv_out, bool_##Val) { \ - TEST_ENV(32); \ - \ - AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 1)); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_bool(out, Val)); \ - AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); \ - VERIFY_BYTES("\xC1\x01" Data); \ -} +#define TEST_BOOL(Val, Data) \ + AVS_UNIT_TEST(tlv_out, bool_##Val) { \ + TEST_ENV(32); \ + \ + AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 1)); \ + AVS_UNIT_ASSERT_SUCCESS(anjay_ret_bool(out, Val)); \ + AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); \ + VERIFY_BYTES("\xC1\x01" Data); \ + } -TEST_BOOL(true, "\1") +TEST_BOOL(true, "\1") TEST_BOOL(false, "\0") -TEST_BOOL(1, "\1") -TEST_BOOL(0, "\0") -TEST_BOOL(42, "\1") - -#define TEST_OBJLNK(Oid, Iid, Data) \ -AVS_UNIT_TEST(tlv_out, objlnk_##Oid##_##Iid) { \ - TEST_ENV(32); \ - \ - AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 1)); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_ret_objlnk(out, Oid, Iid)); \ - AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); \ - VERIFY_BYTES("\xC4\x01" Data); \ -} +TEST_BOOL(1, "\1") +TEST_BOOL(0, "\0") +TEST_BOOL(42, "\1") + +#define TEST_OBJLNK(Oid, Iid, Data) \ + AVS_UNIT_TEST(tlv_out, objlnk_##Oid##_##Iid) { \ + TEST_ENV(32); \ + \ + AVS_UNIT_ASSERT_SUCCESS(_anjay_output_set_id(out, ANJAY_ID_RID, 1)); \ + AVS_UNIT_ASSERT_SUCCESS(anjay_ret_objlnk(out, Oid, Iid)); \ + AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); \ + VERIFY_BYTES("\xC4\x01" Data); \ + } -TEST_OBJLNK( 0, 0, "\x00\x00\x00\x00") -TEST_OBJLNK( 1, 0, "\x00\x01\x00\x00") -TEST_OBJLNK( 0, 1, "\x00\x00\x00\x01") -TEST_OBJLNK( 1, 65535, "\x00\x01\xFF\xFF") -TEST_OBJLNK(65535, 1, "\xFF\xFF\x00\x01") +TEST_OBJLNK(0, 0, "\x00\x00\x00\x00") +TEST_OBJLNK(1, 0, "\x00\x01\x00\x00") +TEST_OBJLNK(0, 1, "\x00\x00\x00\x01") +TEST_OBJLNK(1, 65535, "\x00\x01\xFF\xFF") +TEST_OBJLNK(65535, 1, "\xFF\xFF\x00\x01") TEST_OBJLNK(65535, 65535, "\xFF\xFF\xFF\xFF") ////////////////////////////////////////////////////////////// ENCODING // ARRAY @@ -258,12 +277,11 @@ AVS_UNIT_TEST(tlv_out_array, simple) { AVS_UNIT_ASSERT_SUCCESS(_anjay_output_ctx_destroy(&out)); - VERIFY_BYTES( - "\x88\x01\x0A" // array - "\x41\x2A\x45" // first entry - "\x64\x02\x02\x00\x0A\xA2\x89" // second entry - "\xC1\x02\x04" // another entry - ); + VERIFY_BYTES("\x88\x01\x0A" // array + "\x41\x2A\x45" // first entry + "\x64\x02\x02\x00\x0A\xA2\x89" // second entry + "\xC1\x02\x04" // another entry + ); } AVS_UNIT_TEST(tlv_out_array, too_long) { diff --git a/src/io/text.c b/src/io/text.c index 7244ab025..d085e5b0d 100644 --- a/src/io/text.c +++ b/src/io/text.c @@ -67,7 +67,8 @@ static int text_ret_string(anjay_output_ctx_t *ctx_, const char *value) { int retval = -1; if (!ctx->finished - && !(retval = avs_stream_write(ctx->stream, value, strlen(value)))) { + && !(retval = + avs_stream_write(ctx->stream, value, strlen(value)))) { ctx->finished = true; } return retval; @@ -101,8 +102,8 @@ static int text_ret_i64(anjay_output_ctx_t *ctx_, int64_t value) { return retval; } -static inline int text_ret_floating_point(text_out_t *ctx, double value, - int precision) { +static inline int +text_ret_floating_point(text_out_t *ctx, double value, int precision) { if (ctx->bytes) { return -1; } @@ -112,8 +113,8 @@ static inline int text_ret_floating_point(text_out_t *ctx, double value, // As printing floating-point numbers in C as pure decimal with sane // precision is tricky, let's take the spec a bit loosely for now. if (!ctx->finished - && !(retval = avs_stream_write_f(ctx->stream, "%.*g", - precision, value))) { + && !(retval = avs_stream_write_f(ctx->stream, "%.*g", precision, + value))) { ctx->finished = true; } return retval; @@ -131,17 +132,16 @@ static int text_ret_bool(anjay_output_ctx_t *ctx, bool value) { return text_ret_i32(ctx, value); } -static int text_ret_objlnk(anjay_output_ctx_t *ctx_, - anjay_oid_t oid, anjay_iid_t iid) { +static int +text_ret_objlnk(anjay_output_ctx_t *ctx_, anjay_oid_t oid, anjay_iid_t iid) { text_out_t *ctx = (text_out_t *) ctx_; if (ctx->bytes) { return -1; } int retval = -1; if (!ctx->finished - && !(retval = avs_stream_write_f(ctx->stream, - "%" PRIu16 ":%" PRIu16, - oid, iid))) { + && !(retval = avs_stream_write_f( + ctx->stream, "%" PRIu16 ":%" PRIu16, oid, iid))) { ctx->finished = true; } return retval; @@ -175,9 +175,10 @@ _anjay_output_text_create(avs_stream_abstract_t *stream, int *errno_ptr, anjay_msg_details_t *inout_details) { text_out_t *ctx = (text_out_t *) avs_calloc(1, sizeof(text_out_t)); - if (ctx && ((*errno_ptr = _anjay_handle_requested_format( - &inout_details->format, ANJAY_COAP_FORMAT_PLAINTEXT)) - || _anjay_coap_stream_setup_response(stream, inout_details))) { + if (ctx + && ((*errno_ptr = _anjay_handle_requested_format( + &inout_details->format, ANJAY_COAP_FORMAT_PLAINTEXT)) + || _anjay_coap_stream_setup_response(stream, inout_details))) { avs_free(ctx); return NULL; } @@ -203,11 +204,10 @@ typedef struct { char msg_finished; } text_in_t; -static int has_valid_padding(const char *buffer, - size_t size, - bool msg_finished) { +static int +has_valid_padding(const char *buffer, size_t size, bool msg_finished) { const char *last = buffer + size; - /* Note: buffer is size+1 in length. Last byte is a NULL terminator though. */ + // Note: buffer is size+1 in length. Last byte is a NULL terminator though. assert(!*last); return last - 1 >= buffer && *(last - 1) == '=' && !msg_finished ? -1 : 0; } @@ -242,8 +242,9 @@ static int text_get_some_bytes(anjay_input_ctx_t *ctx_, char stream_msg_finished = 0; while (buf_size > 0) { - if (avs_stream_read(ctx->stream, &stream_bytes_read, &stream_msg_finished, - encoded, sizeof(encoded) - 1)) { + if (avs_stream_read(ctx->stream, &stream_bytes_read, + &stream_msg_finished, encoded, + sizeof(encoded) - 1)) { return -1; } encoded[stream_bytes_read] = '\0'; @@ -273,9 +274,8 @@ static int text_get_some_bytes(anjay_input_ctx_t *ctx_, return 0; } -static int text_get_string(anjay_input_ctx_t *ctx, - char *out_buf, - size_t buf_size) { +static int +text_get_string(anjay_input_ctx_t *ctx, char *out_buf, size_t buf_size) { if (!buf_size || ((text_in_t *) ctx)->bytes_mode) { return -1; } @@ -284,8 +284,8 @@ static int text_get_string(anjay_input_ctx_t *ctx, char *endptr = out_buf + (buf_size - 1); do { size_t bytes_read = 0; - int retval = avs_stream_read(((text_in_t *) ctx)->stream, - &bytes_read, &message_finished, ptr, + int retval = avs_stream_read(((text_in_t *) ctx)->stream, &bytes_read, + &message_finished, ptr, (size_t) (endptr - ptr)); if (retval) { return retval; @@ -345,10 +345,9 @@ static int text_get_i64(anjay_input_ctx_t *ctx, int64_t *value) { long long ll; if (_anjay_safe_strtoll(buf, &ll) #if LLONG_MAX != INT64_MAX - || ll < INT64_MIN - || ll > INT64_MAX + || ll < INT64_MIN || ll > INT64_MAX #endif - ) { + ) { return ANJAY_ERR_BAD_REQUEST; } *value = (int64_t) ll; @@ -424,10 +423,8 @@ static int text_get_objlnk(anjay_input_ctx_t *ctx, *colon = '\0'; long long oid; long long iid; - if (_anjay_safe_strtoll(buf, &oid) - || _anjay_safe_strtoll(colon + 1, &iid) - || oid < 0 || oid > UINT16_MAX - || iid < 0 || iid > UINT16_MAX) { + if (_anjay_safe_strtoll(buf, &oid) || _anjay_safe_strtoll(colon + 1, &iid) + || oid < 0 || oid > UINT16_MAX || iid < 0 || iid > UINT16_MAX) { return ANJAY_ERR_BAD_REQUEST; } *out_oid = (anjay_oid_t) oid; @@ -474,5 +471,5 @@ int _anjay_input_text_create(anjay_input_ctx_t **out, } #ifdef ANJAY_TEST -#include "test/text.c" +# include "test/text.c" #endif diff --git a/src/io/tlv.h b/src/io/tlv.h index 5dd3ca8ce..4e3a26e1b 100644 --- a/src/io/tlv.h +++ b/src/io/tlv.h @@ -29,4 +29,3 @@ typedef enum { VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_IO_TLV_H */ - diff --git a/src/io/tlv_in.c b/src/io/tlv_in.c index 14da87e4e..8cd33468e 100644 --- a/src/io/tlv_in.c +++ b/src/io/tlv_in.c @@ -26,7 +26,7 @@ VISIBILITY_SOURCE_BEGIN typedef struct { - const avs_stream_v_table_t * vtable; + const avs_stream_v_table_t *vtable; avs_stream_abstract_t *backend; char finished; } tlv_single_msg_stream_wrapper_t; @@ -65,8 +65,8 @@ static int tlv_get_some_bytes(anjay_input_ctx_t *ctx_, *out_bytes_read = 0; buf_size = AVS_MIN(buf_size, ctx->length - ctx->bytes_read); int retval = avs_stream_read((avs_stream_abstract_t *) &ctx->stream, - out_bytes_read, &stream_finished, - out_buf, buf_size); + out_bytes_read, &stream_finished, out_buf, + buf_size); ctx->bytes_read += *out_bytes_read; if (retval) { return retval; @@ -109,9 +109,8 @@ static int tlv_read_whole_entry(anjay_input_ctx_t *ctx_, return tlv_read_to_end(ctx_, out_bytes_read, out_buf, buf_size); } -static int tlv_get_string(anjay_input_ctx_t *ctx, - char *out_buf, - size_t buf_size) { +static int +tlv_get_string(anjay_input_ctx_t *ctx, char *out_buf, size_t buf_size) { if (!buf_size) { return -1; } @@ -121,48 +120,49 @@ static int tlv_get_string(anjay_input_ctx_t *ctx, return retval; } -#define DEF_GETI(Bits) \ -static int tlv_get_i##Bits (anjay_input_ctx_t *ctx, int##Bits##_t *value) { \ - uint8_t bytes[Bits / 8]; \ - size_t bytes_read = 0; \ - int retval; \ - if ((retval = tlv_read_whole_entry(ctx, &bytes_read, bytes, sizeof(bytes))) \ - || !avs_is_power_of_2(bytes_read)) { \ - return retval ? retval : ANJAY_ERR_BAD_REQUEST; \ - } \ - *value = (bytes_read > 0 && ((int8_t) bytes[0]) < 0) ? -1 : 0; \ - for (size_t i = 0; i < bytes_read; ++i) { \ - *(uint##Bits##_t *) value <<= 8; \ - *value += bytes[i]; \ - } \ - return 0; \ -} +#define DEF_GETI(Bits) \ + static int tlv_get_i##Bits(anjay_input_ctx_t *ctx, int##Bits##_t *value) { \ + uint8_t bytes[Bits / 8]; \ + size_t bytes_read = 0; \ + int retval; \ + if ((retval = tlv_read_whole_entry(ctx, &bytes_read, bytes, \ + sizeof(bytes))) \ + || !avs_is_power_of_2(bytes_read)) { \ + return retval ? retval : ANJAY_ERR_BAD_REQUEST; \ + } \ + *value = (bytes_read > 0 && ((int8_t) bytes[0]) < 0) ? -1 : 0; \ + for (size_t i = 0; i < bytes_read; ++i) { \ + *(uint##Bits##_t *) value <<= 8; \ + *value += bytes[i]; \ + } \ + return 0; \ + } DEF_GETI(32) DEF_GETI(64) -#define DEF_GETF(Type) \ -static int tlv_get_##Type (anjay_input_ctx_t *ctx, Type *value) { \ - union { \ - uint32_t f32; \ - uint64_t f64; \ - } data; \ - size_t bytes_read = 0; \ - int retval = tlv_read_whole_entry(ctx, &bytes_read, &data, 8); \ - if (retval) { \ - return retval; \ - } \ - switch (bytes_read) { \ - case 4: \ - *value = (Type) _anjay_ntohf(data.f32); \ - return 0; \ - case 8: \ - *value = (Type) _anjay_ntohd(data.f64); \ - return 0; \ - default: \ - return ANJAY_ERR_BAD_REQUEST; \ - } \ -} +#define DEF_GETF(Type) \ + static int tlv_get_##Type(anjay_input_ctx_t *ctx, Type *value) { \ + union { \ + uint32_t f32; \ + uint64_t f64; \ + } data; \ + size_t bytes_read = 0; \ + int retval = tlv_read_whole_entry(ctx, &bytes_read, &data, 8); \ + if (retval) { \ + return retval; \ + } \ + switch (bytes_read) { \ + case 4: \ + *value = (Type) _anjay_ntohf(data.f32); \ + return 0; \ + case 8: \ + *value = (Type) _anjay_ntohd(data.f64); \ + return 0; \ + default: \ + return ANJAY_ERR_BAD_REQUEST; \ + } \ + } DEF_GETF(float) DEF_GETF(double) @@ -190,7 +190,8 @@ static int tlv_get_bool(anjay_input_ctx_t *ctx, bool *value) { } static int tlv_get_objlnk(anjay_input_ctx_t *ctx, - anjay_oid_t *out_oid, anjay_iid_t *out_iid) { + anjay_oid_t *out_oid, + anjay_iid_t *out_iid) { AVS_STATIC_ASSERT(sizeof(uint16_t[2]) == 4, uint16_t_array_size); uint16_t raw[2]; size_t bytes_read = 0; @@ -204,21 +205,20 @@ static int tlv_get_objlnk(anjay_input_ctx_t *ctx, return 0; } -#define DEF_READ_SHORTENED(Type) \ -static int read_shortened_##Type (avs_stream_abstract_t *stream, \ - size_t length, \ - Type *out) { \ - uint8_t bytes[sizeof(Type)]; \ - int retval = avs_stream_read_reliably(stream, bytes, length); \ - if (retval) { \ - return retval; \ - } \ - *out = 0; \ - for (size_t i = 0; i < length; ++i) { \ - *out = (Type) ((*out << 8) + bytes[i]); \ - } \ - return 0; \ -} +#define DEF_READ_SHORTENED(Type) \ + static int read_shortened_##Type(avs_stream_abstract_t *stream, \ + size_t length, Type *out) { \ + uint8_t bytes[sizeof(Type)]; \ + int retval = avs_stream_read_reliably(stream, bytes, length); \ + if (retval) { \ + return retval; \ + } \ + *out = 0; \ + for (size_t i = 0; i < length; ++i) { \ + *out = (Type) ((*out << 8) + bytes[i]); \ + } \ + return 0; \ + } DEF_READ_SHORTENED(uint16_t) DEF_READ_SHORTENED(size_t) @@ -250,7 +250,8 @@ static anjay_id_type_t convert_id_type(uint8_t typefield) { } static int tlv_get_id(anjay_input_ctx_t *ctx_, - anjay_id_type_t *out_type, uint16_t *out_id) { + anjay_id_type_t *out_type, + uint16_t *out_id) { tlv_in_t *ctx = (tlv_in_t *) ctx_; if (ctx->id >= 0) { *out_type = ctx->id_type; @@ -273,16 +274,16 @@ static int tlv_get_id(anjay_input_ctx_t *ctx_, } *out_type = convert_id_type(typefield); if ((retval = - read_shortened_uint16_t((avs_stream_abstract_t *) &ctx->stream, - (typefield & 0x20) ? 2 : 1, out_id))) { + read_shortened_uint16_t((avs_stream_abstract_t *) &ctx->stream, + (typefield & 0x20) ? 2 : 1, out_id))) { return retval; } size_t length_length = ((typefield >> 3) & 3); if (!length_length) { ctx->length = (typefield & 7); - } else if ((retval = - read_shortened_size_t((avs_stream_abstract_t *) &ctx->stream, - length_length, &ctx->length))) { + } else if ((retval = read_shortened_size_t( + (avs_stream_abstract_t *) &ctx->stream, length_length, + &ctx->length))) { return retval; } ctx->bytes_read = 0; @@ -301,9 +302,9 @@ static int tlv_next_entry(anjay_input_ctx_t *ctx_) { size_t ignore_bytes_read; bool message_finished = false; while (!message_finished) { - int retval = tlv_get_some_bytes(ctx_, &ignore_bytes_read, - &message_finished, - ignore, sizeof(ignore)); + int retval = + tlv_get_some_bytes(ctx_, &ignore_bytes_read, &message_finished, + ignore, sizeof(ignore)); if (retval) { return retval; } @@ -323,18 +324,18 @@ static int tlv_in_close(anjay_input_ctx_t *ctx_) { } static const anjay_input_ctx_vtable_t TLV_IN_VTABLE = { - tlv_get_some_bytes, - tlv_get_string, - tlv_get_i32, - tlv_get_i64, - tlv_get_float, - tlv_get_double, - tlv_get_bool, - tlv_get_objlnk, - tlv_in_attach_child, - tlv_get_id, - tlv_next_entry, - tlv_in_close + .some_bytes = tlv_get_some_bytes, + .string = tlv_get_string, + .i32 = tlv_get_i32, + .i64 = tlv_get_i64, + .f32 = tlv_get_float, + .f64 = tlv_get_double, + .boolean = tlv_get_bool, + .objlnk = tlv_get_objlnk, + .attach_child = tlv_in_attach_child, + .get_id = tlv_get_id, + .next_entry = tlv_next_entry, + .close = tlv_in_close }; static int tlv_safe_read(avs_stream_abstract_t *stream_, @@ -380,5 +381,5 @@ int _anjay_input_tlv_create(anjay_input_ctx_t **out, } #ifdef ANJAY_TEST -#include "test/tlv_in.c" +# include "test/tlv_in.c" #endif diff --git a/src/io/tlv_out.c b/src/io/tlv_out.c index c723a6c3e..809a0c772 100644 --- a/src/io/tlv_out.c +++ b/src/io/tlv_out.c @@ -96,20 +96,18 @@ static int write_shortened_u32(avs_stream_abstract_t *stream, uint32_t value) { static size_t header_size(uint16_t id, size_t length) { assert(length == (uint32_t) length); - return 1 + (size_t) u32_length(id) + - ((length > 7) ? (size_t) u32_length((uint32_t) length) : 0); + return 1 + (size_t) u32_length(id) + + ((length > 7) ? (size_t) u32_length((uint32_t) length) : 0); } -static int write_header(avs_stream_abstract_t *stream, - const tlv_id_t *id, - size_t length) { +static int +write_header(avs_stream_abstract_t *stream, const tlv_id_t *id, size_t length) { if (id->id != (uint16_t) id->id || length >> 24) { return -1; } - uint8_t typefield = (uint8_t) ( - ((id->type & 3) << 6) | - ((id->id > UINT8_MAX) ? 0x20 : 0) | - typefield_length((uint32_t) length)); + uint8_t typefield = + (uint8_t) (((id->type & 3) << 6) | ((id->id > UINT8_MAX) ? 0x20 : 0) + | typefield_length((uint32_t) length)); int retval = avs_stream_write(stream, &typefield, 1); if (!retval) { retval = write_shortened_u32(stream, (uint16_t) id->id); @@ -123,8 +121,10 @@ static int write_header(avs_stream_abstract_t *stream, static inline int ensure_valid_for_value(tlv_out_t *ctx) { return (ctx->slave || !(ctx->next_id.type == TLV_ID_RIID - || ctx->next_id.type == TLV_ID_RID) - || ctx->next_id.id < 0) ? -1 : 0; + || ctx->next_id.type == TLV_ID_RID) + || ctx->next_id.id < 0) + ? -1 + : 0; } static int write_entry(avs_stream_abstract_t *stream, @@ -139,8 +139,8 @@ static int write_entry(avs_stream_abstract_t *stream, } static char *add_buffered_entry(tlv_out_t *ctx, size_t length) { - tlv_entry_t *new_entry = (tlv_entry_t *) - AVS_LIST_NEW_BUFFER(sizeof(tlv_entry_t) + length); + tlv_entry_t *new_entry = + (tlv_entry_t *) AVS_LIST_NEW_BUFFER(sizeof(tlv_entry_t) + length); if (!new_entry) { return NULL; } @@ -222,7 +222,7 @@ static anjay_ret_bytes_ctx_t *add_entry(tlv_out_t *ctx, size_t length) { } } else if (ctx->parent) { if ((ctx->bytes_ctx.output.buffer_ptr = - add_buffered_entry(ctx, length))) { + add_buffered_entry(ctx, length))) { ctx->bytes_ctx.vtable = &BUFFERED_BYTES_VTABLE; ctx->bytes_ctx.bytes_left = length; return (anjay_ret_bytes_ctx_t *) &ctx->bytes_ctx; @@ -244,20 +244,21 @@ static int tlv_ret_string(anjay_output_ctx_t *ctx, const char *value) { return anjay_ret_bytes(ctx, value, strlen(value)); } -#define DEF_IRET(Half, Bits) \ -static int tlv_ret_i##Bits(anjay_output_ctx_t *ctx, int##Bits##_t value) { \ - if (value == (int##Half##_t) value) { \ - return tlv_ret_i##Half(ctx, (int##Half##_t) value); \ - } \ - uint##Bits##_t portable = avs_convert_be##Bits ((uint##Bits##_t) value); \ - return anjay_ret_bytes(ctx, &portable, sizeof(portable)); \ -} +#define DEF_IRET(Half, Bits) \ + static int tlv_ret_i##Bits(anjay_output_ctx_t *ctx, int##Bits##_t value) { \ + if (value == (int##Half##_t) value) { \ + return tlv_ret_i##Half(ctx, (int##Half##_t) value); \ + } \ + uint##Bits##_t portable = \ + avs_convert_be##Bits((uint##Bits##_t) value); \ + return anjay_ret_bytes(ctx, &portable, sizeof(portable)); \ + } static int tlv_ret_i8(anjay_output_ctx_t *ctx, int8_t value) { return anjay_ret_bytes(ctx, &value, 1); } -DEF_IRET( 8, 16) +DEF_IRET(8, 16) DEF_IRET(16, 32) DEF_IRET(32, 64) @@ -279,8 +280,8 @@ static int tlv_ret_bool(anjay_output_ctx_t *ctx, bool value) { return tlv_ret_i8(ctx, value); } -static int tlv_ret_objlnk(anjay_output_ctx_t *ctx, - anjay_oid_t oid, anjay_iid_t iid) { +static int +tlv_ret_objlnk(anjay_output_ctx_t *ctx, anjay_oid_t oid, anjay_iid_t iid) { uint32_t portable = avs_convert_be32(((uint32_t) oid << 16) | (uint32_t) iid); return anjay_ret_bytes(ctx, &portable, sizeof(portable)); @@ -299,9 +300,9 @@ static int tlv_slave_finish(tlv_out_t *ctx, tlv_id_type_t next_id_type) { { tlv_entry_t *entry = NULL; AVS_LIST_FOREACH(entry, ctx->entries) { - data_size += header_size((uint16_t) entry->id.id, - entry->data_length) + - entry->data_length; + data_size += + header_size((uint16_t) entry->id.id, entry->data_length) + + entry->data_length; } } char *buffer = (char *) (data_size ? avs_malloc(data_size) : NULL); @@ -328,8 +329,8 @@ static int tlv_slave_finish(tlv_out_t *ctx, tlv_id_type_t next_id_type) { } static anjay_output_ctx_t *tlv_ret_array_start(anjay_output_ctx_t *ctx) { - return tlv_slave_start((tlv_out_t *) ctx, - TLV_ID_RID, TLV_ID_RID_ARRAY, TLV_ID_RIID); + return tlv_slave_start((tlv_out_t *) ctx, TLV_ID_RID, TLV_ID_RID_ARRAY, + TLV_ID_RIID); } static int tlv_ret_array_finish(anjay_output_ctx_t *ctx_) { @@ -341,8 +342,8 @@ static int tlv_ret_array_finish(anjay_output_ctx_t *ctx_) { } static anjay_output_ctx_t *tlv_ret_object_start(anjay_output_ctx_t *ctx) { - return tlv_slave_start((tlv_out_t *) ctx, - TLV_ID_IID, TLV_ID_IID, TLV_ID_RID); + return tlv_slave_start((tlv_out_t *) ctx, TLV_ID_IID, TLV_ID_IID, + TLV_ID_RID); } static int tlv_ret_object_finish(anjay_output_ctx_t *ctx_) { @@ -353,8 +354,8 @@ static int tlv_ret_object_finish(anjay_output_ctx_t *ctx_) { return tlv_slave_finish(ctx, TLV_ID_IID); } -static int tlv_set_id(anjay_output_ctx_t *ctx_, - anjay_id_type_t type, uint16_t id) { +static int +tlv_set_id(anjay_output_ctx_t *ctx_, anjay_id_type_t type, uint16_t id) { tlv_out_t *ctx = (tlv_out_t *) ctx_; if (ctx->slave) { return -1; @@ -390,21 +391,21 @@ static int tlv_output_close(anjay_output_ctx_t *ctx_) { } static const anjay_output_ctx_vtable_t TLV_OUT_VTABLE = { - tlv_errno_ptr, - tlv_ret_bytes, - tlv_ret_string, - tlv_ret_i32, - tlv_ret_i64, - tlv_ret_float, - tlv_ret_double, - tlv_ret_bool, - tlv_ret_objlnk, - tlv_ret_array_start, - tlv_ret_array_finish, - tlv_ret_object_start, - tlv_ret_object_finish, - tlv_set_id, - tlv_output_close + .errno_ptr = tlv_errno_ptr, + .bytes_begin = tlv_ret_bytes, + .string = tlv_ret_string, + .i32 = tlv_ret_i32, + .i64 = tlv_ret_i64, + .f32 = tlv_ret_float, + .f64 = tlv_ret_double, + .boolean = tlv_ret_bool, + .objlnk = tlv_ret_objlnk, + .array_start = tlv_ret_array_start, + .array_finish = tlv_ret_array_finish, + .object_start = tlv_ret_object_start, + .object_finish = tlv_ret_object_finish, + .set_id = tlv_set_id, + .close = tlv_output_close }; static anjay_output_ctx_t *tlv_slave_start(tlv_out_t *ctx, @@ -412,9 +413,7 @@ static anjay_output_ctx_t *tlv_slave_start(tlv_out_t *ctx, tlv_id_type_t new_type, tlv_id_type_t inner_type) { tlv_out_t *object = NULL; - if (ctx->slave - || ctx->next_id.type != expected_type - || ctx->next_id.id < 0 + if (ctx->slave || ctx->next_id.type != expected_type || ctx->next_id.id < 0 || !(object = (tlv_out_t *) avs_calloc(1, sizeof(tlv_out_t)))) { return NULL; } @@ -447,9 +446,10 @@ _anjay_output_tlv_create(avs_stream_abstract_t *stream, int *errno_ptr, anjay_msg_details_t *inout_details) { anjay_output_ctx_t *ctx = _anjay_output_raw_tlv_create(stream); - if (ctx && ((*errno_ptr = _anjay_handle_requested_format( - &inout_details->format, ANJAY_COAP_FORMAT_TLV)) - || _anjay_coap_stream_setup_response(stream, inout_details))) { + if (ctx + && ((*errno_ptr = _anjay_handle_requested_format( + &inout_details->format, ANJAY_COAP_FORMAT_TLV)) + || _anjay_coap_stream_setup_response(stream, inout_details))) { avs_free(ctx); return NULL; } @@ -457,5 +457,5 @@ _anjay_output_tlv_create(avs_stream_abstract_t *stream, } #ifdef ANJAY_TEST -#include "test/tlv_out.c" +# include "test/tlv_out.c" #endif diff --git a/src/io/vtable.h b/src/io/vtable.h index 14d260a11..af178e504 100644 --- a/src/io/vtable.h +++ b/src/io/vtable.h @@ -24,8 +24,8 @@ VISIBILITY_PRIVATE_HEADER_BEGIN typedef int *(*anjay_output_ctx_errno_ptr_t)(anjay_output_ctx_t *); -typedef anjay_ret_bytes_ctx_t * -(*anjay_output_ctx_bytes_begin_t)(anjay_output_ctx_t *, size_t); +typedef anjay_ret_bytes_ctx_t *(*anjay_output_ctx_bytes_begin_t)( + anjay_output_ctx_t *, size_t); typedef int (*anjay_output_ctx_string_t)(anjay_output_ctx_t *, const char *); typedef int (*anjay_output_ctx_i32_t)(anjay_output_ctx_t *, int32_t); typedef int (*anjay_output_ctx_i64_t)(anjay_output_ctx_t *, int64_t); @@ -35,14 +35,15 @@ typedef int (*anjay_output_ctx_boolean_t)(anjay_output_ctx_t *, bool); typedef int (*anjay_output_ctx_objlnk_t)(anjay_output_ctx_t *, anjay_oid_t, anjay_iid_t); -typedef anjay_output_ctx_t * -(*anjay_output_ctx_array_start_t)(anjay_output_ctx_t *); +typedef anjay_output_ctx_t *(*anjay_output_ctx_array_start_t)( + anjay_output_ctx_t *); typedef int (*anjay_output_ctx_array_finish_t)(anjay_output_ctx_t *); -typedef anjay_output_ctx_t * -(*anjay_output_ctx_object_start_t)(anjay_output_ctx_t *); +typedef anjay_output_ctx_t *(*anjay_output_ctx_object_start_t)( + anjay_output_ctx_t *); typedef int (*anjay_output_ctx_object_finish_t)(anjay_output_ctx_t *); typedef int (*anjay_output_ctx_set_id_t)(anjay_output_ctx_t *, - anjay_id_type_t, uint16_t); + anjay_id_type_t, + uint16_t); typedef int (*anjay_output_ctx_close_t)(anjay_output_ctx_t *); typedef struct { @@ -71,8 +72,8 @@ typedef struct { anjay_ret_bytes_ctx_append_t append; } anjay_ret_bytes_ctx_vtable_t; -typedef int (*anjay_input_ctx_bytes_t)(anjay_input_ctx_t *, - size_t *, bool *, void *, size_t); +typedef int (*anjay_input_ctx_bytes_t)( + anjay_input_ctx_t *, size_t *, bool *, void *, size_t); typedef int (*anjay_input_ctx_string_t)(anjay_input_ctx_t *, char *, size_t); typedef int (*anjay_input_ctx_i32_t)(anjay_input_ctx_t *, int32_t *); typedef int (*anjay_input_ctx_i64_t)(anjay_input_ctx_t *, int64_t *); @@ -80,11 +81,13 @@ typedef int (*anjay_input_ctx_f32_t)(anjay_input_ctx_t *, float *); typedef int (*anjay_input_ctx_f64_t)(anjay_input_ctx_t *, double *); typedef int (*anjay_input_ctx_boolean_t)(anjay_input_ctx_t *, bool *); typedef int (*anjay_input_ctx_objlnk_t)(anjay_input_ctx_t *, - anjay_oid_t *, anjay_iid_t *); + anjay_oid_t *, + anjay_iid_t *); typedef int (*anjay_input_ctx_attach_child_t)(anjay_input_ctx_t *, anjay_input_ctx_t *); typedef int (*anjay_input_ctx_get_id_t)(anjay_input_ctx_t *, - anjay_id_type_t *, uint16_t *); + anjay_id_type_t *, + uint16_t *); typedef int (*anjay_input_ctx_next_entry_t)(anjay_input_ctx_t *); typedef int (*anjay_input_ctx_close_t)(anjay_input_ctx_t *); @@ -106,4 +109,3 @@ typedef struct { VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_IO_VTABLE_H */ - diff --git a/src/io_core.c b/src/io_core.c index 6e4d24ba8..ea3f90c7a 100644 --- a/src/io_core.c +++ b/src/io_core.c @@ -27,8 +27,8 @@ #include "coap/content_format.h" -#include "io_core.h" #include "io/vtable.h" +#include "io_core.h" VISIBILITY_SOURCE_BEGIN @@ -109,8 +109,7 @@ uint16_t _anjay_translate_legacy_content_format(uint16_t format) { #endif // WITH_LEGACY_CONTENT_FORMAT_SUPPORT -int _anjay_handle_requested_format(uint16_t *out_ptr, - uint16_t new_value) { +int _anjay_handle_requested_format(uint16_t *out_ptr, uint16_t new_value) { if (*out_ptr == AVS_COAP_FORMAT_NONE) { *out_ptr = new_value; } else if (_anjay_translate_legacy_content_format(*out_ptr) != new_value) { @@ -208,7 +207,8 @@ int anjay_ret_bool(anjay_output_ctx_t *ctx, bool value) { } int anjay_ret_objlnk(anjay_output_ctx_t *ctx, - anjay_oid_t oid, anjay_iid_t iid) { + anjay_oid_t oid, + anjay_iid_t iid) { if (!ctx->vtable->objlnk) { set_errno_not_implemented(ctx); return -1; @@ -236,7 +236,7 @@ int anjay_ret_array_finish(anjay_output_ctx_t *array_ctx) { return array_ctx->vtable->array_finish(array_ctx); } -anjay_output_ctx_t * _anjay_output_object_start(anjay_output_ctx_t *ctx) { +anjay_output_ctx_t *_anjay_output_object_start(anjay_output_ctx_t *ctx) { if (!ctx->vtable->object_start) { set_errno_not_implemented(ctx); return NULL; @@ -253,7 +253,8 @@ int _anjay_output_object_finish(anjay_output_ctx_t *ctx) { } int _anjay_output_set_id(anjay_output_ctx_t *ctx, - anjay_id_type_t type, uint16_t id) { + anjay_id_type_t type, + uint16_t id) { if (!ctx->vtable->set_id) { set_errno_not_implemented(ctx); return -1; @@ -311,7 +312,7 @@ int anjay_get_bytes(anjay_input_ctx_t *ctx, } typedef struct { - const avs_stream_v_table_t * const vtable; + const avs_stream_v_table_t *const vtable; anjay_input_ctx_t *backend; } bytes_stream_t; @@ -327,9 +328,8 @@ static int bytes_stream_read(avs_stream_abstract_t *stream, anjay_input_ctx_t **backend_ptr = &((bytes_stream_t *) stream)->backend; if (*backend_ptr) { bool message_finished; - int retval = anjay_get_bytes(*backend_ptr, - out_bytes_read, &message_finished, - buffer, buffer_length); + int retval = anjay_get_bytes(*backend_ptr, out_bytes_read, + &message_finished, buffer, buffer_length); if (!retval && (*out_message_finished = message_finished)) { *backend_ptr = NULL; } @@ -345,8 +345,10 @@ static int bytes_stream_close(avs_stream_abstract_t *stream) { char buf[256]; size_t bytes_read; char message_finished = 0; - while (!bytes_stream_read(stream, &bytes_read, &message_finished, - buf, sizeof(buf)) && !message_finished); + while (!bytes_stream_read(stream, &bytes_read, &message_finished, buf, + sizeof(buf)) + && !message_finished) + ; return 0; } @@ -412,7 +414,8 @@ int anjay_get_bool(anjay_input_ctx_t *ctx, bool *out) { } int anjay_get_objlnk(anjay_input_ctx_t *ctx, - anjay_oid_t *out_oid, anjay_iid_t *out_iid) { + anjay_oid_t *out_oid, + anjay_iid_t *out_iid) { if (!ctx->vtable->objlnk) { return -1; } @@ -449,7 +452,8 @@ anjay_input_ctx_t *anjay_get_array(anjay_input_ctx_t *ctx) { } int _anjay_input_get_id(anjay_input_ctx_t *ctx, - anjay_id_type_t *out_type, uint16_t *out_id) { + anjay_id_type_t *out_type, + uint16_t *out_id) { if (!ctx->vtable->get_id) { return ANJAY_ERR_BAD_REQUEST; } @@ -487,5 +491,5 @@ int _anjay_input_ctx_destroy(anjay_input_ctx_t **ctx_ptr) { } #ifdef ANJAY_TEST -#include "test/io.c" +# include "test/io.c" #endif diff --git a/src/io_core.h b/src/io_core.h index 8e73b701d..10de26eee 100644 --- a/src/io_core.h +++ b/src/io_core.h @@ -22,8 +22,8 @@ #include -#include #include +#include #include @@ -45,17 +45,17 @@ anjay_input_ctx_constructor_t _anjay_input_text_create; #ifdef WITH_LEGACY_CONTENT_FORMAT_SUPPORT uint16_t _anjay_translate_legacy_content_format(uint16_t format); #else -#define _anjay_translate_legacy_content_format(fmt) (fmt) +# define _anjay_translate_legacy_content_format(fmt) (fmt) #endif int _anjay_handle_requested_format(uint16_t *out_ptr, uint16_t requested_format); -#define ANJAY_OUTCTXERR_FORMAT_MISMATCH (-0xCE0) +#define ANJAY_OUTCTXERR_FORMAT_MISMATCH (-0xCE0) #define ANJAY_OUTCTXERR_METHOD_NOT_IMPLEMENTED (-0xCE1) /* returned from _anjay_output_ctx_destroy if no anjay_ret_* function was * called, making it impossible to determine actual resource format */ -#define ANJAY_OUTCTXERR_ANJAY_RET_NOT_CALLED (-0xCE2) +#define ANJAY_OUTCTXERR_ANJAY_RET_NOT_CALLED (-0xCE2) anjay_output_ctx_t * _anjay_output_dynamic_create(avs_stream_abstract_t *stream, @@ -73,35 +73,36 @@ _anjay_output_text_create(avs_stream_abstract_t *stream, int *errno_ptr, anjay_msg_details_t *inout_details); -anjay_output_ctx_t * -_anjay_output_raw_tlv_create(avs_stream_abstract_t *stream); +anjay_output_ctx_t *_anjay_output_raw_tlv_create(avs_stream_abstract_t *stream); anjay_output_ctx_t * _anjay_output_tlv_create(avs_stream_abstract_t *stream, int *errno_ptr, anjay_msg_details_t *inout_details); -#ifdef WITH_JSON +#if defined(WITH_JSON) || defined(WITH_SENML_JSON) anjay_output_ctx_t * _anjay_output_json_create(avs_stream_abstract_t *stream, int *errno_ptr, anjay_msg_details_t *inout_details, - const anjay_uri_path_t *uri); + const anjay_uri_path_t *uri, + uint16_t format); #endif int *_anjay_output_ctx_errno_ptr(anjay_output_ctx_t *ctx); -anjay_output_ctx_t * _anjay_output_object_start(anjay_output_ctx_t *ctx); +anjay_output_ctx_t *_anjay_output_object_start(anjay_output_ctx_t *ctx); int _anjay_output_object_finish(anjay_output_ctx_t *ctx); int _anjay_output_set_id(anjay_output_ctx_t *ctx, - anjay_id_type_t type, uint16_t id); + anjay_id_type_t type, + uint16_t id); int _anjay_output_ctx_destroy(anjay_output_ctx_t **ctx_ptr); avs_stream_abstract_t *_anjay_input_bytes_stream(anjay_input_ctx_t *ctx); -int _anjay_input_attach_child(anjay_input_ctx_t *ctx, - anjay_input_ctx_t *child); +int _anjay_input_attach_child(anjay_input_ctx_t *ctx, anjay_input_ctx_t *child); anjay_input_ctx_t *_anjay_input_nested_ctx(anjay_input_ctx_t *ctx); int _anjay_input_get_id(anjay_input_ctx_t *ctx, - anjay_id_type_t *out_type, uint16_t *out_id); + anjay_id_type_t *out_type, + uint16_t *out_id); int _anjay_input_next_entry(anjay_input_ctx_t *ctx); typedef struct anjay_output_buf_ctx { diff --git a/src/io_utils.c b/src/io_utils.c index fbb170c02..7fe70c769 100644 --- a/src/io_utils.c +++ b/src/io_utils.c @@ -76,7 +76,8 @@ static int generic_getter(anjay_input_ctx_t *ctx, } if (chunk_bytes_read > 0) { char *bigger_buffer = - (char *) avs_realloc(buffer, buffer_size + chunk_bytes_read); + (char *) avs_realloc(buffer, + buffer_size + chunk_bytes_read); if (!bigger_buffer) { result = ANJAY_ERR_INTERNAL; goto error; diff --git a/src/notify.c b/src/notify.c index e00b6edf1..3a440eb7c 100644 --- a/src/notify.c +++ b/src/notify.c @@ -22,14 +22,13 @@ #include "coap/content_format.h" #include "anjay_core.h" -#include "servers_utils.h" #include "observe/observe_core.h" +#include "servers_utils.h" VISIBILITY_SOURCE_BEGIN #ifdef WITH_OBSERVE -static int observe_notify(anjay_t *anjay, - anjay_notify_queue_t queue) { +static int observe_notify(anjay_t *anjay, anjay_notify_queue_t queue) { anjay_observe_key_t observe_key = { .connection = { .ssid = _anjay_dm_current_ssid(anjay), @@ -51,20 +50,20 @@ static int observe_notify(anjay_t *anjay, AVS_LIST_FOREACH(it2, it->resources_changed) { observe_key.iid = it2->iid; observe_key.rid = it2->rid; - _anjay_update_ret(&ret, - _anjay_observe_notify(anjay, &observe_key, - true)); + _anjay_update_ret( + &ret, _anjay_observe_notify(anjay, &observe_key, true)); } } } return ret; } #else // WITH_OBSERVE -#define observe_notify(anjay, queue) (0) +# define observe_notify(anjay, queue) (0) #endif // WITH_OBSERVE -static int security_modified_notify( - anjay_t *anjay, anjay_notify_queue_object_entry_t *security) { +static int +security_modified_notify(anjay_t *anjay, + anjay_notify_queue_object_entry_t *security) { if (anjay_is_offline(anjay)) { return 0; } @@ -97,8 +96,8 @@ static int server_modified_notify(anjay_t *anjay, MAKE_RESOURCE_PATH(ANJAY_DM_OID_SERVER, it->iid, ANJAY_DM_RID_SERVER_SSID); int64_t ssid; - if (_anjay_dm_res_read_i64(anjay, &path, &ssid) - || ssid <= 0 || ssid >= UINT16_MAX) { + if (_anjay_dm_res_read_i64(anjay, &path, &ssid) || ssid <= 0 + || ssid >= UINT16_MAX) { _anjay_update_ret(&ret, -1); } else if (_anjay_servers_find_active(anjay, (anjay_ssid_t) ssid)) { _anjay_update_ret(&ret, @@ -109,8 +108,7 @@ static int server_modified_notify(anjay_t *anjay, return ret; } -int _anjay_notify_perform(anjay_t *anjay, - anjay_notify_queue_t queue) { +int _anjay_notify_perform(anjay_t *anjay, anjay_notify_queue_t queue) { if (!queue) { return 0; } @@ -137,16 +135,14 @@ int _anjay_notify_perform(anjay_t *anjay, return ret; } -int _anjay_notify_flush(anjay_t *anjay, - anjay_notify_queue_t *queue_ptr) { +int _anjay_notify_flush(anjay_t *anjay, anjay_notify_queue_t *queue_ptr) { int result = _anjay_notify_perform(anjay, *queue_ptr); _anjay_notify_clear_queue(queue_ptr); return result; } static AVS_LIST(anjay_notify_queue_object_entry_t) * -find_or_create_object_entry(anjay_notify_queue_t *out_queue, - anjay_oid_t oid) { +find_or_create_object_entry(anjay_notify_queue_t *out_queue, anjay_oid_t oid) { AVS_LIST(anjay_notify_queue_object_entry_t) *it; AVS_LIST_FOREACH_PTR(it, out_queue) { if ((*it)->oid == oid) { @@ -217,7 +213,7 @@ int _anjay_notify_queue_instance_created(anjay_notify_queue_t *out_queue, return -1; } if (add_entry_to_iid_set( - &(*entry_ptr)->instance_set_changes.known_added_iids, iid)) { + &(*entry_ptr)->instance_set_changes.known_added_iids, iid)) { anjay_log(ERROR, "Out of memory"); delete_notify_queue_object_entry_if_empty(entry_ptr); return -1; @@ -238,7 +234,7 @@ int _anjay_notify_queue_instance_removed(anjay_notify_queue_t *out_queue, return -1; } if (add_entry_to_iid_set( - &(*entry_ptr)->instance_set_changes.known_removed_iids, iid)) { + &(*entry_ptr)->instance_set_changes.known_removed_iids, iid)) { anjay_log(ERROR, "Out of memory"); delete_notify_queue_object_entry_if_empty(entry_ptr); return -1; @@ -250,8 +246,7 @@ int _anjay_notify_queue_instance_removed(anjay_notify_queue_t *out_queue, } int _anjay_notify_queue_instance_set_unknown_change( - anjay_notify_queue_t *out_queue, - anjay_oid_t oid) { + anjay_notify_queue_t *out_queue, anjay_oid_t oid) { AVS_LIST(anjay_notify_queue_object_entry_t) *entry_ptr = find_or_create_object_entry(out_queue, oid); if (!entry_ptr) { @@ -262,9 +257,9 @@ int _anjay_notify_queue_instance_set_unknown_change( return 0; } -static int compare_resource_entries( - const anjay_notify_queue_resource_entry_t *left, - const anjay_notify_queue_resource_entry_t *right) { +static int +compare_resource_entries(const anjay_notify_queue_resource_entry_t *left, + const anjay_notify_queue_resource_entry_t *right) { int result = left->iid - right->iid; if (!result) { result = left->rid - right->rid; @@ -295,7 +290,8 @@ int _anjay_notify_queue_resource_change(anjay_notify_queue_t *out_queue, break; } } - if (!AVS_LIST_INSERT_NEW(anjay_notify_queue_resource_entry_t, res_entry_ptr)) { + if (!AVS_LIST_INSERT_NEW(anjay_notify_queue_resource_entry_t, + res_entry_ptr)) { anjay_log(ERROR, "Out of memory"); if (!(*obj_entry_ptr)->instance_set_changes.instance_set_changed && !(*obj_entry_ptr)->resources_changed) { @@ -333,7 +329,7 @@ int _anjay_notify_instance_created(anjay_t *anjay, anjay_iid_t iid) { int retval; (void) ((retval = _anjay_notify_queue_instance_created( - &anjay->scheduled_notify.queue, oid, iid)) + &anjay->scheduled_notify.queue, oid, iid)) || (retval = reschedule_notify(anjay))); return retval; } @@ -344,7 +340,7 @@ int anjay_notify_changed(anjay_t *anjay, anjay_rid_t rid) { int retval; (void) ((retval = _anjay_notify_queue_resource_change( - &anjay->scheduled_notify.queue, oid, iid, rid)) + &anjay->scheduled_notify.queue, oid, iid, rid)) || (retval = reschedule_notify(anjay))); return retval; } @@ -352,7 +348,7 @@ int anjay_notify_changed(anjay_t *anjay, int anjay_notify_instances_changed(anjay_t *anjay, anjay_oid_t oid) { int retval; (void) ((retval = _anjay_notify_queue_instance_set_unknown_change( - &anjay->scheduled_notify.queue, oid)) + &anjay->scheduled_notify.queue, oid)) || (retval = reschedule_notify(anjay))); return retval; } diff --git a/src/observe/observe_core.c b/src/observe/observe_core.c index 5e22f3b62..88caa0461 100644 --- a/src/observe/observe_core.c +++ b/src/observe/observe_core.c @@ -23,10 +23,10 @@ #include -#include "../servers_utils.h" -#include "../coap/content_format.h" #include "../anjay_core.h" +#include "../coap/content_format.h" #include "../dm/query.h" +#include "../servers_utils.h" #include "observe_internal.h" @@ -82,9 +82,8 @@ int _anjay_observe_entry_cmp(const void *left, const void *right) { int _anjay_observe_init(anjay_observe_state_t *observe, bool confirmable_notifications) { - if (!(observe->connection_entries = - AVS_RBTREE_NEW(anjay_observe_connection_entry_t, - connection_state_cmp))) { + if (!(observe->connection_entries = AVS_RBTREE_NEW( + anjay_observe_connection_entry_t, connection_state_cmp))) { anjay_log(ERROR, "Could not initialize Observe structures"); return -1; } @@ -133,7 +132,9 @@ const anjay_observe_stream_t *_anjay_observe_stream_initializer__(void) { static avs_stream_v_table_t vtable; static const anjay_observe_stream_t initializer = { - .outbuf = { .vtable = &vtable } + .outbuf = { + .vtable = &vtable + } }; static const anjay_coap_stream_ext_t coap_ext = { .setup_response = observe_setup_for_sending @@ -177,9 +178,9 @@ static void clear_entry(anjay_t *anjay, } } -static void delete_connection( - anjay_t *anjay, - AVS_RBTREE_ELEM(anjay_observe_connection_entry_t) *conn_ptr) { +static void +delete_connection(anjay_t *anjay, + AVS_RBTREE_ELEM(anjay_observe_connection_entry_t) *conn_ptr) { _anjay_observe_cleanup_connection(anjay->sched, *conn_ptr); AVS_RBTREE_DELETE_ELEM(anjay->observe.connection_entries, conn_ptr); } @@ -206,9 +207,8 @@ newest_value(const anjay_observe_entry_t *entry) { } } -static int schedule_trigger(anjay_t *anjay, - anjay_observe_entry_t *entry, - int32_t period) { +static int +schedule_trigger(anjay_t *anjay, anjay_observe_entry_t *entry, int32_t period) { if (period < 0) { return 0; } @@ -222,13 +222,14 @@ static int schedule_trigger(anjay_t *anjay, delay = AVS_TIME_DURATION_ZERO; } - anjay_log(TRACE, "Notify %s (format %" PRIu16 ", SSID %" PRIu16 ", " + anjay_log(TRACE, + "Notify %s (format %" PRIu16 ", SSID %" PRIu16 ", " "connection type %d) scheduled: +%ld.%09lds", ANJAY_DEBUG_MAKE_PATH(&MAKE_INSTANCE_OR_RESOURCE_PATH( entry->key.oid, entry->key.iid, entry->key.rid)), entry->key.format, entry->key.connection.ssid, - (int) entry->key.connection.type, - (long) delay.seconds, (long) delay.nanoseconds); + (int) entry->key.connection.type, (long) delay.seconds, + (long) delay.nanoseconds); _anjay_sched_del(anjay->sched, &entry->notify_task); return _anjay_sched(anjay->sched, &entry->notify_task, delay, @@ -240,7 +241,8 @@ create_resource_value(const anjay_msg_details_t *details, anjay_observe_entry_t *ref, const avs_coap_msg_identity_t *identity, double numeric, - const void *data, size_t size) { + const void *data, + size_t size) { AVS_LIST(anjay_observe_resource_value_t) result = (anjay_observe_resource_value_t *) AVS_LIST_NEW_BUFFER( offsetof(anjay_observe_resource_value_t, value) + size); @@ -271,8 +273,8 @@ static int insert_new_value(anjay_observe_connection_entry_t *conn_state, const void *data, size_t size) { AVS_LIST(anjay_observe_resource_value_t) res_value = - create_resource_value(details, entry, identity, - numeric, data, size); + create_resource_value(details, entry, identity, numeric, data, + size); if (!res_value) { return -1; } @@ -296,8 +298,8 @@ static int insert_error(anjay_t *anjay, .msg_code = _anjay_make_error_response_code(outer_result), .format = AVS_COAP_FORMAT_NONE }; - return insert_new_value(conn_state, entry, &details, identity, - NAN, NULL, 0); + return insert_new_value(conn_state, entry, &details, identity, NAN, NULL, + 0); } static int get_effective_attrs(anjay_t *anjay, @@ -324,7 +326,7 @@ static int get_effective_attrs(anjay_t *anjay, } if (details.iid != ANJAY_IID_INVALID && _anjay_dm_map_present_result(_anjay_dm_instance_present( - anjay, obj, details.iid, NULL))) { + anjay, obj, details.iid, NULL))) { // instance is no longer present, use invalid instead details.iid = ANJAY_IID_INVALID; } @@ -334,9 +336,9 @@ static int get_effective_attrs(anjay_t *anjay, } if (details.rid >= 0 && _anjay_dm_map_present_result( - _anjay_dm_resource_supported_and_present( - anjay, obj, key->iid, (anjay_rid_t) details.rid, - NULL))) { + _anjay_dm_resource_supported_and_present( + anjay, obj, key->iid, (anjay_rid_t) details.rid, + NULL))) { // if resource is no longer present, use invalid instead details.rid = -1; } @@ -356,22 +358,21 @@ int _anjay_observe_schedule_trigger(anjay_t *anjay, anjay_dm_internal_res_attrs_t attrs; int result; - (void)((result = get_attrs(anjay, &attrs, &entry->key)) + (void) ((result = get_attrs(anjay, &attrs, &entry->key)) || (result = schedule_trigger(anjay, entry, attrs.standard.common.max_period))); return result; } -static int insert_initial_value( - anjay_t *anjay, - anjay_observe_connection_entry_t *conn_state, - anjay_observe_entry_t *entry, - const anjay_msg_details_t *details, - const avs_coap_msg_identity_t *identity, - double numeric, - const void *data, - size_t size) { +static int insert_initial_value(anjay_t *anjay, + anjay_observe_connection_entry_t *conn_state, + anjay_observe_entry_t *entry, + const anjay_msg_details_t *details, + const avs_coap_msg_identity_t *identity, + double numeric, + const void *data, + size_t size) { assert(!entry->last_sent); assert(!entry->last_unsent); @@ -418,8 +419,10 @@ find_or_create_connection_state(anjay_t *anjay, connection_query(key)); if (!conn) { conn = AVS_RBTREE_ELEM_NEW(anjay_observe_connection_entry_t); - if (!conn || !(conn->entries = AVS_RBTREE_NEW(anjay_observe_entry_t, - _anjay_observe_entry_cmp))) { + if (!conn + || !(conn->entries = + AVS_RBTREE_NEW(anjay_observe_entry_t, + _anjay_observe_entry_cmp))) { anjay_log(ERROR, "Out of memory"); AVS_RBTREE_ELEM_DELETE_DETACHED(&conn); return NULL; @@ -487,8 +490,7 @@ void _anjay_observe_remove_entry(anjay_t *anjay, } } -void _anjay_observe_remove_by_msg_id(anjay_t *anjay, - uint16_t notify_id) { +void _anjay_observe_remove_by_msg_id(anjay_t *anjay, uint16_t notify_id) { AVS_RBTREE_ELEM(anjay_observe_connection_entry_t) conn; AVS_RBTREE_FOREACH(conn, anjay->observe.connection_entries) { AVS_RBTREE_ELEM(anjay_observe_entry_t) entry; @@ -502,9 +504,8 @@ void _anjay_observe_remove_by_msg_id(anjay_t *anjay, } } -static int observe_gc_ssid_iterate(anjay_t *anjay, - anjay_ssid_t ssid, - void *conn_ptr_) { +static int +observe_gc_ssid_iterate(anjay_t *anjay, anjay_ssid_t ssid, void *conn_ptr_) { AVS_RBTREE_ELEM(anjay_observe_connection_entry_t) *conn_ptr = (AVS_RBTREE_ELEM(anjay_observe_connection_entry_t) *) conn_ptr_; while (*conn_ptr && (*conn_ptr)->key.ssid < ssid) { @@ -532,23 +533,23 @@ void _anjay_observe_gc(anjay_t *anjay) { static bool has_pmax_expired(const anjay_observe_resource_value_t *value, const anjay_dm_attributes_t *attrs) { return attrs->max_period >= 0 - && avs_time_real_diff(avs_time_real_now(), value->timestamp).seconds - >= attrs->max_period; + && avs_time_real_diff(avs_time_real_now(), value->timestamp).seconds + >= attrs->max_period; } static bool process_step(const anjay_observe_resource_value_t *previous, const anjay_dm_resource_attributes_t *attrs, double value) { return !isnan(attrs->step) - && fabs(value - previous->numeric) >= attrs->step; + && fabs(value - previous->numeric) >= attrs->step; } static bool process_ltgt(const anjay_observe_resource_value_t *previous, double threshold, double value) { return !isnan(threshold) - && ((previous->numeric <= threshold && value > threshold) - || (previous->numeric >= threshold && value < threshold)); + && ((previous->numeric <= threshold && value > threshold) + || (previous->numeric >= threshold && value < threshold)); } static bool should_update(const anjay_observe_resource_value_t *previous, @@ -565,15 +566,15 @@ static bool should_update(const anjay_observe_resource_value_t *previous, if (isnan(numeric) || isnan(previous->numeric) || (isnan(attrs->greater_than) && isnan(attrs->less_than) - && isnan(attrs->step))) { + && isnan(attrs->step))) { // either previous or current value is not numeric, or none of lt/gt/st // attributes are set - notifying each value change return true; } return process_step(previous, attrs, numeric) - || process_ltgt(previous, attrs->less_than, numeric) - || process_ltgt(previous, attrs->greater_than, numeric); + || process_ltgt(previous, attrs->less_than, numeric) + || process_ltgt(previous, attrs->greater_than, numeric); } static inline ssize_t read_new_value(anjay_t *anjay, @@ -601,7 +602,8 @@ static inline ssize_t read_new_value(anjay_t *anjay, }, .requested_format = entry->key.format, .observe_serial = true - }, out_details, out_numeric, buffer, size); + }, + out_details, out_numeric, buffer, size); } static int get_conn_ref(anjay_t *anjay, @@ -616,27 +618,44 @@ static int get_conn_ref(anjay_t *anjay, } static int ensure_conn_online(anjay_t *anjay, anjay_connection_ref_t ref) { - if (_anjay_connection_current_mode(ref) == ANJAY_CONNECTION_QUEUE - && !_anjay_connection_get_online_socket(ref)) { - bool session_resumed; - if (_anjay_connection_bring_online(anjay, ref, &session_resumed)) { - anjay_log(ERROR, "broken socket for server %" PRIu16, + if (_anjay_connection_current_mode(ref) != ANJAY_CONNECTION_QUEUE + || _anjay_connection_get_online_socket(ref)) { + return 0; + } + + // We're checking the *primary* connection session timestamp, even though + // the notification might not be on a primary connection at all. However, we + // only care about whether we need to re-register, and this only concerns + // the primary connection. If the notification to be sent is not on the + // primary connection, the primary session timestamp is guaranteed not to + // change during _anjay_connection_bring_online(). + anjay_conn_session_token_t previous_session_token = + _anjay_server_primary_session_token(ref.server); + if (_anjay_connection_bring_online(anjay, ref)) { + anjay_log(ERROR, "broken socket for server %" PRIu16, + _anjay_server_ssid(ref.server)); + if (_anjay_schedule_server_reconnect(anjay, ref.server)) { + anjay_log(ERROR, + "could not schedule reconnect for server %" PRIu16, _anjay_server_ssid(ref.server)); - if (_anjay_schedule_server_reconnect(anjay, ref.server)) { - anjay_log(ERROR, - "could not schedule reconnect for server %" PRIu16, - _anjay_server_ssid(ref.server)); - } - return -1; } - if (!session_resumed) { - if (_anjay_schedule_reregister(anjay, ref.server)) { - anjay_log(ERROR, - "could not schedule reregister for server %" PRIu16, - _anjay_server_ssid(ref.server)); - } - return AVS_COAP_CTX_ERR_NETWORK; + return -1; + } + if (!_anjay_conn_session_tokens_equal(_anjay_server_primary_session_token( + ref.server), + previous_session_token)) { + if (_anjay_schedule_reregister(anjay, ref.server)) { + anjay_log(ERROR, + "could not schedule reregister for server %" PRIu16, + _anjay_server_ssid(ref.server)); } + // THIS IS A HACK: We have successfully connected, but we cannot send a + // notification, because Register needs to be sent first. That's why + // AVS_COAP_CTX_ERR_NETWORK is passed down the call stack to break the + // sending procedure. Notifications will be sent through + // _anjay_observe_sched_flush() called from + // perform_registration_action(). + return AVS_COAP_CTX_ERR_NETWORK; } return 0; } @@ -680,8 +699,8 @@ static int send_entry(anjay_t *anjay, anjay_observe_connection_entry_t *conn_state) { int result; anjay_connection_ref_t ref; - if ((result = get_conn_ref(anjay, &ref, - conn_state->key.ssid, conn_state->key.type)) + if ((result = get_conn_ref(anjay, &ref, conn_state->key.ssid, + conn_state->key.type)) || (result = ensure_conn_online(anjay, ref)) || (result = _anjay_bind_server_stream(anjay, ref))) { return result; @@ -699,13 +718,13 @@ static int send_entry(anjay_t *anjay, details.msg_type = AVS_COAP_MSG_CONFIRMABLE; } - (void) ((result = _anjay_coap_stream_setup_request( - anjay->comm_stream, &details, &id->token)) + (void) ((result = _anjay_coap_stream_setup_request(anjay->comm_stream, + &details, &id->token)) || (result = avs_stream_write(anjay->comm_stream, conn_state->unsent->value, conn_state->unsent->value_length)) || (result = _anjay_coap_stream_get_request_identity( - anjay->comm_stream, ¬ify_id)) + anjay->comm_stream, ¬ify_id)) || (result = avs_stream_finish_message(anjay->comm_stream))); _anjay_release_server_stream(anjay); @@ -717,7 +736,7 @@ static int send_entry(anjay_t *anjay, value_sent(conn_state); entry->last_sent->identity.msg_id = notify_id.msg_id; } else if (result == AVS_COAP_CTX_ERR_NETWORK - || result == AVS_COAP_CTX_ERR_TIMEOUT) { + || result == AVS_COAP_CTX_ERR_TIMEOUT) { anjay_log(ERROR, "network communication error while sending Observe"); _anjay_schedule_server_reconnect(anjay, server); } @@ -768,9 +787,10 @@ static observe_server_state_t server_state(anjay_t *anjay, anjay_ssid_t ssid) { } } - anjay_log(TRACE, "observe state for SSID %u: active %d, notification " - "storing %d", ssid, result.server_active, - result.notification_storing_enabled); + anjay_log(TRACE, + "observe state for SSID %u: active %d, notification " + "storing %d", + ssid, result.server_active, result.notification_storing_enabled); return result; } @@ -804,8 +824,7 @@ static int handle_send_queue_entry(anjay_t *anjay, remove_all_unsent_values(conn_state); } } - if (is_error - && result != AVS_COAP_CTX_ERR_NETWORK + if (is_error && result != AVS_COAP_CTX_ERR_NETWORK && (result == 0 || !observe_state.notification_storing_enabled)) { result = 1; } @@ -847,8 +866,8 @@ static void flush_send_queue(anjay_t *anjay, break; } } - if ((result = handle_send_queue_entry(anjay, conn, - *observe_state)) > 0) { + if ((result = handle_send_queue_entry(anjay, conn, *observe_state)) + > 0) { _anjay_observe_remove_entry(anjay, &key); // the above might've deleted the connection entry, // so we "re-find" it to check if it's still valid @@ -862,9 +881,8 @@ static void flush_send_queue(anjay_t *anjay, } static void flush_send_queue_job(anjay_t *anjay, const void *conn_ptr) { - flush_send_queue(anjay, - *(anjay_observe_connection_entry_t *const *) conn_ptr, - NULL); + flush_send_queue( + anjay, *(anjay_observe_connection_entry_t *const *) conn_ptr, NULL); } static int sched_flush_send_queue(anjay_t *anjay, @@ -875,8 +893,8 @@ static int sched_flush_send_queue(anjay_t *anjay, : "flush task already scheduled"); return 0; } - if (_anjay_sched_now(anjay->sched, &conn->flush_task, - flush_send_queue_job, &conn, sizeof(conn))) { + if (_anjay_sched_now(anjay->sched, &conn->flush_task, flush_send_queue_job, + &conn, sizeof(conn))) { anjay_log(WARNING, "Could not schedule notification flush"); return -1; } @@ -891,10 +909,11 @@ int _anjay_observe_sched_flush_current_connection(anjay_t *anjay) { return _anjay_observe_sched_flush(anjay, query_key); } -int _anjay_observe_sched_flush(anjay_t *anjay, - anjay_connection_key_t key) { - anjay_log(TRACE, "scheduling notifications flush for server SSID %u, " - "connection type %d", key.ssid, key.type); +int _anjay_observe_sched_flush(anjay_t *anjay, anjay_connection_key_t key) { + anjay_log(TRACE, + "scheduling notifications flush for server SSID %u, " + "connection type %d", + key.ssid, key.type); anjay_observe_connection_entry_t *conn = AVS_RBTREE_FIND(anjay->observe.connection_entries, connection_query(&key)); @@ -921,8 +940,8 @@ update_notification_value(anjay_t *anjay, return result; } - bool pmax_expired = has_pmax_expired(newest_value(entry), - &attrs.standard.common); + bool pmax_expired = + has_pmax_expired(newest_value(entry), &attrs.standard.common); char buf[ANJAY_MAX_OBSERVABLE_RESOURCE_SIZE]; anjay_msg_details_t observe_details; double numeric = NAN; @@ -934,20 +953,22 @@ update_notification_value(anjay_t *anjay, #ifdef WITH_CON_ATTR if (attrs.custom.data.con >= 0) { observe_details.msg_type = (attrs.custom.data.con > 0) - ? AVS_COAP_MSG_CONFIRMABLE : AVS_COAP_MSG_NON_CONFIRMABLE; + ? AVS_COAP_MSG_CONFIRMABLE + : AVS_COAP_MSG_NON_CONFIRMABLE; } else #endif // WITH_CON_ATTR { observe_details.msg_type = anjay->observe.confirmable_notifications - ? AVS_COAP_MSG_CONFIRMABLE : AVS_COAP_MSG_NON_CONFIRMABLE; + ? AVS_COAP_MSG_CONFIRMABLE + : AVS_COAP_MSG_NON_CONFIRMABLE; } - if (pmax_expired || should_update(newest_value(entry), &attrs.standard, - &observe_details, numeric, - buf, (size_t) size)) { + if (pmax_expired + || should_update(newest_value(entry), &attrs.standard, + &observe_details, numeric, buf, (size_t) size)) { result = insert_new_value(conn_state, entry, &observe_details, - &newest_value(entry)->identity, numeric, - buf, (size_t) size); + &newest_value(entry)->identity, numeric, buf, + (size_t) size); } if (schedule_trigger(anjay, entry, attrs.standard.common.max_period)) { @@ -994,7 +1015,7 @@ static inline int notify_entry(anjay_t *anjay, } #ifdef ANJAY_TEST -#include "test/observe_mock.h" +# include "test/observe_mock.h" #endif // ANJAY_TEST static int observe_notify_bound(anjay_t *anjay, @@ -1035,8 +1056,8 @@ observe_notify_wildcard_impl(anjay_t *anjay, } lower_bound.rid = -1; upper_bound.rid = -1; - return observe_notify_bound(anjay, connection, - &lower_bound, &upper_bound, obj); + return observe_notify_bound(anjay, connection, &lower_bound, &upper_bound, + obj); } static inline int @@ -1044,8 +1065,8 @@ observe_notify_iid_wildcard(anjay_t *anjay, anjay_observe_connection_entry_t *connection, const anjay_observe_key_t *specimen_key, const anjay_dm_object_def_t *const *obj) { - return observe_notify_wildcard_impl(anjay, connection, - specimen_key, obj, true); + return observe_notify_wildcard_impl(anjay, connection, specimen_key, obj, + true); } static inline int @@ -1053,8 +1074,8 @@ observe_notify_rid_wildcard(anjay_t *anjay, anjay_observe_connection_entry_t *connection, const anjay_observe_key_t *specimen_key, const anjay_dm_object_def_t *const *obj) { - return observe_notify_wildcard_impl(anjay, connection, - specimen_key, obj, false); + return observe_notify_wildcard_impl(anjay, connection, specimen_key, obj, + false); } /** @@ -1157,11 +1178,11 @@ static int observe_notify(anjay_t *anjay, } } else { _anjay_update_ret(&retval, - observe_notify_rid_wildcard(anjay, connection, - key, obj)); + observe_notify_rid_wildcard(anjay, connection, key, + obj)); _anjay_update_ret(&retval, - observe_notify_iid_wildcard(anjay, connection, - key, obj)); + observe_notify_iid_wildcard(anjay, connection, key, + obj)); } _anjay_update_ret(&retval, @@ -1184,7 +1205,7 @@ int _anjay_observe_notify(anjay_t *anjay, AVS_RBTREE_FOREACH(connection, anjay->observe.connection_entries) { /* Some compilers complain about promotion of comparison result, so * we're casting it to bool explicitly */ - if ((bool)(connection->key.ssid == key->connection.ssid) + if ((bool) (connection->key.ssid == key->connection.ssid) == invert_server_match) { continue; } @@ -1196,5 +1217,5 @@ int _anjay_observe_notify(anjay_t *anjay, } #ifdef ANJAY_TEST -#include "test/observe.c" +# include "test/observe.c" #endif // ANJAY_TEST diff --git a/src/observe/observe_core.h b/src/observe/observe_core.h index daa5fa76e..8b9cde0bc 100644 --- a/src/observe/observe_core.h +++ b/src/observe/observe_core.h @@ -88,13 +88,11 @@ int _anjay_observe_put_entry(anjay_t *anjay, void _anjay_observe_remove_entry(anjay_t *anjay, const anjay_observe_key_t *key); -void _anjay_observe_remove_by_msg_id(anjay_t *anjay, - uint16_t notify_id); +void _anjay_observe_remove_by_msg_id(anjay_t *anjay, uint16_t notify_id); int _anjay_observe_sched_flush_current_connection(anjay_t *anjay); -int _anjay_observe_sched_flush(anjay_t *anjay, - anjay_connection_key_t key); +int _anjay_observe_sched_flush(anjay_t *anjay, anjay_connection_key_t key); int _anjay_observe_notify(anjay_t *anjay, const anjay_observe_key_t *origin_key, @@ -106,16 +104,15 @@ anjay_output_ctx_t *_anjay_observe_decorate_ctx(anjay_output_ctx_t *backend, #else // WITH_OBSERVE -#define _anjay_observe_init(...) 0 -#define _anjay_observe_cleanup(...) ((void) 0) -#define _anjay_observe_sched_flush_current_connection(...) 0 -#define _anjay_observe_sched_flush(...) 0 -#define _anjay_observe_persist(...) 0 -#define _anjay_observe_restore(...) 0 +# define _anjay_observe_init(...) 0 +# define _anjay_observe_cleanup(...) ((void) 0) +# define _anjay_observe_sched_flush_current_connection(...) 0 +# define _anjay_observe_sched_flush(...) 0 +# define _anjay_observe_persist(...) 0 +# define _anjay_observe_restore(...) 0 #endif // WITH_OBSERVE VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_OBSERVE_CORE_H */ - diff --git a/src/observe/observe_internal.h b/src/observe/observe_internal.h index 4c8cb66aa..261439bbb 100644 --- a/src/observe/observe_internal.h +++ b/src/observe/observe_internal.h @@ -67,4 +67,3 @@ int _anjay_observe_schedule_trigger(anjay_t *anjay, VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_OBSERVE_INTERNAL_H */ - diff --git a/src/observe/observe_io.c b/src/observe/observe_io.c index 050804b44..9cce26a62 100644 --- a/src/observe/observe_io.c +++ b/src/observe/observe_io.c @@ -18,8 +18,8 @@ #include -#include "observe_core.h" #include "../io/vtable.h" +#include "observe_core.h" VISIBILITY_SOURCE_BEGIN @@ -28,14 +28,14 @@ VISIBILITY_SOURCE_BEGIN #define OTHER_ARGS_DECL3(_, T1, T2) , T1 _arg1__, T2 _arg2__ #define OTHER_ARGS_DECL(...) \ - AVS_CONCAT(OTHER_ARGS_DECL, AVS_VARARG_LENGTH(__VA_ARGS__))(__VA_ARGS__) + AVS_CONCAT(OTHER_ARGS_DECL, AVS_VARARG_LENGTH(__VA_ARGS__))(__VA_ARGS__) #define OTHER_ARGS_CALL1 #define OTHER_ARGS_CALL2 , _arg1__ #define OTHER_ARGS_CALL3 , _arg1__, _arg2__ #define OTHER_ARGS_CALL(...) \ - AVS_CONCAT(OTHER_ARGS_CALL, AVS_VARARG_LENGTH(__VA_ARGS__)) + AVS_CONCAT(OTHER_ARGS_CALL, AVS_VARARG_LENGTH(__VA_ARGS__)) typedef struct { const anjay_output_ctx_vtable_t *vtable; @@ -44,36 +44,39 @@ typedef struct { bool value_already_returned; } observe_out_t; -#define NON_NUMERIC(Rettype, Name, ...) \ -static Rettype Name (anjay_output_ctx_t *ctx_ \ - OTHER_ARGS_DECL(__VA_ARGS__)) { \ - observe_out_t *ctx = (observe_out_t *) ctx_; \ - *ctx->out_numeric = NAN; \ - ctx->value_already_returned = true; \ - return AVS_VARARG0(__VA_ARGS__) (ctx->backend \ - OTHER_ARGS_CALL(__VA_ARGS__)); \ -} +#define NON_NUMERIC(Rettype, Name, ...) \ + static Rettype Name( \ + anjay_output_ctx_t *ctx_ OTHER_ARGS_DECL(__VA_ARGS__)) { \ + observe_out_t *ctx = (observe_out_t *) ctx_; \ + *ctx->out_numeric = NAN; \ + ctx->value_already_returned = true; \ + return AVS_VARARG0(__VA_ARGS__)( \ + ctx->backend OTHER_ARGS_CALL(__VA_ARGS__)); \ + } -NON_NUMERIC(anjay_ret_bytes_ctx_t *, observe_bytes_begin, - anjay_ret_bytes_begin, size_t) +NON_NUMERIC(anjay_ret_bytes_ctx_t *, + observe_bytes_begin, + anjay_ret_bytes_begin, + size_t) NON_NUMERIC(int, observe_string, anjay_ret_string, const char *) -NON_NUMERIC(int, observe_bool, anjay_ret_bool, bool) +NON_NUMERIC(int, observe_bool, anjay_ret_bool, bool) NON_NUMERIC(int, observe_objlnk, anjay_ret_objlnk, anjay_oid_t, anjay_iid_t) NON_NUMERIC(anjay_output_ctx_t *, observe_array_start, anjay_ret_array_start) -NON_NUMERIC(anjay_output_ctx_t *, observe_object_start, +NON_NUMERIC(anjay_output_ctx_t *, + observe_object_start, _anjay_output_object_start) -#define NUMERIC(Typeid, Type) \ -static int observe_##Typeid (anjay_output_ctx_t *ctx_, Type value) { \ - observe_out_t *ctx = (observe_out_t *) ctx_; \ - if (ctx->value_already_returned) { \ - *ctx->out_numeric = NAN; \ - } else { \ - *ctx->out_numeric = (double) value; \ - ctx->value_already_returned = true; \ - } \ - return anjay_ret_##Typeid (ctx->backend, value); \ -} +#define NUMERIC(Typeid, Type) \ + static int observe_##Typeid(anjay_output_ctx_t *ctx_, Type value) { \ + observe_out_t *ctx = (observe_out_t *) ctx_; \ + if (ctx->value_already_returned) { \ + *ctx->out_numeric = NAN; \ + } else { \ + *ctx->out_numeric = (double) value; \ + ctx->value_already_returned = true; \ + } \ + return anjay_ret_##Typeid(ctx->backend, value); \ + } NUMERIC(i32, int32_t) NUMERIC(i64, int64_t) @@ -84,8 +87,8 @@ static int *observe_errno_ptr(anjay_output_ctx_t *ctx) { return _anjay_output_ctx_errno_ptr(((observe_out_t *) ctx)->backend); } -static int observe_set_id(anjay_output_ctx_t *ctx, - anjay_id_type_t type, uint16_t id) { +static int +observe_set_id(anjay_output_ctx_t *ctx, anjay_id_type_t type, uint16_t id) { return _anjay_output_set_id(((observe_out_t *) ctx)->backend, type, id); } diff --git a/src/observe/test/observe.c b/src/observe/test/observe.c index b191b5c78..c232b132f 100644 --- a/src/observe/test/observe.c +++ b/src/observe/test/observe.c @@ -28,7 +28,7 @@ #include "../../anjay_core.h" #include "../../sched_internal.h" -#include "../../servers/connection_info.h" +#include "../../servers/server_connections.h" #include "../../servers/servers_internal.h" #include "../../coap/test/utils.h" @@ -63,7 +63,8 @@ static void assert_observe(anjay_t *anjay, int32_t rid, uint16_t format, const anjay_msg_details_t *details, - const void *data, size_t length) { + const void *data, + size_t length) { anjay_observe_key_t key_query = { .connection = { .ssid = ssid, @@ -114,38 +115,40 @@ static void expect_read_notif_storing(anjay_t *anjay, ANJAY_MOCK_DM_BOOL(0, value)); } -#define ASSERT_SUCCESS_TEST_RESULT(Ssid) \ +#define ASSERT_SUCCESS_TEST_RESULT(Ssid) \ assert_observe(anjay, Ssid, 42, 69, 4, AVS_COAP_FORMAT_NONE, \ - &(const anjay_msg_details_t) { \ + &(const anjay_msg_details_t) { \ .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, \ - .msg_code = AVS_COAP_CODE_CONTENT, \ - .format = ANJAY_COAP_FORMAT_PLAINTEXT, \ - .observe_serial = true \ - }, "514", 3) - -#define SUCCESS_TEST(...) \ -DM_TEST_INIT_WITH_SSIDS(__VA_ARGS__); \ -do { \ - for (size_t i = 0; i < AVS_ARRAY_SIZE(ssids); ++i) { \ - DM_TEST_REQUEST(mocksocks[i], CON, GET, ID(0xFA3E), OBSERVE(0), \ - PATH("42", "69", "4")); \ - _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); \ - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); \ - _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, 0, \ - ANJAY_MOCK_DM_INT(0, 514)); \ - DM_TEST_EXPECT_READ_NULL_ATTRS(ssids[i], 69, 4); \ - DM_TEST_EXPECT_RESPONSE(mocksocks[i], ACK, CONTENT, ID(0xFA3E), \ - OBSERVE(0xF40000), CONTENT_FORMAT(PLAINTEXT), \ - PAYLOAD("514")); \ - AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[i])); \ - assert_observe_size(anjay, i + 1); \ - ASSERT_SUCCESS_TEST_RESULT(ssids[i]); \ - } \ - for (size_t i = 0; i < AVS_ARRAY_SIZE(ssids); ++i) { \ - DM_TEST_EXPECT_READ_NULL_ATTRS(ssids[i], 69, 4); \ - } \ - AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); \ -} while (0) + .msg_code = AVS_COAP_CODE_CONTENT, \ + .format = ANJAY_COAP_FORMAT_PLAINTEXT, \ + .observe_serial = true \ + }, \ + "514", 3) + +#define SUCCESS_TEST(...) \ + DM_TEST_INIT_WITH_SSIDS(__VA_ARGS__); \ + do { \ + for (size_t i = 0; i < AVS_ARRAY_SIZE(ssids); ++i) { \ + DM_TEST_REQUEST(mocksocks[i], CON, GET, ID(0xFA3E), OBSERVE(0), \ + PATH("42", "69", "4")); \ + _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); \ + _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); \ + _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, 0, \ + ANJAY_MOCK_DM_INT(0, 514)); \ + DM_TEST_EXPECT_READ_NULL_ATTRS(ssids[i], 69, 4); \ + DM_TEST_EXPECT_RESPONSE(mocksocks[i], ACK, CONTENT, ID(0xFA3E), \ + OBSERVE(0xF40000), \ + CONTENT_FORMAT(PLAINTEXT), \ + PAYLOAD("514")); \ + AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[i])); \ + assert_observe_size(anjay, i + 1); \ + ASSERT_SUCCESS_TEST_RESULT(ssids[i]); \ + } \ + for (size_t i = 0; i < AVS_ARRAY_SIZE(ssids); ++i) { \ + DM_TEST_EXPECT_READ_NULL_ATTRS(ssids[i], 69, 4); \ + } \ + AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); \ + } while (0) AVS_UNIT_TEST(observe, simple) { SUCCESS_TEST(14); @@ -188,15 +191,19 @@ AVS_UNIT_TEST(observe, overwrite) { PATH("42", "69", "4")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); - _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, 0, - ANJAY_MOCK_DM_ARRAY(0, - ANJAY_MOCK_DM_ARRAY_ENTRY(4, ANJAY_MOCK_DM_INT(0, 777)), - ANJAY_MOCK_DM_ARRAY_ENTRY(7, ANJAY_MOCK_DM_STRING(0, "Hi!")))); + _anjay_mock_dm_expect_resource_read( + anjay, &OBJ, 69, 4, 0, + ANJAY_MOCK_DM_ARRAY( + 0, + ANJAY_MOCK_DM_ARRAY_ENTRY(4, ANJAY_MOCK_DM_INT(0, 777)), + ANJAY_MOCK_DM_ARRAY_ENTRY(7, + ANJAY_MOCK_DM_STRING(0, "Hi!")))); DM_TEST_EXPECT_READ_NULL_ATTRS(14, 69, 4); -#define TLV_RESPONSE \ - "\x88\x04\x09" \ - "\x42\x04\x03\x09" \ - "\x43\x07" "Hi!" +#define TLV_RESPONSE \ + "\x88\x04\x09" \ + "\x42\x04\x03\x09" \ + "\x43\x07" \ + "Hi!" DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CONTENT, ID(0xFA3E), OBSERVE(0xF40000), CONTENT_FORMAT(TLV), PAYLOAD(TLV_RESPONSE)); @@ -208,7 +215,8 @@ AVS_UNIT_TEST(observe, overwrite) { .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_TLV, .observe_serial = true - }, TLV_RESPONSE, sizeof(TLV_RESPONSE) - 1); + }, + TLV_RESPONSE, sizeof(TLV_RESPONSE) - 1); #undef TLV_RESPONSE DM_TEST_EXPECT_READ_NULL_ATTRS(14, 69, 4); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); @@ -233,8 +241,10 @@ AVS_UNIT_TEST(observe, instance) { _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 6, 0); DM_TEST_EXPECT_READ_NULL_ATTRS(14, 69, -1); #define TLV_RESPONSE \ - "\xc3\x02" "wow" \ - "\xc8\x04\x0a" "such value" + "\xc3\x02" \ + "wow" \ + "\xc8\x04\x0a" \ + "such value" DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CONTENT, ID(0xFA3E), OBSERVE(0xF40000), CONTENT_FORMAT(TLV), PAYLOAD(TLV_RESPONSE)); @@ -247,7 +257,8 @@ AVS_UNIT_TEST(observe, instance) { .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_TLV, .observe_serial = true - }, TLV_RESPONSE, sizeof(TLV_RESPONSE) - 1); + }, + TLV_RESPONSE, sizeof(TLV_RESPONSE) - 1); #undef TLV_RESPONSE DM_TEST_EXPECT_READ_NULL_ATTRS(14, 69, -1); DM_TEST_EXPECT_READ_NULL_ATTRS(14, 69, 4); @@ -293,10 +304,14 @@ AVS_UNIT_TEST(observe, cancel_deregister) { DM_TEST_FINISH; } -static inline void -remove_server(AVS_LIST(anjay_server_info_t) *server_ptr) { - _anjay_connection_internal_clean_socket( - &(*server_ptr)->data_active.udp_connection); +static inline void remove_server(AVS_LIST(anjay_server_info_t) *server_ptr) { + anjay_server_connection_t *connection = + _anjay_get_server_connection((const anjay_connection_ref_t) { + .server = *server_ptr, + .conn_type = ANJAY_CONNECTION_UDP + }); + AVS_UNIT_ASSERT_NOT_NULL(connection); + _anjay_connection_internal_clean_socket(connection); AVS_LIST_DELETE(server_ptr); } @@ -341,7 +356,7 @@ static void expect_read_res_attrs(anjay_t *anjay, _anjay_mock_dm_expect_resource_read_attrs(anjay, obj_ptr, iid, rid, ssid, 0, attrs); if (!_anjay_dm_attributes_full( - _anjay_dm_get_internal_attrs_const(&attrs->standard.common))) { + _anjay_dm_get_internal_attrs_const(&attrs->standard.common))) { _anjay_mock_dm_expect_instance_read_default_attrs( anjay, obj_ptr, iid, ssid, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); _anjay_mock_dm_expect_object_read_default_attrs( @@ -380,14 +395,19 @@ static void notify_max_period_test(const char *con_notify_ack, DM_TEST_INIT_WITH_SSIDS(14); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_put_entry( - anjay, &(const anjay_observe_key_t) { - { 14, ANJAY_CONNECTION_UDP }, 42, 69, 4, AVS_COAP_FORMAT_NONE - }, &(const anjay_msg_details_t) { + anjay, + &(const anjay_observe_key_t) { { 14, ANJAY_CONNECTION_UDP }, + 42, + 69, + 4, + AVS_COAP_FORMAT_NONE }, + &(const anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_PLAINTEXT, .observe_serial = true - }, &NULL_IDENTITY, 514.0, "514", 3)); + }, + &NULL_IDENTITY, 514.0, "514", 3)); assert_observe_size(anjay, 1); ////// EMPTY SCHEDULER RUN ////// @@ -400,10 +420,9 @@ static void notify_max_period_test(const char *con_notify_ack, expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_STRING(0, "Hello")); - const avs_coap_msg_t *notify_response = COAP_MSG(NON, CONTENT, ID(0x69ED), - OBSERVE(0xF90000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Hello")); + const avs_coap_msg_t *notify_response = + COAP_MSG(NON, CONTENT, ID(0x69ED), OBSERVE(0xF90000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Hello")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response->content, notify_response->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); @@ -415,12 +434,22 @@ static void notify_max_period_test(const char *con_notify_ack, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_PLAINTEXT, .observe_serial = true - }, "Hello", 5); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); - AVS_UNIT_ASSERT_EQUAL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->last_sent->timestamp.since_real_epoch.seconds, - 1010); - AVS_UNIT_ASSERT_EQUAL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->last_confirmable.since_real_epoch.seconds, - 1000); + }, + "Hello", 5); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); + AVS_UNIT_ASSERT_EQUAL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->last_sent->timestamp.since_real_epoch.seconds, + 1010); + AVS_UNIT_ASSERT_EQUAL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->last_confirmable.since_real_epoch.seconds, + 1000); ////// CONFIRMABLE NOTIFICATION ////// _anjay_mock_clock_advance(avs_time_duration_diff( @@ -429,20 +458,24 @@ static void notify_max_period_test(const char *con_notify_ack, expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_STRING(0, "Hi!")); - const avs_coap_msg_t *con_notify_response = COAP_MSG(CON, CONTENT, - ID(0x69EE), - OBSERVE(0xB40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Hi!")); + const avs_coap_msg_t *con_notify_response = + COAP_MSG(CON, CONTENT, ID(0x69EE), OBSERVE(0xB40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Hi!")); avs_unit_mocksock_expect_output(mocksocks[0], con_notify_response->content, con_notify_response->length); avs_unit_mocksock_input(mocksocks[0], con_notify_ack, con_notify_ack_size); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, observe_size_after_ack); if (observe_size_after_ack) { - AVS_UNIT_ASSERT_EQUAL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->last_confirmable.since_real_epoch.seconds, - AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->last_sent->timestamp.since_real_epoch.seconds); - + AVS_UNIT_ASSERT_EQUAL( + AVS_RBTREE_FIRST( + AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->last_confirmable.since_real_epoch.seconds, + AVS_RBTREE_FIRST( + AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->last_sent->timestamp.since_real_epoch.seconds); } DM_TEST_FINISH; @@ -470,14 +503,19 @@ AVS_UNIT_TEST(notify, min_period) { DM_TEST_INIT_WITH_SSIDS(14); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_put_entry( - anjay, &(const anjay_observe_key_t) { - { 14, ANJAY_CONNECTION_UDP }, 42, 69, 4, AVS_COAP_FORMAT_NONE - }, &(const anjay_msg_details_t) { + anjay, + &(const anjay_observe_key_t) { { 14, ANJAY_CONNECTION_UDP }, + 42, + 69, + 4, + AVS_COAP_FORMAT_NONE }, + &(const anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_PLAINTEXT, .observe_serial = true - }, &NULL_IDENTITY, 514.0, "514", 3)); + }, + &NULL_IDENTITY, 514.0, "514", 3)); _anjay_mock_dm_expect_clean(); assert_observe_size(anjay, 1); @@ -493,10 +531,9 @@ AVS_UNIT_TEST(notify, min_period) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_STRING(0, "Hi!")); - const avs_coap_msg_t *notify_response = COAP_MSG(NON, CONTENT, ID(0x69ED), - OBSERVE(0xF90000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Hi!")); + const avs_coap_msg_t *notify_response = + COAP_MSG(NON, CONTENT, ID(0x69ED), OBSERVE(0xF90000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Hi!")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response->content, notify_response->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); @@ -512,7 +549,10 @@ AVS_UNIT_TEST(notify, min_period) { expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_STRING(0, "Hi!")); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); DM_TEST_FINISH; } @@ -523,14 +563,19 @@ AVS_UNIT_TEST(notify, confirmable) { (.confirmable_notifications = true)); DM_TEST_EXPECT_READ_NULL_ATTRS(14, 69, 4); AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_put_entry( - anjay, &(const anjay_observe_key_t) { - { 14, ANJAY_CONNECTION_UDP }, 42, 69, 4, AVS_COAP_FORMAT_NONE - }, &(const anjay_msg_details_t) { + anjay, + &(const anjay_observe_key_t) { { 14, ANJAY_CONNECTION_UDP }, + 42, + 69, + 4, + AVS_COAP_FORMAT_NONE }, + &(const anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_PLAINTEXT, .observe_serial = true - }, &(avs_coap_msg_identity_t) {}, 514.0, "514", 3)); + }, + &(avs_coap_msg_identity_t) {}, 514.0, "514", 3)); assert_observe_size(anjay, 1); ////// EMPTY SCHEDULER RUN ////// @@ -546,10 +591,9 @@ AVS_UNIT_TEST(notify, confirmable) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); DM_TEST_EXPECT_READ_NULL_ATTRS(14, 69, 4); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 42)); - const avs_coap_msg_t *notify_response = COAP_MSG(CON, CONTENT, ID(0x69ED), - OBSERVE(0xF90000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("42")); + const avs_coap_msg_t *notify_response = + COAP_MSG(CON, CONTENT, ID(0x69ED), OBSERVE(0xF90000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("42")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response->content, notify_response->length); const avs_coap_msg_t *notify_ack = COAP_MSG(ACK, EMPTY, ID(0x69ED)); @@ -578,14 +622,19 @@ AVS_UNIT_TEST(notify, extremes) { DM_TEST_INIT_WITH_SSIDS(14); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_put_entry( - anjay, &(const anjay_observe_key_t) { - { 14, ANJAY_CONNECTION_UDP }, 42, 69, 4, AVS_COAP_FORMAT_NONE - }, &(const anjay_msg_details_t) { + anjay, + &(const anjay_observe_key_t) { { 14, ANJAY_CONNECTION_UDP }, + 42, + 69, + 4, + AVS_COAP_FORMAT_NONE }, + &(const anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_PLAINTEXT, .observe_serial = true - }, &NULL_IDENTITY, 514.0, "514", 3)); + }, + &NULL_IDENTITY, 514.0, "514", 3)); _anjay_mock_dm_expect_clean(); assert_observe_size(anjay, 1); @@ -596,15 +645,17 @@ AVS_UNIT_TEST(notify, extremes) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_FLOAT(0, 42.43)); - const avs_coap_msg_t *notify_response = COAP_MSG(NON, CONTENT, ID(0x69ED), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("42.43")); + const avs_coap_msg_t *notify_response = + COAP_MSG(NON, CONTENT, ID(0x69ED), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("42.43")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response->content, notify_response->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// EVEN LESS ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -615,7 +666,10 @@ AVS_UNIT_TEST(notify, extremes) { expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_FLOAT(0, 14.7)); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// IN BETWEEN ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -624,15 +678,17 @@ AVS_UNIT_TEST(notify, extremes) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 695)); - const avs_coap_msg_t *notify_response2 = COAP_MSG(NON, CONTENT, ID(0x69EE), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("695")); + const avs_coap_msg_t *notify_response2 = + COAP_MSG(NON, CONTENT, ID(0x69EE), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("695")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response2->content, notify_response2->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// EQUAL - STILL NOT CROSSING ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -643,7 +699,10 @@ AVS_UNIT_TEST(notify, extremes) { expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 69)); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// GREATER ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -652,15 +711,17 @@ AVS_UNIT_TEST(notify, extremes) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 1024)); - const avs_coap_msg_t *notify_response3 = COAP_MSG(NON, CONTENT, ID(0x69EF), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("1024")); + const avs_coap_msg_t *notify_response3 = + COAP_MSG(NON, CONTENT, ID(0x69EF), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("1024")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response3->content, notify_response3->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// STILL GREATER ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -671,7 +732,10 @@ AVS_UNIT_TEST(notify, extremes) { expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 999)); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// LESS AGAIN ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -680,15 +744,17 @@ AVS_UNIT_TEST(notify, extremes) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_FLOAT(0, -69.75)); - const avs_coap_msg_t *notify_response4 = COAP_MSG(NON, CONTENT, ID(0x69F0), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("-69.75")); + const avs_coap_msg_t *notify_response4 = + COAP_MSG(NON, CONTENT, ID(0x69F0), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("-69.75")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response4->content, notify_response4->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); DM_TEST_FINISH; } @@ -710,14 +776,19 @@ AVS_UNIT_TEST(notify, greater_only) { DM_TEST_INIT_WITH_SSIDS(14); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_put_entry( - anjay, &(const anjay_observe_key_t) { - { 14, ANJAY_CONNECTION_UDP }, 42, 69, 4, AVS_COAP_FORMAT_NONE - }, &(const anjay_msg_details_t) { + anjay, + &(const anjay_observe_key_t) { { 14, ANJAY_CONNECTION_UDP }, + 42, + 69, + 4, + AVS_COAP_FORMAT_NONE }, + &(const anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_PLAINTEXT, .observe_serial = true - }, &NULL_IDENTITY, 514.0, "514", 3)); + }, + &NULL_IDENTITY, 514.0, "514", 3)); _anjay_mock_dm_expect_clean(); assert_observe_size(anjay, 1); @@ -730,7 +801,10 @@ AVS_UNIT_TEST(notify, greater_only) { expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 9001)); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// LESS ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -739,15 +813,17 @@ AVS_UNIT_TEST(notify, greater_only) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 42)); - const avs_coap_msg_t *notify_response = COAP_MSG(NON, CONTENT, ID(0x69ED), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("42")); + const avs_coap_msg_t *notify_response = + COAP_MSG(NON, CONTENT, ID(0x69ED), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("42")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response->content, notify_response->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// GREATER AGAIN ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -756,15 +832,17 @@ AVS_UNIT_TEST(notify, greater_only) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 77)); - const avs_coap_msg_t *notify_response2 = COAP_MSG(NON, CONTENT, ID(0x69EE), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("77")); + const avs_coap_msg_t *notify_response2 = + COAP_MSG(NON, CONTENT, ID(0x69EE), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("77")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response2->content, notify_response2->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); DM_TEST_FINISH; } @@ -786,14 +864,19 @@ AVS_UNIT_TEST(notify, less_only) { DM_TEST_INIT_WITH_SSIDS(14); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_put_entry( - anjay, &(const anjay_observe_key_t) { - { 14, ANJAY_CONNECTION_UDP }, 42, 69, 4, AVS_COAP_FORMAT_NONE - }, &(const anjay_msg_details_t) { + anjay, + &(const anjay_observe_key_t) { { 14, ANJAY_CONNECTION_UDP }, + 42, + 69, + 4, + AVS_COAP_FORMAT_NONE }, + &(const anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_PLAINTEXT, .observe_serial = true - }, &NULL_IDENTITY, 1337.0, "1337", 3)); + }, + &NULL_IDENTITY, 1337.0, "1337", 3)); _anjay_mock_dm_expect_clean(); assert_observe_size(anjay, 1); @@ -804,15 +887,17 @@ AVS_UNIT_TEST(notify, less_only) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 42)); - const avs_coap_msg_t *notify_response = COAP_MSG(NON, CONTENT, ID(0x69ED), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("42")); + const avs_coap_msg_t *notify_response = + COAP_MSG(NON, CONTENT, ID(0x69ED), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("42")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response->content, notify_response->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// STILL LESS ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -823,7 +908,10 @@ AVS_UNIT_TEST(notify, less_only) { expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 514)); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// GREATER ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -832,15 +920,17 @@ AVS_UNIT_TEST(notify, less_only) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 9001)); - const avs_coap_msg_t *notify_response2 = COAP_MSG(NON, CONTENT, ID(0x69EE), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("9001")); + const avs_coap_msg_t *notify_response2 = + COAP_MSG(NON, CONTENT, ID(0x69EE), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("9001")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response2->content, notify_response2->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// LESS AGAIN ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -849,15 +939,17 @@ AVS_UNIT_TEST(notify, less_only) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 69)); - const avs_coap_msg_t *notify_response3 = COAP_MSG(NON, CONTENT, ID(0x69EF), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("69")); + const avs_coap_msg_t *notify_response3 = + COAP_MSG(NON, CONTENT, ID(0x69EF), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("69")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response3->content, notify_response3->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); DM_TEST_FINISH; } @@ -879,14 +971,19 @@ AVS_UNIT_TEST(notify, step) { DM_TEST_INIT_WITH_SSIDS(14); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_put_entry( - anjay, &(const anjay_observe_key_t) { - { 14, ANJAY_CONNECTION_UDP }, 42, 69, 4, AVS_COAP_FORMAT_NONE - }, &(const anjay_msg_details_t) { + anjay, + &(const anjay_observe_key_t) { { 14, ANJAY_CONNECTION_UDP }, + 42, + 69, + 4, + AVS_COAP_FORMAT_NONE }, + &(const anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_PLAINTEXT, .observe_serial = true - }, &NULL_IDENTITY, 514.0, "514", 3)); + }, + &NULL_IDENTITY, 514.0, "514", 3)); _anjay_mock_dm_expect_clean(); assert_observe_size(anjay, 1); @@ -899,7 +996,10 @@ AVS_UNIT_TEST(notify, step) { expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_FLOAT(0, 523.5)); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// INCREASE BY EXACTLY stp ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -908,15 +1008,17 @@ AVS_UNIT_TEST(notify, step) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 524)); - const avs_coap_msg_t *notify_response0 = COAP_MSG(NON, CONTENT, ID(0x69ED), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("524")); + const avs_coap_msg_t *notify_response0 = + COAP_MSG(NON, CONTENT, ID(0x69ED), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("524")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response0->content, notify_response0->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// INCREASE BY OVER stp ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -925,15 +1027,17 @@ AVS_UNIT_TEST(notify, step) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_FLOAT(0, 540.048)); - const avs_coap_msg_t *notify_response1 = COAP_MSG(NON, CONTENT, ID(0x69EE), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("540.048")); + const avs_coap_msg_t *notify_response1 = + COAP_MSG(NON, CONTENT, ID(0x69EE), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("540.048")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response1->content, notify_response1->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// NON-NUMERIC VALUE ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -942,15 +1046,17 @@ AVS_UNIT_TEST(notify, step) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_STRING(0, "trololo")); - const avs_coap_msg_t *notify_response2 = COAP_MSG(NON, CONTENT, ID(0x69EF), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("trololo")); + const avs_coap_msg_t *notify_response2 = + COAP_MSG(NON, CONTENT, ID(0x69EF), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("trololo")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response2->content, notify_response2->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// BACK TO NUMBERS ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -959,15 +1065,17 @@ AVS_UNIT_TEST(notify, step) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 42)); - const avs_coap_msg_t *notify_response3 = COAP_MSG(NON, CONTENT, ID(0x69F0), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("42")); + const avs_coap_msg_t *notify_response3 = + COAP_MSG(NON, CONTENT, ID(0x69F0), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("42")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response3->content, notify_response3->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// TOO LITTLE DECREASE ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -978,7 +1086,10 @@ AVS_UNIT_TEST(notify, step) { expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_FLOAT(0, 32.001)); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// DECREASE BY EXACTLY stp ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -987,15 +1098,17 @@ AVS_UNIT_TEST(notify, step) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 31)); - const avs_coap_msg_t *notify_response4 = COAP_MSG(NON, CONTENT, ID(0x69F1), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("31")); + const avs_coap_msg_t *notify_response4 = + COAP_MSG(NON, CONTENT, ID(0x69F1), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("31")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response4->content, notify_response4->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// DECREASE BY MORE THAN stp ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -1004,15 +1117,17 @@ AVS_UNIT_TEST(notify, step) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 20)); - const avs_coap_msg_t *notify_response5 = COAP_MSG(NON, CONTENT, ID(0x69F2), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("20")); + const avs_coap_msg_t *notify_response5 = + COAP_MSG(NON, CONTENT, ID(0x69F2), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("20")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response5->content, notify_response5->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); ////// INCREASE BY EXACTLY stp ////// expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); @@ -1021,15 +1136,17 @@ AVS_UNIT_TEST(notify, step) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_INT(0, 30)); - const avs_coap_msg_t *notify_response6 = COAP_MSG(NON, CONTENT, ID(0x69F3), - OBSERVE(0xF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("30")); + const avs_coap_msg_t *notify_response6 = + COAP_MSG(NON, CONTENT, ID(0x69F3), OBSERVE(0xF40000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("30")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response6->content, notify_response6->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); assert_observe_size(anjay, 1); - AVS_UNIT_ASSERT_NOT_NULL(AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries)->entries)->notify_task); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_RBTREE_FIRST(AVS_RBTREE_FIRST(anjay->observe.connection_entries) + ->entries) + ->notify_task); DM_TEST_FINISH; } @@ -1048,7 +1165,9 @@ AVS_UNIT_TEST(notify, multiple_formats) { }; static avs_coap_msg_identity_t identity = { .msg_id = 0, - .token = { .size = 1 } + .token = { + .size = 1 + } }; ////// INITIALIZATION ////// @@ -1056,37 +1175,51 @@ AVS_UNIT_TEST(notify, multiple_formats) { identity.token.bytes[0] = 'N'; expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_put_entry( - anjay, &(const anjay_observe_key_t) { - { 14, ANJAY_CONNECTION_UDP }, 42, 69, 4, AVS_COAP_FORMAT_NONE - }, &(const anjay_msg_details_t) { + anjay, + &(const anjay_observe_key_t) { { 14, ANJAY_CONNECTION_UDP }, + 42, + 69, + 4, + AVS_COAP_FORMAT_NONE }, + &(const anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_PLAINTEXT, .observe_serial = true - }, &identity, 514.0, "514", 3)); + }, + &identity, 514.0, "514", 3)); identity.token.bytes[0] = 'P'; expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_put_entry( - anjay, &(const anjay_observe_key_t) { - { 14, ANJAY_CONNECTION_UDP }, - 42, 69, 4, ANJAY_COAP_FORMAT_PLAINTEXT - }, &(const anjay_msg_details_t) { + anjay, + &(const anjay_observe_key_t) { { 14, ANJAY_CONNECTION_UDP }, + 42, + 69, + 4, + ANJAY_COAP_FORMAT_PLAINTEXT }, + &(const anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_PLAINTEXT, .observe_serial = true - }, &identity, 514.0, "514", 3)); + }, + &identity, 514.0, "514", 3)); identity.token.bytes[0] = 'T'; expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_put_entry( - anjay, &(const anjay_observe_key_t) { - { 14, ANJAY_CONNECTION_UDP }, 42, 69, 4, ANJAY_COAP_FORMAT_TLV - }, &(const anjay_msg_details_t) { + anjay, + &(const anjay_observe_key_t) { { 14, ANJAY_CONNECTION_UDP }, + 42, + 69, + 4, + ANJAY_COAP_FORMAT_TLV }, + &(const anjay_msg_details_t) { .msg_type = AVS_COAP_MSG_ACKNOWLEDGEMENT, .msg_code = AVS_COAP_CODE_CONTENT, .format = ANJAY_COAP_FORMAT_TLV, .observe_serial = true - }, &identity, 514.0, "\xc2\x04\x02\x02", 4)); + }, + &identity, 514.0, "\xc2\x04\x02\x02", 4)); assert_observe_size(anjay, 3); ////// NOTIFICATION ////// @@ -1095,33 +1228,29 @@ AVS_UNIT_TEST(notify, multiple_formats) { expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_STRING(0, "Hello")); - const avs_coap_msg_t *n_notify_response = COAP_MSG(NON, CONTENT, - ID(0x69ED, "N"), - OBSERVE(0xF90000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Hello")); + const avs_coap_msg_t *n_notify_response = + COAP_MSG(NON, CONTENT, ID(0x69ED, "N"), OBSERVE(0xF90000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Hello")); avs_unit_mocksock_expect_output(mocksocks[0], n_notify_response->content, n_notify_response->length); // plaintext expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_STRING(0, "Hello")); - const avs_coap_msg_t *p_notify_response = COAP_MSG(NON, CONTENT, - ID(0x69EE, "P"), - OBSERVE(0xF90000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Hello")); + const avs_coap_msg_t *p_notify_response = + COAP_MSG(NON, CONTENT, ID(0x69EE, "P"), OBSERVE(0xF90000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Hello")); avs_unit_mocksock_expect_output(mocksocks[0], p_notify_response->content, - p_notify_response->length); + p_notify_response->length); // TLV expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_STRING(0, "Hello")); - const avs_coap_msg_t *t_notify_response = COAP_MSG(NON, CONTENT, - ID(0x69EF, "T"), - OBSERVE(0xF90000), - CONTENT_FORMAT(TLV), - PAYLOAD("\xc5\x04" "Hello")); + const avs_coap_msg_t *t_notify_response = + COAP_MSG(NON, CONTENT, ID(0x69EF, "T"), OBSERVE(0xF90000), + CONTENT_FORMAT(TLV), + PAYLOAD("\xc5\x04" + "Hello")); avs_unit_mocksock_expect_output(mocksocks[0], t_notify_response->content, t_notify_response->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); @@ -1134,11 +1263,9 @@ AVS_UNIT_TEST(notify, multiple_formats) { expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_BYTES(0, "\x12\x34\x56\x78")); - const avs_coap_msg_t *n_bytes_response = COAP_MSG(NON, CONTENT, - ID(0x69F0, "N"), - OBSERVE(0xFE0000), - CONTENT_FORMAT(OPAQUE), - PAYLOAD("\x12\x34\x56\x78")); + const avs_coap_msg_t *n_bytes_response = + COAP_MSG(NON, CONTENT, ID(0x69F0, "N"), OBSERVE(0xFE0000), + CONTENT_FORMAT(OPAQUE), PAYLOAD("\x12\x34\x56\x78")); avs_unit_mocksock_expect_output(mocksocks[0], n_bytes_response->content, n_bytes_response->length); // plaintext - error @@ -1146,23 +1273,21 @@ AVS_UNIT_TEST(notify, multiple_formats) { expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_BYTES(0, "\x12\x34\x56\x78")); - const avs_coap_msg_t *p_bytes_response = COAP_MSG(NON, CONTENT, - ID(0x69F1, "P"), - OBSERVE(0xFE0000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("EjRWeA==")); + const avs_coap_msg_t *p_bytes_response = + COAP_MSG(NON, CONTENT, ID(0x69F1, "P"), OBSERVE(0xFE0000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("EjRWeA==")); avs_unit_mocksock_expect_output(mocksocks[0], p_bytes_response->content, - p_bytes_response->length); + p_bytes_response->length); // TLV expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); expect_read_res_attrs(anjay, &OBJ, 14, 69, 4, &ATTRS); expect_read_res(anjay, &OBJ, 69, 4, ANJAY_MOCK_DM_BYTES(0, "\x12\x34\x56\x78")); - const avs_coap_msg_t *t_bytes_response = COAP_MSG(NON, CONTENT, - ID(0x69F2, "T"), - OBSERVE(0xFE0000), - CONTENT_FORMAT(TLV), - PAYLOAD("\xc4\x04" "\x12\x34\x56\x78")); + const avs_coap_msg_t *t_bytes_response = + COAP_MSG(NON, CONTENT, ID(0x69F2, "T"), OBSERVE(0xFE0000), + CONTENT_FORMAT(TLV), + PAYLOAD("\xc4\x04" + "\x12\x34\x56\x78")); avs_unit_mocksock_expect_output(mocksocks[0], t_bytes_response->content, t_bytes_response->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); @@ -1186,9 +1311,9 @@ static void test_observe_entry(anjay_t *anjay, AVS_UNIT_ASSERT_NOT_NULL(new_entry); memcpy((void *) (intptr_t) (const void *) &new_entry->key, - (&(const anjay_observe_key_t) { - { ssid, conn_type }, oid, iid, rid, AVS_COAP_FORMAT_NONE - }), sizeof(anjay_observe_key_t)); + (&(const anjay_observe_key_t) { + { ssid, conn_type }, oid, iid, rid, AVS_COAP_FORMAT_NONE }), + sizeof(anjay_observe_key_t)); AVS_RBTREE_ELEM(anjay_observe_entry_t) entry = AVS_RBTREE_INSERT(conn->entries, new_entry); if (entry != new_entry) { @@ -1208,7 +1333,8 @@ static anjay_t *create_test_env(void) { test_observe_entry(anjay, 3, ANJAY_CONNECTION_UDP, 2, 7, 3); test_observe_entry(anjay, 3, ANJAY_CONNECTION_UDP, 6, 0, 1); test_observe_entry(anjay, 3, ANJAY_CONNECTION_UDP, 6, 0, 2); - test_observe_entry(anjay, 8, ANJAY_CONNECTION_UDP, 4, ANJAY_IID_INVALID, -1); + test_observe_entry(anjay, 8, ANJAY_CONNECTION_UDP, 4, ANJAY_IID_INVALID, + -1); test_observe_entry(anjay, 8, ANJAY_CONNECTION_UDP, 6, 0, 1); return anjay; } @@ -1242,8 +1368,7 @@ static int mock_notify_entry(anjay_t *anjay, (void) obj; AVS_UNIT_ASSERT_NOT_NULL(MOCK_NOTIFY); mock_notify_entry_value_t *entry = AVS_LIST_DETACH(&MOCK_NOTIFY); - AVS_UNIT_ASSERT_EQUAL(_anjay_observe_key_cmp(&entity->key, &entry->key), - 0); + AVS_UNIT_ASSERT_EQUAL(_anjay_observe_key_cmp(&entity->key, &entry->key), 0); int retval = entry->retval; AVS_LIST_DELETE(&entry); return retval; @@ -1279,35 +1404,47 @@ AVS_UNIT_TEST(notify, notify_changed) { AVS_UNIT_MOCK(notify_entry) = mock_notify_entry; expect_notify_entry(8, 4, ANJAY_IID_INVALID, -1, AVS_COAP_FORMAT_NONE, 0); - AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_notify(anjay, - &(const anjay_observe_key_t) { - { 1, ANJAY_CONNECTION_UNSET }, - 4, 1, 1, AVS_COAP_FORMAT_NONE - }, true)); + AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_notify( + anjay, + &(const anjay_observe_key_t) { { 1, ANJAY_CONNECTION_UNSET }, + 4, + 1, + 1, + AVS_COAP_FORMAT_NONE }, + true)); expect_notify_clear(); - AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_notify(anjay, - &(const anjay_observe_key_t) { - { 3, ANJAY_CONNECTION_UNSET }, - 2, 7, -1, AVS_COAP_FORMAT_NONE - }, true)); + AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_notify( + anjay, + &(const anjay_observe_key_t) { { 3, ANJAY_CONNECTION_UNSET }, + 2, + 7, + -1, + AVS_COAP_FORMAT_NONE }, + true)); expect_notify_clear(); expect_notify_entry(3, 6, 0, 1, AVS_COAP_FORMAT_NONE, 0); expect_notify_entry(8, 6, 0, 1, AVS_COAP_FORMAT_NONE, 0); - AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_notify(anjay, - &(const anjay_observe_key_t) { - { 1, ANJAY_CONNECTION_UNSET }, - 6, 0, 1, AVS_COAP_FORMAT_NONE - }, true)); + AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_notify( + anjay, + &(const anjay_observe_key_t) { { 1, ANJAY_CONNECTION_UNSET }, + 6, + 0, + 1, + AVS_COAP_FORMAT_NONE }, + true)); expect_notify_clear(); expect_notify_entry(3, 6, 0, 2, AVS_COAP_FORMAT_NONE, 0); - AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_notify(anjay, - &(const anjay_observe_key_t) { - { 1, ANJAY_CONNECTION_UNSET }, - 6, 0, 2, AVS_COAP_FORMAT_NONE - }, true)); + AVS_UNIT_ASSERT_SUCCESS(_anjay_observe_notify( + anjay, + &(const anjay_observe_key_t) { { 1, ANJAY_CONNECTION_UNSET }, + 6, + 0, + 2, + AVS_COAP_FORMAT_NONE }, + true)); expect_notify_clear(); expect_notify_entry(1, 2, 3, 1, AVS_COAP_FORMAT_NONE, 0); @@ -1316,11 +1453,17 @@ AVS_UNIT_TEST(notify, notify_changed) { expect_notify_entry(3, 2, 3, -1, AVS_COAP_FORMAT_NONE, -514); expect_notify_entry(3, 2, 3, 3, AVS_COAP_FORMAT_NONE, 0); expect_notify_entry(3, 2, 7, 3, AVS_COAP_FORMAT_NONE, 0); - AVS_UNIT_ASSERT_EQUAL(_anjay_observe_notify(anjay, - &(const anjay_observe_key_t) { - { ANJAY_IID_INVALID, ANJAY_CONNECTION_UNSET }, - 2, ANJAY_IID_INVALID, -1, AVS_COAP_FORMAT_NONE - }, true), -42); + AVS_UNIT_ASSERT_EQUAL( + _anjay_observe_notify( + anjay, + &(const anjay_observe_key_t) { { ANJAY_IID_INVALID, + ANJAY_CONNECTION_UNSET }, + 2, + ANJAY_IID_INVALID, + -1, + AVS_COAP_FORMAT_NONE }, + true), + -42); expect_notify_clear(); destroy_test_env(anjay); @@ -1328,11 +1471,16 @@ AVS_UNIT_TEST(notify, notify_changed) { AVS_UNIT_TEST(notify, storing_when_inactive) { SUCCESS_TEST(14, 34); + anjay_server_connection_t *connection = + _anjay_get_server_connection((const anjay_connection_ref_t) { + .server = anjay->servers->servers, + .conn_type = ANJAY_CONNECTION_UDP + }); + AVS_UNIT_ASSERT_NOT_NULL(connection); // deactivate the first server - avs_net_abstract_socket_t *socket14 = - anjay->servers->servers->data_active.udp_connection.conn_socket_; - anjay->servers->servers->data_active.udp_connection.conn_socket_ = NULL; + avs_net_abstract_socket_t *socket14 = connection->conn_socket_; + connection->conn_socket_ = NULL; _anjay_observe_gc(anjay); assert_observe_size(anjay, 2); @@ -1357,13 +1505,12 @@ AVS_UNIT_TEST(notify, storing_when_inactive) { _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, 0, ANJAY_MOCK_DM_STRING(0, "Len")); - const avs_coap_msg_t *notify_response = COAP_MSG(NON, CONTENT, ID(0x69ED), - OBSERVE(0xF48000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Len")); + const avs_coap_msg_t *notify_response = + COAP_MSG(NON, CONTENT, ID(0x69ED), OBSERVE(0xF48000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Len")); avs_unit_mocksock_expect_output(mocksocks[1], notify_response->content, - - notify_response->length); + + notify_response->length); DM_TEST_EXPECT_READ_NULL_ATTRS(34, 69, 4); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); @@ -1388,17 +1535,16 @@ AVS_UNIT_TEST(notify, storing_when_inactive) { _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, 0, ANJAY_MOCK_DM_STRING(0, "Luka")); - const avs_coap_msg_t *notify_response2 = COAP_MSG(NON, CONTENT, ID(0x69EE), - OBSERVE(0xF50000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Luka")); + const avs_coap_msg_t *notify_response2 = + COAP_MSG(NON, CONTENT, ID(0x69EE), OBSERVE(0xF50000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Luka")); avs_unit_mocksock_expect_output(mocksocks[1], notify_response2->content, notify_response2->length); DM_TEST_EXPECT_READ_NULL_ATTRS(34, 69, 4); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); // reactivate the server - anjay->servers->servers->data_active.udp_connection.conn_socket_ = socket14; + connection->conn_socket_ = socket14; _anjay_observe_gc(anjay); assert_observe_size(anjay, 2); anjay->current_connection.server = anjay->servers->servers; @@ -1407,16 +1553,14 @@ AVS_UNIT_TEST(notify, storing_when_inactive) { memset(&anjay->current_connection, 0, sizeof(anjay->current_connection)); expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); - const avs_coap_msg_t *notify_response3 = COAP_MSG(NON, CONTENT, ID(0x69EF), - OBSERVE(0xF50000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Rin")); + const avs_coap_msg_t *notify_response3 = + COAP_MSG(NON, CONTENT, ID(0x69EF), OBSERVE(0xF50000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Rin")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response3->content, notify_response3->length); - const avs_coap_msg_t *notify_response4 = COAP_MSG(NON, CONTENT, ID(0x69F0), - OBSERVE(0xF50000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Miku")); + const avs_coap_msg_t *notify_response4 = + COAP_MSG(NON, CONTENT, ID(0x69F0), OBSERVE(0xF50000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Miku")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response4->content, notify_response4->length); DM_TEST_EXPECT_READ_NULL_ATTRS(14, 69, 4); @@ -1427,11 +1571,16 @@ AVS_UNIT_TEST(notify, storing_when_inactive) { AVS_UNIT_TEST(notify, no_storing_when_disabled) { SUCCESS_TEST(14, 34); + anjay_server_connection_t *connection = + _anjay_get_server_connection((const anjay_connection_ref_t) { + .server = anjay->servers->servers, + .conn_type = ANJAY_CONNECTION_UDP + }); + AVS_UNIT_ASSERT_NOT_NULL(connection); // deactivate the first server - avs_net_abstract_socket_t *socket14 = - anjay->servers->servers->data_active.udp_connection.conn_socket_; - anjay->servers->servers->data_active.udp_connection.conn_socket_ = NULL; + avs_net_abstract_socket_t *socket14 = connection->conn_socket_; + connection->conn_socket_ = NULL; _anjay_observe_gc(anjay); assert_observe_size(anjay, 2); @@ -1450,10 +1599,9 @@ AVS_UNIT_TEST(notify, no_storing_when_disabled) { _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, 0, ANJAY_MOCK_DM_STRING(0, "Ia")); - const avs_coap_msg_t *notify_response = COAP_MSG(NON, CONTENT, ID(0x69ED), - OBSERVE(0xF48000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Ia")); + const avs_coap_msg_t *notify_response = + COAP_MSG(NON, CONTENT, ID(0x69ED), OBSERVE(0xF48000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Ia")); avs_unit_mocksock_expect_output(mocksocks[1], notify_response->content, notify_response->length); DM_TEST_EXPECT_READ_NULL_ATTRS(34, 69, 4); @@ -1474,17 +1622,16 @@ AVS_UNIT_TEST(notify, no_storing_when_disabled) { _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, 0, ANJAY_MOCK_DM_STRING(0, "Gumi")); - const avs_coap_msg_t *notify_response2 = COAP_MSG(NON, CONTENT, ID(0x69EE), - OBSERVE(0xF50000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Gumi")); + const avs_coap_msg_t *notify_response2 = + COAP_MSG(NON, CONTENT, ID(0x69EE), OBSERVE(0xF50000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Gumi")); avs_unit_mocksock_expect_output(mocksocks[1], notify_response2->content, notify_response2->length); DM_TEST_EXPECT_READ_NULL_ATTRS(34, 69, 4); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); // reactivate the server - anjay->servers->servers->data_active.udp_connection.conn_socket_ = socket14; + connection->conn_socket_ = socket14; _anjay_observe_gc(anjay); assert_observe_size(anjay, 2); anjay->current_connection.server = anjay->servers->servers; @@ -1548,16 +1695,14 @@ AVS_UNIT_TEST(notify, storing_on_send_error) { // now the notifications shall arrive expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); - const avs_coap_msg_t *notify_response = COAP_MSG(NON, CONTENT, ID(0x69EF), - OBSERVE(0xF50000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Meiko")); + const avs_coap_msg_t *notify_response = + COAP_MSG(NON, CONTENT, ID(0x69EF), OBSERVE(0xF50000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Meiko")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response->content, notify_response->length); - const avs_coap_msg_t *notify_response2 = COAP_MSG(NON, CONTENT, ID(0x69F0), - OBSERVE(0xF50000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Kaito")); + const avs_coap_msg_t *notify_response2 = + COAP_MSG(NON, CONTENT, ID(0x69F0), OBSERVE(0xF50000), + CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Kaito")); avs_unit_mocksock_expect_output(mocksocks[0], notify_response2->content, notify_response2->length); DM_TEST_EXPECT_READ_NULL_ATTRS(14, 69, 4); @@ -1650,9 +1795,8 @@ AVS_UNIT_TEST(notify, storing_of_errors) { // sending is now scheduled, should receive the previous error expect_read_notif_storing(anjay, &FAKE_SERVER, 14, true); - const avs_coap_msg_t *con_notify_response = COAP_MSG(CON, - INTERNAL_SERVER_ERROR, - ID(0x69EE)); + const avs_coap_msg_t *con_notify_response = + COAP_MSG(CON, INTERNAL_SERVER_ERROR, ID(0x69EE)); avs_unit_mocksock_expect_output(mocksocks[0], con_notify_response->content, con_notify_response->length); const avs_coap_msg_t *con_ack = COAP_MSG(ACK, EMPTY, ID(0x69EE)); @@ -1709,7 +1853,7 @@ AVS_UNIT_TEST(notify, reconnect) { // mocking reconnect is rather hard, so let's just check it's scheduled AVS_UNIT_ASSERT_TRUE(anjay->sched->entries - == anjay->servers->servers->next_action_handle); + == anjay->servers->servers->next_action_handle); AVS_UNIT_ASSERT_EQUAL(*(anjay_ssid_t *) &anjay->sched->entries->clb_data, 14); _anjay_sched_del(anjay->sched, diff --git a/src/observe/test/observe_mock.h b/src/observe/test/observe_mock.h index 53807030b..5d96a1a7d 100644 --- a/src/observe/test/observe_mock.h +++ b/src/observe/test/observe_mock.h @@ -20,7 +20,8 @@ #include AVS_UNIT_MOCK_CREATE(_anjay_dm_find_object_by_oid) -#define _anjay_dm_find_object_by_oid(...) AVS_UNIT_MOCK_WRAPPER(_anjay_dm_find_object_by_oid)(__VA_ARGS__) +#define _anjay_dm_find_object_by_oid(...) \ + AVS_UNIT_MOCK_WRAPPER(_anjay_dm_find_object_by_oid)(__VA_ARGS__) AVS_UNIT_MOCK_CREATE(notify_entry) #define notify_entry(...) AVS_UNIT_MOCK_WRAPPER(notify_entry)(__VA_ARGS__) diff --git a/src/sched.c b/src/sched.c index ec9c5a608..e68e975bb 100644 --- a/src/sched.c +++ b/src/sched.c @@ -30,8 +30,8 @@ #define ANJAY_SCHED_C #include "anjay_core.h" -#include "utils_core.h" #include "sched_internal.h" +#include "utils_core.h" #define sched_log(...) _anjay_log(anjay_sched, __VA_ARGS__) @@ -42,7 +42,8 @@ anjay_sched_t *_anjay_sched_get(anjay_t *anjay) { } anjay_sched_t *_anjay_sched_new(anjay_t *anjay) { - anjay_sched_t *sched = (anjay_sched_t *) avs_calloc(1, sizeof(anjay_sched_t)); + anjay_sched_t *sched = + (anjay_sched_t *) avs_calloc(1, sizeof(anjay_sched_t)); if (sched) { sched->anjay = anjay; } @@ -59,10 +60,9 @@ static anjay_sched_entry_t *fetch_task(anjay_sched_t *sched, } } -static anjay_sched_handle_t -sched_delayed(anjay_sched_t *sched, - avs_time_duration_t delay, - AVS_LIST(anjay_sched_entry_t) entry); +static anjay_sched_handle_t sched_delayed(anjay_sched_t *sched, + avs_time_duration_t delay, + AVS_LIST(anjay_sched_entry_t) entry); static void execute_task(anjay_sched_t *sched, AVS_LIST(anjay_sched_entry_t) entry) { @@ -97,10 +97,11 @@ ssize_t _anjay_sched_run(anjay_sched_t *sched) { avs_time_duration_t delay = AVS_TIME_DURATION_ZERO; _anjay_sched_time_to_next(sched, &delay); - sched_log(TRACE, "%lu scheduled tasks remain; next after " - "%" PRId64 ".%09" PRId32, - (unsigned long)AVS_LIST_SIZE(sched->entries), - delay.seconds, delay.nanoseconds); + sched_log(TRACE, + "%lu scheduled tasks remain; next after " + "%" PRId64 ".%09" PRId32, + (unsigned long) AVS_LIST_SIZE(sched->entries), delay.seconds, + delay.nanoseconds); return tasks_executed; } @@ -122,9 +123,8 @@ void _anjay_sched_delete(anjay_sched_t **sched_ptr) { *sched_ptr = NULL; } -static anjay_sched_handle_t -insert_entry(anjay_sched_t *sched, - AVS_LIST(anjay_sched_entry_t) entry) { +static anjay_sched_handle_t insert_entry(anjay_sched_t *sched, + AVS_LIST(anjay_sched_entry_t) entry) { anjay_sched_entry_t **entry_ptr = NULL; if (!sched || sched->shut_down) { @@ -139,18 +139,17 @@ insert_entry(anjay_sched_t *sched, } AVS_LIST_INSERT(entry_ptr, entry); - sched_log(TRACE, "%p inserted; %lu tasks scheduled", - (void*)entry, (unsigned long)AVS_LIST_SIZE(sched->entries)); + sched_log(TRACE, "%p inserted; %lu tasks scheduled", (void *) entry, + (unsigned long) AVS_LIST_SIZE(sched->entries)); return entry; } -static AVS_LIST(anjay_sched_entry_t) -create_entry(anjay_sched_clb_t clb, - const void *clb_data, - size_t clb_data_size) { - AVS_LIST(anjay_sched_entry_t) entry = (anjay_sched_entry_t *) - AVS_LIST_NEW_BUFFER(offsetof(anjay_sched_entry_t, clb_data) - + clb_data_size); +static AVS_LIST(anjay_sched_entry_t) create_entry(anjay_sched_clb_t clb, + const void *clb_data, + size_t clb_data_size) { + AVS_LIST(anjay_sched_entry_t) entry = + (anjay_sched_entry_t *) AVS_LIST_NEW_BUFFER( + offsetof(anjay_sched_entry_t, clb_data) + clb_data_size); if (!entry) { sched_log(ERROR, "Could not allocate scheduler task"); @@ -165,10 +164,9 @@ create_entry(anjay_sched_clb_t clb, return entry; } -static anjay_sched_handle_t -sched_delayed(anjay_sched_t *sched, - avs_time_duration_t delay, - AVS_LIST(anjay_sched_entry_t) entry) { +static anjay_sched_handle_t sched_delayed(anjay_sched_t *sched, + avs_time_duration_t delay, + AVS_LIST(anjay_sched_entry_t) entry) { avs_time_monotonic_t sched_time = avs_time_monotonic_now(); sched_log(TRACE, "current time %" PRId64 ".%09" PRId32, sched_time.since_monotonic_epoch.seconds, @@ -178,11 +176,11 @@ sched_delayed(anjay_sched_t *sched, sched_time = avs_time_monotonic_add(sched_time, delay); } sched_log(TRACE, - "job scheduled at %" PRId64 ".%09" PRId32 - " (+%" PRId64 ".%09" PRId32 ")", + "job scheduled at %" PRId64 ".%09" PRId32 " (+%" PRId64 + ".%09" PRId32 ")", sched_time.since_monotonic_epoch.seconds, - sched_time.since_monotonic_epoch.nanoseconds, - delay.seconds, delay.nanoseconds); + sched_time.since_monotonic_epoch.nanoseconds, delay.seconds, + delay.nanoseconds); entry->when = sched_time; return insert_entry(sched, entry); @@ -222,9 +220,8 @@ static anjay_sched_entry_t **find_task_entry_ptr(anjay_sched_t *sched, anjay_sched_handle_t *handle) { // IAR compiler does not support typeof, so AVS_LIST_FIND_PTR // returns void**, which is not implicitly-convertible - return (AVS_LIST(anjay_sched_entry_t) *) - AVS_LIST_FIND_PTR(&sched->entries, - *((anjay_sched_entry_t **) handle)); + return (AVS_LIST(anjay_sched_entry_t) *) AVS_LIST_FIND_PTR( + &sched->entries, *((anjay_sched_entry_t **) handle)); } int _anjay_sched_del(anjay_sched_t *sched, anjay_sched_handle_t *handle) { @@ -269,5 +266,5 @@ int _anjay_sched_time_to_next(anjay_sched_t *sched, } #ifdef ANJAY_TEST -#include "test/sched.c" +# include "test/sched.c" #endif // ANJAY_TEST diff --git a/src/sched_internal.h b/src/sched_internal.h index 2e032f628..571645476 100644 --- a/src/sched_internal.h +++ b/src/sched_internal.h @@ -20,7 +20,7 @@ VISIBILITY_PRIVATE_HEADER_BEGIN #if !(defined(ANJAY_SCHED_C) || defined(ANJAY_TEST)) -#error "sched_internal.h is not meant to be included from outside sched.c" +# error "sched_internal.h is not meant to be included from outside sched.c" #endif typedef struct { @@ -39,4 +39,3 @@ struct anjay_sched_struct { VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_SCHED_INTERNAL_H */ - diff --git a/src/servers.h b/src/servers.h index aad8ad9b8..7d79a9fc1 100644 --- a/src/servers.h +++ b/src/servers.h @@ -22,8 +22,10 @@ #include #include -#include "utils_core.h" +#include + #include "coap/coap_stream.h" +#include "utils_core.h" VISIBILITY_PRIVATE_HEADER_BEGIN @@ -64,13 +66,13 @@ VISIBILITY_PRIVATE_HEADER_BEGIN // copied from avs_commons/git/http/src/headers.h // see description there for rationale; TODO: move this to public Commons API? // note that this _includes_ the terminating null byte -#define ANJAY_UINT_STR_BUF_SIZE(type) ((12*sizeof(type))/5 + 2) +#define ANJAY_UINT_STR_BUF_SIZE(type) ((12 * sizeof(type)) / 5 + 2) // 6.2.2 Object Version format: // "The Object Version of an Object is composed of 2 digits separated by a dot" // However, we're a bit lenient to support proper numbers and not just digits. #define ANJAY_DM_OBJECT_VERSION_BUF_LENGTH \ - (2 * ANJAY_UINT_STR_BUF_SIZE(unsigned)) + (2 * ANJAY_UINT_STR_BUF_SIZE(unsigned)) typedef struct { anjay_oid_t oid; @@ -114,6 +116,29 @@ typedef struct { anjay_connection_type_t conn_type; } anjay_connection_ref_t; +/** + * Token that changes to a new unique value every time the CoAP endpoint + * association (i.e., DTLS session or raw UDP socket) every time it has been + * established anew. + * + * It is currently implemented as a monotonic timestamp because it's trivial to + * generate such unique value that way as long as it is never persisted. + */ +typedef struct { + avs_time_monotonic_t value; +} anjay_conn_session_token_t; + +static inline void +_anjay_conn_session_token_reset(anjay_conn_session_token_t *out) { + out->value = avs_time_monotonic_now(); +} + +static inline bool +_anjay_conn_session_tokens_equal(anjay_conn_session_token_t left, + anjay_conn_session_token_t right) { + return avs_time_monotonic_equal(left.value, right.value); +} + //////////////////////////////////////////////////////////////////////////////// // METHODS ON THE WHOLE SERVERS SUBSYSTEM ////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// @@ -378,6 +403,11 @@ const anjay_url_t *_anjay_server_uri(anjay_server_info_t *server); * yet be established, or SMS for US or UQS if there is connectivity on the SMS * channel, but no connectivity on the UDP channel. * + * Note that this function automatically recalculates the primary connection + * type if it's not set. Basically the lowest-numbered anjay_connection_type_t + * value that has a valid connected socket associated with it is assigned to + * primary_conn_type. + * * Called from bootstrap_core.c :: request_bootstrap() and * register.c :: bind_server_stream(), to determine which connection to use for * sending the aforementioned messages. @@ -386,15 +416,14 @@ anjay_connection_type_t _anjay_server_primary_conn_type(anjay_server_info_t *server); /** - * Recalculates the "primary" connection type. Basically the lowest-numbered - * anjay_connection_type_t value that has a valid connected socket associated - * with it is assigned to primary_conn_type. + * Gets the token uniquely identifying the CoAP endpoint association (i.e., DTLS + * session or raw UDP socket) of the server's primary connection. * - * This is called from request_bootstrap() and - * ensure_valid_registration_with_ctx(), to determine on which connection to - * send either Request Bootstrap or Register. + * It is used to determine whether reconnect operation re-used the previous + * association or created a new one. */ -int _anjay_server_setup_primary_connection(anjay_server_info_t *server); +anjay_conn_session_token_t +_anjay_server_primary_session_token(anjay_server_info_t *server); /** * Gets the information about current registration status of the server. These @@ -471,9 +500,8 @@ _anjay_connection_current_mode(anjay_connection_ref_t ref); * connection is in queue mode, it schedules closing of the socket (suspending * the connection) after MAX_TRANSMIT_WAIT passes. */ -void -_anjay_connection_schedule_queue_mode_close(anjay_t *anjay, - anjay_connection_ref_t ref); +void _anjay_connection_schedule_queue_mode_close(anjay_t *anjay, + anjay_connection_ref_t ref); /** * Returns the socket associated with a given connection, if it exists and is in @@ -494,9 +522,7 @@ _anjay_connection_get_online_socket(anjay_connection_ref_t ref); * * It is called from observe_core.c :: ensure_conn_online(). */ -int _anjay_connection_bring_online(anjay_t *anjay, - anjay_connection_ref_t ref, - bool *out_session_resumed); +int _anjay_connection_bring_online(anjay_t *anjay, anjay_connection_ref_t ref); /** * Suspends the specified connection (or all connections in the server if diff --git a/src/servers/activate.c b/src/servers/activate.c index 6ebd8431d..fef1cf7d4 100644 --- a/src/servers/activate.c +++ b/src/servers/activate.c @@ -16,18 +16,18 @@ #include -#include #include +#include #include "../dm/query.h" #include "../servers_utils.h" #define ANJAY_SERVERS_INTERNALS -#include "servers_internal.h" #include "activate.h" -#include "connection_info.h" #include "register_internal.h" +#include "server_connections.h" +#include "servers_internal.h" VISIBILITY_SOURCE_BEGIN @@ -89,7 +89,7 @@ initialize_active_server(anjay_t *anjay, anjay_server_info_t *server) { int refresh_result; if (_anjay_find_security_iid(anjay, server->ssid, &security_iid)) { anjay_log(ERROR, "could not find server Security IID"); - } else if (!read_server_uri(anjay, security_iid, &server->data_active.uri)) { + } else if (!read_server_uri(anjay, security_iid, &server->uri)) { if ((refresh_result = _anjay_active_server_refresh(anjay, server))) { anjay_log(TRACE, "could not initialize sockets for SSID %u", server->ssid); @@ -129,9 +129,9 @@ initialize_active_server(anjay_t *anjay, anjay_server_info_t *server) { } if (result == IAS_SUCCESS) { - server->data_inactive.reactivate_time = AVS_TIME_REAL_INVALID; - server->data_inactive.reactivate_failed = false; - server->data_inactive.num_icmp_failures = 0; + server->reactivate_time = AVS_TIME_REAL_INVALID; + server->reactivate_failed = false; + server->num_icmp_failures = 0; } return result; } @@ -142,9 +142,8 @@ bool _anjay_can_retry_with_normal_server(anjay_t *anjay) { if (_anjay_server_active(it) || it->ssid == ANJAY_SSID_BOOTSTRAP) { continue; } - if (!it->data_inactive.reactivate_failed - || it->data_inactive.num_icmp_failures - < anjay->max_icmp_failures) { + if (!it->reactivate_failed + || it->num_icmp_failures < anjay->max_icmp_failures) { // there is hope for a successful non-bootstrap connection return true; } @@ -169,8 +168,8 @@ bool _anjay_should_retry_bootstrap(anjay_t *anjay) { } } return bootstrap_server_exists - && !_anjay_can_retry_with_normal_server(anjay); -#else // WITH_BOOTSTRAP + && !_anjay_can_retry_with_normal_server(anjay); +#else // WITH_BOOTSTRAP (void) anjay; return false; #endif // WITH_BOOTSTRAP @@ -188,8 +187,7 @@ bool anjay_all_connections_failed(anjay_t *anjay) { AVS_LIST(anjay_server_info_t) it; AVS_LIST_FOREACH(it, anjay->servers->servers) { if (_anjay_server_active(it) - || it->data_inactive.num_icmp_failures - < anjay->max_icmp_failures) { + || it->num_icmp_failures < anjay->max_icmp_failures) { return false; } } @@ -214,22 +212,20 @@ static void activate_server_job(anjay_t *anjay, const void *ssid_ptr) { } _anjay_server_clean_active_data(anjay, *server_ptr); - (*server_ptr)->data_inactive.reactivate_failed = true; - uint32_t *num_icmp_failures = - &(*server_ptr)->data_inactive.num_icmp_failures; + (*server_ptr)->reactivate_failed = true; if (registration_result == IAS_CONNECTION_REFUSED) { - ++*num_icmp_failures; + ++(*server_ptr)->num_icmp_failures; } else if (registration_result == IAS_FORBIDDEN - || registration_result == IAS_CONNECTION_ERROR) { - *num_icmp_failures = anjay->max_icmp_failures; + || registration_result == IAS_CONNECTION_ERROR) { + (*server_ptr)->num_icmp_failures = anjay->max_icmp_failures; } - if (*num_icmp_failures < anjay->max_icmp_failures) { + if ((*server_ptr)->num_icmp_failures < anjay->max_icmp_failures) { // We had a failure with either a bootstrap or a non-bootstrap server, // retry till it's possible. - if (_anjay_servers_schedule_next_retryable( - anjay->sched, *server_ptr, activate_server_job, ssid)) { + if (_anjay_servers_schedule_next_retryable(anjay->sched, *server_ptr, + activate_server_job, ssid)) { anjay_log(ERROR, "could not reschedule reactivate job for server SSID %u", ssid); @@ -254,7 +250,7 @@ static void activate_server_job(anjay_t *anjay, const void *ssid_ptr) { ssid); } // kill this job. - (*server_ptr)->data_inactive.reactivate_time = AVS_TIME_REAL_INVALID; + (*server_ptr)->reactivate_time = AVS_TIME_REAL_INVALID; } int _anjay_server_sched_activate(anjay_t *anjay, @@ -262,14 +258,14 @@ int _anjay_server_sched_activate(anjay_t *anjay, avs_time_duration_t reactivate_delay) { // start the backoff procedure from the beginning assert(!_anjay_server_active(server)); - server->data_inactive.reactivate_time = + server->reactivate_time = avs_time_real_add(avs_time_real_now(), reactivate_delay); - server->data_inactive.reactivate_failed = false; - server->data_inactive.num_icmp_failures = 0; + server->reactivate_failed = false; + server->num_icmp_failures = 0; _anjay_sched_del(anjay->sched, &server->next_action_handle); if (_anjay_servers_schedule_first_retryable( - anjay->sched, server, reactivate_delay, - activate_server_job, server->ssid)) { + anjay->sched, server, reactivate_delay, activate_server_job, + server->ssid)) { anjay_log(TRACE, "could not schedule reactivate job for server SSID %u", server->ssid); return -1; @@ -282,14 +278,12 @@ int _anjay_servers_sched_reactivate_all_given_up(anjay_t *anjay) { AVS_LIST(anjay_server_info_t) it; AVS_LIST_FOREACH(it, anjay->servers->servers) { - if (_anjay_server_active(it) - || !it->data_inactive.reactivate_failed - || it->data_inactive.num_icmp_failures - < anjay->max_icmp_failures) { + if (_anjay_server_active(it) || !it->reactivate_failed + || it->num_icmp_failures < anjay->max_icmp_failures) { continue; } - int partial = _anjay_server_sched_activate(anjay, it, - AVS_TIME_DURATION_ZERO); + int partial = + _anjay_server_sched_activate(anjay, it, AVS_TIME_DURATION_ZERO); if (!result) { result = partial; } @@ -330,9 +324,7 @@ int _anjay_server_deactivate(anjay_t *anjay, _anjay_server_deregister(anjay, *server_ptr); } _anjay_server_clean_active_data(anjay, *server_ptr); - // we don't do the following in _anjay_server_cleanup() so that conn_type - // can be reused after restoring from persistence in activate_server_job() - (*server_ptr)->data_active.primary_conn_type = ANJAY_CONNECTION_UNSET; + (*server_ptr)->registration_info.expire_time = AVS_TIME_REAL_INVALID; if (avs_time_duration_valid(reactivate_delay) && _anjay_server_sched_activate(anjay, *server_ptr, reactivate_delay)) { @@ -354,7 +346,6 @@ _anjay_servers_create_inactive(anjay_ssid_t ssid) { } new_server->ssid = ssid; - new_server->data_active.primary_conn_type = ANJAY_CONNECTION_UNSET; - new_server->data_inactive.reactivate_time = AVS_TIME_REAL_INVALID; + new_server->reactivate_time = AVS_TIME_REAL_INVALID; return new_server; } diff --git a/src/servers/activate.h b/src/servers/activate.h index a49abe54a..3e6a8fceb 100644 --- a/src/servers/activate.h +++ b/src/servers/activate.h @@ -23,7 +23,7 @@ #include "../utils_core.h" #ifndef ANJAY_SERVERS_INTERNALS -#error "Headers from servers/ are not meant to be included from outside" +# error "Headers from servers/ are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN diff --git a/src/servers/connection_info.c b/src/servers/connection_info.c deleted file mode 100644 index 98883f8c9..000000000 --- a/src/servers/connection_info.c +++ /dev/null @@ -1,496 +0,0 @@ -/* - * Copyright 2017-2018 AVSystem - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include - -#include -#include - -#include "../servers_utils.h" -#include "../utils_core.h" -#include "../dm/query.h" - -#define ANJAY_SERVERS_CONNECTION_SOURCE -#define ANJAY_SERVERS_INTERNALS - -#include "connection_info.h" -#include "connections_internal.h" -#include "reload.h" -#include "servers_internal.h" - -VISIBILITY_SOURCE_BEGIN - -avs_net_abstract_socket_t *_anjay_connection_internal_get_socket( - const anjay_server_connection_t *connection) { - return connection->conn_socket_; -} - -void -_anjay_connection_internal_clean_socket(anjay_server_connection_t *connection) { - avs_net_socket_cleanup(&connection->conn_socket_); -} - -static int read_binding_mode(anjay_t *anjay, - anjay_ssid_t ssid, - anjay_binding_mode_t *out_binding_mode) { - anjay_uri_path_t path = - MAKE_RESOURCE_PATH(ANJAY_DM_OID_SERVER, ANJAY_IID_INVALID, - ANJAY_DM_RID_SERVER_BINDING); - - if (_anjay_find_server_iid(anjay, ssid, &path.iid) - || _anjay_dm_res_read_string(anjay, &path, *out_binding_mode, - sizeof(*out_binding_mode))) { - anjay_log(WARNING, "could not read binding mode for LwM2M server %u", - ssid); - return -1; - } - if (!anjay_binding_mode_valid(*out_binding_mode)) { - anjay_log(WARNING, "invalid binding mode \"%s\" for LwM2M server %u", - *out_binding_mode, ssid); - return -1; - } - return 0; -} - -anjay_server_connection_mode_t -_anjay_connection_current_mode(anjay_connection_ref_t ref) { - anjay_server_connection_t *connection = _anjay_get_server_connection(ref); - if (connection && _anjay_connection_internal_get_socket(connection)) { - return connection->mode; - } else { - return ANJAY_CONNECTION_DISABLED; - } -} - -bool _anjay_connection_is_online(anjay_server_connection_t *connection) { - avs_net_abstract_socket_t *socket = - _anjay_connection_internal_get_socket(connection); - if (!socket) { - return false; - } - avs_net_socket_opt_value_t opt; - if (avs_net_socket_get_opt(socket, AVS_NET_SOCKET_OPT_STATE, &opt)) { - anjay_log(ERROR, "Could not get socket state"); - return false; - } - return opt.state == AVS_NET_SOCKET_STATE_CONNECTED; -} - -static int recreate_socket(anjay_t *anjay, - const anjay_connection_type_definition_t *def, - anjay_server_connection_t *connection, - anjay_connection_info_t *inout_info, - bool *out_session_resumed) { - anjay_server_dtls_keys_t dtls_keys; - memset(&dtls_keys, 0, sizeof(dtls_keys)); - - // At this point, inout_info has "global" settings filled, - // but transport-specific (i.e. UDP or SMS) fields are not - if (def->get_connection_info(anjay, inout_info, &dtls_keys)) { - anjay_log(DEBUG, "could not get %s connection info for server /%u/%u", - def->name, ANJAY_DM_OID_SECURITY, inout_info->security_iid); - return -1; - } - _anjay_connection_internal_clean_socket(connection); - - // Socket configuration is slightly different between UDP and SMS - // connections. That's why we do the common configuration here... - avs_net_ssl_configuration_t socket_config; - memset(&socket_config, 0, sizeof(socket_config)); - socket_config.version = anjay->dtls_version; - socket_config.session_resumption_buffer = - connection->nontransient_state.dtls_session_buffer; - socket_config.session_resumption_buffer_size = - sizeof(connection->nontransient_state.dtls_session_buffer); - int result = def->get_net_security_info(&socket_config.security, - inout_info, &dtls_keys); - // ...and pass it as an in/out argument to create_connected_socket() so that - // it can do any protocol-specific modifications. - if (!result) { - result = def->create_connected_socket(anjay, connection, &socket_config, - inout_info); - } - if (!result) { - avs_net_socket_opt_value_t session_resumed; - if (avs_net_socket_get_opt(connection->conn_socket_, - AVS_NET_SOCKET_OPT_SESSION_RESUMED, - &session_resumed)) { - *out_session_resumed = false; - } else { - *out_session_resumed = session_resumed.flag; - } - } else { - avs_net_abstract_socket_t *sock = connection->conn_socket_; - if (sock) { - avs_net_socket_close(sock); - } - } - return result; -} - -typedef enum { - RESULT_ERROR, - RESULT_DISABLED, - RESULT_RESUMED, - RESULT_NEW_CONNECTION -} refresh_connection_result_t; - -static refresh_connection_result_t -ensure_socket_connected(anjay_t *anjay, - const anjay_connection_type_definition_t *def, - anjay_server_connection_t *connection, - anjay_connection_info_t *inout_info, - int *out_socket_errno) { - bool session_resumed = false; - avs_net_abstract_socket_t *existing_socket = - _anjay_connection_internal_get_socket(connection); - - *out_socket_errno = 0; - - if (existing_socket == NULL) { - int result = recreate_socket( - anjay, def, connection, inout_info, &session_resumed); - if (result) { - *out_socket_errno = -result; - return RESULT_ERROR; - } - } else { - if (_anjay_connection_is_online(connection)) { - session_resumed = true; - } else { - int result = _anjay_connection_internal_bring_online( - anjay, connection, &session_resumed); - if (result) { - *out_socket_errno = -result; - return RESULT_ERROR; - } - } - } - return session_resumed ? RESULT_RESUMED : RESULT_NEW_CONNECTION; -} - -int _anjay_connection_init_psk_security(avs_net_security_info_t *security, - const anjay_server_dtls_keys_t *keys) { - *security = avs_net_security_info_from_psk( - (avs_net_psk_info_t){ - .psk = keys->secret_key, - .psk_size = keys->secret_key_size, - .identity = keys->pk_or_identity, - .identity_size = keys->pk_or_identity_size - }); - return 0; -} - -avs_net_af_t _anjay_socket_af_from_preferred_endpoint( - const avs_net_resolved_endpoint_t *endpoint) { - /* - * Whenever the socket is bound by connect(), the address family is set to - * match the remote address. If the socket is bound by a bind() call with - * NULL local_addr argument, the address family falls back to the original - * socket preference - by default, AF_UNSPEC. This causes avs_net to attempt - * to bind to [::]:$PORT, even though the remote host may be an IPv4 - * address. This generally works, because IPv4-mapped IPv6 addresses are a - * thing. - * - * On FreeBSD though, IPv4-mapped IPv6 are disabled by default (see: - * "Interaction between IPv4/v6 sockets" at - * https://www.freebsd.org/cgi/man.cgi?query=inet6&sektion=4), which - * effectively breaks all connect() calls after re-binding to a recently - * used port. - * - * To avoid that, we need to provide a local wildcard address appropriate - * for the family used by the remote host. This function determines which - * address family to use; it is then converted into a local address by - * bind_socket() in utils_core.c. - */ - char remote_preferred_host[ - sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")]; - if (!avs_net_resolved_endpoint_get_host( - endpoint, remote_preferred_host, sizeof(remote_preferred_host))) { - if (strchr(remote_preferred_host, ':') != NULL) { - return AVS_NET_AF_INET6; - } else if (strchr(remote_preferred_host, '.') != NULL) { - return AVS_NET_AF_INET4; - } - } - return AVS_NET_AF_UNSPEC; -} - -static const anjay_connection_type_definition_t * -get_connection_type_def(anjay_connection_type_t type) { - switch (type) { - case ANJAY_CONNECTION_UDP: - return &ANJAY_CONNECTION_DEF_UDP; - default: - AVS_UNREACHABLE("unknown connection type"); - return NULL; - } -} - -static refresh_connection_result_t -refresh_connection(anjay_t *anjay, - anjay_connection_ref_t ref, - anjay_connection_info_t *inout_info, - int *out_socket_errno) { - anjay_server_connection_t *out_connection = - _anjay_get_server_connection(ref); - assert(out_connection); - const anjay_connection_type_definition_t *def = - get_connection_type_def(ref.conn_type); - assert(def); - refresh_connection_result_t result = RESULT_DISABLED; - - *out_socket_errno = 0; - - out_connection->mode = _anjay_get_connection_mode(inout_info->binding_mode, - ref.conn_type); - if (out_connection->mode == ANJAY_CONNECTION_DISABLED) { - _anjay_connection_internal_clean_socket(out_connection); - } else { - result = ensure_socket_connected(anjay, def, out_connection, inout_info, - out_socket_errno); - } - return result; -} - -static int get_common_connection_info(anjay_t *anjay, - anjay_server_info_t *server, - anjay_connection_info_t *out_info) { - if (_anjay_find_security_iid(anjay, server->ssid, - &out_info->security_iid)) { - anjay_log(ERROR, "could not find server Security IID"); - return -1; - } - - out_info->uri = &server->data_active.uri; - - if (server->ssid == ANJAY_SSID_BOOTSTRAP) { - int result = avs_simple_snprintf( - out_info->binding_mode, sizeof(out_info->binding_mode), - "%s", _anjay_sms_router(anjay) ? "US" : "U"); - return AVS_MIN(result, 0); - } else if (read_binding_mode(anjay, server->ssid, - &out_info->binding_mode)) { - return -1; - } - return 0; -} - -static bool is_connected(refresh_connection_result_t result) { - return result == RESULT_RESUMED || result == RESULT_NEW_CONNECTION; -} - -int _anjay_active_server_refresh(anjay_t *anjay, anjay_server_info_t *server) { - anjay_log(TRACE, "refreshing SSID %u", server->ssid); - - anjay_connection_info_t server_info; - memset(&server_info, 0, sizeof(server_info)); - if (get_common_connection_info(anjay, server, &server_info)) { - anjay_log(DEBUG, "could not get connection info for SSID %u", - server->ssid); - return -1; - } - - refresh_connection_result_t udp_result = RESULT_DISABLED; - int udp_errno = 0; - refresh_connection_result_t sms_result = RESULT_DISABLED; - int sms_errno = 0; - udp_result = refresh_connection(anjay, - (anjay_connection_ref_t) { - .server = server, - .conn_type = ANJAY_CONNECTION_UDP - }, &server_info, &udp_errno); - (void) sms_errno; - - if (!is_connected(udp_result) && !is_connected(sms_result)) { - return udp_errno ? udp_errno : -1; - } - - - if ((server->data_active.primary_conn_type == ANJAY_CONNECTION_UDP - && udp_result == RESULT_NEW_CONNECTION) - || (server->data_active.primary_conn_type == ANJAY_CONNECTION_SMS - && sms_result == RESULT_NEW_CONNECTION)) { - // mark that the primary connection is no longer valid; - // forces re-register - server->data_active.primary_conn_type = ANJAY_CONNECTION_UNSET; - } - - return udp_errno; -} - -int _anjay_server_setup_primary_connection(anjay_server_info_t *server) { - assert(_anjay_server_active(server)); - server->data_active.primary_conn_type = ANJAY_CONNECTION_UNSET; - anjay_connection_ref_t ref = { - .server = server - }; - for (ref.conn_type = ANJAY_CONNECTION_FIRST_VALID_; - ref.conn_type < ANJAY_CONNECTION_LIMIT_; - ref.conn_type = (anjay_connection_type_t) (ref.conn_type + 1)) { - if (_anjay_connection_get_online_socket(ref)) { - server->data_active.primary_conn_type = ref.conn_type; - return 0; - } - } - - anjay_log(ERROR, "No suitable connection found for SSID = %u", - server->ssid); - return -1; -} - -static void connection_suspend(anjay_connection_ref_t conn_ref) { - const anjay_server_connection_t *connection = - _anjay_get_server_connection(conn_ref); - if (connection) { - avs_net_abstract_socket_t *socket = - _anjay_connection_internal_get_socket(connection); - if (socket) { - avs_net_socket_close(socket); - } - } -} - -void _anjay_connection_suspend(anjay_connection_ref_t conn_ref) { - if (conn_ref.conn_type == ANJAY_CONNECTION_UNSET) { - for (conn_ref.conn_type = ANJAY_CONNECTION_FIRST_VALID_; - conn_ref.conn_type < ANJAY_CONNECTION_LIMIT_; - conn_ref.conn_type = - (anjay_connection_type_t) (conn_ref.conn_type + 1)) { - connection_suspend(conn_ref); - } - } else { - connection_suspend(conn_ref); - } -} - -int -_anjay_connection_internal_bring_online(anjay_t *anjay, - anjay_server_connection_t *connection, - bool *out_session_resumed) { - assert(connection); - assert(connection->conn_socket_); - assert(!_anjay_connection_is_online(connection)); - - avs_net_socket_opt_value_t session_resumed; - char remote_hostname[ANJAY_MAX_URL_HOSTNAME_SIZE]; - char remote_port[ANJAY_MAX_URL_PORT_SIZE]; - if (avs_net_socket_get_remote_hostname(connection->conn_socket_, - remote_hostname, - sizeof(remote_hostname)) - || avs_net_socket_get_remote_port(connection->conn_socket_, - remote_port, - sizeof(remote_port))) { - anjay_log(ERROR, "Could not get peer address and port " - "of a suspended connection"); - return -1; - } - - anjay_socket_bind_config_t bind_config = { - .family = _anjay_socket_af_from_preferred_endpoint( - &connection->nontransient_state.preferred_endpoint), - .last_local_port_buffer = - &connection->nontransient_state.last_local_port, - .static_port_preference = anjay->udp_listen_port - }; - - if (_anjay_bind_and_connect_socket(connection->conn_socket_, &bind_config, - remote_hostname, remote_port)) { - goto close_and_fail; - } - - session_resumed.flag = true; - if (avs_net_socket_get_opt(connection->conn_socket_, - AVS_NET_SOCKET_OPT_SESSION_RESUMED, - &session_resumed)) { - // if avs_net_socket_get_opt() failed, it means that it's not a DTLS - // socket; if remote_port is empty, it means that it's an SMS socket; - // we treat a non-DTLS SMS socket as always "resumed", - // because MSISDN will not change during the library lifetime - *out_session_resumed = !*remote_port; - } else { - *out_session_resumed = session_resumed.flag; - } - anjay_log(INFO, "%s to %s:%s", - *out_session_resumed ? "resumed connection" : "reconnected", - remote_hostname, remote_port); - return 0; - - // Labels must be followed by a statement, not a declaration. - // Fortunately, `;` is a perfectly fine statement in C. -close_and_fail:; - int result = avs_net_socket_errno(connection->conn_socket_); - if (avs_net_socket_close(connection->conn_socket_)) { - anjay_log(ERROR, "Could not close the socket (?!)"); - } - return result; -} - -int _anjay_connection_bring_online(anjay_t *anjay, - anjay_connection_ref_t ref, - bool *out_session_resumed) { - return _anjay_connection_internal_bring_online( - anjay, _anjay_get_server_connection(ref), out_session_resumed); -} - -int _anjay_get_security_info(anjay_t *anjay, - avs_net_security_info_t *out_net_info, - anjay_server_dtls_keys_t *out_dtls_keys, - anjay_iid_t security_iid, - anjay_connection_type_t conn_type) { - const anjay_connection_type_definition_t *conn_def = - get_connection_type_def(conn_type); - anjay_connection_info_t info = { - .security_iid = security_iid - }; - - memset(out_dtls_keys, 0, sizeof(*out_dtls_keys)); - int result; - (void) ((result = conn_def->get_connection_info(anjay, &info, - out_dtls_keys)) - || (result = conn_def->get_net_security_info(out_net_info, &info, - out_dtls_keys))); - return result; -} - -static void queue_mode_close_socket(anjay_t *anjay, const void *ref_ptr) { - (void) anjay; - _anjay_connection_suspend(*(const anjay_connection_ref_t *) ref_ptr); -} - -void -_anjay_connection_schedule_queue_mode_close(anjay_t *anjay, - anjay_connection_ref_t ref) { - anjay_server_connection_t *connection = _anjay_get_server_connection(ref); - assert(connection); - _anjay_sched_del(anjay->sched, - &connection->queue_mode_close_socket_clb_handle); - if (connection->mode != ANJAY_CONNECTION_QUEUE) { - return; - } - - avs_time_duration_t delay = avs_coap_max_transmit_wait( - _anjay_tx_params_for_conn_type(anjay, ref.conn_type)); - - // see comment on field declaration for logic summary - if (_anjay_sched(anjay->sched, - &connection->queue_mode_close_socket_clb_handle, delay, - queue_mode_close_socket, &ref, sizeof(ref))) { - anjay_log(ERROR, "could not schedule queue mode operations"); - } -} diff --git a/src/servers/connection_udp.c b/src/servers/connection_udp.c index 5e7edc1bf..ec850c604 100644 --- a/src/servers/connection_udp.c +++ b/src/servers/connection_udp.c @@ -68,12 +68,14 @@ static bool uri_protocol_matching(anjay_udp_security_mode_t security_mode, const anjay_url_t *uri) { anjay_url_protocol_t expected_proto = (security_mode == ANJAY_UDP_SECURITY_NOSEC) - ? ANJAY_URL_PROTOCOL_COAP : ANJAY_URL_PROTOCOL_COAPS; + ? ANJAY_URL_PROTOCOL_COAP + : ANJAY_URL_PROTOCOL_COAPS; if (uri->protocol != expected_proto) { - anjay_log(WARNING, "URI protocol mismatch: security mode %d requires " - "'%s', but '%s' was configured", (int) security_mode, - uri_protocol_as_string(expected_proto), + anjay_log(WARNING, + "URI protocol mismatch: security mode %d requires " + "'%s', but '%s' was configured", + (int) security_mode, uri_protocol_as_string(expected_proto), uri_protocol_as_string(uri->protocol)); return false; } @@ -97,23 +99,25 @@ static int get_udp_dtls_keys(anjay_t *anjay, size_t *buffer_size_ptr; } values[] = { { - true, - ANJAY_DM_RID_SECURITY_PK_OR_IDENTITY, - out_keys->pk_or_identity, - sizeof(out_keys->pk_or_identity), - &out_keys->pk_or_identity_size - }, { - security_mode != ANJAY_UDP_SECURITY_PSK, - ANJAY_DM_RID_SECURITY_SERVER_PK_OR_IDENTITY, - out_keys->server_pk_or_identity, - sizeof(out_keys->server_pk_or_identity), - &out_keys->server_pk_or_identity_size - }, { - true, - ANJAY_DM_RID_SECURITY_SECRET_KEY, - out_keys->secret_key, - sizeof(out_keys->secret_key), - &out_keys->secret_key_size + .required = true, + .rid = ANJAY_DM_RID_SECURITY_PK_OR_IDENTITY, + .buffer = out_keys->pk_or_identity, + .buffer_capacity = sizeof(out_keys->pk_or_identity), + .buffer_size_ptr = &out_keys->pk_or_identity_size + }, + { + .required = security_mode != ANJAY_UDP_SECURITY_PSK, + .rid = ANJAY_DM_RID_SECURITY_SERVER_PK_OR_IDENTITY, + .buffer = out_keys->server_pk_or_identity, + .buffer_capacity = sizeof(out_keys->server_pk_or_identity), + .buffer_size_ptr = &out_keys->server_pk_or_identity_size + }, + { + .required = true, + .rid = ANJAY_DM_RID_SECURITY_SECRET_KEY, + .buffer = out_keys->secret_key, + .buffer_capacity = sizeof(out_keys->secret_key), + .buffer_size_ptr = &out_keys->secret_key_size } }; @@ -133,15 +137,14 @@ static int get_udp_dtls_keys(anjay_t *anjay, return 0; } -static int -get_udp_connection_info(anjay_t *anjay, - anjay_connection_info_t *inout_info, - anjay_server_dtls_keys_t *dtls_keys) { +static int get_udp_connection_info(anjay_t *anjay, + anjay_connection_info_t *inout_info, + anjay_server_dtls_keys_t *dtls_keys) { if (get_udp_security_mode(anjay, inout_info->security_iid, &inout_info->udp.security_mode) || (inout_info->uri - && !uri_protocol_matching(inout_info->udp.security_mode, - inout_info->uri)) + && !uri_protocol_matching(inout_info->udp.security_mode, + inout_info->uri)) || get_udp_dtls_keys(anjay, inout_info->security_iid, inout_info->udp.security_mode, dtls_keys)) { return -1; @@ -164,11 +167,10 @@ static int init_cert_security(avs_net_security_info_t *security, keys->secret_key_size, NULL); const void *raw_cert_der = keys->server_pk_or_identity_size > 0 - ? keys->server_pk_or_identity - : NULL; - avs_net_trusted_cert_info_t ca = - avs_net_trusted_cert_info_from_buffer( - raw_cert_der, keys->server_pk_or_identity_size); + ? keys->server_pk_or_identity + : NULL; + avs_net_trusted_cert_info_t ca = avs_net_trusted_cert_info_from_buffer( + raw_cert_der, keys->server_pk_or_identity_size); *security = avs_net_security_info_from_certificates( (avs_net_certificate_info_t) { @@ -181,9 +183,10 @@ static int init_cert_security(avs_net_security_info_t *security, return 0; } -static int get_udp_net_security_info(avs_net_security_info_t *out_net_info, - const anjay_connection_info_t *info, - const anjay_server_dtls_keys_t *dtls_keys) { +static int +get_udp_net_security_info(avs_net_security_info_t *out_net_info, + const anjay_connection_info_t *info, + const anjay_server_dtls_keys_t *dtls_keys) { switch (info->udp.security_mode) { case ANJAY_UDP_SECURITY_NOSEC: return 0; @@ -206,16 +209,20 @@ create_connected_udp_socket(anjay_t *anjay, const anjay_connection_info_t *info) { avs_net_socket_type_t type = info->udp.security_mode == ANJAY_UDP_SECURITY_NOSEC - ? AVS_NET_UDP_SOCKET : AVS_NET_DTLS_SOCKET; + ? AVS_NET_UDP_SOCKET + : AVS_NET_DTLS_SOCKET; inout_socket_config->backend_configuration = anjay->udp_socket_config; inout_socket_config->backend_configuration.reuse_addr = 1; inout_socket_config->backend_configuration.preferred_endpoint = &out_conn->nontransient_state.preferred_endpoint; + inout_socket_config->dtls_handshake_timeouts = + &anjay->udp_dtls_hs_tx_params; const void *config_ptr = (type == AVS_NET_DTLS_SOCKET) - ? (const void *) inout_socket_config - : (const void *) &inout_socket_config->backend_configuration; + ? (const void *) inout_socket_config + : (const void *) &inout_socket_config + ->backend_configuration; avs_net_abstract_socket_t *socket = NULL; int result = _anjay_create_connected_udp_socket( @@ -226,7 +233,8 @@ create_connected_udp_socket(anjay_t *anjay, .last_local_port_buffer = &out_conn->nontransient_state.last_local_port, .static_port_preference = anjay->udp_listen_port - }, info->uri); + }, + info->uri); if (!socket) { assert(result); anjay_log(ERROR, "could not create CoAP socket"); diff --git a/src/servers/connections.c b/src/servers/connections.c new file mode 100644 index 000000000..9ec8566b4 --- /dev/null +++ b/src/servers/connections.c @@ -0,0 +1,393 @@ +/* + * Copyright 2017-2018 AVSystem + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "../servers_utils.h" + +#define ANJAY_SERVERS_CONNECTION_SOURCE +#define ANJAY_SERVERS_INTERNALS + +#include "connections_internal.h" +#include "reload.h" + +VISIBILITY_SOURCE_BEGIN + +avs_net_abstract_socket_t *_anjay_connection_internal_get_socket( + const anjay_server_connection_t *connection) { + return connection->conn_socket_; +} + +void _anjay_connection_internal_clean_socket( + anjay_server_connection_t *connection) { + avs_net_socket_cleanup(&connection->conn_socket_); +} + +bool _anjay_connection_is_online(anjay_server_connection_t *connection) { + avs_net_abstract_socket_t *socket = + _anjay_connection_internal_get_socket(connection); + if (!socket) { + return false; + } + avs_net_socket_opt_value_t opt; + if (avs_net_socket_get_opt(socket, AVS_NET_SOCKET_OPT_STATE, &opt)) { + anjay_log(ERROR, "Could not get socket state"); + return false; + } + return opt.state == AVS_NET_SOCKET_STATE_CONNECTED; +} + +int _anjay_connection_init_psk_security(avs_net_security_info_t *security, + const anjay_server_dtls_keys_t *keys) { + *security = avs_net_security_info_from_psk((avs_net_psk_info_t) { + .psk = keys->secret_key, + .psk_size = keys->secret_key_size, + .identity = keys->pk_or_identity, + .identity_size = keys->pk_or_identity_size + }); + return 0; +} + +int _anjay_connection_internal_bring_online( + anjay_t *anjay, anjay_server_connection_t *connection) { + assert(connection); + assert(connection->conn_socket_); + assert(!_anjay_connection_is_online(connection)); + + avs_net_socket_opt_value_t session_resumed; + char remote_hostname[ANJAY_MAX_URL_HOSTNAME_SIZE]; + char remote_port[ANJAY_MAX_URL_PORT_SIZE]; + if (avs_net_socket_get_remote_hostname(connection->conn_socket_, + remote_hostname, + sizeof(remote_hostname)) + || avs_net_socket_get_remote_port(connection->conn_socket_, + remote_port, + sizeof(remote_port))) { + anjay_log(ERROR, "Could not get peer address and port " + "of a suspended connection"); + return -1; + } + + anjay_socket_bind_config_t bind_config = { + .family = _anjay_socket_af_from_preferred_endpoint( + &connection->nontransient_state.preferred_endpoint), + .last_local_port_buffer = + &connection->nontransient_state.last_local_port, + .static_port_preference = anjay->udp_listen_port + }; + + if (_anjay_bind_and_connect_socket(connection->conn_socket_, &bind_config, + remote_hostname, remote_port)) { + goto close_and_fail; + } + + session_resumed.flag = true; + if (avs_net_socket_get_opt(connection->conn_socket_, + AVS_NET_SOCKET_OPT_SESSION_RESUMED, + &session_resumed)) { + // if avs_net_socket_get_opt() failed, it means that it's not a DTLS + // socket; if remote_port is empty, it means that it's an SMS socket; + // we treat a non-DTLS SMS socket as always "resumed", + // because MSISDN will not change during the library lifetime + session_resumed.flag = !*remote_port; + } + if (!session_resumed.flag) { + _anjay_conn_session_token_reset(&connection->session_token); + } + anjay_log(INFO, "%s to %s:%s", + session_resumed.flag ? "resumed connection" : "reconnected", + remote_hostname, remote_port); + return 0; + + // Labels must be followed by a statement, not a declaration. + // Fortunately, `;` is a perfectly fine statement in C. +close_and_fail:; + int result = avs_net_socket_errno(connection->conn_socket_); + if (avs_net_socket_close(connection->conn_socket_)) { + anjay_log(ERROR, "Could not close the socket (?!)"); + } + return result; +} + +avs_net_af_t _anjay_socket_af_from_preferred_endpoint( + const avs_net_resolved_endpoint_t *endpoint) { + /* + * The first time we connect to the server, there is no "preferred + * endpoint" set yet, so endpoint is left uninitialized (filled with + * zeros). + */ + if (endpoint->size == 0) { + return AVS_NET_AF_UNSPEC; + } + + /* + * Whenever the socket is bound by connect(), the address family is set to + * match the remote address. If the socket is bound by a bind() call with + * NULL local_addr argument, the address family falls back to the original + * socket preference - by default, AF_UNSPEC. This causes avs_net to attempt + * to bind to [::]:$PORT, even though the remote host may be an IPv4 + * address. This generally works, because IPv4-mapped IPv6 addresses are a + * thing. + * + * On FreeBSD though, IPv4-mapped IPv6 are disabled by default (see: + * "Interaction between IPv4/v6 sockets" at + * https://www.freebsd.org/cgi/man.cgi?query=inet6&sektion=4), which + * effectively breaks all connect() calls after re-binding to a recently + * used port. + * + * To avoid that, we need to provide a local wildcard address appropriate + * for the family used by the remote host. This function determines which + * address family to use; it is then converted into a local address by + * bind_socket() in utils_core.c. + */ + char remote_preferred_host[sizeof( + "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")]; + if (!avs_net_resolved_endpoint_get_host(endpoint, remote_preferred_host, + sizeof(remote_preferred_host))) { + if (strchr(remote_preferred_host, ':') != NULL) { + return AVS_NET_AF_INET6; + } else if (strchr(remote_preferred_host, '.') != NULL) { + return AVS_NET_AF_INET4; + } + } + return AVS_NET_AF_UNSPEC; +} + +static void connection_cleanup(const anjay_t *anjay, + anjay_server_connection_t *connection) { + _anjay_connection_internal_clean_socket(connection); + _anjay_sched_del(anjay->sched, + &connection->queue_mode_close_socket_clb_handle); +} + +void _anjay_connections_close(const anjay_t *anjay, + anjay_connections_t *connections) { + anjay_connection_type_t conn_type; + ANJAY_CONNECTION_TYPE_FOREACH(conn_type) { + connection_cleanup(anjay, + _anjay_connection_get(connections, conn_type)); + } +} + +anjay_connection_type_t +_anjay_connections_get_primary(anjay_connections_t *connections) { + if (connections->primary_conn_type == ANJAY_CONNECTION_UNSET) { + anjay_connection_type_t conn_type; + ANJAY_CONNECTION_TYPE_FOREACH(conn_type) { + if (_anjay_connection_is_online( + _anjay_connection_get(connections, conn_type))) { + connections->primary_conn_type = conn_type; + break; + } + } + } + return connections->primary_conn_type; +} + +anjay_conn_session_token_t +_anjay_connections_get_primary_session_token(anjay_connections_t *connections) { + if (connections->primary_conn_type == ANJAY_CONNECTION_UNSET) { + anjay_conn_session_token_t result; + _anjay_conn_session_token_reset(&result); + return result; + } else { + return _anjay_connection_get(connections, + connections->primary_conn_type) + ->session_token; + } +} + +static int recreate_socket(anjay_t *anjay, + const anjay_connection_type_definition_t *def, + anjay_server_connection_t *connection, + anjay_connection_info_t *inout_info) { + anjay_server_dtls_keys_t dtls_keys; + memset(&dtls_keys, 0, sizeof(dtls_keys)); + + // At this point, inout_info has "global" settings filled, + // but transport-specific (i.e. UDP or SMS) fields are not + if (def->get_connection_info(anjay, inout_info, &dtls_keys)) { + anjay_log(DEBUG, "could not get %s connection info for server /%u/%u", + def->name, ANJAY_DM_OID_SECURITY, inout_info->security_iid); + return -1; + } + _anjay_connection_internal_clean_socket(connection); + + // Socket configuration is slightly different between UDP and SMS + // connections. That's why we do the common configuration here... + avs_net_ssl_configuration_t socket_config; + memset(&socket_config, 0, sizeof(socket_config)); + socket_config.version = anjay->dtls_version; + socket_config.session_resumption_buffer = + connection->nontransient_state.dtls_session_buffer; + socket_config.session_resumption_buffer_size = + sizeof(connection->nontransient_state.dtls_session_buffer); + int result = def->get_net_security_info(&socket_config.security, inout_info, + &dtls_keys); + // ...and pass it as an in/out argument to create_connected_socket() so that + // it can do any protocol-specific modifications. + if (!result) { + result = def->create_connected_socket(anjay, connection, &socket_config, + inout_info); + } + if (!result) { + avs_net_socket_opt_value_t session_resumed; + if (avs_net_socket_get_opt(connection->conn_socket_, + AVS_NET_SOCKET_OPT_SESSION_RESUMED, + &session_resumed) + || !session_resumed.flag) { + _anjay_conn_session_token_reset(&connection->session_token); + } + } else { + avs_net_abstract_socket_t *sock = connection->conn_socket_; + if (sock) { + avs_net_socket_close(sock); + } + } + return result; +} + +static int +ensure_socket_connected(anjay_t *anjay, + const anjay_connection_type_definition_t *def, + anjay_server_connection_t *connection, + anjay_connection_info_t *inout_info) { + avs_net_abstract_socket_t *existing_socket = + _anjay_connection_internal_get_socket(connection); + + if (existing_socket == NULL) { + return recreate_socket(anjay, def, connection, inout_info); + } else if (!_anjay_connection_is_online(connection)) { + return _anjay_connection_internal_bring_online(anjay, connection); + } + return 0; +} + +static const anjay_connection_type_definition_t * +get_connection_type_def(anjay_connection_type_t type) { + switch (type) { + case ANJAY_CONNECTION_UDP: + return &ANJAY_CONNECTION_DEF_UDP; + default: + AVS_UNREACHABLE("unknown connection type"); + return NULL; + } +} + +static int refresh_connection(anjay_t *anjay, + anjay_connections_t *connections, + anjay_connection_type_t conn_type, + anjay_connection_info_t *inout_info) { + anjay_server_connection_t *out_connection = + _anjay_connection_get(connections, conn_type); + assert(out_connection); + const anjay_connection_type_definition_t *def = + get_connection_type_def(conn_type); + assert(def); + + out_connection->mode = + _anjay_get_connection_mode(inout_info->binding_mode, conn_type); + if (out_connection->mode == ANJAY_CONNECTION_DISABLED) { + _anjay_connection_internal_clean_socket(out_connection); + _anjay_conn_session_token_reset(&out_connection->session_token); + return 0; + } else { + return ensure_socket_connected(anjay, def, out_connection, inout_info); + } +} + +static bool is_connected(anjay_connections_t *connections) { + anjay_connection_type_t conn_type; + ANJAY_CONNECTION_TYPE_FOREACH(conn_type) { + if (_anjay_connection_is_online( + _anjay_connection_get(connections, conn_type))) { + return true; + } + } + return false; +} + +int _anjay_connections_refresh(anjay_t *anjay, + anjay_connections_t *connections, + anjay_iid_t security_iid, + const anjay_url_t *uri, + const char *binding_mode) { + anjay_connection_info_t server_info = { + .security_iid = security_iid, + .uri = uri, + .binding_mode = binding_mode + }; + + anjay_conn_session_token_t previous_session_token = + _anjay_connections_get_primary_session_token(connections); + + int results[ANJAY_CONNECTION_LIMIT_]; + int first_error = 0; + for (size_t i = 0; i < AVS_ARRAY_SIZE(results); ++i) { + if ((results[i] = refresh_connection(anjay, connections, + (anjay_connection_type_t) i, + &server_info)) + && !first_error) { + first_error = results[i]; + } + } + + if (!is_connected(connections)) { + return first_error ? first_error : -1; + } + if (first_error) { + // some connection is available, but some other failed + _anjay_schedule_delayed_reload_servers(anjay); + } + + if (!_anjay_conn_session_tokens_equal( + previous_session_token, + _anjay_connections_get_primary_session_token(connections))) { + // The following causes _anjay_connections_get_primary() to search for + // the primary connection again. In conjunction with what is done in + // _anjay_active_server_refresh(), this forces functions such as + // send_update_sched_job() or reload_active_server() to schedule + // reactivation of the server, and forces + // _anjay_server_ensure_valid_registration() to send Register. + connections->primary_conn_type = ANJAY_CONNECTION_UNSET; + } + + anjay_connection_type_t primary_conn_type = + _anjay_connections_get_primary(connections); + return primary_conn_type != ANJAY_CONNECTION_UNSET + ? results[primary_conn_type] + : first_error; +} + +int _anjay_get_security_info(anjay_t *anjay, + avs_net_security_info_t *out_net_info, + anjay_server_dtls_keys_t *out_dtls_keys, + anjay_iid_t security_iid, + anjay_connection_type_t conn_type) { + const anjay_connection_type_definition_t *conn_def = + get_connection_type_def(conn_type); + anjay_connection_info_t info = { + .security_iid = security_iid + }; + + memset(out_dtls_keys, 0, sizeof(*out_dtls_keys)); + int result; + (void) ((result = + conn_def->get_connection_info(anjay, &info, out_dtls_keys)) + || (result = conn_def->get_net_security_info(out_net_info, &info, + out_dtls_keys))); + return result; +} diff --git a/src/servers/connection_info.h b/src/servers/connections.h similarity index 67% rename from src/servers/connection_info.h rename to src/servers/connections.h index cef5f41eb..de4095fa6 100644 --- a/src/servers/connection_info.h +++ b/src/servers/connections.h @@ -14,14 +14,14 @@ * limitations under the License. */ -#ifndef ANJAY_SERVERS_CONNECTION_INFO_H -#define ANJAY_SERVERS_CONNECTION_INFO_H +#ifndef ANJAY_SERVERS_CONNECTIONS_H +#define ANJAY_SERVERS_CONNECTIONS_H #include "../anjay_core.h" #include "../utils_core.h" #if !defined(ANJAY_SERVERS_INTERNALS) && !defined(ANJAY_TEST) -#error "Headers from servers/ are not meant to be included from outside" +# error "Headers from servers/ are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN @@ -84,9 +84,19 @@ typedef struct { avs_net_abstract_socket_t *conn_socket_; #if defined(__GNUC__) && !defined(__CC_ARM) \ && !(defined(ANJAY_SERVERS_CONNECTION_SOURCE) || defined(ANJAY_TEST)) -#pragma GCC poison conn_socket_ +# pragma GCC poison conn_socket_ #endif + /** + * Token that changes to a new unique value every time the CoAP endpoint + * association (i.e., DTLS session or raw UDP socket) every time it has been + * established anew. + * + * It is used to determine whether reconnect operation re-used the previous + * association or created a new one. + */ + anjay_conn_session_token_t session_token; + /** * The part of active connection state that is intentionally NOT cleaned up * when deactivating the server. It contains: @@ -115,29 +125,60 @@ typedef struct { anjay_sched_handle_t queue_mode_close_socket_clb_handle; } anjay_server_connection_t; -anjay_server_connection_t * -_anjay_get_server_connection(anjay_connection_ref_t ref); +typedef struct { + /** + * Connection (socket, binding) entries - see docs to + * anjay_server_connection_t for details. + */ + anjay_server_connection_t connections_[ANJAY_CONNECTION_LIMIT_]; + + /** + * Information about which connection is currently the "primary" one. The + * "primary" connection is the one on which the autonomous outgoing messages + * (i.e. Register/Update or Bootstrap Request) are sent. See the docs in + * server.h for details (Ctrl+F the word "primary"). + */ + anjay_connection_type_t primary_conn_type; +} anjay_connections_t; + +static inline anjay_server_connection_t * +_anjay_connection_get(anjay_connections_t *connections, + anjay_connection_type_t conn_type) { + assert(conn_type >= (anjay_connection_type_t) 0 + && conn_type < ANJAY_CONNECTION_LIMIT_); + return &connections->connections_[conn_type]; +} + +#if defined(__GNUC__) && !defined(__CC_ARM) +# pragma GCC poison connections_ +#endif avs_net_abstract_socket_t *_anjay_connection_internal_get_socket( const anjay_server_connection_t *connection); -void -_anjay_connection_internal_clean_socket(anjay_server_connection_t *connection); +void _anjay_connection_internal_clean_socket( + anjay_server_connection_t *connection); + +anjay_connection_type_t +_anjay_connections_get_primary(anjay_connections_t *connections); + +anjay_conn_session_token_t +_anjay_connections_get_primary_session_token(anjay_connections_t *connections); bool _anjay_connection_is_online(anjay_server_connection_t *connection); -int -_anjay_connection_internal_bring_online(anjay_t *anjay, - anjay_server_connection_t *connection, - bool *out_session_resumed); +int _anjay_connection_internal_bring_online( + anjay_t *anjay, anjay_server_connection_t *connection); -/** - * @returns @li 0 on success, - * @li a positive errno value in case of a primary socket (UDP) error, - * @li a negative value in case of other error. - */ -int _anjay_active_server_refresh(anjay_t *anjay, anjay_server_info_t *server); +void _anjay_connections_close(const anjay_t *anjay, + anjay_connections_t *connections); + +int _anjay_connections_refresh(anjay_t *anjay, + anjay_connections_t *connections, + anjay_iid_t security_iid, + const anjay_url_t *uri, + const char *binding_mode); VISIBILITY_PRIVATE_HEADER_END -#endif // ANJAY_SERVERS_CONNECTION_INFO_H +#endif // ANJAY_SERVERS_CONNECTIONS_H diff --git a/src/servers/connections_internal.h b/src/servers/connections_internal.h index 92fc62cf5..89077da84 100644 --- a/src/servers/connections_internal.h +++ b/src/servers/connections_internal.h @@ -19,12 +19,12 @@ #include "../servers.h" -#include "connection_info.h" +#include "connections.h" -#if !defined(ANJAY_TEST) \ +#if !defined(ANJAY_TEST) \ && (!defined(ANJAY_SERVERS_INTERNALS) \ - || !defined(ANJAY_SERVERS_CONNECTION_SOURCE)) -#error "connections_internal.h shall only be included from connection_*.c files" + || !defined(ANJAY_SERVERS_CONNECTION_SOURCE)) +# error "connections_internal.h shall only be included from connection_*.c files" #endif VISIBILITY_PRIVATE_HEADER_BEGIN @@ -37,21 +37,22 @@ typedef struct { typedef struct { anjay_iid_t security_iid; const anjay_url_t *uri; - anjay_binding_mode_t binding_mode; + const char *binding_mode; anjay_udp_connection_info_t udp; } anjay_connection_info_t; -typedef int anjay_connection_get_info_t( - anjay_t *anjay, - anjay_connection_info_t *inout_info, - anjay_server_dtls_keys_t *out_dtls_keys); +typedef int +anjay_connection_get_info_t(anjay_t *anjay, + anjay_connection_info_t *inout_info, + anjay_server_dtls_keys_t *out_dtls_keys); typedef int anjay_connection_get_net_security_info_t( avs_net_security_info_t *out_net_info, const anjay_connection_info_t *info, const anjay_server_dtls_keys_t *dtls_keys); -typedef int anjay_connection_create_connected_socket_t(anjay_t *anjay, +typedef int anjay_connection_create_connected_socket_t( + anjay_t *anjay, anjay_server_connection_t *out_connection, avs_net_ssl_configuration_t *inout_socket_config, const anjay_connection_info_t *info); @@ -74,4 +75,3 @@ avs_net_af_t _anjay_socket_af_from_preferred_endpoint( VISIBILITY_PRIVATE_HEADER_END #endif /* ANJAY_SERVERS_CONNECTIONS_INTERNAL_H */ - diff --git a/src/servers/offline.c b/src/servers/offline.c index cd18cb760..713c9176f 100644 --- a/src/servers/offline.c +++ b/src/servers/offline.c @@ -18,11 +18,11 @@ #define ANJAY_SERVERS_INTERNALS -#include "../servers.h" #include "../anjay_core.h" +#include "../servers.h" -#include "connection_info.h" #include "reload.h" +#include "server_connections.h" #include "servers_internal.h" VISIBILITY_SOURCE_BEGIN @@ -44,17 +44,11 @@ static void enter_offline_job(anjay_t *anjay, const void *dummy) { anjay_connection_ref_t ref = { .server = server }; - for (ref.conn_type = ANJAY_CONNECTION_FIRST_VALID_; - ref.conn_type < ANJAY_CONNECTION_LIMIT_; - ref.conn_type = - (anjay_connection_type_t) (ref.conn_type + 1)) { - anjay_server_connection_t *connection = - _anjay_get_server_connection(ref); - if (connection) { - _anjay_connection_internal_clean_socket(connection); - } + ANJAY_CONNECTION_TYPE_FOREACH(ref.conn_type) { + _anjay_connection_internal_clean_socket( + _anjay_get_server_connection(ref)); } - server->data_inactive.reactivate_time = now; + server->reactivate_time = now; } } _anjay_sched_del(anjay->sched, &anjay->reload_servers_sched_job_handle); diff --git a/src/servers/register_internal.c b/src/servers/register_internal.c index eb867fd05..5530c8084 100644 --- a/src/servers/register_internal.c +++ b/src/servers/register_internal.c @@ -23,14 +23,14 @@ #define ANJAY_SERVERS_INTERNALS #include "../anjay_core.h" +#include "../interface/register.h" #include "../servers.h" #include "../servers_utils.h" -#include "../interface/register.h" #include "activate.h" -#include "connection_info.h" -#include "reload.h" #include "register_internal.h" +#include "reload.h" +#include "server_connections.h" #include "servers_internal.h" VISIBILITY_SOURCE_BEGIN @@ -71,7 +71,7 @@ static void send_update_sched_job(anjay_t *anjay, const void *ssid_ptr) { } } // mark that the registration is expired; prevents superfluous Deregister - server->data_active.registration_info.expire_time = AVS_TIME_REAL_INVALID; + server->registration_info.expire_time = AVS_TIME_REAL_INVALID; _anjay_server_deactivate(anjay, ssid, AVS_TIME_DURATION_ZERO); return; retry: @@ -86,16 +86,15 @@ static void send_update_sched_job(anjay_t *anjay, const void *ssid_ptr) { * performing the Update operation. */ static avs_time_duration_t -get_server_update_interval_margin(anjay_t *anjay, - const anjay_server_info_t *server) { +get_server_update_interval_margin(anjay_t *anjay, anjay_server_info_t *server) { avs_time_duration_t half_lifetime = avs_time_duration_div( - avs_time_duration_from_scalar( - server->data_active.registration_info.last_update_params.lifetime_s, - AVS_TIME_S), - ANJAY_UPDATE_INTERVAL_MARGIN_FACTOR); + avs_time_duration_from_scalar( + server->registration_info.last_update_params.lifetime_s, + AVS_TIME_S), + ANJAY_UPDATE_INTERVAL_MARGIN_FACTOR); avs_time_duration_t max_transmit_wait = avs_coap_max_transmit_wait(_anjay_tx_params_for_conn_type( - anjay, server->data_active.primary_conn_type)); + anjay, _anjay_server_primary_conn_type(server))); if (avs_time_duration_less(half_lifetime, max_transmit_wait)) { return half_lifetime; } else { @@ -106,19 +105,19 @@ get_server_update_interval_margin(anjay_t *anjay, static int schedule_update(anjay_t *anjay, anjay_server_info_t *server, avs_time_duration_t delay) { - anjay_log(DEBUG, "scheduling update for SSID %u after " - "%" PRId64 ".%09" PRId32, + anjay_log(DEBUG, + "scheduling update for SSID %u after " + "%" PRId64 ".%09" PRId32, server->ssid, delay.seconds, delay.nanoseconds); return _anjay_servers_schedule_first_retryable( - anjay->sched, server, delay, - send_update_sched_job, server->ssid); + anjay->sched, server, delay, send_update_sched_job, server->ssid); } static int schedule_next_update(anjay_t *anjay, anjay_server_info_t *server) { assert(_anjay_server_active(server)); - avs_time_duration_t remaining = _anjay_register_time_remaining( - &server->data_active.registration_info); + avs_time_duration_t remaining = + _anjay_register_time_remaining(&server->registration_info); avs_time_duration_t interval_margin = get_server_update_interval_margin(anjay, server); remaining = avs_time_duration_diff(remaining, interval_margin); @@ -133,12 +132,12 @@ static int schedule_next_update(anjay_t *anjay, anjay_server_info_t *server) { bool _anjay_server_primary_connection_valid(anjay_server_info_t *server) { assert(_anjay_server_active(server)); - return server->data_active.primary_conn_type != ANJAY_CONNECTION_UNSET - && _anjay_connection_get_online_socket( - (anjay_connection_ref_t) { - .server = server, - .conn_type = server->data_active.primary_conn_type - }) != NULL; + anjay_connection_type_t conn_type = _anjay_server_primary_conn_type(server); + return conn_type != ANJAY_CONNECTION_UNSET + && _anjay_connection_get_online_socket((anjay_connection_ref_t) { + .server = server, + .conn_type = conn_type + }) != NULL; } int _anjay_server_reschedule_update_job(anjay_t *anjay, @@ -162,8 +161,7 @@ static int reschedule_update_for_server(anjay_t *anjay, return 0; } -static int -reschedule_update_for_all_servers(anjay_t *anjay) { +static int reschedule_update_for_all_servers(anjay_t *anjay) { int result = 0; AVS_LIST(anjay_server_info_t) it; @@ -196,8 +194,7 @@ reschedule_update_for_all_servers(anjay_t *anjay) { * - _anjay_schedule_reregister(), although that's probably rather superfluous - * see the docs of that function for details */ -int anjay_schedule_registration_update(anjay_t *anjay, - anjay_ssid_t ssid) { +int anjay_schedule_registration_update(anjay_t *anjay, anjay_ssid_t ssid) { if (anjay_is_offline(anjay)) { anjay_log(ERROR, "cannot schedule registration update while being offline"); @@ -222,7 +219,7 @@ int anjay_schedule_registration_update(anjay_t *anjay, int _anjay_schedule_reregister(anjay_t *anjay, anjay_server_info_t *server) { assert(_anjay_server_active(server)); - server->data_active.registration_info.expire_time = AVS_TIME_REAL_INVALID; + server->registration_info.expire_time = AVS_TIME_REAL_INVALID; return _anjay_server_deactivate(anjay, server->ssid, AVS_TIME_DURATION_ZERO); } @@ -245,15 +242,18 @@ static int registration_update_with_ctx(anjay_registration_update_ctx_t *ctx, return (int) ANJAY_UPDATE_SUCCESS; case ANJAY_REGISTRATION_UPDATE_REJECTED: - anjay_log(DEBUG, "update rejected for SSID = %u; " - "needs re-registration", server->ssid); - server->data_active.registration_info.expire_time = - AVS_TIME_REAL_INVALID; + anjay_log(DEBUG, + "update rejected for SSID = %u; " + "needs re-registration", + server->ssid); + server->registration_info.expire_time = AVS_TIME_REAL_INVALID; return (int) ANJAY_UPDATE_NEEDS_REGISTRATION; case AVS_COAP_CTX_ERR_NETWORK: - anjay_log(ERROR, "network communication error while updating " - "registration for SSID==%" PRIu16, server->ssid); + anjay_log(ERROR, + "network communication error while updating " + "registration for SSID==%" PRIu16, + server->ssid); // We cannot use _anjay_schedule_server_reconnect(), // because it would mean an endless loop without backoff // if the server is down. Instead, we disconnect the socket @@ -261,7 +261,7 @@ static int registration_update_with_ctx(anjay_registration_update_ctx_t *ctx, // _anjay_server_refresh() will reconnect the socket. _anjay_connection_suspend((anjay_connection_ref_t) { .server = server, - .conn_type = server->data_active.primary_conn_type + .conn_type = _anjay_server_primary_conn_type(server) }); return (int) ANJAY_UPDATE_FAILED; @@ -278,29 +278,25 @@ ensure_valid_registration_with_ctx(anjay_t *anjay, anjay_update_result_t update_result; if (!_anjay_server_primary_connection_valid(server)) { - anjay_log(INFO, "No valid existing connection to Registration " - "Interface for SSID = %u, needs re-registration", + anjay_log(ERROR, + "No valid connection to Registration Interface for " + "SSID = %u", server->ssid); - server->data_active.registration_info.expire_time = - AVS_TIME_REAL_INVALID; - update_result = ANJAY_UPDATE_NEEDS_REGISTRATION; + return (int) ANJAY_REGISTRATION_FAILED; } else if (_anjay_server_registration_expired(server)) { update_result = ANJAY_UPDATE_NEEDS_REGISTRATION; } else if (!_anjay_needs_registration_update(ctx)) { update_result = ANJAY_UPDATE_SUCCESS; } else { - update_result = (anjay_update_result_t) - registration_update_with_ctx(ctx, server); + update_result = + (anjay_update_result_t) registration_update_with_ctx(ctx, + server); } switch (update_result) { case (int) ANJAY_UPDATE_SUCCESS: return (int) ANJAY_REGISTRATION_SUCCESS; case (int) ANJAY_UPDATE_NEEDS_REGISTRATION: { - if (!_anjay_server_primary_connection_valid(server) - && _anjay_server_setup_primary_connection(server)) { - return (int) ANJAY_REGISTRATION_FAILED; - } int retval = _anjay_register(ctx); if (retval) { anjay_log(DEBUG, "re-registration failed"); @@ -342,10 +338,11 @@ static int perform_registration_action(anjay_t *anjay, int retval = action(anjay, &ctx, server); if (!retval) { // Ignore errors, failure to flush notifications is not fatal. - _anjay_observe_sched_flush(anjay, (anjay_connection_key_t) { - .ssid = server->ssid, - .type = server->data_active.primary_conn_type - }); + _anjay_observe_sched_flush( + anjay, (anjay_connection_key_t) { + .ssid = server->ssid, + .type = _anjay_server_primary_conn_type(server) + }); } _anjay_registration_update_ctx_release(&ctx); @@ -364,12 +361,12 @@ registration_update_if_possible_with_ctx(anjay_t *anjay, (void) anjay; if (!_anjay_server_primary_connection_valid(server)) { - anjay_log(INFO, "No valid existing connection to Registration " - "Interface for SSID = %u, needs re-registration", + anjay_log(INFO, + "No valid connection to Registration Interface for " + "SSID = %u", server->ssid); - server->data_active.registration_info.expire_time = - AVS_TIME_REAL_INVALID; - return (int) ANJAY_UPDATE_NEEDS_REGISTRATION; + server->registration_info.expire_time = AVS_TIME_REAL_INVALID; + return (int) ANJAY_UPDATE_FAILED; } if (_anjay_server_registration_expired(server)) { @@ -380,8 +377,7 @@ registration_update_if_possible_with_ctx(anjay_t *anjay, } anjay_update_result_t -_anjay_server_registration_update(anjay_t *anjay, - anjay_server_info_t *server) { +_anjay_server_registration_update(anjay_t *anjay, anjay_server_info_t *server) { int result = perform_registration_action( anjay, server, registration_update_if_possible_with_ctx); return result >= 0 ? (anjay_update_result_t) result : ANJAY_UPDATE_FAILED; @@ -390,18 +386,18 @@ _anjay_server_registration_update(anjay_t *anjay, anjay_registration_result_t _anjay_server_ensure_valid_registration(anjay_t *anjay, anjay_server_info_t *server) { - int result = perform_registration_action( - anjay, server, ensure_valid_registration_with_ctx); - return result >= 0 - ? (anjay_registration_result_t) result : ANJAY_REGISTRATION_FAILED; + int result = + perform_registration_action(anjay, server, + ensure_valid_registration_with_ctx); + return result >= 0 ? (anjay_registration_result_t) result + : ANJAY_REGISTRATION_FAILED; } -int _anjay_server_deregister(anjay_t *anjay, - anjay_server_info_t *server) { +int _anjay_server_deregister(anjay_t *anjay, anjay_server_info_t *server) { assert(_anjay_server_active(server)); anjay_connection_ref_t connection = { .server = server, - .conn_type = server->data_active.primary_conn_type + .conn_type = _anjay_server_primary_conn_type(server) }; if (connection.conn_type == ANJAY_CONNECTION_UNSET || _anjay_bind_server_stream(anjay, connection)) { @@ -410,8 +406,8 @@ int _anjay_server_deregister(anjay_t *anjay, return 0; } - int result = _anjay_deregister( - anjay, server->data_active.registration_info.endpoint_path); + int result = + _anjay_deregister(anjay, server->registration_info.endpoint_path); if (result) { anjay_log(ERROR, "could not send De-Register request: %d", result); } @@ -423,7 +419,7 @@ int _anjay_server_deregister(anjay_t *anjay, const anjay_registration_info_t * _anjay_server_registration_info(anjay_server_info_t *server) { assert(_anjay_server_active(server)); - return &server->data_active.registration_info; + return &server->registration_info; } static avs_time_real_t get_registration_expire_time(int64_t lifetime_s) { @@ -437,7 +433,7 @@ void _anjay_server_update_registration_info( AVS_LIST(const anjay_string_t) *move_endpoint_path, anjay_update_parameters_t *move_params) { assert(_anjay_server_active(server)); - anjay_registration_info_t *info = &server->data_active.registration_info; + anjay_registration_info_t *info = &server->registration_info; if (move_endpoint_path && move_endpoint_path != &info->endpoint_path) { AVS_LIST_CLEAR(&info->endpoint_path); diff --git a/src/servers/register_internal.h b/src/servers/register_internal.h index e744fb5b5..3fb24ba1d 100644 --- a/src/servers/register_internal.h +++ b/src/servers/register_internal.h @@ -20,13 +20,12 @@ #include "../anjay_core.h" #ifndef ANJAY_SERVERS_INTERNALS -#error "Headers from servers/ are not meant to be included from outside" +# error "Headers from servers/ are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN -bool -_anjay_server_primary_connection_valid(anjay_server_info_t *server); +bool _anjay_server_primary_connection_valid(anjay_server_info_t *server); typedef enum { ANJAY_UPDATE_SUCCESS = 0, @@ -67,8 +66,7 @@ _anjay_server_ensure_valid_registration(anjay_t *anjay, int _anjay_server_reschedule_update_job(anjay_t *anjay, anjay_server_info_t *server); -int _anjay_server_deregister(anjay_t *anjay, - anjay_server_info_t *server); +int _anjay_server_deregister(anjay_t *anjay, anjay_server_info_t *server); VISIBILITY_PRIVATE_HEADER_END diff --git a/src/servers/reload.c b/src/servers/reload.c index 1cd285779..c1b926a7b 100644 --- a/src/servers/reload.c +++ b/src/servers/reload.c @@ -16,22 +16,23 @@ #include -#include "../servers_utils.h" #include "../dm/query.h" +#include "../servers_utils.h" #define ANJAY_SERVERS_INTERNALS -#include "connection_info.h" -#include "servers_internal.h" #include "activate.h" #include "register_internal.h" #include "reload.h" +#include "server_connections.h" +#include "servers_internal.h" VISIBILITY_SOURCE_BEGIN -static int reload_active_server(anjay_t *anjay, - anjay_server_info_t *server) { +static int reload_active_server(anjay_t *anjay, anjay_server_info_t *server) { assert(_anjay_server_active(server)); + anjay_conn_session_token_t previous_session_token = + _anjay_server_primary_session_token(server); if (server->ssid != ANJAY_SSID_BOOTSTRAP && _anjay_server_registration_expired(server)) { // Registration expired - we need to re-register, but we only call @@ -44,22 +45,21 @@ static int reload_active_server(anjay_t *anjay, } if (server->ssid == ANJAY_SSID_BOOTSTRAP) { - if (!_anjay_server_primary_connection_valid(server)) { + if (!_anjay_conn_session_tokens_equal( + previous_session_token, + _anjay_server_primary_session_token(server))) { return _anjay_bootstrap_update_reconnected(anjay); } } else { - if (!_anjay_server_primary_connection_valid(server)) { - // invalidate registration lifetime, so that retries of the call - // to this function will actually retry registering even if - // reconnection succeeds - server->data_active.registration_info.expire_time = - AVS_TIME_REAL_INVALID; + if (_anjay_server_registration_expired(server) + || !_anjay_server_primary_connection_valid(server)) { goto deactivate; } - _anjay_observe_sched_flush(anjay, (anjay_connection_key_t) { - .ssid = server->ssid, - .type = server->data_active.primary_conn_type - }); + _anjay_observe_sched_flush( + anjay, (anjay_connection_key_t) { + .ssid = server->ssid, + .type = _anjay_server_primary_conn_type(server) + }); } return 0; deactivate: @@ -73,10 +73,9 @@ static void reload_server_by_ssid_job(anjay_t *anjay, const void *ssid_ptr) { AVS_LIST(anjay_server_info_t) server = _anjay_servers_find_active(anjay, ssid); - if (server - && reload_active_server(anjay, server) + if (server && reload_active_server(anjay, server) && _anjay_servers_schedule_next_retryable( - anjay->sched, server, reload_server_by_ssid_job, ssid)) { + anjay->sched, server, reload_server_by_ssid_job, ssid)) { anjay_log(ERROR, "could not reschedule reload_server_by_ssid_job"); } } @@ -95,11 +94,11 @@ static int reload_server_by_ssid(anjay_t *anjay, anjay_log(TRACE, "reloading active server SSID %u", ssid); return reload_active_server(anjay, server); } else if (!server->next_action_handle - && avs_time_real_valid(server->data_inactive.reactivate_time)) { + && avs_time_real_valid(server->reactivate_time)) { return _anjay_server_sched_activate( - anjay, server, avs_time_real_diff( - server->data_inactive.reactivate_time, - avs_time_real_now())); + anjay, server, + avs_time_real_diff(server->reactivate_time, + avs_time_real_now())); } else { return 0; } @@ -150,7 +149,7 @@ reload_server_by_security_iid(anjay_t *anjay, } static void reload_servers_sched_job(anjay_t *anjay, const void *unused) { - (void)unused; + (void) unused; anjay_log(TRACE, "reloading servers"); anjay_servers_t old_servers = *anjay->servers; @@ -163,9 +162,8 @@ static void reload_servers_sched_job(anjay_t *anjay, const void *unused) { const anjay_dm_object_def_t *const *obj = _anjay_dm_find_object_by_oid(anjay, ANJAY_DM_OID_SECURITY); if (obj - && _anjay_dm_foreach_instance(anjay, obj, - reload_server_by_security_iid, - &reload_state) + && _anjay_dm_foreach_instance( + anjay, obj, reload_server_by_security_iid, &reload_state) && !reload_state.retval) { reload_state.retval = -1; } @@ -174,15 +172,15 @@ static void reload_servers_sched_job(anjay_t *anjay, const void *unused) { // scheduled for activation - schedule that. It's necessary to perform // Client-Initiated Bootstrap if Server-Initiated Bootstrap is disabled in // configuration. - if (!reload_state.retval - && anjay->servers->servers + if (!reload_state.retval && anjay->servers->servers && !AVS_LIST_NEXT(anjay->servers->servers) && anjay->servers->servers->ssid == ANJAY_SSID_BOOTSTRAP && !_anjay_server_active(anjay->servers->servers) && !anjay->servers->servers->next_action_handle - && !anjay->servers->servers->data_inactive.reactivate_failed) { - reload_state.retval = _anjay_server_sched_activate( - anjay, anjay->servers->servers, AVS_TIME_DURATION_ZERO); + && !anjay->servers->servers->reactivate_failed) { + reload_state.retval = + _anjay_server_sched_activate(anjay, anjay->servers->servers, + AVS_TIME_DURATION_ZERO); } if (reload_state.retval) { @@ -192,8 +190,7 @@ static void reload_servers_sched_job(anjay_t *anjay, const void *unused) { AVS_LIST_DELETABLE_FOREACH_PTR(server_ptr, helper, &old_servers.servers) { if (_anjay_server_active(*server_ptr)) { - _anjay_servers_add(anjay->servers, - AVS_LIST_DETACH(server_ptr)); + _anjay_servers_add(anjay->servers, AVS_LIST_DETACH(server_ptr)); } } anjay_log(ERROR, "reloading servers failed, re-scheduling job"); @@ -211,7 +208,7 @@ static void reload_servers_sched_job(anjay_t *anjay, const void *unused) { _anjay_servers_internal_deregister(anjay, &old_servers); _anjay_servers_internal_cleanup(anjay, &old_servers); anjay_log(TRACE, "%lu servers reloaded", - (unsigned long)AVS_LIST_SIZE(anjay->servers->servers)); + (unsigned long) AVS_LIST_SIZE(anjay->servers->servers)); } static int schedule_reload_servers(anjay_t *anjay, bool delayed) { @@ -239,8 +236,8 @@ int _anjay_schedule_reload_server(anjay_t *anjay, anjay_server_info_t *server) { assert(_anjay_server_active(server)); _anjay_sched_del(anjay->sched, &server->next_action_handle); if (_anjay_servers_schedule_first_retryable( - anjay->sched, server, AVS_TIME_DURATION_ZERO, - reload_server_by_ssid_job, server->ssid)) { + anjay->sched, server, AVS_TIME_DURATION_ZERO, + reload_server_by_ssid_job, server->ssid)) { anjay_log(ERROR, "could not schedule reload_server_by_ssid_job"); return -1; } diff --git a/src/servers/reload.h b/src/servers/reload.h index f27a2014e..6e1abc637 100644 --- a/src/servers/reload.h +++ b/src/servers/reload.h @@ -20,7 +20,7 @@ #include #if !defined(ANJAY_SERVERS_INTERNALS) && !defined(ANJAY_TEST) -#error "Headers from servers/ are not meant to be included from outside" +# error "Headers from servers/ are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN diff --git a/src/servers/server_connections.c b/src/servers/server_connections.c new file mode 100644 index 000000000..8059a5a18 --- /dev/null +++ b/src/servers/server_connections.c @@ -0,0 +1,163 @@ +/* + * Copyright 2017-2018 AVSystem + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +#include +#include + +#include "../dm/query.h" +#include "../servers_utils.h" +#include "../utils_core.h" + +#define ANJAY_SERVERS_INTERNALS + +#include "reload.h" +#include "server_connections.h" +#include "servers_internal.h" + +VISIBILITY_SOURCE_BEGIN + +static int read_binding_mode(anjay_t *anjay, + anjay_ssid_t ssid, + anjay_binding_mode_t *out_binding_mode) { + anjay_uri_path_t path = + MAKE_RESOURCE_PATH(ANJAY_DM_OID_SERVER, ANJAY_IID_INVALID, + ANJAY_DM_RID_SERVER_BINDING); + + if (_anjay_find_server_iid(anjay, ssid, &path.iid) + || _anjay_dm_res_read_string(anjay, &path, *out_binding_mode, + sizeof(*out_binding_mode))) { + anjay_log(WARNING, "could not read binding mode for LwM2M server %u", + ssid); + return -1; + } + if (!anjay_binding_mode_valid(*out_binding_mode)) { + anjay_log(WARNING, "invalid binding mode \"%s\" for LwM2M server %u", + *out_binding_mode, ssid); + return -1; + } + return 0; +} + +anjay_server_connection_mode_t +_anjay_connection_current_mode(anjay_connection_ref_t ref) { + anjay_server_connection_t *connection = _anjay_get_server_connection(ref); + if (_anjay_connection_internal_get_socket(connection)) { + return connection->mode; + } else { + return ANJAY_CONNECTION_DISABLED; + } +} + +anjay_conn_session_token_t +_anjay_server_primary_session_token(anjay_server_info_t *server) { + return _anjay_connections_get_primary_session_token(&server->connections); +} + +int _anjay_active_server_refresh(anjay_t *anjay, anjay_server_info_t *server) { + anjay_log(TRACE, "refreshing SSID %u", server->ssid); + + anjay_iid_t security_iid; + if (_anjay_find_security_iid(anjay, server->ssid, &security_iid)) { + anjay_log(ERROR, "could not find server Security IID"); + return -1; + } + + anjay_binding_mode_t binding_mode; + if (server->ssid == ANJAY_SSID_BOOTSTRAP) { + if (avs_simple_snprintf(binding_mode, sizeof(binding_mode), "%s", + _anjay_sms_router(anjay) ? "US" : "U") + < 0) + return -1; + } else if (read_binding_mode(anjay, server->ssid, &binding_mode)) { + return -1; + } + + anjay_conn_session_token_t previous_session_token = + _anjay_server_primary_session_token(server); + + int result = _anjay_connections_refresh(anjay, + &server->connections, + security_iid, + &server->uri, + binding_mode); + + if (!result + && !_anjay_conn_session_tokens_equal( + previous_session_token, + _anjay_server_primary_session_token(server))) { + // The following causes _anjay_server_registration_expired() to return + // true. In conjunction with what is done in + // _anjay_connections_refresh(), this forces functions such as + // send_update_sched_job() or reload_active_server() to schedule + // reactivation of the server, and forces + // _anjay_server_ensure_valid_registration() to send Register. + server->registration_info.expire_time = AVS_TIME_REAL_INVALID; + } + return result; +} + +static void connection_suspend(anjay_connection_ref_t conn_ref) { + avs_net_abstract_socket_t *socket = _anjay_connection_internal_get_socket( + _anjay_get_server_connection(conn_ref)); + if (socket) { + avs_net_socket_close(socket); + } +} + +void _anjay_connection_suspend(anjay_connection_ref_t conn_ref) { + if (conn_ref.conn_type == ANJAY_CONNECTION_UNSET) { + ANJAY_CONNECTION_TYPE_FOREACH(conn_ref.conn_type) { + connection_suspend(conn_ref); + } + } else { + connection_suspend(conn_ref); + } +} + +int _anjay_connection_bring_online(anjay_t *anjay, anjay_connection_ref_t ref) { + return _anjay_connection_internal_bring_online( + anjay, _anjay_get_server_connection(ref)); +} + +static void queue_mode_close_socket(anjay_t *anjay, const void *ref_ptr) { + (void) anjay; + _anjay_connection_suspend(*(const anjay_connection_ref_t *) ref_ptr); +} + +void _anjay_connection_schedule_queue_mode_close(anjay_t *anjay, + anjay_connection_ref_t ref) { + anjay_server_connection_t *connection = _anjay_get_server_connection(ref); + assert(connection); + _anjay_sched_del(anjay->sched, + &connection->queue_mode_close_socket_clb_handle); + if (connection->mode != ANJAY_CONNECTION_QUEUE) { + return; + } + + avs_time_duration_t delay = avs_coap_max_transmit_wait( + _anjay_tx_params_for_conn_type(anjay, ref.conn_type)); + + // see comment on field declaration for logic summary + if (_anjay_sched(anjay->sched, + &connection->queue_mode_close_socket_clb_handle, delay, + queue_mode_close_socket, &ref, sizeof(ref))) { + anjay_log(ERROR, "could not schedule queue mode operations"); + } +} diff --git a/src/servers/server_connections.h b/src/servers/server_connections.h new file mode 100644 index 000000000..2e102a8cd --- /dev/null +++ b/src/servers/server_connections.h @@ -0,0 +1,46 @@ +/* + * Copyright 2017-2018 AVSystem + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANJAY_SERVERS_SERVER_CONNECTIONS_H +#define ANJAY_SERVERS_SERVER_CONNECTIONS_H + +#include "../anjay_core.h" +#include "../utils_core.h" + +#if !defined(ANJAY_SERVERS_INTERNALS) && !defined(ANJAY_TEST) +# error "Headers from servers/ are not meant to be included from outside" +#endif + +#include "connections.h" +#include "servers_internal.h" + +VISIBILITY_PRIVATE_HEADER_BEGIN + +static inline anjay_server_connection_t * +_anjay_get_server_connection(anjay_connection_ref_t ref) { + return _anjay_connection_get(&ref.server->connections, ref.conn_type); +} + +/** + * @returns @li 0 on success, + * @li a positive errno value in case of a primary socket (UDP) error, + * @li a negative value in case of other error. + */ +int _anjay_active_server_refresh(anjay_t *anjay, anjay_server_info_t *server); + +VISIBILITY_PRIVATE_HEADER_END + +#endif // ANJAY_SERVERS_SERVER_CONNECTIONS_H diff --git a/src/servers/servers_internal.c b/src/servers/servers_internal.c index fbaa37436..e22e45284 100644 --- a/src/servers/servers_internal.c +++ b/src/servers/servers_internal.c @@ -25,15 +25,15 @@ #define ANJAY_SERVERS_INTERNALS -#include "../dm/query.h" #include "../anjay_core.h" +#include "../dm/query.h" +#include "../interface/register.h" #include "../servers.h" #include "../servers_utils.h" -#include "../interface/register.h" #include "activate.h" -#include "connection_info.h" #include "register_internal.h" +#include "server_connections.h" #include "servers_internal.h" VISIBILITY_SOURCE_BEGIN @@ -45,8 +45,8 @@ int _anjay_servers_schedule_first_retryable(anjay_sched_t *sched, anjay_ssid_t ssid) { assert(!server->next_action_handle); server->next_retry_delay = avs_time_duration_from_scalar(1, AVS_TIME_S); - return _anjay_sched(sched, &server->next_action_handle, delay, - clb, &ssid, sizeof(ssid)); + return _anjay_sched(sched, &server->next_action_handle, delay, clb, &ssid, + sizeof(ssid)); } int _anjay_servers_schedule_next_retryable(anjay_sched_t *sched, @@ -56,9 +56,9 @@ int _anjay_servers_schedule_next_retryable(anjay_sched_t *sched, static const avs_time_duration_t MAX_BACKOFF = { 120, 0 }; assert(!server->next_action_handle); - int result = _anjay_sched(sched, &server->next_action_handle, - server->next_retry_delay, - clb, &ssid, sizeof(ssid)); + int result = + _anjay_sched(sched, &server->next_action_handle, + server->next_retry_delay, clb, &ssid, sizeof(ssid)); if (result) { return result; } @@ -70,25 +70,18 @@ int _anjay_servers_schedule_next_retryable(anjay_sched_t *sched, return 0; } -static void connection_cleanup(const anjay_t *anjay, - anjay_server_connection_t *connection) { - _anjay_connection_internal_clean_socket(connection); - _anjay_sched_del(anjay->sched, - &connection->queue_mode_close_socket_clb_handle); -} - void _anjay_server_clean_active_data(const anjay_t *anjay, anjay_server_info_t *server) { _anjay_sched_del(anjay->sched, &server->next_action_handle); - connection_cleanup(anjay, &server->data_active.udp_connection); + _anjay_connections_close(anjay, &server->connections); } void _anjay_server_cleanup(const anjay_t *anjay, anjay_server_info_t *server) { anjay_log(TRACE, "clear_server SSID %u", server->ssid); _anjay_server_clean_active_data(anjay, server); - _anjay_registration_info_cleanup(&server->data_active.registration_info); - _anjay_url_cleanup(&server->data_active.uri); + _anjay_registration_info_cleanup(&server->registration_info); + _anjay_url_cleanup(&server->uri); } anjay_servers_t *_anjay_servers_create(void) { @@ -106,8 +99,7 @@ void _anjay_servers_internal_deregister(anjay_t *anjay, } } -void _anjay_servers_internal_cleanup(anjay_t *anjay, - anjay_servers_t *servers) { +void _anjay_servers_internal_cleanup(anjay_t *anjay, anjay_servers_t *servers) { anjay_log(TRACE, "cleaning up %lu servers", (unsigned long) AVS_LIST_SIZE(servers->servers)); @@ -146,7 +138,7 @@ void _anjay_servers_cleanup_inactive(anjay_t *anjay) { avs_net_abstract_socket_t * _anjay_connection_get_online_socket(anjay_connection_ref_t ref) { anjay_server_connection_t *connection = _anjay_get_server_connection(ref); - if (!connection || !_anjay_connection_is_online(connection)) { + if (!_anjay_connection_is_online(connection)) { return NULL; } return _anjay_connection_internal_get_socket(connection); @@ -196,12 +188,11 @@ AVS_LIST(const anjay_socket_entry_t) anjay_get_socket_entries(anjay_t *anjay) { assert(udp_connection); avs_net_abstract_socket_t *udp_socket = _anjay_connection_internal_get_socket(udp_connection); - if (udp_socket - && _anjay_connection_is_online(udp_connection) + if (udp_socket && _anjay_connection_is_online(udp_connection) && !add_socket_onto_list( - tail_ptr, udp_socket, ANJAY_SOCKET_TRANSPORT_UDP, - ref.server->ssid, - udp_connection->mode == ANJAY_CONNECTION_QUEUE)) { + tail_ptr, udp_socket, ANJAY_SOCKET_TRANSPORT_UDP, + ref.server->ssid, + udp_connection->mode == ANJAY_CONNECTION_QUEUE)) { AVS_LIST_ADVANCE_PTR(&tail_ptr); } @@ -243,8 +234,8 @@ _anjay_servers_find_insert_ptr(anjay_servers_t *servers, anjay_ssid_t ssid) { return it; } -AVS_LIST(anjay_server_info_t) * -_anjay_servers_find_ptr(anjay_servers_t *servers, anjay_ssid_t ssid) { +AVS_LIST(anjay_server_info_t) *_anjay_servers_find_ptr(anjay_servers_t *servers, + anjay_ssid_t ssid) { AVS_LIST(anjay_server_info_t) *ptr = _anjay_servers_find_insert_ptr(servers, ssid); if (*ptr && (*ptr)->ssid == ssid) { @@ -260,8 +251,10 @@ static void disable_server_job(anjay_t *anjay, const void *ssid_ptr) { anjay_iid_t server_iid; if (_anjay_find_server_iid(anjay, ssid, &server_iid)) { - anjay_log(DEBUG, "no Server Object Instance with SSID = %u, disabling " - "skipped", ssid); + anjay_log(DEBUG, + "no Server Object Instance with SSID = %u, disabling " + "skipped", + ssid); } else { const avs_time_duration_t disable_timeout = _anjay_disable_timeout_from_server_iid(anjay, server_iid); @@ -276,8 +269,8 @@ static void disable_server_job(anjay_t *anjay, const void *ssid_ptr) { * does the deactivation procedure work. */ int anjay_disable_server(anjay_t *anjay, anjay_ssid_t ssid) { - if (_anjay_sched_now(anjay->sched, NULL, - disable_server_job, &ssid, sizeof(ssid))) { + if (_anjay_sched_now(anjay->sched, NULL, disable_server_job, &ssid, + sizeof(ssid))) { anjay_log(ERROR, "could not schedule disable_server_job"); return -1; } @@ -297,9 +290,11 @@ static void disable_server_with_timeout_job(anjay_t *anjay, anjay_log(ERROR, "unable to deactivate server: %" PRIu16, data->ssid); } else { if (avs_time_duration_valid(data->timeout)) { - anjay_log(INFO, "server %" PRIu16 " disabled for %" PRId64 - ".%09" PRId32 " seconds", data->ssid, - data->timeout.seconds, data->timeout.nanoseconds); + anjay_log(INFO, + "server %" PRIu16 " disabled for %" PRId64 ".%09" PRId32 + " seconds", + data->ssid, data->timeout.seconds, + data->timeout.nanoseconds); } else { anjay_log(INFO, "server %" PRIu16 " disabled", data->ssid); } @@ -346,8 +341,7 @@ int anjay_disable_server_with_timeout(anjay_t *anjay, * Server, Client-Initiated Bootstrap is not supposed to be performed, and * Server-Initiated Bootstrap is administratively disabled. */ -int anjay_enable_server(anjay_t *anjay, - anjay_ssid_t ssid) { +int anjay_enable_server(anjay_t *anjay, anjay_ssid_t ssid) { if (ssid == ANJAY_SSID_ANY) { anjay_log(WARNING, "invalid SSID: %u", ssid); return -1; @@ -378,12 +372,9 @@ bool _anjay_server_active(anjay_server_info_t *server) { anjay_connection_ref_t ref = { .server = server }; - for (ref.conn_type = ANJAY_CONNECTION_FIRST_VALID_; - ref.conn_type < ANJAY_CONNECTION_LIMIT_; - ref.conn_type = (anjay_connection_type_t) (ref.conn_type + 1)) { - anjay_server_connection_t *connection = - _anjay_get_server_connection(ref); - if (connection && _anjay_connection_internal_get_socket(connection)) { + ANJAY_CONNECTION_TYPE_FOREACH(ref.conn_type) { + if (_anjay_connection_internal_get_socket( + _anjay_get_server_connection(ref))) { return true; } } @@ -391,27 +382,17 @@ bool _anjay_server_active(anjay_server_info_t *server) { return false; } -anjay_server_connection_t * -_anjay_get_server_connection(anjay_connection_ref_t ref) { - switch (ref.conn_type) { - case ANJAY_CONNECTION_UDP: - return &ref.server->data_active.udp_connection; - default: - return NULL; - } -} - anjay_ssid_t _anjay_server_ssid(anjay_server_info_t *server) { return server->ssid; } anjay_connection_type_t _anjay_server_primary_conn_type(anjay_server_info_t *server) { - return server->data_active.primary_conn_type; + return _anjay_connections_get_primary(&server->connections); } const anjay_url_t *_anjay_server_uri(anjay_server_info_t *server) { - return &server->data_active.uri; + return &server->uri; } int _anjay_servers_foreach_ssid(anjay_t *anjay, diff --git a/src/servers/servers_internal.h b/src/servers/servers_internal.h index a128500b7..e5c270702 100644 --- a/src/servers/servers_internal.h +++ b/src/servers/servers_internal.h @@ -21,10 +21,10 @@ #include "../servers.h" -#include "connection_info.h" +#include "connections.h" #if !defined(ANJAY_SERVERS_INTERNALS) && !defined(ANJAY_TEST) -#error "Headers from servers/ are not meant to be included from outside" +# error "Headers from servers/ are not meant to be included from outside" #endif VISIBILITY_PRIVATE_HEADER_BEGIN @@ -107,86 +107,70 @@ struct anjay_server_info_struct { avs_time_duration_t next_retry_delay; /** - * The fields in data_active substruct are valid only for active servers... - * except they are not. They are also used for determining whether the - * server is active or not (as sockets are stored within - * anjay_server_connection_t objects, see the main docstring for + * Cached URI of the given server - this is exactly the value returned by + * _anjay_server_uri(). + */ + anjay_url_t uri; + + /** + * State of all connections to remote servers possible for a given server. + * The anjay_connections_t type wraps the actual server connections, + * information about which is currently the "primary" one, and manages the + * connection state flow. + * + * This object is also used for determining whether the server is active or + * not (as sockets are stored inside, see the main docstring for * anjay_server_info_t for details), and also holds non-transient data that * is of no use when the server is inactive, but is preserved between * activation attempts (so that session resumption works across * activations). */ - struct { - /** - * Cached URI of the given server - this is exactly the value returned - * by _anjay_server_uri(). - */ - anjay_url_t uri; - - /** - * Connection (socket, binding) entries - see docs to - * anjay_server_connection_t in connection_info.h for details. - */ - anjay_server_connection_t udp_connection; - - /** - * Information about which connection is currently the "primary" one. - * The "primary" connection is the one on which the autonomous outgoing - * messages (i.e. Register/Update or Bootstrap Request) are sent. See - * the docs in server.h for details (Ctrl+F the word "primary"). - */ - anjay_connection_type_t primary_conn_type; - - /** - * Information about current registration status of the server. See the - * docs for _anjay_server_registration_info() and - * _anjay_server_update_registration_info() for details. - */ - anjay_registration_info_t registration_info; - } data_active; + anjay_connections_t connections; + + /** + * Information about current registration status of the server. See the + * docs for _anjay_server_registration_info() and + * _anjay_server_update_registration_info() for details. + */ + anjay_registration_info_t registration_info; + + /** + * When a reactivate job is scheduled (and its handle stored in + * next_action_handle), this field is filled with the time for which the + * reactivate job is (initially) scheduled. If Anjay enters offline mode, we + * delete all such jobs (because we don't want servers to be activated + * during offline mode) - but thanks to this value, we can reschedule + * activation at appropriate time after exiting offline mode. + * + * This logic has been first introduced in internal diff D7056, which + * limited the number of places in code where Registers and Updates may + * happen, to deliver more consistent behaviour of those. Previously, + * enter_offline_job() did not completely deactivate the servers, but just + * suspended (closed) their sockets, and + * _anjay_server_ensure_valid_registration() was called directly from + * reload_active_server() (as the servers exiting from offline modes were + * considered active). This yielded inconsistent behaviour of Update error + * handling - Updates generated in this way were not degenerating to + * Registers immediately. + */ + avs_time_real_t reactivate_time; + + /** + * True if, and only if, the last activation attempt was unsuccessful, for + * whatever reason - not necessarily those included in num_icmp_failures + * logic. + */ + bool reactivate_failed; /** - * The fields in data_inactive substruct are valid only for inactive servers + * Counter that is increased in case of some kind of ICMP Unreachable + * message received while trying to communicate with the server. + * + * Its value also skips to anjay->max_icmp_failures in case of a 4.03 + * Forbidden CoAP response to Register, a network timeout, or a DTLS + * handshake error. */ - struct { - /** - * When a reactivate job is scheduled (and its handle stored in - * next_action_handle), this field is filled with the time for which the - * reactivate job is (initially) scheduled. If Anjay enters offline - * mode, we delete all such jobs (because we don't want servers to be - * activated during offline mode) - but thanks to this value, we can - * reschedule activation at appropriate time after exiting offline mode. - * - * This logic has been first introduced in internal diff D7056, which - * limited the number of places in code where Registers and Updates may - * happen, to deliver more consistent behaviour of those. Previously, - * enter_offline_job() did not completely deactivate the servers, but - * just suspended (closed) their sockets, and - * _anjay_server_ensure_valid_registration() was called directly from - * reload_active_server() (as the servers exiting from offline modes - * were considered active). This yielded inconsistent behaviour of - * Update error handling - Updates generated in this way were not - * degenerating to Registers immediately. - */ - avs_time_real_t reactivate_time; - - /** - * True if, and only if, the last activation attempt was unsuccessful, - * for whatever reason - not necessarily those included in - * num_icmp_failures logic. - */ - bool reactivate_failed; - - /** - * Counter that is increased in case of some kind of ICMP Unreachable - * message received while trying to communicate with the server. - * - * Its value also skips to anjay->max_icmp_failures in case of a 4.03 - * Forbidden CoAP response to Register, a network timeout, or a DTLS - * handshake error. - */ - uint32_t num_icmp_failures; - } data_inactive; + uint32_t num_icmp_failures; }; /** @@ -222,8 +206,7 @@ int _anjay_servers_schedule_next_retryable(anjay_sched_t *sched, void _anjay_servers_internal_deregister(anjay_t *anjay, anjay_servers_t *servers); -void _anjay_servers_internal_cleanup(anjay_t *anjay, - anjay_servers_t *servers); +void _anjay_servers_internal_cleanup(anjay_t *anjay, anjay_servers_t *servers); void _anjay_server_clean_active_data(const anjay_t *anjay, anjay_server_info_t *server); @@ -238,8 +221,8 @@ bool _anjay_server_active(anjay_server_info_t *server); AVS_LIST(anjay_server_info_t) * _anjay_servers_find_insert_ptr(anjay_servers_t *servers, anjay_ssid_t ssid); -AVS_LIST(anjay_server_info_t) * -_anjay_servers_find_ptr(anjay_servers_t *servers, anjay_ssid_t ssid); +AVS_LIST(anjay_server_info_t) *_anjay_servers_find_ptr(anjay_servers_t *servers, + anjay_ssid_t ssid); VISIBILITY_PRIVATE_HEADER_END diff --git a/src/servers_utils.c b/src/servers_utils.c index d99b3c0c2..280b5bcde 100644 --- a/src/servers_utils.c +++ b/src/servers_utils.c @@ -69,9 +69,8 @@ typedef struct { anjay_server_info_t *out; } find_active_args_t; -static int find_active_clb(anjay_t *anjay, - anjay_server_info_t *server, - void *args_) { +static int +find_active_clb(anjay_t *anjay, anjay_server_info_t *server, void *args_) { (void) anjay; find_active_args_t *args = (find_active_args_t *) args_; if (_anjay_server_ssid(server) == args->ssid) { @@ -103,15 +102,16 @@ bool _anjay_server_registration_expired(anjay_server_info_t *server) { // the direction of this comparison is chosen so that it causes the // registration to be considered expired if (!avs_time_duration_less(AVS_TIME_DURATION_ZERO, remaining)) { - anjay_log(DEBUG, "Registration Lifetime expired for SSID = %u, " - "forcing re-register", _anjay_server_ssid(server)); + anjay_log(DEBUG, + "Registration Lifetime expired for SSID = %u, " + "forcing re-register", + _anjay_server_ssid(server)); return true; } return false; } -int _anjay_schedule_socket_update(anjay_t *anjay, - anjay_iid_t security_iid) { +int _anjay_schedule_socket_update(anjay_t *anjay, anjay_iid_t security_iid) { anjay_ssid_t ssid; anjay_server_info_t *server; if (!_anjay_ssid_from_security_iid(anjay, security_iid, &ssid) @@ -138,21 +138,15 @@ AVS_LIST(avs_net_abstract_socket_t *const) anjay_get_sockets(anjay_t *anjay) { return &anjay_get_socket_entries(anjay)->socket; } +static const char CONN_TYPE_LETTERS[] = { + [ANJAY_CONNECTION_UDP] = 'U' +}; + anjay_server_connection_mode_t _anjay_get_connection_mode(const char *binding_mode, anjay_connection_type_t conn_type) { - char type_letter; - switch (conn_type) { - case ANJAY_CONNECTION_UDP: - type_letter = 'U'; - break; - case ANJAY_CONNECTION_SMS: - type_letter = 'S'; - break; - default: - return ANJAY_CONNECTION_DISABLED; - } - const char *type_letter_ptr = strchr(binding_mode, type_letter); + const char *type_letter_ptr = + strchr(binding_mode, CONN_TYPE_LETTERS[conn_type]); if (!type_letter_ptr) { return ANJAY_CONNECTION_DISABLED; } @@ -163,71 +157,25 @@ _anjay_get_connection_mode(const char *binding_mode, } } -static inline int append_str(char **ptr, const char *endptr, const char *str) { - int result = avs_simple_snprintf(*ptr, (size_t) (endptr - *ptr), "%s", str); - if (result >= 0) { - *ptr += result; - return 0; - } - return result; -} +void _anjay_server_actual_binding_mode(anjay_binding_mode_t *out_binding_mode, + anjay_server_info_t *server) { + AVS_STATIC_ASSERT(sizeof(*out_binding_mode) + > (sizeof("xQ") - 1) * ANJAY_CONNECTION_LIMIT_, + anjay_binding_mode_t_size); -static int -binding_mode_from_connection_modes(anjay_binding_mode_t *out_binding_mode, - anjay_server_connection_mode_t udp_mode, - anjay_server_connection_mode_t sms_mode) { - memset(*out_binding_mode, 0, sizeof(*out_binding_mode)); char *ptr = *out_binding_mode; - const char *endptr = ptr + sizeof(*out_binding_mode); - switch (udp_mode) { - case ANJAY_CONNECTION_ONLINE: - if (append_str(&ptr, endptr, "U")) { - return -1; - } - break; - case ANJAY_CONNECTION_QUEUE: - if (append_str(&ptr, endptr, "UQ")) { - return -1; - } - break; - case ANJAY_CONNECTION_DISABLED: - default: - break; - } - switch (sms_mode) { - case ANJAY_CONNECTION_ONLINE: - if (append_str(&ptr, endptr, "S")) { - return -1; - } - break; - case ANJAY_CONNECTION_QUEUE: - if (append_str(&ptr, endptr, "SQ")) { - return -1; + anjay_connection_ref_t ref = { + .server = server + }; + ANJAY_CONNECTION_TYPE_FOREACH(ref.conn_type) { + anjay_server_connection_mode_t mode = + _anjay_connection_current_mode(ref); + if (mode != ANJAY_CONNECTION_DISABLED) { + *ptr++ = CONN_TYPE_LETTERS[ref.conn_type]; + if (mode == ANJAY_CONNECTION_QUEUE) { + *ptr++ = 'Q'; + } } - break; - case ANJAY_CONNECTION_DISABLED: - default: - break; - } - return anjay_binding_mode_valid(*out_binding_mode) ? 0 : -1; -} - -int -_anjay_server_actual_binding_mode(anjay_binding_mode_t *out_binding_mode, - anjay_server_info_t *server) { - if (!server) { - return -1; } - anjay_server_connection_mode_t udp_mode = - _anjay_connection_current_mode((anjay_connection_ref_t) { - .server = server, - .conn_type = ANJAY_CONNECTION_UDP - }); - anjay_server_connection_mode_t sms_mode = - _anjay_connection_current_mode((anjay_connection_ref_t) { - .server = server, - .conn_type = ANJAY_CONNECTION_SMS - }); - return binding_mode_from_connection_modes(out_binding_mode, - udp_mode, sms_mode); + *ptr = '\0'; } diff --git a/src/servers_utils.h b/src/servers_utils.h index 4c15bb7fb..4f8f7a7e8 100644 --- a/src/servers_utils.h +++ b/src/servers_utils.h @@ -39,8 +39,7 @@ anjay_server_info_t *_anjay_servers_find_active(anjay_t *anjay, bool _anjay_server_registration_expired(anjay_server_info_t *server); -int _anjay_schedule_socket_update(anjay_t *anjay, - anjay_iid_t security_iid); +int _anjay_schedule_socket_update(anjay_t *anjay, anjay_iid_t security_iid); /** * Determines the connection mode (offline, online or queue-mode) for a specific @@ -58,9 +57,8 @@ _anjay_get_connection_mode(const char *binding_mode, * be US, but the UDP connection failed and is not available - "S" * is returned. */ -int -_anjay_server_actual_binding_mode(anjay_binding_mode_t *out_binding_mode, - anjay_server_info_t *server); +void _anjay_server_actual_binding_mode(anjay_binding_mode_t *out_binding_mode, + anjay_server_info_t *server); VISIBILITY_PRIVATE_HEADER_END diff --git a/src/test/anjay.c b/src/test/anjay.c index 1f4b8bef2..bd5c76ec8 100644 --- a/src/test/anjay.c +++ b/src/test/anjay.c @@ -25,7 +25,7 @@ #include #include "../coap/test/utils.h" -#include "../servers/servers_internal.h" +#include "../servers/server_connections.h" AVS_UNIT_GLOBAL_INIT(verbose) { #ifdef WITH_AVS_LOG @@ -35,24 +35,24 @@ AVS_UNIT_GLOBAL_INIT(verbose) { #endif } -#define TEST_NULLABLE_STRING_EQUAL(Actual, Expected) \ - do { \ - if (Expected != NULL) { \ - AVS_UNIT_ASSERT_NOT_NULL((Actual)); \ +#define TEST_NULLABLE_STRING_EQUAL(Actual, Expected) \ + do { \ + if (Expected != NULL) { \ + AVS_UNIT_ASSERT_NOT_NULL((Actual)); \ AVS_UNIT_ASSERT_EQUAL_STRING((Actual), (Expected)); \ - } else { \ - AVS_UNIT_ASSERT_NULL((Actual)); \ - } \ + } else { \ + AVS_UNIT_ASSERT_NULL((Actual)); \ + } \ } while (0) #define TEST_SPLIT_QUERY_STRING(QueryString, ExpectedKey, ExpectedValue) \ - do { \ - char buf[] = QueryString; \ - const char *key; \ - const char *value; \ - split_query_string(buf, &key, &value); \ - TEST_NULLABLE_STRING_EQUAL(key, ExpectedKey); \ - TEST_NULLABLE_STRING_EQUAL(value, ExpectedValue); \ + do { \ + char buf[] = QueryString; \ + const char *key; \ + const char *value; \ + split_query_string(buf, &key, &value); \ + TEST_NULLABLE_STRING_EQUAL(key, ExpectedKey); \ + TEST_NULLABLE_STRING_EQUAL(value, ExpectedValue); \ } while (0) AVS_UNIT_TEST(parse_headers, split_query_string) { @@ -66,25 +66,25 @@ AVS_UNIT_TEST(parse_headers, split_query_string) { #undef TEST_SPLIT_QUERY_STRING #undef TEST_NULLABLE_STRING_EQUAL -#define TEST_PARSE_ATTRIBUTE_SUCCESS(Key, Value, ExpectedField, \ - ExpectedHasField, ExpectedValue) \ - do { \ - anjay_request_attributes_t attrs; \ - memset(&attrs, 0, sizeof(attrs)); \ - AVS_UNIT_ASSERT_SUCCESS(parse_attribute(&attrs, (Key), (Value))); \ - AVS_UNIT_ASSERT_EQUAL(attrs.values.ExpectedField, (ExpectedValue)); \ - anjay_request_attributes_t expected; \ - memset(&expected, 0, sizeof(expected)); \ - expected.ExpectedHasField = true; \ - expected.values.ExpectedField = (ExpectedValue); \ - AVS_UNIT_ASSERT_EQUAL_BYTES_SIZED(&attrs, &expected, \ +#define TEST_PARSE_ATTRIBUTE_SUCCESS(Key, Value, ExpectedField, \ + ExpectedHasField, ExpectedValue) \ + do { \ + anjay_request_attributes_t attrs; \ + memset(&attrs, 0, sizeof(attrs)); \ + AVS_UNIT_ASSERT_SUCCESS(parse_attribute(&attrs, (Key), (Value))); \ + AVS_UNIT_ASSERT_EQUAL(attrs.values.ExpectedField, (ExpectedValue)); \ + anjay_request_attributes_t expected; \ + memset(&expected, 0, sizeof(expected)); \ + expected.ExpectedHasField = true; \ + expected.values.ExpectedField = (ExpectedValue); \ + AVS_UNIT_ASSERT_EQUAL_BYTES_SIZED(&attrs, &expected, \ sizeof(anjay_request_attributes_t)); \ } while (0) -#define TEST_PARSE_ATTRIBUTE_FAIL(Key, Value) \ - do { \ - anjay_request_attributes_t attrs; \ - memset(&attrs, 0, sizeof(attrs)); \ +#define TEST_PARSE_ATTRIBUTE_FAIL(Key, Value) \ + do { \ + anjay_request_attributes_t attrs; \ + memset(&attrs, 0, sizeof(attrs)); \ AVS_UNIT_ASSERT_FAILED(parse_attribute(&attrs, (Key), (Value))); \ } while (0); @@ -114,18 +114,18 @@ AVS_UNIT_TEST(parse_headers, parse_attribute) { TEST_PARSE_ATTRIBUTE_FAIL("gt", "tweet"); TEST_PARSE_ATTRIBUTE_FAIL("gt", ""); - TEST_PARSE_ATTRIBUTE_SUCCESS("lt", "456", - standard.less_than, has_less_than, 456.0); - TEST_PARSE_ATTRIBUTE_SUCCESS("lt", "456.7", - standard.less_than, has_less_than, 456.7); - TEST_PARSE_ATTRIBUTE_SUCCESS("lt", NULL, standard.less_than, - has_less_than, NAN); + TEST_PARSE_ATTRIBUTE_SUCCESS("lt", "456", standard.less_than, has_less_than, + 456.0); + TEST_PARSE_ATTRIBUTE_SUCCESS("lt", "456.7", standard.less_than, + has_less_than, 456.7); + TEST_PARSE_ATTRIBUTE_SUCCESS("lt", NULL, standard.less_than, has_less_than, + NAN); TEST_PARSE_ATTRIBUTE_FAIL("lt", "squeak"); TEST_PARSE_ATTRIBUTE_FAIL("lt", ""); - TEST_PARSE_ATTRIBUTE_SUCCESS("st", "567", standard.step, has_step, 567.0); + TEST_PARSE_ATTRIBUTE_SUCCESS("st", "567", standard.step, has_step, 567.0); TEST_PARSE_ATTRIBUTE_SUCCESS("st", "567.8", standard.step, has_step, 567.8); - TEST_PARSE_ATTRIBUTE_SUCCESS("st", NULL, standard.step, has_step, NAN); + TEST_PARSE_ATTRIBUTE_SUCCESS("st", NULL, standard.step, has_step, NAN); TEST_PARSE_ATTRIBUTE_FAIL("st", "moo"); TEST_PARSE_ATTRIBUTE_FAIL("st", ""); @@ -138,43 +138,43 @@ AVS_UNIT_TEST(parse_headers, parse_attribute) { #undef TEST_PARSE_ATTRIBUTE_FAILED #ifdef WITH_CUSTOM_ATTRIBUTES -#define ASSERT_CUSTOM_ATTRIBUTE_VALUES_EQUAL(actual, expected) \ - AVS_UNIT_ASSERT_EQUAL(actual.custom.data.con, expected.custom.data.con) +# define ASSERT_CUSTOM_ATTRIBUTE_VALUES_EQUAL(actual, expected) \ + AVS_UNIT_ASSERT_EQUAL(actual.custom.data.con, expected.custom.data.con) #else // WITH_CUSTOM_ATTRIBUTES -#define ASSERT_CUSTOM_ATTRIBUTE_VALUES_EQUAL(actual, expected) ((void) 0) +# define ASSERT_CUSTOM_ATTRIBUTE_VALUES_EQUAL(actual, expected) ((void) 0) #endif // WITH_CUSTOM_ATTRIBUTES -#define ASSERT_ATTRIBUTE_VALUES_EQUAL(actual, expected) \ - do { \ - ASSERT_CUSTOM_ATTRIBUTE_VALUES_EQUAL(actual, expected); \ - AVS_UNIT_ASSERT_EQUAL(actual.standard.common.min_period, \ - expected.standard.common.min_period); \ - AVS_UNIT_ASSERT_EQUAL(actual.standard.common.max_period, \ - expected.standard.common.max_period); \ - AVS_UNIT_ASSERT_EQUAL(actual.standard.greater_than, \ - expected.standard.greater_than); \ - AVS_UNIT_ASSERT_EQUAL(actual.standard.less_than, \ - expected.standard.less_than); \ +#define ASSERT_ATTRIBUTE_VALUES_EQUAL(actual, expected) \ + do { \ + ASSERT_CUSTOM_ATTRIBUTE_VALUES_EQUAL(actual, expected); \ + AVS_UNIT_ASSERT_EQUAL(actual.standard.common.min_period, \ + expected.standard.common.min_period); \ + AVS_UNIT_ASSERT_EQUAL(actual.standard.common.max_period, \ + expected.standard.common.max_period); \ + AVS_UNIT_ASSERT_EQUAL(actual.standard.greater_than, \ + expected.standard.greater_than); \ + AVS_UNIT_ASSERT_EQUAL(actual.standard.less_than, \ + expected.standard.less_than); \ AVS_UNIT_ASSERT_EQUAL(actual.standard.step, expected.standard.step); \ } while (0) #ifdef WITH_CUSTOM_ATTRIBUTES -#define ASSERT_CUSTOM_ATTRIBUTE_FLAGS_EQUAL(actual, expected) \ - AVS_UNIT_ASSERT_EQUAL(actual.custom.has_con, expected.custom.has_con) +# define ASSERT_CUSTOM_ATTRIBUTE_FLAGS_EQUAL(actual, expected) \ + AVS_UNIT_ASSERT_EQUAL(actual.custom.has_con, expected.custom.has_con) #else // WITH_CUSTOM_ATTRIBUTES -#define ASSERT_CUSTOM_ATTRIBUTE_FLAGS_EQUAL(actual, expected) ((void) 0) +# define ASSERT_CUSTOM_ATTRIBUTE_FLAGS_EQUAL(actual, expected) ((void) 0) #endif // WITH_CUSTOM_ATTRIBUTES -#define ASSERT_ATTRIBUTES_EQUAL(actual, expected) \ - do { \ +#define ASSERT_ATTRIBUTES_EQUAL(actual, expected) \ + do { \ AVS_UNIT_ASSERT_EQUAL(actual.has_min_period, expected.has_min_period); \ AVS_UNIT_ASSERT_EQUAL(actual.has_max_period, expected.has_max_period); \ - AVS_UNIT_ASSERT_EQUAL(actual.has_greater_than, \ - expected.has_greater_than); \ - AVS_UNIT_ASSERT_EQUAL(actual.has_less_than, expected.has_less_than); \ - AVS_UNIT_ASSERT_EQUAL(actual.has_step, expected.has_step); \ - ASSERT_CUSTOM_ATTRIBUTE_FLAGS_EQUAL(actual, expected); \ - ASSERT_ATTRIBUTE_VALUES_EQUAL(actual.values, expected.values); \ + AVS_UNIT_ASSERT_EQUAL(actual.has_greater_than, \ + expected.has_greater_than); \ + AVS_UNIT_ASSERT_EQUAL(actual.has_less_than, expected.has_less_than); \ + AVS_UNIT_ASSERT_EQUAL(actual.has_step, expected.has_step); \ + ASSERT_CUSTOM_ATTRIBUTE_FLAGS_EQUAL(actual, expected); \ + ASSERT_ATTRIBUTE_VALUES_EQUAL(actual.values, expected.values); \ } while (0) AVS_UNIT_TEST(parse_headers, parse_attributes) { @@ -185,8 +185,8 @@ AVS_UNIT_TEST(parse_headers, parse_attributes) { anjay_request_attributes_t expected_attrs; // no query-strings - AVS_UNIT_ASSERT_SUCCESS(parse_attributes( - COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &attrs)); + AVS_UNIT_ASSERT_SUCCESS( + parse_attributes(COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &attrs)); AVS_UNIT_ASSERT_EQUAL_BYTES_SIZED(&attrs, &empty_attrs, sizeof(attrs)); // single query-string @@ -221,28 +221,27 @@ AVS_UNIT_TEST(parse_headers, parse_attributes) { // unrecognized query-string first AVS_UNIT_ASSERT_FAILED(parse_attributes( - COAP_MSG(CON, GET, ID(0), QUERY("WhyDidTheyBuildThe=Stonehenge", - "pmax=20")), + COAP_MSG(CON, GET, ID(0), + QUERY("WhyDidTheyBuildThe=Stonehenge", "pmax=20")), &attrs)); // unrecognized query-string last AVS_UNIT_ASSERT_FAILED(parse_attributes( - COAP_MSG(CON, GET, ID(0), QUERY("gt=30.5", - "AllICanThinkOfIsStonehenge")), + COAP_MSG(CON, GET, ID(0), + QUERY("gt=30.5", "AllICanThinkOfIsStonehenge")), &attrs)); // multiple unrecognized query-strings AVS_UNIT_ASSERT_FAILED(parse_attributes( - COAP_MSG(CON, GET, ID(0), QUERY("Stonehenge", "Stonehenge", - "LotsOfStonesInARow")), + COAP_MSG(CON, GET, ID(0), + QUERY("Stonehenge", "Stonehenge", "LotsOfStonesInARow")), &attrs)); // single query-string among multiple unrecognized ones AVS_UNIT_ASSERT_FAILED(parse_attributes( - COAP_MSG(CON, GET, ID(0), QUERY("TheyWere=25Tons", - "EachStoneMyFriend", "lt=40.5", - "ButAmazinglyThey", - "GotThemAllDownInTheSand")), + COAP_MSG(CON, GET, ID(0), + QUERY("TheyWere=25Tons", "EachStoneMyFriend", "lt=40.5", + "ButAmazinglyThey", "GotThemAllDownInTheSand")), &attrs)); // invalid query-string value @@ -267,70 +266,59 @@ AVS_UNIT_TEST(parse_headers, parse_uri) { AVS_UNIT_ASSERT_SUCCESS(parse_request_uri( COAP_MSG(CON, GET, ID(0), PATH("1")), &is_bs, &uri)); AVS_UNIT_ASSERT_FALSE(is_bs); - AVS_UNIT_ASSERT_TRUE(_anjay_uri_path_has_oid(&uri)); + AVS_UNIT_ASSERT_EQUAL(uri.type, ANJAY_PATH_OBJECT); AVS_UNIT_ASSERT_EQUAL(uri.oid, 1); - AVS_UNIT_ASSERT_FALSE(_anjay_uri_path_has_iid(&uri)); - AVS_UNIT_ASSERT_FALSE(_anjay_uri_path_has_rid(&uri)); // OID+IID AVS_UNIT_ASSERT_SUCCESS(parse_request_uri( COAP_MSG(CON, GET, ID(0), PATH("2", "3")), &is_bs, &uri)); AVS_UNIT_ASSERT_FALSE(is_bs); - AVS_UNIT_ASSERT_TRUE(_anjay_uri_path_has_oid(&uri)); + AVS_UNIT_ASSERT_EQUAL(uri.type, ANJAY_PATH_INSTANCE); AVS_UNIT_ASSERT_EQUAL(uri.oid, 2); - AVS_UNIT_ASSERT_TRUE(_anjay_uri_path_has_iid(&uri)); AVS_UNIT_ASSERT_EQUAL(uri.iid, 3); - AVS_UNIT_ASSERT_FALSE(_anjay_uri_path_has_rid(&uri)); // OID+IID+RID AVS_UNIT_ASSERT_SUCCESS(parse_request_uri( COAP_MSG(CON, GET, ID(0), PATH("4", "5", "6")), &is_bs, &uri)); AVS_UNIT_ASSERT_FALSE(is_bs); - AVS_UNIT_ASSERT_TRUE(_anjay_uri_path_has_oid(&uri)); + AVS_UNIT_ASSERT_EQUAL(uri.type, ANJAY_PATH_RESOURCE); AVS_UNIT_ASSERT_EQUAL(uri.oid, 4); - AVS_UNIT_ASSERT_TRUE(_anjay_uri_path_has_iid(&uri)); AVS_UNIT_ASSERT_EQUAL(uri.iid, 5); - AVS_UNIT_ASSERT_TRUE(_anjay_uri_path_has_rid(&uri)); AVS_UNIT_ASSERT_EQUAL(uri.rid, 6); // max valid OID/IID/RID AVS_UNIT_ASSERT_SUCCESS(parse_request_uri( - COAP_MSG(CON, GET, ID(0), PATH("65535", "65534", "65535")), - &is_bs, &uri)); + COAP_MSG(CON, GET, ID(0), PATH("65535", "65534", "65535")), &is_bs, + &uri)); AVS_UNIT_ASSERT_FALSE(is_bs); - AVS_UNIT_ASSERT_TRUE(_anjay_uri_path_has_oid(&uri)); + AVS_UNIT_ASSERT_EQUAL(uri.type, ANJAY_PATH_RESOURCE); AVS_UNIT_ASSERT_EQUAL(uri.oid, 65535); - AVS_UNIT_ASSERT_TRUE(_anjay_uri_path_has_iid(&uri)); AVS_UNIT_ASSERT_EQUAL(uri.iid, 65534); - AVS_UNIT_ASSERT_TRUE(_anjay_uri_path_has_rid(&uri)); AVS_UNIT_ASSERT_EQUAL(uri.rid, 65535); // Bootstrap URI AVS_UNIT_ASSERT_SUCCESS(parse_request_uri( COAP_MSG(CON, GET, ID(0), PATH("bs")), &is_bs, &uri)); AVS_UNIT_ASSERT_TRUE(is_bs); - AVS_UNIT_ASSERT_FALSE(_anjay_uri_path_has_oid(&uri)); - AVS_UNIT_ASSERT_FALSE(_anjay_uri_path_has_iid(&uri)); - AVS_UNIT_ASSERT_FALSE(_anjay_uri_path_has_rid(&uri)); + AVS_UNIT_ASSERT_EQUAL(uri.type, ANJAY_PATH_ROOT); // no Request-Uri AVS_UNIT_ASSERT_SUCCESS(parse_request_uri( COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &is_bs, &uri)); AVS_UNIT_ASSERT_FALSE(is_bs); - AVS_UNIT_ASSERT_FALSE(_anjay_uri_path_has_oid(&uri)); - AVS_UNIT_ASSERT_FALSE(_anjay_uri_path_has_iid(&uri)); - AVS_UNIT_ASSERT_FALSE(_anjay_uri_path_has_rid(&uri)); + AVS_UNIT_ASSERT_EQUAL(uri.type, ANJAY_PATH_ROOT); // prefix - AVS_UNIT_ASSERT_FAILED(parse_request_uri( - COAP_MSG(CON, GET, ID(0), PATH("they're taking the hobbits", - "to isengard", "7", "8", "9")), - &is_bs, &uri)); + AVS_UNIT_ASSERT_FAILED( + parse_request_uri(COAP_MSG(CON, GET, ID(0), + PATH("they're taking the hobbits", + "to isengard", "7", "8", "9")), + &is_bs, &uri)); // prefix that looks like OID + OID+IID+RID AVS_UNIT_ASSERT_FAILED(parse_request_uri( - COAP_MSG(CON, GET, ID(0), PATH("100", "10", "11", "12")), - &is_bs, &uri)); + COAP_MSG(CON, GET, ID(0), PATH("100", "10", "11", "12")), &is_bs, + &uri)); // prefix that looks like OID/IID/RID + string + OID only AVS_UNIT_ASSERT_FAILED(parse_request_uri( @@ -340,8 +328,8 @@ AVS_UNIT_TEST(parse_headers, parse_uri) { // trailing non-numeric segment AVS_UNIT_ASSERT_FAILED(parse_request_uri( - COAP_MSG(CON, GET, ID(0), PATH("14", "NopeChuckTesta")), - &is_bs, &uri)); + COAP_MSG(CON, GET, ID(0), PATH("14", "NopeChuckTesta")), &is_bs, + &uri)); // invalid OID AVS_UNIT_ASSERT_FAILED(parse_request_uri( @@ -353,8 +341,8 @@ AVS_UNIT_TEST(parse_headers, parse_uri) { // invalid RID AVS_UNIT_ASSERT_FAILED(parse_request_uri( - COAP_MSG(CON, GET, ID(0), PATH("16", "17", "65536")), - &is_bs, &uri)); + COAP_MSG(CON, GET, ID(0), PATH("16", "17", "65536")), &is_bs, + &uri)); // BS and something more AVS_UNIT_ASSERT_FAILED(parse_request_uri( @@ -368,8 +356,8 @@ AVS_UNIT_TEST(parse_headers, parse_action) { request.msg_type = AVS_COAP_MSG_CONFIRMABLE; request.request_code = AVS_COAP_CODE_GET; - AVS_UNIT_ASSERT_SUCCESS(parse_action( - COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); + AVS_UNIT_ASSERT_SUCCESS( + parse_action(COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); AVS_UNIT_ASSERT_EQUAL(request.action, ANJAY_ACTION_READ); request.msg_type = AVS_COAP_MSG_CONFIRMABLE; @@ -383,50 +371,50 @@ AVS_UNIT_TEST(parse_headers, parse_action) { request.msg_type = AVS_COAP_MSG_CONFIRMABLE; request.request_code = AVS_COAP_CODE_POST; request.uri.type = ANJAY_PATH_RESOURCE; - AVS_UNIT_ASSERT_SUCCESS(parse_action( - COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); + AVS_UNIT_ASSERT_SUCCESS( + parse_action(COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); AVS_UNIT_ASSERT_EQUAL(request.action, ANJAY_ACTION_EXECUTE); request.msg_type = AVS_COAP_MSG_CONFIRMABLE; request.request_code = AVS_COAP_CODE_POST; request.uri.type = ANJAY_PATH_OBJECT; request.content_format = ANJAY_COAP_FORMAT_PLAINTEXT; - AVS_UNIT_ASSERT_SUCCESS(parse_action( - COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); + AVS_UNIT_ASSERT_SUCCESS( + parse_action(COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); AVS_UNIT_ASSERT_EQUAL(request.action, ANJAY_ACTION_CREATE); request.msg_type = AVS_COAP_MSG_CONFIRMABLE; request.request_code = AVS_COAP_CODE_POST; request.uri.type = ANJAY_PATH_INSTANCE; request.content_format = ANJAY_COAP_FORMAT_TLV; - AVS_UNIT_ASSERT_SUCCESS(parse_action( - COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); + AVS_UNIT_ASSERT_SUCCESS( + parse_action(COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); AVS_UNIT_ASSERT_EQUAL(request.action, ANJAY_ACTION_WRITE_UPDATE); request.msg_type = AVS_COAP_MSG_CONFIRMABLE; request.request_code = AVS_COAP_CODE_PUT; request.content_format = AVS_COAP_FORMAT_NONE; - AVS_UNIT_ASSERT_SUCCESS(parse_action( - COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); + AVS_UNIT_ASSERT_SUCCESS( + parse_action(COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); AVS_UNIT_ASSERT_EQUAL(request.action, ANJAY_ACTION_WRITE_ATTRIBUTES); request.msg_type = AVS_COAP_MSG_CONFIRMABLE; request.request_code = AVS_COAP_CODE_PUT; request.content_format = ANJAY_COAP_FORMAT_PLAINTEXT; - AVS_UNIT_ASSERT_SUCCESS(parse_action( - COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); + AVS_UNIT_ASSERT_SUCCESS( + parse_action(COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); AVS_UNIT_ASSERT_EQUAL(request.action, ANJAY_ACTION_WRITE); request.msg_type = AVS_COAP_MSG_CONFIRMABLE; request.request_code = AVS_COAP_CODE_DELETE; - AVS_UNIT_ASSERT_SUCCESS(parse_action( - COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); + AVS_UNIT_ASSERT_SUCCESS( + parse_action(COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); AVS_UNIT_ASSERT_EQUAL(request.action, ANJAY_ACTION_DELETE); request.msg_type = AVS_COAP_MSG_CONFIRMABLE; request.request_code = AVS_COAP_CODE_NOT_FOUND; - AVS_UNIT_ASSERT_FAILED(parse_action( - COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); + AVS_UNIT_ASSERT_FAILED( + parse_action(COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &request)); } AVS_UNIT_TEST(parse_headers, parse_observe) { @@ -443,8 +431,8 @@ AVS_UNIT_TEST(parse_headers, parse_observe) { AVS_UNIT_ASSERT_FAILED(parse_observe( COAP_MSG(CON, GET, ID(0), OBSERVE(514), NO_PAYLOAD), &observe)); - AVS_UNIT_ASSERT_SUCCESS(parse_observe( - COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &observe)); + AVS_UNIT_ASSERT_SUCCESS( + parse_observe(COAP_MSG(CON, GET, ID(0), NO_PAYLOAD), &observe)); AVS_UNIT_ASSERT_EQUAL(observe, ANJAY_COAP_OBSERVE_NONE); } @@ -452,166 +440,22 @@ static time_t sched_time_to_next_s(anjay_sched_t *sched) { avs_time_duration_t sched_delay; AVS_UNIT_ASSERT_SUCCESS(_anjay_sched_time_to_next(sched, &sched_delay)); if (!avs_time_duration_less( - sched_delay, - avs_time_duration_from_scalar(500, AVS_TIME_MS))) { // rounding + sched_delay, + avs_time_duration_from_scalar(500, AVS_TIME_MS))) { // rounding return sched_delay.seconds + 1; } else { return sched_delay.seconds; } } -AVS_UNIT_TEST(queue_mode, behaviour) { - static const anjay_dm_internal_res_attrs_t ATTRS = { - .standard = { - .common = { - .min_period = 0, - .max_period = 9001 - }, - .greater_than = ANJAY_ATTRIB_VALUE_NONE, - .less_than = ANJAY_ATTRIB_VALUE_NONE, - .step = ANJAY_ATTRIB_VALUE_NONE - } - }; - - ////// INIT ////// - DM_TEST_INIT_WITH_SSIDS(42); - anjay->servers->servers->data_active.udp_connection.mode = - ANJAY_CONNECTION_QUEUE; - DM_TEST_REQUEST(mocksocks[0], CON, GET, ID(0xFA3E), OBSERVE(0), - PATH("42", "69", "4")); - _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); - _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, 0, - ANJAY_MOCK_DM_INT(0, 514)); - DM_TEST_EXPECT_READ_NULL_ATTRS(42, 69, 4); - DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CONTENT, ID(0xFA3E), - OBSERVE(0XF40000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("514")); - AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); - - // observe::flush_send_queue() - AVS_UNIT_ASSERT_EQUAL(sched_time_to_next_s(anjay->sched), 0); - _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); - _anjay_mock_dm_expect_resource_read_attrs(anjay, &OBJ, 69, 4, 42, 0, - &ATTRS); - AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); - AVS_UNIT_ASSERT_EQUAL(sched_time_to_next_s(anjay->sched), 93); - avs_unit_mocksock_assert_expects_met(mocksocks[0]); - - ////// QUEUE MODE - EMPTY PASS ////// - AVS_UNIT_ASSERT_NOT_NULL( - anjay->servers->servers->data_active.udp_connection.queue_mode_close_socket_clb_handle); - _anjay_mock_clock_advance(avs_time_duration_from_scalar(93, AVS_TIME_S)); - AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); - - AVS_UNIT_ASSERT_NULL(anjay_get_sockets(anjay)); - AVS_UNIT_ASSERT_NULL(anjay_get_socket_entries(anjay)); - AVS_UNIT_ASSERT_NULL( - anjay->servers->servers->data_active.udp_connection.queue_mode_close_socket_clb_handle); - - ////// NOTIFY - TRIGGER QUEUE MODE ////// - _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); - _anjay_mock_dm_expect_resource_read_attrs(anjay, &OBJ, 69, 4, 42, 0, - &ATTRS); - AVS_UNIT_ASSERT_SUCCESS(anjay_notify_changed(anjay, 42, 69, 4)); - avs_unit_mocksock_assert_expects_met(mocksocks[0]); - - ////// NOTIFICATION ////// - _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SERVER, 0, -1, 0); - _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); - _anjay_mock_dm_expect_resource_read_attrs(anjay, &OBJ, 69, 4, 42, 0, - &ATTRS); - _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); - _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, 0, - ANJAY_MOCK_DM_STRING(0, "Hello")); - const avs_coap_msg_t *notify_response = COAP_MSG(NON, CONTENT, ID(0x69ED), - OBSERVE(0x228000), - CONTENT_FORMAT(PLAINTEXT), - PAYLOAD("Hello")); - avs_unit_mocksock_assert_expects_met(mocksocks[0]); - avs_unit_mocksock_expect_remote_hostname(mocksocks[0], - "server.example.org"); - avs_unit_mocksock_expect_remote_port(mocksocks[0], "8378"); - avs_unit_mocksock_expect_connect(mocksocks[0], - "server.example.org", "8378"); - avs_unit_mocksock_expect_local_port(mocksocks[0], "65432"); - avs_unit_mocksock_expect_get_opt(mocksocks[0], - AVS_NET_SOCKET_OPT_SESSION_RESUMED, - (avs_net_socket_opt_value_t) { - .flag = true - }); - avs_unit_mocksock_expect_output(mocksocks[0], notify_response->content, - notify_response->length); - - ////// EXECUTE SCHEDULER ////// - while (sched_time_to_next_s(anjay->sched) <= 0) { - AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); - } - - ////// QUEUED RPC ////// - DM_TEST_REQUEST(mocksocks[0], CON, GET, ID(0xFB3E), PATH("42", "3", "1")); - _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 3, 1); - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 3, 1, 1); - _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 3, 1, 0, - ANJAY_MOCK_DM_STRING(0, "Hi!")); - DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CONTENT, ID(0xFB3E), - CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Hi!")); - AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); - - ////// NEXT QUEUED RPC - CANCEL NOTIFICATION ////// - DM_TEST_REQUEST(mocksocks[0], CON, GET, ID(0xFC3E), OBSERVE(0x01), - PATH("42", "69", "4")); - _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); - _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, 0, - ANJAY_MOCK_DM_STRING(0, "Meh")); - DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CONTENT, ID(0xFC3E), - CONTENT_FORMAT(PLAINTEXT), PAYLOAD("Meh")); - AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); - - ////// EXECUTE SCHEDULER ////// - while (sched_time_to_next_s(anjay->sched) <= 0) { - AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); - } - - ////// ASSERT QUEUE MODE ////// - AVS_UNIT_ASSERT_NOT_NULL( - anjay->servers->servers->data_active.udp_connection.queue_mode_close_socket_clb_handle); - { - AVS_LIST(avs_net_abstract_socket_t *const) sockets = - anjay_get_sockets(anjay); - AVS_UNIT_ASSERT_NOT_NULL(sockets); - AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(sockets), 1); - avs_net_abstract_socket_t *socket = *sockets; - - AVS_LIST(const anjay_socket_entry_t) entries = - anjay_get_socket_entries(anjay); - AVS_UNIT_ASSERT_NOT_NULL(entries); - AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(entries), 1); - AVS_UNIT_ASSERT_TRUE(entries->socket == socket); - AVS_UNIT_ASSERT_EQUAL(entries->transport, ANJAY_SOCKET_TRANSPORT_UDP); - AVS_UNIT_ASSERT_EQUAL(entries->ssid, 42); - AVS_UNIT_ASSERT_TRUE(entries->queue_mode); - } - AVS_UNIT_ASSERT_EQUAL(sched_time_to_next_s(anjay->sched), 93); - _anjay_mock_clock_advance(avs_time_duration_from_scalar(93, AVS_TIME_S)); - AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); - - AVS_UNIT_ASSERT_NULL(anjay_get_sockets(anjay)); - AVS_UNIT_ASSERT_NULL(anjay_get_socket_entries(anjay)); - AVS_UNIT_ASSERT_NULL( - anjay->servers->servers->data_active.udp_connection.queue_mode_close_socket_clb_handle); - - DM_TEST_FINISH; -} - AVS_UNIT_TEST(queue_mode, change) { DM_TEST_INIT_WITH_OBJECTS(&OBJ, &FAKE_SECURITY2, &FAKE_SERVER); + anjay_server_connection_t *connection = + _anjay_get_server_connection((const anjay_connection_ref_t) { + .server = anjay->servers->servers, + .conn_type = ANJAY_CONNECTION_UDP + }); + AVS_UNIT_ASSERT_NOT_NULL(connection); ////// WRITE NEW BINDING ////// // Write to Binding - dummy data to assert it is actually queried via Read DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("1", "1", "7"), @@ -645,8 +489,7 @@ AVS_UNIT_TEST(queue_mode, change) { AVS_UNIT_ASSERT_EQUAL(entries->ssid, 1); AVS_UNIT_ASSERT_FALSE(entries->queue_mode); } - AVS_UNIT_ASSERT_NULL( - anjay->servers->servers->data_active.udp_connection.queue_mode_close_socket_clb_handle); + AVS_UNIT_ASSERT_NULL(connection->queue_mode_close_socket_clb_handle); ////// REFRESH BINDING MODE ////// // query SSID in Security @@ -691,28 +534,25 @@ AVS_UNIT_TEST(queue_mode, change) { _anjay_mock_dm_expect_resource_read(anjay, &FAKE_SERVER, 1, ANJAY_DM_RID_SERVER_LIFETIME, 0, ANJAY_MOCK_DM_INT(0, 9001)); - const avs_coap_msg_t *update = COAP_MSG(CON, POST, ID(0x69ED), - CONTENT_FORMAT(APPLICATION_LINK), - QUERY("lt=9001", "b=UQ"), - PAYLOAD(",")); + const avs_coap_msg_t *update = + COAP_MSG(CON, POST, ID(0x69ED), CONTENT_FORMAT(APPLICATION_LINK), + QUERY("lt=9001", "b=UQ"), PAYLOAD(",")); avs_unit_mocksock_expect_output(mocksocks[0], update->content, update->length); - const avs_coap_msg_t *update_response = COAP_MSG(ACK, CHANGED, ID(0x69ED), - NO_PAYLOAD); + const avs_coap_msg_t *update_response = + COAP_MSG(ACK, CHANGED, ID(0x69ED), NO_PAYLOAD); avs_unit_mocksock_input(mocksocks[0], update_response->content, update_response->length); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); - AVS_UNIT_ASSERT_NOT_NULL( - anjay->servers->servers->data_active.udp_connection.queue_mode_close_socket_clb_handle); + AVS_UNIT_ASSERT_NOT_NULL(connection->queue_mode_close_socket_clb_handle); AVS_UNIT_ASSERT_EQUAL(sched_time_to_next_s(anjay->sched), 93); _anjay_mock_clock_advance(avs_time_duration_from_scalar(93, AVS_TIME_S)); AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); AVS_UNIT_ASSERT_NULL(anjay_get_sockets(anjay)); AVS_UNIT_ASSERT_NULL(anjay_get_socket_entries(anjay)); - AVS_UNIT_ASSERT_NULL( - anjay->servers->servers->data_active.udp_connection.queue_mode_close_socket_clb_handle); + AVS_UNIT_ASSERT_NULL(connection->queue_mode_close_socket_clb_handle); DM_TEST_FINISH; } diff --git a/src/test/dm.c b/src/test/dm.c index 894ba7542..e69c2acec 100644 --- a/src/test/dm.c +++ b/src/test/dm.c @@ -24,9 +24,9 @@ #include #include "../anjay_core.h" +#include "../coap/test/utils.h" #include "../io/vtable.h" #include "../servers/servers_internal.h" -#include "../coap/test/utils.h" AVS_UNIT_TEST(debug, debug_make_path_macro) { anjay_request_t request; @@ -70,9 +70,8 @@ AVS_UNIT_TEST(dm_read, resource_read_err_concrete) { NO_PAYLOAD); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); - _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 69, 4, - ANJAY_ERR_UNAUTHORIZED, - ANJAY_MOCK_DM_NONE); + _anjay_mock_dm_expect_resource_read( + anjay, &OBJ, 69, 4, ANJAY_ERR_UNAUTHORIZED, ANJAY_MOCK_DM_NONE); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, UNAUTHORIZED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); @@ -156,8 +155,10 @@ AVS_UNIT_TEST(dm_read, instance_some) { _anjay_mock_dm_expect_resource_read(anjay, &OBJ, 13, 6, 0, ANJAY_MOCK_DM_STRING(0, "Hello")); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CONTENT, ID(0xFA3E), - CONTENT_FORMAT(TLV), PAYLOAD("\xc1\x00\x45" - "\xc5\x06" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\xc1\x00\x45" + "\xc5\x06" + "Hello")); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; } @@ -224,7 +225,7 @@ AVS_UNIT_TEST(dm_read, object_empty) { DM_TEST_REQUEST(mocksocks[0], CON, GET, ID(0xFA3E), PATH("42"), NO_PAYLOAD); _anjay_mock_dm_expect_instance_it(anjay, &OBJ, 0, 0, ANJAY_IID_INVALID); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CONTENT, ID(0xFA3E), - CONTENT_FORMAT(TLV), NO_PAYLOAD); + CONTENT_FORMAT(TLV), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; } @@ -412,7 +413,9 @@ AVS_UNIT_TEST(dm_write, resource_unsupported_format) { AVS_UNIT_TEST(dm_write, resource_with_mismatched_tlv_rid) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "514", "4"), - CONTENT_FORMAT(TLV), PAYLOAD("\xc5\x05" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\xc5\x05" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 514, 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, BAD_REQUEST, ID(0xFA3E), NO_PAYLOAD); @@ -423,8 +426,10 @@ AVS_UNIT_TEST(dm_write, resource_with_mismatched_tlv_rid) { AVS_UNIT_TEST(dm_write, instance) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), - CONTENT_FORMAT(TLV), PAYLOAD("\xc1\x00\x0d" - "\xc5\x06" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\xc1\x00\x0d" + "\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 0, ANJAY_MOCK_DM_INT(0, 13), 0); @@ -438,8 +443,10 @@ AVS_UNIT_TEST(dm_write, instance) { AVS_UNIT_TEST(dm_write, instance_unsupported_format) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), - CONTENT_FORMAT_VALUE(0x4242), PAYLOAD("\xc1\x00\x0d" - "\xc5\x06" "Hello")); + CONTENT_FORMAT_VALUE(0x4242), + PAYLOAD("\xc1\x00\x0d" + "\xc5\x06" + "Hello")); // 4.15 Unsupported Content Format DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, UNSUPPORTED_CONTENT_FORMAT, ID(0xFA3E), NO_PAYLOAD); @@ -450,14 +457,15 @@ AVS_UNIT_TEST(dm_write, instance_unsupported_format) { AVS_UNIT_TEST(dm_write, instance_partial) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("42", "69"), - CONTENT_FORMAT(TLV), PAYLOAD("\xc1\x00\x0d" - "\xc5\x06" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\xc1\x00\x0d" + "\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 0, ANJAY_MOCK_DM_INT(0, 13), 0); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 6, - ANJAY_MOCK_DM_STRING(0, "Hello"), - 0); + ANJAY_MOCK_DM_STRING(0, "Hello"), 0); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; @@ -466,15 +474,16 @@ AVS_UNIT_TEST(dm_write, instance_partial) { AVS_UNIT_TEST(dm_write, instance_full) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("25", "69"), - CONTENT_FORMAT(TLV), PAYLOAD("\xc1\x00\x0d" - "\xc5\x06" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\xc1\x00\x0d" + "\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ_WITH_RESET, 69, 1); _anjay_mock_dm_expect_instance_reset(anjay, &OBJ_WITH_RESET, 69, 0); _anjay_mock_dm_expect_resource_write(anjay, &OBJ_WITH_RESET, 69, 0, ANJAY_MOCK_DM_INT(0, 13), 0); _anjay_mock_dm_expect_resource_write(anjay, &OBJ_WITH_RESET, 69, 6, - ANJAY_MOCK_DM_STRING(0, "Hello"), - 0); + ANJAY_MOCK_DM_STRING(0, "Hello"), 0); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; @@ -483,15 +492,17 @@ AVS_UNIT_TEST(dm_write, instance_full) { AVS_UNIT_TEST(dm_write, instance_superfluous_instance) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("25", "69"), - CONTENT_FORMAT(TLV), PAYLOAD("\x08\x45\x0a" "\xc1\x00\x0d" - "\xc5\x06" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\x08\x45\x0a" + "\xc1\x00\x0d" + "\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ_WITH_RESET, 69, 1); _anjay_mock_dm_expect_instance_reset(anjay, &OBJ_WITH_RESET, 69, 0); _anjay_mock_dm_expect_resource_write(anjay, &OBJ_WITH_RESET, 69, 0, ANJAY_MOCK_DM_INT(0, 13), 0); _anjay_mock_dm_expect_resource_write(anjay, &OBJ_WITH_RESET, 69, 6, - ANJAY_MOCK_DM_STRING(0, "Hello"), - 0); + ANJAY_MOCK_DM_STRING(0, "Hello"), 0); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; @@ -500,8 +511,11 @@ AVS_UNIT_TEST(dm_write, instance_superfluous_instance) { AVS_UNIT_TEST(dm_write, instance_inconsistent_instance) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), - CONTENT_FORMAT(TLV), PAYLOAD("\x08\x4d\x0a" "\xc1\x00\x0d" - "\xc5\x06" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\x08\x4d\x0a" + "\xc1\x00\x0d" + "\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, BAD_REQUEST, ID(0xFA3E), NO_PAYLOAD); @@ -512,8 +526,10 @@ AVS_UNIT_TEST(dm_write, instance_inconsistent_instance) { AVS_UNIT_TEST(dm_write, instance_wrong_type) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), - CONTENT_FORMAT(TLV), PAYLOAD("\x01\x00\x0d" - "\xc5\x06" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\x01\x00\x0d" + "\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, BAD_REQUEST, ID(0xFA3E), NO_PAYLOAD); @@ -524,8 +540,10 @@ AVS_UNIT_TEST(dm_write, instance_wrong_type) { AVS_UNIT_TEST(dm_write, instance_nonexistent) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42", "69"), - CONTENT_FORMAT(TLV), PAYLOAD("\xc1\x00\x0d" - "\xc5\x06" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\xc1\x00\x0d" + "\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 0); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, NOT_FOUND, ID(0xFA3E), NO_PAYLOAD); @@ -536,8 +554,11 @@ AVS_UNIT_TEST(dm_write, instance_nonexistent) { AVS_UNIT_TEST(dm_write, no_instance) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, PUT, ID(0xFA3E), PATH("42"), - CONTENT_FORMAT(TLV), PAYLOAD("\x08\x45\x0a" "\xc1\x00\x0d" - "\xc5\x06" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\x08\x45\x0a" + "\xc1\x00\x0d" + "\xc5\x06" + "Hello")); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, METHOD_NOT_ALLOWED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); @@ -620,12 +641,16 @@ AVS_UNIT_TEST(dm_execute, instance_inexistent) { DM_TEST_FINISH; } -static int execute_get_arg_value_invalid_args(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_execute_ctx_t *ctx) { - (void)iid; (void)rid; (void)anjay; (void)obj_ptr; +static int +execute_get_arg_value_invalid_args(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_execute_ctx_t *ctx) { + (void) iid; + (void) rid; + (void) anjay; + (void) obj_ptr; int ret; int arg; bool has_value; @@ -652,10 +677,11 @@ AVS_UNIT_TEST(dm_execute, execute_get_arg_value_invalid_args) { DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("128", "514", "1"), PAYLOAD("0='foobarbaz'")); - _anjay_mock_dm_expect_instance_present(anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1); - _anjay_mock_dm_expect_resource_present(anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1, 1); + _anjay_mock_dm_expect_instance_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1); + _anjay_mock_dm_expect_resource_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1, + 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); @@ -667,7 +693,10 @@ static int valid_args_execute(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_execute_ctx_t *ctx) { - (void)iid; (void)rid; (void)anjay; (void)obj_ptr; + (void) iid; + (void) rid; + (void) anjay; + (void) obj_ptr; int ret; int arg; bool has_value; @@ -700,23 +729,27 @@ AVS_UNIT_TEST(dm_execute, valid_args) { DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("128", "514", "1"), PAYLOAD("0,1,2")); - _anjay_mock_dm_expect_instance_present(anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1); - _anjay_mock_dm_expect_resource_present(anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1, 1); + _anjay_mock_dm_expect_instance_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1); + _anjay_mock_dm_expect_resource_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1, + 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; } - -static int valid_args_with_values_execute(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_execute_ctx_t *ctx) { - (void)iid; (void)rid; (void)anjay; (void)obj_ptr; +static int +valid_args_with_values_execute(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_execute_ctx_t *ctx) { + (void) iid; + (void) rid; + (void) anjay; + (void) obj_ptr; int ret; int arg; bool has_value; @@ -755,23 +788,28 @@ AVS_UNIT_TEST(dm_execute, valid_args_with_values) { EXECUTE_OBJ->handlers.resource_execute = valid_args_with_values_execute; DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("128", "514", "1"), PAYLOAD("0,1='value',2")); - - _anjay_mock_dm_expect_instance_present(anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1); - _anjay_mock_dm_expect_resource_present(anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1, 1); + + _anjay_mock_dm_expect_instance_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1); + _anjay_mock_dm_expect_resource_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1, + 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; } -static int valid_values_partial_read_execute(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_execute_ctx_t *ctx) { - (void)iid; (void)rid; (void)anjay; (void)obj_ptr; +static int +valid_values_partial_read_execute(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_execute_ctx_t *ctx) { + (void) iid; + (void) rid; + (void) anjay; + (void) obj_ptr; int ret; int arg; bool has_value; @@ -803,22 +841,27 @@ AVS_UNIT_TEST(dm_execute, valid_values_partial_read) { DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("128", "514", "1"), PAYLOAD("1='verylongvalue'")); - _anjay_mock_dm_expect_instance_present(anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1); - _anjay_mock_dm_expect_resource_present(anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1, 1); + _anjay_mock_dm_expect_instance_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1); + _anjay_mock_dm_expect_resource_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1, + 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; } -static int valid_values_skipping_execute(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_execute_ctx_t *ctx) { - (void)iid; (void)rid; (void)anjay; (void)obj_ptr; +static int +valid_values_skipping_execute(anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_execute_ctx_t *ctx) { + (void) iid; + (void) rid; + (void) anjay; + (void) obj_ptr; int ret; int arg; bool has_value; @@ -859,10 +902,11 @@ AVS_UNIT_TEST(dm_execute, valid_values_skipping) { DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("128", "514", "1"), PAYLOAD("1='verylongvalue',2,3")); - _anjay_mock_dm_expect_instance_present(anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1); - _anjay_mock_dm_expect_resource_present(anjay, - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1, 1); + _anjay_mock_dm_expect_instance_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1); + _anjay_mock_dm_expect_resource_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, 1, + 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); @@ -870,11 +914,14 @@ AVS_UNIT_TEST(dm_execute, valid_values_skipping) { } static int invalid_input_execute(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_execute_ctx_t *ctx) { - (void)iid; (void)rid; (void)anjay; (void)obj_ptr; + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_execute_ctx_t *ctx) { + (void) iid; + (void) rid; + (void) anjay; + (void) obj_ptr; int ret; int arg; bool has_value; @@ -893,6 +940,7 @@ static int invalid_input_execute(anjay_t *anjay, } AVS_UNIT_TEST(dm_execute, invalid_input) { + // clang-format off static const char* invalid_inputs[] = { "a", "0=", @@ -904,18 +952,20 @@ AVS_UNIT_TEST(dm_execute, invalid_input) { "0='val',11", "0='val" }; + // clang-format on EXECUTE_OBJ->handlers.resource_execute = invalid_input_execute; for (size_t i = 0; i < AVS_ARRAY_SIZE(invalid_inputs); i++) { DM_TEST_INIT; - DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), - PATH("128", "514", "1"), - PAYLOAD_EXTERNAL(invalid_inputs[i], - strlen(invalid_inputs[i]))); - _anjay_mock_dm_expect_instance_present(anjay, - (const anjay_dm_object_def_t *const *)&EXECUTE_OBJ, 514, 1); - _anjay_mock_dm_expect_resource_present(anjay, - (const anjay_dm_object_def_t *const *)&EXECUTE_OBJ, 514, 1, 1); + DM_TEST_REQUEST( + mocksocks[0], CON, POST, ID(0xFA3E), PATH("128", "514", "1"), + PAYLOAD_EXTERNAL(invalid_inputs[i], strlen(invalid_inputs[i]))); + _anjay_mock_dm_expect_instance_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, + 1); + _anjay_mock_dm_expect_resource_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, + 1, 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); @@ -928,7 +978,10 @@ static int valid_input_execute(anjay_t *anjay, anjay_iid_t iid, anjay_rid_t rid, anjay_execute_ctx_t *ctx) { - (void)iid; (void)rid; (void)anjay; (void)obj_ptr; + (void) iid; + (void) rid; + (void) anjay; + (void) obj_ptr; int ret; int arg; bool has_value; @@ -941,25 +994,22 @@ static int valid_input_execute(anjay_t *anjay, } AVS_UNIT_TEST(dm_execute, valid_input) { - static const char* valid_inputs[] = { - "", - "0='ala'", - "2='10.3'", - "7,0='https://www.oma.org'", - "0,1,2,3,4" - }; + static const char *valid_inputs[] = { "", "0='ala'", "2='10.3'", + "7,0='https://www.oma.org'", + "0,1,2,3,4" }; EXECUTE_OBJ->handlers.resource_execute = valid_input_execute; for (size_t i = 0; i < AVS_ARRAY_SIZE(valid_inputs); i++) { DM_TEST_INIT; - DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), - PATH("128", "514", "1"), - PAYLOAD_EXTERNAL(valid_inputs[i], - strlen(valid_inputs[i]))); - _anjay_mock_dm_expect_instance_present(anjay, - (const anjay_dm_object_def_t *const *)&EXECUTE_OBJ, 514, 1); - _anjay_mock_dm_expect_resource_present(anjay, - (const anjay_dm_object_def_t *const *)&EXECUTE_OBJ, 514, 1, 1); + DM_TEST_REQUEST( + mocksocks[0], CON, POST, ID(0xFA3E), PATH("128", "514", "1"), + PAYLOAD_EXTERNAL(valid_inputs[i], strlen(valid_inputs[i]))); + _anjay_mock_dm_expect_instance_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, + 1); + _anjay_mock_dm_expect_resource_present( + anjay, (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, 514, + 1, 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); @@ -975,19 +1025,19 @@ AVS_UNIT_TEST(dm_write_attributes, resource) { _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 514, 4, 1); _anjay_mock_dm_expect_resource_read_attrs( anjay, &OBJ, 514, 4, 77, 0, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); - _anjay_mock_dm_expect_resource_write_attrs(anjay, &OBJ, 514, 4, 77, + _anjay_mock_dm_expect_resource_write_attrs( + anjay, &OBJ, 514, 4, 77, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = 42, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - }, - .greater_than = ANJAY_ATTRIB_VALUE_NONE, - .less_than = ANJAY_ATTRIB_VALUE_NONE, - .step = 0.7 - } - }, 0); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = 42, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + }, + .greater_than = ANJAY_ATTRIB_VALUE_NONE, + .less_than = ANJAY_ATTRIB_VALUE_NONE, + .step = 0.7 + } }, + 0); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; @@ -1000,16 +1050,15 @@ AVS_UNIT_TEST(dm_write_attributes, instance) { _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 77, 1); _anjay_mock_dm_expect_instance_read_default_attrs( anjay, &OBJ, 77, 42, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); - _anjay_mock_dm_expect_instance_write_default_attrs(anjay, &OBJ, 77, 42, + _anjay_mock_dm_expect_instance_write_default_attrs( + anjay, &OBJ, 77, 42, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 69, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - } - }, 0); - DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), - NO_PAYLOAD); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 69, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + } }, + 0); + DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; } @@ -1020,14 +1069,14 @@ AVS_UNIT_TEST(dm_write_attributes, object) { QUERY("pmax=514")); _anjay_mock_dm_expect_object_read_default_attrs( anjay, &OBJ, 666, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); - _anjay_mock_dm_expect_object_write_default_attrs(anjay, &OBJ, 666, + _anjay_mock_dm_expect_object_write_default_attrs( + anjay, &OBJ, 666, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = ANJAY_ATTRIB_PERIOD_NONE, - .max_period = 514 - } - }, 0); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = 514 + } }, + 0); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CHANGED, ID(0xFA3E), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; @@ -1064,37 +1113,34 @@ AVS_UNIT_TEST(dm_discover, resource) { _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); _anjay_mock_dm_expect_resource_dim(anjay, &OBJ, 69, 4, ANJAY_DM_DIM_INVALID); - _anjay_mock_dm_expect_resource_read_attrs(anjay, &OBJ, 69, 4, 7, 0, + _anjay_mock_dm_expect_resource_read_attrs( + anjay, &OBJ, 69, 4, 7, 0, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = ANJAY_ATTRIB_PERIOD_NONE, - .max_period = 514 - }, - .greater_than = ANJAY_ATTRIB_VALUE_NONE, - .less_than = 6.46, - .step = ANJAY_ATTRIB_VALUE_NONE - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = 514 + }, + .greater_than = ANJAY_ATTRIB_VALUE_NONE, + .less_than = 6.46, + .step = ANJAY_ATTRIB_VALUE_NONE + } }); - _anjay_mock_dm_expect_instance_read_default_attrs(anjay, &OBJ, 69, 7, 0, + _anjay_mock_dm_expect_instance_read_default_attrs( + anjay, &OBJ, 69, 7, 0, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = ANJAY_ATTRIB_PERIOD_NONE, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + } }); - _anjay_mock_dm_expect_object_read_default_attrs(anjay, &OBJ, 7, 0, + _anjay_mock_dm_expect_object_read_default_attrs( + anjay, &OBJ, 7, 0, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 10, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 10, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + } }); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CONTENT, ID(0xfa3e), CONTENT_FORMAT(APPLICATION_LINK), @@ -1110,19 +1156,18 @@ AVS_UNIT_TEST(dm_discover, resource_multiple_servers) { _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 69, 4, 1); _anjay_mock_dm_expect_resource_dim(anjay, &OBJ, 69, 4, 54); - _anjay_mock_dm_expect_resource_read_attrs(anjay, &OBJ, 69, 4, 34, 0, + _anjay_mock_dm_expect_resource_read_attrs( + anjay, &OBJ, 69, 4, 34, 0, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = 10, - .max_period = 514 - }, - .greater_than = ANJAY_ATTRIB_VALUE_NONE, - .less_than = 6.46, - .step = ANJAY_ATTRIB_VALUE_NONE - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = 10, + .max_period = 514 + }, + .greater_than = ANJAY_ATTRIB_VALUE_NONE, + .less_than = 6.46, + .step = ANJAY_ATTRIB_VALUE_NONE + } }); #ifdef WITH_CUSTOM_ATTRIBUTES _anjay_mock_dm_expect_instance_read_default_attrs( anjay, &OBJ, 69, 34, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); @@ -1143,24 +1188,21 @@ AVS_UNIT_TEST(dm_discover, instance) { ACCEPT(0x28), NO_PAYLOAD); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 514, 1); - _anjay_mock_dm_expect_instance_read_default_attrs(anjay, &OBJ, 514, 69, 0, + _anjay_mock_dm_expect_instance_read_default_attrs( + anjay, &OBJ, 514, 69, 0, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 666, - .max_period = 777 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 666, + .max_period = 777 + } }); for (size_t i = 0; i < OBJ->supported_rids.count; ++i) { if (OBJ->supported_rids.rids[i] > 1) { - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 514, - OBJ->supported_rids.rids[i], - 0); + _anjay_mock_dm_expect_resource_present( + anjay, &OBJ, 514, OBJ->supported_rids.rids[i], 0); } else { - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 514, - OBJ->supported_rids.rids[i], - 1); + _anjay_mock_dm_expect_resource_present( + anjay, &OBJ, 514, OBJ->supported_rids.rids[i], 1); anjay_dm_internal_res_attrs_t attrs = ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY; attrs.standard.greater_than = (double) OBJ->supported_rids.rids[i]; @@ -1168,8 +1210,8 @@ AVS_UNIT_TEST(dm_discover, instance) { OBJ->supported_rids.rids[i], ANJAY_DM_DIM_INVALID); _anjay_mock_dm_expect_resource_read_attrs( - anjay, &OBJ, 514, OBJ->supported_rids.rids[i], 69, - 0, &attrs); + anjay, &OBJ, 514, OBJ->supported_rids.rids[i], 69, 0, + &attrs); } } @@ -1187,24 +1229,21 @@ AVS_UNIT_TEST(dm_discover, instance_multiple_servers) { ACCEPT(0x28), NO_PAYLOAD); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 514, 1); - _anjay_mock_dm_expect_instance_read_default_attrs(anjay, &OBJ, 514, 69, 0, + _anjay_mock_dm_expect_instance_read_default_attrs( + anjay, &OBJ, 514, 69, 0, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = 666, - .max_period = 777 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = 666, + .max_period = 777 + } }); for (size_t i = 0; i < OBJ->supported_rids.count; ++i) { if (OBJ->supported_rids.rids[i] > 1) { - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 514, - OBJ->supported_rids.rids[i], - 0); + _anjay_mock_dm_expect_resource_present( + anjay, &OBJ, 514, OBJ->supported_rids.rids[i], 0); } else { - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, 514, - OBJ->supported_rids.rids[i], - 1); + _anjay_mock_dm_expect_resource_present( + anjay, &OBJ, 514, OBJ->supported_rids.rids[i], 1); anjay_dm_internal_res_attrs_t attrs = ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY; attrs.standard.greater_than = (double) OBJ->supported_rids.rids[i]; @@ -1212,8 +1251,8 @@ AVS_UNIT_TEST(dm_discover, instance_multiple_servers) { OBJ->supported_rids.rids[i], ANJAY_DM_DIM_INVALID); _anjay_mock_dm_expect_resource_read_attrs( - anjay, &OBJ, 514, OBJ->supported_rids.rids[i], 69, - 0, &attrs); + anjay, &OBJ, 514, OBJ->supported_rids.rids[i], 69, 0, + &attrs); } } @@ -1229,19 +1268,15 @@ AVS_UNIT_TEST(dm_discover, object) { DM_TEST_INIT_WITH_SSIDS(2); DM_TEST_REQUEST(mocksocks[0], CON, GET, ID(0xFA3E), PATH("42"), ACCEPT(0x28), NO_PAYLOAD); - _anjay_mock_dm_expect_object_read_default_attrs(anjay, &OBJ, 2, 0, + _anjay_mock_dm_expect_object_read_default_attrs( + anjay, &OBJ, 2, 0, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = ANJAY_ATTRIB_PERIOD_NONE, - .max_period = 514 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = 514 + } }); - int presence[][7] = { - { 1, 0, 0, 1, 1, 0, 1 }, - { 0, 0, 0, 0, 1, 1, 1 } - }; + int presence[][7] = { { 1, 0, 0, 1, 1, 0, 1 }, { 0, 0, 0, 0, 1, 1, 1 } }; const size_t ITERATIONS = sizeof(presence) / sizeof(presence[0]); for (anjay_iid_t iid = 0; iid < ITERATIONS; ++iid) { _anjay_mock_dm_expect_instance_it(anjay, &OBJ, iid, 0, iid); @@ -1267,19 +1302,15 @@ AVS_UNIT_TEST(dm_discover, object_multiple_servers) { DM_TEST_INIT_WITH_SSIDS(2, 3); DM_TEST_REQUEST(mocksocks[0], CON, GET, ID(0xFA3E), PATH("42"), ACCEPT(0x28), NO_PAYLOAD); - _anjay_mock_dm_expect_object_read_default_attrs(anjay, &OBJ, 2, 0, + _anjay_mock_dm_expect_object_read_default_attrs( + anjay, &OBJ, 2, 0, &(const anjay_dm_internal_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .min_period = ANJAY_ATTRIB_PERIOD_NONE, - .max_period = 514 - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .min_period = ANJAY_ATTRIB_PERIOD_NONE, + .max_period = 514 + } }); - int presence[][7] = { - { 1, 0, 0, 1, 1, 0, 1 }, - { 0, 0, 0, 0, 1, 1, 1 } - }; + int presence[][7] = { { 1, 0, 0, 1, 1, 0, 1 }, { 0, 0, 0, 0, 1, 1, 1 } }; const size_t ITERATIONS = sizeof(presence) / sizeof(presence[0]); for (anjay_iid_t iid = 0; iid < ITERATIONS; ++iid) { _anjay_mock_dm_expect_instance_it(anjay, &OBJ, iid, 0, iid); @@ -1327,12 +1358,11 @@ AVS_UNIT_TEST(dm_discover, multiple_servers_empty) { _anjay_mock_dm_expect_resource_dim(anjay, &OBJ, 69, 4, ANJAY_DM_DIM_INVALID); _anjay_mock_dm_expect_resource_read_attrs( - anjay, &OBJ, 69, 4, 34, 0, - &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); + anjay, &OBJ, 69, 4, 34, 0, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); _anjay_mock_dm_expect_instance_read_default_attrs( anjay, &OBJ, 69, 34, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); - _anjay_mock_dm_expect_object_read_default_attrs(anjay, &OBJ, 34, 0, - &ANJAY_DM_INTERNAL_ATTRS_EMPTY); + _anjay_mock_dm_expect_object_read_default_attrs( + anjay, &OBJ, 34, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CONTENT, ID(0xfa3e), CONTENT_FORMAT(APPLICATION_LINK), @@ -1344,7 +1374,9 @@ AVS_UNIT_TEST(dm_discover, multiple_servers_empty) { AVS_UNIT_TEST(dm_create, only_iid) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("42"), - CONTENT_FORMAT(TLV), PAYLOAD("\x20" "\x02\x02")); + CONTENT_FORMAT(TLV), + PAYLOAD("\x20" + "\x02\x02")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 514, 0); _anjay_mock_dm_expect_instance_create(anjay, &OBJ, 514, 1, 0, 514); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, CREATED, ID(0xFA3E), @@ -1356,7 +1388,9 @@ AVS_UNIT_TEST(dm_create, only_iid) { AVS_UNIT_TEST(dm_create, failure) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("42"), - CONTENT_FORMAT(TLV), PAYLOAD("\x20" "\x02\x02")); + CONTENT_FORMAT(TLV), + PAYLOAD("\x20" + "\x02\x02")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 514, 0); _anjay_mock_dm_expect_instance_create(anjay, &OBJ, 514, 1, -1, 514); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, INTERNAL_SERVER_ERROR, @@ -1368,7 +1402,9 @@ AVS_UNIT_TEST(dm_create, failure) { AVS_UNIT_TEST(dm_create, already_exists) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("42"), - CONTENT_FORMAT(TLV), PAYLOAD("\x00" "\x45")); + CONTENT_FORMAT(TLV), + PAYLOAD("\x00" + "\x45")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 1); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, BAD_REQUEST, ID(0xfa3e), NO_PAYLOAD); @@ -1379,7 +1415,9 @@ AVS_UNIT_TEST(dm_create, already_exists) { AVS_UNIT_TEST(dm_create, wrong_iid) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("42"), - CONTENT_FORMAT(TLV), PAYLOAD("\x20" "\x02\x02")); + CONTENT_FORMAT(TLV), + PAYLOAD("\x20" + "\x02\x02")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 514, 0); _anjay_mock_dm_expect_instance_create(anjay, &OBJ, 514, 1, 0, 7); DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, INTERNAL_SERVER_ERROR, @@ -1403,8 +1441,10 @@ AVS_UNIT_TEST(dm_create, no_iid) { AVS_UNIT_TEST(dm_create, with_data) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("42"), - CONTENT_FORMAT(TLV), PAYLOAD("\xc1\x00\x0d" - "\xc5\x06" "Hello")); + CONTENT_FORMAT(TLV), + PAYLOAD("\xc1\x00\x0d" + "\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_create(anjay, &OBJ, ANJAY_IID_INVALID, 1, 0, 69); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 0, @@ -1421,7 +1461,10 @@ AVS_UNIT_TEST(dm_create, with_iid_and_data) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("42"), CONTENT_FORMAT(TLV), - PAYLOAD("\x08\x45\x0a" "\xc1\x00\x0d" "\xc5\x06" "Hello")); + PAYLOAD("\x08\x45\x0a" + "\xc1\x00\x0d" + "\xc5\x06" + "Hello")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 0); _anjay_mock_dm_expect_instance_create(anjay, &OBJ, 69, 1, 0, 69); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 0, @@ -1438,8 +1481,10 @@ AVS_UNIT_TEST(dm_create, multiple_iids) { DM_TEST_INIT; DM_TEST_REQUEST(mocksocks[0], CON, POST, ID(0xFA3E), PATH("42"), CONTENT_FORMAT(TLV), - PAYLOAD("\x08\x45\x03" "\xc1\x00\x2a" - "\x08\x2a\x03" "\xc1\x03\x45")); + PAYLOAD("\x08\x45\x03" + "\xc1\x00\x2a" + "\x08\x2a\x03" + "\xc1\x03\x45")); _anjay_mock_dm_expect_instance_present(anjay, &OBJ, 69, 0); _anjay_mock_dm_expect_instance_create(anjay, &OBJ, 69, 1, 0, 69); _anjay_mock_dm_expect_resource_write(anjay, &OBJ, 69, 0, @@ -1505,7 +1550,8 @@ static int succeed() { } static int mock_get_id(anjay_input_ctx_t *in_ctx, - anjay_id_type_t *out_id_type, uint16_t *out_id) { + anjay_id_type_t *out_id_type, + uint16_t *out_id) { (void) in_ctx; *out_id_type = ANJAY_ID_RID; *out_id = 0; @@ -1525,25 +1571,27 @@ AVS_UNIT_TEST(dm_operations, unimplemented) { anjay_t anjay; memset(&anjay, 0, sizeof(anjay)); - coap_stream_mock_t mock = { \ + coap_stream_mock_t mock = { .vtable = &(const avs_stream_v_table_t) { - .write_some = (avs_stream_write_some_t) fail, + .write_some = (avs_stream_write_some_t) fail, .finish_message = (avs_stream_finish_message_t) fail, - .read = (avs_stream_read_t) fail, - .peek = (avs_stream_peek_t) fail, - .reset = (avs_stream_reset_t) fail, - .close = (avs_stream_close_t) fail, - .get_errno = (avs_stream_errno_t) fail, - .extension_list = &(const avs_stream_v_table_extension_t[]) { - { - ANJAY_COAP_STREAM_EXTENSION, - &(anjay_coap_stream_ext_t) { - .setup_response = - (anjay_coap_stream_setup_response_t *) succeed - } - }, - AVS_STREAM_V_TABLE_EXTENSION_NULL - }[0] + .read = (avs_stream_read_t) fail, + .peek = (avs_stream_peek_t) fail, + .reset = (avs_stream_reset_t) fail, + .close = (avs_stream_close_t) fail, + .get_errno = (avs_stream_errno_t) fail, + .extension_list = + &(const avs_stream_v_table_extension_t[]) { + { + .id = ANJAY_COAP_STREAM_EXTENSION, + .data = &(anjay_coap_stream_ext_t) { + .setup_response = + (anjay_coap_stream_setup_response_t *) + succeed + } + }, + AVS_STREAM_V_TABLE_EXTENSION_NULL + }[0] } }; @@ -1596,7 +1644,7 @@ AVS_UNIT_TEST(dm_operations, unimplemented) { anjay.current_connection.server = &(anjay_server_info_t) { .ssid = 0 }; - anjay_uri_path_t uri_object = MAKE_OBJECT_PATH(1337); + anjay_uri_path_t uri_object = MAKE_OBJECT_PATH(1337); anjay_uri_path_t uri_instance = MAKE_INSTANCE_PATH(1337, 0); anjay_uri_path_t uri_resource = MAKE_RESOURCE_PATH(1337, 0, 0); ASSERT_ACTION_FAILS(ANJAY_ACTION_READ, @@ -1623,13 +1671,14 @@ AVS_UNIT_TEST(dm_operations, unimplemented) { // Cancel Observe does not call any handlers, so it does not fail } -static const anjay_dm_attrs_query_details_t DM_EFFECTIVE_ATTRS_STANDARD_QUERY = { - .obj = &OBJ, - .iid = 69, - .rid = 4, - .ssid = 1, - .with_server_level_attrs = true -}; +static const anjay_dm_attrs_query_details_t + DM_EFFECTIVE_ATTRS_STANDARD_QUERY = { + .obj = &OBJ, + .iid = 69, + .rid = 4, + .ssid = 1, + .with_server_level_attrs = true + }; AVS_UNIT_TEST(dm_effective_attrs, resource_full) { DM_TEST_INIT; @@ -1658,7 +1707,8 @@ AVS_UNIT_TEST(dm_effective_attrs, resource_full) { AVS_UNIT_TEST(dm_effective_attrs, fallback_to_instance) { DM_TEST_INIT; (void) mocksocks; - _anjay_mock_dm_expect_resource_read_attrs(anjay, &OBJ, 69, 4, 1, 0, + _anjay_mock_dm_expect_resource_read_attrs( + anjay, &OBJ, 69, 4, 1, 0, &(const anjay_dm_internal_res_attrs_t) { .standard = { .common = { @@ -1670,7 +1720,8 @@ AVS_UNIT_TEST(dm_effective_attrs, fallback_to_instance) { .step = ANJAY_ATTRIB_VALUE_NONE } }); - _anjay_mock_dm_expect_instance_read_default_attrs(anjay, &OBJ, 69, 1, 0, + _anjay_mock_dm_expect_instance_read_default_attrs( + anjay, &OBJ, 69, 1, 0, &(const anjay_dm_internal_attrs_t) { .standard = { .min_period = 514, @@ -1680,7 +1731,8 @@ AVS_UNIT_TEST(dm_effective_attrs, fallback_to_instance) { anjay_dm_internal_res_attrs_t attrs; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_effective_attrs( anjay, &DM_EFFECTIVE_ATTRS_STANDARD_QUERY, &attrs)); - _anjay_mock_dm_assert_attributes_equal(&attrs, + _anjay_mock_dm_assert_attributes_equal( + &attrs, &(const anjay_dm_internal_res_attrs_t) { .standard = { .common = { @@ -1698,7 +1750,8 @@ AVS_UNIT_TEST(dm_effective_attrs, fallback_to_instance) { AVS_UNIT_TEST(dm_effective_attrs, fallback_to_object) { DM_TEST_INIT; (void) mocksocks; - _anjay_mock_dm_expect_resource_read_attrs(anjay, &OBJ, 69, 4, 1, 0, + _anjay_mock_dm_expect_resource_read_attrs( + anjay, &OBJ, 69, 4, 1, 0, &(const anjay_dm_internal_res_attrs_t) { .standard = { .common = ANJAY_DM_ATTRIBS_EMPTY, @@ -1707,14 +1760,16 @@ AVS_UNIT_TEST(dm_effective_attrs, fallback_to_object) { .step = 6.9 } }); - _anjay_mock_dm_expect_instance_read_default_attrs(anjay, &OBJ, 69, 1, 0, + _anjay_mock_dm_expect_instance_read_default_attrs( + anjay, &OBJ, 69, 1, 0, &(const anjay_dm_internal_attrs_t) { .standard = { .min_period = ANJAY_ATTRIB_PERIOD_NONE, .max_period = 777 } }); - _anjay_mock_dm_expect_object_read_default_attrs(anjay, &OBJ, 1, 0, + _anjay_mock_dm_expect_object_read_default_attrs( + anjay, &OBJ, 1, 0, &(const anjay_dm_internal_attrs_t) { .standard = { .min_period = 514, @@ -1724,7 +1779,8 @@ AVS_UNIT_TEST(dm_effective_attrs, fallback_to_object) { anjay_dm_internal_res_attrs_t attrs; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_effective_attrs( anjay, &DM_EFFECTIVE_ATTRS_STANDARD_QUERY, &attrs)); - _anjay_mock_dm_assert_attributes_equal(&attrs, + _anjay_mock_dm_assert_attributes_equal( + &attrs, &(const anjay_dm_internal_res_attrs_t) { .standard = { .common = { @@ -1746,7 +1802,8 @@ AVS_UNIT_TEST(dm_effective_attrs, fallback_to_server) { anjay, &OBJ, 69, 4, 1, 0, &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); _anjay_mock_dm_expect_instance_read_default_attrs( anjay, &OBJ, 69, 1, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); - _anjay_mock_dm_expect_object_read_default_attrs(anjay, &OBJ, 1, 0, + _anjay_mock_dm_expect_object_read_default_attrs( + anjay, &OBJ, 1, 0, &(const anjay_dm_internal_attrs_t) { .standard = { .min_period = 4, @@ -1767,7 +1824,8 @@ AVS_UNIT_TEST(dm_effective_attrs, fallback_to_server) { anjay_dm_internal_res_attrs_t attrs; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_effective_attrs( anjay, &DM_EFFECTIVE_ATTRS_STANDARD_QUERY, &attrs)); - _anjay_mock_dm_assert_attributes_equal(&attrs, + _anjay_mock_dm_assert_attributes_equal( + &attrs, &(const anjay_dm_internal_res_attrs_t) { .standard = { .common = { @@ -1795,7 +1853,8 @@ AVS_UNIT_TEST(dm_effective_attrs, resource_fail) { AVS_UNIT_TEST(dm_effective_attrs, for_instance) { DM_TEST_INIT; (void) mocksocks; - _anjay_mock_dm_expect_instance_read_default_attrs(anjay, &OBJ, 69, 1, 0, + _anjay_mock_dm_expect_instance_read_default_attrs( + anjay, &OBJ, 69, 1, 0, &(const anjay_dm_internal_attrs_t) { .standard = { .min_period = 9, @@ -1806,7 +1865,8 @@ AVS_UNIT_TEST(dm_effective_attrs, for_instance) { anjay_dm_attrs_query_details_t details = DM_EFFECTIVE_ATTRS_STANDARD_QUERY; details.rid = -1; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_effective_attrs(anjay, &details, &attrs)); - _anjay_mock_dm_assert_attributes_equal(&attrs, + _anjay_mock_dm_assert_attributes_equal( + &attrs, &(const anjay_dm_internal_res_attrs_t) { .standard = { .common = { @@ -1836,7 +1896,8 @@ AVS_UNIT_TEST(dm_effective_attrs, instance_fail) { AVS_UNIT_TEST(dm_effective_attrs, for_object) { DM_TEST_INIT; (void) mocksocks; - _anjay_mock_dm_expect_object_read_default_attrs(anjay, &OBJ, 1, 0, + _anjay_mock_dm_expect_object_read_default_attrs( + anjay, &OBJ, 1, 0, &(const anjay_dm_internal_attrs_t) { .standard = { .min_period = 6, @@ -1848,7 +1909,8 @@ AVS_UNIT_TEST(dm_effective_attrs, for_object) { details.rid = -1; details.iid = ANJAY_IID_INVALID; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_effective_attrs(anjay, &details, &attrs)); - _anjay_mock_dm_assert_attributes_equal(&attrs, + _anjay_mock_dm_assert_attributes_equal( + &attrs, &(const anjay_dm_internal_res_attrs_t) { .standard = { .common = { @@ -1901,19 +1963,18 @@ AVS_UNIT_TEST(dm_effective_attrs, server_default) { details.rid = -1; details.iid = ANJAY_IID_INVALID; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_effective_attrs(anjay, &details, &attrs)); - _anjay_mock_dm_assert_attributes_equal(&attrs, + _anjay_mock_dm_assert_attributes_equal( + &attrs, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = 0, - .max_period = 404 - }, - .greater_than = ANJAY_ATTRIB_VALUE_NONE, - .less_than = ANJAY_ATTRIB_VALUE_NONE, - .step = ANJAY_ATTRIB_VALUE_NONE - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = 0, + .max_period = 404 + }, + .greater_than = ANJAY_ATTRIB_VALUE_NONE, + .less_than = ANJAY_ATTRIB_VALUE_NONE, + .step = ANJAY_ATTRIB_VALUE_NONE + } }); DM_TEST_FINISH; } @@ -1929,19 +1990,18 @@ AVS_UNIT_TEST(dm_effective_attrs, no_server) { details.rid = -1; details.iid = ANJAY_IID_INVALID; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_effective_attrs(anjay, &details, &attrs)); - _anjay_mock_dm_assert_attributes_equal(&attrs, + _anjay_mock_dm_assert_attributes_equal( + &attrs, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = ANJAY_DM_DEFAULT_PMIN_VALUE, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - }, - .greater_than = ANJAY_ATTRIB_VALUE_NONE, - .less_than = ANJAY_ATTRIB_VALUE_NONE, - .step = ANJAY_ATTRIB_VALUE_NONE - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = ANJAY_DM_DEFAULT_PMIN_VALUE, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + }, + .greater_than = ANJAY_ATTRIB_VALUE_NONE, + .less_than = ANJAY_ATTRIB_VALUE_NONE, + .step = ANJAY_ATTRIB_VALUE_NONE + } }); DM_TEST_FINISH; } @@ -1967,19 +2027,18 @@ AVS_UNIT_TEST(dm_effective_attrs, no_resources) { details.rid = -1; details.iid = ANJAY_IID_INVALID; AVS_UNIT_ASSERT_SUCCESS(_anjay_dm_effective_attrs(anjay, &details, &attrs)); - _anjay_mock_dm_assert_attributes_equal(&attrs, + _anjay_mock_dm_assert_attributes_equal( + &attrs, &(const anjay_dm_internal_res_attrs_t) { - _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER - .standard = { - .common = { - .min_period = ANJAY_DM_DEFAULT_PMIN_VALUE, - .max_period = ANJAY_ATTRIB_PERIOD_NONE - }, - .greater_than = ANJAY_ATTRIB_VALUE_NONE, - .less_than = ANJAY_ATTRIB_VALUE_NONE, - .step = ANJAY_ATTRIB_VALUE_NONE - } - }); + _ANJAY_DM_CUSTOM_ATTRS_INITIALIZER.standard = { + .common = { + .min_period = ANJAY_DM_DEFAULT_PMIN_VALUE, + .max_period = ANJAY_ATTRIB_PERIOD_NONE + }, + .greater_than = ANJAY_ATTRIB_VALUE_NONE, + .less_than = ANJAY_ATTRIB_VALUE_NONE, + .step = ANJAY_ATTRIB_VALUE_NONE + } }); DM_TEST_FINISH; } @@ -2079,7 +2138,7 @@ AVS_UNIT_TEST(dm_resource_operations, nonwritable_resource) { _anjay_mock_dm_expect_resource_operations(anjay, &OBJ_WITH_RES_OPS, 4, ANJAY_DM_RESOURCE_OP_BIT_R, 0); // 4.05 Method Not Allowed - DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, METHOD_NOT_ALLOWED,ID(0xfa3e), + DM_TEST_EXPECT_RESPONSE(mocksocks[0], ACK, METHOD_NOT_ALLOWED, ID(0xfa3e), NO_PAYLOAD); AVS_UNIT_ASSERT_SUCCESS(anjay_serve(anjay, mocksocks[0])); DM_TEST_FINISH; diff --git a/src/test/io.c b/src/test/io.c index 106e5d1f8..7483baf72 100644 --- a/src/test/io.c +++ b/src/test/io.c @@ -21,33 +21,32 @@ #include -#define TEST_ENV(Data) \ - avs_stream_abstract_t *stream = NULL; \ - AVS_UNIT_ASSERT_SUCCESS(avs_unit_memstream_alloc(&stream, sizeof(Data))); \ +#define TEST_ENV(Data) \ + avs_stream_abstract_t *stream = NULL; \ + AVS_UNIT_ASSERT_SUCCESS(avs_unit_memstream_alloc(&stream, sizeof(Data))); \ AVS_UNIT_ASSERT_SUCCESS(avs_stream_write(stream, Data, sizeof(Data) - 1)); \ - anjay_input_ctx_t *ctx; \ + anjay_input_ctx_t *ctx; \ AVS_UNIT_ASSERT_SUCCESS(_anjay_input_tlv_create(&ctx, &stream, true)); -#define TEST_TEARDOWN \ - _anjay_input_ctx_destroy(&ctx) +#define TEST_TEARDOWN _anjay_input_ctx_destroy(&ctx) AVS_UNIT_TEST(input_array, example) { - TEST_ENV( // example from spec 6.3.3.2 + TEST_ENV( // example from spec 6.3.3.2 "\x08\x00\x11" // Object Instance 0 "\xC1\x00\x03" // Resource 0 - Object ID == 3 "\xC1\x01\x01" // Resource 1 - Instance ID == 1 - "\x86\x02" // Resource 2 - ACL array + "\x86\x02" // Resource 2 - ACL array "\x41\x01\xE0" // [1] -> -32 "\x41\x02\x80" // [2] -> -128 "\xC1\x03\x01" // Resource 3 - ACL owner == 1 "\x08\x01\x11" // Object Instance 1 "\xC1\x00\x04" // Resource 0 - Object ID == 4 "\xC1\x01\x02" // Resource 1 - Instance ID == 2 - "\x86\x02" // Resource 2 - ACL array + "\x86\x02" // Resource 2 - ACL array "\x41\x01\x80" // [1] -> -128 "\x41\x02\x80" // [2] -> -128 "\xC1\x03\x01" // Resource 3 - ACL owner == 1 - ); + ); anjay_id_type_t type; uint16_t id; diff --git a/src/test/sched.c b/src/test/sched.c index 4731a69cf..ba866d5a6 100644 --- a/src/test/sched.c +++ b/src/test/sched.c @@ -16,11 +16,11 @@ #include -#include #include +#include static void increment_task(anjay_t *anjay, const void *counter_ptr_ptr) { - (void)anjay; + (void) anjay; ++**(int *const *) counter_ptr_ptr; } @@ -30,9 +30,7 @@ typedef struct { static sched_test_env_t setup_test(void) { _anjay_mock_clock_start(avs_time_monotonic_from_scalar(0, AVS_TIME_S)); - return (sched_test_env_t){ - _anjay_sched_new(NULL) - }; + return (sched_test_env_t) { _anjay_sched_new(NULL) }; } static void teardown_test(sched_test_env_t *env) { @@ -45,9 +43,9 @@ AVS_UNIT_TEST(sched, sched_now) { int counter = 0; anjay_sched_handle_t task = NULL; - AVS_UNIT_ASSERT_SUCCESS( - _anjay_sched_now(env.sched, &task, increment_task, - &(int *) { &counter }, sizeof(int *))); + AVS_UNIT_ASSERT_SUCCESS(_anjay_sched_now(env.sched, &task, increment_task, + &(int *) { &counter }, + sizeof(int *))); AVS_UNIT_ASSERT_NOT_NULL(task); AVS_UNIT_ASSERT_EQUAL(1, _anjay_sched_run(env.sched)); AVS_UNIT_ASSERT_EQUAL(1, counter); @@ -63,9 +61,9 @@ AVS_UNIT_TEST(sched, sched_delayed) { avs_time_duration_from_scalar(1, AVS_TIME_S); int counter = 0; anjay_sched_handle_t task = NULL; - AVS_UNIT_ASSERT_SUCCESS( - _anjay_sched(env.sched, &task, delay, increment_task, - &(int *) { &counter }, sizeof(int *))); + AVS_UNIT_ASSERT_SUCCESS(_anjay_sched(env.sched, &task, delay, + increment_task, &(int *) { &counter }, + sizeof(int *))); AVS_UNIT_ASSERT_NOT_NULL(task); AVS_UNIT_ASSERT_EQUAL(0, _anjay_sched_run(env.sched)); AVS_UNIT_ASSERT_EQUAL(0, counter); @@ -86,9 +84,9 @@ AVS_UNIT_TEST(sched, sched_del) { avs_time_duration_from_scalar(1, AVS_TIME_S); int counter = 0; anjay_sched_handle_t task = NULL; - AVS_UNIT_ASSERT_SUCCESS( - _anjay_sched(env.sched, &task, delay, increment_task, - &(int *) { &counter }, sizeof(int *))); + AVS_UNIT_ASSERT_SUCCESS(_anjay_sched(env.sched, &task, delay, + increment_task, &(int *) { &counter }, + sizeof(int *))); AVS_UNIT_ASSERT_NOT_NULL(task); AVS_UNIT_ASSERT_EQUAL(0, _anjay_sched_run(env.sched)); AVS_UNIT_ASSERT_EQUAL(0, counter); diff --git a/src/test/utils.c b/src/test/utils.c index 5adf27162..34e1be14c 100644 --- a/src/test/utils.c +++ b/src/test/utils.c @@ -23,26 +23,27 @@ #include #include -#define ANJAY_URL_EMPTY \ - (anjay_url_t) { \ - .uri_path = NULL, .uri_query = NULL \ +#define ANJAY_URL_EMPTY \ + (anjay_url_t) { \ + .uri_path = NULL, \ + .uri_query = NULL \ } #define AUTO_URL(Name) \ __attribute__((__cleanup__(_anjay_url_cleanup))) anjay_url_t Name - AVS_UNIT_TEST(parse_url, invalid_protocol_terminator) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap:/acs.avsystem.com", - &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap:/acs.avsystem.com", &parsed_url)); } AVS_UNIT_TEST(parse_url, square_bracket_enclosed_host_address_too_long) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - char url[ANJAY_MAX_URL_HOSTNAME_SIZE + sizeof("coap://[]") + 1] = "coap://["; + char url[ANJAY_MAX_URL_HOSTNAME_SIZE + sizeof("coap://[]") + 1] = + "coap://["; memset(url + sizeof("coap://[") - 1, 'A', sizeof(url) - sizeof("coap://[") - 1); @@ -54,8 +55,8 @@ AVS_UNIT_TEST(parse_url, square_bracket_enclosed_host_address_too_long) { AVS_UNIT_TEST(parse_url, without_credentials_port_and_path) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_SUCCESS(_anjay_parse_url("coap://acs.avsystem.com", - &parsed_url)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_parse_url("coap://acs.avsystem.com", &parsed_url)); AVS_UNIT_ASSERT_EQUAL(parsed_url.protocol, ANJAY_URL_PROTOCOL_COAP); AVS_UNIT_ASSERT_EQUAL_STRING(parsed_url.host, "acs.avsystem.com"); AVS_UNIT_ASSERT_EQUAL_STRING(parsed_url.port, ""); @@ -71,43 +72,43 @@ AVS_UNIT_TEST(parse_url, with_port_and_path) { AVS_UNIT_TEST(parse_url, without_password_with_user) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://user@acs.avsystem.com:123", - &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://user@acs.avsystem.com:123", &parsed_url)); } AVS_UNIT_TEST(parse_url, with_empty_user) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://@acs.avsystem.com:123", - &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://@acs.avsystem.com:123", &parsed_url)); } AVS_UNIT_TEST(parse_url, with_user_and_empty_password) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://user:@acs.avsystem.com:123", - &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://user:@acs.avsystem.com:123", &parsed_url)); } AVS_UNIT_TEST(parse_url, with_empty_user_and_empty_password) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://:@acs.avsystem.com:123", - &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://:@acs.avsystem.com:123", &parsed_url)); } AVS_UNIT_TEST(parse_url, with_user_and_password) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://user:password@acs.avsystem.com:123", - &parsed_url)); + AVS_UNIT_ASSERT_FAILED(_anjay_parse_url( + "coap://user:password@acs.avsystem.com:123", &parsed_url)); } AVS_UNIT_TEST(parse_url, escaped_credentials) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://user%25:p%40ssword@acs.avsystem.com", - &parsed_url)); + AVS_UNIT_ASSERT_FAILED(_anjay_parse_url( + "coap://user%25:p%40ssword@acs.avsystem.com", &parsed_url)); } AVS_UNIT_TEST(parse_url, coaps_url) { @@ -115,20 +116,21 @@ AVS_UNIT_TEST(parse_url, coaps_url) { { parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_SUCCESS(_anjay_parse_url("coaps://[12::34]", &parsed_url)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_parse_url("coaps://[12::34]", &parsed_url)); AVS_UNIT_ASSERT_EQUAL(parsed_url.protocol, ANJAY_URL_PROTOCOL_COAPS); AVS_UNIT_ASSERT_EQUAL_STRING(parsed_url.host, "12::34"); } { parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_SUCCESS(_anjay_parse_url("coaps://acs.avsystem.com", - &parsed_url)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_parse_url("coaps://acs.avsystem.com", &parsed_url)); AVS_UNIT_ASSERT_EQUAL(parsed_url.protocol, ANJAY_URL_PROTOCOL_COAPS); } { parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_SUCCESS(_anjay_parse_url("coaps://acs.avsystem.com:123", - &parsed_url)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_parse_url("coaps://acs.avsystem.com:123", &parsed_url)); AVS_UNIT_ASSERT_EQUAL(parsed_url.protocol, ANJAY_URL_PROTOCOL_COAPS); } } @@ -136,38 +138,38 @@ AVS_UNIT_TEST(parse_url, coaps_url) { AVS_UNIT_TEST(parse_url, null_in_username_and_password) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://user%00:password@acs.avsystem.com", - &parsed_url)); - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://user:pas%00sword@acs.avsystem.com", - &parsed_url)); + AVS_UNIT_ASSERT_FAILED(_anjay_parse_url( + "coap://user%00:password@acs.avsystem.com", &parsed_url)); + AVS_UNIT_ASSERT_FAILED(_anjay_parse_url( + "coap://user:pas%00sword@acs.avsystem.com", &parsed_url)); } AVS_UNIT_TEST(parse_url, port_length) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_SUCCESS(_anjay_parse_url("coap://acs.avsystem.com:1234", - &parsed_url)); - AVS_UNIT_ASSERT_SUCCESS(_anjay_parse_url("coap://acs.avsystem.com:12345", - &parsed_url)); - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://acs.avsystem.com:123456", - &parsed_url)); - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://acs.avsystem.com:1234567", - &parsed_url)); - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://acs.avsystem.com:", - &parsed_url)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_parse_url("coap://acs.avsystem.com:1234", &parsed_url)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_parse_url("coap://acs.avsystem.com:12345", &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://acs.avsystem.com:123456", &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://acs.avsystem.com:1234567", &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://acs.avsystem.com:", &parsed_url)); } AVS_UNIT_TEST(parse_url, port_invalid_characters) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_SUCCESS(_anjay_parse_url("coap://acs.avsystem.com:12345", - &parsed_url)); - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://acs.avsystem.com:1_234", - &parsed_url)); - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://acs.avsystem.com:http", - &parsed_url)); - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://acs.avsystem.com:12345_", - &parsed_url)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_parse_url("coap://acs.avsystem.com:12345", &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://acs.avsystem.com:1_234", &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://acs.avsystem.com:http", &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://acs.avsystem.com:12345_", &parsed_url)); } AVS_UNIT_TEST(parse_url, ipv6_address) { @@ -189,21 +191,21 @@ AVS_UNIT_TEST(parse_url, ipv6_address_with_port_and_path) { AVS_UNIT_TEST(parse_url, ipv6_address_with_credentials) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://user%25:p%40ssword@[12::34]:56/78", - &parsed_url)); + AVS_UNIT_ASSERT_FAILED(_anjay_parse_url( + "coap://user%25:p%40ssword@[12::34]:56/78", &parsed_url)); } AVS_UNIT_TEST(parse_url, invalid_ipv6_address) { anjay_url_t parsed_url = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_SUCCESS(_anjay_parse_url("coap://[12:ff:ff::34]", - &parsed_url)); - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://12:ff:ff::34]", - &parsed_url)); - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://[12:ff:ff::34", - &parsed_url)); - AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://[12:ff:ff::34]:", - &parsed_url)); + AVS_UNIT_ASSERT_SUCCESS( + _anjay_parse_url("coap://[12:ff:ff::34]", &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://12:ff:ff::34]", &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://[12:ff:ff::34", &parsed_url)); + AVS_UNIT_ASSERT_FAILED( + _anjay_parse_url("coap://[12:ff:ff::34]:", &parsed_url)); } AVS_UNIT_TEST(parse_url, empty_host) { @@ -259,8 +261,7 @@ AVS_UNIT_TEST(parse_url, basic_segments) { AVS_UNIT_TEST(parse_url, one_segment_empty) { AUTO_URL(url) = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_SUCCESS( - _anjay_parse_url("coaps://avsystem.com//", &url)); + AVS_UNIT_ASSERT_SUCCESS(_anjay_parse_url("coaps://avsystem.com//", &url)); AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(url.uri_path), 1); AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(url.uri_query), 0); @@ -269,8 +270,7 @@ AVS_UNIT_TEST(parse_url, one_segment_empty) { AVS_UNIT_TEST(parse_url, two_segments_empty) { AUTO_URL(url) = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_SUCCESS( - _anjay_parse_url("coaps://avsystem.com///", &url)); + AVS_UNIT_ASSERT_SUCCESS(_anjay_parse_url("coaps://avsystem.com///", &url)); AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(url.uri_path), 2); AVS_UNIT_ASSERT_EQUAL(AVS_LIST_SIZE(url.uri_query), 0); @@ -341,8 +341,7 @@ AVS_UNIT_TEST(parse_url, weird_query) { AVS_UNIT_TEST(parse_url, bad_percent_encoding) { AUTO_URL(url) = ANJAY_URL_EMPTY; - AVS_UNIT_ASSERT_FAILED( - _anjay_parse_url("coap://avsystem.com/fo%xa", &url)); + AVS_UNIT_ASSERT_FAILED(_anjay_parse_url("coap://avsystem.com/fo%xa", &url)); AVS_UNIT_ASSERT_FAILED( _anjay_parse_url("coap://avsystem.com/foo?b%xar", &url)); AVS_UNIT_ASSERT_NULL(url.uri_query); diff --git a/src/utils_core.c b/src/utils_core.c index 3033bb093..338b8eff2 100644 --- a/src/utils_core.c +++ b/src/utils_core.c @@ -45,19 +45,20 @@ static int url_parse_chunks(const char **url, const char *ptr = *url; do { if ((*ptr == '\0' || *ptr == delimiter || *ptr == parser_terminator) - && ptr != *url) { - const size_t chunk_len = (size_t)(ptr - *url - 1); + && ptr != *url) { + const size_t chunk_len = (size_t) (ptr - *url - 1); if (hint == URL_PARSE_HINT_SKIP_TRAILING_SEPARATOR - && (*ptr == '\0' || *ptr == parser_terminator) && !chunk_len) { + && (*ptr == '\0' || *ptr == parser_terminator) + && !chunk_len) { // trailing separator, ignoring *url = ptr; return 0; } if (out_chunks) { - AVS_LIST(anjay_string_t) chunk = - (AVS_LIST(anjay_string_t)) AVS_LIST_NEW_BUFFER(chunk_len + 1); + AVS_LIST(anjay_string_t) chunk = (AVS_LIST( + anjay_string_t)) AVS_LIST_NEW_BUFFER(chunk_len + 1); if (!chunk) { anjay_log(ERROR, "out of memory"); return -1; @@ -69,7 +70,7 @@ static int url_parse_chunks(const char **url, memcpy(chunk, *url + 1, chunk_len); if (avs_url_percent_decode(chunk->c_str, - &(size_t[]) { 0 }[0])) { + &(size_t[]){ 0 }[0])) { return -1; } } @@ -137,13 +138,13 @@ int _anjay_parse_url(const char *raw_url, anjay_url_t *out_parsed_url) { if (!result) { const char *path_ptr = avs_url_path(avs_url); if (path_ptr) { - result = url_parse_chunks( - &path_ptr, '/', '?', URL_PARSE_HINT_SKIP_TRAILING_SEPARATOR, - &out_parsed_url->uri_path); + result = url_parse_chunks(&path_ptr, '/', '?', + URL_PARSE_HINT_SKIP_TRAILING_SEPARATOR, + &out_parsed_url->uri_path); if (!result && *path_ptr == '?') { - result = url_parse_chunks( - &path_ptr, '&', '\0', URL_PARSE_HINT_NONE, - &out_parsed_url->uri_query); + result = url_parse_chunks(&path_ptr, '&', '\0', + URL_PARSE_HINT_NONE, + &out_parsed_url->uri_query); } } } @@ -168,14 +169,14 @@ uint32_t _anjay_rand32(anjay_rand_seed_t *seed) { #else -#if AVS_RAND_MAX >= UINT32_MAX -#define RAND32_ITERATIONS 1 -#elif AVS_RAND_MAX >= UINT16_MAX -#define RAND32_ITERATIONS 2 -#else +# if AVS_RAND_MAX >= UINT32_MAX +# define RAND32_ITERATIONS 1 +# elif AVS_RAND_MAX >= UINT16_MAX +# define RAND32_ITERATIONS 2 +# else /* standard guarantees RAND_MAX to be at least 32767 */ -#define RAND32_ITERATIONS 3 -#endif +# define RAND32_ITERATIONS 3 +# endif uint32_t _anjay_rand32(anjay_rand_seed_t *seed) { uint32_t result = 0; @@ -219,7 +220,7 @@ AVS_LIST(const anjay_string_t) _anjay_make_string_list(const char *string, while (str) { size_t len = strlen(str) + 1; if (!(*strings_list_endptr = - (AVS_LIST(anjay_string_t)) AVS_LIST_NEW_BUFFER(len))) { + (AVS_LIST(anjay_string_t)) AVS_LIST_NEW_BUFFER(len))) { anjay_log(ERROR, "out of memory"); AVS_LIST_CLEAR(&strings_list); break; @@ -235,8 +236,8 @@ AVS_LIST(const anjay_string_t) _anjay_make_string_list(const char *string, } bool anjay_binding_mode_valid(const char *binding_mode) { - static const char *const VALID_BINDINGS[] = - { "U", "UQ", "S", "SQ", "US", "UQS" }; + static const char *const VALID_BINDINGS[] = { "U", "UQ", "S", + "SQ", "US", "UQS" }; for (size_t i = 0; i < AVS_ARRAY_SIZE(VALID_BINDINGS); ++i) { if (strcmp(binding_mode, VALID_BINDINGS[i]) == 0) { return true; @@ -253,7 +254,8 @@ static int append_string_query_arg(AVS_LIST(const anjay_string_t) *list, (AVS_LIST(anjay_string_t)) AVS_LIST_NEW_BUFFER(size); if (!arg - || avs_simple_snprintf(arg->c_str, size, "%s=%s", name, value) < 0) { + || avs_simple_snprintf(arg->c_str, size, "%s=%s", name, value) + < 0) { AVS_LIST_CLEAR(&arg); } else { AVS_LIST_APPEND(list, arg); @@ -283,8 +285,10 @@ _anjay_make_query_string_list(const char *version, size_t lt_size = sizeof("lt=") + 16; AVS_LIST(anjay_string_t) lt = (AVS_LIST(anjay_string_t)) AVS_LIST_NEW_BUFFER(lt_size); - if (!lt || avs_simple_snprintf(lt->c_str, lt_size, - "lt=%" PRId64, *lifetime) < 0) { + if (!lt + || avs_simple_snprintf(lt->c_str, lt_size, "lt=%" PRId64, + *lifetime) + < 0) { goto fail; } AVS_LIST_APPEND(&list, lt); @@ -319,11 +323,12 @@ static int bind_socket(avs_net_abstract_socket_t *socket, if (bind_conf->static_port_preference) { if (avs_simple_snprintf(static_preferred_port, sizeof(static_preferred_port), "%" PRIu16, - bind_conf->static_port_preference) < 0) { + bind_conf->static_port_preference) + < 0) { AVS_UNREACHABLE("Could not convert preferred port number"); } } else if (bind_conf->last_local_port_buffer - && **bind_conf->last_local_port_buffer) { + && **bind_conf->last_local_port_buffer) { if (!avs_net_socket_bind(socket, local_addr, *bind_conf->last_local_port_buffer)) { return 0; @@ -354,8 +359,8 @@ int _anjay_bind_and_connect_socket(avs_net_abstract_socket_t *socket, } if (avs_net_socket_connect(socket, remote_host, remote_port)) { - anjay_log(ERROR, "could not connect to %s:%s", - remote_host, remote_port); + anjay_log(ERROR, "could not connect to %s:%s", remote_host, + remote_port); return -1; } @@ -374,12 +379,12 @@ int _anjay_bind_and_connect_socket(avs_net_abstract_socket_t *socket, return 0; } -int -_anjay_create_connected_udp_socket(avs_net_abstract_socket_t **out, - avs_net_socket_type_t type, - const void *socket_config, - const anjay_socket_bind_config_t *bind_conf, - const anjay_url_t *uri) { +int _anjay_create_connected_udp_socket( + avs_net_abstract_socket_t **out, + avs_net_socket_type_t type, + const void *socket_config, + const anjay_socket_bind_config_t *bind_conf, + const anjay_url_t *uri) { int result = 0; assert(!*out); @@ -387,33 +392,33 @@ _anjay_create_connected_udp_socket(avs_net_abstract_socket_t **out, case AVS_NET_UDP_SOCKET: case AVS_NET_DTLS_SOCKET: if (avs_net_socket_create(out, type, socket_config)) { - result = -ENOMEM; + result = ENOMEM; anjay_log(ERROR, "could not create CoAP socket"); goto fail; } - if (_anjay_bind_and_connect_socket(*out, bind_conf, - uri->host, uri->port)) { + if (_anjay_bind_and_connect_socket(*out, bind_conf, uri->host, + uri->port)) { goto fail; } return 0; default: anjay_log(ERROR, "unsupported socket type requested: %d", type); - return -EPROTONOSUPPORT; + return EPROTONOSUPPORT; } fail: if (*out) { - result = -avs_net_socket_errno(*out); + result = avs_net_socket_errno(*out); } if (!result) { - result = -EPROTO; + result = EPROTO; } avs_net_socket_cleanup(out); return result; } #ifdef ANJAY_TEST -#include "test/utils.c" +# include "test/utils.c" #endif // ANJAY_TEST diff --git a/src/utils_core.h b/src/utils_core.h index dae380316..2c5a09b6f 100644 --- a/src/utils_core.h +++ b/src/utils_core.h @@ -39,8 +39,7 @@ int _anjay_copy_string_list(AVS_LIST(const anjay_string_t) *outptr, AVS_LIST(const anjay_string_t) input); AVS_LIST(const anjay_string_t) -_anjay_make_string_list(const char *string, - ... /* strings */) AVS_F_SENTINEL; +_anjay_make_string_list(const char *string, ... /* strings */) AVS_F_SENTINEL; // Const pointer cast is to ensure, that passed NULL will have the proper type, // regardless of toolchain used @@ -78,12 +77,12 @@ int _anjay_bind_and_connect_socket(avs_net_abstract_socket_t *socket, const char *remote_host, const char *remote_port); -int -_anjay_create_connected_udp_socket(avs_net_abstract_socket_t **out, - avs_net_socket_type_t type, - const void *socket_config, - const anjay_socket_bind_config_t *bind_conf, - const anjay_url_t *uri); +int _anjay_create_connected_udp_socket( + avs_net_abstract_socket_t **out, + avs_net_socket_type_t type, + const void *socket_config, + const anjay_socket_bind_config_t *bind_conf, + const anjay_url_t *uri); static inline size_t _anjay_max_power_of_2_not_greater_than(size_t bound) { int exponent = -1; diff --git a/test/codegen/CMakeLists.txt b/test/codegen/CMakeLists.txt index 84f010088..a92709572 100644 --- a/test/codegen/CMakeLists.txt +++ b/test/codegen/CMakeLists.txt @@ -47,9 +47,13 @@ endforeach() add_library(codegen_check OBJECT EXCLUDE_FROM_ALL ${CODEGEN_SOURCES}) set_target_properties(codegen_check PROPERTIES COMPILE_FLAGS "-Wno-missing-declarations -Wno-unused-variable -Wno-unused-parameter") +target_include_directories(codegen_check PRIVATE + $) add_library(codegen_check_cxx OBJECT EXCLUDE_FROM_ALL ${CODEGEN_CXX_SOURCES}) set_target_properties(codegen_check_cxx PROPERTIES COMPILE_FLAGS "-Wno-missing-declarations -Wno-unused-variable -Wno-unused-parameter") +target_include_directories(codegen_check_cxx PRIVATE + $) add_dependencies(check codegen_check codegen_check_cxx) diff --git a/test/doc/runtest.py b/test/doc/runtest.py index 111394681..fc1360387 100755 --- a/test/doc/runtest.py +++ b/test/doc/runtest.py @@ -19,9 +19,9 @@ import logging import os import re -import requests +import grequests import sys -from functools import lru_cache +from collections import defaultdict ROOT_DIR = os.path.abspath(os.path.dirname(__file__)) HTTP_STATUS_OK = 200 @@ -29,14 +29,6 @@ REGEX = r'<(http.*?)>`_' DEFAULT_DOC_PATH = os.path.normpath(os.path.join(ROOT_DIR, '../../doc/sphinx/source')) -@lru_cache() -def is_valid_url(url): - logging.info('Processing %s', url) - try: - return requests.head(url, allow_redirects=True).status_code == HTTP_STATUS_OK - except requests.ConnectionError: - return False - def explore(path): for root, directories, file_names in os.walk(path): for file_name in file_names: @@ -44,7 +36,7 @@ def explore(path): if file_path.lower().endswith(FILE_EXTENSION): with open(file_path, encoding="utf-8") as f: content = f.read() - yield (file_name, content) + yield (file_path, content) def find_urls(rst_content): lines = enumerate(rst_content.splitlines(), 1) @@ -52,19 +44,28 @@ def find_urls(rst_content): for line_number, line_content in lines for found_url in re.findall(REGEX, line_content)) -def find_invalid_urls(rst_content): - return ((line_number, found_url) - for line_number, found_url in find_urls(rst_content) - if not is_valid_url(found_url)) +def find_invalid_urls(urls): + responses = grequests.map(grequests.head(url, allow_redirects=True) + for url in urls) + invalid_urls = defaultdict(list) + for url, status in zip(urls, responses): + if not status or status.status_code != HTTP_STATUS_OK: + invalid_urls[url] = urls[url] + return invalid_urls def report(path): - found_invalid_urls = [(file_name, line_number, found_url) - for file_name, content in explore(path) - for line_number, found_url in find_invalid_urls(content)] - if found_invalid_urls: + urls = defaultdict(list) + for file_path, content in explore(path): + for line_number, found_url in find_urls(content): + urls[found_url].append((file_path, line_number)) + + invalid_urls = find_invalid_urls(urls) + if invalid_urls: logging.warning('There are invalid urls.') - for file_name, line_number, url in found_invalid_urls: - print('%s\t%s\t%s' % (file_name, line_number, url)) + for (url, details) in invalid_urls.items(): + print('URL: %s in:' % url) + for item in details: + print('\t%s:%s' % item) sys.exit(-1) else: logging.info('All urls are valid.') diff --git a/test/fuzz/coap/stream.c b/test/fuzz/coap/stream.c index 3e0c02062..7b81eddea 100644 --- a/test/fuzz/coap/stream.c +++ b/test/fuzz/coap/stream.c @@ -19,20 +19,24 @@ #include #include -#include #include +#include #include #include "../../../src/coap/coap_stream.h" -static int success() { return 0; } -static int fail() { return -1; } +static int success() { + return 0; +} +static int fail() { + return -1; +} static int stdin_recv(avs_net_abstract_socket_t *socket, size_t *out_bytes_received, void *buffer, size_t buffer_length) { - (void)socket; + (void) socket; size_t bytes_read = fread(buffer, 1, buffer_length, stdin); if (bytes_read == 0 || bytes_read >= buffer_length) { @@ -46,8 +50,8 @@ static int stdin_recv(avs_net_abstract_socket_t *socket, static int stdin_get_opt(avs_net_abstract_socket_t *socket, avs_net_socket_opt_key_t opt, avs_net_socket_opt_value_t *value) { - (void)socket; - (void)opt; + (void) socket; + (void) opt; if (opt == AVS_NET_SOCKET_OPT_RECV_TIMEOUT) { value->recv_timeout = avs_time_duration_from_scalar(1, AVS_TIME_S); } else { @@ -59,39 +63,37 @@ static int stdin_get_opt(avs_net_abstract_socket_t *socket, static const avs_net_socket_v_table_t STDIN_SOCKET_VTABLE = { .receive = stdin_recv, - .connect = (avs_net_socket_connect_t)success, - .bind = (avs_net_socket_bind_t)success, - .close = (avs_net_socket_close_t)success, - .cleanup = (avs_net_socket_cleanup_t)success, - - .decorate = (avs_net_socket_decorate_t)fail, - .send = (avs_net_socket_send_t)fail, - .send_to = (avs_net_socket_send_to_t)fail, - .receive_from = (avs_net_socket_receive_from_t)fail, - .accept = (avs_net_socket_accept_t)fail, - .shutdown = (avs_net_socket_shutdown_t)fail, - .get_system_socket = (avs_net_socket_get_system_t)fail, - .get_interface_name = (avs_net_socket_get_interface_t)fail, - .get_remote_host = (avs_net_socket_get_remote_host_t)fail, - .get_remote_port = (avs_net_socket_get_remote_port_t)fail, - .get_local_port = (avs_net_socket_get_local_port_t)fail, + .connect = (avs_net_socket_connect_t) success, + .bind = (avs_net_socket_bind_t) success, + .close = (avs_net_socket_close_t) success, + .cleanup = (avs_net_socket_cleanup_t) success, + + .decorate = (avs_net_socket_decorate_t) fail, + .send = (avs_net_socket_send_t) fail, + .send_to = (avs_net_socket_send_to_t) fail, + .receive_from = (avs_net_socket_receive_from_t) fail, + .accept = (avs_net_socket_accept_t) fail, + .shutdown = (avs_net_socket_shutdown_t) fail, + .get_system_socket = (avs_net_socket_get_system_t) fail, + .get_interface_name = (avs_net_socket_get_interface_t) fail, + .get_remote_host = (avs_net_socket_get_remote_host_t) fail, + .get_remote_port = (avs_net_socket_get_remote_port_t) fail, + .get_local_port = (avs_net_socket_get_local_port_t) fail, .get_opt = stdin_get_opt, - .set_opt = (avs_net_socket_set_opt_t)success, - .get_errno = (avs_net_socket_errno_t)success, + .set_opt = (avs_net_socket_set_opt_t) success, + .get_errno = (avs_net_socket_errno_t) success, }; struct stdin_socket { const avs_net_socket_v_table_t *const vtable; -} stdin_socket_struct = { - &STDIN_SOCKET_VTABLE -}; +} stdin_socket_struct = { &STDIN_SOCKET_VTABLE }; int main(int argc, char **argv) { - (void)argc; - (void)argv; + (void) argc; + (void) argv; avs_net_abstract_socket_t *stdin_socket = - (avs_net_abstract_socket_t*)&stdin_socket_struct; + (avs_net_abstract_socket_t *) &stdin_socket_struct; avs_coap_ctx_t *coap = NULL; avs_stream_abstract_t *stream = NULL; @@ -105,12 +107,12 @@ int main(int argc, char **argv) { char message_finished = 0; if (avs_coap_ctx_create(&coap, 0) - || _anjay_coap_stream_create(&stream, coap, - in_buffer, sizeof(in_buffer), - out_buffer, sizeof(out_buffer)) + || _anjay_coap_stream_create(&stream, coap, in_buffer, + sizeof(in_buffer), out_buffer, + sizeof(out_buffer)) || avs_stream_net_setsock(stream, stdin_socket) - || avs_stream_read(stream, &bytes_read, &message_finished, - buffer, sizeof(buffer))) { + || avs_stream_read(stream, &bytes_read, &message_finished, buffer, + sizeof(buffer))) { retval = -1; } diff --git a/test/fuzz/coap/stream_request.c b/test/fuzz/coap/stream_request.c index 32299d732..76f310e80 100644 --- a/test/fuzz/coap/stream_request.c +++ b/test/fuzz/coap/stream_request.c @@ -16,12 +16,12 @@ #include +#include #include #include -#include -#include #include +#include #include #include "../../../src/coap/coap_stream.h" @@ -31,13 +31,15 @@ typedef struct mock_socket { uint16_t last_msg_id; } mock_socket_t; -static int success() { return 0; } +static int success() { + return 0; +} static int mock_recv(avs_net_abstract_socket_t *socket, size_t *out_bytes_received, void *buffer, size_t buffer_length) { - (void)socket; + (void) socket; assert(buffer_length >= 4); // version @@ -50,17 +52,16 @@ static int mock_recv(avs_net_abstract_socket_t *socket, *out_bytes_received = 4; memcpy(buffer, EMPTY_ACK, sizeof(EMPTY_ACK) - 1); - uint16_t id = ((mock_socket_t*)socket)->last_msg_id; - memcpy((char*)buffer + sizeof(EMPTY_ACK) - 1, &id, sizeof(id)); + uint16_t id = ((mock_socket_t *) socket)->last_msg_id; + memcpy((char *) buffer + sizeof(EMPTY_ACK) - 1, &id, sizeof(id)); return 0; } -static int mock_send(avs_net_abstract_socket_t *socket, - const void *buffer, - size_t size) { +static int +mock_send(avs_net_abstract_socket_t *socket, const void *buffer, size_t size) { assert(size >= 4); - memcpy(&((mock_socket_t*)socket)->last_msg_id, - (const char*)buffer + 2, + memcpy(&((mock_socket_t *) socket)->last_msg_id, + (const char *) buffer + 2, sizeof(uint16_t)); return 0; } @@ -69,23 +70,23 @@ static const avs_net_socket_v_table_t MOCK_SOCKET_VTABLE = { .receive = mock_recv, .send = mock_send, - .accept = (avs_net_socket_accept_t) success, - .bind = (avs_net_socket_bind_t) success, - .cleanup = (avs_net_socket_cleanup_t) success, - .close = (avs_net_socket_close_t) success, - .connect = (avs_net_socket_connect_t) success, - .decorate = (avs_net_socket_decorate_t) success, - .get_interface_name = (avs_net_socket_get_interface_t) success, - .get_local_port = (avs_net_socket_get_local_port_t) success, - .get_opt = (avs_net_socket_get_opt_t) success, - .get_remote_host = (avs_net_socket_get_remote_host_t) success, - .get_remote_port = (avs_net_socket_get_remote_port_t) success, - .get_system_socket = (avs_net_socket_get_system_t) success, - .get_errno = (avs_net_socket_errno_t) success, - .receive_from = (avs_net_socket_receive_from_t) success, - .send_to = (avs_net_socket_send_to_t) success, - .set_opt = (avs_net_socket_set_opt_t) success, - .shutdown = (avs_net_socket_shutdown_t) success, + .accept = (avs_net_socket_accept_t) success, + .bind = (avs_net_socket_bind_t) success, + .cleanup = (avs_net_socket_cleanup_t) success, + .close = (avs_net_socket_close_t) success, + .connect = (avs_net_socket_connect_t) success, + .decorate = (avs_net_socket_decorate_t) success, + .get_interface_name = (avs_net_socket_get_interface_t) success, + .get_local_port = (avs_net_socket_get_local_port_t) success, + .get_opt = (avs_net_socket_get_opt_t) success, + .get_remote_host = (avs_net_socket_get_remote_host_t) success, + .get_remote_port = (avs_net_socket_get_remote_port_t) success, + .get_system_socket = (avs_net_socket_get_system_t) success, + .get_errno = (avs_net_socket_errno_t) success, + .receive_from = (avs_net_socket_receive_from_t) success, + .send_to = (avs_net_socket_send_to_t) success, + .set_opt = (avs_net_socket_set_opt_t) success, + .shutdown = (avs_net_socket_shutdown_t) success, }; mock_socket_t mock_socket_struct = { @@ -101,74 +102,72 @@ enum { OP_SET_ERROR, }; -static void perform_op(FILE *cmd_stream, - avs_stream_abstract_t *stream) { +static void perform_op(FILE *cmd_stream, avs_stream_abstract_t *stream) { char cmd; if (fread(&cmd, 1, 1, cmd_stream) != 1) { return; } switch (cmd) { - case OP_SETUP_REQUEST: - { - anjay_msg_details_t details; - avs_coap_token_t token; - - memset(&details, 0, sizeof(details)); - - if (fread(&details, - sizeof(details.msg_type) + sizeof(details.msg_code) - + sizeof(details.format) + sizeof(details.observe_serial), - 1, cmd_stream) != 1 - || details.msg_type < _AVS_COAP_MSG_FIRST - || details.msg_type > _AVS_COAP_MSG_LAST - || fread(&token.size, 1, 1, cmd_stream) != 1 - || token.size > 8 - || (token.size > 0 && fread(&token.bytes, token.size, 1, cmd_stream) != 1)) { - return; - } - - _anjay_coap_stream_setup_request(stream, &details, &token); + case OP_SETUP_REQUEST: { + anjay_msg_details_t details; + avs_coap_token_t token; + + memset(&details, 0, sizeof(details)); + + if (fread(&details, + sizeof(details.msg_type) + sizeof(details.msg_code) + + sizeof(details.format) + + sizeof(details.observe_serial), + 1, cmd_stream) + != 1 + || details.msg_type < _AVS_COAP_MSG_FIRST + || details.msg_type > _AVS_COAP_MSG_LAST + || fread(&token.size, 1, 1, cmd_stream) != 1 || token.size > 8 + || (token.size > 0 + && fread(&token.bytes, token.size, 1, cmd_stream) != 1)) { + return; } - case OP_WRITE: - { - uint16_t size; - char buffer[UINT16_MAX]; - if (fread(&size, sizeof(size), 1, cmd_stream) != 1 - || fread(buffer, size, 1, cmd_stream) != 1) { - return; - } + _anjay_coap_stream_setup_request(stream, &details, &token); + } + case OP_WRITE: { + uint16_t size; + char buffer[UINT16_MAX]; - avs_stream_write(stream, buffer, (size_t)size); + if (fread(&size, sizeof(size), 1, cmd_stream) != 1 + || fread(buffer, size, 1, cmd_stream) != 1) { return; } + + avs_stream_write(stream, buffer, (size_t) size); + return; + } case OP_FINISH_MESSAGE: avs_stream_finish_message(stream); return; case OP_RESET: avs_stream_reset(stream); return; - case OP_SET_ERROR: - { - uint8_t code; - if (fread(&code, sizeof(code), 1, cmd_stream) != 1) { - return; - } - - _anjay_coap_stream_set_error(stream, code); + case OP_SET_ERROR: { + uint8_t code; + if (fread(&code, sizeof(code), 1, cmd_stream) != 1) { + return; } + + _anjay_coap_stream_set_error(stream, code); + } default: return; } } int main(int argc, char **argv) { - (void)argc; - (void)argv; + (void) argc; + (void) argv; avs_net_abstract_socket_t *mock_socket = - (avs_net_abstract_socket_t*)&mock_socket_struct; + (avs_net_abstract_socket_t *) &mock_socket_struct; avs_coap_ctx_t *coap = NULL; avs_stream_abstract_t *stream = NULL; @@ -178,16 +177,15 @@ int main(int argc, char **argv) { int retval = -1; if (avs_coap_ctx_create(&coap, 0) - || _anjay_coap_stream_create(&stream, coap, - in_buffer, sizeof(in_buffer), - out_buffer, sizeof(out_buffer)) + || _anjay_coap_stream_create(&stream, coap, in_buffer, + sizeof(in_buffer), out_buffer, + sizeof(out_buffer)) || avs_stream_net_setsock(stream, mock_socket)) { goto exit; } while (!feof(stdin)) { perform_op(stdin, stream); - } retval = 0; diff --git a/test/fuzz/coap/stream_response.c b/test/fuzz/coap/stream_response.c index e9f976594..1adc6e569 100644 --- a/test/fuzz/coap/stream_response.c +++ b/test/fuzz/coap/stream_response.c @@ -16,12 +16,12 @@ #include +#include #include #include -#include -#include #include +#include #include #include "../../../src/coap/coap_stream.h" @@ -31,13 +31,15 @@ typedef struct mock_socket { uint16_t last_msg_id; } mock_socket_t; -static int success() { return 0; } +static int success() { + return 0; +} static int mock_recv(avs_net_abstract_socket_t *socket, size_t *out_bytes_received, void *buffer, size_t buffer_length) { - (void)socket; + (void) socket; assert(buffer_length >= 4); // version @@ -50,31 +52,31 @@ static int mock_recv(avs_net_abstract_socket_t *socket, *out_bytes_received = 4; memcpy(buffer, EMPTY_CONFIRMABLE, sizeof(EMPTY_CONFIRMABLE) - 1); - uint16_t id = ((mock_socket_t*)socket)->last_msg_id; - memcpy((char*)buffer + sizeof(EMPTY_CONFIRMABLE) - 1, &id, sizeof(id)); + uint16_t id = ((mock_socket_t *) socket)->last_msg_id; + memcpy((char *) buffer + sizeof(EMPTY_CONFIRMABLE) - 1, &id, sizeof(id)); return 0; } static const avs_net_socket_v_table_t MOCK_SOCKET_VTABLE = { .receive = mock_recv, - .accept = (avs_net_socket_accept_t) success, - .bind = (avs_net_socket_bind_t) success, - .cleanup = (avs_net_socket_cleanup_t) success, - .close = (avs_net_socket_close_t) success, - .connect = (avs_net_socket_connect_t) success, - .decorate = (avs_net_socket_decorate_t) success, - .get_interface_name = (avs_net_socket_get_interface_t) success, - .get_local_port = (avs_net_socket_get_local_port_t) success, - .get_opt = (avs_net_socket_get_opt_t) success, - .get_remote_host = (avs_net_socket_get_remote_host_t) success, - .get_remote_port = (avs_net_socket_get_remote_port_t) success, - .get_system_socket = (avs_net_socket_get_system_t) success, - .get_errno = (avs_net_socket_errno_t) success, - .receive_from = (avs_net_socket_receive_from_t) success, - .send = (avs_net_socket_send_t) success, - .send_to = (avs_net_socket_send_to_t) success, - .set_opt = (avs_net_socket_set_opt_t) success, - .shutdown = (avs_net_socket_shutdown_t) success, + .accept = (avs_net_socket_accept_t) success, + .bind = (avs_net_socket_bind_t) success, + .cleanup = (avs_net_socket_cleanup_t) success, + .close = (avs_net_socket_close_t) success, + .connect = (avs_net_socket_connect_t) success, + .decorate = (avs_net_socket_decorate_t) success, + .get_interface_name = (avs_net_socket_get_interface_t) success, + .get_local_port = (avs_net_socket_get_local_port_t) success, + .get_opt = (avs_net_socket_get_opt_t) success, + .get_remote_host = (avs_net_socket_get_remote_host_t) success, + .get_remote_port = (avs_net_socket_get_remote_port_t) success, + .get_system_socket = (avs_net_socket_get_system_t) success, + .get_errno = (avs_net_socket_errno_t) success, + .receive_from = (avs_net_socket_receive_from_t) success, + .send = (avs_net_socket_send_t) success, + .send_to = (avs_net_socket_send_to_t) success, + .set_opt = (avs_net_socket_set_opt_t) success, + .shutdown = (avs_net_socket_shutdown_t) success, }; mock_socket_t mock_socket_struct = { @@ -91,74 +93,71 @@ enum { OP_SET_ERROR, }; -static void perform_op(FILE *cmd_stream, - avs_stream_abstract_t *stream) { +static void perform_op(FILE *cmd_stream, avs_stream_abstract_t *stream) { char cmd; if (fread(&cmd, 1, 1, cmd_stream) != 1) { return; } switch (cmd) { - case OP_GET_INCOMING_MSG: - { - const avs_coap_msg_t *msg; - if (_anjay_coap_stream_get_incoming_msg(stream, &msg) == 0) { - assert(avs_coap_msg_is_valid(msg)); - } + case OP_GET_INCOMING_MSG: { + const avs_coap_msg_t *msg; + if (_anjay_coap_stream_get_incoming_msg(stream, &msg) == 0) { + assert(avs_coap_msg_is_valid(msg)); } - case OP_SETUP_RESPONSE: - { - anjay_msg_details_t details; - memset(&details, 0, sizeof(details)); - - if (fread(&details, - sizeof(details.msg_type) + sizeof(details.msg_code) - + sizeof(details.format) + sizeof(details.observe_serial), - 1, cmd_stream) != 1) { - return; - } - - _anjay_coap_stream_setup_response(stream, &details); + } + case OP_SETUP_RESPONSE: { + anjay_msg_details_t details; + memset(&details, 0, sizeof(details)); + + if (fread(&details, + sizeof(details.msg_type) + sizeof(details.msg_code) + + sizeof(details.format) + + sizeof(details.observe_serial), + 1, cmd_stream) + != 1) { + return; } - case OP_WRITE: - { - uint16_t size; - char buffer[UINT16_MAX]; - if (fread(&size, sizeof(size), 1, cmd_stream) != 1 - || fread(buffer, size, 1, cmd_stream) != 1) { - return; - } + _anjay_coap_stream_setup_response(stream, &details); + } + case OP_WRITE: { + uint16_t size; + char buffer[UINT16_MAX]; - avs_stream_write(stream, buffer, (size_t)size); + if (fread(&size, sizeof(size), 1, cmd_stream) != 1 + || fread(buffer, size, 1, cmd_stream) != 1) { return; } + + avs_stream_write(stream, buffer, (size_t) size); + return; + } case OP_FINISH_MESSAGE: avs_stream_finish_message(stream); return; case OP_RESET: avs_stream_reset(stream); return; - case OP_SET_ERROR: - { - uint8_t code; - if (fread(&code, sizeof(code), 1, cmd_stream) != 1) { - return; - } - - _anjay_coap_stream_set_error(stream, code); + case OP_SET_ERROR: { + uint8_t code; + if (fread(&code, sizeof(code), 1, cmd_stream) != 1) { + return; } + + _anjay_coap_stream_set_error(stream, code); + } default: return; } } int main(int argc, char **argv) { - (void)argc; - (void)argv; + (void) argc; + (void) argv; avs_net_abstract_socket_t *mock_socket = - (avs_net_abstract_socket_t*)&mock_socket_struct; + (avs_net_abstract_socket_t *) &mock_socket_struct; avs_coap_ctx_t *coap = NULL; avs_stream_abstract_t *stream = NULL; @@ -168,16 +167,15 @@ int main(int argc, char **argv) { int retval = -1; if (avs_coap_ctx_create(&coap, 0) - || _anjay_coap_stream_create(&stream, coap, - in_buffer, sizeof(in_buffer), - out_buffer, sizeof(out_buffer)) + || _anjay_coap_stream_create(&stream, coap, in_buffer, + sizeof(in_buffer), out_buffer, + sizeof(out_buffer)) || avs_stream_net_setsock(stream, mock_socket)) { goto exit; } while (!feof(stdin)) { perform_op(stdin, stream); - } retval = 0; diff --git a/test/include/anjay_test/dm.h b/test/include/anjay_test/dm.h index 7b740948b..89707a4d4 100644 --- a/test/include/anjay_test/dm.h +++ b/test/include/anjay_test/dm.h @@ -33,20 +33,23 @@ avs_net_abstract_socket_t *_anjay_test_dm_install_socket(anjay_t *anjay, anjay_ssid_t ssid); void _anjay_test_dm_finish(anjay_t *anjay); -int _anjay_test_dm_fake_security_instance_it(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t *out, - void **cookie); - -int _anjay_test_dm_fake_security_instance_present(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid); - -int _anjay_test_dm_fake_security_read(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_output_ctx_t *ctx); +int _anjay_test_dm_fake_security_instance_it( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t *out, + void **cookie); + +int _anjay_test_dm_fake_security_instance_present( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid); + +int _anjay_test_dm_fake_security_read( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_output_ctx_t *ctx); static inline int _anjay_test_dm_instance_reset_NOOP(anjay_t *anjay, @@ -58,185 +61,172 @@ _anjay_test_dm_instance_reset_NOOP(anjay_t *anjay, return 0; } -static const anjay_dm_object_def_t *const OBJ = - &(const anjay_dm_object_def_t) { - .oid = 42, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS(0, 1, 2, 3, 4, 5, 6), - .handlers = { - ANJAY_MOCK_DM_HANDLERS, - .instance_reset = _anjay_test_dm_instance_reset_NOOP - } - }; +static const anjay_dm_object_def_t *const OBJ = &(const anjay_dm_object_def_t) { + .oid = 42, + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(0, 1, 2, 3, 4, 5, 6), + .handlers = { ANJAY_MOCK_DM_HANDLERS, + .instance_reset = _anjay_test_dm_instance_reset_NOOP } +}; static const anjay_dm_object_def_t *const OBJ_NOATTRS = &(const anjay_dm_object_def_t) { .oid = 93, .supported_rids = ANJAY_DM_SUPPORTED_RIDS(0, 1, 2, 3, 4, 5, 6), - .handlers = { - ANJAY_MOCK_DM_HANDLERS_NOATTRS, - .instance_reset = _anjay_test_dm_instance_reset_NOOP - } + .handlers = { ANJAY_MOCK_DM_HANDLERS_NOATTRS, + .instance_reset = _anjay_test_dm_instance_reset_NOOP } }; static const anjay_dm_object_def_t *const OBJ_WITH_RESET = &(const anjay_dm_object_def_t) { .oid = 25, .supported_rids = ANJAY_DM_SUPPORTED_RIDS(0, 1, 2, 3, 4, 5, 6), - .handlers = { - ANJAY_MOCK_DM_HANDLERS, - .instance_reset = _anjay_mock_dm_instance_reset - } + .handlers = { ANJAY_MOCK_DM_HANDLERS, + .instance_reset = _anjay_mock_dm_instance_reset } }; -static anjay_dm_object_def_t *const EXECUTE_OBJ = - &(anjay_dm_object_def_t) { - .oid = 128, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS(0, 1, 2, 3, 4, 5, 6), - .handlers = { - ANJAY_MOCK_DM_HANDLERS - } - }; - -static const anjay_dm_object_def_t *const FAKE_SECURITY = - &(const anjay_dm_object_def_t) { - .oid = 0, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - ANJAY_DM_RID_SECURITY_BOOTSTRAP, - ANJAY_DM_RID_SECURITY_SSID, - ANJAY_DM_RID_SECURITY_BOOTSTRAP_TIMEOUT), - .handlers = { - .instance_it = _anjay_test_dm_fake_security_instance_it, - .instance_present = _anjay_test_dm_fake_security_instance_present, - .resource_present = anjay_dm_resource_present_TRUE, - .resource_read = _anjay_test_dm_fake_security_read, - .transaction_begin = anjay_dm_transaction_NOOP, - .transaction_validate = anjay_dm_transaction_NOOP, - .transaction_commit = anjay_dm_transaction_NOOP, - .transaction_rollback = anjay_dm_transaction_NOOP - } - }; - -static const anjay_dm_object_def_t *const FAKE_SECURITY2 = - &(const anjay_dm_object_def_t) { - .oid = 0, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - ANJAY_DM_RID_SECURITY_SERVER_URI, - ANJAY_DM_RID_SECURITY_BOOTSTRAP, - ANJAY_DM_RID_SECURITY_MODE, - ANJAY_DM_RID_SECURITY_SSID), - .handlers = { - ANJAY_MOCK_DM_HANDLERS - } - }; - -static const anjay_dm_object_def_t *const FAKE_SERVER = - &(const anjay_dm_object_def_t) { - .oid = 1, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS( - ANJAY_DM_RID_SERVER_SSID, - ANJAY_DM_RID_SERVER_LIFETIME, - ANJAY_DM_RID_SERVER_DEFAULT_PMIN, - ANJAY_DM_RID_SERVER_DEFAULT_PMAX, - ANJAY_DM_RID_SERVER_NOTIFICATION_STORING, - ANJAY_DM_RID_SERVER_BINDING), - .handlers = { - ANJAY_MOCK_DM_HANDLERS - } - }; - -static const anjay_dm_object_def_t *const OBJ_WITH_RES_OPS = - &(const anjay_dm_object_def_t) { - .oid = 667, - .supported_rids = ANJAY_DM_SUPPORTED_RIDS(4), - .handlers = { - ANJAY_MOCK_DM_HANDLERS, - .resource_operations = _anjay_mock_dm_resource_operations - } - }; - -#define DM_TEST_CONFIGURATION(...) \ - &(anjay_configuration_t) { \ - .endpoint_name = "urn:dev:os:anjay-test", \ - .in_buffer_size = 4096, \ - .out_buffer_size = 4096, \ - __VA_ARGS__ \ - } +static anjay_dm_object_def_t *const EXECUTE_OBJ = &(anjay_dm_object_def_t) { + .oid = 128, + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(0, 1, 2, 3, 4, 5, 6), + .handlers = { ANJAY_MOCK_DM_HANDLERS } +}; + +static const anjay_dm_object_def_t *const FAKE_SECURITY = &( + const anjay_dm_object_def_t) { + .oid = 0, + .supported_rids = + ANJAY_DM_SUPPORTED_RIDS(ANJAY_DM_RID_SECURITY_BOOTSTRAP, + ANJAY_DM_RID_SECURITY_SSID, + ANJAY_DM_RID_SECURITY_BOOTSTRAP_TIMEOUT), + .handlers = { + .instance_it = _anjay_test_dm_fake_security_instance_it, + .instance_present = _anjay_test_dm_fake_security_instance_present, + .resource_present = anjay_dm_resource_present_TRUE, + .resource_read = _anjay_test_dm_fake_security_read, + .transaction_begin = anjay_dm_transaction_NOOP, + .transaction_validate = anjay_dm_transaction_NOOP, + .transaction_commit = anjay_dm_transaction_NOOP, + .transaction_rollback = anjay_dm_transaction_NOOP + } +}; + +static const anjay_dm_object_def_t *const FAKE_SECURITY2 = &( + const anjay_dm_object_def_t) { + .oid = 0, + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(ANJAY_DM_RID_SECURITY_SERVER_URI, + ANJAY_DM_RID_SECURITY_BOOTSTRAP, + ANJAY_DM_RID_SECURITY_MODE, + ANJAY_DM_RID_SECURITY_SSID), + .handlers = { ANJAY_MOCK_DM_HANDLERS } +}; + +static const anjay_dm_object_def_t *const FAKE_SERVER = &( + const anjay_dm_object_def_t) { + .oid = 1, + .supported_rids = + ANJAY_DM_SUPPORTED_RIDS(ANJAY_DM_RID_SERVER_SSID, + ANJAY_DM_RID_SERVER_LIFETIME, + ANJAY_DM_RID_SERVER_DEFAULT_PMIN, + ANJAY_DM_RID_SERVER_DEFAULT_PMAX, + ANJAY_DM_RID_SERVER_NOTIFICATION_STORING, + ANJAY_DM_RID_SERVER_BINDING), + .handlers = { ANJAY_MOCK_DM_HANDLERS } +}; + +static const anjay_dm_object_def_t *const OBJ_WITH_RES_OPS = &( + const anjay_dm_object_def_t) { + .oid = 667, + .supported_rids = ANJAY_DM_SUPPORTED_RIDS(4), + .handlers = { ANJAY_MOCK_DM_HANDLERS, + .resource_operations = _anjay_mock_dm_resource_operations } +}; + +#define DM_TEST_CONFIGURATION(...) \ + &(anjay_configuration_t) { \ + .endpoint_name = "urn:dev:os:anjay-test", \ + .in_buffer_size = 4096, \ + .out_buffer_size = 4096, __VA_ARGS__ \ + } #define DM_TEST_ESCAPE_PARENS_IMPL(...) __VA_ARGS__ #define DM_TEST_ESCAPE_PARENS(...) DM_TEST_ESCAPE_PARENS_IMPL __VA_ARGS__ -#define DM_TEST_INIT_GENERIC(Objects, Ssids, AddConfig) \ +#define DM_TEST_INIT_GENERIC(Objects, Ssids, AddConfig) \ _anjay_mock_clock_start(avs_time_monotonic_from_scalar(1000, AVS_TIME_S)); \ - anjay_t *anjay = _anjay_test_dm_init( \ - DM_TEST_CONFIGURATION(DM_TEST_ESCAPE_PARENS(AddConfig))); \ - const anjay_dm_object_def_t *const *obj_defs[] = \ - { DM_TEST_ESCAPE_PARENS(Objects) }; \ - for (size_t i = 0; i < AVS_ARRAY_SIZE(obj_defs); ++i) { \ - AVS_UNIT_ASSERT_SUCCESS(anjay_register_object(anjay, obj_defs[i])); \ - } \ - anjay_ssid_t ssids[] = { DM_TEST_ESCAPE_PARENS(Ssids) }; \ - avs_net_abstract_socket_t *mocksocks[AVS_ARRAY_SIZE(ssids)]; \ - for (size_t i = AVS_ARRAY_SIZE(ssids) - 1; \ - i < AVS_ARRAY_SIZE(ssids); --i) { \ - mocksocks[i] = _anjay_test_dm_install_socket(anjay, ssids[i]); \ - avs_unit_mocksock_enable_recv_timeout_getsetopt( \ - mocksocks[i], avs_time_duration_from_scalar(1, AVS_TIME_S)); \ - avs_unit_mocksock_enable_inner_mtu_getopt(mocksocks[i], 1252); \ - avs_unit_mocksock_enable_state_getopt(mocksocks[i]); \ - } \ - AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); \ + anjay_t *anjay = _anjay_test_dm_init( \ + DM_TEST_CONFIGURATION(DM_TEST_ESCAPE_PARENS(AddConfig))); \ + const anjay_dm_object_def_t *const *obj_defs[] = { DM_TEST_ESCAPE_PARENS( \ + Objects) }; \ + for (size_t i = 0; i < AVS_ARRAY_SIZE(obj_defs); ++i) { \ + AVS_UNIT_ASSERT_SUCCESS(anjay_register_object(anjay, obj_defs[i])); \ + } \ + anjay_ssid_t ssids[] = { DM_TEST_ESCAPE_PARENS(Ssids) }; \ + avs_net_abstract_socket_t *mocksocks[AVS_ARRAY_SIZE(ssids)]; \ + for (size_t i = AVS_ARRAY_SIZE(ssids) - 1; i < AVS_ARRAY_SIZE(ssids); \ + --i) { \ + mocksocks[i] = _anjay_test_dm_install_socket(anjay, ssids[i]); \ + avs_unit_mocksock_enable_recv_timeout_getsetopt( \ + mocksocks[i], avs_time_duration_from_scalar(1, AVS_TIME_S)); \ + avs_unit_mocksock_enable_inner_mtu_getopt(mocksocks[i], 1252); \ + avs_unit_mocksock_enable_state_getopt(mocksocks[i]); \ + } \ + AVS_UNIT_ASSERT_SUCCESS(anjay_sched_run(anjay)); \ _anjay_test_dm_unsched_reload_sockets(anjay) -#define DM_TEST_DEFAULT_OBJECTS \ - &OBJ, \ - &FAKE_SECURITY, \ - &FAKE_SERVER, \ - (const anjay_dm_object_def_t *const *) &OBJ_WITH_RES_OPS, \ - (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, \ - (const anjay_dm_object_def_t *const *) &OBJ_WITH_RESET +#define DM_TEST_DEFAULT_OBJECTS \ + &OBJ, &FAKE_SECURITY, &FAKE_SERVER, \ + (const anjay_dm_object_def_t *const *) &OBJ_WITH_RES_OPS, \ + (const anjay_dm_object_def_t *const *) &EXECUTE_OBJ, \ + (const anjay_dm_object_def_t *const *) &OBJ_WITH_RESET #define DM_TEST_INIT_WITH_OBJECTS(...) \ - DM_TEST_INIT_GENERIC((__VA_ARGS__), (1), ()) + DM_TEST_INIT_GENERIC((__VA_ARGS__), (1), ()) #define DM_TEST_INIT_WITH_SSIDS(...) \ - DM_TEST_INIT_GENERIC((DM_TEST_DEFAULT_OBJECTS), (__VA_ARGS__), ()) + DM_TEST_INIT_GENERIC((DM_TEST_DEFAULT_OBJECTS), (__VA_ARGS__), ()) #define DM_TEST_INIT DM_TEST_INIT_WITH_SSIDS(1) #define DM_TEST_INIT_WITH_CONFIG(...) \ - DM_TEST_INIT_GENERIC((DM_TEST_DEFAULT_OBJECTS), (1), (__VA_ARGS__)) + DM_TEST_INIT_GENERIC((DM_TEST_DEFAULT_OBJECTS), (1), (__VA_ARGS__)) #define DM_TEST_FINISH _anjay_test_dm_finish(anjay) -#define DM_TEST_EXPECT_RESPONSE(Mocksock, Type, Code, Id, \ - .../* Payload, Opts... */) do { \ - const avs_coap_msg_t *response = COAP_MSG(Type, Code, Id, __VA_ARGS__); \ - avs_unit_mocksock_expect_output(Mocksock, response->content, \ - response->length); \ -} while (0) - -#define DM_TEST_REQUEST(Mocksock, Type, Code, Id, \ - .../* Payload, Opts... */) do { \ - const avs_coap_msg_t *request = COAP_MSG(Type, Code, Id, __VA_ARGS__); \ - avs_unit_mocksock_input(Mocksock, request->content, request->length); \ -} while (0) - -#define DM_TEST_EXPECT_READ_NULL_ATTRS(Ssid, Iid, Rid) do { \ - _anjay_mock_dm_expect_instance_present(anjay, &OBJ, Iid, 1); \ - if (Rid >= 0) { \ - _anjay_mock_dm_expect_resource_present(anjay, &OBJ, Iid, (anjay_rid_t) Rid, \ - 1); \ - _anjay_mock_dm_expect_resource_read_attrs( \ - anjay, &OBJ, Iid, (anjay_rid_t) Rid, Ssid, 0, \ - &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); \ - } \ - _anjay_mock_dm_expect_instance_read_default_attrs( \ - anjay, &OBJ, Iid, Ssid, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); \ - _anjay_mock_dm_expect_object_read_default_attrs( \ - anjay, &OBJ, Ssid, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); \ - _anjay_mock_dm_expect_instance_it(anjay, &FAKE_SERVER, 0, 0, \ - ANJAY_IID_INVALID); \ -} while (0) +#define DM_TEST_EXPECT_RESPONSE( \ + Mocksock, Type, Code, Id, ... /* Payload, Opts... */) \ + do { \ + const avs_coap_msg_t *response = \ + COAP_MSG(Type, Code, Id, __VA_ARGS__); \ + avs_unit_mocksock_expect_output( \ + Mocksock, response->content, response->length); \ + } while (0) + +#define DM_TEST_REQUEST(Mocksock, Type, Code, Id, ... /* Payload, Opts... */) \ + do { \ + const avs_coap_msg_t *request = COAP_MSG(Type, Code, Id, __VA_ARGS__); \ + avs_unit_mocksock_input(Mocksock, request->content, request->length); \ + } while (0) + +#define DM_TEST_EXPECT_READ_NULL_ATTRS(Ssid, Iid, Rid) \ + do { \ + _anjay_mock_dm_expect_instance_present(anjay, &OBJ, Iid, 1); \ + if (Rid >= 0) { \ + _anjay_mock_dm_expect_resource_present( \ + anjay, &OBJ, Iid, (anjay_rid_t) Rid, 1); \ + _anjay_mock_dm_expect_resource_read_attrs( \ + anjay, \ + &OBJ, \ + Iid, \ + (anjay_rid_t) Rid, \ + Ssid, \ + 0, \ + &ANJAY_DM_INTERNAL_RES_ATTRS_EMPTY); \ + } \ + _anjay_mock_dm_expect_instance_read_default_attrs( \ + anjay, &OBJ, Iid, Ssid, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); \ + _anjay_mock_dm_expect_object_read_default_attrs( \ + anjay, &OBJ, Ssid, 0, &ANJAY_DM_INTERNAL_ATTRS_EMPTY); \ + _anjay_mock_dm_expect_instance_it( \ + anjay, &FAKE_SERVER, 0, 0, ANJAY_IID_INVALID); \ + } while (0) #endif /* ANJAY_TEST_DM_H */ - diff --git a/test/include/anjay_test/mock_dm.h b/test/include/anjay_test/mock_dm.h index a379dcb0d..01a6e1c87 100644 --- a/test/include/anjay_test/mock_dm.h +++ b/test/include/anjay_test/mock_dm.h @@ -62,94 +62,99 @@ struct anjay_mock_dm_data_array_struct { anjay_mock_dm_data_t value; }; -#define ANJAY_MOCK_DM_NONE (&(const anjay_mock_dm_data_t) { \ - .type = MOCK_DATA_NONE \ -}) +#define ANJAY_MOCK_DM_NONE \ + (&(const anjay_mock_dm_data_t) { \ + .type = MOCK_DATA_NONE \ + }) #pragma GCC diagnostic ignored "-Wunused-value" -#define ANJAY_MOCK_DM_BYTES(Retval, Str) (&(const anjay_mock_dm_data_t) { \ - .type = MOCK_DATA_BYTES, \ - .data = { \ - .bytes = { \ - .data = Str, \ - .length = sizeof(Str) - 1 \ - } \ - }, \ - .expected_retval = Retval \ -}) +#define ANJAY_MOCK_DM_BYTES(Retval, Str) \ + (&(const anjay_mock_dm_data_t) { \ + .type = MOCK_DATA_BYTES, \ + .data = { \ + .bytes = { \ + .data = Str, \ + .length = sizeof(Str) - 1 \ + } \ + }, \ + .expected_retval = Retval \ + }) -#define ANJAY_MOCK_DM_STRING(Retval, Str) (&(const anjay_mock_dm_data_t) {\ - .type = MOCK_DATA_STRING, \ - .data = { \ - .str = Str \ - }, \ - .expected_retval = Retval \ -}) +#define ANJAY_MOCK_DM_STRING(Retval, Str) \ + (&(const anjay_mock_dm_data_t) { \ + .type = MOCK_DATA_STRING, \ + .data = { \ + .str = Str \ + }, \ + .expected_retval = Retval \ + }) -#define ANJAY_MOCK_DM_INT(Retval, Value) (&(const anjay_mock_dm_data_t) { \ - .type = MOCK_DATA_INT, \ - .data = { \ - .i = Value \ - }, \ - .expected_retval = Retval \ -}) +#define ANJAY_MOCK_DM_INT(Retval, Value) \ + (&(const anjay_mock_dm_data_t) { \ + .type = MOCK_DATA_INT, \ + .data = { \ + .i = Value \ + }, \ + .expected_retval = Retval \ + }) #define ANJAY_MOCK_DM_FLOAT(Retval, Value) \ -(&(const anjay_mock_dm_data_t) { \ - .type = MOCK_DATA_FLOAT, \ - .data = { \ - .f = Value \ - }, \ - .expected_retval = Retval \ -}) + (&(const anjay_mock_dm_data_t) { \ + .type = MOCK_DATA_FLOAT, \ + .data = { \ + .f = Value \ + }, \ + .expected_retval = Retval \ + }) -#define ANJAY_MOCK_DM_BOOL(Retval, Value) (&(const anjay_mock_dm_data_t) {\ - .type = MOCK_DATA_BOOL, \ - .data = { \ - .b = Value \ - }, \ - .expected_retval = Retval \ -}) +#define ANJAY_MOCK_DM_BOOL(Retval, Value) \ + (&(const anjay_mock_dm_data_t) { \ + .type = MOCK_DATA_BOOL, \ + .data = { \ + .b = Value \ + }, \ + .expected_retval = Retval \ + }) #define ANJAY_MOCK_DM_OBJLNK(Retval, Oid, Iid) \ -(&(const anjay_mock_dm_data_t) { \ - .type = MOCK_DATA_OBJLNK, \ - .data = { \ - .objlnk = { \ - .oid = Oid, \ - .iid = Iid \ - } \ - }, \ - .expected_retval = Retval \ -}) + (&(const anjay_mock_dm_data_t) { \ + .type = MOCK_DATA_OBJLNK, \ + .data = { \ + .objlnk = { \ + .oid = Oid, \ + .iid = Iid \ + } \ + }, \ + .expected_retval = Retval \ + }) #define ANJAY_MOCK_DM_ARRAY_REST__(Arg, ...) __VA_ARGS__ -#define ANJAY_MOCK_DM_ARRAY_COMMON__(Type, ...) \ -(&(const anjay_mock_dm_data_t) { \ - .type = Type, \ - .data = { \ - .array = &(const anjay_mock_dm_data_array_t *const []) { \ - ANJAY_MOCK_DM_ARRAY_REST__(__VA_ARGS__, NULL) \ - }[0] \ - }, \ - .expected_retval = AVS_VARARG0(__VA_ARGS__) \ -}) +#define ANJAY_MOCK_DM_ARRAY_COMMON__(Type, ...) \ + (&(const anjay_mock_dm_data_t) { \ + .type = Type, \ + .data = { \ + .array = \ + &(const anjay_mock_dm_data_array_t *const[]) { \ + ANJAY_MOCK_DM_ARRAY_REST__(__VA_ARGS__, NULL) }[0] \ + }, \ + .expected_retval = AVS_VARARG0(__VA_ARGS__) \ + }) #define ANJAY_MOCK_DM_ARRAY(...) \ - ANJAY_MOCK_DM_ARRAY_COMMON__(MOCK_DATA_ARRAY, __VA_ARGS__) + ANJAY_MOCK_DM_ARRAY_COMMON__(MOCK_DATA_ARRAY, __VA_ARGS__) #define ANJAY_MOCK_DM_ARRAY_NOFINISH(...) \ - ANJAY_MOCK_DM_ARRAY_COMMON__(MOCK_DATA_ARRAY, __VA_ARGS__) + ANJAY_MOCK_DM_ARRAY_COMMON__(MOCK_DATA_ARRAY, __VA_ARGS__) -#define ANJAY_MOCK_DM_ARRAY_ENTRY(...) (&(const anjay_mock_dm_data_array_t) { \ - .index = AVS_VARARG0(__VA_ARGS__), \ - .value = *ANJAY_MOCK_DM_ARRAY_REST__(__VA_ARGS__) \ -}) +#define ANJAY_MOCK_DM_ARRAY_ENTRY(...) \ + (&(const anjay_mock_dm_data_array_t) { \ + .index = AVS_VARARG0(__VA_ARGS__), \ + .value = *ANJAY_MOCK_DM_ARRAY_REST__(__VA_ARGS__) \ + }) void _anjay_mock_dm_assert_common_attributes_equal( - const anjay_dm_internal_attrs_t *a, - const anjay_dm_internal_attrs_t *b); + const anjay_dm_internal_attrs_t *a, const anjay_dm_internal_attrs_t *b); void _anjay_mock_dm_assert_attributes_equal( const anjay_dm_internal_res_attrs_t *a, @@ -162,8 +167,10 @@ anjay_dm_instance_it_t _anjay_mock_dm_instance_it; anjay_dm_instance_present_t _anjay_mock_dm_instance_present; anjay_dm_instance_create_t _anjay_mock_dm_instance_create; anjay_dm_instance_remove_t _anjay_mock_dm_instance_remove; -anjay_dm_instance_read_default_attrs_t _anjay_mock_dm_instance_read_default_attrs; -anjay_dm_instance_write_default_attrs_t _anjay_mock_dm_instance_write_default_attrs; +anjay_dm_instance_read_default_attrs_t + _anjay_mock_dm_instance_read_default_attrs; +anjay_dm_instance_write_default_attrs_t + _anjay_mock_dm_instance_write_default_attrs; anjay_dm_resource_present_t _anjay_mock_dm_resource_present; anjay_dm_resource_read_t _anjay_mock_dm_resource_read; anjay_dm_resource_write_t _anjay_mock_dm_resource_write; @@ -173,124 +180,144 @@ anjay_dm_resource_read_attrs_t _anjay_mock_dm_resource_read_attrs; anjay_dm_resource_write_attrs_t _anjay_mock_dm_resource_write_attrs; anjay_dm_resource_operations_t _anjay_mock_dm_resource_operations; -#define ANJAY_MOCK_DM_HANDLERS_NOATTRS \ - .instance_it = _anjay_mock_dm_instance_it, \ +#define ANJAY_MOCK_DM_HANDLERS_NOATTRS \ + .instance_it = _anjay_mock_dm_instance_it, \ .instance_present = _anjay_mock_dm_instance_present, \ - .instance_create = _anjay_mock_dm_instance_create, \ - .instance_remove = _anjay_mock_dm_instance_remove, \ + .instance_create = _anjay_mock_dm_instance_create, \ + .instance_remove = _anjay_mock_dm_instance_remove, \ .resource_present = _anjay_mock_dm_resource_present, \ - .resource_read = _anjay_mock_dm_resource_read, \ - .resource_write = _anjay_mock_dm_resource_write, \ + .resource_read = _anjay_mock_dm_resource_read, \ + .resource_write = _anjay_mock_dm_resource_write, \ .resource_execute = _anjay_mock_dm_resource_execute, \ .resource_dim = _anjay_mock_dm_resource_dim -#define ANJAY_MOCK_DM_HANDLERS \ - ANJAY_MOCK_DM_HANDLERS_NOATTRS, \ - .object_read_default_attrs = _anjay_mock_dm_object_read_default_attrs, \ - .object_write_default_attrs = _anjay_mock_dm_object_write_default_attrs, \ - .instance_read_default_attrs = _anjay_mock_dm_instance_read_default_attrs, \ - .instance_write_default_attrs = _anjay_mock_dm_instance_write_default_attrs, \ - .resource_read_attrs = _anjay_mock_dm_resource_read_attrs, \ - .resource_write_attrs = _anjay_mock_dm_resource_write_attrs, \ - .transaction_begin = anjay_dm_transaction_NOOP, \ - .transaction_validate = anjay_dm_transaction_NOOP, \ - .transaction_commit = anjay_dm_transaction_NOOP, \ - .transaction_rollback = anjay_dm_transaction_NOOP \ +#define ANJAY_MOCK_DM_HANDLERS \ + ANJAY_MOCK_DM_HANDLERS_NOATTRS, \ + .object_read_default_attrs = \ + _anjay_mock_dm_object_read_default_attrs, \ + .object_write_default_attrs = \ + _anjay_mock_dm_object_write_default_attrs, \ + .instance_read_default_attrs = \ + _anjay_mock_dm_instance_read_default_attrs, \ + .instance_write_default_attrs = \ + _anjay_mock_dm_instance_write_default_attrs, \ + .resource_read_attrs = _anjay_mock_dm_resource_read_attrs, \ + .resource_write_attrs = _anjay_mock_dm_resource_write_attrs, \ + .transaction_begin = anjay_dm_transaction_NOOP, \ + .transaction_validate = anjay_dm_transaction_NOOP, \ + .transaction_commit = anjay_dm_transaction_NOOP, \ + .transaction_rollback = anjay_dm_transaction_NOOP -void _anjay_mock_dm_expect_object_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - int retval, - const anjay_dm_internal_attrs_t *attrs); -void _anjay_mock_dm_expect_object_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - const anjay_dm_internal_attrs_t *attrs, - int retval); -void _anjay_mock_dm_expect_instance_reset(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - int retval); -void _anjay_mock_dm_expect_instance_it(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - uintptr_t iteration, - int retval, - anjay_iid_t out); -void _anjay_mock_dm_expect_instance_present(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - int retval); -void _anjay_mock_dm_expect_instance_create(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - int retval, - anjay_iid_t out_iid); -void _anjay_mock_dm_expect_instance_remove(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - int retval); -void _anjay_mock_dm_expect_instance_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - int retval, - const anjay_dm_internal_attrs_t *attrs); -void _anjay_mock_dm_expect_instance_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - const anjay_dm_internal_attrs_t *attrs, - int retval); -void _anjay_mock_dm_expect_resource_present(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - int retval); -void _anjay_mock_dm_expect_resource_operations(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_rid_t rid, - anjay_dm_resource_op_mask_t mask, - int retval); -void _anjay_mock_dm_expect_resource_read(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - int retval, - const anjay_mock_dm_data_t *data); -void _anjay_mock_dm_expect_resource_write(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - const anjay_mock_dm_data_t *data, - int retval); -void _anjay_mock_dm_expect_resource_execute(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - const anjay_mock_dm_data_t *data, - int retval); -void _anjay_mock_dm_expect_resource_dim(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - int retval); -void _anjay_mock_dm_expect_resource_read_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_ssid_t ssid, - int retval, - const anjay_dm_internal_res_attrs_t *attrs); -void _anjay_mock_dm_expect_resource_write_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_ssid_t ssid, - const anjay_dm_internal_res_attrs_t *attrs, - int retval); +void _anjay_mock_dm_expect_object_read_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + int retval, + const anjay_dm_internal_attrs_t *attrs); +void _anjay_mock_dm_expect_object_write_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + const anjay_dm_internal_attrs_t *attrs, + int retval); +void _anjay_mock_dm_expect_instance_reset( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + int retval); +void _anjay_mock_dm_expect_instance_it( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + uintptr_t iteration, + int retval, + anjay_iid_t out); +void _anjay_mock_dm_expect_instance_present( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + int retval); +void _anjay_mock_dm_expect_instance_create( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + int retval, + anjay_iid_t out_iid); +void _anjay_mock_dm_expect_instance_remove( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + int retval); +void _anjay_mock_dm_expect_instance_read_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + int retval, + const anjay_dm_internal_attrs_t *attrs); +void _anjay_mock_dm_expect_instance_write_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + const anjay_dm_internal_attrs_t *attrs, + int retval); +void _anjay_mock_dm_expect_resource_present( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + int retval); +void _anjay_mock_dm_expect_resource_operations( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_rid_t rid, + anjay_dm_resource_op_mask_t mask, + int retval); +void _anjay_mock_dm_expect_resource_read( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + int retval, + const anjay_mock_dm_data_t *data); +void _anjay_mock_dm_expect_resource_write( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + const anjay_mock_dm_data_t *data, + int retval); +void _anjay_mock_dm_expect_resource_execute( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + const anjay_mock_dm_data_t *data, + int retval); +void _anjay_mock_dm_expect_resource_dim( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + int retval); +void _anjay_mock_dm_expect_resource_read_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_ssid_t ssid, + int retval, + const anjay_dm_internal_res_attrs_t *attrs); +void _anjay_mock_dm_expect_resource_write_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_ssid_t ssid, + const anjay_dm_internal_res_attrs_t *attrs, + int retval); void _anjay_mock_dm_expect_clean(void); void _anjay_mock_dm_expected_commands_clear(void); #endif /* ANJAY_TEST_MOCK_DM_H */ - diff --git a/test/include/anjay_test/utils.h b/test/include/anjay_test/utils.h index cf93004ce..671b61720 100644 --- a/test/include/anjay_test/utils.h +++ b/test/include/anjay_test/utils.h @@ -19,7 +19,6 @@ #include -#define SCOPED_PTR(Type, Deleter) \ - __attribute__((__cleanup__(Deleter))) Type * +#define SCOPED_PTR(Type, Deleter) __attribute__((__cleanup__(Deleter))) Type * #endif /* ANJAY_TEST_UTILS_H */ diff --git a/test/integration/framework/nsh-lwm2m/pymbedtls/src/context.cpp b/test/integration/framework/nsh-lwm2m/pymbedtls/src/context.cpp index f4f8f0d47..ba8270830 100644 --- a/test/integration/framework/nsh-lwm2m/pymbedtls/src/context.cpp +++ b/test/integration/framework/nsh-lwm2m/pymbedtls/src/context.cpp @@ -22,10 +22,8 @@ using namespace std; namespace ssl { -Context::Context(std::shared_ptr security, - bool debug) - : security_(security), - debug_(debug) { +Context::Context(std::shared_ptr security, bool debug) + : security_(security), debug_(debug) { memset(&session_cache_, 0, sizeof(session_cache_)); mbedtls_ssl_cache_init(&session_cache_); } @@ -34,4 +32,4 @@ Context::~Context() { mbedtls_ssl_cache_free(&session_cache_); } -} +} // namespace ssl diff --git a/test/integration/framework/nsh-lwm2m/pymbedtls/src/pymbedtls.cpp b/test/integration/framework/nsh-lwm2m/pymbedtls/src/pymbedtls.cpp index 7d92b64db..ecb8fedda 100644 --- a/test/integration/framework/nsh-lwm2m/pymbedtls/src/pymbedtls.cpp +++ b/test/integration/framework/nsh-lwm2m/pymbedtls/src/pymbedtls.cpp @@ -37,64 +37,61 @@ PYBIND11_MODULE(pymbedtls, m) { using namespace ssl; py::class_>(m, "SecurityInfo") - .def("name", &SecurityInfo::name) - ; + .def("name", &SecurityInfo::name); - py::class_>(m, "PskSecurity") - .def(py::init(), - py::arg("key"), - py::arg("identity")) - ; + py::class_>( + m, "PskSecurity") + .def(py::init(), + py::arg("key"), + py::arg("identity")); - py::class_>(m, "CertSecurity") - .def(py::init(), - py::arg("ca_path"), - py::arg("ca_file"), - py::arg("crt_file"), - py::arg("key_file")) - ; + py::class_>( + m, "CertSecurity") + .def(py::init(), + py::arg("ca_path"), + py::arg("ca_file"), + py::arg("crt_file"), + py::arg("key_file")); py::class_>(m, "Context") - .def(py::init, bool>(), - py::arg("security"), - py::arg("debug") = false) - ; + .def(py::init, bool>(), + py::arg("security"), + py::arg("debug") = false); py::class_(m, "ServerSocket") - .def(py::init, py::object>(), - py::arg("context"), - py::arg("socket")) - .def("accept", &ServerSocket::accept, - py::arg("handshake_timeouts_s") = py::none()) - .def("__getattr__", &ServerSocket::__getattr__) - .def("__setattr__", &ServerSocket::__setattr__) - ; + .def(py::init, py::object>(), + py::arg("context"), + py::arg("socket")) + .def("accept", &ServerSocket::accept, + py::arg("handshake_timeouts_s") = py::none()) + .def("__getattr__", &ServerSocket::__getattr__) + .def("__setattr__", &ServerSocket::__setattr__); - auto socket_scope = py::class_(m, "Socket") - .def(py::init, py::object, SocketType>(), - py::arg("context"), - py::arg("socket"), - py::arg("socket_type")) - .def("connect", &Socket::connect, - py::arg("address_port"), - py::arg("handshake_timeouts_s") = py::none()) - .def("send", &Socket::send) - .def("sendall", &Socket::send) - .def("sendto", &method_unimplemented) - .def("recv", &Socket::recv) - .def("recv_into", &method_unimplemented) - .def("recvfrom", &method_unimplemented) - .def("recvfrom_into", &method_unimplemented) - .def("settimeout", &Socket::settimeout) - .def("__getattr__", &Socket::__getattr__) - .def("__setattr__", &Socket::__setattr__) - ; + auto socket_scope = + py::class_(m, "Socket") + .def(py::init, py::object, + SocketType>(), + py::arg("context"), + py::arg("socket"), + py::arg("socket_type")) + .def("connect", &Socket::connect, py::arg("address_port"), + py::arg("handshake_timeouts_s") = py::none()) + .def("send", &Socket::send) + .def("sendall", &Socket::send) + .def("sendto", &method_unimplemented) + .def("recv", &Socket::recv) + .def("recv_into", &method_unimplemented) + .def("recvfrom", &method_unimplemented) + .def("recvfrom_into", &method_unimplemented) + .def("settimeout", &Socket::settimeout) + .def("__getattr__", &Socket::__getattr__) + .def("__setattr__", &Socket::__setattr__); py::enum_(socket_scope, "Type") - .value("Client", SocketType::Client) - .value("Server", SocketType::Server) - .export_values() - ; + .value("Client", SocketType::Client) + .value("Server", SocketType::Server) + .export_values(); // most verbose logs available mbedtls_debug_set_threshold(4); } diff --git a/test/integration/framework/nsh-lwm2m/pymbedtls/src/security.cpp b/test/integration/framework/nsh-lwm2m/pymbedtls/src/security.cpp index 2b372e2ce..471e771e3 100644 --- a/test/integration/framework/nsh-lwm2m/pymbedtls/src/security.cpp +++ b/test/integration/framework/nsh-lwm2m/pymbedtls/src/security.cpp @@ -27,12 +27,12 @@ using namespace std; namespace ssl { void PskSecurity::configure(Socket &socket) { - mbedtls_ssl_conf_psk( - &socket.config_, - reinterpret_cast(key_.data()), - key_.size(), - reinterpret_cast(identity_.data()), - identity_.size()); + mbedtls_ssl_conf_psk(&socket.config_, + reinterpret_cast(key_.data()), + key_.size(), + reinterpret_cast( + identity_.data()), + identity_.size()); static int psk_ciphersuites[2] = { MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8, 0 }; mbedtls_ssl_conf_ciphersuites(&socket.config_, psk_ciphersuites); @@ -54,19 +54,20 @@ CertSecurity::CertSecurity(const char *ca_path, int result; if (ca_path - && (result = mbedtls_x509_crt_parse_path(&ca_certs_, ca_path))) { + && (result = mbedtls_x509_crt_parse_path(&ca_certs_, ca_path))) { throw mbedtls_error(string("Could not load certificates from CA-path ") + ca_path, result); } if (ca_file - && (result = mbedtls_x509_crt_parse_file(&ca_certs_, ca_file))) { + && (result = mbedtls_x509_crt_parse_file(&ca_certs_, ca_file))) { throw mbedtls_error(string("Could not load certificate from CA-file ") + ca_file, result); } if (key_file - && (result = mbedtls_pk_parse_keyfile(&pk_ctx_, key_file, nullptr))) { + && (result = mbedtls_pk_parse_keyfile( + &pk_ctx_, key_file, nullptr))) { throw mbedtls_error( string("Could not parse private-key file ") + key_file, result); } diff --git a/test/integration/framework/nsh-lwm2m/pymbedtls/src/socket.cpp b/test/integration/framework/nsh-lwm2m/pymbedtls/src/socket.cpp index ec1d3dc3e..d000bfbdc 100644 --- a/test/integration/framework/nsh-lwm2m/pymbedtls/src/socket.cpp +++ b/test/integration/framework/nsh-lwm2m/pymbedtls/src/socket.cpp @@ -19,8 +19,8 @@ #include -#include #include +#include #include "common.hpp" #include "context.hpp" @@ -128,10 +128,10 @@ void debug_mbedtls(void * /*ctx*/, Socket::Socket(std::shared_ptr context, py::object py_socket, SocketType type) - : context_(context) - , type_(type) - , py_socket_(py_socket) - , in_handshake_(false) { + : context_(context), + type_(type), + py_socket_(py_socket), + in_handshake_(false) { mbedtls_ssl_init(&mbedtls_context_); // Zeroize cookie context. This prevents issuse // https://github.com/ARMmbed/mbedtls/issues/843. @@ -162,8 +162,7 @@ Socket::Socket(std::shared_ptr context, } // TODO - mbedtls_ssl_conf_min_version(&config_, - MBEDTLS_SSL_MAJOR_VERSION_3, + mbedtls_ssl_conf_min_version(&config_, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); mbedtls_ssl_conf_rng(&config_, mbedtls_ctr_drbg_random, &rng_); @@ -182,7 +181,8 @@ Socket::Socket(std::shared_ptr context, mbedtls_ssl_conf_session_cache(&config_, context_->session_cache(), mbedtls_ssl_cache_get, mbedtls_ssl_cache_set); - mbedtls_ssl_set_bio(&mbedtls_context_, this, &Socket::_send, NULL, &Socket::_recv); + mbedtls_ssl_set_bio(&mbedtls_context_, this, &Socket::_send, NULL, + &Socket::_recv); mbedtls_ssl_set_timer_cb(&mbedtls_context_, &timer_, mbedtls_timing_set_delay, mbedtls_timing_get_delay); @@ -204,8 +204,7 @@ void Socket::connect(py::tuple address_port, py::object handshake_timeouts_s_) { auto handshake_timeouts_s = py::cast(handshake_timeouts_s_); auto min = py::cast(handshake_timeouts_s[0]); auto max = py::cast(handshake_timeouts_s[1]); - mbedtls_ssl_conf_handshake_timeout(&config_, - uint32_t(min * 1000.0), + mbedtls_ssl_conf_handshake_timeout(&config_, uint32_t(min * 1000.0), uint32_t(max * 1000.0)); } @@ -240,7 +239,7 @@ void Socket::send(const string &data) { data.size() - total_sent); if (sent < 0) { if (sent == MBEDTLS_ERR_SSL_WANT_READ - || sent == MBEDTLS_ERR_SSL_WANT_WRITE) { + || sent == MBEDTLS_ERR_SSL_WANT_WRITE) { continue; } else { throw mbedtls_error("mbedtls_ssl_write failed", sent); @@ -275,7 +274,7 @@ void Socket::settimeout(py::object timeout_s_or_none) { uint32_t timeout_ms = 0; // no timeout if (!timeout_s_or_none.is(py::none())) { - timeout_ms = (uint32_t)(py::cast(timeout_s_or_none) * 1000.0); + timeout_ms = (uint32_t) (py::cast(timeout_s_or_none) * 1000.0); } mbedtls_ssl_conf_read_timeout(&config_, timeout_ms); @@ -329,8 +328,7 @@ void enable_reuse(const py::object &socket) { ServerSocket::ServerSocket(std::shared_ptr context, py::object py_socket) - : context_(context), - py_socket_(py_socket) { + : context_(context), py_socket_(py_socket) { enable_reuse(py_socket_); } diff --git a/test/integration/suites/default/access_control.py b/test/integration/suites/default/access_control.py index fe5a0a738..a7164c2b4 100644 --- a/test/integration/suites/default/access_control.py +++ b/test/integration/suites/default/access_control.py @@ -110,8 +110,13 @@ def update_access(self, server, oid, iid, acl, expected_acl=None, expect_error_c accept=coap.ContentFormat.APPLICATION_LWM2M_TLV) self.assertEqual(expected_tlv, read_tlv.content) - def setUp(self, servers=2, **kwargs): - extra_args = sum((['--access-entry', '1337', str(ssid)] for ssid in range(2, servers + 1)), []) + def setUp(self, servers=2, extra_cmdline_args=[], **kwargs): + if isinstance(servers, int): + servers_count = servers + else: + servers_count = len(servers) + extra_args = sum((['--access-entry', '1337', str(ssid)] for ssid in range(2, servers_count + 1)), + extra_cmdline_args) self.setup_demo_with_servers(servers=servers, extra_cmdline_args=extra_args, **kwargs) diff --git a/test/integration/suites/default/json_encoding.py b/test/integration/suites/default/json_encoding.py index e4f54503b..c49b92eef 100644 --- a/test/integration/suites/default/json_encoding.py +++ b/test/integration/suites/default/json_encoding.py @@ -20,12 +20,14 @@ from . import plaintext_base64 as pb64 +IID = 1 + class JsonEncodingTest: class Test(test_suite.Lwm2mSingleServerTest, test_suite.Lwm2mDmOperations): def setUp(self): super().setUp() - self.create_instance(self.serv, oid=OID.Test, iid=1) + self.create_instance(self.serv, oid=OID.Test, iid=IID) def as_json(pkt): @@ -34,14 +36,14 @@ def as_json(pkt): class JsonEncodingBnResource(JsonEncodingTest.Test): def runTest(self): - res = as_json(self.read_resource(self.serv, oid=OID.Test, iid=1, rid=0, + res = as_json(self.read_resource(self.serv, oid=OID.Test, iid=IID, rid=0, accept=coap.ContentFormat.APPLICATION_LWM2M_JSON)) self.assertEqual('/%d/1/0' % OID.Test, res['bn']) class JsonEncodingBnInstance(JsonEncodingTest.Test): def runTest(self): - res = as_json(self.read_instance(self.serv, oid=OID.Test, iid=1, + res = as_json(self.read_instance(self.serv, oid=OID.Test, iid=IID, accept=coap.ContentFormat.APPLICATION_LWM2M_JSON)) self.assertEqual('/%d/1' % OID.Test, res['bn']) @@ -58,7 +60,7 @@ def runTest(self): responses = [ as_json(self.read_object(self.serv, oid=OID.Test, accept=coap.ContentFormat.APPLICATION_LWM2M_JSON)), - as_json(self.read_instance(self.serv, oid=OID.Test, iid=1, + as_json(self.read_instance(self.serv, oid=OID.Test, iid=IID, accept=coap.ContentFormat.APPLICATION_LWM2M_JSON)) ] for response in responses: self.assertTrue(len(response['e']) > 0) @@ -66,7 +68,7 @@ def runTest(self): for resource in response['e']: self.assertEqual('/', resource['n'][0]) - resource = as_json(self.read_resource(self.serv, oid=OID.Test, iid=1, rid=0, + resource = as_json(self.read_resource(self.serv, oid=OID.Test, iid=IID, rid=0, accept=coap.ContentFormat.APPLICATION_LWM2M_JSON)) # Resource path is in 'bn', therefore no 'n' parameter is specified by the client self.assertFalse('n' in resource['e']) @@ -77,9 +79,9 @@ def runTest(self): some_bytes = pb64.test_object_bytes_generator(51) some_bytes_b64 = base64.encodebytes(some_bytes).replace(b'\n', b'') - self.write_resource(self.serv, oid=OID.Test, iid=1, rid=RID.Test.ResRawBytes, content=some_bytes_b64) + self.write_resource(self.serv, oid=OID.Test, iid=IID, rid=RID.Test.ResRawBytes, content=some_bytes_b64) - result = as_json(self.read_resource(self.serv, oid=OID.Test, iid=1, + result = as_json(self.read_resource(self.serv, oid=OID.Test, iid=IID, rid=RID.Test.ResRawBytes, accept=coap.ContentFormat.APPLICATION_LWM2M_JSON)) @@ -94,10 +96,10 @@ def runTest(self): values[i] = random.randint(0, 2**31) execute_args = ','.join("%d='%d'" % (k, v) for k, v in values.items()) - self.execute_resource(self.serv, oid=OID.Test, iid=1, rid=RID.Test.ResInitIntArray, + self.execute_resource(self.serv, oid=OID.Test, iid=IID, rid=RID.Test.ResInitIntArray, content=bytes(execute_args, encoding='utf-8')) - result = as_json(self.read_resource(self.serv, oid=OID.Test, iid=1, + result = as_json(self.read_resource(self.serv, oid=OID.Test, iid=IID, rid=RID.Test.ResOpaqueArray, accept=coap.ContentFormat.APPLICATION_LWM2M_JSON)) diff --git a/test/integration/suites/default/queue_mode.py b/test/integration/suites/default/queue_mode.py new file mode 100644 index 000000000..6b4c7dd13 --- /dev/null +++ b/test/integration/suites/default/queue_mode.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2017-2018 AVSystem +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import time + +from framework.lwm2m_test import * +from . import access_control, retransmissions + + +class QueueModeBehaviour(retransmissions.RetransmissionTest.TestMixin, + access_control.AccessControl.Test): + PSK_IDENTITY = b'test-identity' + PSK_KEY = b'test-key' + + def setUp(self): + super().setUp( + servers=[ + Lwm2mServer(coap.DtlsServer(psk_key=self.PSK_KEY, psk_identity=self.PSK_IDENTITY)), + Lwm2mServer(coap.DtlsServer(psk_key=self.PSK_KEY, psk_identity=self.PSK_IDENTITY))], + extra_cmdline_args=['--identity', + str(binascii.hexlify(self.PSK_IDENTITY), 'ascii'), + '--key', str(binascii.hexlify(self.PSK_KEY), 'ascii')]) + + def tearDown(self): + self.teardown_demo_with_servers(deregister_servers=[self.servers[1]]) + + def runTest(self): + # create the test object and give read access to servers[0] + self.create_instance(server=self.servers[1], oid=OID.Test) + self.update_access(server=self.servers[1], oid=OID.Test, iid=1, + acl=[access_control.make_acl_entry(1, access_control.ACCESS_MASK_READ), + access_control.make_acl_entry(2, access_control.ACCESS_MASK_OWNER)]) + + # put servers[0] into queue mode + self.write_resource(self.servers[0], OID.Server, 1, RID.Server.Binding, b'UQ') + self.assertDemoUpdatesRegistration(self.servers[0], binding='UQ', content=ANY) + + # Observe the Counter argument + self.observe(self.servers[0], OID.Test, 1, RID.Test.Counter) + + # Wait until servers[0] closes its socket due to queue mode of operation. This happens after + # MAX_TRANSMIT_WAIT value derived from UDP connection parameters - by default, 93 seconds, + # but for the values set in RetransmissionTest.TestMixin, it's 14 seconds + time.sleep(12) + self.assertEqual(self.get_socket_count(), 2) + time.sleep(4) + self.assertEqual(self.get_socket_count(), 1) + + # Trigger Notification from the non-queue server + self.execute_resource(self.servers[1], OID.Test, 1, RID.Test.IncrementCounter) + + self.assertDtlsReconnect(self.servers[0]) + pkt = self.servers[0].recv() + self.assertIsInstance(pkt, Lwm2mNotify) + self.assertEqual(self.get_socket_count(), 2) + + # "queued RPCs" + self.read_resource(self.servers[0], OID.Test, 1, RID.Test.Timestamp) + # cancel observation + self.observe(self.servers[0], OID.Test, 1, RID.Test.Counter, observe=1) + + # assert queue mode operation again + time.sleep(12) + self.assertEqual(self.get_socket_count(), 2) + time.sleep(4) + self.assertEqual(self.get_socket_count(), 1) diff --git a/test/integration/suites/default/retransmissions.py b/test/integration/suites/default/retransmissions.py index 1ea1ec12e..19ea0c74e 100644 --- a/test/integration/suites/default/retransmissions.py +++ b/test/integration/suites/default/retransmissions.py @@ -19,12 +19,40 @@ from framework.lwm2m_test import * -COAP_DEFAULT_ACK_RANDOM_FACTOR = 1.5 -COAP_DEFAULT_ACK_TIMEOUT = 2 -COAP_DEFAULT_MAX_RETRANSMIT = 4 +class RetransmissionTest: + class TestMixin: + # Note that these values differ from default ones in Anjay. This is done to + # speed up the test execution a bit by limiting the number of retransmissions + # as well as wait intervals between them. + MAX_ICMP_FAILURES=7 + ACK_RANDOM_FACTOR=1.0 + ACK_TIMEOUT=2.0 + MAX_RETRANSMIT=2 + CONFIRMABLE_NOTIFICATIONS=False + + def setUp(self, *args, **kwargs): + extra_cmdline_args=[ + '--max-icmp-failures', str(self.MAX_ICMP_FAILURES), + '--ack-random-factor', str(self.ACK_RANDOM_FACTOR), + '--ack-timeout', str(self.ACK_TIMEOUT), + '--max-retransmit', str(self.MAX_RETRANSMIT), + '--dtls-hs-retry-wait-min', str(self.ACK_TIMEOUT * self.ACK_RANDOM_FACTOR), + '--dtls-hs-retry-wait-max', str(self.ACK_TIMEOUT * 2**self.MAX_RETRANSMIT * self.ACK_RANDOM_FACTOR), + ] + if self.CONFIRMABLE_NOTIFICATIONS: + extra_cmdline_args += ['--confirmable-notifications'] + if 'extra_cmdline_args' in kwargs: + kwargs['extra_cmdline_args'] = extra_cmdline_args + kwargs['extra_cmdline_args'] + super().setUp(*args, **kwargs) + else: + super().setUp(*args, **kwargs, extra_cmdline_args=extra_cmdline_args) + + +class ReconnectRetryTest(test_suite.PcapEnabledTest, + RetransmissionTest.TestMixin, + test_suite.Lwm2mDtlsSingleServerTest): + MAX_ICMP_FAILURES=3 - -class ReconnectRetryTest(test_suite.PcapEnabledTest, test_suite.Lwm2mDtlsSingleServerTest): def setup_demo_with_servers(self, **kwargs): for server in kwargs['servers']: self._server_close_stack.enter_context(server.fake_close()) @@ -32,7 +60,7 @@ def setup_demo_with_servers(self, **kwargs): def setUp(self): self._server_close_stack = contextlib.ExitStack() - super().setUp(extra_cmdline_args=['--max-icmp-failures', '3'], auto_register=False) + super().setUp(auto_register=False) def tearDown(self): self._server_close_stack.close() # in case runTest() failed @@ -40,19 +68,19 @@ def tearDown(self): def runTest(self): # wait until ultimate failure - self.wait_until_icmp_unreachable_count(3, timeout_s=16) + self.wait_until_icmp_unreachable_count(self.MAX_ICMP_FAILURES, timeout_s=16) # check that there are no more attempts time.sleep(16) - self.assertEqual(3, self.count_icmp_unreachable_packets()) + self.assertEqual(self.MAX_ICMP_FAILURES, self.count_icmp_unreachable_packets()) # attempt reconnection self.communicate('reconnect') - # let demo make 2 unsuccessful connections - self.wait_until_icmp_unreachable_count(5, timeout_s=8) + # let demo make 2 more unsuccessful connections + self.wait_until_icmp_unreachable_count(self.MAX_ICMP_FAILURES+2, timeout_s=8) self._server_close_stack.close() # unclose the server socket self.assertDemoRegisters(self.serv, timeout_s=16) - self.assertEqual(5, self.count_icmp_unreachable_packets()) + self.assertEqual(self.MAX_ICMP_FAILURES+2, self.count_icmp_unreachable_packets()) # Tests below check that Anjay does not go crazy when faced with network connection problems while attempting to send @@ -60,9 +88,10 @@ def runTest(self): # any backoff, and so on - so we test that the behaviour is sane. -class NotificationTimeoutReconnectTest(test_suite.Lwm2mDtlsSingleServerTest, test_suite.Lwm2mDmOperations): - def setUp(self): - super().setUp(extra_cmdline_args=['--confirmable-notifications']) +class NotificationTimeoutReconnectTest(RetransmissionTest.TestMixin, + test_suite.Lwm2mDtlsSingleServerTest, + test_suite.Lwm2mDmOperations): + CONFIRMABLE_NOTIFICATIONS=True def runTest(self): self.create_instance(self.serv, oid=OID.Test, iid=1) @@ -72,18 +101,17 @@ def runTest(self): first_attempt = time.time() self.assertIsInstance(first_pkt, Lwm2mNotify) - for attempt in range(COAP_DEFAULT_MAX_RETRANSMIT): + for attempt in range(self.MAX_RETRANSMIT): self.assertIsInstance(self.serv.recv(timeout_s=30), Lwm2mNotify) last_attempt = time.time() - transmit_span_lower_bound = COAP_DEFAULT_ACK_TIMEOUT * ((2 ** COAP_DEFAULT_MAX_RETRANSMIT) - 1) - transmit_span_upper_bound = transmit_span_lower_bound * COAP_DEFAULT_ACK_RANDOM_FACTOR + transmit_span_lower_bound = self.ACK_TIMEOUT * ((2 ** self.MAX_RETRANSMIT) - 1) + transmit_span_upper_bound = transmit_span_lower_bound * self.ACK_RANDOM_FACTOR self.assertGreater(last_attempt - first_attempt, transmit_span_lower_bound - 1) self.assertLess(last_attempt - first_attempt, transmit_span_upper_bound + 1) - self.assertDtlsReconnect(timeout_s=COAP_DEFAULT_ACK_RANDOM_FACTOR * COAP_DEFAULT_ACK_TIMEOUT * ( - 2 ** COAP_DEFAULT_MAX_RETRANSMIT) + 1) + self.assertDtlsReconnect(timeout_s=self.ACK_RANDOM_FACTOR * self.ACK_TIMEOUT * (2 ** self.MAX_RETRANSMIT) + 1) pkt = self.serv.recv(timeout_s=1) self.assertIsInstance(pkt, Lwm2mNotify) @@ -92,10 +120,10 @@ def runTest(self): class NotificationIcmpReconnectTest(test_suite.PcapEnabledTest, + RetransmissionTest.TestMixin, test_suite.Lwm2mSingleServerTest, test_suite.Lwm2mDmOperations): - def setUp(self): - super().setUp(extra_cmdline_args=['--confirmable-notifications']) + CONFIRMABLE_NOTIFICATIONS=True def runTest(self): self.create_instance(self.serv, oid=OID.Test, iid=1) @@ -116,10 +144,10 @@ def runTest(self): class NotificationDtlsIcmpReconnectTest(test_suite.PcapEnabledTest, + RetransmissionTest.TestMixin, test_suite.Lwm2mDtlsSingleServerTest, test_suite.Lwm2mDmOperations): - def setUp(self): - super().setUp(extra_cmdline_args=['--confirmable-notifications']) + CONFIRMABLE_NOTIFICATIONS=True def runTest(self): self.create_instance(self.serv, oid=OID.Test, iid=1) diff --git a/test/integration/suites/testfest/dm/firmware_update.py b/test/integration/suites/testfest/dm/firmware_update.py index 2432e2641..f5b96b84d 100644 --- a/test/integration/suites/testfest/dm/firmware_update.py +++ b/test/integration/suites/testfest/dm/firmware_update.py @@ -38,9 +38,9 @@ def collect_values(self, path: Lwm2mPath, final_value, max_iterations=100, step_ time.sleep(step_time) return observed_values - def setUp(self): + def setUp(self, extra_cmdline_args=[]): self.ANJAY_MARKER_FILE = generate_temp_filename(dir='/tmp', prefix='anjay-fw-updated-') - super().setUp(fw_updated_marker_path=self.ANJAY_MARKER_FILE) + super().setUp(fw_updated_marker_path=self.ANJAY_MARKER_FILE, extra_cmdline_args=extra_cmdline_args) def tearDown(self): # reset the state machine @@ -51,8 +51,8 @@ def tearDown(self): super().tearDown() class TestWithCoapServer(Test): - def setUp(self, coap_server=None): - super().setUp() + def setUp(self, coap_server=None, extra_cmdline_args=[]): + super().setUp(extra_cmdline_args=extra_cmdline_args) from framework.coap_file_server import CoapFileServerThread self.server_thread = CoapFileServerThread(coap_server=coap_server) @@ -837,7 +837,7 @@ class MuteServer(coap.Server): def send(self, *args, **kwargs): pass - super().setUp(coap_server=MuteServer()) + super().setUp(coap_server=MuteServer(), extra_cmdline_args=['--fwu-ack-timeout', '1']) with self.file_server as file_server: file_server.set_resource('/firmware', pkg) @@ -881,7 +881,7 @@ def runTest(self): # Resource with value "4" indicates the firmware Package Delivery # aborted due to connection lost dur the Package delivery. observed_values = self.collect_values(ResPath.FirmwareUpdate.State, b'0', - max_iterations=100, step_time=1) + max_iterations=50, step_time=1) self.assertEqual(b'0', observed_values[-1]) self.assertEqual({b'0', b'1'}, set(observed_values)) self.assertEqual(b'4', self.test_read(ResPath.FirmwareUpdate.UpdateResult)) diff --git a/test/src/dm.c b/test/src/dm.c index e650b9126..77bd2e773 100644 --- a/test/src/dm.c +++ b/test/src/dm.c @@ -18,15 +18,15 @@ #include -#include -#include #include +#include +#include #include "../../src/anjay_core.h" // HACK to enable _anjay_server_cleanup #define ANJAY_SERVERS_INTERNALS -#include "../../src/servers/connection_info.h" +#include "../../src/servers/server_connections.h" #include "../../src/servers/servers_internal.h" #undef ANJAY_SERVERS_INTERNALS @@ -48,29 +48,38 @@ void _anjay_test_dm_unsched_reload_sockets(anjay_t *anjay) { avs_net_abstract_socket_t *_anjay_test_dm_install_socket(anjay_t *anjay, anjay_ssid_t ssid) { - AVS_UNIT_ASSERT_NOT_NULL(AVS_LIST_INSERT_NEW(anjay_server_info_t, - &anjay->servers->servers)); + AVS_UNIT_ASSERT_NOT_NULL( + AVS_LIST_INSERT_NEW(anjay_server_info_t, &anjay->servers->servers)); anjay->servers->servers->ssid = ssid; avs_net_abstract_socket_t *socket = NULL; _anjay_mocksock_create(&socket, 1252, 1252); avs_unit_mocksock_expect_connect(socket, "", ""); AVS_UNIT_ASSERT_SUCCESS(avs_net_socket_connect(socket, "", "")); - anjay->servers->servers->data_active.udp_connection.conn_socket_ = socket; - anjay->servers->servers->data_active.udp_connection.mode = ANJAY_CONNECTION_ONLINE; - anjay->servers->servers->data_active.primary_conn_type = ANJAY_CONNECTION_UDP; - anjay->servers->servers->data_active.registration_info.expire_time.since_real_epoch.seconds = INT64_MAX; - return _anjay_connection_internal_get_socket( - &anjay->servers->servers->data_active.udp_connection); + anjay->servers->servers->connections.primary_conn_type = + ANJAY_CONNECTION_UDP; + anjay->servers->servers->registration_info.expire_time.since_real_epoch + .seconds = INT64_MAX; + anjay_server_connection_t *connection = + _anjay_get_server_connection((const anjay_connection_ref_t) { + .server = anjay->servers->servers, + .conn_type = ANJAY_CONNECTION_UDP + }); + AVS_UNIT_ASSERT_NOT_NULL(connection); + connection->conn_socket_ = socket; + connection->mode = ANJAY_CONNECTION_ONLINE; + return socket; } void _anjay_test_dm_finish(anjay_t *anjay) { anjay_server_info_t *server; AVS_LIST_FOREACH(server, anjay->servers->servers) { - avs_net_abstract_socket_t *socket = - _anjay_connection_internal_get_socket( - &server->data_active.udp_connection); - avs_unit_mocksock_assert_expects_met(socket); - avs_unit_mocksock_assert_io_clean(socket); + anjay_server_connection_t *connection = + _anjay_get_server_connection((const anjay_connection_ref_t) { + .server = server, + .conn_type = ANJAY_CONNECTION_UDP + }); + avs_unit_mocksock_assert_expects_met(connection->conn_socket_); + avs_unit_mocksock_assert_io_clean(connection->conn_socket_); } _anjay_mock_dm_expect_clean(); AVS_LIST_CLEAR(&anjay->servers->servers) { @@ -80,10 +89,11 @@ void _anjay_test_dm_finish(anjay_t *anjay) { _anjay_mock_clock_finish(); } -int _anjay_test_dm_fake_security_instance_it(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t *out, - void **cookie_) { +int _anjay_test_dm_fake_security_instance_it( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t *out, + void **cookie_) { (void) obj_ptr; AVS_LIST(anjay_server_info_t) *cookie = (AVS_LIST(anjay_server_info_t) *) cookie_; @@ -100,9 +110,10 @@ int _anjay_test_dm_fake_security_instance_it(anjay_t *anjay, return 0; } -int _anjay_test_dm_fake_security_instance_present(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid) { +int _anjay_test_dm_fake_security_instance_present( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid) { (void) obj_ptr; AVS_LIST(anjay_server_info_t) server; AVS_LIST_FOREACH(server, anjay->servers->servers) { @@ -113,11 +124,12 @@ int _anjay_test_dm_fake_security_instance_present(anjay_t *anjay, return 0; } -int _anjay_test_dm_fake_security_read(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_output_ctx_t *ctx) { +int _anjay_test_dm_fake_security_read( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_output_ctx_t *ctx) { (void) anjay; (void) obj_ptr; switch (rid) { diff --git a/test/src/mock_clock.c b/test/src/mock_clock.c index 932c51ce3..ddb6cfd48 100644 --- a/test/src/mock_clock.c +++ b/test/src/mock_clock.c @@ -48,8 +48,9 @@ static int (*orig_clock_gettime)(clockid_t, struct timespec *); AVS_UNIT_GLOBAL_INIT(verbose) { (void) verbose; - orig_clock_gettime = (int (*)(clockid_t, struct timespec *)) (intptr_t) - dlsym(RTLD_NEXT, "clock_gettime"); + typedef int (*clock_gettime_t)(clockid_t, struct timespec *); + orig_clock_gettime = + (clock_gettime_t) (intptr_t) dlsym(RTLD_NEXT, "clock_gettime"); } int clock_gettime(clockid_t clock, struct timespec *t) { diff --git a/test/src/mock_dm.c b/test/src/mock_dm.c index 3a347a3a3..3f583f1a4 100644 --- a/test/src/mock_dm.c +++ b/test/src/mock_dm.c @@ -81,18 +81,20 @@ typedef struct { static AVS_LIST(anjay_mock_dm_expected_command_t) EXPECTED_COMMANDS; -#define DM_ACTION_COMMON(UName) do { \ - AVS_UNIT_ASSERT_NOT_NULL(EXPECTED_COMMANDS); \ - AVS_UNIT_ASSERT_TRUE(EXPECTED_COMMANDS->command == MOCK_DM_##UName); \ - AVS_UNIT_ASSERT_TRUE(EXPECTED_COMMANDS->anjay == anjay); \ - AVS_UNIT_ASSERT_TRUE(EXPECTED_COMMANDS->obj_ptr == obj_ptr); \ -} while (0) - -#define DM_ACTION_RETURN do { \ - int retval = EXPECTED_COMMANDS->retval; \ - AVS_LIST_DELETE(&EXPECTED_COMMANDS); \ - return retval; \ -} while (0) +#define DM_ACTION_COMMON(UName) \ + do { \ + AVS_UNIT_ASSERT_NOT_NULL(EXPECTED_COMMANDS); \ + AVS_UNIT_ASSERT_TRUE(EXPECTED_COMMANDS->command == MOCK_DM_##UName); \ + AVS_UNIT_ASSERT_TRUE(EXPECTED_COMMANDS->anjay == anjay); \ + AVS_UNIT_ASSERT_TRUE(EXPECTED_COMMANDS->obj_ptr == obj_ptr); \ + } while (0) + +#define DM_ACTION_RETURN \ + do { \ + int retval = EXPECTED_COMMANDS->retval; \ + AVS_LIST_DELETE(&EXPECTED_COMMANDS); \ + return retval; \ + } while (0) void _anjay_mock_dm_assert_common_attributes_equal( const anjay_dm_internal_attrs_t *a, @@ -115,10 +117,11 @@ void _anjay_mock_dm_assert_attributes_equal( AVS_UNIT_ASSERT_FIELD_EQUAL(a, b, standard.step); } -int _anjay_mock_dm_object_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - anjay_dm_attributes_t *out) { +int _anjay_mock_dm_object_read_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + anjay_dm_attributes_t *out) { DM_ACTION_COMMON(OBJECT_READ_DEFAULT_ATTRS); EXPECTED_COMMANDS->input.ssid = ssid; *_anjay_dm_get_internal_attrs(out) = @@ -126,10 +129,11 @@ int _anjay_mock_dm_object_read_default_attrs(anjay_t *anjay, DM_ACTION_RETURN; } -int _anjay_mock_dm_object_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - const anjay_dm_attributes_t *attrs) { +int _anjay_mock_dm_object_write_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + const anjay_dm_attributes_t *attrs) { DM_ACTION_COMMON(OBJECT_WRITE_DEFAULT_ATTRS); EXPECTED_COMMANDS->input.ssid = ssid; _anjay_mock_dm_assert_common_attributes_equal( @@ -151,19 +155,20 @@ int _anjay_mock_dm_instance_it(anjay_t *anjay, anjay_iid_t *out, void **cookie) { DM_ACTION_COMMON(INSTANCE_IT); - AVS_UNIT_ASSERT_EQUAL((*(uintptr_t *) cookie)++, EXPECTED_COMMANDS->input.iteration); + AVS_UNIT_ASSERT_EQUAL((*(uintptr_t *) cookie)++, + EXPECTED_COMMANDS->input.iteration); *out = EXPECTED_COMMANDS->value.output_iid; DM_ACTION_RETURN; } -#define INSTANCE_ACTION(LName, UName) \ -int _anjay_mock_dm_instance_##LName (anjay_t *anjay, \ - const anjay_dm_object_def_t *const *obj_ptr, \ - anjay_iid_t iid) { \ - DM_ACTION_COMMON(INSTANCE_##UName); \ - AVS_UNIT_ASSERT_EQUAL(iid, EXPECTED_COMMANDS->input.iid); \ - DM_ACTION_RETURN; \ -} +#define INSTANCE_ACTION(LName, UName) \ + int _anjay_mock_dm_instance_##LName( \ + anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, \ + anjay_iid_t iid) { \ + DM_ACTION_COMMON(INSTANCE_##UName); \ + AVS_UNIT_ASSERT_EQUAL(iid, EXPECTED_COMMANDS->input.iid); \ + DM_ACTION_RETURN; \ + } INSTANCE_ACTION(present, PRESENT) INSTANCE_ACTION(remove, REMOVE) @@ -173,17 +178,19 @@ int _anjay_mock_dm_instance_create(anjay_t *anjay, anjay_iid_t *inout_iid, anjay_ssid_t ssid) { DM_ACTION_COMMON(INSTANCE_CREATE); - AVS_UNIT_ASSERT_EQUAL(*inout_iid, EXPECTED_COMMANDS->input.ssid_and_iid.iid); + AVS_UNIT_ASSERT_EQUAL(*inout_iid, + EXPECTED_COMMANDS->input.ssid_and_iid.iid); AVS_UNIT_ASSERT_EQUAL(ssid, EXPECTED_COMMANDS->input.ssid_and_iid.ssid); *inout_iid = EXPECTED_COMMANDS->value.output_iid; DM_ACTION_RETURN; } -int _anjay_mock_dm_instance_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - anjay_dm_attributes_t *out) { +int _anjay_mock_dm_instance_read_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + anjay_dm_attributes_t *out) { DM_ACTION_COMMON(INSTANCE_READ_DEFAULT_ATTRS); AVS_UNIT_ASSERT_EQUAL(ssid, EXPECTED_COMMANDS->input.ssid_and_iid.ssid); AVS_UNIT_ASSERT_EQUAL(iid, EXPECTED_COMMANDS->input.ssid_and_iid.iid); @@ -192,11 +199,12 @@ int _anjay_mock_dm_instance_read_default_attrs(anjay_t *anjay, DM_ACTION_RETURN; } -int _anjay_mock_dm_instance_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - const anjay_dm_attributes_t *attrs) { +int _anjay_mock_dm_instance_write_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + const anjay_dm_attributes_t *attrs) { DM_ACTION_COMMON(INSTANCE_WRITE_DEFAULT_ATTRS); AVS_UNIT_ASSERT_EQUAL(ssid, EXPECTED_COMMANDS->input.ssid_and_iid.ssid); AVS_UNIT_ASSERT_EQUAL(iid, EXPECTED_COMMANDS->input.ssid_and_iid.iid); @@ -216,10 +224,11 @@ int _anjay_mock_dm_resource_present(anjay_t *anjay, DM_ACTION_RETURN; } -int _anjay_mock_dm_resource_operations(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_rid_t rid, - anjay_dm_resource_op_mask_t *out) { +int _anjay_mock_dm_resource_operations( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_rid_t rid, + anjay_dm_resource_op_mask_t *out) { DM_ACTION_COMMON(RESOURCE_OPERATIONS); AVS_UNIT_ASSERT_EQUAL(rid, EXPECTED_COMMANDS->input.rid); *out = EXPECTED_COMMANDS->value.mask; @@ -276,7 +285,8 @@ static int output_array(anjay_output_ctx_t *ctx, return -1; } for (const anjay_mock_dm_data_array_t *const *entry = array; - entry && *entry; ++entry) { + entry && *entry; + ++entry) { int retval = anjay_ret_array_index(array_ctx, (*entry)->index); if (retval) { return retval; @@ -311,23 +321,21 @@ static void perform_input(anjay_input_ctx_t *ctx, switch (input->type) { case MOCK_DATA_NONE: return; - case MOCK_DATA_BYTES: - { + case MOCK_DATA_BYTES: { size_t bytes_read; bool message_finished; char buf[input->data.bytes.length]; - retval = anjay_get_bytes(ctx, &bytes_read, &message_finished, - buf, sizeof (buf)); + retval = anjay_get_bytes(ctx, &bytes_read, &message_finished, buf, + sizeof(buf)); if (!retval) { - AVS_UNIT_ASSERT_EQUAL(bytes_read, sizeof (buf)); + AVS_UNIT_ASSERT_EQUAL(bytes_read, sizeof(buf)); AVS_UNIT_ASSERT_TRUE(message_finished); AVS_UNIT_ASSERT_EQUAL_BYTES_SIZED(buf, input->data.bytes.data, input->data.bytes.length); } break; } - case MOCK_DATA_STRING: - { + case MOCK_DATA_STRING: { char buf[strlen(input->data.str) + 1]; retval = anjay_get_string(ctx, buf, sizeof(buf)); if (!retval) { @@ -335,8 +343,7 @@ static void perform_input(anjay_input_ctx_t *ctx, } break; } - case MOCK_DATA_INT: - { + case MOCK_DATA_INT: { int64_t value; retval = anjay_get_i64(ctx, &value); if (!retval) { @@ -344,8 +351,7 @@ static void perform_input(anjay_input_ctx_t *ctx, } break; } - case MOCK_DATA_FLOAT: - { + case MOCK_DATA_FLOAT: { double value; retval = anjay_get_double(ctx, &value); if (!retval) { @@ -353,8 +359,7 @@ static void perform_input(anjay_input_ctx_t *ctx, } break; } - case MOCK_DATA_BOOL: - { + case MOCK_DATA_BOOL: { bool value; retval = anjay_get_bool(ctx, &value); if (!retval) { @@ -362,8 +367,7 @@ static void perform_input(anjay_input_ctx_t *ctx, } break; } - case MOCK_DATA_OBJLNK: - { + case MOCK_DATA_OBJLNK: { anjay_oid_t oid; anjay_iid_t iid; retval = anjay_get_objlnk(ctx, &oid, &iid); @@ -389,7 +393,8 @@ static int input_array(anjay_input_ctx_t *ctx, return -1; } for (const anjay_mock_dm_data_array_t *const *entry = array; - entry && *entry; ++entry) { + entry && *entry; + ++entry) { perform_input(array_ctx, &(*entry)->value); } return 0; @@ -429,12 +434,13 @@ int _anjay_mock_dm_resource_dim(anjay_t *anjay, DM_ACTION_RETURN; } -int _anjay_mock_dm_resource_read_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_ssid_t ssid, - anjay_dm_resource_attributes_t *out) { +int _anjay_mock_dm_resource_read_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_ssid_t ssid, + anjay_dm_resource_attributes_t *out) { DM_ACTION_COMMON(RESOURCE_READ_ATTRS); AVS_UNIT_ASSERT_EQUAL(ssid, EXPECTED_COMMANDS->input.ssid_iid_rid.ssid); AVS_UNIT_ASSERT_EQUAL(iid, EXPECTED_COMMANDS->input.ssid_iid_rid.iid); @@ -444,12 +450,13 @@ int _anjay_mock_dm_resource_read_attrs(anjay_t *anjay, DM_ACTION_RETURN; } -int _anjay_mock_dm_resource_write_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_ssid_t ssid, - const anjay_dm_resource_attributes_t *attrs) { +int _anjay_mock_dm_resource_write_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_ssid_t ssid, + const anjay_dm_resource_attributes_t *attrs) { DM_ACTION_COMMON(RESOURCE_WRITE_ATTRS); AVS_UNIT_ASSERT_EQUAL(ssid, EXPECTED_COMMANDS->input.ssid_iid_rid.ssid); AVS_UNIT_ASSERT_EQUAL(iid, EXPECTED_COMMANDS->input.ssid_iid_rid.iid); @@ -468,11 +475,12 @@ static anjay_mock_dm_expected_command_t *new_expected_command(void) { return new_command; } -void _anjay_mock_dm_expect_object_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - int retval, - const anjay_dm_internal_attrs_t *attrs) { +void _anjay_mock_dm_expect_object_read_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + int retval, + const anjay_dm_internal_attrs_t *attrs) { anjay_mock_dm_expected_command_t *command = new_expected_command(); command->command = MOCK_DM_OBJECT_READ_DEFAULT_ATTRS; command->anjay = anjay; @@ -486,11 +494,12 @@ void _anjay_mock_dm_expect_object_read_default_attrs(anjay_t *anjay, } } -void _anjay_mock_dm_expect_object_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_ssid_t ssid, - const anjay_dm_internal_attrs_t *attrs, - int retval) { +void _anjay_mock_dm_expect_object_write_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_ssid_t ssid, + const anjay_dm_internal_attrs_t *attrs, + int retval) { anjay_mock_dm_expected_command_t *command = new_expected_command(); command->command = MOCK_DM_OBJECT_WRITE_DEFAULT_ATTRS; command->anjay = anjay; @@ -500,10 +509,11 @@ void _anjay_mock_dm_expect_object_write_default_attrs(anjay_t *anjay, command->value.common_attributes = *attrs; } -void _anjay_mock_dm_expect_instance_reset(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - int retval) { +void _anjay_mock_dm_expect_instance_reset( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + int retval) { anjay_mock_dm_expected_command_t *command = new_expected_command(); command->command = MOCK_DM_INSTANCE_RESET; command->anjay = anjay; @@ -512,11 +522,12 @@ void _anjay_mock_dm_expect_instance_reset(anjay_t *anjay, command->retval = retval; } -void _anjay_mock_dm_expect_instance_it(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - uintptr_t iteration, - int retval, - anjay_iid_t out) { +void _anjay_mock_dm_expect_instance_it( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + uintptr_t iteration, + int retval, + anjay_iid_t out) { anjay_mock_dm_expected_command_t *command = new_expected_command(); command->command = MOCK_DM_INSTANCE_IT; command->anjay = anjay; @@ -526,28 +537,28 @@ void _anjay_mock_dm_expect_instance_it(anjay_t *anjay, command->retval = retval; } -#define EXPECT_INSTANCE_ACTION(LName, UName) \ -void _anjay_mock_dm_expect_instance_##LName (anjay_t *anjay, \ - const anjay_dm_object_def_t *const *obj_ptr, \ - anjay_iid_t iid, \ - int retval) { \ - anjay_mock_dm_expected_command_t *command = new_expected_command(); \ - command->command = MOCK_DM_INSTANCE_##UName; \ - command->anjay = anjay; \ - command->obj_ptr = obj_ptr; \ - command->input.iid = iid; \ - command->retval = retval; \ -} +#define EXPECT_INSTANCE_ACTION(LName, UName) \ + void _anjay_mock_dm_expect_instance_##LName( \ + anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, \ + anjay_iid_t iid, int retval) { \ + anjay_mock_dm_expected_command_t *command = new_expected_command(); \ + command->command = MOCK_DM_INSTANCE_##UName; \ + command->anjay = anjay; \ + command->obj_ptr = obj_ptr; \ + command->input.iid = iid; \ + command->retval = retval; \ + } EXPECT_INSTANCE_ACTION(present, PRESENT) EXPECT_INSTANCE_ACTION(remove, REMOVE) -void _anjay_mock_dm_expect_instance_create(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - int retval, - anjay_iid_t out_iid) { +void _anjay_mock_dm_expect_instance_create( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + int retval, + anjay_iid_t out_iid) { anjay_mock_dm_expected_command_t *command = new_expected_command(); command->command = MOCK_DM_INSTANCE_CREATE; command->anjay = anjay; @@ -558,12 +569,13 @@ void _anjay_mock_dm_expect_instance_create(anjay_t *anjay, command->value.output_iid = out_iid; } -void _anjay_mock_dm_expect_instance_read_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - int retval, - const anjay_dm_internal_attrs_t *attrs) { +void _anjay_mock_dm_expect_instance_read_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + int retval, + const anjay_dm_internal_attrs_t *attrs) { anjay_mock_dm_expected_command_t *command = new_expected_command(); command->command = MOCK_DM_INSTANCE_READ_DEFAULT_ATTRS; command->anjay = anjay; @@ -578,12 +590,13 @@ void _anjay_mock_dm_expect_instance_read_default_attrs(anjay_t *anjay, } } -void _anjay_mock_dm_expect_instance_write_default_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_ssid_t ssid, - const anjay_dm_internal_attrs_t *attrs, - int retval) { +void _anjay_mock_dm_expect_instance_write_default_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_ssid_t ssid, + const anjay_dm_internal_attrs_t *attrs, + int retval) { anjay_mock_dm_expected_command_t *command = new_expected_command(); command->command = MOCK_DM_INSTANCE_WRITE_DEFAULT_ATTRS; command->anjay = anjay; @@ -594,28 +607,30 @@ void _anjay_mock_dm_expect_instance_write_default_attrs(anjay_t *anjay, command->value.common_attributes = *attrs; } -#define EXPECT_RESOURCE_ACTION_COMMON(UName) \ +#define EXPECT_RESOURCE_ACTION_COMMON(UName) \ anjay_mock_dm_expected_command_t *command = new_expected_command(); \ - command->command = MOCK_DM_RESOURCE_##UName; \ - command->anjay = anjay; \ - command->obj_ptr = obj_ptr; \ - command->input.iid_and_rid.iid = iid; \ - command->input.iid_and_rid.rid = rid; \ + command->command = MOCK_DM_RESOURCE_##UName; \ + command->anjay = anjay; \ + command->obj_ptr = obj_ptr; \ + command->input.iid_and_rid.iid = iid; \ + command->input.iid_and_rid.rid = rid; \ command->retval = retval; -void _anjay_mock_dm_expect_resource_present(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - int retval) { +void _anjay_mock_dm_expect_resource_present( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + int retval) { EXPECT_RESOURCE_ACTION_COMMON(PRESENT); } -void _anjay_mock_dm_expect_resource_operations(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_rid_t rid, - anjay_dm_resource_op_mask_t mask, - int retval) { +void _anjay_mock_dm_expect_resource_operations( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_rid_t rid, + anjay_dm_resource_op_mask_t mask, + int retval) { anjay_mock_dm_expected_command_t *command = new_expected_command(); command->command = MOCK_DM_RESOURCE_OPERATIONS; command->anjay = anjay; @@ -625,51 +640,56 @@ void _anjay_mock_dm_expect_resource_operations(anjay_t *anjay, command->value.mask = mask; } -void _anjay_mock_dm_expect_resource_read(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - int retval, - const anjay_mock_dm_data_t *data) { +void _anjay_mock_dm_expect_resource_read( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + int retval, + const anjay_mock_dm_data_t *data) { EXPECT_RESOURCE_ACTION_COMMON(READ); command->value.data = *data; } -void _anjay_mock_dm_expect_resource_write(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - const anjay_mock_dm_data_t *data, - int retval) { +void _anjay_mock_dm_expect_resource_write( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + const anjay_mock_dm_data_t *data, + int retval) { EXPECT_RESOURCE_ACTION_COMMON(WRITE); command->value.data = *data; } -void _anjay_mock_dm_expect_resource_execute(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - const anjay_mock_dm_data_t *data, - int retval) { +void _anjay_mock_dm_expect_resource_execute( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + const anjay_mock_dm_data_t *data, + int retval) { EXPECT_RESOURCE_ACTION_COMMON(EXECUTE); command->value.data = *data; } -void _anjay_mock_dm_expect_resource_dim(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - int retval) { +void _anjay_mock_dm_expect_resource_dim( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + int retval) { EXPECT_RESOURCE_ACTION_COMMON(DIM); } -void _anjay_mock_dm_expect_resource_read_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_ssid_t ssid, - int retval, - const anjay_dm_internal_res_attrs_t *attrs) { +void _anjay_mock_dm_expect_resource_read_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_ssid_t ssid, + int retval, + const anjay_dm_internal_res_attrs_t *attrs) { anjay_mock_dm_expected_command_t *command = new_expected_command(); command->command = MOCK_DM_RESOURCE_READ_ATTRS; command->anjay = anjay; @@ -685,13 +705,14 @@ void _anjay_mock_dm_expect_resource_read_attrs(anjay_t *anjay, } } -void _anjay_mock_dm_expect_resource_write_attrs(anjay_t *anjay, - const anjay_dm_object_def_t *const *obj_ptr, - anjay_iid_t iid, - anjay_rid_t rid, - anjay_ssid_t ssid, - const anjay_dm_internal_res_attrs_t *attrs, - int retval) { +void _anjay_mock_dm_expect_resource_write_attrs( + anjay_t *anjay, + const anjay_dm_object_def_t *const *obj_ptr, + anjay_iid_t iid, + anjay_rid_t rid, + anjay_ssid_t ssid, + const anjay_dm_internal_res_attrs_t *attrs, + int retval) { anjay_mock_dm_expected_command_t *command = new_expected_command(); command->command = MOCK_DM_RESOURCE_WRITE_ATTRS; command->anjay = anjay; diff --git a/tools/anjay_codegen.py b/tools/anjay_codegen.py index ddd16170f..68e7fc7fe 100755 --- a/tools/anjay_codegen.py +++ b/tools/anjay_codegen.py @@ -146,7 +146,7 @@ static int instance_present(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid) { - (void)anjay; + (void) anjay; return find_instance(get_obj(obj_ptr), iid) != NULL; } @@ -154,7 +154,7 @@ const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t *out, void **cookie) { - (void)anjay; + (void) anjay; AVS_LIST({{ obj_inst_type }}) curr = (AVS_LIST({{ obj_inst_type }}))*cookie; if (!curr) { @@ -235,7 +235,7 @@ static int instance_remove(anjay_t *anjay, const anjay_dm_object_def_t *const *obj_ptr, anjay_iid_t iid) { - (void)anjay; + (void) anjay; {{ obj_repr_type }} *obj = get_obj(obj_ptr); assert(obj); @@ -281,7 +281,7 @@ anjay_iid_t iid, anjay_rid_t rid, anjay_output_ctx_t *ctx) { - (void)anjay; + (void) anjay; {{ obj_repr_type }} *obj = get_obj(obj_ptr); assert(obj); @@ -312,7 +312,7 @@ anjay_iid_t iid, anjay_rid_t rid, anjay_input_ctx_t *ctx) { - (void)anjay; + (void) anjay; {{ obj_repr_type }} *obj = get_obj(obj_ptr); assert(obj); @@ -343,7 +343,7 @@ anjay_iid_t iid, anjay_rid_t rid, anjay_execute_ctx_t *arg_ctx) { - (void)arg_ctx; + (void) arg_ctx; {{ obj_repr_type }} *obj = get_obj(obj_ptr); assert(obj); diff --git a/tools/check-license-headers.py b/tools/check-license-headers.py index ae627de07..e4e4fe1c4 100755 --- a/tools/check-license-headers.py +++ b/tools/check-license-headers.py @@ -50,7 +50,9 @@ '^README\.md$', '^README\.Windows\.md$', '^valgrind_test\.supp$', - '\.png$' + '^\.clang-format$', + '\.png$', + '\.svg$' ])) diff --git a/travis/centos7/Dockerfile b/travis/centos7/Dockerfile index 0498393a0..cb269a6d5 100644 --- a/travis/centos7/Dockerfile +++ b/travis/centos7/Dockerfile @@ -19,7 +19,7 @@ RUN yum install -y which git make cmake boost-python-devel mbedtls-devel openssl # required to compile pybind11 RUN yum install -y python-tools RUN yum install -y python35u python35u-devel python35u-pip -RUN python3.5 -m pip install cryptography requests +RUN python3.5 -m pip install cryptography requests grequests RUN ln -s /usr/bin/python3.5 /usr/bin/python3 COPY . /Anjay CMD cd Anjay && ./devconfig --with-valgrind && make -j && cd test/integration && make integration_check diff --git a/travis/coverity/Dockerfile b/travis/coverity/Dockerfile index b9c053676..7e4f3e365 100644 --- a/travis/coverity/Dockerfile +++ b/travis/coverity/Dockerfile @@ -14,7 +14,7 @@ FROM ubuntu:16.04 RUN apt-get update -RUN apt-get install -y git build-essential cmake libmbedtls-dev \ +RUN apt-get install -y python3-pip git build-essential cmake libmbedtls-dev \ libssl-dev python3 libpython3-dev wget python3-cryptography \ python3-sphinx python3-requests valgrind curl RUN sed -i -e "s/-Wdate-time/ /g" \ diff --git a/travis/prepare.sh b/travis/prepare.sh index 4930f498a..d4cceeb20 100644 --- a/travis/prepare.sh +++ b/travis/prepare.sh @@ -15,7 +15,7 @@ case "$TRAVIS_OS_NAME" in osx) HOMEBREW_NO_AUTO_UPDATE=1 brew install cmake mbedtls python3 - python3 -m pip install cryptography sphinx sphinx_rtd_theme requests + python3 -m pip install cryptography sphinx sphinx_rtd_theme requests grequests # Travis sets CC=gcc for some reason, revert to OSX's default (clang) unset CC diff --git a/travis/ubuntu-16.04/Dockerfile b/travis/ubuntu-16.04/Dockerfile index d636d9d6b..76418fdaa 100644 --- a/travis/ubuntu-16.04/Dockerfile +++ b/travis/ubuntu-16.04/Dockerfile @@ -14,9 +14,10 @@ FROM ubuntu:16.04 RUN apt-get update -RUN apt-get install -y git build-essential cmake libmbedtls-dev \ +RUN apt-get install -y python3-pip git build-essential cmake libmbedtls-dev \ libssl-dev python3 libpython3-dev python3-cryptography \ python3-sphinx valgrind python3-requests +RUN pip3 install grequests RUN sed -i -e "s/-Wdate-time/ /g" \ /usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/Makefile \ /usr/lib/python3.5/plat-x86_64-linux-gnu/_sysconfigdata_m.py