From a4f306dc27ae8b2f3e5fd81e04a8ca096dbb78f6 Mon Sep 17 00:00:00 2001 From: Sungbae Yoo Date: Tue, 5 Apr 2016 16:17:03 +0900 Subject: [PATCH] Add signal handling of zone Change-Id: I04745c2122b1e2731c44ad012f89cf6cc66239da Signed-off-by: Sungbae Yoo --- libs/dpm/zone.cpp | 18 ++++++++++-------- libs/dpm/zone.h | 16 +++++++--------- libs/policy-client.cpp | 27 ++++++++++++++++++++++----- libs/policy-client.h | 4 ++++ server/zone.cpp | 3 +++ tools/zone-setup-wizard/include/zone-setup.h | 1 + tools/zone-setup-wizard/src/main.c | 12 ++++++------ 7 files changed, 53 insertions(+), 28 deletions(-) diff --git a/libs/dpm/zone.cpp b/libs/dpm/zone.cpp index 036ab5f..4d2afc7 100644 --- a/libs/dpm/zone.cpp +++ b/libs/dpm/zone.cpp @@ -87,22 +87,24 @@ int dpm_get_zone_state(dpm_client_h handle, const char* name) return DPM_ERROR_INVALID_PARAMETER; } -int dpm_subscribe_zone_signal(dpm_client_h handle, dpm_zone_signal_cb callback, void* user_data) +int dpm_subscribe_zone_signal(dpm_client_h handle, const char* signal, dpm_zone_signal_cb callback, void* user_data) { RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER); + RET_ON_FAILURE(signal, DPM_ERROR_INVALID_PARAMETER); RET_ON_FAILURE(callback, DPM_ERROR_INVALID_PARAMETER); - /* TODO : should implement */ + std::string sigName = "Zone::"; + sigName += signal; - return DPM_ERROR_NOT_SUPPORTED; + DevicePolicyClient &client = GetDevicePolicyClient(handle); + return client.subscribeSignal(sigName, callback, user_data); } -int dpm_unsubscribe_zone_signal(dpm_client_h handle, dpm_zone_signal_cb callback) +int dpm_unsubscribe_zone_signal(dpm_client_h handle, int callback_id) { RET_ON_FAILURE(handle, DPM_ERROR_INVALID_PARAMETER); - RET_ON_FAILURE(callback, DPM_ERROR_INVALID_PARAMETER); + RET_ON_FAILURE(callback_id >= 0, DPM_ERROR_INVALID_PARAMETER); - /* TODO : should implement */ - - return DPM_ERROR_NOT_SUPPORTED; + DevicePolicyClient &client = GetDevicePolicyClient(handle); + return client.unsubscribeSignal(callback_id); } diff --git a/libs/dpm/zone.h b/libs/dpm/zone.h index 8b8b173..04ef966 100644 --- a/libs/dpm/zone.h +++ b/libs/dpm/zone.h @@ -175,20 +175,19 @@ DPM_API int dpm_get_zone_state(dpm_client_h handle, const char *name); /** * @brief Called when a zone signal occurs */ -typedef void(*dpm_zone_signal_cb)(zone_state_e event, const char* name, void *info, void *user_data); +typedef void(*dpm_zone_signal_cb)(const char* name, const char* object, void *user_data); /** * @brief API to attach a listener to get zone signal. * @details Each zone signals are sent when zone state is changed. * To catch the events, listener should be added in advance. * @since_tizen 3.0 - * @param[in] handle the device policy client handle + * @param[in] handle the device policy client handlei + * @param[in] signal The signal of the container to be monitored * @param[in] callback The listener function to be called * @param[in] user_data The user data passed to the listener function - * @return #DPM_ERROR_NONE on success, otherwise a negative value - * @retval #DPM_ERROR_NONE Successful + * @return Listener identifier on success, otherwise negative value * @retval #DPM_ERROR_INVALID_PARAMETER Invalid parameter - * @retval #DPM_ERROR_PERMISSION_DENIED The application does not have * the privilege to call this API * @pre handle must be created by dpm_create_client() * @post @@ -196,7 +195,7 @@ typedef void(*dpm_zone_signal_cb)(zone_state_e event, const char* name, void *in * @see dpm_destroy_client() * @see dpm_subscribe_zone_signal() */ -DPM_API int dpm_subscribe_zone_signal(dpm_client_h handle, dpm_zone_signal_cb callback, void* user_data); +DPM_API int dpm_subscribe_zone_signal(dpm_client_h handle, const char* signal, dpm_zone_signal_cb callback, void* user_data); /** * @brief API to detach the listener from zone signal. @@ -204,11 +203,10 @@ DPM_API int dpm_subscribe_zone_signal(dpm_client_h handle, dpm_zone_signal_cb ca * though zone state is changed. * @since_tizen 3.0 * @param[in] handle the device policy client handle - * @param[in] callback The listener function to be removed + * @param[in] callback_id The listener identifier to be removed * @return #DPM_ERROR_NONE on success, otherwise a negative value * @retval #DPM_ERROR_NONE Successful * @retval #DPM_ERROR_INVALID_PARAMETER Invalid parameter - * @retval #DPM_ERROR_PERMISSION_DENIED The application does not have * the privilege to call this API * @pre handle must be created by dpm_create_client() * @post @@ -216,7 +214,7 @@ DPM_API int dpm_subscribe_zone_signal(dpm_client_h handle, dpm_zone_signal_cb ca * @see dpm_destroy_client() * @see dpm_unsubscribe_zone_signal() */ -DPM_API int dpm_unsubscribe_zone_signal(dpm_client_h handle, dpm_zone_signal_cb callback); +DPM_API int dpm_unsubscribe_zone_signal(dpm_client_h handle, int callback_id); /** * @} diff --git a/libs/policy-client.cpp b/libs/policy-client.cpp index c274ec3..aa0243f 100644 --- a/libs/policy-client.cpp +++ b/libs/policy-client.cpp @@ -18,8 +18,8 @@ namespace { -const std::string POLICY_SUBSCRIBER_REGISTER = "Server::registerNotificationSubscriber"; -const std::string POLICY_SUBSCRIBER_UNREGISTER = "Server::unregisterNotificationSubscriber"; +const std::string SUBSCRIBER_REGISTER = "Server::registerNotificationSubscriber"; +const std::string SUBSCRIBER_UNREGISTER = "Server::unregisterNotificationSubscriber"; const std::string POLICY_MANAGER_ADDRESS = "/tmp/.device-policy-manager"; @@ -60,15 +60,32 @@ int DevicePolicyClient::subscribePolicyChange(const std::string& name, const PolicyChangeListener& listener, void* data) { - auto listenerDispatcher = [listener, data](const std::string& policy, std::string state) { + auto listenerDispatcher = [listener, data](const std::string& policy, std::string &state) { listener(policy.c_str(), state.c_str(), data); }; - return client->subscribe(POLICY_SUBSCRIBER_REGISTER, + return client->subscribe(SUBSCRIBER_REGISTER, name, listenerDispatcher); } int DevicePolicyClient::unsubscribePolicyChange(int subscriberId) { - return client->unsubscribe(POLICY_SUBSCRIBER_UNREGISTER, subscriberId); + return client->unsubscribe(SUBSCRIBER_UNREGISTER, subscriberId); +} + +int DevicePolicyClient::subscribeSignal(const std::string& name, + const SignalListener& listener, + void* data) +{ + auto listenerDispatcher = [listener, data](std::string &name, std::string &from, std::string &object) { + listener(from.c_str(), object.c_str(), data); + }; + + return client->subscribe + (SUBSCRIBER_REGISTER, name, listenerDispatcher); +} + +int DevicePolicyClient::unsubscribeSignal(int subscriberId) +{ + return client->unsubscribe(SUBSCRIBER_UNREGISTER, subscriberId); } diff --git a/libs/policy-client.h b/libs/policy-client.h index e854381..8bc2997 100644 --- a/libs/policy-client.h +++ b/libs/policy-client.h @@ -24,6 +24,7 @@ #include "rmi/client.h" typedef std::function PolicyChangeListener; +typedef std::function SignalListener; class DevicePolicyClient { public: @@ -39,6 +40,9 @@ public: int subscribePolicyChange(const std::string& name, const PolicyChangeListener& listener, void* data); int unsubscribePolicyChange(int subscriberId); + int subscribeSignal(const std::string& name, const SignalListener& listener, void* data); + int unsubscribeSignal(int subscriberId); + template Policy createPolicyInterface(Args&&... args) noexcept { diff --git a/server/zone.cpp b/server/zone.cpp index fff3b2b..32e54a0 100644 --- a/server/zone.cpp +++ b/server/zone.cpp @@ -104,6 +104,9 @@ Zone::Zone(PolicyControlContext& ctx) manager.registerNonparametricMethod(this, (std::vector)(Zone::getZoneList)()); manager.registerParametricMethod(this, (int)(Zone::getZoneState)(std::string)); + + manager.createNotification("Zone::created"); + manager.createNotification("Zone::removed"); } Zone::~Zone() diff --git a/tools/zone-setup-wizard/include/zone-setup.h b/tools/zone-setup-wizard/include/zone-setup.h index ac797d9..c3e34d1 100644 --- a/tools/zone-setup-wizard/include/zone-setup.h +++ b/tools/zone-setup-wizard/include/zone-setup.h @@ -51,6 +51,7 @@ typedef struct { char *zone_name; char *provision_path; dpm_client_h dpm_client; + int dpm_zone_signal_cb_id; bool create_done; } appdata_s; diff --git a/tools/zone-setup-wizard/src/main.c b/tools/zone-setup-wizard/src/main.c index 3828590..6db75d1 100644 --- a/tools/zone-setup-wizard/src/main.c +++ b/tools/zone-setup-wizard/src/main.c @@ -19,12 +19,10 @@ #include "zone-setup.h" #include "widget.h" -static void __create_zone_done(zone_state_e event, const char *name, void *info, void *user_data) +static void __create_zone_done(const char *from, const char *info, void *user_data) { appdata_s *ad = (appdata_s *) user_data; - if (event == DPM_ZONE_DEFINED) - ad->create_done = true; - return ; + ad->create_done = true; } static bool __app_create(void *data) @@ -46,7 +44,7 @@ static void __app_terminate(void *data) { appdata_s *ad = (appdata_s *) data; - dpm_unsubscribe_zone_signal(ad->dpm_client, __create_zone_done); + dpm_unsubscribe_zone_signal(ad->dpm_client, ad->dpm_zone_signal_cb_id); dpm_destroy_client(ad->dpm_client); ad->dpm_client = NULL; return ; @@ -75,7 +73,9 @@ static void __app_control(app_control_h app_control, void *data) ui_app_exit(); } - if (dpm_subscribe_zone_signal(ad->dpm_client, __create_zone_done, ad) != 0) { + ad->dpm_zone_signal_cb_id = dpm_subscribe_zone_signal(ad->dpm_client, "created", __create_zone_done, ad); + + if (ad-> dpm_zone_signal_cb_id < 0) { dlog_print(DLOG_ERROR, LOG_TAG, "failed to set signal callback"); ui_app_exit(); } -- 2.7.4