mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-15 16:36:52 +01:00
Add a new sensor network Bluetooth RFCOMM
Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
14
MQTTSNPacket/samples/linux/rfcomm/build
Normal file
14
MQTTSNPacket/samples/linux/rfcomm/build
Normal file
@@ -0,0 +1,14 @@
|
||||
rm -rf bin
|
||||
mkdir bin
|
||||
cd bin
|
||||
|
||||
PKTSRC=../../../../src
|
||||
SRC=..
|
||||
gcc -Wall -c $SRC/rfcomm.c -Os -s
|
||||
gcc -Wall $SRC/qos0pub.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNPacket.c $PKTSRC/MQTTSNConnectClient.c -o qos0pub -Os -s
|
||||
gcc -Wall $SRC/qos0pub_register.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNDeserializePublish.c $PKTSRC/MQTTSNPacket.c $PKTSRC/MQTTSNConnectClient.c -o qos0pub_register -Os -s
|
||||
gcc -Wall $SRC/qos-1pub.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNPacket.c -o qos-1pub -Os -s
|
||||
gcc -Wall $SRC/qos-1pub_extended.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNPacket.c -o qos-1pub_extended -Os -s
|
||||
gcc -Wall $SRC/qos1pub.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNDeserializePublish.c $PKTSRC/MQTTSNPacket.c $PKTSRC/MQTTSNConnectClient.c -o qos1pub -Os -s
|
||||
gcc -Wall $SRC/pub0sub1.c rfcomm.o -lbluetooth -I $PKTSRC $PKTSRC/MQTTSNSerializePublish.c $PKTSRC/MQTTSNDeserializePublish.c $PKTSRC/MQTTSNPacket.c $PKTSRC/MQTTSNConnectClient.c $PKTSRC/MQTTSNSubscribeClient.c -o pub0sub1 -Os -s
|
||||
rm rfcomm.o
|
||||
164
MQTTSNPacket/samples/linux/rfcomm/pub0sub1.c
Normal file
164
MQTTSNPacket/samples/linux/rfcomm/pub0sub1.c
Normal file
@@ -0,0 +1,164 @@
|
||||
/*******************************************************************************
|
||||
* 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:
|
||||
* Normal topic name is automatically registered at subscription, then
|
||||
* a message is published and the node receives it itself
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "MQTTSNPacket.h"
|
||||
#include "rfcomm.h"
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int rc = 0;
|
||||
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;
|
||||
unsigned char dup = 0;
|
||||
int qos = 1;
|
||||
unsigned char retained = 0;
|
||||
short packetid = 1;
|
||||
char *topicname = "a long topic name";
|
||||
char *host = "";
|
||||
int channel = 1;
|
||||
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
|
||||
unsigned short topicid;
|
||||
|
||||
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)
|
||||
{
|
||||
goto exit;
|
||||
}
|
||||
|
||||
options.clientID.cstring = "pub0sub1 MQTT-SN";
|
||||
len = MQTTSNSerialize_connect(buf, buflen, &options);
|
||||
rc = rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
/* wait for connack */
|
||||
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_CONNACK)
|
||||
{
|
||||
int connack_rc = -1;
|
||||
|
||||
if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
|
||||
{
|
||||
printf("Unable to connect, return code %d\n", connack_rc);
|
||||
goto exit;
|
||||
}
|
||||
else
|
||||
printf("connected rc %d\n", connack_rc);
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
|
||||
/* subscribe */
|
||||
printf("Subscribing\n");
|
||||
topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
topic.data.long_.name = topicname;
|
||||
topic.data.long_.len = strlen(topic.data.long_.name);
|
||||
len = MQTTSNSerialize_subscribe(buf, buflen, 0, 2, packetid, &topic);
|
||||
rc = rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_SUBACK) /* wait for suback */
|
||||
{
|
||||
unsigned short submsgid;
|
||||
int granted_qos;
|
||||
unsigned char returncode;
|
||||
|
||||
rc = MQTTSNDeserialize_suback(&granted_qos, &topicid, &submsgid, &returncode, buf, buflen);
|
||||
if (granted_qos != 2 || returncode != 0)
|
||||
{
|
||||
printf("granted qos != 2, %d return code %d\n", granted_qos, returncode);
|
||||
goto exit;
|
||||
}
|
||||
else
|
||||
printf("suback topic id %d\n", topicid);
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
printf("Publishing\n");
|
||||
/* publish with short name */
|
||||
topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
topic.data.id = topicid;
|
||||
++packetid;
|
||||
len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
|
||||
topic, payload, payloadlen);
|
||||
rc = rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
/* wait for puback */
|
||||
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_PUBACK)
|
||||
{
|
||||
unsigned short packet_id, topic_id;
|
||||
unsigned char returncode;
|
||||
|
||||
if (MQTTSNDeserialize_puback(&topic_id, &packet_id, &returncode, buf, buflen) != 1 || returncode != MQTTSN_RC_ACCEPTED)
|
||||
printf("Unable to publish, return code %d\n", returncode);
|
||||
else
|
||||
printf("puback received, msgid %d topic id %d\n", packet_id, topic_id);
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
printf("Receive publish\n");
|
||||
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_PUBLISH)
|
||||
{
|
||||
unsigned short packet_id;
|
||||
int qos, payloadlen;
|
||||
unsigned char* payload;
|
||||
unsigned char dup, retained;
|
||||
MQTTSN_topicid pubtopic;
|
||||
|
||||
if (MQTTSNDeserialize_publish(&dup, &qos, &retained, &packet_id, &pubtopic,
|
||||
&payload, &payloadlen, buf, buflen) != 1)
|
||||
printf("Error deserializing publish\n");
|
||||
else
|
||||
printf("publish received, id %d qos %d\n", packet_id, qos);
|
||||
|
||||
if (qos == 1)
|
||||
{
|
||||
len = MQTTSNSerialize_puback(buf, buflen, pubtopic.data.id, packet_id, MQTTSN_RC_ACCEPTED);
|
||||
rc = rfcomm_sendPacketBuffer(buf, len);
|
||||
if (rc == 0)
|
||||
printf("puback sent\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
len = MQTTSNSerialize_disconnect(buf, buflen, 0);
|
||||
rc = rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
exit:
|
||||
rfcomm_close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
68
MQTTSNPacket/samples/linux/rfcomm/qos-1pub.c
Normal file
68
MQTTSNPacket/samples/linux/rfcomm/qos-1pub.c
Normal file
@@ -0,0 +1,68 @@
|
||||
/*******************************************************************************
|
||||
* 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;
|
||||
}
|
||||
70
MQTTSNPacket/samples/linux/rfcomm/qos-1pub_extended.c
Normal file
70
MQTTSNPacket/samples/linux/rfcomm/qos-1pub_extended.c
Normal file
@@ -0,0 +1,70 @@
|
||||
/*******************************************************************************
|
||||
* 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:
|
||||
* Extension to the specs in which a node can send a normal (long) topic name inside the
|
||||
* payload area to avoid the registration process and the usage of short/predefined types
|
||||
*******************************************************************************/
|
||||
|
||||
#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 *topicname = "a long topic name";
|
||||
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;;
|
||||
}
|
||||
|
||||
topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
topic.data.long_.name = topicname;
|
||||
topic.data.long_.len = strlen(topicname);
|
||||
|
||||
len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
|
||||
topic, payload, payloadlen);
|
||||
|
||||
rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
rfcomm_close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
93
MQTTSNPacket/samples/linux/rfcomm/qos0pub.c
Normal file
93
MQTTSNPacket/samples/linux/rfcomm/qos0pub.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/*******************************************************************************
|
||||
* 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:
|
||||
* 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)
|
||||
{
|
||||
int rc = 0;
|
||||
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 = 0;
|
||||
int retained = 0;
|
||||
short packetid = 0;
|
||||
// char *topicname = "a long topic name";
|
||||
char *host = "";
|
||||
int channel = 1;
|
||||
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
|
||||
|
||||
if (argc > 1)
|
||||
host = argv[1];
|
||||
|
||||
if (argc > 2)
|
||||
channel = atoi(argv[2]);
|
||||
|
||||
printf("Sending to address %s port %d\n", host, channel);
|
||||
if (rfcomm_open(host, channel) < 0)
|
||||
{
|
||||
goto exit;
|
||||
}
|
||||
|
||||
options.clientID.cstring = "myclientid";
|
||||
len = MQTTSNSerialize_connect(buf, buflen, &options);
|
||||
rc = rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
/* wait for connack */
|
||||
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_CONNACK)
|
||||
{
|
||||
int connack_rc = -1;
|
||||
|
||||
if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
|
||||
{
|
||||
printf("Unable to connect, return code %d\n", connack_rc);
|
||||
goto exit;
|
||||
}
|
||||
else
|
||||
printf("connected rc %d\n", connack_rc);
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
|
||||
/* 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);
|
||||
rc = rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
printf("rc %d from send packet for publish length %d\n", rc, len);
|
||||
|
||||
exit:
|
||||
rfcomm_close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
120
MQTTSNPacket/samples/linux/rfcomm/qos0pub_register.c
Normal file
120
MQTTSNPacket/samples/linux/rfcomm/qos0pub_register.c
Normal file
@@ -0,0 +1,120 @@
|
||||
/*******************************************************************************
|
||||
* 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:
|
||||
* Normal topic name used to show registration process
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "MQTTSNPacket.h"
|
||||
#include "rfcomm.h"
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int rc = 0;
|
||||
unsigned char buf[200];
|
||||
int buflen = sizeof(buf);
|
||||
MQTTSN_topicid topic;
|
||||
MQTTSNString topicstr;
|
||||
unsigned char* payload = (unsigned char*)"mypayload";
|
||||
int payloadlen = strlen((char*)payload);
|
||||
int len = 0;
|
||||
int dup = 0;
|
||||
int qos = 0;
|
||||
int retained = 0;
|
||||
short packetid = 0;
|
||||
char *topicname = "a long topic name";
|
||||
char *host = "";
|
||||
int channel = 1;
|
||||
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
|
||||
unsigned short topicid;
|
||||
|
||||
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, (unsigned char) channel) < 0)
|
||||
{
|
||||
goto exit;
|
||||
}
|
||||
|
||||
options.clientID.cstring = "myclientid";
|
||||
len = MQTTSNSerialize_connect(buf, buflen, &options);
|
||||
rc = rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
/* wait for connack */
|
||||
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_CONNACK)
|
||||
{
|
||||
int connack_rc = -1;
|
||||
|
||||
if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
|
||||
{
|
||||
printf("Unable to connect, return code %d\n", connack_rc);
|
||||
goto exit;
|
||||
}
|
||||
else
|
||||
printf("connected rc %d\n", connack_rc);
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
/* register topic name */
|
||||
printf("Registering\n");
|
||||
topicstr.cstring = topicname;
|
||||
topicstr.lenstring.len = strlen(topicname);
|
||||
len = MQTTSNSerialize_register(buf, buflen, 0, packetid, &topicstr);
|
||||
rc = rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_REGACK) /* wait for regack */
|
||||
{
|
||||
unsigned short submsgid;
|
||||
unsigned char returncode;
|
||||
|
||||
rc = MQTTSNDeserialize_regack(&topicid, &submsgid, &returncode, buf, buflen);
|
||||
if (returncode != 0)
|
||||
{
|
||||
printf("return code %d\n", returncode);
|
||||
goto exit;
|
||||
}
|
||||
else
|
||||
printf("regack topic id %d\n", topicid);
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
/* publish with obtained id */
|
||||
printf("Publishing\n");
|
||||
topic.type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
topic.data.id = topicid;
|
||||
++packetid;
|
||||
len = MQTTSNSerialize_publish(buf, buflen, dup, qos, retained, packetid,
|
||||
topic, payload, payloadlen);
|
||||
rc = rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
printf("rc %d from send packet for publish length %d\n", rc, len);
|
||||
|
||||
exit:
|
||||
rfcomm_close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
105
MQTTSNPacket/samples/linux/rfcomm/qos1pub.c
Normal file
105
MQTTSNPacket/samples/linux/rfcomm/qos1pub.c
Normal file
@@ -0,0 +1,105 @@
|
||||
/*******************************************************************************
|
||||
* 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:
|
||||
* 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 = 1;
|
||||
int retained = 0;
|
||||
short packetid = 1;
|
||||
char *host = "";
|
||||
int channel = 1;
|
||||
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
|
||||
|
||||
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)
|
||||
{
|
||||
goto exit;
|
||||
}
|
||||
|
||||
options.clientID.cstring = "myclientid";
|
||||
len = MQTTSNSerialize_connect(buf, buflen, &options);
|
||||
rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
/* wait for connack */
|
||||
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_CONNACK)
|
||||
{
|
||||
int connack_rc = -1;
|
||||
|
||||
if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
|
||||
{
|
||||
printf("Unable to connect, return code %d\n", connack_rc);
|
||||
goto exit;
|
||||
}
|
||||
else
|
||||
printf("connected rc %d\n", connack_rc);
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
/* publish with short name */
|
||||
topic.type = MQTTSN_TOPIC_TYPE_SHORT;
|
||||
memcpy(topic.data.short_name, "tt", 2);
|
||||
len = MQTTSNSerialize_publish(buf, buflen - len, dup, qos, retained, packetid,
|
||||
topic, payload, payloadlen);
|
||||
rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
/* wait for puback */
|
||||
if (MQTTSNPacket_read(buf, buflen, rfcomm_getdata) == MQTTSN_PUBACK)
|
||||
{
|
||||
unsigned short packet_id, topic_id;
|
||||
unsigned char returncode;
|
||||
|
||||
if (MQTTSNDeserialize_puback(&topic_id, &packet_id, &returncode, buf, buflen) != 1 || returncode != MQTTSN_RC_ACCEPTED)
|
||||
printf("Unable to publish, return code %d\n", returncode);
|
||||
else
|
||||
printf("puback received, id %d\n", packet_id);
|
||||
}
|
||||
else
|
||||
goto exit;
|
||||
|
||||
len = MQTTSNSerialize_disconnect(buf, buflen, 0);
|
||||
rfcomm_sendPacketBuffer(buf, len);
|
||||
|
||||
exit:
|
||||
rfcomm_close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
115
MQTTSNPacket/samples/linux/rfcomm/rfcomm.c
Normal file
115
MQTTSNPacket/samples/linux/rfcomm/rfcomm.c
Normal file
@@ -0,0 +1,115 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2021 tomoaki@tomy-tech.com
|
||||
*
|
||||
* 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:
|
||||
* Tomoaki Yamaguchi - initial implementation
|
||||
*******************************************************************************/
|
||||
#if defined(WIN32) || defined(__APP__)
|
||||
#error "Only available on Linux."
|
||||
#endif
|
||||
|
||||
|
||||
#if !defined(SOCKET_ERROR)
|
||||
/** error in socket operation */
|
||||
#define SOCKET_ERROR -1
|
||||
#endif
|
||||
|
||||
#define INVALID_SOCKET SOCKET_ERROR
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
#include <bluetooth/rfcomm.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include "rfcomm.h"
|
||||
|
||||
static int mysock = INVALID_SOCKET;
|
||||
|
||||
int Socket_error(char* aString, int sock)
|
||||
{
|
||||
if (errno != EINTR && errno != EAGAIN && errno != EINPROGRESS && errno != EWOULDBLOCK)
|
||||
{
|
||||
if (strcmp(aString, "shutdown") != 0 || (errno != ENOTCONN && errno != ECONNRESET))
|
||||
{
|
||||
int orig_errno = errno;
|
||||
char* errmsg = strerror(errno);
|
||||
|
||||
printf("Socket error %d (%s) in %s for socket %d\n", orig_errno, errmsg, aString, sock);
|
||||
}
|
||||
}
|
||||
return -errno;
|
||||
}
|
||||
|
||||
|
||||
int rfcomm_sendPacketBuffer(unsigned char* buf, int buflen)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if ((rc = write(mysock, buf, buflen)) == SOCKET_ERROR)
|
||||
{
|
||||
Socket_error("sendto", mysock);
|
||||
}
|
||||
else
|
||||
{
|
||||
rc = 0;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
int rfcomm_getdata(unsigned char* buf, int count)
|
||||
{
|
||||
int rc = recv(mysock, buf, count, 0);
|
||||
printf("received %d bytes count %d\n", rc, (int) count);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
return >=0 for a socket descriptor, <0 for an error code
|
||||
*/
|
||||
int rfcomm_open(char* addr, unsigned char channel)
|
||||
{
|
||||
const int reuse = 1;
|
||||
|
||||
mysock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
|
||||
if (mysock == INVALID_SOCKET)
|
||||
return Socket_error("socket", mysock);
|
||||
|
||||
setsockopt(mysock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
|
||||
|
||||
struct sockaddr_rc addru = { 0 };
|
||||
addru.rc_family = AF_BLUETOOTH;
|
||||
addru.rc_channel = channel;
|
||||
str2ba(addr, &addru.rc_bdaddr);
|
||||
|
||||
// connect to server
|
||||
errno = 0;
|
||||
if (connect(mysock, (struct sockaddr *) &addru, sizeof(addru)) < 0)
|
||||
{
|
||||
rfcomm_close();
|
||||
return Socket_error("connect", mysock);
|
||||
}
|
||||
return mysock;
|
||||
}
|
||||
|
||||
int rfcomm_close()
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = shutdown(mysock, SHUT_WR);
|
||||
rc = close(mysock);
|
||||
mysock = INVALID_SOCKET;
|
||||
return rc;
|
||||
}
|
||||
21
MQTTSNPacket/samples/linux/rfcomm/rfcomm.h
Normal file
21
MQTTSNPacket/samples/linux/rfcomm/rfcomm.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/*******************************************************************************
|
||||
* 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 - "commonalization" from prior samples and/or documentation extension
|
||||
*******************************************************************************/
|
||||
|
||||
int rfcom_sendPacketBuffer(unsigned char* buf, int buflen);
|
||||
int rfcomm_getdata(unsigned char* buf, int count);
|
||||
int rfcomm_open(char* address, unsigned char channel);
|
||||
int rfcomm_close(void);
|
||||
Reference in New Issue
Block a user