4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include "bluetooth-api.h"
23 #include "bt-internal-types.h"
25 #include "bt-common.h"
26 #include "bt-request-sender.h"
27 #include "bt-event-handler.h"
29 BT_EXPORT_API int bluetooth_rfcomm_connect(const bluetooth_device_address_t *remote_bt_address,
30 const char *remote_uuid)
34 bt_user_info_t *user_info;
35 char uuid[BLUETOOTH_UUID_STRING_MAX];
37 BT_CHECK_PARAMETER(remote_bt_address);
38 BT_CHECK_PARAMETER(remote_uuid);
42 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
44 user_info = _bt_get_user_data(BT_COMMON);
45 retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
47 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
48 /* In now, we only support to connecty using UUID */
49 connect_type = BT_RFCOMM_UUID;
51 g_array_append_vals(in_param1, remote_bt_address,
52 sizeof(bluetooth_device_address_t));
54 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
55 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
57 g_array_append_vals(in_param3, &connect_type, sizeof(int));
59 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
60 BT_RFCOMM_CLIENT_CONNECT,
63 user_info->cb, user_info->user_data);
65 BT_DBG("result: %x", result);
67 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
72 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
75 int connected = FALSE;
80 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
82 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_IS_CONNECTED,
83 in_param1, in_param2, in_param3, in_param4, &out_param);
85 BT_DBG("result: %x", result);
87 if (result == BLUETOOTH_ERROR_NONE) {
88 connected = g_array_index(out_param,
91 BT_ERR("Fail to send request");
94 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
99 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
102 int service_function;
107 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
109 /* Support the OSP */
110 if (socket_fd == -1) {
112 service_function = BT_RFCOMM_CLIENT_CANCEL_CONNECT;
114 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
115 service_function = BT_RFCOMM_SOCKET_DISCONNECT;
118 result = _bt_send_request(BT_BLUEZ_SERVICE, service_function,
119 in_param1, in_param2, in_param3, in_param4, &out_param);
121 BT_DBG("result: %x", result);
123 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
128 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
133 BT_CHECK_PARAMETER(buf);
135 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
138 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
140 buffer = g_malloc0(length + 1);
142 g_strlcpy(buffer, buf, length + 1);
144 g_array_append_vals(in_param1, &fd, sizeof(int));
145 g_array_append_vals(in_param2, &length, sizeof(int));
146 g_array_append_vals(in_param3, buffer, length);
148 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
149 in_param1, in_param2, in_param3, in_param4, &out_param);
151 BT_DBG("result: %x", result);
153 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);