Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindj-nordic committed Oct 24, 2024
1 parent a7652fb commit 2666f2d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 37 deletions.
2 changes: 2 additions & 0 deletions samples/cellular/http_update/modem_full_update/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion samples/cellular/http_update/modem_full_update/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 4 additions & 8 deletions subsys/net/lib/download_client/src/client_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
43 changes: 28 additions & 15 deletions subsys/net/lib/download_client/src/download_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)) {
Expand All @@ -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) {
Expand All @@ -470,7 +479,6 @@ int download_client_start(struct download_client *dlc,
return err;
}

printk("Transport initialized\n");
k_sleep(K_MSEC(1000));

out:
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions subsys/net/lib/download_client/src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 12 additions & 7 deletions subsys/net/lib/download_client/src/transports/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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)
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions subsys/net/lib/fota_download/src/fota_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 2666f2d

Please sign in to comment.