tizen 2.3.1 release
[framework/telephony/libtcore.git] / src / co_ss.c
1 /*
2  * libtcore
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 #include <string.h>
24
25 #include <glib.h>
26
27 #include "tcore.h"
28 #include "plugin.h"
29 #include "queue.h"
30 #include "user_request.h"
31 #include "co_ss.h"
32
33 #define _check_null( name, value, err ) { \
34         if ( !value ) { \
35                 dbg("[error] %s : NULL", name ); \
36                 return err; \
37         } \
38 }
39
40 struct ussd_session {
41         gboolean session;
42         enum tcore_ss_ussd_type type;
43         void* data;
44         int data_len;
45 };
46
47 struct private_object_data {
48         struct ussd_session ussd_s;
49         struct tcore_ss_operations *ops;
50 };
51
52 static TReturn _dispatcher(CoreObject *o, UserRequest *ur)
53 {
54         enum tcore_request_command command;
55         TReturn ret = 0;
56
57         struct private_object_data *po = NULL;
58         po = tcore_object_ref_object(o);
59
60         _check_null( "po", po, TCORE_RETURN_FAILURE);
61         _check_null( "po->ops", po->ops, TCORE_RETURN_FAILURE);
62         _check_null( "ur", ur, TCORE_RETURN_FAILURE);
63
64         command = tcore_user_request_get_command(ur);
65         switch (command) {
66                 case TREQ_SS_BARRING_ACTIVATE:
67                         if (!po->ops->barring_activate)
68                                 return TCORE_RETURN_ENOSYS;
69
70                         ret = po->ops->barring_activate(o, ur);
71                         break;
72
73                 case TREQ_SS_BARRING_DEACTIVATE:
74                         if (!po->ops->barring_deactivate)
75                                 return TCORE_RETURN_ENOSYS;
76
77                         ret = po->ops->barring_deactivate(o, ur);
78                         break;
79
80                 case TREQ_SS_BARRING_CHANGE_PASSWORD:
81                         if (!po->ops->barring_change_password)
82                                 return TCORE_RETURN_ENOSYS;
83
84                         ret = po->ops->barring_change_password(o, ur);
85                         break;
86
87                 case TREQ_SS_BARRING_GET_STATUS:
88                         if (!po->ops->barring_get_status)
89                                 return TCORE_RETURN_ENOSYS;
90
91                         ret = po->ops->barring_get_status(o, ur);
92                         break;
93
94                 case TREQ_SS_FORWARDING_ACTIVATE:
95                         if (!po->ops->forwarding_activate)
96                                 return TCORE_RETURN_ENOSYS;
97
98                         ret = po->ops->forwarding_activate(o, ur);
99                         break;
100
101                 case TREQ_SS_FORWARDING_DEACTIVATE:
102                         if (!po->ops->forwarding_deactivate)
103                                 return TCORE_RETURN_ENOSYS;
104
105                         ret = po->ops->forwarding_deactivate(o, ur);
106                         break;
107
108                 case TREQ_SS_FORWARDING_REGISTER:
109                         if (!po->ops->forwarding_register)
110                                 return TCORE_RETURN_ENOSYS;
111
112                         ret = po->ops->forwarding_register(o, ur);
113                         break;
114
115                 case TREQ_SS_FORWARDING_DEREGISTER:
116                         if (!po->ops->forwarding_deregister)
117                                 return TCORE_RETURN_ENOSYS;
118
119                         ret = po->ops->forwarding_deregister(o, ur);
120                         break;
121
122                 case TREQ_SS_FORWARDING_GET_STATUS:
123                         if (!po->ops->forwarding_get_status)
124                                 return TCORE_RETURN_ENOSYS;
125
126                         ret = po->ops->forwarding_get_status(o, ur);
127                         break;
128
129                 case TREQ_SS_WAITING_ACTIVATE:
130                         if (!po->ops->waiting_activate)
131                                 return TCORE_RETURN_ENOSYS;
132
133                         ret = po->ops->waiting_activate(o, ur);
134                         break;
135
136                 case TREQ_SS_WAITING_DEACTIVATE:
137                         if (!po->ops->waiting_deactivate)
138                                 return TCORE_RETURN_ENOSYS;
139
140                         ret = po->ops->waiting_deactivate(o, ur);
141                         break;
142
143                 case TREQ_SS_WAITING_GET_STATUS:
144                         if (!po->ops->waiting_get_status)
145                                 return TCORE_RETURN_ENOSYS;
146
147                         ret = po->ops->waiting_get_status(o, ur);
148                         break;
149
150                 case TREQ_SS_CLI_ACTIVATE:
151                         if (!po->ops->cli_activate)
152                                 return TCORE_RETURN_ENOSYS;
153
154                         ret = po->ops->cli_activate(o, ur);
155                         break;
156
157                 case TREQ_SS_CLI_DEACTIVATE:
158                         if (!po->ops->cli_deactivate)
159                                 return TCORE_RETURN_ENOSYS;
160
161                         ret = po->ops->cli_deactivate(o, ur);
162                         break;
163
164                 case TREQ_SS_CLI_SET_STATUS:
165                         if (!po->ops->cli_set_status)
166                                 return TCORE_RETURN_ENOSYS;
167
168                         ret = po->ops->cli_set_status(o, ur);
169                         break;
170
171                 case TREQ_SS_CLI_GET_STATUS:
172                         if (!po->ops->cli_get_status)
173                                 return TCORE_RETURN_ENOSYS;
174
175                         ret = po->ops->cli_get_status(o, ur);
176                         break;
177
178                 case TREQ_SS_SEND_USSD:
179                         if (!po->ops->send_ussd)
180                                 return TCORE_RETURN_ENOSYS;
181
182                         ret = po->ops->send_ussd(o, ur);
183                         break;
184
185                 case TREQ_SS_SET_AOC:
186                         if (!po->ops->set_aoc)
187                                 return TCORE_RETURN_ENOSYS;
188
189                         ret = po->ops->set_aoc(o, ur);
190                         break;
191
192                 case TREQ_SS_GET_AOC:
193                         if (!po->ops->get_aoc)
194                                 return TCORE_RETURN_ENOSYS;
195
196                         ret = po->ops->get_aoc(o, ur);
197                         break;
198
199                 default:
200                         break;
201         }
202
203         return ret;
204 }
205
206 static void _clone_hook(CoreObject *src, CoreObject *dest)
207 {
208         struct private_object_data *src_po = NULL;
209         struct private_object_data *dest_po = NULL;
210
211         if (!src || !dest)
212                 return;
213
214         dest_po = calloc(1, sizeof(struct private_object_data));
215         if (!dest_po) {
216                 tcore_object_link_object(dest, NULL);
217                 return;
218         }
219
220         src_po = tcore_object_ref_object(src);
221         dest_po->ops = src_po->ops;
222
223         tcore_object_link_object(dest, dest_po);
224 }
225
226 static void _free_hook(CoreObject *o)
227 {
228         struct private_object_data *po = NULL;
229
230         po = tcore_object_ref_object(o);
231         if (!po)
232                 return;
233
234         free(po);
235         tcore_object_link_object(o, NULL);
236 }
237
238 static void _ussd_session_init(struct ussd_session *ussd_s)
239 {
240         ussd_s->session = FALSE;
241         ussd_s->type = 0;
242         ussd_s->data = 0;
243         ussd_s->data_len = 0;
244 }
245
246 struct ussd_session* tcore_ss_ussd_create_session(CoreObject *o,
247                 enum tcore_ss_ussd_type type, void *data, int data_len)
248 {
249         struct private_object_data *po = NULL;
250
251         po = tcore_object_ref_object(o);
252         if (!po)
253                 return 0;
254
255         if (type < TCORE_SS_USSD_TYPE_USER_INITIATED
256                         || type > TCORE_SS_USSD_TYPE_NETWORK_INITIATED) {
257                 dbg("[ error ] wrong ussd type : (0x%x)", type);
258                 return 0;
259         }
260
261         if (!po->ussd_s.session) {
262                 po->ussd_s.session = TRUE;
263                 po->ussd_s.type = type;
264                 po->ussd_s.data = data;
265
266                 if (data_len < 0)
267                         po->ussd_s.data_len = 0;
268                 else
269                         po->ussd_s.data_len = data_len;
270
271                 return &po->ussd_s;
272
273         }
274         else {
275                 dbg("[ error ] already exist ussd session, type : (0x%x)", po->ussd_s.type);
276                 return 0;
277         }
278 }
279
280 void tcore_ss_ussd_destroy_session(struct ussd_session *ussd_s)
281 {
282         if (!ussd_s || !ussd_s->session) {
283                 return;
284         }
285         else {
286
287                 _ussd_session_init(ussd_s);
288         }
289 }
290
291 struct ussd_session* tcore_ss_ussd_get_session(CoreObject *o)
292 {
293         struct private_object_data *po = NULL;
294
295         po = tcore_object_ref_object(o);
296         if (!po)
297                 return 0;
298
299         if (!po->ussd_s.session)
300                 return 0;
301         else
302                 return &po->ussd_s;
303 }
304
305 enum tcore_ss_ussd_type tcore_ss_ussd_get_session_type(struct ussd_session *ussd_s)
306 {
307         if (!ussd_s || !ussd_s->session) {
308                 dbg("[ error ] there is no session");
309                 return 0;
310
311         }
312         else {
313                 return ussd_s->type;
314         }
315 }
316
317 void tcore_ss_ussd_set_session_type(struct ussd_session *ussd_s,
318                 enum tcore_ss_ussd_type type)
319 {
320         if (!ussd_s || !ussd_s->session) {
321                 dbg("[ error ] there is no session");
322                 return;
323
324         }
325         else {
326                 ussd_s->type = type;
327         }
328 }
329
330 int tcore_ss_ussd_get_session_data(struct ussd_session* ussd_s, void **data)
331 {
332         if (!ussd_s || !ussd_s->session) {
333                 dbg("[ error ] there is no session");
334                 return -1;
335
336         }
337         else {
338
339                 *data = ussd_s->data;
340                 return ussd_s->data_len;
341         }
342 }
343
344 void tcore_ss_ussd_set_session_data(struct ussd_session* ussd_s, void *data, int data_len)
345 {
346         if (!ussd_s || !ussd_s->session) {
347                 dbg("[ error ] there is no session");
348                 return ;
349
350         }
351         else {
352
353                 ussd_s->data = data;
354                 ussd_s->data_len = data_len;
355         }
356 }
357
358
359 CoreObject *tcore_ss_new(TcorePlugin *p, const char *name,
360                 struct tcore_ss_operations *ops, TcoreHal *hal)
361 {
362         CoreObject *o = NULL;
363         struct private_object_data *po = NULL;
364
365         if (!p)
366                 return NULL;
367
368         o = tcore_object_new(p, name, hal);
369         if (!o)
370                 return NULL;
371
372         po = calloc(1, sizeof(struct private_object_data));
373         if (!po) {
374                 tcore_object_free(o);
375                 return NULL;
376         }
377
378         po->ops = ops;
379
380         _ussd_session_init(&po->ussd_s);
381
382         tcore_object_set_type(o, CORE_OBJECT_TYPE_SS);
383         tcore_object_link_object(o, po);
384         tcore_object_set_free_hook(o, _free_hook);
385         tcore_object_set_clone_hook(o, _clone_hook);
386         tcore_object_set_dispatcher(o, _dispatcher);
387
388         return o;
389 }
390
391 void tcore_ss_free(CoreObject *o)
392 {
393         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SS);
394
395         tcore_object_free(o);
396 }
397
398 void tcore_ss_set_ops(CoreObject *o, struct tcore_ss_operations *ops)
399 {
400         struct private_object_data *po = NULL;
401
402         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_SS);
403
404         po = (struct private_object_data *)tcore_object_ref_object(o);
405         if (!po) {
406                 return;
407         }
408
409         po->ops = ops;
410 }
411