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_any_boards {
101 	struct resp_probe_objects_array objects[2];
102 	size_t objects_len;
103 	const char *product;
104 	const char *supported_hardware;
105 };
106 
107 struct resp_probe_some_boards {
108 	struct resp_probe_objects_array objects[2];
109 	size_t objects_len;
110 	const char *product;
111 	const char *supported_hardware[CONFIG_UPDATEHUB_SUPPORTED_HARDWARE_MAX];
112 	size_t supported_hardware_len;
113 };
114 
115 struct updatehub_config_device_identity {
116 	const char *id;
117 };
118 
119 struct report {
120 	const char *product_uid;
121 	const char *hardware;
122 	const char *version;
123 	struct updatehub_config_device_identity device_identity;
124 	const char *status;
125 	const char *package_uid;
126 	const char *error_message;
127 	const char *previous_state;
128 };
129 
130 struct probe {
131 	const char *product_uid;
132 	const char *hardware;
133 	const char *version;
134 	struct updatehub_config_device_identity device_identity;
135 };
136 
137 static const struct json_obj_descr recv_probe_objects_descr[] = {
138 	JSON_OBJ_DESCR_PRIM(struct resp_probe_objects,
139 			    mode, JSON_TOK_STRING),
140 	JSON_OBJ_DESCR_PRIM(struct resp_probe_objects,
141 			    sha256sum, JSON_TOK_STRING),
142 	JSON_OBJ_DESCR_PRIM(struct resp_probe_objects,
143 			    size, JSON_TOK_NUMBER),
144 };
145 
146 static const struct json_obj_descr recv_probe_objects_descr_array[] = {
147 	JSON_OBJ_DESCR_OBJECT(struct resp_probe_objects_array,
148 			      objects, recv_probe_objects_descr),
149 };
150 
151 static const struct json_obj_descr recv_probe_sh_string_descr[] = {
152 	JSON_OBJ_DESCR_PRIM(struct resp_probe_any_boards,
153 			    product, JSON_TOK_STRING),
154 	JSON_OBJ_DESCR_PRIM_NAMED(struct resp_probe_any_boards,
155 				  "supported-hardware", supported_hardware,
156 				  JSON_TOK_STRING),
157 	JSON_OBJ_DESCR_ARRAY_ARRAY(struct resp_probe_any_boards,
158 				   objects, 2, 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_array_descr[] = {
164 	JSON_OBJ_DESCR_PRIM(struct resp_probe_some_boards,
165 			    product, JSON_TOK_STRING),
166 	JSON_OBJ_DESCR_ARRAY_NAMED(struct resp_probe_some_boards,
167 				   "supported-hardware", supported_hardware,
168 				   CONFIG_UPDATEHUB_SUPPORTED_HARDWARE_MAX,
169 				   supported_hardware_len, JSON_TOK_STRING),
170 	JSON_OBJ_DESCR_ARRAY_ARRAY(struct resp_probe_some_boards,
171 				   objects, 2, objects_len,
172 				   recv_probe_objects_descr_array,
173 				   ARRAY_SIZE(recv_probe_objects_descr_array)),
174 };
175 
176 static const struct json_obj_descr device_identity_descr[] = {
177 	JSON_OBJ_DESCR_PRIM(struct updatehub_config_device_identity,
178 			    id, JSON_TOK_STRING),
179 };
180 
181 static const struct json_obj_descr send_report_descr[] = {
182 	JSON_OBJ_DESCR_PRIM_NAMED(struct report,
183 				  "product-uid", product_uid,
184 				  JSON_TOK_STRING),
185 	JSON_OBJ_DESCR_OBJECT_NAMED(struct report,
186 				    "device-identity", device_identity,
187 				    device_identity_descr),
188 	JSON_OBJ_DESCR_PRIM_NAMED(struct report,
189 				  "error-message", error_message,
190 				  JSON_TOK_STRING),
191 	JSON_OBJ_DESCR_PRIM_NAMED(struct report,
192 				  "previous-state", previous_state,
193 				  JSON_TOK_STRING),
194 	JSON_OBJ_DESCR_PRIM(struct report,
195 			    version, JSON_TOK_STRING),
196 	JSON_OBJ_DESCR_PRIM(struct report,
197 			    hardware, JSON_TOK_STRING),
198 	JSON_OBJ_DESCR_PRIM_NAMED(struct report,
199 				  "package-uid", package_uid,
200 				  JSON_TOK_STRING),
201 	JSON_OBJ_DESCR_PRIM(struct report,
202 			    status, JSON_TOK_STRING),
203 };
204 
205 static const struct json_obj_descr send_probe_descr[] = {
206 	JSON_OBJ_DESCR_PRIM_NAMED(struct probe,
207 				  "product-uid", product_uid,
208 				  JSON_TOK_STRING),
209 	JSON_OBJ_DESCR_OBJECT_NAMED(struct probe,
210 				    "device-identity", device_identity,
211 				    device_identity_descr),
212 	JSON_OBJ_DESCR_PRIM(struct probe,
213 			    version, JSON_TOK_STRING),
214 	JSON_OBJ_DESCR_PRIM(struct probe,
215 			    hardware, JSON_TOK_STRING),
216 };
217 
218 /**
219  * @}
220  */
221 
222 #endif /* __UPDATEHUB_PRIV_H__ */
223