Skip to content

Commit

Permalink
fix(modem): Updated per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Nov 28, 2023
1 parent 6fef7d4 commit 5a05843
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static const char *TAG = "manual_ota";
bool manual_ota::begin()
{
if (status != state::UNDEF) {
ESP_LOGE(TAG, "Invalid state");
ESP_LOGE(TAG, "Invalid state for manual_ota::perform");
return false;
}
const esp_partition_t *configured = esp_ota_get_boot_partition();
Expand All @@ -31,6 +31,7 @@ bool manual_ota::begin()
status = state::INIT;
max_buffer_size_ = size_ * 1024;
if (mode_ == mode::BATCH) {
#ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_CUSTOM_TRANSPORT
esp_transport_handle_t tcp = esp_transport_tcp_init();
ssl_ = esp_transport_batch_tls_init(tcp, max_buffer_size_);
http_.config_.transport = ssl_;
Expand All @@ -40,7 +41,10 @@ bool manual_ota::begin()
if (!esp_transport_batch_set_cn(ssl_, common_name_)) {
return fail();
}

#else
ESP_LOGE(TAG, "mode::BATCH Cannot be used without CONFIG_ESP_HTTP_CLIENT_ENABLE_CUSTOM_TRANSPORT");
return false;
#endif
}

if (!http_.init()) {
Expand Down Expand Up @@ -76,7 +80,7 @@ bool manual_ota::begin()
bool manual_ota::perform()
{
if (status != state::IMAGE_CHECK && status != state::START) {
ESP_LOGE(TAG, "Invalid state");
ESP_LOGE(TAG, "Invalid state for manual_ota::perform");
return false;
}
esp_err_t err = esp_http_client_open(http_.handle_, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

class TlsTransport: public Tls {
public:
explicit TlsTransport(esp_transport_handle_t parent) : Tls(), transport_(parent), read_len(0), offset(0) {}
explicit TlsTransport(esp_transport_handle_t parent):
Tls(), transport_(parent), last_timeout(0), read_len(0), offset(0) {}
int send(const unsigned char *buf, size_t len) override;
int recv(unsigned char *buf, size_t len) override;
static bool set_func(esp_transport_handle_t tls_transport);
Expand Down Expand Up @@ -122,7 +123,10 @@ int TlsTransport::transport::connect(esp_transport_handle_t t, const char *host,
ESP_LOGI(TAG, "Failed to handshake");
return ret;
}
tls->get_session();
if (!tls->get_session()) {
// we're not able to save session, report an error and continue (next connection will be slower)
ESP_LOGW(TAG, "Failed to save session");
}
ESP_LOGI(TAG, "After handshake");
return 0;
}
Expand Down Expand Up @@ -238,7 +242,7 @@ bool TlsTransport::prepare_buffer(size_t max_size)
return true;
}

int esp_transport_batch_tls_pre_read(esp_transport_handle_t t, int len, int timeout_ms)
int esp_transport_batch_tls_pre_read(esp_transport_handle_t t, size_t len, int timeout_ms)
{
auto tls = static_cast<TlsTransport *>(esp_transport_get_context_data(t));
return tls->preread(len, timeout_ms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ esp_transport_handle_t esp_transport_batch_tls_init(esp_transport_handle_t paren
* @param timeout_ms Timeout in ms
* @return true If read from the parent transport completed successfully
*/
bool esp_transport_batch_tls_pre_read(esp_transport_handle_t t, int len, int timeout_ms);
bool esp_transport_batch_tls_pre_read(esp_transport_handle_t t, size_t len, int timeout_ms);

/**
* @brief Set the CA Certificate to verify the server
Expand Down
8 changes: 8 additions & 0 deletions components/esp_modem/test/target_ota/main/ota_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class StatusHandler {

void remove_mqtt()
{
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
esp_mqtt_client_unregister_event(mqtt, MQTT_EVENT_ANY, on_event);
#endif
mqtt = nullptr;
}

Expand Down Expand Up @@ -152,6 +154,10 @@ void ota_task(void *ctx)
ota.http_.config_.cert_pem = ca_cert_pem;
ota.size_ = 32;
ota.common_name_ = CONFIG_TEST_OTA_CN;
#ifndef CONFIG_ESP_HTTP_CLIENT_ENABLE_CUSTOM_TRANSPORT
// will have to use NORMAL mode, before custom transport is supported in IDF
ota.mode_ = manual_ota::mode::NORMAL;
#endif

ota.begin();
while (true) {
Expand Down Expand Up @@ -257,11 +263,13 @@ extern "C" void app_main(void)


esp_modem::SignalGroup ota_done{};
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
// now stop the LCP keepalive before performing OTA
esp_netif_ppp_config_t cfg;
ESP_ERROR_CHECK(esp_netif_ppp_get_params(esp_netif, &cfg));
cfg.ppp_lcp_echo_disabled = true;
ESP_ERROR_CHECK(esp_netif_ppp_set_params(esp_netif, &cfg));
#endif

// Run the OTA in a separate task to keep sending commands to the modem at the same time
xTaskCreate(ota_task, "ota_task", 8192, &ota_done, 5, nullptr);
Expand Down
7 changes: 5 additions & 2 deletions components/esp_modem/test/target_ota/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_PARTITION_TABLE_TWO_OTA=y
CONFIG_TEST_USE_VFS_TERM=y
CONFIG_TEST_OTA_URI="https://raw.githubusercontent.com/david-cermak/esp-network-examples/test/ota/hello_world.bin"
CONFIG_TEST_OTA_URI="https://raw.githubusercontent.com/espressif/esp-protocols/master/components/esp_modem/test/target_ota/bin/blink.bin"
CONFIG_TEST_OTA_CA_CERT="MIIEvjCCA6agAwIBAgIQBtjZBNVYQ0b2ii+nVCJ+xDANBgkqhkiG9w0BAQsFADBhMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBDQTAeFw0yMTA0MTQwMDAwMDBaFw0zMTA0MTMyMzU5NTlaME8xCzAJBgNVBAYTAlVTMRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxKTAnBgNVBAMTIERpZ2lDZXJ0IFRMUyBSU0EgU0hBMjU2IDIwMjAgQ0ExMIIBIjANBgkqhkiG9w0B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/50tx8bIKuxT8b1Z11dmzzp/2n3YWzW2fP9NsarA4h20ksudYbj/NhVfSbCEXffPgK2fPOre3qGNm+499iTcc+G33Mw+nur7SpZyEKEOxEXGlLzyQ4UfaJbcme6ce1XR2bFuAJKZTRei9AqPCCcUZlM51Ke92sRKw2Sfh3oius2FkOH6ipjv3U/697EA7sKPPcw7+uvTPyLNhBzPvOk"
CONFIG_TEST_OTA_CN="github.com"
CONFIG_COMPILER_CXX_EXCEPTIONS=y
CONFIG_ESP_HTTP_CLIENT_ENABLE_CUSTOM_TRANSPORT=y

# This is not supported in IDF yet
# CONFIG_ESP_HTTP_CLIENT_ENABLE_CUSTOM_TRANSPORT=y
CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
CONFIG_LWIP_PPP_SUPPORT=y
Expand Down

0 comments on commit 5a05843

Please sign in to comment.