aboutsummaryrefslogtreecommitdiffhomepage
path: root/backends/mqtt.h
blob: c684f9903868a7e1736ac7508889917248f1f906 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "midimonster.h"

MM_PLUGIN_API int init();
static int mqtt_configure(char* option, char* value);
static int mqtt_configure_instance(instance* inst, char* option, char* value);
static int mqtt_instance(instance* inst);
static channel* mqtt_channel(instance* inst, char* spec, uint8_t flags);
static int mqtt_set(instance* inst, size_t num, channel** c, channel_value* v);
static int mqtt_handle(size_t num, managed_fd* fds);
static int mqtt_start(size_t n, instance** inst);
static int mqtt_shutdown(size_t n, instance** inst);

#define MQTT_PORT "1883"
#define MQTT_TLS_PORT "8883"
#define MQTT_BUFFER_LENGTH 8192
#define MQTT_KEEPALIVE 10 
#define MQTT_VERSION_DEFAULT 0x05

#define MQTT5_NO_LOCAL 0x04

enum /*_mqtt_property_storage_classes*/ {
	STORAGE_U8,
	STORAGE_U16,
	STORAGE_U32,
	STORAGE_VARINT,
	STORAGE_PREFIXED,
	STORAGE_PREFIXPAIR
};

enum {
	MSG_RESERVED = 0x00,
	MSG_CONNECT = 0x10,
	MSG_CONNACK = 0x20,
	MSG_PUBLISH = 0x30,
	MSG_PUBACK = 0x40,
	MSG_PUBREC = 0x50,
	MSG_PUBREL = 0x60,
	MSG_PUBCOMP = 0x70,
	MSG_SUBSCRIBE = 0x82,
	MSG_SUBACK = 0x90,
	MSG_UNSUBSCRIBE = 0xA0,
	MSG_UNSUBACK = 0xB0,
	MSG_PINGREQ = 0xC0,
	MSG_PINGRESP = 0xD0,
	MSG_DISCONNECT = 0xE0,
	MSG_AUTH = 0xF0
};

typedef struct /*_mqtt_value_mapping*/ {
	double min;
	double max;
	double normal;
	char* discrete;
} mqtt_channel_value;

typedef struct /*_mqtt_channel*/ {
	char* topic;
	uint16_t topic_alias_sent;
	uint16_t topic_alias_rcvd;
	uint8_t flags;

	size_t values;
	mqtt_channel_value* value;
} mqtt_channel_data;

typedef struct /*_mqtt_instance_data*/ {
	uint8_t tls;
	char* host;
	char* port;
	uint8_t mqtt_version;

	char* user;
	char* password;
	char* client_id;

	size_t nchannels;
	mqtt_channel_data* channel;

	int fd;
	uint8_t receive_buffer[MQTT_BUFFER_LENGTH];
	size_t receive_offset;

	uint64_t last_control;
	uint16_t packet_identifier;
	uint16_t server_max_alias;
	uint16_t current_alias;
} mqtt_instance_data;