This repository has been archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
14 changed files
with
1,432 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
Oops, something went wrong.