Home | History | Annotate | Download | only in charger_touch
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "CHARGER_TOUCH"
     18 #include <hardware_legacy/uevent.h>
     19 #include <utils/Log.h>
     20 #include <fcntl.h>
     21 
     22 #define POWER_SUPPLY_PATH "/sys/class/power_supply"
     23 #define TOUCH_PATH "/sys/devices/virtual/input/qcom_touch/charger"
     24 
     25 const char* WIRELESS = "change@/devices/platform/bq51051b_wlc/power_supply/wireless";
     26 const char* USB = "change@/devices/platform/msm_ssbi.0/pm8921-core/pm8921-charger/power_supply/usb";
     27 
     28 enum {
     29     NO_CHARGER,
     30     CHARGER_WIRELESS,
     31     CHARGER_USB,
     32     CHARGER_AC
     33 };
     34 
     35 static void read_path(const char* path, char* buf, size_t size)
     36 {
     37     int fd = open(path, O_RDONLY, 0);
     38     int count;
     39 
     40     if (fd < 0) {
     41         ALOGE("Could not open %s", path);
     42         return;
     43     }
     44     count = read(fd, buf, size);
     45 
     46     close(fd);
     47 }
     48 
     49 static void write_path(int type)
     50 {
     51     int fd = open(TOUCH_PATH, O_RDWR, 0);
     52     char buf[2];
     53 
     54     if (fd < 0) {
     55         ALOGE("Could not open %s", TOUCH_PATH);
     56         return;
     57     }
     58 
     59     snprintf(buf, sizeof(buf), "%d", type);
     60     write(fd, buf, 1);
     61 
     62     close(fd);
     63 }
     64 
     65 static void handle_uevent(const char* udata)
     66 {
     67     const char *str = udata;
     68     char path[PATH_MAX];
     69     char wlc[2], usb[2], ac[2];
     70     int type = NO_CHARGER;
     71 
     72     memset(wlc, 0, 2);
     73     memset(usb, 0, 2);
     74     memset(ac, 0, 2);
     75 
     76     if (!strncmp(str, WIRELESS, strlen(WIRELESS))) {
     77         snprintf(path, sizeof(path), "%s/wireless/online", POWER_SUPPLY_PATH);
     78         read_path(path, wlc, 1);
     79         if (!strncmp(wlc, "1", 1))
     80             type = CHARGER_WIRELESS;
     81 
     82         ALOGE("Type: %d", type);
     83         write_path(type);
     84     } else if (!strncmp(str, USB, strlen(USB))) {
     85         snprintf(path, sizeof(path), "%s/usb/online", POWER_SUPPLY_PATH);
     86         read_path(path, usb, 1);
     87 
     88         snprintf(path, sizeof(path), "%s/pm8921-dc/online", POWER_SUPPLY_PATH);
     89         read_path(path, ac, 1);
     90 
     91         if (!strncmp(usb, "1", 1)) {
     92             type = CHARGER_USB;
     93         } else if (!strncmp(ac, "1", 1)) {
     94             type = CHARGER_AC;
     95         }
     96 
     97         ALOGE("Type: %d", type);
     98         write_path(type);
     99     }
    100 
    101 }
    102 
    103 static void event_loop(void)
    104 {
    105     int len = 0;
    106     static char udata[4096];
    107     memset(udata, 0, sizeof(udata));
    108 
    109     uevent_init();
    110 
    111     while (1) {
    112         len = uevent_next_event(udata, sizeof(udata) - 2);
    113         handle_uevent(udata);
    114     }
    115 }
    116 
    117 int main()
    118 {
    119     event_loop();
    120     return 0;
    121 }
    122