static int stop_timers(void) { TM_DBG_LOG("stop_timers\n"); // restart base_timer { struct itimerspec it; it.it_interval.tv_sec = 0; it.it_interval.tv_nsec = 0; it.it_value.tv_sec = 0; it.it_value.tv_nsec = 0; if (timer_settime(base_timer, 0, &it, NULL) == -1) { TM_INFO_LOG("fail to timer_settime base_timer\n"); return -1; } } // restart level_timer { struct itimerspec it; it.it_interval.tv_sec = 0; it.it_interval.tv_nsec = 0; it.it_value.tv_sec = 0; it.it_value.tv_nsec = 0; if (timer_settime(level_timer, 0, &it, NULL) == -1) { TM_INFO_LOG("fail to timer_settime base_timer\n"); return -1; } } return 0; }
static int thermal_create_timers(void) { struct sigevent evp; TM_DBG_LOG("thermal_create_timers\n"); memset(&evp, 0, sizeof(struct sigevent)); evp.sigev_value.sival_int = 111; evp.sigev_notify = SIGEV_THREAD; evp.sigev_notify_function = timer_thread; if (timer_create(CLOCK_REALTIME, &evp, &base_timer) == -1) { TM_INFO_LOG("fail to create base_timer\n"); return -1; } memset(&evp, 0, sizeof(struct sigevent)); evp.sigev_value.sival_int = 222; evp.sigev_notify = SIGEV_THREAD; evp.sigev_notify_function = timer_thread; if (timer_create(CLOCK_REALTIME, &evp, &level_timer) == -1) { TM_INFO_LOG("fail to create level_timer\n"); return -1; } return 0; }
static void set_wifi_throttle(int level) { int i = 0; for ( i=0; i<IFC_NUM; i++) { TM_DBG_LOG("checking %s", IFC_PATH[i]); if (0 == access(IFC_PATH[i], R_OK)) { char buf[80]; int fd = open(IFC_PATH[i], O_RDONLY); if (fd < 0) { TM_INFO_LOG("Can't open %s: %s", IFC_PATH[i], strerror(errno)); continue; } int len = read(fd, buf, sizeof(buf) - 1); if (len < 0) { TM_INFO_LOG("Can't read %s: %s", IFC_PATH[i], strerror(errno)); continue; } close(fd); if(!strncmp (buf, "up", 2)) { ifc_set_throttle(IFC_NAME[i], level * ONE_MBITS_PER_SEC, level * ONE_MBITS_PER_SEC); #ifdef NEVER exe_cmd(i, level); #endif /* NEVER */ } else TM_DBG_LOG("%s is down!", IFC_NAME[i]); } } }
static void signal_handler(int signo, siginfo_t *si, void *uc) { switch(si->si_signo) { // Add more signo or code to expand thermald case SIGIO: if(1 == si->si_code) { system("am start com.mediatek.thermalmanager/.ShutDownAlertDialogActivity"); TM_INFO_LOG("thermal shutdown signal received, si_signo=%d, si_code=%d\n", si->si_signo, si->si_code); } break; default: TM_INFO_LOG("what!!!\n"); break; } }
static void set_md_dul_throttle(int level) // level 1~10, 1: unlimit for 1s and zero for 9s, ..., 10: unlimit { TM_DBG_LOG("set_md_dul_throttle %d\n", level); if (level >=1 && level < 10) { level_timeout = level; reset_timers(); } else if (level == 10) { set_md_ul_throttle(-1); stop_timers(); } else TM_INFO_LOG("invalid level %d, ignored\n", level); // if 10 set unlimit and stop timer // if timer not created, create timer // if timer created, reset timer // unlimit ul, and start timer for level s // when times out, limit ul to 1kbps, and start timer for 10 - level s }
static void set_md_ul_throttle(int level) { int i = 0; TM_DBG_LOG("set_md_ul_throttle %d\n", level); for ( i=0; i<MD_IFC_NUM; i++) { TM_DBG_LOG("checking %s", MD_IFC_PATH[i]); if (0 == access(MD_IFC_PATH[i], R_OK)) { char buf[80]; int fd = open(MD_IFC_PATH[i], O_RDONLY); if (fd < 0) { TM_INFO_LOG("Can't open %s: %s", MD_IFC_PATH[i], strerror(errno)); continue; } int len = read(fd, buf, sizeof(buf) - 1); if (len < 0) { TM_INFO_LOG("Can't read %s: %s", MD_IFC_PATH[i], strerror(errno)); continue; } close(fd); #if 0 if(!strncmp (buf, "up", 2)) { ifc_set_throttle(MD_IFC_NAME[i], -1, level); #ifdef NEVER exe_cmd(i, level); #endif /* NEVER */ } else TM_DBG_LOG("%s is down!", MD_IFC_NAME[i]); #else if(!strncmp (buf, "down", 4)) { TM_DBG_LOG("%s is down!", MD_IFC_NAME[i]); } else { ifc_set_throttle(MD_IFC_NAME[i], -1, level); #ifdef NEVER exe_cmd(i, level); #endif /* NEVER */ } #endif } } }
void timer_thread(union sigval v) { TM_DBG_LOG("timer_thread function! %d\n", v.sival_int); if (v.sival_int == 111) { // unlimit set_md_ul_throttle(-1); // restart base_timer { struct itimerspec it; it.it_interval.tv_sec = 10; it.it_interval.tv_nsec = 0; it.it_value.tv_sec = 10; it.it_value.tv_nsec = 0; if (timer_settime(base_timer, 0, &it, NULL) == -1) { TM_INFO_LOG("fail to timer_settime base_timer\n"); } } // restart level_timer { struct itimerspec it; it.it_interval.tv_sec = 10; it.it_interval.tv_nsec = 0; it.it_value.tv_sec = level_timeout; it.it_value.tv_nsec = 0; if (timer_settime(level_timer, 0, &it, NULL) == -1) { TM_INFO_LOG("fail to timer_settime base_timer\n"); } } } else if (v.sival_int == 222) { // limit set_md_ul_throttle(1); } }
int main(int argc, char *argv[]) { int fd = open(PROCFS_MTK_CL_SD_PID, O_RDWR); int pid = getpid(); int ret = 0; char pid_string[32] = {0}; struct sigaction act; TM_INFO_LOG("START+++++++++ %d", getpid()); /* Create signal handler */ memset(&act, 0, sizeof(act)); act.sa_flags = SA_SIGINFO; //act.sa_handler = signal_handler; act.sa_sigaction = signal_handler; sigemptyset(&act.sa_mask); sigaction(SIGIO, &act, NULL); /* Write pid to procfs */ sprintf(pid_string, "%d", pid); ret = write(fd, pid_string, sizeof(char) * strlen(pid_string)); if (ret <= 0) { TM_INFO_LOG("Fail to write %d to %s %x\n", pid, PROCFS_MTK_CL_SD_PID, ret); } else { TM_INFO_LOG("Success to write %d to %s\n", pid, PROCFS_MTK_CL_SD_PID); } close(fd); TM_INFO_LOG("Enter infinite loop"); while(1) { sleep(100); } TM_INFO_LOG("END-----------"); return 0; }
static int reset_timers(void) { TM_DBG_LOG("reset_timers\n"); // unlimit set_md_ul_throttle(-1); // restart base_timer { struct itimerspec it; it.it_interval.tv_sec = 10; it.it_interval.tv_nsec = 0; it.it_value.tv_sec = 10; it.it_value.tv_nsec = 0; if (timer_settime(base_timer, 0, &it, NULL) == -1) { TM_INFO_LOG("fail to timer_settime base_timer\n"); return -1; } } // restart level_timer { struct itimerspec it; it.it_interval.tv_sec = 10; it.it_interval.tv_nsec = 0; it.it_value.tv_sec = level_timeout; it.it_value.tv_nsec = 0; if (timer_settime(level_timer, 0, &it, NULL) == -1) { TM_INFO_LOG("fail to timer_settime base_timer\n"); return -1; } } return 0; }
static void exe_cmd(int wifi_ifc, int level) { if (0 == access(THROTTLE_SCRIPT_PATH, R_OK | X_OK) && wifi_ifc >= 0) { char cmd[256] = {0}; sprintf(cmd, "%s %s %d %d", THROTTLE_SCRIPT_PATH, IFC_NAME[wifi_ifc], level * ONE_MBITS_PER_SEC, level * ONE_MBITS_PER_SEC); TM_INFO_LOG("cmd=%s", cmd); /*Need to execute twice to effect the command*/ int ret = system(cmd); if ((-1 == ret) || (0 != WEXITSTATUS(ret))) { TM_INFO_LOG("1. executing %s failed: %s", THROTTLE_SCRIPT_PATH, strerror(errno)); } ret = system(cmd); if ((-1 == ret) || (0 != WEXITSTATUS(ret))) { TM_INFO_LOG("2. executing %s failed: %s", THROTTLE_SCRIPT_PATH, strerror(errno)); } } else { TM_INFO_LOG("failed to access %s", THROTTLE_SCRIPT_PATH); } }