88960f36548125118b5af3b4767183f054dcf582
[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(sizeof(struct private_object_data), 1);
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_SET_FLIGHTMODE:
106                         if (!po->ops->set_flight_mode)
107                                 return TCORE_RETURN_ENOSYS;
108
109                         return po->ops->set_flight_mode(o, ur);
110                         break;
111
112                 case TREQ_MODEM_GET_IMEI:
113                         if (!po->ops->get_imei)
114                                 return TCORE_RETURN_ENOSYS;
115
116                         return po->ops->get_imei(o, ur);
117                         break;
118
119                 case TREQ_MODEM_GET_VERSION:
120                         if (!po->ops->get_version)
121                                 return TCORE_RETURN_ENOSYS;
122
123                         return po->ops->get_version(o, ur);
124                         break;
125
126                 case TREQ_MODEM_GET_SN:
127                         if (!po->ops->get_sn)
128                                 return TCORE_RETURN_ENOSYS;
129
130                         return po->ops->get_sn(o, ur);
131                         break;
132
133                 case TREQ_MODEM_SET_DUN_PIN_CONTROL:
134                         if (!po->ops->dun_pin_ctrl)
135                                 return TCORE_RETURN_ENOSYS;
136
137                         return po->ops->dun_pin_ctrl(o, ur);
138                         break;
139
140                 default:
141                         return TCORE_RETURN_EINVAL;
142         }
143
144         return TCORE_RETURN_SUCCESS;
145 }
146
147 CoreObject *tcore_modem_new(TcorePlugin *p, const char *name,
148                 struct tcore_modem_operations *ops, TcoreHal *hal)
149 {
150         CoreObject *o = NULL;
151         struct private_object_data *po = NULL;
152
153         if (!p)
154                 return NULL;
155
156         o = tcore_object_new(p, name, hal);
157         if (!o)
158                 return NULL;
159
160         po = calloc(sizeof(struct private_object_data), 1);
161         if (!po) {
162                 tcore_object_free(o);
163                 return NULL;
164         }
165
166         po->ops = ops;
167
168         tcore_object_set_type(o, CORE_OBJECT_TYPE_MODEM);
169         tcore_object_link_object(o, po);
170         tcore_object_set_free_hook(o, _free_hook);
171         tcore_object_set_clone_hook(o, _clone_hook);
172         tcore_object_set_dispatcher(o, _dispatcher);
173
174         return o;
175 }
176
177 void tcore_modem_free(CoreObject *o)
178 {
179         struct private_object_data *po = NULL;
180
181         CORE_OBJECT_CHECK(o, CORE_OBJECT_TYPE_MODEM);
182
183         po = tcore_object_ref_object(o);
184         if (!po)
185                 return;
186
187         free(po);
188         tcore_object_free(o);
189 }
190
191 TReturn tcore_modem_set_flight_mode_state(CoreObject *o, gboolean flag)
192 {
193         struct private_object_data *po = NULL;
194
195         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_MODEM, TCORE_RETURN_EINVAL);
196
197         po = tcore_object_ref_object(o);
198         if (!po)
199                 return TCORE_RETURN_EINVAL;
200
201         po->flight_mode = flag;
202
203         return TRUE;
204 }
205
206 gboolean tcore_modem_get_flight_mode_state(CoreObject *o)
207 {
208         struct private_object_data *po = NULL;
209
210         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_MODEM, FALSE);
211
212         po = tcore_object_ref_object(o);
213         if (!po)
214                 return FALSE;
215
216         return po->flight_mode;
217 }
218
219 TReturn tcore_modem_set_powered(CoreObject *o, gboolean pwr)
220 {
221         TcoreHal *h;
222
223         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_MODEM, TCORE_RETURN_EINVAL);
224
225         h = tcore_object_get_hal(o);
226         if (!h)
227                 return TCORE_RETURN_FAILURE;
228
229         tcore_hal_set_power_state(h, pwr);
230
231         return TRUE;
232 }
233
234 gboolean tcore_modem_get_powered(CoreObject *o)
235 {
236         TcoreHal *h;
237
238         CORE_OBJECT_CHECK_RETURN(o, CORE_OBJECT_TYPE_MODEM, FALSE);
239
240         h = tcore_object_get_hal(o);
241         if (!h)
242                 return FALSE;
243
244         return tcore_hal_get_power_state(h);
245 }