7cbc98bfd1f2a44a0eb70d09d705bbe1e0ebf96e
[bertos.git] / bertos / net / pocketcmd.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  * \brief PocketBus command abstraction layer.
33  *
34  * \version $Id: pocketcmd.h 20030 2007-12-04 16:16:09Z batt $
35  *
36  * \author Francesco Sacchi <batt@develer.com>
37  *
38  * $WIZARD_MODULE = {
39  * "name" : "pocketcmd",
40  * "depends" : ["timer", "pocketbus"],
41  * "configuration" : ""
42  * }
43  */
44
45 #ifndef NET_POCKETCMD_H
46 #define NET_POCKETCMD_H
47
48 #include "pocketbus.h"
49 #include <cfg/compiler.h>
50
51 #define PKTCMD_NULL 0 ///< pocketBus Null command
52
53 #define PKTCMD_REPLY_TIMEOUT 50 ///< Command replay timeout in milliseconds
54
55 typedef uint16_t pocketcmd_t; ///< Type for Command IDs
56
57 /**
58  * Header for transmitted pocketBus Commands.
59  */
60 typedef struct PocketCmdHdr
61 {
62         pocketcmd_t cmd; ///< command ID
63 } PocketCmdHdr;
64
65 /**
66  * This ensure that endianess convertion functions work on
67  * the right data size.
68  * \{
69  */
70 STATIC_ASSERT(sizeof(pocketcmd_t) == sizeof(uint16_t));
71 /*\}*/
72
73 /* fwd declaration */
74 struct PocketCmdCtx;
75
76 /**
77  * pocketBus command message structure.
78  */
79 typedef struct PocketCmdMsg
80 {
81         struct PocketCmdCtx *cmd_ctx; ///< command context
82         pocketcmd_t cmd;              ///< command id
83         pocketbus_len_t len;          ///< optional arg length
84         const uint8_t *buf;           ///< optional arguments
85 } PocketCmdMsg;
86
87 /**
88  * Type for command hooks.
89  */
90 typedef void (*pocketcmd_hook_t)(struct PocketCmdMsg *cmd_msg);
91
92 /**
93  * Type for lookup function hooks.
94  */
95 typedef pocketcmd_hook_t (*pocketcmd_lookup_t)(pocketcmd_t cmd);
96
97 /**
98  * pocketBus context for command layer communications.
99  */
100 typedef struct PocketCmdCtx
101 {
102         struct PocketBusCtx *bus_ctx; ///< pocketBus context
103         pocketbus_addr_t addr;        ///< Our address
104         pocketcmd_lookup_t search;    ///< Lookup function used to search for command callbacks
105         pocketcmd_t waiting;          ///< The command ID we are waiting for or PKTCMD_NULL.
106         ticks_t reply_timer;          ///< For waiting_reply
107 } PocketCmdCtx;
108
109 /**
110  * Set slave address \a addr for pocketBus command layer.
111  * If we are a slave this is *our* address.
112  * If we are the master this is the slave address to send messages to.
113  */
114 INLINE void pocketcmd_setAddr(struct PocketCmdCtx *ctx, pocketbus_addr_t addr)
115 {
116         ctx->addr = addr;
117 }
118
119 void pocketcmd_init(struct PocketCmdCtx *ctx, struct PocketBusCtx *bus_ctx, pocketbus_addr_t addr, pocketcmd_lookup_t search);
120 void pocketcmd_poll(struct PocketCmdCtx *ctx);
121 bool pocketcmd_send(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len, bool has_replay);
122 void pocketcmd_replyNak(struct PocketCmdMsg *msg);
123 void pocketcmd_replyAck(struct PocketCmdMsg *msg);
124
125 /**
126  * Helper function used by master to send a command to slave \a addr.
127  */
128 INLINE bool pocketcmd_masterSend(struct PocketCmdCtx *ctx, pocketbus_addr_t addr, pocketcmd_t cmd, const void *buf, size_t len)
129 {
130         pocketcmd_setAddr(ctx, addr);
131         return pocketcmd_send(ctx, cmd, buf, len, true);
132 }
133
134 /**
135  * Helper function used by slave to reply to a master command.
136  */
137 INLINE bool pocketcmd_slaveReply(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len)
138 {
139         return pocketcmd_send(ctx, cmd, buf, len, false);
140 }
141
142
143 #endif /* NET_POCKETCMD_H */