4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: DongHoo Park <donghoo.park@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
33 #include <co_context.h>
35 #define INDICATOR_UPDATE_INTERVAL 1
36 #define INDICATOR_PROCFILE "/proc/net/dev"
37 #define INDICATOR_BUFF_SIZE 4096
38 #define DATABASE_PATH "/opt/dbspace/.dnet.db"
39 #define NO_RX_PKT_TIMEOUT 30
41 typedef enum _indicator_state {
44 INDICATOR_ONLINE = 0x03,
47 typedef enum _ps_call_state {
48 PS_CALL_STATE_RESULT_OK = 0x00,
49 PS_CALL_STATE_RESULT_CONNECT = 0x01,
50 PS_CALL_STATE_RESULT_NO_CARRIER = 0x03,
53 struct indicator_device_state {
63 static struct indicator_device_state indicator_info = {
68 static gboolean _indicator_update_callback(gpointer user_data);
70 static void _indicator_initialize(Server *s)
72 indicator_info.prev_rx = 0;
73 indicator_info.prev_tx = 0;
74 indicator_info.curr_rx = 0;
75 indicator_info.curr_tx = 0;
78 static gboolean _indicator_start_updater(Server *s)
81 gpointer vconf_handle;
83 dbg("indicator is started");
85 strg_vconf = tcore_server_find_storage(s, "vconf");
86 vconf_handle = tcore_storage_create_handle(strg_vconf, "vconf");
88 err("fail to create vconf db_handle");
90 tcore_storage_set_int(strg_vconf, STORAGE_KEY_PACKET_SERVICE_STATE, INDICATOR_ON);
95 _indicator_initialize(s);
97 src = g_timeout_source_new_seconds(INDICATOR_UPDATE_INTERVAL);
98 g_source_set_callback(src, _indicator_update_callback, s, NULL);
99 g_source_set_priority(src, G_PRIORITY_HIGH);
100 g_source_attach(src, NULL);
106 static gboolean _indicator_stop_updater(Server *s)
109 gpointer vconf_handle;
110 int t_rx = 0, t_tx = 0;
112 dbg("indicator is stopped");
113 strg_vconf = tcore_server_find_storage(s, "vconf");
114 vconf_handle = tcore_storage_create_handle(strg_vconf, "vconf");
116 err("fail to create vconf db_handle");
118 tcore_storage_set_int(strg_vconf, STORAGE_KEY_PACKET_SERVICE_STATE, INDICATOR_OFF);
120 t_rx = tcore_storage_get_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_TOTAL_RCV);
121 t_tx = tcore_storage_get_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_TOTAL_SNT);
122 t_rx += (int)indicator_info.curr_rx;
123 t_tx += (int)indicator_info.curr_tx;
125 tcore_storage_set_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_TOTAL_RCV, t_rx);
126 tcore_storage_set_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_TOTAL_SNT, t_tx);
127 tcore_storage_set_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_LAST_RCV, (int)indicator_info.curr_rx);
128 tcore_storage_set_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_LAST_SNT, (int)indicator_info.curr_tx);
133 g_source_destroy(src);
139 static gint _indicator_get_proc_ver(gchar *buff)
141 if (strstr(buff, "compressed")) return 3;
142 if (strstr(buff, "bytes")) return 2;
146 static gint _indicator_get_pkt(gchar *buff, gint proc_ver, guint64 *rx_pkt, guint64 *tx_pkt)
152 memset(s_rx, 0 , 100);
153 memset(s_tx, 0 , 100);
160 result = sscanf(buff,
161 "%s %*s %*s %*s %*s %*s %*s %*s %s %*s %*s %*s %*s %*s %*s %*s",
165 result = sscanf(buff,
166 "%s %*s %*s %*s %*s %*s %*s %*s %s %*s %*s %*s %*s %*s %*s %*s",
170 result = sscanf(buff,
171 "%s %*s %*s %*s %*s %*s %*s %*s %s %*s %*s %*s %*s %*s %*s %*s",
175 dbg("stats unknown version");
179 *rx_pkt = g_ascii_strtoull(s_rx, NULL, 10);
180 *tx_pkt = g_ascii_strtoull(s_tx, NULL, 10);
185 static gboolean _indicator_get_pktcnt(gpointer user_data)
189 gchar buff[INDICATOR_BUFF_SIZE];
192 pf = fopen(INDICATOR_PROCFILE, "r");
194 err("indicator fail to open file(%s), errno(%d)", INDICATOR_PROCFILE, errno);
198 rv = fgets(buff, sizeof(buff), pf);
199 rv = fgets(buff, sizeof(buff), pf);
200 proc_ver = _indicator_get_proc_ver(buff);
202 while (fgets(buff, sizeof(buff), pf)) {
206 gchar *ifname, *entry;
209 while (*ifname == ' ')
211 entry = strrchr(ifname, ':');
214 result = _indicator_get_pkt(entry, proc_ver, &rx_pkt, &tx_pkt);
216 err("stats fail to get proc field");
221 if ( g_strcmp0(ifname, indicator_info.devname) == 0 ){
222 indicator_info.prev_rx = indicator_info.curr_rx;
223 indicator_info.prev_tx = indicator_info.curr_tx;
224 indicator_info.curr_rx = rx_pkt;
225 indicator_info.curr_tx = tx_pkt;
234 static gboolean _indicator_update(Server *s)
238 gpointer vconf_handle;
240 strg_vconf = tcore_server_find_storage(s, "vconf");
241 vconf_handle = tcore_storage_create_handle(strg_vconf, "vconf");
243 err("fail to create vconf db_handle");
245 pkt_state = tcore_storage_get_int(strg_vconf, STORAGE_KEY_PACKET_SERVICE_STATE);
247 if(!indicator_info.active) return FALSE;
249 if ((indicator_info.curr_rx > indicator_info.prev_rx)
250 || (indicator_info.curr_tx > indicator_info.prev_tx)) {
251 if(pkt_state != INDICATOR_ONLINE)
252 tcore_storage_set_int(strg_vconf, STORAGE_KEY_PACKET_SERVICE_STATE, INDICATOR_ONLINE);
254 else{ //rx, tx are the same as before
255 if(pkt_state != INDICATOR_ON)
256 tcore_storage_set_int(strg_vconf, STORAGE_KEY_PACKET_SERVICE_STATE, INDICATOR_ON);
262 static gboolean _indicator_update_callback(gpointer user_data)
267 s = (Server *)user_data;
269 rv = _indicator_get_pktcnt(NULL);
275 rv = _indicator_update(s);
284 static enum tcore_hook_return __on_hook_powered(Server *s, CoreObject *source,
285 enum tcore_notification_command command, unsigned int data_len, void *data, void *user_data)
287 struct tnoti_modem_power *modem_power = NULL;
289 dbg("powered event called");
290 g_return_val_if_fail(data != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
292 modem_power = (struct tnoti_modem_power *)data;
293 if ( modem_power->state == MODEM_STATE_ERROR ){
294 indicator_info.active = FALSE;
295 g_free(indicator_info.devname);
296 indicator_info.devname = NULL;
297 _indicator_stop_updater(s);
300 return TCORE_HOOK_RETURN_CONTINUE;
303 static enum tcore_hook_return __on_hook_callstatus(Server *s, CoreObject *source,
304 enum tcore_notification_command command, unsigned int data_len, void *data,
308 CoreObject *co_ps = NULL, *co_context = NULL;
309 struct tnoti_ps_call_status *cstatus = NULL;
311 dbg("call status event");
312 g_return_val_if_fail(data != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
315 dbg("ps object(%p)", co_ps);
316 co_context = tcore_ps_ref_context_by_role(co_ps, CONTEXT_ROLE_INTERNET);
317 con_id = tcore_context_get_id(co_context);
318 dbg("context(%p) con_id(%d)", co_context, con_id);
320 cstatus = (struct tnoti_ps_call_status *) data;
321 dbg("call status event cid(%d) state(%d) reason(%d)", cstatus->context_id, cstatus->state, cstatus->result);
323 if(con_id != cstatus->context_id)
324 return TCORE_HOOK_RETURN_CONTINUE;
326 if (cstatus->state == PS_CALL_STATE_RESULT_OK) {
328 dbg("Just noti for PDP define complete, do nothing.");
329 return TCORE_HOOK_RETURN_CONTINUE;
331 else if (cstatus->state == PS_CALL_STATE_RESULT_CONNECT) {
332 indicator_info.active = TRUE;
333 indicator_info.devname = tcore_context_get_ipv4_devname(co_context);
334 _indicator_start_updater(s);
335 return TCORE_HOOK_RETURN_CONTINUE;
338 indicator_info.active = FALSE;
339 g_free(indicator_info.devname);
340 indicator_info.devname = NULL;
341 _indicator_stop_updater(s);
343 return TCORE_HOOK_RETURN_CONTINUE;
346 static gboolean on_load()
348 dbg("Indicator plugin load!");
352 static gboolean on_init(TcorePlugin *p)
355 s = tcore_plugin_ref_server(p);
356 tcore_server_add_notification_hook(s, TNOTI_MODEM_POWER, __on_hook_powered, NULL);
357 tcore_server_add_notification_hook(s, TNOTI_PS_CALL_STATUS, __on_hook_callstatus, NULL);
358 dbg("initialized Indicator plugin!");
362 static void on_unload(TcorePlugin *p)
368 struct tcore_plugin_define_desc plugin_define_desc =
371 .priority = TCORE_PLUGIN_PRIORITY_MID + 2,