Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 0645641

Browse files
committed
feat: Construct MQTT interface of TA
Construct the rough structure of MQTT interface for tangle-accelerator. The main function is in `mqtt_main.c`. Callback functions of MQTT client locate in `duplex_callback.[c, h]`, and more important the `mqtt_request_handler()` locates in `duplex_callback.c` as well. Other functions for `main()` function locate in `client_common.[c, h]` CAUTION: The current version is single thread. To support multiple thread, it necessitates reformation of the code.
1 parent 5783147 commit 0645641

25 files changed

+859
-678
lines changed

Doxyfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ INPUT = . \
88
request \
99
response \
1010
serializer \
11-
utils
11+
utils \
12+
connectivity/mqtt
1213
FILE_PATTERNS = *.h \
1314
*.md
1415
EXAMPLE_PATH = tests

accelerator/BUILD

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,15 @@ cc_binary(
2828

2929
cc_binary(
3030
name = "accelerator_mqtt",
31-
srcs = ["mqtt_main.c"],
31+
srcs = ["mqtt_interface.c"],
32+
copts = [
33+
"-DLOGGER_ENABLE",
34+
"-DENABLE_MQTT",
35+
],
3236
deps = [
3337
":ta_config",
3438
":ta_errors",
35-
"//mqtt_utils",
39+
"//connectivity/mqtt:mqtt_utils",
3640
"@entangled//utils/handles:signal",
3741
],
3842
)

accelerator/config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ extern "C" {
2828

2929
#define TA_VERSION "tangle-accelerator/0.5.0"
3030
#define TA_HOST "localhost"
31+
#ifdef ENABLE_MQTT
32+
#define MQTT_HOST "localhost"
33+
#define TOPIC_ROOT "NB/root/topics"
34+
#endif
3135
#define TA_PORT "8000"
3236
#define TA_THREAD_COUNT 10
3337
#define IRI_HOST "localhost"

accelerator/errors.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern "C" {
5454
#define SC_MODULE_CONF (0x07 << SC_MODULE_SHIFT)
5555
#define SC_MODULE_UTILS (0x08 << SC_MODULE_SHIFT)
5656
#define SC_MODULE_HTTP (0x09 << SC_MODULE_SHIFT)
57+
#define SC_MODULE_MQTT (0x0A << SC_MODULE_SHIFT)
5758
/** @} */
5859

5960
/** @name serverity code */
@@ -168,6 +169,28 @@ typedef enum {
168169
/**< URL doesn't match regular expression rule */
169170
SC_HTTP_URL_PARSE_ERROR = 0x05 | SC_MODULE_HTTP | SC_SEVERITY_MAJOR,
170171
/**< URL parameter parsing error */
172+
173+
// MQTT module
174+
SC_MQTT_OOM = 0x01 | SC_MODULE_MQTT | SC_SEVERITY_FATAL,
175+
/**< Fail to create MQTT object */
176+
SC_MQTT_NULL = 0x02 | SC_MODULE_MQTT | SC_SEVERITY_FATAL,
177+
/**< NULL object in MQTT */
178+
SC_MQTT_INVALID_REGEX = 0x03 | SC_MODULE_MQTT | SC_SEVERITY_MAJOR,
179+
/**< Invalid URL regular expression rule in MQTT */
180+
SC_MQTT_TOPIC_NOT_MATCH = 0x04 | SC_MODULE_MQTT | SC_SEVERITY_MAJOR,
181+
/**< Topic doesn't match regular expression rule */
182+
SC_MQTT_URL_PARSE_ERROR = 0x05 | SC_MODULE_MQTT | SC_SEVERITY_MAJOR,
183+
/**< Topic parameter parsing error */
184+
SC_MQTT_INIT = 0x06 | SC_MODULE_MQTT | SC_SEVERITY_MAJOR,
185+
/**< Error during initialization*/
186+
SC_MOSQ_OBJ_INIT_ERROR = 0x07 | SC_MODULE_MQTT | SC_SEVERITY_MAJOR,
187+
/**< Error in initializing mosquitto object */
188+
SC_MQTT_TOPIC_SET = 0x08 | SC_MODULE_MQTT | SC_SEVERITY_MAJOR,
189+
/**< Error in setting topic */
190+
SC_MQTT_OPT_SET = 0x09 | SC_MODULE_MQTT | SC_SEVERITY_MAJOR,
191+
/**< Error in setting options of `struct mosquitto` object */
192+
SC_CLIENT_CONNTECT = 0x0A | SC_MODULE_MQTT | SC_SEVERITY_MAJOR,
193+
/**< Error in connecting to broker */
171194
} status_t;
172195

173196
typedef enum {

accelerator/mqtt_interface.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "config.h"
2+
#include "connectivity/mqtt/client_common.h"
3+
#include "connectivity/mqtt/duplex_callback.h"
4+
#include "connectivity/mqtt/duplex_utils.h"
5+
#include "errors.h"
6+
7+
int main(int argc, char *argv[]) {
8+
status_t ret;
9+
mosq_config_t cfg;
10+
struct mosquitto *mosq = NULL;
11+
12+
// Initialize `mosq` and `cfg`
13+
// if we want to opertate this program under multi-threading, see https://github.com/eclipse/mosquitto/issues/450
14+
ret = duplex_config_init(&mosq, &cfg);
15+
if (ret != SC_OK) {
16+
goto done;
17+
}
18+
19+
// Set callback functions
20+
duplex_callback_func_set(mosq);
21+
22+
// The following one line is used for testing if this server work fine with requests with given topics.
23+
// Uncomment it if it is necessitated
24+
// gossip_channel_set(&cfg, MQTT_HOST, "NB/test/room1", "NB/test/room2");
25+
26+
// Set the configures and message for testing
27+
ret = gossip_api_channels_set(&cfg, MQTT_HOST, TOPIC_ROOT);
28+
if (ret != SC_OK) {
29+
goto done;
30+
}
31+
32+
// Set cfg as `userdata` field of `mosq` which allows the callback functions to use `cfg`.
33+
mosquitto_user_data_set(mosq, &cfg);
34+
35+
// Start listening subscribing topics, once we received a message from the listening topics, we can send corresponding
36+
// message.
37+
// if we need to take the above task forever, just put it in a infinite loop.
38+
do {
39+
ret = duplex_client_start(mosq, &cfg);
40+
} while (!ret);
41+
42+
done:
43+
mosquitto_destroy(mosq);
44+
mosquitto_lib_cleanup();
45+
mosq_config_free(&cfg);
46+
return ret;
47+
}

accelerator/mqtt_main.c

Lines changed: 0 additions & 52 deletions
This file was deleted.

mqtt_utils/BUILD renamed to connectivity/mqtt/BUILD

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ cc_library(
99
"duplex_utils.h",
1010
],
1111
visibility = ["//visibility:public"],
12-
deps = [":mqtt_common"],
12+
deps = [
13+
":mqtt_common",
14+
"//accelerator:ta_errors",
15+
],
1316
)
1417

1518
cc_library(
@@ -24,5 +27,9 @@ cc_library(
2427
"pub_utils.h",
2528
"sub_utils.h",
2629
],
27-
deps = ["//third_party:mosquitto"],
30+
deps = [
31+
"//accelerator:ta_config",
32+
"//accelerator:ta_errors",
33+
"//third_party:mosquitto",
34+
],
2835
)

0 commit comments

Comments
 (0)