From ade592e99b54c9156e258b1d890ee28ec1465ba8 Mon Sep 17 00:00:00 2001 From: Corentin Lecouvey Date: Thu, 19 Sep 2013 16:30:32 +0200 Subject: [PATCH] Add notifications in bt-service - use LIBNOTIFICATION_SUPPORT build option - bt-service-agent-notification.c source code sends bluetooth notifications to the notification daemon. This fix is a part of TIVI-1690 (bluetooth pairing) since it just sends requests to the notification daemon. Homescreen part should display the notification themselves. Change-Id: Id82dee9de9199a37b700d27c0d0804c3e11bdd1a --- CMakeLists.txt | 6 + bt-service/CMakeLists.txt | 14 +- bt-service/bt-service-agent-notification.c | 242 +++++++++++++++++++++ bt-service/bt-service-agent.c | 8 +- bt-service/include/bt-service-agent-notification.h | 32 +++ packaging/bluetooth-frwk.spec | 11 +- 6 files changed, 305 insertions(+), 8 deletions(-) create mode 100644 bt-service/bt-service-agent-notification.c create mode 100644 bt-service/include/bt-service-agent-notification.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f82e79..81a231c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,12 @@ OPTION(LIBNOTIFY_SUPPORT "Enable libnotify for popup" Off) IF(LIBNOTIFY_SUPPORT) ADD_DEFINITIONS("-DLIBNOTIFY_SUPPORT") ENDIF(LIBNOTIFY_SUPPORT) + +OPTION(LIBNOTIFICATION_SUPPORT "Use Tizen notification system" Off) +IF(LIBNOTIFICATION_SUPPORT) + ADD_DEFINITIONS("-DLIBNOTIFICATION_SUPPORT") +ENDIF(LIBNOTIFICATION_SUPPORT) + OPTION(MULTI_USER_SUPPORT "Enable multi-user support" OFF) IF(MULTI_USER_SUPPORT) ADD_DEFINITIONS("-DMULTI_USER_SUPPORT") diff --git a/bt-service/CMakeLists.txt b/bt-service/CMakeLists.txt index fb2df17..33831d7 100644 --- a/bt-service/CMakeLists.txt +++ b/bt-service/CMakeLists.txt @@ -31,6 +31,12 @@ bt-popup.c ) ENDIF(LIBNOTIFY_SUPPORT) +IF(LIBNOTIFICATION_SUPPORT) +LIST(APPEND SRCS +bt-service-agent-notification.c +) +ENDIF(LIBNOTIFICATION_SUPPORT) + IF("${CMAKE_BUILD_TYPE}" STREQUAL "") SET(CMAKE_BUILD_TYPE "Release") ENDIF("${CMAKE_BUILD_TYPE}" STREQUAL "") @@ -80,10 +86,10 @@ ADD_DEFINITIONS("-DAPP_DIR=\"${APP_DIR}\"") ADD_DEFINITIONS("-DAPP_LOCALEDIR=\"${APP_LOCALEDIR}\"") ADD_DEFINITIONS("-DAPP_SYSCONFDIR=\"${APP_SYSCONFDIR}\"") -IF(LIBNOTIFY_SUPPORT) +IF(LIBNOTIFY_SUPPORT OR LIBNOTIFICATION_SUPPORT) ADD_DEFINITIONS("-DDATA_DIR_ICON=\"${SHARE_INSTALL_PREFIX}\"") MESSAGE("Icon used for notification is : ${SHARE_INSTALL_PREFIX}/icons/default/bt-icon.png") -ENDIF(LIBNOTIFY_SUPPORT) +ENDIF(LIBNOTIFY_SUPPORT OR LIBNOTIFICATION_SUPPORT) SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") @@ -100,7 +106,7 @@ ENDIF(LIBNOTIFY_SUPPORT) IF(NOT MULTI_USER_SUPPORT) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/org.projectx.bt.service DESTINATION share/dbus-1/system-services) -ENDIF(MULTI_USER_SUPPORT) +ENDIF(NOT MULTI_USER_SUPPORT) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/auto-pair-blacklist DESTINATION /opt/var/lib/bluetooth) @@ -108,7 +114,7 @@ INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/auto-pair-blacklist DESTINATION /opt/v # install booting script IF(NOT MULTI_USER_SUPPORT) INSTALL(PROGRAMS ${CMAKE_CURRENT_SOURCE_DIR}/bluetooth-frwk-service DESTINATION /etc/rc.d/init.d) -ENDIF(MULTI_USER_SUPPORT) +ENDIF(NOT MULTI_USER_SUPPORT) IF(MULTI_USER_SUPPORT) install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/bluetooth-frwk-service_user.conf DESTINATION /etc/dbus-1/system.d) diff --git a/bt-service/bt-service-agent-notification.c b/bt-service/bt-service-agent-notification.c new file mode 100644 index 0000000..d240d7a --- /dev/null +++ b/bt-service/bt-service-agent-notification.c @@ -0,0 +1,242 @@ +/* + * bluetooth-frwk + * + * Copyright (c) 2013 Intel Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "bt-service-agent-notification.h" + +const char* +error_to_string(notification_error_e error) +{ + if (error == NOTIFICATION_ERROR_INVALID_DATA) + return "NOTIFICATION_ERROR_INVALID_DATA"; + if (error == NOTIFICATION_ERROR_NO_MEMORY) + return "NOTIFICATION_ERROR_NO_MEMORY"; + if (error == NOTIFICATION_ERROR_FROM_DB) + return "NOTIFICATION_ERROR_FROM_DB"; + if (error == NOTIFICATION_ERROR_ALREADY_EXIST_ID) + return "NOTIFICATION_ERROR_ALREADY_EXIST_ID"; + if (error == NOTIFICATION_ERROR_FROM_DBUS) + return "NOTIFICATION_ERROR_FROM_DBUS"; + if (error == NOTIFICATION_ERROR_NOT_EXIST_ID) + return "NOTIFICATION_ERROR_NOT_EXIST_ID"; + if (error == NOTIFICATION_ERROR_IO) + return "NOTIFICATION_ERROR_IO"; + if (error == NOTIFICATION_ERROR_SERVICE_NOT_READY) + return "NOTIFICATION_ERROR_SERVICE_NOT_READY"; + if (error == NOTIFICATION_ERROR_NONE) + return "NOTIFICATION_ERROR_NONE"; + + return "UNHANDLED ERROR"; +} + +static int +__notification_set_text(notification_h noti, char *title, char *body) +{ + notification_error_e err = NOTIFICATION_ERROR_NONE; + + err = notification_set_text( noti, NOTIFICATION_TEXT_TYPE_TITLE, + title, + NULL, NOTIFICATION_VARIABLE_TYPE_NONE); + if (err != NOTIFICATION_ERROR_NONE) { + BT_ERR("Unable to set notification title: %s", error_to_string(err)); + return BT_FAILED; + } + + err = notification_set_text( noti, NOTIFICATION_TEXT_TYPE_CONTENT, + body, + NULL, NOTIFICATION_VARIABLE_TYPE_NONE); + if (err != NOTIFICATION_ERROR_NONE) { + BT_ERR("Unable to set notification content: %s", error_to_string(err)); + return BT_FAILED; + } + return BT_SUCCESS; +} + +int +notification_launch(bundle * user_data) +{ + int ret = BT_SUCCESS; + const char *device_name = NULL; + const char *passkey = NULL; + const char *file = NULL; + const char *event_type = NULL; + char *title = NULL; + char *body = NULL; + notification_h notif; + notification_error_e err = NOTIFICATION_ERROR_NONE; + + event_type = bundle_get_val(user_data, "event-type"); + BT_DBG("create notification for '%s' event", event_type); + + notif = notification_new(NOTIFICATION_TYPE_NOTI, + NOTIFICATION_GROUP_ID_NONE, + NOTIFICATION_PRIV_ID_NONE); + if (notif == NULL) { + BT_ERR("Failed to create notification: %s", error_to_string(err)); + return BT_FAILED; + } + + err = notification_set_pkgname(notif, "bluetooth-frwk-bt-service"); + if (err != NOTIFICATION_ERROR_NONE) { + BT_ERR("Unable to set pkgname: %s", error_to_string(err)); + return BT_FAILED; + } + + err = notification_set_image(notif, NOTIFICATION_IMAGE_TYPE_ICON, BT_ICON); + if (err != NOTIFICATION_ERROR_NONE) { + BT_ERR("Unable to set notification icon path: %s", error_to_string(err)); + return BT_FAILED; + } + + /* + * Pass the full bundle to the notification + */ + err = notification_set_execute_option(notif, NOTIFICATION_EXECUTE_TYPE_SINGLE_LAUNCH, NULL, NULL, user_data); + if (err != NOTIFICATION_ERROR_NONE) { + BT_ERR("Unable to set notification icon path: %s", error_to_string(err)); + return BT_FAILED; + } + + if(!strcasecmp(event_type, "pin-request")) { + device_name = (gchar*) bundle_get_val(user_data, "device-name"); + + title = g_strdup_printf("Bluetooth pairing request"); + body = g_strdup_printf("Enter PIN to pair with %s (Try 0000 or 1234)", device_name); + + ret = __notification_set_text(notif, title, body); + + g_free(title); + g_free(body); + } else if (!strcasecmp(event_type, "passkey-confirm-request")){ + device_name = (gchar*) bundle_get_val(user_data, "device-name"); + passkey = (gchar*) bundle_get_val(user_data, "passkey"); + + title = g_strdup_printf("Bluetooth passkey confirm request"); + body = g_strdup_printf("Confirm passkey is %s to pair with %s", passkey, device_name); + + ret = __notification_set_text(notif, title, body); + + g_free(title); + g_free(body); + } else if (!strcasecmp(event_type, "passkey-request")) { + device_name = (gchar*) bundle_get_val(user_data, "device-name"); + + title = g_strdup_printf("Bluetooth pairing request"); + body = g_strdup_printf("Enter PIN to pair with %s (Try 0000 or 1234)", device_name); + + ret = __notification_set_text(notif, title, body); + + g_free(title); + g_free(body); + } else if (!strcasecmp(event_type, "passkey-display-request")) { + device_name = (gchar*) bundle_get_val(user_data, "device-name"); + passkey = (gchar*) bundle_get_val(user_data, "passkey"); + + title = g_strdup_printf("Bluetooth passkey display request"); + body = g_strdup_printf("Enter %s on %s to pair", passkey, device_name); + + ret = __notification_set_text(notif, title, body); + + g_free(title); + g_free(body); + } else if (!strcasecmp(event_type, "authorize-request")) { + device_name = (gchar*) bundle_get_val(user_data, "device-name"); + + title = g_strdup_printf("Bluetooth authorize request"); + body = g_strdup_printf("Allow %s to connect?", device_name); + + ret = __notification_set_text(notif, title, body); + + g_free(title); + g_free(body); + } else if (!strcasecmp(event_type, "app-confirm-request")) { + /* FIXME Seems to be an osp mechanism so not implemented to be confirmed */ + BT_DBG("app-confirm-request even_type seems to be an osp mechanism so not implemented in gnome environment; to be confirmed"); + ret = BT_FAILED; + } else if (!strcasecmp(event_type, "push-authorize-request")) { + file = (gchar*) bundle_get_val(user_data, "file"); + device_name = (gchar*) bundle_get_val(user_data, "device-name"); + + title = g_strdup_printf("Bluetooth push authorize request"); + body = g_strdup_printf("Receive %s from %s?", file, device_name); + + ret = __notification_set_text(notif, title, body); + + g_free(title); + g_free(body); + } else if (!strcasecmp(event_type, "confirm-overwrite-request")) { + /* FIXME Seems to be an osp mechanism so not implemented to be confirmed*/ + BT_DBG("confirm-overwrite-request even_type seems to be an osp mechanism so not implemented in gnome environment; to be confirmed"); + ret = BT_FAILED; + } else if (!strcasecmp(event_type, "keyboard-passkey-request")) { + device_name = (gchar*) bundle_get_val(user_data, "device-name"); + passkey = (gchar*) bundle_get_val(user_data, "passkey"); + + title = g_strdup_printf("Bluetooth keyboard passkey request"); + body = g_strdup_printf("Enter %s on %s to pair", passkey, device_name); + + ret = __notification_set_text(notif, title, body); + + g_free(title); + g_free(body); + } else if (!strcasecmp(event_type, "bt-information")) { + /* FIXME Seems to be an osp mechanism so not implemented to be confirmed */ + BT_DBG("bt-information even_type seems to be an osp mechanism so not implemented in gnome environment; to be confirmed"); + ret = BT_FAILED; + } else if (!strcasecmp(event_type, "exchange-request")) { + device_name = (gchar*) bundle_get_val(user_data, "device-name"); + + title = g_strdup_printf("Bluetooth exchange request"); + body = g_strdup_printf("exchange-request from %s", device_name); + + ret = __notification_set_text(notif, title, body); + g_free(title); + g_free(body); + } else if (!strcasecmp(event_type, "phonebook-request")) { + device_name = bundle_get_val(user_data, "device-name"); + + title = g_strdup_printf("Bluetooth phonebook request"); + body = g_strdup_printf("Allow %s phonebook access", device_name); + + ret = __notification_set_text(notif, title, body); + + g_free(title); + g_free(body); + } else if (!strcasecmp(event_type, "message-request")) { + device_name = bundle_get_val(user_data, "device-name"); + + title = g_strdup_printf("Bluetooth keyboard passkey request"); + body = g_strdup_printf("Allow %s to access messages?", device_name); + + ret = __notification_set_text(notif, title, body); + + g_free(title); + g_free(body); + } else { + ret = BT_FAILED; + } + + err = notification_insert(notif, NULL); + if (err != NOTIFICATION_ERROR_NONE) { + BT_ERR("Unable to insert notification: %s\n", error_to_string(err)); + return BT_FAILED; + } + + return ret; +} + diff --git a/bt-service/bt-service-agent.c b/bt-service/bt-service-agent.c index 345a65a..4892930 100644 --- a/bt-service/bt-service-agent.c +++ b/bt-service/bt-service-agent.c @@ -35,8 +35,10 @@ #include "bt-service-device.h" #include "bt-service-audio.h" -#ifdef LIBNOTIFY_SUPPORT +#if defined(LIBNOTIFY_SUPPORT) #include "bt-popup.h" +#elif defined(LIBNOTIFICATION_SUPPORT) +#include "bt-service-agent-notification.h" #else #include #endif @@ -85,8 +87,10 @@ static int __syspopup_launch(gpointer user_data) { int ret; bundle *b = (bundle *) user_data; -#ifdef LIBNOTIFY_SUPPORT +#if defined(LIBNOTIFY_SUPPORT) ret = notify_launch(b); +#elif defined(LIBNOTIFICATION_SUPPORT) + ret = notification_launch(b); #else ret = syspopup_launch("bt-syspopup", b); #endif diff --git a/bt-service/include/bt-service-agent-notification.h b/bt-service/include/bt-service-agent-notification.h new file mode 100644 index 0000000..19d1634 --- /dev/null +++ b/bt-service/include/bt-service-agent-notification.h @@ -0,0 +1,32 @@ +/* + * bluetooth-frwk + * + * Copyright (c) 2013 Intel Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include +#include +#include +#include "bt-service-common.h" + +#define BT_ICON DATA_DIR_ICON"/icons/default/bt-icon.png" +#define BT_SUCCESS 0 +#define BT_FAILED 1 + +int notification_launch(bundle *user_data); diff --git a/packaging/bluetooth-frwk.spec b/packaging/bluetooth-frwk.spec index d6d6fb7..8cbfc5d 100644 --- a/packaging/bluetooth-frwk.spec +++ b/packaging/bluetooth-frwk.spec @@ -1,5 +1,7 @@ %bcond_with bluetooth_frwk_libnotify +%bcond_with bluetooth_frwk_libnotification %bcond_with multi_user + Name: bluetooth-frwk Summary: Bluetooth framework for BlueZ and Obexd Version: 0.2.57 @@ -91,9 +93,14 @@ export LDFLAGS+=" -Wl,--rpath=%{_libdir} -Wl,--as-needed -Wl,--unresolved-symbol -DMULTI_USER_SUPPORT=Off \ %endif %if %{with bluetooth_frwk_libnotify} - -DLIBNOTIFY_SUPPORT=On + -DLIBNOTIFY_SUPPORT=On \ +%else + -DLIBNOTIFY_SUPPORT=Off \ +%endif +%if %{with bluetooth_frwk_libnotification} + -DLIBNOTIFICATION_SUPPORT=On %else - -DLIBNOTIFY_SUPPORT=Off + -DLIBNOTIFICATION_SUPPORT=Off %endif make -- 2.7.4