Initial commit: Geminux 0.1.0 (helio) for Planet Computers Gemini PDA (MT6797X)
This commit is contained in:
commit
034c1f61b1
816 files changed
+223435
No files matched your search
@@ -0,0 +1,64 @@
|
||||
CC ?= gcc
|
||||
PKG_CONFIG ?= pkg-config
|
||||
PREFIX ?= /usr
|
||||
DESTDIR ?=
|
||||
KERNEL_INC ?= ../../prebuilt/kernel/include
|
||||
|
||||
CFLAGS += -std=c11 -Wall -Wextra -O2 -I. -I$(KERNEL_INC)
|
||||
X11_CFLAGS = $(shell $(PKG_CONFIG) --cflags x11)
|
||||
X11_LIBS = $(shell $(PKG_CONFIG) --libs x11)
|
||||
GIO_CFLAGS = $(shell $(PKG_CONFIG) --cflags gio-2.0 glib-2.0 gobject-2.0)
|
||||
GIO_LIBS = $(shell $(PKG_CONFIG) --libs gio-2.0 glib-2.0 gobject-2.0)
|
||||
|
||||
BINS = \
|
||||
geminux-powerd \
|
||||
geminux-power-ctl \
|
||||
geminux-statusd \
|
||||
geminux-ccci-daemon \
|
||||
geminux-modem-ctl \
|
||||
geminux-bt-obex \
|
||||
geminux-leds \
|
||||
geminux-power-audit \
|
||||
geminux-checkpw
|
||||
|
||||
all: $(BINS)
|
||||
|
||||
geminux-powerd: geminux-powerd.c geminux-power-policy.c geminux-power-policy.h
|
||||
$(CC) $(CFLAGS) -o $@ geminux-powerd.c geminux-power-policy.c
|
||||
|
||||
geminux-power-ctl: geminux-power-ctl.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
geminux-statusd: geminux-statusd.c
|
||||
$(CC) $(CFLAGS) $(X11_CFLAGS) -o $@ $< $(X11_LIBS)
|
||||
|
||||
geminux-ccci-daemon: geminux-ccci-daemon.c geminux-ccci-proto.c geminux-ccci-proto.h
|
||||
$(CC) $(CFLAGS) -o $@ geminux-ccci-daemon.c geminux-ccci-proto.c
|
||||
|
||||
geminux-modem-ctl: geminux-modem-ctl.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
geminux-bt-obex: geminux-bt-obex.c
|
||||
$(CC) $(CFLAGS) $(GIO_CFLAGS) -o $@ $< $(GIO_LIBS)
|
||||
|
||||
geminux-leds: geminux-leds.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
geminux-power-audit: geminux-power-audit.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
geminux-checkpw: geminux-checkpw.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
install: all
|
||||
mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/sbin
|
||||
cp -f geminux-power-ctl geminux-statusd geminux-modem-ctl \
|
||||
geminux-bt-obex geminux-power-audit $(DESTDIR)$(PREFIX)/bin/
|
||||
cp -f geminux-powerd geminux-ccci-daemon geminux-leds \
|
||||
geminux-checkpw $(DESTDIR)$(PREFIX)/sbin/
|
||||
chmod 0755 $(DESTDIR)$(PREFIX)/bin/geminux-* $(DESTDIR)$(PREFIX)/sbin/geminux-*
|
||||
|
||||
clean:
|
||||
rm -f $(BINS)
|
||||
|
||||
.PHONY: all install clean
|
||||
@@ -0,0 +1,78 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <gio/gio.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static GMainLoop *g_loop = NULL;
|
||||
|
||||
static void notify_event(const char *title, const char *body) {
|
||||
const char *argv[] = {"notify-send", title, body, NULL};
|
||||
g_spawn_async(NULL, (gchar **)argv, NULL, G_SPAWN_SEARCH_PATH,
|
||||
NULL, NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
static void on_bluez_props(GDBusConnection *conn, const gchar *sender,
|
||||
const gchar *obj_path, const gchar *iface_name,
|
||||
const gchar *signal_name, GVariant *params,
|
||||
gpointer ud) {
|
||||
(void)conn; (void)sender; (void)iface_name; (void)signal_name; (void)ud;
|
||||
const gchar *prop_iface = NULL;
|
||||
GVariant *changed = NULL;
|
||||
GVariant *invalidated = NULL;
|
||||
g_variant_get(params, "(&s@a{sv}@as)", &prop_iface, &changed, &invalidated);
|
||||
|
||||
if (prop_iface && strcmp(prop_iface, "org.bluez.Device1") == 0 && changed) {
|
||||
gboolean connected = FALSE;
|
||||
if (g_variant_lookup(changed, "Connected", "b", &connected)) {
|
||||
notify_event("Bluetooth", connected ? "Device connected" : "Device disconnected");
|
||||
}
|
||||
} else if (prop_iface && strcmp(prop_iface, "org.bluez.obex.Transfer1") == 0 && changed) {
|
||||
const gchar *status = NULL;
|
||||
if (g_variant_lookup(changed, "Status", "&s", &status) &&
|
||||
status && strcmp(status, "complete") == 0) {
|
||||
const gchar *name = NULL;
|
||||
g_variant_lookup(changed, "Name", "&s", &name);
|
||||
char msg[256];
|
||||
snprintf(msg, sizeof(msg), "Received Bluetooth file: %s",
|
||||
(name && name[0]) ? name : obj_path);
|
||||
notify_event("Bluetooth OBEX", msg);
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) g_variant_unref(changed);
|
||||
if (invalidated) g_variant_unref(invalidated);
|
||||
}
|
||||
|
||||
static void handle_sig(int sig) {
|
||||
(void)sig;
|
||||
if (g_loop) g_main_loop_quit(g_loop);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
signal(SIGTERM, handle_sig);
|
||||
signal(SIGINT, handle_sig);
|
||||
signal(SIGCHLD, SIG_IGN);
|
||||
|
||||
g_loop = g_main_loop_new(NULL, FALSE);
|
||||
GDBusConnection *sys = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
|
||||
if (sys) {
|
||||
g_dbus_connection_signal_subscribe(
|
||||
sys, "org.bluez", "org.freedesktop.DBus.Properties",
|
||||
"PropertiesChanged", NULL, "org.bluez.Device1",
|
||||
G_DBUS_SIGNAL_FLAGS_NONE, on_bluez_props, NULL, NULL);
|
||||
}
|
||||
GDBusConnection *ses = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
|
||||
if (ses) {
|
||||
g_dbus_connection_signal_subscribe(
|
||||
ses, "org.bluez.obex", "org.freedesktop.DBus.Properties",
|
||||
"PropertiesChanged", NULL, "org.bluez.obex.Transfer1",
|
||||
G_DBUS_SIGNAL_FLAGS_NONE, on_bluez_props, NULL, NULL);
|
||||
}
|
||||
|
||||
g_main_loop_run(g_loop);
|
||||
if (sys) g_object_unref(sys);
|
||||
if (ses) g_object_unref(ses);
|
||||
g_main_loop_unref(g_loop);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
#define _GNU_SOURCE
|
||||
#include "geminux-ccci-proto.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <grp.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/signalfd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define SOCK_PATH "/run/geminux/ccci.sock"
|
||||
#define NVRAM_DIR "/var/lib/geminux/md_nvram"
|
||||
#define FW_MD1_IMG "/lib/firmware/mediatek/mt6797/md1rom.img"
|
||||
|
||||
typedef struct {
|
||||
int epfd;
|
||||
int sig_fd;
|
||||
int srv_fd;
|
||||
int uev_fd;
|
||||
int ctl_fd;
|
||||
int fs_fd;
|
||||
int rpc_fd;
|
||||
bool fw_present;
|
||||
bool stage_attempted;
|
||||
geminux_ccci_sm_t sm;
|
||||
} ccci_ctx_t;
|
||||
|
||||
static void add_ep(int epfd, int fd) {
|
||||
if (fd < 0) return;
|
||||
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fd };
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
|
||||
}
|
||||
|
||||
static int advance_ccci_stage(int target_stage) {
|
||||
int sfd = open("/sys/class/mt6797_ccci/md1/stage", O_WRONLY | O_CLOEXEC);
|
||||
if (sfd < 0) return -1;
|
||||
|
||||
char stage_buf[16];
|
||||
int len = snprintf(stage_buf, sizeof(stage_buf), "%d\n", target_stage);
|
||||
ssize_t w = (len > 0) ? write(sfd, stage_buf, (size_t)len) : -1;
|
||||
close(sfd);
|
||||
return (w > 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
static void maybe_advance_ccci_stage(ccci_ctx_t *ctx) {
|
||||
if (ctx->stage_attempted || ctx->sm.flight_mode) return;
|
||||
if (access("/etc/geminux/ccci-autostart", F_OK) != 0) return;
|
||||
|
||||
int mfd = open("/sys/class/mt6797_ccci/md1/mblock_info", O_RDONLY | O_CLOEXEC);
|
||||
if (mfd < 0) return;
|
||||
|
||||
char info[256] = {0};
|
||||
ssize_t n = read(mfd, info, sizeof(info) - 1);
|
||||
close(mfd);
|
||||
if (n <= 0) return;
|
||||
|
||||
ctx->stage_attempted = true;
|
||||
if (!strstr(info, "handoff=1")) return;
|
||||
|
||||
(void)advance_ccci_stage(3);
|
||||
}
|
||||
|
||||
static void open_ccci_devs(ccci_ctx_t *ctx) {
|
||||
if (ctx->ctl_fd < 0) {
|
||||
ctx->ctl_fd = open("/dev/ccci_monitor", O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
||||
if (ctx->ctl_fd < 0)
|
||||
ctx->ctl_fd = open("/dev/ccci_md_ctl", O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
||||
if (ctx->ctl_fd >= 0) {
|
||||
add_ep(ctx->epfd, ctx->ctl_fd);
|
||||
maybe_advance_ccci_stage(ctx);
|
||||
__u32 st = 0;
|
||||
if (ioctl(ctx->ctl_fd, CCCI_IOC_GET_MD_STATE, &st) == 0 && st <= MT6797_MD_STATE_RESET) {
|
||||
ctx->sm.state = (enum mt6797_ccci_md_state)st;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ctx->fs_fd < 0) {
|
||||
ctx->fs_fd = open("/dev/ccci_fs", O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
||||
if (ctx->fs_fd >= 0) add_ep(ctx->epfd, ctx->fs_fd);
|
||||
}
|
||||
if (ctx->rpc_fd < 0) {
|
||||
ctx->rpc_fd = open("/dev/ccci_rpc", O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
||||
if (ctx->rpc_fd >= 0) add_ep(ctx->epfd, ctx->rpc_fd);
|
||||
}
|
||||
ctx->fw_present = (access(FW_MD1_IMG, R_OK) == 0);
|
||||
}
|
||||
|
||||
static void handle_ctl_read(ccci_ctx_t *ctx) {
|
||||
uint8_t buf[256];
|
||||
ssize_t n;
|
||||
while ((n = read(ctx->ctl_fd, buf, sizeof(buf))) >= (ssize_t)sizeof(struct mt6797_ccci_header)) {
|
||||
struct mt6797_ccci_header rx, tx;
|
||||
bool need_reply = false;
|
||||
geminux_ccci_unpack_hdr(buf, &rx);
|
||||
if (geminux_ccci_handle_ctrl(&ctx->sm, &rx, &tx, &need_reply) && need_reply) {
|
||||
ssize_t unused = write(ctx->ctl_fd, &tx, sizeof(tx));
|
||||
(void)unused;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_fs_or_rpc(ccci_ctx_t *ctx, int fd) {
|
||||
uint8_t buf[4096];
|
||||
ssize_t n;
|
||||
while ((n = read(fd, buf, sizeof(buf))) >= (ssize_t)sizeof(struct mt6797_ccci_header)) {
|
||||
ctx->sm.rx_packets++;
|
||||
struct mt6797_ccci_header rx, tx;
|
||||
geminux_ccci_unpack_hdr(buf, &rx);
|
||||
/* Echo minimal ACK framing on FS/RPC TX channel for clean modem handshake */
|
||||
geminux_ccci_pack_hdr(&tx, rx.data0, 0,
|
||||
(fd == ctx->fs_fd) ? MT6797_CCCI_CH_FS_TX : MT6797_CCCI_CH_RPC_TX,
|
||||
rx.reserved);
|
||||
ctx->sm.tx_packets++;
|
||||
ssize_t unused = write(fd, &tx, sizeof(tx));
|
||||
(void)unused;
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_client(ccci_ctx_t *ctx, int cfd) {
|
||||
char buf[128] = {0};
|
||||
ssize_t n = read(cfd, buf, sizeof(buf) - 1);
|
||||
if (n <= 0) return;
|
||||
while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) buf[--n] = '\0';
|
||||
|
||||
if (strcmp(buf, "status") == 0) {
|
||||
struct mt6797_ccci_status st;
|
||||
memset(&st, 0, sizeof(st));
|
||||
if (ctx->ctl_fd >= 0) ioctl(ctx->ctl_fd, CCCI_IOC_GET_STATUS, &st);
|
||||
dprintf(cfd, "state=%s dev=%d fw=%d flight=%d tx=%u rx=%u\n",
|
||||
geminux_ccci_state_name(ctx->sm.state),
|
||||
ctx->ctl_fd >= 0 ? 1 : 0,
|
||||
ctx->fw_present ? 1 : 0,
|
||||
ctx->sm.flight_mode ? 1 : 0,
|
||||
ctx->sm.tx_packets, ctx->sm.rx_packets);
|
||||
} else if (strcmp(buf, "reset") == 0) {
|
||||
if (ctx->ctl_fd >= 0) {
|
||||
ioctl(ctx->ctl_fd, CCCI_IOC_MD_RESET);
|
||||
ctx->sm.state = MT6797_MD_STATE_RESET;
|
||||
dprintf(cfd, "OK reset\n");
|
||||
} else {
|
||||
dprintf(cfd, "DEGRADED no /dev/ccci_md_ctl\n");
|
||||
}
|
||||
} else if (strcmp(buf, "boot") == 0) {
|
||||
ctx->sm.flight_mode = false;
|
||||
if (advance_ccci_stage(3) == 0) {
|
||||
ctx->stage_attempted = true;
|
||||
dprintf(cfd, "OK stage=3\n");
|
||||
} else {
|
||||
dprintf(cfd, "DEGRADED stage=3 unavailable\n");
|
||||
}
|
||||
} else if (strcmp(buf, "flight on") == 0) {
|
||||
ctx->sm.flight_mode = true;
|
||||
if (ctx->ctl_fd >= 0) ioctl(ctx->ctl_fd, CCCI_IOC_ENTER_DEEP_FLIGHT);
|
||||
dprintf(cfd, "OK flight=on\n");
|
||||
} else if (strcmp(buf, "flight off") == 0) {
|
||||
ctx->sm.flight_mode = false;
|
||||
(void)advance_ccci_stage(3);
|
||||
if (ctx->ctl_fd >= 0) ioctl(ctx->ctl_fd, CCCI_IOC_LEAVE_DEEP_FLIGHT);
|
||||
dprintf(cfd, "OK flight=off\n");
|
||||
} else {
|
||||
dprintf(cfd, "ERR unknown command\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
ccci_ctx_t ctx;
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
ctx.ctl_fd = ctx.fs_fd = ctx.rpc_fd = -1;
|
||||
geminux_ccci_sm_init(&ctx.sm);
|
||||
|
||||
mkdir("/run/geminux", 0755);
|
||||
mkdir("/var/lib/geminux", 0755);
|
||||
mkdir(NVRAM_DIR, 0755);
|
||||
|
||||
sigset_t mask;
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, SIGTERM);
|
||||
sigaddset(&mask, SIGINT);
|
||||
sigprocmask(SIG_BLOCK, &mask, NULL);
|
||||
ctx.sig_fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
|
||||
|
||||
unlink(SOCK_PATH);
|
||||
ctx.srv_fd = socket(AF_UNIX, SOCK_STREAM | O_NONBLOCK | O_CLOEXEC, 0);
|
||||
if (ctx.srv_fd >= 0) {
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", SOCK_PATH);
|
||||
if (bind(ctx.srv_fd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
|
||||
listen(ctx.srv_fd, 8);
|
||||
struct group *gr = getgrnam("wheel");
|
||||
if (gr) chown(SOCK_PATH, 0, gr->gr_gid);
|
||||
chmod(SOCK_PATH, 0660);
|
||||
}
|
||||
}
|
||||
|
||||
ctx.uev_fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
|
||||
if (ctx.uev_fd >= 0) {
|
||||
struct sockaddr_nl nl = { .nl_family = AF_NETLINK, .nl_pid = (uint32_t)getpid(), .nl_groups = 1 };
|
||||
bind(ctx.uev_fd, (struct sockaddr *)&nl, sizeof(nl));
|
||||
}
|
||||
|
||||
ctx.epfd = epoll_create1(EPOLL_CLOEXEC);
|
||||
add_ep(ctx.epfd, ctx.sig_fd);
|
||||
add_ep(ctx.epfd, ctx.srv_fd);
|
||||
add_ep(ctx.epfd, ctx.uev_fd);
|
||||
open_ccci_devs(&ctx);
|
||||
|
||||
struct epoll_event events[12];
|
||||
bool running = true;
|
||||
while (running) {
|
||||
int n = epoll_wait(ctx.epfd, events, 12, -1);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int fd = events[i].data.fd;
|
||||
if (fd == ctx.sig_fd) {
|
||||
struct signalfd_siginfo si;
|
||||
while (read(ctx.sig_fd, &si, sizeof(si)) > 0) {}
|
||||
running = false;
|
||||
} else if (fd == ctx.srv_fd) {
|
||||
int cfd;
|
||||
while ((cfd = accept4(ctx.srv_fd, NULL, NULL, SOCK_CLOEXEC)) >= 0) {
|
||||
handle_client(&ctx, cfd);
|
||||
close(cfd);
|
||||
}
|
||||
} else if (fd == ctx.uev_fd) {
|
||||
char ubuf[1024];
|
||||
while (recv(ctx.uev_fd, ubuf, sizeof(ubuf), 0) > 0) {}
|
||||
open_ccci_devs(&ctx);
|
||||
} else if (fd == ctx.ctl_fd) {
|
||||
handle_ctl_read(&ctx);
|
||||
} else if (fd == ctx.fs_fd || fd == ctx.rpc_fd) {
|
||||
handle_fs_or_rpc(&ctx, fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unlink(SOCK_PATH);
|
||||
close(ctx.epfd);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#include "geminux-ccci-proto.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static uint32_t get_le32(const uint8_t *p) {
|
||||
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
|
||||
((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
|
||||
}
|
||||
|
||||
void geminux_ccci_sm_init(geminux_ccci_sm_t *sm) {
|
||||
memset(sm, 0, sizeof(*sm));
|
||||
sm->state = MT6797_MD_STATE_GATED;
|
||||
}
|
||||
|
||||
void geminux_ccci_pack_hdr(struct mt6797_ccci_header *hdr, uint32_t data0,
|
||||
uint32_t data1, uint32_t ch, uint32_t reserved) {
|
||||
hdr->data0 = data0;
|
||||
hdr->data1 = data1;
|
||||
hdr->channel = ch;
|
||||
hdr->reserved = reserved;
|
||||
}
|
||||
|
||||
void geminux_ccci_unpack_hdr(const uint8_t raw[16], struct mt6797_ccci_header *hdr) {
|
||||
hdr->data0 = get_le32(raw + 0);
|
||||
hdr->data1 = get_le32(raw + 4);
|
||||
hdr->channel = get_le32(raw + 8);
|
||||
hdr->reserved = get_le32(raw + 12);
|
||||
}
|
||||
|
||||
const char *geminux_ccci_state_name(enum mt6797_ccci_md_state st) {
|
||||
switch (st) {
|
||||
case MT6797_MD_STATE_INVALID: return "INVALID";
|
||||
case MT6797_MD_STATE_GATED: return "GATED";
|
||||
case MT6797_MD_STATE_BOOTING: return "BOOTING";
|
||||
case MT6797_MD_STATE_HS1: return "HS1";
|
||||
case MT6797_MD_STATE_READY: return "READY";
|
||||
case MT6797_MD_STATE_EXCEPTION: return "EXCEPTION";
|
||||
case MT6797_MD_STATE_RESET: return "RESET";
|
||||
default: return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
bool geminux_ccci_handle_ctrl(geminux_ccci_sm_t *sm,
|
||||
const struct mt6797_ccci_header *rx,
|
||||
struct mt6797_ccci_header *tx_reply,
|
||||
bool *need_reply) {
|
||||
if (!sm || !rx || !tx_reply || !need_reply) return false;
|
||||
*need_reply = false;
|
||||
sm->rx_packets++;
|
||||
|
||||
if (rx->channel != MT6797_CCCI_CH_CONTROL_RX &&
|
||||
rx->channel != MT6797_CCCI_CH_STATUS_RX) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (rx->data1) {
|
||||
case MT6797_CCCI_MSG_HS1:
|
||||
sm->state = MT6797_MD_STATE_HS1;
|
||||
geminux_ccci_pack_hdr(tx_reply, 0, MT6797_CCCI_MSG_HS1,
|
||||
MT6797_CCCI_CH_CONTROL_TX, rx->reserved);
|
||||
*need_reply = true;
|
||||
sm->tx_packets++;
|
||||
return true;
|
||||
case MT6797_CCCI_MSG_MD_INIT_START_BOOT:
|
||||
sm->state = MT6797_MD_STATE_BOOTING;
|
||||
return true;
|
||||
case MT6797_CCCI_MSG_NORMAL_BOOT_READY:
|
||||
case MT6797_CCCI_MSG_META_BOOT_READY:
|
||||
sm->state = MT6797_MD_STATE_READY;
|
||||
return true;
|
||||
case MT6797_CCCI_MSG_MD_EX:
|
||||
sm->state = MT6797_MD_STATE_EXCEPTION;
|
||||
return true;
|
||||
case MT6797_CCCI_MSG_MD_EX_REC_OK:
|
||||
sm->state = MT6797_MD_STATE_RESET;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef GEMINUX_CCCI_PROTO_H
|
||||
#define GEMINUX_CCCI_PROTO_H
|
||||
|
||||
#include <linux/mt6797_ccci.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
enum mt6797_ccci_md_state state;
|
||||
bool flight_mode;
|
||||
uint32_t tx_packets;
|
||||
uint32_t rx_packets;
|
||||
} geminux_ccci_sm_t;
|
||||
|
||||
void geminux_ccci_sm_init(geminux_ccci_sm_t *sm);
|
||||
void geminux_ccci_pack_hdr(struct mt6797_ccci_header *hdr, uint32_t data0,
|
||||
uint32_t data1, uint32_t ch, uint32_t reserved);
|
||||
void geminux_ccci_unpack_hdr(const uint8_t raw[16], struct mt6797_ccci_header *hdr);
|
||||
const char *geminux_ccci_state_name(enum mt6797_ccci_md_state st);
|
||||
bool geminux_ccci_handle_ctrl(geminux_ccci_sm_t *sm,
|
||||
const struct mt6797_ccci_header *rx,
|
||||
struct mt6797_ccci_header *tx_reply,
|
||||
bool *need_reply);
|
||||
|
||||
#endif /* GEMINUX_CCCI_PROTO_H */
|
||||
@@ -0,0 +1,47 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <crypt.h>
|
||||
#include <shadow.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int ct_strcmp(const char *a, const char *b) {
|
||||
size_t la = strlen(a), lb = strlen(b);
|
||||
unsigned int diff = (unsigned int)(la ^ lb);
|
||||
size_t m = la < lb ? la : lb;
|
||||
for (size_t i = 0; i < m; i++) {
|
||||
diff |= (unsigned int)((unsigned char)a[i] ^ (unsigned char)b[i]);
|
||||
}
|
||||
return diff != 0;
|
||||
}
|
||||
|
||||
static void trim_nl(char *s) {
|
||||
size_t n = strlen(s);
|
||||
while (n > 0 && (s[n - 1] == '\n' || s[n - 1] == '\r')) s[--n] = '\0';
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char user[64] = {0};
|
||||
char pass[128] = {0};
|
||||
if (!fgets(user, sizeof(user), stdin) || !fgets(pass, sizeof(pass), stdin)) {
|
||||
return 1;
|
||||
}
|
||||
trim_nl(user);
|
||||
trim_nl(pass);
|
||||
if (!user[0]) return 1;
|
||||
|
||||
struct spwd *sp = getspnam(user);
|
||||
if (!sp || !sp->sp_pwdp || !sp->sp_pwdp[0] || sp->sp_pwdp[0] == '!' || sp->sp_pwdp[0] == '*') {
|
||||
volatile char *vp = pass;
|
||||
for (size_t i = 0; i < sizeof(pass); i++) vp[i] = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *hashed = crypt(pass, sp->sp_pwdp);
|
||||
volatile char *vp = pass;
|
||||
for (size_t i = 0; i < sizeof(pass); i++) vp[i] = 0;
|
||||
if (!hashed) return 1;
|
||||
|
||||
return ct_strcmp(hashed, sp->sp_pwdp) == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <grp.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/signalfd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define LEDS_SOCK "/run/geminux/leds.sock"
|
||||
|
||||
static void write_led(const char *channel, int val) {
|
||||
DIR *d = opendir("/sys/class/leds");
|
||||
if (!d) return;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
if (strstr(de->d_name, channel)) {
|
||||
char p[320];
|
||||
snprintf(p, sizeof(p), "/sys/class/leds/%s/brightness", de->d_name);
|
||||
int fd = open(p, O_WRONLY | O_CLOEXEC);
|
||||
if (fd >= 0) {
|
||||
dprintf(fd, "%d\n", val);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
static void apply_rgb(int r, int g, int b) {
|
||||
write_led("red", r);
|
||||
write_led("green", g);
|
||||
write_led("blue", b);
|
||||
}
|
||||
|
||||
static void sync_charging_led(void) {
|
||||
DIR *d = opendir("/sys/class/power_supply");
|
||||
if (!d) return;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320], typ[32] = {0}, st[32] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/type", de->d_name);
|
||||
FILE *ft = fopen(p, "r");
|
||||
if (!ft) continue;
|
||||
if (fgets(typ, sizeof(typ), ft)) {
|
||||
size_t ln = strlen(typ);
|
||||
while (ln > 0 && typ[ln - 1] == '\n') typ[--ln] = '\0';
|
||||
}
|
||||
fclose(ft);
|
||||
if (strcmp(typ, "Battery") != 0) continue;
|
||||
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/status", de->d_name);
|
||||
FILE *fs = fopen(p, "r");
|
||||
if (fs) {
|
||||
if (fgets(st, sizeof(st), fs)) {
|
||||
size_t ln = strlen(st);
|
||||
while (ln > 0 && st[ln - 1] == '\n') st[--ln] = '\0';
|
||||
}
|
||||
fclose(fs);
|
||||
}
|
||||
if (strcmp(st, "Charging") == 0) apply_rgb(32, 16, 0);
|
||||
else if (strcmp(st, "Full") == 0) apply_rgb(0, 32, 0);
|
||||
else apply_rgb(0, 0, 0);
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
mkdir("/run/geminux", 0755);
|
||||
|
||||
sigset_t mask;
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, SIGTERM);
|
||||
sigaddset(&mask, SIGINT);
|
||||
sigprocmask(SIG_BLOCK, &mask, NULL);
|
||||
int sig_fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
|
||||
|
||||
unlink(LEDS_SOCK);
|
||||
int srv_fd = socket(AF_UNIX, SOCK_STREAM | O_NONBLOCK | O_CLOEXEC, 0);
|
||||
if (srv_fd >= 0) {
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", LEDS_SOCK);
|
||||
if (bind(srv_fd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
|
||||
listen(srv_fd, 8);
|
||||
struct group *gr = getgrnam("wheel");
|
||||
if (gr) chown(LEDS_SOCK, 0, gr->gr_gid);
|
||||
chmod(LEDS_SOCK, 0660);
|
||||
}
|
||||
}
|
||||
|
||||
int uev_fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
|
||||
if (uev_fd >= 0) {
|
||||
struct sockaddr_nl nl = { .nl_family = AF_NETLINK, .nl_pid = (uint32_t)getpid(), .nl_groups = 1 };
|
||||
bind(uev_fd, (struct sockaddr *)&nl, sizeof(nl));
|
||||
}
|
||||
|
||||
int tmr_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
|
||||
|
||||
int epfd = epoll_create1(EPOLL_CLOEXEC);
|
||||
int fds[4] = {sig_fd, srv_fd, uev_fd, tmr_fd};
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (fds[i] >= 0) {
|
||||
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fds[i] };
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, fds[i], &ev);
|
||||
}
|
||||
}
|
||||
|
||||
sync_charging_led();
|
||||
|
||||
struct epoll_event events[8];
|
||||
bool running = true;
|
||||
while (running) {
|
||||
int n = epoll_wait(epfd, events, 8, -1);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int fd = events[i].data.fd;
|
||||
if (fd == sig_fd) {
|
||||
running = false;
|
||||
} else if (fd == srv_fd) {
|
||||
int cfd;
|
||||
while ((cfd = accept4(srv_fd, NULL, NULL, SOCK_CLOEXEC)) >= 0) {
|
||||
char cmd[128] = {0};
|
||||
ssize_t r = read(cfd, cmd, sizeof(cmd) - 1);
|
||||
if (r > 0) {
|
||||
while (r > 0 && (cmd[r - 1] == '\n' || cmd[r - 1] == '\r')) cmd[--r] = '\0';
|
||||
int rr = 0, gg = 0, bb = 0, ms = 0;
|
||||
if (strcmp(cmd, "off") == 0) {
|
||||
apply_rgb(0, 0, 0);
|
||||
} else if (sscanf(cmd, "rgb %d %d %d", &rr, &gg, &bb) == 3) {
|
||||
apply_rgb(rr, gg, bb);
|
||||
} else if (sscanf(cmd, "pulse %d %d %d %d", &rr, &gg, &bb, &ms) == 4) {
|
||||
apply_rgb(rr, gg, bb);
|
||||
struct itimerspec its;
|
||||
memset(&its, 0, sizeof(its));
|
||||
its.it_value.tv_sec = ms / 1000;
|
||||
its.it_value.tv_nsec = (ms % 1000) * 1000000L;
|
||||
timerfd_settime(tmr_fd, 0, &its, NULL);
|
||||
}
|
||||
}
|
||||
dprintf(cfd, "OK\n");
|
||||
close(cfd);
|
||||
}
|
||||
} else if (fd == tmr_fd) {
|
||||
uint64_t exp = 0;
|
||||
while (read(tmr_fd, &exp, sizeof(exp)) > 0) {}
|
||||
sync_charging_led();
|
||||
} else if (fd == uev_fd) {
|
||||
char ubuf[1024];
|
||||
while (recv(uev_fd, ubuf, sizeof(ubuf), 0) > 0) {}
|
||||
sync_charging_led();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unlink(LEDS_SOCK);
|
||||
close(epfd);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/signalfd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
#include <termios.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define MODEM_SOCK "/run/geminux/modem.sock"
|
||||
#define STATE_FILE "/run/geminux/modem-state"
|
||||
#define AT_DEV "/dev/ttyC0"
|
||||
|
||||
typedef struct {
|
||||
int epfd;
|
||||
int sig_fd;
|
||||
int srv_fd;
|
||||
int at_fd;
|
||||
bool in_call;
|
||||
bool muted;
|
||||
bool speaker;
|
||||
char active_peer[64];
|
||||
} modem_ctx_t;
|
||||
|
||||
static void spawn_bg(const char *const argv[]) {
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
setsid();
|
||||
pid_t p2 = fork();
|
||||
if (p2 == 0) {
|
||||
int devnull = open("/dev/null", O_RDWR);
|
||||
if (devnull >= 0) {
|
||||
dup2(devnull, STDIN_FILENO);
|
||||
dup2(devnull, STDOUT_FILENO);
|
||||
dup2(devnull, STDERR_FILENO);
|
||||
if (devnull > 2) close(devnull);
|
||||
}
|
||||
execvp(argv[0], (char *const *)argv);
|
||||
_exit(127);
|
||||
}
|
||||
_exit(0);
|
||||
}
|
||||
if (pid > 0) waitpid(pid, NULL, 0);
|
||||
}
|
||||
|
||||
static void write_state(const modem_ctx_t *ctx) {
|
||||
FILE *f = fopen(STATE_FILE, "w");
|
||||
if (!f) return;
|
||||
if (ctx->in_call) {
|
||||
fprintf(f, "CALL:%s\n", ctx->active_peer[0] ? ctx->active_peer : "ACTIVE");
|
||||
} else if (ctx->at_fd >= 0) {
|
||||
fprintf(f, "4G:READY\n");
|
||||
} else {
|
||||
fprintf(f, "4G:STANDBY\n");
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
static void send_at(modem_ctx_t *ctx, const char *cmd) {
|
||||
if (ctx->at_fd < 0) return;
|
||||
dprintf(ctx->at_fd, "%s\r\n", cmd);
|
||||
}
|
||||
|
||||
static void set_audio_verb(const char *verb) {
|
||||
const char *cmd[] = {"alsaucm", "-c", "mt6797-gemini", "set", "_verb", verb, NULL};
|
||||
spawn_bg(cmd);
|
||||
}
|
||||
|
||||
static void save_sms_row(const char *dir_tag, const char *peer, const char *text) {
|
||||
const char *home = getenv("HOME");
|
||||
char dir[512], fp[600], ts[32];
|
||||
snprintf(dir, sizeof(dir), "%s/.local/share/geminux/sms",
|
||||
(home && home[0]) ? home : "/home/user");
|
||||
mkdir(dir, 0755);
|
||||
snprintf(fp, sizeof(fp), "%s/%s.tsv", dir, peer);
|
||||
FILE *f = fopen(fp, "a");
|
||||
if (!f) return;
|
||||
time_t now = time(NULL);
|
||||
struct tm tm;
|
||||
localtime_r(&now, &tm);
|
||||
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M", &tm);
|
||||
fprintf(f, "%s\t%s\t%s\n", dir_tag, ts, text);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
static void open_at_port(modem_ctx_t *ctx) {
|
||||
if (ctx->at_fd >= 0) return;
|
||||
int fd = open(AT_DEV, O_RDWR | O_NOCTTY | O_NONBLOCK | O_CLOEXEC);
|
||||
if (fd < 0) return;
|
||||
struct termios tio;
|
||||
if (tcgetattr(fd, &tio) == 0) {
|
||||
cfmakeraw(&tio);
|
||||
cfsetispeed(&tio, B115200);
|
||||
cfsetospeed(&tio, B115200);
|
||||
tcsetattr(fd, TCSANOW, &tio);
|
||||
}
|
||||
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fd };
|
||||
if (epoll_ctl(ctx->epfd, EPOLL_CTL_ADD, fd, &ev) == 0) {
|
||||
ctx->at_fd = fd;
|
||||
send_at(ctx, "ATE0");
|
||||
send_at(ctx, "AT+CMGF=1");
|
||||
} else {
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_cmd(modem_ctx_t *ctx, int cfd) {
|
||||
char buf[512] = {0};
|
||||
ssize_t n = read(cfd, buf, sizeof(buf) - 1);
|
||||
if (n <= 0) return;
|
||||
while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) buf[--n] = '\0';
|
||||
|
||||
if (strncmp(buf, "dial ", 5) == 0) {
|
||||
snprintf(ctx->active_peer, sizeof(ctx->active_peer), "%s", buf + 5);
|
||||
ctx->in_call = true;
|
||||
const char *inh[] = {"geminux-power-ctl", "inhibit", "add", "call", NULL};
|
||||
spawn_bg(inh);
|
||||
set_audio_verb("VoiceCall");
|
||||
char at[96];
|
||||
snprintf(at, sizeof(at), "ATD%s;", ctx->active_peer);
|
||||
send_at(ctx, at);
|
||||
write_state(ctx);
|
||||
dprintf(cfd, "OK dialing %s\n", ctx->active_peer);
|
||||
} else if (strcmp(buf, "answer") == 0) {
|
||||
ctx->in_call = true;
|
||||
const char *inh[] = {"geminux-power-ctl", "inhibit", "add", "call", NULL};
|
||||
spawn_bg(inh);
|
||||
set_audio_verb("VoiceCall");
|
||||
send_at(ctx, "ATA");
|
||||
write_state(ctx);
|
||||
dprintf(cfd, "OK answered\n");
|
||||
} else if (strcmp(buf, "hangup") == 0) {
|
||||
ctx->in_call = false;
|
||||
ctx->active_peer[0] = '\0';
|
||||
send_at(ctx, "ATH");
|
||||
set_audio_verb("HiFi");
|
||||
const char *inh[] = {"geminux-power-ctl", "inhibit", "del", "call", NULL};
|
||||
spawn_bg(inh);
|
||||
write_state(ctx);
|
||||
dprintf(cfd, "OK hangup\n");
|
||||
} else if (strcmp(buf, "mute on") == 0 || strcmp(buf, "mute off") == 0) {
|
||||
ctx->muted = (strcmp(buf, "mute on") == 0);
|
||||
const char *m[] = {"wpctl", "set-mute", "@DEFAULT_AUDIO_SOURCE@", ctx->muted ? "1" : "0", NULL};
|
||||
spawn_bg(m);
|
||||
dprintf(cfd, "OK mute=%d\n", ctx->muted ? 1 : 0);
|
||||
} else if (strcmp(buf, "speaker on") == 0 || strcmp(buf, "speaker off") == 0) {
|
||||
ctx->speaker = (strcmp(buf, "speaker on") == 0);
|
||||
dprintf(cfd, "OK speaker=%d\n", ctx->speaker ? 1 : 0);
|
||||
} else if (strncmp(buf, "dtmf ", 5) == 0 && buf[5]) {
|
||||
char at[32];
|
||||
snprintf(at, sizeof(at), "AT+VTS=\"%c\"", buf[5]);
|
||||
send_at(ctx, at);
|
||||
dprintf(cfd, "OK dtmf\n");
|
||||
} else if (strncmp(buf, "sms-send ", 9) == 0) {
|
||||
char *sp = strchr(buf + 9, ' ');
|
||||
if (!sp) {
|
||||
dprintf(cfd, "ERR usage sms-send <num> <text>\n");
|
||||
return;
|
||||
}
|
||||
*sp = '\0';
|
||||
const char *peer = buf + 9;
|
||||
const char *text = sp + 1;
|
||||
save_sms_row("OUT", peer, text);
|
||||
const char *mm[] = {"mmcli", "-m", "0", "--messaging-create-sms", text, NULL};
|
||||
spawn_bg(mm);
|
||||
dprintf(cfd, "OK sms queued\n");
|
||||
} else if (strcmp(buf, "status") == 0) {
|
||||
dprintf(cfd, "in_call=%d at_dev=%d peer=%s\n",
|
||||
ctx->in_call ? 1 : 0, ctx->at_fd >= 0 ? 1 : 0,
|
||||
ctx->active_peer[0] ? ctx->active_peer : "none");
|
||||
} else {
|
||||
dprintf(cfd, "ERR unknown\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
modem_ctx_t ctx;
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
ctx.at_fd = -1;
|
||||
mkdir("/run/geminux", 0755);
|
||||
|
||||
sigset_t mask;
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, SIGTERM);
|
||||
sigaddset(&mask, SIGINT);
|
||||
sigprocmask(SIG_BLOCK, &mask, NULL);
|
||||
ctx.sig_fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
|
||||
|
||||
unlink(MODEM_SOCK);
|
||||
ctx.srv_fd = socket(AF_UNIX, SOCK_STREAM | O_NONBLOCK | O_CLOEXEC, 0);
|
||||
if (ctx.srv_fd >= 0) {
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", MODEM_SOCK);
|
||||
if (bind(ctx.srv_fd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
|
||||
listen(ctx.srv_fd, 8);
|
||||
chmod(MODEM_SOCK, 0660);
|
||||
}
|
||||
}
|
||||
|
||||
ctx.epfd = epoll_create1(EPOLL_CLOEXEC);
|
||||
int fixed[2] = {ctx.sig_fd, ctx.srv_fd};
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (fixed[i] >= 0) {
|
||||
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fixed[i] };
|
||||
epoll_ctl(ctx.epfd, EPOLL_CTL_ADD, fixed[i], &ev);
|
||||
}
|
||||
}
|
||||
open_at_port(&ctx);
|
||||
write_state(&ctx);
|
||||
|
||||
struct epoll_event events[8];
|
||||
bool running = true;
|
||||
while (running) {
|
||||
int n = epoll_wait(ctx.epfd, events, 8, -1);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int fd = events[i].data.fd;
|
||||
if (fd == ctx.sig_fd) {
|
||||
running = false;
|
||||
} else if (fd == ctx.srv_fd) {
|
||||
int cfd;
|
||||
while ((cfd = accept4(ctx.srv_fd, NULL, NULL, SOCK_CLOEXEC)) >= 0) {
|
||||
handle_cmd(&ctx, cfd);
|
||||
close(cfd);
|
||||
}
|
||||
} else if (fd == ctx.at_fd) {
|
||||
char atbuf[512] = {0};
|
||||
ssize_t r = read(ctx.at_fd, atbuf, sizeof(atbuf) - 1);
|
||||
if (r > 0) {
|
||||
if (strstr(atbuf, "RING")) {
|
||||
const char *ntf[] = {"notify-send", "Incoming Call", "Voice call on MT6797 LTE", NULL};
|
||||
spawn_bg(ntf);
|
||||
const char *cs[] = {"geminux-callscreen", "Incoming", "--incoming", NULL};
|
||||
spawn_bg(cs);
|
||||
} else if (strstr(atbuf, "+CMT:")) {
|
||||
const char *ntf[] = {"notify-send", "New SMS", "Message received", NULL};
|
||||
spawn_bg(ntf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unlink(MODEM_SOCK);
|
||||
close(ctx.epfd);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <dirent.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int read_str(const char *path, char *buf, size_t sz) {
|
||||
FILE *f = fopen(path, "r");
|
||||
if (!f) return -1;
|
||||
if (!fgets(buf, (int)sz, f)) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
size_t n = strlen(buf);
|
||||
while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) buf[--n] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
bool json = (argc > 1 && strcmp(argv[1], "--json") == 0);
|
||||
|
||||
char mem_sleep[128] = "unknown", zram_algo[128] = "unknown";
|
||||
char profile[64] = "balanced";
|
||||
read_str("/sys/power/mem_sleep", mem_sleep, sizeof(mem_sleep));
|
||||
read_str("/sys/block/zram0/comp_algorithm", zram_algo, sizeof(zram_algo));
|
||||
read_str("/run/geminux/power-profile", profile, sizeof(profile));
|
||||
|
||||
char bat_cap[32] = "?", bat_status[32] = "Unknown", bat_uv[32] = "?", bat_ua[32] = "?";
|
||||
DIR *d = opendir("/sys/class/power_supply");
|
||||
if (d) {
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320], typ[32] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/type", de->d_name);
|
||||
if (read_str(p, typ, sizeof(typ)) == 0 && strcmp(typ, "Battery") == 0) {
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/capacity", de->d_name);
|
||||
read_str(p, bat_cap, sizeof(bat_cap));
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/status", de->d_name);
|
||||
read_str(p, bat_status, sizeof(bat_status));
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/voltage_now", de->d_name);
|
||||
read_str(p, bat_uv, sizeof(bat_uv));
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/current_now", de->d_name);
|
||||
read_str(p, bat_ua, sizeof(bat_ua));
|
||||
break;
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
int online_cpus = 0, a72_online = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
char p[128], v[16] = "1";
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpu%d/online", i);
|
||||
read_str(p, v, sizeof(v));
|
||||
if (atoi(v) != 0) {
|
||||
online_cpus++;
|
||||
if (i >= 8) a72_online++;
|
||||
}
|
||||
}
|
||||
|
||||
char gov0[64] = "unknown";
|
||||
read_str("/sys/devices/system/cpu/cpufreq/policy0/scaling_governor", gov0, sizeof(gov0));
|
||||
|
||||
if (json) {
|
||||
printf("{\"profile\":\"%s\",\"battery_pct\":\"%s\",\"battery_status\":\"%s\","
|
||||
"\"voltage_uv\":\"%s\",\"current_ua\":\"%s\",\"online_cpus\":%d,"
|
||||
"\"a72_online\":%d,\"governor\":\"%s\",\"zram_algo\":\"%s\",\"mem_sleep\":\"%s\"}\n",
|
||||
profile, bat_cap, bat_status, bat_uv, bat_ua, online_cpus, a72_online,
|
||||
gov0, zram_algo, mem_sleep);
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("Geminux Power & Efficiency Audit\n");
|
||||
printf("----------------------------------------\n");
|
||||
printf("Power Profile : %s\n", profile);
|
||||
printf("Battery Status : %s%% (%s, V=%s uV, I=%s uA)\n", bat_cap, bat_status, bat_uv, bat_ua);
|
||||
printf("CPUs Online : %d total (%d A72 big cores online)\n", online_cpus, a72_online);
|
||||
printf("CPUfreq Governor : %s\n", gov0);
|
||||
printf("ZRAM Compression : %s\n", zram_algo);
|
||||
printf("Suspend Modes : %s\n", mem_sleep);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define SOCK_PATH "/run/geminux/powerd.sock"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "Usage: %s <status|suspend|profile eco|balanced|performance|inhibit add|del TAG|lid-suspend on|off>\n",
|
||||
argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char cmd[256] = {0};
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (i > 1 && strlen(cmd) + 1 < sizeof(cmd)) strcat(cmd, " ");
|
||||
strncat(cmd, argv[i], sizeof(cmd) - strlen(cmd) - 2);
|
||||
}
|
||||
|
||||
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd >= 0) {
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", SOCK_PATH);
|
||||
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
|
||||
dprintf(fd, "%s\n", cmd);
|
||||
shutdown(fd, SHUT_WR);
|
||||
char resp[512];
|
||||
ssize_t n;
|
||||
while ((n = read(fd, resp, sizeof(resp))) > 0) {
|
||||
fwrite(resp, 1, (size_t)n, stdout);
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
if (strcmp(cmd, "suspend") == 0) {
|
||||
int pfd = open("/sys/power/state", O_WRONLY);
|
||||
if (pfd >= 0) {
|
||||
ssize_t w = write(pfd, "mem", 3);
|
||||
close(pfd);
|
||||
return (w == 3) ? 0 : 1;
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "geminux-power-ctl: cannot connect to %s\n", SOCK_PATH);
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "geminux-power-policy.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
void geminux_policy_init(geminux_power_policy_t *p) {
|
||||
memset(p, 0, sizeof(*p));
|
||||
p->profile = POWER_PROFILE_BALANCED;
|
||||
p->lid_suspend_enabled = true;
|
||||
}
|
||||
|
||||
bool geminux_policy_set_profile_name(geminux_power_policy_t *p, const char *name) {
|
||||
if (!p || !name) return false;
|
||||
if (strcmp(name, "eco") == 0) {
|
||||
p->profile = POWER_PROFILE_ECO;
|
||||
return true;
|
||||
}
|
||||
if (strcmp(name, "balanced") == 0) {
|
||||
p->profile = POWER_PROFILE_BALANCED;
|
||||
return true;
|
||||
}
|
||||
if (strcmp(name, "performance") == 0) {
|
||||
p->profile = POWER_PROFILE_PERFORMANCE;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *geminux_policy_profile_name(geminux_profile_t prof) {
|
||||
switch (prof) {
|
||||
case POWER_PROFILE_ECO: return "eco";
|
||||
case POWER_PROFILE_PERFORMANCE: return "performance";
|
||||
case POWER_PROFILE_BALANCED:
|
||||
default: return "balanced";
|
||||
}
|
||||
}
|
||||
|
||||
geminux_profile_params_t geminux_policy_params(geminux_profile_t prof, bool on_ac) {
|
||||
geminux_profile_params_t out;
|
||||
out.governor = "schedutil";
|
||||
if (prof == POWER_PROFILE_ECO) {
|
||||
out.a72_online = false;
|
||||
out.a53_max_khz = 1391000UL;
|
||||
out.a72_max_khz = 0UL;
|
||||
out.dim_timeout_s = on_ac ? 180 : 60;
|
||||
out.suspend_timeout_s = on_ac ? 0 : 180;
|
||||
} else if (prof == POWER_PROFILE_PERFORMANCE) {
|
||||
out.a72_online = true;
|
||||
out.a53_max_khz = 1846000UL;
|
||||
out.a72_max_khz = 2600000UL;
|
||||
out.dim_timeout_s = on_ac ? 600 : 180;
|
||||
out.suspend_timeout_s = 0;
|
||||
} else {
|
||||
out.a72_online = true;
|
||||
out.a53_max_khz = 1547000UL;
|
||||
out.a72_max_khz = 2002000UL;
|
||||
out.dim_timeout_s = on_ac ? 300 : 120;
|
||||
out.suspend_timeout_s = on_ac ? 0 : 300;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
bool geminux_policy_add_inhibit(geminux_power_policy_t *p, const char *tag) {
|
||||
if (!p || !tag || !tag[0]) return false;
|
||||
for (int i = 0; i < p->inhibit_count; i++) {
|
||||
if (strcmp(p->inhibitors[i], tag) == 0) return true;
|
||||
}
|
||||
if (p->inhibit_count >= GEMINUX_MAX_INHIBITORS) return false;
|
||||
snprintf(p->inhibitors[p->inhibit_count], sizeof(p->inhibitors[0]), "%s", tag);
|
||||
p->inhibit_count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool geminux_policy_del_inhibit(geminux_power_policy_t *p, const char *tag) {
|
||||
if (!p || !tag || !tag[0]) return false;
|
||||
for (int i = 0; i < p->inhibit_count; i++) {
|
||||
if (strcmp(p->inhibitors[i], tag) == 0) {
|
||||
for (int j = i; j + 1 < p->inhibit_count; j++) {
|
||||
memcpy(p->inhibitors[j], p->inhibitors[j + 1], sizeof(p->inhibitors[0]));
|
||||
}
|
||||
p->inhibit_count--;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool geminux_should_suspend_on_lid(const geminux_power_policy_t *p) {
|
||||
if (!p || !p->lid_closed || !p->lid_suspend_enabled) return false;
|
||||
if (p->hdmi_connected) return false;
|
||||
if (p->inhibit_count > 0) return false;
|
||||
if (p->on_ac_power && p->inhibit_ac_lid) return false;
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef GEMINUX_POWER_POLICY_H
|
||||
#define GEMINUX_POWER_POLICY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#define GEMINUX_MAX_INHIBITORS 16
|
||||
|
||||
typedef enum {
|
||||
POWER_PROFILE_ECO = 0,
|
||||
POWER_PROFILE_BALANCED = 1,
|
||||
POWER_PROFILE_PERFORMANCE = 2,
|
||||
} geminux_profile_t;
|
||||
|
||||
typedef struct {
|
||||
geminux_profile_t profile;
|
||||
bool lid_closed;
|
||||
bool lid_suspend_enabled;
|
||||
bool on_ac_power;
|
||||
bool inhibit_ac_lid;
|
||||
bool hdmi_connected;
|
||||
char inhibitors[GEMINUX_MAX_INHIBITORS][32];
|
||||
int inhibit_count;
|
||||
} geminux_power_policy_t;
|
||||
|
||||
typedef struct {
|
||||
const char *governor;
|
||||
bool a72_online;
|
||||
unsigned long a53_max_khz;
|
||||
unsigned long a72_max_khz;
|
||||
int dim_timeout_s;
|
||||
int suspend_timeout_s;
|
||||
} geminux_profile_params_t;
|
||||
|
||||
void geminux_policy_init(geminux_power_policy_t *p);
|
||||
bool geminux_policy_set_profile_name(geminux_power_policy_t *p, const char *name);
|
||||
const char *geminux_policy_profile_name(geminux_profile_t prof);
|
||||
geminux_profile_params_t geminux_policy_params(geminux_profile_t prof, bool on_ac);
|
||||
bool geminux_policy_add_inhibit(geminux_power_policy_t *p, const char *tag);
|
||||
bool geminux_policy_del_inhibit(geminux_power_policy_t *p, const char *tag);
|
||||
bool geminux_should_suspend_on_lid(const geminux_power_policy_t *p);
|
||||
|
||||
#endif /* GEMINUX_POWER_POLICY_H */
|
||||
@@ -0,0 +1,367 @@
|
||||
#define _GNU_SOURCE
|
||||
#include "geminux-power-policy.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <grp.h>
|
||||
#include <linux/input.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/signalfd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define SOCK_PATH "/run/geminux/powerd.sock"
|
||||
#define PROFILE_FILE "/run/geminux/power-profile"
|
||||
#define MAX_EVDEVS 16
|
||||
|
||||
typedef struct {
|
||||
int epfd;
|
||||
int sig_fd;
|
||||
int uev_fd;
|
||||
int srv_fd;
|
||||
int tmr_fd;
|
||||
int ev_fds[MAX_EVDEVS];
|
||||
int num_ev;
|
||||
int saved_brightness;
|
||||
geminux_power_policy_t policy;
|
||||
} powerd_ctx_t;
|
||||
|
||||
static int write_sysfs_str(const char *path, const char *val) {
|
||||
int fd = open(path, O_WRONLY | O_CLOEXEC);
|
||||
if (fd < 0) return -1;
|
||||
ssize_t w = write(fd, val, strlen(val));
|
||||
close(fd);
|
||||
return (w >= 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
static int read_sysfs_line(const char *path, char *buf, size_t sz) {
|
||||
FILE *f = fopen(path, "r");
|
||||
if (!f) return -1;
|
||||
if (!fgets(buf, (int)sz, f)) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
size_t n = strlen(buf);
|
||||
while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) buf[--n] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void refresh_ac_and_hdmi(powerd_ctx_t *ctx) {
|
||||
ctx->policy.on_ac_power = false;
|
||||
DIR *d = opendir("/sys/class/power_supply");
|
||||
if (d) {
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320], typ[32] = {0}, onl[16] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/type", de->d_name);
|
||||
if (read_sysfs_line(p, typ, sizeof(typ)) == 0 && strcmp(typ, "Battery") != 0) {
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/online", de->d_name);
|
||||
if (read_sysfs_line(p, onl, sizeof(onl)) == 0 && atoi(onl) == 1) {
|
||||
ctx->policy.on_ac_power = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
ctx->policy.hdmi_connected = false;
|
||||
d = opendir("/sys/class/drm");
|
||||
if (d) {
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (!strstr(de->d_name, "HDMI")) continue;
|
||||
char p[320], st[32] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/drm/%s/status", de->d_name);
|
||||
if (read_sysfs_line(p, st, sizeof(st)) == 0 && strcmp(st, "connected") == 0) {
|
||||
ctx->policy.hdmi_connected = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
}
|
||||
|
||||
static void apply_cpu_policy(powerd_ctx_t *ctx) {
|
||||
geminux_profile_params_t par = geminux_policy_params(ctx->policy.profile, ctx->policy.on_ac_power);
|
||||
|
||||
for (int cpu = 8; cpu <= 9; cpu++) {
|
||||
char p[128];
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpu%d/online", cpu);
|
||||
write_sysfs_str(p, par.a72_online ? "1" : "0");
|
||||
}
|
||||
|
||||
DIR *d = opendir("/sys/devices/system/cpu/cpufreq");
|
||||
if (d) {
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (strncmp(de->d_name, "policy", 6) != 0) continue;
|
||||
char p[256], freq_buf[32];
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_governor", de->d_name);
|
||||
write_sysfs_str(p, par.governor);
|
||||
|
||||
int first_cpu = atoi(de->d_name + 6);
|
||||
unsigned long cap = (first_cpu >= 8) ? par.a72_max_khz : par.a53_max_khz;
|
||||
if (cap > 0) {
|
||||
snprintf(p, sizeof(p), "/sys/devices/system/cpu/cpufreq/%s/scaling_max_freq", de->d_name);
|
||||
snprintf(freq_buf, sizeof(freq_buf), "%lu", cap);
|
||||
write_sysfs_str(p, freq_buf);
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
mkdir("/run/geminux", 0755);
|
||||
write_sysfs_str(PROFILE_FILE, geminux_policy_profile_name(ctx->policy.profile));
|
||||
|
||||
struct itimerspec its;
|
||||
memset(&its, 0, sizeof(its));
|
||||
if (par.suspend_timeout_s > 0 && !ctx->policy.hdmi_connected && ctx->policy.inhibit_count == 0) {
|
||||
its.it_value.tv_sec = par.suspend_timeout_s;
|
||||
}
|
||||
timerfd_settime(ctx->tmr_fd, 0, &its, NULL);
|
||||
}
|
||||
|
||||
static void toggle_backlight(powerd_ctx_t *ctx, bool on) {
|
||||
DIR *d = opendir("/sys/class/backlight");
|
||||
if (!d) return;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320], cur[32] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/backlight/%s/brightness", de->d_name);
|
||||
if (!on) {
|
||||
if (read_sysfs_line(p, cur, sizeof(cur)) == 0 && atoi(cur) > 0) {
|
||||
ctx->saved_brightness = atoi(cur);
|
||||
}
|
||||
write_sysfs_str(p, "0");
|
||||
} else {
|
||||
char restore[32];
|
||||
int val = ctx->saved_brightness > 0 ? ctx->saved_brightness : 128;
|
||||
snprintf(restore, sizeof(restore), "%d", val);
|
||||
write_sysfs_str(p, restore);
|
||||
}
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
toggle_backlight(ctx, false);
|
||||
write_sysfs_str("/sys/power/mem_sleep", "deep");
|
||||
if (write_sysfs_str("/sys/power/state", "mem") == 0) {
|
||||
/* Woken back up from S3 suspend */
|
||||
toggle_backlight(ctx, true);
|
||||
refresh_ac_and_hdmi(ctx);
|
||||
apply_cpu_policy(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static void scan_input_devices(powerd_ctx_t *ctx) {
|
||||
for (int i = 0; i < ctx->num_ev; i++) {
|
||||
epoll_ctl(ctx->epfd, EPOLL_CTL_DEL, ctx->ev_fds[i], NULL);
|
||||
close(ctx->ev_fds[i]);
|
||||
}
|
||||
ctx->num_ev = 0;
|
||||
|
||||
for (int i = 0; i < 32 && ctx->num_ev < MAX_EVDEVS; i++) {
|
||||
char p[64];
|
||||
snprintf(p, sizeof(p), "/dev/input/event%d", i);
|
||||
int fd = open(p, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
||||
if (fd < 0) continue;
|
||||
unsigned long evbits = 0;
|
||||
if (ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), &evbits) >= 0 &&
|
||||
((evbits & (1UL << EV_SW)) || (evbits & (1UL << EV_KEY)))) {
|
||||
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fd };
|
||||
if (epoll_ctl(ctx->epfd, EPOLL_CTL_ADD, fd, &ev) == 0) {
|
||||
ctx->ev_fds[ctx->num_ev++] = fd;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
|
||||
char buf[256] = {0};
|
||||
ssize_t n = read(cfd, buf, sizeof(buf) - 1);
|
||||
if (n <= 0) return;
|
||||
while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) buf[--n] = '\0';
|
||||
|
||||
if (strncmp(buf, "profile ", 8) == 0) {
|
||||
if (geminux_policy_set_profile_name(&ctx->policy, buf + 8)) {
|
||||
apply_cpu_policy(ctx);
|
||||
dprintf(cfd, "OK profile=%s\n", geminux_policy_profile_name(ctx->policy.profile));
|
||||
} else {
|
||||
dprintf(cfd, "ERR unknown profile\n");
|
||||
}
|
||||
} else if (strcmp(buf, "suspend") == 0) {
|
||||
dprintf(cfd, "OK suspending\n");
|
||||
trigger_suspend(ctx);
|
||||
} else if (strncmp(buf, "inhibit add ", 12) == 0) {
|
||||
geminux_policy_add_inhibit(&ctx->policy, buf + 12);
|
||||
apply_cpu_policy(ctx);
|
||||
dprintf(cfd, "OK inhibits=%d\n", ctx->policy.inhibit_count);
|
||||
} else if (strncmp(buf, "inhibit del ", 12) == 0) {
|
||||
geminux_policy_del_inhibit(&ctx->policy, buf + 12);
|
||||
apply_cpu_policy(ctx);
|
||||
dprintf(cfd, "OK inhibits=%d\n", ctx->policy.inhibit_count);
|
||||
} else if (strcmp(buf, "lid-suspend on") == 0) {
|
||||
ctx->policy.lid_suspend_enabled = true;
|
||||
dprintf(cfd, "OK lid_suspend=on\n");
|
||||
} else if (strcmp(buf, "lid-suspend off") == 0) {
|
||||
ctx->policy.lid_suspend_enabled = false;
|
||||
dprintf(cfd, "OK lid_suspend=off\n");
|
||||
} else if (strcmp(buf, "status") == 0) {
|
||||
dprintf(cfd, "profile=%s ac=%d hdmi=%d lid_closed=%d lid_suspend=%d inhibits=%d\n",
|
||||
geminux_policy_profile_name(ctx->policy.profile),
|
||||
ctx->policy.on_ac_power ? 1 : 0,
|
||||
ctx->policy.hdmi_connected ? 1 : 0,
|
||||
ctx->policy.lid_closed ? 1 : 0,
|
||||
ctx->policy.lid_suspend_enabled ? 1 : 0,
|
||||
ctx->policy.inhibit_count);
|
||||
} else {
|
||||
dprintf(cfd, "ERR unknown command\n");
|
||||
}
|
||||
}
|
||||
|
||||
static int setup_unix_server(void) {
|
||||
mkdir("/run/geminux", 0755);
|
||||
unlink(SOCK_PATH);
|
||||
int fd = socket(AF_UNIX, SOCK_STREAM | O_NONBLOCK | O_CLOEXEC, 0);
|
||||
if (fd < 0) return -1;
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", SOCK_PATH);
|
||||
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0 || listen(fd, 8) < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
struct group *gr = getgrnam("wheel");
|
||||
if (gr) chown(SOCK_PATH, 0, gr->gr_gid);
|
||||
chmod(SOCK_PATH, 0660);
|
||||
return fd;
|
||||
}
|
||||
|
||||
static int setup_uevent_socket(void) {
|
||||
int fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
|
||||
if (fd < 0) return -1;
|
||||
struct sockaddr_nl nls;
|
||||
memset(&nls, 0, sizeof(nls));
|
||||
nls.nl_family = AF_NETLINK;
|
||||
nls.nl_pid = (uint32_t)getpid();
|
||||
nls.nl_groups = 1;
|
||||
if (bind(fd, (struct sockaddr *)&nls, sizeof(nls)) < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
powerd_ctx_t ctx;
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
ctx.saved_brightness = 128;
|
||||
geminux_policy_init(&ctx.policy);
|
||||
|
||||
sigset_t mask;
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, SIGTERM);
|
||||
sigaddset(&mask, SIGINT);
|
||||
sigaddset(&mask, SIGHUP);
|
||||
sigprocmask(SIG_BLOCK, &mask, NULL);
|
||||
ctx.sig_fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
|
||||
|
||||
ctx.epfd = epoll_create1(EPOLL_CLOEXEC);
|
||||
ctx.tmr_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
|
||||
ctx.srv_fd = setup_unix_server();
|
||||
ctx.uev_fd = setup_uevent_socket();
|
||||
|
||||
int fixed[4] = {ctx.sig_fd, ctx.tmr_fd, ctx.srv_fd, ctx.uev_fd};
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (fixed[i] >= 0) {
|
||||
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fixed[i] };
|
||||
epoll_ctl(ctx.epfd, EPOLL_CTL_ADD, fixed[i], &ev);
|
||||
}
|
||||
}
|
||||
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
scan_input_devices(&ctx);
|
||||
apply_cpu_policy(&ctx);
|
||||
|
||||
struct epoll_event events[16];
|
||||
bool running = true;
|
||||
while (running) {
|
||||
int n = epoll_wait(ctx.epfd, events, 16, -1);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int fd = events[i].data.fd;
|
||||
if (fd == ctx.sig_fd) {
|
||||
struct signalfd_siginfo si;
|
||||
while (read(ctx.sig_fd, &si, sizeof(si)) > 0) {
|
||||
if (si.ssi_signo == SIGHUP) {
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
apply_cpu_policy(&ctx);
|
||||
} else {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
} else if (fd == ctx.srv_fd) {
|
||||
int cfd;
|
||||
while ((cfd = accept4(ctx.srv_fd, NULL, NULL, SOCK_CLOEXEC)) >= 0) {
|
||||
handle_client_cmd(&ctx, cfd);
|
||||
close(cfd);
|
||||
}
|
||||
} else if (fd == ctx.tmr_fd) {
|
||||
uint64_t exp = 0;
|
||||
while (read(ctx.tmr_fd, &exp, sizeof(exp)) > 0) {}
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
if (!ctx.policy.on_ac_power && !ctx.policy.hdmi_connected && ctx.policy.inhibit_count == 0) {
|
||||
trigger_suspend(&ctx);
|
||||
}
|
||||
} else if (fd == ctx.uev_fd) {
|
||||
char ubuf[2048];
|
||||
while (recv(ctx.uev_fd, ubuf, sizeof(ubuf), 0) > 0) {}
|
||||
refresh_ac_and_hdmi(&ctx);
|
||||
apply_cpu_policy(&ctx);
|
||||
} else {
|
||||
struct input_event iev;
|
||||
while (read(fd, &iev, sizeof(iev)) == (ssize_t)sizeof(iev)) {
|
||||
if (iev.type == EV_SW && iev.code == SW_LID) {
|
||||
ctx.policy.lid_closed = (iev.value != 0);
|
||||
if (ctx.policy.lid_closed) {
|
||||
if (geminux_should_suspend_on_lid(&ctx.policy)) {
|
||||
trigger_suspend(&ctx);
|
||||
}
|
||||
} else {
|
||||
toggle_backlight(&ctx, true);
|
||||
apply_cpu_policy(&ctx);
|
||||
}
|
||||
} else if (iev.type == EV_KEY && iev.code == KEY_POWER && iev.value == 1) {
|
||||
trigger_suspend(&ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unlink(SOCK_PATH);
|
||||
close(ctx.epfd);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <linux/rtnetlink.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <sys/signalfd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
static int read_line(const char *path, char *buf, size_t sz) {
|
||||
FILE *f = fopen(path, "r");
|
||||
if (!f) return -1;
|
||||
if (!fgets(buf, (int)sz, f)) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
size_t n = strlen(buf);
|
||||
while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) buf[--n] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool iface_up(const char *ifname) {
|
||||
char p[128], st[32] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/net/%s/operstate", ifname);
|
||||
if (read_line(p, st, sizeof(st)) != 0) return false;
|
||||
return strcmp(st, "up") == 0 || strcmp(st, "unknown") == 0;
|
||||
}
|
||||
|
||||
static void read_battery(char *out, size_t sz) {
|
||||
snprintf(out, sz, "BAT:--");
|
||||
DIR *d = opendir("/sys/class/power_supply");
|
||||
if (!d) return;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char p[320], typ[32] = {0}, cap[16] = {0}, st[24] = {0};
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/type", de->d_name);
|
||||
if (read_line(p, typ, sizeof(typ)) != 0 || strcmp(typ, "Battery") != 0) continue;
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/capacity", de->d_name);
|
||||
read_line(p, cap, sizeof(cap));
|
||||
snprintf(p, sizeof(p), "/sys/class/power_supply/%s/status", de->d_name);
|
||||
read_line(p, st, sizeof(st));
|
||||
const char *tag = (strcmp(st, "Charging") == 0) ? "+" :
|
||||
(strcmp(st, "Full") == 0) ? "=" : "";
|
||||
snprintf(out, sz, "%s%% %s%s", cap[0] ? cap : "?", tag,
|
||||
strcmp(st, "Charging") == 0 ? "CHG" : "BAT");
|
||||
break;
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
static void update_status(Display *dpy) {
|
||||
char bat[48], mdm[64] = {0}, prof[32] = {0}, net[64] = {0};
|
||||
read_battery(bat, sizeof(bat));
|
||||
if (read_line("/run/geminux/modem-state", mdm, sizeof(mdm)) != 0) {
|
||||
snprintf(mdm, sizeof(mdm), "4G:OFF");
|
||||
}
|
||||
if (read_line("/run/geminux/power-profile", prof, sizeof(prof)) != 0) {
|
||||
snprintf(prof, sizeof(prof), "balanced");
|
||||
}
|
||||
|
||||
if (iface_up("wlan0")) snprintf(net, sizeof(net), "WiFi:UP");
|
||||
else if (iface_up("usb0")) snprintf(net, sizeof(net), "USB:UP");
|
||||
else if (iface_up("ccmni0") || iface_up("ccmni1")) snprintf(net, sizeof(net), "LTE:UP");
|
||||
else snprintf(net, sizeof(net), "Net:OFF");
|
||||
|
||||
time_t now = time(NULL);
|
||||
struct tm tm;
|
||||
localtime_r(&now, &tm);
|
||||
char clk[32];
|
||||
strftime(clk, sizeof(clk), "%a %d %H:%M", &tm);
|
||||
|
||||
char bar[256];
|
||||
snprintf(bar, sizeof(bar), " %s | %s | %s | %s | %s ", net, mdm, prof, bat, clk);
|
||||
XStoreName(dpy, DefaultRootWindow(dpy), bar);
|
||||
XFlush(dpy);
|
||||
}
|
||||
|
||||
static void arm_minute_timer(int tfd) {
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_REALTIME, &now);
|
||||
long rem = 60 - (now.tv_sec % 60);
|
||||
if (rem <= 0) rem = 60;
|
||||
struct itimerspec its;
|
||||
memset(&its, 0, sizeof(its));
|
||||
its.it_value.tv_sec = rem;
|
||||
its.it_interval.tv_sec = 60;
|
||||
timerfd_settime(tfd, 0, &its, NULL);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
Display *dpy = XOpenDisplay(NULL);
|
||||
if (!dpy) {
|
||||
fprintf(stderr, "geminux-statusd: cannot open X11 display\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
sigset_t mask;
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, SIGUSR1);
|
||||
sigaddset(&mask, SIGTERM);
|
||||
sigaddset(&mask, SIGINT);
|
||||
sigprocmask(SIG_BLOCK, &mask, NULL);
|
||||
int sig_fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
|
||||
|
||||
int rt_fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_ROUTE);
|
||||
if (rt_fd >= 0) {
|
||||
struct sockaddr_nl nl = { .nl_family = AF_NETLINK, .nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR };
|
||||
bind(rt_fd, (struct sockaddr *)&nl, sizeof(nl));
|
||||
}
|
||||
|
||||
int uev_fd = socket(AF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
|
||||
if (uev_fd >= 0) {
|
||||
struct sockaddr_nl nl = { .nl_family = AF_NETLINK, .nl_groups = 1 };
|
||||
bind(uev_fd, (struct sockaddr *)&nl, sizeof(nl));
|
||||
}
|
||||
|
||||
int ino_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
||||
if (ino_fd >= 0) {
|
||||
inotify_add_watch(ino_fd, "/run/geminux", IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE);
|
||||
}
|
||||
|
||||
int tmr_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
|
||||
if (tmr_fd >= 0) arm_minute_timer(tmr_fd);
|
||||
|
||||
int epfd = epoll_create1(EPOLL_CLOEXEC);
|
||||
int fds[5] = {sig_fd, rt_fd, uev_fd, ino_fd, tmr_fd};
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (fds[i] >= 0) {
|
||||
struct epoll_event ev = { .events = EPOLLIN, .data.fd = fds[i] };
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, fds[i], &ev);
|
||||
}
|
||||
}
|
||||
|
||||
update_status(dpy);
|
||||
|
||||
struct epoll_event events[12];
|
||||
bool running = true;
|
||||
while (running) {
|
||||
int n = epoll_wait(epfd, events, 12, -1);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
break;
|
||||
}
|
||||
bool dirty = false;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int fd = events[i].data.fd;
|
||||
if (fd == sig_fd) {
|
||||
struct signalfd_siginfo si;
|
||||
while (read(sig_fd, &si, sizeof(si)) > 0) {
|
||||
if (si.ssi_signo == SIGUSR1) dirty = true;
|
||||
else running = false;
|
||||
}
|
||||
} else {
|
||||
char drain[1024];
|
||||
while (read(fd, drain, sizeof(drain)) > 0) {}
|
||||
dirty = true;
|
||||
}
|
||||
}
|
||||
if (running && dirty) update_status(dpy);
|
||||
}
|
||||
|
||||
close(epfd);
|
||||
XCloseDisplay(dpy);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user