1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <utils/Log.h> #include <fcntl.h> #include "main.h" #include <cutils/properties.h> #include <sys/reboot.h> #include <errno.h> #include <time.h> #include <stdarg.h>
int showLowBattLogo = false; int inExiting = false;
static int write_to_file(const char* path, const char* buf, int size) { if (!path) { KPOC_LOGE("null path to write"); return 0; } #ifdef VERBOSE_OUTPUT KPOC_LOGI("%s: path: %s, buf: %s, size: %d\n",__FUNCTION__, path ,buf, size); #endif
int fd = open(path, O_RDWR); if (fd == -1) { KPOC_LOGE("Could not open '%s'\n", path); return 0; }
int count = write(fd, buf, size); if (count != size) { KPOC_LOGE("write file (%s) fail, count: %d\n", path, count); close(fd); return 0; }
close(fd); return count; }
void set_int_value(const char * path, const int value) { int ret = 0; char buf[32];
ret = snprintf(buf, sizeof(buf), "%d", value); if (ret < 0) { KPOC_LOGI("%s: failed at snprintf\n", __func__); return; } #ifdef VERBOSE_OUTPUT KPOC_LOGI("%s: %s, %s\n", __func__, path ,buf); #endif write_to_file(path, buf, strlen(buf)); }
/* return value: * 0, error or read nothing * !0, read counts */ static int read_from_file(const char* path, char* buf, int size) { if (!path) { return 0; }
int fd = open(path, O_RDONLY); if (fd == -1) { return 0; }
int count = read(fd, buf, size); if (count > 0) { count = (count < size) ? count : size - 1; while (count > 0 && buf[count-1] == '\n') count--; buf[count] = '\0'; } else { buf[0] = '\0'; }
close(fd); return count; }
int get_int_value(const char * path) { int size = 32; char buf[size]; if(!read_from_file(path, buf, size)) return -1; return atoi(buf); }
int readSys_int(char const * path) { int fd;
if (path == NULL) { return -1; }
fd = open(path, O_RDONLY);
if (fd >= 0) { uint32_t buffer[8] = {0}; int amt = read(fd, buffer, sizeof(uint32_t)*8); if (amt > 0) { KPOC_LOGI("boot mode struct:size=%d,tag=%d,mode=%d\n", buffer[0],buffer[1],buffer[2]); close(fd); return buffer[2]; } else { KPOC_LOGI("read boot mode struct:size <=0"); close(fd); } }
KPOC_LOGI("Fail to open boot mode file %s\n", path); return -errno; }
static const char *bat_notify_path[] = { "/sys/devices/platform/charger/BatteryNotify", "/sys/devices/platform/mt-battery/BatteryNotify", };
/* * return value: * 1: abnormal status * 0: normal status * If path is not found, return 0 */ int get_battnotify_status() { int i = 0; int bat_status = 0;
/* Find path for battery status */ for (i = 0; i < ARRAY_SIZE(bat_notify_path); i++) { bat_status = get_int_value(bat_notify_path[i]); if (bat_status != -1) break; } KPOC_LOGI("charger battStatus=%d, idx=%d\n", bat_status, i);
if (bat_status > 0) return 1;
return 0; }
int is_wireless_charging() { int wireless_online; int ret; HealthInfo info = {};
ret = get_health_info(&info); if (ret < 0) { KPOC_LOGI("%s: cannot get HealthInfo\n", __func__); return 0; }
wireless_online = info.legacy.chargerWirelessOnline; KPOC_LOGI("wireless_charging: %d\n", wireless_online);
return wireless_online; }
bool time_exceed(struct timeval start, int duration_msec) { struct timeval now; gettimeofday(&now, NULL);
if((now.tv_sec - start.tv_sec)*1000000 + now.tv_usec - start.tv_usec > duration_msec*1000) return true; else return false; } |