Files
paho.mqtt-sn.embedded-c/MQTTSNPacket/samples/linux/rfcomm/qos-1pub.c
2021-06-07 18:00:02 +09:00

69 lines
1.8 KiB
C

/*******************************************************************************
* 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
*
* Description:
* A qos -1 message can be sent without connecting
* Short topic name used to avoid registration process
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MQTTSNPacket.h"
#include "rfcomm.h"
int main(int argc, char** argv)
{
unsigned char buf[200];
int buflen = sizeof(buf);
MQTTSN_topicid topic;
unsigned char* payload = (unsigned char*)"mypayload";
int payloadlen = strlen((char*)payload);
int len = 0;
int dup = 0;
int qos = 3;
int retained = 0;
short packetid = 0;
char *host = "";
int channel = 1;
if (argc > 1)
host = argv[1];
if (argc > 2)
channel = atoi(argv[2]);
printf("Sending to address %s channel %d\n", host, channel);
if (rfcomm_open(host, channel) < 0)
{
return -1;
}
/* publish with short name */
topic.type = MQTTSN_TOPIC_TYPE_SHORT;
memcpy(topic.data.short_name, "tt", 2);
len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
topic, payload, payloadlen);
rfcomm_sendPacketBuffer(buf, len);
rfcomm_close();
return 0;
}