4 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
7 * \version $Id: pocketcmd.h 20030 2007-12-04 16:16:09Z batt $
9 * \author Francesco Sacchi <batt@develer.com>
11 * \brief pocketBus protocol command layer interface.
14 #ifndef NET_POCKETCMD_H
15 #define NET_POCKETCMD_H
17 #include "pocketbus.h"
18 #include <cfg/compiler.h>
20 #define PKTCMD_NULL 0 ///< pocketBus Null command
22 #define PKTCMD_REPLY_TIMEOUT 50 ///< Command replay timeout in milliseconds
24 typedef uint16_t pocketcmd_t; ///< Type for Command IDs
27 * Header for transmitted pocketBus Commands.
29 typedef struct PocketCmdHdr
31 pocketcmd_t cmd; ///< command ID
35 * This ensure that endianess convertion functions work on
36 * the right data size.
39 STATIC_ASSERT(sizeof(pocketcmd_t) == sizeof(uint16_t));
46 * pocketBus command message structure.
48 typedef struct PocketCmdMsg
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
57 * Type for command hooks.
59 typedef void (*pocketcmd_hook_t)(struct PocketCmdMsg *cmd_msg);
62 * Type for lookup function hooks.
64 typedef pocketcmd_hook_t (*pocketcmd_lookup_t)(pocketcmd_t cmd);
67 * pocketBus context for command layer communications.
69 typedef struct PocketCmdCtx
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
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.
83 INLINE void pocketcmd_setAddr(struct PocketCmdCtx *ctx, pocketbus_addr_t addr)
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);
95 * Helper function used by master to send a command to slave \a addr.
97 INLINE bool pocketcmd_masterSend(struct PocketCmdCtx *ctx, pocketbus_addr_t addr, pocketcmd_t cmd, const void *buf, size_t len)
99 pocketcmd_setAddr(ctx, addr);
100 return pocketcmd_send(ctx, cmd, buf, len, true);
104 * Helper function used by slave to reply to a master command.
106 INLINE bool pocketcmd_slaveReply(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len)
108 return pocketcmd_send(ctx, cmd, buf, len, false);
112 #endif /* NET_POCKETCMD_H */