|
| 1 | +#!/system/bin/sh |
| 2 | +# FastCharge Next – boot service |
| 3 | +set -eu |
| 4 | + |
| 5 | +MODDIR="${0%/*}" |
| 6 | +LOG="$MODDIR/log.txt" |
| 7 | +CFG="/sdcard/FastCharge/config.prop" |
| 8 | + |
| 9 | +log() { |
| 10 | + [ "${LOG_ENABLED:-1}" = "1" ] && echo "$(date '+%F %T') | $*" >> "$LOG" |
| 11 | +} |
| 12 | + |
| 13 | +# Load config (with defaults) |
| 14 | +CURRENT_MAX_MA=2000 |
| 15 | +CC_CURRENT_MAX_MA=0 |
| 16 | +CONSTANT_VOLTAGE_MAX_UV=0 |
| 17 | +TEMP_HOT_C=42 |
| 18 | +TEMP_COOL_C=39 |
| 19 | +LOG_ENABLED=1 |
| 20 | + |
| 21 | +[ -f "$CFG" ] && . "$CFG" || true |
| 22 | + |
| 23 | +to_uA() { # mA -> uA |
| 24 | + awk "BEGIN{printf \"%d\", $1*1000}" |
| 25 | +} |
| 26 | + |
| 27 | +# Find candidate sysfs nodes |
| 28 | +PS_BASE="/sys/class/power_supply" |
| 29 | +CAND_USB="$PS_BASE/usb $PS_BASE/charger $PS_BASE/dc $PS_BASE/ac" |
| 30 | +BAT="$PS_BASE/battery" |
| 31 | + |
| 32 | +first_writable() { |
| 33 | + for f in $1; do |
| 34 | + if [ -e "$f" ] && [ -w "$f" ]; then |
| 35 | + echo "$f"; return 0 |
| 36 | + fi |
| 37 | + done |
| 38 | + return 1 |
| 39 | +} |
| 40 | + |
| 41 | +USB_CURRENT_NODES="" |
| 42 | +for p in $CAND_USB; do |
| 43 | + [ -d "$p" ] || continue |
| 44 | + for n in current_max input_current_limit; do |
| 45 | + USB_CURRENT_NODES="$USB_CURRENT_NODES $p/$n" |
| 46 | + done |
| 47 | +done |
| 48 | + |
| 49 | +BAT_CURRENT_NODES="$BAT/constant_charge_current_max $BAT/charge_current_max" |
| 50 | +BAT_VOLT_NODE="$BAT/constant_charge_voltage_max" |
| 51 | +TEMP_NODE="$BAT/temp" |
| 52 | + |
| 53 | +USB_NODE="$(first_writable "$USB_CURRENT_NODES" || true)" |
| 54 | +BAT_CUR_NODE="$(first_writable "$BAT_CURRENT_NODES" || true)" |
| 55 | +BAT_VOLT_NODE_WRITABLE="$(first_writable "$BAT_VOLT_NODE" || true)" |
| 56 | + |
| 57 | +log "Detected nodes: USB=$USB_NODE BAT_CUR=$BAT_CUR_NODE BAT_VOLT=$BAT_VOLT_NODE_WRITABLE TEMP=$TEMP_NODE" |
| 58 | + |
| 59 | +TARGET_USB_UA="$(to_uA "$CURRENT_MAX_MA")" |
| 60 | +TARGET_BAT_UA="$(to_uA "${CC_CURRENT_MAX_MA:-0}")" |
| 61 | + |
| 62 | +apply_targets() { |
| 63 | + local why="${1:-apply}" |
| 64 | + if [ -n "${USB_NODE:-}" ] && [ "${CURRENT_MAX_MA:-0}" -gt 0 ]; then |
| 65 | + echo "$TARGET_USB_UA" > "$USB_NODE" 2>/dev/null || log "WARN: write USB current failed" |
| 66 | + log "$why: set USB current_max to ${CURRENT_MAX_MA}mA" |
| 67 | + fi |
| 68 | + if [ -n "${BAT_CUR_NODE:-}" ] && [ "${CC_CURRENT_MAX_MA:-0}" -gt 0 ]; then |
| 69 | + echo "$TARGET_BAT_UA" > "$BAT_CUR_NODE" 2>/dev/null || log "WARN: write BAT current failed" |
| 70 | + log "$why: set BAT constant_charge_current_max to ${CC_CURRENT_MAX_MA}mA" |
| 71 | + fi |
| 72 | + if [ -n "${BAT_VOLT_NODE_WRITABLE:-}" ] && [ "${CONSTANT_VOLTAGE_MAX_UV:-0}" -gt 0 ]; then |
| 73 | + echo "$CONSTANT_VOLTAGE_MAX_UV" > "$BAT_VOLT_NODE_WRITABLE" 2>/dev/null || log "WARN: write BAT voltage failed" |
| 74 | + log "$why: set BAT constant_charge_voltage_max to ${CONSTANT_VOLTAGE_MAX_UV}uV" |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | +clamp() { |
| 79 | + awk -v v="$1" -v lo="$2" -v hi="$3" 'BEGIN{ if(v<lo) v=lo; if(v>hi) v=hi; printf "%d", v }' |
| 80 | +} |
| 81 | + |
| 82 | +CURRENT_MAX_MA="$(clamp "${CURRENT_MAX_MA:-0}" 500 3000)" |
| 83 | +CC_CURRENT_MAX_MA="$(clamp "${CC_CURRENT_MAX_MA:-0}" 0 3000)" |
| 84 | +CONSTANT_VOLTAGE_MAX_UV="$(clamp "${CONSTANT_VOLTAGE_MAX_UV:-0}" 0 4600000)" |
| 85 | +TEMP_HOT_C="$(clamp "${TEMP_HOT_C:-42}" 35 48)" |
| 86 | +TEMP_COOL_C="$(clamp "${TEMP_COOL_C:-39}" 30 47)" |
| 87 | + |
| 88 | +log "Config: USB=${CURRENT_MAX_MA}mA BAT=${CC_CURRENT_MAX_MA}mA VOLT=${CONSTANT_VOLTAGE_MAX_UV}uV HOT=${TEMP_HOT_C}C COOL=${TEMP_COOL_C}C" |
| 89 | + |
| 90 | +apply_targets "initial" |
| 91 | + |
| 92 | +prev_state="normal" |
| 93 | +while :; do |
| 94 | + sleep 20 |
| 95 | + [ -e "$TEMP_NODE" ] || continue |
| 96 | + raw="$(cat "$TEMP_NODE" 2>/dev/null || echo 0)" |
| 97 | + temp_c="$(awk "BEGIN{printf \"%.1f\", $raw/10}")" |
| 98 | + |
| 99 | + if awk "BEGIN{exit !($temp_c >= $TEMP_HOT_C)}"; then |
| 100 | + backoff_mA=1000 |
| 101 | + if [ "$prev_state" != "hot" ]; then |
| 102 | + if [ -n "${USB_NODE:-}" ]; then |
| 103 | + echo "$(to_uA $backoff_mA)" > "$USB_NODE" 2>/dev/null || true |
| 104 | + fi |
| 105 | + if [ -n "${BAT_CUR_NODE:-}" ] && [ "${CC_CURRENT_MAX_MA:-0}" -gt 0 ]; then |
| 106 | + echo "$(to_uA $backoff_mA)" > "$BAT_CUR_NODE" 2>/dev/null || true |
| 107 | + fi |
| 108 | + log "thermal: $temp_c C >= ${TEMP_HOT_C}C -> backoff to ${backoff_mA}mA" |
| 109 | + prev_state="hot" |
| 110 | + fi |
| 111 | + elif awk "BEGIN{exit !($temp_c <= $TEMP_COOL_C)}"; then |
| 112 | + if [ "$prev_state" = "hot" ]; then |
| 113 | + apply_targets "restore" |
| 114 | + log "thermal: $temp_c C <= ${TEMP_COOL_C}C -> restored targets" |
| 115 | + prev_state="normal" |
| 116 | + fi |
| 117 | + fi |
| 118 | +done |
0 commit comments