tizen 2.3.1 release
[framework/telephony/tel-plugin-dbus_tapi.git] / src / oem.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <pthread.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <time.h>
7 #include <glib.h>
8 #include <gio/gio.h>
9
10 #include <tcore.h>
11 #include <server.h>
12 #include <plugin.h>
13 #include <hal.h>
14 #include <communicator.h>
15 #include <queue.h>
16 #include <user_request.h>
17
18 #include "generated-code.h"
19 #include "common.h"
20
21 #define TYPE_FACTORY            0x00020000
22 #define MAKE_REQ_CMD(id)        (TREQ_CUSTOM|TYPE_FACTORY|id)
23 #define GET_OEM_ID(cmd) (cmd&0x0000FFFF)
24
25 static void _emit_oem_response(struct dbus_request_info *dbus_info, int oem_id, const void *data, unsigned int data_len)
26 {
27         if (!dbus_info || !oem_id || !data || !data_len) {
28                 dbg("Invalid Data! dbus_info=%p, oem_id=0x%x, data=%p, data_len=%d", dbus_info, oem_id, data, data_len);
29                 return ;
30         }
31
32         if (dbus_info->interface_object) {
33                 gchar *encoded_data = g_base64_encode((const guchar*)data, data_len);
34                 if (dbus_info->invocation) {
35                         telephony_oem_complete_send_oem_data_with_response(dbus_info->interface_object, dbus_info->invocation, oem_id, encoded_data);
36                 } else {
37                         telephony_oem_emit_oem_data(dbus_info->interface_object, oem_id, encoded_data);
38                 }
39                 g_free(encoded_data);
40         }
41 }
42
43 static void _emit_oem_notification(TelephonyOEM *oem, int oem_id, const void *data, unsigned int data_len)
44 {
45         gchar *encoded_data = NULL;
46
47         if (!oem || !oem_id || !data || !data_len) {
48                 dbg("Invalid Data! oem=%p, oem_id=0x%x, data=%p, data_len=%d", oem, oem_id, data, data_len);
49                 return ;
50         }
51
52         encoded_data = g_base64_encode((const guchar*)data, data_len);
53         telephony_oem_emit_oem_data(oem, oem_id, encoded_data);
54         g_free(encoded_data);
55 }
56
57 static gboolean
58 send_oem_data(TelephonyOEM *oem,
59         GDBusMethodInvocation *invocation,
60         gint arg_oem_id,
61         const gchar *arg_data,
62         gpointer user_data,
63         gboolean remove_invocation)
64 {
65         struct custom_data *ctx = user_data;
66         UserRequest *ur = NULL;
67         TReturn ret;
68         gint result = 1;
69         guchar *decoded_data = NULL;
70         gsize length;
71
72         if (!check_access_control (invocation, AC_MODEM, "w"))
73                 return TRUE;
74
75         ur = MAKE_UR(ctx, oem, invocation);
76         decoded_data = g_base64_decode(arg_data, &length);
77
78         tcore_user_request_set_data(ur, length, decoded_data);
79         g_free(decoded_data);
80
81         tcore_user_request_set_command(ur, MAKE_REQ_CMD(arg_oem_id));
82
83         ret = tcore_communicator_dispatch_request(ctx->comm, ur);
84         if (ret != TCORE_RETURN_SUCCESS) {
85                 FAIL_RESPONSE (invocation, DEFAULT_MSG_REQ_FAILED);
86                 tcore_user_request_unref(ur);
87                 return TRUE;
88         }
89
90         if (remove_invocation) {
91                 struct dbus_request_info *dbus_info = tcore_user_request_ref_user_info(ur);
92                 if (dbus_info)
93                         dbus_info->invocation = NULL;
94
95                 telephony_oem_complete_send_oem_data(oem, invocation, result);
96         }
97         return TRUE;
98 }
99
100 static gboolean
101 on_send_oem_data (TelephonyOEM *oem,
102         GDBusMethodInvocation *invocation,
103         gint arg_oem_id,
104         const gchar *arg_data,
105         gpointer user_data)
106 {
107         return send_oem_data(oem, invocation, arg_oem_id, arg_data, user_data, TRUE);
108 }
109
110 static gboolean
111 on_send_oem_data_with_response (TelephonyOEM *oem,
112         GDBusMethodInvocation *invocation,
113         gint arg_oem_id,
114         const gchar *arg_data,
115         gpointer user_data)
116 {
117         return send_oem_data(oem, invocation, arg_oem_id, arg_data, user_data, FALSE);
118 }
119
120 gboolean dbus_plugin_setup_oem_interface(TelephonyObjectSkeleton *object, struct custom_data *ctx)
121 {
122         TelephonyOEM *oem;
123
124         oem = telephony_oem_skeleton_new();
125         telephony_object_skeleton_set_oem(object, oem);
126
127         g_object_unref(oem);
128
129         dbg("oem = %p", oem);
130
131         g_signal_connect (oem,
132                         "handle-send-oem-data",
133                         G_CALLBACK (on_send_oem_data),
134                         ctx);
135
136         g_signal_connect (oem,
137                         "handle-send-oem-data-with-response",
138                         G_CALLBACK (on_send_oem_data_with_response),
139                         ctx);
140
141         return TRUE;
142 }
143
144 gboolean dbus_plugin_oem_response(struct custom_data *ctx, UserRequest *ur, struct dbus_request_info *dbus_info, enum tcore_response_command command, unsigned int data_len, const void *data)
145 {
146         _emit_oem_response(dbus_info, GET_OEM_ID(command), data, data_len);
147         return TRUE;
148 }
149
150 gboolean dbus_plugin_oem_notification(struct custom_data *ctx, CoreObject *source, TelephonyObjectSkeleton *object, enum tcore_notification_command command, unsigned int data_len, const void *data)
151 {
152         _emit_oem_notification(telephony_object_peek_oem(TELEPHONY_OBJECT(object)), GET_OEM_ID(command), data, data_len);
153         return TRUE;
154 }
155