Fix dual-SIM operator/MSISDN parsing, secondary SIM USSD, and callscreen auto-close

This commit is contained in:
Luxferre committed 2026-10-27 12:01:04 +02:00
1 parent 46f1255905
commit 9a079e1068
16 files changed
+503 -173

No files matched your search

+225 -72
View File
@@ -474,16 +474,6 @@ int callscreen_run_main(int argc, char** argv) {
return spk ? (68 + (c * 26) / 100) : (68 + (c * 24) / 100);
};
auto hwVolToPct = [](long hwVol, bool spk) -> int {
if (hwVol < 68) {
return 85;
}
int span = spk ? 26 : 24;
int pct = static_cast<int>(std::lround(((hwVol - 68) * 100.0) / span));
pct = std::clamp(pct, 5, 100);
return std::max(5, ((pct + 2) / 5) * 5);
};
auto refreshHardwareAudioState = [&]() {
unsigned int dec1 = alsa.getEnum("DEC1 MUX", 1);
if (dec1 > 0) {
@@ -1288,68 +1278,235 @@ int callscreen_run_main(int argc, char** argv) {
render();
};
auto queryQmiVoiceCalls = [&](std::string& outId, std::string& outDir,
std::string& outState, std::string& outNum,
bool& outSawTerminated) -> bool {
constexpr int kAfQipcrtr = 42;
constexpr uint32_t kQrtrPortCtrl = 0xfffffffeu;
constexpr uint32_t kQrtrTypeNewServer = 4;
constexpr uint32_t kQrtrTypeNewLookup = 10;
constexpr uint32_t kQmiServiceVoice = 9;
struct SockAddrQrtr {
unsigned short sq_family;
uint32_t sq_node;
uint32_t sq_port;
};
struct QrtrCtrlPkt {
uint32_t cmd;
uint32_t service;
uint32_t instance;
uint32_t node;
uint32_t port;
} __attribute__((packed));
int lfd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (lfd < 0) {
return false;
}
SockAddrQrtr localSq {};
socklen_t sl = sizeof(localSq);
if (::getsockname(lfd, reinterpret_cast<struct sockaddr*>(&localSq), &sl) < 0) {
::close(lfd);
return false;
}
SockAddrQrtr ctrlSq { kAfQipcrtr, localSq.sq_node, kQrtrPortCtrl };
QrtrCtrlPkt lookup { kQrtrTypeNewLookup, kQmiServiceVoice, 0, 0, 0 };
if (::sendto(lfd, &lookup, sizeof(lookup), 0,
reinterpret_cast<const struct sockaddr*>(&ctrlSq),
sizeof(ctrlSq)) < 0) {
::close(lfd);
return false;
}
uint32_t vNode = 0, vPort = 0;
bool foundSrv = false;
for (int i = 0; i < 8; ++i) {
struct pollfd pfd { lfd, POLLIN, 0 };
if (::poll(&pfd, 1, 40) <= 0) break;
QrtrCtrlPkt pkt {};
ssize_t n = ::recv(lfd, &pkt, sizeof(pkt), 0);
if (n >= static_cast<ssize_t>(sizeof(pkt)) && pkt.cmd == kQrtrTypeNewServer) {
if (pkt.service == kQmiServiceVoice && pkt.port != 0) {
vNode = pkt.node;
vPort = pkt.port;
foundSrv = true;
break;
}
if (pkt.service == 0 && pkt.node == 0 && pkt.port == 0) break;
}
}
::close(lfd);
if (!foundSrv) {
return false;
}
SockAddrQrtr vSq { kAfQipcrtr, vNode, vPort };
for (int sub : {1, 0}) {
int sfd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (sfd < 0) continue;
uint8_t bindReq[11] = {
0x00, 0x01, 0x00, 0x44, 0x00, 0x04, 0x00,
0x01, 0x01, 0x00, static_cast<uint8_t>(sub)
};
::sendto(sfd, bindReq, sizeof(bindReq), 0,
reinterpret_cast<const struct sockaddr*>(&vSq), sizeof(vSq));
struct pollfd pfd { sfd, POLLIN, 0 };
if (::poll(&pfd, 1, 40) > 0) {
uint8_t tmp[256];
::recv(sfd, tmp, sizeof(tmp), 0);
}
uint8_t infoReq[7] = { 0x00, 0x02, 0x00, 0x2F, 0x00, 0x00, 0x00 };
::sendto(sfd, infoReq, sizeof(infoReq), 0,
reinterpret_cast<const struct sockaddr*>(&vSq), sizeof(vSq));
if (::poll(&pfd, 1, 50) > 0) {
uint8_t buf[1024];
ssize_t rlen = ::recv(sfd, buf, sizeof(buf), 0);
if (rlen >= 7 && buf[0] == 0x02 && buf[3] == 0x2F) {
const uint8_t* rawCi = nullptr;
uint16_t lenCi = 0;
const uint8_t* rawNum = nullptr;
uint16_t lenNum = 0;
size_t pos = 7;
while (pos + 3 <= static_cast<size_t>(rlen)) {
uint8_t tt = buf[pos];
uint16_t tl = static_cast<uint16_t>(buf[pos + 1]) |
(static_cast<uint16_t>(buf[pos + 2]) << 8);
pos += 3;
if (pos + tl > static_cast<size_t>(rlen)) break;
if (tt == 0x10) {
rawCi = buf + pos;
lenCi = tl;
} else if (tt == 0x11) {
rawNum = buf + pos;
lenNum = tl;
}
pos += tl;
}
if (rawCi && lenCi >= 1) {
uint8_t cnt = rawCi[0];
size_t off = 1;
for (uint8_t i = 0; i < cnt && off + 7 <= lenCi; ++i, off += 7) {
uint8_t cid = rawCi[off];
uint8_t cstate = rawCi[off + 1];
uint8_t cdir = rawCi[off + 3];
if (cstate == 8 || cstate == 9) {
outSawTerminated = true;
continue;
}
std::string st;
if (cstate == 3) st = "active";
else if (cstate == 2 || cstate == 7) st = "ringing-in";
else if (cstate == 5) st = "ringing-out";
else if (cstate == 1 || cstate == 4 || cstate == 10) st = "dialing";
else if (cstate == 6) st = "held";
if (!st.empty() && outId.empty()) {
outId = (sub == 1) ? ("sub1_" + std::to_string(cid))
: std::to_string(cid);
outState = st;
outDir = (cdir == 2) ? "incoming" : "outgoing";
if (rawNum && lenNum >= 1) {
uint8_t ncnt = rawNum[0];
size_t noff = 1;
for (uint8_t ni = 0; ni < ncnt && noff + 3 <= lenNum; ++ni) {
uint8_t ncid = rawNum[noff];
uint8_t nlen = rawNum[noff + 2];
noff += 3;
if (noff + nlen > lenNum) break;
if (ncid == cid && nlen > 0) {
outNum.assign(reinterpret_cast<const char*>(rawNum + noff), nlen);
outNum = trimStr(outNum);
}
noff += nlen;
}
}
}
}
}
}
}
::close(sfd);
}
return true;
};
auto syncModemCalls = [&]() {
if (callId == "sim" || callId == "test") {
return;
}
FILE* fp = ::popen("mmcli -m any --voice-list-calls -a 2>/dev/null", "r");
if (!fp) {
return;
}
char line[512];
std::string foundId;
std::string foundDir;
std::string foundState;
std::string foundNum;
bool sawTerminated = false;
while (std::fgets(line, sizeof(line), fp)) {
std::string s(line);
if (s.find("terminated") != std::string::npos ||
s.find("No calls") != std::string::npos) {
continue;
FILE* fp = ::popen("mmcli -m any --voice-list-calls -a 2>/dev/null", "r");
if (fp) {
char line[512];
while (std::fgets(line, sizeof(line), fp)) {
std::string s(line);
if (s.find("No calls") != std::string::npos) {
continue;
}
if (s.find("terminated") != std::string::npos) {
sawTerminated = true;
continue;
}
size_t callPos = s.find("/Call/");
if (callPos == std::string::npos) {
continue;
}
size_t idStart = callPos + 6;
size_t idEnd = idStart;
while (idEnd < s.size() && std::isdigit(static_cast<unsigned char>(s[idEnd]))) {
++idEnd;
}
if (idEnd == idStart) {
continue;
}
foundId = s.substr(idStart, idEnd - idStart);
if (s.find("incoming") != std::string::npos) {
foundDir = "incoming";
} else if (s.find("outgoing") != std::string::npos) {
foundDir = "outgoing";
}
size_t lp = s.find('(');
size_t rp = s.find(')', lp == std::string::npos ? 0 : lp);
if (lp != std::string::npos && rp != std::string::npos && rp > lp + 1) {
foundState = s.substr(lp + 1, rp - lp - 1);
}
break;
}
size_t callPos = s.find("/Call/");
if (callPos == std::string::npos) {
continue;
}
size_t idStart = callPos + 6;
size_t idEnd = idStart;
while (idEnd < s.size() && std::isdigit(static_cast<unsigned char>(s[idEnd]))) {
++idEnd;
}
if (idEnd == idStart) {
continue;
}
foundId = s.substr(idStart, idEnd - idStart);
if (s.find("incoming") != std::string::npos) {
foundDir = "incoming";
} else if (s.find("outgoing") != std::string::npos) {
foundDir = "outgoing";
}
size_t lp = s.find('(');
size_t rp = s.find(')', lp == std::string::npos ? 0 : lp);
if (lp != std::string::npos && rp != std::string::npos && rp > lp + 1) {
foundState = s.substr(lp + 1, rp - lp - 1);
}
break;
::pclose(fp);
}
if (sawTerminated) {
runAsyncCmd("sxmo_modem.sh checkforfinishedcalls >/dev/null 2>&1");
}
if (foundId.empty()) {
queryQmiVoiceCalls(foundId, foundDir, foundState, foundNum, sawTerminated);
}
::pclose(fp);
if (!foundId.empty()) {
if (callId.empty() || callId == "-") {
callId = foundId;
}
if (callNumber.empty()) {
char numCmd[160];
std::snprintf(numCmd, sizeof(numCmd),
"sxmo_modemcall.sh vid_to_number '%s' 2>/dev/null",
foundId.c_str());
FILE* nfp = ::popen(numCmd, "r");
if (nfp) {
char nbuf[128];
if (std::fgets(nbuf, sizeof(nbuf), nfp)) {
updateCallerDetails(foundId, trimStr(nbuf));
if (!foundNum.empty()) {
updateCallerDetails(foundId, foundNum);
} else {
char numCmd[160];
std::snprintf(numCmd, sizeof(numCmd),
"sxmo_modemcall.sh vid_to_number '%s' 2>/dev/null",
foundId.c_str());
FILE* nfp = ::popen(numCmd, "r");
if (nfp) {
char nbuf[128];
if (std::fgets(nbuf, sizeof(nbuf), nfp)) {
updateCallerDetails(foundId, trimStr(nbuf));
}
::pclose(nfp);
}
::pclose(nfp);
}
}
if (foundState == "active" && callState != CallState::Active) {
@@ -1377,26 +1534,22 @@ int callscreen_run_main(int argc, char** argv) {
render();
}
} else if (callState == CallState::Active || callState == CallState::Incoming ||
(callState == CallState::Outgoing && nowMs() - callStartMs > 6000)) {
if (!isCallActiveOrRingingFast() || ::access("/tmp/sxmo_active_call", F_OK) == 0) {
// Verify sxmo_calls dir is empty before auto-ending
std::string callsDir = getRuntimeFile("sxmo_calls");
bool anyFile = false;
DIR* d = ::opendir(callsDir.c_str());
if (d) {
struct dirent* ent;
while ((ent = ::readdir(d)) != nullptr) {
if (ent->d_name[0] != '.' && ent->d_name[0] != '\0') {
anyFile = true;
break;
}
sawTerminated ||
(callState == CallState::Outgoing &&
((!callId.empty() && callId != "-") || nowMs() - callStartMs > 3500))) {
std::string callsDir = getRuntimeFile("sxmo_calls");
DIR* d = ::opendir(callsDir.c_str());
if (d) {
struct dirent* ent;
while ((ent = ::readdir(d)) != nullptr) {
if (ent->d_name[0] != '.' && ent->d_name[0] != '\0') {
std::string fpPath = callsDir + "/" + ent->d_name;
::unlink(fpPath.c_str());
}
::closedir(d);
}
if (!anyFile) {
transitionToEnded("CALL ENDED");
}
::closedir(d);
}
transitionToEnded("CALL ENDED");
}
};