From 6573c1d0e230eb603280fd605beab76e4f0c8c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eivind=20J=C3=B8lsgard?= Date: Thu, 24 Oct 2024 07:28:47 +0200 Subject: [PATCH] tmp --- .../http_update/modem_full_update/prj.conf | 2 + .../http_update/modem_full_update/src/main.c | 7 ++- .../lib/download_client/src/download_client.c | 43 ++++++++++++++----- subsys/net/lib/download_client/src/parse.c | 10 +++-- .../lib/download_client/src/transports/http.c | 17 +++++--- 5 files changed, 58 insertions(+), 21 deletions(-) diff --git a/samples/cellular/http_update/modem_full_update/prj.conf b/samples/cellular/http_update/modem_full_update/prj.conf index 762119a9034..60ac87806c0 100644 --- a/samples/cellular/http_update/modem_full_update/prj.conf +++ b/samples/cellular/http_update/modem_full_update/prj.conf @@ -8,6 +8,8 @@ CONFIG_NCS_SAMPLES_DEFAULTS=y CONFIG_REBOOT=y CONFIG_NEWLIB_LIBC=y +CONFIG_NET_IPV4=y + # Network CONFIG_NETWORKING=y CONFIG_NET_SOCKETS=y diff --git a/samples/cellular/http_update/modem_full_update/src/main.c b/samples/cellular/http_update/modem_full_update/src/main.c index 701db84b9b2..b9e9c196c83 100644 --- a/samples/cellular/http_update/modem_full_update/src/main.c +++ b/samples/cellular/http_update/modem_full_update/src/main.c @@ -238,7 +238,12 @@ static void current_version_display(void) static int apply_state(enum fota_state new_state) { - __ASSERT(state != new_state, "State already set: %d", state); + printk("Apply state %d\n", new_state); + + //__ASSERT(state != new_state, "State already set: %d", state); + if (state == new_state) { + printk("Same state! %d\n", new_state); + } state = new_state; diff --git a/subsys/net/lib/download_client/src/download_client.c b/subsys/net/lib/download_client/src/download_client.c index 85c2acd9ed5..1e23aff361d 100644 --- a/subsys/net/lib/download_client/src/download_client.c +++ b/subsys/net/lib/download_client/src/download_client.c @@ -49,6 +49,7 @@ char *state_to_str(int state) { static void state_set(struct download_client *dlc, int state) { + printk("Set dlc state %d\n", state); k_mutex_lock(&dlc->mutex, K_FOREVER); dlc->state = state; k_mutex_unlock(&dlc->mutex); @@ -110,7 +111,6 @@ static int transport_close(struct download_client *dlc) { } err = ((struct dlc_transport *)dlc->transport)->close(dlc); - close_evt_send(dlc); return err; } @@ -148,12 +148,13 @@ static int reconnect(struct download_client *dlc) static void restart_and_suspend(struct download_client *dlc) { - if (!is_state(dlc, DOWNLOAD_CLIENT_DOWNLOADING)) { - return; - } + //if (!is_state(dlc, DOWNLOAD_CLIENT_DOWNLOADING)) { + // return; + //} if (!dlc->host_config.keep_connection) { transport_close(dlc); + close_evt_send(dlc); state_set(dlc, DOWNLOAD_CLIENT_IDLE); return; } @@ -305,6 +306,7 @@ void download_thread(void *cli, void *a, void *b) rc = error_evt_send(dlc, rc); if (rc) { restart_and_suspend(dlc); + continue; } rc = reconnect(dlc); @@ -317,6 +319,7 @@ void download_thread(void *cli, void *a, void *b) if (is_state(dlc, DOWNLOAD_CLIENT_DEINITIALIZING)) { printk("Deinitializing download client"); transport_close(dlc); + close_evt_send(dlc); transport_deinit(dlc); state_set(dlc, DOWNLOAD_CLIENT_DEINITIALIZED); deinit_evt_send(dlc); @@ -439,8 +442,18 @@ int download_client_start(struct download_client *dlc, } if (!dlc->transport) { - LOG_ERR("Protocol not specified for %s", uri); - return -EINVAL; + LOG_WRN("Protocol not specified for %s, attempting http", uri); + STRUCT_SECTION_FOREACH(dlc_transport_entry, entry) { + if (entry->transport->proto_supported(dlc, "http://")) { + dlc->transport = entry->transport; + break; + } + } + + if (!dlc->transport) { + LOG_ERR("Protocol not specified for %s and http could not be used", uri); + return -EINVAL; + } }; printk("Found transport, state is %d", dlc->state); @@ -483,13 +496,23 @@ int download_client_start(struct download_client *dlc, int download_client_stop(struct download_client *const dlc) { - if (dlc == NULL || is_state(dlc, DOWNLOAD_CLIENT_IDLE)) { + if (dlc == NULL || + is_state(dlc, DOWNLOAD_CLIENT_IDLE) || + is_state(dlc, DOWNLOAD_CLIENT_CONNECTED) || + is_state(dlc, DOWNLOAD_CLIENT_DEINITIALIZED)) { return -EINVAL; } - transport_close(dlc); - close_evt_send(dlc); - state_set(dlc, DOWNLOAD_CLIENT_IDLE); + error_evt_send(dlc, -ECANCELED); + + if (!dlc->host_config.keep_connection) { + transport_close(dlc); + close_evt_send(dlc); + state_set(dlc, DOWNLOAD_CLIENT_IDLE); + return 0; + } + + state_set(dlc, DOWNLOAD_CLIENT_CONNECTED); return 0; } diff --git a/subsys/net/lib/download_client/src/parse.c b/subsys/net/lib/download_client/src/parse.c index 8b383220b91..806c4bfd3f0 100644 --- a/subsys/net/lib/download_client/src/parse.c +++ b/subsys/net/lib/download_client/src/parse.c @@ -113,12 +113,14 @@ int url_parse_file(const char *url, char *file, size_t len) if (err) { return -EINVAL; } - err = swallow(&cur, "/"); - if (err) { - return -EINVAL; - } + } + //err = swallow(&cur, "/"); + //if (err) { + // return -EINVAL; + //} + if (strlen(cur) + 1 > len) { return -E2BIG; } diff --git a/subsys/net/lib/download_client/src/transports/http.c b/subsys/net/lib/download_client/src/transports/http.c index 642a9b59da9..38c9642c549 100644 --- a/subsys/net/lib/download_client/src/transports/http.c +++ b/subsys/net/lib/download_client/src/transports/http.c @@ -149,6 +149,8 @@ int http_get_request_send(struct download_client *dlc) LOG_HEXDUMP_DBG(dlc->config.buf, len, "HTTP request"); } + printk("SENDING REQUEST\n%s\n", dlc->config.buf); + err = client_socket_send(http->sock.fd, dlc->config.buf, len); if (err) { LOG_ERR("Failed to send HTTP request, errno %d", errno); @@ -172,6 +174,8 @@ static int http_header_parse(struct download_client *dlc, size_t buf_len) http = (struct transport_params_http *)dlc->transport_internal; + printk("%s", dlc->config.buf); + p = strnstr(dlc->config.buf, "\r\n\r\n", dlc->config.buf_size); if (p) { /* End of header received */ @@ -252,11 +256,6 @@ static int http_header_parse(struct download_client *dlc, size_t buf_len) * Verify that we have received everything that we need. */ - if (!dlc->file_size) { - LOG_ERR("File size not set"); - return -EBADMSG; - } - if (!http->header.status_code) { LOG_ERR("Server response malformed: status code not found"); return -EBADMSG; @@ -268,6 +267,11 @@ static int http_header_parse(struct download_client *dlc, size_t buf_len) return -EBADMSG; } + if (!dlc->file_size) { + LOG_ERR("File size not set"); + return -EBADMSG; + } + return parse_len; } @@ -375,7 +379,8 @@ static int dlc_http_init(struct download_client *dlc, struct download_client_hos http->sock.proto = IPPROTO_TCP; http->sock.type = SOCK_STREAM; - if (strncmp(uri, "https://", 8) == 0) { + if (strncmp(uri, "https://", 8) == 0 || + (host_cfg->sec_tag_count != 0 && host_cfg->sec_tag_list == NULL)) { http->sock.proto = IPPROTO_TLS_1_2; http->sock.type = SOCK_STREAM;