fix log types in command line
[protos/xbee-avr.git] / rc_proto.h
1 #ifndef RC_PROTO_H
2 #define RC_PROTO_H
3
4 /* generic header */
5 struct rc_proto_hdr {
6         uint8_t type;
7 } __attribute__((packed));
8
9
10 /* send a hello message, no answer is expected from the peer */
11 #define RC_PROTO_HELLO 0x00
12 struct rc_proto_hello {
13         uint8_t type;
14         uint8_t datalen; /* len of data excluding header */
15         uint8_t data[];
16 } __attribute__((packed));
17
18 /* send an echo request, expect an echo reply from the peer */
19 #define RC_PROTO_ECHO_REQ 0x01
20 struct rc_proto_echo_req {
21         uint8_t type;
22         int8_t power;
23         uint8_t datalen; /* len of data excluding header */
24         uint8_t data[];
25 } __attribute__((packed));
26
27 /* reply to an echo request */
28 #define RC_PROTO_ECHO_ANS 0x02
29 struct rc_proto_echo_ans {
30         uint8_t type;
31         uint8_t datalen; /* len of data excluding header */
32         uint8_t data[];
33 } __attribute__((packed));
34
35 /* send a power level probe to the peer: no answer is expected, but the peer
36  * will know that a packet with this power-level is received. It can also ask
37  * the RSSI of this packet. */
38 #define RC_PROTO_POWER_PROBE 0x03
39 struct rc_proto_power_probe {
40         uint8_t type;
41         uint8_t power_level;
42 } __attribute__((packed));
43
44 /* acknowledge a servo command */
45 #define RC_PROTO_ACK 0x04
46 struct rc_proto_ack {
47         uint8_t type;
48         uint8_t seq;
49 } __attribute__((packed));
50
51 /*
52  * If type < 0x3f, it's a servo command. The size of the message is critical
53  * because it's send very often. The "type" field contains the bitfield of
54  * servos present in the body of the message. A sequence number (5bits) and the
55  * power level (3 bits) are also sent. The servos are listed as 10bits fields,
56  * without padding.
57  *
58  * Example: we send servo 0 (=0x123) and servo 3 (=0x234). Power = 2.
59  *   command type (RC_PROTO_SERVO)
60  *   power_level = 0 (3 bits, LSB)  \
61  *   bypass = 0 (1 bit)              > one byte
62  *   seq = 1 (4 bits, MSB)          /
63  *   servo_val[0]=0x123
64  *   servo_val[1]=0x234
65  *   servo_val[2]=0x321
66  *   servo_val[3]=0x123
67  *   servo_val[4]=0x234
68  *   servo_val[5]=0x321
69  *
70  * -> 0x05 0x10 0x48 0xe3 0x4c 0x85 0x23 0x8d 0x32 0x10
71  *    type psb  |ser0  ser1  ser2  ser3  |ser4  ser5
72  */
73 #define RC_PROTO_SERVO 0x05
74 #define RC_PROTO_SERVO_LEN 10 /* len of frame */
75 struct rc_proto_servo {
76         uint8_t type;
77         uint8_t data[];
78 };
79
80 /* send stats */
81 #define RC_PROTO_STATS 0x06
82 struct rc_proto_stats {
83         uint8_t type;
84         uint8_t stats[]; /* format is struct rc_proto_stats_data */
85 } __attribute__((packed));
86
87 /* send a Hello message to a peer */
88 int8_t rc_proto_send_hello(uint64_t addr, void *data, uint8_t data_len,
89         int8_t power);
90
91 int8_t rc_proto_send_echo_req(uint64_t addr, void *data, uint8_t data_len,
92         int8_t power);
93
94 /* reception of a xbee message */
95 int rc_proto_rx(struct xbee_recv_hdr *recvframe, unsigned len);
96
97 /* dmp statistics related to rc_proto */
98 void rc_proto_dump_stats(void);
99
100 /* set the peer xbee address */
101 void rc_proto_set_dstaddr(uint64_t addr);
102
103 /* get the peer xbee address */
104 uint64_t rc_proto_get_dstaddr(void);
105
106 void rc_proto_dump_servos(void);
107
108 /* tx mode */
109 #define RC_PROTO_FLAGS_TX_OFF        0x00
110 #define RC_PROTO_FLAGS_TX_BYPASS     0x01
111 #define RC_PROTO_FLAGS_TX_COPY_SPI   0x02
112 #define RC_PROTO_FLAGS_TX_RESERVED   0x03
113 #define RC_PROTO_FLAGS_TX_MASK       0x03
114
115 /* if set, copy received servo values to SPI */
116 #define RC_PROTO_FLAGS_RX_COPY_SPI   0x04
117
118 /* if set, switch to bypass when no servo is received during some time */
119 #define RC_PROTO_FLAGS_RX_AUTOBYPASS 0x08
120
121 /* if set, send stats periodically to the peer (1 sec) */
122 #define RC_PROTO_FLAGS_TX_STATS      0x10
123
124 /* if set, send power probe periodically to the peer (500 ms) */
125 #define RC_PROTO_FLAGS_TX_POW_PROBE  0x20
126
127 void rc_proto_set_mode(uint8_t flags);
128
129 uint8_t rc_proto_get_mode(void);
130
131 /* initialize rc_proto module */
132 void rc_proto_init(void);
133
134 #endif