Skip to content

Commit 329b629

Browse files
committed
separated MQTTTransfort into individual files
1 parent ea9fb92 commit 329b629

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <Arduino.h>
2+
#include <ESP8266WiFi.h>
3+
#include <WiFiClientSecure.h>
4+
#include <StreamString.h>
5+
#include <base64.h>
6+
#include "ESP8266MQTTClient.h"
7+
#include "MQTTTransport.h"
8+
9+
MQTTTransportTraits::~MQTTTransportTraits()
10+
{
11+
}
12+
13+
std::unique_ptr<WiFiClient> MQTTTransportTraits::create()
14+
{
15+
return std::unique_ptr<WiFiClient>(new WiFiClient());
16+
}
17+
18+
bool MQTTTransportTraits::verify(WiFiClient& client, const char* host)
19+
{
20+
return true;
21+
}
22+
23+
24+
MQTTTLSTraits::MQTTTLSTraits(const String& fingerprint) :
25+
_fingerprint(fingerprint)
26+
{
27+
}
28+
29+
std::unique_ptr<WiFiClient> MQTTTLSTraits::create()
30+
{
31+
return std::unique_ptr<WiFiClient>(new WiFiClientSecure());
32+
}
33+
34+
bool MQTTTLSTraits::verify(WiFiClient& client, const char* host)
35+
{
36+
auto wcs = reinterpret_cast<WiFiClientSecure&>(client);
37+
return wcs.verify(_fingerprint.c_str(), host);
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef _MQTT_TRANSPORT_H_
2+
#define _MQTT_TRANSPORT_H_
3+
// WebSocket protocol constants
4+
// First byte
5+
#define WS_FIN 0x80
6+
#define WS_OPCODE_TEXT 0x01
7+
#define WS_OPCODE_BINARY 0x02
8+
#define WS_OPCODE_CLOSE 0x08
9+
#define WS_OPCODE_PING 0x09
10+
#define WS_OPCODE_PONG 0x0a
11+
// Second byte
12+
#define WS_MASK 0x80
13+
#define WS_SIZE16 126
14+
#define WS_SIZE64 127
15+
16+
17+
class MQTTTransportTraits
18+
{
19+
public:
20+
virtual ~MQTTTransportTraits();
21+
virtual std::unique_ptr<WiFiClient> create();
22+
virtual bool verify(WiFiClient& client, const char* host);
23+
protected:
24+
std::unique_ptr<WiFiClient> _tcp;
25+
};
26+
27+
class MQTTTLSTraits : public MQTTTransportTraits
28+
{
29+
public:
30+
MQTTTLSTraits(const String& fingerprint);
31+
std::unique_ptr<WiFiClient> create();
32+
bool verify(WiFiClient& client, const char* host);
33+
protected:
34+
String _fingerprint;
35+
};
36+
#endif

0 commit comments

Comments
 (0)