blob: c5279576aa68c74a7f8b3be73f15ca797676f0a1 [file] [log] [blame]
Austin Schuh7bc59052020-02-16 23:48:33 -08001#include "aos/network/message_bridge_protocol.h"
2
3#include <string_view>
4
Philipp Schrader790cb542023-07-05 21:06:52 -07005#include "flatbuffers/flatbuffers.h"
6
Austin Schuh7bc59052020-02-16 23:48:33 -08007#include "aos/configuration.h"
8#include "aos/flatbuffer_merge.h"
9#include "aos/flatbuffers.h"
10#include "aos/network/connect_generated.h"
Austin Schuh7bc59052020-02-16 23:48:33 -080011
Stephan Pleinesf63bde82024-01-13 15:59:33 -080012namespace aos::message_bridge {
Austin Schuh7bc59052020-02-16 23:48:33 -080013
14aos::FlatbufferDetachedBuffer<aos::message_bridge::Connect> MakeConnectMessage(
15 const Configuration *config, const Node *my_node,
Austin Schuhb0e439d2023-05-15 10:55:40 -070016 std::string_view remote_name, const UUID &boot_uuid,
17 std::string_view config_sha256) {
Austin Schuh7bc59052020-02-16 23:48:33 -080018 CHECK(config->has_nodes()) << ": Config must have nodes to transfer.";
19
20 flatbuffers::FlatBufferBuilder fbb;
21 fbb.ForceDefaults(true);
22
Austin Schuh20ac95d2020-12-05 17:24:19 -080023 flatbuffers::Offset<flatbuffers::String> boot_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -080024 boot_uuid.PackString(&fbb);
Austin Schuh20ac95d2020-12-05 17:24:19 -080025
Austin Schuha4fc60f2020-11-01 23:06:47 -080026 flatbuffers::Offset<Node> node_offset =
27 RecursiveCopyFlatBuffer<Node>(my_node, &fbb);
Austin Schuh7bc59052020-02-16 23:48:33 -080028 const std::string_view node_name = my_node->name()->string_view();
29
30 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
31 for (const Channel *channel : *config->channels()) {
32 if (channel->has_destination_nodes()) {
33 for (const Connection *connection : *channel->destination_nodes()) {
34 if (connection->name()->string_view() == node_name &&
35 channel->source_node()->string_view() == remote_name) {
Austin Schuha1250ce2020-03-15 22:48:15 -070036 // Remove the schema to save some space on the wire.
Austin Schuh530f9ee2023-05-15 14:29:31 -070037 flatbuffers::Offset<flatbuffers::String> name_offset =
38 fbb.CreateSharedString(channel->name()->string_view());
39 flatbuffers::Offset<flatbuffers::String> type_offset =
40 fbb.CreateSharedString(channel->type()->string_view());
41
42 // We only really care about name, type, and max size.
43 Channel::Builder channel_builder(fbb);
44 channel_builder.add_name(name_offset);
45 channel_builder.add_type(type_offset);
46 channel_offsets.emplace_back(channel_builder.Finish());
Austin Schuh7bc59052020-02-16 23:48:33 -080047 }
48 }
49 }
50 }
51
52 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
53 channels_offset = fbb.CreateVector(channel_offsets);
54
Austin Schuhb0e439d2023-05-15 10:55:40 -070055 flatbuffers::Offset<flatbuffers::String> config_sha256_offset =
56 fbb.CreateString(config_sha256);
57
Austin Schuh7bc59052020-02-16 23:48:33 -080058 Connect::Builder connect_builder(fbb);
59 connect_builder.add_channels_to_transfer(channels_offset);
60 connect_builder.add_node(node_offset);
Austin Schuh20ac95d2020-12-05 17:24:19 -080061 connect_builder.add_boot_uuid(boot_uuid_offset);
Austin Schuhb0e439d2023-05-15 10:55:40 -070062 connect_builder.add_config_sha256(config_sha256_offset);
Austin Schuh7bc59052020-02-16 23:48:33 -080063 fbb.Finish(connect_builder.Finish());
64
65 return fbb.Release();
66}
67
Stephan Pleinesf63bde82024-01-13 15:59:33 -080068} // namespace aos::message_bridge