Firmware SDK
twr_radio.h
1 #ifndef _TWR_RADIO_H
2 #define _TWR_RADIO_H
3 
4 #include <twr_common.h>
5 #include <twr_button.h>
6 #include <twr_led.h>
7 #include <twr_spirit1.h>
8 
12 
13 #ifndef TWR_RADIO_MAX_DEVICES
14 #define TWR_RADIO_MAX_DEVICES 4
15 #endif
16 
17 #ifndef TWR_RADIO_PUB_QUEUE_BUFFER_SIZE
18 #define TWR_RADIO_PUB_QUEUE_BUFFER_SIZE 512
19 #endif
20 
21 #ifndef TWR_RADIO_RX_QUEUE_BUFFER_SIZE
22 #define TWR_RADIO_RX_QUEUE_BUFFER_SIZE 128
23 #endif
24 
25 #define TWR_RADIO_ID_SIZE 6
26 #define TWR_RADIO_HEAD_SIZE (TWR_RADIO_ID_SIZE + 2)
27 #define TWR_RADIO_MAX_BUFFER_SIZE (TWR_SPIRIT1_MAX_PACKET_SIZE - TWR_RADIO_HEAD_SIZE)
28 #define TWR_RADIO_MAX_TOPIC_LEN (TWR_RADIO_MAX_BUFFER_SIZE - 1 - 4 - 1)
29 #define TWR_RADIO_NULL_BOOL 0xff
30 #define TWR_RADIO_NULL_INT INT32_MIN
31 #define TWR_RADIO_NULL_FLOAT NAN
32 #define TWR_RADIO_NULL_UINT32 UINT32_MAX
33 #define TWR_RADIO_NULL_UINT16 UINT16_MAX
34 
36 
37 typedef enum
38 {
41 
44 
47 
50 
52 
53 typedef enum
54 {
55  TWR_RADIO_EVENT_INIT_FAILURE = 0,
56  TWR_RADIO_EVENT_INIT_DONE = 1,
57  TWR_RADIO_EVENT_ATTACH = 2,
58  TWR_RADIO_EVENT_ATTACH_FAILURE = 3,
59  TWR_RADIO_EVENT_DETACH = 4,
60  TWR_RADIO_EVENT_SCAN_FIND_DEVICE = 5,
61  TWR_RADIO_EVENT_PAIRED = 6,
62  TWR_RADIO_EVENT_UNPAIRED = 7,
63  TWR_RADIO_EVENT_TX_DONE = 8,
64  TWR_RADIO_EVENT_TX_ERROR = 9
65 
66 } twr_radio_event_t;
67 
68 typedef enum
69 {
70  TWR_RADIO_HEADER_PAIRING = 0x00,
71  TWR_RADIO_HEADER_PUB_PUSH_BUTTON = 0x01,
72  TWR_RADIO_HEADER_PUB_TEMPERATURE = 0x02,
73  TWR_RADIO_HEADER_PUB_HUMIDITY = 0x03,
74  TWR_RADIO_HEADER_PUB_LUX_METER = 0x04,
75  TWR_RADIO_HEADER_PUB_BAROMETER = 0x05,
76  TWR_RADIO_HEADER_PUB_CO2 = 0x06,
77  TWR_RADIO_HEADER_PUB_BUFFER = 0x07,
78  TWR_RADIO_HEADER_NODE_ATTACH = 0x08,
79  TWR_RADIO_HEADER_NODE_DETACH = 0x09,
80  TWR_RADIO_HEADER_PUB_BATTERY = 0x0a,
81  TWR_RADIO_HEADER_PUB_INFO = 0x0b, // deprecated
82 
83  TWR_RADIO_HEADER_PUB_ACCELERATION = 0x0d,
84  TWR_RADIO_HEADER_PUB_TOPIC_STRING = 0x0e,
85  TWR_RADIO_HEADER_PUB_TOPIC_UINT32 = 0x0f,
86  TWR_RADIO_HEADER_PUB_TOPIC_BOOL = 0x10,
87  TWR_RADIO_HEADER_PUB_TOPIC_INT = 0x11,
88  TWR_RADIO_HEADER_PUB_TOPIC_FLOAT = 0x12,
89  TWR_RADIO_HEADER_PUB_EVENT_COUNT = 0x13,
90  TWR_RADIO_HEADER_PUB_STATE = 0x14,
91  TWR_RADIO_HEADER_NODE_STATE_SET = 0x15,
92  TWR_RADIO_HEADER_NODE_STATE_GET = 0x16,
93  TWR_RADIO_HEADER_NODE_BUFFER = 0x17,
94  TWR_RADIO_HEADER_NODE_LED_STRIP_COLOR_SET = 0x18,
95  TWR_RADIO_HEADER_NODE_LED_STRIP_BRIGHTNESS_SET = 0x19,
96  TWR_RADIO_HEADER_NODE_LED_STRIP_COMPOUND_SET = 0x1a,
97  TWR_RADIO_HEADER_NODE_LED_STRIP_EFFECT_SET = 0x1b,
98  TWR_RADIO_HEADER_NODE_LED_STRIP_THERMOMETER_SET = 0x1c,
99  TWR_RADIO_HEADER_SUB_DATA = 0x1d,
100  TWR_RADIO_HEADER_PUB_VALUE_INT = 0x1e,
101 
102  TWR_RADIO_HEADER_SUB_REG = 0x20,
103 
104  TWR_RADIO_HEADER_ACK = 0xaa,
105 
106 
107 } twr_radio_header_t;
108 
110 
111 typedef enum
112 {
113  TWR_RADIO_SUB_PT_BOOL = 0,
114  TWR_RADIO_SUB_PT_INT = 1,
115  TWR_RADIO_SUB_PT_FLOAT = 2,
116  TWR_RADIO_SUB_PT_STRING = 3,
117  TWR_RADIO_SUB_PT_NULL = 4,
118 
120 
121 typedef struct twr_radio_sub_t twr_radio_sub_t;
122 
124  const char *topic;
125  twr_radio_sub_pt_t type;
126  void (*callback)(uint64_t *id, const char *, void *, void *);
127  void *param;
128 };
129 
130 typedef struct
131 {
132  uint64_t id;
133  uint16_t message_id;
134  bool message_id_synced;
135  twr_radio_mode_t mode;
136  int rssi;
137 
139 
142 
144 
145 void twr_radio_set_event_handler(void (*event_handler)(twr_radio_event_t, void *), void *event_param);
146 
147 void twr_radio_listen(twr_tick_t timeout);
148 
149 void twr_radio_pairing_request(const char *firmware, const char *version);
150 
151 void twr_radio_pairing_mode_start(void);
152 
153 void twr_radio_pairing_mode_stop(void);
154 
155 bool twr_radio_peer_device_add(uint64_t id);
156 
157 bool twr_radio_peer_device_remove(uint64_t id);
158 
159 bool twr_radio_peer_device_purge_all(void);
160 
161 void twr_radio_get_peer_id(uint64_t *id, int length);
162 
163 void twr_radio_scan_start(void);
164 
165 void twr_radio_scan_stop(void);
166 
167 void twr_radio_automatic_pairing_start(void);
168 
169 void twr_radio_automatic_pairing_stop(void);
170 
171 uint64_t twr_radio_get_my_id(void);
172 
173 uint64_t twr_radio_get_event_id(void);
174 
175 bool twr_radio_is_peer_device(uint64_t id);
176 
177 bool twr_radio_pub_queue_put(const void *buffer, size_t length);
178 
180 
182 
183 void twr_radio_set_subs(twr_radio_sub_t *subs, int length);
184 
185 bool twr_radio_send_sub_data(uint64_t *id, uint8_t order, void *payload, size_t size);
186 
187 void twr_radio_set_rx_timeout_for_sleeping_node(twr_tick_t timeout);
188 
189 twr_radio_peer_t *twr_radio_get_peer_device(uint64_t id);
190 
191 uint8_t *twr_radio_id_to_buffer(uint64_t *id, uint8_t *buffer);
192 uint8_t *twr_radio_bool_to_buffer(bool *value, uint8_t *buffer);
193 uint8_t *twr_radio_int_to_buffer(int *value, uint8_t *buffer);
194 uint8_t *twr_radio_uint16_to_buffer(uint16_t *value, uint8_t *buffer);
195 uint8_t *twr_radio_uint32_to_buffer(uint32_t *value, uint8_t *buffer);
196 uint8_t *twr_radio_float_to_buffer(float *value, uint8_t *buffer);
197 uint8_t *twr_radio_data_to_buffer(void *data, size_t length, uint8_t *buffer);
198 uint8_t *twr_radio_id_from_buffer(uint8_t *buffer, uint64_t *id);
199 uint8_t *twr_radio_bool_from_buffer(uint8_t *buffer, bool *value, bool **pointer);
200 uint8_t *twr_radio_int_from_buffer(uint8_t *buffer, int *value, int **pointer);
201 uint8_t *twr_radio_uint16_from_buffer(uint8_t *buffer, uint16_t *value, uint16_t **pointer);
202 uint8_t *twr_radio_uint32_from_buffer(uint8_t *buffer, uint32_t *value, uint32_t **pointer);
203 uint8_t *twr_radio_float_from_buffer(uint8_t *buffer, float *value, float **pointer);
204 uint8_t *twr_radio_data_from_buffer(uint8_t *buffer, void *data, size_t length);
205 
206 void twr_radio_init_pairing_button();
207 
209 
210 #endif // _TWR_RADIO_H
twr_radio_mode_t
Radio mode.
Definition: twr_radio.h:38
void twr_radio_init(twr_radio_mode_t mode)
Initialize radio.
Definition: twr_radio.c:89
void twr_radio_pub_queue_clear()
Clear the publish message queue.
Definition: twr_radio.c:272
twr_radio_sub_pt_t
Subscribe payload type.
Definition: twr_radio.h:112
@ TWR_RADIO_MODE_NODE_LISTENING
Node listening mode.
Definition: twr_radio.h:46
@ TWR_RADIO_MODE_UNKNOWN
Unknown mode.
Definition: twr_radio.h:40
@ TWR_RADIO_MODE_GATEWAY
Gateway mode.
Definition: twr_radio.h:43
@ TWR_RADIO_MODE_NODE_SLEEPING
Node sleeping mode, suitable for battery.
Definition: twr_radio.h:49
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16