1
#include "program_options.h"
3
#include "com/ubuntu/location/provider_factory.h"
5
#include "com/ubuntu/location/service/default_configuration.h"
6
#include "com/ubuntu/location/service/implementation.h"
8
#include <org/freedesktop/dbus/announcer.h>
9
#include <org/freedesktop/dbus/asio/executor.h>
13
namespace cul = com::ubuntu::location;
14
namespace culs = com::ubuntu::location::service;
15
namespace dbus = org::freedesktop::dbus;
17
int main(int argc, char** argv)
19
cul::ProgramOptions options;
21
options.add("help", "Produces this help message");
24
"The well-known bus to announce the service upon",
25
std::string{"session"});
26
options.add_composed<std::vector<std::string>>(
28
"The providers that should be added to the engine");
30
if (!options.parse_from_command_line_args(argc, argv))
33
if (options.value_count_for_key("help") > 0)
35
options.print_help(std::cout);
39
if (options.value_count_for_key("provider") == 0)
41
std::cout << "A set of providers need to be specified. The following providers are known:" << std::endl;
42
cul::ProviderFactory::instance().enumerate(
43
[](const std::string& name, const cul::ProviderFactory::Factory&)
45
std::cout << "\t" << name << std::endl;
50
auto selected_providers = options.value_for_key<std::vector<std::string>>("provider");
52
std::map<std::string, cul::ProviderFactory::Configuration> config_lut;
53
std::set<cul::Provider::Ptr> instantiated_providers;
55
for (const std::string& provider : selected_providers)
57
std::cout << "Instantiating and configuring: " << provider << std::endl;
58
options.enumerate_unrecognized_options(
59
[&config_lut, provider](const std::string& s)
61
std::stringstream in(s);
62
std::string key, value;
64
std::getline(in, key, '=');
65
std::getline(in, value, '=');
67
std::size_t pos = key.find(provider);
68
if (pos == std::string::npos)
70
static const std::string option_marker{"--"};
71
static const std::string scope_separator{"::"};
72
key = key.erase(key.find_first_of(option_marker), option_marker.size());
73
key = key.erase(key.find_first_of(provider), provider.size());
74
key = key.erase(key.find_first_of(scope_separator), scope_separator.size());
76
std::cout << "\t" << key << " -> " << value << std::endl;
78
config_lut[provider][key] = value;
83
auto p = cul::ProviderFactory::instance().create_provider_for_name_with_config(
85
config_lut[provider]);
88
instantiated_providers.insert(p);
90
throw std::runtime_error("Problem instantiating provider");
92
} catch(const std::runtime_error& e)
94
std::cerr << "Exception instantiating provider: " << e.what() << " ... Aborting now." << std::endl;
99
static const std::map<std::string, dbus::WellKnownBus> lut =
101
{"session", dbus::WellKnownBus::session},
102
{"system", dbus::WellKnownBus::system},
107
new dbus::Bus{lut.at(options.value_for_key<std::string>("bus"))}
110
bus->install_executor(
112
new dbus::asio::Executor{bus}));
114
culs::DefaultConfiguration config;
116
auto location_service =
117
dbus::announce_service_on_bus<
123
instantiated_providers,
124
config.the_provider_selection_policy()),
125
config.the_permission_manager());
127
std::thread t{[bus](){bus->run();}};