tizen 2.3 release
[kernel/api/system-resource.git] / src / network / roaming.c
1 /*
2  *  resourced
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 /*
21  *
22  * @file roaming.c
23  *
24  * @desc Roaming persistent object. Due roaming changes not so often we can keep it in
25  *  our memory and handle roaming changes.
26  *  In this file we keep roaming state in global variable and change it in callback.
27  */
28
29 #include <glib.h>
30 #include <telephony_network.h>
31
32 #include "roaming.h"
33 #include "trace.h"
34 #include "macro.h"
35
36 static resourced_roaming_type roaming_state;
37
38 /* for avoiding dependency in this file */
39
40 static GSList *roaming_callbacks;
41
42 static void invoke_roaming_callbacks(void)
43 {
44         GSList *func_iter = NULL;
45         gslist_for_each_item(func_iter, roaming_callbacks) {
46                 if (func_iter && func_iter->data)
47                         ((roaming_cb)func_iter->data)();
48         }
49 }
50
51 void regist_roaming_cb(roaming_cb cb)
52 {
53         roaming_callbacks = g_slist_prepend(roaming_callbacks, cb);
54 }
55
56 static void on_roaming_change(bool new_roaming,
57         void UNUSED *user_data)
58 {
59         _D("Roaming is changed %d", (int)new_roaming);
60         roaming_state = new_roaming ? RESOURCED_ROAMING_ENABLE : RESOURCED_ROAMING_DISABLE;
61         invoke_roaming_callbacks();
62 }
63
64 /**
65  * @brief Get initial value for roaming and sets callback for handling roaming change
66  */
67 static void init_roaming_state(void)
68 {
69         bool roaming = false;
70
71         if (network_info_set_roaming_state_changed_cb(on_roaming_change,
72                 NULL) != NETWORK_INFO_ERROR_NONE)
73                 _E("Can not register callback for handle roaming changes.");
74
75         if (network_info_is_roaming(&roaming) != NETWORK_INFO_ERROR_NONE)
76                 _E("Failed to get initial roaming state!");
77
78         roaming_state = roaming ?
79                 RESOURCED_ROAMING_ENABLE : RESOURCED_ROAMING_DISABLE;
80 }
81
82 resourced_roaming_type get_roaming(void)
83 {
84         execute_once {
85                 init_roaming_state();
86         }
87         return roaming_state;
88 }
89