We have upgraded the community system as part of the upgrade a password reset is required for all users before login in.

where can I find the sample codes to publish/subscribe using SSL/TLS(mosquitto) in C language for omega2?



  • I want to make the code that publish/subscribe using SSL/TLS(mosquitto).
    anyone know that, please let me know.
    thanks.



  • @jongrock-chun Here is some code you can use, it is based upon some code by IBM, some is mine but I can't recall how much of it so I left the copyright at the top.

     /*******************************************************************************
     * Copyright (c) 2012, 2020 IBM Corp.
     *
     * All rights reserved. This program and the accompanying materials
     * are made available under the terms of the Eclipse Public License v2.0
     * and Eclipse Distribution License v1.0 which accompany this distribution. 
     *
     * The Eclipse Public License is available at 
     *   https://www.eclipse.org/legal/epl-2.0/
     * and the Eclipse Distribution License is available at 
     *   http://www.eclipse.org/org/documents/edl-v10.php.
     *
     * Contributors:
     *    Ian Craggs - initial contribution
     *******************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <string.h>
    #include <syslog.h>
    #include <signal.h>
    #include <MQTTAsync.h>
    #include <json-c/json.h>
    #include "datadb.h"
    #include "shared.h"
    #include "utils.h"
    
    
    #define ADDRESS     "ssl://mqtt.myapp.com:8883"
    #define CLIENTID    "myapp"
    #define TOPIC       "myapp/rpt"
    #define PAYLOAD     "myapp Data"
    #define QOS         1
    #define TIMEOUT     10000L
    
    #define CA_PATH            "/opt/myapp/certs/ca/ca.crt"
    #define CLIENT_CERT_PATH   "/opt/myapp/certs/client/client.crt"
    #define CLIENT_KEY_PATH    "/opt/myapp/certs/client/client.key"
        
    
    int disc_finished = 0;
    int subscribed = 0;
    int finished = 0;
    pid_t process_id = 0;
    
    void brag() {
    
        syslog(LOG_INFO, "%s v%s\n", PRODUCT_NAME, VERSION);
        syslog(LOG_INFO, "%s %s\n", COPYRIGHT, COMPANY);
    }
    
    
    void connlost(void *context, char *cause) {
        MQTTAsync client = (MQTTAsync) context;
        MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
        int rc;
    
        syslog(LOG_INFO, "Connection lost");
        if (cause)
            syslog(LOG_INFO, "     cause: %s", cause);
    
        syslog(LOG_INFO, "Reconnecting");
        conn_opts.keepAliveInterval = 20;
        conn_opts.cleansession = 1;
        if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS) {
            syslog(LOG_INFO, "Failed to start connect, return code %d", rc);
            finished = 1;
        }
    }
    
    int msgarrvd(void *context, char *topicName, int topicLen, MQTTAsync_message *message) {
    
       ***************** Cut Some Code *************************
    
        parsed_json = json_tokener_parse((char*) message->payload);
        json_object_object_get_ex(parsed_json, "v", &version);
        json_object_object_get_ex(parsed_json, "h", &host);
    
        syslog(LOG_INFO,  "------------------------------------------\n");
        syslog(LOG_INFO, "Host:         %12s", json_object_get_string(host));
        syslog(LOG_INFO, "Version:      %12s", json_object_get_string(version));
        
       ***************** Cut Some Code *************************
        
        MQTTAsync_freeMessage(&message);
        MQTTAsync_free(topicName);
        return 1;
    }
    
    void onDisconnectFailure(void* context, MQTTAsync_failureData* response) {
        syslog(LOG_INFO, "Disconnect failed, rc %d\n", response->code);
        disc_finished = 1;
    }
    
    void onDisconnect(void* context, MQTTAsync_successData* response) {
        syslog(LOG_INFO, "Successful disconnection");
        disc_finished = 1;
    }
    
    void onSubscribe(void* context, MQTTAsync_successData* response) {
        syslog(LOG_INFO, "Subscribe succeeded");
        subscribed = 1;
    }
    
    void onSubscribeFailure(void* context, MQTTAsync_failureData* response) {
        syslog(LOG_INFO, "Subscribe failed, rc %d, %s", response->code, response->message);
        finished = 1;
    }
    
    void onConnectFailure(void* context, MQTTAsync_failureData* response) {
        syslog(LOG_INFO, "Connect failed, rc %d, %s", response->code, response->message);
        //raise(SIGABRT);
        finished = 1;
    }
    
    void onConnect(void* context, MQTTAsync_successData* response) {
        MQTTAsync client = (MQTTAsync) context;
        MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
        int rc;
    
        syslog(LOG_INFO, "Successful connection");
    
        syslog(LOG_INFO, "Subscribing to topic %s for client %s using QoS%d", TOPIC, CLIENTID, QOS);
        opts.onSuccess = onSubscribe;
        opts.onFailure = onSubscribeFailure;
        opts.context = client;
        if ((rc = MQTTAsync_subscribe(client, TOPIC, QOS, &opts)) != MQTTASYNC_SUCCESS) {
            syslog(LOG_INFO, "Failed to start subscribe, return code %d", rc);
            finished = 1;
        }
    }
    
    int main(int argc, char* argv[]) {
        MQTTAsync client;
        MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
        MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
        MQTTAsync_SSLOptions ssl_opts = MQTTAsync_SSLOptions_initializer;
        ssl_opts.enableServerCertAuth = 1;
        conn_opts.ssl = &ssl_opts;        
        
        conn_opts.ssl->trustStore = CA_PATH;
        conn_opts.ssl->privateKey = CLIENT_KEY_PATH;
        conn_opts.ssl->keyStore = CLIENT_CERT_PATH;
        conn_opts.ssl->sslVersion = MQTT_SSL_VERSION_TLS_1_2;
        
        int rc;
        int ch;
    
        setlogmask(LOG_UPTO(LOG_DEBUG));
        openlog(LOG_NAME, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);    
    
       ***************** Cut Some Code *************************
    
        pid_t sid = 0;
        // Create child process
        process_id = fork();
        // Indication of fork() failure
        if (process_id < 0) {
            syslog(LOG_INFO, "Fork Failed");
            exit(EXIT_FAILURE);
        }
        // PARENT PROCESS. Need to kill it.
        if (process_id > 0) {
            syslog(LOG_INFO, "process_id of child process %d \n", process_id);
            exit(EXIT_SUCCESS);
        }
    
        //set new session
        sid = setsid();
        if (sid < 0) {
            exit(SIGABRT);
        }
        // Change the current working directory to root.
        int r = chdir("/");
        // Close stdin. stdout and stderr
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
        syslog(LOG_INFO,  "Done closing standard files");   
        
        syslog(LOG_INFO,"Creating Async Client at URL %s", ADDRESS);
        if ((rc = MQTTAsync_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL))
                != MQTTASYNC_SUCCESS) {
            syslog(LOG_INFO, "Failed to create client, return code %d\n", rc);
            rc = EXIT_FAILURE;
            goto exit;
        }
        syslog(LOG_INFO,"Client done");
        if ((rc = MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL)) != MQTTASYNC_SUCCESS) {
            syslog(LOG_INFO, "Failed to set callbacks, return code %d\n", rc);
            rc = EXIT_FAILURE;
            goto destroy_exit;
        }
    
        syslog(LOG_INFO,"Callbacks done");
        conn_opts.keepAliveInterval = 20;
        conn_opts.cleansession = 1;
        conn_opts.onSuccess = onConnect;
        conn_opts.onFailure = onConnectFailure;
        conn_opts.context = client;
            
        if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS) {
           syslog(LOG_INFO, "Failed to start connect, return code %d\n", rc);
            rc = EXIT_FAILURE;
            goto destroy_exit;
        }
        syslog(LOG_INFO,"Connection done");
        while (!subscribed && !finished)
           usleep(30000L);
    
        if (finished)
            goto exit;
        syslog(LOG_INFO,  "About to start the loop\n");
        do {
            //ch = getchar();
            usleep(2500L);
        } while (1==1 && !finished);
    
        disc_opts.onSuccess = onDisconnect;
        disc_opts.onFailure = onDisconnectFailure;
        if ((rc = MQTTAsync_disconnect(client, &disc_opts)) != MQTTASYNC_SUCCESS) {
            syslog(LOG_INFO, "Failed to start disconnect, return code %d\n", rc);
            rc = EXIT_FAILURE;
            goto destroy_exit;
        }
        while (!disc_finished) {
            usleep(10000L);
        }
    
    destroy_exit:
        syslog(LOG_INFO, "Destroy_Exit"); 
        MQTTAsync_destroy(&client);
    exit:
        syslog(LOG_INFO, "Exiting"); 
    
        close_db();
        closelog();
        return rc;
    }
    


  • @crispyoz thanks a lot again.

    I'll post the result here after executing the above code.


Log in to reply
 

Looks like your connection to Community was lost, please wait while we try to reconnect.