Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers qos1pub.c Source File

qos1pub.c

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corp.
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Ian Craggs - initial API and implementation and/or initial documentation
00015  *    Sergio R. Caprile - clarifications and/or documentation extension
00016  *
00017  * Description:
00018  * Short topic name used to avoid registration process
00019  *******************************************************************************/
00020 
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <stdlib.h>
00024 
00025 #include "MQTTSNPacket.h"
00026 #include "lowlevel.h"
00027 
00028 
00029 int main(int argc, char** argv)
00030 {
00031     int rc = 0;
00032     int mysock;
00033     unsigned char buf[200];
00034     int buflen = sizeof(buf);
00035     MQTTSN_topicid topic;
00036     unsigned char* payload = (unsigned char*)"mypayload";
00037     int payloadlen = strlen((char*)payload);
00038     int len = 0;
00039     int dup = 0;
00040     int qos = 1;
00041     int retained = 0;
00042     short packetid = 1;
00043     char *host = "127.0.0.1";
00044     int port = 1883;
00045     MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
00046 
00047     mysock = lowlevel_open();
00048     if(mysock < 0)
00049         return mysock;
00050 
00051     if (argc > 1)
00052         host = argv[1];
00053 
00054     if (argc > 2)
00055         port = atoi(argv[2]);
00056 
00057     printf("Sending to hostname %s port %d\n", host, port);
00058 
00059     options.clientID.cstring = "myclientid";
00060     len = MQTTSNSerialize_connect(buf, buflen, &options);
00061     rc = lowlevel_sendPacketBuffer(host, port, buf, len);
00062 
00063     /* wait for connack */
00064     if (MQTTSNPacket_read(buf, buflen, lowlevel_getdata) == MQTTSN_CONNACK)
00065     {
00066         int connack_rc = -1;
00067 
00068         if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
00069         {
00070             printf("Unable to connect, return code %d\n", connack_rc);
00071             goto exit;
00072         }
00073         else 
00074             printf("connected rc %d\n", connack_rc);
00075     }
00076     else
00077         goto exit;
00078 
00079     /* publish with short name */
00080     topic.type = MQTTSN_TOPIC_TYPE_SHORT;
00081     memcpy(topic.data.short_name, "tt", 2);
00082     len = MQTTSNSerialize_publish(buf, buflen - len, dup, qos, retained, packetid,
00083             topic, payload, payloadlen);
00084     rc = lowlevel_sendPacketBuffer(host, port, buf, len);
00085 
00086     /* wait for puback */
00087     if (MQTTSNPacket_read(buf, buflen, lowlevel_getdata) == MQTTSN_PUBACK)
00088     {
00089         unsigned short packet_id, topic_id;
00090         unsigned char returncode;
00091 
00092         if (MQTTSNDeserialize_puback(&topic_id, &packet_id, &returncode, buf, buflen) != 1 || returncode != MQTTSN_RC_ACCEPTED)
00093             printf("Unable to publish, return code %d\n", returncode);
00094         else 
00095             printf("puback received, id %d\n", packet_id);
00096     }
00097     else
00098         goto exit;
00099 
00100     len = MQTTSNSerialize_disconnect(buf, buflen, 0);
00101     rc = lowlevel_sendPacketBuffer(host, port, buf, len);
00102 
00103 exit:
00104     lowlevel_close();
00105 
00106     return 0;
00107 }