View Single Post
Old 12-10-2021, 08:38 PM   #4
katadelos
rm -rf /
katadelos ought to be getting tired of karma fortunes by now.katadelos ought to be getting tired of karma fortunes by now.katadelos ought to be getting tired of karma fortunes by now.katadelos ought to be getting tired of karma fortunes by now.katadelos ought to be getting tired of karma fortunes by now.katadelos ought to be getting tired of karma fortunes by now.katadelos ought to be getting tired of karma fortunes by now.katadelos ought to be getting tired of karma fortunes by now.katadelos ought to be getting tired of karma fortunes by now.katadelos ought to be getting tired of karma fortunes by now.katadelos ought to be getting tired of karma fortunes by now.
 
Posts: 219
Karma: 3333683
Join Date: Nov 2019
Location: United Kingdom
Device: K5, KT, KT2, KT3, KT4, KV, PW2, PW3, PW4, PW5
I got bored and wrote a quick script that should handle your use case:

Code:
import subprocess
from http.server import *

class KindleLipcHandler(BaseHTTPRequestHandler):
    def _set_backlight_level(self, level):
        subprocess.run(
            f"lipc-set-prop com.lab126.powerd flIntensity {level}", 
            shell=True
        )

    def _get_backlight_level(self):
        level = subprocess.check_output(
            "lipc-get-prop com.lab126.powerd flIntensity", 
            shell=True
        )
        return int(level.strip().decode())

    def _wakeup(self):
        subprocess.run(
            "lipc-set-prop com.lab126.powerd wakeUp 1", 
            shell=True
        )

    def handle_backlight(self, path_tokens):
        action = path_tokens[-1]
        current_level = self._get_backlight_level()
        new_level = current_level

        if action == "up":
            if current_level < 24:
                new_level = current_level + 1
        elif action == "down":
            if current_level > 0:
                new_level = current_level - 1
        elif action.isdigit():
            if int(action) in range(0,25):
                new_level = int(action)

        if new_level != current_level:
            self._set_backlight_level(new_level)
            return 1

        return 0

    def handle_wake(self):
        self._wakeup()        
        return 1

    def do_GET(self):
        path_tokens = self.path.split("/")
        t_len = len(path_tokens)
        ret = 0
        if t_len == 3:
            if path_tokens[1] == "backlight":
                ret = self.handle_backlight(path_tokens)
        elif t_len == 2:
            if path_tokens[1] == "wake":
                ret = self.handle_wake()

        if ret:
            self.send_response(200)
        else:
            self.send_response(500)
        self.send_header("Content-type", "text/html")
        self.end_headers()

        if ret:
            res = "<html>OK</html>"
        else:
            res = "<html>FAIL</html>"
        self.wfile.write(res.encode())

def run(server_class=HTTPServer, handler_class=KindleLipcHandler):
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()

run()
It provides a very simple API accessible over HTTP with the following endpoints:
  • http://{device_ip}/wake - Wake device from sleep
  • http://{device_ip}/brightness/up - Set brightness to next level up
  • http://{device_ip}/brightness/down - Set brightness to next level down
  • http://{device_ip}/brightness/{0,24} - Set brightness to specific level

To run it, install NiLuJe's build of Python3 from here, save the script above to /mnt/us/server.py and run nohup python3 /mnt/us/server.py & from a shell using SSH.

You'll probably need to open port 8000 on the firewall of the Kindle before you can call the endpoints over WiFi.

Last edited by katadelos; 12-10-2021 at 08:42 PM.
katadelos is offline   Reply With Quote