From 76c18f38b739b3f75757b4adfef76d0b4af49960 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Wed, 9 Dec 2020 14:45:36 +0100 Subject: [PATCH] Dummy connect --- mqttpacket.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- mqttpacket.h | 5 +++++ types.cpp | 10 ++++++++++ types.h | 20 ++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) diff --git a/mqttpacket.cpp b/mqttpacket.cpp index 50c9eb5..cb73c57 100644 --- a/mqttpacket.cpp +++ b/mqttpacket.cpp @@ -1,6 +1,7 @@ #include "mqttpacket.h" #include #include +#include MqttPacket::MqttPacket(char *buf, size_t len, size_t fixed_header_length, Client *sender) : bites(len), @@ -26,6 +27,25 @@ MqttPacket::MqttPacket(const ConnAck &connAck) : } +MqttPacket::MqttPacket(const SubAck &subAck) : + bites(3) +{ + packetType = PacketType::SUBACK; + char first_byte = static_cast(packetType) << 4; + writeByte(first_byte); + writeByte((subAck.packet_id & 0xF0) >> 8); + writeByte(subAck.packet_id & 0x0F); + + std::vector returnList; + for (SubAckReturnCodes code : subAck.responses) + { + returnList.push_back(static_cast(code)); + } + + bites.insert(bites.end(), returnList.begin(), returnList.end()); + bites[1] = returnList.size() + 1; // TODO: make some generic way of calculating the header and use the multi-byte length +} + void MqttPacket::handle() { if (packetType == PacketType::CONNECT) @@ -33,7 +53,7 @@ void MqttPacket::handle() else if (packetType == PacketType::PINGREQ) std::cout << "PING" << std::endl; else if (packetType == PacketType::SUBSCRIBE) - std::cout << "Sub" << std::endl; + handleSubscribe(); } void MqttPacket::handleConnect() @@ -117,6 +137,34 @@ void MqttPacket::handleConnect() } } +void MqttPacket::handleSubscribe() +{ + uint16_t packet_id = readTwoBytesToUInt16(); + + std::list subs; // TODO: list of tuples, probably + while (remainingAfterPos() > 1) + { + uint16_t topicLength = readTwoBytesToUInt16(); + std::string topic(readBytes(topicLength), topicLength); + std::cout << sender->repr() << " Subscribed to " << topic << std::endl; + subs.push_back(std::move(topic)); + } + + char flags = readByte(); + + SubAck subAck(packet_id, subs); + MqttPacket response(subAck); + sender->writeMqttPacket(response); + sender->writeBufIntoFd(); +} + +void MqttPacket::handlePing() +{ + +} + + + char *MqttPacket::readBytes(size_t length) { if (pos + length > bites.size()) @@ -154,6 +202,11 @@ uint16_t MqttPacket::readTwoBytesToUInt16() return i; } +size_t MqttPacket::remainingAfterPos() +{ + return bites.size() - pos; +} + diff --git a/mqttpacket.h b/mqttpacket.h index 39fea97..ac2fed8 100644 --- a/mqttpacket.h +++ b/mqttpacket.h @@ -25,14 +25,19 @@ class MqttPacket char readByte(); void writeByte(char b); uint16_t readTwoBytesToUInt16(); + size_t remainingAfterPos(); public: PacketType packetType = PacketType::Reserved; MqttPacket(char *buf, size_t len, size_t fixed_header_length, Client *sender); MqttPacket(const ConnAck &connAck); + MqttPacket(const SubAck &subAck); void handle(); void handleConnect(); + void handleSubscribe(); + void handlePing(); + size_t getSize() { return bites.size(); } const std::vector &getBites() { return bites; } diff --git a/types.cpp b/types.cpp index e71cd93..8911257 100644 --- a/types.cpp +++ b/types.cpp @@ -5,3 +5,13 @@ ConnAck::ConnAck(ConnAckReturnCodes return_code) : { } + +SubAck::SubAck(uint16_t packet_id, const std::list &subs) : + packet_id(packet_id) +{ + // dummy + for(size_t i = 0; i < subs.size(); i++) + { + responses.push_back(SubAckReturnCodes::MaxQoS0); + } +} diff --git a/types.h b/types.h index cfc854e..9432a90 100644 --- a/types.h +++ b/types.h @@ -1,6 +1,10 @@ #ifndef TYPES_H #define TYPES_H +#include "stdint.h" +#include +#include + enum class PacketType { Reserved = 0, @@ -46,4 +50,20 @@ public: ConnAckReturnCodes return_code; }; +enum class SubAckReturnCodes +{ + MaxQoS0 = 0, + MaxQoS1 = 1, + MaxQoS2 = 2, + Fail = 0x80 +}; + +class SubAck +{ +public: + uint16_t packet_id; + std::list responses; + SubAck(uint16_t packet_id, const std::list &subs); +}; + #endif // TYPES_H -- libgit2 0.21.4