Commit 76c18f38b739b3f75757b4adfef76d0b4af49960

Authored by Wiebe Cazemier
1 parent 5dcad433

Dummy connect

mqttpacket.cpp
1 1 #include "mqttpacket.h"
2 2 #include <cstring>
3 3 #include <iostream>
  4 +#include <list>
4 5  
5 6 MqttPacket::MqttPacket(char *buf, size_t len, size_t fixed_header_length, Client *sender) :
6 7 bites(len),
... ... @@ -26,6 +27,25 @@ MqttPacket::MqttPacket(const ConnAck &amp;connAck) :
26 27  
27 28 }
28 29  
  30 +MqttPacket::MqttPacket(const SubAck &subAck) :
  31 + bites(3)
  32 +{
  33 + packetType = PacketType::SUBACK;
  34 + char first_byte = static_cast<char>(packetType) << 4;
  35 + writeByte(first_byte);
  36 + writeByte((subAck.packet_id & 0xF0) >> 8);
  37 + writeByte(subAck.packet_id & 0x0F);
  38 +
  39 + std::vector<char> returnList;
  40 + for (SubAckReturnCodes code : subAck.responses)
  41 + {
  42 + returnList.push_back(static_cast<char>(code));
  43 + }
  44 +
  45 + bites.insert(bites.end(), returnList.begin(), returnList.end());
  46 + bites[1] = returnList.size() + 1; // TODO: make some generic way of calculating the header and use the multi-byte length
  47 +}
  48 +
29 49 void MqttPacket::handle()
30 50 {
31 51 if (packetType == PacketType::CONNECT)
... ... @@ -33,7 +53,7 @@ void MqttPacket::handle()
33 53 else if (packetType == PacketType::PINGREQ)
34 54 std::cout << "PING" << std::endl;
35 55 else if (packetType == PacketType::SUBSCRIBE)
36   - std::cout << "Sub" << std::endl;
  56 + handleSubscribe();
37 57 }
38 58  
39 59 void MqttPacket::handleConnect()
... ... @@ -117,6 +137,34 @@ void MqttPacket::handleConnect()
117 137 }
118 138 }
119 139  
  140 +void MqttPacket::handleSubscribe()
  141 +{
  142 + uint16_t packet_id = readTwoBytesToUInt16();
  143 +
  144 + std::list<std::string> subs; // TODO: list of tuples, probably
  145 + while (remainingAfterPos() > 1)
  146 + {
  147 + uint16_t topicLength = readTwoBytesToUInt16();
  148 + std::string topic(readBytes(topicLength), topicLength);
  149 + std::cout << sender->repr() << " Subscribed to " << topic << std::endl;
  150 + subs.push_back(std::move(topic));
  151 + }
  152 +
  153 + char flags = readByte();
  154 +
  155 + SubAck subAck(packet_id, subs);
  156 + MqttPacket response(subAck);
  157 + sender->writeMqttPacket(response);
  158 + sender->writeBufIntoFd();
  159 +}
  160 +
  161 +void MqttPacket::handlePing()
  162 +{
  163 +
  164 +}
  165 +
  166 +
  167 +
120 168 char *MqttPacket::readBytes(size_t length)
121 169 {
122 170 if (pos + length > bites.size())
... ... @@ -154,6 +202,11 @@ uint16_t MqttPacket::readTwoBytesToUInt16()
154 202 return i;
155 203 }
156 204  
  205 +size_t MqttPacket::remainingAfterPos()
  206 +{
  207 + return bites.size() - pos;
  208 +}
  209 +
157 210  
158 211  
159 212  
... ...
mqttpacket.h
... ... @@ -25,14 +25,19 @@ class MqttPacket
25 25 char readByte();
26 26 void writeByte(char b);
27 27 uint16_t readTwoBytesToUInt16();
  28 + size_t remainingAfterPos();
28 29  
29 30 public:
30 31 PacketType packetType = PacketType::Reserved;
31 32 MqttPacket(char *buf, size_t len, size_t fixed_header_length, Client *sender);
32 33 MqttPacket(const ConnAck &connAck);
  34 + MqttPacket(const SubAck &subAck);
33 35  
34 36 void handle();
35 37 void handleConnect();
  38 + void handleSubscribe();
  39 + void handlePing();
  40 +
36 41 size_t getSize() { return bites.size(); }
37 42 const std::vector<char> &getBites() { return bites; }
38 43  
... ...
types.cpp
... ... @@ -5,3 +5,13 @@ ConnAck::ConnAck(ConnAckReturnCodes return_code) :
5 5 {
6 6  
7 7 }
  8 +
  9 +SubAck::SubAck(uint16_t packet_id, const std::list<std::string> &subs) :
  10 + packet_id(packet_id)
  11 +{
  12 + // dummy
  13 + for(size_t i = 0; i < subs.size(); i++)
  14 + {
  15 + responses.push_back(SubAckReturnCodes::MaxQoS0);
  16 + }
  17 +}
... ...
1 1 #ifndef TYPES_H
2 2 #define TYPES_H
3 3  
  4 +#include "stdint.h"
  5 +#include <list>
  6 +#include <string>
  7 +
4 8 enum class PacketType
5 9 {
6 10 Reserved = 0,
... ... @@ -46,4 +50,20 @@ public:
46 50 ConnAckReturnCodes return_code;
47 51 };
48 52  
  53 +enum class SubAckReturnCodes
  54 +{
  55 + MaxQoS0 = 0,
  56 + MaxQoS1 = 1,
  57 + MaxQoS2 = 2,
  58 + Fail = 0x80
  59 +};
  60 +
  61 +class SubAck
  62 +{
  63 +public:
  64 + uint16_t packet_id;
  65 + std::list<SubAckReturnCodes> responses;
  66 + SubAck(uint16_t packet_id, const std::list<std::string> &subs);
  67 +};
  68 +
49 69 #endif // TYPES_H
... ...