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
+420
-30
@@ -7,6 +7,7 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -31,6 +32,11 @@
|
||||
#define HIT_WIFI_DISC 12
|
||||
#define HIT_DNS_CUSTOM 13
|
||||
#define HIT_DNS_APPLY 14
|
||||
#define HIT_CELL_INPUT 15
|
||||
#define HIT_CELL_ADD 16
|
||||
#define HIT_CELL_USE 17
|
||||
#define HIT_CELL_DEL 18
|
||||
#define HIT_CELL_DATA 19
|
||||
|
||||
enum {
|
||||
WORK_NONE = 0,
|
||||
@@ -39,6 +45,12 @@ enum {
|
||||
WORK_WIFI_DISC,
|
||||
WORK_DNS_APPLY,
|
||||
WORK_DNS_TEST,
|
||||
WORK_ESIM_LIST,
|
||||
WORK_ESIM_DL,
|
||||
WORK_ESIM_USE,
|
||||
WORK_ESIM_DEL,
|
||||
WORK_CELL_DATA,
|
||||
WORK_CELL_CFG,
|
||||
WORK_BT_PWR,
|
||||
WORK_BT_SCAN,
|
||||
WORK_BT_CONN,
|
||||
@@ -52,17 +64,30 @@ typedef struct {
|
||||
char col3[48];
|
||||
} net_row_t;
|
||||
|
||||
typedef struct {
|
||||
char idx_tag[8];
|
||||
bool enabled;
|
||||
char masked_iccid[32];
|
||||
char label[128];
|
||||
} esim_ui_row_t;
|
||||
|
||||
typedef struct {
|
||||
gemui_app_t *app;
|
||||
int tab; /* 0=Wi-Fi, 1=DNS, 2=Cellular/4G, 3=Bluetooth, 4=USB/HDMI */
|
||||
int tab; /* 0=Wi-Fi, 1=DNS, 2=4G/eSIM, 3=Bluetooth, 4=USB/HDMI */
|
||||
bool wifi_on;
|
||||
bool wifi_connected;
|
||||
bool cellular_on;
|
||||
bool cell_data_up;
|
||||
bool cell_ims_on;
|
||||
bool cell_physical_sim;
|
||||
bool esim_loaded;
|
||||
bool bt_on;
|
||||
bool bt_svc_up;
|
||||
bool hdmi_on;
|
||||
int dns_mode;
|
||||
net_row_t rows[32];
|
||||
esim_ui_row_t esim_profiles[16];
|
||||
int esim_count;
|
||||
geminux_wifi_group_t wifi_groups[GEMINUX_MAX_WIFI_GROUPS];
|
||||
geminux_bt_dev_t bt_devs[GEMINUX_MAX_BT_DEVS];
|
||||
int num_rows;
|
||||
@@ -72,8 +97,14 @@ typedef struct {
|
||||
char psk[128];
|
||||
char active_dns[128];
|
||||
char custom_dns[192];
|
||||
char cell_slot[16];
|
||||
char cell_apn[64];
|
||||
char cell_ip[48];
|
||||
char cell_input[256];
|
||||
char cell_summary[128];
|
||||
bool focus_psk;
|
||||
bool focus_dns;
|
||||
bool focus_cell;
|
||||
bool show_psk;
|
||||
|
||||
pid_t worker_pid;
|
||||
@@ -94,7 +125,7 @@ static uint64_t net_now_ms(void) {
|
||||
|
||||
static int net_visible_rows(const net_state_t *st) {
|
||||
int h = st->app ? gemui_height(st->app) : 940;
|
||||
int foot_h = (st->tab == 0 || st->tab == 1 || st->tab == 3) ? 94 : 0;
|
||||
int foot_h = (st->tab <= 3) ? 94 : 0;
|
||||
int box_h = h - (92 + 82 + 88) - 20 - foot_h;
|
||||
int vis = (box_h - 8) / 72;
|
||||
return vis > 0 ? vis : 1;
|
||||
@@ -369,6 +400,149 @@ static void refresh_bt(net_state_t *st, const char *prev_mac) {
|
||||
}
|
||||
}
|
||||
|
||||
static void parse_esim_list_output(net_state_t *st, const char *raw) {
|
||||
st->esim_count = 0;
|
||||
st->esim_loaded = true;
|
||||
if (!raw || !raw[0]) return;
|
||||
|
||||
char tmp[4096];
|
||||
snprintf(tmp, sizeof(tmp), "%s", raw);
|
||||
char *save = NULL;
|
||||
for (char *ln = strtok_r(tmp, "\r\n", &save); ln; ln = strtok_r(NULL, "\r\n", &save)) {
|
||||
if (strcmp(ln, "NO_PROFILES") == 0) {
|
||||
st->esim_count = 0;
|
||||
return;
|
||||
}
|
||||
if (strncmp(ln, "PROFILE\t", 8) != 0) continue;
|
||||
if (st->esim_count >= 16) break;
|
||||
char *f1 = ln + 8;
|
||||
char *t2 = strchr(f1, '\t');
|
||||
if (!t2) continue;
|
||||
*t2++ = '\0';
|
||||
char *t3 = strchr(t2, '\t');
|
||||
if (!t3) continue;
|
||||
*t3++ = '\0';
|
||||
char *t4 = strchr(t3, '\t');
|
||||
if (!t4) continue;
|
||||
*t4++ = '\0';
|
||||
|
||||
esim_ui_row_t *p = &st->esim_profiles[st->esim_count++];
|
||||
memset(p, 0, sizeof(*p));
|
||||
snprintf(p->idx_tag, sizeof(p->idx_tag), "%s", f1);
|
||||
p->enabled = (strcmp(t2, "enabled") == 0);
|
||||
snprintf(p->masked_iccid, sizeof(p->masked_iccid), "%s", t3);
|
||||
snprintf(p->label, sizeof(p->label), "%s", t4[0] ? t4 : "eSIM Profile");
|
||||
}
|
||||
}
|
||||
|
||||
static void refresh_cellular(net_state_t *st) {
|
||||
char out[4096] = {0};
|
||||
st->scroll_off = 0;
|
||||
gemui_unix_cmd("/run/geminux/ccci.sock", "status", out, sizeof(out));
|
||||
st->cellular_on = strstr(out, "READY") != NULL;
|
||||
|
||||
snprintf(st->cell_slot, sizeof(st->cell_slot), "esim");
|
||||
snprintf(st->cell_apn, sizeof(st->cell_apn), "internet");
|
||||
st->cell_ims_on = true;
|
||||
st->cell_data_up = false;
|
||||
st->cell_physical_sim = false;
|
||||
st->cell_ip[0] = '\0';
|
||||
st->cell_summary[0] = '\0';
|
||||
|
||||
char op[64] = "", tech[32] = "", sig[16] = "", mmst[32] = "offline";
|
||||
out[0] = '\0';
|
||||
const char *st_cmd[] = {"doas", "/usr/sbin/geminux-cellctl", "status", NULL};
|
||||
if (gemui_run_cmd_timeout(st_cmd, out, sizeof(out), 1500) == 0 && out[0]) {
|
||||
char *save = NULL;
|
||||
for (char *ln = strtok_r(out, "\r\n", &save); ln; ln = strtok_r(NULL, "\r\n", &save)) {
|
||||
if (strncmp(ln, "slot=", 5) == 0) {
|
||||
snprintf(st->cell_slot, sizeof(st->cell_slot), "%s", ln + 5);
|
||||
st->cell_physical_sim = (strcmp(st->cell_slot, "physical") == 0);
|
||||
} else if (strncmp(ln, "apn=", 4) == 0) {
|
||||
snprintf(st->cell_apn, sizeof(st->cell_apn), "%s", ln + 4);
|
||||
} else if (strncmp(ln, "ims=", 4) == 0) {
|
||||
st->cell_ims_on = (strcmp(ln + 4, "0") != 0);
|
||||
} else if (strncmp(ln, "ccmni0_up=", 10) == 0) {
|
||||
st->cell_data_up = (atoi(ln + 10) != 0);
|
||||
} else if (strncmp(ln, "ccmni0_ip=", 10) == 0) {
|
||||
if (strcmp(ln + 10, "none") != 0) snprintf(st->cell_ip, sizeof(st->cell_ip), "%s", ln + 10);
|
||||
} else if (strncmp(ln, "mm_state=", 9) == 0) {
|
||||
snprintf(mmst, sizeof(mmst), "%s", ln + 9);
|
||||
} else if (strncmp(ln, "operator=", 9) == 0 && strcmp(ln + 9, "none") != 0) {
|
||||
snprintf(op, sizeof(op), "%s", ln + 9);
|
||||
} else if (strncmp(ln, "access_tech=", 12) == 0 && strcmp(ln + 12, "none") != 0) {
|
||||
snprintf(tech, sizeof(tech), "%s", ln + 12);
|
||||
} else if (strncmp(ln, "signal=", 7) == 0) {
|
||||
snprintf(sig, sizeof(sig), "%s", ln + 7);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int r = 0;
|
||||
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
|
||||
"SIM Slot: %s", st->cell_physical_sim ? "Physical Micro-SIM (Tray)" : "Soldered eSIM (ST33 eUICC)");
|
||||
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "%s",
|
||||
st->cell_physical_sim ? "Physical SIM" : "Embedded eSIM");
|
||||
snprintf(st->rows[r].col3, sizeof(st->rows[0].col3), "Switch Slot");
|
||||
r++;
|
||||
|
||||
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
|
||||
"4G Data (ccmni0) — APN: %s", st->cell_apn);
|
||||
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "%s",
|
||||
(st->cell_data_up && st->cell_ip[0]) ? st->cell_ip : "Disconnected");
|
||||
snprintf(st->rows[r].col3, sizeof(st->rows[0].col3), "%s",
|
||||
st->cell_data_up ? "Active" : "Down");
|
||||
r++;
|
||||
|
||||
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
|
||||
"IMS / VoLTE & VoWiFi Calling");
|
||||
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "%s",
|
||||
st->cell_ims_on ? "Enabled (CEVDP=3)" : "Disabled");
|
||||
snprintf(st->rows[r].col3, sizeof(st->rows[0].col3), "%s",
|
||||
st->cell_ims_on ? "ON" : "OFF");
|
||||
r++;
|
||||
|
||||
if (!st->esim_loaded) {
|
||||
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
|
||||
"eSIM Profiles: Press 'Scan eSIM Profiles' to query ST33");
|
||||
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "lpac AT Bridge");
|
||||
snprintf(st->rows[r].col3, sizeof(st->rows[0].col3), "Ready");
|
||||
r++;
|
||||
} else if (st->esim_count == 0) {
|
||||
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
|
||||
"eSIM Profiles: Empty (paste LPA:1$... below to install)");
|
||||
st->rows[r].col2[0] = '\0';
|
||||
st->rows[r].col3[0] = '\0';
|
||||
r++;
|
||||
} else {
|
||||
for (int i = 0; i < st->esim_count && r < 24; i++) {
|
||||
const esim_ui_row_t *p = &st->esim_profiles[i];
|
||||
snprintf(st->rows[r].col1, sizeof(st->rows[0].col1),
|
||||
"%seSIM %s: %.100s", p->enabled ? "* " : "", p->idx_tag, p->label);
|
||||
snprintf(st->rows[r].col2, sizeof(st->rows[0].col2), "%s",
|
||||
p->enabled ? "Enabled" : "Disabled");
|
||||
snprintf(st->rows[r].col3, sizeof(st->rows[0].col3), "%s", p->masked_iccid);
|
||||
r++;
|
||||
}
|
||||
}
|
||||
st->num_rows = r;
|
||||
if (st->sel_row < 0 || st->sel_row >= st->num_rows) st->sel_row = 0;
|
||||
net_clamp_scroll(st);
|
||||
|
||||
if (op[0]) {
|
||||
snprintf(st->status_text, sizeof(st->status_text),
|
||||
"MD1: %s | %.48s (%.24s %.8s%%) | %.36s",
|
||||
st->cellular_on ? "READY" : "GATED",
|
||||
op, tech[0] ? tech : mmst, sig[0] ? sig : "0",
|
||||
st->cell_data_up && st->cell_ip[0] ? st->cell_ip : st->cell_slot);
|
||||
} else {
|
||||
snprintf(st->status_text, sizeof(st->status_text),
|
||||
"MT6797 MD1: %s | State: %.24s | Slot: %s | Data: %.36s",
|
||||
st->cellular_on ? "READY" : "GATED", mmst, st->cell_slot,
|
||||
st->cell_data_up && st->cell_ip[0] ? st->cell_ip : "down");
|
||||
}
|
||||
}
|
||||
|
||||
static void refresh_tab(net_state_t *st) {
|
||||
char prev_key[96] = {0};
|
||||
if (st->tab == 0 && st->sel_row >= 0 && st->sel_row < st->num_rows) {
|
||||
@@ -378,7 +552,7 @@ static void refresh_tab(net_state_t *st) {
|
||||
}
|
||||
|
||||
st->num_rows = 0;
|
||||
st->sel_row = -1;
|
||||
if (st->tab != 2) st->sel_row = -1;
|
||||
static char out[16384];
|
||||
out[0] = '\0';
|
||||
|
||||
@@ -387,23 +561,7 @@ static void refresh_tab(net_state_t *st) {
|
||||
} else if (st->tab == 1) {
|
||||
refresh_dns(st);
|
||||
} else if (st->tab == 2) {
|
||||
st->scroll_off = 0;
|
||||
gemui_unix_cmd("/run/geminux/ccci.sock", "status", out, sizeof(out));
|
||||
st->cellular_on = strstr(out, "READY") != NULL;
|
||||
snprintf(st->status_text, sizeof(st->status_text), "CCCI Modem: %s",
|
||||
out[0] ? out : "Degraded / No MD1 firmware loaded");
|
||||
const char *mm[] = {"mmcli", "-m", "0", NULL};
|
||||
if (gemui_run_cmd_timeout(mm, out, sizeof(out), 900) == 0) {
|
||||
char *save = NULL;
|
||||
for (char *ln = strtok_r(out, "\n", &save); ln && st->num_rows < 20; ln = strtok_r(NULL, "\n", &save)) {
|
||||
while (*ln == ' ' || *ln == '|') ln++;
|
||||
if (!ln[0]) continue;
|
||||
snprintf(st->rows[st->num_rows].col1, sizeof(st->rows[0].col1), "%s", ln);
|
||||
st->rows[st->num_rows].col2[0] = '\0';
|
||||
st->rows[st->num_rows].col3[0] = '\0';
|
||||
st->num_rows++;
|
||||
}
|
||||
}
|
||||
refresh_cellular(st);
|
||||
} else if (st->tab == 3) {
|
||||
refresh_bt(st, prev_key);
|
||||
} else {
|
||||
@@ -429,6 +587,16 @@ static void refresh_tab(net_state_t *st) {
|
||||
|
||||
static void on_worker_timer(gemui_app_t *app, void *ud);
|
||||
|
||||
typedef int (*net_child_fn)(void *ctx);
|
||||
typedef struct {
|
||||
char cmd[32];
|
||||
char arg1[256];
|
||||
} cell_job_t;
|
||||
|
||||
static bool start_worker(net_state_t *st, int kind, const char *label,
|
||||
int timeout_ms, net_child_fn fn, void *ctx);
|
||||
static int child_cell_run(void *ctx);
|
||||
|
||||
static void cancel_worker(net_state_t *st, const char *toast) {
|
||||
if (st->worker_pid <= 0) return;
|
||||
kill(-st->worker_pid, SIGTERM);
|
||||
@@ -555,6 +723,48 @@ static void on_worker_fd(gemui_app_t *app, int fd, void *ud) {
|
||||
gemui_show_toast(app, rc == 0 ? "Bluetooth device disconnected" : "Disconnect failed", 2000);
|
||||
} else if (kind == WORK_BT_REM) {
|
||||
gemui_show_toast(app, rc == 0 ? "Removed Bluetooth device" : "Remove failed", 2000);
|
||||
} else if (kind == WORK_ESIM_LIST) {
|
||||
if (rc == 0) {
|
||||
parse_esim_list_output(st, st->worker_out);
|
||||
snprintf(toast, sizeof(toast), "ST33 eUICC: %d profile(s)", st->esim_count);
|
||||
gemui_show_toast(app, toast, 2200);
|
||||
} else {
|
||||
snprintf(toast, sizeof(toast), err[0] ? "eSIM scan: %.175s" : "eSIM scan failed", err);
|
||||
gemui_show_toast(app, toast, 3200);
|
||||
}
|
||||
} else if (kind == WORK_ESIM_DL || kind == WORK_ESIM_USE || kind == WORK_ESIM_DEL) {
|
||||
if (rc == 0) {
|
||||
if (kind == WORK_ESIM_DL) memset(st->cell_input, 0, sizeof(st->cell_input));
|
||||
const char *msg = (kind == WORK_ESIM_DL) ? "eSIM profile installed"
|
||||
: (kind == WORK_ESIM_DEL) ? "eSIM profile deleted"
|
||||
: "eSIM profile state updated";
|
||||
gemui_show_toast(app, msg, 2200);
|
||||
cell_job_t rjob;
|
||||
memset(&rjob, 0, sizeof(rjob));
|
||||
snprintf(rjob.cmd, sizeof(rjob.cmd), "esim-list");
|
||||
start_worker(st, WORK_ESIM_LIST, "Refreshing eSIM profiles...", 25000, child_cell_run, &rjob);
|
||||
return;
|
||||
} else {
|
||||
snprintf(toast, sizeof(toast), err[0] ? "eSIM error: %.175s" : "eSIM operation failed", err);
|
||||
gemui_show_toast(app, toast, 3400);
|
||||
}
|
||||
} else if (kind == WORK_CELL_DATA) {
|
||||
if (rc == 0) {
|
||||
gemui_show_toast(app, "4G LTE data state updated", 2200);
|
||||
} else {
|
||||
snprintf(toast, sizeof(toast), err[0] ? "4G error: %.175s" : "4G connection failed", err);
|
||||
gemui_show_toast(app, toast, 3400);
|
||||
}
|
||||
} else if (kind == WORK_CELL_CFG) {
|
||||
if (rc == 0) {
|
||||
if (strncmp(st->cell_input, "LPA:", 4) != 0 && strncmp(st->cell_input, "1$", 2) != 0) {
|
||||
memset(st->cell_input, 0, sizeof(st->cell_input));
|
||||
}
|
||||
gemui_show_toast(app, err[0] ? err : "Cellular setting updated", 2200);
|
||||
} else {
|
||||
snprintf(toast, sizeof(toast), err[0] ? "Cellular: %.175s" : "Cellular update failed", err);
|
||||
gemui_show_toast(app, toast, 3200);
|
||||
}
|
||||
}
|
||||
|
||||
refresh_tab(st);
|
||||
@@ -584,8 +794,6 @@ static void on_worker_timer(gemui_app_t *app, void *ud) {
|
||||
gemui_request_redraw(app);
|
||||
}
|
||||
|
||||
typedef int (*net_child_fn)(void *ctx);
|
||||
|
||||
static bool start_worker(net_state_t *st, int kind, const char *label,
|
||||
int timeout_ms, net_child_fn fn, void *ctx) {
|
||||
if (st->worker_pid > 0) {
|
||||
@@ -675,8 +883,26 @@ static void wifi_delete_ssid_profiles(const char *ssid) {
|
||||
}
|
||||
}
|
||||
|
||||
static void ensure_wifi_iface(void) {
|
||||
char air[16] = {0};
|
||||
if (gemui_read_file_line("/run/geminux/airplane-mode", air, sizeof(air)) == 0 && air[0] == '1') {
|
||||
return;
|
||||
}
|
||||
if (access("/sys/class/net/wlan0", F_OK) == 0) return;
|
||||
const char *rst[] = {"doas", "/usr/bin/geminux-power-ctl", "wifi-restore", NULL};
|
||||
gemui_run_cmd_timeout(rst, NULL, 0, 8000);
|
||||
for (int i = 0; i < 25 && access("/sys/class/net/wlan0", F_OK) != 0; i++) {
|
||||
poll(NULL, 0, 100);
|
||||
}
|
||||
if (access("/sys/class/net/wlan0", F_OK) == 0) {
|
||||
const char *on[] = {"nmcli", "radio", "wifi", "on", NULL};
|
||||
gemui_run_cmd_timeout(on, NULL, 0, 4000);
|
||||
}
|
||||
}
|
||||
|
||||
static int child_wifi_connect(void *ctx) {
|
||||
wifi_job_t *j = ctx;
|
||||
ensure_wifi_iface();
|
||||
char out[512] = {0};
|
||||
int cfg_rc = 0;
|
||||
|
||||
@@ -740,6 +966,7 @@ static int child_wifi_connect(void *ctx) {
|
||||
|
||||
static int child_wifi_rescan(void *ctx) {
|
||||
(void)ctx;
|
||||
ensure_wifi_iface();
|
||||
char out[256] = {0};
|
||||
const char *argv[] = {"nmcli", "dev", "wifi", "rescan", NULL};
|
||||
int rc = gemui_run_cmd_timeout(argv, out, sizeof(out), 8000);
|
||||
@@ -756,6 +983,99 @@ static int child_wifi_disconnect(void *ctx) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int child_cell_run(void *ctx) {
|
||||
const cell_job_t *j = ctx;
|
||||
char out[4096] = {0};
|
||||
const char *argv[5];
|
||||
int n = 0;
|
||||
argv[n++] = "doas";
|
||||
argv[n++] = "/usr/sbin/geminux-cellctl";
|
||||
argv[n++] = j->cmd;
|
||||
if (j->arg1[0]) argv[n++] = j->arg1;
|
||||
argv[n] = NULL;
|
||||
int rc = gemui_run_cmd_timeout(argv, out, sizeof(out), 95000);
|
||||
if (out[0]) fputs(out, stdout);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static void paste_clipboard_into(char *dst, size_t dst_sz) {
|
||||
char buf[512] = {0};
|
||||
const char *clip_argv[] = {"xclip", "-o", "-selection", "clipboard", NULL};
|
||||
if (gemui_run_cmd_timeout(clip_argv, buf, sizeof(buf), 400) != 0 || !buf[0]) {
|
||||
const char *prim_argv[] = {"xclip", "-o", "-selection", "primary", NULL};
|
||||
if (gemui_run_cmd_timeout(prim_argv, buf, sizeof(buf), 400) != 0 || !buf[0]) return;
|
||||
}
|
||||
size_t w = 0;
|
||||
for (size_t i = 0; buf[i] && w + 1 < dst_sz; i++) {
|
||||
unsigned char c = (unsigned char)buf[i];
|
||||
if (c < 0x20 || c == ' ' || c == '\t') continue;
|
||||
dst[w++] = (char)c;
|
||||
}
|
||||
if (w > 0) dst[w] = '\0';
|
||||
}
|
||||
|
||||
static void trigger_cell_add(net_state_t *st) {
|
||||
if (!st->cell_input[0]) {
|
||||
gemui_show_toast(st->app, "Enter an LPA:1$... code or APN first", 1800);
|
||||
st->focus_cell = true;
|
||||
gemui_request_redraw(st->app);
|
||||
return;
|
||||
}
|
||||
cell_job_t job;
|
||||
memset(&job, 0, sizeof(job));
|
||||
if (strncmp(st->cell_input, "LPA:", 4) == 0 || strncmp(st->cell_input, "lpa:", 4) == 0 ||
|
||||
strncmp(st->cell_input, "1$", 2) == 0) {
|
||||
snprintf(job.cmd, sizeof(job.cmd), "esim-download");
|
||||
snprintf(job.arg1, sizeof(job.arg1), "%s", st->cell_input);
|
||||
start_worker(st, WORK_ESIM_DL, "Downloading eSIM profile...", 95000, child_cell_run, &job);
|
||||
} else {
|
||||
snprintf(job.cmd, sizeof(job.cmd), "apn-set");
|
||||
snprintf(job.arg1, sizeof(job.arg1), "%s", st->cell_input);
|
||||
start_worker(st, WORK_CELL_CFG, "Saving cellular APN...", 8000, child_cell_run, &job);
|
||||
}
|
||||
}
|
||||
|
||||
static void trigger_cell_use(net_state_t *st) {
|
||||
cell_job_t job;
|
||||
memset(&job, 0, sizeof(job));
|
||||
if (st->sel_row == 0) {
|
||||
snprintf(job.cmd, sizeof(job.cmd), "sim-slot");
|
||||
snprintf(job.arg1, sizeof(job.arg1), "%s", st->cell_physical_sim ? "esim" : "physical");
|
||||
start_worker(st, WORK_CELL_CFG, "Switching SIM slot...", 12000, child_cell_run, &job);
|
||||
} else if (st->sel_row == 1) {
|
||||
snprintf(job.cmd, sizeof(job.cmd), "%s", st->cell_data_up ? "data-down" : "data-up");
|
||||
start_worker(st, WORK_CELL_DATA,
|
||||
st->cell_data_up ? "Disconnecting 4G LTE..." : "Connecting 4G LTE (ccmni0)...",
|
||||
45000, child_cell_run, &job);
|
||||
} else if (st->sel_row == 2) {
|
||||
snprintf(job.cmd, sizeof(job.cmd), "ims-set");
|
||||
snprintf(job.arg1, sizeof(job.arg1), "%s", st->cell_ims_on ? "off" : "on");
|
||||
start_worker(st, WORK_CELL_CFG, "Updating IMS / VoLTE...", 12000, child_cell_run, &job);
|
||||
} else if (st->esim_loaded && st->sel_row >= 3 && st->sel_row < 3 + st->esim_count) {
|
||||
const esim_ui_row_t *p = &st->esim_profiles[st->sel_row - 3];
|
||||
snprintf(job.cmd, sizeof(job.cmd), "%s", p->enabled ? "esim-disable" : "esim-enable");
|
||||
snprintf(job.arg1, sizeof(job.arg1), "%s", p->idx_tag);
|
||||
start_worker(st, WORK_ESIM_USE,
|
||||
p->enabled ? "Disabling eSIM profile..." : "Enabling eSIM profile...",
|
||||
30000, child_cell_run, &job);
|
||||
} else {
|
||||
gemui_show_toast(st->app, "Select Slot, 4G Data, IMS, or an eSIM profile", 1800);
|
||||
}
|
||||
}
|
||||
|
||||
static void trigger_cell_del(net_state_t *st) {
|
||||
if (!st->esim_loaded || st->sel_row < 3 || st->sel_row >= 3 + st->esim_count) {
|
||||
gemui_show_toast(st->app, "Select an eSIM profile row to delete", 1800);
|
||||
return;
|
||||
}
|
||||
const esim_ui_row_t *p = &st->esim_profiles[st->sel_row - 3];
|
||||
cell_job_t job;
|
||||
memset(&job, 0, sizeof(job));
|
||||
snprintf(job.cmd, sizeof(job.cmd), "esim-delete");
|
||||
snprintf(job.arg1, sizeof(job.arg1), "%s", p->idx_tag);
|
||||
start_worker(st, WORK_ESIM_DEL, "Deleting eSIM profile...", 30000, child_cell_run, &job);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
int mode;
|
||||
char custom[192];
|
||||
@@ -999,7 +1319,7 @@ static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
|
||||
int pad = 20;
|
||||
int top = 92;
|
||||
int tab_w = (w - pad * 2 - 4 * 12) / 5;
|
||||
static const char *tabs[5] = {"Wi-Fi", "DNS", "4G / CCCI", "Bluetooth", "USB & HDMI"};
|
||||
static const char *tabs[5] = {"Wi-Fi", "DNS", "4G / eSIM", "Bluetooth", "USB & HDMI"};
|
||||
for (int i = 0; i < 5; i++) {
|
||||
gemui_draw_button(app, cr, (gemui_rect_t){pad + i * (tab_w + 12), top, tab_w, 66},
|
||||
tabs[i], HIT_TAB, i, false, st->tab == i, false);
|
||||
@@ -1022,9 +1342,10 @@ static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
|
||||
dns_act, HIT_ACTION, 1, st->worker_pid == 0, st->worker_pid > 0, false);
|
||||
} else if (st->tab == 2) {
|
||||
gemui_draw_toggle(app, cr, (gemui_rect_t){pad, cy, ctrl_w, 72},
|
||||
"MT6797 Modem Active", st->cellular_on, HIT_TOGGLE, 2, false);
|
||||
"MT6797 Cellular Radio", st->cellular_on, HIT_TOGGLE, 2, false);
|
||||
const char *esim_act = (st->worker_pid > 0) ? "Cancel Operation" : "Scan eSIM Profiles";
|
||||
gemui_draw_button(app, cr, (gemui_rect_t){pad + ctrl_w + 12, cy, ctrl_w, 72},
|
||||
"Reset CCCI Modem", HIT_ACTION, 2, false, false, false);
|
||||
esim_act, HIT_ACTION, 2, st->worker_pid == 0, st->worker_pid > 0, false);
|
||||
} else if (st->tab == 3) {
|
||||
gemui_draw_toggle(app, cr, (gemui_rect_t){pad, cy, ctrl_w, 72},
|
||||
"Bluetooth Radio", st->bt_on, HIT_TOGGLE, 3, false);
|
||||
@@ -1039,7 +1360,7 @@ static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
|
||||
"Mirror Primary Display", HIT_ACTION, 4, true, false, false);
|
||||
}
|
||||
|
||||
int foot_h = (st->tab == 0 || st->tab == 1 || st->tab == 3) ? 94 : 0;
|
||||
int foot_h = (st->tab <= 3) ? 94 : 0;
|
||||
gemui_rect_t box = {pad, cy + 88, w - pad * 2, h - (cy + 88) - pad - foot_h};
|
||||
gemui_fill_round(cr, box, 12, t->bg_bar);
|
||||
gemui_stroke_round(cr, box, 12, 1.5, t->border);
|
||||
@@ -1093,6 +1414,24 @@ static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
|
||||
st->custom_dns, false, st->focus_dns, HIT_DNS_CUSTOM, 0);
|
||||
gemui_draw_button(app, cr, (gemui_rect_t){w - pad - aw, h - 92, aw, 72},
|
||||
"Apply DNS Profile", HIT_DNS_APPLY, 0, true, false, false);
|
||||
} else if (st->tab == 2) {
|
||||
int dw = 210;
|
||||
int rmw = 180;
|
||||
int usew = 220;
|
||||
int addw = 210;
|
||||
int iw = w - pad * 2 - dw - rmw - usew - addw - 48;
|
||||
gemui_draw_input(app, cr, (gemui_rect_t){pad, h - 92, iw, 72},
|
||||
"Paste LPA:1$smdp$code (Ctrl+V) or enter APN...",
|
||||
st->cell_input, false, st->focus_cell, HIT_CELL_INPUT, 0);
|
||||
gemui_draw_button(app, cr, (gemui_rect_t){pad + iw + 12, h - 92, addw, 72},
|
||||
"Add eSIM / APN", HIT_CELL_ADD, 0, true, false, false);
|
||||
gemui_draw_button(app, cr, (gemui_rect_t){pad + iw + addw + 24, h - 92, usew, 72},
|
||||
"Enable / Toggle", HIT_CELL_USE, 0, false, false, false);
|
||||
gemui_draw_button(app, cr, (gemui_rect_t){pad + iw + addw + usew + 36, h - 92, rmw, 72},
|
||||
"Delete eSIM", HIT_CELL_DEL, 0, false, false, false);
|
||||
gemui_draw_button(app, cr, (gemui_rect_t){w - pad - dw, h - 92, dw, 72},
|
||||
st->cell_data_up ? "Disconnect 4G" : "Connect 4G",
|
||||
HIT_CELL_DATA, 0, !st->cell_data_up, st->cell_data_up, false);
|
||||
} else if (st->tab == 3) {
|
||||
int bw = (w - pad * 2 - 3 * 12) / 4;
|
||||
gemui_draw_button(app, cr, (gemui_rect_t){pad, h - 92, bw, 72},
|
||||
@@ -1114,6 +1453,7 @@ static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *
|
||||
st->tab = (st->tab + 1) % 5;
|
||||
st->focus_psk = false;
|
||||
st->focus_dns = false;
|
||||
st->focus_cell = false;
|
||||
refresh_tab(st);
|
||||
gemui_request_redraw(app);
|
||||
} else if (ks == XK_Up && st->num_rows > 0) {
|
||||
@@ -1129,7 +1469,10 @@ static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *
|
||||
} else if (ks == XK_Return || ks == XK_KP_Enter) {
|
||||
if (st->tab == 0) connect_selected_wifi(st);
|
||||
else if (st->tab == 1) apply_selected_dns(st);
|
||||
else if (st->tab == 3) start_bt_dev_action(st, WORK_BT_CONN);
|
||||
else if (st->tab == 2) {
|
||||
if (st->focus_cell && st->cell_input[0]) trigger_cell_add(st);
|
||||
else trigger_cell_use(st);
|
||||
} else if (st->tab == 3) start_bt_dev_action(st, WORK_BT_CONN);
|
||||
} else if ((ks == XK_BackSpace || ks == XK_Delete) && st->tab == 0 && (st->focus_psk || st->sel_row >= 0)) {
|
||||
st->focus_psk = true;
|
||||
gemui_utf8_pop_char(st->psk);
|
||||
@@ -1139,6 +1482,10 @@ static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *
|
||||
st->sel_row = GEMINUX_DNS_CUSTOM;
|
||||
gemui_utf8_pop_char(st->custom_dns);
|
||||
gemui_request_redraw(app);
|
||||
} else if ((ks == XK_BackSpace || ks == XK_Delete) && st->tab == 2 && (st->focus_cell || st->cell_input[0])) {
|
||||
st->focus_cell = true;
|
||||
gemui_utf8_pop_char(st->cell_input);
|
||||
gemui_request_redraw(app);
|
||||
} else if ((state & ControlMask) && (ks == XK_u || ks == XK_U) && st->tab == 0) {
|
||||
st->focus_psk = true;
|
||||
memset(st->psk, 0, sizeof(st->psk));
|
||||
@@ -1148,6 +1495,14 @@ static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *
|
||||
st->sel_row = GEMINUX_DNS_CUSTOM;
|
||||
memset(st->custom_dns, 0, sizeof(st->custom_dns));
|
||||
gemui_request_redraw(app);
|
||||
} else if ((state & ControlMask) && (ks == XK_u || ks == XK_U) && st->tab == 2) {
|
||||
st->focus_cell = true;
|
||||
memset(st->cell_input, 0, sizeof(st->cell_input));
|
||||
gemui_request_redraw(app);
|
||||
} else if ((state & ControlMask) && (ks == XK_v || ks == XK_V) && st->tab == 2) {
|
||||
paste_clipboard_into(st->cell_input, sizeof(st->cell_input));
|
||||
st->focus_cell = true;
|
||||
gemui_request_redraw(app);
|
||||
} else if (st->tab == 0 && (st->focus_psk || st->sel_row >= 0) && utf8 && utf8[0]) {
|
||||
st->focus_psk = true;
|
||||
size_t cur = strlen(st->psk), add = strlen(utf8);
|
||||
@@ -1163,6 +1518,13 @@ static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *
|
||||
memcpy(st->custom_dns + cur, utf8, add + 1);
|
||||
gemui_request_redraw(app);
|
||||
}
|
||||
} else if (st->tab == 2 && utf8 && utf8[0]) {
|
||||
st->focus_cell = true;
|
||||
size_t cur = strlen(st->cell_input), add = strlen(utf8);
|
||||
if (cur + add < sizeof(st->cell_input)) {
|
||||
memcpy(st->cell_input + cur, utf8, add + 1);
|
||||
gemui_request_redraw(app);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1190,11 +1552,13 @@ static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, v
|
||||
st->tab = hit->val;
|
||||
st->focus_psk = false;
|
||||
st->focus_dns = false;
|
||||
st->focus_cell = false;
|
||||
refresh_tab(st);
|
||||
} else if (hit->id == HIT_ROW) {
|
||||
st->sel_row = hit->val;
|
||||
if (st->tab == 0) st->focus_psk = true;
|
||||
else if (st->tab == 1) st->focus_dns = (hit->val == GEMINUX_DNS_CUSTOM);
|
||||
else if (st->tab == 2) st->focus_cell = false;
|
||||
} else if (hit->id == HIT_PSK) {
|
||||
st->focus_psk = true;
|
||||
} else if (hit->id == HIT_SHOW_PSK) {
|
||||
@@ -1212,6 +1576,25 @@ static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, v
|
||||
} else if (hit->id == HIT_DNS_APPLY) {
|
||||
apply_selected_dns(st);
|
||||
return;
|
||||
} else if (hit->id == HIT_CELL_INPUT) {
|
||||
st->focus_cell = true;
|
||||
} else if (hit->id == HIT_CELL_ADD) {
|
||||
trigger_cell_add(st);
|
||||
return;
|
||||
} else if (hit->id == HIT_CELL_USE) {
|
||||
trigger_cell_use(st);
|
||||
return;
|
||||
} else if (hit->id == HIT_CELL_DEL) {
|
||||
trigger_cell_del(st);
|
||||
return;
|
||||
} else if (hit->id == HIT_CELL_DATA) {
|
||||
cell_job_t job;
|
||||
memset(&job, 0, sizeof(job));
|
||||
snprintf(job.cmd, sizeof(job.cmd), "%s", st->cell_data_up ? "data-down" : "data-up");
|
||||
start_worker(st, WORK_CELL_DATA,
|
||||
st->cell_data_up ? "Disconnecting 4G LTE..." : "Connecting 4G LTE (ccmni0)...",
|
||||
45000, child_cell_run, &job);
|
||||
return;
|
||||
} else if (hit->id == HIT_BT_CONN) {
|
||||
start_bt_dev_action(st, WORK_BT_CONN);
|
||||
return;
|
||||
@@ -1267,9 +1650,16 @@ static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, v
|
||||
}
|
||||
return;
|
||||
} else if (hit->val == 2) {
|
||||
gemui_unix_cmd("/run/geminux/ccci.sock", "reset", NULL, 0);
|
||||
gemui_show_toast(app, "Sent CCCI reset request", 1600);
|
||||
refresh_tab(st);
|
||||
if (st->worker_pid > 0) {
|
||||
cancel_worker(st, "Cancelled operation");
|
||||
} else {
|
||||
cell_job_t job;
|
||||
memset(&job, 0, sizeof(job));
|
||||
snprintf(job.cmd, sizeof(job.cmd), "esim-list");
|
||||
start_worker(st, WORK_ESIM_LIST, "Scanning ST33 eUICC profiles...", 25000,
|
||||
child_cell_run, &job);
|
||||
}
|
||||
return;
|
||||
} else if (hit->val == 3) {
|
||||
if (st->worker_pid > 0) {
|
||||
cancel_worker(st, st->worker_kind == WORK_BT_SCAN ? "Bluetooth scan stopped" : "Cancelled");
|
||||
|
||||
@@ -11,6 +11,8 @@ XTOUCH_CFLAGS = $(shell $(PKG_CONFIG) --cflags x11 xi xtst)
|
||||
XTOUCH_LIBS = $(shell $(PKG_CONFIG) --libs x11 xi xtst) -lm
|
||||
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)
|
||||
ALSA_CFLAGS = $(shell $(PKG_CONFIG) --cflags alsa)
|
||||
ALSA_LIBS = $(shell $(PKG_CONFIG) --libs alsa)
|
||||
|
||||
BINS = \
|
||||
geminux-powerd \
|
||||
@@ -19,6 +21,7 @@ BINS = \
|
||||
geminux-touchd \
|
||||
geminux-ccci-daemon \
|
||||
geminux-modem-ctl \
|
||||
geminux-cellctl \
|
||||
geminux-bt-obex \
|
||||
geminux-leds \
|
||||
geminux-power-audit \
|
||||
@@ -41,11 +44,14 @@ geminux-statusd: geminux-statusd.c geminux-power-policy.h geminux-tz-valid.h
|
||||
geminux-touchd: geminux-touchd.c geminux-touch-gesture.c geminux-touch-gesture.h
|
||||
$(CC) $(CFLAGS) $(XTOUCH_CFLAGS) -o $@ geminux-touchd.c geminux-touch-gesture.c $(XTOUCH_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-ccci-daemon: geminux-ccci-daemon.c geminux-ccci-proto.c geminux-ccci-proto.h geminux-ccci-fs.c geminux-ccci-fs.h
|
||||
$(CC) $(CFLAGS) -o $@ geminux-ccci-daemon.c geminux-ccci-proto.c geminux-ccci-fs.c
|
||||
|
||||
geminux-modem-ctl: geminux-modem-ctl.c
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
$(CC) $(CFLAGS) $(ALSA_CFLAGS) -o $@ $< $(ALSA_LIBS)
|
||||
|
||||
geminux-cellctl: geminux-cellctl.c geminux-cell-proto.c geminux-cell-proto.h
|
||||
$(CC) $(CFLAGS) -o $@ geminux-cellctl.c geminux-cell-proto.c
|
||||
|
||||
geminux-bt-obex: geminux-bt-obex.c
|
||||
$(CC) $(CFLAGS) $(GIO_CFLAGS) -o $@ $< $(GIO_LIBS)
|
||||
@@ -72,7 +78,7 @@ install: all
|
||||
mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/sbin
|
||||
cp -f geminux-power-ctl geminux-statusd geminux-touchd geminux-modem-ctl \
|
||||
geminux-bt-obex geminux-power-audit $(DESTDIR)$(PREFIX)/bin/
|
||||
cp -f geminux-powerd geminux-ccci-daemon geminux-leds \
|
||||
cp -f geminux-powerd geminux-ccci-daemon geminux-cellctl geminux-leds \
|
||||
geminux-checkpw geminux-wifi-nvram geminux-tzctl geminux-dnsctl $(DESTDIR)$(PREFIX)/sbin/
|
||||
chmod 0755 $(DESTDIR)$(PREFIX)/bin/geminux-* $(DESTDIR)$(PREFIX)/sbin/geminux-*
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
#define _GNU_SOURCE
|
||||
#include "geminux-ccci-fs.h"
|
||||
#include "geminux-ccci-proto.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <grp.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@@ -19,6 +21,8 @@
|
||||
#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"
|
||||
#define FW_MD1_PART "/dev/disk/by-partlabel/md1img"
|
||||
#define CCCI_LOG "/var/log/geminux-ccci.log"
|
||||
|
||||
typedef struct {
|
||||
int epfd;
|
||||
@@ -27,10 +31,10 @@ typedef struct {
|
||||
int uev_fd;
|
||||
int ctl_fd;
|
||||
int fs_fd;
|
||||
int rpc_fd;
|
||||
bool fw_present;
|
||||
bool stage_attempted;
|
||||
geminux_ccci_sm_t sm;
|
||||
geminux_ccci_fs_t fs;
|
||||
} ccci_ctx_t;
|
||||
|
||||
static void add_ep(int epfd, int fd) {
|
||||
@@ -39,6 +43,13 @@ static void add_ep(int epfd, int fd) {
|
||||
epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
|
||||
}
|
||||
|
||||
static void refresh_md_state(ccci_ctx_t *ctx) {
|
||||
if (ctx->ctl_fd < 0) return;
|
||||
__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;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -69,6 +80,10 @@ static void maybe_advance_ccci_stage(ccci_ctx_t *ctx) {
|
||||
}
|
||||
|
||||
static void open_ccci_devs(ccci_ctx_t *ctx) {
|
||||
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->ctl_fd < 0) {
|
||||
ctx->ctl_fd = open("/dev/ccci_monitor", O_RDWR | O_NONBLOCK | O_CLOEXEC);
|
||||
if (ctx->ctl_fd < 0)
|
||||
@@ -76,21 +91,10 @@ static void open_ccci_devs(ccci_ctx_t *ctx) {
|
||||
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;
|
||||
}
|
||||
refresh_md_state(ctx);
|
||||
}
|
||||
}
|
||||
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);
|
||||
ctx->fw_present = (access(FW_MD1_PART, R_OK) == 0 || access(FW_MD1_IMG, R_OK) == 0);
|
||||
}
|
||||
|
||||
static void handle_ctl_read(ccci_ctx_t *ctx) {
|
||||
@@ -107,20 +111,31 @@ static void handle_ctl_read(ccci_ctx_t *ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_fs_or_rpc(ccci_ctx_t *ctx, int fd) {
|
||||
static int send_fs_packet(const uint8_t *pkt, size_t pkt_len, void *user) {
|
||||
ccci_ctx_t *ctx = (ccci_ctx_t *)user;
|
||||
if (!ctx || ctx->fs_fd < 0) return -1;
|
||||
|
||||
for (int attempt = 0; attempt < 25; attempt++) {
|
||||
ssize_t w = write(ctx->fs_fd, pkt, pkt_len);
|
||||
if (w == (ssize_t)pkt_len) {
|
||||
ctx->sm.tx_packets++;
|
||||
return 0;
|
||||
}
|
||||
if (w < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
|
||||
return -1;
|
||||
struct pollfd pfd = { .fd = ctx->fs_fd, .events = POLLOUT };
|
||||
poll(&pfd, 1, 10);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void handle_fs_read(ccci_ctx_t *ctx) {
|
||||
uint8_t buf[4096];
|
||||
ssize_t n;
|
||||
while ((n = read(fd, buf, sizeof(buf))) >= (ssize_t)sizeof(struct mt6797_ccci_header)) {
|
||||
while ((n = read(ctx->fs_fd, buf, sizeof(buf))) >= (ssize_t)GEMINUX_CCCI_FS_HDR_LEN) {
|
||||
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;
|
||||
(void)geminux_ccci_fs_process_packet(&ctx->fs, buf, (size_t)n,
|
||||
send_fs_packet, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,6 +149,7 @@ static void handle_client(ccci_ctx_t *ctx, int cfd) {
|
||||
struct mt6797_ccci_status st;
|
||||
memset(&st, 0, sizeof(st));
|
||||
if (ctx->ctl_fd >= 0) ioctl(ctx->ctl_fd, CCCI_IOC_GET_STATUS, &st);
|
||||
refresh_md_state(ctx);
|
||||
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,
|
||||
@@ -141,6 +157,7 @@ static void handle_client(ccci_ctx_t *ctx, int cfd) {
|
||||
ctx->sm.flight_mode ? 1 : 0,
|
||||
ctx->sm.tx_packets, ctx->sm.rx_packets);
|
||||
} else if (strcmp(buf, "reset") == 0) {
|
||||
geminux_ccci_fs_reset(&ctx->fs);
|
||||
if (ctx->ctl_fd >= 0) {
|
||||
ioctl(ctx->ctl_fd, CCCI_IOC_MD_RESET);
|
||||
ctx->sm.state = MT6797_MD_STATE_RESET;
|
||||
@@ -150,8 +167,10 @@ static void handle_client(ccci_ctx_t *ctx, int cfd) {
|
||||
}
|
||||
} else if (strcmp(buf, "boot") == 0) {
|
||||
ctx->sm.flight_mode = false;
|
||||
geminux_ccci_fs_reset(&ctx->fs);
|
||||
if (advance_ccci_stage(3) == 0) {
|
||||
ctx->stage_attempted = true;
|
||||
refresh_md_state(ctx);
|
||||
dprintf(cfd, "OK stage=3\n");
|
||||
} else {
|
||||
dprintf(cfd, "DEGRADED stage=3 unavailable\n");
|
||||
@@ -174,8 +193,9 @@ int main(void) {
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
ccci_ctx_t ctx;
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
ctx.ctl_fd = ctx.fs_fd = ctx.rpc_fd = -1;
|
||||
ctx.ctl_fd = ctx.fs_fd = -1;
|
||||
geminux_ccci_sm_init(&ctx.sm);
|
||||
geminux_ccci_fs_init(&ctx.fs, NULL, CCCI_LOG);
|
||||
|
||||
mkdir("/run/geminux", 0755);
|
||||
mkdir("/var/lib/geminux", 0755);
|
||||
@@ -203,7 +223,7 @@ int main(void) {
|
||||
}
|
||||
}
|
||||
|
||||
ctx.uev_fd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
|
||||
ctx.uev_fd = socket(PF_NETLINK, SOCK_DGRAM | O_NONBLOCK | O_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));
|
||||
@@ -244,17 +264,18 @@ int main(void) {
|
||||
need_ccci = true;
|
||||
}
|
||||
}
|
||||
if (need_ccci && (ctx.ctl_fd < 0 || ctx.fs_fd < 0 || ctx.rpc_fd < 0)) {
|
||||
if (need_ccci && (ctx.ctl_fd < 0 || ctx.fs_fd < 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);
|
||||
} else if (fd == ctx.fs_fd) {
|
||||
handle_fs_read(&ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
geminux_ccci_fs_reset(&ctx.fs);
|
||||
unlink(SOCK_PATH);
|
||||
close(ctx.epfd);
|
||||
return 0;
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,90 @@
|
||||
#ifndef GEMINUX_CCCI_FS_H
|
||||
#define GEMINUX_CCCI_FS_H
|
||||
|
||||
#include <linux/mt6797_ccci.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define GEMINUX_CCCI_FS_MORE 0x80000000U
|
||||
#define GEMINUX_CCCI_FS_REPLY_MASK 0xFFFF0000U
|
||||
#define GEMINUX_CCCI_FS_MAX_SLOTS 5U
|
||||
#define GEMINUX_CCCI_FS_MAX_CHUNK 3456U
|
||||
#define GEMINUX_CCCI_FS_MAX_STREAM 16896U
|
||||
#define GEMINUX_CCCI_FS_MAX_FDS 64U
|
||||
#define GEMINUX_CCCI_FS_MAX_FINDS 16U
|
||||
#define GEMINUX_CCCI_FS_HDR_LEN 20U
|
||||
|
||||
enum geminux_ccci_fs_op {
|
||||
GEMINUX_CCCI_FS_OP_OPEN = 0x1001U,
|
||||
GEMINUX_CCCI_FS_OP_SEEK = 0x1002U,
|
||||
GEMINUX_CCCI_FS_OP_READ = 0x1003U,
|
||||
GEMINUX_CCCI_FS_OP_WRITE = 0x1004U,
|
||||
GEMINUX_CCCI_FS_OP_CLOSE = 0x1005U,
|
||||
GEMINUX_CCCI_FS_OP_CLOSEALL = 0x1006U,
|
||||
GEMINUX_CCCI_FS_OP_CREATEDIR = 0x1007U,
|
||||
GEMINUX_CCCI_FS_OP_REMOVEDIR = 0x1008U,
|
||||
GEMINUX_CCCI_FS_OP_GETFILESIZE = 0x1009U,
|
||||
GEMINUX_CCCI_FS_OP_GETFOLDERSIZE = 0x100AU,
|
||||
GEMINUX_CCCI_FS_OP_RENAME = 0x100BU,
|
||||
GEMINUX_CCCI_FS_OP_MOVE = 0x100CU,
|
||||
GEMINUX_CCCI_FS_OP_COUNT = 0x100DU,
|
||||
GEMINUX_CCCI_FS_OP_GETDISKINFO = 0x100EU,
|
||||
GEMINUX_CCCI_FS_OP_DELETE = 0x100FU,
|
||||
GEMINUX_CCCI_FS_OP_GETATTRIBUTES = 0x1010U,
|
||||
GEMINUX_CCCI_FS_OP_OPENHINT = 0x1011U,
|
||||
GEMINUX_CCCI_FS_OP_FINDFIRST = 0x1012U,
|
||||
GEMINUX_CCCI_FS_OP_FINDNEXT = 0x1013U,
|
||||
GEMINUX_CCCI_FS_OP_FINDCLOSE = 0x1014U,
|
||||
GEMINUX_CCCI_FS_OP_LOCKFAT = 0x1015U,
|
||||
GEMINUX_CCCI_FS_OP_UNLOCKALL = 0x1016U,
|
||||
GEMINUX_CCCI_FS_OP_SHUTDOWN = 0x1017U,
|
||||
GEMINUX_CCCI_FS_OP_XDELETE = 0x1018U,
|
||||
GEMINUX_CCCI_FS_OP_CLEARDISKFLAG = 0x1019U,
|
||||
GEMINUX_CCCI_FS_OP_GETDRIVE = 0x101AU,
|
||||
GEMINUX_CCCI_FS_OP_GETCLUSTERSIZE = 0x101BU,
|
||||
GEMINUX_CCCI_FS_OP_CHECKDISK = 0x101CU,
|
||||
GEMINUX_CCCI_FS_OP_RESTORE = 0x1021U,
|
||||
GEMINUX_CCCI_FS_OP_CMPT_READ = 0x1022U,
|
||||
};
|
||||
|
||||
typedef int (*geminux_ccci_fs_send_fn)(const uint8_t *pkt, size_t pkt_len, void *user);
|
||||
|
||||
typedef struct {
|
||||
bool active;
|
||||
uint32_t op_id;
|
||||
size_t len;
|
||||
uint8_t buf[GEMINUX_CCCI_FS_MAX_STREAM];
|
||||
} geminux_ccci_fs_slot_t;
|
||||
|
||||
typedef struct {
|
||||
int fd;
|
||||
} geminux_ccci_fs_fd_t;
|
||||
|
||||
typedef struct {
|
||||
void *dirp;
|
||||
char dir_path[1024];
|
||||
char pattern[256];
|
||||
} geminux_ccci_fs_find_t;
|
||||
|
||||
typedef struct {
|
||||
char nvdata_md_dir[256];
|
||||
char nvdata_root_dir[256];
|
||||
char protect_f_dir[256];
|
||||
char protect_s_dir[256];
|
||||
char nvcfg_dir[256];
|
||||
char log_path[256];
|
||||
bool override_mode;
|
||||
geminux_ccci_fs_slot_t slots[GEMINUX_CCCI_FS_MAX_SLOTS];
|
||||
geminux_ccci_fs_fd_t fds[GEMINUX_CCCI_FS_MAX_FDS];
|
||||
geminux_ccci_fs_find_t finds[GEMINUX_CCCI_FS_MAX_FINDS];
|
||||
} geminux_ccci_fs_t;
|
||||
|
||||
void geminux_ccci_fs_init(geminux_ccci_fs_t *fs, const char *override_root,
|
||||
const char *log_path);
|
||||
void geminux_ccci_fs_reset(geminux_ccci_fs_t *fs);
|
||||
int geminux_ccci_fs_process_packet(geminux_ccci_fs_t *fs,
|
||||
const uint8_t *pkt, size_t pkt_len,
|
||||
geminux_ccci_fs_send_fn send_fn, void *user);
|
||||
|
||||
#endif /* GEMINUX_CCCI_FS_H */
|
||||
@@ -0,0 +1,598 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "geminux-cell-proto.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static int ci_starts_with(const char *s, const char *prefix) {
|
||||
while (*prefix) {
|
||||
if (toupper((unsigned char)*s) != toupper((unsigned char)*prefix)) return 0;
|
||||
s++;
|
||||
prefix++;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool is_digits_only(const char *s) {
|
||||
if (!s || !s[0]) return false;
|
||||
for (size_t i = 0; s[i]; i++) {
|
||||
if (!isdigit((unsigned char)s[i])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int geminux_esim_normalize_at_line(const char *line, bool in_ccho_open,
|
||||
char *out, size_t out_sz) {
|
||||
if (!line || !out || out_sz < 8) return -1;
|
||||
|
||||
while (*line == '\r' || *line == '\n' || *line == ' ' || *line == '\t') line++;
|
||||
|
||||
char trim[1024];
|
||||
snprintf(trim, sizeof(trim), "%s", line);
|
||||
size_t n = strlen(trim);
|
||||
while (n > 0 && (trim[n - 1] == '\r' || trim[n - 1] == '\n' ||
|
||||
trim[n - 1] == ' ' || trim[n - 1] == '\t')) {
|
||||
trim[--n] = '\0';
|
||||
}
|
||||
if (n == 0) {
|
||||
out[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* lpac v2.3.0 driver/apdu/at.c hangs on +CME/+CMS ERROR because it only matches "ERROR" */
|
||||
if (ci_starts_with(trim, "+CME ERROR") || ci_starts_with(trim, "+CMS ERROR")) {
|
||||
snprintf(out, out_sz, "ERROR\r\n");
|
||||
return (int)strlen(out);
|
||||
}
|
||||
|
||||
/* Drop unsolicited URCs that can race ahead of AT+CGLA / AT+CCHO responses */
|
||||
static const char *const urc_prefixes[] = {
|
||||
"+EIND:", "+ESIMS:", "+CREG:", "+CGREG:", "+CEREG:", "+CIREG:",
|
||||
"+ECSQ:", "+CSQ:", "+EUSIM:", "+STKPCI:", "+CGEV:", "+CPIN:", "^SYSINFO"
|
||||
};
|
||||
for (size_t i = 0; i < sizeof(urc_prefixes) / sizeof(urc_prefixes[0]); i++) {
|
||||
if (ci_starts_with(trim, urc_prefixes[i])) {
|
||||
out[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (strcmp(trim, "RING") == 0 || strcmp(trim, "NO CARRIER") == 0) {
|
||||
out[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* MT6797 MOLY.LR11 returns bare "<channel>" to AT+CCHO="<AID>" instead of "+CCHO: <channel>" */
|
||||
if (in_ccho_open && n <= 3 && is_digits_only(trim) && atoi(trim) > 0) {
|
||||
int written = snprintf(out, out_sz, "+CCHO: %s\r\n", trim);
|
||||
if (written < 0 || (size_t)written >= out_sz) return -1;
|
||||
return written;
|
||||
}
|
||||
|
||||
int written = snprintf(out, out_sz, "%s\r\n", trim);
|
||||
if (written < 0 || (size_t)written >= out_sz) return -1;
|
||||
return written;
|
||||
}
|
||||
|
||||
void geminux_esim_track_tx_cmd(const char *cmd_line, bool *in_ccho_open) {
|
||||
if (!cmd_line || !in_ccho_open) return;
|
||||
while (*cmd_line == ' ' || *cmd_line == '\t' || *cmd_line == '\r' || *cmd_line == '\n') {
|
||||
cmd_line++;
|
||||
}
|
||||
if (!cmd_line[0]) return;
|
||||
if (ci_starts_with(cmd_line, "AT+CCHO=")) {
|
||||
const char *rhs = cmd_line + 8;
|
||||
*in_ccho_open = (*rhs != '?');
|
||||
} else if (ci_starts_with(cmd_line, "AT+CCHC") ||
|
||||
ci_starts_with(cmd_line, "AT+CGLA")) {
|
||||
*in_ccho_open = false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool valid_smdp_host(const char *s) {
|
||||
if (!s || !s[0]) return false;
|
||||
size_t n = strlen(s);
|
||||
if (n < 3 || n > 127) return false;
|
||||
if (s[0] == '.' || s[0] == '-' || s[n - 1] == '.' || s[n - 1] == '-') return false;
|
||||
bool has_dot = false;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
unsigned char c = (unsigned char)s[i];
|
||||
if (c == '.') has_dot = true;
|
||||
if (!(isalnum(c) || c == '.' || c == '-')) return false;
|
||||
}
|
||||
return has_dot;
|
||||
}
|
||||
|
||||
static bool valid_matching_id(const char *s) {
|
||||
if (!s) return false;
|
||||
for (size_t i = 0; s[i]; i++) {
|
||||
unsigned char c = (unsigned char)s[i];
|
||||
if (!(isalnum(c) || c == '.' || c == '_' || c == '-')) return false;
|
||||
}
|
||||
return strlen(s) <= 127;
|
||||
}
|
||||
|
||||
int geminux_parse_lpa_code(const char *input,
|
||||
char *smdp, size_t smdp_sz,
|
||||
char *matching_id, size_t mid_sz,
|
||||
char *conf_code, size_t conf_sz,
|
||||
bool *req_conf) {
|
||||
if (!input || !smdp || smdp_sz == 0 || !matching_id || mid_sz == 0) return -1;
|
||||
smdp[0] = '\0';
|
||||
matching_id[0] = '\0';
|
||||
if (conf_code && conf_sz > 0) conf_code[0] = '\0';
|
||||
if (req_conf) *req_conf = false;
|
||||
|
||||
while (*input == ' ' || *input == '\t' || *input == '\r' || *input == '\n') input++;
|
||||
char buf[512];
|
||||
snprintf(buf, sizeof(buf), "%s", input);
|
||||
size_t n = strlen(buf);
|
||||
while (n > 0 && (buf[n - 1] == ' ' || buf[n - 1] == '\t' ||
|
||||
buf[n - 1] == '\r' || buf[n - 1] == '\n')) {
|
||||
buf[--n] = '\0';
|
||||
}
|
||||
if (n == 0) return -1;
|
||||
|
||||
const char *body = NULL;
|
||||
if (ci_starts_with(buf, "LPA:1$")) {
|
||||
body = buf + 6;
|
||||
} else if (strncmp(buf, "1$", 2) == 0) {
|
||||
body = buf + 2;
|
||||
} else {
|
||||
if (strchr(buf, '$') != NULL) return -1;
|
||||
if (!valid_smdp_host(buf)) return -1;
|
||||
snprintf(smdp, smdp_sz, "%s", buf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Parse $ delimited segments after "1$" */
|
||||
char work[512];
|
||||
snprintf(work, sizeof(work), "%s", body);
|
||||
char *fields[5] = {NULL, NULL, NULL, NULL, NULL};
|
||||
int nf = 0;
|
||||
char *p = work;
|
||||
fields[nf++] = p;
|
||||
while (*p && nf < 5) {
|
||||
if (*p == '$') {
|
||||
*p = '\0';
|
||||
fields[nf++] = p + 1;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
if (strchr(p, '$') != NULL) return -1;
|
||||
|
||||
if (!valid_smdp_host(fields[0])) return -1;
|
||||
if (nf >= 2 && !valid_matching_id(fields[1])) return -1;
|
||||
if (nf >= 3 && fields[2][0] && !valid_matching_id(fields[2])) return -1;
|
||||
if (nf >= 4 && fields[3][0]) {
|
||||
if (strcmp(fields[3], "1") == 0) {
|
||||
if (req_conf) *req_conf = true;
|
||||
} else if (strcmp(fields[3], "0") != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(smdp, smdp_sz, "%s", fields[0]);
|
||||
if (nf >= 2 && fields[1][0]) {
|
||||
snprintf(matching_id, mid_sz, "%s", fields[1]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool geminux_validate_apn(const char *apn) {
|
||||
if (!apn || !apn[0]) return false;
|
||||
size_t n = strlen(apn);
|
||||
if (n > 63) return false;
|
||||
if (apn[0] == '.' || apn[0] == '-' || apn[n - 1] == '.' || apn[n - 1] == '-') {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
unsigned char c = (unsigned char)apn[i];
|
||||
if (!(isalnum(c) || c == '.' || c == '_' || c == '-')) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void geminux_mask_iccid(const char *iccid, char *out, size_t out_sz) {
|
||||
if (!out || out_sz == 0) return;
|
||||
if (!iccid) {
|
||||
snprintf(out, out_sz, "<REDACTED>");
|
||||
return;
|
||||
}
|
||||
size_t n = strlen(iccid);
|
||||
if (n < 8) {
|
||||
snprintf(out, out_sz, "<REDACTED>");
|
||||
return;
|
||||
}
|
||||
if (out_sz >= 21) {
|
||||
snprintf(out, out_sz, "%.4s\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2\xe2\x80\xa2%s",
|
||||
iccid, iccid + (n - 4));
|
||||
} else {
|
||||
snprintf(out, out_sz, "%.4s****%s", iccid, iccid + (n - 4));
|
||||
}
|
||||
}
|
||||
|
||||
static const char *skip_ws(const char *p) {
|
||||
while (p && (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')) p++;
|
||||
return p;
|
||||
}
|
||||
|
||||
static const char *skip_json_string(const char *p) {
|
||||
if (!p || *p != '"') return p;
|
||||
p++;
|
||||
while (*p && *p != '"') {
|
||||
if (*p == '\\' && p[1]) p += 2;
|
||||
else p++;
|
||||
}
|
||||
return (*p == '"') ? p + 1 : p;
|
||||
}
|
||||
|
||||
static const char *skip_json_value(const char *p) {
|
||||
p = skip_ws(p);
|
||||
if (!p || !*p) return p;
|
||||
if (*p == '"') return skip_json_string(p);
|
||||
if (*p == '{' || *p == '[') {
|
||||
char open = *p, close = (*p == '{') ? '}' : ']';
|
||||
int depth = 0;
|
||||
while (*p) {
|
||||
if (*p == '"') {
|
||||
p = skip_json_string(p);
|
||||
continue;
|
||||
}
|
||||
if (*p == open) depth++;
|
||||
else if (*p == close) {
|
||||
depth--;
|
||||
if (depth == 0) return p + 1;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
while (*p && *p != ',' && *p != '}' && *p != ']' &&
|
||||
*p != ' ' && *p != '\t' && *p != '\r' && *p != '\n') {
|
||||
p++;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static bool extract_json_string(const char *p, char *out, size_t out_sz) {
|
||||
if (!p || *p != '"') return false;
|
||||
p++;
|
||||
size_t w = 0;
|
||||
while (*p && *p != '"') {
|
||||
unsigned char ch;
|
||||
if (*p == '\\' && p[1]) {
|
||||
p++;
|
||||
if (*p == 'n') ch = '\n';
|
||||
else if (*p == 'r') ch = '\r';
|
||||
else if (*p == 't') ch = '\t';
|
||||
else ch = (unsigned char)*p;
|
||||
p++;
|
||||
} else {
|
||||
ch = (unsigned char)*p++;
|
||||
}
|
||||
if (w + 1 < out_sz) out[w++] = (char)ch;
|
||||
}
|
||||
out[w] = '\0';
|
||||
return *p == '"';
|
||||
}
|
||||
|
||||
/* Locate a top-level key within the JSON object starting at obj_start ('{') */
|
||||
static const char *find_obj_key(const char *obj_start, const char *key,
|
||||
const char **obj_end_out) {
|
||||
const char *p = skip_ws(obj_start);
|
||||
if (!p || *p != '{') return NULL;
|
||||
p++;
|
||||
size_t klen = strlen(key);
|
||||
while (*p) {
|
||||
p = skip_ws(p);
|
||||
if (*p == '}') {
|
||||
if (obj_end_out) *obj_end_out = p + 1;
|
||||
return NULL;
|
||||
}
|
||||
if (*p == ',') {
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
if (*p != '"') return NULL;
|
||||
char kbuf[64];
|
||||
extract_json_string(p, kbuf, sizeof(kbuf));
|
||||
p = skip_ws(skip_json_string(p));
|
||||
if (*p != ':') return NULL;
|
||||
p = skip_ws(p + 1);
|
||||
if (strlen(kbuf) == klen && strcmp(kbuf, key) == 0) {
|
||||
return p;
|
||||
}
|
||||
p = skip_json_value(p);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *find_lpa_payload(const char *json) {
|
||||
if (!json) return NULL;
|
||||
const char *p = json;
|
||||
while (*p) {
|
||||
const char *line_start = p;
|
||||
while (*p && *p != '\n') p++;
|
||||
const char *obj = skip_ws(line_start);
|
||||
if (*obj == '{') {
|
||||
const char *tval = find_obj_key(obj, "type", NULL);
|
||||
if (tval && *tval == '"') {
|
||||
char tbuf[32];
|
||||
if (extract_json_string(tval, tbuf, sizeof(tbuf)) && strcmp(tbuf, "lpa") == 0) {
|
||||
const char *pay = find_obj_key(obj, "payload", NULL);
|
||||
if (pay && *pay == '{') return pay;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (*p == '\n') p++;
|
||||
}
|
||||
const char *pay = strstr(json, "\"payload\"");
|
||||
if (pay) {
|
||||
const char *colon = strchr(pay + 9, ':');
|
||||
if (colon) {
|
||||
const char *v = skip_ws(colon + 1);
|
||||
if (*v == '{') return v;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void build_profile_label(geminux_esim_profile_t *prof) {
|
||||
if (prof->nickname[0]) {
|
||||
if (prof->provider[0] && strcmp(prof->nickname, prof->provider) != 0) {
|
||||
snprintf(prof->label, sizeof(prof->label), "%.58s \xe2\x80\x94 %.58s",
|
||||
prof->nickname, prof->provider);
|
||||
} else {
|
||||
snprintf(prof->label, sizeof(prof->label), "%s", prof->nickname);
|
||||
}
|
||||
} else if (prof->provider[0] && prof->name[0] &&
|
||||
strcmp(prof->provider, prof->name) != 0) {
|
||||
snprintf(prof->label, sizeof(prof->label), "%.58s (%.58s)",
|
||||
prof->provider, prof->name);
|
||||
} else if (prof->provider[0]) {
|
||||
snprintf(prof->label, sizeof(prof->label), "%s", prof->provider);
|
||||
} else if (prof->name[0]) {
|
||||
snprintf(prof->label, sizeof(prof->label), "%s", prof->name);
|
||||
} else {
|
||||
snprintf(prof->label, sizeof(prof->label), "eSIM Profile");
|
||||
}
|
||||
}
|
||||
|
||||
int geminux_parse_lpac_profiles_json(const char *json,
|
||||
geminux_esim_profile_t *out, int max_profiles,
|
||||
int *err_code, char *err_msg, size_t err_msg_sz) {
|
||||
if (err_code) *err_code = -1;
|
||||
if (err_msg && err_msg_sz > 0) err_msg[0] = '\0';
|
||||
if (!json) return -1;
|
||||
|
||||
const char *payload = find_lpa_payload(json);
|
||||
if (!payload) {
|
||||
if (err_msg && err_msg_sz > 0) snprintf(err_msg, err_msg_sz, "Missing lpa JSON payload");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int code = -1;
|
||||
const char *cval = find_obj_key(payload, "code", NULL);
|
||||
if (cval) code = atoi(cval);
|
||||
if (err_code) *err_code = code;
|
||||
|
||||
const char *mval = find_obj_key(payload, "message", NULL);
|
||||
char msg[96] = {0};
|
||||
if (mval && *mval == '"') extract_json_string(mval, msg, sizeof(msg));
|
||||
|
||||
const char *dval = find_obj_key(payload, "data", NULL);
|
||||
if (code != 0) {
|
||||
char dstr[96] = {0};
|
||||
if (dval && *dval == '"') extract_json_string(dval, dstr, sizeof(dstr));
|
||||
if (err_msg && err_msg_sz > 0) {
|
||||
if (dstr[0]) snprintf(err_msg, err_msg_sz, "%.40s: %.48s", msg[0] ? msg : "error", dstr);
|
||||
else snprintf(err_msg, err_msg_sz, "%s", msg[0] ? msg : "lpac error");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
if (err_msg && err_msg_sz > 0 && msg[0]) snprintf(err_msg, err_msg_sz, "%s", msg);
|
||||
|
||||
if (!dval || *dval != '[') {
|
||||
return (dval && strncmp(dval, "null", 4) == 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
const char *p = skip_ws(dval + 1);
|
||||
int count = 0;
|
||||
while (*p && *p != ']') {
|
||||
p = skip_ws(p);
|
||||
if (*p == ',') {
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
if (*p != '{') break;
|
||||
|
||||
geminux_esim_profile_t item;
|
||||
memset(&item, 0, sizeof(item));
|
||||
|
||||
const char *v;
|
||||
char state[32] = {0};
|
||||
if ((v = find_obj_key(p, "iccid", NULL)) && *v == '"') {
|
||||
extract_json_string(v, item.iccid, sizeof(item.iccid));
|
||||
}
|
||||
if ((v = find_obj_key(p, "profileState", NULL)) && *v == '"') {
|
||||
extract_json_string(v, state, sizeof(state));
|
||||
item.enabled = (strcmp(state, "enabled") == 0);
|
||||
}
|
||||
if ((v = find_obj_key(p, "profileNickname", NULL)) && *v == '"') {
|
||||
extract_json_string(v, item.nickname, sizeof(item.nickname));
|
||||
}
|
||||
if ((v = find_obj_key(p, "serviceProviderName", NULL)) && *v == '"') {
|
||||
extract_json_string(v, item.provider, sizeof(item.provider));
|
||||
}
|
||||
if ((v = find_obj_key(p, "profileName", NULL)) && *v == '"') {
|
||||
extract_json_string(v, item.name, sizeof(item.name));
|
||||
}
|
||||
geminux_mask_iccid(item.iccid, item.masked_iccid, sizeof(item.masked_iccid));
|
||||
build_profile_label(&item);
|
||||
|
||||
if (out && count < max_profiles && item.iccid[0]) {
|
||||
out[count] = item;
|
||||
}
|
||||
if (item.iccid[0]) count++;
|
||||
p = skip_json_value(p);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int geminux_parse_lpac_chip_json(const char *json, geminux_esim_chip_t *out) {
|
||||
if (!json || !out) return -1;
|
||||
memset(out, 0, sizeof(*out));
|
||||
out->code = -1;
|
||||
|
||||
const char *payload = find_lpa_payload(json);
|
||||
if (!payload) {
|
||||
snprintf(out->message, sizeof(out->message), "Missing lpa JSON payload");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *cval = find_obj_key(payload, "code", NULL);
|
||||
if (cval) out->code = atoi(cval);
|
||||
const char *mval = find_obj_key(payload, "message", NULL);
|
||||
if (mval && *mval == '"') extract_json_string(mval, out->message, sizeof(out->message));
|
||||
|
||||
const char *data = find_obj_key(payload, "data", NULL);
|
||||
if (out->code != 0 || !data || *data != '{') {
|
||||
if (data && *data == '"') extract_json_string(data, out->message, sizeof(out->message));
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *v;
|
||||
if ((v = find_obj_key(data, "eidValue", NULL)) && *v == '"') {
|
||||
extract_json_string(v, out->eid, sizeof(out->eid));
|
||||
geminux_mask_iccid(out->eid, out->masked_eid, sizeof(out->masked_eid));
|
||||
}
|
||||
const char *info2 = find_obj_key(data, "EUICCInfo2", NULL);
|
||||
if (info2 && *info2 == '{') {
|
||||
if ((v = find_obj_key(info2, "svn", NULL)) && *v == '"') {
|
||||
extract_json_string(v, out->svn, sizeof(out->svn));
|
||||
}
|
||||
const char *res = find_obj_key(info2, "extCardResource", NULL);
|
||||
if (res && *res == '{') {
|
||||
if ((v = find_obj_key(res, "freeNonVolatileMemory", NULL))) {
|
||||
out->free_nvram = strtoul(v, NULL, 10);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool extract_ipv4_tok(const char *tok, char *dst, size_t dst_sz) {
|
||||
if (!tok || !tok[0] || strchr(tok, ':') != NULL) return false;
|
||||
char tmp[64];
|
||||
snprintf(tmp, sizeof(tmp), "%s", tok);
|
||||
char *slash = strchr(tmp, '/');
|
||||
if (slash) *slash = '\0';
|
||||
|
||||
unsigned int oct[8];
|
||||
int cnt = 0;
|
||||
char *save = NULL;
|
||||
for (char *p = strtok_r(tmp, ".", &save); p; p = strtok_r(NULL, ".", &save)) {
|
||||
if (!is_digits_only(p) || cnt >= 8) return false;
|
||||
unsigned long v = strtoul(p, NULL, 10);
|
||||
if (v > 255UL) return false;
|
||||
oct[cnt++] = (unsigned int)v;
|
||||
}
|
||||
if (cnt != 4 && cnt != 8) return false;
|
||||
if (oct[0] == 0 && oct[1] == 0 && oct[2] == 0 && oct[3] == 0) return false;
|
||||
snprintf(dst, dst_sz, "%u.%u.%u.%u", oct[0], oct[1], oct[2], oct[3]);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void unquote_csv_field(char *s) {
|
||||
size_t n = strlen(s);
|
||||
if (n >= 2 && s[0] == '"' && s[n - 1] == '"') {
|
||||
memmove(s, s + 1, n - 2);
|
||||
s[n - 2] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
int geminux_parse_cgpaddr_and_rdp(const char *at_resp,
|
||||
char *ipv4, size_t ipv4_sz,
|
||||
char *gw4, size_t gw4_sz,
|
||||
char *dns1, size_t dns1_sz,
|
||||
char *dns2, size_t dns2_sz) {
|
||||
if (!at_resp) return -1;
|
||||
if (ipv4 && ipv4_sz) ipv4[0] = '\0';
|
||||
if (gw4 && gw4_sz) gw4[0] = '\0';
|
||||
if (dns1 && dns1_sz) dns1[0] = '\0';
|
||||
if (dns2 && dns2_sz) dns2[0] = '\0';
|
||||
|
||||
char copy[4096];
|
||||
snprintf(copy, sizeof(copy), "%s", at_resp);
|
||||
|
||||
char *save_ln = NULL;
|
||||
for (char *ln = strtok_r(copy, "\r\n", &save_ln); ln; ln = strtok_r(NULL, "\r\n", &save_ln)) {
|
||||
while (*ln == ' ' || *ln == '\t') ln++;
|
||||
if (ci_starts_with(ln, "+CGPADDR:")) {
|
||||
const char *p = strchr(ln, ',');
|
||||
while (p) {
|
||||
p++;
|
||||
while (*p == ' ' || *p == '"') p++;
|
||||
char tok[64];
|
||||
size_t w = 0;
|
||||
while (*p && *p != '"' && *p != ',' && w + 1 < sizeof(tok)) tok[w++] = *p++;
|
||||
tok[w] = '\0';
|
||||
char parsed[32];
|
||||
if (extract_ipv4_tok(tok, parsed, sizeof(parsed)) && ipv4 && !ipv4[0]) {
|
||||
snprintf(ipv4, ipv4_sz, "%s", parsed);
|
||||
}
|
||||
p = strchr(p, ',');
|
||||
}
|
||||
} else if (ci_starts_with(ln, "+CGCONTRDP:")) {
|
||||
/* Split CSV fields manually so empty consecutive commas (e.g. ,,) are preserved */
|
||||
char *fields[12];
|
||||
int nf = 0;
|
||||
char *c = strchr(ln, ':');
|
||||
if (!c) continue;
|
||||
c++;
|
||||
while (*c == ' ') c++;
|
||||
fields[nf++] = c;
|
||||
while (*c && nf < 12) {
|
||||
if (*c == ',') {
|
||||
*c = '\0';
|
||||
char *nxt = c + 1;
|
||||
while (*nxt == ' ') nxt++;
|
||||
fields[nf++] = nxt;
|
||||
c = nxt;
|
||||
continue;
|
||||
}
|
||||
c++;
|
||||
}
|
||||
/* fields: [0]=cid, [1]=bearer, [2]=apn, [3]=local_addr, [4]=gw, [5]=dns1, [6]=dns2 */
|
||||
char parsed[32];
|
||||
if (nf > 3) {
|
||||
unquote_csv_field(fields[3]);
|
||||
if (strchr(fields[3], ':') != NULL) continue; /* IPv6 PDP line; skip for IPv4 parser */
|
||||
if (extract_ipv4_tok(fields[3], parsed, sizeof(parsed)) && ipv4 && !ipv4[0]) {
|
||||
snprintf(ipv4, ipv4_sz, "%s", parsed);
|
||||
}
|
||||
}
|
||||
if (nf > 4) {
|
||||
unquote_csv_field(fields[4]);
|
||||
if (extract_ipv4_tok(fields[4], parsed, sizeof(parsed)) && gw4 && !gw4[0]) {
|
||||
snprintf(gw4, gw4_sz, "%s", parsed);
|
||||
}
|
||||
}
|
||||
if (nf > 5) {
|
||||
unquote_csv_field(fields[5]);
|
||||
if (extract_ipv4_tok(fields[5], parsed, sizeof(parsed)) && dns1 && !dns1[0]) {
|
||||
snprintf(dns1, dns1_sz, "%s", parsed);
|
||||
}
|
||||
}
|
||||
if (nf > 6) {
|
||||
unquote_csv_field(fields[6]);
|
||||
if (extract_ipv4_tok(fields[6], parsed, sizeof(parsed)) && dns2 && !dns2[0]) {
|
||||
snprintf(dns2, dns2_sz, "%s", parsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (ipv4 && ipv4[0]) ? 0 : -1;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Geminux MT6797 Cellular, SGP.22 eSIM (lpac AT bridge), and PDN parsing helpers.
|
||||
* Pure C11 with zero external library dependencies for unit testing & reuse.
|
||||
*/
|
||||
#ifndef GEMINUX_CELL_PROTO_H
|
||||
#define GEMINUX_CELL_PROTO_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define GEMINUX_MAX_ESIM_PROFILES 16
|
||||
|
||||
typedef struct {
|
||||
char iccid[32];
|
||||
char masked_iccid[32];
|
||||
bool enabled;
|
||||
char nickname[64];
|
||||
char provider[64];
|
||||
char name[64];
|
||||
char label[128];
|
||||
} geminux_esim_profile_t;
|
||||
|
||||
typedef struct {
|
||||
int code;
|
||||
char message[96];
|
||||
char eid[48];
|
||||
char masked_eid[48];
|
||||
char svn[24];
|
||||
unsigned long free_nvram;
|
||||
} geminux_esim_chip_t;
|
||||
|
||||
int geminux_esim_normalize_at_line(const char *line, bool in_ccho_open,
|
||||
char *out, size_t out_sz);
|
||||
void geminux_esim_track_tx_cmd(const char *cmd_line, bool *in_ccho_open);
|
||||
|
||||
int geminux_parse_lpa_code(const char *input,
|
||||
char *smdp, size_t smdp_sz,
|
||||
char *matching_id, size_t mid_sz,
|
||||
char *conf_code, size_t conf_sz,
|
||||
bool *req_conf);
|
||||
bool geminux_validate_apn(const char *apn);
|
||||
void geminux_mask_iccid(const char *iccid, char *out, size_t out_sz);
|
||||
|
||||
int geminux_parse_lpac_profiles_json(const char *json,
|
||||
geminux_esim_profile_t *out, int max_profiles,
|
||||
int *err_code, char *err_msg, size_t err_msg_sz);
|
||||
int geminux_parse_lpac_chip_json(const char *json, geminux_esim_chip_t *out);
|
||||
|
||||
int geminux_parse_cgpaddr_and_rdp(const char *at_resp,
|
||||
char *ipv4, size_t ipv4_sz,
|
||||
char *gw4, size_t gw4_sz,
|
||||
char *dns1, size_t dns1_sz,
|
||||
char *dns2, size_t dns2_sz);
|
||||
|
||||
#endif /* GEMINUX_CELL_PROTO_H */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -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;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
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",
|
||||
fprintf(stderr, "Usage: %s <status|suspend|wifi-restore|profile eco|balanced|performance|inhibit add|del TAG|lid-suspend on|off>\n",
|
||||
argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -57,6 +57,7 @@ typedef struct {
|
||||
bool applied_interactive_boost;
|
||||
pid_t consys_restore_pid;
|
||||
bool consys_suspended;
|
||||
bool suspend_ebusy_on_ac;
|
||||
bool bt_was_loaded;
|
||||
bool bt_was_blocked;
|
||||
bool gnss_was_loaded;
|
||||
@@ -584,6 +585,9 @@ static void set_bt_rfkill_soft_blocked(bool blocked) {
|
||||
* unloading reverse dependents lets mt6797_consys_remove() power off CONN/VCN rails. */
|
||||
static void prepare_consys_for_suspend(powerd_ctx_t *ctx) {
|
||||
reap_consys_restore(ctx, true);
|
||||
if (ctx->consys_suspended) {
|
||||
return;
|
||||
}
|
||||
if (access("/sys/module/mt6797_consys", F_OK) != 0) {
|
||||
ctx->consys_suspended = false;
|
||||
return;
|
||||
@@ -772,10 +776,13 @@ static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
restore_cpus_after_resume(ctx);
|
||||
bool saw_wake_input = wait_post_resume_lid_events(ctx, 90);
|
||||
refresh_ac_and_hdmi(ctx);
|
||||
if (!ctx->policy.lid_closed || !geminux_should_suspend_on_lid(&ctx->policy)) {
|
||||
ctx->suspend_ebusy_on_ac = (!suspended && ctx->policy.on_ac_power);
|
||||
if (!ctx->policy.lid_closed || !geminux_should_suspend_on_lid(&ctx->policy) ||
|
||||
ctx->suspend_ebusy_on_ac) {
|
||||
restore_consys_after_resume(ctx);
|
||||
}
|
||||
if ((suspended || saw_wake_input) && !ctx->policy.lid_closed) {
|
||||
ctx->suspend_ebusy_on_ac = false;
|
||||
ctx->idle_stage = IDLE_STAGE_ACTIVE;
|
||||
ctx->last_input_ms = monotonic_ms();
|
||||
toggle_backlight(ctx, true);
|
||||
@@ -784,8 +791,9 @@ static void trigger_suspend(powerd_ctx_t *ctx) {
|
||||
} else {
|
||||
toggle_backlight(ctx, false);
|
||||
ctx->idle_stage = IDLE_STAGE_LOCKED_BLANK;
|
||||
if (ctx->policy.lid_closed ? geminux_should_suspend_on_lid(&ctx->policy)
|
||||
: (!ctx->policy.on_ac_power && !saw_wake_input)) {
|
||||
if (!ctx->suspend_ebusy_on_ac &&
|
||||
(ctx->policy.lid_closed ? geminux_should_suspend_on_lid(&ctx->policy)
|
||||
: (!ctx->policy.on_ac_power && !saw_wake_input))) {
|
||||
arm_timer_ms(ctx->tmr_fd, 2000);
|
||||
} else {
|
||||
arm_timer_ms(ctx->tmr_fd, 0);
|
||||
@@ -862,6 +870,15 @@ static void handle_client_cmd(powerd_ctx_t *ctx, int cfd) {
|
||||
ctx->policy.lid_suspend_enabled = false;
|
||||
restore_consys_after_resume(ctx);
|
||||
dprintf(cfd, "OK lid_suspend=off\n");
|
||||
} else if (strcmp(buf, "wifi-restore") == 0) {
|
||||
if (access("/sys/class/net/wlan0", F_OK) != 0) {
|
||||
ctx->consys_suspended = true;
|
||||
restore_consys_after_resume(ctx);
|
||||
reap_consys_restore(ctx, true);
|
||||
dprintf(cfd, "OK wifi-restore\n");
|
||||
} else {
|
||||
dprintf(cfd, "OK wlan0-present\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),
|
||||
@@ -1015,10 +1032,18 @@ int main(void) {
|
||||
restore_consys_after_resume(&ctx);
|
||||
}
|
||||
if (prev_ac != ctx.policy.on_ac_power || prev_hdmi != ctx.policy.hdmi_connected) {
|
||||
if (!ctx.policy.lid_closed) {
|
||||
note_user_activity(&ctx);
|
||||
if (!ctx.policy.on_ac_power) {
|
||||
ctx.suspend_ebusy_on_ac = false;
|
||||
}
|
||||
if (prev_ac && !ctx.policy.on_ac_power && ctx.policy.lid_closed &&
|
||||
geminux_should_suspend_on_lid(&ctx.policy)) {
|
||||
trigger_suspend(&ctx);
|
||||
} else {
|
||||
if (!ctx.policy.lid_closed) {
|
||||
note_user_activity(&ctx);
|
||||
}
|
||||
apply_cpu_policy(&ctx, false);
|
||||
}
|
||||
apply_cpu_policy(&ctx, false);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -1038,6 +1063,7 @@ int main(void) {
|
||||
toggle_backlight(&ctx, false);
|
||||
}
|
||||
} else {
|
||||
ctx.suspend_ebusy_on_ac = false;
|
||||
restore_consys_after_resume(&ctx);
|
||||
ctx.idle_stage = IDLE_STAGE_ACTIVE;
|
||||
ctx.last_input_ms = monotonic_ms();
|
||||
|
||||
Reference in new issue
Block a user