BSB Developer Docs
Loading...
Searching...
No Matches
cli_intercom_common_i.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <stdint.h>
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14// ===================
15// Protocol definition
16// ===================
17
18typedef enum {
19 // Status messages (length == 0, no payload):
20 CliIntercomMessageTypeSpawn,
21 CliIntercomMessageTypeDisconnect,
22
23 // Payload messages (length >= 0, has payload):
24 CliIntercomMessageTypeData,
25
26 // Don't use:
27 CliIntercomMessageTypeMAX,
28} CliIntercomMessageType;
29
30#define CliIntercomMessageTypeStatusMAX CliIntercomMessageTypeData
31
32#define CLI_INTERCOM_MESSAGE_TYPE_BITS (2)
33#define CLI_INTERCOM_PAYLOAD_LENGTH_BITS (6)
34
35// =====================================
36// Serialization/deserialization helpers
37// =====================================
38
39#define CLI_INTERCOM_MAX_PAYLOAD_LEN ((size_t)((1 << CLI_INTERCOM_PAYLOAD_LENGTH_BITS) - 1))
40
41static_assert(CLI_INTERCOM_MESSAGE_TYPE_BITS + CLI_INTERCOM_PAYLOAD_LENGTH_BITS == 8);
42static_assert(CliIntercomMessageTypeMAX <= (1 << CLI_INTERCOM_MESSAGE_TYPE_BITS));
43
44static inline uint8_t
45 cli_intercom_construct_status(CliIntercomMessageType msg_type, uint8_t length) {
46 furi_assert(msg_type < CliIntercomMessageTypeMAX);
47 furi_assert(length <= CLI_INTERCOM_MAX_PAYLOAD_LEN);
48
49#ifdef BSB_MCU_SI917
50 furi_assert(msg_type != CliIntercomMessageTypeSpawn); // can't spawn a shell on u5
51#endif
52 return (msg_type << CLI_INTERCOM_PAYLOAD_LENGTH_BITS) | length;
53}
54
55static inline void
56 cli_intercom_parse_status(uint8_t status, CliIntercomMessageType* msg_type, uint8_t* length) {
57 *msg_type = status >> CLI_INTERCOM_PAYLOAD_LENGTH_BITS;
58 *length = status & CLI_INTERCOM_MAX_PAYLOAD_LEN;
59 if(*msg_type < CliIntercomMessageTypeStatusMAX) furi_assert(!*length);
60
61#ifdef BSB_MCU_U5
62 furi_assert(*msg_type != CliIntercomMessageTypeSpawn); // can't spawn a shell on u5
63#endif
64}
65
66#ifdef __cplusplus
67}
68#endif