mosquitto/openssl 在RK3288上的编译以及MQTT客户端的代码示例
2024-09-01 13:12:43
1,依赖库openssl 的交叉编译
(1)配置编译器信息
setarch i386 ./config no-asm shared --cross-compile-prefix=arm-linux-androideabi-
(2)修改Makefile
删除-m32
(3)编译(指定编译器)
make CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++
2,mosquitto 的交叉编译
(1)修改该config.mk
WITH_STATIC_LIBRARIES:=yes
CFLAGS += -I/where_is_your_openssl_headerfiles/
LDFLAGS += -L/where_is_your_openssl_staticlib/
(2)编译
make CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++
3,基于mosquitto的MQTT client
代码中struct mqtt_conf是自定义结构,mqtt_send是一个双向链表实现的队列。
void start_mqtt_module(struct mqtt_conf *conf)
{
if (!conf->server || conf->server_port <= ) {
DEBUG_MSG("[%s] invalid parameters for mqtt module\n", THISFILE);
return;
} int rc = ; DEBUG_MSG("init_default_mqtt_parameters clientid %s\n", conf->clientid); mosquitto_lib_init(); mosq = mosquitto_new(conf->clientid, conf->clean_session, NULL); #ifdef MQTT_DEBUGLOG
mosquitto_log_callback_set(mosq, log_callback);
#endif mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_publish_callback_set(mosq, on_publish);
mosquitto_disconnect_callback_set(mosq, on_disconnect); mosquitto_username_pw_set(mosq, conf->username, conf->password); //配置证书
if (!conf->disable_ssl) {
rc = mosquitto_tls_set(mosq,
conf->cafile,
conf->capath,
conf->certfile,
conf->keyfile, /* Notice: DO NOT encrypt your private key */
NULL /* password callback */
);
if (MOSQ_ERR_SUCCESS != rc) {
DEBUG_MSG("[%s] failed to set tls certification info\n", THISFILE);
} rc = mosquitto_tls_opts_set(mosq, SSL_VERIFY_PEER, NULL, NULL);
if (MOSQ_ERR_SUCCESS != rc) {
DEBUG_MSG("[%s] failed to set tls option\n", THISFILE);
} /* set this to false(default) means check server hostname under TLS protocol */
rc = mosquitto_tls_insecure_set(mosq, true);
if (MOSQ_ERR_SUCCESS != rc) {
DEBUG_MSG("[%s] failed to set incecrue\n", THISFILE);
}
}
//链接服务器
do {
rc = mosquitto_connect(mosq, conf->server, conf->server_port, );
if (MOSQ_ERR_SUCCESS != rc) {
DEBUG_MSG("[%s] failed to connect mqtt server %s:%d\n", THISFILE, conf->server, conf->server_port);
}
} while (rc != MOSQ_ERR_SUCCESS); DEBUG_MSG("[%s] connect mqtt server %s:%d OK\n", THISFILE, conf->server, conf->server_port); set_queue_alive(&mqtt_send); sleep();
//新线程用于定时发送 MQTT PING
create_function_thread(mqtt_loop, NULL); struct mqtt_msg *msg; while (run == -) {
msg = NULL;
//从队列中取出一个消息PUB出去
msg = (struct mqtt_msg *)dequeue(&mqtt_send);
if (msg) {
/* dispatch msg according to Qos */
rc = mosquitto_publish(mosq, NULL, msg->topic, msg->datalen, msg->data, msg->qos, false); if (MOSQ_ERR_SUCCESS == rc) {
DEBUG_MSG("[%s] mosquitto_publish success\n", THISFILE);
mqtt_pubs_succ++;
} else {
DEBUG_MSG("[%s] mosquitto_publish failed %d\n", THISFILE, rc);
mqtt_pubs_fail++;
} free(msg);
}
}
mosquitto_lib_cleanup();
}
/*
MQTT ping and reply for keepalive
*/
void *mqtt_loop(void *arg)
{
uint32_t cnt = ;
while (run == -){
int rc = mosquitto_loop(mosq, -, );
if (MOSQ_ERR_SUCCESS != rc) {
DEBUG_MSG("[%s] mosquitto_loop error %d\n", THISFILE, rc);
if (rc == MOSQ_ERR_ERRNO) {
DEBUG_MSG("[%s] mosquitto_loop error %s, errno %d\n", THISFILE, strerror(errno), errno);
}
mosquitto_lib_cleanup();
break;
} else {
//DEBUG_MSG("[%s] mosquitto_loop started! %d\n", THISFILE, cnt);
}
cnt++;
//3s keepalive
usleep(DELTA_US);
} return NULL;
}
最新文章
- 在Javascript中onclick()方法应用
- Kanzi Studio中的概念
- 配置hibernate
- Scrum会议2(Beta版本)
- Codeforce Round #226 Div2
- 多线程同步内功心法——PV操作上(未完待续。。。)
- URAL 2040 (回文自动机)
- Thread类详解
- JS中apply与call的含义与区别
- Build path contains duplicate entry
- Cmake出现CMake Error: Could not find CMAKE_ROOT !!!
- 主机服务绑定IP
- stark组件开发之组合搜索高级显示和扩展
- PHP与Excel 笔记
- Visio 保存卡死解决办法
- php实现单点登录实例
- Mysql 2条记录 差值计算
- SpringMVC中的Model和ModelAndView的区别
- Spring-Resource接口
- NET Core 实战:使用 NLog 将日志信息记录到 MongoDB