extcon: support usage count about extcon-related devices 81/98781/2
authorKunhoon Baik <knhoon.baik@samsung.com>
Sat, 19 Nov 2016 02:55:13 +0000 (11:55 +0900)
committertaeyoung <ty317.kim@samsung.com>
Mon, 21 Nov 2016 02:46:30 +0000 (11:46 +0900)
 - earjack connection count
 - charger conneciton count
 - battery full charging count (exception)

Change-Id: Id3956708bfb373939553165bd1f7f85087721a7a
Signed-off-by: taeyoung <ty317.kim@samsung.com>
src/battery/power-supply.c
src/extcon/earjack.c
src/extcon/extcon.h
src/extcon/extcon_count.c [new file with mode: 0755]
src/extcon/extcon_count.h [new file with mode: 0755]

index 5e14169..91e9aea 100644 (file)
@@ -36,6 +36,7 @@
 #include "apps/apps.h"
 #include "power-supply.h"
 #include "battery.h"
+#include "extcon/extcon_count.h"
 
 #define BATTERY_NAME        "battery"
 #define CHARGEFULL_NAME     "Full"
@@ -497,6 +498,8 @@ static void check_online(void)
                old_online = VCONFKEY_SYSMAN_CHARGER_CONNECTED;
                vconf_set_int(VCONFKEY_SYSMAN_CHARGER_STATUS, old_online);
                power_supply_broadcast(CHARGER_STATUS_SIGNAL, old_online);
+               extcon_get_count(EXTCON_TA);
+               extcon_set_count(EXTCON_TA, 1);
                check_power_supply(old_online);
                charger_state_send_system_event(CHARGE_STATUS_CONNECTED);
                if (charge_status != old_charge_status)
@@ -613,6 +616,23 @@ static void check_capacity_status(const char *env_value)
        battery.capacity = atoi(env_value);
 }
 
+static void update_capacity_full(void)
+{
+       static int old;
+       int delta;
+
+       if (battery.online <= POWER_SUPPLY_TYPE_BATTERY ||
+           old == 0 ||
+           old >= battery.capacity) {
+               old = battery.capacity;
+               return;
+       }
+       delta = battery.capacity - old;
+       old = battery.capacity;
+       extcon_get_count(EXTCON_BATTERY_FULL);
+       extcon_set_count(EXTCON_BATTERY_FULL, delta);
+}
+
 static void process_power_supply(void *data)
 {
        static struct battery_status old;
@@ -636,6 +656,7 @@ static void process_power_supply(void *data)
        check_battery_status();
        device_notify(DEVICE_NOTIFIER_POWER_SUPPLY, NULL);
        device_notify(DEVICE_NOTIFIER_BATTERY_CHARGING, &battery.charge_now);
+       update_capacity_full();
 }
 
 static void uevent_power_handler(struct udev_device *dev)
index 204004f..17c8896 100644 (file)
@@ -25,6 +25,7 @@
 #include "core/log.h"
 #include "display/poll.h"
 #include "extcon/extcon.h"
+#include "extcon/extcon_count.h"
 
 #define SIGNAL_EARJACK_STATE   "ChangedEarjack"
 #define GET_EARJACK_STATE              "Earjack"
@@ -72,6 +73,11 @@ static int earjack_update(int status)
        if (status != 0)
                pm_change_internal(INTERNAL_LOCK_EARJACK, LCD_NORMAL);
 
+       if (CONNECTED(status)) {
+               extcon_get_count(EXTCON_EARJACK);
+               extcon_set_count(EXTCON_EARJACK, 1);
+       }
+
        return 0;
 }
 
index eb10fc9..df42bbc 100755 (executable)
@@ -59,6 +59,8 @@ static void __DESTRUCTOR__ extcon_exit(void) \
        remove_extcon(&dev); \
 }
 
+#define CONNECTED(val) ((val) != 0)
+
 void add_extcon(struct extcon_ops *dev);
 void remove_extcon(struct extcon_ops *dev);
 
diff --git a/src/extcon/extcon_count.c b/src/extcon/extcon_count.c
new file mode 100755 (executable)
index 0000000..4527a13
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2015 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 <stdio.h>
+#include <device-node.h>
+#include <vconf.h>
+#include <fcntl.h>
+
+#include "core/log.h"
+#include "core/list.h"
+#include "core/common.h"
+#include "core/devices.h"
+#include "extcon_count.h"
+
+#define BUF_MAX 256
+
+// TODO : storage location should be configurable by configuration file
+static struct extcon_device {
+       const enum extcon_type type;
+       const char *str;
+       int fd;
+       unsigned long count;
+} extcon_devices[] = {
+       { EXTCON_TA,           "/opt/share/batt_cable_count",   0, 0},
+       { EXTCON_EARJACK,      "/opt/share/earjack_count",      0, 0},
+       { EXTCON_BATTERY_FULL, "/opt/share/battery_full_count", 0, 0},
+};
+
+int extcon_get_count(int index)
+{
+       int fd;
+       int r;
+       int ret = 0;
+       char buf[BUF_MAX];
+
+       if (extcon_devices[index].fd) {
+               close(extcon_devices[index].fd);
+               extcon_devices[index].fd = 0;
+       }
+       fd = open(extcon_devices[index].str, O_RDWR);
+       if (fd < 0)
+               return -ENOENT;
+
+       r = read(fd, buf, BUF_MAX);
+       if (r < 0 || r >= BUF_MAX) {
+               close(fd);
+               ret = -EIO;
+               return ret;
+       }
+
+       buf[r] = '\0';
+       extcon_devices[index].fd = fd;
+       extcon_devices[index].count = strtoul(buf, NULL, 10);
+       _I("get extcon(%d:%x) count %lu",
+               index, extcon_devices[index].fd, extcon_devices[index].count);
+
+       return ret;
+}
+
+int extcon_set_count(int index, unsigned long count)
+{
+       int r;
+       int ret = 0;
+       char buf[BUF_MAX];
+
+       extcon_devices[index].count += count;
+
+       if (extcon_devices[index].fd < 0) {
+               _E("cannot open file(%s)", extcon_devices[index].str);
+               return -ENOENT;
+       }
+       lseek(extcon_devices[index].fd, 0, SEEK_SET);
+       _I("ext(%d) count %lu", index, extcon_devices[index].count);
+       snprintf(buf, sizeof(buf), "%lu", extcon_devices[index].count);
+
+       r = write(extcon_devices[index].fd, buf, strlen(buf));
+       if (r < 0)
+               ret = -EIO;
+       return ret;
+}
+
+static int extcon_create_count(int index)
+{
+       int fd;
+       int r;
+       int ret = 0;
+       char buf[BUF_MAX];
+       fd = open(extcon_devices[index].str, O_RDWR | O_CREAT, 0644);
+       if (fd < 0) {
+               _E("cannot open file(%s)", extcon_devices[index].str);
+               return -ENOENT;
+       }
+       snprintf(buf, sizeof(buf), "%lu", extcon_devices[index].count);
+       r = write(fd, buf, strlen(buf));
+       if (r < 0)
+               ret = -EIO;
+
+       if (ret != 0) {
+               close(fd);
+               _E("cannot write file(%s)", extcon_devices[index].str);
+               return ret;
+       }
+       extcon_devices[index].fd = fd;
+       _I("create extcon(%d:%x) %s",
+               index, extcon_devices[index].fd, extcon_devices[index].str);
+       return ret;
+}
+
+static int extcon_count_probe(void *data)
+{
+       return 0;
+}
+
+static void extcon_count_init(void *data)
+{
+       int i, ret;
+
+       for (i = 0; i < ARRAY_SIZE(extcon_devices); i++) {
+               if (extcon_get_count(i) >= 0)
+                       continue;
+               ret = extcon_create_count(i);
+               if (ret < 0)
+                       break;
+       }
+}
+
+static void extcon_count_exit(void *data)
+{
+}
+
+static const struct device_ops extcon_count_ops = {
+       .name   = "extcon_count",
+       .probe  = extcon_count_probe,
+       .init   = extcon_count_init,
+       .exit   = extcon_count_exit,
+};
+
+DEVICE_OPS_REGISTER(&extcon_count_ops)
diff --git a/src/extcon/extcon_count.h b/src/extcon/extcon_count.h
new file mode 100755 (executable)
index 0000000..acd3c06
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2015 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 __EXTCON_COUNT_H__
+#define __EXTCON_COUNT_H__
+
+#include "core/common.h"
+
+enum extcon_type {
+       EXTCON_TA = 0,
+       EXTCON_EARJACK,
+       EXTCON_BATTERY_FULL,
+};
+
+int extcon_set_count(int index, unsigned long count);
+int extcon_get_count(int index);
+
+#endif /* __EXTCON_COUNT_H__ */