Add SAT and SMS fixes
[platform/core/telephony/tel-plugin-dbus_tapi.git] / src / dtapi_oem.c
1 /*
2  * tel-plugin-dbus-tapi
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include <glib.h>
25
26 #include <tcore.h>
27 #include <communicator.h>
28 #include <user_request.h>
29
30 #include "generated-code.h"
31 #include "dtapi_common.h"
32
33 #define TYPE_FACTORY            (0x00020000)
34 #define MAKE_REQ_CMD(id)        (TREQ_CUSTOM | TYPE_FACTORY | id)
35 #define GET_OEM_ID(cmd) (cmd & 0x0000FFFF)
36
37 static void _emit_oem_response(struct dbus_request_info *dbus_info,
38         int oem_id, const void *data, unsigned int data_len)
39 {
40         if (!dbus_info || !oem_id || !data || !data_len) {
41                 dbg("Invalid Data! dbus_info=%p, oem_id=0x%x, data=%p, data_len=%d",
42                         dbus_info, oem_id, data, data_len);
43                 return;
44         }
45
46         if (dbus_info->interface_object) {
47                 gchar *encoded_data = g_base64_encode((const guchar*)data, data_len);
48
49                 /*
50                  * Send Response if invocation is non-null,
51                  * else Send Notification.
52                  */
53                 dbg("oem_id=0x%x, data=%p, data_len=%d", oem_id, data, data_len);
54                 if (dbus_info->invocation) {
55                         telephony_oem_complete_send_oem_data_with_response(dbus_info->interface_object,
56                                 dbus_info->invocation, oem_id, encoded_data);
57                 } else {
58                         telephony_oem_emit_oem_data(dbus_info->interface_object, oem_id, encoded_data);
59                 }
60
61                 g_free(encoded_data);
62         }
63 }
64
65 static void _emit_oem_notification(TelephonyOEM *oem,
66         int oem_id, const void *data, unsigned int data_len)
67 {
68         gchar *encoded_data = NULL;
69
70         if (!oem || !oem_id || !data || !data_len) {
71                 dbg("Invalid Data! oem_id=0x%x, data=%p, data_len=%d",
72                         oem_id, data, data_len);
73                 return;
74         }
75
76         dbg("oem_id=0x%x, data=%p, data_len=%d", oem_id, data, data_len);
77         encoded_data = g_base64_encode((const guchar*)data, data_len);
78         telephony_oem_emit_oem_data(oem, oem_id, encoded_data);
79         g_free(encoded_data);
80 }
81
82 static gboolean send_oem_data(TelephonyOEM *oem,
83         GDBusMethodInvocation *invocation,
84         gint arg_oem_id,
85         const gchar *arg_data,
86         gpointer user_data,
87         gboolean remove_invocation)
88 {
89         struct custom_data *ctx = user_data;
90         UserRequest *ur = NULL;
91         TReturn ret;
92         gint result = 1;
93         guchar *decoded_data = NULL;
94         gsize length;
95
96         ur = MAKE_UR(ctx, oem, invocation);
97
98         decoded_data = g_base64_decode(arg_data, &length);
99         tcore_user_request_set_data(ur, length, decoded_data);
100         g_free(decoded_data);
101
102         tcore_user_request_set_command(ur, MAKE_REQ_CMD(arg_oem_id));
103
104         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
105         if (ret != TCORE_RETURN_SUCCESS) {
106                 FAIL_RESPONSE(invocation, DEFAULT_MSG_REQ_FAILED);
107                 tcore_user_request_unref(ur);
108                 return TRUE;
109         }
110
111         if (remove_invocation) {
112                 struct dbus_request_info *dbus_info = tcore_user_request_ref_user_info(ur);
113                 if (dbus_info)
114                         dbus_info->invocation = NULL;
115
116                 telephony_oem_complete_send_oem_data(oem, invocation, result);
117         }
118
119         return TRUE;
120 }
121
122 static gboolean on_send_oem_data(TelephonyOEM *oem,
123         GDBusMethodInvocation *invocation,
124         gint arg_oem_id,
125         const gchar *arg_data,
126         gpointer user_data)
127 {
128         return send_oem_data(oem, invocation, arg_oem_id, arg_data, user_data, TRUE);
129 }
130
131 static gboolean on_send_oem_data_with_response(TelephonyOEM *oem,
132         GDBusMethodInvocation *invocation,
133         gint arg_oem_id,
134         const gchar *arg_data,
135         gpointer user_data)
136 {
137         return send_oem_data(oem, invocation, arg_oem_id, arg_data, user_data, FALSE);
138 }
139
140 gboolean dbus_plugin_setup_oem_interface(TelephonyObjectSkeleton *object,
141         struct custom_data *ctx)
142 {
143         TelephonyOEM *oem;
144
145         oem = telephony_oem_skeleton_new();
146         telephony_object_skeleton_set_oem(object, oem);
147
148         g_object_unref(oem);
149
150         dbg("oem = %p", oem);
151
152         /*
153          * Register signal handlers for OEM interface
154          */
155         g_signal_connect(oem,
156                 "handle-send-oem-data",
157                 G_CALLBACK(on_send_oem_data), ctx);
158
159         g_signal_connect(oem,
160                 "handle-send-oem-data-with-response",
161                 G_CALLBACK(on_send_oem_data_with_response), ctx);
162
163         return TRUE;
164 }
165
166 gboolean dbus_plugin_oem_response(struct custom_data *ctx,
167         UserRequest *ur, struct dbus_request_info *dbus_info,
168         enum tcore_response_command command, unsigned int data_len, const void *data)
169 {
170         _emit_oem_response(dbus_info, GET_OEM_ID(command), data, data_len);
171
172         return TRUE;
173 }
174
175 gboolean dbus_plugin_oem_notification(struct custom_data *ctx,
176         CoreObject *source, TelephonyObjectSkeleton *object,
177         enum tcore_notification_command command, unsigned int data_len, const void *data)
178 {
179         _emit_oem_notification(telephony_object_peek_oem(TELEPHONY_OBJECT(object)),
180                 GET_OEM_ID(command), data, data_len);
181
182         return TRUE;
183 }
184