Skip to content

Commit

Permalink
Add support for IDF SDK 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
fadushin committed Dec 26, 2023
1 parent 97f0d32 commit 645cf06
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

strategy:
matrix:
idf-version: ["4.4.3"]
idf-version: ["4.4.3", "5.1.2"]
otp: ["24"]
elixir_version: ["1.11"]
soc: ["esp32", "esp32c3", "esp32s2", "esp32s3"]
Expand All @@ -41,7 +41,7 @@ jobs:
run: apt update -y

- name: "Install deps"
run: DEBIAN_FRONTEND=noninteractive apt install -y git cmake
run: DEBIAN_FRONTEND=noninteractive apt install -y git cmake zlib

- name: "System info"
run: |
Expand Down
21 changes: 16 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# 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
# 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,
Expand All @@ -22,13 +22,24 @@ set(ATOMVM_MQTT_CLIENT_COMPONENT_SRCS
"ports/atomvm_mqtt_client.c"
)

# WHOLE_ARCHIVE option is supported only with esp-idf 5.x
# A link option will be used with esp-idf 4.x
if(IDF_VERSION_MAJOR EQUAL 5)
set(OPTIONAL_WHOLE_ARCHIVE WHOLE_ARCHIVE)
else()
set(OPTIONAL_WHOLE_ARCHIVE "")
endif()

idf_component_register(
SRCS ${ATOMVM_MQTT_CLIENT_COMPONENT_SRCS}
INCLUDE_DIRS "ports/include"
PRIV_REQUIRES "libatomvm" "avm_sys" "mqtt"
${OPTIONAL_WHOLE_ARCHIVE}
)

idf_build_set_property(
LINK_OPTIONS "-Wl,--whole-archive ${CMAKE_CURRENT_BINARY_DIR}/lib${COMPONENT_NAME}.a -Wl,--no-whole-archive"
APPEND
)
if(IDF_VERSION_MAJOR EQUAL 4)
idf_build_set_property(
LINK_OPTIONS "-Wl,--whole-archive ${CMAKE_CURRENT_BINARY_DIR}/lib${COMPONENT_NAME}.a -Wl,--no-whole-archive"
APPEND
)
endif()
17 changes: 13 additions & 4 deletions ports/atomvm_mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,11 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
TRACE(TAG ": mqtt_event_handler\n");
esp_mqtt_event_handle_t event = event_data;

#if ESP_IDF_VERSION_MAJOR >= 5
Context *ctx = (Context *) handler_args;
#else
Context *ctx = (Context *) event->user_context;
#endif
GlobalContext *global = ctx->global;

struct platform_data *plfdat = (struct platform_data *) ctx->platform_data;
Expand Down Expand Up @@ -529,8 +533,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
TRACE(TAG ": TOPIC=%.*s\n", event->topic_len, event->topic);
TRACE(TAG ": DATA=%.*s\n", event->data_len, event->data);

int topic_size = term_binary_data_size_in_terms(event->topic_len) + BINARY_HEADER_SIZE;
int data_size = term_binary_data_size_in_terms(event->data_len) + BINARY_HEADER_SIZE;
int topic_size = term_binary_heap_size(event->topic_len);
int data_size = term_binary_heap_size(event->data_len);

size_t requested_size = TUPLE_SIZE(4) + topic_size + data_size;
Heap heap;
Expand Down Expand Up @@ -585,12 +589,12 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
}

case MQTT_EVENT_BEFORE_CONNECT: {
ESP_LOGI(TAG, "MQTT_EVENT_BEFORE_CONNECT event_id: %d", event_id);
ESP_LOGI(TAG, "MQTT_EVENT_BEFORE_CONNECT event_id: %li", event_id);
break;
}

default:
ESP_LOGW(TAG, "Other event. event_id: %d", event_id);
ESP_LOGW(TAG, "Other event. event_id: %li", event_id);
break;
}

Expand Down Expand Up @@ -778,9 +782,14 @@ Context *atomvm_mqtt_client_create_port(GlobalContext *global, term opts)
// Note that char * values passed into this struct are copied into the MQTT state
const char *client_id = get_default_client_id();
esp_mqtt_client_config_t mqtt_cfg = {
#if ESP_IDF_VERSION_MAJOR >= 5
.broker.address.uri = url_str,
.credentials.client_id = client_id
#else
.uri = url_str,
.client_id = client_id,
.user_context = (void *) ctx
#endif
};
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);

Expand Down

0 comments on commit 645cf06

Please sign in to comment.