John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 1 | #ifndef AOS_CONFIGURATION_H_ |
| 2 | #define AOS_CONFIGURATION_H_ |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 3 | |
| 4 | #include <stdint.h> |
| 5 | #include <sys/socket.h> |
| 6 | #include <netinet/in.h> |
| 7 | #include <arpa/inet.h> |
| 8 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 9 | #include <string_view> |
| 10 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 11 | #include "aos/configuration_generated.h" |
| 12 | #include "aos/flatbuffers.h" |
| 13 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 14 | namespace aos { |
| 15 | |
| 16 | // Holds global configuration data. All of the functions are safe to call |
| 17 | // from wherever. |
| 18 | namespace configuration { |
| 19 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 20 | // Reads a json configuration. This includes all imports and merges. Note: |
| 21 | // duplicate imports will result in a CHECK. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 22 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 23 | const std::string_view path); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 24 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | // Sorts and merges entries in a config. |
| 26 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 27 | const Flatbuffer<Configuration> &config); |
| 28 | |
| 29 | // Adds schema definitions to a sorted and merged config from the provided |
| 30 | // schema list. |
| 31 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 32 | const Flatbuffer<Configuration> &config, |
| 33 | const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas); |
| 34 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 35 | // Returns the resolved location for a name, type, and application name. Returns |
| 36 | // nullptr if none is found. |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 37 | // |
| 38 | // If the application name is empty, it is ignored. Maps are processed in |
| 39 | // reverse order, and application specific first. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 40 | const Channel *GetChannel( |
| 41 | const Configuration *config, const std::string_view name, |
| 42 | const std::string_view type, |
| 43 | const std::string_view application_name); |
| 44 | inline const Channel *GetChannel( |
| 45 | const Flatbuffer<Configuration> &config, |
| 46 | const std::string_view name, |
| 47 | const std::string_view type, |
| 48 | const std::string_view application_name) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 49 | return GetChannel(&config.message(), name, type, application_name); |
| 50 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 51 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 52 | // Returns the Node out of the config with the matching name, or nullptr if it |
| 53 | // can't be found. |
| 54 | const Node *GetNode(const Configuration *config, std::string_view name); |
| 55 | // Returns the Node out of the configuration which matches our hostname. |
| 56 | // CHECKs if it can't be found. |
| 57 | const Node *GetMyNode(const Configuration *config); |
| 58 | const Node *GetNodeFromHostname(const Configuration *config, |
| 59 | std::string_view name); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 60 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 61 | // Returns true if the provided channel is sendable on the provided node. |
| 62 | bool ChannelIsSendableOnNode(const Channel *channel, const Node *node); |
| 63 | // Returns true if the provided channel is able to be watched or fetched on the |
| 64 | // provided node. |
| 65 | bool ChannelIsReadableOnNode(const Channel *channel, const Node *node); |
| 66 | |
| 67 | // TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 68 | |
| 69 | // Returns the "root directory" for this run. Under linux, this is the |
| 70 | // directory where the executable is located (from /proc/self/exe) |
| 71 | // The return value will always be to a static string, so no freeing is |
| 72 | // necessary. |
| 73 | const char *GetRootDirectory(); |
| 74 | // Returns the directory where logs get written. Relative to GetRootDirectory(). |
| 75 | // The return value will always be to a static string, so no freeing is |
| 76 | // necessary. |
| 77 | const char *GetLoggingDirectory(); |
| 78 | |
| 79 | } // namespace configuration |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 80 | |
| 81 | // Compare and equality operators for Channel. Note: these only check the name |
| 82 | // and type for equality. |
| 83 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 84 | const FlatbufferDetachedBuffer<Channel> &rhs); |
| 85 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 86 | const FlatbufferDetachedBuffer<Channel> &rhs); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 87 | } // namespace aos |
| 88 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 89 | #endif // AOS_CONFIGURATION_H_ |