charge-thresholds.c - system76-tools - collection of utilities for system76 laptops
 (HTM) git clone https://git.parazyd.org/system76-tools
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       charge-thresholds.c (1860B)
       ---
            1 /* suid tool for setting battery charge thresholds
            2  * GPL-3
            3  */
            4 #include <stdio.h>
            5 #include <string.h>
            6 #include <stdlib.h>
            7 
            8 #include "arg.h"
            9 #include "common.h"
           10 
           11 static const char *START_FD = "/sys/class/power_supply/BAT0/charge_control_start_threshold";
           12 static const char *END_FD = "/sys/class/power_supply/BAT0/charge_control_end_threshold";
           13 
           14 char *argv0;
           15 
           16 static void usage(void)
           17 {
           18         die("usage: %s full-charge|balanced|max-lifespan\n\n"
           19         "full-charge:\n"
           20         "Battery is charged to its full capacity for longest possible use on\n"
           21         "battery power. Charging resumes when the battery falls below 96%%.\n\n"
           22 
           23         "balanced:\n"
           24         "Use this threshold when you unplug frequently but don't need the\n"
           25         "full battery capacity. Charging stops when the battery reaches 90%%\n"
           26         "capacity and resumes when it falls below 85%%\n\n"
           27 
           28         "max-lifespan:\n"
           29         "Use this threshold if you rarely use the system on battery for\n"
           30         "extended periods. Charging stops when the battery reaches 60%%\n"
           31         "capacity and resumes when battery falls below 50%%\n", argv0);
           32 }
           33 
           34 int main(int argc, char *argv[])
           35 {
           36     int start, end; 
           37 
           38     ARGBEGIN {
           39     } ARGEND;
           40 
           41     if (argc != 1)
           42             usage();
           43 
           44     if (!strcmp(argv[0], "full-charge")) {
           45         start = 96;
           46         end = 100;
           47     } else if (!strcmp(argv[0], "balanced")) {
           48         start = 86;
           49         end = 90;
           50     } else if (!strcmp(argv[0], "max-lifespan")) {
           51         start = 50;
           52         end = 60;
           53     } else {
           54         usage();
           55         return 1;
           56     }
           57 
           58     /* Without this, setting start threshold may fail if the previous end
           59      * threshold is higher */
           60         if (write_oneshot_int(END_FD, 100))
           61                 die("Could not open %s for writing:", END_FD);
           62 
           63         if (write_oneshot_int(START_FD, start))
           64                 die("Could not open %s for writing:", START_FD);
           65 
           66         if (write_oneshot_int(END_FD, end))
           67                 die("Could not open %s for writing:", END_FD);
           68 
           69     return 0;
           70 }