diff options
Diffstat (limited to 'ahl-binding/ahl-binding.cpp')
-rw-r--r-- | ahl-binding/ahl-binding.cpp | 94 |
1 files changed, 82 insertions, 12 deletions
diff --git a/ahl-binding/ahl-binding.cpp b/ahl-binding/ahl-binding.cpp index 98d36b6..1446a81 100644 --- a/ahl-binding/ahl-binding.cpp +++ b/ahl-binding/ahl-binding.cpp @@ -89,6 +89,15 @@ void ahl_api_get_roles(afb_req_t req) } /** + * @brief Callback invoked when clients call the verb 'active'. + * @param[in] req Request to handle. + */ +void ahl_api_active(afb_req_t req) +{ + ahl_binding_t::instance().active(req); +} + +/** * @brief Callback invoked when clients call the verb 'subscribe'. * @param[in] req Request to handle. */ @@ -181,16 +190,16 @@ int ahl_binding_t::init() } AFB_API_NOTICE(handle_, "Required '%s' API found!", HAL_MGR_API); - afb_api_seal(handle_); - AFB_API_NOTICE(handle_, "API is now sealed!"); - - volume_changed_ = afb_api_make_event(handle_, "volume_changed"); + volume_changed_ = afb_api_make_event(handle_, VOLUME_CHANGED); if(!afb_event_is_valid(volume_changed_)) { - AFB_API_ERROR(handle_, "Failed to create the \"volume_changed\" event!"); + AFB_API_ERROR(handle_, "Failed to create the \"" VOLUME_CHANGED "\" event!"); return -2; } + afb_api_seal(handle_); + AFB_API_NOTICE(handle_, "API is now sealed!"); + if (update_streams()) return -1; return 0; } @@ -202,7 +211,7 @@ int ahl_binding_t::init() int ahl_binding_t::update_streams() { json_object* loaded = nullptr; - size_t hals_count = 0, streams_count = 0; + size_t hals_count = 0, streams_count = 0, playbacks_count = 0; if (afb_api_call_sync(handle_, "4a-hal-manager", "loaded", json_object_new_object(), &loaded, nullptr, nullptr)) { @@ -217,7 +226,7 @@ int ahl_binding_t::update_streams() 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!"); + AFB_API_ERROR(handle_, "Expected an array from '4a-hal-manager/loaded', but got something else: %s", json_object_to_json_string(loaded)); json_object_put(loaded); return -1; } @@ -260,6 +269,24 @@ int ahl_binding_t::update_streams() ); } + json_object* playbacksJ = nullptr; + json_object_object_get_ex(info, "playbacks", &playbacksJ); + playbacks_count = json_object_array_length(playbacksJ); + for(int j = 0; j < playbacks_count; ++j) + { + json_object * nameJ = nullptr, * mixerJ = nullptr; + json_object * playbackJ = json_object_array_get_idx(playbacksJ, j); + + json_object_object_get_ex(playbackJ, "name", &nameJ); + json_object_object_get_ex(playbackJ, "mixer-name", &mixerJ); + + update_stream( + halname, + json_object_get_string(nameJ), + json_object_get_string(mixerJ) + ); + } + json_object_put(info); } json_object_put(loaded); @@ -277,7 +304,7 @@ void ahl_binding_t::update_stream(std::string halname, std::string stream, std:: { for(auto& r : roles_) { - if(r.stream() == stream) + if(r.resource() == stream) { if (r.device_uri().size()) AFB_API_WARNING(handle_, "Multiple stream with same name: '%s'.", stream.c_str()); @@ -311,6 +338,18 @@ void ahl_binding_t::load_static_verbs() if (afb_api_add_verb( handle_, + "active", + "Control the active role", + ahl_api_active, + 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, @@ -450,20 +489,51 @@ void ahl_binding_t::get_roles(afb_req_t req) const afb_req_success(req, result, nullptr); } +void ahl_binding_t::active(afb_req_t req) +{ + role_t* activerole = nullptr; + for(role_t& r : roles_) + { + if (r.opened()) + { + if (activerole) + { + if (activerole->priority() < r.priority()) + activerole = &r; + } + else activerole = &r; + } + } + + if (!activerole) + { + afb_req_fail(req, "No active role!", nullptr); + return; + } + + activerole->invoke(req); + + //json_object* response = nullptr; + //char* error = nullptr; + //char* info = nullptr; + //afb_api_call_sync(handle_, HL_API_NAME, activerole->uid().c_str(), afb_req_json(req), &response, &error, &info); + //afb_req_reply(req, response, error, info); +} + void ahl_binding_t::subscribe(afb_req_t req) const { if (afb_req_subscribe(req, volume_changed_)) - afb_req_fail(req, "Failed to subscribe to \"volume_changed\" event!", nullptr); + afb_req_fail(req, "Failed to subscribe to \"" VOLUME_CHANGED "\" event!", nullptr); else - afb_req_success(req, nullptr, "Subscribed to \"volume_changed\" event!"); + afb_req_success(req, nullptr, "Subscribed to \"" VOLUME_CHANGED "\" event!"); } void ahl_binding_t::unsubscribe(afb_req_t req) const { if (afb_req_unsubscribe(req, volume_changed_)) - afb_req_fail(req, "Failed to unsubscribe from \"volume_changed\" event!", nullptr); + afb_req_fail(req, "Failed to unsubscribe from \"" VOLUME_CHANGED "\" event!", nullptr); else - afb_req_success(req, nullptr, "Unsubscribed from \"volume_changed\" event!"); + afb_req_success(req, nullptr, "Unsubscribed from \"" VOLUME_CHANGED "\" event!"); } int ahl_binding_t::emit_volume_changed(const std::string& role, int volume) const |