Added a plugin for net popup 18/131918/5
authorhyunuktak <hyunuk.tak@samsung.com>
Wed, 31 May 2017 08:05:26 +0000 (17:05 +0900)
committerhyunuktak <hyunuk.tak@samsung.com>
Thu, 15 Jun 2017 01:33:57 +0000 (10:33 +0900)
Change-Id: I8ebc30530c1f819832f144fb78528193b48007f6
Signed-off-by: hyunuktak <hyunuk.tak@samsung.com>
CMakeLists.txt
include/stc-manager-plugin.h [new file with mode: 0755]
include/stc-plugin.h [new file with mode: 0755]
packaging/stc-manager.spec
plugin/CMakeLists.txt [new file with mode: 0755]
plugin/stc-plugin.c [new file with mode: 0755]
src/monitor/stc-monitor.c
src/stc-manager-plugin.c [new file with mode: 0755]
src/stc-manager.c

index 95356ec..4dfaf15 100644 (file)
@@ -3,7 +3,9 @@ PROJECT(stc-manager C)
 SET(PACKAGE ${PROJECT_NAME})
 SET(INTERFACES "${CMAKE_SOURCE_DIR}/interfaces")
 SET(PREFIX ${CMAKE_INSTALL_PREFIX})
+SET(LIBDIR ${PREFIX}/${LIB_PATH})
 
 SET(DATA_DIR   ${CMAKE_SOURCE_DIR}/data)
 
 ADD_SUBDIRECTORY(src)
+ADD_SUBDIRECTORY(plugin)
diff --git a/include/stc-manager-plugin.h b/include/stc-manager-plugin.h
new file mode 100755 (executable)
index 0000000..6eae115
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __STC_MANAGER_PLUGIN_H__
+#define __STC_MANAGER_PLUGIN_H__
+
+#define STC_PLUGIN_FILEPATH            "/usr/lib/stc-manager-plugin.so"
+
+void stc_manager_plugin_init(void);
+void stc_manager_plugin_deinit(void);
+
+int stc_send_warn_message_to_net_popup(const char *content,
+               const char *type, const char *app_id, const char *iftype, const char *warn);
+int stc_send_restriction_message_to_net_popup(const char *content,
+               const char *type, const char *app_id, const char *iftype, const char *limit);
+
+#endif /* __STC_MANAGER_PLUGIN_H__ */
diff --git a/include/stc-plugin.h b/include/stc-plugin.h
new file mode 100755 (executable)
index 0000000..3bbcedb
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __STC_PLUGIN_H__
+#define __STC_PLUGIN_H__
+
+#include <glib.h>
+
+typedef struct {
+       int(*send_restriction_message_to_net_popup) (const char *,
+               const char *, const char *, const char *, const char *);
+       int(*send_warn_message_to_net_popup) (const char *,
+               const char *, const char *, const char *, const char *);
+} stc_plugin_s;
+
+#endif /* __STC_PLUGIN_H__ */
index d575b3b..83b2561 100644 (file)
@@ -30,6 +30,14 @@ BuildRequires:  python-xml
 %description
 A smart traffic control manager to manage traffic counting and bandwidth limitation
 
+%package plugin
+Summary: STC manager plugin
+BuildRequires: pkgconfig(bundle)
+BuildRequires: pkgconfig(syspopup-caller)
+
+%description plugin
+A smart traffic control manager extension for plugin
+
 %prep
 %setup -q
 chmod 644 %{SOURCE0}
@@ -42,6 +50,7 @@ export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE"
 
 %cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \
        -DBIN_DIR=%{_bindir} \
+       -DLIB_PATH=%{_lib} \
        -DENABLE_DATABASE=%{enable_database} \
        -DDATABASE_FULL_PATH=%{database_full_path} \
        -DENABLE_STATISTICS=%{enable_statistics} \
@@ -94,3 +103,7 @@ cp resources/dbus/stc-manager.conf %{buildroot}%{_sysconfdir}/dbus-1/system.d/st
 %config(noreplace) %attr(660, root, root) %{database_full_path}
 %config(noreplace) %attr(660, root, root) %{database_full_path}-journal
 %endif
+
+%files plugin
+%manifest %{name}.manifest
+%attr(500,root,root) %{_libdir}/stc-manager-plugin.so
diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt
new file mode 100755 (executable)
index 0000000..66ab497
--- /dev/null
@@ -0,0 +1,36 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT(stc-manager-plugin C)
+
+# Set required packages
+INCLUDE(FindPkgConfig)
+PKG_CHECK_MODULES(pkgs_plugin REQUIRED
+       dlog
+       gio-2.0
+       gio-unix-2.0
+       glib-2.0
+       bundle
+       syspopup-caller
+       )
+
+FOREACH(flag ${pkgs_plugin_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+# INCLUDE_DIRECTORIES(SRCS include)
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -g -Werror -fvisibility=hidden")
+SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
+SET(CMAKE_C_FLAGS_RELEASE "-O2")
+
+SET(SRCS_PLUGIN
+       stc-plugin.c
+       )
+
+# library build
+ADD_LIBRARY(${PROJECT_NAME} SHARED ${SRCS_PLUGIN})
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_plugin_LDFLAGS})
+SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "" OUTPUT_NAME ${PROJECT_NAME})
+
+# install
+INSTALL(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${LIBDIR})
diff --git a/plugin/stc-plugin.c b/plugin/stc-plugin.c
new file mode 100755 (executable)
index 0000000..13f0306
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syspopup_caller.h>
+#include <bundle.h>
+#include <bundle_internal.h>
+#include <dlog.h>
+
+#include "stc-plugin.h"
+
+#ifdef USE_DLOG
+#include <dlog.h>
+
+#undef LOG_TAG
+#define LOG_TAG "STC_MANAGER"
+
+#define STC_LOGD(format, args...) LOGD(format, ##args)
+#define STC_LOGI(format, args...) LOGI(format, ##args)
+#define STC_LOGW(format, args...) LOGW(format, ##args)
+#define STC_LOGE(format, args...) LOGE(format, ##args)
+
+#define __STC_LOG_FUNC_ENTER__ LOGD("Enter")
+#define __STC_LOG_FUNC_EXIT__ LOGD("Quit")
+
+#else /* USE_DLOG */
+
+#define STC_LOGD(format, args...)
+#define STC_LOGI(format, args...)
+#define STC_LOGW(format, args...)
+#define STC_LOGE(format, args...)
+
+#define __STC_LOG_FUNC_ENTER__
+#define __STC_LOG_FUNC_EXIT__
+
+#endif /* USE_DLOG */
+
+static int __stc_send_warn_message_to_net_popup(const char *content,
+               const char *type, const char *app_id, const char *iftype, const char *warn)
+{
+       int ret = 0;
+       bundle *b = bundle_create();
+
+       STC_LOGD("Warn message : content[%s] type[%s] app_id[%s] limit[%s]",
+               title, content, type, app_id, limit);
+
+       bundle_add(b, "_SYSPOPUP_CONTENT_", content);
+       bundle_add(b, "_SYSPOPUP_TYPE_", type);
+       bundle_add(b, "_APP_ID_", app_id);
+       bundle_add(b, "_IF_TYPE_", iftype);
+       bundle_add(b, "_WARN_LIMIT_", warn);
+
+       ret = syspopup_launch("net-popup", b);
+
+       bundle_free(b);
+
+       return ret;
+}
+
+static int __stc_send_restriction_message_to_net_popup(const char *content,
+               const char *type, const char *app_id, const char *iftype, const char *limit)
+{
+       int ret = 0;
+       bundle *b = bundle_create();
+
+       STC_LOGD("Restriction message : content[%s] type[%s] app_id[%s] limit[%s]",
+               title, content, type, app_id);
+
+       bundle_add(b, "_SYSPOPUP_CONTENT_", content);
+       bundle_add(b, "_SYSPOPUP_TYPE_", type);
+       bundle_add(b, "_APP_ID_", app_id);
+       bundle_add(b, "_IF_TYPE_", iftype);
+       bundle_add(b, "_RESTRICTION_LIMIT_", limit);
+
+       ret = syspopup_launch("net-popup", b);
+
+       bundle_free(b);
+
+       return ret;
+}
+
+
+API stc_plugin_s stc_plugin = {
+       .send_warn_message_to_net_popup =
+               __stc_send_warn_message_to_net_popup,
+       .send_restriction_message_to_net_popup =
+               __stc_send_restriction_message_to_net_popup
+};
+
index b12920e..b4f43b3 100755 (executable)
@@ -24,6 +24,9 @@
 #include "table-statistics.h"
 #include "table-counters.h"
 #include "stc-monitor.h"
+#include "stc-manager-plugin.h"
+
+#define MAX_INT_LENGTH 128
 
 typedef struct {
        stc_app_key_s *app_key;
@@ -553,6 +556,8 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key,
                if (rstn_value->counter.in_bytes >= rstn_value->warn_limit.in_bytes
                    && rstn_value->warn_limit_crossed_notified == FALSE) {
                        gboolean rv = FALSE;
+                       char iftype[MAX_INT_LENGTH];
+                       char byte[MAX_INT_LENGTH];
                        stc_s *stc = (stc_s *)stc_get_manager();
                        ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
 
@@ -563,6 +568,11 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key,
                                                          g_variant_new("(s)", rstn_key->app_id));
                        if (rv == TRUE)
                                rstn_value->warn_limit_crossed_notified = TRUE;
+
+                       snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
+                       snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes);
+                       stc_send_warn_message_to_net_popup("warn threshold crossed",
+                               "warning_noti", rstn_key->app_id, iftype, byte);
                }
 
                /* block immediately */
@@ -573,6 +583,9 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key,
 
                        if (rstn_value->rstn_limit_crossed_notified == FALSE) {
                                gboolean rv = FALSE;
+                               int ret = 0;
+                               char iftype[MAX_INT_LENGTH];
+                               char byte[MAX_INT_LENGTH];
                                stc_s *stc = (stc_s *)stc_get_manager();
                                ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
 
@@ -583,6 +596,11 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key,
                                                                  g_variant_new("(s)", rstn_key->app_id));
                                if (rv == TRUE)
                                        rstn_value->rstn_limit_crossed_notified = TRUE;
+
+                               snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
+                               snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.in_bytes);
+                               stc_send_restriction_message_to_net_popup("restriction threshold crossed",
+                                       "restriction_noti", rstn_key->app_id, iftype, byte);
                        }
                }
 
@@ -595,6 +613,8 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key,
                if (rstn_value->counter.out_bytes >= rstn_value->limit.out_bytes
                    && rstn_value->warn_limit_crossed_notified == FALSE) {
                        gboolean rv = FALSE;
+                       char iftype[MAX_INT_LENGTH];
+                       char byte[MAX_INT_LENGTH];
                        stc_s *stc = (stc_s *)stc_get_manager();
                        ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
 
@@ -605,6 +625,11 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key,
                                                          g_variant_new("(s)", rstn_key->app_id));
                        if (rv == TRUE)
                                rstn_value->warn_limit_crossed_notified = TRUE;
+
+                       snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
+                       snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes);
+                       stc_send_warn_message_to_net_popup("warn threshold crossed",
+                               "warning_noti", rstn_key->app_id, iftype, byte);
                }
 
                /* block immediately */
@@ -615,6 +640,8 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key,
 
                        if (rstn_value->rstn_limit_crossed_notified == FALSE) {
                                gboolean rv = FALSE;
+                               char iftype[MAX_INT_LENGTH];
+                               char byte[MAX_INT_LENGTH];
                                stc_s *stc = (stc_s *)stc_get_manager();
                                ret_value_msg_if(stc == NULL, FALSE, "failed to get stc data");
 
@@ -625,6 +652,11 @@ static gboolean __rstn_counter_update_foreach_classid(gpointer key,
                                                                  g_variant_new("(s)", rstn_key->app_id));
                                if (rv == TRUE)
                                        rstn_value->rstn_limit_crossed_notified = TRUE;
+
+                               snprintf(iftype, MAX_INT_LENGTH, "%d", rstn_key->iftype);
+                               snprintf(byte, MAX_INT_LENGTH, "%lld", rstn_value->limit.out_bytes);
+                               stc_send_restriction_message_to_net_popup("restriction threshold crossed",
+                                       "restriction_noti", rstn_key->app_id, iftype, byte);
                        }
                }
 
diff --git a/src/stc-manager-plugin.c b/src/stc-manager-plugin.c
new file mode 100755 (executable)
index 0000000..d25b1a8
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <dlfcn.h>
+
+#include "stc-manager.h"
+#include "stc-plugin.h"
+#include "stc-manager-plugin.h"
+
+static gboolean stc_plugin_enabled = FALSE;
+static void *handle_plugin;
+static stc_plugin_s *stc_plugin;
+
+void stc_manager_plugin_init(void)
+{
+       __STC_LOG_FUNC_ENTER__;
+
+       handle_plugin = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW);
+       if (!handle_plugin) {
+               STC_LOGE("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror());
+               return;
+       }
+
+       stc_plugin = dlsym(handle_plugin, "stc_plugin");
+       if (!stc_plugin) {
+               STC_LOGE("Can't load symbol: %s", dlerror());
+               dlclose(handle_plugin);
+               return;
+       }
+
+       stc_plugin_enabled = TRUE;
+
+       __STC_LOG_FUNC_EXIT__;
+}
+
+void stc_manager_plugin_deinit(void)
+{
+       __STC_LOG_FUNC_ENTER__;
+
+       if (!stc_plugin_enabled)
+               return;
+
+       stc_plugin_enabled = FALSE;
+       dlclose(handle_plugin);
+
+       __STC_LOG_FUNC_EXIT__;
+}
+
+int stc_send_warn_message_to_net_popup(const char *content,
+               const char *type, const char *app_id, const char *iftype, const char *warn)
+{
+       __STC_LOG_FUNC_ENTER__;
+
+       if (!stc_plugin_enabled) {
+               STC_LOGE("Plugin wasn't enabled");
+               return 0;
+       }
+
+       if (!stc_plugin) {
+               STC_LOGE("Plugin wasn't loaded");
+               return 0;
+       }
+
+       return stc_plugin->send_warn_message_to_net_popup(content, type, app_id, iftype, warn);
+}
+
+int stc_send_restriction_message_to_net_popup(const char *content,
+               const char *type, const char *app_id, const char *iftype, const char *limit)
+{
+       __STC_LOG_FUNC_ENTER__;
+
+       if (!stc_plugin_enabled) {
+               STC_LOGE("Plugin wasn't enabled");
+               return 0;
+       }
+
+       if (!stc_plugin) {
+               STC_LOGE("Plugin wasn't loaded");
+               return 0;
+       }
+
+       return stc_plugin->send_restriction_message_to_net_popup(content, type, app_id, iftype, limit);
+}
+
index bbabfa9..69349ae 100755 (executable)
@@ -24,6 +24,7 @@
 #include "helper-cgroup.h"
 #include "helper-nfacct-rule.h"
 #include "stc-monitor.h"
+#include "stc-manager-plugin.h"
 
 static stc_s *g_stc = NULL;
 
@@ -40,6 +41,7 @@ static void __stc_manager_deinit(void)
        stc_deinit_db_guard();
        stc_db_deinitialize();
        stc_manager_gdbus_deinit((gpointer)g_stc);
+       stc_manager_plugin_deinit();
 
        STC_LOGI("stc manager deinitialized");
        FREE(g_stc);
@@ -64,6 +66,7 @@ static stc_s *__stc_manager_init(void)
 
        stc_monitor_init();
        stc_manager_gdbus_init((gpointer)stc);
+       stc_manager_plugin_init();
 
        STC_LOGI("stc manager initialized");
        __STC_LOG_FUNC_EXIT__;