diff --git a/samples/build b/samples/build new file mode 100644 index 0000000..82f8e8a --- /dev/null +++ b/samples/build @@ -0,0 +1,2 @@ +gcc -Wall qos0pub.c -I ../src ../src/MQTTSNSerializePublish.c ../src/MQTTSNPacket.c ../src/MQTTSNConnectClient.c -o qos0pub -Os -s +gcc -Wall qos-1pub.c -I ../src ../src/MQTTSNSerializePublish.c ../src/MQTTSNPacket.c -o qos-1pub -Os -s diff --git a/samples/qos0pub.c b/samples/qos0pub.c new file mode 100644 index 0000000..77a07dd --- /dev/null +++ b/samples/qos0pub.c @@ -0,0 +1,152 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ + +#include "MQTTSNPacket.h" + +#include + +#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 +#include +#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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#if defined(WIN32) +#include +#else +#include +#include +#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 + rc = 0; + return rc; +} + + +int main(int argc, char** argv) +{ + int rc = 0; + unsigned char buf[200]; + int buflen = sizeof(buf); + int mysock = 0; + 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, packetid = 0; + char *host = "127.0.0.1"; + char *topicname = "a long topic name"; + int port = 1883; + MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer; + + if (argc > 1) + host = argv[1]; + + if (argc > 2) + port = atoi(argv[2]); + + 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 = "myclientid"; + len = MQTTSNSerialize_connect(buf, buflen, &options); + rc = sendPacketBuffer(mysock, host, port, buf, len); + + topic.type = MQTTSN_TOPIC_TYPE_NORMAL; + topic.data.qos3.longname = topicname; + topic.data.qos3.longlen = strlen(topicname); + len = MQTTSNSerialize_publish(buf, buflen - len, dup, qos, retained, packetid, + topic, payload, payloadlen); + rc = sendPacketBuffer(mysock, host, port, buf, len); + + rc = shutdown(mysock, SHUT_WR); + rc = close(mysock); + + return 0; +} diff --git a/src/MQTTSNConnect.h b/src/MQTTSNConnect.h index c84a17d..67a3ca6 100644 --- a/src/MQTTSNConnect.h +++ b/src/MQTTSNConnect.h @@ -30,7 +30,7 @@ typedef struct int willFlag; } MQTTSNPacket_connectData; -#define MQTTSNPacket_connectData_initializer { {'M', 'Q', 'S', 'C'}, 0, {NULL, {0, NULL}}, 0, 0, 0 } +#define MQTTSNPacket_connectData_initializer { {'M', 'Q', 'S', 'C'}, 0, {NULL, {0, NULL}}, 10, 0, 0 } int MQTTSNSerialize_connect(unsigned char* buf, int buflen, MQTTSNPacket_connectData* options); int MQTTSNDeserialize_connect(MQTTSNPacket_connectData* data, unsigned char* buf, int len); diff --git a/src/MQTTSNDeserializePublish.c b/src/MQTTSNDeserializePublish.c index d18c7da..a0c764a 100644 --- a/src/MQTTSNDeserializePublish.c +++ b/src/MQTTSNDeserializePublish.c @@ -57,7 +57,7 @@ int MQTTSNDeserialize_publish(int* dup, int* qos, int* retained, int* packetid, *retained = flags.bits.retain; topic->type = flags.bits.topicIdType; - if (topic.type == MQTTSN_TOPIC_TYPE_NORMAL && qos == 3) + if (topic->type == MQTTSN_TOPIC_TYPE_NORMAL && *qos == 3) { /* special arrangement for long topic names in QoS -1 publishes. The length of the topic is in the topicid field */ topic->data.qos3.longlen = readInt(&curdata); @@ -71,9 +71,9 @@ int MQTTSNDeserialize_publish(int* dup, int* qos, int* retained, int* packetid, } *packetid = readInt(&curdata); - if (topic.type == MQTTSN_TOPIC_TYPE_NORMAL && qos == 3) + if (topic->type == MQTTSN_TOPIC_TYPE_NORMAL && *qos == 3) { - topic->data.qos3.longname = curdata; + topic->data.qos3.longname = (char*)curdata; curdata += topic->data.qos3.longlen; } diff --git a/src/MQTTSNSerializePublish.c b/src/MQTTSNSerializePublish.c index 60b25e1..b4a0ea2 100644 --- a/src/MQTTSNSerializePublish.c +++ b/src/MQTTSNSerializePublish.c @@ -46,7 +46,7 @@ int MQTTSNSerialize_publishLength(int payloadlen, MQTTSN_topicid topic, int qos) * @param qos integer - the MQTT QoS value * @param retained integer - the MQTT retained flag * @param packetid integer - the MQTT packet identifier - * @param topicName MQTTString - the MQTT topic in the publish + * @param topic MQTTSN_topicid - the MQTT topic in the publish * @param payload byte buffer - the MQTT publish payload * @param payloadlen integer - the length of the MQTT payload * @return the length of the serialized data. <= 0 indicates error diff --git a/test/build_test b/test/build_test index 9ef86cd..5264f72 100644 --- a/test/build_test +++ b/test/build_test @@ -1 +1 @@ -gcc -Wall test1.c -I../src ../src/MQTTSNConnectClient.c ../src/MQTTSNConnectServer.c ../src/MQTTSNPacket.c ../src/MQTTSNSerializePublish.c ../src/MQTTSNDeserializePublish.c +gcc -Wall test1.c -o test1 -I../src ../src/MQTTSNConnectClient.c ../src/MQTTSNConnectServer.c ../src/MQTTSNPacket.c ../src/MQTTSNSerializePublish.c ../src/MQTTSNDeserializePublish.c