Doc fixes.
[bertos.git] / net / pocketcmd.h
1 /**
2  * \file
3  * <!--
4  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
5  * -->
6  *
7  * \version $Id: pocketcmd.h 20030 2007-12-04 16:16:09Z batt $
8  *
9  * \author Francesco Sacchi <batt@develer.com>
10  *
11  * \brief pocketBus protocol command layer interface.
12  */
13
14 #ifndef NET_POCKETCMD_H
15 #define NET_POCKETCMD_H
16
17 #include "pocketbus.h"
18 #include <cfg/compiler.h>
19
20 #define PKTCMD_NULL 0 ///< pocketBus Null command
21
22 #define PKTCMD_REPLY_TIMEOUT 50 ///< Command replay timeout in milliseconds
23
24 typedef uint16_t pocketcmd_t; ///< Type for Command IDs
25
26 /**
27  * Header for transmitted pocketBus Commands.
28  */
29 typedef struct PocketCmdHdr
30 {
31         pocketcmd_t cmd; ///< command ID
32 } PocketCmdHdr;
33
34 /**
35  * This ensure that endianess convertion functions work on
36  * the right data size.
37  * \{
38  */
39 STATIC_ASSERT(sizeof(pocketcmd_t) == sizeof(uint16_t));
40 /*\}*/
41
42 /* fwd declaration */
43 struct PocketCmdCtx;
44
45 /**
46  * pocketBus command message structure.
47  */
48 typedef struct PocketCmdMsg
49 {
50         struct PocketCmdCtx *cmd_ctx; ///< command context
51         pocketcmd_t cmd;              ///< command id
52         pocketbus_len_t len;          ///< optional arg length
53         const uint8_t *buf;           ///< optional arguments
54 } PocketCmdMsg;
55
56 /**
57  * Type for command hooks.
58  */
59 typedef void (*pocketcmd_hook_t)(struct PocketCmdMsg *cmd_msg);
60
61 /**
62  * Type for lookup function hooks.
63  */
64 typedef pocketcmd_hook_t (*pocketcmd_lookup_t)(pocketcmd_t cmd);
65
66 /**
67  * pocketBus context for command layer communications.
68  */
69 typedef struct PocketCmdCtx
70 {
71         struct PocketBusCtx *bus_ctx; ///< pocketBus context
72         pocketbus_addr_t addr;        ///< Our address
73         pocketcmd_lookup_t search;    ///< Lookup function used to search for command callbacks
74         pocketcmd_t waiting;          ///< The command ID we are waiting for or PKTCMD_NULL.
75         ticks_t reply_timer;          ///< For waiting_reply
76 } PocketCmdCtx;
77
78 /**
79  * Set slave address \a addr for pocketBus command layer.
80  * If we are a slave this is *our* address.
81  * If we are the master this is the slave address to send messages to.
82  */
83 INLINE void pocketcmd_setAddr(struct PocketCmdCtx *ctx, pocketbus_addr_t addr)
84 {
85         ctx->addr = addr;
86 }
87
88 void pocketcmd_init(struct PocketCmdCtx *ctx, struct PocketBusCtx *bus_ctx, pocketbus_addr_t addr, pocketcmd_lookup_t search);
89 void pocketcmd_poll(struct PocketCmdCtx *ctx);
90 bool pocketcmd_send(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len, bool has_replay);
91 void pocketcmd_replyNak(struct PocketCmdMsg *msg);
92 void pocketcmd_replyAck(struct PocketCmdMsg *msg);
93
94 /**
95  * Helper function used by master to send a command to slave \a addr.
96  */
97 INLINE bool pocketcmd_masterSend(struct PocketCmdCtx *ctx, pocketbus_addr_t addr, pocketcmd_t cmd, const void *buf, size_t len)
98 {
99         pocketcmd_setAddr(ctx, addr);
100         return pocketcmd_send(ctx, cmd, buf, len, true);
101 }
102
103 /**
104  * Helper function used by slave to reply to a master command.
105  */
106 INLINE bool pocketcmd_slaveReply(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len)
107 {
108         return pocketcmd_send(ctx, cmd, buf, len, false);
109 }
110
111
112 #endif /* NET_POCKETCMD_H */