#include "apps/apps.h"
#include "power-supply.h"
#include "battery.h"
+#include "extcon/extcon_count.h"
#define BATTERY_NAME "battery"
#define CHARGEFULL_NAME "Full"
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)
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;
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)
--- /dev/null
+/*
+ * 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)
--- /dev/null
+/*
+ * 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__ */