4 #include <twr_common.h>
5 #include <twr_button.h>
7 #include <twr_spirit1.h>
13 #ifndef TWR_RADIO_MAX_DEVICES
14 #define TWR_RADIO_MAX_DEVICES 4
17 #ifndef TWR_RADIO_PUB_QUEUE_BUFFER_SIZE
18 #define TWR_RADIO_PUB_QUEUE_BUFFER_SIZE 512
21 #ifndef TWR_RADIO_RX_QUEUE_BUFFER_SIZE
22 #define TWR_RADIO_RX_QUEUE_BUFFER_SIZE 128
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
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
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,
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,
102 TWR_RADIO_HEADER_SUB_REG = 0x20,
104 TWR_RADIO_HEADER_ACK = 0xaa,
107 } twr_radio_header_t;
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,
126 void (*callback)(uint64_t *id,
const char *,
void *,
void *);
134 bool message_id_synced;
145 void twr_radio_set_event_handler(
void (*event_handler)(twr_radio_event_t,
void *),
void *event_param);
149 void twr_radio_pairing_request(
const char *firmware,
const char *version);
151 void twr_radio_pairing_mode_start(
void);
153 void twr_radio_pairing_mode_stop(
void);
155 bool twr_radio_peer_device_add(uint64_t
id);
157 bool twr_radio_peer_device_remove(uint64_t
id);
159 bool twr_radio_peer_device_purge_all(
void);
161 void twr_radio_get_peer_id(uint64_t *
id,
int length);
163 void twr_radio_scan_start(
void);
165 void twr_radio_scan_stop(
void);
167 void twr_radio_automatic_pairing_start(
void);
169 void twr_radio_automatic_pairing_stop(
void);
171 uint64_t twr_radio_get_my_id(
void);
173 uint64_t twr_radio_get_event_id(
void);
175 bool twr_radio_is_peer_device(uint64_t
id);
177 bool twr_radio_pub_queue_put(
const void *buffer,
size_t length);
185 bool twr_radio_send_sub_data(uint64_t *
id, uint8_t order,
void *payload,
size_t size);
187 void twr_radio_set_rx_timeout_for_sleeping_node(
twr_tick_t timeout);
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);
206 void twr_radio_init_pairing_button();
twr_radio_mode_t
Radio mode.
void twr_radio_init(twr_radio_mode_t mode)
Initialize radio.
void twr_radio_pub_queue_clear()
Clear the publish message queue.
twr_radio_sub_pt_t
Subscribe payload type.
@ TWR_RADIO_MODE_NODE_LISTENING
Node listening mode.
@ TWR_RADIO_MODE_UNKNOWN
Unknown mode.
@ TWR_RADIO_MODE_GATEWAY
Gateway mode.
@ TWR_RADIO_MODE_NODE_SLEEPING
Node sleeping mode, suitable for battery.
uint64_t twr_tick_t
Timestamp data type.