APUE Learning Example Source Code
Guo Wenxue
2019-03-28 d421b7660c948c2e22798a79aa480343027243c0
ch8_tlv/tlv_sample.c
@@ -29,6 +29,7 @@
#define PACK_HEADER        0xFD
/* Tag definition */
enum
{
   TAG_LOGON=1,
@@ -53,6 +54,8 @@
   bytes = packtlv_led(buf, sizeof(buf), ON);
          /* print every byte in the buffer as HEX and corresponding charactor,
     which is not printable charactor will be instead as '?' */
   dump_buf(buf, bytes);
   bytes = packtlv_logon(buf, sizeof(buf), "iot@yun");
@@ -68,7 +71,7 @@
   unsigned short      crc16 = 0;
   int                 pack_len = 0;
   int                 data_len = 0;
   int                 ofset = 0;
   int                 ofset = 0; /* index position for the buf */
   if(!buf || !pwd || size<TLV_MIN_SIZE )
   {
@@ -84,7 +87,8 @@
   buf[ofset] = TAG_LOGON;
   ofset += 1;
   /* $pwd too long maybe result buffer overflow, if it's too long then truncate it */
   /* $pwd too long maybe result buffer overflow, so we need check the buffer
    * is large enuf or not. If buf size is not enuf we will truncate $pwd string */
   if( strlen(pwd) <= size-TLV_FIXED_SIZE )
      data_len = strlen(pwd);
   else
@@ -99,8 +103,10 @@
   memcpy(&buf[ofset], pwd, data_len);
   ofset += data_len;
   /* Calc CRC16 value from Packet Head(buf[0])~ Packet Value(buf[ofset]) */
   crc16 = crc_itu_t(IoT_MAGIC_CRC, buf, ofset);
   /* Calc CRC16 checksum value from Packet Head(buf[0])~ Value(buf[ofset]) */
   crc16 = crc_itu_t(MAGIC_CRC, buf, ofset);
   /* Append the 2 Bytes CRC16 checksum value into the last two bytes in packet buffer */
   ushort_to_bytes(&buf[ofset], crc16);
   ofset += 2;
@@ -132,10 +138,10 @@
   /* Value */
   buf[3] = (OFF==cmd) ? 0x00 : 0x01;
   /* Calc CRC16 value from Packet Head(buf[0])~ Packet Value(buf[3]) */
   crc16 = crc_itu_t(IoT_MAGIC_CRC, buf, 4);
   /* Calc CRC16 checksum value from Packet Head(buf[0])~ Packet Value(buf[3]) */
   crc16 = crc_itu_t(MAGIC_CRC, buf, 4);
   /* Append the 2 Bytes CRC16 value into the packet buffer */
   /* Append the 2 Bytes CRC16 checksum value into the last two bytes in packet buffer */
   ushort_to_bytes(&buf[4], crc16);
   return pack_len;
@@ -167,6 +173,7 @@
    "????????????????"
    "????????????????";
/* print every byte in the buffer as HEX and corresponding charactor, which is not printable charactor will be instead as '?' */
void dump_buf(char *data, int len)
{
    int rc;