diff options
Diffstat (limited to 'ahl-binding/ahl-binding.cpp')
-rw-r--r-- | ahl-binding/ahl-binding.cpp | 528 |
1 files changed, 264 insertions, 264 deletions
diff --git a/ahl-binding/ahl-binding.cpp b/ahl-binding/ahl-binding.cpp index 98d36b6..3a16197 100644 --- a/ahl-binding/ahl-binding.cpp +++ b/ahl-binding/ahl-binding.cpp @@ -25,7 +25,7 @@ */ int ahl_api_create(void*, afb_api_t handle) { - return ahl_binding_t::instance().preinit(handle); + return ahl_binding_t::instance().preinit(handle); } /** @@ -35,19 +35,19 @@ int ahl_api_create(void*, afb_api_t handle) */ int afbBindingEntry(afb_api_t handle) { - using namespace std::placeholders; - assert(handle != nullptr); - - afb_api_new_api( - handle, - HL_API_NAME, - HL_API_INFO, - 1, - ahl_api_create, - nullptr - ); - - return 0; + using namespace std::placeholders; + assert(handle != nullptr); + + afb_api_new_api( + handle, + HL_API_NAME, + HL_API_INFO, + 1, + ahl_api_create, + nullptr + ); + + return 0; } /** @@ -56,7 +56,7 @@ int afbBindingEntry(afb_api_t handle) */ int ahl_api_init(afb_api_t) { - return ahl_binding_t::instance().init(); + return ahl_binding_t::instance().init(); } /** @@ -66,7 +66,7 @@ int ahl_api_init(afb_api_t) */ void ahl_api_on_event(afb_api_t, const char* e, struct json_object* o) { - ahl_binding_t::instance().event(e, o); + ahl_binding_t::instance().event(e, o); } /** @@ -76,7 +76,7 @@ void ahl_api_on_event(afb_api_t, const char* e, struct json_object* o) */ int ahl_api_config_roles(afb_api_t, CtlSectionT*, json_object* o) { - return ahl_binding_t::instance().parse_roles_config(o); + return ahl_binding_t::instance().parse_roles_config(o); } /** @@ -85,7 +85,7 @@ int ahl_api_config_roles(afb_api_t, CtlSectionT*, json_object* o) */ void ahl_api_get_roles(afb_req_t req) { - ahl_binding_t::instance().get_roles(req); + ahl_binding_t::instance().get_roles(req); } /** @@ -94,7 +94,7 @@ void ahl_api_get_roles(afb_req_t req) */ void ahl_api_subscribe(afb_req_t req) { - ahl_binding_t::instance().subscribe(req); + ahl_binding_t::instance().subscribe(req); } /** @@ -103,7 +103,7 @@ void ahl_api_subscribe(afb_req_t req) */ void ahl_api_unsubscribe(afb_req_t req) { - ahl_binding_t::instance().unsubscribe(req); + ahl_binding_t::instance().unsubscribe(req); } /** @@ -114,17 +114,17 @@ void ahl_api_unsubscribe(afb_req_t req) */ void ahl_api_role(afb_req_t req) { - role_t* role = (role_t*)req->vcbdata; - assert(role != nullptr); + role_t* role = (role_t*)req->vcbdata; + assert(role != nullptr); - role->invoke(req); + role->invoke(req); } /** * @brief Default constructor. */ ahl_binding_t::ahl_binding_t() - : handle_{nullptr} + : handle_{nullptr} { } @@ -134,8 +134,8 @@ ahl_binding_t::ahl_binding_t() */ ahl_binding_t& ahl_binding_t::instance() { - static ahl_binding_t s; - return s; + static ahl_binding_t s; + return s; } /** @@ -145,26 +145,26 @@ ahl_binding_t& ahl_binding_t::instance() */ int ahl_binding_t::preinit(afb_api_t handle) { - handle_ = handle; - - try - { - load_static_verbs(); - load_controller_configs(); - - if (afb_api_on_event(handle_, ahl_api_on_event)) - throw std::runtime_error("Failed to register event handler callback."); - - if (afb_api_on_init(handle_, ahl_api_init)) - throw std::runtime_error("Failed to register init handler callback."); - } - catch(std::exception& e) - { - AFB_API_ERROR(handle, "%s", e.what()); - return -1; - } - - return 0; + handle_ = handle; + + try + { + load_static_verbs(); + load_controller_configs(); + + if (afb_api_on_event(handle_, ahl_api_on_event)) + throw std::runtime_error("Failed to register event handler callback."); + + if (afb_api_on_init(handle_, ahl_api_init)) + throw std::runtime_error("Failed to register init handler callback."); + } + catch(std::exception& e) + { + AFB_API_ERROR(handle, "%s", e.what()); + return -1; + } + + return 0; } /** @@ -172,17 +172,17 @@ int ahl_binding_t::preinit(afb_api_t handle) */ int ahl_binding_t::init() { - using namespace std::placeholders; + using namespace std::placeholders; - if (afb_api_require_api(handle_, HAL_MGR_API, 1)) - { - AFB_API_ERROR(handle_, "Failed to require '%s' API!", HAL_MGR_API); - return -1; - } - AFB_API_NOTICE(handle_, "Required '%s' API found!", HAL_MGR_API); + if (afb_api_require_api(handle_, HAL_MGR_API, 1)) + { + AFB_API_ERROR(handle_, "Failed to require '%s' API!", HAL_MGR_API); + return -1; + } + AFB_API_NOTICE(handle_, "Required '%s' API found!", HAL_MGR_API); - afb_api_seal(handle_); - AFB_API_NOTICE(handle_, "API is now sealed!"); + afb_api_seal(handle_); + AFB_API_NOTICE(handle_, "API is now sealed!"); volume_changed_ = afb_api_make_event(handle_, "volume_changed"); if(!afb_event_is_valid(volume_changed_)) @@ -191,8 +191,8 @@ int ahl_binding_t::init() return -2; } - if (update_streams()) return -1; - return 0; + if (update_streams()) return -1; + return 0; } /** @@ -201,70 +201,70 @@ int ahl_binding_t::init() */ int ahl_binding_t::update_streams() { - json_object* loaded = nullptr; - size_t hals_count = 0, streams_count = 0; - - if (afb_api_call_sync(handle_, "4a-hal-manager", "loaded", json_object_new_object(), &loaded, nullptr, nullptr)) - { - AFB_API_ERROR(handle_, "Failed to call 'loaded' verb on '4a-hal-manager' API!"); - if (loaded) - { - AFB_API_ERROR(handle_, "%s", json_object_to_json_string(loaded)); - json_object_put(loaded); - } - return -1; - } - - if (!json_object_is_type(loaded, json_type_array)) - { - AFB_API_ERROR(handle_, "Expected an array from '4a-hal-manager/loaded', but got something else!"); - json_object_put(loaded); - return -1; - } - hals_count = json_object_array_length(loaded); - - for(int i = 0; i < hals_count; ++i) - { - json_object* info = nullptr; - - const char* halname = json_object_get_string(json_object_array_get_idx(loaded, i)); - AFB_API_DEBUG(handle_, "Found an active HAL: %s", halname); - - if (afb_api_call_sync(handle_, halname, "info", json_object_new_object(), &info, nullptr, nullptr)) - { - AFB_API_ERROR(handle_, "Failed to call 'info' verb on '%s' API!", halname); - if (info) - { - AFB_API_ERROR(handle_, "%s", json_object_to_json_string(info)); - json_object_put(info); - } - json_object_put(loaded); - return -1; - } - - json_object* streamsJ = nullptr; - json_object_object_get_ex(info, "streams", &streamsJ); - streams_count = json_object_array_length(streamsJ); - for(int j = 0; j < streams_count; ++j) - { - json_object * nameJ = nullptr, * cardIdJ = nullptr; - json_object * streamJ = json_object_array_get_idx(streamsJ, j); - - json_object_object_get_ex(streamJ, "name", &nameJ); - json_object_object_get_ex(streamJ, "cardId", &cardIdJ); - - update_stream( - halname, - json_object_get_string(nameJ), - json_object_get_string(cardIdJ) - ); - } - - json_object_put(info); - } - json_object_put(loaded); - - return 0; + json_object* loaded = nullptr; + size_t hals_count = 0, streams_count = 0; + + if (afb_api_call_sync(handle_, "4a-hal-manager", "loaded", json_object_new_object(), &loaded, nullptr, nullptr)) + { + AFB_API_ERROR(handle_, "Failed to call 'loaded' verb on '4a-hal-manager' API!"); + if (loaded) + { + AFB_API_ERROR(handle_, "%s", json_object_to_json_string(loaded)); + json_object_put(loaded); + } + return -1; + } + + if (!json_object_is_type(loaded, json_type_array)) + { + AFB_API_ERROR(handle_, "Expected an array from '4a-hal-manager/loaded', but got something else!"); + json_object_put(loaded); + return -1; + } + hals_count = json_object_array_length(loaded); + + for(int i = 0; i < hals_count; ++i) + { + json_object* info = nullptr; + + const char* halname = json_object_get_string(json_object_array_get_idx(loaded, i)); + AFB_API_DEBUG(handle_, "Found an active HAL: %s", halname); + + if (afb_api_call_sync(handle_, halname, "info", json_object_new_object(), &info, nullptr, nullptr)) + { + AFB_API_ERROR(handle_, "Failed to call 'info' verb on '%s' API!", halname); + if (info) + { + AFB_API_ERROR(handle_, "%s", json_object_to_json_string(info)); + json_object_put(info); + } + json_object_put(loaded); + return -1; + } + + json_object* streamsJ = nullptr; + json_object_object_get_ex(info, "streams", &streamsJ); + streams_count = json_object_array_length(streamsJ); + for(int j = 0; j < streams_count; ++j) + { + json_object * nameJ = nullptr, * cardIdJ = nullptr; + json_object * streamJ = json_object_array_get_idx(streamsJ, j); + + json_object_object_get_ex(streamJ, "name", &nameJ); + json_object_object_get_ex(streamJ, "cardId", &cardIdJ); + + update_stream( + halname, + json_object_get_string(nameJ), + json_object_get_string(cardIdJ) + ); + } + + json_object_put(info); + } + json_object_put(loaded); + + return 0; } /** @@ -275,179 +275,179 @@ int ahl_binding_t::update_streams() */ void ahl_binding_t::update_stream(std::string halname, std::string stream, std::string deviceid) { - for(auto& r : roles_) - { - if(r.stream() == stream) - { - if (r.device_uri().size()) - AFB_API_WARNING(handle_, "Multiple stream with same name: '%s'.", stream.c_str()); - else - { - r.device_uri(deviceid); - r.hal(halname); - } - } - } + for(auto& r : roles_) + { + if(r.stream() == stream) + { + if (r.device_uri().size()) + AFB_API_WARNING(handle_, "Multiple stream with same name: '%s'.", stream.c_str()); + else + { + r.device_uri(deviceid); + r.hal(halname); + } + } + } } void ahl_binding_t::event(std::string name, json_object* arg) { - AFB_API_DEBUG(handle_, "Event '%s' received with the following arg: %s", name.c_str(), json_object_to_json_string(arg)); + AFB_API_DEBUG(handle_, "Event '%s' received with the following arg: %s", name.c_str(), json_object_to_json_string(arg)); } void ahl_binding_t::load_static_verbs() { - if (afb_api_add_verb( - handle_, - "get_roles", - "Retrieve array of available audio roles", - ahl_api_get_roles, - nullptr, - nullptr, - AFB_SESSION_NONE_X2, 0)) - { - throw std::runtime_error("Failed to add 'get_role' verb to the API."); - } - - if (afb_api_add_verb( - handle_, - "subscribe", - "Subscribe to \"volume_changed\" event", - ahl_api_subscribe, - nullptr, - nullptr, - AFB_SESSION_NONE_X2, 0)) - { - throw std::runtime_error("Failed to add 'subscribe' verb to the API."); - } - - if (afb_api_add_verb( - handle_, - "unsubscribe", - "Unsubscribe to \"volume_changed\" event", - ahl_api_unsubscribe, - nullptr, - nullptr, - AFB_SESSION_NONE_X2, 0)) - { - throw std::runtime_error("Failed to add 'unsubscribe' verb to the API."); - } + if (afb_api_add_verb( + handle_, + "get_roles", + "Retrieve array of available audio roles", + ahl_api_get_roles, + nullptr, + nullptr, + AFB_SESSION_NONE_X2, 0)) + { + throw std::runtime_error("Failed to add 'get_role' verb to the API."); + } + + if (afb_api_add_verb( + handle_, + "subscribe", + "Subscribe to \"volume_changed\" event", + ahl_api_subscribe, + nullptr, + nullptr, + AFB_SESSION_NONE_X2, 0)) + { + throw std::runtime_error("Failed to add 'subscribe' verb to the API."); + } + + if (afb_api_add_verb( + handle_, + "unsubscribe", + "Unsubscribe to \"volume_changed\" event", + ahl_api_unsubscribe, + nullptr, + nullptr, + AFB_SESSION_NONE_X2, 0)) + { + throw std::runtime_error("Failed to add 'unsubscribe' verb to the API."); + } } void ahl_binding_t::load_controller_configs() { - char* dir_list = getenv("CONTROL_CONFIG_PATH"); - if (!dir_list) dir_list = strdup(CONTROL_CONFIG_PATH); - struct json_object* config_files = CtlConfigScan(dir_list, "policy"); - if (!config_files) throw std::runtime_error("No config files found!"); - - // Only one file should be found this way, but read all just in case - size_t config_files_count = json_object_array_length(config_files); - for(int i = 0; i < config_files_count; ++i) - { - config_entry_t file {json_object_array_get_idx(config_files, i)}; - - if(load_controller_config(file.filepath()) < 0) - { - std::stringstream ss; - ss << "Failed to load config file '" - << file.filename() - << "' from '" - << file.fullpath() - << "'!"; - throw std::runtime_error(ss.str()); - } - } + char* dir_list = getenv("CONTROL_CONFIG_PATH"); + if (!dir_list) dir_list = strdup(CONTROL_CONFIG_PATH); + struct json_object* config_files = CtlConfigScan(dir_list, "policy"); + if (!config_files) throw std::runtime_error("No config files found!"); + + // Only one file should be found this way, but read all just in case + size_t config_files_count = json_object_array_length(config_files); + for(int i = 0; i < config_files_count; ++i) + { + config_entry_t file {json_object_array_get_idx(config_files, i)}; + + if(load_controller_config(file.filepath()) < 0) + { + std::stringstream ss; + ss << "Failed to load config file '" + << file.filename() + << "' from '" + << file.fullpath() + << "'!"; + throw std::runtime_error(ss.str()); + } + } } int ahl_binding_t::load_controller_config(const std::string& path) { - CtlConfigT* controller_config; - - controller_config = CtlLoadMetaData(handle_, path.c_str()); - if (!controller_config) - { - AFB_API_ERROR(handle_, "Failed to load controller from config file!"); - return -1; - } - - static CtlSectionT controller_sections[] = - { - {.key = "plugins", .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = PluginConfig, .handle = nullptr, .actions = nullptr}, - {.key = "onload", .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = OnloadConfig, .handle = nullptr, .actions = nullptr}, - {.key = "controls", .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = ControlConfig, .handle = nullptr, .actions = nullptr}, - {.key = "events", .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = EventConfig, .handle = nullptr, .actions = nullptr}, - {.key = "roles", .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = ahl_api_config_roles, .handle = nullptr, .actions = nullptr }, - {.key = nullptr, .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = nullptr, .handle = nullptr, .actions = nullptr} - }; - - CtlLoadSections(handle_, controller_config, controller_sections); - - return 0; + CtlConfigT* controller_config; + + controller_config = CtlLoadMetaData(handle_, path.c_str()); + if (!controller_config) + { + AFB_API_ERROR(handle_, "Failed to load controller from config file!"); + return -1; + } + + static CtlSectionT controller_sections[] = + { + {.key = "plugins", .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = PluginConfig, .handle = nullptr, .actions = nullptr}, + {.key = "onload", .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = OnloadConfig, .handle = nullptr, .actions = nullptr}, + {.key = "controls", .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = ControlConfig, .handle = nullptr, .actions = nullptr}, + {.key = "events", .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = EventConfig, .handle = nullptr, .actions = nullptr}, + {.key = "roles", .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = ahl_api_config_roles, .handle = nullptr, .actions = nullptr }, + {.key = nullptr, .uid = nullptr, .info = nullptr, .prefix = nullptr, .loadCB = nullptr, .handle = nullptr, .actions = nullptr} + }; + + CtlLoadSections(handle_, controller_config, controller_sections); + + return 0; } int ahl_binding_t::parse_roles_config(json_object* o) { - assert(o != nullptr); - assert(json_object_is_type(o, json_type_array)); + assert(o != nullptr); + assert(json_object_is_type(o, json_type_array)); - if (roles_.size()) return 0; // Roles already added, ignore. + if (roles_.size()) return 0; // Roles already added, ignore. - size_t count = json_object_array_length(o); - roles_.reserve(count); - for(int i = 0; i < count; ++i) - { - json_object* jr = json_object_array_get_idx(o, i); - assert(jr != nullptr); + size_t count = json_object_array_length(o); + roles_.reserve(count); + for(int i = 0; i < count; ++i) + { + json_object* jr = json_object_array_get_idx(o, i); + assert(jr != nullptr); - roles_.push_back(role_t(jr)); - role_t& r = roles_[roles_.size() - 1]; - if(create_api_verb(&r)) - return -1; - } + roles_.push_back(role_t(jr)); + role_t& r = roles_[roles_.size() - 1]; + if(create_api_verb(&r)) + return -1; + } - return 0; + return 0; } int ahl_binding_t::create_api_verb(role_t* r) { - AFB_API_NOTICE(handle_, "New audio role: %s", r->uid().c_str()); - - if (afb_api_add_verb( - handle_, - r->uid().c_str(), - r->description().c_str(), - ahl_api_role, - r, - nullptr, - AFB_SESSION_NONE_X2, 0)) - { - AFB_API_ERROR(handle_, "Failed to add '%s' verb to the API.", - r->uid().c_str()); - return -1; - } - - return 0; + AFB_API_NOTICE(handle_, "New audio role: %s", r->uid().c_str()); + + if (afb_api_add_verb( + handle_, + r->uid().c_str(), + r->description().c_str(), + ahl_api_role, + r, + nullptr, + AFB_SESSION_NONE_X2, 0)) + { + AFB_API_ERROR(handle_, "Failed to add '%s' verb to the API.", + r->uid().c_str()); + return -1; + } + + return 0; } void ahl_binding_t::get_roles(afb_req_t req) const { - json_bool verbose = FALSE; - json_object* arg = afb_req_json(req); - json_object* jverbose; - if (arg != nullptr) - { - json_bool ret = json_object_object_get_ex(arg, "verbose", &jverbose); - if (ret) verbose = json_object_get_boolean(jverbose); - } - - json_object* result = json_object_new_array(); - for(const auto& r : roles_) - { - if (verbose == TRUE || r.device_uri().size()) - json_object_array_add(result, json_object_new_string(r.uid().c_str())); - } - afb_req_success(req, result, nullptr); + json_bool verbose = FALSE; + json_object* arg = afb_req_json(req); + json_object* jverbose; + if (arg != nullptr) + { + json_bool ret = json_object_object_get_ex(arg, "verbose", &jverbose); + if (ret) verbose = json_object_get_boolean(jverbose); + } + + json_object* result = json_object_new_array(); + for(const auto& r : roles_) + { + if (verbose == TRUE || r.device_uri().size()) + json_object_array_add(result, json_object_new_string(r.uid().c_str())); + } + afb_req_success(req, result, nullptr); } void ahl_binding_t::subscribe(afb_req_t req) const @@ -477,10 +477,10 @@ int ahl_binding_t::emit_volume_changed(const std::string& role, int volume) cons const std::vector<role_t> ahl_binding_t::roles() const { - return roles_; + return roles_; } afb_api_t ahl_binding_t::handle() const { - return handle_; + return handle_; } |