1, depending on the cross-compiled libraries openssl
(1) Configuration information compiler
setarch i386 ./config no-asm shared --cross-compile-prefix=arm-linux-androideabi-
(2) modify the Makefile
Delete -m32
(3) The compiler (Compiler specified)
make CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++
2, the cross-compiler mosquitto
(1) modify the config.mk
WITH_STATIC_LIBRARIES:=yes
CFLAGS += -I/where_is_your_openssl_headerfiles/
LDFLAGS += -L/where_is_your_openssl_staticlib/
(2) Compile
make CC=arm-linux-androideabi-gcc CXX=arm-linux-androideabi-g++
3, based on the MQTT client mosquitto
Struct mqtt_conf code is a custom configuration, mqtt_send implementation is a doubly linked list queue.
void start_mqtt_module(struct mqtt_conf *conf) { if (!conf->server || conf->server_port <= 0) { DEBUG_MSG("[%s] invalid parameters for mqtt module\n", THISFILE); return; } int rc = 0; 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, 60); if (MOSQ_ERR_SUCCESS != rc) { DEBUG_MSG("[%s] failed to connect mqtt server %s:%d\n", ThisFile, conf -> Server, conf -> SERVER_PORT); } } the while (RC =! MOSQ_ERR_SUCCESS); DEBUG_MSG ( " [% S] Connect MQTT Server% S:% D the OK \ n- " , thisFile, conf -> Server , conf -> SERVER_PORT); set_queue_alive ( & mqtt_send); SLEEP ( . 1 ); // new thread timing for transmitting the PING the MQTT create_function_thread (mqtt_loop, NULL); struct mqtt_msg * MSG; the while (RUN == - . 1 ) { MSG = NULL; // remove a message from the queue out 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 = 0; while (run == -1){ int rc = mosquitto_loop(mosq, -1, 1); 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; }