blob: c661c4c56cf85225c83219e65d9f4617e9d8c2d5 [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
12namespace aos {
13namespace message_bridge {
14
15aos::FlatbufferDetachedBuffer<aos::message_bridge::Connect> MakeConnectMessage(
16 const Configuration *config, const Node *my_node,
Austin Schuhb0e439d2023-05-15 10:55:40 -070017 std::string_view remote_name, const UUID &boot_uuid,
18 std::string_view config_sha256) {
Austin Schuh7bc59052020-02-16 23:48:33 -080019 CHECK(config->has_nodes()) << ": Config must have nodes to transfer.";
20
21 flatbuffers::FlatBufferBuilder fbb;
22 fbb.ForceDefaults(true);
23
Austin Schuh20ac95d2020-12-05 17:24:19 -080024 flatbuffers::Offset<flatbuffers::String> boot_uuid_offset =
Austin Schuh5e2bfb82021-03-13 22:46:55 -080025 boot_uuid.PackString(&fbb);
Austin Schuh20ac95d2020-12-05 17:24:19 -080026
Austin Schuha4fc60f2020-11-01 23:06:47 -080027 flatbuffers::Offset<Node> node_offset =
28 RecursiveCopyFlatBuffer<Node>(my_node, &fbb);
Austin Schuh7bc59052020-02-16 23:48:33 -080029 const std::string_view node_name = my_node->name()->string_view();
30
31 std::vector<flatbuffers::Offset<Channel>> channel_offsets;
32 for (const Channel *channel : *config->channels()) {
33 if (channel->has_destination_nodes()) {
34 for (const Connection *connection : *channel->destination_nodes()) {
35 if (connection->name()->string_view() == node_name &&
36 channel->source_node()->string_view() == remote_name) {
Austin Schuha1250ce2020-03-15 22:48:15 -070037 // Remove the schema to save some space on the wire.
Austin Schuh530f9ee2023-05-15 14:29:31 -070038 flatbuffers::Offset<flatbuffers::String> name_offset =
39 fbb.CreateSharedString(channel->name()->string_view());
40 flatbuffers::Offset<flatbuffers::String> type_offset =
41 fbb.CreateSharedString(channel->type()->string_view());
42
43 // We only really care about name, type, and max size.
44 Channel::Builder channel_builder(fbb);
45 channel_builder.add_name(name_offset);
46 channel_builder.add_type(type_offset);
47 channel_offsets.emplace_back(channel_builder.Finish());
Austin Schuh7bc59052020-02-16 23:48:33 -080048 }
49 }
50 }
51 }
52
53 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>>
54 channels_offset = fbb.CreateVector(channel_offsets);
55
Austin Schuhb0e439d2023-05-15 10:55:40 -070056 flatbuffers::Offset<flatbuffers::String> config_sha256_offset =
57 fbb.CreateString(config_sha256);
58
Austin Schuh7bc59052020-02-16 23:48:33 -080059 Connect::Builder connect_builder(fbb);
60 connect_builder.add_channels_to_transfer(channels_offset);
61 connect_builder.add_node(node_offset);
Austin Schuh20ac95d2020-12-05 17:24:19 -080062 connect_builder.add_boot_uuid(boot_uuid_offset);
Austin Schuhb0e439d2023-05-15 10:55:40 -070063 connect_builder.add_config_sha256(config_sha256_offset);
Austin Schuh7bc59052020-02-16 23:48:33 -080064 fbb.Finish(connect_builder.Finish());
65
66 return fbb.Release();
67}
68
69} // namespace message_bridge
70} // namespace aos