2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <syspopup_caller.h>
24 #include <bundle_internal.h>
26 #include "bluetooth-api.h"
27 #include "bt-internal-types.h"
29 #include "bt-service-common.h"
30 #include "bt-service-device.h"
31 #include "bt-service-util.h"
32 #include "bt-service-event.h"
33 #include "bt-service-otp.h"
35 #define BT_OTP_SERVICE_NAME "org.projectx.otp"
36 #define BT_OTP_OBJECT_PATH "/org/projectx/otp"
37 #define BT_OTP_INTERFACE_NAME "org.projectx.otp_service"
39 #define BT_OTP_BASE_DIR_PATH "/home/owner/media/otp/"
41 #define GATT_CHAR_INTERFACE "org.bluez.GattCharacteristic1"
43 #define GATT_DEFAULT_TIMEOUT (6 * 1000) /* Dependent on supervision timeout 6 sec */
44 #define BT_INDICATION_TIMEOUT_MAX 15000 /* Timeout for Indication from OTP Server in msec */
46 /* OTP Notification Request structure */
50 unsigned int notification_timeout_id;
52 } bt_otp_notification_info;
54 /* OTP transport specific data read Request info structure */
59 } bt_otp_read_req_info;
61 static GSList *otp_read_req_info_list = NULL;
63 static GSList *otp_notification_info_list = NULL;
65 static GDBusProxy *otp_gproxy;
67 static GDBusProxy *_bt_core_gdbus_init_otp_proxy(void)
71 GDBusConnection *conn;
75 conn = _bt_gdbus_get_system_gconn();
79 proxy = g_dbus_proxy_new_sync(conn,
80 G_DBUS_PROXY_FLAGS_NONE, NULL,
83 BT_OTP_INTERFACE_NAME,
87 BT_ERR("Unable to create proxy: %s", err->message);
98 GDBusProxy *_bt_core_gdbus_get_otp_proxy(void)
100 return (otp_gproxy) ? otp_gproxy : _bt_core_gdbus_init_otp_proxy();
103 void server_init_cb(GObject *object, GAsyncResult *res,
106 BT_INFO("Server Init completed");
107 GError *error = NULL;
108 GVariant *result, *out_param, *param;
109 request_info_t *req_info = NULL;
110 int status = BLUETOOTH_ERROR_NONE;
111 bool server_state = false;
113 result = g_dbus_proxy_call_finish(otp_gproxy, res, &error);
115 if (result == NULL) {
116 BT_ERR("Dbus-RPC is failed\n");
118 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
119 error->code, error->message);
120 g_clear_error(&error);
121 status = BLUETOOTH_ERROR_INTERNAL;
126 g_variant_get(result, "(i)", &status);
129 BT_DBG("Status [%d]", status);
131 if (status == BLUETOOTH_ERROR_NONE)
134 param = g_variant_new("(ib)", status, server_state);
136 req_info = _bt_get_request_info(GPOINTER_TO_INT(user_data));
138 /* Send the event to application */
139 _bt_send_event(BT_OTP_EVENT,
140 BLUETOOTH_EVENT_OTP_SERVER_STATE_CHANGED,
143 out_param = g_variant_new_from_data((const GVariantType*)"i",
144 result, sizeof(int), TRUE, NULL, NULL);
147 g_dbus_method_invocation_return_value(req_info->context,
148 g_variant_new("(iv)", status, out_param));
150 _bt_delete_request_list(req_info->req_id);
153 g_variant_unref(result);
156 int bt_otp_server_init(int request_id, const char *directory)
158 BT_INFO("relative_path: [%s]", directory);
159 char *base_dir = g_strconcat(BT_OTP_BASE_DIR_PATH, directory, NULL);
163 otp_gproxy = _bt_core_gdbus_get_otp_proxy();
165 BT_DBG("Couldn't get service proxy");
167 return BLUETOOTH_ERROR_INTERNAL;
170 g_dbus_proxy_call(otp_gproxy,
174 G_DBUS_CALL_FLAGS_NONE, -1,
176 (GAsyncReadyCallback) server_init_cb,
177 GINT_TO_POINTER(request_id));
181 return BLUETOOTH_ERROR_NONE;
184 void server_deinit_cb(GObject *object, GAsyncResult *res,
187 BT_INFO("Server Deinit completed");
188 GError *error = NULL;
189 GVariant *result, *out_param, *param;
190 request_info_t *req_info = NULL;
191 int status = BLUETOOTH_ERROR_NONE;
192 bool server_state = false;
194 result = g_dbus_proxy_call_finish(otp_gproxy, res, &error);
196 if (result == NULL) {
197 BT_ERR("Dbus-RPC is failed\n");
199 BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
200 error->code, error->message);
201 g_clear_error(&error);
202 status = BLUETOOTH_ERROR_INTERNAL;
207 g_variant_get(result, "(i)", &status);
210 BT_DBG("Status [%d]", status);
212 param = g_variant_new("(ib)", status, server_state);
214 req_info = _bt_get_request_info(GPOINTER_TO_INT(user_data));
216 /* Send the event to application */
217 _bt_send_event(BT_OTP_EVENT,
218 BLUETOOTH_EVENT_OTP_SERVER_STATE_CHANGED,
222 out_param = g_variant_new_from_data((const GVariantType*)"i",
223 result, sizeof(int), TRUE, NULL, NULL);
225 g_dbus_method_invocation_return_value(req_info->context,
226 g_variant_new("(iv)", status, out_param));
228 _bt_delete_request_list(req_info->req_id);
231 g_variant_unref(result);
234 g_object_unref(otp_gproxy);
239 int bt_otp_server_deinit(int request_id)
243 otp_gproxy = _bt_core_gdbus_get_otp_proxy();
245 BT_DBG("Couldn't get service proxy");
246 return BLUETOOTH_ERROR_INTERNAL;
249 g_dbus_proxy_call(otp_gproxy,
251 NULL, G_DBUS_CALL_FLAGS_NONE,
253 (GAsyncReadyCallback) server_deinit_cb,
254 GINT_TO_POINTER(request_id));
257 return BLUETOOTH_ERROR_NONE;
260 int __get_handle_length(char *handle)
263 while (handle && (handle[i] != '\0')) {
269 static bt_otp_read_req_info *__bt_otp_get_read_info(char *handle)
272 bt_otp_read_req_info *info = NULL;
273 BT_INFO("Found waiting for OTP Read from charc handle[%s]", handle);
274 for (l = otp_read_req_info_list; l != NULL; l = g_slist_next(l)) {
275 info = (bt_otp_read_req_info *)l->data;
279 if (!g_strcmp0(info->handle, handle)) {
280 BT_INFO("Found waiting for OTP Read from remote addr[%s]",
288 static void __bt_otp_remove_read_info(bt_otp_read_req_info *info)
290 BT_DBG("Removing Read Req Info [%s]", info->handle);
292 otp_read_req_info_list = g_slist_remove(otp_read_req_info_list, info);
294 g_free(info->handle);
296 g_free(info->sender);
300 static void __bt_otp_read_char_cb(GObject *source_object,
301 GAsyncResult *res, gpointer user_data)
303 bt_gatt_char_descriptor_property_t att_value = { 0, };
304 GDBusConnection *system_gconn = NULL;
305 GVariant *var_data, *param = NULL;
306 int result = BLUETOOTH_ERROR_NONE;
307 bt_otp_read_req_info *info = NULL;
308 GByteArray *gp_byte_array = NULL;
309 request_info_t *req_info = NULL;
310 GVariantIter *iter = NULL;
311 GVariant *value = NULL;
312 char *otp_data = NULL;
313 GVariant *out_param1;
314 GError *error = NULL;
319 system_gconn = _bt_gdbus_get_system_gconn();
321 handle = (char *)user_data;
322 info = __bt_otp_get_read_info(handle);
324 value = g_dbus_connection_call_finish(system_gconn, res, &error);
327 BT_ERR("Error : %s \n", error->message);
330 req_info = _bt_get_request_info(info->req_id);
331 __bt_otp_remove_read_info(info);
333 result = BLUETOOTH_ERROR_INTERNAL;
337 gp_byte_array = g_byte_array_new();
338 g_variant_get(value, "(ay)", &iter);
340 while (g_variant_iter_loop(iter, "y", &g_byte))
341 g_byte_array_append(gp_byte_array, &g_byte, 1);
343 if (gp_byte_array->len != 0) {
344 att_value.val_len = (unsigned int)gp_byte_array->len;
345 att_value.val = (unsigned char *)gp_byte_array->data;
348 otp_data = (char *)g_memdup(att_value.val, att_value.val_len);
350 var_data = g_variant_new_from_data((const GVariantType*)"ay",
351 otp_data, att_value.val_len, TRUE, NULL, NULL);
354 param = g_variant_new("(isn@ay)", result, handle, att_value.val_len, var_data);
355 _bt_send_event_to_dest(info->sender, BT_OTP_EVENT,
356 BLUETOOTH_EVENT_OTP_READ_CHAR_VAL,
358 req_info = _bt_get_request_info(info->req_id);
359 __bt_otp_remove_read_info(info);
363 if (req_info == NULL) {
364 BT_ERR("OTP data read Request not found!!");
368 if (req_info->context == NULL)
371 out_param1 = g_variant_new_from_data((const GVariantType*)"ay",
372 handle, __get_handle_length(handle), TRUE, NULL, NULL);
373 g_dbus_method_invocation_return_value(req_info->context,
374 g_variant_new("(iv)", result, out_param1));
376 _bt_delete_request_list(req_info->req_id);
381 g_clear_error(&error);
383 g_byte_array_free(gp_byte_array, TRUE);
387 g_variant_unref(value);
389 g_variant_iter_free(iter);
395 int _bt_otp_read_characteristic_value(int request_id, char *sender, char *handle)
397 GDBusConnection *conn;
398 bt_otp_read_req_info *info = NULL;
399 char *charc_handle = g_strdup(handle);
400 GVariantBuilder *builder = NULL;
403 BT_CHECK_PARAMETER(handle, return);
404 BT_CHECK_PARAMETER(sender, return);
406 conn = _bt_gdbus_get_system_gconn();
407 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
409 BT_DBG("Read OTP Characteristic from server handle [%s]", handle);
411 /* If OTP data read already pending on same Server, then return In progress */
412 if (__bt_otp_get_read_info(handle) != NULL) {
413 BT_ERR("Read Req is ongoing in remote server [%s]", charc_handle);
414 g_free(charc_handle);
415 return BLUETOOTH_ERROR_IN_PROGRESS;
418 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
420 g_variant_builder_add(builder, "{sv}", "offset",
421 g_variant_new("q", offset));
423 g_dbus_connection_call(conn,
428 g_variant_new("(a{sv})", builder),
429 G_VARIANT_TYPE("(ay)"),
430 G_DBUS_CALL_FLAGS_NONE,
433 (GAsyncReadyCallback)__bt_otp_read_char_cb,
434 (gpointer)charc_handle);
436 /* Save Info in pending list */
437 info = g_malloc0(sizeof(bt_otp_read_req_info));
438 info->handle = g_strdup(handle);
439 BT_INFO("Found waiting for OTP Read from charc handle[%s] [%s]", info->handle, handle);
440 info->sender = g_strdup(sender);
441 info->req_id = request_id;
442 otp_read_req_info_list = g_slist_append(otp_read_req_info_list, info);
445 return BLUETOOTH_ERROR_NONE;
448 static bt_otp_notification_info *__bt_otp_get_notification_info(char *handle)
451 bt_otp_notification_info *info = NULL;
453 for (l = otp_notification_info_list; l != NULL; l = g_slist_next(l)) {
454 info = (bt_otp_notification_info *)l->data;
458 if (!g_strcmp0(info->handle, handle)) {
459 BT_INFO("Found waiting for Ind from Server addr[%s]",
467 static void __bt_otp_remove_notification_info(bt_otp_notification_info *info)
469 BT_DBG("Removing Notification Info [%s]", info->handle);
471 otp_notification_info_list = g_slist_remove(otp_notification_info_list, info);
473 g_free(info->handle);
475 g_free(info->sender);
476 if (info->notification_timeout_id > 0) {
477 g_source_remove(info->notification_timeout_id);
478 info->notification_timeout_id = 0;
483 static void __bt_otp_notification_enable_request_cb(GObject *source_object,
484 GAsyncResult *res, gpointer user_data)
486 GError *error = NULL;
487 GDBusConnection *system_gconn = NULL;
488 GVariant *value = NULL;
489 GVariant *param = NULL;
490 GVariant *out_param1 = NULL;
491 int result = BLUETOOTH_ERROR_NONE;
493 bt_otp_notification_info *info = NULL;
494 request_info_t *req_info = NULL;
497 system_gconn = _bt_gdbus_get_system_gconn();
498 value = g_dbus_connection_call_finish(system_gconn, res, &error);
501 BT_ERR("Error : %s \n", error->message);
502 if (g_strrstr(error->message, "Already notifying"))
503 result = BLUETOOTH_ERROR_NONE;
504 else if (g_strrstr(error->message, "In Progress"))
505 result = BLUETOOTH_ERROR_IN_PROGRESS;
506 else if (g_strrstr(error->message, "Operation is not supported"))
507 result = BLUETOOTH_ERROR_NOT_SUPPORT;
508 else if (g_strrstr(error->message, "Write not permitted") ||
509 g_strrstr(error->message, "Operation Not Authorized"))
510 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
511 else if (g_strrstr(error->message, "Not paired"))
512 result = BLUETOOTH_ERROR_NOT_PAIRED;
514 result = BLUETOOTH_ERROR_INTERNAL;
516 BT_DBG("OTP CCCD enable request successful, send event to BT App");
519 handle = (char *)user_data;
520 info = __bt_otp_get_notification_info(handle);
523 req_info = _bt_get_request_info(info->req_id);
525 /* If CCCD Enable request failed for any reason, reset timer */
526 if (result != BLUETOOTH_ERROR_NONE && info != NULL) {
527 BT_ERR("Activation Request failed");
529 if (info->notification_timeout_id > 0) {
530 g_source_remove(info->notification_timeout_id);
531 info->notification_timeout_id = 0;
534 /* Remove Indication Info */
535 __bt_otp_remove_notification_info(info);
537 /* CCCD Enable Request successful */
539 param = g_variant_new("(is)", result, handle);
540 _bt_send_event_to_dest(info->sender, BT_OTP_EVENT,
541 BLUETOOTH_EVENT_OTP_NOTIFICATION_ENABLED,
546 if (req_info == NULL) {
547 BT_ERR("OTP Control Point CCCD Enable Request is not found!!");
551 if (req_info->context == NULL)
554 out_param1 = g_variant_new_from_data((const GVariantType*)"ay",
555 handle, __get_handle_length(handle), TRUE, NULL, NULL);
556 g_dbus_method_invocation_return_value(req_info->context,
557 g_variant_new("(iv)", result, out_param1));
559 _bt_delete_request_list(req_info->req_id);
563 g_variant_unref(value);
565 g_clear_error(&error);
573 int _bt_otp_enable_notification(int request_id, char *sender, char *handle)
575 bt_otp_notification_info *info = NULL;
576 char *charc_handle = g_strdup(handle);
577 GDBusConnection *conn;
579 BT_CHECK_PARAMETER(handle, return);
580 BT_CHECK_PARAMETER(sender, return);
582 conn = _bt_gdbus_get_system_gconn();
583 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
585 BT_DBG("OTP Control point CCCD Handle [%s]", handle);
587 if (__bt_otp_get_notification_info(handle) != NULL) {
588 BT_ERR("Activation is already ongoing for same remote server");
589 g_free(charc_handle);
590 return BLUETOOTH_ERROR_IN_PROGRESS;
593 BT_INFO("Start Notify to Bluez");
594 g_dbus_connection_call(conn,
601 G_DBUS_CALL_FLAGS_NONE,
602 GATT_DEFAULT_TIMEOUT, NULL,
603 (GAsyncReadyCallback)__bt_otp_notification_enable_request_cb,
604 (gpointer)charc_handle);
606 info = g_malloc0(sizeof(bt_otp_notification_info));
607 info->handle = g_strdup(handle);
608 info->sender = g_strdup(sender);
609 info->req_id = request_id;
610 otp_notification_info_list = g_slist_append(otp_notification_info_list, info);
613 return BLUETOOTH_ERROR_NONE;
616 static void __bt_otp_write_request_cb(GObject *source_object,
617 GAsyncResult *res, gpointer user_data)
619 GError *error = NULL;
620 GDBusConnection *system_gconn = NULL;
621 GVariant *value = NULL;
622 GVariant *param = NULL;
623 GVariant *out_param1 = NULL;
624 int result = BLUETOOTH_ERROR_NONE;
626 bt_otp_notification_info *info = NULL;
627 request_info_t *req_info = NULL;
630 system_gconn = _bt_gdbus_get_system_gconn();
631 value = g_dbus_connection_call_finish(system_gconn, res, &error);
634 BT_ERR("Error : %s \n", error->message);
635 /* Process error->message to narrow down the att_ecode */
636 result = BLUETOOTH_ERROR_INTERNAL;
639 handle = (char *)user_data;
640 info = __bt_otp_get_notification_info(handle);
642 req_info = _bt_get_request_info(info->req_id);
644 /* Is Activation request failed for any reason, reset timer */
645 if (result != BLUETOOTH_ERROR_NONE && info != NULL) {
646 BT_ERR("Activation Request failed");
647 /* Remove Indication Info */
648 __bt_otp_remove_notification_info(info);
650 /* Activation Request successful */
652 param = g_variant_new("(is)", result, handle);
653 _bt_send_event_to_dest(info->sender, BT_OTP_EVENT,
654 BLUETOOTH_EVENT_OTP_WRITE_CHAR_VAL,
659 if (req_info == NULL) {
660 BT_ERR("OTP Write Request is not found!!");
664 if (req_info->context == NULL)
667 out_param1 = g_variant_new_from_data((const GVariantType*)"ay",
668 handle, __get_handle_length(handle), TRUE, NULL, NULL);
669 g_dbus_method_invocation_return_value(req_info->context,
670 g_variant_new("(iv)", result, out_param1));
672 _bt_delete_request_list(req_info->req_id);
676 g_variant_unref(value);
678 g_clear_error(&error);
686 static void __bt_otp_send_indication_event(bt_otp_notification_info *info,
687 unsigned char *buffer, int len, int result)
692 otp_data = g_variant_new_from_data((const GVariantType*)"ay",
693 buffer, len, TRUE, NULL, NULL);
695 BT_DBG("Send Indication event to sender");
696 param = g_variant_new("(is@ay)", result, info->handle, otp_data);
697 _bt_send_event_to_dest(info->sender, BT_OTP_EVENT,
698 BLUETOOTH_EVENT_OTP_INDICATION,
701 /* Remove info from list */
702 __bt_otp_remove_notification_info(info);
705 static bool __bt_otp_indication_timeout_cb(gpointer user_data)
708 handle = (char *) user_data;
709 bt_otp_notification_info *info = NULL;
711 info = __bt_otp_get_notification_info(handle);
712 BT_DBG("Activation timer Expired [Server] [%s]", info->handle);
714 __bt_otp_send_indication_event(info, NULL, 0, BLUETOOTH_ERROR_INTERNAL);
719 int _bt_otp_write_characteristic_value(int request_id, char *sender, char *handle,
720 unsigned char *param, int length)
722 GVariantBuilder *builder1;
725 GVariantBuilder *builder2;
727 bt_otp_notification_info *info = NULL;
728 GDBusConnection *conn;
729 char *charc_handle = g_strdup(handle);
734 BT_CHECK_PARAMETER(handle, return);
735 BT_CHECK_PARAMETER(sender, return);
736 BT_CHECK_PARAMETER(param, return);
738 conn = _bt_gdbus_get_system_gconn();
739 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
741 BT_DBG("OTP Write Characteristic value handle [%s] data length [%d]", handle, length);
742 /* Check if activation is ongoing for the same Remote Server */
744 info = __bt_otp_get_notification_info(handle);
745 if (info && info->notification_timeout_id > 0) {
746 BT_ERR("Write Request is already ongoing in remote server");
747 g_free(charc_handle);
748 return BLUETOOTH_ERROR_IN_PROGRESS;
751 builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
753 for (i = 0; i < length; i++) {
754 g_variant_builder_add(builder1, "y", param[i]);
757 val = g_variant_new("(ay)", builder1);
758 builder2 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
760 g_variant_builder_add(builder2, "{sv}", "offset",
761 g_variant_new_uint16(offset));
763 options = g_variant_new("a{sv}", builder2);
765 /* Activate Control Point */
766 g_dbus_connection_call(conn,
771 g_variant_new("(@ay@a{sv})",
774 G_DBUS_CALL_FLAGS_NONE,
776 (GAsyncReadyCallback)__bt_otp_write_request_cb,
777 (gpointer)charc_handle);
779 g_variant_builder_unref(builder1);
780 g_variant_builder_unref(builder2);
783 info = g_malloc0(sizeof(bt_otp_notification_info));
784 info->handle = g_strdup(handle);
785 info->sender = g_strdup(sender);
786 otp_notification_info_list = g_slist_append(otp_notification_info_list, info);
788 info->req_id = request_id;
789 info->notification_timeout_id = g_timeout_add(BT_INDICATION_TIMEOUT_MAX,
790 (GSourceFunc)__bt_otp_indication_timeout_cb, (gpointer)info->handle);
793 return BLUETOOTH_ERROR_NONE;
796 void _bt_otp_check_indication(const char *path, GVariant *msg)
798 bt_otp_notification_info *info = NULL;
799 unsigned char *buffer = NULL;
802 GVariant *value = NULL;
805 info = __bt_otp_get_notification_info((char *)path);
808 g_variant_get(msg, "(is@ay)", NULL, NULL, &value);
809 len = g_variant_get_size(value);
810 BT_DBG("Indication data from Server len[%d]", len);
812 buffer = (unsigned char *)g_variant_get_data(value);
814 for (i = 0; i < len; i++)
815 BT_DBG("%.2x", buffer[i]);
819 if (info->notification_timeout_id > 0)
820 g_source_remove(info->notification_timeout_id);
822 /* Send Indication & info removed internally */
823 __bt_otp_send_indication_event(info, buffer, len, BLUETOOTH_ERROR_NONE);
826 g_variant_unref(value);