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

Commit a517b37

Browse files
committed
feat: Apply MQTT duplex client as a frame for TA
Apply a `mosquitto` based client as the frame of the MQTT protocol interface of tangled-accelerator.
1 parent 1428633 commit a517b37

File tree

14 files changed

+1432
-0
lines changed

14 files changed

+1432
-0
lines changed

accelerator/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ cc_binary(
2626
],
2727
)
2828

29+
cc_binary(
30+
name = "accelerator_mqtt",
31+
srcs = ["mqtt_main.c"],
32+
deps = [
33+
":ta_config",
34+
":ta_errors",
35+
"//mqtt_utils",
36+
"@entangled//utils/handles:signal",
37+
],
38+
)
39+
2940
cc_image(
3041
name = "ta_image",
3142
binary = ":accelerator",

accelerator/mqtt_main.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "mqtt_utils/client_common.h"
2+
#include "mqtt_utils/duplex_callback.h"
3+
#include "mqtt_utils/duplex_utils.h"
4+
5+
int main(int argc, char *argv[]) {
6+
rc_mosq_retcode_t ret;
7+
mosq_config_t cfg;
8+
struct mosquitto *mosq = NULL;
9+
10+
// Initialize `mosq` and `cfg`
11+
// if we want to opertate this program under multi-threading, see https://github.com/eclipse/mosquitto/issues/450
12+
duplex_config_init(&mosq, &cfg);
13+
14+
// Set callback functions
15+
ret = duplex_callback_func_set(mosq, &cfg);
16+
if (ret) {
17+
fprintf(stderr, "Error: %s\n", mosquitto_strerror(ret));
18+
goto done;
19+
}
20+
21+
// Set the configures and message for testing
22+
ret = gossip_channel_set(&cfg, HOST, TOPIC, TOPIC_RES);
23+
if (ret) {
24+
fprintf(stderr, "Error: %s\n", mosquitto_strerror(ret));
25+
goto done;
26+
}
27+
28+
// Set the message that is going to be sent. This function could be used in the function `duplex_loop`
29+
// We just put it here for demostration.
30+
ret = gossip_message_set(&cfg, MESSAGE);
31+
if (ret) {
32+
fprintf(stderr, "Error: %s\n", mosquitto_strerror(ret));
33+
goto done;
34+
}
35+
36+
// Set cfg as `userdata` field of `mosq` which allows the callback functions to use `cfg`.
37+
mosquitto_user_data_set(mosq, &cfg);
38+
39+
// Start listening subscribing topics, once we received a message from the listening topics, we can send corresponding
40+
// message.
41+
// if we need to take the above task forever, just put it in a infinit loop.
42+
ret = duplex_loop(mosq, &cfg);
43+
if (ret) {
44+
fprintf(stderr, "Error: %s\n", mosquitto_strerror(ret));
45+
}
46+
47+
done:
48+
mosquitto_destroy(mosq);
49+
mosquitto_lib_cleanup();
50+
mosq_config_cleanup(&cfg);
51+
return ret;
52+
}

mqtt_utils/BUILD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cc_library(
2+
name = "mqtt_utils",
3+
srcs = [
4+
"duplex_callback.c",
5+
"duplex_utils.c",
6+
],
7+
hdrs = [
8+
"duplex_callback.h",
9+
"duplex_utils.h",
10+
],
11+
visibility = ["//visibility:public"],
12+
deps = [":mqtt_common"],
13+
)
14+
15+
cc_library(
16+
name = "mqtt_common",
17+
srcs = [
18+
"client_common.c",
19+
"pub_utils.c",
20+
"sub_utils.c",
21+
],
22+
hdrs = [
23+
"client_common.h",
24+
"pub_utils.h",
25+
"sub_utils.h",
26+
],
27+
deps = ["//third_party:mosquitto"],
28+
)

0 commit comments

Comments
 (0)