C / C++
TiJet C SDK 提供设备连接、参数访问、打印、打印控制、升级、事件、日志和版本接口。
快速开始
#include <tijet/tijet.h>
ti_client_t *cli = ti_create();
if (cli == NULL) {
return 1;
}
if (ti_connect(cli, NULL) != TI_OK) {
fprintf(stderr, "%s\n", ti_last_error(cli));
ti_destroy(cli);
return 1;
}
ti_set_int(cli, TI_PARAM_PRINT_SPEED_TIMER, 10000);
ti_disconnect(cli);
ti_destroy(cli);
C++ 兼容性
C++ 应用使用同一个 <tijet/tijet.h> 头文件、同一套 TI_PARAM_* 常量和与快速开始完全相同的函数调用方式,不需要额外的 C++ SDK 或头文件。
类型
ti_client_t
客户端句柄。通过 ti_create() 创建,通过 ti_destroy() 释放。
ti_event_callback_t
typedef void (*ti_event_callback_t)(const char *event_json, void *user_data);
event_json:事件数据,指针仅在回调执行期间有效。user_data:注册回调时传入的自定义指针。
ti_log_callback_t
typedef void (*ti_log_callback_t)(const char *log_line, void *user_data);
log_line:日志文本,指针仅在回调执行期间有效。user_data:注册回调时传入的自定义指针。
生命周期
ti_create
ti_client_t *ti_create(void);
创建客户端句柄。
- 参数:无。
- 返回:成功时返回非 NULL 句柄;无法分配客户端时返回
NULL。 - 注意:句柄使用完毕后必须调用
ti_destroy()。
ti_destroy
void ti_destroy(ti_client_t *client);
停止事件和日志订阅、断开连接并释放客户端。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 客户端句柄。允许传 NULL,此时不做任何操作。 |
- 返回:无。
连接
ti_connect
int ti_connect(ti_client_t *client, const char *address);
连接设备。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | ti_create() 创建的客户端句柄。 |
address | const char * | 输入 | 设备地址。NULL 或空字符串使用本机默认设备;host:port 使用 TCP;其他非空值表示本机设备路径。 |
- 返回:
TI_OK:连接可用。TI_ERR_INVALID_ARG:client为NULL。TI_ERR_TIMEOUT:连接在超时前未就绪。
- 注意:已有连接时调用会先断开旧连接。
ti_disconnect
int ti_disconnect(ti_client_t *client);
停止订阅并断开设备连接。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 客户端句柄。 |
- 返回:
TI_OK:已断开,或此前已经断开。TI_ERR_INVALID_ARG:client为NULL。
ti_is_connected
int ti_is_connected(const ti_client_t *client);
查询连接当前是否可用。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | const ti_client_t * | 输入 | 客户端句柄。 |
- 返回:已连接且可用时返回
1;已断开或client为NULL时返回0。
ti_network_config
int ti_network_config(ti_client_t *client,
const char *ip,
const char *ip_mask,
const char *gateway);
配置设备网络参数。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 已连接的客户端句柄。 |
ip | const char * | 输入 | IPv4 地址;NULL 或空字符串表示不修改。 |
ip_mask | const char * | 输入 | 子网掩码;NULL 或空字符串表示不修改。 |
gateway | const char * | 输入 | 默认网关;NULL 或空字符串表示不修改。 |
- 返回:
TI_OK:配置已接受。TI_ERR_INVALID_ARG:client为NULL,或三个配置项全部为空。TI_ERR_NOT_CONNECTED:客户端未连接。TI_ERR_SERVER:设备拒绝配置。
参数
ti_set_parameter
int ti_set_parameter(ti_client_t *client,
const char *key,
const char *value);
使用字符串形式设置参数。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 已连接的客户端句柄。 |
key | const char * | 输入 | 参数注册表中区分大小写的 Key。 |
value | const char * | 输入 | 字符串值;bool 使用 "true" 或 "false"。 |
- 返回:
TI_OK:参数已接受。TI_ERR_INVALID_ARG:任一参数为NULL。TI_ERR_NOT_CONNECTED:客户端未连接。TI_ERR_SERVER:设备拒绝 Key 或值。
ti_get_parameter
int ti_get_parameter(const ti_client_t *client,
const char *key,
char *value_buf,
size_t buf_size);
读取参数到调用方提供的缓冲区。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | const ti_client_t * | 输入 | 已连接的客户端句柄。 |
key | const char * | 输入 | 区分大小写的参数 Key。 |
value_buf | char * | 输出 | 输出缓冲区。 |
buf_size | size_t | 输入 | 缓冲区字节数,包含结尾空字符所需空间,且必须大于 0。 |
- 返回:
TI_OK:参数值复制成功。TI_ERR_INVALID_ARG:client、key或value_buf为NULL。TI_ERR_NOT_CONNECTED:客户端未连接。TI_ERR_BUFFER_TOO_SMALL:缓冲区不足,输出已被截断。TI_ERR_SERVER:设备没有该参数。
ti_set_int
int ti_set_int(ti_client_t *client, const char *key, int32_t value);
设置 int32 参数。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 已连接的客户端句柄。 |
key | const char * | 输入 | 参数 Key。 |
value | int32_t | 输入 | 整数。 |
- 返回:与
ti_set_parameter()相同。
ti_get_int
int ti_get_int(const ti_client_t *client, const char *key, int32_t *out);
读取 int32 参数。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | const ti_client_t * | 输入 | 已连接的客户端句柄。 |
key | const char * | 输入 | 参数 Key。 |
out | int32_t * | 输出 | 接收解析后的整数,不能为 NULL。 |
- 返回:与
ti_get_parameter()相同。
ti_set_double
int ti_set_double(ti_client_t *client, const char *key, double value);
设置 double 参数。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 已连接的客户端句柄。 |
key | const char * | 输入 | 参数 Key。 |
value | double | 输入 | double 值。 |
- 返回:与
ti_set_parameter()相同。
ti_get_double
int ti_get_double(const ti_client_t *client, const char *key, double *out);
读取 double 参数。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | const ti_client_t * | 输入 | 已连接的客户端句柄。 |
key | const char * | 输入 | 参数 Key。 |
out | double * | 输出 | 接收解析后的值,不能为 NULL。 |
- 返回:与
ti_get_parameter()相同。
ti_set_bool
int ti_set_bool(ti_client_t *client, const char *key, int value);
设置 bool 参数。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 已连接的客户端句柄。 |
key | const char * | 输入 | 参数 Key。 |
value | int | 输入 | 非 0 写入 "true",0 写入 "false"。 |
- 返回:与
ti_set_parameter()相同。
ti_get_bool
int ti_get_bool(const ti_client_t *client, const char *key, int *out);
读取 bool 参数。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | const ti_client_t * | 输入 | 已连接的客户端句柄。 |
key | const char * | 输入 | 参数 Key。 |
out | int * | 输出 | 值为 "true" / "1" 时写入 1,否则写入 0;不能为 NULL。 |
- 返回:与
ti_get_parameter()相同。
C 的 _X Key 宏
包含 _X 的参数使用函数式宏。应传入数字字面量:
ti_get_double(cli, TI_PARAM_INK_CARD_INK_TOTAL(1), &ink_total);
宏参数会被预处理器转成文本,因此运行时变量会被转换成变量名。编号来自变量时,应显式构造 Key:
char key[64];
snprintf(key, sizeof(key), "INK_CARD_INK_TOTAL_%u", head_no);
ti_get_double(cli, key, &ink_total);
打印
ti_set_print_data
int ti_set_print_data(ti_client_t *client,
const uint8_t *data,
size_t data_len,
uint32_t index);
上传一份打印数据。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 已连接的客户端句柄。 |
data | const uint8_t * | 输入 | 打印数据字节,不能为 NULL。 |
data_len | size_t | 输入 | 数据字节数,必须大于 0。 |
index | uint32_t | 输入 | 由应用定义的打印数据序号。 |
- 返回:
TI_OK:上传完成。TI_ERR_INVALID_ARG:必填参数无效。TI_ERR_NOT_CONNECTED:客户端未连接。TI_ERR_SERVER:设备拒绝数据或通信失败。
- 注意:
TI_OK不代表设备已经可以打印,应以打印启动结果通知作为最终启动结果。
ti_print_control
int ti_print_control(ti_client_t *client, const char *key);
执行打印控制指令。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 已连接的客户端句柄。 |
key | const char * | 输入 | 大写指令:"START_PRINT"、"STOP_PRINT"、"PAUSE_PRINT"、"RESUME_PRINT" 或 "ANALOG_TRIGGER"。 |
- 返回:
TI_OK:指令已接受。TI_ERR_INVALID_ARG:client/key为NULL,或指令不受支持。TI_ERR_NOT_CONNECTED:客户端未连接。TI_ERR_SERVER:设备拒绝或执行失败。
升级
ti_upgrade
int ti_upgrade(ti_client_t *client,
const char *key,
const char *file_path);
上传升级文件。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 已连接的客户端句柄。 |
key | const char * | 输入 | 升级目标。整机升级使用 TI_UPGRADE_TARGET_SYSTEM,不能为空。 |
file_path | const char * | 输入 | 已存在且非空的文件路径,最大 256 MiB。 |
- 返回:
TI_OK:上传完成,设备已开始处理文件。TI_ERR_INVALID_ARG:参数无效或文件不可用。TI_ERR_NOT_CONNECTED:客户端未连接。TI_ERR_TIMEOUT:上传超时。TI_ERR_SERVER:设备拒绝文件或通信失败。
- 注意:该调用会阻塞,上传超时最长约 300 秒,不要在 UI 线程调用。
TI_OK不代表升级文件已经可用,应以升级状态通知为准。
事件
ti_set_event_callback
void ti_set_event_callback(ti_client_t *client,
ti_event_callback_t callback,
void *user_data);
注册或清除事件回调。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 客户端句柄;NULL 时忽略。 |
callback | ti_event_callback_t | 输入 | 回调函数;传 NULL 清除回调。 |
user_data | void * | 输入 | 原样传给回调的自定义指针,可为 NULL。 |
- 返回:无。
- 注意:应在
ti_start_event_subscription()之前调用;回调中不要长时间阻塞。
ti_start_event_subscription
int ti_start_event_subscription(ti_client_t *client);
启动事件通知。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 已连接的客户端句柄。 |
- 返回:
TI_OK:订阅已启动。TI_ERR_INVALID_ARG:client为NULL。TI_ERR_NOT_CONNECTED:客户端未连接。TI_ERR_INTERNAL:SDK 无法启动订阅。
- 注意:重复调用会重启订阅;启动后到首个事件到达之间可能有短暂延迟。
ti_stop_event_subscription
int ti_stop_event_subscription(ti_client_t *client);
停止事件通知。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 客户端句柄。 |
- 返回:
TI_OK:已停止,或此前没有运行中的订阅。TI_ERR_INVALID_ARG:client为NULL。
- 注意:函数会等待订阅停止后返回。
日志
ti_set_log_callback
void ti_set_log_callback(ti_client_t *client,
ti_log_callback_t callback,
void *user_data);
注册或清除日志回调。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 客户端句柄;NULL 时忽略。 |
callback | ti_log_callback_t | 输入 | 回调函数;传 NULL 后日志会被丢弃。 |
user_data | void * | 输入 | 原样传给回调的自定义指针,可为 NULL。 |
- 返回:无。
- 注意:应在
ti_start_log_subscription()之前调用;回调中不要长时间阻塞。
ti_start_log_subscription
int ti_start_log_subscription(ti_client_t *client);
启动日志通知。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 已连接的客户端句柄。 |
- 返回:
TI_OK:订阅已启动。TI_ERR_INVALID_ARG:client为NULL。TI_ERR_NOT_CONNECTED:客户端未连接。TI_ERR_INTERNAL:SDK 无法启动订阅。
ti_stop_log_subscription
int ti_stop_log_subscription(ti_client_t *client);
停止日志通知。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | ti_client_t * | 输入 | 客户端句柄。 |
- 返回:
TI_OK:已停止,或此前没有运行中的订阅。TI_ERR_INVALID_ARG:client为NULL。
错误与版本
ti_last_error
const char *ti_last_error(const ti_client_t *client);
获取客户端最近一次错误文本。
| 参数 | 类型 | 方向 | 说明 |
|---|---|---|---|
client | const ti_client_t * | 输入 | 客户端句柄。 |
- 返回:可读错误字符串;
client为NULL时返回无效句柄提示。 - 注意:返回字符串不需要释放,并在同一客户端的下一次操作前保持有效。
ti_version
const char *ti_version(void);
获取 SDK 版本。
- 参数:无。
- 返回:
major.minor.patch格式的静态字符串。 - 注意:返回字符串不需要释放。
状态码
| 值 | 含义 |
|---|---|
TI_OK | 成功 |
TI_ERR_INVALID_ARG | 参数无效 |
TI_ERR_NOT_CONNECTED | 未连接 |
TI_ERR_TIMEOUT | 操作超时 |
TI_ERR_BUFFER_TOO_SMALL | 输出缓冲区不足 |
TI_ERR_SERVER | 设备拒绝操作 |
TI_ERR_INTERNAL | SDK 无法完成操作 |
完整参数见参数注册表。