blob: b8373910ea7578dacff2154720da24565b3d1350 [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#ifndef AOS_CONFIGURATION_H_
2#define AOS_CONFIGURATION_H_
Brian Silverman66f079a2013-08-26 16:24:30 -07003
4#include <stdint.h>
5#include <sys/socket.h>
6#include <netinet/in.h>
7#include <arpa/inet.h>
8
James Kuszmaul3ae42262019-11-08 12:33:41 -08009#include <string_view>
10
Austin Schuhcb108412019-10-13 16:09:54 -070011#include "aos/configuration_generated.h"
12#include "aos/flatbuffers.h"
13
Brian Silverman66f079a2013-08-26 16:24:30 -070014namespace aos {
15
16// Holds global configuration data. All of the functions are safe to call
17// from wherever.
18namespace configuration {
19
Austin Schuhcb108412019-10-13 16:09:54 -070020// Reads a json configuration. This includes all imports and merges. Note:
21// duplicate imports will result in a CHECK.
Austin Schuh40485ed2019-10-26 21:51:44 -070022FlatbufferDetachedBuffer<Configuration> ReadConfig(
James Kuszmaul3ae42262019-11-08 12:33:41 -080023 const std::string_view path);
Austin Schuhcb108412019-10-13 16:09:54 -070024
Alex Perrycb7da4b2019-08-28 19:35:56 -070025// Sorts and merges entries in a config.
26FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
27 const Flatbuffer<Configuration> &config);
28
29// Adds schema definitions to a sorted and merged config from the provided
30// schema list.
31FlatbufferDetachedBuffer<Configuration> MergeConfiguration(
32 const Flatbuffer<Configuration> &config,
33 const std::vector<aos::FlatbufferString<reflection::Schema>> &schemas);
34
Austin Schuh40485ed2019-10-26 21:51:44 -070035// Returns the resolved location for a name, type, and application name. Returns
36// nullptr if none is found.
Austin Schuhcb108412019-10-13 16:09:54 -070037//
38// If the application name is empty, it is ignored. Maps are processed in
39// reverse order, and application specific first.
James Kuszmaul3ae42262019-11-08 12:33:41 -080040const Channel *GetChannel(
41 const Configuration *config, const std::string_view name,
42 const std::string_view type,
43 const std::string_view application_name);
44inline 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 Schuh40485ed2019-10-26 21:51:44 -070049 return GetChannel(&config.message(), name, type, application_name);
50}
Austin Schuhcb108412019-10-13 16:09:54 -070051
Alex Perrycb7da4b2019-08-28 19:35:56 -070052// TODO(austin): GetSchema<T>(const Flatbuffer<Configuration> &config);
53
Brian Silverman66f079a2013-08-26 16:24:30 -070054// Returns "our" IP address.
55const in_addr &GetOwnIPAddress();
56
57// Returns the "root directory" for this run. Under linux, this is the
58// directory where the executable is located (from /proc/self/exe)
59// The return value will always be to a static string, so no freeing is
60// necessary.
61const char *GetRootDirectory();
62// Returns the directory where logs get written. Relative to GetRootDirectory().
63// The return value will always be to a static string, so no freeing is
64// necessary.
65const char *GetLoggingDirectory();
66
67} // namespace configuration
Alex Perrycb7da4b2019-08-28 19:35:56 -070068
69// Compare and equality operators for Channel. Note: these only check the name
70// and type for equality.
71bool operator<(const FlatbufferDetachedBuffer<Channel> &lhs,
72 const FlatbufferDetachedBuffer<Channel> &rhs);
73bool operator==(const FlatbufferDetachedBuffer<Channel> &lhs,
74 const FlatbufferDetachedBuffer<Channel> &rhs);
Brian Silverman66f079a2013-08-26 16:24:30 -070075} // namespace aos
76
John Park398c74a2018-10-20 21:17:39 -070077#endif // AOS_CONFIGURATION_H_