summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Collignon <loic.collignon@iot.bzh>2018-12-18 11:55:58 +0100
committerLoïc Collignon <loic.collignon@iot.bzh>2018-12-20 18:13:14 +0100
commit86a65245de04317a0d8b420f8bcdb08639976082 (patch)
tree5784fc1e34ff3dbb9d25a9899331ec23683f6e62
parentd0fc54659d0f6d5e6110e607f4ee49b52b0387d5 (diff)
Added an 'activerole' verb
Control the currently opened role with the higher priority. Volume changed event nows specify if the corresponding role was the active one when the event was emitted. Bug: SPEC-1313 Change-Id: I87ae89ef03357a3537ada86f1dd3f319d13cbe85 Signed-off-by: Loïc Collignon <loic.collignon@iot.bzh>
-rw-r--r--ahl-binding/ahl-api.cpp9
-rw-r--r--ahl-binding/ahl-api.hpp1
-rw-r--r--ahl-binding/ahl-binding.cpp44
-rw-r--r--ahl-binding/ahl-binding.hpp4
4 files changed, 56 insertions, 2 deletions
diff --git a/ahl-binding/ahl-api.cpp b/ahl-binding/ahl-api.cpp
index c5ade37..7ab3add 100644
--- a/ahl-binding/ahl-api.cpp
+++ b/ahl-binding/ahl-api.cpp
@@ -88,6 +88,15 @@ void ahl_api_get_roles(afb_req_t req)
}
/**
+ * @brief Callback invoked when clients call the verb 'activerole'.
+ * @param[in] req Request to handle.
+ */
+void ahl_api_activerole(afb_req_t req)
+{
+ ahl_binding_t::instance().activerole(req);
+}
+
+/**
* @brief Callback invoked when clients call the verb 'subscribe'.
* @param[in] req Request to handle.
*/
diff --git a/ahl-binding/ahl-api.hpp b/ahl-binding/ahl-api.hpp
index fed48ba..7561ab4 100644
--- a/ahl-binding/ahl-api.hpp
+++ b/ahl-binding/ahl-api.hpp
@@ -25,6 +25,7 @@ int ahl_api_init(afb_api_t);
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);
void ahl_api_get_roles(afb_req_t req);
+void ahl_api_activerole(afb_req_t req);
void ahl_api_subscribe(afb_req_t req);
void ahl_api_unsubscribe(afb_req_t req);
void ahl_api_role(afb_req_t req); \ No newline at end of file
diff --git a/ahl-binding/ahl-binding.cpp b/ahl-binding/ahl-binding.cpp
index af1ae79..84ba60a 100644
--- a/ahl-binding/ahl-binding.cpp
+++ b/ahl-binding/ahl-binding.cpp
@@ -46,6 +46,18 @@ void ahl_binding_t::load_static_verbs()
if (afb_api_add_verb(
handle_,
+ "activerole",
+ "Controls the currently active role",
+ ahl_api_activerole,
+ nullptr,
+ nullptr,
+ AFB_SESSION_NONE_X2, 0))
+ {
+ throw std::runtime_error("Failed to add 'activerole' verb to the API.");
+ }
+
+ if (afb_api_add_verb(
+ handle_,
"subscribe",
"Subscribe to \"volume_changed\" event",
ahl_api_subscribe,
@@ -250,6 +262,18 @@ int ahl_binding_t::create_api_verb(role_t* r)
return 0;
}
+role_t* ahl_binding_t::get_active_role()
+{
+ role_t* active = nullptr;
+ for(auto& r : roles_)
+ {
+ if (r.opened() && (active == nullptr || r.priority() > active->priority()))
+ active = &r;
+ }
+
+ return active;
+}
+
/**
* @brief Get the singleton instance.
* @return The unique instance.
@@ -352,12 +376,19 @@ void ahl_binding_t::event(std::string name, json_object* arg)
* @param[in] volume New volume.
* @return Zero on success, non-zero otherwise.
*/
-int ahl_binding_t::emit_volume_changed(const std::string& role, int volume) const
+int ahl_binding_t::emit_volume_changed(const std::string& role, int volume)
{
json_object* data = json_object_new_object();
json_object_object_add(data, "role", json_object_new_string(role.c_str()));
json_object_object_add(data, "volume", json_object_new_int(volume));
+ bool active = false;
+ role_t* activerole = get_active_role();
+ if (activerole && activerole->uid() == role)
+ active = true;
+
+ json_object_object_add(data, "active", json_object_new_boolean(active));
+
return afb_event_push(volume_changed_, data);
}
@@ -416,6 +447,17 @@ void ahl_binding_t::get_roles(afb_req_t req) const
}
/**
+ * @brief Controls the currently active role.
+ * @param[in] req Request to answer.
+ */
+void ahl_binding_t::activerole(afb_req_t req)
+{
+ role_t* role = get_active_role();
+ if (!role) afb_req_fail(req, "No active role!", nullptr);
+ else role->invoke(req);
+}
+
+/**
* @brief Subscribe to events.
* @param[in] req Request to answer.
*/
diff --git a/ahl-binding/ahl-binding.hpp b/ahl-binding/ahl-binding.hpp
index 881d906..47c343a 100644
--- a/ahl-binding/ahl-binding.hpp
+++ b/ahl-binding/ahl-binding.hpp
@@ -51,6 +51,7 @@ private:
int update_streams();
void update_stream(std::string hal, std::string stream, std::string deviceuri);
int create_api_verb(role_t* r);
+ role_t* get_active_role();
public:
static ahl_binding_t& instance();
@@ -60,10 +61,11 @@ public:
int preinit(afb_api_t handle);
int init();
void event(std::string name, json_object* arg);
- int emit_volume_changed(const std::string& role, int volume) const;
+ int emit_volume_changed(const std::string& role, int volume);
int parse_roles_config(json_object* o);
void get_roles(afb_req_t req) const;
+ void activerole(afb_req_t req);
void subscribe(afb_req_t req) const;
void unsubscribe(afb_req_t req) const;
};