Skip to content

Commit

Permalink
Anjay-esp32-client 22.12
Browse files Browse the repository at this point in the history
Bugfixes
- Fixed a minor bug related to WiFi reconfiguration - after changing the Enable resource (12/x/1) value Anjay ESP32 Client didn't send the response code to the server
  • Loading branch information
ferre111 authored and Kucmasz committed Dec 13, 2022
1 parent e6e9aad commit 8beedba
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 95 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 22.12 (December 13th, 2022)

### Bugfixes

- Fixed a minor bug related to WiFi reconfiguration - after changing the Enable resource (12/x/1) value Anjay ESP32 Client didn't send the response code to the server

## 22.08 (August 12th, 2022)

### Features
Expand Down
2 changes: 1 addition & 1 deletion main/anjay
Submodule anjay updated 180 files
36 changes: 36 additions & 0 deletions main/anjay-config/anjay/anjay_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,27 @@
*/
#define ANJAY_WITH_NET_STATS

/**
* Enable support for communication timestamp
* (<c>anjay_get_server_last_registration_time()</c>
* <c>anjay_get_server_next_update_time()</c> and
* <c>anjay_get_server_last_communication_time()</c>) APIs.
*/
#define ANJAY_WITH_COMMUNICATION_TIMESTAMP_API

/**
* Enable support for the <c>anjay_resource_observation_status()</c> API.
*/
#define ANJAY_WITH_OBSERVATION_STATUS

/**
* Maximum number of servers observing a given Resource listed by
* <c>anjay_resource_observation_status()</c> function.
*
* Only meaningful if <c>ANJAY_WITH_OBSERVATION_STATUS</c> is enabled.
*/
#define ANJAY_MAX_OBSERVATION_SERVERS_REPORTED_NUMBER 0

/**
* Enable guarding of all accesses to <c>anjay_t</c> with a mutex.
*/
Expand Down Expand Up @@ -438,6 +454,26 @@
*/
#define ANJAY_DTLS_SESSION_BUFFER_SIZE 1024

/**
* Value of Content-Format used in Send messages. Only a few specific values are
* supported:
*
* - @c AVS_COAP_FORMAT_NONE means no default value is used and Anjay will
* decide the format based on the what is available.
* - @c AVS_COAP_FORMAT_OMA_LWM2M_CBOR Anjay will generate a Send message in
* LwM2M CBOR format.
* - @c AVS_COAP_FORMAT_SENML_CBOR Anjay will generate a Send message in SenML
* CBOR format.
* - @c AVS_COAP_FORMAT_SENML_JSON Anjay will generate a Send message in SenML
* JSON format.
*
* Note that to use a specific format it must be available during compilation.
*
* The default value defined in CMake build scripts is
* <c>AVS_COAP_FORMAT_NONE</c>.
*/
#define ANJAY_DEFAULT_SEND_FORMAT AVS_COAP_FORMAT_NONE

/**
* Optional Anjay modules.
*/
Expand Down
38 changes: 38 additions & 0 deletions main/anjay-config/avsystem/commons/avs_commons_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,44 @@
* allocator.
*/
#define AVS_COMMONS_UTILS_WITH_STANDARD_ALLOCATOR

/**
* Enable the alternate implementation of avs_malloc(), avs_free(), avs_calloc()
* and avs_realloc() that uses system malloc(), free() and realloc() calls, but
* includes additional fixup code that ensures proper alignment to
* <c>AVS_ALIGNOF(avs_max_align_t)</c> (usually 8 bytes on common platforms).
*
* <c>AVS_COMMONS_UTILS_WITH_STANDARD_ALLOCATOR</c> and
* <c>AVS_COMMONS_UTILS_WITH_ALIGNFIX_ALLOCATOR</c> cannot be enabled at the
* same time.
*
* NOTE: This implementation is only intended for platforms where the system
* allocator does not properly conform to the alignment requirements.
*
* It comes with an additional runtime costs:
*
* - <c>AVS_ALIGNOF(avs_max_align_t)</c> bytes (usually 8) of additional
* overhead for each allocated memory block
* - Additional memmove() for every realloc() that returned a block that is not
* properly aligned
* - avs_calloc() is implemented as avs_malloc() followed by an explicit
* memset(); this may be suboptimal on some platforms
*
* If these costs are unacceptable for you, you may want to consider fixing,
* replacing or reconfiguring your system allocator for conformance, or
* implementing a custom one instead.
*
* Please note that some code in avs_commons and dependent projects (e.g. Anjay)
* may include runtime assertions for proper memory alignment that will be
* triggered when using a non-conformant standard allocator. Such allocators are
* relatively common in embedded SDKs. This "alignfix" allocator is intended to
* work around these issues. On some platforms (e.g. x86) those alignment issues
* may not actually cause any problems - so you may want to consider disabling
* runtime assertions instead. Please carefully examine your target platform's
* alignment requirements and behavior of misaligned memory accesses (including
* 64-bit data types such as <c>int64_t</c> and <c>double</c>) before doing so.
*/
/* #undef AVS_COMMONS_UTILS_WITH_ALIGNFIX_ALLOCATOR */
/**@}*/

#endif /* AVS_COMMONS_CONFIG_H */
188 changes: 101 additions & 87 deletions main/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static wifi_config_t wifi_config;

#ifdef CONFIG_ANJAY_WIFI_CONNECT_IPV6
# define MAX_IP6_ADDRS_PER_NETIF (5)
# define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (2)
# define NO_OF_IP_ADDRESSES_TO_WAIT_FOR (2)

# if defined(CONFIG_ANJAY_WIFI_CONNECT_IPV6_PREF_LOCAL_LINK)
# define ANJAY_CONNECT_PREFERRED_IPV6_TYPE ESP_IP6_ADDR_IS_LINK_LOCAL
Expand All @@ -54,7 +54,7 @@ static wifi_config_t wifi_config;
# endif // if-elif ANJAY_WIFI_CONNECT_IPV6_PREF_...

#else
# define NR_OF_IP_ADDRESSES_TO_WAIT_FOR (1)
# define NO_OF_IP_ADDRESSES_TO_WAIT_FOR (1)
#endif // CONFIG_ANJAY_WIFI_CONNECT_IPV6

#define MAX_WAITING_TIME_FOR_IP 15000 // in ms
Expand All @@ -76,8 +76,8 @@ static const char *s_ipv6_addr_types[] = { "ESP_IP6_ADDR_IS_UNKNOWN",

static const char *TAG = "anjay_connect";

static esp_netif_t *wifi_start(void);
static void wifi_stop(void);
static void disconnect(void);
static void deinit(void);

/**
* @brief Checks the netif description if it contains specified prefix.
Expand All @@ -88,19 +88,10 @@ static bool is_our_netif(const char *prefix, esp_netif_t *netif) {
return strncmp(prefix, esp_netif_get_desc(netif), strlen(prefix) - 1) == 0;
}

/* set up connection, Wi-Fi */
static void start(void) {

s_anjay_esp_netif = wifi_start();

/* create semaphore if at least one interface is active */
s_semph_get_ip_addrs =
xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
}

/* tear down connection, release resources */
static void stop(void) {
wifi_stop();
disconnect();
deinit();
}

static esp_ip4_addr_t s_ip_addr;
Expand Down Expand Up @@ -146,55 +137,6 @@ static void on_got_ipv6(void *arg,

#endif // CONFIG_ANJAY_WIFI_CONNECT_IPV6

esp_err_t connect_internal(wifi_config_t *conf) {
wifi_config = *conf;
start();
ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
ESP_LOGI(TAG, "Waiting for IP(s)");
for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) {
if (xSemaphoreTake(s_semph_get_ip_addrs,
pdMS_TO_TICKS(MAX_WAITING_TIME_FOR_IP))
== pdFALSE) {
disconnect_internal();
return ESP_FAIL;
}
}
// iterate over active interfaces, and print out IPs of "our" netifs
esp_netif_t *netif = NULL;
esp_netif_ip_info_t ip;
for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) {
netif = esp_netif_next(netif);
if (is_our_netif(TAG, netif)) {
ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif));
ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));

ESP_LOGI(TAG, "- IPv4 address: " IPSTR, IP2STR(&ip.ip));
#ifdef CONFIG_ANJAY_WIFI_CONNECT_IPV6
esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF];
int ip6_addrs = esp_netif_get_all_ip6(netif, ip6);
for (int j = 0; j < ip6_addrs; ++j) {
esp_ip6_addr_type_t ipv6_type =
esp_netif_ip6_get_addr_type(&(ip6[j]));
ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s",
IPV62STR(ip6[j]), s_ipv6_addr_types[ipv6_type]);
}
#endif // CONFIG_ANJAY_WIFI_CONNECT_IPV6
}
}
return ESP_OK;
}

esp_err_t disconnect_internal(void) {
if (s_semph_get_ip_addrs == NULL) {
return ESP_ERR_INVALID_STATE;
}
vSemaphoreDelete(s_semph_get_ip_addrs);
s_semph_get_ip_addrs = NULL;
stop();
ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&stop));
return ESP_OK;
}

static void on_wifi_disconnect(void *arg,
esp_event_base_t event_base,
int32_t event_id,
Expand All @@ -218,31 +160,18 @@ static void on_wifi_connect(void *esp_netif,

#endif // CONFIG_ANJAY_WIFI_CONNECT_IPV6

static esp_netif_t *wifi_start(void) {
char *desc;
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

esp_netif_inherent_config_t esp_netif_config =
ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
// Prefix the interface description with the module TAG
// Warning: the interface desc is used in tests to capture actual connection
// details (IP, gw, mask)
asprintf(&desc, "%s: %s", TAG, esp_netif_config.if_desc);
esp_netif_config.if_desc = desc;
esp_netif_config.route_prio = 128;
esp_netif_t *netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
free(desc);
esp_wifi_set_default_wifi_sta_handlers();
static esp_err_t connect(wifi_config_t *conf) {
wifi_config = *conf;

ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT,
WIFI_EVENT_STA_DISCONNECTED,
&on_wifi_disconnect, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
&on_got_ip, NULL));
#ifdef CONFIG_ANJAY_WIFI_CONNECT_IPV6
ESP_ERROR_CHECK(esp_event_handler_register(
WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, netif));
ESP_ERROR_CHECK(
esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED,
&on_wifi_connect, s_anjay_esp_netif));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6,
&on_got_ipv6, NULL));
#endif // CONFIG_ANJAY_WIFI_CONNECT_IPV6
Expand All @@ -253,11 +182,43 @@ static esp_netif_t *wifi_start(void) {
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
esp_wifi_connect();
return netif;

ESP_ERROR_CHECK(esp_register_shutdown_handler(&stop));
ESP_LOGI(TAG, "Waiting for IP(s)");
for (int i = 0; i < NO_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) {
if (xSemaphoreTake(s_semph_get_ip_addrs,
pdMS_TO_TICKS(MAX_WAITING_TIME_FOR_IP))
== pdFALSE) {
wifi_disconnect();
return ESP_FAIL;
}
}
// iterate over active interfaces, and print out IPs of "our" netifs
esp_netif_t *netif = NULL;
esp_netif_ip_info_t ip;
for (int i = 0; i < esp_netif_get_nr_of_ifs(); ++i) {
netif = esp_netif_next(netif);
if (is_our_netif(TAG, netif)) {
ESP_LOGI(TAG, "Connected to %s", esp_netif_get_desc(netif));
ESP_ERROR_CHECK(esp_netif_get_ip_info(netif, &ip));

ESP_LOGI(TAG, "- IPv4 address: " IPSTR, IP2STR(&ip.ip));
#ifdef CONFIG_ANJAY_WIFI_CONNECT_IPV6
esp_ip6_addr_t ip6[MAX_IP6_ADDRS_PER_NETIF];
int ip6_addrs = esp_netif_get_all_ip6(netif, ip6);
for (int j = 0; j < ip6_addrs; ++j) {
esp_ip6_addr_type_t ipv6_type =
esp_netif_ip6_get_addr_type(&(ip6[j]));
ESP_LOGI(TAG, "- IPv6 address: " IPV6STR ", type: %s",
IPV62STR(ip6[j]), s_ipv6_addr_types[ipv6_type]);
}
#endif // CONFIG_ANJAY_WIFI_CONNECT_IPV6
}
}
return ESP_OK;
}

static void wifi_stop(void) {
esp_netif_t *wifi_netif = s_anjay_esp_netif;
static void disconnect(void) {
ESP_ERROR_CHECK(esp_event_handler_unregister(
WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect));
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
Expand All @@ -273,9 +234,62 @@ static void wifi_stop(void) {
return;
}
ESP_ERROR_CHECK(err);
}

static void deinit(void) {
ESP_ERROR_CHECK(esp_wifi_deinit());
ESP_ERROR_CHECK(
esp_wifi_clear_default_wifi_driver_and_handlers(wifi_netif));
esp_netif_destroy(wifi_netif);
esp_wifi_clear_default_wifi_driver_and_handlers(s_anjay_esp_netif));
esp_netif_destroy(s_anjay_esp_netif);
s_anjay_esp_netif = NULL;
}

void wifi_initialize(void) {
char *desc;

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

esp_netif_inherent_config_t esp_netif_config =
ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
// Prefix the interface description with the module TAG
// Warning: the interface desc is used in tests to capture actual
// connection details (IP, gw, mask)
asprintf(&desc, "%s: %s", TAG, esp_netif_config.if_desc);
esp_netif_config.if_desc = desc;
esp_netif_config.route_prio = 128;
s_anjay_esp_netif = esp_netif_create_wifi(WIFI_IF_STA, &esp_netif_config);
free(desc);
esp_wifi_set_default_wifi_sta_handlers();

/* create semaphore if at least one interface is active */
s_semph_get_ip_addrs =
xSemaphoreCreateCounting(NO_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
}

esp_err_t wifi_connect(wifi_config_t *conf) {
return connect(conf);
}

esp_err_t wifi_disconnect(void) {
if (s_semph_get_ip_addrs == NULL) {
return ESP_ERR_INVALID_STATE;
}
disconnect();
ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&stop));

return ESP_OK;
}

esp_err_t wifi_deinitialize(void) {
if (s_semph_get_ip_addrs == NULL) {
return ESP_ERR_INVALID_STATE;
}
vSemaphoreDelete(s_semph_get_ip_addrs);
s_semph_get_ip_addrs = NULL;

deinit();
ESP_ERROR_CHECK(esp_unregister_shutdown_handler(&stop));

return ESP_OK;
}
6 changes: 4 additions & 2 deletions main/connect.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include "esp_log.h"
#include "esp_wifi.h"

esp_err_t connect_internal(wifi_config_t *conf);
esp_err_t disconnect_internal(void);
void wifi_initialize(void);
esp_err_t wifi_connect(wifi_config_t *conf);
esp_err_t wifi_disconnect(void);
esp_err_t wifi_deinitialize(void);

#endif // _CONNECT_H_
2 changes: 1 addition & 1 deletion main/generate_version.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --always
if(GIT_DESCRIBE_RESULT EQUAL 0)
string(STRIP "${CLIENT_VERSION_S}" CLIENT_VERSION)
else()
set(CLIENT_VERSION "22.08")
set(CLIENT_VERSION "22.12")
endif()
# Input and output files are extracted from parameters.
configure_file("${INPUT_FILE}" "${OUTPUT_FILE}" @ONLY)
Loading

0 comments on commit 8beedba

Please sign in to comment.