zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    oracle 按关键字排序前几行
    oracle 查看某表的前10行
    linux 7安装部署Redis
    oracle 查看库表状态
    centos 7 启动和关闭zabbix 服务
    oracle 创建用户密码及赋予登录权限
    linux 控制root登录宿主机时间
    centos 更改用户登录宿主机时间
    oracle 查询、创建、删除 数据库用户
    Django基础四之模板系统
  • 原文地址:https://www.cnblogs.com/rayfloyd/p/11692161.html
Copyright © 2011-2022 走看看