4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
32 * \brief PocketBus command abstraction layer.
34 * \author Francesco Sacchi <batt@develer.com>
36 * $WIZ$ module_name = "pocketcmd"
37 * $WIZ$ module_depends = "timer", "pocketbus"
40 #ifndef NET_POCKETCMD_H
41 #define NET_POCKETCMD_H
43 #include "pocketbus.h"
44 #include <cfg/compiler.h>
46 #define PKTCMD_NULL 0 ///< pocketBus Null command
48 typedef uint16_t pocketcmd_t; ///< Type for Command IDs
51 * Header for transmitted pocketBus Commands.
53 typedef struct PocketCmdHdr
55 pocketcmd_t cmd; ///< command ID
59 * This ensure that endianess convertion functions work on
60 * the right data size.
63 STATIC_ASSERT(sizeof(pocketcmd_t) == sizeof(uint16_t));
70 * pocketBus command message structure.
72 typedef struct PocketCmdMsg
74 struct PocketCmdCtx *cmd_ctx; ///< command context
75 pocketcmd_t cmd; ///< command id
76 pocketbus_len_t len; ///< optional arg length
77 const uint8_t *buf; ///< optional arguments
81 * Type for command hooks.
83 typedef void (*pocketcmd_hook_t)(struct PocketCmdMsg *cmd_msg);
86 * Type for lookup function hooks.
88 typedef pocketcmd_hook_t (*pocketcmd_lookup_t)(pocketcmd_t cmd);
91 * pocketBus context for command layer communications.
93 typedef struct PocketCmdCtx
95 struct PocketBusCtx *bus_ctx; ///< pocketBus context
96 pocketbus_addr_t addr; ///< Our address
97 pocketcmd_lookup_t search; ///< Lookup function used to search for command callbacks
98 pocketcmd_t waiting; ///< The command ID we are waiting for or PKTCMD_NULL.
99 ticks_t reply_timer; ///< For waiting_reply
103 * Set slave address \a addr for pocketBus command layer.
104 * If we are a slave this is *our* address.
105 * If we are the master this is the slave address to send messages to.
107 INLINE void pocketcmd_setAddr(struct PocketCmdCtx *ctx, pocketbus_addr_t addr)
112 void pocketcmd_init(struct PocketCmdCtx *ctx, struct PocketBusCtx *bus_ctx, pocketbus_addr_t addr, pocketcmd_lookup_t search);
113 void pocketcmd_poll(struct PocketCmdCtx *ctx);
114 bool pocketcmd_send(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len, bool has_replay);
115 bool pocketcmd_recv(struct PocketCmdCtx *ctx, PocketCmdMsg *recv_msg);
116 void pocketcmd_replyNak(struct PocketCmdMsg *msg);
117 void pocketcmd_replyAck(struct PocketCmdMsg *msg);
120 * Helper function used by master to send a command to slave \a addr.
122 INLINE bool pocketcmd_masterSend(struct PocketCmdCtx *ctx, pocketbus_addr_t addr, pocketcmd_t cmd, const void *buf, size_t len)
124 pocketcmd_setAddr(ctx, addr);
125 return pocketcmd_send(ctx, cmd, buf, len, true);
129 * Helper function used by slave to reply to a master command.
131 INLINE bool pocketcmd_slaveReply(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len)
133 return pocketcmd_send(ctx, cmd, buf, len, false);
137 * Return true if message contain NAK.
139 INLINE bool pocketcmd_checkNak(struct PocketCmdMsg *msg)
141 if (msg->buf[0] == POCKETBUS_NAK)
149 #endif /* NET_POCKETCMD_H */