397cd6ebbda86526b0eea0b7b1c00cda8abdf2b3
[bertos.git] / net / pocketbus.h
1 /**
2  * \file
3  * <!--
4  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
5  * -->
6  *
7  * \version $Id: pocketbus.h 20131 2007-12-13 17:39:55Z batt $
8  *
9  * \author Francesco Sacchi <batt@develer.com>
10  *
11  * \brief pocketBus protocol interface.
12  */
13
14 #ifndef NET_POCKETBUS_H
15 #define NET_POCKETBUS_H
16
17 #include <algos/rotating_hash.h>
18 #include <drv/ser.h>
19 #include <kern/kfile.h>
20 #include <cfg/compiler.h>
21 #include "appconfig.h" //for CONFIG_POCKETBUS_BUFLEN
22
23 /**
24  * pocketBus special characters definitions.
25  * \{
26  */
27 #define POCKETBUS_STX 0x02 //ASCII STX
28 #define POCKETBUS_ETX 0x03 //ASCII ETX
29 #define POCKETBUS_ESC 0x1B //ASCII ESC
30 #define POCKETBUS_ACK 0x06 //ASCII ACK
31 #define POCKETBUS_NAK 0x15 //ASCII NAK
32 /*\}*/
33
34 #define POCKETBUS_BROADCAST_ADDR 0xFFFF ///< pocketBus broadcast address
35
36 /**
37  * Type for pocketBus length.
38  */
39 typedef uint16_t pocketbus_len_t;
40
41 /**
42  * Type for pocketBus addresses.
43  */
44 typedef uint16_t pocketbus_addr_t;
45
46 /**
47  * Header of pocketBus messages.
48  */
49 typedef struct PocketBusHdr
50 {
51         #define POCKETBUS_VER 1 
52         uint8_t ver;   ///< packet version
53         pocketbus_addr_t addr; ///< slave address
54 } PocketBusHdr;
55
56 /**
57  * pocketBus context structure.
58  */
59 typedef struct PocketBusCtx
60 {
61         struct KFile *fd;   ///< File descriptor
62         bool sync;           ///< Status flag: true if we have received an STX, false otherwise
63         bool escape;         ///< Status flag: true if we are in escape mode, false otherwise
64         rotating_t in_cks;   ///< Checksum computation for received data.
65         rotating_t out_cks;  ///< Checksum computation for transmitted data.
66         pocketbus_len_t len; ///< Received length
67         uint8_t buf[CONFIG_POCKETBUS_BUFLEN]; ///< receiving Buffer
68 } PocketBusCtx;
69
70 /**
71  * Structure holding pocketBus message parameters.
72  */
73 typedef struct PocketMsg
74 {
75         struct PocketBusCtx *ctx; ///< pocketBus message context
76         pocketbus_addr_t addr;    ///< address for received packet
77         pocketbus_len_t len;      ///< payload length 
78         const uint8_t *payload;   ///< payload data
79 } PocketMsg;
80
81 /**
82  * This ensure that endianess convertion functions work on
83  * the right data size.
84  * \{
85  */
86 STATIC_ASSERT(sizeof(pocketbus_addr_t) == sizeof(uint16_t));
87 STATIC_ASSERT(sizeof(rotating_t) == sizeof(uint16_t));
88 /*\}*/
89
90 void pocketbus_putchar(struct PocketBusCtx *ctx, uint8_t c);
91 void pocketbus_begin(struct PocketBusCtx *ctx, pocketbus_addr_t addr);
92 void pocketbus_write(struct PocketBusCtx *ctx, const void *_data, size_t len);
93 void pocketbus_end(struct PocketBusCtx *ctx);
94
95 void pocketbus_send(struct PocketBusCtx *ctx, pocketbus_addr_t addr, const void *data, size_t len);
96 bool pocketbus_recv(struct PocketBusCtx *ctx, struct PocketMsg *msg);
97 void pocketbus_init(struct PocketBusCtx *ctx, struct KFile *fd);
98
99 #endif /* NET_POCKETBUS_H */