1 /*
2  * Copyright (c) 2017 Linaro Limited
3  * Copyright (c) 2018-2019 Foundries.io
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #define LOG_MODULE_NAME net_lwm2m_obj_firmware_pull
9 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
10 
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
13 
14 #include <string.h>
15 
16 #include "lwm2m_pull_context.h"
17 #include "lwm2m_engine.h"
18 
set_update_result(uint16_t obj_inst_id,int error_code)19 static void set_update_result(uint16_t obj_inst_id, int error_code)
20 {
21 	int result;
22 
23 	if (!error_code) {
24 		lwm2m_firmware_set_update_state_inst(obj_inst_id, STATE_DOWNLOADED);
25 		return;
26 	}
27 
28 	switch (error_code) {
29 	case -ENOMEM:
30 		result = RESULT_OUT_OF_MEM;
31 		break;
32 	case -ENOSPC:
33 		result = RESULT_NO_STORAGE;
34 		break;
35 	case -EFAULT:
36 		result = RESULT_INTEGRITY_FAILED;
37 		break;
38 	case -ENOMSG:
39 		result = RESULT_CONNECTION_LOST;
40 		break;
41 	case -ENOTSUP:
42 		result = RESULT_INVALID_URI;
43 		break;
44 	case -EPROTONOSUPPORT:
45 		result = RESULT_UNSUP_PROTO;
46 		break;
47 	default:
48 		result = RESULT_UPDATE_FAILED;
49 		break;
50 	}
51 
52 	lwm2m_firmware_set_update_result(result);
53 }
54 
55 static struct requesting_object req = {
56 	.obj_inst_id = 0,
57 	.is_firmware_uri = true,
58 	.result_cb = set_update_result,
59 	.verify_cb = NULL
60 };
61 
62 /* TODO: */
lwm2m_firmware_cancel_transfer(void)63 int lwm2m_firmware_cancel_transfer(void)
64 {
65 	return 0;
66 }
67 
lwm2m_firmware_start_transfer(uint16_t obj_inst_id,char * package_uri)68 int lwm2m_firmware_start_transfer(uint16_t obj_inst_id, char *package_uri)
69 {
70 	int error_code;
71 
72 	req.write_cb = lwm2m_firmware_get_write_cb();
73 
74 	/* start file transfer work */
75 	error_code = lwm2m_pull_context_start_transfer(package_uri, req, K_NO_WAIT);
76 
77 	if (error_code) {
78 		return error_code;
79 	}
80 
81 	return 0;
82 }
83