paho.mqtt.embedded-c MQTTPacket pub0sub1.c hacking

/*******************************************************************************
 *          paho.mqtt.embedded-c MQTTPacket pub0sub1.c hacking
 * 说明:
 *     跟一下paho.mqtt.embedded-c中的MQTT协议怎么使用,协议大体什么意思。
 * 
 *                                          2017-12-5 深圳 南山平山村 曾剑锋
 ******************************************************************************/

/*******************************************************************************
 * Copyright (c) 2014 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Ian Craggs - initial API and implementation and/or initial documentation
 *    Sergio R. Caprile - clarifications and/or documentation extension
 *******************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "MQTTPacket.h"
#include "transport.h"

/* This is in order to get an asynchronous signal to stop the sample,
as the code loops waiting for msgs on the subscribed topic.
Your actual code will depend on your hw and approach*/
#include <signal.h>

int toStop = 0;

void cfinish(int sig)
{
    signal(SIGINT, NULL);
    toStop = 1;
}

void stop_init(void)
{
    signal(SIGINT, cfinish);
    signal(SIGTERM, cfinish);
}
/* */

int main(int argc, char *argv[])
{
    /**
     * typedef struct
     * {
     *     /** The eyecatcher for this structure.  must be MQTC. */
     *     char struct_id[4];
     *     /** The version number of this structure.  Must be 0 */
     *     int struct_version;
     *     /** Version of MQTT to be used.  3 = 3.1 4 = 3.1.1
     *       */
     *     unsigned char MQTTVersion;
     *     MQTTString clientID;
     *     unsigned short keepAliveInterval;
     *     unsigned char cleansession;
     *     unsigned char willFlag;
     *     MQTTPacket_willOptions will;
     *     MQTTString username;
     *     MQTTString password;
     * } MQTTPacket_connectData;
     * 
     * #define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, 
     *     MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
     * 
     * typedef struct
     * {
     *     /** The eyecatcher for this structure.  must be MQTW. */
     *     char struct_id[4];
     *     /** The version number of this structure.  Must be 0 */
     *     int struct_version;
     *     /** The LWT topic to which the LWT message will be published. */
     *     MQTTString topicName;
     *     /** The LWT payload. */
     *     MQTTString message;
     *     /**
     *       * The retained flag for the LWT message (see MQTTAsync_message.retained).
     *       */
     *     unsigned char retained;
     *     /**
     *       * The quality of service setting for the LWT message (see
     *       * MQTTAsync_message.qos and @ref qos).
     *       */
     *     char qos;
     * } MQTTPacket_willOptions;
     *
     * #define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }
     * typedef struct
     * {
     *     char* cstring;
     *     MQTTLenString lenstring;
     * } MQTTString;
     *
     * typedef struct
       * {
       *     int len;
       *     char* data;
       * } MQTTLenString;
     *
     * data:
     *     * data.struct_id = MQTC
     *     * data.struct_version = 0
     *     * data.MQTTVersion = 4    // 4 = 3.1.1
     *     * data.clientID.len = 0
     *     * data.keepAliveInterval = 60
     *     * data.cleansession = 1
     *     * data.willFlag = 0
     *     * data.will.struct_id = MQTW
     *     * data.will.struct_version = 0
     *     * data.will.topicName.len = 0
     *     * data.will.message.len = 0
     *     * data.will.retained = 0
     *     * data.will.qos = 0
     *     * data.username.len = 0
     *     * data.password.len = 0
     */
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    int rc = 0;
    int mysock = 0;
    unsigned char buf[200];
    int buflen = sizeof(buf);
    int msgid = 1;
    // #define MQTTString_initializer {NULL, {0, NULL}}
    MQTTString topicString = MQTTString_initializer;
    int req_qos = 0;
    char* payload = "mypayload";
    int payloadlen = strlen(payload);
    int len = 0;
    char *host = "m2m.eclipse.org";
    int port = 1883;

    stop_init();
    if (argc > 1)
        host = argv[1];

    if (argc > 2)
        port = atoi(argv[2]);

    // 这里相当于打开一个socket,如果用SIM800C,这里相当于要初始化SIM800C到可以Send数据的程度。
    mysock = transport_open(host, port);
    if(mysock < 0)
        return mysock;

    printf("Sending to hostname %s port %d
", host, port);

    // 重新设置data中的设置的值
    data.clientID.cstring = "me";
    data.keepAliveInterval = 20;
    data.cleansession = 1;
    data.username.cstring = "testuser";
    data.password.cstring = "testpassword";

    // 这个函数主要工作就是将data中的数据,依照MQTT协议,将数据转换成buf数据数组,便于transport_sendPacketBuffer进行发送
    len = MQTTSerialize_connect(buf, buflen, &data);
    // 开始传送buf中的数据
    rc = transport_sendPacketBuffer(mysock, buf, len);

    /* wait for connack */
    // MQTTPacket_read会回调transport_getdata函数,从而获取返回值
    if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
    {
        unsigned char sessionPresent, connack_rc;

        if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0)
        {
            printf("Unable to connect, return code %d
", connack_rc);
            goto exit;
        }
    }
    else
        goto exit;

    /* subscribe */
    topicString.cstring = "substopic";
    // 合成订阅数据数组
    len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos);
    // 发送合成的订阅数据数组
    rc = transport_sendPacketBuffer(mysock, buf, len);
    if (MQTTPacket_read(buf, buflen, transport_getdata) == SUBACK)     /* wait for suback */
    {
        unsigned short submsgid;
        int subcount;
        int granted_qos;

        rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen);
        if (granted_qos != 0)
        {
            printf("granted qos != 0, %d
", granted_qos);
            goto exit;
        }
    }
    else
        goto exit;

    /* loop getting msgs on subscribed topic */
    topicString.cstring = "pubtopic";
    while (!toStop)
    {
        /* transport_getdata() has a built-in 1 second timeout,
        your mileage will vary */
        // 收到请求发送数据
        if (MQTTPacket_read(buf, buflen, transport_getdata) == PUBLISH)
        {
            unsigned char dup;
            int qos;
            unsigned char retained;
            unsigned short msgid;
            int payloadlen_in;
            unsigned char* payload_in;
            int rc;
            MQTTString receivedTopic;

            // 逆向解析输出收到的数据,deserialize、serialize,这两个单词表意很清楚
            rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic,
                    &payload_in, &payloadlen_in, buf, buflen);
            printf("message arrived %.*s
", payloadlen_in, payload_in);
        }

        printf("publishing reading
");
        // 正向合成需要发送的数据
        len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, (unsigned char*)payload, payloadlen);
        rc = transport_sendPacketBuffer(mysock, buf, len);
    }

    printf("disconnecting
");
    // 断开TCP连接
    len = MQTTSerialize_disconnect(buf, buflen);
    rc = transport_sendPacketBuffer(mysock, buf, len);

exit:
    // SIM800C断开连接
    transport_close(mysock);

    return 0;
}
原文地址:https://www.cnblogs.com/zengjfgit/p/7988880.html