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;
}

最新文章

  1. 在Javascript中onclick()方法应用
  2. Kanzi Studio中的概念
  3. 配置hibernate
  4. Scrum会议2(Beta版本)
  5. Codeforce Round #226 Div2
  6. 多线程同步内功心法——PV操作上(未完待续。。。)
  7. URAL 2040 (回文自动机)
  8. Thread类详解
  9. JS中apply与call的含义与区别
  10. Build path contains duplicate entry
  11. Cmake出现CMake Error: Could not find CMAKE_ROOT !!!
  12. 主机服务绑定IP
  13. stark组件开发之组合搜索高级显示和扩展
  14. PHP与Excel 笔记
  15. Visio 保存卡死解决办法
  16. php实现单点登录实例
  17. Mysql 2条记录 差值计算
  18. SpringMVC中的Model和ModelAndView的区别
  19. Spring-Resource接口
  20. NET Core 实战:使用 NLog 将日志信息记录到 MongoDB

热门文章

  1. apache添加虚拟主机(windows下)
  2. web前端学习(三)css学习笔记部分(7)-- 文字和字体相关样式、盒子相关样式、背景与边框相关样式
  3. cmd 运行 python
  4. 如何在CentOS 7 / Fedora 31/30/29上安装ELK Stack
  5. 【python小随笔】celery周期任务(简单原理)
  6. linux系统 (实验一)实验楼的课程笔记
  7. font-weight
  8. Servlet表单处理
  9. Servlet容器container
  10. NSArray 查询数组中的对象