/********************************************************************************* * Copyright: (C) 2019 LingYun IoT studio * All rights reserved. * * Filename: tlv_sample.c * Description: This file * * Version: 1.0.0(03/27/2019) * Author: Guo Wenxue * ChangeLog: 1, Release initial version on "03/27/2019 09:20:25 PM" * ********************************************************************************/ #include #include #include "crc-itu-t.h" #define OFF 0 #define ON 1 #define BUFSIZE 128 /* TLV Packet format: * *+------------+---------+------------+-----------+-----------+ *| Header(1B) | Tag(1B) | Length(1B) | Value(1B) | CRC16(2B) | *+------------+---------+------------+-----------+-----------+ */ #define PACK_HEADER 0xFD /* Tag definition */ enum { TAG_LOGON=1, TAG_CAMERA, TAG_LED, }; /* TLV packet fixed segement size, 1B Head, 1B Tag, 1B length, 2B CRC16, total 5B. */ #define TLV_FIXED_SIZE 5 /* TLV packet Minimum size is fixed bytes + 1 byte data */ #define TLV_MIN_SIZE (TLV_FIXED_SIZE+1) int packtlv_logon(char *buf, int size, char *pwd); int packtlv_led(char *buf, int size, int cmd); void dump_buf(char *data, int len); int main(int argc, char **argv) { char buf[BUFSIZE]; int bytes; 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"); dump_buf(buf, bytes); return 0; } int packtlv_logon(char *buf, int size, char *pwd) { unsigned short crc16 = 0; int pack_len = 0; int data_len = 0; int ofset = 0; /* index position for the buf */ if(!buf || !pwd || size?" "@ABCDEFGHIJKLMNO" "PQRSTUVWXYZ[\\]^_" "`abcdefghijklmno" "pqrstuvwxyz{|}~ " " " " " " ???????????????" "????????????????" "????????????????" "????????????????" "????????????????" "????????????????"; /* 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; int idx; char prn[LINELEN]; char lit[CHARS_PER_LINE + 1]; char hc[4]; short line_done = 1; rc = len; idx = 0; lit[CHARS_PER_LINE] = '\0'; while (rc > 0) { if (line_done) snprintf(prn, LINELEN, "%08X: ", idx); do { unsigned char c = data[idx]; snprintf(hc, 4, "%02X ", c); strncat(prn, hc, 4); lit[idx % CHARS_PER_LINE] = print_char[c]; ++idx; } while (--rc > 0 && (idx % CHARS_PER_LINE != 0)); line_done = (idx % CHARS_PER_LINE) == 0; if (line_done) printf("%s %s\n", prn, lit); else if (rc == 0) strncat(prn, " ", LINELEN); } if (!line_done) { lit[(idx % CHARS_PER_LINE)] = '\0'; while ((++idx % CHARS_PER_LINE) != 0) strncat(prn, " ", LINELEN); printf("%s %s\n", prn, lit); } }