aboutsummaryrefslogtreecommitdiff
path: root/src/packet.h
blob: a5e2ce789fc47dafcd39508cef17cde8e7dd4bf9 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef PACKET_H
#define PACKET_H

#include "config.h"

#include <stdlib.h>
#include <stdint.h>
#include <assert.h>

#include <bstrlib/bstrlib.h>

#include "queue.h"

enum
{
    MqttPacketTypeConnect = 0x1,
    MqttPacketTypeConnAck = 0x2,
    MqttPacketTypePublish = 0x3,
    MqttPacketTypePubAck = 0x4,
    MqttPacketTypePubRec = 0x5,
    MqttPacketTypePubRel = 0x6,
    MqttPacketTypePubComp = 0x7,
    MqttPacketTypeSubscribe = 0x8,
    MqttPacketTypeSubAck = 0x9,
    MqttPacketTypeUnsubscribe = 0xA,
    MqttPacketTypeUnsubAck = 0xB,
    MqttPacketTypePingReq = 0xC,
    MqttPacketTypePingResp = 0xD,
    MqttPacketTypeDisconnect = 0xE
};

enum MqttPacketState
{
    MqttPacketStateReadType,
    MqttPacketStateReadRemainingLength,
    MqttPacketStateReadPayload,
    MqttPacketStateReadComplete,

    MqttPacketStateWriteType,
    MqttPacketStateWriteRemainingLength,
    MqttPacketStateWritePayload,
    MqttPacketStateWriteComplete
};

struct MqttMessage;

typedef struct MqttPacket MqttPacket;

struct MqttPacket
{
    int type;
    int flags;
    int state;
    uint16_t id;
    size_t remainingLength;
    size_t remainingLengthMul;
    /* TODO: maybe switch to have a StringStream here? */
    bstring payload;
    struct MqttMessage *message;
    SIMPLEQ_ENTRY(MqttPacket) sendQueue;
};

const char *MqttPacketName(int type);

MqttPacket *MqttPacketNew(int type);

MqttPacket *MqttPacketWithIdNew(int type, uint16_t id);

void MqttPacketFree(MqttPacket *packet);

#endif