Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers qos-1pub.c Source File

qos-1pub.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  * A qos -1 message can be sent without connecting
00019  * Short topic name used to avoid registration process
00020  *******************************************************************************/
00021 
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <stdlib.h>
00025 
00026 #include "MQTTSNPacket.h"
00027 #include "lowlevel.h"
00028 
00029 
00030 int main(int argc, char** argv)
00031 {
00032     int rc = 0;
00033     int mysock;
00034     unsigned char buf[200];
00035     int buflen = sizeof(buf);
00036     MQTTSN_topicid topic;
00037     unsigned char* payload = (unsigned char*)"mypayload";
00038     int payloadlen = strlen((char*)payload);
00039     int len = 0;
00040     int dup = 0;
00041     int qos = 3;
00042     int retained = 0;
00043     short packetid = 0;
00044     char *host = "127.0.0.1";
00045     int port = 1883;
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     /* publish with short name */
00060     topic.type = MQTTSN_TOPIC_TYPE_SHORT;
00061     memcpy(topic.data.short_name, "tt", 2);
00062     len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
00063             topic, payload, payloadlen);
00064 
00065     rc = lowlevel_sendPacketBuffer(host, port, buf, len);
00066 
00067     lowlevel_close();
00068 
00069     return 0;
00070 }