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 f92b9d8edbc..b426e86dea2 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/client_socket.c b/subsys/net/lib/download_client/src/client_socket.c index cec67dd4a70..aef8b19873c 100644 --- a/subsys/net/lib/download_client/src/client_socket.c +++ b/subsys/net/lib/download_client/src/client_socket.c @@ -138,11 +138,10 @@ int client_socket_host_lookup(const char * const hostname, uint8_t pdn_id, struc int err; char pdnserv[4]; char *servname = NULL; - //char hostname[HOSTNAME_SIZE]; struct addrinfo *ai; struct addrinfo hints = {}; - printk("host lookup %s, pdn id %d\n", hostname, pdn_id); + LOG_DBG("host lookup %s, pdn id %d\n", hostname, pdn_id); if (pdn_id) { hints.ai_flags = AI_PDNSERV; @@ -151,7 +150,6 @@ int client_socket_host_lookup(const char * const hostname, uint8_t pdn_id, struc } #if CONFIG_NET_IPV6 - printk("IPv6\n"); hints.ai_family = AF_INET6; err = getaddrinfo(hostname, servname, &hints, &ai); if (err) { @@ -163,21 +161,19 @@ int client_socket_host_lookup(const char * const hostname, uint8_t pdn_id, struc #endif #if CONFIG_NET_IPV4 - printk("IPv4\n"); hints.ai_family = AF_INET; err = getaddrinfo(hostname, servname, &hints, &ai); if (err) { - printk("Failed to resolve hostname %s on %s, err %d\n", + LOG_DBG("Failed to resolve hostname %s on %s, err %d\n", hostname, str_family(hints.ai_family), err); } else { goto out; } #endif - printk("Failed to resolve hostname %s\n", hostname); + LOG_ERR("Failed to resolve hostname %s\n", hostname); return -EHOSTUNREACH; out: - printk("End of host lookup\n"); k_sleep(K_SECONDS(1)); memcpy(sa, ai->ai_addr, ai->ai_addrlen); freeaddrinfo(ai); @@ -206,7 +202,7 @@ int client_socket_configure_and_connect( goto cleanup; } - printk("family: %d, type: %d, proto: %d\n", + LOG_DBG("family: %d, type: %d, proto: %d\n", remote_addr->sa_family, type, proto); *fd = socket(remote_addr->sa_family, type, proto); diff --git a/subsys/net/lib/download_client/src/download_client.c b/subsys/net/lib/download_client/src/download_client.c index 85c2acd9ed5..636aafef58e 100644 --- a/subsys/net/lib/download_client/src/download_client.c +++ b/subsys/net/lib/download_client/src/download_client.c @@ -110,7 +110,6 @@ static int transport_close(struct download_client *dlc) { } err = ((struct dlc_transport *)dlc->transport)->close(dlc); - close_evt_send(dlc); return err; } @@ -154,6 +153,7 @@ static void restart_and_suspend(struct download_client *dlc) { if (!dlc->host_config.keep_connection) { transport_close(dlc); + close_evt_send(dlc); state_set(dlc, DOWNLOAD_CLIENT_IDLE); return; } @@ -305,6 +305,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); @@ -315,8 +316,8 @@ 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); @@ -429,7 +430,7 @@ int download_client_start(struct download_client *dlc, dlc->transport = NULL; - printk("Finding transport for %s\n", uri); + LOG_INF("Finding transport for %s", uri); STRUCT_SECTION_FOREACH(dlc_transport_entry, entry) { if (entry->transport->proto_supported(dlc, uri)) { @@ -439,11 +440,19 @@ 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; + } + } - printk("Found transport, state is %d", dlc->state); + if (!dlc->transport) { + LOG_ERR("Protocol not specified for %s and http could not be used", uri); + return -EINVAL; + } + }; if (is_state(dlc, DOWNLOAD_CLIENT_CONNECTED)) { if (dlc->transport == transport_connected) { @@ -470,7 +479,6 @@ int download_client_start(struct download_client *dlc, return err; } - printk("Transport initialized\n"); k_sleep(K_MSEC(1000)); out: @@ -483,13 +491,21 @@ 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); + 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; } @@ -504,9 +520,6 @@ int download_client_get(struct download_client *dlc, return -EINVAL; } - printk("Get file\n"); - k_sleep(K_SECONDS(1)); - k_mutex_lock(&dlc->mutex, K_FOREVER); rc = download_client_start(dlc, host_config, uri, from); diff --git a/subsys/net/lib/download_client/src/parse.c b/subsys/net/lib/download_client/src/parse.c index 8b383220b91..a735f3230aa 100644 --- a/subsys/net/lib/download_client/src/parse.c +++ b/subsys/net/lib/download_client/src/parse.c @@ -113,10 +113,12 @@ 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) { diff --git a/subsys/net/lib/download_client/src/transports/http.c b/subsys/net/lib/download_client/src/transports/http.c index 642a9b59da9..6caba308cf2 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"); } + LOG_DBG("http 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; + LOG_DBG("(partial) http header response:\n%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; } @@ -290,7 +294,7 @@ static int http_header_parse(struct download_client *dlc, size_t buf_len) } /* Returns: - * length of data payload received on success + * Length of data payload left to process on success * Negative errno on error. */ int http_parse(struct download_client *dlc, size_t 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; diff --git a/subsys/net/lib/fota_download/src/fota_download.c b/subsys/net/lib/fota_download/src/fota_download.c index f23a4400160..84ac2c2e6ff 100644 --- a/subsys/net/lib/fota_download/src/fota_download.c +++ b/subsys/net/lib/fota_download/src/fota_download.c @@ -333,7 +333,7 @@ static int download_client_callback(const struct download_client_evt *event) goto error_and_close; } else { - LOG_ERR("Download client error"); + LOG_ERR("Download client error event"); err = dfu_target_done(false); if (err == -EACCES) { LOG_DBG("No DFU target was initialized"); @@ -582,7 +582,7 @@ int fota_download(const char *uri, static int sec_tag_list_copy[CONFIG_FOTA_DOWNLOAD_SEC_TAG_LIST_SIZE_MAX]; struct download_client_host_cfg host_config = { .pdn_id = pdn_id, - .range_override = fragment_size, + //.range_override = fragment_size, }; if (sec_tag_count > ARRAY_SIZE(sec_tag_list_copy)) {