Documentation

Documentation

Includes

tuya_cloud_types.h

#include "tuya_cloud_types.h"
#include "tuya_cloud_types.h"

Macros

Marco __C_TLV_H__

#define __C_TLV_H__


      

Marco UINT8(ptr)

#define UINT8(ptr)                  ((UINT8_T *)(ptr))
/* 强转成某一类型的指针 */

Marco INT8(ptr)

#define INT8(ptr)                   ((INT8_T *)(ptr))


      

Marco UINT16(ptr)

#define UINT16(ptr)                 ((UINT16_T *)(ptr))


      

Marco INT16(ptr)

#define INT16(ptr)                  ((INT16_T *)(ptr))


      

Marco UINT32(ptr)

#define UINT32(ptr)                 ((UINT_T *)(ptr))


      

Marco INT32(ptr)

#define INT32(ptr)                  ((INT_T *)(ptr))


      

Marco PACKED

#define PACKED                  __attribute__((packed))
/* 向指针赋值 */

Marco PUT8(ptr, c)

#define PUT8(ptr, c)            (*UINT8((ptr)) = (UINT8_T)(c))


      

Marco PUT16(ptr, s)

#define PUT16(ptr, s)           (*UINT16((ptr)) = (UINT16_T)(s))


      

Marco PUT32(ptr, l)

#define PUT32(ptr, l)           (*UINT32((ptr)) = (UINT_T)(l))


      

Marco GET_UINT8(ptr)

#define GET_UINT8(ptr)             (*UINT8((ptr)))
/* 从指针取值 */

Marco GET_INT8(ptr)

#define GET_INT8(ptr)              (*INT8((ptr)))


      

Marco GET_UINT16(ptr)

#define GET_UINT16(ptr)            (*UINT16((ptr)))


      

Marco GET_INT16(ptr)

#define GET_INT16(ptr)             (*INT16((ptr)))


      

Marco GET_UINT32(ptr)

#define GET_UINT32(ptr)            (*UINT32((ptr)))


      

Marco GET_INT32(ptr)

#define GET_INT32(ptr)             (*INT32((ptr)))


      

Marco ADVANCE8(ptr)

#define ADVANCE8(ptr)               ptr = (UINT8(UINT8((ptr)) + 1))
/* 指针向下移位 */

Marco ADVANCE16(ptr)

#define ADVANCE16(ptr)              ptr = (UINT8(UINT16((ptr)) + 1))


      

Marco ADVANCE32(ptr)

#define ADVANCE32(ptr)              ptr = (UINT8(UINT32((ptr)) + 1))


      

Marco MSG_MAGIC

#define MSG_MAGIC                (305419896)


      

Marco MSG_AVAILABLE_TLVS(msg)

#define MSG_AVAILABLE_TLVS(msg)  (((msg)->capacity) - ((msg)->nitems))


      

Marco MSG_TLV(msg, index)

#define MSG_TLV(msg, index)      (&((msg)->tlvs[(index)]))


      

Marco MSG_LAST_TLV(msg)

#define MSG_LAST_TLV(msg)     (MSG_TLV((msg), (msg)->nitems)) //last unvalid tlv element


      

Marco MSG_SIZE(msg)

#define MSG_SIZE(msg)         ((UINT16_T)(SIZEOF(message_t) + SIZEOF(tlv_t) * (msg)->capacity))

      

Functions

Func msg_free

VOID msg_free(message_t *msg);
/**
 * @brief free msg
 * 
 * @param[in] msg refer to message_t
 *
 */

Func msg_get_packed_size

UINT_T msg_get_packed_size(message_t * msg);
/**
 * @brief get a packed message length
 * 
 * @param[in] msg refer to message_t
 *
 * @return packed length
 */

Func msg_print

VOID msg_print(message_t * msg);
/**
 * @brief dump a message data
 * 
 * @param[in] msg refer to message_t
 *
 */

Func msg_pack

OPERATE_RET msg_pack(message_t *msg, UINT8_T *out, UINT_T *out_size);
/**
 * @brief pack message to stream
 * 
 * @param[in] msg refer to message_t
 * @param[out] out need malloc first by user
 * @param[in] out_size buffer size
 * @param[out] out_size packed size
 *
 * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
 */

Vars

Variable message_t

message_t * msg_init(UINT_T max_items);
/**
 * @brief create a new msg
 * 
 * @param[in] max_items item
 *
 * @return refer to message_t
 */

Variable tlv_t

tlv_t * msg_append(message_t *msg, tlv_type_t type, UINT16_T id, VOID *value, UINT16_T length);
/**
 * @brief add itme to msg
 * 
 * @param[in] msg refer to message_t
 * @param[in] type refer to tlv_type_t
 * @param[in] id id
 * @param[in] value value
 * @param[in] length length
 *
 * @return refer to tlv_t
 */

Variable message_t

message_t * msg_unpack(UINT8_T *packed, UINT_T size);
/**
 * @brief unpack stream to message
 * 
 * @param[in] packed stream
 * @param[in] size stream size
 *
 * @return refer to message_t
 */

Consts

Types

Typedefs

Typedef tlv_type_t;

typedef enum {
 TLV_TYPE_INT8  = 7,
 TLV_TYPE_UINT8 = 8,
 TLV_TYPE_INT16 = 9,
 TLV_TYPE_UINT16 = 10,
 TLV_TYPE_INT32 = 11,
 TLV_TYPE_UINT32 = 12,
 TLV_TYPE_BYTES = 13,
   TLV_TYPE_MSG   = 14, //Compound nested structure
} tlv_type_t;


      

Typedef tlv_t;

typedef struct {
   UINT16_T id;
   UINT16_T type;
   UINT16_T length;
   UINT8_T * value;
} PACKED tlv_t;
/* tlv/msg structures */

Typedef message_t;

typedef struct {
   UINT_T magic;
   UINT_T nitems;
   UINT_T capacity;
   tlv_t tlvs[0];
} PACKED message_t;