From 33a94063da7261ecced063b6e8a80c2c77567cd3 Mon Sep 17 00:00:00 2001 From: asterix Date: Fri, 7 May 2010 13:31:47 +0000 Subject: [PATCH] Clean up. Refactor module implementing keytag_recv. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@3625 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cfg/cfg_keytag.h | 7 +------ bertos/net/keytag.c | 42 +++++++++++++++++++++++++++++++---------- bertos/net/keytag.h | 4 +++- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/bertos/cfg/cfg_keytag.h b/bertos/cfg/cfg_keytag.h index 7c72875f..8bedadf6 100644 --- a/bertos/cfg/cfg_keytag.h +++ b/bertos/cfg/cfg_keytag.h @@ -55,12 +55,7 @@ * Max buffer lenght * $WIZ$ type = "int" */ -#define CONFIG_TAG_MAX_LEN 14 - -/** - * Label to put before the tag code. - */ -#define CONFIG_TAG_LABEL "tag " +#define CONFIG_TAG_MAX_LEN 14U #endif /* CFG_KEYTAG_H */ diff --git a/bertos/net/keytag.c b/bertos/net/keytag.c index f686a7aa..2376305a 100644 --- a/bertos/net/keytag.c +++ b/bertos/net/keytag.c @@ -53,6 +53,7 @@ #include +#include /** * Starting communication char (STX). */ @@ -69,14 +70,22 @@ static void keytag_clearPkt(struct TagPacket *pkt) pkt->len = 0; } -void keytag_init(struct TagPacket *pkt, struct KFile *comm, struct KFile *tag) +/** + * DEPRECATED FUCNTIONS + * To read the tag string from device you shoul use the keytag_recv + * fuction, that return the string if we had received it. + */ +void keytag_poll(struct TagPacket *pkt) { - keytag_clearPkt(pkt); - pkt->host = comm; - pkt->tag = tag; + #warning __FILTER_NEXT_WARNING__ + #warning keytag_poll function is depreca use keytag_recv instead + uint8_t buf[CONFIG_TAG_MAX_LEN]; + int len; + if ((len = keytag_recv(pkt, buf, sizeof(buf))) != EOF) + kfile_write(pkt->host, buf, len); } -void keytag_poll(struct TagPacket *pkt) +int keytag_recv(struct TagPacket *pkt, uint8_t *tag, size_t len) { int c; @@ -98,11 +107,12 @@ void keytag_poll(struct TagPacket *pkt) if (c == TAG_ETX) { /* Terminate the tag string */ - uint16_t len = MIN((uint16_t)CONFIG_TAG_MAX_LEN, pkt->len); - pkt->buf[len] = '\0'; - /* Write read TAG on communication serial */ - kfile_printf(pkt->host, "%s%s", CONFIG_TAG_LABEL, pkt->buf); + size_t tag_len = MIN(len, pkt->len); + + /* Save read tag */ + memcpy(tag, pkt->buf, tag_len); pkt->sync = false; + return tag_len; } else { @@ -126,8 +136,20 @@ void keytag_poll(struct TagPacket *pkt) } if (kfile_error(pkt->tag) != 0) { - LOG_ERR("Error %08x\n", kfile_error(pkt->tag)); + LOG_ERR("Error %04x\n", kfile_error(pkt->tag)); kfile_clearerr(pkt->tag); } + return EOF; } + +/** + * Init the keytag module. + */ +void keytag_init(struct TagPacket *pkt, struct KFile *comm, struct KFile *tag) +{ + keytag_clearPkt(pkt); + pkt->tag = tag; + pkt->host = comm; +} + diff --git a/bertos/net/keytag.h b/bertos/net/keytag.h index 375b490e..f086bc34 100644 --- a/bertos/net/keytag.h +++ b/bertos/net/keytag.h @@ -56,11 +56,13 @@ typedef struct TagPacket KFile *tag; ///< Tag communication channel KFile *host; ///< Host communication channel bool sync; ///< Status flag: true if we find an STX - uint16_t len; ///< Packet lenght + size_t len; ///< Packet lenght uint8_t buf[CONFIG_TAG_MAX_LEN]; ///< Reception buffer } TagPacket; void keytag_init(struct TagPacket *pkt, struct KFile *comm, struct KFile *tag); +int keytag_recv(struct TagPacket *pkt, uint8_t *tag, size_t len); + void keytag_poll(struct TagPacket *pkt); #endif /* NET_TAG_H */ -- 2.25.1