diff options
author | Loïc Collignon <loic.collignon@iot.bzh> | 2018-10-19 13:50:35 +0200 |
---|---|---|
committer | Loïc Collignon <loic.collignon@iot.bzh> | 2018-10-22 10:04:42 +0200 |
commit | 29f1daa03ca6a664b64ba600e50e4ec949b6cc11 (patch) | |
tree | 74da011abe57deb322aa6d07f3d86f23a789217b /ahl-binding/role.cpp | |
parent | 9b62cff70a07bebe28e8cd5fb496c7bf5fef04f8 (diff) |
Migration to Afb API v3
Migrate the API to get rid of dynapi by using API v3 instead.
Also fix possible bugs and memory leaks when startup fails.
Bug: SPEC-1546
Change-Id: I7cf8e9454d6910b1258dfe671dedde3b84855fc4
Signed-off-by: Loïc Collignon <loic.collignon@iot.bzh>
Diffstat (limited to 'ahl-binding/role.cpp')
-rw-r--r-- | ahl-binding/role.cpp | 110 |
1 files changed, 54 insertions, 56 deletions
diff --git a/ahl-binding/role.cpp b/ahl-binding/role.cpp index a122fc2..b48b58e 100644 --- a/ahl-binding/role.cpp +++ b/ahl-binding/role.cpp @@ -109,7 +109,7 @@ const std::vector<interrupt_t>& role_t::interrupts() const return interrupts_; } -int role_t::apply_policy(afb_request* req) +int role_t::apply_policy(afb_req_t req) { if(interrupts_.size()) { @@ -135,36 +135,34 @@ int role_t::apply_policy(afb_request* req) json_object_get(i.args()); json_object* result = nullptr; - AFB_DYNAPI_DEBUG(ahl_binding_t::instance().handle(), - "Call '%s'/'%s' '%s", + AFB_API_DEBUG(ahl_binding_t::instance().handle(), "Call '%s'/'%s' '%s", r.hal().c_str(), r.stream().c_str(), json_object_to_json_string(arg)); - if(afb_dynapi_call_sync(ahl_binding_t::instance().handle(), r.hal().c_str(), r.stream().c_str(), arg, &result)) + if(afb_api_call_sync(ahl_binding_t::instance().handle(), r.hal().c_str(), r.stream().c_str(), arg, &result, nullptr, nullptr)) { - afb_request_fail(req, "Failed to call 'ramp' action on stream", nullptr); + afb_req_fail(req, "Failed to call 'ramp' action on stream", nullptr); return -1; } - AFB_DYNAPI_DEBUG(ahl_binding_t::instance().handle(), - "POLICY: Applying a ramp to '%s' stream because '%s' is opened and have higher priority!", + AFB_API_DEBUG(ahl_binding_t::instance().handle(), "POLICY: Applying a ramp to '%s' stream because '%s' is opened and have higher priority!", r.stream().c_str(), stream_.c_str()); } } } else { - afb_request_fail(req, "Unkown interrupt uid!", nullptr); + afb_req_fail(req, "Unkown interrupt uid!", nullptr); return -1; } } return 0; } -void role_t::invoke(afb_request* req) +void role_t::invoke(afb_req_t req) { - json_object* arg = afb_request_json(req); + json_object* arg = afb_req_json(req); if (arg == nullptr) { - afb_request_fail(req, "No valid argument!", nullptr); + afb_req_fail(req, "No valid argument!", nullptr); return; } @@ -172,14 +170,14 @@ void role_t::invoke(afb_request* req) json_bool ret = json_object_object_get_ex(arg, "action", &jaction); if (!ret) { - afb_request_fail(req, "No valid action!", nullptr); + afb_req_fail(req, "No valid action!", nullptr); return; } std::string action = json_object_get_string(jaction); if (action.size() == 0) { - afb_request_fail(req, "No valid action!", nullptr); + afb_req_fail(req, "No valid action!", nullptr); return; } @@ -189,36 +187,36 @@ void role_t::invoke(afb_request* req) 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); + else afb_req_fail(req, "Unknown action!", nullptr); } -void role_t::open(afb_request* r, json_object* o) +void role_t::open(afb_req_t r, json_object* o) { if (opened_) { - afb_request_fail(r, "Already opened!", nullptr); + afb_req_fail(r, "Already opened!", nullptr); return; } if (!apply_policy(r)) { - afb_request_context_set( + afb_req_context_set( r, this, [](void* arg) { role_t * role = (role_t*) arg; - afb_dynapi * api = ahl_binding_t::instance().handle(); + afb_api_t api = ahl_binding_t::instance().handle(); - AFB_DYNAPI_DEBUG(api, "Released role %s\n", role->uid_.c_str()); + AFB_API_DEBUG(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. + // because in the context here, the afb_req_t 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( + afb_api_call( api, role->hal_.c_str(), role->stream_.c_str(), @@ -232,88 +230,88 @@ void role_t::open(afb_request* r, json_object* o) json_object* result = json_object_new_object(); json_object_object_add(result, "device_uri", json_object_new_string(device_uri_.c_str())); - afb_request_success(r, result, nullptr); + afb_req_success(r, result, nullptr); } } -void role_t::close(afb_request* r, json_object* o) +void role_t::close(afb_req_t r, json_object* o) { if (!opened_) { - afb_request_success(r, nullptr, "Already closed!"); + afb_req_success(r, nullptr, "Already closed!"); return; } - if(!afb_request_context_get(r)) + if(!afb_req_context_get(r)) { - afb_request_fail(r, "Stream is opened by another client!", nullptr); + afb_req_fail(r, "Stream is opened by another client!", nullptr); return; } opened_ = false; - afb_request_success(r, nullptr, "Stream closed!"); + afb_req_success(r, nullptr, "Stream closed!"); } -void role_t::mute(afb_request * r, json_object* o) { +void role_t::mute(afb_req_t r, json_object* o) { do_mute(r, true); } -void role_t::unmute(afb_request *r, json_object *o) { +void role_t::unmute(afb_req_t r, json_object *o) { do_mute(r, false); } -void role_t::do_mute(afb_request * r, bool v) { +void role_t::do_mute(afb_req_t 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_api_t api = ahl_binding_t::instance().handle(); - afb_dynapi_call( + afb_api_call( api, hal_.c_str(), stream_.c_str(), a, - [](void* closure, int status, json_object* result, afb_dynapi* handle) + [](void* closure, json_object* result, const char* error, const char* info, afb_api_t handle) { - AFB_DYNAPI_DEBUG(handle, "Got the following answer: %s", json_object_to_json_string(result)); - afb_request* r = (afb_request*)closure; + AFB_API_DEBUG(handle, "Got the following answer: %s", json_object_to_json_string(result)); + afb_req_t r = (afb_req_t)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); + if (error) afb_req_fail(r, json_object_to_json_string(result), nullptr); + else afb_req_success(r, result, nullptr); + afb_req_unref(r); }, - afb_request_addref(r)); + afb_req_addref(r)); } -void role_t::volume(afb_request* r, json_object* o) +void role_t::volume(afb_req_t r, json_object* o) { - afb_dynapi * api = ahl_binding_t::instance().handle(); + afb_api_t api = ahl_binding_t::instance().handle(); - if(!afb_request_has_permission(r, "urn:AGL:permission::public:4a-audio-mixer")) + if(!afb_req_has_permission(r, "urn:AGL:permission::public:4a-audio-mixer")) { if (!opened_) { - afb_request_fail(r, "You have to open the stream first!", nullptr); + afb_req_fail(r, "You have to open the stream first!", nullptr); return; } - if(!afb_request_context_get(r)) + if(!afb_req_context_get(r)) { - afb_request_fail(r, "Stream is opened by another client!", nullptr); + afb_req_fail(r, "Stream is opened by another client!", nullptr); return; } } else { - AFB_DYNAPI_DEBUG(api, "Granted special audio-mixer permission to change volume"); + AFB_API_DEBUG(api, "Granted special audio-mixer permission to change volume"); } json_object* value; json_bool ret = json_object_object_get_ex(o, "value", &value); if (!ret) { - afb_request_fail(r, "No value given!", nullptr); + afb_req_fail(r, "No value given!", nullptr); return; } @@ -322,24 +320,24 @@ void role_t::volume(afb_request* r, json_object* o) json_object* a = json_object_new_object(); json_object_object_add(a, "volume", value); - afb_dynapi_call( + afb_api_call( api, hal_.c_str(), stream_.c_str(), a, - [](void* closure, int status, json_object* result, afb_dynapi* handle) + [](void* closure, json_object* result, const char* error, const char* info, afb_api_t handle) { - AFB_DYNAPI_DEBUG(handle, "Got the following answer: %s", json_object_to_json_string(result)); - afb_request* r = (afb_request*)closure; + AFB_API_DEBUG(handle, "Got the following answer: %s", json_object_to_json_string(result)); + afb_req_t r = (afb_req_t)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); + if (error) afb_req_fail(r, json_object_to_json_string(result), nullptr); + else afb_req_success(r, result, nullptr); + afb_req_unref(r); }, - afb_request_addref(r)); + afb_req_addref(r)); } -void role_t::interrupt(afb_request* r, json_object* o) +void role_t::interrupt(afb_req_t r, json_object* o) { } |