/*******************************************************************************
* 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 = ; void cfinish(int sig)
{
signal(SIGINT, NULL);
toStop = ;
} 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[];
* /** 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, {, NULL}}, {NULL, {, NULL}} }
*
* typedef struct
* {
* /** The eyecatcher for this structure. must be MQTW. */
* char struct_id[];
* /** 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 =
* * data.MQTTVersion = // 4 = 3.1.1
* * data.clientID.len =
* * data.keepAliveInterval =
* * data.cleansession =
* * data.willFlag =
* * data.will.struct_id = MQTW
* * data.will.struct_version =
* * data.will.topicName.len =
* * data.will.message.len =
* * data.will.retained =
* * data.will.qos =
* * data.username.len =
* * data.password.len =
*/
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
int rc = ;
int mysock = ;
unsigned char buf[];
int buflen = sizeof(buf);
int msgid = ;
// #define MQTTString_initializer {NULL, {0, NULL}}
MQTTString topicString = MQTTString_initializer;
int req_qos = ;
char* payload = "mypayload";
int payloadlen = strlen(payload);
int len = ;
char *host = "m2m.eclipse.org";
int port = ; stop_init();
if (argc > )
host = argv[]; if (argc > )
port = atoi(argv[]); // 这里相当于打开一个socket,如果用SIM800C,这里相当于要初始化SIM800C到可以Send数据的程度。
mysock = transport_open(host, port);
if(mysock < )
return mysock; printf("Sending to hostname %s port %d\n", host, port); // 重新设置data中的设置的值
data.clientID.cstring = "me";
data.keepAliveInterval = ;
data.cleansession = ;
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) != || connack_rc != )
{
printf("Unable to connect, return code %d\n", connack_rc);
goto exit;
}
}
else
goto exit; /* subscribe */
topicString.cstring = "substopic";
// 合成订阅数据数组
len = MQTTSerialize_subscribe(buf, buflen, , msgid, , &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, , &subcount, &granted_qos, buf, buflen);
if (granted_qos != )
{
printf("granted qos != 0, %d\n", 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\n", payloadlen_in, payload_in);
} printf("publishing reading\n");
// 正向合成需要发送的数据
len = MQTTSerialize_publish(buf, buflen, , , , , topicString, (unsigned char*)payload, payloadlen);
rc = transport_sendPacketBuffer(mysock, buf, len);
} printf("disconnecting\n");
// 断开TCP连接
len = MQTTSerialize_disconnect(buf, buflen);
rc = transport_sendPacketBuffer(mysock, buf, len); exit:
// SIM800C断开连接
transport_close(mysock); return ;
}

paho.mqtt.embedded-c MQTTPacket pub0sub1.c hacking的更多相关文章

  1. paho.mqtt.embedded-c MQTTPacket transport.c hacking

    /******************************************************************************* * paho.mqtt.embedde ...

  2. Paho - MQTT C Cient的实现

    来自我的CSDN博客   在前几天,我大致了解了一下Paho C项目,并对其的一些内容进行了翻译.俗话说,光说不练假把戏,今天就给大家讲一下使用Paho的客户端库文件实现MQTT C Client的过 ...

  3. [3] MQTT,mosquitto,Eclipse Paho---怎样使用 Eclipse Paho MQTT工具来发送订阅MQTT消息?

    在上两节,笔者主要介绍了 MQTT,mosquitto,Eclipse Paho的基本概念已经怎样安装mosquitto. 在这个章节我们就来看看怎样用 Eclipse Paho MQTT工具来发送接 ...

  4. vc2015编译paho.mqtt.c-1.1.0

    vc2015打开“\paho.mqtt.c-1.1.0\Windows Build\Paho C MQTT APIs.sln” 将文件“\paho.mqtt.c-1.1.0\src\VersionIn ...

  5. paho.mqtt.c打印日志

    mqtt中自身就带有日志系统Log.h和Log.c,这些日志文件是在客户端调用MQTTClient_create函数是初始化的,MQTTClient_create源码如下: int MQTTClien ...

  6. Eclipse Paho MQTT Utility

    下载地址: https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho. ...

  7. 3.MQTT paho

    一.概述 遥测传输 (MQTT) 是轻量级基于代理的发布/订阅的消息传输协议,设计思想是开放.简单.轻量.易于实现.这些特点使它适用于受限环境.例如,但不仅限于此: 网络代价昂贵,带宽低.不可靠. 在 ...

  8. Paho -物联网 MQTT C Cient的实现和详解

    概述   在文章Paho - MQTT C Cient的实现中,我介绍了如何使用Paho开源项目创建MQTTClient_pulish客户端.但只是简单的介绍了使用方法,而且客户端的结果与之前介绍的并 ...

  9. MQTT和paho(一)

    参考链接:http://blog.csdn.net/yangzl2008/article/details/8861069 一.mqtt 1.简单介绍 http://mqtt.org/software ...

随机推荐

  1. selenium-chrome-headless

    #coding=utf-8 from selenium import webdriver import time chrome_options = webdriver.ChromeOptions() ...

  2. python 二叉排序树

    class BSTNode: def __init__(self, data, left=None, right=None): self.data = data self.left = left se ...

  3. MongoDB(课时17 更新函数)

    3.4.3 数据更新操作 MongoDB数据存的是副本数据, 最终的数据还要保存在传统的数据库里,所以如果关系型数据库里数据变了,最好的方法是删除里面的MongoDB数据重新插入. 在MongoDB里 ...

  4. Error updating database. Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: 'as3'

    执行更新时的出错信息 Whitelabel Error Page This application has no explicit mapping for /error, so you are see ...

  5. WAV文件格式解析及处理

    RIFF file format RIFF全称为资源互换文件格式(Resources Interchange File Format),是Windows下大部分多媒体文件遵循的一种文件结构.RIFF文 ...

  6. WCF配置后支持通过URL进行http方式调用

    最近遇到一个小型项目,主要就是通过手机写入NFC信息,思考许久后决定就写一个简单的CS程序来搞定这个问题,可是当涉及到手机和PC通信的时候首先考虑到的就是IIS,同时因为数据库是SQLite,思前想后 ...

  7. canvas和图片之间的互相装换

    canvas和图片之间的互相装换 一.总结 一句话总结:一个是canvas的drawImage方法,一个是canvas的toDataURL方法 canvas drawImage() Image对象 c ...

  8. php 获取自己的公网IP

    <?php $externalContent = file_get_contents('http://checkip.dyndns.com/'); preg_match('/Current IP ...

  9. SpringBoot读取war包jar包Resource资源文件解决办法

    SpringBoot读取war包jar包Resource资源文件解决办法 场景描述 在开发过程中我们经常会碰到要在代码中获取资源文件的情况,而我在最近在SpringBoot项目中时碰到一个问题,就是在 ...

  10. vs2015下通过opencv使用hdf5

    因为使用Kinect SDK编程,又需求高速文件I/O,所以通过opencv接口使用hdf5. (opencv 3.1以上版本,在其Extra Modules中支持hdf5) 一. 环境 OS: Wi ...