mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-16 08:56:51 +01:00
Author: Sergio R. Caprile <scaprile@gmail.com>
moved all common socket support functions to a new file (transport). added new samples to show specific actions. Signed-off-by: Sergio R. Caprile <scaprile@gmail.com>
This commit is contained in:
@@ -12,111 +12,25 @@
|
||||
*
|
||||
* 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 "MQTTSNPacket.h"
|
||||
|
||||
#if !defined(SOCKET_ERROR)
|
||||
/** error in socket operation */
|
||||
#define SOCKET_ERROR -1
|
||||
#endif
|
||||
|
||||
#if defined(WIN32)
|
||||
/* default on Windows is 64 - increase to make Linux and Windows the same */
|
||||
#define FD_SETSIZE 1024
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#define MAXHOSTNAMELEN 256
|
||||
#define EAGAIN WSAEWOULDBLOCK
|
||||
#define EINTR WSAEINTR
|
||||
#define EINVAL WSAEINVAL
|
||||
#define EINPROGRESS WSAEINPROGRESS
|
||||
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||
#define ENOTCONN WSAENOTCONN
|
||||
#define ECONNRESET WSAECONNRESET
|
||||
#define ioctl ioctlsocket
|
||||
#define socklen_t int
|
||||
#else
|
||||
#define INVALID_SOCKET SOCKET_ERROR
|
||||
#include <sys/socket.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/time.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#if defined(WIN32)
|
||||
#include <Iphlpapi.h>
|
||||
#else
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
#endif
|
||||
|
||||
|
||||
int Socket_error(char* aString, int sock)
|
||||
{
|
||||
#if defined(WIN32)
|
||||
int errno;
|
||||
#endif
|
||||
|
||||
#if defined(WIN32)
|
||||
errno = WSAGetLastError();
|
||||
#endif
|
||||
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 sendPacketBuffer(int asocket, char* host, int port, unsigned char* buf, int buflen)
|
||||
{
|
||||
struct sockaddr_in cliaddr;
|
||||
int rc = 0;
|
||||
|
||||
memset(&cliaddr, 0, sizeof(cliaddr));
|
||||
cliaddr.sin_family = AF_INET;
|
||||
cliaddr.sin_addr.s_addr = inet_addr(host);
|
||||
cliaddr.sin_port = htons(port);
|
||||
|
||||
if ((rc = sendto(asocket, buf, buflen, 0, (const struct sockaddr*)&cliaddr, sizeof(cliaddr))) == SOCKET_ERROR)
|
||||
Socket_error("sendto", asocket);
|
||||
else if (rc == buflen)
|
||||
rc = 0;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
int mysock = 0;
|
||||
char *host = "127.0.0.1";
|
||||
int port = 1884;
|
||||
|
||||
int getdata(unsigned char* buf, int count)
|
||||
{
|
||||
int rc = recvfrom(mysock, buf, count, 0, NULL, NULL);
|
||||
printf("received %d bytes count %d\n", rc, (int)count);
|
||||
return rc;
|
||||
}
|
||||
#include "MQTTSNPacket.h"
|
||||
#include "transport.h"
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int rc = 0;
|
||||
int mysock;
|
||||
unsigned char buf[200];
|
||||
int buflen = sizeof(buf);
|
||||
MQTTSN_topicid topic;
|
||||
@@ -128,9 +42,15 @@ int main(int argc, char** argv)
|
||||
unsigned char retained = 0;
|
||||
short packetid = 1;
|
||||
char *topicname = "a long topic name";
|
||||
char *host = "127.0.0.1";
|
||||
int port = 1883;
|
||||
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
|
||||
unsigned short topicid;
|
||||
|
||||
mysock = transport_open();
|
||||
if(mysock < 0)
|
||||
return mysock;
|
||||
|
||||
if (argc > 1)
|
||||
host = argv[1];
|
||||
|
||||
@@ -139,16 +59,12 @@ int main(int argc, char** argv)
|
||||
|
||||
printf("Sending to hostname %s port %d\n", host, port);
|
||||
|
||||
mysock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (mysock == INVALID_SOCKET)
|
||||
rc = Socket_error("socket", mysock);
|
||||
|
||||
options.clientID.cstring = "pub0sub1 MQTT-SN";
|
||||
len = MQTTSNSerialize_connect(buf, buflen, &options);
|
||||
rc = sendPacketBuffer(mysock, host, port, buf, len);
|
||||
rc = transport_sendPacketBuffer(host, port, buf, len);
|
||||
|
||||
/* wait for connack */
|
||||
if (MQTTSNPacket_read(buf, buflen, getdata) == MQTTSN_CONNACK)
|
||||
if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
|
||||
{
|
||||
int connack_rc = -1;
|
||||
|
||||
@@ -169,10 +85,10 @@ int main(int argc, char** argv)
|
||||
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, /*msgid*/ 1, &topic);
|
||||
rc = sendPacketBuffer(mysock, host, port, buf, len);
|
||||
len = MQTTSNSerialize_subscribe(buf, buflen, 0, 2, packetid, &topic);
|
||||
rc = transport_sendPacketBuffer(host, port, buf, len);
|
||||
|
||||
if (MQTTSNPacket_read(buf, buflen, getdata) == MQTTSN_SUBACK) /* wait for suback */
|
||||
if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_SUBACK) /* wait for suback */
|
||||
{
|
||||
unsigned short submsgid;
|
||||
int granted_qos;
|
||||
@@ -194,12 +110,13 @@ int main(int argc, char** argv)
|
||||
/* 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 = sendPacketBuffer(mysock, host, port, buf, len);
|
||||
rc = transport_sendPacketBuffer(host, port, buf, len);
|
||||
|
||||
/* wait for puback */
|
||||
if (MQTTSNPacket_read(buf, buflen, getdata) == MQTTSN_PUBACK)
|
||||
if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_PUBACK)
|
||||
{
|
||||
unsigned short packet_id, topic_id;
|
||||
unsigned char returncode;
|
||||
@@ -213,7 +130,7 @@ int main(int argc, char** argv)
|
||||
goto exit;
|
||||
|
||||
printf("Receive publish\n");
|
||||
if (MQTTSNPacket_read(buf, buflen, getdata) == MQTTSN_PUBLISH)
|
||||
if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_PUBLISH)
|
||||
{
|
||||
unsigned short packet_id;
|
||||
int qos, payloadlen;
|
||||
@@ -230,7 +147,7 @@ int main(int argc, char** argv)
|
||||
if (qos == 1)
|
||||
{
|
||||
len = MQTTSNSerialize_puback(buf, buflen, pubtopic.data.id, packet_id, MQTTSN_RC_ACCEPTED);
|
||||
rc = sendPacketBuffer(mysock, host, port, buf, len);
|
||||
rc = transport_sendPacketBuffer(host, port, buf, len);
|
||||
if (rc == 0)
|
||||
printf("puback sent\n");
|
||||
}
|
||||
@@ -239,11 +156,10 @@ int main(int argc, char** argv)
|
||||
goto exit;
|
||||
|
||||
len = MQTTSNSerialize_disconnect(buf, buflen, 0);
|
||||
rc = sendPacketBuffer(mysock, host, port, buf, len);
|
||||
rc = transport_sendPacketBuffer(host, port, buf, len);
|
||||
|
||||
exit:
|
||||
rc = shutdown(mysock, SHUT_WR);
|
||||
rc = close(mysock);
|
||||
transport_close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user