Merge the code from tizen_2.4
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-rfcomm-server.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <dbus/dbus.h>
25 #include <glib.h>
26 #include <dlog.h>
27 #include <string.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
30
31 #include "bluetooth-api.h"
32 #include "bt-internal-types.h"
33
34 #include "bt-service-common.h"
35 #include "bt-service-event.h"
36 #include "bt-service-util.h"
37 #include "bt-service-rfcomm-client.h"
38 #include "bt-service-rfcomm-server.h"
39 #include "bt-service-agent.h"
40
41 /* Range of RFCOMM server ID : 0 ~ 244 */
42 #define BT_RFCOMM_SERVER_ID_MAX 245
43
44 #define BT_RFCOMM_PROXY_ADDRESS "x00/bluez/rfcomm"
45 #define BT_RFCOMM_SOCKET_ADDRESS "/bluez/rfcomm"
46
47 typedef struct {
48         int data_fd;
49         char *uuid;
50         char *remote_address;
51 } bt_rfcomm_event_info_t;
52
53 GSList *server_list;
54 bt_rfcomm_server_info_t *_bt_rfcomm_get_server_info_using_uuid(char *uuid)
55 {
56         GSList *l;
57         bt_rfcomm_server_info_t *server_info;
58
59         retv_if(uuid == NULL, NULL);
60
61         for (l = server_list; l != NULL; l = l->next) {
62                 server_info = l->data;
63
64                 if (server_info == NULL)
65                         continue;
66
67                 if (g_strcmp0(server_info->uuid, uuid) == 0)
68                         return server_info;
69         }
70
71         return NULL;
72 }
73
74 int _bt_rfcomm_create_socket(char *sender, char *uuid)
75 {
76         return BLUETOOTH_ERROR_INTERNAL;
77 }
78
79 int __bt_rfcomm_server_get_address(bt_rfcomm_server_info_t *server_info)
80 {
81         return BLUETOOTH_ERROR_NONE;
82 }
83
84 int _bt_rfcomm_listen(int socket_fd, int max_pending, gboolean is_native)
85 {
86         return BLUETOOTH_ERROR_NONE;
87 }
88
89 int _bt_rfcomm_remove_socket(int socket_fd)
90 {
91         return BLUETOOTH_ERROR_NONE;
92 }
93
94 int _bt_rfcomm_server_disconnect(int data_fd)
95 {
96         return BLUETOOTH_ERROR_NONE;
97 }
98
99 /* To support the BOT  */
100 int _bt_rfcomm_is_uuid_available(char *uuid, gboolean *available)
101 {
102         return BLUETOOTH_ERROR_NONE;
103 }
104
105 /* To support the BOT  */
106 int _bt_rfcomm_accept_connection(void)
107 {
108         BT_DBG("+");
109         if (!_bt_agent_reply_authorize(TRUE))
110                 return BLUETOOTH_ERROR_INTERNAL;
111
112         BT_DBG("-");
113         return BLUETOOTH_ERROR_NONE;
114 }
115
116 /* To support the BOT  */
117 int _bt_rfcomm_reject_connection(void)
118 {
119         BT_DBG("+");
120         if (!_bt_agent_reply_authorize(FALSE))
121                 return BLUETOOTH_ERROR_INTERNAL;
122
123         BT_DBG("-");
124         return BLUETOOTH_ERROR_NONE;
125 }
126
127 int _bt_rfcomm_server_disconnect_all_connection(void)
128 {
129         GSList *l;
130         bt_rfcomm_server_info_t *server_info;
131
132         for (l = server_list; l != NULL; l = l->next) {
133                 server_info = l->data;
134
135                 if (server_info == NULL)
136                         continue;
137
138                 _bt_rfcomm_disconnect(server_info->data_fd);
139         }
140
141         return BLUETOOTH_ERROR_NONE;
142 }
143
144 int _bt_rfcomm_server_check_existence(gboolean *existence)
145 {
146         BT_CHECK_PARAMETER(existence, return);
147
148         if (server_list && g_slist_length(server_list) > 0) {
149                 *existence = TRUE;
150         } else {
151                 *existence = FALSE;
152         }
153
154         return BLUETOOTH_ERROR_NONE;
155 }
156
157 int _bt_rfcomm_server_check_termination(char *name)
158 {
159         GSList *l;
160         bt_rfcomm_server_info_t *server_info;
161
162         BT_CHECK_PARAMETER(name, return);
163
164         for (l = server_list; l != NULL; l = l->next) {
165                 server_info = l->data;
166
167                 if (server_info == NULL)
168                         continue;
169
170                 if (g_strcmp0(server_info->sender, name) == 0) {
171                         _bt_rfcomm_remove_socket(server_info->control_fd);
172                 }
173         }
174
175         return BLUETOOTH_ERROR_NONE;
176 }
177
178