• mosquitto/openssl 在RK3288上的编译以及MQTT客户端的代码示例


    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 <= 0) {
            DEBUG_MSG("[%s] invalid parameters for mqtt module
    ", THISFILE);
            return;
        }
    
        int rc = 0;
    
        DEBUG_MSG("init_default_mqtt_parameters clientid %s
    ", 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
    ", 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
    ", 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
    ", 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
    ", THISFILE, conf->server, conf->server_port);
            }
        } while (rc != MOSQ_ERR_SUCCESS);
    
        DEBUG_MSG("[%s] connect mqtt server %s:%d OK
    ", THISFILE, conf->server, conf->server_port);
    
        set_queue_alive(&mqtt_send);
    
        sleep(1);
            //新线程用于定时发送 MQTT PING
        create_function_thread(mqtt_loop, NULL);
    
        struct mqtt_msg *msg;
    
        while (run == -1) {
            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
    ", THISFILE);
                    mqtt_pubs_succ++;
                } else {
                    DEBUG_MSG("[%s] mosquitto_publish failed %d
    ", 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
    ", THISFILE, rc);
                if (rc == MOSQ_ERR_ERRNO) {
                    DEBUG_MSG("[%s] mosquitto_loop error %s, errno %d
    ", THISFILE, strerror(errno), errno);
                }
                mosquitto_lib_cleanup();
                break;
            } else {
                //DEBUG_MSG("[%s] mosquitto_loop started! %d
    ", THISFILE, cnt);
            }
            cnt++;
            //3s  keepalive
            usleep(DELTA_US);
        }
    
        return NULL;
    }
  • 相关阅读:
    核函数基础一简单解释
    矩阵的基本性质 之 正规矩阵,矩阵的迹,行列式,伴随矩阵,矩阵的逆,对角矩阵,矩阵求导
    矩阵的基本性质 之 对称矩阵,Hermite矩阵,正交矩阵,酉矩阵
    矩阵的基本性质 之 矩阵加减法,数乘,乘法,转置
    机器学习实战基础(二十七):sklearn中的降维算法PCA和SVD(八)PCA对手写数字数据集的降维
    拉格朗日对偶性
    批处理符号2
    批处理符号1
    set命令
    goto命令
  • 原文地址:https://www.cnblogs.com/rayfloyd/p/11692161.html
Copyright © 2020-2023  润新知