initial MAP stub implementation
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-map-client.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <glib.h>
19 #include <dlog.h>
20 #include <string.h>
21 #include <mime_type.h>
22 #include <aul.h>
23 #include <glib.h>
24 #include <gio/gio.h>
25
26 #include "bluetooth-api.h"
27 #include "bt-internal-types.h"
28
29 #include "bt-service-common.h"
30 #include "bt-service-event.h"
31 #include "bt-service-util.h"
32 #include "bt-service-map-client.h"
33 #include "bt-service-obex-agent.h"
34 #include "bt-service-adapter.h"
35
36 #define DBUS_TIEMOUT 20 * 1000  /* 20 Seconds */
37
38 bt_session_info_t *session_info;
39
40 static void __bt_free_session_info(bt_session_info_t *info)
41 {
42         ret_if(info == NULL);
43
44         /* TODO: MAP bt_session_info_t, see bt-service-opp-client.c */
45
46         g_free(info->address);
47         g_free(info);
48 }
49
50 static void __bt_free_session_data(gpointer data)
51 {
52         bt_session_data_t *info = data;
53
54         ret_if(info == NULL);
55
56         _bt_delete_request_id(info->request_id);
57
58         /* TODO: MAP bt_session_data_t, see bt-service-opp-client.c */
59
60         g_free(info->address);
61         g_free(info);
62 }
63
64 static void __bt_session_release_cb(GDBusProxy *proxy,
65                                 GAsyncResult *res, gpointer user_data)
66 {
67         BT_DBG("+");
68         ret_if(session_info == NULL);
69
70         GError *error = NULL;
71         int result = BLUETOOTH_ERROR_NONE;
72         GVariant *param = NULL;
73         g_dbus_proxy_call_finish(proxy, res, &error);
74         if (proxy)
75                 g_object_unref(proxy);
76
77         if (error) {
78                 BT_ERR("%s", error->message);
79                 g_error_free(error);
80
81                 result = BLUETOOTH_ERROR_INTERNAL;
82         } else {
83                 BT_DBG("Session Removed");
84         }
85
86         session_info->result = result;
87         param = g_variant_new("(isi)", session_info->result,
88                                 session_info->address,
89                                 session_info->request_id);
90         /* Send the event in only error none case */
91         _bt_send_event(BT_OPP_CLIENT_EVENT,
92                         BLUETOOTH_EVENT_OPC_DISCONNECTED,
93                         param);
94
95         __bt_free_session_info(session_info);
96         session_info = NULL;
97
98         _bt_map_client_event_deinit();
99
100         /* Operate remain works */
101         /* TODO: MAP */
102
103         return;
104 fail:
105
106         BT_DBG("-");
107
108         return;
109 }
110
111 static int _bt_remove_session()
112 {
113         GDBusConnection *g_conn;
114         GDBusProxy *session_proxy;
115         GError *err = NULL;
116
117         g_conn = _bt_gdbus_get_session_gconn();
118         retv_if(g_conn == NULL, BLUETOOTH_ERROR_INTERNAL);
119         retv_if(session_info->session_path == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
120
121         session_proxy = g_dbus_proxy_new_sync(g_conn, G_DBUS_PROXY_FLAGS_NONE,
122                                                 NULL, BT_OBEXD_DBUS_NAME,
123                                                 BT_OBEX_CLIENT_PATH,
124                                                 BT_OBEX_CLIENT_INTERFACE,
125                                                 NULL, &err);
126
127         retv_if(session_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
128
129         g_dbus_proxy_call(session_proxy, "RemoveSession",
130                 g_variant_new("(o)", session_info->session_path),
131                 G_DBUS_CALL_FLAGS_NONE,
132                 DBUS_TIEMOUT, NULL,
133                 (GAsyncReadyCallback)__bt_session_release_cb,
134                 NULL);
135
136         return BLUETOOTH_ERROR_NONE;
137 }
138
139 void _bt_map_disconnected(const char *session_path)
140 {
141         BT_DBG("+");
142         GVariant *param = NULL;
143         ret_if(session_info == NULL);
144
145         if (g_strcmp0(session_info->session_path,
146                         session_path) != 0) {
147                 BT_INFO("Path mismatch, previous transfer failed! Returning");
148                 return;
149         }
150
151         param = g_variant_new("(isi)", session_info->result,
152                                 session_info->address,
153                                 session_info->request_id);
154         _bt_send_event(BT_MAP_CLIENT_EVENT,
155                         BLUETOOTH_EVENT_MAP_DISCONNECTED,
156                         param);
157
158         __bt_free_session_info(session_info);
159         session_info = NULL;
160
161         BT_DBG("-");
162 }
163
164 static void __bt_create_session_cb(GDBusProxy *proxy,
165                                 GAsyncResult *res, gpointer user_data)
166 {
167         BT_DBG("+");
168
169         GError *error = NULL;
170         GVariant *value;
171         int result = BLUETOOTH_ERROR_NONE;
172         char *session_path = NULL;
173         GVariant *param = NULL;
174
175         value = g_dbus_proxy_call_finish(proxy, res, &error);
176         if (value) {
177                 g_variant_get(value, "(o)", &session_path);
178                 g_variant_unref(value);
179         }
180         if (error) {
181
182                 BT_ERR("%s", error->message);
183                 g_clear_error(&error);
184
185                 result = BLUETOOTH_ERROR_INTERNAL;
186         } else {
187                 BT_DBG("Session created");
188                 if (session_info != NULL)
189                         session_info->session_path = g_strdup(session_path);
190         }
191         g_free(session_path);
192         g_object_unref(proxy);
193         ret_if(session_info == NULL);
194
195         session_info->result = result;
196         param = g_variant_new("(isi)", result,
197                                 session_info->address,
198                                 session_info->request_id);
199         /* Send the event in only error none case */
200         _bt_send_event(BT_MAP_CLIENT_EVENT,
201                         BLUETOOTH_EVENT_MAP_CONNECTED,
202                         param);
203
204         if (result != BLUETOOTH_ERROR_NONE) {
205                 BT_ERR("Calling __bt_session_release");
206                 gboolean ret = __bt_session_release();
207
208                 __bt_free_session_info(session_info);
209                 session_info = NULL;
210
211                 if (ret == FALSE) {
212                         BT_DBG("ReleaseSession Not called");
213                         /* Operate remain works */
214
215                         /* TODO: MAP */
216
217                 }
218         } else {
219
220                 /* TODO: MAP */
221
222         }
223         BT_DBG("-");
224
225 }
226
227 static int __bt_opp_client_start_XXXXXXXXXX(int request_id, char *address
228                                         /* parameters................... */)
229 {
230         /* TODO: MAP */
231 }
232
233 /* TODO: MAP */