X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fnet%2Fkeytag.c;h=dac1a11cb963582899f621c89fd2d1fcbbbc3d11;hb=HEAD;hp=f686a7aaa27d43c0383d66ce13df7d9ccae13c7c;hpb=4c1f21207a513ed836c86143b550092bae866084;p=bertos.git diff --git a/bertos/net/keytag.c b/bertos/net/keytag.c index f686a7aa..dac1a11c 100644 --- a/bertos/net/keytag.c +++ b/bertos/net/keytag.c @@ -31,8 +31,12 @@ * --> * * \author Andrea Grandi + * \author Daniele Basile * - * \brief Tag protocol (protocol). + * \brief KeyTAG parser. + * + * This module parse TAG message that come from comunication channel, + * and convert the tag value into string. * * TAG protocol is decribed in this way: *
@@ -51,8 +55,9 @@
 #include 
 #include 
 
-#include 
+#include 
 
+#include 
 /**
  * Starting communication char (STX).
  */
@@ -69,14 +74,25 @@ 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;
+	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)
+/**
+ * Receive the tag message from channel, and if
+ * the tag is good put the converted string into given buffer.
+ * The fuction return the len of found tag string, otherwise EOF.
+ */
+int keytag_recv(struct TagPacket *pkt, uint8_t *tag, size_t len)
 {
 	int c;
 
@@ -98,11 +114,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 +143,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;
+}
+