Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 1 | #include "aos/network/message_bridge_protocol.h" |
| 2 | |
| 3 | #include <string_view> |
| 4 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 5 | #include "flatbuffers/flatbuffers.h" |
| 6 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 7 | #include "aos/configuration.h" |
| 8 | #include "aos/flatbuffer_merge.h" |
| 9 | #include "aos/flatbuffers.h" |
| 10 | #include "aos/network/connect_generated.h" |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 11 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 12 | namespace aos::message_bridge { |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 13 | |
| 14 | aos::FlatbufferDetachedBuffer<aos::message_bridge::Connect> MakeConnectMessage( |
| 15 | const Configuration *config, const Node *my_node, |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 16 | std::string_view remote_name, const UUID &boot_uuid, |
| 17 | std::string_view config_sha256) { |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 18 | CHECK(config->has_nodes()) << ": Config must have nodes to transfer."; |
| 19 | |
| 20 | flatbuffers::FlatBufferBuilder fbb; |
| 21 | fbb.ForceDefaults(true); |
| 22 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 23 | flatbuffers::Offset<flatbuffers::String> boot_uuid_offset = |
Austin Schuh | 5e2bfb8 | 2021-03-13 22:46:55 -0800 | [diff] [blame] | 24 | boot_uuid.PackString(&fbb); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 25 | |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 26 | flatbuffers::Offset<Node> node_offset = |
| 27 | RecursiveCopyFlatBuffer<Node>(my_node, &fbb); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 28 | 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 Schuh | a1250ce | 2020-03-15 22:48:15 -0700 | [diff] [blame] | 36 | // Remove the schema to save some space on the wire. |
Austin Schuh | 530f9ee | 2023-05-15 14:29:31 -0700 | [diff] [blame] | 37 | 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 Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Channel>>> |
| 53 | channels_offset = fbb.CreateVector(channel_offsets); |
| 54 | |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 55 | flatbuffers::Offset<flatbuffers::String> config_sha256_offset = |
| 56 | fbb.CreateString(config_sha256); |
| 57 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 58 | Connect::Builder connect_builder(fbb); |
| 59 | connect_builder.add_channels_to_transfer(channels_offset); |
| 60 | connect_builder.add_node(node_offset); |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 61 | connect_builder.add_boot_uuid(boot_uuid_offset); |
Austin Schuh | b0e439d | 2023-05-15 10:55:40 -0700 | [diff] [blame] | 62 | connect_builder.add_config_sha256(config_sha256_offset); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 63 | fbb.Finish(connect_builder.Finish()); |
| 64 | |
| 65 | return fbb.Release(); |
| 66 | } |
| 67 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 68 | } // namespace aos::message_bridge |