forked from openthread/openthread
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmqttsn_serializer.cpp
710 lines (649 loc) · 22.2 KB
/
mqttsn_serializer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
* Copyright (c) 2018, Vit Holasek
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <stddef.h>
#include <openthread/platform/toolchain.h>
#include <MQTTSNPacket.h>
#include "mqttsn_serializer.hpp"
/**
* @file
* This file contains implementation of MQTT-SN protocol v1.2 messages serialization with Eclipse Paho serializer.
*
*/
namespace ot {
namespace Mqttsn {
static int32_t PacketDecode(const unsigned char* aData, size_t aLength)
{
int lenlen = 0;
int datalen = 0;
lenlen = MQTTSNPacket_decode(const_cast<unsigned char*>(aData), aLength, &datalen);
if (datalen != static_cast<int>(aLength))
{
return MQTTSNPACKET_READ_ERROR;
}
return aData[lenlen]; // return the packet type
}
otError MessageBase::DeserializeMessageType(const uint8_t* aBuffer, int32_t aBufferLength, MessageType* aMessageType)
{
int32_t type = PacketDecode(aBuffer, aBufferLength);
if (type == MQTTSNPACKET_READ_ERROR)
{
return OT_ERROR_FAILED;
}
*aMessageType = static_cast<MessageType>(type);
return OT_ERROR_NONE;
}
otError AdvertiseMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_advertise(aBuffer, aBufferLength, mGatewayId, mDuration);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError AdvertiseMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
if (MQTTSNDeserialize_advertise(&mGatewayId, &mDuration, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
return OT_ERROR_NONE;
}
otError SearchGwMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_searchgw(aBuffer, aBufferLength, mRadius);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError SearchGwMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
if (MQTTSNDeserialize_searchgw(&mRadius, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
return OT_ERROR_NONE;
}
otError GwInfoMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = -1;
if (mHasAddress)
{
// paho does not use const parameters even if data are not changed
uint8_t *addressBytes = const_cast<uint8_t *>(mAddress.mFields.m8);
length = MQTTSNSerialize_gwinfo(aBuffer, aBufferLength, mGatewayId,
OT_IP6_ADDRESS_SIZE, reinterpret_cast<unsigned char*>(addressBytes));
}
else
{
length = MQTTSNSerialize_gwinfo(aBuffer, aBufferLength, mGatewayId, 0, NULL);
}
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError GwInfoMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
otError error = OT_ERROR_NONE;
unsigned short addressLength;
unsigned char* address;
int32_t length = MQTTSNDeserialize_gwinfo(&mGatewayId, &addressLength, &address, const_cast<unsigned char*>(aBuffer), aBufferLength);
VerifyOrExit(length > 0, error = OT_ERROR_FAILED);
if (addressLength > 0)
{
VerifyOrExit(addressLength == OT_IP6_ADDRESS_SIZE, error = OT_ERROR_INVALID_ARGS);
memcpy(mAddress.mFields.m8, address, OT_IP6_ADDRESS_SIZE);
mHasAddress = true;
}
else
{
mHasAddress = false;
}
exit:
return error;
}
otError ConnectMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
MQTTSNString clientId = MQTTSNString_initializer;
clientId.cstring = const_cast<char *>(mClientId.AsCString());
options.clientID = clientId;
options.duration = mDuration;
options.cleansession = static_cast<unsigned char>(mCleanSessionFlag);
int32_t length = MQTTSNSerialize_connect(aBuffer, aBufferLength, &options);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError ConnectMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
otError error = OT_ERROR_NONE;
MQTTSNPacket_connectData data = MQTTSNPacket_connectData_initializer;
VerifyOrExit(MQTTSNDeserialize_connect(&data, const_cast<unsigned char*>(aBuffer), aBufferLength) == 1, error = OT_ERROR_FAILED);
mCleanSessionFlag = static_cast<bool>(data.cleansession);
mWillFlag = static_cast<bool>(data.willFlag);
mDuration = data.duration;
mClientId.Clear().Append("%.*s", data.clientID.lenstring.len, data.clientID.lenstring.data);
exit:
return error;
}
otError ConnackMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_connack(aBuffer, aBufferLength, mReturnCode);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError ConnackMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
int code;
if (MQTTSNDeserialize_connack(&code, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
mReturnCode = static_cast<ReturnCode>(code);
return OT_ERROR_NONE;
}
otError RegisterMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
MQTTSNString topicName = MQTTSNString_initializer;
topicName.cstring = const_cast<char*>(mTopicName.AsCString());
int32_t length = MQTTSNSerialize_register(aBuffer, aBufferLength, static_cast<unsigned short>(mTopicId), mMessageId, &topicName);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError RegisterMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
unsigned short topicId;
MQTTSNString topicName = MQTTSNString_initializer;
if (MQTTSNDeserialize_register(&topicId, &mMessageId, &topicName, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
mTopicId = static_cast<TopicId>(topicId);
mTopicName.Clear().Append("%.*s", topicName.lenstring.len, topicName.lenstring.data);
return OT_ERROR_NONE;
}
otError RegackMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_regack(aBuffer, aBufferLength, static_cast<unsigned char>(mTopicId),
mMessageId, static_cast<unsigned char>(mReturnCode));
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError RegackMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
unsigned short topicId;
unsigned char code;
if (MQTTSNDeserialize_regack(&topicId, &mMessageId, &code, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
mTopicId = static_cast<TopicId>(topicId);
mReturnCode = static_cast<ReturnCode>(code);
return OT_ERROR_NONE;
}
otError PublishMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
MQTTSN_topicid topicId;
memset(&topicId, 0, sizeof(topicId));
// For QoS-1 only short topic name and predefined topic ID are allowed
// In addition Message ID must be null
if (mQos == kQosm1 && (mMessageId != 0 || mTopic.GetType() != kPredefinedTopicId))
return OT_ERROR_INVALID_ARGS;
switch (mTopic.GetType())
{
case kTopicId:
topicId.type = MQTTSN_TOPIC_TYPE_NORMAL;
topicId.data.id = static_cast<unsigned short>(mTopic.GetTopicId());
break;
case kPredefinedTopicId:
topicId.type = MQTTSN_TOPIC_TYPE_PREDEFINED;
topicId.data.id = static_cast<unsigned short>(mTopic.GetTopicId());
break;
case kShortTopicName:
topicId.type = MQTTSN_TOPIC_TYPE_SHORT;
memcpy(topicId.data.short_name, mTopic.GetTopicName(), 2);
break;
default:
return OT_ERROR_INVALID_STATE;
}
int32_t length = MQTTSNSerialize_publish(aBuffer, aBufferLength, static_cast<unsigned char>(mDupFlag), static_cast<unsigned char>(mQos),
static_cast<unsigned char>(mRetainedFlag), mMessageId, topicId, const_cast<unsigned char*>(mPayload), mPayloadLength);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError PublishMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
otError error = OT_ERROR_NONE;
unsigned char dup;
int qos;
unsigned char retained;
MQTTSN_topicid topicId;
unsigned char* payload;
int payloadLength;
memset(&topicId, 0, sizeof(topicId));
if (MQTTSNDeserialize_publish(&dup, &qos, &retained, &mMessageId, &topicId,
&payload, &payloadLength, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
error = OT_ERROR_FAILED;
goto exit;
}
mDupFlag = static_cast<bool>(dup);
mRetainedFlag = static_cast<bool>(retained);
mQos = static_cast<Qos>(qos);
switch (topicId.type)
{
case MQTTSN_TOPIC_TYPE_PREDEFINED:
mTopic = Topic::FromPredefinedTopicId(topicId.data.id);
break;
case MQTTSN_TOPIC_TYPE_NORMAL:
mTopic = Topic::FromTopicId(topicId.data.id);
break;
case MQTTSN_TOPIC_TYPE_SHORT:
mTopic = Topic::FromShortTopicName(topicId.data.short_name);
break;
default:
error = OT_ERROR_INVALID_STATE;
goto exit;
}
mPayload = payload;
mPayloadLength = static_cast<int32_t>(payloadLength);
exit:
return error;
}
otError PubackMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
unsigned short topicId = 0;
switch (mTopic.GetType())
{
case kShortTopicName:
topicId = ((unsigned short) mTopic.GetTopicName()[0] << 8) | (unsigned short) mTopic.GetTopicName()[1];
break;
case kTopicId:
case kPredefinedTopicId:
topicId = mTopic.GetTopicId();
break;
default:
return OT_ERROR_INVALID_ARGS;
}
int32_t length = MQTTSNSerialize_puback(aBuffer, aBufferLength, topicId,
mMessageId, static_cast<unsigned char>(mReturnCode));
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError PubackMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
unsigned char code;
unsigned short topicId;
if (MQTTSNDeserialize_puback(&topicId, &mMessageId, &code, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
mReturnCode = static_cast<ReturnCode>(code);
mTopic = Topic::FromTopicId(topicId);
return OT_ERROR_NONE;
}
otError PubcompMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_pubcomp(aBuffer, aBufferLength, mMessageId);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError PubcompMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
unsigned char type;
unsigned short messageId;
if (MQTTSNDeserialize_ack(&type, &messageId, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1
|| type != MQTTSN_PUBCOMP)
{
return OT_ERROR_FAILED;
}
mMessageId = messageId;
return OT_ERROR_NONE;
}
otError PubrecMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_pubrec(aBuffer, aBufferLength, mMessageId);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError PubrecMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
unsigned char type;
unsigned short messageId;
if (MQTTSNDeserialize_ack(&type, &messageId, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1
|| type != MQTTSN_PUBREC)
{
return OT_ERROR_FAILED;
}
mMessageId = messageId;
return OT_ERROR_NONE;
}
otError PubrelMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_pubrel(aBuffer, aBufferLength, mMessageId);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError PubrelMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
unsigned char type;
unsigned short messageId;
if (MQTTSNDeserialize_ack(&type, &messageId, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1
|| type != MQTTSN_PUBREC)
{
return OT_ERROR_FAILED;
}
mMessageId = messageId;
return OT_ERROR_NONE;
}
otError SubscribeMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
MQTTSN_topicid topicId;
int32_t len = 0;
memset(&topicId, 0, sizeof(topicId));
switch (mTopic.GetType())
{
case kTopicName:
topicId.type = MQTTSN_TOPIC_TYPE_NORMAL;
len = strlen(mTopic.GetTopicName());
if (len > kMaxTopicNameLength) return OT_ERROR_INVALID_ARGS;
topicId.data.long_.name = const_cast<char*>(mTopic.GetTopicName());
topicId.data.long_.len = len;
break;
case kShortTopicName:
topicId.type = MQTTSN_TOPIC_TYPE_SHORT;
memcpy(topicId.data.short_name, mTopic.GetTopicName(), sizeof(topicId.data.short_name));
break;
case kPredefinedTopicId:
case kTopicId:
topicId.type = MQTTSN_TOPIC_TYPE_PREDEFINED;
topicId.data.id = static_cast<unsigned short>(mTopic.GetTopicId());
break;
default:
return OT_ERROR_INVALID_STATE;
}
int32_t length = MQTTSNSerialize_subscribe(aBuffer, aBufferLength, static_cast<unsigned char>(mDupFlag),
static_cast<int>(mQos), mMessageId, &topicId);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError SubscribeMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
otError error = OT_ERROR_NONE;
unsigned char dup;
int qos;
MQTTSN_topicid topicId;
memset(&topicId, 0, sizeof(topicId));
if (MQTTSNDeserialize_subscribe(&dup, &qos, &mMessageId, &topicId, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
error = OT_ERROR_FAILED;
goto exit;
}
mDupFlag = static_cast<bool>(dup);
mQos = static_cast<Qos>(qos);
switch (topicId.type)
{
case MQTTSN_TOPIC_TYPE_NORMAL:
mTopicName.Clear().Append("%.*s", topicId.data.long_.len, topicId.data.long_.name);
mTopic = Topic::FromTopicName(mTopicName.AsCString());
break;
case MQTTSN_TOPIC_TYPE_SHORT:
mTopicName.Clear().Append("%.*s", 2, topicId.data.short_name);
mTopic = Topic::FromShortTopicName(mTopicName.AsCString());
break;
case MQTTSN_TOPIC_TYPE_PREDEFINED:
mTopic = Topic::FromTopicId(static_cast<TopicId>(topicId.data.id));
break;
default:
error = OT_ERROR_INVALID_STATE;
goto exit;
}
exit:
return error;
}
otError SubackMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_suback(aBuffer, aBufferLength, static_cast<int>(mQos),
static_cast<unsigned short>(mTopicId), mMessageId, static_cast<unsigned char>(mReturnCode));
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError SubackMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
unsigned short topicId;
unsigned char code;
int qos;
if (MQTTSNDeserialize_suback(&qos, &topicId, &mMessageId, &code, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
mTopicId = static_cast<TopicId>(topicId);
mReturnCode = static_cast<ReturnCode>(code);
mQos = static_cast<Qos>(qos);
return OT_ERROR_NONE;
}
otError UnsubscribeMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
MQTTSN_topicid topicId;
int32_t len;
memset(&topicId, 0, sizeof(topicId));
switch (mTopic.GetType())
{
case kTopicName:
topicId.type = MQTTSN_TOPIC_TYPE_NORMAL;
len = strlen(mTopic.GetTopicName());
if (len > kMaxTopicNameLength) return OT_ERROR_INVALID_ARGS;
topicId.data.long_.name = const_cast<char*>(mTopic.GetTopicName());
topicId.data.long_.len = len;
break;
case kShortTopicName:
topicId.type = MQTTSN_TOPIC_TYPE_SHORT;
memcpy(topicId.data.short_name, mTopic.GetTopicName(),
sizeof(topicId.data.short_name));
break;
case kPredefinedTopicId:
case kTopicId:
topicId.type = MQTTSN_TOPIC_TYPE_PREDEFINED;
topicId.data.id = static_cast<unsigned short>(mTopic.GetTopicId());
break;
default:
return OT_ERROR_INVALID_STATE;
}
int32_t length = MQTTSNSerialize_unsubscribe(aBuffer, aBufferLength, mMessageId, &topicId);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError UnsubscribeMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
otError error = OT_ERROR_NONE;
MQTTSN_topicid topicId;
memset(&topicId, 0, sizeof(topicId));
if (MQTTSNDeserialize_unsubscribe(&mMessageId, &topicId, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
error = OT_ERROR_FAILED;
goto exit;
}
switch (topicId.type)
{
case MQTTSN_TOPIC_TYPE_NORMAL:
mTopicName.Clear().Append("%.*s", topicId.data.long_.len, topicId.data.long_.name);
mTopic = Topic::FromTopicName(mTopicName.AsCString());
break;
case MQTTSN_TOPIC_TYPE_SHORT:
mTopicName.Clear().Append("%.*s", 2, topicId.data.short_name);
mTopic = Topic::FromShortTopicName(mTopicName.AsCString());
break;
case MQTTSN_TOPIC_TYPE_PREDEFINED:
mTopic = Topic::FromTopicId(static_cast<TopicId>(topicId.data.id));
break;
default:
error = OT_ERROR_INVALID_STATE;
goto exit;
}
exit:
return error;
}
otError UnsubackMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_unsuback(aBuffer, aBufferLength, mMessageId);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError UnsubackMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
if (MQTTSNDeserialize_unsuback(&mMessageId, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
return OT_ERROR_NONE;
}
otError PingreqMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
MQTTSNString clientId = MQTTSNString_initializer;
clientId.cstring = const_cast<char*>(mClientId.AsCString());
int32_t length = MQTTSNSerialize_pingreq(aBuffer, aBufferLength, clientId);
if (length < 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError PingreqMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
MQTTSNString clientId = MQTTSNString_initializer;
if (MQTTSNDeserialize_pingreq(&clientId, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
if (clientId.lenstring.data != NULL)
{
mClientId.Clear().Append("%.*s", clientId.lenstring.len, clientId.lenstring.data);
}
return OT_ERROR_NONE;
}
otError PingrespMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_pingresp(aBuffer, aBufferLength);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError PingrespMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
if (MQTTSNDeserialize_pingresp(const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
return OT_ERROR_NONE;
}
otError DisconnectMessage::Serialize(uint8_t* aBuffer, uint8_t aBufferLength, int32_t* aLength) const
{
int32_t length = MQTTSNSerialize_disconnect(aBuffer, aBufferLength, mDuration);
if (length <= 0)
{
return OT_ERROR_FAILED;
}
*aLength = length;
return OT_ERROR_NONE;
}
otError DisconnectMessage::Deserialize(const uint8_t* aBuffer, int32_t aBufferLength)
{
int duration;
if (MQTTSNDeserialize_disconnect(&duration, const_cast<unsigned char*>(aBuffer), aBufferLength) != 1)
{
return OT_ERROR_FAILED;
}
mDuration = duration;
return OT_ERROR_NONE;
}
}
}