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.
23 #ifdef TIZEN_FEATURE_BT_DPM
24 #include "bt-service-dpm.h"
31 #include "bluetooth-api.h"
32 #include "bt-internal-types.h"
34 #include "bt-service-common.h"
35 #include "bt-service-event.h"
36 #include "bt-service-util.h"
37 #include "bt-service-obex-agent.h"
38 #include "bt-service-obex-server.h"
40 #include <oal-device-mgr.h>
42 #define DBUS_TIMEOUT 20 * 1000 /* 20 Seconds */
43 #define BT_OBEX_SERVER_AGENT_PATH "/org/obex/server_agent"
45 #define BT_OBEX_SERVICE "org.bluez.obex"
46 #define BT_OBEX_MANAGER "org.bluez.obex.AgentManager1"
47 #define BT_OBEX_PATH "/org/bluez/obex"
49 #define BT_OBEX_PATH_PREFIX "/opt/usr/media"
50 #define BT_OBEX_DEFAULT_PATH "/opt/usr/home/owner/media"
51 #define BT_OBEX_PATH_MAX_LENGTH 255
67 GDBusMethodInvocation *reply_context;
74 unsigned char contact_auth_info[5];
87 bt_auth_info_t *auth_info;
88 bt_server_info_t *native_server;
89 bt_server_info_t *custom_server;
90 } bt_obex_agent_info_t;
98 static GSList *transfers;
99 static bt_obex_agent_info_t agent_info;
100 static GSList *session_list = NULL;
102 static char *pending_auth_address = NULL;
104 static bt_session_info_t *__bt_find_session_by_path(char *transfer_path)
107 bt_session_info_t *session;
109 retv_if(transfer_path == NULL, NULL);
111 for (l = session_list; l != NULL; l = l->next) {
117 if (g_strcmp0(session->path, transfer_path) == 0)
124 static GQuark __bt_obex_error_quark(void)
126 static GQuark quark = 0;
128 quark = g_quark_from_static_string("agent");
133 static bt_transfer_info_t *__bt_find_transfer_by_id(int transfer_id)
136 bt_transfer_info_t *transfer;
138 for (l = transfers; l != NULL; l = l->next) {
141 if (transfer == NULL)
144 if (transfer->transfer_id == transfer_id)
151 bt_transfer_info_t *__bt_find_transfer_by_address(const char *address)
155 bt_transfer_info_t *transfer;
157 retv_if(address == NULL, NULL);
159 for (l = transfers; l != NULL; l = l->next) {
162 if (transfer == NULL)
165 if (g_strcmp0(transfer->address, address) == 0)
172 static bt_transfer_info_t *__bt_find_transfer_by_path(const char *transfer_path)
175 bt_transfer_info_t *transfer;
177 retv_if(transfer_path == NULL, NULL);
179 for (l = transfers; l != NULL; l = l->next) {
182 if (transfer == NULL)
185 if (g_strcmp0(transfer->path, transfer_path) == 0)
192 static void __bt_free_server_info(bt_server_info_t *server_info)
194 ret_if(server_info == NULL);
196 g_free(server_info->sender);
197 g_free(server_info->dest_path);
201 static void __bt_free_auth_info(bt_auth_info_t *auto_info)
203 ret_if(auto_info == NULL);
205 g_free(auto_info->filename);
206 g_free(auto_info->transfer_path);
207 g_free(auto_info->device_name);
208 g_free(auto_info->address);
212 static void __bt_free_transfer_info(bt_transfer_info_t *transfer_info)
214 ret_if(transfer_info == NULL);
216 g_free(transfer_info->path);
217 g_free(transfer_info->filename);
218 g_free(transfer_info->file_path);
219 g_free(transfer_info->type);
220 g_free(transfer_info->device_name);
221 g_free(transfer_info->address);
222 g_free(transfer_info);
225 void _bt_obex_check_pending_transfer(const char *address)
228 GVariant *param = NULL;
229 bt_transfer_info_t *transfer_info = __bt_find_transfer_by_address(address);
230 if (transfer_info != NULL) {
231 int result = BLUETOOTH_ERROR_CANCEL;
232 param = g_variant_new("(issssstii)", result,
233 transfer_info->filename,
235 transfer_info->device_name,
236 transfer_info->file_path,
237 transfer_info->address,
238 transfer_info->file_size,
239 transfer_info->transfer_id,
240 agent_info.server_type);
241 _bt_send_event(BT_OPP_SERVER_EVENT,
242 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_COMPLETED,
244 transfers = g_slist_remove(transfers, transfer_info);
245 __bt_free_transfer_info(transfer_info);
250 static char *__bt_get_remote_device_name(const char *bdaddress)
252 char *device_path = NULL;
255 GVariant *result = NULL;
257 GDBusProxy *device_proxy;
258 GDBusConnection *conn;
260 retv_if(bdaddress == NULL, NULL);
262 device_path = _bt_get_device_object_path((char *)bdaddress);
263 retv_if(device_path == NULL, NULL);
265 conn = _bt_get_system_gconn();
266 retv_if(conn == NULL, NULL);
267 BT_INFO("Device_path %s", device_path);
268 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
271 BT_PROPERTIES_INTERFACE,
275 retv_if(device_proxy == NULL, NULL);
277 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
278 g_variant_new("(s)", BT_DEVICE_INTERFACE),
279 G_DBUS_CALL_FLAGS_NONE,
283 BT_ERR("DBus Error : %s", err->message);
287 if (result == NULL) {
288 BT_ERR("g_dbus_proxy_call_sync function return NULL");
291 g_variant_get(result, "(@a{sv})", &value);
294 GVariant *temp_value = g_variant_lookup_value(value, "Alias",
295 G_VARIANT_TYPE_STRING);
296 g_variant_get(temp_value, "s", &name);
298 g_variant_unref(temp_value);
301 DBG_SECURE("Alias Name [%s]", name);
303 temp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
304 g_variant_get(temp_value, "s", &name);
306 g_variant_unref(temp_value);
307 DBG_SECURE("Name = %s", name);
310 g_variant_unref(result);
311 g_object_unref(device_proxy);
315 void __bt_get_auth_info(GVariant *reply, char *auth_info)
319 char *manufacturer_data = NULL;
320 int manufacturer_data_len;
321 gboolean is_alias_set;
322 GVariantIter *value_iter;
326 tmp_value = g_variant_lookup_value(reply, "IsAliasSet",
327 G_VARIANT_TYPE_BOOLEAN);
329 is_alias_set = g_variant_get_boolean(tmp_value);
330 g_variant_unref(tmp_value);
332 is_alias_set = FALSE;
334 if (is_alias_set == FALSE) {
335 tmp_value = g_variant_lookup_value(reply, "LegacyManufacturerDataLen",
336 G_VARIANT_TYPE_UINT16);
338 manufacturer_data_len = g_variant_get_uint16(tmp_value);
339 if (manufacturer_data_len >
340 BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
341 BT_ERR("manufacturer_data_len is too long");
342 manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
344 g_variant_unref(tmp_value);
346 manufacturer_data_len = 0;
348 tmp_value = g_variant_lookup_value(reply, "LegacyManufacturerData",
349 G_VARIANT_TYPE_ARRAY);
351 if ((manufacturer_data_len == 0) ||
352 manufacturer_data_len != g_variant_get_size(tmp_value)) {
353 BT_ERR("manufacturer data length doesn't match");
354 manufacturer_data_len = 0;
355 manufacturer_data = NULL;
357 manufacturer_data = g_malloc0(manufacturer_data_len);
358 g_variant_get(tmp_value, "ay", &value_iter);
359 while (g_variant_iter_loop(value_iter, "y", &m_value))
360 manufacturer_data[i++] = m_value;
362 g_variant_unref(tmp_value);
364 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
365 manufacturer_data_len = 0;
366 manufacturer_data = NULL;
368 /*minimum Size of the samsung specific manufacturer data is greater than 30 */
369 if (manufacturer_data_len < 30) {
370 g_free(manufacturer_data);
373 if (manufacturer_data[0] != 0x00 || manufacturer_data[1] != 0x75) {
374 BT_DBG("This is not a samsung specific manufaturer data");
375 g_free(manufacturer_data);
379 /* 2 samsung (0x00 0x75) + 1 (control and version) + 1 (service ID) +
380 1 (discovery version) + 1 (associated service ID)
381 2 (Proxamity and locality) + 2 (Device type and icon) */
385 memcpy(auth_info, &(manufacturer_data[cursor]), 5);
387 g_free(manufacturer_data);
390 static void __bt_get_remote_device_name_authinfo(const char *bdaddress,
391 char **device_name, unsigned char *auth_info)
393 char *device_path = NULL;
395 gboolean is_alias_set;
397 GVariant *result = NULL;
399 GDBusProxy *device_proxy;
400 GDBusConnection *conn;
402 ret_if(bdaddress == NULL);
404 device_path = _bt_get_device_object_path((char *)bdaddress);
405 ret_if(device_path == NULL);
407 conn = _bt_get_system_gconn();
408 ret_if(conn == NULL);
409 BT_INFO("Device_path %s", device_path);
410 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
413 BT_PROPERTIES_INTERFACE,
417 ret_if(device_proxy == NULL);
419 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
420 g_variant_new("(s)", BT_DEVICE_INTERFACE),
421 G_DBUS_CALL_FLAGS_NONE,
425 BT_ERR("DBus Error : %s", err->message);
429 if (result == NULL) {
430 BT_ERR("g_dbus_proxy_call_sync function return NULL");
433 g_variant_get(result, "(@a{sv})", &value);
436 GVariant *temp_value = g_variant_lookup_value(value, "Alias",
437 G_VARIANT_TYPE_STRING);
438 g_variant_get(temp_value, "s", &name);
440 g_variant_unref(temp_value);
443 DBG_SECURE("Alias Name [%s]", name);
445 temp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
446 g_variant_get(temp_value, "s", &name);
448 g_variant_unref(temp_value);
449 DBG_SECURE("Name = %s", name);
451 temp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
453 is_alias_set = g_variant_get_boolean(temp_value);
454 g_variant_unref(temp_value);
456 is_alias_set = FALSE;
459 if (is_alias_set == FALSE) {
460 DBG_SECURE("Do Nothing");
461 __bt_get_auth_info(value, (char *)auth_info);
464 g_variant_unref(result);
465 g_object_unref(device_proxy);
467 *device_name = g_strdup(name);
472 static int __bt_get_transfer_id(const char *path)
478 tmp = strrchr(path, 'r') + 1;
479 retv_if(tmp == NULL, -1);
484 static GDBusProxy *__bt_get_transfer_proxy(const char *transfer_path)
486 GDBusConnection *conn;
490 conn = _bt_get_system_gconn();
491 retv_if(conn == NULL, NULL);
493 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
494 NULL, BT_OBEX_SERVICE_NAME,
496 BT_OBEX_TRANSFER_INTERFACE,
500 BT_ERR("Error : %s", err->message);
508 static GDBusProxy *__bt_get_transfer_properties_proxy(const char *transfer_path)
510 GDBusConnection *conn;
513 conn = _bt_get_system_gconn();
514 retv_if(conn == NULL, NULL);
516 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
517 NULL, BT_OBEX_SERVICE_NAME,
519 BT_PROPERTIES_INTERFACE,
522 BT_ERR("Error : %s", err->message);
529 static int __bt_get_transfer_properties(bt_transfer_info_t *transfer_info,
530 const char *transfer_path)
532 GDBusProxy *transfer_proxy;
533 GVariant *result = NULL;
535 GVariantIter *iter = NULL;
536 BT_CHECK_PARAMETER(transfer_info, return);
537 BT_CHECK_PARAMETER(transfer_path, return);
539 transfer_proxy = __bt_get_transfer_properties_proxy(transfer_path);
541 retv_if(transfer_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
543 result = g_dbus_proxy_call_sync(transfer_proxy, "GetAll",
544 g_variant_new("(s)", BT_OBEX_TRANSFER_INTERFACE),
545 G_DBUS_CALL_FLAGS_NONE,
550 BT_ERR("DBus Error : %s", err->message);
554 if (result == NULL) {
555 BT_ERR("g_dbus_proxy_call_sync function return NULL");
559 g_variant_get(result, "(a{sv})", &iter);
560 g_variant_unref(result);
565 while (g_variant_iter_loop(iter, "{sv}", &key, &val)) {
566 if (g_strcmp0(key, "Operation") == 0) {
567 transfer_info->type = g_variant_dup_string(val, &len);
568 } else if (g_strcmp0(key, "Name") == 0) {
569 transfer_info->filename = g_variant_dup_string(val, &len);
570 } else if (g_strcmp0(key, "Size") == 0) {
571 transfer_info->file_size = g_variant_get_uint64(val);
572 } else if (g_strcmp0(key, "Address") == 0) {
573 transfer_info->address = g_variant_dup_string(val, &len);
574 BT_INFO("addressss %s", transfer_info->address);
575 } else if (g_strcmp0(key, "Filename") == 0) {
576 transfer_info->file_path = g_variant_dup_string(val, &len);
577 if (!transfer_info->file_path)
578 transfer_info->file_path = g_strdup(transfer_info->filename);
581 g_variant_iter_free(iter);
583 if (transfer_info->address == NULL)
585 transfer_info->device_name = __bt_get_remote_device_name(transfer_info->address);
586 transfer_info->transfer_id = __bt_get_transfer_id(transfer_path);
587 if (!transfer_info->device_name)
588 transfer_info->device_name = g_strdup(transfer_info->address);
590 if (transfer_info->type == NULL)
593 transfer_info->path = g_strdup(transfer_path);
596 g_object_unref(transfer_proxy);
597 return BLUETOOTH_ERROR_NONE;
600 g_object_unref(transfer_proxy);
601 return BLUETOOTH_ERROR_INTERNAL;
604 static gboolean __bt_authorize_cb(GDBusMethodInvocation *context,
608 char *device_name = NULL;
609 unsigned char auth_info[5] = {0, };
610 int result = BLUETOOTH_ERROR_NONE;
611 GDBusProxy *transfer_properties_proxy;
612 char * bdaddress = NULL;
615 GVariant *param = NULL;
617 bt_session_info_t *session_info = NULL;
618 #ifdef TIZEN_FEATURE_BT_DPM
619 int value = DPM_BT_ERROR;
622 BT_DBG(" path [%s] \n", path);
624 transfer_properties_proxy = __bt_get_transfer_properties_proxy(path);
626 retv_if(transfer_properties_proxy == NULL, FALSE);
628 ret = g_dbus_proxy_call_sync(transfer_properties_proxy, "GetAll",
629 g_variant_new("(s)", BT_OBEX_TRANSFER_INTERFACE),
630 G_DBUS_CALL_FLAGS_NONE,
634 BT_ERR("DBus Error : %s", err->message);
639 BT_ERR("g_dbus_proxy_call_sync function return NULL");
642 g_variant_get(ret, "(a{sv})", &iter);
643 g_variant_unref(ret);
645 g_object_unref(transfer_properties_proxy);
649 __bt_free_auth_info(agent_info.auth_info);
651 agent_info.auth_info = g_malloc(sizeof(bt_auth_info_t));
653 memset(agent_info.auth_info, 0, sizeof(bt_auth_info_t));
655 agent_info.auth_info->reply_context = context;
657 agent_info.auth_info->transfer_path = g_strdup(path);
659 #ifdef TIZEN_FEATURE_BT_DPM
660 _bt_dpm_get_allow_bluetooth_mode(&value);
661 if (value == DPM_BT_HANDSFREE_ONLY) {
662 /* Free auth info in next function */
663 _bt_obex_server_reject_authorize();
671 while (g_variant_iter_loop(iter, "{sv}", &key, &val)) {
672 if (g_strcmp0(key, "Name") == 0)
673 agent_info.auth_info->filename = g_variant_dup_string(val, &len);
674 else if (g_strcmp0(key, "Address") == 0)
675 bdaddress = g_variant_dup_string(val, &len);
676 else if (g_strcmp0(key, "Size") == 0)
677 agent_info.auth_info->file_size = g_variant_get_uint64(val);
679 g_variant_iter_free(iter);
682 __bt_get_remote_device_name_authinfo(bdaddress, &device_name, auth_info);
685 device_name = g_strdup(bdaddress);
687 agent_info.auth_info->address = g_strdup(bdaddress);
688 agent_info.auth_info->device_name = device_name;
689 memcpy(agent_info.auth_info->contact_auth_info, auth_info, 5);
691 session_info = __bt_find_session_by_path((char *)path);
692 if (NULL == session_info) {
693 session_info = g_malloc0(sizeof(bt_session_info_t));
694 session_info->path = g_strdup(path);
695 session_info->address = g_strdup(bdaddress);
696 session_info->authorized = FALSE;
697 session_list = g_slist_append(session_list, session_info);
700 g_object_unref(transfer_properties_proxy);
703 if (agent_info.server_type == BT_CUSTOM_SERVER) {
704 /* No need to send the event */
705 _bt_obex_server_accept_authorize(agent_info.auth_info->filename, FALSE);
709 if (session_info->authorized == FALSE) {
710 if (headed_plugin_info->plugin_headed_enabled)
711 headed_plugin_info->headed_plugin->bt_launch_system_popup(BT_AGENT_EVENT_EXCHANGE_REQUEST, device_name,
712 auth_info, NULL, NULL, BT_OBEX_SERVER_AGENT_PATH);
714 /* TODO_40 : 4.0 merge */
715 param = g_variant_new("(istss)", result,
716 agent_info.auth_info->filename,
717 agent_info.auth_info->file_size,
718 agent_info.auth_info->address,
719 agent_info.auth_info->device_name);
720 _bt_send_event(BT_OPP_SERVER_EVENT,
721 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE, param);
727 void _bt_obex_transfer_started(const char *transfer_path)
729 bt_transfer_info_t *transfer_info;
730 request_info_t *req_info;
731 GVariant *out_param1 = NULL;
732 GVariant *param = NULL;
733 GVariantBuilder *builder = NULL;
734 int result = BLUETOOTH_ERROR_NONE;
737 BT_DBG("%s", transfer_path);
739 transfer_info = g_malloc0(sizeof(bt_transfer_info_t));
741 if (agent_info.auth_info != NULL
742 && g_strcmp0(transfer_path, agent_info.auth_info->transfer_path) == 0) {
743 transfer_info->filename = g_strdup(agent_info.auth_info->filename);
744 transfer_info->file_size = agent_info.auth_info->file_size;
745 transfer_info->type = g_strdup(TRANSFER_PUT);
746 transfer_info->path = g_strdup(agent_info.auth_info->transfer_path);
747 transfer_info->device_name = g_strdup(agent_info.auth_info->device_name);
748 transfer_info->transfer_id = __bt_get_transfer_id(transfer_path);
749 transfer_info->file_path = agent_info.auth_info->file_path;
750 transfer_info->address = g_strdup(agent_info.auth_info->address);
753 if (__bt_get_transfer_properties(transfer_info, transfer_path) < 0) {
754 BT_ERR("Get Properties failed");
755 __bt_free_transfer_info(transfer_info);
758 agent_info.server_type = BT_FTP_SERVER;
761 if (agent_info.server_type == BT_CUSTOM_SERVER) {
762 if (agent_info.custom_server == NULL) {
763 __bt_free_transfer_info(transfer_info);
764 __bt_free_auth_info(agent_info.auth_info);
765 agent_info.auth_info = NULL;
769 req_info = _bt_get_request_info(agent_info.accept_id);
770 if (req_info == NULL || req_info->context == NULL) {
771 BT_ERR("info is NULL");
775 agent_info.accept_id = 0;
776 result = BLUETOOTH_ERROR_NONE;
777 GArray *g_out_param1 = NULL;
778 g_out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
779 if (out_param1 == NULL) {
780 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
781 g_out_param1->data, g_out_param1->len,
785 g_dbus_method_invocation_return_value(req_info->context,
786 g_variant_new("(iv)", result, out_param1));
787 g_array_free(g_out_param1, TRUE);
788 _bt_delete_request_list(req_info->req_id);
791 transfers = g_slist_append(transfers, transfer_info);
793 BT_DBG("Transfer id %d", transfer_info->transfer_id);
795 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
796 for (i = 0; i < 5; i++) {
797 if (agent_info.auth_info)
798 g_variant_builder_add(builder, "y", agent_info.auth_info->contact_auth_info[i]);
801 param = g_variant_new("(isssstii(ay))", result,
802 transfer_info->device_name,
803 transfer_info->filename,
805 transfer_info->address,
806 transfer_info->file_size,
807 transfer_info->transfer_id,
808 agent_info.server_type,
811 __bt_free_auth_info(agent_info.auth_info);
812 agent_info.auth_info = NULL;
814 g_variant_builder_unref(builder);
816 _bt_send_event(BT_OPP_SERVER_EVENT,
817 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED,
821 void _bt_obex_transfer_progress(const char *transfer_path,
825 bt_transfer_info_t *transfer_info;
826 int current_progress = 0;
827 int previous_progress;
828 GVariant *param = NULL;
829 int result = BLUETOOTH_ERROR_NONE;
831 transfer_info = __bt_find_transfer_by_path(transfer_path);
832 ret_if(transfer_info == NULL);
834 current_progress = (int)(((gdouble)transferred /
835 (gdouble)transfer_info->file_size) * 100);
837 previous_progress = (int)(((gdouble)transfer_info->progress /
838 (gdouble)transfer_info->file_size) * 100);
840 if (current_progress == previous_progress) {
841 BT_DBG("Same Percentage Value: Do not emit Signal");
845 transfer_info->progress = transferred;
846 param = g_variant_new("(isssstiii)", result,
847 transfer_info->filename,
849 transfer_info->device_name,
850 transfer_info->address,
851 transfer_info->file_size,
852 transfer_info->transfer_id,
854 agent_info.server_type);
855 _bt_send_event(BT_OPP_SERVER_EVENT,
856 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_PROGRESS,
861 void _bt_obex_transfer_completed(const char *transfer_path, gboolean success)
863 bt_transfer_info_t *transfer_info;
864 GVariantBuilder *builder = NULL;
865 GVariant *param = NULL;
868 BT_DBG("Transfer [%s] Success [%d] \n", transfer_path, success);
870 result = (success == TRUE) ? BLUETOOTH_ERROR_NONE
871 : BLUETOOTH_ERROR_CANCEL;
873 transfer_info = __bt_find_transfer_by_path(transfer_path);
875 if (transfer_info == NULL) {
876 BT_DBG("Very small files receiving case, did not get Active status from obexd");
877 if (agent_info.auth_info == NULL ||
878 g_strcmp0(transfer_path,
879 agent_info.auth_info->transfer_path) != 0) {
880 BT_ERR("auth_info is NULL, returning");
884 transfer_info = g_new0(bt_transfer_info_t, 1);
886 transfer_info->filename = g_strdup(agent_info.auth_info->filename);
887 transfer_info->file_size = agent_info.auth_info->file_size;
888 transfer_info->type = g_strdup(TRANSFER_PUT);
889 transfer_info->path = g_strdup(agent_info.auth_info->transfer_path);
890 transfer_info->device_name = g_strdup(agent_info.auth_info->device_name);
891 transfer_info->transfer_id = __bt_get_transfer_id(transfer_path);
892 transfer_info->file_path = agent_info.auth_info->file_path;
893 transfer_info->address = g_strdup(agent_info.auth_info->address);
895 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
896 for (i = 0; i < 5; i++)
897 g_variant_builder_add(builder, "y", agent_info.auth_info->contact_auth_info[i]);
899 param = g_variant_new("(isssstii(ay))", result,
900 transfer_info->device_name,
901 transfer_info->filename,
903 transfer_info->address,
904 transfer_info->file_size,
905 transfer_info->transfer_id,
906 agent_info.server_type,
908 _bt_send_event(BT_OPP_SERVER_EVENT,
909 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_STARTED,
911 g_variant_builder_unref(builder);
913 param = g_variant_new("(issssstii)", result,
914 transfer_info->filename,
916 transfer_info->device_name,
917 transfer_info->file_path,
918 transfer_info->address,
919 transfer_info->file_size,
920 transfer_info->transfer_id,
921 agent_info.server_type);
922 _bt_send_event(BT_OPP_SERVER_EVENT,
923 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_COMPLETED,
925 transfers = g_slist_remove(transfers, transfer_info);
926 __bt_free_transfer_info(transfer_info);
929 void _bt_obex_transfer_connected(const char *obj_path)
933 int result = BLUETOOTH_ERROR_NONE;
934 GVariant *param = NULL;
935 bt_transfer_info_t *transfer_info = NULL;
937 transfer_info = g_new0(bt_transfer_info_t, 1);
938 __bt_get_transfer_properties(transfer_info, obj_path);
939 DBG_SECURE("Address[%s] Name[%s] TransferID[%d] ", transfer_info->address,
940 transfer_info->device_name, transfer_info->transfer_id);
942 param = g_variant_new("(issi)", result, transfer_info->address,
943 transfer_info->device_name, transfer_info->transfer_id);
945 _bt_send_event(BT_OPP_SERVER_EVENT,
946 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_CONNECTED,
949 __bt_free_transfer_info(transfer_info);
953 void _bt_obex_transfer_disconnected(char * obj_path)
957 int result = BLUETOOTH_ERROR_NONE;
958 GVariant *param = NULL;
959 bt_session_info_t *session = NULL;
960 int transfer_id = -1;
962 session = __bt_find_session_by_path(obj_path);
963 ret_if(session == NULL);
965 transfer_id = __bt_get_transfer_id(obj_path);
966 DBG_SECURE("transfer_id: [%d]", transfer_id);
968 DBG_SECURE("%s", session->address);
969 param = g_variant_new("(isi)", result, session->address, transfer_id);
970 _bt_send_event(BT_OPP_SERVER_EVENT,
971 BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_DISCONNECTED,
973 session_list = g_slist_remove(session_list, session);
974 g_free(session->address);
975 g_free(session->path);
980 int _bt_register_obex_server(void)
982 GDBusConnection *g_conn;
983 GDBusProxy *manager_proxy;
984 GVariant *result = NULL;
985 GError *g_error = NULL;
987 /* Get the session bus. */
988 g_conn = _bt_get_system_gconn();
989 retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
991 _bt_obex_agent_new(BT_OBEX_SERVER_AGENT_PATH);
993 _bt_obex_setup(BT_OBEX_SERVER_AGENT_PATH);
995 _bt_obex_set_authorize_cb(BT_OBEX_SERVER_AGENT_PATH,
996 __bt_authorize_cb, NULL);
998 manager_proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
999 NULL, BT_OBEX_SERVICE,
1004 if (manager_proxy == NULL)
1005 return BLUETOOTH_ERROR_INTERNAL;
1007 result = g_dbus_proxy_call_sync(manager_proxy, "RegisterAgent",
1008 g_variant_new("(o)", BT_OBEX_SERVER_AGENT_PATH),
1009 G_DBUS_CALL_FLAGS_NONE,
1013 if (g_error != NULL) {
1014 BT_ERR("Agent registration failed: %s\n", g_error->message);
1015 g_object_unref(manager_proxy);
1016 g_error_free(g_error);
1017 return BLUETOOTH_ERROR_INTERNAL;
1021 g_variant_unref(result);
1023 agent_info.proxy = manager_proxy;
1025 return BLUETOOTH_ERROR_NONE;
1028 int _bt_unregister_obex_server(void)
1030 GVariant *result = NULL;
1031 GError *g_error = NULL;
1033 retv_if(agent_info.proxy == NULL,
1034 BLUETOOTH_ERROR_INTERNAL);
1036 result = g_dbus_proxy_call_sync(agent_info.proxy, "UnregisterAgent",
1037 g_variant_new("(o)", BT_OBEX_SERVER_AGENT_PATH),
1038 G_DBUS_CALL_FLAGS_NONE,
1041 if (g_error != NULL) {
1042 BT_ERR("Agent unregistration failed: %s", g_error->message);
1043 g_error_free(g_error);
1047 g_variant_unref(result);
1049 _bt_obex_agent_destroy(BT_OBEX_SERVER_AGENT_PATH);
1050 g_object_unref(agent_info.proxy);
1051 agent_info.proxy = NULL;
1053 return BLUETOOTH_ERROR_NONE;
1056 gboolean __bt_check_folder_path(const char *dest_path)
1060 retv_if(dest_path == NULL, FALSE);
1062 dp = opendir(dest_path);
1065 BT_ERR("The directory does not exist");
1074 char *__bt_transfer_folder_path(char *dest_path)
1076 char *dst_path = (char *)g_malloc0(BT_OBEX_PATH_MAX_LENGTH);
1077 if (g_str_has_prefix(dest_path, BT_OBEX_PATH_PREFIX))
1078 snprintf(dst_path, BT_OBEX_PATH_MAX_LENGTH, BT_OBEX_DEFAULT_PATH"%s", dest_path + strlen(BT_OBEX_PATH_PREFIX));
1080 snprintf(dst_path, BT_OBEX_PATH_MAX_LENGTH, "%s", dest_path);
1082 BT_INFO("obex transfed path : %s", dst_path);
1086 int _bt_obex_server_allocate(char *sender, const char *dest_path, int app_pid, gboolean is_native)
1091 dst_path = __bt_transfer_folder_path((char *)dest_path);
1093 if (__bt_check_folder_path(dst_path) == FALSE) {
1095 return BLUETOOTH_ERROR_INVALID_PARAM;
1098 if (is_native == TRUE) {
1099 if (agent_info.native_server) {
1100 BT_ERR("obex native server busy");
1102 return BLUETOOTH_ERROR_DEVICE_BUSY;
1105 /* Force to change the control to native */
1106 agent_info.native_server = g_malloc0(sizeof(bt_server_info_t));
1107 agent_info.native_server->dest_path = g_strdup(dst_path);
1108 agent_info.native_server->sender = g_strdup(sender);
1109 agent_info.native_server->app_pid = app_pid;
1110 agent_info.server_type = BT_NATIVE_SERVER;
1111 if (OAL_STATUS_SUCCESS !=
1112 device_set_osp_server(OAL_OSP_SERVER_OBEX, FALSE))
1113 BT_ERR("device_set_osp_server failed");
1115 if (agent_info.custom_server) {
1116 BT_ERR("obex custom server busy");
1118 return BLUETOOTH_ERROR_DEVICE_BUSY;
1121 /* Force to change the control to custom */
1122 agent_info.custom_server = g_malloc0(sizeof(bt_server_info_t));
1123 agent_info.custom_server->dest_path = g_strdup(dst_path);
1124 agent_info.custom_server->sender = g_strdup(sender);
1125 agent_info.custom_server->app_pid = app_pid;
1126 agent_info.server_type = BT_CUSTOM_SERVER;
1127 if (OAL_STATUS_SUCCESS !=
1128 device_set_osp_server(OAL_OSP_SERVER_OBEX, TRUE))
1129 BT_ERR("device_set_osp_server failed");
1134 return BLUETOOTH_ERROR_NONE;
1137 int _bt_obex_server_deallocate(int app_pid, gboolean is_native)
1139 if (is_native == TRUE) {
1140 retv_if(agent_info.native_server == NULL,
1141 BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST);
1143 retv_if(agent_info.native_server->app_pid != app_pid,
1144 BLUETOOTH_ERROR_ACCESS_DENIED);
1146 __bt_free_server_info(agent_info.native_server);
1147 agent_info.native_server = NULL;
1149 /* Change the control to custom */
1150 if (agent_info.custom_server &&
1151 agent_info.server_type != BT_CUSTOM_SERVER) {
1153 agent_info.server_type = BT_CUSTOM_SERVER;
1155 if (OAL_STATUS_SUCCESS !=
1156 device_set_osp_server(OAL_OSP_SERVER_OBEX, TRUE))
1157 BT_ERR("device_set_osp_server failed");
1160 retv_if(agent_info.custom_server == NULL,
1161 BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST);
1163 retv_if(agent_info.custom_server->app_pid != app_pid,
1164 BLUETOOTH_ERROR_ACCESS_DENIED);
1167 __bt_free_server_info(agent_info.custom_server);
1168 agent_info.custom_server = NULL;
1170 /* Change the control to native */
1171 if (agent_info.native_server &&
1172 agent_info.server_type != BT_NATIVE_SERVER) {
1174 agent_info.server_type = BT_NATIVE_SERVER;
1176 if (OAL_STATUS_SUCCESS !=
1177 device_set_osp_server(OAL_OSP_SERVER_OBEX, FALSE))
1178 BT_ERR("device_set_osp_server failed");
1182 return BLUETOOTH_ERROR_NONE;
1185 int _bt_obex_server_accept_authorize(const char *filename, gboolean is_native)
1187 char file_path[BT_FILE_PATH_MAX] = { 0 };
1188 bt_server_info_t *server_info;
1190 BT_CHECK_PARAMETER(filename, return);
1192 retv_if(agent_info.auth_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1194 retv_if(agent_info.auth_info->reply_context == NULL,
1195 BLUETOOTH_ERROR_INTERNAL);
1197 if (is_native == TRUE)
1198 server_info = agent_info.native_server;
1200 server_info = agent_info.custom_server;
1202 retv_if(server_info == NULL, BLUETOOTH_ERROR_INTERNAL);
1204 if (server_info->dest_path != NULL)
1205 snprintf(file_path, sizeof(file_path), "%s/%s",
1206 server_info->dest_path, filename);
1208 snprintf(file_path, sizeof(file_path), "%s", filename);
1210 g_dbus_method_invocation_return_value(agent_info.auth_info->reply_context,
1211 g_variant_new("(s)", &file_path));
1212 agent_info.auth_info->reply_context = NULL;
1213 agent_info.auth_info->file_path = g_strdup(file_path);
1214 g_free(agent_info.auth_info->filename);
1215 agent_info.auth_info->filename = g_strdup(filename);
1217 return BLUETOOTH_ERROR_NONE;
1220 void _bt_obex_server_reply_accept(void)
1222 GVariant *param = NULL;
1223 bt_session_info_t *session_info = NULL;
1224 int result = BLUETOOTH_ERROR_NONE;
1225 param = g_variant_new("(istss)", result,
1226 agent_info.auth_info->filename,
1227 agent_info.auth_info->file_size,
1228 agent_info.auth_info->address,
1229 agent_info.auth_info->device_name);
1230 BT_INFO("Send Obex Authorize");
1231 _bt_send_event(BT_OPP_SERVER_EVENT, BLUETOOTH_EVENT_OBEX_SERVER_TRANSFER_AUTHORIZE, param);
1233 session_info = __bt_find_session_by_path(agent_info.auth_info->transfer_path);
1235 if (NULL == session_info)
1236 BT_ERR("Couldn't get the session info from the list");
1238 session_info->authorized = TRUE;
1241 int _bt_obex_server_reject_authorize(void)
1245 retv_if(agent_info.auth_info->reply_context == NULL,
1246 BLUETOOTH_ERROR_INTERNAL);
1248 g_error = g_error_new(__bt_obex_error_quark(),
1249 BT_OBEX_AGENT_ERROR_CANCEL,
1252 g_dbus_method_invocation_return_gerror(agent_info.auth_info->reply_context,
1254 g_error_free(g_error);
1256 __bt_free_auth_info(agent_info.auth_info);
1257 agent_info.auth_info = NULL;
1259 return BLUETOOTH_ERROR_NONE;
1262 int _bt_obex_server_set_destination_path(const char *dest_path,
1265 bt_server_info_t *server_info;
1266 BT_CHECK_PARAMETER(dest_path, return);
1269 dst_path = __bt_transfer_folder_path((char *)dest_path);
1273 dp = opendir(dst_path);
1276 BT_ERR("The directory does not exist");
1278 return BLUETOOTH_ERROR_INVALID_PARAM;
1283 if (is_native == TRUE)
1284 server_info = agent_info.native_server;
1286 server_info = agent_info.custom_server;
1289 BT_ERR("obex server info is NULL");
1291 return BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST;
1294 g_free(server_info->dest_path);
1295 server_info->dest_path = g_strdup(dst_path);
1298 return BLUETOOTH_ERROR_NONE;
1301 int _bt_obex_server_set_root(const char *root)
1303 GVariant *result = NULL;
1304 GError *g_error = NULL;
1305 GVariant *folder = NULL;
1306 char *string = "Root";
1309 BT_CHECK_PARAMETER(root, return);
1312 dst_root = __bt_transfer_folder_path((char *)root);
1314 if (!agent_info.proxy) {
1315 BT_ERR("obex agent_info proxy error");
1317 return BLUETOOTH_ERROR_INTERNAL;
1320 dp = opendir(dst_root);
1323 BT_ERR("The directory does not exist");
1325 return BLUETOOTH_ERROR_INVALID_PARAM;
1330 folder = g_variant_new_string(dst_root);
1331 result = g_dbus_proxy_call_sync(agent_info.proxy, "SetProperty",
1332 g_variant_new("(sv)", string, folder),
1333 G_DBUS_CALL_FLAGS_NONE,
1338 BT_ERR("SetProperty Fail: %s", g_error->message);
1339 g_error_free(g_error);
1341 return BLUETOOTH_ERROR_INTERNAL;
1345 g_variant_unref(result);
1348 return BLUETOOTH_ERROR_NONE;
1351 int _bt_obex_server_cancel_transfer(int transfer_id)
1353 bt_transfer_info_t *transfer = NULL;
1355 GVariant *result = NULL;
1358 transfer = __bt_find_transfer_by_id(transfer_id);
1360 retv_if(transfer == NULL, BLUETOOTH_ERROR_NOT_FOUND);
1361 proxy = __bt_get_transfer_proxy(transfer->path);
1363 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1365 result = g_dbus_proxy_call_sync(proxy, "Cancel", NULL,
1366 G_DBUS_CALL_FLAGS_NONE,
1367 DBUS_TIMEOUT, NULL, &err);
1369 BT_ERR("Dbus Err: %s", err->message);
1370 g_clear_error(&err);
1373 g_object_unref(proxy);
1376 g_variant_unref(result);
1378 return BLUETOOTH_ERROR_NONE;
1381 int _bt_obex_server_cancel_all_transfers(void)
1384 bt_transfer_info_t *transfer;
1386 for (l = transfers; l != NULL; l = l->next) {
1389 if (transfer == NULL)
1392 _bt_obex_server_cancel_transfer(transfer->transfer_id);
1395 return BLUETOOTH_ERROR_NONE;
1398 int _bt_obex_server_is_activated(gboolean *activated)
1400 BT_CHECK_PARAMETER(activated, return);
1402 if (agent_info.custom_server)
1407 return BLUETOOTH_ERROR_NONE;
1410 int _bt_obex_server_check_allocation(gboolean *allocation)
1412 BT_CHECK_PARAMETER(allocation, return);
1414 if (agent_info.native_server || agent_info.custom_server)
1417 *allocation = FALSE;
1419 return BLUETOOTH_ERROR_NONE;
1422 int _bt_obex_server_check_termination(char *sender)
1424 BT_CHECK_PARAMETER(sender, return);
1426 if (agent_info.native_server) {
1427 if (g_strcmp0(sender, agent_info.native_server->sender) == 0) {
1428 _bt_obex_server_deallocate(agent_info.native_server->app_pid,
1433 if (agent_info.custom_server) {
1434 if (g_strcmp0(sender, agent_info.custom_server->sender) == 0) {
1435 _bt_obex_server_deallocate(agent_info.custom_server->app_pid,
1440 return BLUETOOTH_ERROR_NONE;
1443 int _bt_obex_server_is_receiving(gboolean *receiving)
1445 BT_CHECK_PARAMETER(receiving, return);
1447 if (transfers == NULL || g_slist_length(transfers) == 0)
1452 return BLUETOOTH_ERROR_NONE;
1455 void _bt_obex_server_set_pending_conn_auth_device_addr(const char *address)
1457 if (pending_auth_address)
1458 g_free(pending_auth_address);
1460 pending_auth_address = g_strdup(address);
1463 /* To support the BOT */
1464 int _bt_obex_server_accept_connection(int request_id)
1467 bt_address_t bd_addr;
1469 BT_INFO("address: %s", pending_auth_address);
1470 _bt_convert_addr_string_to_type(bd_addr.addr, pending_auth_address);
1471 res = device_reply_auth_request(&bd_addr, OPP_SERVICE_ID, TRUE, FALSE);
1472 g_free(pending_auth_address);
1473 pending_auth_address = NULL;
1474 if (res != OAL_STATUS_SUCCESS) {
1475 BT_ERR("device_reply_auth_request failed");
1476 return BLUETOOTH_ERROR_INTERNAL;
1479 agent_info.accept_id = request_id;
1481 return BLUETOOTH_ERROR_NONE;
1484 /* To support the BOT */
1485 int _bt_obex_server_reject_connection(void)
1488 bt_address_t bd_addr;
1490 BT_INFO("address: %s", pending_auth_address);
1491 _bt_convert_addr_string_to_type(bd_addr.addr, pending_auth_address);
1492 res = device_reply_auth_request(&bd_addr, OPP_SERVICE_ID, FALSE, FALSE);
1493 g_free(pending_auth_address);
1494 pending_auth_address = NULL;
1495 if (res != OAL_STATUS_SUCCESS) {
1496 BT_ERR("device_reply_auth_request failed");
1497 return BLUETOOTH_ERROR_INTERNAL;
1500 return BLUETOOTH_ERROR_NONE;
1503 int _bt_opp_get_server_progress(int transfer_id, guint8 *progress)
1505 bt_transfer_info_t *requested_transfer = NULL;
1506 requested_transfer = __bt_find_transfer_by_id(transfer_id);
1507 if (requested_transfer == NULL) {
1508 BT_ERR("No Matching Inbound transfer");
1509 return BLUETOOTH_ERROR_NOT_FOUND;
1512 *progress = (int)(((double)requested_transfer->progress /
1513 requested_transfer->file_size) * 100);
1515 BT_DBG("Percentage: %d", *progress);
1516 return BLUETOOTH_ERROR_NONE;
1519 gboolean _bt_obex_server_is_custom(void)
1521 return (agent_info.server_type == BT_CUSTOM_SERVER) ? TRUE : FALSE;