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