From 76a85ec06b4ed6c3fd0dc0c8d45f2ef5ce8cb531 Mon Sep 17 00:00:00 2001 From: Ian Craggs Date: Wed, 16 Jul 2014 15:26:13 +0100 Subject: [PATCH] Search and advertise --- src/MQTTSNPacket.h | 1 + src/MQTTSNSearch.h | 31 ++++++++++++ src/MQTTSNSearchClient.c | 105 +++++++++++++++++++++++++++++++++++++++ src/MQTTSNSearchServer.c | 101 +++++++++++++++++++++++++++++++++++++ test/build_test | 4 +- 5 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 src/MQTTSNSearch.h create mode 100644 src/MQTTSNSearchClient.c create mode 100644 src/MQTTSNSearchServer.c diff --git a/src/MQTTSNPacket.h b/src/MQTTSNPacket.h index aff4c45..3449e6a 100644 --- a/src/MQTTSNPacket.h +++ b/src/MQTTSNPacket.h @@ -124,6 +124,7 @@ int MQTTstrlen(MQTTString mqttstring); #include "MQTTSNPublish.h" #include "MQTTSNSubscribe.h" #include "MQTTSNUnsubscribe.h" +#include "MQTTSNSearch.h" #include diff --git a/src/MQTTSNSearch.h b/src/MQTTSNSearch.h new file mode 100644 index 0000000..aa98b00 --- /dev/null +++ b/src/MQTTSNSearch.h @@ -0,0 +1,31 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ + +#if !defined(MQTTSNSEARCH_H_) +#define MQTTSNSEARCH_H_ + +int MQTTSNSerialize_advertise(unsigned char* buf, int buflen, unsigned char gatewayid, unsigned short duration); +int MQTTSNDeserialize_advertise(unsigned char* gatewayid, unsigned short* duration, unsigned char* buf, int buflen); + +int MQTTSNSerialize_searchgw(unsigned char* buf, int buflen, unsigned char radius); +int MQTTSNDeserialize_searchgw(unsigned char* radius, unsigned char* buf, int buflen); + +int MQTTSNSerialize_gwinfo(unsigned char* buf, int buflen, unsigned char gatewayid, unsigned short gatewayaddress_len, + unsigned char* gatewayaddress); +int MQTTSNDeserialize_gwinfo(unsigned char* gatewayid, unsigned short* gatewayaddress_len, unsigned char** gatewayaddress, + unsigned char* buf, int buflen); + +#endif /* MQTTSNSEARCH_H_ */ diff --git a/src/MQTTSNSearchClient.c b/src/MQTTSNSearchClient.c new file mode 100644 index 0000000..f9ff623 --- /dev/null +++ b/src/MQTTSNSearchClient.c @@ -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 + *******************************************************************************/ + +#include "MQTTSNPacket.h" +#include "StackTrace.h" + +#include + + +int MQTTSNDeserialize_advertise(unsigned char* gatewayid, unsigned short* duration, unsigned char* buf, int buflen) +{ + unsigned char* curdata = buf; + unsigned char* enddata = NULL; + int rc = 0; + int mylen = 0; + + FUNC_ENTRY; + curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ + enddata = buf + mylen; + if (enddata - curdata > buflen) + goto exit; + + if (readChar(&curdata) != MQTTSN_ADVERTISE) + goto exit; + + *gatewayid = readChar(&curdata); + *duration = readInt(&curdata); + + rc = 1; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + +int MQTTSNSerialize_searchgw(unsigned char* buf, int buflen, unsigned char radius) +{ + unsigned char *ptr = buf; + int len = 0; + int rc = 0; + + FUNC_ENTRY; + if ((len = MQTTSNPacket_len(2)) > buflen) + { + rc = MQTTSNPACKET_BUFFER_TOO_SHORT; + goto exit; + } + ptr += MQTTSNPacket_encode(ptr, len); /* write length */ + writeChar(&ptr, MQTTSN_SEARCHGW); /* write message type */ + + writeChar(&ptr, radius); + + rc = ptr - buf; +exit: + FUNC_EXIT_RC(rc); + return rc; + +} + + +int MQTTSNDeserialize_gwinfo(unsigned char* gatewayid, unsigned short* gatewayaddress_len, + unsigned char** gatewayaddress, unsigned char* buf, int buflen) +{ + unsigned char* curdata = buf; + unsigned char* enddata = NULL; + int rc = 0; + int mylen = 0; + + FUNC_ENTRY; + curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ + enddata = buf + mylen; + if (enddata - curdata > buflen) + goto exit; + + if (readChar(&curdata) != MQTTSN_GWINFO) + goto exit; + + *gatewayid = readChar(&curdata); + + *gatewayaddress_len = enddata - curdata; + *gatewayaddress = curdata; + + rc = 1; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + + + + diff --git a/src/MQTTSNSearchServer.c b/src/MQTTSNSearchServer.c new file mode 100644 index 0000000..b69c244 --- /dev/null +++ b/src/MQTTSNSearchServer.c @@ -0,0 +1,101 @@ +/******************************************************************************* + * 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 "StackTrace.h" + +#include + + +int MQTTSNSerialize_advertise(unsigned char* buf, int buflen, unsigned char gatewayid, unsigned short duration) +{ + unsigned char *ptr = buf; + int len = 0; + int rc = 0; + + FUNC_ENTRY; + if ((len = MQTTSNPacket_len(4)) > buflen) + { + rc = MQTTSNPACKET_BUFFER_TOO_SHORT; + goto exit; + } + ptr += MQTTSNPacket_encode(ptr, len); /* write length */ + writeChar(&ptr, MQTTSN_ADVERTISE); /* write message type */ + + writeChar(&ptr, gatewayid); + writeInt(&ptr, duration); + + rc = ptr - buf; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + +int MQTTSNDeserialize_searchgw(unsigned char* radius, unsigned char* buf, int buflen) +{ + unsigned char* curdata = buf; + unsigned char* enddata = NULL; + int rc = 0; + int mylen = 0; + + FUNC_ENTRY; + curdata += (rc = MQTTSNPacket_decode(curdata, buflen, &mylen)); /* read length */ + enddata = buf + mylen; + if (enddata - curdata > buflen) + goto exit; + + if (readChar(&curdata) != MQTTSN_SEARCHGW) + goto exit; + + *radius = readChar(&curdata); + + rc = 1; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + +int MQTTSNSerialize_gwinfo(unsigned char* buf, int buflen, unsigned char gatewayid, unsigned short gatewayaddress_len, + unsigned char* gatewayaddress) +{ + unsigned char *ptr = buf; + int len = 0; + int rc = 0; + + FUNC_ENTRY; + if ((len = MQTTSNPacket_len(2 + gatewayaddress_len)) > buflen) + { + rc = MQTTSNPACKET_BUFFER_TOO_SHORT; + goto exit; + } + ptr += MQTTSNPacket_encode(ptr, len); /* write length */ + writeChar(&ptr, MQTTSN_GWINFO); /* write message type */ + + writeChar(&ptr, gatewayid); + memcpy(ptr, gatewayaddress, gatewayaddress_len); + ptr += gatewayaddress_len; + + rc = ptr - buf; +exit: + FUNC_EXIT_RC(rc); + return rc; + +} + + + diff --git a/test/build_test b/test/build_test index 5d7bbb9..74e6c61 100644 --- a/test/build_test +++ b/test/build_test @@ -1,3 +1,3 @@ -gcc -Wall test1.c -o test1 -I../src ../src/MQTTSNConnectClient.c ../src/MQTTSNConnectServer.c ../src/MQTTSNPacket.c ../src/MQTTSNSerializePublish.c ../src/MQTTSNDeserializePublish.c ../src/MQTTSNSubscribeClient.c ../src/MQTTSNSubscribeServer.c ../src/MQTTSNUnsubscribeClient.c ../src/MQTTSNUnsubscribeServer.c +gcc -Wall test1.c -o test1 -I../src ../src/MQTTSNConnectClient.c ../src/MQTTSNConnectServer.c ../src/MQTTSNPacket.c ../src/MQTTSNSerializePublish.c ../src/MQTTSNDeserializePublish.c ../src/MQTTSNSubscribeClient.c ../src/MQTTSNSubscribeServer.c ../src/MQTTSNUnsubscribeClient.c ../src/MQTTSNUnsubscribeServer.c ../src/MQTTSNSearchClient.c ../src/MQTTSNSearchServer.c -gcc -Wall test2.c -o test2 -I../src ../src/MQTTSNConnectClient.c ../src/MQTTSNConnectServer.c ../src/MQTTSNPacket.c ../src/MQTTSNSerializePublish.c ../src/MQTTSNDeserializePublish.c ../src/MQTTSNSubscribeClient.c +gcc -Wall test2.c -o test2 -I../src ../src/MQTTSNConnectClient.c ../src/MQTTSNConnectServer.c ../src/MQTTSNPacket.c ../src/MQTTSNSerializePublish.c ../src/MQTTSNDeserializePublish.c ../src/MQTTSNSubscribeClient.c