4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * @desc Implementation of API network statistics update
28 #include "resourced.h"
31 #include "edbus-handler.h"
33 #include "rd-network.h"
36 static E_DBus_Signal_Handler *handler;
37 static E_DBus_Connection *edbus_conn;
39 static dbus_bool_t dbus_call_method(const char *dest, const char *path,
40 const char *interface, const char *method)
46 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
48 _E("dbus_bus_get failed\n");
52 msg = dbus_message_new_method_call(dest, path, interface, method);
54 _E("Create dbus message failed\n");
58 ret = dbus_connection_send(conn, msg, NULL);
59 dbus_connection_flush(conn);
61 dbus_message_unref(msg);
62 dbus_connection_unref(conn);
66 API resourced_ret_c resourced_update_statistics(void)
68 dbus_bool_t ret = dbus_call_method(BUS_NAME,
69 RESOURCED_PATH_NETWORK,
70 RESOURCED_INTERFACE_NETWORK,
71 RESOURCED_NETWORK_UPDATE);
73 _D("Error resourced update statistics\n");
74 return RESOURCED_ERROR_FAIL;
77 return RESOURCED_ERROR_NONE;
80 struct update_context {
85 static void network_update_dbus_handler(void *user_data, DBusMessage *msg)
87 struct update_context *context;
88 struct network_update_info info;
90 ret_msg_if(user_data == NULL,
91 "Not valid user data");
93 ret_msg_if(context->cb == NULL,
94 "Not valid user data");
96 if (context->cb(&info, context->user_data) == NETWORK_CANCEL) {
97 network_unregister_update_cb();
101 API network_error_e network_register_update_cb(network_update_cb update_cb,
104 static int edbus_init_val;
105 static struct update_context context;
107 ret_value_msg_if(update_cb == NULL, NETWORK_ERROR_INVALID_PARAMETER,
108 "Please provide valid callback argument!");
110 ret_value_msg_if(handler != NULL, NETWORK_ERROR_INVALID_PARAMETER,
111 "Only one callback is supported!");
113 context.user_data = user_data;
114 context.cb = update_cb;
116 edbus_init_val = e_dbus_init();
117 ret_value_msg_if(edbus_init_val == 0,
118 NETWORK_ERROR_FAIL, "Fail to initialize dbus!");
120 edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
121 if (edbus_conn == NULL)
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);
133 return NETWORK_ERROR_NONE;
135 e_dbus_connection_close(edbus_conn);
139 return NETWORK_ERROR_FAIL;
142 API void network_unregister_update_cb(void)
144 e_dbus_signal_handler_del(edbus_conn, handler);
145 e_dbus_connection_close(edbus_conn);