1 /*
2  * Copyright (c) 2018 O.S.Systems
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /** @file
8  *
9  *  @brief This file contains structures representing JSON messages
10  *  exchanged with a UpdateHub
11  */
12 
13 #ifndef __UPDATEHUB_PRIV_H__
14 #define __UPDATEHUB_PRIV_H__
15 
16 #define UPDATEHUB_API_HEADER \
17 	"Api-Content-Type: application/vnd.updatehub-v1+json"
18 
19 enum updatehub_uri_path {
20 	UPDATEHUB_PROBE = 0,
21 	UPDATEHUB_REPORT,
22 	UPDATEHUB_DOWNLOAD,
23 };
24 
25 enum updatehub_state {
26 	UPDATEHUB_STATE_DOWNLOADING = 0,
27 	UPDATEHUB_STATE_DOWNLOADED,
28 	UPDATEHUB_STATE_INSTALLING,
29 	UPDATEHUB_STATE_INSTALLED,
30 	UPDATEHUB_STATE_REBOOTING,
31 	UPDATEHUB_STATE_ERROR,
32 };
33 
updatehub_response(enum updatehub_response response)34 static char *updatehub_response(enum updatehub_response response)
35 {
36 	switch (response) {
37 	case UPDATEHUB_NETWORKING_ERROR:
38 		return "Fail to connect to the UpdateHub server";
39 	case UPDATEHUB_INCOMPATIBLE_HARDWARE:
40 		return "Incompatible hardware";
41 	case UPDATEHUB_METADATA_ERROR:
42 		return "Fail to parse or to encode the metadata";
43 	case UPDATEHUB_DOWNLOAD_ERROR:
44 		return "Fail while downloading the update package";
45 	case UPDATEHUB_INSTALL_ERROR:
46 		return "Fail while installing the update package";
47 	case UPDATEHUB_FLASH_INIT_ERROR:
48 		return "Fail to initialize the flash";
49 	case UPDATEHUB_NO_UPDATE:
50 		return "No update available";
51 	default:
52 		return NULL;
53 	}
54 }
55 
uri_path(enum updatehub_uri_path type)56 static const char *uri_path(enum updatehub_uri_path type)
57 {
58 	switch (type) {
59 	case UPDATEHUB_PROBE:
60 		return "upgrades";
61 	case UPDATEHUB_REPORT:
62 		return "report";
63 	case UPDATEHUB_DOWNLOAD:
64 		return "products";
65 	default:
66 		return NULL;
67 	}
68 }
69 
state_name(enum updatehub_state state)70 static const char *state_name(enum updatehub_state state)
71 {
72 	switch (state) {
73 	case UPDATEHUB_STATE_DOWNLOADING:
74 		return "downloading";
75 	case UPDATEHUB_STATE_DOWNLOADED:
76 		return "downloaded";
77 	case UPDATEHUB_STATE_INSTALLING:
78 		return "installing";
79 	case UPDATEHUB_STATE_INSTALLED:
80 		return "installed";
81 	case UPDATEHUB_STATE_REBOOTING:
82 		return "rebooting";
83 	case UPDATEHUB_STATE_ERROR:
84 		return "error";
85 	default:
86 		return NULL;
87 	}
88 }
89 
90 struct resp_probe_objects {
91 	const char *mode;
92 	const char *sha256sum;
93 	int size;
94 };
95 
96 struct resp_probe_objects_array {
97 	struct resp_probe_objects objects;
98 };
99 
100 struct resp_probe_objects_array_array {
101 	struct resp_probe_objects_array objects[4];
102 	size_t objects_len;
103 };
104 
105 struct resp_probe_any_boards {
106 	struct resp_probe_objects_array_array objects[2];
107 	size_t objects_len;
108 	const char *product;
109 	const char *supported_hardware;
110 };
111 
112 struct resp_probe_some_boards {
113 	struct resp_probe_objects_array_array objects[2];
114 	size_t objects_len;
115 	const char *product;
116 	const char *supported_hardware[CONFIG_UPDATEHUB_SUPPORTED_HARDWARE_MAX];
117 	size_t supported_hardware_len;
118 };
119 
120 struct updatehub_config_device_identity {
121 	const char *id;
122 };
123 
124 struct report {
125 	const char *product_uid;
126 	const char *hardware;
127 	const char *version;
128 	struct updatehub_config_device_identity device_identity;
129 	const char *status;
130 	const char *package_uid;
131 	const char *error_message;
132 	const char *previous_state;
133 };
134 
135 struct probe {
136 	const char *product_uid;
137 	const char *hardware;
138 	const char *version;
139 	struct updatehub_config_device_identity device_identity;
140 };
141 
142 static const struct json_obj_descr recv_probe_objects_descr[] = {
143 	JSON_OBJ_DESCR_PRIM(struct resp_probe_objects,
144 			    mode, JSON_TOK_STRING),
145 	JSON_OBJ_DESCR_PRIM(struct resp_probe_objects,
146 			    sha256sum, JSON_TOK_STRING),
147 	JSON_OBJ_DESCR_PRIM(struct resp_probe_objects,
148 			    size, JSON_TOK_NUMBER),
149 };
150 
151 static const struct json_obj_descr recv_probe_objects_descr_array[] = {
152 	JSON_OBJ_DESCR_OBJECT(struct resp_probe_objects_array,
153 			      objects, recv_probe_objects_descr),
154 };
155 
156 static const struct json_obj_descr recv_probe_objects_descr_array_array[] = {
157 	JSON_OBJ_DESCR_ARRAY_ARRAY(struct resp_probe_objects_array_array,
158 				   objects, 4, objects_len,
159 				   recv_probe_objects_descr_array,
160 				   ARRAY_SIZE(recv_probe_objects_descr_array)),
161 };
162 
163 static const struct json_obj_descr recv_probe_sh_string_descr[] = {
164 	JSON_OBJ_DESCR_PRIM(struct resp_probe_any_boards,
165 			    product, JSON_TOK_STRING),
166 	JSON_OBJ_DESCR_PRIM_NAMED(struct resp_probe_any_boards,
167 				  "supported-hardware", supported_hardware,
168 				  JSON_TOK_STRING),
169 	JSON_OBJ_DESCR_ARRAY_ARRAY(struct resp_probe_any_boards,
170 				   objects, 2, objects_len,
171 				   recv_probe_objects_descr_array_array,
172 				   ARRAY_SIZE(recv_probe_objects_descr_array_array)),
173 };
174 
175 static const struct json_obj_descr recv_probe_sh_array_descr[] = {
176 	JSON_OBJ_DESCR_PRIM(struct resp_probe_some_boards,
177 			    product, JSON_TOK_STRING),
178 	JSON_OBJ_DESCR_ARRAY_NAMED(struct resp_probe_some_boards,
179 				   "supported-hardware", supported_hardware,
180 				   CONFIG_UPDATEHUB_SUPPORTED_HARDWARE_MAX,
181 				   supported_hardware_len, JSON_TOK_STRING),
182 	JSON_OBJ_DESCR_ARRAY_ARRAY(struct resp_probe_some_boards,
183 				   objects, 2, objects_len,
184 				   recv_probe_objects_descr_array_array,
185 				   ARRAY_SIZE(recv_probe_objects_descr_array_array)),
186 };
187 
188 static const struct json_obj_descr device_identity_descr[] = {
189 	JSON_OBJ_DESCR_PRIM(struct updatehub_config_device_identity,
190 			    id, JSON_TOK_STRING),
191 };
192 
193 static const struct json_obj_descr send_report_descr[] = {
194 	JSON_OBJ_DESCR_PRIM_NAMED(struct report,
195 				  "product-uid", product_uid,
196 				  JSON_TOK_STRING),
197 	JSON_OBJ_DESCR_OBJECT_NAMED(struct report,
198 				    "device-identity", device_identity,
199 				    device_identity_descr),
200 	JSON_OBJ_DESCR_PRIM_NAMED(struct report,
201 				  "error-message", error_message,
202 				  JSON_TOK_STRING),
203 	JSON_OBJ_DESCR_PRIM_NAMED(struct report,
204 				  "previous-state", previous_state,
205 				  JSON_TOK_STRING),
206 	JSON_OBJ_DESCR_PRIM(struct report,
207 			    version, JSON_TOK_STRING),
208 	JSON_OBJ_DESCR_PRIM(struct report,
209 			    hardware, JSON_TOK_STRING),
210 	JSON_OBJ_DESCR_PRIM_NAMED(struct report,
211 				  "package-uid", package_uid,
212 				  JSON_TOK_STRING),
213 	JSON_OBJ_DESCR_PRIM(struct report,
214 			    status, JSON_TOK_STRING),
215 };
216 
217 static const struct json_obj_descr send_probe_descr[] = {
218 	JSON_OBJ_DESCR_PRIM_NAMED(struct probe,
219 				  "product-uid", product_uid,
220 				  JSON_TOK_STRING),
221 	JSON_OBJ_DESCR_OBJECT_NAMED(struct probe,
222 				    "device-identity", device_identity,
223 				    device_identity_descr),
224 	JSON_OBJ_DESCR_PRIM(struct probe,
225 			    version, JSON_TOK_STRING),
226 	JSON_OBJ_DESCR_PRIM(struct probe,
227 			    hardware, JSON_TOK_STRING),
228 };
229 
230 /**
231  * @}
232  */
233 
234 #endif /* __UPDATEHUB_PRIV_H__ */
235