-
Notifications
You must be signed in to change notification settings - Fork 4
/
skyproto.h
374 lines (306 loc) · 8.72 KB
/
skyproto.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* Copyright (C) 2021-2022 Skycharge GmbH
* Author: Roman Penyaev <r.peniaev@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SKYPROTO_H
#define SKYPROTO_H
#include "types.h"
/**
* enum sky_proto_type - skycharge protocol request/responses
*
* BEWARE:
* 1. keep requests/responses sequential, one by one.
* 2. requests are odd, responses are even.
* 3. events start from 128.
*/
enum sky_proto_type {
SKY_UNKNOWN_REQRSP = 0,
/*
* Device requests/responses.
* Format of requests is the following:
* REQ
* DEVPORT (taken from from @sky_dev_info.portname)
* DEVUUID (taken from from @sky_dev_info.dev_uuid)
*
* That is ugly but that simplifies a lot skybroker.
*/
SKY_GET_DEV_PARAMS_REQ = 1,
SKY_GET_DEV_PARAMS_RSP = 2,
SKY_SET_DEV_PARAMS_REQ = 3,
SKY_SET_DEV_PARAMS_RSP = 4,
SKY_RESUME_SCAN_REQ = 5,
SKY_RESUME_SCAN_RSP = 6,
SKY_STOP_SCAN_REQ = 7,
SKY_STOP_SCAN_RSP = 8,
SKY_OPEN_DRONEPORT_REQ = 9,
SKY_OPEN_DRONEPORT_RSP = 10,
SKY_CLOSE_DRONEPORT_REQ = 11,
SKY_CLOSE_DRONEPORT_RSP = 12,
SKY_CHARGING_STATE_REQ = 13,
SKY_CHARGING_STATE_RSP = 14,
SKY_RESET_DEV_REQ = 15,
SKY_RESET_DEV_RSP = 16,
/*
* Format of the request is the following:
* REQ
* USRUUID (taken from from @sky_dev_conf.remote.usr_uuid)
*/
SKY_DEVS_LIST_REQ = 17,
SKY_DEVS_LIST_RSP = 18,
/*
* Format of the request is the following:
* REQ
* USRUUID (taken from from @sky_dev_conf.remote.usr_uuid)
*/
SKY_PEERINFO_REQ = 19,
SKY_PEERINFO_RSP = 20,
/*
* Normal device requests/responses. See format above
*/
SKY_GPSDATA_REQ = 21,
SKY_GPSDATA_RSP = 22,
SKY_DRONEDETECT_REQ = 23,
SKY_DRONEDETECT_RSP = 24,
SKY_DRONEPORT_STATE_REQ = 25,
SKY_DRONEPORT_STATE_RSP = 26,
SKY_SINK_GET_INFO_REQ = 27,
SKY_SINK_GET_INFO_RSP = 28,
SKY_SINK_GET_DEV_PARAMS_REQ = 29,
SKY_SINK_GET_DEV_PARAMS_RSP = 30,
SKY_SINK_SET_DEV_PARAMS_REQ = 31,
SKY_SINK_SET_DEV_PARAMS_RSP = 32,
SKY_SINK_START_CHARGE_REQ = 33,
SKY_SINK_START_CHARGE_RSP = 34,
SKY_SINK_STOP_CHARGE_REQ = 35,
SKY_SINK_STOP_CHARGE_RSP = 36,
SKY_GET_SUBSCRIPTION_TOKEN_REQ = 37,
SKY_GET_SUBSCRIPTION_TOKEN_RSP = 38,
SKY_SINK_FLASH_ERASE_PAGE_REQ = 39,
SKY_SINK_FLASH_ERASE_PAGE_RSP = 40,
SKY_SINK_FLASH_WRITE_CHUNK_REQ = 41,
SKY_SINK_FLASH_ERASE_CHUNK_RSP = 42,
SKY_SINK_FLASH_INFO_REQ = 43,
SKY_SINK_FLASH_INFO_RSP = 44,
SKY_SINK_FLASH_SET_START_ADDR_REQ = 45,
SKY_SINK_FLASH_SET_START_ADDR_RSP = 46,
SKY_SINK_PASSTHRU_MSG_SEND_REQ = 47,
SKY_SINK_PASSTHRU_MSG_SEND_RSP = 48,
SKY_SINK_PASSTHRU_MSG_RECV_REQ = 49,
SKY_SINK_PASSTHRU_MSG_RECV_RSP = 50,
SKY_LAST_REQRSP,
/* Events */
SKY_CHARGING_STATE_EV = 128
};
enum {
DEFAULT_TIMEOUT = 30000,
PORTNAME_LEN = 32,
SKY_PROTO_VERSION = 0x0500,
SKY_HEARTBEAT_IVL_MS = 5000,
SKY_HEARTBEAT_CNT = 3,
SKY_DISCOVERY_PORT = 6666,
SKY_DISCOVERY_MS = 500, /* wait if no broadcasting */
};
#define SKY_DISCOVERY_MAGIC "SKYBROCKER"
#pragma GCC diagnostic push
#pragma GCC diagnostic warning "-Wpadded"
struct sky_discovery {
char magic[16];
le16 proto_version;
le16 servers_port;
le16 sub_port;
le16 clients_port;
le16 pub_port;
char padding[18];
};
struct sky_req_hdr {
le16 type;
le16 proto_version; /*
* Protocol version before 0x0400 does not have
* `proto_version`, so for versions below this
* member is equal to 0.
*/
};
struct sky_rsp_hdr {
le16 type;
le16 error;
};
struct sky_get_dev_params_req {
struct sky_req_hdr hdr;
le32 dev_params_bits;
};
struct sky_get_dev_params_rsp {
struct sky_rsp_hdr hdr;
le32 dev_params[];
};
struct sky_set_dev_params_req {
struct sky_req_hdr hdr;
le32 dev_params_bits;
le32 dev_params[];
};
/*
* Union of all possible requests for allocating the structure on a stack
*/
union sky_req {
struct sky_req_hdr hdr;
struct sky_get_dev_params_req get_dev_params;
struct sky_set_dev_params_req set_dev_params;
};
struct sky_subscription_token_rsp {
struct sky_rsp_hdr hdr;
le16 len;
uint8_t buf[];
};
struct sky_dev_info {
le16 dev_type;
le16 info_len; /*
* Protocol version before 0x0400 does not have
* `info_len`, so for versions below this member
* is equal to 0.
*/
le32 fw_version;
char dev_name[16];
unsigned char dev_uuid[16];
char portname[PORTNAME_LEN];
/* The members below were added for the protocol version 0x0400 */
le16 proto_version;
le16 padding;
le32 hw_version;
le32 plc_proto_version;
struct {
le32 part1;
le32 part2;
le32 part3;
} hw_uid;
};
struct sky_sink_get_info_rsp {
struct sky_rsp_hdr hdr;
le32 fw_version;
le32 hw_version;
le32 plc_proto_version;
struct {
le32 part1;
le32 part2;
le32 part3;
} hw_uid;
};
struct sky_devs_list_rsp {
struct sky_rsp_hdr hdr;
le16 num_devs;
le16 info_off; /*
* Protocol version before 0x0400 does not have `info_off`,
* so for old protocol versions this member is equal to 0.
* New members should be added strictly after `info_off`.
*/
struct sky_dev_info info[]; /*
* This can't be accessed as an array,
* in protocol version 0x400 `info->info_len`
* has to be taken into consideration.
*/
};
struct sky_charging_state_rsp {
struct sky_rsp_hdr hdr;
le16 voltage_mV;
le16 current_mA;
uint8_t dev_hw_state;
uint8_t dev_hw_reason;
le16 state_of_charge; /* 0-100% */
le16 until_full_secs;
le16 charging_secs;
le16 mux_temperature_C;
le16 sink_temperature_C;
le32 energy_mWh;
le32 charge_mAh;
uint8_t mux_humidity_perc; /* 0-100% */
uint8_t link_quality_factor; /* 0-100% */
le16 padding;
struct {
le32 bytes;
le32 packets;
le32 err_bytes;
le32 err_packets;
} tx;
struct {
le32 bytes;
le32 packets;
le32 err_bytes;
le32 err_packets;
} rx;
le32 charging_session_id; /* Random ID for each charging session */
};
struct sky_peerinfo_rsp {
struct sky_rsp_hdr hdr;
le16 proto_version;
le16 padding;
le32 server_version;
char reserved[52];
};
struct sky_droneport_state_rsp {
struct sky_rsp_hdr hdr;
le32 status;
};
struct sky_gpsdata_rsp {
struct sky_rsp_hdr hdr;
le32 padding1;
/* Dilution of precision factors */
struct {
le64 xdop, ydop, pdop, hdop, vdop, tdop, gdop;
} dop;
struct {
le32 mode; /* Mode of fix */
le32 padding1;
le64 time; /* Time of update, unix time in sec with fractional part */
le64 latitude; /* Latitude in degrees (valid if mode >= 2) */
le64 longitude; /* Longitude in degrees (valid if mode >= 2) */
le64 altitude; /* Altitude in meters (valid if mode == 3) */
le32 padding2[8];
} fix;
le32 status; /* GPS status -- always valid */
le32 satellites_used; /* Number of satellites used in solution */
le32 padding2[16];
};
struct sky_dronedetect_rsp {
struct sky_rsp_hdr hdr;
le16 status;
};
struct sky_sink_passthru_msg_send_req {
struct sky_req_hdr hdr;
uint8_t padding;
uint8_t len;
uint8_t buf[];
};
struct sky_sink_passthru_msg_recv_rsp {
struct sky_rsp_hdr hdr;
uint8_t padding;
uint8_t len;
uint8_t buf[];
};
#pragma GCC diagnostic pop
#endif /* SKYPROTO_H */