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 struct indicator_device_state {
57 static struct indicator_device_state indicator_info = {
62 static gboolean _indicator_update_callback(gpointer user_data);
64 static void _indicator_initialize(Server *s)
66 indicator_info.prev_rx = 0;
67 indicator_info.prev_tx = 0;
68 indicator_info.curr_rx = 0;
69 indicator_info.curr_tx = 0;
72 static gboolean _indicator_start_updater(Server *s)
75 gpointer vconf_handle;
77 dbg("indicator is started");
79 strg_vconf = tcore_server_find_storage(s, "vconf");
80 vconf_handle = tcore_storage_create_handle(strg_vconf, "vconf");
82 err("fail to create vconf db_handle");
84 tcore_storage_set_int(strg_vconf, STORAGE_KEY_PACKET_SERVICE_STATE, INDICATOR_ON);
89 _indicator_initialize(s);
91 src = g_timeout_source_new_seconds(INDICATOR_UPDATE_INTERVAL);
92 g_source_set_callback(src, _indicator_update_callback, s, NULL);
93 g_source_set_priority(src, G_PRIORITY_HIGH);
94 g_source_attach(src, NULL);
100 static gboolean _indicator_stop_updater(Server *s)
103 gpointer vconf_handle;
104 int t_rx = 0, t_tx = 0;
106 dbg("indicator is stopped");
107 strg_vconf = tcore_server_find_storage(s, "vconf");
108 vconf_handle = tcore_storage_create_handle(strg_vconf, "vconf");
110 err("fail to create vconf db_handle");
112 tcore_storage_set_int(strg_vconf, STORAGE_KEY_PACKET_SERVICE_STATE, INDICATOR_OFF);
114 t_rx = tcore_storage_get_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_TOTAL_RCV);
115 t_tx = tcore_storage_get_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_TOTAL_SNT);
116 t_rx += (int)indicator_info.curr_rx;
117 t_tx += (int)indicator_info.curr_tx;
119 tcore_storage_set_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_TOTAL_RCV, t_rx);
120 tcore_storage_set_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_TOTAL_SNT, t_tx);
121 tcore_storage_set_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_LAST_RCV, (int)indicator_info.curr_rx);
122 tcore_storage_set_int(strg_vconf, STORAGE_KEY_CELLULAR_PKT_LAST_SNT, (int)indicator_info.curr_tx);
127 g_source_destroy(src);
133 static gint _indicator_get_proc_ver(gchar *buff)
135 if (strstr(buff, "compressed")) return 3;
136 if (strstr(buff, "bytes")) return 2;
140 static gint _indicator_get_pkt(gchar *buff, gint proc_ver, guint64 *rx_pkt, guint64 *tx_pkt)
146 memset(s_rx, 0 , 100);
147 memset(s_tx, 0 , 100);
154 result = sscanf(buff,
155 "%s %*s %*s %*s %*s %*s %*s %*s %s %*s %*s %*s %*s %*s %*s %*s",
159 result = sscanf(buff,
160 "%s %*s %*s %*s %*s %*s %*s %*s %s %*s %*s %*s %*s %*s %*s %*s",
164 result = sscanf(buff,
165 "%s %*s %*s %*s %*s %*s %*s %*s %s %*s %*s %*s %*s %*s %*s %*s",
169 dbg("stats unknown version");
173 *rx_pkt = g_ascii_strtoull(s_rx, NULL, 10);
174 *tx_pkt = g_ascii_strtoull(s_tx, NULL, 10);
179 static gboolean _indicator_get_pktcnt(gpointer user_data)
183 gchar buff[INDICATOR_BUFF_SIZE];
185 pf = fopen(INDICATOR_PROCFILE, "r");
187 err("indicator fail to open file(%s), errno(%d)", INDICATOR_PROCFILE, errno);
191 fgets(buff, sizeof(buff), pf);
192 fgets(buff, sizeof(buff), pf);
193 proc_ver = _indicator_get_proc_ver(buff);
195 while (fgets(buff, sizeof(buff), pf)) {
199 gchar *ifname, *entry;
202 while (*ifname == ' ')
204 entry = strrchr(ifname, ':');
207 result = _indicator_get_pkt(entry, proc_ver, &rx_pkt, &tx_pkt);
209 err("stats fail to get proc field");
214 if ( g_strcmp0(ifname, indicator_info.devname) == 0 ){
215 indicator_info.prev_rx = indicator_info.curr_rx;
216 indicator_info.prev_tx = indicator_info.curr_tx;
217 indicator_info.curr_rx = rx_pkt;
218 indicator_info.curr_tx = tx_pkt;
227 static gboolean _indicator_update(Server *s)
231 gpointer vconf_handle;
233 strg_vconf = tcore_server_find_storage(s, "vconf");
234 vconf_handle = tcore_storage_create_handle(strg_vconf, "vconf");
236 err("fail to create vconf db_handle");
238 pkt_state = tcore_storage_get_int(strg_vconf, STORAGE_KEY_PACKET_SERVICE_STATE);
240 if(!indicator_info.active) return FALSE;
242 if ((indicator_info.curr_rx > indicator_info.prev_rx)
243 || (indicator_info.curr_tx > indicator_info.prev_tx)) {
244 if(pkt_state != INDICATOR_ONLINE)
245 tcore_storage_set_int(strg_vconf, STORAGE_KEY_PACKET_SERVICE_STATE, INDICATOR_ONLINE);
247 else{ //rx, tx are the same as before
248 if(pkt_state != INDICATOR_ON)
249 tcore_storage_set_int(strg_vconf, STORAGE_KEY_PACKET_SERVICE_STATE, INDICATOR_ON);
255 static gboolean _indicator_update_callback(gpointer user_data)
260 s = (Server *)user_data;
262 rv = _indicator_get_pktcnt(NULL);
268 rv = _indicator_update(s);
277 static enum tcore_hook_return __on_hook_powered(Server *s, CoreObject *source,
278 enum tcore_notification_command command, unsigned int data_len, void *data, void *user_data)
280 struct tnoti_modem_power *modem_power = NULL;
282 dbg("powered event called");
283 g_return_val_if_fail(data != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
285 modem_power = (struct tnoti_modem_power *)data;
286 if ( modem_power->state == MODEM_STATE_ERROR ){
287 indicator_info.active = FALSE;
288 g_free(indicator_info.devname);
289 indicator_info.devname = NULL;
290 _indicator_stop_updater(s);
293 return TCORE_HOOK_RETURN_CONTINUE;
296 static enum tcore_hook_return __on_hook_callstatus(Server *s, CoreObject *source,
297 enum tcore_notification_command command, unsigned int data_len, void *data,
300 unsigned int con_id = 0;
301 CoreObject *co_ps = NULL, *co_context = NULL;
302 struct tnoti_ps_call_status *cstatus = NULL;
304 dbg("call status event");
305 g_return_val_if_fail(data != NULL, TCORE_HOOK_RETURN_STOP_PROPAGATION);
308 dbg("ps object(%p)", co_ps);
309 co_context = tcore_ps_ref_context_by_role(co_ps, CONTEXT_ROLE_INTERNET);
310 con_id = tcore_context_get_id(co_context);
311 dbg("context(%p) con_id(%d)", co_context, con_id);
313 cstatus = (struct tnoti_ps_call_status *) data;
314 dbg("call status event cid(%d) state(%d)", cstatus->context_id, cstatus->state);
316 if(con_id != cstatus->context_id)
317 return TCORE_HOOK_RETURN_CONTINUE;
319 if (cstatus->state == PS_DATA_CALL_CTX_DEFINED) {
321 dbg("Just noti for PDP define complete, do nothing.");
322 return TCORE_HOOK_RETURN_CONTINUE;
324 else if (cstatus->state == PS_DATA_CALL_CONNECTED) {
325 indicator_info.active = TRUE;
326 indicator_info.devname = tcore_context_get_ipv4_devname(co_context);
327 _indicator_start_updater(s);
328 return TCORE_HOOK_RETURN_CONTINUE;
331 indicator_info.active = FALSE;
332 g_free(indicator_info.devname);
333 indicator_info.devname = NULL;
334 _indicator_stop_updater(s);
336 return TCORE_HOOK_RETURN_CONTINUE;
339 static gboolean on_load()
341 dbg("Indicator plugin load!");
345 static gboolean on_init(TcorePlugin *p)
348 s = tcore_plugin_ref_server(p);
349 tcore_server_add_notification_hook(s, TNOTI_MODEM_POWER, __on_hook_powered, NULL);
350 tcore_server_add_notification_hook(s, TNOTI_PS_CALL_STATUS, __on_hook_callstatus, NULL);
351 dbg("initialized Indicator plugin!");
355 static void on_unload(TcorePlugin *p)
361 struct tcore_plugin_define_desc plugin_define_desc =
364 .priority = TCORE_PLUGIN_PRIORITY_MID + 2,