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 76ccf6a commit 6573c1d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 21 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
43 changes: 33 additions & 10 deletions subsys/net/lib/download_client/src/download_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
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,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;
}
Expand Down
17 changes: 11 additions & 6 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");
}

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);
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;

printk("%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 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

0 comments on commit 6573c1d

Please sign in to comment.