2026-11-22 10:49:59 +02:00
|
|
|
#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>
|
|
|
|
|
|
2026-11-22 15:49:38 +02:00
|
|
|
#define MODEM_SOCK "/run/geminux/modem.sock"
|
|
|
|
|
#define MODEM_SOCK_FALLBACK "/tmp/geminux-modem.sock"
|
|
|
|
|
#define STATE_FILE "/run/geminux/modem-state"
|
|
|
|
|
#define AT_DEV "/dev/ttyC0"
|
2026-11-22 10:49:59 +02:00
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
int epfd;
|
|
|
|
|
int sig_fd;
|
|
|
|
|
int srv_fd;
|
|
|
|
|
int at_fd;
|
|
|
|
|
bool in_call;
|
|
|
|
|
bool muted;
|
|
|
|
|
bool speaker;
|
|
|
|
|
char active_peer[64];
|
2026-11-22 15:49:38 +02:00
|
|
|
char sock_path[64];
|
2026-11-22 10:49:59 +02:00
|
|
|
} 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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 15:49:38 +02:00
|
|
|
static int bind_modem_socket(modem_ctx_t *ctx) {
|
|
|
|
|
const char *candidates[2] = { MODEM_SOCK, MODEM_SOCK_FALLBACK };
|
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
|
unlink(candidates[i]);
|
|
|
|
|
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", candidates[i]);
|
|
|
|
|
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == 0 && listen(fd, 8) == 0) {
|
|
|
|
|
chmod(candidates[i], 0660);
|
|
|
|
|
snprintf(ctx->sock_path, sizeof(ctx->sock_path), "%s", candidates[i]);
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
|
|
|
|
ctx->sock_path[0] = '\0';
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 10:49:59 +02:00
|
|
|
int main(void) {
|
2026-11-23 08:21:37 +02:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2026-11-22 10:49:59 +02:00
|
|
|
modem_ctx_t ctx;
|
|
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
2026-11-22 15:49:38 +02:00
|
|
|
ctx.srv_fd = -1;
|
2026-11-22 10:49:59 +02:00
|
|
|
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);
|
|
|
|
|
|
2026-11-22 15:49:38 +02:00
|
|
|
ctx.srv_fd = bind_modem_socket(&ctx);
|
2026-11-22 10:49:59 +02:00
|
|
|
|
|
|
|
|
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) {
|
2026-11-22 15:49:38 +02:00
|
|
|
if (events[i].events & (EPOLLERR | EPOLLHUP)) {
|
|
|
|
|
epoll_ctl(ctx.epfd, EPOLL_CTL_DEL, ctx.srv_fd, NULL);
|
|
|
|
|
close(ctx.srv_fd);
|
|
|
|
|
ctx.srv_fd = -1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-11-22 10:49:59 +02:00
|
|
|
int cfd;
|
|
|
|
|
while ((cfd = accept4(ctx.srv_fd, NULL, NULL, SOCK_CLOEXEC)) >= 0) {
|
|
|
|
|
handle_cmd(&ctx, cfd);
|
|
|
|
|
close(cfd);
|
|
|
|
|
}
|
2026-11-22 15:49:38 +02:00
|
|
|
if (errno == EINVAL || errno == EBADF || errno == ENOTSOCK || errno == EOPNOTSUPP) {
|
|
|
|
|
epoll_ctl(ctx.epfd, EPOLL_CTL_DEL, ctx.srv_fd, NULL);
|
|
|
|
|
close(ctx.srv_fd);
|
|
|
|
|
ctx.srv_fd = -1;
|
|
|
|
|
}
|
2026-11-22 10:49:59 +02:00
|
|
|
} else if (fd == ctx.at_fd) {
|
2026-11-22 15:49:38 +02:00
|
|
|
if (events[i].events & (EPOLLHUP | EPOLLERR)) {
|
|
|
|
|
epoll_ctl(ctx.epfd, EPOLL_CTL_DEL, ctx.at_fd, NULL);
|
|
|
|
|
close(ctx.at_fd);
|
|
|
|
|
ctx.at_fd = -1;
|
|
|
|
|
write_state(&ctx);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-11-22 10:49:59 +02:00
|
|
|
char atbuf[512] = {0};
|
|
|
|
|
ssize_t r = read(ctx.at_fd, atbuf, sizeof(atbuf) - 1);
|
2026-11-22 15:49:38 +02:00
|
|
|
if (r == 0 || (r < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) {
|
|
|
|
|
epoll_ctl(ctx.epfd, EPOLL_CTL_DEL, ctx.at_fd, NULL);
|
|
|
|
|
close(ctx.at_fd);
|
|
|
|
|
ctx.at_fd = -1;
|
|
|
|
|
write_state(&ctx);
|
|
|
|
|
} else if (r > 0) {
|
2026-11-22 10:49:59 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 15:49:38 +02:00
|
|
|
if (ctx.sock_path[0]) unlink(ctx.sock_path);
|
2026-11-22 10:49:59 +02:00
|
|
|
close(ctx.epfd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|