Fix comments and add wizard info.
[bertos.git] / bertos / net / pocketbus.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \version $Id: pocketbus.h 20131 2007-12-13 17:39:55Z batt $
33  *
34  * \author Francesco Sacchi <batt@develer.com>
35  *
36  * \brief Basical functions to use pocketBus protocol.
37  *
38  * $WIZARD_MODULE = {
39  * "name" : "pocketbus",
40  * "depends" : ["rotating_hash", "kfile"],
41  * "configuration" : "bertos/cfg/cfg_pocketbus.h"
42  * }
43  */
44
45 #ifndef NET_POCKETBUS_H
46 #define NET_POCKETBUS_H
47
48 #include "cfg/cfg_pocketbus.h"        /* for CONFIG_POCKETBUS_BUFLEN */
49 #include <cfg/compiler.h>
50
51 #include <algo/rotating_hash.h>
52
53 #include <drv/ser.h>
54
55 #include <kern/kfile.h>
56
57 /**
58  * pocketBus special characters definitions.
59  * \{
60  */
61 #define POCKETBUS_STX 0x02 //ASCII STX
62 #define POCKETBUS_ETX 0x03 //ASCII ETX
63 #define POCKETBUS_ESC 0x1B //ASCII ESC
64 #define POCKETBUS_ACK 0x06 //ASCII ACK
65 #define POCKETBUS_NAK 0x15 //ASCII NAK
66 /*\}*/
67
68 #define POCKETBUS_BROADCAST_ADDR 0xFFFF ///< pocketBus broadcast address
69
70 /**
71  * Type for pocketBus length.
72  */
73 typedef uint16_t pocketbus_len_t;
74
75 /**
76  * Type for pocketBus addresses.
77  */
78 typedef uint16_t pocketbus_addr_t;
79
80 /**
81  * Header of pocketBus messages.
82  */
83 typedef struct PocketBusHdr
84 {
85         #define POCKETBUS_VER 1
86         uint8_t ver;   ///< packet version
87         pocketbus_addr_t addr; ///< slave address
88 } PocketBusHdr;
89
90 /**
91  * pocketBus context structure.
92  */
93 typedef struct PocketBusCtx
94 {
95         struct KFile *fd;   ///< File descriptor
96         bool sync;           ///< Status flag: true if we have received an STX, false otherwise
97         bool escape;         ///< Status flag: true if we are in escape mode, false otherwise
98         rotating_t in_cks;   ///< Checksum computation for received data.
99         rotating_t out_cks;  ///< Checksum computation for transmitted data.
100         pocketbus_len_t len; ///< Received length
101         uint8_t buf[CONFIG_POCKETBUS_BUFLEN]; ///< receiving Buffer
102 } PocketBusCtx;
103
104 /**
105  * Structure holding pocketBus message parameters.
106  */
107 typedef struct PocketMsg
108 {
109         struct PocketBusCtx *ctx; ///< pocketBus message context
110         pocketbus_addr_t addr;    ///< address for received packet
111         pocketbus_len_t len;      ///< payload length
112         const uint8_t *payload;   ///< payload data
113 } PocketMsg;
114
115 /**
116  * This ensure that endianess convertion functions work on
117  * the right data size.
118  * \{
119  */
120 STATIC_ASSERT(sizeof(pocketbus_addr_t) == sizeof(uint16_t));
121 STATIC_ASSERT(sizeof(rotating_t) == sizeof(uint16_t));
122 /*\}*/
123
124 void pocketbus_putchar(struct PocketBusCtx *ctx, uint8_t c);
125 void pocketbus_begin(struct PocketBusCtx *ctx, pocketbus_addr_t addr);
126 void pocketbus_write(struct PocketBusCtx *ctx, const void *_data, size_t len);
127 void pocketbus_end(struct PocketBusCtx *ctx);
128
129 void pocketbus_send(struct PocketBusCtx *ctx, pocketbus_addr_t addr, const void *data, size_t len);
130 bool pocketbus_recv(struct PocketBusCtx *ctx, struct PocketMsg *msg);
131 void pocketbus_init(struct PocketBusCtx *ctx, struct KFile *fd);
132
133 #endif /* NET_POCKETBUS_H */