O2 2.0
A communication protocol for interactive music and media applications.
mqttcomm.h
1// mqttcomm.h -- headers for MQTT protocol implementation
2//
3// Roger B. Dannenberg
4// October 2020
5
6/*
7mqttcomm abstracts details of mqtt message formation and parsing.
8The user should subclass the abstract MQTTcomm class and implement
9methods for handling received messages.
10*/
11
12#ifndef O2_NO_MQTT
13
14class MQTTcomm : O2obj {
15 // input buffer for MQTT messages incoming:
16 Vec<uint8_t> mqtt_input;
17 int connack_count;
18 int connack_expected;
19 O2time connack_time;
20 int puback_count;
21 int puback_expected;
22 O2time puback_time;
23 int suback_count;
24 int suback_expected;
25 O2time suback_time;
26
27 int packet_id;
28
29public:
30 MQTTcomm() {
31 connack_count = 0;
32 connack_expected = 0;
33 connack_time = 0;
34 puback_count = 0;
35 puback_expected = 0;
36 puback_time = 0;
37 suback_count = 0;
38 suback_expected = 0;
39 suback_time = 0;
40
41 packet_id = 0;
42 }
43
44 O2err initialize(const char *server, int port);
45 void finish() { mqtt_input.finish(); }
46 O2err subscribe(const char *topic, bool block);
47 O2err subscribe(const char *topic) { return subscribe(topic, true); }
48 void deliver(const char *data, int len); // incoming bytes from TCP
49 bool handle_first_msg(); // process next MQTT message from input stream
50 O2err publish(const char *subtopic, const uint8_t *payload,
51 int payload_len, const char *suffix, int retain, bool block);
52 O2err publish(const char *subtopic, const uint8_t *payload,
53 int payload_len, const char *suffix, int retain) {
54 return publish(subtopic, payload, payload_len, suffix, retain, true);
55 }
56
57 // msg owned by callee, send by TCP to MQTT broker:
58 virtual O2err msg_send(O2netmsg_ptr msg, bool block) = 0;
59 O2err msg_send(O2netmsg_ptr msg) { return msg_send(msg, true); }
60 // data is owned by caller, an MQTT publish message has arrived. Handle it:
61 virtual void deliver_mqtt_msg(const char *topic, int topic_len,
62 uint8_t *payload, int payload_len) = 0;
63};
64
65#endif
Definition: mqttcomm.h:14
Definition: o2obj.h:9
Definition: vec.h:6
double O2time
O2 timestamps are doubles representing seconds since the approximate start time of the ensemble.
Definition: o2.h:625
O2err
return values used generally by O2 functions
Definition: o2.h:329
Definition: o2network.h:57