#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define MODEM_SOCK "/run/geminux/modem.sock" #define MODEM_SOCK_FALLBACK "/tmp/geminux-modem.sock" #define IMS_SOCK_PATH "/run/geminux/ims.sock" #define IMS_STATE_FILE "/run/geminux/ims-state" #define AIRPLANE_RUN_FILE "/run/geminux/airplane-mode" #define STATE_FILE "/run/geminux/modem-state" #define MD1_STATE_SYSFS "/sys/class/mt6797_ccci/md1/state" #define AT_SECONDARY_DEV "/dev/ttyC2" #define ALSA_UCM_CARD "mt6797-mt6351" #define HOSTLESS_SPEECH_PCM "hw:0,8" typedef struct { int epfd; int sig_fd; int srv_fd; int at_fd; int dbus_fd; pid_t dbus_pid; int dbus_event; /* 1=Voice.CallAdded, 2=Messaging.Added */ char dbus_line[512]; size_t dbus_len; char last_call_path[96]; char pending_sms_path[96]; snd_pcm_t *speech_dl; snd_pcm_t *speech_ul; bool in_call; bool muted; bool speaker; char active_peer[64]; char sock_path[64]; char last_state[96]; } 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 int run_capture(const char *const argv[], char *out, size_t out_sz) { if (out && out_sz > 0) out[0] = '\0'; int pfd[2] = {-1, -1}; if (out && out_sz > 0 && pipe2(pfd, O_CLOEXEC) != 0) return -1; pid_t pid = fork(); if (pid < 0) { if (pfd[0] >= 0) { close(pfd[0]); close(pfd[1]); } return -1; } if (pid == 0) { int devnull = open("/dev/null", O_RDWR); if (devnull >= 0) { dup2(devnull, STDIN_FILENO); if (pfd[1] < 0) dup2(devnull, STDOUT_FILENO); dup2(devnull, STDERR_FILENO); if (devnull > 2) close(devnull); } if (pfd[1] >= 0) { close(pfd[0]); dup2(pfd[1], STDOUT_FILENO); if (pfd[1] > 2) close(pfd[1]); } execvp(argv[0], (char *const *)argv); _exit(127); } if (pfd[1] >= 0) close(pfd[1]); if (pfd[0] >= 0) { size_t pos = 0; while (pos + 1 < out_sz) { ssize_t r = read(pfd[0], out + pos, out_sz - 1 - pos); if (r > 0) pos += (size_t)r; else if (r < 0 && errno == EINTR) continue; else break; } out[pos] = '\0'; close(pfd[0]); } int status = 0; while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {} return WIFEXITED(status) ? WEXITSTATUS(status) : 1; } static bool md1_is_ready(void) { int fd = open(MD1_STATE_SYSFS, O_RDONLY | O_CLOEXEC); if (fd < 0) return false; char buf[16] = {0}; ssize_t n = read(fd, buf, sizeof(buf) - 1); close(fd); return (n > 0 && atoi(buf) == 4); } static bool ccmni0_has_ip(void) { int sock = socket(AF_INET, SOCK_DGRAM | O_CLOEXEC, 0); if (sock < 0) return false; struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "ccmni0"); bool up = (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0 && (ifr.ifr_flags & IFF_UP) != 0); bool has_ip = false; if (up) { memset(&ifr, 0, sizeof(ifr)); snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "ccmni0"); if (ioctl(sock, SIOCGIFADDR, &ifr) == 0) { const struct sockaddr_in *sa = (const struct sockaddr_in *)&ifr.ifr_addr; has_ip = (sa->sin_addr.s_addr != 0); } } close(sock); return up && has_ip; } static bool is_airplane_active(void) { int fd = open(AIRPLANE_RUN_FILE, O_RDONLY | O_CLOEXEC); if (fd < 0) return false; char buf[8] = {0}; ssize_t n = read(fd, buf, sizeof(buf) - 1); close(fd); return (n > 0 && buf[0] == '1'); } static bool ims_is_registered(char *mode_out, size_t sz) { if (mode_out && sz > 0) mode_out[0] = '\0'; if (is_airplane_active()) return false; int fd = open(IMS_STATE_FILE, O_RDONLY | O_CLOEXEC); if (fd < 0) return false; char buf[32] = {0}; ssize_t n = read(fd, buf, sizeof(buf) - 1); close(fd); if (n <= 0) return false; while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r')) buf[--n] = '\0'; if (strcmp(buf, "VoLTE") == 0 || strcmp(buf, "VoWiFi") == 0) { if (mode_out && sz > 0) snprintf(mode_out, sz, "%s", buf); return true; } return false; } static int ims_send_cmd(const char *cmd, char *resp, size_t resp_sz) { if (resp && resp_sz > 0) resp[0] = '\0'; int fd = socket(AF_UNIX, SOCK_STREAM | 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", IMS_SOCK_PATH); if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) { close(fd); return -1; } dprintf(fd, "%s\n", cmd); shutdown(fd, SHUT_WR); char buf[256] = {0}; ssize_t r = read(fd, buf, sizeof(buf) - 1); close(fd); if (r <= 0) return -1; while (r > 0 && (buf[r - 1] == '\n' || buf[r - 1] == '\r')) buf[--r] = '\0'; if (resp && resp_sz > 0) snprintf(resp, resp_sz, "%s", buf); return (strncmp(buf, "OK", 2) == 0) ? 0 : -1; } static void write_state(modem_ctx_t *ctx) { char next[96]; char ims_mode[32] = {0}; if (is_airplane_active()) { snprintf(next, sizeof(next), "AIRPLANE\n"); } else if (ctx->in_call) { snprintf(next, sizeof(next), "CALL:%s\n", ctx->active_peer[0] ? ctx->active_peer : "ACTIVE"); } else if (ims_is_registered(ims_mode, sizeof(ims_mode))) { snprintf(next, sizeof(next), "4G:%s\n", ims_mode); } else if (ccmni0_has_ip()) { snprintf(next, sizeof(next), "4G:LTE\n"); } else if (md1_is_ready() || ctx->at_fd >= 0) { snprintf(next, sizeof(next), "4G:READY\n"); } else { snprintf(next, sizeof(next), "4G:STANDBY\n"); } if (strcmp(ctx->last_state, next) == 0) return; FILE *f = fopen(STATE_FILE, "w"); if (!f) return; fputs(next, f); fclose(f); snprintf(ctx->last_state, sizeof(ctx->last_state), "%s", next); } 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 open_hostless_stream(snd_pcm_t **pcm_out, snd_pcm_stream_t stream) { if (*pcm_out) return; snd_pcm_t *pcm = NULL; if (snd_pcm_open(&pcm, HOSTLESS_SPEECH_PCM, stream, SND_PCM_NONBLOCK) < 0) return; if (snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 2, 16000, 0, 100000) < 0) { if (snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 2, 8000, 0, 100000) < 0) { snd_pcm_close(pcm); return; } } if (snd_pcm_prepare(pcm) < 0) { snd_pcm_close(pcm); return; } if (stream == SND_PCM_STREAM_PLAYBACK) { static const int16_t silence[1600 * 2] = {0}; if (snd_pcm_writei(pcm, silence, 1600) < 0) { snd_pcm_close(pcm); return; } if (snd_pcm_state(pcm) == SND_PCM_STATE_PREPARED && snd_pcm_start(pcm) < 0) { snd_pcm_close(pcm); return; } } else { if (snd_pcm_start(pcm) < 0) { snd_pcm_close(pcm); return; } } *pcm_out = pcm; } static void start_hostless_speech(modem_ctx_t *ctx) { open_hostless_stream(&ctx->speech_dl, SND_PCM_STREAM_PLAYBACK); open_hostless_stream(&ctx->speech_ul, SND_PCM_STREAM_CAPTURE); } static void stop_hostless_speech(modem_ctx_t *ctx) { if (ctx->speech_dl) { snd_pcm_drop(ctx->speech_dl); snd_pcm_close(ctx->speech_dl); ctx->speech_dl = NULL; } if (ctx->speech_ul) { snd_pcm_drop(ctx->speech_ul); snd_pcm_close(ctx->speech_ul); ctx->speech_ul = NULL; } } static void restore_hifi_audio(void) { const char *cmd[] = { "sh", "-c", "printf 'set _verb HiFi\\nset _enadev Speaker\\nset _enadev Mic\\n' | " "alsaucm -c " ALSA_UCM_CARD " -b - >/dev/null 2>&1", NULL }; run_capture(cmd, NULL, 0); } static void apply_call_audio(modem_ctx_t *ctx) { const char *script = ctx->speaker ? "printf 'set _verb VoiceCall\\nset _disdev Earpiece\\nset _enadev Speaker\\nset _enadev Mic\\n' | " "alsaucm -c " ALSA_UCM_CARD " -b - >/dev/null 2>&1" : "printf 'set _verb VoiceCall\\nset _disdev Speaker\\nset _enadev Earpiece\\nset _enadev Mic\\n' | " "alsaucm -c " ALSA_UCM_CARD " -b - >/dev/null 2>&1"; const char *cmd[] = {"sh", "-c", script, NULL}; run_capture(cmd, NULL, 0); if (ctx->in_call) start_hostless_speech(ctx); } 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[0] ? peer : "unknown"); 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; const char *direct = getenv("GEMINUX_MODEM_DIRECT_AT"); if (!direct || strcmp(direct, "1") != 0) return; int fd = open(AT_SECONDARY_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 start_dbus_monitor(modem_ctx_t *ctx) { if (ctx->dbus_fd >= 0 || access("/usr/bin/dbus-monitor", X_OK) != 0) return; int pfd[2]; if (pipe2(pfd, O_NONBLOCK | O_CLOEXEC) != 0) return; pid_t pid = fork(); if (pid < 0) { close(pfd[0]); close(pfd[1]); return; } if (pid == 0) { int devnull = open("/dev/null", O_RDWR); if (devnull >= 0) { dup2(devnull, STDIN_FILENO); dup2(devnull, STDERR_FILENO); if (devnull > 2) close(devnull); } close(pfd[0]); dup2(pfd[1], STDOUT_FILENO); if (pfd[1] > 2) close(pfd[1]); execl("/usr/bin/dbus-monitor", "dbus-monitor", "--system", "type='signal',interface='org.freedesktop.ModemManager1.Modem.Voice'", "type='signal',interface='org.freedesktop.ModemManager1.Modem.Messaging'", NULL); _exit(127); } close(pfd[1]); struct epoll_event ev = { .events = EPOLLIN, .data.fd = pfd[0] }; if (epoll_ctl(ctx->epfd, EPOLL_CTL_ADD, pfd[0], &ev) == 0) { ctx->dbus_fd = pfd[0]; ctx->dbus_pid = pid; ctx->dbus_event = 0; ctx->dbus_len = 0; } else { close(pfd[0]); kill(pid, SIGTERM); waitpid(pid, NULL, 0); } } static void stop_dbus_monitor(modem_ctx_t *ctx) { if (ctx->dbus_fd >= 0) { epoll_ctl(ctx->epfd, EPOLL_CTL_DEL, ctx->dbus_fd, NULL); close(ctx->dbus_fd); ctx->dbus_fd = -1; } if (ctx->dbus_pid > 0) { kill(ctx->dbus_pid, SIGTERM); waitpid(ctx->dbus_pid, NULL, 0); ctx->dbus_pid = -1; } } static void parse_mmcli_kv(const char *raw, const char *key, char *dst, size_t dst_sz) { dst[0] = '\0'; char copy[2048]; snprintf(copy, sizeof(copy), "%s", raw); size_t klen = strlen(key); char *save = NULL; for (char *ln = strtok_r(copy, "\r\n", &save); ln; ln = strtok_r(NULL, "\r\n", &save)) { while (*ln == ' ') ln++; if (strncmp(ln, key, klen) == 0 && ln[klen] == ':') { const char *v = ln + klen + 1; while (*v == ' ') v++; if (strcmp(v, "--") != 0) snprintf(dst, dst_sz, "%s", v); return; } } } static void handle_mm_call_added(modem_ctx_t *ctx, const char *call_path) { if (!call_path[0] || strcmp(ctx->last_call_path, call_path) == 0) return; char out[2048] = {0}; const char *cmd[] = {"mmcli", "-o", call_path, "-K", NULL}; if (run_capture(cmd, out, sizeof(out)) != 0 || !out[0]) return; char dir[32] = {0}, state[32] = {0}, number[64] = {0}; parse_mmcli_kv(out, "call.properties.direction", dir, sizeof(dir)); parse_mmcli_kv(out, "call.properties.state", state, sizeof(state)); parse_mmcli_kv(out, "call.properties.number", number, sizeof(number)); if (strcmp(dir, "incoming") != 0 || strcmp(state, "ringing-in") != 0) return; snprintf(ctx->last_call_path, sizeof(ctx->last_call_path), "%s", call_path); snprintf(ctx->active_peer, sizeof(ctx->active_peer), "%s", number[0] ? number : "Unknown"); const char *ntf[] = {"notify-send", "Incoming Call", ctx->active_peer, NULL}; spawn_bg(ntf); const char *cs[] = {"geminux-callscreen", ctx->active_peer, "--incoming", NULL}; spawn_bg(cs); } static void handle_mm_sms_added(modem_ctx_t *ctx, const char *sms_path) { (void)ctx; if (!sms_path[0]) return; char out[2048] = {0}; const char *cmd[] = {"mmcli", "-s", sms_path, "-K", NULL}; if (run_capture(cmd, out, sizeof(out)) != 0 || !out[0]) return; char pdu[32] = {0}, sender[64] = {0}, text[256] = {0}; parse_mmcli_kv(out, "sms.properties.pdu-type", pdu, sizeof(pdu)); parse_mmcli_kv(out, "sms.content.number", sender, sizeof(sender)); parse_mmcli_kv(out, "sms.content.text", text, sizeof(text)); if (strcmp(pdu, "submit") == 0) return; save_sms_row("IN", sender[0] ? sender : "unknown", text[0] ? text : "Message received"); const char *ntf[] = {"notify-send", sender[0] ? sender : "New SMS", text[0] ? text : "Message received", NULL}; spawn_bg(ntf); } static void handle_dbus_line(modem_ctx_t *ctx, const char *line) { if (strncmp(line, "signal ", 7) == 0) { ctx->dbus_event = 0; ctx->pending_sms_path[0] = '\0'; if (strstr(line, "interface=org.freedesktop.ModemManager1.Modem.Voice") && strstr(line, "member=CallAdded")) { ctx->dbus_event = 1; } else if (strstr(line, "interface=org.freedesktop.ModemManager1.Modem.Messaging") && strstr(line, "member=Added")) { ctx->dbus_event = 2; } return; } if (ctx->dbus_event == 0) return; const char *op = strstr(line, "object path \""); if (op) { op += 13; const char *eq = strchr(op, '"'); if (eq && (size_t)(eq - op) < 95) { char path[96]; memcpy(path, op, (size_t)(eq - op)); path[eq - op] = '\0'; if (ctx->dbus_event == 1) { ctx->dbus_event = 0; handle_mm_call_added(ctx, path); } else if (ctx->dbus_event == 2) { snprintf(ctx->pending_sms_path, sizeof(ctx->pending_sms_path), "%s", path); } } return; } if (ctx->dbus_event == 2 && ctx->pending_sms_path[0]) { if (strstr(line, "boolean true")) { char path[96]; snprintf(path, sizeof(path), "%s", ctx->pending_sms_path); ctx->pending_sms_path[0] = '\0'; ctx->dbus_event = 0; handle_mm_sms_added(ctx, path); } else if (strstr(line, "boolean false")) { ctx->pending_sms_path[0] = '\0'; ctx->dbus_event = 0; } } } 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", (int)(sizeof(ctx->active_peer) - 1), buf + 5); ctx->in_call = true; const char *inh[] = {"geminux-power-ctl", "inhibit", "add", "call", NULL}; spawn_bg(inh); apply_call_audio(ctx); bool via_ims = ims_is_registered(NULL, 0) && (ims_send_cmd(buf, NULL, 0) == 0); if (!via_ims) { if (ctx->at_fd >= 0) { char at[96]; snprintf(at, sizeof(at), "ATD%s;", ctx->active_peer); send_at(ctx, at); } else { const char *mm[] = { "sh", "-c", "p=$(mmcli -m any --voice-create-call=\"number=$1\" 2>/dev/null | " "sed -n 's|.*\\(/org/freedesktop/ModemManager1/Call/[0-9]*\\).*|\\1|p' | head -n1); " "[ -n \"$p\" ] && mmcli -o \"$p\" --start >/dev/null 2>&1", "geminux-dial", ctx->active_peer, NULL }; spawn_bg(mm); } } write_state(ctx); dprintf(cfd, "OK dialing %s%s\n", ctx->active_peer, via_ims ? " (IMS)" : ""); } else if (strcmp(buf, "answer") == 0) { ctx->in_call = true; const char *inh[] = {"geminux-power-ctl", "inhibit", "add", "call", NULL}; spawn_bg(inh); apply_call_audio(ctx); bool via_ims = (ims_send_cmd("answer", NULL, 0) == 0); if (!via_ims) { if (ctx->at_fd >= 0) { send_at(ctx, "ATA"); } else { const char *mm[] = { "sh", "-c", "for p in $(mmcli -m any --voice-list-calls 2>/dev/null | " "sed -n 's|.*\\(/org/freedesktop/ModemManager1/Call/[0-9]*\\).*|\\1|p'); do " "mmcli -o \"$p\" --accept >/dev/null 2>&1; done", NULL }; spawn_bg(mm); } } write_state(ctx); dprintf(cfd, "OK answered\n"); } else if (strcmp(buf, "hangup") == 0) { ctx->in_call = false; ctx->active_peer[0] = '\0'; ctx->last_call_path[0] = '\0'; (void)ims_send_cmd("hangup", NULL, 0); if (ctx->at_fd >= 0) { send_at(ctx, "ATH"); } else { const char *mm[] = {"mmcli", "-m", "any", "--voice-hangup-all", NULL}; spawn_bg(mm); } stop_hostless_speech(ctx); restore_hifi_audio(); 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); (void)ims_send_cmd(buf, NULL, 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); (void)ims_send_cmd(buf, NULL, 0); if (ctx->in_call) apply_call_audio(ctx); dprintf(cfd, "OK speaker=%d\n", ctx->speaker ? 1 : 0); } else if (strncmp(buf, "dtmf ", 5) == 0 && buf[5]) { if (ims_send_cmd(buf, NULL, 0) != 0) { if (ctx->at_fd >= 0) { char at[32]; snprintf(at, sizeof(at), "AT+VTS=\"%c\"", buf[5]); send_at(ctx, at); } else { char digit[2] = {buf[5], '\0'}; const char *mm[] = { "sh", "-c", "for p in $(mmcli -m any --voice-list-calls 2>/dev/null | " "sed -n 's|.*\\(/org/freedesktop/ModemManager1/Call/[0-9]*\\).*|\\1|p'); do " "mmcli -o \"$p\" --send-dtmf=\"$1\" >/dev/null 2>&1; done", "geminux-dtmf", digit, NULL }; spawn_bg(mm); } } 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 \n"); return; } char raw_cmd[512]; snprintf(raw_cmd, sizeof(raw_cmd), "sms-send %s", buf + 9); *sp = '\0'; const char *peer = buf + 9; const char *text = sp + 1; save_sms_row("OUT", peer, text); if (!ims_is_registered(NULL, 0) || ims_send_cmd(raw_cmd, NULL, 0) != 0) { const char *mm[] = { "sh", "-c", "p=$(mmcli -m any --messaging-create-sms=\"number=$1,text=$2\" 2>/dev/null | " "sed -n 's|.*\\(/org/freedesktop/ModemManager1/SMS/[0-9]*\\).*|\\1|p' | head -n1); " "[ -n \"$p\" ] && mmcli -s \"$p\" --send >/dev/null 2>&1", "geminux-sms", peer, text, NULL }; spawn_bg(mm); } dprintf(cfd, "OK sms queued\n"); } else if (strncmp(buf, "ims-ring ", 9) == 0) { snprintf(ctx->active_peer, sizeof(ctx->active_peer), "%.*s", (int)(sizeof(ctx->active_peer) - 1), buf + 9); const char *ntf[] = {"notify-send", "Incoming IMS Call", ctx->active_peer[0] ? ctx->active_peer : "Unknown", NULL}; spawn_bg(ntf); const char *cs[] = {"geminux-callscreen", ctx->active_peer[0] ? ctx->active_peer : "Unknown", "--incoming", NULL}; spawn_bg(cs); dprintf(cfd, "OK\n"); } else if (strncmp(buf, "ims-sms ", 8) == 0) { char *sp = strchr(buf + 8, ' '); const char *peer = sp ? (buf + 8) : "unknown"; if (sp) *sp = '\0'; const char *text = (sp && sp[1]) ? (sp + 1) : (buf + 8); save_sms_row("IN", peer, text); const char *ntf[] = {"notify-send", peer, text, NULL}; spawn_bg(ntf); dprintf(cfd, "OK\n"); } else if (strcmp(buf, "ims-ended") == 0) { ctx->in_call = false; ctx->active_peer[0] = '\0'; stop_hostless_speech(ctx); restore_hifi_audio(); const char *inh[] = {"geminux-power-ctl", "inhibit", "del", "call", NULL}; spawn_bg(inh); write_state(ctx); dprintf(cfd, "OK\n"); } else if (strcmp(buf, "status") == 0) { dprintf(cfd, "in_call=%d at_dev=%d peer=%s\n", ctx->in_call ? 1 : 0, (md1_is_ready() || ctx->at_fd >= 0) ? 1 : 0, ctx->active_peer[0] ? ctx->active_peer : "none"); } else { dprintf(cfd, "ERR unknown\n"); } } static void handle_at_urc(const char *atbuf) { 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); return; } const char *cmt = strstr(atbuf, "+CMT:"); if (cmt) { char sender[64] = "unknown"; char body[256] = ""; const char *q1 = strchr(cmt, '"'); if (q1) { const char *q2 = strchr(q1 + 1, '"'); if (q2 && (size_t)(q2 - q1 - 1) < sizeof(sender)) { memcpy(sender, q1 + 1, (size_t)(q2 - q1 - 1)); sender[q2 - q1 - 1] = '\0'; } } const char *nl = strchr(cmt, '\n'); if (nl) { nl++; size_t i = 0; while (nl[i] && nl[i] != '\r' && nl[i] != '\n' && i + 1 < sizeof(body)) { body[i] = nl[i]; i++; } body[i] = '\0'; } save_sms_row("IN", sender, body[0] ? body : "Message received"); const char *ntf[] = {"notify-send", "New SMS", body[0] ? body : "Message received", NULL}; spawn_bg(ntf); } } 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; } int main(void) { signal(SIGPIPE, SIG_IGN); modem_ctx_t ctx; memset(&ctx, 0, sizeof(ctx)); ctx.srv_fd = -1; ctx.at_fd = -1; ctx.dbus_fd = -1; ctx.dbus_pid = -1; mkdir("/run/geminux", 0755); int lock_fd = open("/tmp/geminux-modem-ctl.lock", O_CREAT | O_RDWR | O_CLOEXEC, 0600); if (lock_fd >= 0 && flock(lock_fd, LOCK_EX | LOCK_NB) != 0) { close(lock_fd); return 0; } 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); ctx.srv_fd = bind_modem_socket(&ctx); 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); start_dbus_monitor(&ctx); write_state(&ctx); struct epoll_event events[8]; bool running = true; while (running) { int n = epoll_wait(ctx.epfd, events, 8, 15000); if (n < 0) { if (errno == EINTR) continue; break; } if (n == 0) { write_state(&ctx); if (ctx.dbus_fd < 0) start_dbus_monitor(&ctx); continue; } 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) { 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; } int cfd; while ((cfd = accept4(ctx.srv_fd, NULL, NULL, SOCK_CLOEXEC)) >= 0) { handle_cmd(&ctx, cfd); close(cfd); } 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; } } else if (fd == ctx.at_fd) { 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; } char atbuf[512] = {0}; ssize_t r = read(ctx.at_fd, atbuf, sizeof(atbuf) - 1); 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) { handle_at_urc(atbuf); } } else if (fd == ctx.dbus_fd) { char buf[1024]; ssize_t r; bool dead = false; while ((r = read(ctx.dbus_fd, buf, sizeof(buf))) > 0) { for (ssize_t k = 0; k < r; k++) { char ch = buf[k]; if (ch == '\r') continue; if (ch == '\n') { ctx.dbus_line[ctx.dbus_len] = '\0'; if (ctx.dbus_len > 0) handle_dbus_line(&ctx, ctx.dbus_line); ctx.dbus_len = 0; } else if (ctx.dbus_len + 1 < sizeof(ctx.dbus_line)) { ctx.dbus_line[ctx.dbus_len++] = ch; } } } if (r == 0 || (r < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) { dead = true; } if (dead || (events[i].events & (EPOLLHUP | EPOLLERR))) { stop_dbus_monitor(&ctx); } } } } stop_hostless_speech(&ctx); stop_dbus_monitor(&ctx); if (ctx.sock_path[0]) unlink(ctx.sock_path); close(ctx.epfd); return 0; }