跳到主要内容

动态库说明

头文件libtriton.h内容如下:

#pragma once

#ifdef libtriton_EXPORTS
#define LIBTRITON_API __declspec(dllexport)
#else
#define LIBTRITON_API __declspec(dllimport)
#endif

#ifndef NELEMS
#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
#endif

// Max length for 'action' in struct Payload
#define MAX_ACTION_LENGTH 256
// Max length for 'keyValue' in struct Payload
#define MAX_KEYVALUE_PAIRS 5000
// Max length for 'key' in struct KeyValue
#define MAX_KEY_LENGTH 4096
// Max length for 'value' in struct KeyValue
#define MAX_VALUE_LENGTH 4096
// Max length for received message
#define MAX_MESSAGE_LENGTH 65536

typedef struct KeyValue {
char* key;
char* value;
}KeyValue;

typedef struct Payload {
int size;
char* action;
KeyValue *keyValue;
}Payload;

#ifdef __cplusplus
extern "C" {
#endif

// Send data
// Parameters:
// kvs - array of key value pairs (KeyValue structs) to be sent
// size - length of array (kvs)
// Must use freeKeyValue() to free allocated KeyValue struct array afterwards
// Return values:
// 0 - success
// -1 - error connecting to server
// -2 - error writing to server
// -3 - error reading response from server
LIBTRITON_API int Send(KeyValue kvs[], int size);

LIBTRITON_API int Send_Compat(KeyValue kvs[], int size);

// Receive data
// Returns received data payload
// Must use freePayload() to release memory afterwards
LIBTRITON_API Payload* Receive();

LIBTRITON_API Payload* Receive_Compat();

// MQTT status
// Returns MQTT connection status
// 0 - Not connected
// 1 - Connected
LIBTRITON_API int Status();

LIBTRITON_API int Status_Compat();


// Get device UUID
// Parameters
// buffer: buffer for storing returned UUID
// Returns UUID length
LIBTRITON_API int GetUUID(char *buffer);

LIBTRITON_API int GetUUID_Compat(char *buffer);

// Frees allocated memory for received payload
LIBTRITON_API void freePayload(Payload *payload);

// Frees allocated KeyValues struct
LIBTRITON_API void freeKeyValues(KeyValue *keyValues, int size);

#ifdef __cplusplus
}
#endif
提示
  • 需要发送的数据按结构体KeyValue构造,将KeyValue数组传入发送函数Send()
  • 带有_Compat后缀的函数为兼容模式函数,一般情况下不需使用
  • Receive函数必须由调用者定期调用以获取消息
  • 接收完成后需要调用freePayload函数以释放内存
  • 发送完成后需要调用freeKeyvalues函数以释放内存