2 * tel-plugin-socket-communicator
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Ja-young Gu <jygu@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 #include <glib-object.h>
31 #include <communicator.h>
32 #include <core_object.h>
34 #include <user_request.h>
39 #include "generated-code.h"
42 static gboolean on_sap_connect(TelephonySap *sap, GDBusMethodInvocation *invocation,
43 gint arg_req_max_size, gpointer user_data)
45 struct custom_data *ctx = user_data;
46 UserRequest *ur = NULL;
48 struct treq_sap_req_connect req_conn;
50 ur = MAKE_UR(ctx, sap, invocation);
51 memset(&req_conn, 0, sizeof(struct treq_sap_req_connect));
53 req_conn.max_msg_size = (unsigned short)arg_req_max_size;
55 tcore_user_request_set_data(ur, sizeof(struct treq_sap_req_connect), &req_conn);
56 tcore_user_request_set_command(ur, TREQ_SAP_REQ_CONNECT);
57 tcore_communicator_dispatch_request(ctx->comm, ur);
62 static gboolean on_sap_disconnect(TelephonySap *sap, GDBusMethodInvocation *invocation, gpointer user_data)
64 struct custom_data *ctx = user_data;
65 UserRequest *ur = NULL;
67 struct treq_sap_req_disconnect req_disconn;
69 ur = MAKE_UR(ctx, sap, invocation);
70 memset(&req_disconn, 0, sizeof(struct treq_sap_req_disconnect));
72 tcore_user_request_set_data(ur, sizeof(struct treq_sap_req_disconnect), &req_disconn);
73 tcore_user_request_set_command(ur, TREQ_SAP_REQ_DISCONNECT);
74 tcore_communicator_dispatch_request(ctx->comm, ur);
79 static gboolean on_sap_get_status(TelephonySap *sap, GDBusMethodInvocation *invocation, gpointer user_data)
81 struct custom_data *ctx = user_data;
82 UserRequest *ur = NULL;
84 struct treq_sap_req_status req_status;
86 ur = MAKE_UR(ctx, sap, invocation);
87 memset(&req_status, 0, sizeof(struct treq_sap_req_status));
89 tcore_user_request_set_data(ur, sizeof(struct treq_sap_req_status), &req_status);
90 tcore_user_request_set_command(ur, TREQ_SAP_REQ_STATUS);
91 tcore_communicator_dispatch_request(ctx->comm, ur);
96 static gboolean on_sap_get_atr(TelephonySap *sap, GDBusMethodInvocation *invocation, gpointer user_data)
98 struct custom_data *ctx = user_data;
99 UserRequest *ur = NULL;
101 struct treq_sap_req_atr req_atr;
103 ur = MAKE_UR(ctx, sap, invocation);
104 memset(&req_atr, 0, sizeof(struct treq_sap_req_atr));
106 tcore_user_request_set_data(ur, sizeof(struct treq_sap_req_atr), &req_atr);
107 tcore_user_request_set_command(ur, TREQ_SAP_REQ_ATR);
108 tcore_communicator_dispatch_request(ctx->comm, ur);
113 static gboolean on_sap_transfer_apdu(TelephonySap *sap, GDBusMethodInvocation *invocation,
114 GVariant *arg_req_apdu, gpointer user_data)
116 struct custom_data *ctx = user_data;
117 UserRequest *ur = NULL;
118 struct treq_sap_transfer_apdu t_apdu;
120 GVariantIter *iter = NULL;
121 GVariant *inner_gv = NULL;
125 dbg("Func Entrance");
126 memset(&t_apdu, 0, sizeof(struct treq_sap_transfer_apdu));
128 inner_gv = g_variant_get_variant(arg_req_apdu);
130 g_variant_get(inner_gv, "ay", &iter);
131 while ( g_variant_iter_loop (iter, "y", &rt_i)) {
132 t_apdu.apdu_data[i] = rt_i;
135 t_apdu.apdu_length = (unsigned int)i;
136 g_variant_iter_free(iter);
137 g_variant_unref(inner_gv);
138 g_variant_unref(arg_req_apdu);
140 for(i=0; i < (int)t_apdu.apdu_length; i++)
141 dbg("apdu[%d][0x%02x]",i, t_apdu.apdu_data[i]);
143 ur = MAKE_UR(ctx, sap, invocation);
144 tcore_user_request_set_data(ur, sizeof(struct treq_sap_transfer_apdu), &t_apdu);
145 tcore_user_request_set_command(ur, TREQ_SAP_TRANSFER_APDU);
146 tcore_communicator_dispatch_request(ctx->comm, ur);
151 static gboolean on_sap_set_protocol(TelephonySap *sap, GDBusMethodInvocation *invocation,
152 gint arg_protocol, gpointer user_data)
154 struct custom_data *ctx = user_data;
155 UserRequest *ur = NULL;
157 struct treq_sap_set_protocol set_protocol;
159 ur = MAKE_UR(ctx, sap, invocation);
160 memset(&set_protocol, 0, sizeof(struct treq_sap_set_protocol));
162 set_protocol.protocol = arg_protocol;
164 tcore_user_request_set_data(ur, sizeof(struct treq_sap_set_protocol), &set_protocol);
165 tcore_user_request_set_command(ur, TREQ_SAP_SET_PROTOCOL);
166 tcore_communicator_dispatch_request(ctx->comm, ur);
171 static gboolean on_sap_set_power(TelephonySap *sap, GDBusMethodInvocation *invocation,
172 gint arg_mode, gpointer user_data)
174 struct custom_data *ctx = user_data;
175 UserRequest *ur = NULL;
177 struct treq_sap_set_power set_power;
179 ur = MAKE_UR(ctx, sap, invocation);
180 memset(&set_power, 0, sizeof(struct treq_sap_set_power));
182 set_power.mode = arg_mode;
184 tcore_user_request_set_data(ur, sizeof(struct treq_sap_set_power), &set_power);
185 tcore_user_request_set_command(ur, TREQ_SAP_SET_POWER);
186 tcore_communicator_dispatch_request(ctx->comm, ur);
191 static gboolean on_sap_get_card_reader_status(TelephonySap *sap, GDBusMethodInvocation *invocation, gpointer user_data)
193 struct custom_data *ctx = user_data;
194 UserRequest *ur = NULL;
196 struct treq_sap_req_cardreaderstatus req_reader;
198 ur = MAKE_UR(ctx, sap, invocation);
199 memset(&req_reader, 0, sizeof(struct treq_sap_req_cardreaderstatus));
201 tcore_user_request_set_data(ur, sizeof(struct treq_sap_req_cardreaderstatus), &req_reader);
202 tcore_user_request_set_command(ur, TREQ_SAP_REQ_CARDREADERSTATUS);
203 tcore_communicator_dispatch_request(ctx->comm, ur);
208 gboolean dbus_plugin_setup_sap_interface(TelephonyObjectSkeleton *object, struct custom_data *ctx)
212 sap = telephony_sap_skeleton_new();
213 telephony_object_skeleton_set_sap(object, sap);
216 dbg("sap = %p", sap);
218 g_signal_connect (sap,
220 G_CALLBACK (on_sap_connect),
223 g_signal_connect (sap,
225 G_CALLBACK (on_sap_disconnect),
228 g_signal_connect (sap,
230 G_CALLBACK (on_sap_get_status),
233 g_signal_connect (sap,
235 G_CALLBACK (on_sap_get_atr),
238 g_signal_connect (sap,
239 "handle-transfer-apdu",
240 G_CALLBACK (on_sap_transfer_apdu),
243 g_signal_connect (sap,
244 "handle-set-protocol",
245 G_CALLBACK (on_sap_set_protocol),
248 g_signal_connect (sap,
250 G_CALLBACK (on_sap_set_power),
253 g_signal_connect (sap,
254 "handle-get-card-reader-status",
255 G_CALLBACK (on_sap_get_card_reader_status),
261 gboolean dbus_plugin_sap_response(struct custom_data *ctx, UserRequest *ur,
262 struct dbus_request_info *dbus_info, enum tcore_response_command command,
263 unsigned int data_len, const void *data)
265 const struct tresp_sap_req_connect *sap_conn = data;
266 const struct tresp_sap_req_disconnect *sap_disconn = data;
267 const struct tresp_sap_req_status *sap_status = data;
268 const struct tresp_sap_req_atr *sap_atr = data;
269 const struct tresp_sap_transfer_apdu *sap_apdu = data;
270 const struct tresp_sap_set_protocol *sap_protocol = data;
271 const struct tresp_sap_set_power *sap_power = data;
272 const struct tresp_sap_req_cardreaderstatus *sap_reader = data;
274 dbg("application Command = [0x%x], data_len = %d",command, data_len);
277 case TRESP_SAP_REQ_CONNECT:
278 dbg("dbus comm - TRESP_SAP_REQ_CONNECT");
279 telephony_sap_complete_connect(dbus_info->interface_object, dbus_info->invocation,
280 sap_conn->status, sap_conn->max_msg_size);
283 case TRESP_SAP_REQ_DISCONNECT:
284 dbg("dbus comm - TRESP_SAP_REQ_DISCONNECT");
285 telephony_sap_complete_disconnect(dbus_info->interface_object, dbus_info->invocation,
286 sap_disconn->result);
289 case TRESP_SAP_REQ_STATUS:
290 dbg("dbus comm - TRESP_SAP_REQ_STATUS");
291 telephony_sap_complete_get_status(dbus_info->interface_object, dbus_info->invocation,
295 case TRESP_SAP_REQ_ATR: {
296 GVariantBuilder *builder = NULL;
297 GVariant * atr_gv = NULL;
298 GVariant *inner_gv = NULL;
301 dbg("dbus comm - TRESP_SAP_REQ_ATR");
302 builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
303 for(i = 0; i < (int)sap_atr->atr_length; i++) {
304 dbg("sap_atr->atr[%d][0x%02x]", i,sap_atr->atr[i]);
305 g_variant_builder_add (builder, "y", sap_atr->atr[i]);
307 inner_gv = g_variant_builder_end(builder);
308 /* g_variant_builder_unref (builder);*/
309 atr_gv = g_variant_new("v", inner_gv);
311 telephony_sap_complete_get_atr(dbus_info->interface_object, dbus_info->invocation,
312 sap_atr->result, atr_gv);
316 case TRESP_SAP_TRANSFER_APDU: {
317 GVariantBuilder *builder = NULL;
318 GVariant * apdu_gv = NULL;
319 GVariant *inner_gv = NULL;
322 dbg("dbus comm - TRESP_SAP_TRANSFER_APDU");
323 builder = g_variant_builder_new (G_VARIANT_TYPE ("ay"));
324 for(i = 0; i < (int)sap_apdu->resp_apdu_length; i++) {
325 dbg("sap_apdu->resp_adpdu[%d][0x%02x]", i,sap_apdu->resp_adpdu[i]);
326 g_variant_builder_add (builder, "y", sap_apdu->resp_adpdu[i]);
328 inner_gv = g_variant_builder_end(builder);
329 /* g_variant_builder_unref (builder);*/
330 apdu_gv = g_variant_new("v", inner_gv);
332 telephony_sap_complete_transfer_apdu(dbus_info->interface_object, dbus_info->invocation,
333 sap_apdu->result, apdu_gv);
337 case TRESP_SAP_SET_PROTOCOL:
338 dbg("dbus comm - TRESP_SAP_SET_PROTOCOL");
339 telephony_sap_complete_set_protocol(dbus_info->interface_object, dbus_info->invocation,
340 sap_protocol->result);
343 case TRESP_SAP_SET_POWER:
344 dbg("dbus comm - TRESP_SAP_SET_POWER");
345 telephony_sap_complete_set_power(dbus_info->interface_object, dbus_info->invocation,
349 case TRESP_SAP_REQ_CARDREADERSTATUS:
350 dbg("dbus comm - TRESP_SAP_REQ_CARDREADERSTATUS");
351 telephony_sap_complete_get_card_reader_status(dbus_info->interface_object, dbus_info->invocation,
352 sap_reader->result, sap_reader->reader_status);
356 dbg("not handled command[%d]", command);
363 gboolean dbus_plugin_sap_notification(struct custom_data *ctx, const char *plugin_name,
364 TelephonyObjectSkeleton *object, enum tcore_notification_command command,
365 unsigned int data_len, const void *data)
368 const struct tnoti_sap_status_changed *n_sap_status = data;
369 const struct tnoti_sap_disconnect *n_sap_disconn = data;
372 dbg("object is NULL");
376 sap = telephony_object_peek_sap(TELEPHONY_OBJECT(object));
377 dbg("sap = %p", sap);
379 dbg("notification !!! (command = 0x%x, data_len = %d)", command, data_len);
382 case TNOTI_SAP_STATUS:
383 dbg("notified sap_status[%d]", n_sap_status->status);
384 telephony_sap_emit_status(sap, n_sap_status->status);
386 case TNOTI_SAP_DISCONNECT:
387 dbg("notified sap_disconnect type[%d]", n_sap_disconn->type);
388 telephony_sap_emit_disconnect(sap, n_sap_disconn->type);
391 dbg("not handled command[%d]", command);