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

Commit

Permalink
feat: Apply MQTT duplex client as a frame for TA
Browse files Browse the repository at this point in the history
Apply a `mosquitto` based client as the frame of the MQTT protocol interface of
tangled-accelerator.
  • Loading branch information
howjmay committed Jul 30, 2019
1 parent 1428633 commit a517b37
Show file tree
Hide file tree
Showing 14 changed files with 1,432 additions and 0 deletions.
11 changes: 11 additions & 0 deletions accelerator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ cc_binary(
],
)

cc_binary(
name = "accelerator_mqtt",
srcs = ["mqtt_main.c"],
deps = [
":ta_config",
":ta_errors",
"//mqtt_utils",
"@entangled//utils/handles:signal",
],
)

cc_image(
name = "ta_image",
binary = ":accelerator",
Expand Down
52 changes: 52 additions & 0 deletions accelerator/mqtt_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "mqtt_utils/client_common.h"
#include "mqtt_utils/duplex_callback.h"
#include "mqtt_utils/duplex_utils.h"

int main(int argc, char *argv[]) {
rc_mosq_retcode_t ret;
mosq_config_t cfg;
struct mosquitto *mosq = NULL;

// Initialize `mosq` and `cfg`
// if we want to opertate this program under multi-threading, see https://github.com/eclipse/mosquitto/issues/450
duplex_config_init(&mosq, &cfg);

// Set callback functions
ret = duplex_callback_func_set(mosq, &cfg);
if (ret) {
fprintf(stderr, "Error: %s\n", mosquitto_strerror(ret));
goto done;
}

// Set the configures and message for testing
ret = gossip_channel_set(&cfg, HOST, TOPIC, TOPIC_RES);
if (ret) {
fprintf(stderr, "Error: %s\n", mosquitto_strerror(ret));
goto done;
}

// Set the message that is going to be sent. This function could be used in the function `duplex_loop`
// We just put it here for demostration.
ret = gossip_message_set(&cfg, MESSAGE);
if (ret) {
fprintf(stderr, "Error: %s\n", mosquitto_strerror(ret));
goto done;
}

// Set cfg as `userdata` field of `mosq` which allows the callback functions to use `cfg`.
mosquitto_user_data_set(mosq, &cfg);

// Start listening subscribing topics, once we received a message from the listening topics, we can send corresponding
// message.
// if we need to take the above task forever, just put it in a infinit loop.
ret = duplex_loop(mosq, &cfg);
if (ret) {
fprintf(stderr, "Error: %s\n", mosquitto_strerror(ret));
}

done:
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
mosq_config_cleanup(&cfg);
return ret;
}
28 changes: 28 additions & 0 deletions mqtt_utils/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cc_library(
name = "mqtt_utils",
srcs = [
"duplex_callback.c",
"duplex_utils.c",
],
hdrs = [
"duplex_callback.h",
"duplex_utils.h",
],
visibility = ["//visibility:public"],
deps = [":mqtt_common"],
)

cc_library(
name = "mqtt_common",
srcs = [
"client_common.c",
"pub_utils.c",
"sub_utils.c",
],
hdrs = [
"client_common.h",
"pub_utils.h",
"sub_utils.h",
],
deps = ["//third_party:mosquitto"],
)
Loading

0 comments on commit a517b37

Please sign in to comment.