diff options
-rw-r--r-- | ahl-binding/role.cpp | 66 | ||||
-rw-r--r-- | ahl-binding/role.hpp | 6 | ||||
-rw-r--r-- | conf.d/project/etc/policy-4a-sample1.json | 8 |
3 files changed, 75 insertions, 5 deletions
diff --git a/ahl-binding/role.cpp b/ahl-binding/role.cpp index 5b2f95e..1b60a40 100644 --- a/ahl-binding/role.cpp +++ b/ahl-binding/role.cpp @@ -187,6 +187,8 @@ void role_t::invoke(afb_request* req) else if (action == "close") close(req, arg); else if (action == "volume") volume(req, arg); else if (action == "interrupt") interrupt(req, arg); + else if (action == "mute") mute(req, arg); + else if (action == "unmute") unmute(req, arg); else afb_request_fail(req, "Unknown action!", nullptr); } @@ -200,7 +202,31 @@ void role_t::open(afb_request* r, json_object* o) if (!apply_policy(r)) { - afb_request_context_set(r, this, nullptr); + afb_request_context_set( + r, + this, + [](void* arg) + { + role_t * role = (role_t*) arg; + afb_dynapi * api = ahl_binding_t::instance().handle(); + + AFB_DYNAPI_NOTICE(api, "Released role %s\n", role->uid_.c_str()); + role->opened_ = false; + + // send a mute command to the HAL. We cannot reuse the do_mute function, + // because in the context here, the afb_request is no longer valid. + json_object* a = json_object_new_object(); + json_object_object_add(a, "mute", json_object_new_boolean(true)); + + afb_dynapi_call( + api, + role->hal_.c_str(), + role->stream_.c_str(), + a, + NULL, + NULL); + } + ); opened_ = true; json_object* result = json_object_new_object(); @@ -228,8 +254,42 @@ void role_t::close(afb_request* r, json_object* o) afb_request_success(r, nullptr, "Stream closed!"); } +void role_t::mute(afb_request * r, json_object* o) { + do_mute(r, true); +} + +void role_t::unmute(afb_request *r, json_object *o) { + do_mute(r, false); +} + +void role_t::do_mute(afb_request * r, bool v) { + + json_object* a = json_object_new_object(); + json_object_object_add(a, "mute", json_object_new_boolean(v)); + afb_dynapi * api = ahl_binding_t::instance().handle(); + + afb_dynapi_call( + api, + hal_.c_str(), + stream_.c_str(), + a, + [](void* closure, int status, json_object* result, afb_dynapi* handle) + { + AFB_DYNAPI_DEBUG(handle, "Got the following answer: %s", json_object_to_json_string(result)); + afb_request* r = (afb_request*)closure; + + json_object_get(result); + if (status) afb_request_fail(r, json_object_to_json_string(result), nullptr); + else afb_request_success(r, result, nullptr); + afb_request_unref(r); + }, + afb_request_addref(r)); +} + void role_t::volume(afb_request* r, json_object* o) { + afb_dynapi * api = ahl_binding_t::instance().handle(); + if(!afb_request_has_permission(r, "urn:AGL:permission::public:4a-audio-mixer")) { if (!opened_) @@ -246,7 +306,7 @@ void role_t::volume(afb_request* r, json_object* o) } else { - AFB_DYNAPI_NOTICE(ahl_binding_t::instance().handle(), "Granted special audio-mixer permission to change volume"); + AFB_DYNAPI_NOTICE(api, "Granted special audio-mixer permission to change volume"); } json_object* value; @@ -263,7 +323,7 @@ void role_t::volume(afb_request* r, json_object* o) json_object_object_add(a, "volume", value); afb_dynapi_call( - r->api, + api, hal_.c_str(), stream_.c_str(), a, diff --git a/ahl-binding/role.hpp b/ahl-binding/role.hpp index 6831195..5e64e11 100644 --- a/ahl-binding/role.hpp +++ b/ahl-binding/role.hpp @@ -37,11 +37,13 @@ private: int apply_policy(afb_request* req); + void do_mute(afb_request*, bool); + public: explicit role_t() = default; explicit role_t(const role_t&) = default; explicit role_t(role_t&&) = default; - + ~role_t() = default; role_t& operator=(const role_t&) = default; @@ -75,4 +77,6 @@ public: void close(afb_request* r, json_object* o); void volume(afb_request* r, json_object* o); void interrupt(afb_request* r, json_object* o); + void mute(afb_request* r, json_object* o); + void unmute(afb_request* r, json_object* o); }; diff --git a/conf.d/project/etc/policy-4a-sample1.json b/conf.d/project/etc/policy-4a-sample1.json index a1aebcd..673caa7 100644 --- a/conf.d/project/etc/policy-4a-sample1.json +++ b/conf.d/project/etc/policy-4a-sample1.json @@ -14,8 +14,14 @@ "events": [], "roles":[ { + "uid": "radio", + "description": "Radio (tuner)", + "priority": 0, + "stream": "radio_stream" + }, + { "uid": "multimedia", - "description": "Multimedia content (e.g. tuner, media player, etc.)", + "description": "Multimedia content (e.g. media player, etc.)", "priority": 0, "stream": "multimedia" }, |