pocketcmd.h
Go to the documentation of this file.00001
00040 #ifndef NET_POCKETCMD_H
00041 #define NET_POCKETCMD_H
00042
00043 #include "pocketbus.h"
00044 #include <cfg/compiler.h>
00045
00046 #define PKTCMD_NULL 0 ///< pocketBus Null command
00047
00048 typedef uint16_t pocketcmd_t;
00049
00053 typedef struct PocketCmdHdr
00054 {
00055 pocketcmd_t cmd;
00056 } PocketCmdHdr;
00057
00063 STATIC_ASSERT(sizeof(pocketcmd_t) == sizeof(uint16_t));
00064
00065
00066
00067 struct PocketCmdCtx;
00068
00072 typedef struct PocketCmdMsg
00073 {
00074 struct PocketCmdCtx *cmd_ctx;
00075 pocketcmd_t cmd;
00076 pocketbus_len_t len;
00077 const uint8_t *buf;
00078 } PocketCmdMsg;
00079
00083 typedef void (*pocketcmd_hook_t)(struct PocketCmdMsg *cmd_msg);
00084
00088 typedef pocketcmd_hook_t (*pocketcmd_lookup_t)(pocketcmd_t cmd);
00089
00093 typedef struct PocketCmdCtx
00094 {
00095 struct PocketBusCtx *bus_ctx;
00096 pocketbus_addr_t addr;
00097 pocketcmd_lookup_t search;
00098 pocketcmd_t waiting;
00099 ticks_t reply_timer;
00100 } PocketCmdCtx;
00101
00107 INLINE void pocketcmd_setAddr(struct PocketCmdCtx *ctx, pocketbus_addr_t addr)
00108 {
00109 ctx->addr = addr;
00110 }
00111
00112 void pocketcmd_init(struct PocketCmdCtx *ctx, struct PocketBusCtx *bus_ctx, pocketbus_addr_t addr, pocketcmd_lookup_t search);
00113 void pocketcmd_poll(struct PocketCmdCtx *ctx);
00114 bool pocketcmd_send(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len, bool has_replay);
00115 bool pocketcmd_recv(struct PocketCmdCtx *ctx, PocketCmdMsg *recv_msg);
00116 void pocketcmd_replyNak(struct PocketCmdMsg *msg);
00117 void pocketcmd_replyAck(struct PocketCmdMsg *msg);
00118
00122 INLINE bool pocketcmd_masterSend(struct PocketCmdCtx *ctx, pocketbus_addr_t addr, pocketcmd_t cmd, const void *buf, size_t len)
00123 {
00124 pocketcmd_setAddr(ctx, addr);
00125 return pocketcmd_send(ctx, cmd, buf, len, true);
00126 }
00127
00131 INLINE bool pocketcmd_slaveReply(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len)
00132 {
00133 return pocketcmd_send(ctx, cmd, buf, len, false);
00134 }
00135
00139 INLINE bool pocketcmd_checkNak(struct PocketCmdMsg *msg)
00140 {
00141 if (msg->buf[0] == POCKETBUS_NAK)
00142 return true;
00143
00144 return false;
00145 }
00146
00147
00148
00149 #endif