tizen 2.3 release
[kernel/api/system-resource.git] / src / network / update.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2014 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  * @file update.c
22  *
23  * @desc Implementation of API network statistics update
24  *
25  */
26
27
28 #include "resourced.h"
29
30 #include "const.h"
31 #include "edbus-handler.h"
32 #include "macro.h"
33 #include "rd-network.h"
34 #include "trace.h"
35
36 static E_DBus_Signal_Handler *handler;
37 static E_DBus_Connection *edbus_conn;
38
39 static dbus_bool_t dbus_call_method(const char *dest, const char *path,
40                                      const char *interface, const char *method)
41 {
42         DBusConnection *conn;
43         DBusMessage *msg;
44         dbus_bool_t ret;
45
46         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
47         if (!conn) {
48                 _E("dbus_bus_get failed\n");
49                 return FALSE;
50         }
51
52         msg = dbus_message_new_method_call(dest, path, interface, method);
53         if (!msg) {
54                 _E("Create dbus message failed\n");
55                 return FALSE;
56         }
57
58         ret = dbus_connection_send(conn, msg, NULL);
59         dbus_connection_flush(conn);
60
61         dbus_message_unref(msg);
62         dbus_connection_unref(conn);
63         return ret;
64 }
65
66 API resourced_ret_c resourced_update_statistics(void)
67 {
68         dbus_bool_t ret = dbus_call_method(BUS_NAME,
69                                             RESOURCED_PATH_NETWORK,
70                                             RESOURCED_INTERFACE_NETWORK,
71                                             RESOURCED_NETWORK_UPDATE);
72         if (ret == FALSE) {
73                 _D("Error resourced update statistics\n");
74                 return RESOURCED_ERROR_FAIL;
75         }
76
77         return RESOURCED_ERROR_NONE;
78 }
79
80 struct update_context {
81         void *user_data;
82         network_update_cb cb;
83 };
84
85 static void network_update_dbus_handler(void *user_data, DBusMessage *msg)
86 {
87         struct update_context *context;
88         struct network_update_info info;
89
90         ret_msg_if(user_data == NULL,
91                 "Not valid user data");
92         context = user_data;
93         ret_msg_if(context->cb == NULL,
94                 "Not valid user data");
95
96         if (context->cb(&info, context->user_data) == NETWORK_CANCEL) {
97                 network_unregister_update_cb();
98         }
99 }
100
101 API network_error_e network_register_update_cb(network_update_cb update_cb,
102         void *user_data)
103 {
104         static int edbus_init_val;
105         static struct update_context context;
106
107         ret_value_msg_if(update_cb == NULL, NETWORK_ERROR_INVALID_PARAMETER,
108                 "Please provide valid callback argument!");
109
110         ret_value_msg_if(handler != NULL, NETWORK_ERROR_INVALID_PARAMETER,
111                 "Only one callback is supported!");
112
113         context.user_data = user_data;
114         context.cb = update_cb;
115
116         edbus_init_val = e_dbus_init();
117         ret_value_msg_if(edbus_init_val == 0,
118                  NETWORK_ERROR_FAIL, "Fail to initialize dbus!");
119
120         edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
121         if (edbus_conn == NULL)
122                 goto dbus_release;
123
124         handler = e_dbus_signal_handler_add(edbus_conn, NULL,
125                 RESOURCED_PATH_NETWORK,
126                 RESOURCED_INTERFACE_NETWORK,
127                 RESOURCED_NETWORK_UPDATE_FINISH,
128                 network_update_dbus_handler, &context);
129
130         if (handler == NULL)
131                 goto dbus_close;
132
133         return NETWORK_ERROR_NONE;
134 dbus_close:
135         e_dbus_connection_close(edbus_conn);
136
137 dbus_release:
138         e_dbus_shutdown();
139         return NETWORK_ERROR_FAIL;
140 }
141
142 API void network_unregister_update_cb(void)
143 {
144         e_dbus_signal_handler_del(edbus_conn, handler);
145         e_dbus_connection_close(edbus_conn);
146         e_dbus_shutdown();
147
148         handler = NULL;
149         edbus_conn = NULL;
150 }
151