4 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
7 * \version $Id: pocketbus.h 20131 2007-12-13 17:39:55Z batt $
9 * \author Francesco Sacchi <batt@develer.com>
11 * \brief pocketBus protocol interface.
14 #ifndef NET_POCKETBUS_H
15 #define NET_POCKETBUS_H
17 #include <algo/rotating_hash.h>
19 #include <kern/kfile.h>
20 #include <cfg/compiler.h>
21 #include "appconfig.h" //for CONFIG_POCKETBUS_BUFLEN
24 * pocketBus special characters definitions.
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
34 #define POCKETBUS_BROADCAST_ADDR 0xFFFF ///< pocketBus broadcast address
37 * Type for pocketBus length.
39 typedef uint16_t pocketbus_len_t;
42 * Type for pocketBus addresses.
44 typedef uint16_t pocketbus_addr_t;
47 * Header of pocketBus messages.
49 typedef struct PocketBusHdr
51 #define POCKETBUS_VER 1
52 uint8_t ver; ///< packet version
53 pocketbus_addr_t addr; ///< slave address
57 * pocketBus context structure.
59 typedef struct PocketBusCtx
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
71 * Structure holding pocketBus message parameters.
73 typedef struct PocketMsg
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
82 * This ensure that endianess convertion functions work on
83 * the right data size.
86 STATIC_ASSERT(sizeof(pocketbus_addr_t) == sizeof(uint16_t));
87 STATIC_ASSERT(sizeof(rotating_t) == sizeof(uint16_t));
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);
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);
99 #endif /* NET_POCKETBUS_H */