mco_iot_comm_register_callback
此函数为通信器 comm 在 ARF(物联网)运行时注册一个回调函数结构体 iot_comm_callback_t 。
请注意,对于所有当前处于活动状态的回调,上下文必须是唯一的;也就是说,无法注册两个具有相同上下文的回调。
MCO_RET mco_iot_comm_register_callback(
mco_iot_comm_h comm,
iot_comm_callback_t *callback,
void *context
);有关概述,请参阅主动复制结构页面。
参数
comm
mco_iot_comm_h
要注册回调函数的通信器对象。
callback
iot_comm_callback_t
带有已初始化函数指针的回调结构体 iot_comm_callback_t。
context
void
用户定义的上下文(指向包含应用程序特定数据的结构的指针)。
返回
MCO_S_OK
回调函数已成功注册。
MCO_E_ILLEGAL_PARAM
参数中的值无效/不合适。
MCO_E_NOMEM
无法为回调描述符分配内存。
MCO_E_IOT_DUPLICATE_CALLBACK
具有相同上下文的回调函数已注册(请参阅上面的注释)。
示例
下面的代码片段定义了 5 个回调函数,初始化 ARF(物联网)运行时环境,在打开并连接数据库之后,创建通信器,然后注册这些回调函数:
int iot_on_connect(mco_iot_connection_h iotc, void *context)
{
mco_iot_connection_stat_t iotc_stat;
char address[MCO_NET_INETADDR_STRING_LEN];
mco_iot_conn_get_stat(iotc, &iotc_stat);
mco_net_ntoa(&iotc_stat.inetaddr, address);
printf("Agent %llu connected. Address %s:%d\n", mco_iot_conn_get_agent_id(iotc), address, iotc_stat.port);
return IOT_CALLBACK_OK;
}
int iot_on_receive(mco_iot_connection_h iotc, mco_iot_read_stream_t *stream, void *context)
{
printf("DATA received from agent %llu\n", mco_iot_conn_get_agent_id(iotc));
return IOT_CALLBACK_OK;
}
int iot_on_ack(mco_iot_connection_h iotc, const iot_ack_t *ack, void *context)
{
printf("ACK received from agent %llu : %llu->%llu, ts=%llu, errcode=%d (%s)\n", mco_iot_conn_get_agent_id(iotc),
ack->sender_agent_id, ack->receiver_agent_id, ack->timestamp, ack->error_code, mco_strerror(ack->error_code));
return IOT_CALLBACK_OK;
}
int iot_on_disconnect(mco_iot_connection_h iotc, void *context)
{
mco_iot_connection_stat_t iotc_stat;
mco_iot_conn_get_stat(iotc, &iotc_stat);
printf("Agent %llu disconnected. Bytes sent %llu, recevied %llu\n", mco_iot_conn_get_agent_id(iotc),
iotc_stat.sent_bytes, iotc_stat.recv_bytes);
return IOT_CALLBACK_OK;
}
int iot_on_destroy(mco_iot_connection_h iotc, void *context)
{
printf("Agent connection %llu destroyed\n", mco_iot_conn_get_agent_id(iotc));
return IOT_CALLBACK_OK;
}
int main(int argc, char *argv[])
{
mco_db_params_t db_params;
mco_device_t dev;
mco_db_h db;
mco_iot_replicator_params_t repl_params;
mco_iot_comm_params_t comm_params;
mco_iot_replicator_h repl;
mco_iot_comm_h comm;
const char *conn_string = (argc > 1) ? argv[1] : "127.0.0.1:15000";
iot_comm_callback_t iot_callbacks = {&iot_on_connect, &iot_on_receive, &iot_on_ack, &iot_on_disconnect, &iot_on_destroy};
/* Initialize SmartEDB and IoT runtimes */
mco_error_set_handler(&sample_errhandler);
mco_runtime_start();
mco_iot_init();
/* Create database */
dev.type = MCO_MEMORY_CONV;
dev.assignment = MCO_MEMORY_ASSIGN_DATABASE;
dev.size = DEVICE_DATABASE_SIZE;
dev.dev.conv.ptr = (void*)malloc(dev.size);
mco_db_params_init (&db_params);
db_params.db_max_connections = 5;
if (argc > 2) {
db_params.iot_agent_id = atoi(argv[2]); /* Override the agent_id in the schema */
}
CHECK(mco_db_open_dev(db_name, iotdevice_get_dictionary(), &dev, 1, &db_params));
CHECK(mco_db_connect(db_name, &db));
mco_iot_comm_params_init(&comm_params);
CHECK(mco_iot_comm_create(&comm_params, &comm));
...
CHECK(mco_iot_comm_register_callback(comm, &iot_callbacks, comm));
}文件
- 头文件:
mcoiot.h - 源文件:
mcoiotcomm.c - 库:
libmcoiotcomm.a
