blob: c8338515c11f81ff68bbd4c038302063f13c9ee1 (
plain)
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
|
#include "packet.h"
#include "log.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
const char *MqttPacketName(int type)
{
switch (type)
{
case MqttPacketTypeConnect: return "CONNECT";
case MqttPacketTypeConnAck: return "CONNACK";
case MqttPacketTypePublish: return "PUBLISH";
case MqttPacketTypePubAck: return "PUBACK";
case MqttPacketTypePubRec: return "PUBREC";
case MqttPacketTypePubRel: return "PUBREL";
case MqttPacketTypePubComp: return "PUBCOMP";
case MqttPacketTypeSubscribe: return "SUBSCRIBE";
case MqttPacketTypeSubAck: return "SUBACK";
case MqttPacketTypeUnsubscribe: return "UNSUBSCRIBE";
case MqttPacketTypeUnsubAck: return "UNSUBACK";
case MqttPacketTypePingReq: return "PINGREQ";
case MqttPacketTypePingResp: return "PINGRESP";
case MqttPacketTypeDisconnect: return "DISCONNECT";
default: return NULL;
}
}
MqttPacket *MqttPacketNew(int type)
{
MqttPacket *packet = NULL;
packet = (MqttPacket *) calloc(1, sizeof(*packet));
if (!packet)
return NULL;
packet->type = type;
return packet;
}
MqttPacket *MqttPacketWithIdNew(int type, uint16_t id)
{
MqttPacket *packet = MqttPacketNew(type);
if (!packet)
return NULL;
packet->id = id;
return packet;
}
void MqttPacketFree(MqttPacket *packet)
{
bdestroy(packet->payload);
free(packet);
}
|