APUE Learning Example Source Code
Guo Wenxue
2019-03-28 d421b7660c948c2e22798a79aa480343027243c0
Add tlv makefile and comment for source code
2 files modified
1 files added
60 ■■■■ changed files
ch8_tlv/crc-itu-t.h 28 ●●●●● patch | view | raw | blame | history
ch8_tlv/makefile 11 ●●●●● patch | view | raw | blame | history
ch8_tlv/tlv_sample.c 21 ●●●●● patch | view | raw | blame | history
ch8_tlv/crc-itu-t.h
@@ -13,14 +13,38 @@
#ifndef CRC_ITU_T_H
#define CRC_ITU_T_H
#define IoT_MAGIC_CRC           0x1E50
#define MAGIC_CRC           0x1E50
extern const unsigned short  crc_itu_t_table[256];
/**
 * crc_itu_t - Compute the CRC-ITU-T for the data buffer
 *
 * @crc:     previous CRC value, we use $IoT_MAGIC_CRC
 * @buffer:  data pointer
 * @len:     number of bytes in the buffer
 *
 * Returns the updated CRC value
 */
extern unsigned short crc_itu_t(unsigned short magic_crc, const unsigned char *buffer, unsigned int len);
/*
 * ushort_to_bytes - Convert  unsigned short CRC16 value to two bytes
 * @bytes:    two bytes CRC16 value saving buffer, buffer size must more than 2 bytes
 * @crc:    crc_itu_t() compute returnd value( unsigned short CRC16 checksum)
 */
extern int ushort_to_bytes(unsigned char *bytes, unsigned short val);
/*
 * bytes_to_ushort - Convert two bytes mode CRC value into unsigned short CRC16 value
 * @bytes:    bytes mode CRC value first byte position
 * @len:      bytes mode CRC value length
 */
extern unsigned short bytes_to_ushort(unsigned char *bytes, int len);
extern unsigned short crc_itu_t(unsigned short crc, const unsigned char *buffer, unsigned int len);
static inline unsigned short crc_itu_t_byte(unsigned short crc, const unsigned char data)
{
ch8_tlv/makefile
New file
@@ -0,0 +1,11 @@
BINAME=tlv_sample
all:
    gcc *.c -o ${BINAME}
clean:
    rm -f ${BINAME}
run: all
    ./${BINAME}
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;