From 56df77502283f05666f8e80e7b246c3a500e1485 Mon Sep 17 00:00:00 2001 From: asterix Date: Mon, 31 Aug 2009 15:13:33 +0000 Subject: [PATCH] Fix alignment problem on arm. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@2830 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/net/pocketbus.c | 8 ++++++-- bertos/net/pocketbus.h | 3 ++- bertos/net/pocketcmd.c | 20 +++++++++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/bertos/net/pocketbus.c b/bertos/net/pocketbus.c index a9cb4727..61d4d2b4 100644 --- a/bertos/net/pocketbus.c +++ b/bertos/net/pocketbus.c @@ -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) { diff --git a/bertos/net/pocketbus.h b/bertos/net/pocketbus.h index 6740678a..e67f2634 100644 --- a/bertos/net/pocketbus.h +++ b/bertos/net/pocketbus.h @@ -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. */ diff --git a/bertos/net/pocketcmd.c b/bertos/net/pocketcmd.c index 9bb65f16..3c92a2fa 100644 --- a/bertos/net/pocketcmd.c +++ b/bertos/net/pocketcmd.c @@ -65,6 +65,7 @@ #include #include +#include #include @@ -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) */ -- 2.25.1