Add MT6797 MD1 CCCI boot/RPC/FS, lpac eSIM, voice audio, and CONSYS Wi-Fi fix
This commit is contained in:
1 parent
1b93479666
commit
92a31539e6
34 files changed
+8556
-117
No files matched your search
@@ -1,12 +1,16 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <alsa/asoundlib.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <net/if.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.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>
|
||||
@@ -19,18 +23,31 @@
|
||||
#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"
|
||||
#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[]) {
|
||||
@@ -54,17 +71,100 @@ static void spawn_bg(const char *const argv[]) {
|
||||
if (pid > 0) waitpid(pid, NULL, 0);
|
||||
}
|
||||
|
||||
static void write_state(const modem_ctx_t *ctx) {
|
||||
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 void write_state(modem_ctx_t *ctx) {
|
||||
char next[96];
|
||||
if (ctx->in_call) {
|
||||
snprintf(next, sizeof(next), "CALL:%s\n",
|
||||
ctx->active_peer[0] ? ctx->active_peer : "ACTIVE");
|
||||
} 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;
|
||||
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");
|
||||
}
|
||||
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) {
|
||||
@@ -72,9 +172,78 @@ static void send_at(modem_ctx_t *ctx, const char *cmd) {
|
||||
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 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) {
|
||||
@@ -83,7 +252,7 @@ static void save_sms_row(const char *dir_tag, const char *peer, const char *text
|
||||
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);
|
||||
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);
|
||||
@@ -96,7 +265,10 @@ static void save_sms_row(const char *dir_tag, const char *peer, const char *text
|
||||
|
||||
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);
|
||||
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) {
|
||||
@@ -115,6 +287,166 @@ static void open_at_port(modem_ctx_t *ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -122,29 +454,59 @@ static void handle_cmd(modem_ctx_t *ctx, int cfd) {
|
||||
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);
|
||||
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);
|
||||
set_audio_verb("VoiceCall");
|
||||
char at[96];
|
||||
snprintf(at, sizeof(at), "ATD%s;", ctx->active_peer);
|
||||
send_at(ctx, at);
|
||||
apply_call_audio(ctx);
|
||||
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\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");
|
||||
apply_call_audio(ctx);
|
||||
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';
|
||||
send_at(ctx, "ATH");
|
||||
set_audio_verb("HiFi");
|
||||
ctx->last_call_path[0] = '\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);
|
||||
@@ -156,11 +518,24 @@ static void handle_cmd(modem_ctx_t *ctx, int cfd) {
|
||||
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);
|
||||
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]) {
|
||||
char at[32];
|
||||
snprintf(at, sizeof(at), "AT+VTS=\"%c\"", buf[5]);
|
||||
send_at(ctx, at);
|
||||
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, ' ');
|
||||
@@ -172,18 +547,60 @@ static void handle_cmd(modem_ctx_t *ctx, int cfd) {
|
||||
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};
|
||||
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 (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->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++) {
|
||||
@@ -211,6 +628,8 @@ int main(void) {
|
||||
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);
|
||||
|
||||
sigset_t mask;
|
||||
@@ -231,16 +650,22 @@ int main(void) {
|
||||
}
|
||||
}
|
||||
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, -1);
|
||||
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) {
|
||||
@@ -278,20 +703,37 @@ int main(void) {
|
||||
ctx.at_fd = -1;
|
||||
write_state(&ctx);
|
||||
} else 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);
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user