4 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
7 * \version $Id: pocketcmd.c 16587 2007-10-02 14:31:02Z batt $
9 * \author Francesco Sacchi <batt@develer.com>
11 * \brief pocketBus protocol Command layer implementation.
13 * This module implements command layer over pocketBus
15 * Payload packets received by pocketBus are first checked for
17 * If a packet is addressed to us we look for a suitable
18 * callback function to call.
20 * The received payload format is as follows:
22 * +----------------------------------------+
24 * +----------------------------------------+
29 * The CMD ID used is the same supplied by the master when
30 * the command was sent.
33 #include "pocketcmd.h"
34 #include "pocketbus.h"
36 #include <cfg/macros.h>
37 #include <cfg/debug.h>
38 #include <cfg/module.h>
40 #include <drv/timer.h>
42 #include <mware/byteorder.h>
47 * pocketBus Command poll function.
48 * Call it to read and process pocketBus commands.
50 void pocketcmd_poll(struct PocketCmdCtx *ctx)
54 /* Try to read a packet from pocketBus */
55 while (pocketbus_recv(ctx->bus_ctx, &msg))
58 if (msg.addr == ctx->addr ||
59 msg.addr == POCKETBUS_BROADCAST_ADDR)
61 const PocketCmdHdr *hdr = (const PocketCmdHdr *)msg.payload;
62 pocketcmd_t cmd = be16_to_cpu(hdr->cmd);
64 /* We're no longer waiting for a reply (in case we were) */
65 if (cmd == ctx->waiting)
66 ctx->waiting = PKTCMD_NULL;
68 /* Check for command callback */
69 pocketcmd_hook_t callback = ctx->search(cmd);
71 /* Call it if exists */
76 cmd_msg.cmd_ctx = ctx;
78 cmd_msg.len = msg.len - sizeof(PocketCmdHdr);
79 cmd_msg.buf = msg.payload + sizeof(PocketCmdHdr);
88 * Send command \a cmd to/from slave adding \a len arguments in \a buf.
89 * Address used is contained in \a ctx->addr .
90 * If we are master and the message has a reply, you must set \a wait_reply to true.
91 * \return true if all is ok, false if we are already waiting a replay from another slave.
93 bool pocketcmd_send(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len, bool wait_reply)
95 /* Check if we are waiting a reply from someone */
96 if (ctx->waiting != PKTCMD_NULL)
98 /* Check is reply timeout is elapsed */
99 if (timer_clock() - ctx->reply_timer < ms_to_ticks(PKTCMD_REPLY_TIMEOUT))
101 TRACEMSG("Pkt discard! waiting cmd[%04X]\n", ctx->waiting);
106 TRACEMSG("Timeout waiting cmd[%04X]\n", ctx->waiting);
107 ctx->waiting = PKTCMD_NULL;
112 cmd = cpu_to_be16(cmd);
115 pocketbus_begin(ctx->bus_ctx, ctx->addr);
116 pocketbus_write(ctx->bus_ctx, &cmd, sizeof(cmd));
117 pocketbus_write(ctx->bus_ctx, buf, len);
118 pocketbus_end(ctx->bus_ctx);
123 ctx->reply_timer = timer_clock();
129 * Init pocketBus command layer.
130 * \a ctx is pocketBus command layer context.
131 * \a bus_ctx is pocketBus context.
132 * \a addr is slave address (see pocketcmd_setAddr for details.)
133 * \a search is the lookup function used to search command ID callbacks.
135 void pocketcmd_init(struct PocketCmdCtx *ctx, struct PocketBusCtx *bus_ctx, pocketbus_addr_t addr, pocketcmd_lookup_t search)
142 memset(ctx, 0, sizeof(*ctx));
143 ctx->bus_ctx = bus_ctx;
144 ctx->search = search;
145 pocketcmd_setAddr(ctx, addr);
149 * Helper function used to reply to master with an ACK.
151 void pocketcmd_replyAck(struct PocketCmdMsg *msg)
153 uint8_t ack[] = { POCKETBUS_ACK };
155 pocketcmd_slaveReply(msg->cmd_ctx, msg->cmd, ack, sizeof(ack));
159 * Helper function used to reply to master with a NAK.
161 void pocketcmd_replyNak(struct PocketCmdMsg *msg)
163 uint8_t nak[] = { POCKETBUS_NAK };
165 pocketcmd_slaveReply(msg->cmd_ctx, msg->cmd, nak, sizeof(nak));