Update preset.
[bertos.git] / bertos / net / pocketcmd.c
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 protocol Command layer implementation.
33  *
34  * This module implements command layer over pocketBus
35  * protocol.
36  * Payload packets received by pocketBus are first checked for
37  * address matching.
38  * If a packet is addressed to us we look for a suitable
39  * callback function to call.
40  *
41  * The received payload format is as follows:
42  * <pre>
43  * +----------------------------------------+
44  * |  CMD |            DATA                 |
45  * +----------------------------------------+
46  * |      |                                 |
47  * +  2B  +           0..N Byte             +
48  * </pre>
49  *
50  * The CMD ID used is the same supplied by the master when
51  * the command was sent.
52  *
53  * \author Francesco Sacchi <batt@develer.com>
54  */
55
56 #include "pocketcmd.h"
57 #include "pocketbus.h"
58
59 #include "cfg/cfg_pocketbus.h"
60
61 // Define logging setting (for cfg/log.h module).
62 #define LOG_LEVEL         POCKETBUS_LOG_LEVEL
63 #define LOG_VERBOSITY     POCKETBUS_LOG_FORMAT
64 #include <cfg/log.h>
65 #include <cfg/debug.h>
66 #include <cfg/macros.h>
67 #include <cfg/module.h>
68
69 #include <drv/timer.h>
70
71 #include <cpu/byteorder.h>
72 #include <cpu/detect.h>
73
74 #include <string.h>
75
76 /**
77  * pocketBus Command poll function.
78  * Call it to read and process pocketBus commands.
79  */
80 void pocketcmd_poll(struct PocketCmdCtx *ctx)
81 {
82         PocketCmdMsg msg;
83         while (pocketcmd_recv(ctx, &msg))
84         {
85                 /* Check for command callback */
86                 pocketcmd_hook_t callback = ctx->search(msg.cmd);
87
88                 /* Call it if exists */
89                 if (callback)
90                         callback(&msg);
91         }
92 }
93
94
95
96 /**
97  * pocketBus Command recv function.
98  * Call it to read and process pocketBus commands.
99  */
100 bool pocketcmd_recv(struct PocketCmdCtx *ctx, PocketCmdMsg *recv_msg)
101 {
102         PocketMsg msg;
103
104         /* Try to read a packet from pocketBus */
105         while (pocketbus_recv(ctx->bus_ctx, &msg))
106         {
107                 /* Check address */
108                 if (msg.addr == ctx->addr ||
109                     msg.addr == POCKETBUS_BROADCAST_ADDR)
110                 {
111
112                         #if CPU_AVR
113                                 const PocketCmdHdr *hdr = (const PocketCmdHdr *)msg.payload;
114                         #else
115                                 #if !CPU_ARM
116                                         #warning Fix alignment problem..
117                                         /*
118                                          * The code below make one memcopy, this the only way to
119                                          * solve alignment problem on ARM. If you are use other
120                                          * architecture you should find other way to optimize
121                                          * this code.
122                                          */
123                                 #endif
124                                 PocketCmdHdr hd;
125                                 memcpy(&hd, msg.payload, sizeof(PocketCmdHdr));
126                                 const PocketCmdHdr *hdr =  &hd;
127                         #endif
128
129                         pocketcmd_t cmd = be16_to_cpu(hdr->cmd);
130
131                         /* We're no longer waiting for a reply (in case we were) */
132                         if (cmd == ctx->waiting)
133                                 ctx->waiting = PKTCMD_NULL;
134
135                         recv_msg->cmd_ctx = ctx;
136                         recv_msg->cmd = cmd;
137                         recv_msg->len = msg.len - sizeof(PocketCmdHdr);
138                         recv_msg->buf = msg.payload + sizeof(PocketCmdHdr);
139
140                         return true;
141                 }
142         }
143
144         return false;
145 }
146
147
148 /**
149  * Send command \a cmd to/from slave adding \a len arguments in \a buf.
150  * Address used is contained in \a ctx->addr .
151  * If we are master and the message has a reply, you must set \a wait_reply to true.
152  * \return true if all is ok, false if we are already waiting a replay from another slave.
153  */
154 bool pocketcmd_send(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len, bool wait_reply)
155 {
156         /* Check if we are waiting a reply from someone */
157         if (ctx->waiting != PKTCMD_NULL)
158         {
159                 /* Check is reply timeout is elapsed */
160                 if (timer_clock() - ctx->reply_timer < ms_to_ticks(CONFIG_POCKETBUS_CMD_REPLY_TIMEOUT))
161                 {
162                         LOG_ERR("Pkt discard! waiting cmd[%04X]\n", ctx->waiting);
163                         return false;
164                 }
165                 else
166                 {
167                         LOG_INFO("Timeout waiting cmd[%04X]\n", ctx->waiting);
168                         ctx->waiting = PKTCMD_NULL;
169                 }
170         }
171
172         /* Endianess! */
173         cmd = cpu_to_be16(cmd);
174
175         /* Send packet */
176         pocketbus_begin(ctx->bus_ctx, ctx->addr);
177         pocketbus_write(ctx->bus_ctx, &cmd, sizeof(cmd));
178         pocketbus_write(ctx->bus_ctx, buf, len);
179         pocketbus_end(ctx->bus_ctx);
180
181         if (wait_reply)
182         {
183                 ctx->waiting = be16_to_cpu(cmd);
184                 ctx->reply_timer = timer_clock();
185         }
186
187         return true;
188 }
189
190 /**
191  * Init pocketBus command layer.
192  * \a ctx is pocketBus command layer context.
193  * \a bus_ctx is pocketBus context.
194  * \a addr is slave address (see pocketcmd_setAddr for details.)
195  * \a search is the lookup function used to search command ID callbacks.
196  */
197 void pocketcmd_init(struct PocketCmdCtx *ctx, struct PocketBusCtx *bus_ctx, pocketbus_addr_t addr, pocketcmd_lookup_t search)
198 {
199         ASSERT(ctx);
200         ASSERT(bus_ctx);
201         ASSERT(search);
202         MOD_CHECK(timer);
203
204         memset(ctx, 0, sizeof(*ctx));
205         ctx->bus_ctx = bus_ctx;
206         ctx->search = search;
207         pocketcmd_setAddr(ctx, addr);
208 }
209
210 /**
211  * Helper function used to reply to master with an ACK.
212  */
213 void pocketcmd_replyAck(struct PocketCmdMsg *msg)
214 {
215         uint8_t ack[] = { POCKETBUS_ACK };
216
217         pocketcmd_slaveReply(msg->cmd_ctx, msg->cmd, ack, sizeof(ack));
218 }
219
220 /**
221  * Helper function used to reply to master with a NAK.
222  */
223 void pocketcmd_replyNak(struct PocketCmdMsg *msg)
224 {
225         uint8_t nak[] = { POCKETBUS_NAK };
226
227         pocketcmd_slaveReply(msg->cmd_ctx, msg->cmd, nak, sizeof(nak));
228 }
229