--- /dev/null
+/*
+ * 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 "bm_listeners.h"
+#include "bm_dbg.h"
+#include <bm_common.h>
+#include <bd_history_item.h>
+#include <vconf.h>
+#include <pthread.h>
+
+
+
+wifi_manager_h wifi = NULL;
+static bool display_on;
+static int brightness_val = 0;
+static int brightness_flag = 0;
+static history_item_s *data_obj = NULL;
+
+static int listener_hsp =0;
+static int bt_hsp=0;
+static int bt_connected_hsp=0;
+static int bt_scan_hsp=0;
+static int bt_flag =0;
+static int wifi_flag =0;
+static int wifi_hsp=0;
+static int wifi_scan_hsp=0;
+static int wifi_connected_hsp=0;
+static int display_hsp=0;
+static event_pool events_buf[10];
+static int buff_count = 0;
+static pthread_cond_t fill = PTHREAD_COND_INITIALIZER;
+static pthread_cond_t empty = PTHREAD_COND_INITIALIZER;
+static pthread_mutex_t battery_lister_mutex = PTHREAD_MUTEX_INITIALIZER;
+static int fill_ptr = 0;
+static int use_ptr = 0;
+
+
+
+
+static int bm_battery_level_listener()
+{
+ ENTER;
+ int ret_val = BATTERY_MONITOR_ERROR_NONE;
+
+ int battery_level;
+ ret_val = device_battery_get_percent(&battery_level);
+ if(ret_val != BATTERY_MONITOR_ERROR_NONE)
+ {
+ _ERR("Failed to get battery percentage (%d)", ret_val);
+ return -1;
+ }
+
+ EXIT;
+ return battery_level;
+}
+
+
+
+
+static int bm_battery_status_listener()
+{
+ ENTER;
+ int ret_val = BATTERY_MONITOR_ERROR_NONE;
+
+ device_battery_status_e battery_status;
+ ret_val = device_battery_get_status(&battery_status);
+ if(ret_val != BATTERY_MONITOR_ERROR_NONE)
+ {
+ _ERR("Failed to get battery status (%d)", ret_val);
+ return -1;
+ }
+
+ BD_BSTA_IDX bm_status;
+
+ if (battery_status == DEVICE_BATTERY_STATUS_CHARGING)
+ bm_status = BD_BSTA_CHAR;
+ else if (battery_status == DEVICE_BATTERY_STATUS_DISCHARGING)
+ bm_status = BD_BSTA_DISC;
+ else if (battery_status == DEVICE_BATTERY_STATUS_FULL)
+ bm_status = BD_BSTA_FULL;
+ else if (battery_status == DEVICE_BATTERY_STATUS_NOT_CHARGING)
+ bm_status = BD_BSTA_NCHA;
+ else
+ return BD_BSTA_NONE;
+
+ EXIT;
+ return bm_status;
+}
+
+
+
+static int bm_battery_health_listener()
+{
+ ENTER;
+ int ret_val = BATTERY_MONITOR_ERROR_NONE;
+
+ device_battery_health_e health;
+ BD_BHLT_IDX bm_health;
+ ret_val = device_battery_get_health(&health);
+ if(ret_val != BATTERY_MONITOR_ERROR_NONE)
+ {
+ _ERR("Failed to get battery health (%d)", ret_val);
+ return -1;
+ }
+
+
+ if (health == DEVICE_BATTERY_HEALTH_GOOD)
+ bm_health = BD_BHLT_GOOD;
+ else if (health == DEVICE_BATTERY_HEALTH_COLD)
+ bm_health = BD_BHLT_CLD;
+ else if (health == DEVICE_BATTERY_HEALTH_DEAD)
+ bm_health = BD_BHLT_DEAD;
+ else if (health == DEVICE_BATTERY_HEALTH_OVER_HEAT)
+ bm_health = BD_BHLT_OVRH;
+ else if (health == DEVICE_BATTERY_HEALTH_OVER_VOLTAGE)
+ bm_health = BD_BHLT_OVRV;
+ else
+ return BD_BHLT_NONE;
+
+
+ EXIT;
+ return bm_health;
+}
+
+
+
+static int bm_battery_plugtype_listener()
+{
+ ENTER;
+ int ret_val = BATTERY_MONITOR_ERROR_NONE;
+
+ device_battery_power_source_e source;
+ ret_val = device_battery_get_power_source(&source);
+ if(ret_val != BATTERY_MONITOR_ERROR_NONE)
+ {
+ _ERR("Failed to get battery percentage (%d)", ret_val);
+ return -1;
+ }
+
+
+ BD_BPLT_IDX bm_plugtype =BD_BPLT_NONE;
+ if (source == DEVICE_BATTERY_POWER_SOURCE_AC)
+ bm_plugtype = BD_BPLT_AC;
+ else if (source == DEVICE_BATTERY_POWER_SOURCE_USB)
+ bm_plugtype = BD_BPLT_USB;
+ else if (source == DEVICE_BATTERY_POWER_SOURCE_WIRELESS)
+ bm_plugtype = BD_BPLT_WL;
+ else if (source == DEVICE_BATTERY_POWER_SOURCE_NONE)
+ bm_plugtype = BD_BPLT_NONE;
+
+ EXIT;
+ return bm_plugtype;
+}
+
+static int bm_battery_voltage_temperature_charge_listener(history_item_s *ts)
+{
+ ENTER;
+
+ device_battery_property_e property;
+ int val,ret;
+
+ /* get the battery voltage*/
+
+ property = DEVICE_BATTERY_PROPERTY_VOLTAGE_NOW;
+ ret = device_battery_get_property(property, &val);
+ if (ret != BATTERY_MONITOR_ERROR_NONE) {
+ _ERR("Failed to get battery voltage (%d)", ret); //LCOV_EXCL_LINE Logs
+ return ret;
+ }
+ ts->battery_voltage = val;
+
+ /* get the battery temperature*/
+
+ property = DEVICE_BATTERY_PROPERTY_TEMPERATURE;
+ ret = device_battery_get_property(property, &val);
+ if (ret != BATTERY_MONITOR_ERROR_NONE) {
+ _ERR("Failed to get battery temperature (%d)", ret); //LCOV_EXCL_LINE Logs
+ return ret;
+ }
+ ts->battery_temperature = val;
+
+ /* get the battery charge*/
+ /* property = DEVICE_BATTERY_PROPERTY_CAPACITY;
+ ret = device_battery_get_property(property, &val);
+ if (ret != BATTERY_MONITOR_ERROR_NONE) {
+ _ERR("Failed to get battery capacity (%d)", ret); //LCOV_EXCL_LINE Logs
+ return ret;
+ }
+ _INFO("success to get battery capacity (%d)", ret);
+ */
+ ts->battery_charge = 0;
+ EXIT;
+ return BATTERY_MONITOR_ERROR_NONE;
+}
+
+
+
+
+static void fill_bt_adapter_state_changed_data(int adapter_state)
+{
+ ENTER;
+
+ _INFO("state: %s",(adapter_state == BT_ADAPTER_ENABLED) ?"ENABLED" : "DISABLED");
+ int bt_state ;
+ if(adapter_state == BT_ADAPTER_ENABLED)
+ bt_state =1;
+ else
+ bt_state=0;
+
+ _INFO("__bm_bt_adapter_state_changed_cb bt_state = %d ", bt_state);
+
+
+ if(data_obj)
+ {
+ if(adapter_state == BT_ADAPTER_ENABLED)
+ {
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_ACTIVE;
+ data_obj->event_code |= ET_FLAG_START;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ listener_hsp++;
+ bt_hsp = listener_hsp;
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = bt_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return;
+ }
+
+ data_obj->state_2 |= ST2_BLUETOOTH_ON_FLAG;
+ }
+ else
+ {
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_ACTIVE;
+ data_obj->event_code |= ET_FLAG_FINISH;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = bt_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+
+ data_obj->state_2 &= ~(ST2_BLUETOOTH_ON_FLAG);
+ }
+ }
+
+ EXIT;
+ return ;
+
+
+
+}
+
+
+
+
+
+static void fill_bt_device_discovery_state_changed_data(int discovery_state)
+{
+
+ ENTER;
+ _INFO("__bt_adapter_state_changed_cb");
+ _INFO(" discovery_state = %d", discovery_state);
+
+ if(data_obj)
+ {
+ switch (discovery_state) {
+ case BT_ADAPTER_DEVICE_DISCOVERY_STARTED:
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code = ET_CONNECTIVITY_CHANGED;
+ data_obj->event_code |= ET_FLAG_START;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ if(!bt_flag)
+ {
+ listener_hsp++;
+ bt_scan_hsp = listener_hsp;
+ bt_flag =1;
+ }
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = bt_scan_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ data_obj->state_2 |= ST2_BLUETOOTH_SCAN_FLAG;
+ break;
+ case BT_ADAPTER_DEVICE_DISCOVERY_FINISHED:
+
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code = ET_CONNECTIVITY_CHANGED;
+ data_obj->event_code |= ET_FLAG_FINISH;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = bt_scan_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ data_obj->state_2 &= ~(ST2_BLUETOOTH_SCAN_FLAG);
+ bt_flag =0;
+ break;
+ default:
+ return;
+ }
+ }
+
+ EXIT;
+
+}
+
+
+
+
+
+static void fill_bt_device_connection_state_changed_data(int connected)
+{
+ ENTER;
+
+ _INFO("__bt_device_connection_state_changed_cb [%d]", connected);
+ if (!connected)
+ {
+ _INFO("bt is disconnected");
+ }
+
+
+ if(data_obj)
+ {
+ if(connected)
+ {
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_CONNECTIVITY_CHANGED ;
+ data_obj->event_code |= ET_FLAG_START;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ listener_hsp++;
+ bt_connected_hsp = listener_hsp;
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = bt_connected_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ }
+ else
+ {
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_CONNECTIVITY_CHANGED;
+ data_obj->event_code |= ET_FLAG_FINISH;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = bt_connected_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ }
+ }
+
+ EXIT;
+}
+
+
+static void fill_wifi_device_state_data(int state)
+{
+ ENTER;
+
+ _INFO(" Wifi Device state changed callback");
+
+ if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED)
+ _INFO(", state : Activated\n");
+ else
+ _INFO(", state : Deactivated\n");
+
+ if(data_obj)
+ {
+ if(state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED)
+ {
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_FLAG_START;
+ data_obj->event_code |= ET_CONNECTIVITY_CHANGED;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ listener_hsp++;
+ wifi_hsp = listener_hsp;
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = wifi_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ data_obj->state_2 |= ST2_WIFI_ON_FLAG;
+ }
+ else
+ {
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_FLAG_FINISH;
+ data_obj->event_code |= ET_CONNECTIVITY_CHANGED;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = wifi_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ data_obj->state_2 &= ~(ST2_WIFI_ON_FLAG);
+ }
+ }
+
+
+
+ EXIT;
+}
+
+
+
+
+static void fill_wifi_scan_changed_data(int state)
+{
+ ENTER;
+
+ _INFO("Wifi Scan changed, scan state : %d\n", state);
+
+ if(data_obj)
+ {
+ if(state == WIFI_MANAGER_SCAN_STATE_SCANNING)
+ {
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_FLAG_START;
+ data_obj->event_code |= ET_CONNECTIVITY_CHANGED;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ if(!wifi_flag)
+ {
+ listener_hsp++;
+ wifi_scan_hsp = listener_hsp;
+ wifi_flag =1;
+ }
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = wifi_scan_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ data_obj->state_1 |= ST1_WIFI_SCAN_FLAG;
+ }
+ else
+ {
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_FLAG_FINISH;
+ data_obj->event_code |= ET_CONNECTIVITY_CHANGED;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = wifi_scan_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ data_obj->state_1 &= ~(ST1_WIFI_SCAN_FLAG);
+ wifi_flag =0;
+ }
+ }
+
+ EXIT;
+}
+
+
+static void fill_wifi_connection_state_changed_data(int state)
+{
+ ENTER;
+
+ _INFO("Wifi Connection changed, connection state : %d\n", state);
+
+ if(data_obj)
+ {
+ switch (state) {
+ case WIFI_MANAGER_CONNECTION_STATE_CONNECTED:
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_CONNECTIVITY_CHANGED;
+ data_obj->event_code |= ET_FLAG_START;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ _INFO("event Tag creation succeeded \n");
+ listener_hsp++;
+ wifi_connected_hsp = listener_hsp;
+ data_obj->event_tag->sp_idx = wifi_connected_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ break;
+ case WIFI_MANAGER_CONNECTION_STATE_DISCONNECTED:
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_CONNECTIVITY_CHANGED;
+ data_obj->event_code |= ET_FLAG_FINISH;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = wifi_connected_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ EXIT;
+}
+
+
+
+
+
+static void fill_device_display_change(int value)
+{
+ ENTER;
+ _INFO("device state changed succeeded\n");
+
+ switch (value) {
+ case DISPLAY_STATE_NORMAL:
+ _INFO("Display on");
+ display_on = true;
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_SCREEN_WAKE_UP;
+ data_obj->event_code |= ET_FLAG_START;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ listener_hsp++;
+ display_hsp = listener_hsp;
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = display_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ data_obj->state_1 |= ST1_SCREEN_ON_FLAG;
+ data_obj->state_1 &= ~(ST1_SCREEN_DOZE_FLAG);
+ break;
+ case DISPLAY_STATE_SCREEN_DIM:
+ _INFO("Display dim");
+ data_obj->event_code = ET_NONE;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = display_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ data_obj->state_1 |= ST1_SCREEN_DOZE_FLAG;
+ break;
+ case DISPLAY_STATE_SCREEN_OFF:
+ _INFO("Display off");
+ data_obj->event_code = ET_NONE;
+ data_obj->event_code |= ET_SCREEN_WAKE_UP;
+ data_obj->event_code |= ET_FLAG_FINISH;
+ data_obj->event_tag = NULL;
+ data_obj->event_tag = (history_tag_s *)calloc(1, sizeof(history_tag_s));
+ if(data_obj->event_tag)
+ {
+ _INFO("event Tag creation succeeded \n");
+ data_obj->event_tag->sp_idx = display_hsp;
+ }
+ else
+ {
+ _ERR(" data_obj->event_tag object creation fails ");
+ return ;
+ }
+ data_obj->state_1 &= ~(ST1_SCREEN_ON_FLAG);
+ data_obj->state_1 &= ~(ST1_SCREEN_DOZE_FLAG);
+ display_on = false;
+ break;
+ }
+
+
+ EXIT;
+}
+
+
+
+static void fill_display_brightness_change(int brightness_level)
+{
+ ENTER;
+
+ _INFO(" device brightness changed =%d ",brightness_level);
+
+
+ if(brightness_level >0 && brightness_level <=10)
+ {
+ data_obj->state_1 &= ~brightness_val;
+ brightness_val=0;
+ data_obj->state_1 |= brightness_val;
+ data_obj->event_code = ET_NONE;
+ data_obj->event_tag = NULL;
+ }
+ else if(brightness_level >10 && brightness_level <=25)
+ {
+ data_obj->state_1 &= ~brightness_val;
+ brightness_val=1;
+ data_obj->state_1 |= brightness_val;
+ data_obj->event_code = ET_NONE;
+ data_obj->event_tag = NULL;
+ }
+ else if(brightness_level >25 && brightness_level <=50)
+ {
+ data_obj->state_1 &= ~brightness_val;
+ brightness_val=2;
+ data_obj->state_1 |= brightness_val;
+ data_obj->event_code = ET_NONE;
+ data_obj->event_tag = NULL;
+ }
+ else if(brightness_level >50 && brightness_level <=75)
+ {
+ data_obj->state_1 &= ~brightness_val;
+ brightness_val=3;
+ data_obj->state_1 |= brightness_val;
+ data_obj->event_code = ET_NONE;
+ data_obj->event_tag = NULL;
+ }
+ else if(brightness_level >75 && brightness_level <=100)
+ {
+ data_obj->state_1 &= ~brightness_val;
+ brightness_val=4;
+ data_obj->state_1 |= brightness_val;
+ data_obj->event_code = ET_NONE;
+ data_obj->event_tag = NULL;
+ }
+
+
+ EXIT;
+}
+
+
+
+
+
+static void bm_create_event_data(int type,int val)
+{
+ ENTER;
+ if(!data_obj)
+ return;
+
+
+ switch(type) {
+ case LISTEN_BT_STATE:
+ fill_bt_adapter_state_changed_data(val);
+ break;
+ case LISTEN_BT_CONNECTION_STATE:
+ fill_bt_device_connection_state_changed_data(val);
+ break;
+ case LISTEN_BT_SCAN_STATE:
+ fill_bt_device_discovery_state_changed_data(val);
+ break;
+ case LISTEN_WIFI_STATE:
+ fill_wifi_device_state_data(val);
+ break;
+ case LISTEN_WIFI_CONNECTION_STATE:
+ fill_wifi_connection_state_changed_data(val);
+ break;
+ case LISTEN_WIFI_SCAN_STATE:
+ fill_wifi_scan_changed_data(val);
+ break;
+ case LISTEN_DEVICE_DISPLAY_STATE:
+ fill_device_display_change(val);
+ break;
+ case LISTEN_BRIGHTNESS_CHANGE:
+ fill_display_brightness_change(val);
+ break;
+ default:
+ return;
+ }
+
+
+ data_obj->cmd_s = CM_UPD;
+ data_obj->time_s = 0;
+ data_obj->time_current = 0;
+
+
+ data_obj->battery_level = bm_battery_level_listener();
+ if(data_obj->battery_level < 0)
+ {
+ _ERR("Failed to get ts->battery_level = %d ", data_obj->battery_level);
+ return;
+ }
+
+ data_obj->battery_status = bm_battery_status_listener();
+ if(data_obj->battery_status < 0)
+ {
+ _ERR("Failed to get ts->battery_status = %d ", data_obj->battery_status);
+ return;
+ }
+
+ data_obj->battery_health = bm_battery_health_listener();
+ if(data_obj->battery_health < 0)
+ {
+ _ERR("Failed to get ts->battery_health = %d ", data_obj->battery_health);
+ return;
+ }
+
+ data_obj->battery_plugtype = bm_battery_plugtype_listener();
+ if(data_obj->battery_plugtype < 0)
+ {
+ _ERR("Failed to get ts->battery_plugtype = %d ", data_obj->battery_plugtype);
+ return;
+ }
+
+ _INFO("battery_level = %d , battery_status = %d , battery_health = %d , battery_plugtype = %d ", data_obj->battery_level , data_obj->battery_status ,data_obj->battery_health, data_obj->battery_plugtype);
+
+ int ret = bm_battery_voltage_temperature_charge_listener(data_obj);
+ if(ret != BATTERY_MONITOR_ERROR_NONE)
+ {
+ _ERR("bm_battery_voltage_temperature_charge_listener failed ");
+ return;
+ }
+ else
+ _INFO("battery_voltage = %d ,battery_temperature = %d ,battery_charge = %d ", data_obj->battery_voltage , data_obj->battery_temperature ,data_obj->battery_charge);
+
+ if(data_obj->event_tag){
+ data_obj->event_tag->uid = 1000 ;
+ data_obj->event_tag->string_info = g_string_sized_new(30);
+ if(!data_obj->event_tag->string_info)
+ {
+ _ERR("memory allocation failed ");
+ return;
+ }
+ g_string_append(data_obj->event_tag->string_info, "Tizen");
+ _DBG("Data Value of listener event uid : %d ", data_obj->event_tag->uid);
+ _DBG("Data Value of listener event string : %s , sp index =%d", data_obj->event_tag->string_info->str,data_obj->event_tag->sp_idx);
+ }
+
+ data_obj->wakelock_tag = NULL;
+ data_obj->wakereason_tag = NULL;
+
+
+ if(bd_store_history_item(data_obj) !=BATTERY_MONITOR_ERROR_NONE)
+ {
+ _ERR("Failed to store history items ");
+ return;
+ }
+
+ EXIT;
+
+}
+
+
+
+
+void* event_producer(void *data)
+{
+ ENTER;
+ event_pool *event = (event_pool*) data;
+ if(event == NULL)
+ return NULL;
+
+ pthread_mutex_lock(&battery_lister_mutex);
+ while (buff_count == MAX_BUFF)
+ pthread_cond_wait(&empty, &battery_lister_mutex);
+
+ events_buf[fill_ptr].type = event->type;
+ events_buf[fill_ptr].val = event->val;
+ _INFO("type: %d and val = %d", events_buf[fill_ptr].type , events_buf[fill_ptr].val);
+ fill_ptr = (fill_ptr + 1) % MAX_BUFF;
+ buff_count++;
+
+ pthread_cond_signal(&fill);
+ pthread_mutex_unlock(&battery_lister_mutex);
+
+ pthread_exit(NULL);
+ BM_FREE(event);
+ EXIT;
+ return NULL;
+
+}
+
+
+
+
+void __bm_bt_adapter_state_changed_cb(int result,
+ bt_adapter_state_e adapter_state, void *user_data)
+{
+ ENTER;
+ _INFO("__bt_adapter_state_changed_cb");
+ _INFO("result: %d", result);
+ _INFO("state: %s",(adapter_state == BT_ADAPTER_ENABLED) ?"ENABLED" : "DISABLED");
+ int bt_state ;
+ if(adapter_state == BT_ADAPTER_ENABLED)
+ bt_state =1;
+ else
+ bt_state=0;
+
+ _INFO("__bm_bt_adapter_state_changed_cb bt_state = %d ", bt_state);
+ _INFO("buff_count: %d and fill_ptr = %d", buff_count , fill_ptr);
+
+ event_pool *bt_event = (event_pool *)calloc(1, sizeof(event_pool));
+ if(!bt_event)
+ {
+ _ERR("Failed to allocated memory ");
+ return;
+ }
+ bt_event->type = LISTEN_BT_STATE;
+ bt_event->val = bt_state;
+
+ pthread_t producer ;
+ if(pthread_create(&producer, NULL, event_producer, bt_event))
+ {
+ _ERR("Failed to pthread_create.");
+ return;
+ }
+
+
+ EXIT;
+}
+
+
+
+
+void __bm_bt_device_connection_state_changed_cb(bool connected,
+ bt_device_connection_info_s *conn_info, void *user_data)
+{
+ ENTER;
+
+ _INFO("__bt_device_connection_state_changed_cb [%d]", connected);
+ _INFO("address [%s]", conn_info->remote_address);
+ _INFO("link type [%d]", conn_info->link);
+ if (!connected)
+ {
+ _INFO("disconnection reason [%d]", conn_info->disconn_reason);
+ }
+
+
+ event_pool *bt_event = (event_pool *)calloc(1, sizeof(event_pool));
+ if(!bt_event)
+ {
+ _ERR("Failed to allocated memory ");
+ return;
+ }
+ bt_event->type = LISTEN_BT_CONNECTION_STATE;
+ bt_event->val = connected;
+
+ pthread_t producer ;
+ if(pthread_create(&producer, NULL, event_producer, bt_event))
+ {
+ _ERR("Failed to pthread_create.");
+ return;
+ }
+
+
+ EXIT;
+}
+
+
+
+
+
+static void __bm_bt_device_discovery_state_changed_cb(
+ int result, bt_adapter_device_discovery_state_e discovery_state,
+ bt_adapter_device_discovery_info_s *discovery_info,
+ void *user_data)
+{
+
+ ENTER;
+ _INFO("__bt_adapter_state_changed_cb");
+ _INFO("result: %d , discovery_state = %d", result, discovery_state);
+ if(discovery_state > 1)
+ {
+ _INFO(" This state will be handled later");
+ return;
+ }
+
+ event_pool *bt_event = (event_pool *)calloc(1, sizeof(event_pool));
+ if(!bt_event)
+ {
+ _ERR("Failed to allocated memory ");
+ return;
+ }
+ bt_event->type = LISTEN_BT_SCAN_STATE;
+ bt_event->val = discovery_state;
+ pthread_t producer ;
+ if(pthread_create(&producer, NULL, event_producer, bt_event))
+ {
+ _ERR("Failed to pthread_create.");
+ return;
+ }
+
+ EXIT;
+}
+
+
+
+static void __bm_wifi_device_state_callback(wifi_manager_device_state_e state, void* user_data)
+{
+ ENTER;
+
+ _INFO("Wifi Device state changed callback");
+
+ if (state == WIFI_MANAGER_DEVICE_STATE_ACTIVATED)
+ _INFO(", state : Activated\n");
+ else
+ _INFO(", state : Deactivated\n");
+
+ event_pool *wifi_event = (event_pool *)calloc(1, sizeof(event_pool));
+ if(!wifi_event)
+ {
+ _ERR("Failed to allocated memory ");
+ return;
+ }
+ wifi_event->type = LISTEN_WIFI_STATE;
+ wifi_event->val = state;
+
+ pthread_t producer ;
+ if(pthread_create(&producer, NULL, event_producer, wifi_event))
+ {
+ _ERR("Failed to pthread_create.");
+ return;
+ }
+
+
+
+ EXIT;
+}
+
+
+
+
+
+static void __bm_wifi_scan_changed_callback(wifi_manager_scan_state_e state, void* user_data)
+{
+ ENTER;
+
+ _INFO("Wifi Scan changed, scan state : %d\n", state);
+
+ event_pool *wifi_event = (event_pool *)calloc(1, sizeof(event_pool));
+ if(!wifi_event)
+ {
+ _ERR("Failed to allocated memory ");
+ return;
+ }
+ wifi_event->type = LISTEN_WIFI_SCAN_STATE;
+ wifi_event->val = state;
+
+ pthread_t producer ;
+ if(pthread_create(&producer, NULL, event_producer, wifi_event))
+ {
+ _ERR("Failed to pthread_create.");
+ return;
+ }
+
+ EXIT;
+}
+
+
+
+static void __bm_wifi_connection_state_changed_callback(wifi_manager_connection_state_e state, wifi_manager_ap_h ap, void *user_data)
+{
+ ENTER;
+
+ _INFO("Wifi Connection changed, new connection state : %d\n", state);
+ if(state == -1 || state == 1 || state == 2)
+ {
+ _ERR("These connection states are not related ");
+ return;
+ }
+
+ event_pool *wifi_event = (event_pool *)calloc(1, sizeof(event_pool));
+ if(!wifi_event)
+ {
+ _ERR("Failed to allocated memory ");
+ return;
+ }
+ wifi_event->type = LISTEN_WIFI_CONNECTION_STATE;
+ wifi_event->val = state;
+
+ pthread_t producer ;
+ if(pthread_create(&producer, NULL, event_producer, wifi_event))
+ {
+ _ERR("Failed to pthread_create.");
+ return;
+ }
+
+ EXIT;
+}
+
+
+
+static void __bm_device_display_change(device_callback_e type, void *value, void *user_data)
+{
+ ENTER;
+ _INFO("device state changed succeeded\n");
+
+ if (type != DEVICE_CALLBACK_DISPLAY_STATE)
+ return;
+
+ display_state_e state = (display_state_e)value;
+
+ event_pool *device_event = (event_pool *)calloc(1, sizeof(event_pool));
+ if(!device_event)
+ {
+ _ERR("Failed to allocated memory ");
+ return;
+ }
+ device_event->type = LISTEN_DEVICE_DISPLAY_STATE;
+ device_event->val = state;
+
+ pthread_t producer ;
+ if(pthread_create(&producer, NULL, event_producer, device_event))
+ {
+ _ERR("Failed to pthread_create.");
+ return;
+ }
+
+ EXIT;
+}
+
+
+
+static void __bm_display_brightness_change(keynode_t *key, void* data)
+{
+ ENTER;
+ _INFO("device brightness changed succeeded\n");
+ int brightness_level =0;
+ vconf_get_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, &brightness_level);
+
+ int flag;
+ if(brightness_level >0 && brightness_level <=10)
+ {
+ flag = 0;
+ if(brightness_flag == flag)
+ return;
+ else
+ brightness_flag = flag;
+ }
+ else if(brightness_level >10 && brightness_level <=25)
+ {
+ flag = 1;
+ if(brightness_flag == flag)
+ return;
+ else
+ brightness_flag = flag;
+ }
+ else if(brightness_level >25 && brightness_level <=50)
+ {
+ flag = 2;
+ if(brightness_flag == flag)
+ return;
+ else
+ brightness_flag = flag;
+ }
+ else if(brightness_level >50 && brightness_level <=75)
+ {
+ flag = 3;
+ if(brightness_flag == flag)
+ return;
+ else
+ brightness_flag = flag;
+ }
+ else if(brightness_level >75 && brightness_level <=100)
+ {
+ flag = 4;
+ if(brightness_flag == flag)
+ return;
+ else
+ brightness_flag = flag;
+ }
+
+
+ _INFO(" device brightness changed =%d ",brightness_level);
+
+ event_pool *display_event = (event_pool *)calloc(1, sizeof(event_pool));
+ if(!display_event)
+ {
+ _ERR("Failed to allocated memory ");
+ return;
+ }
+ display_event->type = LISTEN_BRIGHTNESS_CHANGE;
+ display_event->val = brightness_level;
+
+
+ pthread_t producer ;
+ if(pthread_create(&producer, NULL, event_producer, display_event))
+ {
+ _ERR("Failed to pthread_create.");
+ return;
+ }
+
+
+ EXIT;
+}
+
+
+
+static int bm_screen_subscribe_listeners()
+{
+ ENTER;
+
+ /* Screen callbacks */
+ int ret = device_add_callback(DEVICE_CALLBACK_DISPLAY_STATE, __bm_device_display_change, NULL);
+ if (ret != BATTERY_MONITOR_ERROR_NONE) {
+ _ERR("device_add_callback failed: %s", get_error_message(ret));
+ return 1;
+ }
+
+ EXIT;
+ return 0;
+
+}
+
+
+
+static int bm_display_brightness_listeners()
+{
+ ENTER;
+
+ /* Screen callbacks */
+ int ret = 0;
+ ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, __bm_display_brightness_change, NULL);
+ if (ret != 0) {
+ _ERR("failed to register a cb key:%s err:%d","VCONFKEY_SETAPPL_LCD_BRIGHTNESS", ret);
+ return 1;
+ }
+
+ EXIT;
+ return 0;
+
+}
+
+
+
+
+static int bm_wifi_subscribe_listeners()
+{
+ ENTER;
+
+ /* Wifi callbacks */
+ int rv = wifi_manager_initialize(&wifi);
+ if (rv == WIFI_MANAGER_ERROR_NONE)
+ {
+ int ret = wifi_manager_set_device_state_changed_cb(wifi, __bm_wifi_device_state_callback, NULL);
+ if (ret == WIFI_MANAGER_ERROR_NONE) {
+ _INFO("wifi_manager_set_device_state_changed_cb success : [%p]\n",__bm_wifi_device_state_callback);
+ }
+ else
+ {
+ _ERR("wifi_manager_set_device_state_changed_cb failed");
+ }
+
+ ret = wifi_manager_set_scan_state_changed_cb(wifi, __bm_wifi_scan_changed_callback, NULL);
+ if (ret == WIFI_MANAGER_ERROR_NONE) {
+ _INFO("wifi_manager_set_scan_state_changed_cb success\n");
+ } else {
+ _ERR("wifi_manager_set_scan_state_changed_cb failed");
+ }
+
+ ret = wifi_manager_set_connection_state_changed_cb(wifi, __bm_wifi_connection_state_changed_callback, NULL);
+ if (ret == WIFI_MANAGER_ERROR_NONE) {
+ _INFO("wifi_manager_set_connection_state_changed_cb success\n");
+ } else {
+ _ERR("wifi_manager_set_scan_state_changed_cb failed");
+ }
+ } else {
+ _ERR(" Wifi init failed \n");
+ return 1;
+ }
+
+ _INFO("Wifi init succeeded\n");
+
+ EXIT;
+ return 0;
+
+}
+
+
+static int bm_bluetooth_subscribe_listeners()
+{
+ ENTER;
+
+ /* Bluetooth callbacks */
+ int rv = bt_initialize();
+
+ if (rv == BT_ERROR_NONE) {
+ int ret = bt_device_set_connection_state_changed_cb(__bm_bt_device_connection_state_changed_cb, NULL);
+ _INFO("bt_device_set_connection_state_changed_cb ret =%d \n",ret);
+
+ ret = bt_adapter_set_state_changed_cb(__bm_bt_adapter_state_changed_cb, NULL);
+ _INFO("bt_adapter_set_state_changed_cb ret =%d \n", ret);
+
+ ret = bt_adapter_set_device_discovery_state_changed_cb(__bm_bt_device_discovery_state_changed_cb, NULL);
+ _INFO("bt_adapter_set_device_discovery_state_changed_cb ret =%d \n", ret);
+
+ } else {
+ _ERR(" bluetooth init failed \n");
+ return 1;
+ }
+
+ _INFO("bluetooth init succeeded\n");
+ EXIT;
+ return 0;
+
+}
+
+
+
+
+void* event_consumer()
+{
+ ENTER;
+
+ while(1)
+ {
+ pthread_mutex_lock(&battery_lister_mutex);
+ while (buff_count == 0)
+ pthread_cond_wait(&fill, &battery_lister_mutex);
+
+ bm_create_event_data(events_buf[use_ptr].type, events_buf[use_ptr].val);
+
+ _INFO("type: %d and val = %d", events_buf[use_ptr].type,events_buf[use_ptr].val);
+ _INFO("buff_count: %d and fill_ptr = %d , use_ptr = %d", buff_count,fill_ptr,use_ptr);
+
+ use_ptr = (use_ptr + 1) % MAX_BUFF;
+ buff_count--;
+
+ pthread_cond_signal(&empty);
+ pthread_mutex_unlock(&battery_lister_mutex);
+ }
+ EXIT;
+}
+
+
+
+static int bm_initialize_history_data_obj()
+{
+ ENTER;
+
+ pthread_t consumer;
+ if(pthread_create(&consumer, NULL, event_consumer, NULL))
+ {
+ _ERR("Failed to pthread_create.");
+ return BATTERY_MONITOR_ERROR_NOT_SUPPORTED;
+ }
+
+ if(data_obj != NULL)
+ {
+ _ERR(" History data object already exits");
+ return BATTERY_MONITOR_ERROR_NONE;
+ }
+ data_obj = (history_item_s *)calloc(1, sizeof(history_item_s));
+ if(data_obj)
+ {
+ data_obj->state_1 = 0;
+ data_obj->state_2 = 0;
+ data_obj->event_code = ET_NONE;
+ data_obj->event_tag = NULL;
+ }
+ else
+ {
+ _ERR(" history_item_s data object creation fails ");
+ return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
+ }
+
+ EXIT;
+ return BATTERY_MONITOR_ERROR_NONE;
+}
+
+
+
+
+
+int bd_initialize_listeners(void)
+{
+ ENTER;
+
+
+
+ /* Initialize Hash Table */
+ if(!bm_initialize_history_data_obj()) {
+ _INFO("bm_initialize_history_data_obj succeeded \n");
+ } else {
+ _ERR("bm_initialize_history_data_obj failed \n");
+ return BATTERY_MONITOR_ERROR_OUT_OF_MEMORY;
+ }
+
+
+
+ /* Wifi callbacks */
+ if(!bm_wifi_subscribe_listeners())
+ _INFO("Wifi listeners succeeded to subscribed\n");
+ else
+ _ERR("Wifi listeners failed to subscribed\n");
+
+
+
+ /* BlueTooth callbacks */
+
+ if(!bm_bluetooth_subscribe_listeners())
+ _INFO("bluetooth listeners succeeded to subscribed\n");
+ else
+ _ERR("bluetooth listeners failed to subscribed\n");
+
+
+ /* Screen wakeup callbacks */
+ if(!bm_screen_subscribe_listeners())
+ _INFO("Display listeners succeeded to subscribed\n");
+ else
+ _ERR("Display listeners failed to subscribed\n");
+
+
+ /* Brightness change callbacks */
+ if(!bm_display_brightness_listeners())
+ _INFO("Brightness listeners succeeded to subscribed\n");
+ else
+ _ERR("Brightness listeners failed to subscribed\n");
+
+ EXIT;
+ return BATTERY_MONITOR_ERROR_NONE;
+}
+
+
+int bd_deinitialize_listeners()
+{
+ ENTER;
+
+ _INFO("de-initializing History listeners");
+
+
+ /* wifi */
+
+ /*unset the callbacks related to wifi*/
+ if (wifi_manager_unset_connection_state_changed_cb(wifi) != WIFI_MANAGER_ERROR_NONE)
+ _ERR("wifi_manager_unset_connection_state_changed_cb() failed.\n");
+ else
+ _ERR("wifi_manager_unset_connection_state_changed_cb() success.\n");
+
+ if (wifi_manager_deinitialize(wifi) != WIFI_MANAGER_ERROR_NONE)
+ _ERR("wifi_manager_deinitialize() failed.\n");
+ else
+ _ERR("wifi_manager_deinitialize() success.\n");
+
+
+ /* Bluetooth */
+
+ /* unset bluetooth device connection state changed state event callback*/
+ if (bt_device_unset_connection_state_changed_cb() != BT_ERROR_NONE)
+ _ERR("bt_device_unset_connection_state_changed_cb() failed.\n");
+ else
+ _ERR("bt_device_unset_connection_state_changed_cb() success.\n");
+
+ /* unset bluetooth adapter changed state event callback*/
+ if (bt_adapter_unset_state_changed_cb() != BT_ERROR_NONE)
+ _ERR("bt_adapter_unset_state_changed_cb() failed.\n");
+ else
+ _ERR("bt_adapter_unset_state_changed_cb() success.\n");
+
+ /* deinit bluetooth api*/
+ if (bt_deinitialize() != BT_ERROR_NONE)
+ _ERR("bt_deinitialize() failed.\n");
+ else
+ _ERR("bt_deinitialize() success.\n");
+
+
+ //Brightness
+ if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_LCD_BRIGHTNESS,__bm_display_brightness_change) < 0)
+ {
+ _ERR("failed to de-register vconf callback");
+ }
+
+ if(data_obj)
+ {
+ BM_FREE(data_obj);
+ }
+
+ EXIT;
+ return BATTERY_MONITOR_ERROR_NONE;
+
+}
+
+
+
+
+
+
+
+
+