new spi protocol that supports PPM generation
[protos/xbee-avr.git] / xbee_proto.h
1 /*
2  * Copyright (c) 2011, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef _XBEE_PROTO_H_
29 #define _XBEE_PROTO_H_
30
31 /* protocol headers */
32
33 #define XBEE_DELIMITER 0x7E
34 #define XBEE_MAX_FRAME_LEN 0x40
35
36 struct xbee_hdr {
37         uint8_t delimiter;
38         uint16_t len;
39         uint8_t type;
40         uint8_t id;
41 } __attribute__((packed));
42
43 #define XBEE_TYPE_ATCMD 0x08
44 struct xbee_atcmd_hdr {
45         uint16_t cmd;
46         uint8_t params[];
47 } __attribute__((packed));
48
49 #define XBEE_TYPE_ATCMD_Q 0x09
50 struct xbee_atcmd_q_hdr {
51         uint16_t cmd;
52         uint8_t params[];
53 } __attribute__((packed));
54
55 #define XBEE_TYPE_XMIT 0x10
56 struct xbee_xmit_hdr {
57         uint64_t dstaddr;
58         uint16_t reserved;
59         uint8_t bcast_radius;
60         uint8_t opts;
61         uint8_t data[];
62 } __attribute__((packed));
63
64 #define XBEE_TYPE_EXPL_XMIT 0x11
65 struct xbee_expl_xmit_hdr {
66         uint64_t dstaddr;
67         uint16_t reserved;
68         uint8_t src_endpoint;
69         uint8_t dst_endpoint;
70         uint16_t cluster_id;
71         uint16_t profile_id;
72         uint8_t bcast_radius;
73         uint8_t opts;
74         uint8_t data[];
75 } __attribute__((packed));
76
77 #define XBEE_TYPE_RMT_ATCMD 0x17
78 struct xbee_rmt_atcmd_hdr {
79         uint64_t dstaddr;
80         uint16_t reserved;
81         uint8_t opts;
82         uint16_t cmd;
83         uint8_t params[];
84 } __attribute__((packed));
85
86 #define XBEE_TYPE_ATRESP 0x88
87 struct xbee_atresp_hdr {
88         uint16_t cmd;
89         uint8_t status;
90         uint8_t data[];
91 } __attribute__((packed));
92
93 #define XBEE_TYPE_MODEM_STATUS 0x8A
94 struct xbee_modem_status_hdr {
95         /* empty */
96 } __attribute__((packed));
97
98 #define XBEE_TYPE_XMIT_STATUS 0x8B
99 struct xbee_xmit_status_hdr {
100         uint16_t reserved;
101         uint8_t xmit_retry_cnt;
102         uint8_t delivery_status;
103         uint8_t discovery_status;
104 } __attribute__((packed));
105
106 #define XBEE_TYPE_RECV 0x90
107 struct xbee_recv_hdr {
108         uint64_t srcaddr;
109         uint16_t reserved;
110         uint8_t opts;
111         uint8_t data[];
112 } __attribute__((packed));
113
114 #define XBEE_TYPE_EXPL_RECV 0x91
115 struct xbee_expl_recv_hdr {
116         uint64_t srcaddr;
117         uint16_t reserved;
118         uint8_t src_endpoint;
119         uint8_t dst_endpoint;
120         uint16_t cluster_id;
121         uint16_t profile_id;
122         uint8_t opts;
123         uint8_t data[];
124 } __attribute__((packed));
125
126 #define XBEE_TYPE_NODE_ID 0x95
127 struct xbee_node_id_hdr {
128         uint64_t srcaddr;
129         uint16_t srcnetwork;
130         uint8_t opts;
131         uint16_t dstnetwork;
132         uint64_t dstaddr;
133         uint8_t ni_string[];
134         /* uint16_t parentaddr; after variable field */
135 } __attribute__((packed));
136
137 #define XBEE_TYPE_RMT_ATRESP 0x97
138 struct xbee_rmt_atresp_hdr {
139         uint64_t srcaddr;
140         uint16_t reserved;
141         uint16_t cmd;
142         uint8_t status;
143         uint8_t data[];
144 } __attribute__((packed));
145
146 struct xbee_dev;
147
148 /* return negative on error, 0 if there is not frame, or framelen */
149 int xbee_proto_get_frame(struct xbee_dev *dev, void *buf, unsigned len);
150
151 /* send a frame */
152 int xbee_proto_xmit(struct xbee_dev *dev, uint8_t id, uint8_t type,
153                     void *buf, unsigned len);
154
155 void xbee_proto_rx(struct xbee_dev *dev);
156
157 #endif /* _XBEE_PROTO_H_ */