Initial plugin for wifi 50/213150/4
authorhyunuk.tak <hyunuk.tak@samsung.com>
Mon, 2 Sep 2019 05:30:59 +0000 (14:30 +0900)
committerhyunuk.tak <hyunuk.tak@samsung.com>
Wed, 27 Nov 2019 23:07:47 +0000 (08:07 +0900)
Change-Id: I4bb3297c4678967ac355f981b8f34b41025561ab
Signed-off-by: hyunuk.tak <hyunuk.tak@samsung.com>
CMakeLists.txt
packaging/batterymonitor-plugins.spec
plugin/wifi/CMakeLists.txt [new file with mode: 0644]
plugin/wifi/include/bm_log.h [new file with mode: 0755]
plugin/wifi/include/bm_plugin_interface.h [new file with mode: 0755]
plugin/wifi/include/bm_wifi_plugin.h [new file with mode: 0755]
plugin/wifi/include/bm_wifi_util.h [new file with mode: 0755]
plugin/wifi/src/bm_wifi_plugin.c [new file with mode: 0755]

index de11c86..c06492c 100644 (file)
@@ -2,3 +2,4 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 
 ADD_SUBDIRECTORY(plugin/cpu)
 ADD_SUBDIRECTORY(plugin/display)
+ADD_SUBDIRECTORY(plugin/wifi)
index 51f7686..a783c8e 100644 (file)
@@ -12,6 +12,7 @@ BuildRequires:  pkgconfig(aul)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(libtzplatform-config)
+BuildRequires:  pkgconfig(pkgmgr-info)
 
 Requires(post):  /sbin/ldconfig
 Requires(postun):  /sbin/ldconfig
@@ -36,6 +37,12 @@ Summary: Battery-Monitor display plugin
 %description display
 Display plugin for Battery-Monitor
 
+%package wifi
+Summary:  Battery-Monitor device network plugin
+
+%description wifi
+Device network plugin for Battery-Monitor
+
 %prep
 %setup -q
 cp %{SOURCE1001} .
@@ -67,3 +74,8 @@ mkdir -p %{buildroot}%{BATTERY_MONITOR_LIBDIR}
 %manifest %{name}.manifest
 %license LICENSE
 %{BATTERY_MONITOR_LIBDIR}/lib%{name}-display*
+
+%files wifi
+%manifest %{name}.manifest
+%license LICENSE
+%{BATTERY_MONITOR_LIBDIR}/lib%{name}-wifi*
diff --git a/plugin/wifi/CMakeLists.txt b/plugin/wifi/CMakeLists.txt
new file mode 100644 (file)
index 0000000..b22632e
--- /dev/null
@@ -0,0 +1,34 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
+PROJECT("batterymonitor-plugins-wifi" C)
+
+SET(CMAKE_INSTALL_PREFIX /usr)
+SET(PREFIX ${CMAKE_INSTALL_PREFIX})
+
+SET(PKG_MODULES
+       aul
+       dlog
+       glib-2.0
+       pkgmgr-info
+)
+INCLUDE(FindPkgConfig)
+pkg_check_modules(${PROJECT_NAME} REQUIRED ${PKG_MODULES})
+
+FOREACH(flag ${${PROJECT_NAME}_CFLAGS})
+       SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
+ENDFOREACH(flag)
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIC -Wall -Werror -fvisibility=hidden")
+SET(CMAKE_C_FLAGS_DEBUG "-O0 -g")
+
+SET(SOURCES
+       src/bm_wifi_plugin.c
+)
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
+
+ADD_LIBRARY(${PROJECT_NAME} SHARED ${SOURCES})
+TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${${PROJECT_NAME}_LDFLAGS})
+SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES
+               VERSION ${FULLVER} SOVERSION ${MAJORVER} CLEAN_DIRECT_OUTPUT 1)
+
+INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${BATTERY_MONITOR_LIBDIR})
diff --git a/plugin/wifi/include/bm_log.h b/plugin/wifi/include/bm_log.h
new file mode 100755 (executable)
index 0000000..5d8a497
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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 __BM_LOG_H__
+#define __BM_LOG_H__
+
+#include <dlog.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "BM_WIFI"
+
+#ifndef _BME
+#define _E(fmt, args...) LOGE("[%s:%d] "fmt"\n", __func__, __LINE__, ##args)
+#endif
+
+#ifndef _BMD
+#define _D(fmt, args...) LOGD("[%s:%d] "fmt"\n", __func__, __LINE__, ##args)
+#endif
+
+#ifndef _BMI
+#define _I(fmt, args...) LOGI("[%s:%d] "fmt"\n", __func__, __LINE__, ##args)
+#endif
+
+#ifndef _BMW
+#define _W(fmt, args...) LOGW("[%s:%d] "fmt"\n", __func__, __LINE__, ##args)
+#endif
+
+#define ENTER dlog_print(DLOG_DEBUG, LOG_TAG, "%s : %s(%d) >>>>>>>> called", rindex(__FILE__, '/') + 1, __func__, __LINE__)
+#define EXIT  dlog_print(DLOG_DEBUG, LOG_TAG, "%s : %s(%d) <<<<<<<< ended", rindex(__FILE__, '/') + 1, __func__, __LINE__)
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BM_LOG_H__ */
+
+
diff --git a/plugin/wifi/include/bm_plugin_interface.h b/plugin/wifi/include/bm_plugin_interface.h
new file mode 100755 (executable)
index 0000000..fbd067d
--- /dev/null
@@ -0,0 +1,287 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved.
+ *
+ * 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 __BM_PLUGIN_INTERFACE_H__
+#define __BM_PLUGIN_INTERFACE_H__
+
+#include <stdio.h>
+#include <gmodule.h>
+#include <time.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef unsigned int   uint;
+typedef unsigned char  uchar;
+
+#define BM_DATA_TYPE_MAX (BM_DATA_TYPE_BATTERY + 1)
+#define BM_PLUGIN_ID_MAX (BM_PLUGIN_ID_BATTERY + 1)
+#define BM_DURATION_TYPE_MAX (BM_DURATION_TYPE_1WEEK + 1)
+
+typedef enum {
+       BM_PLUGIN_ERROR_NONE = TIZEN_ERROR_NONE,                                /**< Successful */
+       BM_PLUGIN_ERROR_NO_DATA = TIZEN_ERROR_NO_DATA,                          /**< No data */
+       BM_PLUGIN_ERROR_INVALID_PARAMETER = TIZEN_ERROR_INVALID_PARAMETER,      /**< Invalid parameter */
+       BM_PLUGIN_ERROR_OUT_OF_MEMORY = TIZEN_ERROR_OUT_OF_MEMORY,              /**< Out of memory */
+       BM_PLUGIN_ERROR_NOT_SUPPORTED = TIZEN_ERROR_NOT_SUPPORTED,              /**< Not supported */
+       BM_PLUGIN_ERROR_SERVICE_UNAVAILABLE = TIZEN_ERROR_UNKNOWN - 1           /**< Service unavailable */
+} bm_plugin_error_e;
+
+/*
+ * enum for plugin identifier
+ */
+typedef enum {
+       BM_PLUGIN_ID_BLE=0,             /**< Bluetooth Low Energy */
+       BM_PLUGIN_ID_WIFI,              /**< Wi-Fi */
+       BM_PLUGIN_ID_CPU,               /**< CPU */
+       BM_PLUGIN_ID_DISPLAY,           /**< Display */
+       BM_PLUGIN_ID_DEVICE_NETWORK,    /**< Network */
+       BM_PLUGIN_ID_GPS_SENSOR,        /**< GPS */
+       BM_PLUGIN_ID_HRM_SENSOR,        /**< Heart Rate Monitor */
+       BM_PLUGIN_ID_BATTERY            /**< Battery */
+} bm_plugin_id_e;
+
+/*
+ * enum for feature data collection period
+ */
+typedef enum {
+       BM_DURATION_TYPE_1DAY = 0,      /**< Set the period from a day ago to now */
+       BM_DURATION_TYPE_1WEEK          /**< Set the period from a week ago to now */
+} bm_duration_type_e;
+
+/*
+ * enum for feature data type
+ */
+typedef enum {
+       BM_DATA_TYPE_BLE=0,             /**< Bluetooth Low Energy */
+       BM_DATA_TYPE_WIFI,              /**< Wi-Fi */
+       BM_DATA_TYPE_CPU,               /**< CPU */
+       BM_DATA_TYPE_DISPLAY,           /**< Display */
+       BM_DATA_TYPE_DEVICE_NETWORK,    /**< Network */
+       BM_DATA_TYPE_GPS_SENSOR,        /**< GPS */
+       BM_DATA_TYPE_HRM_SENSOR,        /**< Heart Rate Monitor */
+       BM_DATA_TYPE_BATTERY            /**< Battery */
+} bm_plugin_data_type_e;
+
+/*
+ * common structure for application time map.
+ */
+typedef struct {
+       char *app_id;           /**< application id */
+       uint time;              /**< total duration for which application as active */
+} app_time_map_st1;
+
+typedef struct {
+       char *app_id;           /**< application id */
+       uint time;              /**< total duration for which application as active */
+       uint rx;                /**< Total Bytes received by the application during runtime */
+       uint tx;                /**< Total Bytes transmitted by the application during runtime */
+} app_time_map_st2;
+
+/*
+ * structure for "display" feature data.
+ */
+typedef struct {
+       uint high;                      /**< total time in milliseconds during which brightness intensity was high */
+       uint low;                       /**< total time in milliseconds during which brightness intensity was low */
+       uint med;                       /**< total time in milliseconds during which brightness intensity was medium */
+       time_t start;                   /**< start time of feature data collection session */
+       time_t stop;                    /**< stop time of feature data collection session */
+       GSList *atm_list;               /**< application time map('app_time_map_st1') list for all active applications between start & stop */
+       GSList *display_list;           /**< list of nodes of 'bm_display_st' for next consecutive sessions */
+} bm_display_st;
+
+/*
+ * structure for "cpu" feature data.
+ */
+typedef struct {
+       uint cpuTicks;                  /**< CPU Usage during the session */
+       uint cpuTimeBackground;         /**< Time spent in Background State average operating frequency will be used to calculate the power */
+       uint cpuTimeForeground;         /**< Time spent in Foreground state average operating frequency will be used to calculate the power */
+       GSList *atm_list;               /**< application time map('app_time_map_st1') list for all active applications between start & stop */
+       GSList *cpu_data_list;          /**< list of nodes of 'bm_cpu_st' for next consecutive sessions */
+} bm_cpu_st;
+
+/*
+ * structure for "device_network" feature data.
+ */
+typedef struct {
+       uint time_level_0;              /**< Total time in milliseconds during which signal_strength was on level 0 */
+       uint time_level_1;              /**< Total time in milliseconds during which signal_strength was on level 1 */
+       uint time_level_2;              /**< Total time in milliseconds during which signal_strength was on level 2 */
+       uint time_level_3;              /**< Total time in milliseconds during which signal_strength was on level 3 */
+       uint time_level_4;              /**< Total time in milliseconds during which signal_strength was on level 4 */
+       uint time_level_5;              /**< Total time in milliseconds during which signal_strength was on level 5 */
+       uint time_level_6;              /**< Total time in milliseconds during which signal_strength was on level 6 */
+       GSList *atm_list;               /**< application time map('app_time_map_st2') list for all active applications between start & stop */
+       GSList *dn_data_list;           /**< list of nodes of 'bm_device_network_st' for next consecutive sessions */
+} bm_device_network_st;
+
+/*
+ * structure for "Wi-Fi" feature data.
+ */
+typedef struct {
+       uint time_level_0;              /**< Total time in milliseconds during which rssi was on level 0 */
+       uint time_level_1;              /**< Total time in milliseconds during which rssi was on level 1 */
+       uint time_level_2;              /**< Total time in milliseconds during which rssi was on level 2 */
+       uint time_level_3;              /**< Total time in milliseconds during which rssi was on level 3 */
+       uint time_level_4;              /**< Total time in milliseconds during which rssi was on level 4 */
+       time_t startTime;               /**< start time of feature data collection session */
+       time_t endTime;                 /**< end time of feature data collection session */
+       uint scanTime;                  /**< Total time spent by the resource in (scan) mode during the session */
+       GSList *atm_list;               /**< application time map('app_time_map_st2') list for all active applications between start & stop */
+       GSList *wifi_data_list;         /**< list of nodes of 'bm_wifi_st' for next consecutive sessions */
+} bm_wifi_st;
+
+/*
+ * structure for "bluetooth" feature data.
+ */
+typedef struct {
+       uint scanTime;                  /**< Total time spent by the resource in (scan) mode during the session */
+       uint connectedTime;             /**< Total time spent by the resource in (connected) mode during the session */
+       time_t startTime;               /**< start time of feature data collection session */
+       time_t stopTime;                /**< stop time of feature data collection session */
+       GSList *atm_list;               /**< application time map('app_time_map_st2') list for all active applications between start & stop */
+       GSList *bt_data_list;           /**< list of nodes of 'bm_bluetooth_st' for next consecutive sessions */
+} bm_bluetooth_st;
+
+/*
+ * structure for "gps" feature data.
+ */
+typedef struct {
+       time_t connStartTime;           /**< start time of feature data collection session */
+       time_t connStopTime;            /**< stop time of feature data collection session */
+       GSList *atm_list;               /**< application time map('app_time_map_st1') list for all active applications between start & stop */
+       GSList *gps_data_list;          /**< list of nodes of 'bm_gps_st' for next consecutive sessions */
+} bm_gps_st;
+
+/*
+ * structure for "hrm-sensor" feature data.
+ */
+typedef struct {
+       uint sensor_hub_wakeup_time;    /**< wake-up time of sensor-hub */
+       uint sensor_hub_wakeup_count;   /**< wake-up count of sensor-hub */
+       uint sensor_wakeup_time;        /**< sensor wake-up time */
+       uint sensor_wakeup_count;       /**< sensor wake-up count */
+       GSList *atm_list;               /**< application time map('app_time_map_st1') list for all active applications between start & stop */
+       GSList *hrm_data_list;          /**< list of nodes of 'bm_hrm_sensor_st' for next consecutive sessions */
+} bm_hrm_sensor_st;
+
+/*
+ * structure for "battery" feature data.
+ */
+typedef struct {
+       uint battery_percent;           /**< Battery percent */
+       uint is_charging;               /**< State of charging */
+} bm_battery_st;
+
+/* ========================================================================== */
+
+/*
+ * battery-monitor data handle
+ */
+typedef void *bm_data_h;
+
+/*
+ * Callback to be invoked when service is going OFF
+ *
+ * [Param] handle - handle through which service will provide data
+ *        to battery-monitor service daemon.
+ *
+ * [Param] result - the result of callback operation
+ */
+typedef void (*bm_data_callback)(bm_plugin_error_e result, bm_data_h *handle);
+
+/*
+ * battery-monitor callback structure
+ */
+typedef struct {
+       bm_data_callback provide_feature_data;
+} bm_callbacks_st;
+
+/*
+ * structure for plugin API's
+ */
+typedef struct {
+       /*
+       * [Sync API] To initialize plugin
+       */
+       int (*init)(const bm_callbacks_st *callbacks);
+       /*
+       * [Sync API] To deinitialize plugin
+       */
+       int (*deinit)(void);
+       /*
+       * [Sync API] To get feature data from plugin
+       *
+       * [Param] handle - battery-monitor data handle through which plugin
+       *       will provide data to the manager. This handle will be passed
+       *       as reference, plugin will assign a valid object which will be
+       *       set free by the manager; battery-monitor manager will typecast
+       *       the object provided through handle for each plugin and will use
+       *       it for further calculation.
+       *
+       * [Param] data_type - type of data like - BLE, WiFi, CPU etc.
+       */
+       int (*get_feature_data)(bm_data_h *handle, bm_plugin_data_type_e data_type);
+} bm_api_st;
+
+/*
+ * structure for plugin information
+ */
+typedef struct {
+       /*
+       * Respective "identifier" of Plugin
+       */
+       int id;
+       /*
+       * Respective "name" of Plugin
+       */
+       const char *name;
+       /*
+       * Respective "author" of Plugin
+       */
+       const char *author;
+       /*
+       * Respective "version" of Plugin
+       */
+       const char *version;
+       /*
+       * Plugin init function
+       */
+       int (*init)(bm_api_st **api);
+       /*
+       * Plugin de-init function
+       */
+       int (*deinit)(void);
+} bm_plugin_st;
+
+
+/*
+ *  macro for adding plugin
+ */
+#define BM_PLUGIN_ADD(id, name, author, version, init, deinit) \
+               __attribute__ ((visibility("default"))) bm_plugin_st bm_plugin = \
+               { id, name, author, version, init, deinit };
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BM_PLUGIN_INTERFACE_H__ */
diff --git a/plugin/wifi/include/bm_wifi_plugin.h b/plugin/wifi/include/bm_wifi_plugin.h
new file mode 100755 (executable)
index 0000000..c391db6
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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 __BM_WIFI_PLUGIN_H__
+#define __BM_WIFI_PLUGIN_H__
+
+#include <gmodule.h>
+#include <time.h>
+#include <bm_plugin_interface.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * battery-monitor data handle
+ */
+typedef void *bm_data_h;
+
+#ifdef ENABLE /* these structures are defined in common plugin interface */
+/*
+ * alias for long data types
+ */
+typedef unsigned int           uint;
+typedef unsigned char          uchar;
+
+/*
+ * structure for application time map.
+ */
+typedef struct {
+       char *app_id;           /* application id */
+       uint time;              /* total duration for which application as active */
+       uint rx;                /* Total Bytes received by the application during runtime */
+       uint tx;                /* Total Bytes transmitted by the application during runtime */
+} app_time_map_st2;
+
+/*
+ * structure for "Wi-Fi" feature data.
+ */
+typedef struct {
+       uint time_level_0;              /**< Total time in milliseconds during which rssi was on level 0 */
+       uint time_level_1;              /**< Total time in milliseconds during which rssi was on level 1 */
+       uint time_level_2;              /**< Total time in milliseconds during which rssi was on level 2 */
+       uint time_level_3;              /**< Total time in milliseconds during which rssi was on level 3 */
+       uint time_level_4;              /**< Total time in milliseconds during which rssi was on level 4 */
+       time_t startTime;               /**< start time of feature data collection session */
+       time_t endTime;                 /**< end time of feature data collection session */
+       uint scanTime;                  /**< Total time spent by the resource in (scan) mode during the session */
+       GSList *atm_list;               /**< application time map('app_time_map_st2') list for all active applications between start & stop */
+       GSList *wifi_data_list;         /**< list of nodes of 'bm_wifi_st' for next consecutive sessions */
+} bm_wifi_st;
+#endif /* end ENABLE */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BM_WIFI_PLUGIN_H__ */
+
+
diff --git a/plugin/wifi/include/bm_wifi_util.h b/plugin/wifi/include/bm_wifi_util.h
new file mode 100755 (executable)
index 0000000..218e65e
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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 __BM_WIFI_UTIL_H__
+#define __BM_WIFI_UTIL_H__
+
+#include <stdlib.h>
+#include <tizen.h>
+#include <glib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define NAME           "wifi_plugin"
+#define AUTHOR         "SAMSUNG"
+#define VERSION                "0.1"
+
+#define BUFFER_SZ      200
+
+#define BMP_RETURN_VAL(eval, expr, ret_val, X) \
+       if (!(eval)) { \
+               expr; \
+               return ret_val; \
+       } else {}
+
+#define BMP_SNPRINTF(dest, size, format, arg...) \
+       do { \
+               snprintf(dest, size-1, format, ##arg); \
+       } while (0)
+
+#define BMP_MEMSET(dest, value, size) \
+       do { \
+               memset(dest, value, size); \
+       } while (0)
+
+#define BMP_CATCH_ERROR(eval, expr, error_val, X) \
+       if (!(eval)) { \
+               expr; \
+               error_code = (error_val); \
+               goto CATCH; \
+       } else {}
+
+#define BMP_CATCH_ERROR_P(eval, expr, error_val, X) \
+       if (!(eval)) { \
+               expr; \
+               *error_code = (error_val); \
+               goto CATCH; \
+       } else { }
+
+#define BMP_FREE(ptr)ptr \
+       if (ptr != NULL) { \
+               free(ptr); \
+               ptr = NULL; \
+       } \
+
+#define BMP_GFREE(ptr)ptr \
+       if (ptr != NULL) { \
+               g_free(ptr); \
+               ptr = NULL; \
+       } \
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BM_WIFI_UTIL_H__ */
+
+
diff --git a/plugin/wifi/src/bm_wifi_plugin.c b/plugin/wifi/src/bm_wifi_plugin.c
new file mode 100755 (executable)
index 0000000..28a928e
--- /dev/null
@@ -0,0 +1,314 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <glib.h>
+#include <pkgmgr-info.h>
+#include <aul.h>
+#include <gio/gio.h>
+#include <glib-object.h>
+
+#include "bm_wifi_plugin.h"
+#include "bm_wifi_util.h"
+#include "bm_log.h"
+
+#define NETCONFIG_SERVICE                              "net.netconfig"
+#define NETCONFIG_NETWORK_PATH                 "/net/netconfig/network"
+#define NETCONFIG_BATTERY_INTERFACE            "net.netconfig.battery"
+
+#define BATTERY_ATM_LIST            "atm_list"
+#define BATTERY_ATM_APP_ID          "atm_app_id"
+#define BATTERY_ATM_RX              "atm_rx"
+#define BATTERY_ATM_TX              "atm_tx"
+
+#define BATTERY_WIFI_TIME_LEVEL_0   "time_level_0"
+#define BATTERY_WIFI_TIME_LEVEL_1   "time_level_1"
+#define BATTERY_WIFI_TIME_LEVEL_2   "time_level_2"
+#define BATTERY_WIFI_TIME_LEVEL_3   "time_level_3"
+#define BATTERY_WIFI_TIME_LEVEL_4   "time_level_4"
+#define BATTERY_WIFI_START_TIME     "start_time"
+#define BATTERY_WIFI_END_TIME       "end_time"
+#define BATTERY_WIFI_SCAN_TIME      "scan_time"
+
+#define BM_WIFI_DBUS_REPLY_TIMEOUT        (15 * 1000)
+
+typedef void (*bm_dbus_dict_cb) (const char *key, GVariant *value, void *user_data);
+
+static GVariant *__invoke_dbus_method(const char *dest, const char *path,
+               const char *interface_name, const char *method, GVariant *params)
+{
+
+       GError *error = NULL;
+       GVariant *reply = NULL;
+       GDBusConnection *connection;
+
+       connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+       if (connection == NULL) {
+               _E("failed to get dbus connection");
+               return reply;
+       }
+
+       reply = g_dbus_connection_call_sync(
+                       connection,
+                       dest,
+                       path,
+                       interface_name,
+                       method,
+                       params,
+                       NULL,
+                       G_DBUS_CALL_FLAGS_NONE,
+                       BM_WIFI_DBUS_REPLY_TIMEOUT,
+                       NULL,
+                       &error);
+
+       g_object_unref(connection);
+
+       if (reply == NULL) {
+               if (error != NULL) {
+                       _E("failed to call dbus method, error [%d: %s]", error->code, error->message);
+                       g_error_free(error);
+               } else {
+                       _E("failed to call dbus method");
+               }
+
+               return NULL;
+       }
+
+       return reply;
+}
+
+static void __bm_dbus_dictionary_foreach(GVariantIter *iter,
+               bm_dbus_dict_cb cb, void *user_data)
+{
+       char *key = NULL;
+       GVariant *value = NULL;
+
+       if (!cb)
+               return;
+
+       while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
+               if (key)
+                       cb(key, value, user_data);
+       }
+}
+
+static void __bm_extract_app(const char *key, GVariant *value, void *user_data)
+{
+       app_time_map_st2 *app_data = (app_time_map_st2 *)user_data;
+
+       if (app_data == NULL)
+               return;
+
+       if (!g_strcmp0(key, BATTERY_ATM_APP_ID)) {
+               const char *app_id = g_variant_get_string(value, NULL);
+               app_data->app_id = g_strdup(app_id);
+       } else if (!g_strcmp0(key, BATTERY_ATM_RX)) {
+               app_data->rx = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_ATM_TX)) {
+               app_data->tx = g_variant_get_uint32(value);
+       }
+}
+
+static void __bm_extract_wifi(const char *key, GVariant *value, void *user_data)
+{
+       bm_wifi_st *wifi_data = (bm_wifi_st *)user_data;
+
+       if (wifi_data == NULL)
+               return;
+
+       if (!g_strcmp0(key, BATTERY_WIFI_TIME_LEVEL_0)) {
+               wifi_data->time_level_0 = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_WIFI_TIME_LEVEL_1)) {
+               wifi_data->time_level_1 = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_WIFI_TIME_LEVEL_2)) {
+               wifi_data->time_level_2 = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_WIFI_TIME_LEVEL_3)) {
+               wifi_data->time_level_3 = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_WIFI_TIME_LEVEL_4)) {
+               wifi_data->time_level_4 = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_WIFI_START_TIME)) {
+               wifi_data->startTime = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_WIFI_END_TIME)) {
+               wifi_data->endTime = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_WIFI_SCAN_TIME)) {
+               wifi_data->scanTime = g_variant_get_uint32(value);
+       } else if (!g_strcmp0(key, BATTERY_ATM_LIST)) {
+               GVariant *list_value = NULL;
+               GVariantIter *iter = NULL;
+               GVariantIter *iter_row = NULL;
+
+               g_variant_get(value, "v", &list_value);
+               g_variant_get(list_value, "aa{sv}", &iter);
+
+               while (g_variant_iter_next(iter, "a{sv}", &iter_row)) {
+                       app_time_map_st2 *app_data = g_try_malloc0(sizeof(app_time_map_st2));
+                       if (app_data != NULL) {
+                               memset(app_data, 0, sizeof(app_time_map_st2));
+
+                               __bm_dbus_dictionary_foreach(iter_row, __bm_extract_app, app_data);
+
+                               wifi_data->atm_list = g_slist_append(wifi_data->atm_list, app_data);
+                       }
+
+                       g_variant_iter_free(iter_row);
+               }
+
+               g_variant_iter_free(iter);
+       }
+}
+
+static int __bm_wifi_get_list(bm_data_h *handle)
+{
+       GVariant *message = NULL;
+       GVariantIter *iter = NULL;
+       GVariantIter *iter_row = NULL;
+       bm_wifi_st *wifi_list = NULL;
+       bm_wifi_st *curr_wifi_data = NULL;
+
+       message = __invoke_dbus_method(NETCONFIG_SERVICE,
+               NETCONFIG_NETWORK_PATH, NETCONFIG_BATTERY_INTERFACE,
+               "GetBatteryWifiList", NULL);
+
+       if (message == NULL)
+               return BM_PLUGIN_ERROR_NO_DATA;
+
+       g_variant_get(message, "(aa{sv})", &iter);
+
+       while (g_variant_iter_next(iter, "a{sv}", &iter_row)) {
+               bm_wifi_st *wifi_data = g_try_malloc0(sizeof(bm_wifi_st));
+               if (wifi_data != NULL) {
+                       memset(wifi_data, 0, sizeof(bm_wifi_st));
+
+                       __bm_dbus_dictionary_foreach(iter_row, __bm_extract_wifi, wifi_data);
+
+                       if (wifi_list == NULL) {
+                               wifi_list = wifi_data;
+                               curr_wifi_data = wifi_data;
+                       } else {
+                               curr_wifi_data->wifi_data_list = g_slist_append(curr_wifi_data->wifi_data_list, wifi_data);
+                               curr_wifi_data = wifi_data;
+                       }
+               }
+               g_variant_iter_free(iter_row);
+       }
+
+       g_variant_iter_free(iter);
+       g_variant_unref(message);
+
+       *handle = wifi_list;
+
+       return BM_PLUGIN_ERROR_NONE;
+}
+
+static void __bm_wifi_print_list(bm_data_h handle)
+{
+       bm_wifi_st *wifi_data = (bm_wifi_st *)handle;
+
+       _D("===============================================================");
+
+       while (wifi_data != NULL) {
+               GSList *atm_list = NULL;
+               GSList *data_list = NULL;
+
+               _D("wifi start[%ld] end[%ld] scan[%d] rssi[%d/%d/%d/%d/%d]",
+                       wifi_data->startTime, wifi_data->endTime, wifi_data->scanTime,
+                       wifi_data->time_level_0, wifi_data->time_level_1, wifi_data->time_level_2,
+                       wifi_data->time_level_3, wifi_data->time_level_4);
+
+               for (atm_list = wifi_data->atm_list; atm_list != NULL; atm_list = atm_list->next) {
+                       app_time_map_st2 *app_data = (app_time_map_st2 *)atm_list->data;
+                       _D("app_id[%s] rx[%d] tx[%d]", app_data->app_id, app_data->rx, app_data->tx);
+               }
+
+               _D("===============================================================");
+
+               if (wifi_data->wifi_data_list == NULL)
+                       break;
+
+               data_list = wifi_data->wifi_data_list;
+               wifi_data = (bm_wifi_st *)data_list->data;
+       }
+}
+
+int init(const bm_callbacks_st *callbacks)
+{
+       ENTER;
+
+       if (!callbacks) {
+               _E("invalid input param");
+               return BM_PLUGIN_ERROR_INVALID_PARAMETER;
+       }
+
+/*     Initialize the necessary paramters and callbacks if required */
+
+       EXIT;
+       return BM_PLUGIN_ERROR_NONE;
+}
+
+int deinit()
+{
+       ENTER;
+
+/*     De-initialize callbacks & parameters */
+
+       EXIT;
+       return BM_PLUGIN_ERROR_NONE;
+}
+
+int get_feature_data(bm_data_h *handle, bm_plugin_data_type_e type)
+{
+       ENTER;
+
+       int ret = 0;
+
+       ret = __bm_wifi_get_list(handle);
+       if (ret == 0) {
+               __bm_wifi_print_list(*handle);
+               EXIT;
+               return BM_PLUGIN_ERROR_NONE;
+       } else {
+               EXIT;
+               return BM_PLUGIN_ERROR_NO_DATA;
+       }
+}
+
+static bm_api_st bm_wifi_api = {
+       .init = init,
+       .deinit = deinit,
+       .get_feature_data = get_feature_data
+};
+
+int plugin_init(bm_api_st ** api)
+{
+       ENTER;
+
+       *api = &bm_wifi_api;
+
+       EXIT;
+       return BM_PLUGIN_ERROR_NONE;
+}
+
+int plugin_deinit(void)
+{
+       ENTER;
+
+       EXIT;
+       return BM_PLUGIN_ERROR_NONE;
+}
+
+BM_PLUGIN_ADD(BM_PLUGIN_ID_WIFI, NAME, AUTHOR, VERSION, plugin_init, plugin_deinit);