99601a420b8946d07d03e0447fb0b145a82f9858
[framework/telephony/tel-plugin-socket_communicator.git] / plugin / src / socket_ps.c
1 /*
2  * tel-plugin-socket-communicator
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 <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <glib-object.h>
26
27 #include <tcore.h>
28 #include <server.h>
29 #include <plugin.h>
30 #include <hal.h>
31 #include <communicator.h>
32 #include <core_object.h>
33 #include <queue.h>
34 #include <user_request.h>
35 #include <util.h>
36 #include <co_ps.h>
37
38 #include "sipc.h"
39 #include "tapi_common.h"
40 #include "module_req.h"
41
42 static void _request_fail(UserRequest *ur, int cmd, int resp_len, void *resp)
43 {
44         tcore_user_request_send_response(ur, cmd, resp_len, resp);
45         tcore_user_request_free(ur);
46 }
47
48 gboolean scomm_service_request_ps(unsigned int ch_id, Communicator *c, TcorePlugin *plugin,
49                 tapi_service_command_e cmd, gchar *data, void **outparam)
50 {
51         int ret = 0;
52         struct _sipc_marshal_object *out_obj;
53         struct _sipc_marshal_object *in_obj;
54         gboolean result = TRUE;
55         UserRequest *ur = NULL;
56         struct tcore_user_info ui = { 0, 0, 0, NULL, 0, 0, NULL };
57
58         in_obj = sipc_util_marshal_object_deserializer(data);
59         if (!in_obj) {
60                 dbg("in_obj is NULL");
61                 result = FALSE;
62                 goto RETURN;
63         }
64
65         ur = tcore_user_request_new(c, tcore_plugin_get_description(plugin)->name);
66         if (!ur) {
67                 dbg("ur is NULL");
68                 result = FALSE;
69                 goto RETURN;
70         }
71
72         ui.channel_id = ch_id;
73         ui.client_cmd = cmd;
74         tcore_user_request_set_user_info(ur, &ui);
75
76         switch (cmd) {
77                 case TAPI_SERVICE_PS_SET_DUN_PIN_CONTROL: {
78                         struct treq_ps_set_dun_pin_control data_pincontrol;
79                         struct tresp_ps_set_dun_pin_control resp;
80                         gchar *str_signal = NULL;
81                         gboolean status;
82                         enum telephony_ps_dun_pincontrol_signal in_signal = DUN_PINCONTROL_SIGNAL_DCD;
83
84                         str_signal = sipc_util_marshal_object_get_string(in_obj, "signal");
85                         status = sipc_util_marshal_object_get_boolean(in_obj, "status");
86
87                         if (!str_signal) {
88                                 /* fail */
89                                 resp.result = TCORE_RETURN_EINVAL;
90                                 _request_fail(ur, TRESP_PS_SET_DUN_PIN_CONTROL, sizeof(struct tresp_ps_set_dun_pin_control), &resp);
91                                 result = FALSE;
92                                 break;
93                         }
94                         else {
95                                 dbg("signal = [%s]", str_signal);
96                         }
97
98                         if (!g_strcmp0(str_signal, "dcd")) {
99                                 in_signal = DUN_PINCONTROL_SIGNAL_DCD;
100                         }
101                         else if (!g_strcmp0(str_signal, "dtr")) {
102                                 in_signal = DUN_PINCONTROL_SIGNAL_DTR;
103                         }
104                         else if (!g_strcmp0(str_signal, "dsr")) {
105                                 in_signal = DUN_PINCONTROL_SIGNAL_DSR;
106                         }
107                         else if (!g_strcmp0(str_signal, "rts")) {
108                                 in_signal = DUN_PINCONTROL_SIGNAL_RTS;
109                         }
110                         else if (!g_strcmp0(str_signal, "cts")) {
111                                 in_signal = DUN_PINCONTROL_SIGNAL_CTS;
112                         }
113                         else if (!g_strcmp0(str_signal, "ri")) {
114                                 in_signal = DUN_PINCONTROL_SIGNAL_RI;
115                         }
116                         else {
117                                 /* fail */
118                                 g_free(str_signal);
119                                 resp.result = TCORE_RETURN_EINVAL;
120                                 _request_fail(ur, TRESP_PS_SET_DUN_PIN_CONTROL, sizeof(struct tresp_ps_set_dun_pin_control), &resp);
121                                 result = FALSE;
122                                 break;
123                         }
124
125                         g_free(str_signal);
126
127                         dbg("signal = %d, status = %d", in_signal, status);
128
129                         data_pincontrol.signal = in_signal;
130                         data_pincontrol.status = status;
131
132                         tcore_user_request_set_data(ur, sizeof(struct treq_ps_set_dun_pin_control), &data_pincontrol);
133                         tcore_user_request_set_command(ur, TREQ_PS_SET_DUN_PIN_CONTROL);
134                 }
135                         break;
136         }
137
138 RETURN:
139         sipc_util_marshal_object_destory(in_obj);
140         out_obj = sipc_util_marshal_object_create();
141         sipc_util_marshal_object_add_data(out_obj, "result", &result, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
142
143         dbg("result = %d", result);
144
145         *outparam = sipc_util_marshal_object_serializer(out_obj);
146         sipc_util_marshal_object_destory(out_obj);
147
148         if (result == FALSE) {
149                 return FALSE;
150         }
151
152         ret = tcore_communicator_dispatch_request(c, ur);
153         if (ret != TCORE_RETURN_SUCCESS) {
154                 return FALSE;
155         }
156
157         return TRUE;
158 }
159
160 gboolean scomm_service_response_ps(Communicator *comm, UserRequest *ur, enum tcore_response_command command,
161                 unsigned int data_len, const void *data)
162 {
163         const struct tresp_ps_set_dun_pin_control *resp_dun_pin_control = data;
164         int ret = 0;
165         gchar *out_d = NULL, *serial_d = NULL;
166         struct custom_data *ctx = NULL;
167         const struct tcore_user_info *ui;
168         struct _tapi_header hdr;
169         struct _sipc_marshal_object* out_obj = NULL;
170         struct _sipc_marshal_object* in_obj = NULL;
171
172         ctx = tcore_communicator_ref_user_data(comm);
173         if (!ctx) {
174                 dbg("user_data is NULL");
175                 return FALSE;
176         }
177
178         ui = tcore_user_request_ref_user_info(ur);
179
180         dbg("response command = [0x%x], data_len = %d", command, data_len);
181         dbg("channel_id = %d, client_cmd = %d", ui->channel_id, ui->client_cmd);
182
183         memset(&hdr, 0, sizeof(struct _tapi_header));
184         hdr.cmd = (tapi_service_command_e) ui->client_cmd;
185
186         out_obj = sipc_util_marshal_object_create();
187
188         switch (command) {
189                 case TRESP_PS_SET_DUN_PIN_CONTROL:
190                         dbg("TRESP_PS_SET_DUN_PIN_CONTROL");
191                         sipc_util_marshal_object_add_data(out_obj, "result", (void*) &resp_dun_pin_control->result,
192                                         SIPC_MARSHAL_DATA_INT_TYPE);
193                         break;
194         }
195
196         if (out_obj) {
197                 serial_d = sipc_util_marshal_object_serializer(out_obj);
198                 hdr.data_len = strlen(serial_d);
199                 sipc_util_marshal_object_destory(out_obj);
200         }
201
202         out_d = g_new0(char, sizeof(struct _tapi_header) + hdr.data_len);
203         memcpy(out_d, &hdr, sizeof(struct _tapi_header));
204
205         if (serial_d) {
206                 memcpy(out_d + sizeof(struct _tapi_header), serial_d, hdr.data_len);
207                 dbg("serial_d = [%s]", serial_d);
208                 g_free(serial_d);
209         }
210
211         ret = sipc_server_send(ctx->sk_server, ui->channel_id, out_d, sizeof(struct _tapi_header) + hdr.data_len,
212                         SIPC_SEND_DATA_ASYNC);
213         g_free(out_d);
214
215         if (ret < 0) {
216                 dbg("ret = %d", ret);
217                 return FALSE;
218         }
219
220         return TRUE;
221 }
222
223 gboolean scomm_service_notification_ps(Communicator *comm, CoreObject *source, enum tcore_notification_command command,
224                 unsigned int data_len, const void *data)
225 {
226         const struct tnoti_ps_dun_pin_control *noti_pin_info = data;
227
228         int ret = 0;
229         struct custom_data *ctx = NULL;
230         struct _tapi_header hdr;
231         struct _sipc_marshal_object* out_obj = NULL;
232         gchar *out_d = NULL, *serial_d = NULL;
233
234         ctx = tcore_communicator_ref_user_data(comm);
235         if (!ctx) {
236                 dbg("user_data is NULL");
237                 return FALSE;
238         }
239
240         dbg("notification !!! (command = 0x%x, data_len = %d)", command, data_len);
241
242         switch (command) {
243                 case TNOTI_PS_DUN_PIN_CONTROL: {
244                         gchar *str_signal = NULL;
245                         gint status;
246
247                         switch (noti_pin_info->signal) {
248                                 case DUN_PINCONTROL_SIGNAL_DCD:
249                                         str_signal = strdup("dcd");
250                                         break;
251
252                                 case DUN_PINCONTROL_SIGNAL_DTR:
253                                         str_signal = strdup("dtr");
254                                         break;
255
256                                 case DUN_PINCONTROL_SIGNAL_DSR:
257                                         str_signal = strdup("dsr");
258                                         break;
259
260                                 case DUN_PINCONTROL_SIGNAL_RTS:
261                                         str_signal = strdup("rts");
262                                         break;
263
264                                 case DUN_PINCONTROL_SIGNAL_CTS:
265                                         str_signal = strdup("cts");
266                                         break;
267
268                                 case DUN_PINCONTROL_SIGNAL_RI:
269                                         str_signal = strdup("ri");
270                                         break;
271                         }
272                         status = noti_pin_info->status;
273
274                         hdr.cmd = TAPI_NOTI_PS_DUN_PIN_CONTROL;
275
276                         dbg("signal = [%s], status = %d", str_signal, status);
277
278                         out_obj = sipc_util_marshal_object_create();
279                         sipc_util_marshal_object_add_data(out_obj, "signal", str_signal, SIPC_MARSHAL_DATA_STRING_TYPE);
280                         sipc_util_marshal_object_add_data(out_obj, "status", &status, SIPC_MARSHAL_DATA_BOOLEAN_TYPE);
281                 }
282                         break;
283
284                 case TNOTI_PS_EXTERNAL_CALL:
285                         hdr.cmd = TAPI_NOTI_PS_EXTERNAL_CALL;
286                         /* no data */
287                         break;
288         }
289
290         if (out_obj) {
291                 serial_d = sipc_util_marshal_object_serializer(out_obj);
292                 hdr.data_len = strlen(serial_d);
293                 sipc_util_marshal_object_destory(out_obj);
294         }
295
296         out_d = g_new0(char, sizeof(struct _tapi_header)+hdr.data_len);
297         memcpy(out_d, &hdr, sizeof(struct _tapi_header));
298         if (serial_d) {
299                 memcpy(out_d + sizeof(struct _tapi_header), serial_d, hdr.data_len);
300                 g_free(serial_d);
301         }
302
303         dbg("len = %d, out_d = [%s]", sizeof(struct _tapi_header) + hdr.data_len, out_d);
304
305         ret = sipc_server_broadcast(ctx->sk_server, out_d, sizeof(struct _tapi_header) + hdr.data_len);
306         dbg("ret = %d", ret);
307         g_free(out_d);
308
309         return TRUE;
310 }