Fix alignment problem on arm.
authorasterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Mon, 31 Aug 2009 15:13:33 +0000 (15:13 +0000)
committerasterix <asterix@38d2e660-2303-0410-9eaa-f027e97ec537>
Mon, 31 Aug 2009 15:13:33 +0000 (15:13 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2830 38d2e660-2303-0410-9eaa-f027e97ec537

bertos/net/pocketbus.c
bertos/net/pocketbus.h
bertos/net/pocketcmd.c

index a9cb4727c162362e7f594a702c5853e0c350a756..61d4d2b44261432885e33fa0bb81050a5c8d7330 100644 (file)
@@ -208,12 +208,16 @@ bool pocketbus_recv(struct PocketBusCtx *ctx, struct PocketMsg *msg)
 
                                /* Compute checksum */
                                rotating_update(ctx->buf, ctx->len, &ctx->in_cks);
-                               rotating_t recv_cks = be16_to_cpu(*((rotating_t *)(ctx->buf + ctx->len)));
+                               uint8_t cks_h = *(ctx->buf + ctx->len);
+                               uint8_t cks_l = *(ctx->buf + ctx->len + 1);
+
+                               rotating_t recv_cks = (cks_h << 8) | cks_l;
 
                                /* Checksum check */
                                if (recv_cks == ctx->in_cks)
                                {
-                                       PocketBusHdr *hdr = (PocketBusHdr *)(ctx->buf);
+                                       PocketBusHdr *hdr = (PocketBusHdr *)ctx;
+
                                        /* Check packet version */
                                        if (hdr->ver == POCKETBUS_VER)
                                        {
index 6740678ac2ecc1fb428b70f86f3413171fc82779..e67f2634575bc6fe2a68f29e2f0b178367247939 100644 (file)
@@ -88,15 +88,16 @@ typedef struct PocketBusHdr
  */
 typedef struct PocketBusCtx
 {
+       uint8_t buf[CONFIG_POCKETBUS_BUFLEN]; ///< receiving Buffer
        struct KFile *fd;   ///< File descriptor
        bool sync;           ///< Status flag: true if we have received an STX, false otherwise
        bool escape;         ///< Status flag: true if we are in escape mode, false otherwise
        rotating_t in_cks;   ///< Checksum computation for received data.
        rotating_t out_cks;  ///< Checksum computation for transmitted data.
        pocketbus_len_t len; ///< Received length
-       uint8_t buf[CONFIG_POCKETBUS_BUFLEN]; ///< receiving Buffer
 } PocketBusCtx;
 
+STATIC_ASSERT(offsetof(PocketBusCtx, buf) == 0);
 /**
  * Structure holding pocketBus message parameters.
  */
index 9bb65f16eafbfd69b4bd44b9f5d5be2859641fc4..3c92a2faf5883fe2c326334d5c818a29867faf7c 100644 (file)
@@ -65,6 +65,7 @@
 #include <drv/timer.h>
 
 #include <cpu/byteorder.h>
+#include <cpu/detect.h>
 
 #include <string.h>
 
@@ -83,7 +84,24 @@ void pocketcmd_poll(struct PocketCmdCtx *ctx)
                if (msg.addr == ctx->addr ||
                    msg.addr == POCKETBUS_BROADCAST_ADDR)
                {
-                       const PocketCmdHdr *hdr = (const PocketCmdHdr *)msg.payload;
+
+                       #if CPU_AVR
+                               const PocketCmdHdr *hdr = (const PocketCmdHdr *)msg.payload;
+                       #else
+                               #if !CPU_ARM
+                                       #warning Fix alignment problem..
+                                       /*
+                                        * The code below make one memcopy, this the only way to
+                                        * solve alignment problem on ARM. If you are use other
+                                        * architecture you should find other way to optimize
+                                        * this code.
+                                        */
+                               #endif
+                               PocketCmdHdr hd;
+                               memcpy(&hd, msg.payload, sizeof(PocketCmdHdr));
+                               const PocketCmdHdr *hdr =  &hd;
+                       #endif
+
                        pocketcmd_t cmd = be16_to_cpu(hdr->cmd);
 
                        /* We're no longer waiting for a reply (in case we were) */