void warn_power_drop(void) { if(power_status() == WARN_POWER_OFF) { #if ( WARN_EN > 0 ) RT_DEBUG_LOG(DEBUG_WARN, ("power drop warn.\n")); warn_judge_set(RT_NULL, WARN_POWER_DROP_SET, WHICH_WARN_POWER_DROP); #endif } }
int main(int argc, char *argv[]) { const char *fname = apmdev; int ctl_fd, sock_fd, ch, suspends, standbys, resumes; int statonly = 0; int powerstatus = 0, powerbak = 0, powerchange = 0; int noacsleep = 0; struct timespec ts = {TIMO, 0}, sts = {0, 0}; struct apm_power_info pinfo; time_t apmtimeout = 0; const char *sockname = sockfile; int kq, nchanges; struct kevent ev[2]; int ncpu_mib[2] = { CTL_HW, HW_NCPU }; int ncpu; size_t ncpu_sz = sizeof(ncpu); while ((ch = getopt(argc, argv, "aACdHLsf:t:S:")) != -1) switch(ch) { case 'a': noacsleep = 1; break; case 'd': debug = 1; break; case 'f': fname = optarg; break; case 'S': sockname = optarg; break; case 't': ts.tv_sec = strtoul(optarg, NULL, 0); if (ts.tv_sec == 0) usage(); break; case 's': /* status only */ statonly = 1; break; case 'A': if (doperf != PERF_NONE) usage(); doperf = PERF_AUTO; break; case 'C': if (doperf != PERF_NONE) usage(); doperf = PERF_COOL; break; case 'L': if (doperf != PERF_NONE) usage(); doperf = PERF_MANUAL; setperf(PERFMIN); break; case 'H': if (doperf != PERF_NONE) usage(); doperf = PERF_MANUAL; setperf(PERFMAX); break; case '?': default: usage(); } argc -= optind; argv += optind; if (argc != 0) usage(); if (doperf == PERF_NONE) doperf = PERF_MANUAL; if (debug) openlog(__progname, LOG_CONS, LOG_LOCAL1); else { if (daemon(0, 0) < 0) error("failed to daemonize", NULL); openlog(__progname, LOG_CONS, LOG_DAEMON); setlogmask(LOG_UPTO(LOG_NOTICE)); } (void) signal(SIGTERM, sigexit); (void) signal(SIGHUP, sigexit); (void) signal(SIGINT, sigexit); if ((ctl_fd = open(fname, O_RDWR)) == -1) { if (errno != ENXIO && errno != ENOENT) error("cannot open device file `%s'", fname); } else if (fcntl(ctl_fd, F_SETFD, FD_CLOEXEC) == -1) error("cannot set close-on-exec for `%s'", fname); sock_fd = bind_socket(sockname); if (fcntl(sock_fd, F_SETFD, FD_CLOEXEC) == -1) error("cannot set close-on-exec for the socket", NULL); power_status(ctl_fd, 1, &pinfo); if (statonly) exit(0); set_driver_messages(ctl_fd, APM_PRINT_OFF); kq = kqueue(); if (kq <= 0) error("kqueue", NULL); EV_SET(&ev[0], sock_fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, NULL); if (ctl_fd == -1) nchanges = 1; else { EV_SET(&ev[1], ctl_fd, EVFILT_READ, EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, NULL); nchanges = 2; } if (kevent(kq, ev, nchanges, NULL, 0, &sts) < 0) error("kevent", NULL); if (sysctl(ncpu_mib, 2, &ncpu, &ncpu_sz, NULL, 0) < 0) error("cannot read hw.ncpu", NULL); if (doperf == PERF_AUTO || doperf == PERF_COOL) { setperf(0); setperf(100); } for (;;) { int rv; sts = ts; if (doperf == PERF_AUTO || doperf == PERF_COOL) { sts.tv_sec = 1; perf_status(&pinfo, ncpu); } apmtimeout += sts.tv_sec; if ((rv = kevent(kq, NULL, 0, ev, 1, &sts)) < 0) break; if (apmtimeout >= ts.tv_sec) { apmtimeout = 0; /* wakeup for timeout: take status */ powerbak = power_status(ctl_fd, 0, &pinfo); if (powerstatus != powerbak) { powerstatus = powerbak; powerchange = 1; } } if (!rv) continue; if (ev->ident == ctl_fd) { suspends = standbys = resumes = 0; syslog(LOG_DEBUG, "apmevent %04x index %d", (int)APM_EVENT_TYPE(ev->data), (int)APM_EVENT_INDEX(ev->data)); switch (APM_EVENT_TYPE(ev->data)) { case APM_SUSPEND_REQ: case APM_USER_SUSPEND_REQ: case APM_CRIT_SUSPEND_REQ: case APM_BATTERY_LOW: suspends++; break; case APM_USER_STANDBY_REQ: case APM_STANDBY_REQ: standbys++; break; #if 0 case APM_CANCEL: suspends = standbys = 0; break; #endif case APM_NORMAL_RESUME: case APM_CRIT_RESUME: case APM_SYS_STANDBY_RESUME: powerbak = power_status(ctl_fd, 0, &pinfo); if (powerstatus != powerbak) { powerstatus = powerbak; powerchange = 1; } resumes++; break; case APM_POWER_CHANGE: powerbak = power_status(ctl_fd, 0, &pinfo); if (powerstatus != powerbak) { powerstatus = powerbak; powerchange = 1; } break; default: ; } if ((standbys || suspends) && noacsleep && power_status(ctl_fd, 0, &pinfo)) syslog(LOG_DEBUG, "no! sleep! till brooklyn!"); else if (suspends) suspend(ctl_fd); else if (standbys) stand_by(ctl_fd); else if (resumes) { do_etc_file(_PATH_APM_ETC_RESUME); syslog(LOG_NOTICE, "system resumed from sleep"); } if (powerchange) { if (powerstatus) do_etc_file(_PATH_APM_ETC_POWERUP); else do_etc_file(_PATH_APM_ETC_POWERDOWN); powerchange = 0; } } else if (ev->ident == sock_fd) switch (handle_client(sock_fd, ctl_fd)) { case NORMAL: break; case SUSPENDING: suspend(ctl_fd); break; case STANDING_BY: stand_by(ctl_fd); break; case HIBERNATING: hibernate(ctl_fd); break; } } error("kevent loop", NULL); return 1; }
void notify_touch(int perc) { power_status(perc); }
enum apm_state handle_client(int sock_fd, int ctl_fd) { /* accept a handle from the client, process it, then clean up */ int cli_fd; struct sockaddr_un from; socklen_t fromlen; struct apm_command cmd; struct apm_reply reply; int cpuspeed_mib[] = {CTL_HW, HW_CPUSPEED}; int cpuspeed = 0; size_t cpuspeed_sz = sizeof(cpuspeed); fromlen = sizeof(from); cli_fd = accept(sock_fd, (struct sockaddr *)&from, &fromlen); if (cli_fd == -1) { syslog(LOG_INFO, "client accept failure: %m"); return NORMAL; } if (recv(cli_fd, &cmd, sizeof(cmd), 0) != sizeof(cmd)) { (void) close(cli_fd); syslog(LOG_INFO, "client size botch"); return NORMAL; } if (cmd.vno != APMD_VNO) { close(cli_fd); /* terminate client */ /* no error message, just drop it. */ return NORMAL; } power_status(ctl_fd, 0, &reply.batterystate); switch (cmd.action) { case SUSPEND: reply.newstate = SUSPENDING; break; case STANDBY: reply.newstate = STANDING_BY; break; case HIBERNATE: reply.newstate = HIBERNATING; break; case SETPERF_LOW: doperf = PERF_MANUAL; reply.newstate = NORMAL; syslog(LOG_NOTICE, "setting hw.setperf to %d", PERFMIN); setperf(PERFMIN); break; case SETPERF_HIGH: doperf = PERF_MANUAL; reply.newstate = NORMAL; syslog(LOG_NOTICE, "setting hw.setperf to %d", PERFMAX); setperf(PERFMAX); break; case SETPERF_AUTO: doperf = PERF_AUTO; reply.newstate = NORMAL; syslog(LOG_NOTICE, "setting hw.setperf automatically"); break; case SETPERF_COOL: doperf = PERF_COOL; reply.newstate = NORMAL; syslog(LOG_NOTICE, "setting hw.setperf for cool running"); break; default: reply.newstate = NORMAL; break; } if (sysctl(cpuspeed_mib, 2, &cpuspeed, &cpuspeed_sz, NULL, 0) < 0) syslog(LOG_INFO, "cannot read hw.cpuspeed"); reply.cpuspeed = cpuspeed; reply.perfmode = doperf; reply.vno = APMD_VNO; if (send(cli_fd, &reply, sizeof(reply), 0) != sizeof(reply)) syslog(LOG_INFO, "client reply botch"); close(cli_fd); return reply.newstate; }
int main (int argc, char *argv[]) { spawn_term((argc >= 2) ? argv[1] : NULL); char cmd[51]; // stores the command that was entered char* token[MAX_TOKENS]; // points to parts of the command unsigned i; int pwm, motor_num, target_setting; for (;;) { printf (LINE_START); cmd_ok = 0; assert(fgets (cmd, 50, stdin)); // use strtok to grab each part of the command token[0] = strtok (cmd, " \n"); for (i = 1; i < MAX_TOKENS; i++) token[i] = strtok (NULL, " \n"); if (token[0] == NULL) help (); else if (!strcmp(token[0], "q") || !strcmp(token[0], "exit")) exit_safe(); else if (!strcmp(token[0], ":q")) printf ("this isnt vim lol\n"); else if (!strcmp(token[0], "help")) { if (token[1] == NULL) help (); else if (!strcmp(token[1],"motor") || !strcmp(token[1],"m")) help_motor(); else if (!strcmp(token[1],"dyn") || !strcmp(token[1],"d")) help_dyn(); else if (!strcmp(token[1],"power") || !strcmp(token[1],"p")) help_power(); } else if (!strcmp(token[0], "motor") || !strcmp(token[0], "m")) { if (token[1] == NULL || !strcmp (token[1], "status")) motor_status(); else if (!strcmp (token[1], "all")) { pwm = atoi_safe(token[2]); // note an invalid token will cause pwm = 0, which is ok motor_set (pwm, H_ALL); } else if (!strcmp (token[1], "fwd")) { pwm = atoi_safe(token[2]); // note an invalid token will cause pwm = 0, which is ok motor_set (pwm, H_FWD); } else if (!strcmp (token[1], "rise")) { pwm = atoi_safe(token[2]); // note an invalid token will cause pwm = 0, which is ok motor_set (pwm, H_RISE); } else { motor_num = atoi_safe (token[1]); // 1-indexed motor_num--; // Internally, this is 0-indexed pwm = atoi_safe (token[2]); switch (motor_num) { case M_FRONT_LEFT: motor_set (pwm, H_FRONT_LEFT); break; case M_FRONT_RIGHT: motor_set (pwm, H_FRONT_RIGHT); break; case M_FWD_LEFT: motor_set (pwm, H_FWD_LEFT); break; case M_FWD_RIGHT: motor_set (pwm, H_FWD_RIGHT); break; case M_REAR: motor_set (pwm, H_REAR); break; default: printf ("**** Invalid motor number.\n"); } } } else if (!strcmp(token[0], "power") || !strcmp(token[0], "p")) { if (token[1] == NULL || !strcmp (token[1], "status")) power_status(); else if (!strcmp (token[1], "on") || !strcmp (token[1], "1")) { power_on(); } else if (!strcmp (token[1], "start") || !strcmp (token[1], "2")) { startup_sequence(); } else if (!strcmp (token[1], "off") || !strcmp (token[1], "0")) { power_off(); } } else if (!strcmp(token[0], "dyn") || !strcmp(token[0], "d")) { if (token[1] == NULL) dyn_status(); else if (!strcmp (token[1], "depth")) { target_setting = atoi_safe(token[2]); // Note an invalid token will cause target_setting = 0, which is ok dyn_set_target_depth(target_setting); } } if (cmd_ok != 1) cmd_error(); } }