Skip to content

Commit c051051

Browse files
author
Maksymilian Wojczuk
committed
Binary download
Fixed some issues with sockets and namings
1 parent 35030b8 commit c051051

File tree

5 files changed

+82
-62
lines changed

5 files changed

+82
-62
lines changed

Inc/communication/binary_download.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _BINARY_DOWNLOAD_H
22
#define _BINARY_DOWNLOAD_H
33

4+
#include "object_target.h"
5+
46
// Downlaod States
57
#define NO_DOWNLOAD_DATA 0
68
#define DOWNLOAD_IN_PROGRESS 1
@@ -17,7 +19,5 @@
1719
#define USB_ERROR -6
1820

1921

20-
21-
22-
int startDownload(char *path, char *filename);
22+
int startDownload(target_instance_t *targetP);
2323
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef __OBJECT_TARGET_H
2+
#define __OBJECT_TARGET_H
3+
4+
5+
/*
6+
* Multiple instance objects can use userdata to store data that will be shared between the different instances.
7+
* The lwm2m_object_t object structure - which represent every object of the liblwm2m as seen in the single instance
8+
* object - contain a chained list called instanceList with the object specific structure target_instance_t:
9+
*/
10+
typedef struct _target_instance_
11+
{
12+
/*
13+
* The first two are mandatories and represent the pointer to the next instance and the ID of this one. The rest
14+
* is the instance scope user data (uint8_t target in this case)
15+
*/
16+
struct _target_instance_ * next; // matches lwm2m_list_t::next
17+
uint16_t shortID; // matches lwm2m_list_t::id
18+
19+
uint8_t flash_state;
20+
uint32_t * target_type;
21+
char * firmware_url;
22+
uint8_t download_state;
23+
int16_t download_error;
24+
uint32_t firmware_version;
25+
char * binary_filename;
26+
} target_instance_t;
27+
28+
29+
#endif

Src/communication/binary_download.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22
#include "lwip/sockets.h"
33
#include "communication_config.h"
44
#include "binary_download.h"
5+
#include "errno.h"
6+
7+
8+
static void download_error(target_instance_t *targetP, int err, int socket) {
9+
targetP->download_state = DOWNLOAD_ERROR;
10+
targetP->download_error = err;
11+
if(err != SOCKET_ERROR){
12+
lwip_close(socket);
13+
}
14+
vTaskDelete(NULL);
15+
}
16+
17+
int startDownload(target_instance_t *targetP) {
518

6-
int startDownload(char *path, char *filename) {
719
int socket;
820
socket = lwip_socket(AF_INET, SOCK_STREAM, 0);
921
if (socket < 0 ) {
10-
printf("cannot create socket\r\n");
11-
return SOCKET_ERROR;
22+
printf("cannot create socket: %d\r\n", errno);
23+
download_error(targetP, SOCKET_ERROR, socket);
1224
}
1325

1426
struct sockaddr_in clientAddress;
@@ -20,15 +32,15 @@ int startDownload(char *path, char *filename) {
2032
if (result < 0) {
2133
printf("cannot connect to server, err: %d\r\n", result);
2234
lwip_close(socket);
23-
return CONNECTION_ERROR;
35+
download_error(targetP,CONNECTION_ERROR, socket);
2436
}
2537

2638
char message[200];
27-
sprintf(message, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n Connection: keep-alive\r\n\r\n Keep-Alive: 300\r\n", path, SERVER_IP);
39+
sprintf(message, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n Connection: keep-alive\r\n\r\n Keep-Alive: 300\r\n", targetP->firmware_url, SERVER_IP);
2840

2941
if( lwip_send(socket , message , strlen(message) , 0) < 0) {
3042
printf("Send failed\r\n");
31-
return REQUEST_ERROR;
43+
download_error(targetP,REQUEST_ERROR, socket);
3244
}
3345

3446
int server_response_length = 300;
@@ -40,17 +52,19 @@ int startDownload(char *path, char *filename) {
4052
int received_len = lwip_recv(socket, server_reply , sizeof server_reply - 1, 0);
4153
if( received_len < 0 ){
4254
printf("recv failed\r\n");
43-
return RECEIVE_ERROR;
55+
download_error(targetP,RECEIVE_ERROR, socket);
4456
}
4557

4658
if(!lwm2m_strncmp(server_reply, "HTTP/1.1 200 OK", 15) == 0){
4759
long response_code;
48-
printf("Wrong server_reply\r\n");
49-
if(xatoi(server_reply + 9, &response_code) != 1) {
60+
char *asd;
61+
asd = server_reply + 9;
62+
if(xatoi(&asd, &response_code) != 1) {
5063
printf("Error reading Server response code\r\n");
51-
return UNKNOWN_ERROR;
64+
download_error(targetP,UNKNOWN_ERROR, socket);
5265
}
53-
return response_code;
66+
printf("Server Response status:%d\r\n", response_code);
67+
download_error(targetP, response_code, socket);
5468
}
5569
printf("Server responded with 200!\r\n");
5670
for(int i = 15 ; i < received_len ; i++) {
@@ -59,7 +73,7 @@ int startDownload(char *path, char *filename) {
5973
asd = server_reply + i + 15;
6074
if (xatoi(&asd, &payload_len) != 1) {
6175
printf("Error reading Content-Length\r\n");
62-
return UNKNOWN_ERROR;
76+
download_error(targetP, UNKNOWN_ERROR, socket);
6377
}
6478
break;
6579
}
@@ -70,22 +84,26 @@ int startDownload(char *path, char *filename) {
7084

7185
if( received_len < 0 ){
7286
printf("recv failed\r\n");
73-
return RECEIVE_ERROR;
87+
download_error(targetP, RECEIVE_ERROR, socket);
7488
}
7589

7690
total_received_len += received_len;
7791
server_reply[received_len] = '\0';
7892

7993
if(get_usb_ready()) {
80-
usb_write(server_reply, filename, received_len);
94+
usb_write(server_reply, targetP->binary_filename, received_len);
8195
} else {
82-
return USB_ERROR;
96+
download_error(targetP, USB_ERROR, socket);
8397
}
8498

8599
if( total_received_len >= payload_len ){
86100
break;
87101
}
88102
}
89103

90-
return NO_ERROR;
104+
targetP->download_state = DOWNLOAD_COMPLETED;
105+
targetP->download_error = NO_ERROR;
106+
lwip_close(socket);
107+
vTaskDelete();
108+
91109
}

Src/communication/wakaama_client/objects/object_target.c

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <string.h>
2828
#include <ctype.h>
2929
#include <limits.h>
30+
#include "object_target.h"
3031
#include "binary_download.h"
3132

3233
static void prv_output_buffer(uint8_t * buffer,
@@ -66,29 +67,6 @@ static void prv_output_buffer(uint8_t * buffer,
6667
}
6768
}
6869

69-
/*
70-
* Multiple instance objects can use userdata to store data that will be shared between the different instances.
71-
* The lwm2m_object_t object structure - which represent every object of the liblwm2m as seen in the single instance
72-
* object - contain a chained list called instanceList with the object specific structure target_instance_t:
73-
*/
74-
typedef struct _target_instance_
75-
{
76-
/*
77-
* The first two are mandatories and represent the pointer to the next instance and the ID of this one. The rest
78-
* is the instance scope user data (uint8_t target in this case)
79-
*/
80-
struct _target_instance_ * next; // matches lwm2m_list_t::next
81-
uint16_t shortID; // matches lwm2m_list_t::id
82-
83-
uint8_t flash_state;
84-
uint32_t * target_type;
85-
char * firmware_url;
86-
uint8_t download_state;
87-
uint16_t download_error;
88-
uint32_t firmware_version;
89-
char * binary_filename;
90-
} target_instance_t;
91-
9270
static uint8_t target_read(uint16_t instanceId,
9371
int * numDataP,
9472
lwm2m_data_t ** dataArrayP,
@@ -225,22 +203,26 @@ static uint8_t target_write(uint16_t instanceId,
225203
if (!dataArray[i].type == LWM2M_TYPE_STRING && !dataArray[i].type == LWM2M_TYPE_OPAQUE) {
226204
return COAP_400_BAD_REQUEST;
227205
}
228-
if (targetP->firmware_url != NULL) {
229-
lwm2m_free(targetP->firmware_url);
230-
}
206+
// if (targetP->firmware_url != NULL) {
207+
// lwm2m_free(targetP->firmware_url);
208+
// }
209+
dataArray[i].value.asBuffer.buffer[dataArray[i].value.asBuffer.length] = '\0';
231210
targetP->firmware_url = lwm2m_strdup((char*)dataArray[i].value.asBuffer.buffer);
232211
targetP->firmware_version = lwm2m_gettime();
233212
targetP->download_state = DOWNLOAD_IN_PROGRESS;
234213
sprintf(targetP->binary_filename, "%d", targetP->firmware_version);
235214

236-
int res = startDownload(targetP->firmware_url, targetP->binary_filename);
237-
if (res == NO_ERROR) {
238-
targetP->download_state = DOWNLOAD_COMPLETED;
239-
targetP->download_error = NO_ERROR;
240-
} else {
241-
targetP->download_state = DOWNLOAD_ERROR;
242-
targetP->download_error = res;
243-
}
215+
216+
xTaskCreate(startDownload, NULL, 2000, targetP, 2, NULL);
217+
218+
// int res = startDownload(targetP->firmware_url, targetP->binary_filename);
219+
// if (res == NO_ERROR) {
220+
// targetP->download_state = DOWNLOAD_COMPLETED;
221+
// targetP->download_error = NO_ERROR;
222+
// } else {
223+
// targetP->download_state = DOWNLOAD_ERROR;
224+
// targetP->download_error = res;
225+
// }
244226
}
245227
break;
246228
case 3:

Src/main.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,6 @@ void StartDefaultTask(void const * argument)
372372
fflush(stdout);
373373
get_line(usrInput, 2);
374374
printf("%s\n", usrInput);
375-
char *filename;
376-
filename = pvPortMalloc(100);
377375
switch(usrInput[0]) {
378376
case 'l':
379377
if (get_usb_ready()) {
@@ -385,13 +383,6 @@ void StartDefaultTask(void const * argument)
385383
usb_write("asd", "temp", 3);
386384
}
387385
break;
388-
case 'd':
389-
sprintf(filename,"%d", lwm2m_gettime());
390-
int res = startDownload("/asd.txt", filename);
391-
if(res != NO_ERROR){
392-
printf("error downloading file: %d\r\n", res);
393-
}
394-
break;
395386
}
396387
}
397388
/* USER CODE END 5 */

0 commit comments

Comments
 (0)