Appearance
TTLV Data API
TTLV Data API Overview
Function | Description |
---|---|
Ql_iotTtlvCountGet() | Gets the number of nodes in the TTLV data table. |
Ql_iotTtlvNodeGet() | Extracts nodes from the TTLV data table. |
Ql_iotTtlvFree() | Releases TTLV data table resources. |
Ql_iotTtlvNodeGetType() | Gets data type from a specified TTLV data node. |
Ql_iotTtlvNodeGetBool() | Gets Boolean data from a node. |
Ql_iotTtlvNodeGetInt() | Gets integer data from a TTLV data node. |
Ql_iotTtlvNodeGetFloat() | Gets float data from a TTLV data node. |
Ql_iotTtlvNodeGetString() | Gets string data from a TTLV data node. |
Ql_iotTtlvNodeGetByte() | Gets byte data from a TTLV data node. |
Ql_iotTtlvNodeGetStruct() | Gets struct data from a TTLV data node. |
Ql_iotTtlvIdGetType() | Gets data type from a specified feature ID. |
Ql_iotTtlvIdGetBool() | Gets Boolean data from a specified feature ID. |
Ql_iotTtlvIdGetInt() | Gets integer data from a specified feature ID. |
Ql_iotTtlvIdGetFloat() | Gets float data from a specified feature ID. |
Ql_iotTtlvIdGetString() | Gets string data from a specified feature ID. |
Ql_iotTtlvIdGetByte() | Gets byte data from a specified feature ID. |
Ql_iotTtlvIdGetStruct() | Gets struct data from a specified feature ID. |
Ql_iotTtlvIdAddBool() | Adds Boolean data node to TTLV data table. |
Ql_iotTtlvIdAddInt() | Adds integer data node to TTLV data table. |
Ql_iotTtlvIdAddFloat() | Adds float data node to TTLV data table. |
Ql_iotTtlvIdAddString() | Adds string data node to TTLV data table. |
Ql_iotTtlvIdAddByte() | Adds byte data node to TTLV data table. |
Ql_iotTtlvIdAddStruct() | Adds struct data node to TTLV data table. |
API Description
Ql_iotTtlvCountGet
This function gets the number of nodes in the TTLV data table.
Prototype
c
quint32_t Ql_iotTtlvCountGet(const void *ttlvHead)
Parameter
- Input
- const void *
ttlvHead
: TTLV data table.
- const void *
Return Value
The number of nodes in the TTLV data table.
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
c
quint32_t count = Ql_iotTtlvCountGet(ttlvHead);
Ql_iotTtlvNodeGet
This function extracts data nodes from the TTLV data table.
Prototype
c
void *Ql_iotTtlvNodeGet(const void *ttlvHead, quint16_t index, quint16_t *id, QIot_dpDataType_e
*type)
Parameter
Input
- const void *
ttlvHead
: TTLV Data table. - quint16_t
index
: Node index.
- const void *
Output
- quint16_t *
id
: Feature ID. - QIot_dpDataType_e *
type
: Node type. See Data Node Type for details.
- quint16_t *
Return Value
Data node pointer. If the value is NULL, the acquisition fails.
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
c
quint32_t count = Ql_iotTtlvCountGet(ttlvHead);
quint32_t i;
for(i=0;i<count;i++)
{
uint16_t id;
QIot_dpDataType_e type;
void *node = Ql_iotTtlvNodeGet(ttlvHead, i, &id, &type);
if(node)
{
switch (id)
{
case 1:
{
/*Code*/
break;
}
case 2:
{
/*Code*/
break;
}
default:break;
}
}
}
Ql_iotTtlvFree
This function releases TTLV data table resources. After creating TTLV data tables or adding nodes, you must call this function to release table resources.
Prototype
c
void Ql_iotTtlvFree(void **ttlvHead)
Parameter
- Input
- void **
ttlvHead
: TTLV Data table.
- void **
Return Value
None
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
c
void *ttlvHead = NULL;
Ql_iotTtlvIdAddInt(&ttlvHead, 1, 100);
Ql_iotCmdBusPhymodelReport(1, ttlvHead);
Ql_iotTtlvFree(&ttlvHead); //Release the pointer.
Ql_iotTtlvNodeGetType
This function gets node type.
Prototype
c
qbool Ql_iotTtlvNodeGetType(const void *ttlvNode, QIot_dpDataType_e *type)
Parameter
Input
- const void *
ttlvNode
: Node.
- const void *
Output
- QIot_dpDataType_e *
type
: Node type. See Data Node Type for details.
- QIot_dpDataType_e *
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
c
void *ttlvNode = NULL;
QIot_dpDataType_e type;
qbool ret = Ql_iotTtlvNodeGetType(&ttlvNode,&type);
Ql_iotTtlvNodeGetBool
This function gets Boolean data from a TTLV data node.
Prototype
c
qbool Ql_iotTtlvNodeGetBool(const void *ttlvNode, qbool *value)
Parameter
Input
- const void *
ttlvNode
: Node.
- const void *
Output
- qbool *
value
: Node data.
- qbool *
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
See Example of Getting Node Data for details.
Ql_iotTtlvNodeGetInt
This function gets integer data from a TTLV data node.
Prototype
c
qbool Ql_iotTtlvNodeGetInt(const void *ttlvNode, int *value)
Parameter
Input
- const void *
ttlvNode
: Node.
- const void *
Output
- int *
value
: Node data.
- int *
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
See Example of Getting Node Data for details.
Ql_iotTtlvNodeGetFloat
This function gets float data from a TTLV data node.
Prototype
c
qbool Ql_iotTtlvNodeGetFloat(const void *ttlvNode, double *value)
Parameter
Input
- const void *
ttlvNode
: Node.
- const void *
Output
- double *
value
: Node data.
- double *
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.6.1 and above.
Example
See Example of Getting Node Data for details.
Ql_iotTtlvNodeGetString
This function gets string from a TTLV data node.
Prototype
c
char *Ql_iotTtlvNodeGetString(const void *ttlvNode)
Parameter
- Input
- const void *
ttlvNode
: Node.
- const void *
Return Value
NULL
: Failed executionOthers
: Node data
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
See Example of Getting Node Data for details.
Ql_iotTtlvNodeGetByte
This function gets byte from a TTLV data node.
Prototype
c
quint32_t Ql_iotTtlvNodeGetByte(const void *ttlvNode, quint8_t **value)
Parameter
Input
- const void *
ttlvNode
: Node.
- const void *
Output
- quint8_t **
value
: Node data.
- quint8_t **
Return Value
0
: Failed execution> 0
: Length of the actually read node data
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
See Example of Getting Node Data for details.
Ql_iotTtlvNodeGetStruct
This function gets struct data from a node.
Prototype
c
void *Ql_iotTtlvNodeGetStruct(const void *ttlvNode)
Parameter
- Input
- const void *
ttlvNode
: Node.
- const void *
Return Value
Pointer to the TTLV data table.
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
See Example of Getting Node Data for details.
Ql_iotTtlvIdGetType
This function gets the data type from a specified feature ID.
Prototype
c
qbool Ql_iotTtlvIdGetType(const void *ttlvHead, quint16_t id, QIot_dpDataType_e *type)
Parameter
Input
- const void *
ttlvHead
:TTLV data table. - quint16_t
id
: Feature ID.
- const void *
Output
- QIot_dpDataType_e *
type
: Node data type. See Node Type for details.
- QIot_dpDataType_e *
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
c
void *ttlvHead = Ql_iotTtlvUnformat(payload, payloadLen);
if (NULL == ttlvHead)
{
return;
}
QIot_dpDataType_e type;
qbool ret = Ql_iotTtlvIdGetType(ttlvHead, 1, &type);
Ql_iotTtlvFree(&ttlvHead);
Ql_iotTtlvIdGetBool
This function gets Boolean data from a specified feature ID.
Prototype
c
qbool Ql_iotTtlvIdGetBool(const void *ttlvHead, quint16_t id, qbool *value)
Parameter
Input
- const void *
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID.
- const void *
Output
- qbool *
value
: Node data.
- qbool *
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
c
void *ttlvHead = Ql_iotTtlvUnformat(payload, payloadLen);
if (NULL == ttlvHead)
{
return;
}
qbool reauth_flag = FALSE;
qbool ret = Ql_iotTtlvIdGetBool(ttlvHead, 1, &reauth_flag);
Ql_iotTtlvFree(&ttlvHead);
Ql_iotTtlvIdGetInt
This function gets integer data from a specified feature ID.
Prototype
c
qbool Ql_iotTtlvIdGetInt(const void *ttlvHead, quint16_t id, int *value)
Parameter
Input
- const void *
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID.
- const void *
Output
- int *
value
: Node data.
- int *
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
c
void *ttlvHead = Ql_iotTtlvUnformat(payload, payloadLen);
if (NULL == ttlvHead)
{
return;
}
double increment;
Ql_iotTtlvIdGetFloat(modelInfoHead, 1,&increment);
Ql_iotTtlvFree(&ttlvHead);
Ql_iotTtlvIdGetFloat
This function gets float data from a specified feature ID.
Prototype
c
Qbool Ql_iotTtlvIdGetFloat(const void *ttlvHead, quint16_t id, double *value)
Parameter
Input
- const void *
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID.
- const void *
Output
- double *
value
: Node data.
- double *
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.6.1 and above.
Example
c
void *ttlvHead = Ql_iotTtlvUnformat(payload, payloadLen);
if (NULL == ttlvHead)
{
return;
}
qint64_t componentType;
char *cfgData = Ql_iotTtlvIdGetString(ttlvHead, 1);
Ql_iotTtlvFree(&ttlvHead);
Ql_iotTtlvIdGetString
This function gets string data from a specified feature ID.
Prototype
c
char *Ql_iotTtlvIdGetString(const void *ttlvHead, quint16_t id)
Parameter
- Input
- const void *
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID.
- const void *
Return Value
NULL
: Failed executionOthers
: Node data
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
c
void *ttlvHead = Ql_iotTtlvUnformat(payload, payloadLen);
if (NULL == ttlvHead)
{
return;
}
quint8_t *encryData = NULL;
quint32_t encryLen = Ql_iotTtlvIdGetByte(ttlvHead, 1,&encryData);
Ql_iotTtlvFree(&ttlvHead);
Ql_iotTtlvIdGetByte
This function gets byte data from a specified feature ID.
Prototype
c
quint32_t Ql_iotTtlvIdGetByte(const void *ttlvHead, quint16_t id, quint8_t **value)
Parameter
Input
- const void *
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID.
- const void *
Output
- quint8_t **
value
: Node data.
- quint8_t **
Return Value
0
: Failed execution> 0
: Length of actually read node data
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
c
void *ttlvHead = Ql_iotTtlvUnformat(payload, payloadLen);
if (NULL == ttlvHead)
{
return;
}
qint64_t componentType;
void *dnsInfo = Ql_iotTtlvIdGetStruct(ttlvHead, 1);
Ql_iotTtlvFree(&ttlvHead);
Ql_iotTtlvIdGetStruct
This function gets struct data from a specified feature ID.
Prototype
c
void *Ql_iotTtlvIdGetStruct(const void *ttlvHead, quint16_t id)
Parameter
- Input
- const void *
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID
- const void *
Return Value
Pointer to the TTLV data table.
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
c
void *ttlvHead = Ql_iotTtlvUnformat(payload, payloadLen);
if (NULL == ttlvHead)
{
return;
}
qint64_t componentType;
void *dnsInfo = Ql_iotTtlvIdGetStruct(ttlvHead, 1);
Ql_iotTtlvFree(&ttlvHead);
Ql_iotTtlvIdAddBool
This function adds a Boolean node to TTLV data table.
Prototype
c
qbool Ql_iotTtlvIdAddBool(void **ttlvHead, quint16_t id, qbool value)
Parameter
- Input
- void **
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID. - qbool
value
: Node data.
- void **
Return Value
True
: Successful executionFalse
: Failed execution
Example
See Example of Adding Node Data for details.
Ql_iotTtlvIdAddInt
This function adds an integer node to TTLV data table.
Prototype
c
qbool Ql_iotTtlvIdAddInt(void **ttlvHead, quint16_t id, qint64_t num)
Parameter
- Input
- void **
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID. - qint64_t
num
: Node data.
- void **
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.6.1 and above.
Example
See Example of Adding Node Data for details.
Ql_iotTtlvIdAddFloat
This function adds a float node to TTLV data table.
Prototype
c
qbool Ql_iotTtlvIdAddFloat(void **ttlvHead, quint16_t id, double num)
Parameter
- Input
- void **
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID. - double
num
: Node data.
- void **
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.6.1 and above.
Example
See Example of Adding Node Data for details.
Ql_iotTtlvIdAddString
This function adds a string node to TTLV data table.
Prototype
c
qbool Ql_iotTtlvIdAddString(void **ttlvHead, quint16_t id, const char *data)
Parameter
- Input
- void **
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID. - quint8_t *
data
: Node data.
- void **
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
See Example of Adding Node Data for details.
Ql_iotTtlvIdAddByte
This function adds a byte node to TTLV data table.
Prototype
c
qbool Ql_iotTtlvIdAddByte(void **ttlvHead, quint16_t id, quint8_t *data, quint32_t len)
Parameter
- Input
- void **
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID. - quint8_t *
data
: Node data. - quint32_t
len
: Length of node data.
- void **
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
See Example of Adding Node Data for details.
Ql_iotTtlvIdAddStruct
This function adds a struct node to TTLV data table.
Prototype
c
qbool Ql_iotTtlvIdAddStruct(void **ttlvHead, quint16_t id, void *vStruct)
Parameter
- Input
- void **
ttlvHead
: TTLV data table. - quint16_t
id
: Feature ID. - void *
vStruct
: Node data.
- void **
Return Value
True
: Successful executionFalse
: Failed execution
NOTE
- This function is supported in SDK version 2.3.3 and above.
Example
See Example of Adding Node Data for details.
Node Type
Enumeration Definition
ctypedef enum { QIOT_DPDATA_TYPE_BOOL = 0, QIOT_DPDATA_TYPE_NUM, QIOT_DPDATA_TYPE_BYTE, QIOT_DPDATA_TYPE_STRUCT, } QIot_dpDataType_e;
Member
Member Description QIOT_DPDATA_TYPE_BOOL Bool QIOT_DPDATA_TYPE_NUM Number QIOT_DPDATA_TYPE_BYTE Byte stream QIOT_DPDATA_TYPE_STRUCT Structure
Example of Getting Node Data
c
void Ql_iotTtlvHandle(const void *ttlvHead)
{
quint32_t count = Ql_iotTtlvCountGet(ttlvHead);
quint32_t i;
for(i=0;i<count;i++)
{
uint16_t id;
QIot_dpDataType_e type;
void *node = Ql_iotTtlvNodeGet(ttlvHead, i, &id, &type);
if(node)
{
switch (type)
{
case QIOT_DPDATA_TYPE_BOOL:
{
qbool value;
Ql_iotTtlvNodeGetBool(node, &value);
printf("model id:%d data:%s\r\n",id,value?"TRUE":"FALSE");
break;
}
case QIOT_DPDATA_TYPE_INT:
{
qint64_t num;
Ql_iotTtlvNodeGetInt(node, &num);
printf("model id:%d data:"PRINTF_S64"\r\n",id,num);
break;
}
case QIOT_DPDATA_TYPE_FLOAT:
{
double num;
Ql_iotTtlvNodeGetFloat(node, &num);
printf("model id:%d data:%lg\r\n",id,num);
break;
}
case QIOT_DPDATA_TYPE_BYTE:
{
quint8_t *value;
quint32_t len = Ql_iotTtlvNodeGetByte(node, &value);
printf("model id:%d data:%.*s\r\n",id,len,value);
break;
}
case QIOT_DPDATA_TYPE_STRUCT:
Ql_iotTtlvHandle(Ql_iotTtlvNodeGetStruct(node));
break;
default:
break;
}
}
}
}
Example of Adding Data to TTLV Node
c
/**************************************************************************
** Function @brief: Event callback function.
** Input @param :
** Output @retval:
***************************************************************************/
void Ql_iotSubEventCB(quint32_t event, qint32_t errcode, const char *subPk, const char *subDk, const void *value, quint32_t valLen)
{
printf("subPk[%s] subDk[%s] valLen[%d]\r\n", subPk, subDk, valLen);
switch (event)
{
/* Receive data */
case QIOT_ATEVENT_TYPE_RECV:
printf("data recv event,code:%d\r\n",errcode);
/* Receive the request for TSL from Developer Center */
if(QIOT_RECV_SUCC_PHYMODEL_REQ == errcode && value)
{
quint16_t pkgId = *(quint16_t *)value;
quint16_t *ids = (quint16_t *)((quint8_t*)value+sizeof(quint16_t));
void *ttlvHead = NULL;
printf("model read event,pkgid:%d\r\n",pkgId);
quint32_t i;
for(i=0;i<valLen;i++)
{
quint16_t modelId = ids[i];
printf("modelId:%d\r\n",modelId);
/* id1:bool id2:int id3:string id4:int array id5:string array*/
switch (modelId)
{
case 1:
Ql_iotTtlvIdAddBool(&ttlvHead, modelId, TRUE);
break;
case 2:
Ql_iotTtlvIdAddInt(&ttlvHead, modelId, 1);
break;
case 3:
Ql_iotTtlvIdAddString(&ttlvHead, modelId, "hello world");
break;
case 4:
{
void *ttlvArrayHead = NULL;
Ql_iotTtlvIdAddInt(&ttlvArrayHead, 1, 1);
Ql_iotTtlvIdAddInt(&ttlvArrayHead, 2, 2);
Ql_iotTtlvIdAddInt(&ttlvArrayHead, 3, 3);
Ql_iotTtlvIdAddStruct(&ttlvHead, modelId, ttlvArrayHead);
}
break;
case 5:
{
void *ttlvArrayHead = NULL;
Ql_iotTtlvIdAddString(&ttlvArrayHead, 1, "hello a");
Ql_iotTtlvIdAddString(&ttlvArrayHead, 2, "hello b");
Ql_iotTtlvIdAddString(&ttlvArrayHead, 3, "hello c");
Ql_iotTtlvIdAddStruct(&ttlvHead, modelId, ttlvArrayHead);
}
break;
default:
break;
}
}
Ql_iotSubDevTslAck(subPk, subDk, pkgId, ttlvHead);
Ql_iotTtlvFree(&ttlvHead);
}
break;
default:
break;
}
}