tizen 2.3.1 release
[framework/telephony/libtcore.git] / src / co_modem.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 "user_request.h"
30 #include "co_modem.h"
31 #include "hal.h"
32
33 struct private_object_data {
34         struct tcore_modem_operations *ops;
35
36         gboolean flight_mode;
37         gboolean powered;
38 };
39
40 static void _free_hook(CoreObject *o)
41 {
42         struct private_object_data *po = NULL;
43
44         po = tcore_object_ref_object(o);
45         if (po) {
46                 free(po);
47                 tcore_object_link_object(o, NULL);
48         }
49 }
50
51 static void _clone_hook(CoreObject *src, CoreObject *dest)
52 {
53         struct private_object_data *src_po = NULL;
54         struct private_object_data *dest_po = NULL;
55
56         if (!src || !dest)
57                 return;
58
59         dest_po = calloc(1, sizeof(struct private_object_data));
60         if (!dest_po) {
61                 tcore_object_link_object(dest, NULL);
62                 return;
63         }
64
65         src_po = tcore_object_ref_object(src);
66         dest_po->ops = src_po->ops;
67
68         tcore_object_link_object(dest, dest_po);
69 }
70
71 static TReturn _dispatcher(CoreObject *o, UserRequest *ur)
72 {
73         enum tcore_request_command command;
74         struct private_object_data *po = NULL;
75
76         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_MODEM, TCORE_RETURN_EINVAL);
77
78         po = tcore_object_ref_object(o);
79         if (!po || !po->ops)
80                 return TCORE_RETURN_ENOSYS;
81
82         command = tcore_user_request_get_command(ur);
83         switch (command) {
84                 case TREQ_MODEM_POWER_ON:
85                         if (!po->ops->power_on)
86                                 return TCORE_RETURN_ENOSYS;
87
88                         return po->ops->power_on(o, ur);
89                         break;
90
91                 case TREQ_MODEM_POWER_OFF:
92                         if (!po->ops->power_off)
93                                 return TCORE_RETURN_ENOSYS;
94
95                         return po->ops->power_off(o, ur);
96                         break;
97
98                 case TREQ_MODEM_POWER_RESET:
99                         if (!po->ops->power_reset)
100                                 return TCORE_RETURN_ENOSYS;
101
102                         return po->ops->power_reset(o, ur);
103                         break;
104
105                 case TREQ_MODEM_POWER_LOW:
106                         if (!po->ops->power_low)
107                                 return TCORE_RETURN_ENOSYS;
108
109                         return po->ops->power_low(o, ur);
110                         break;
111
112                 case TREQ_MODEM_SET_FLIGHTMODE:
113                         if (!po->ops->set_flight_mode)
114                                 return TCORE_RETURN_ENOSYS;
115
116                         return po->ops->set_flight_mode(o, ur);
117                         break;
118
119                 case TREQ_MODEM_GET_IMEI:
120                         if (!po->ops->get_imei)
121                                 return TCORE_RETURN_ENOSYS;
122
123                         return po->ops->get_imei(o, ur);
124                         break;
125
126                 case TREQ_MODEM_GET_VERSION:
127                         if (!po->ops->get_version)
128                                 return TCORE_RETURN_ENOSYS;
129
130                         return po->ops->get_version(o, ur);
131                         break;
132
133                 case TREQ_MODEM_GET_SN:
134                         if (!po->ops->get_sn)
135                                 return TCORE_RETURN_ENOSYS;
136
137                         return po->ops->get_sn(o, ur);
138                         break;
139
140                 case TREQ_MODEM_SET_DUN_PIN_CONTROL:
141                         if (!po->ops->dun_pin_ctrl)
142                                 return TCORE_RETURN_ENOSYS;
143
144                         return po->ops->dun_pin_ctrl(o, ur);
145                         break;
146
147                 case TREQ_MODEM_GET_FLIGHTMODE:
148                         if (!po->ops->get_flight_mode)
149                                 return TCORE_RETURN_ENOSYS;
150
151                         return po->ops->get_flight_mode(o, ur);
152                         break;
153
154                 default:
155                         return TCORE_RETURN_EINVAL;
156         }
157
158         return TCORE_RETURN_SUCCESS;
159 }
160
161 CoreObject *tcore_modem_new(TcorePlugin *p, const char *name,
162                 struct tcore_modem_operations *ops, TcoreHal *hal)
163 {
164         CoreObject *o = NULL;
165         struct private_object_data *po = NULL;
166
167         if (!p)
168                 return NULL;
169
170         o = tcore_object_new(p, name, hal);
171         if (!o)
172                 return NULL;
173
174         po = calloc(1, sizeof(struct private_object_data));
175         if (!po) {
176                 tcore_object_free(o);
177                 return NULL;
178         }
179
180         po->ops = ops;
181
182         tcore_object_set_type(o, CORE_OBJECT_TYPE_MODEM);
183         tcore_object_link_object(o, po);
184         tcore_object_set_free_hook(o, _free_hook);
185         tcore_object_set_clone_hook(o, _clone_hook);
186         tcore_object_set_dispatcher(o, _dispatcher);
187
188         return o;
189 }
190
191 void tcore_modem_free(CoreObject *o)
192 {
193         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_MODEM);
194         tcore_object_free(o);
195 }
196
197 void tcore_modem_set_ops(CoreObject *o, struct tcore_modem_operations *ops)
198 {
199         struct private_object_data *po = NULL;
200
201         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_MODEM);
202
203         po = (struct private_object_data *)tcore_object_ref_object(o);
204         if (!po) {
205                 return;
206         }
207
208         po->ops = ops;
209 }
210
211 TReturn tcore_modem_set_flight_mode_state(CoreObject *o, gboolean flag)
212 {
213         struct private_object_data *po = NULL;
214
215         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_MODEM, TCORE_RETURN_EINVAL);
216
217         po = tcore_object_ref_object(o);
218         if (!po)
219                 return TCORE_RETURN_EINVAL;
220
221         po->flight_mode = flag;
222
223         return TCORE_RETURN_SUCCESS;
224 }
225
226 gboolean tcore_modem_get_flight_mode_state(CoreObject *o)
227 {
228         struct private_object_data *po = NULL;
229
230         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_MODEM, FALSE);
231
232         po = tcore_object_ref_object(o);
233         if (!po)
234                 return FALSE;
235
236         return po->flight_mode;
237 }
238
239 TReturn tcore_modem_set_powered(CoreObject *o, gboolean pwr)
240 {
241         TcoreHal *h;
242
243         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_MODEM, TCORE_RETURN_EINVAL);
244
245         h = tcore_object_get_hal(o);
246         if (!h)
247                 return TCORE_RETURN_FAILURE;
248
249         tcore_hal_set_power_state(h, pwr);
250
251         return TCORE_RETURN_SUCCESS;
252 }
253
254 gboolean tcore_modem_get_powered(CoreObject *o)
255 {
256         TcoreHal *h;
257
258         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_MODEM, FALSE);
259
260         h = tcore_object_get_hal(o);
261         if (!h)
262                 return FALSE;
263
264         return tcore_hal_get_power_state(h);
265 }