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 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 9 | #include "absl/strings/string_view.h" |
| 10 | #include "aos/configuration_generated.h" |
| 11 | #include "aos/flatbuffers.h" |
| 12 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 13 | namespace aos { |
| 14 | |
| 15 | // Holds global configuration data. All of the functions are safe to call |
| 16 | // from wherever. |
| 17 | namespace configuration { |
| 18 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 19 | // Reads a json configuration. This includes all imports and merges. Note: |
| 20 | // duplicate imports will result in a CHECK. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 21 | FlatbufferDetachedBuffer<Configuration> ReadConfig( |
| 22 | const absl::string_view path); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 23 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | // Sorts and merges entries in a config. |
| 25 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 26 | const Flatbuffer<Configuration> &config); |
| 27 | |
| 28 | // Adds schema definitions to a sorted and merged config from the provided |
| 29 | // schema list. |
| 30 | FlatbufferDetachedBuffer<Configuration> MergeConfiguration( |
| 31 | const Flatbuffer<Configuration> &config, |
| 32 | const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas); |
| 33 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 34 | // Returns the resolved location for a name, type, and application name. Returns |
| 35 | // nullptr if none is found. |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 36 | // |
| 37 | // If the application name is empty, it is ignored. Maps are processed in |
| 38 | // reverse order, and application specific first. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 39 | const Channel *GetChannel(const Configuration *config, |
| 40 | const absl::string_view name, |
| 41 | const absl::string_view type, |
| 42 | const absl::string_view application_name); |
| 43 | inline const Channel *GetChannel(const Flatbuffer<Configuration> &config, |
| 44 | const absl::string_view name, |
| 45 | const absl::string_view type, |
| 46 | const absl::string_view application_name) { |
| 47 | return GetChannel(&config.message(), name, type, application_name); |
| 48 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 49 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 50 | // TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config); |
| 51 | |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 52 | // Returns "our" IP address. |
| 53 | const in_addr &GetOwnIPAddress(); |
| 54 | |
| 55 | // Returns the "root directory" for this run. Under linux, this is the |
| 56 | // directory where the executable is located (from /proc/self/exe) |
| 57 | // The return value will always be to a static string, so no freeing is |
| 58 | // necessary. |
| 59 | const char *GetRootDirectory(); |
| 60 | // Returns the directory where logs get written. Relative to GetRootDirectory(). |
| 61 | // The return value will always be to a static string, so no freeing is |
| 62 | // necessary. |
| 63 | const char *GetLoggingDirectory(); |
| 64 | |
| 65 | } // namespace configuration |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 66 | |
| 67 | // Compare and equality operators for Channel. Note: these only check the name |
| 68 | // and type for equality. |
| 69 | bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 70 | const FlatbufferDetachedBuffer<Channel> &rhs); |
| 71 | bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs, |
| 72 | const FlatbufferDetachedBuffer<Channel> &rhs); |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 73 | } // namespace aos |
| 74 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 75 | #endif // AOS_CONFIGURATION_H_ |