Rollback changes to submit TIZEN:COMMON project
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-rfcomm-client.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <string.h>
21
22 #include "bluetooth-api.h"
23 #include "bt-internal-types.h"
24
25 #include "bt-common.h"
26 #include "bt-request-sender.h"
27 #include "bt-event-handler.h"
28
29 BT_EXPORT_API int bluetooth_rfcomm_connect(const bluetooth_device_address_t *remote_bt_address,
30                                                 const char *remote_uuid)
31 {
32         int result;
33         int connect_type;
34         bt_user_info_t *user_info;
35         char uuid[BLUETOOTH_UUID_STRING_MAX];
36
37         BT_CHECK_PARAMETER(remote_bt_address, return);
38         BT_CHECK_PARAMETER(remote_uuid, return);
39         BT_CHECK_ENABLED(return);
40
41         BT_INIT_PARAMS();
42         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
43
44         user_info = _bt_get_user_data(BT_COMMON);
45         retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
46
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;
50
51         g_array_append_vals(in_param1, remote_bt_address,
52                                 sizeof(bluetooth_device_address_t));
53
54         g_strlcpy(uuid, remote_uuid, sizeof(uuid));
55         g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
56
57         g_array_append_vals(in_param3, &connect_type, sizeof(int));
58
59         result = _bt_send_request_async(BT_BLUEZ_SERVICE,
60                                 BT_RFCOMM_CLIENT_CONNECT,
61                                 in_param1, in_param2,
62                                 in_param3, in_param4,
63                                 user_info->cb, user_info->user_data);
64
65         BT_DBG("result: %x", result);
66
67         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
68
69         return result;
70 }
71
72 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
73 {
74         int result;
75         int connected = FALSE;
76
77         BT_CHECK_ENABLED(return);
78
79         BT_INIT_PARAMS();
80         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
81
82         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_IS_CONNECTED,
83                 in_param1, in_param2, in_param3, in_param4, &out_param);
84
85         BT_DBG("result: %x", result);
86
87         if (result == BLUETOOTH_ERROR_NONE) {
88                 connected = g_array_index(out_param,
89                                 int, 0);
90         } else {
91                 BT_ERR("Fail to send request");
92         }
93
94         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
95
96         return connected;
97 }
98
99 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
100 {
101         int result;
102         int service_function;
103
104         BT_CHECK_ENABLED(return);
105
106         BT_INIT_PARAMS();
107         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
108
109         /* Support the OSP */
110         if (socket_fd == -1) {
111                 /* Cancel connect */
112                 service_function = BT_RFCOMM_CLIENT_CANCEL_CONNECT;
113         } else {
114                 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
115                 service_function = BT_RFCOMM_SOCKET_DISCONNECT;
116         }
117
118         result = _bt_send_request(BT_BLUEZ_SERVICE, service_function,
119                 in_param1, in_param2, in_param3, in_param4, &out_param);
120
121         BT_DBG("result: %x", result);
122
123         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
124
125         return result;
126 }
127
128 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
129 {
130         int result;
131         char *buffer;
132
133         BT_CHECK_PARAMETER(buf, return);
134         BT_CHECK_ENABLED(return);
135         retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
136
137         BT_INIT_PARAMS();
138         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
139
140         buffer = g_malloc0(length + 1);
141
142         g_strlcpy(buffer, buf, length + 1);
143
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);
147
148         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
149                 in_param1, in_param2, in_param3, in_param4, &out_param);
150
151         BT_DBG("result: %x", result);
152
153         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
154
155         g_free(buffer);
156
157         return result;
158 }
159