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

+112 -62
View File
@@ -1579,6 +1579,37 @@ inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) {
bool hasNas = lookupService(kQmiServiceNas, nasNode, nasPort);
bool hasDms = lookupService(kQmiServiceDms, dmsNode, dmsPort);
auto recvQmiResp = [](int sfd, uint16_t expTxn, uint16_t expMsg,
uint8_t* outBuf, size_t maxLen, int timeoutMs) -> ssize_t {
int64_t deadline = nowMs() + timeoutMs;
while (nowMs() < deadline) {
int rem = static_cast<int>(std::max<int64_t>(5, deadline - nowMs()));
struct pollfd pfd { sfd, POLLIN, 0 };
if (::poll(&pfd, 1, rem) <= 0) break;
ssize_t n = ::recv(sfd, outBuf, maxLen, 0);
if (n >= 7 && outBuf[0] == 0x02) {
uint16_t rTxn = static_cast<uint16_t>(outBuf[1]) |
(static_cast<uint16_t>(outBuf[2]) << 8);
uint16_t rMsg = static_cast<uint16_t>(outBuf[3]) |
(static_cast<uint16_t>(outBuf[4]) << 8);
if (rTxn == expTxn && rMsg == expMsg) {
return n;
}
}
}
return -1;
};
auto cleanPrintable = [](const std::string& s) -> std::string {
std::string out;
for (unsigned char c : s) {
if (c >= 32 && c < 127) {
out.push_back(static_cast<char>(c));
}
}
return trimStr(out);
};
auto querySubMeta = [&](int slot, int sub, std::string& outOp,
std::string& outRat, std::string& outNum) {
if (sub < 0) return;
@@ -1586,45 +1617,59 @@ inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) {
int sfd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (sfd >= 0) {
SockAddrQrtr nasSq { kAfQipcrtr, nasNode, nasPort };
uint8_t buf[1024];
uint8_t bindReq[11] = {
0x00, 0x01, 0x00, 0x45, 0x00, 0x04, 0x00,
0x01, 0x01, 0x00, static_cast<uint8_t>(sub)
};
::sendto(sfd, bindReq, sizeof(bindReq), 0,
reinterpret_cast<const struct sockaddr*>(&nasSq), sizeof(nasSq));
struct pollfd pfd { sfd, POLLIN, 0 };
if (::poll(&pfd, 1, 40) > 0) {
uint8_t tmp[256];
::recv(sfd, tmp, sizeof(tmp), 0);
}
uint8_t ssReq[7] = { 0x00, 0x02, 0x00, 0x24, 0x00, 0x00, 0x00 };
::sendto(sfd, ssReq, sizeof(ssReq), 0,
recvQmiResp(sfd, 1, 0x0045, buf, sizeof(buf), 60);
uint8_t sysReq[7] = { 0x00, 0x02, 0x00, 0x4D, 0x00, 0x00, 0x00 };
::sendto(sfd, sysReq, sizeof(sysReq), 0,
reinterpret_cast<const struct sockaddr*>(&nasSq), sizeof(nasSq));
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] == 0x24) {
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 == 0x01 && tl >= 5 && buf[pos + 3] >= 1) {
uint8_t rb = buf[pos + 4];
if (rb == 8) outRat = "4G";
else if (rb == 5) outRat = "3G";
else if (rb == 4) outRat = "2G";
} else if (tt == 0x12 && tl >= 5) {
uint8_t dlen = buf[pos + 4];
if (5 + dlen <= tl && dlen > 0) {
outOp.assign(reinterpret_cast<const char*>(buf + pos + 5), dlen);
outOp = trimStr(outOp);
}
}
pos += tl;
ssize_t rlen = recvQmiResp(sfd, 2, 0x004D, buf, sizeof(buf), 60);
if (rlen >= 7) {
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 (tl >= 1 && buf[pos] == 2) {
if (tt == 0x14) outRat = "4G";
else if (tt == 0x13 && outRat.empty()) outRat = "3G";
else if (tt == 0x12 && outRat.empty()) outRat = "2G";
}
pos += tl;
}
}
uint8_t opReq[7] = { 0x00, 0x03, 0x00, 0x39, 0x00, 0x00, 0x00 };
::sendto(sfd, opReq, sizeof(opReq), 0,
reinterpret_cast<const struct sockaddr*>(&nasSq), sizeof(nasSq));
rlen = recvQmiResp(sfd, 3, 0x0039, buf, sizeof(buf), 60);
if (rlen >= 7) {
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 && tl >= 2) {
uint8_t slen = buf[pos + 1];
if (2 + slen <= tl && slen > 0) {
std::string raw(reinterpret_cast<const char*>(buf + pos + 2), slen);
outOp = cleanPrintable(raw);
}
} else if (tt == 0x13 && tl > 0 && outOp.empty()) {
std::string raw(reinterpret_cast<const char*>(buf + pos), tl);
outOp = cleanPrintable(raw);
}
pos += tl;
}
}
::close(sfd);
@@ -1634,40 +1679,40 @@ inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) {
int sfd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (sfd >= 0) {
SockAddrQrtr dmsSq { kAfQipcrtr, dmsNode, dmsPort };
uint8_t bindReq[11] = {
0x00, 0x01, 0x00, 0x54, 0x00, 0x04, 0x00,
0x01, 0x01, 0x00, static_cast<uint8_t>(sub)
uint8_t buf[512];
uint8_t bindReq[14] = {
0x00, 0x01, 0x00, 0x54, 0x00, 0x07, 0x00,
0x01, 0x04, 0x00, static_cast<uint8_t>(sub + 1), 0x00, 0x00, 0x00
};
::sendto(sfd, bindReq, sizeof(bindReq), 0,
reinterpret_cast<const struct sockaddr*>(&dmsSq), sizeof(dmsSq));
struct pollfd pfd { sfd, POLLIN, 0 };
if (::poll(&pfd, 1, 40) > 0) {
uint8_t tmp[256];
::recv(sfd, tmp, sizeof(tmp), 0);
}
recvQmiResp(sfd, 1, 0x0054, buf, sizeof(buf), 60);
uint8_t msReq[7] = { 0x00, 0x02, 0x00, 0x24, 0x00, 0x00, 0x00 };
::sendto(sfd, msReq, sizeof(msReq), 0,
reinterpret_cast<const struct sockaddr*>(&dmsSq), sizeof(dmsSq));
if (::poll(&pfd, 1, 50) > 0) {
uint8_t buf[512];
ssize_t rlen = ::recv(sfd, buf, sizeof(buf), 0);
if (rlen >= 7 && buf[0] == 0x02 && buf[3] == 0x24) {
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 == 0x01 && tl > 0) {
std::string raw(reinterpret_cast<const char*>(buf + pos), tl);
raw = trimStr(raw);
if (!raw.empty()) {
outNum = (raw.front() == '+') ? raw : ("+" + raw);
ssize_t rlen = recvQmiResp(sfd, 2, 0x0024, buf, sizeof(buf), 60);
if (rlen >= 7) {
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 == 0x01 && tl > 0) {
std::string raw(reinterpret_cast<const char*>(buf + pos), tl);
std::string digits;
for (char c : raw) {
if ((c >= '0' && c <= '9') || (c == '+' && digits.empty())) {
digits.push_back(c);
}
}
pos += tl;
if (!digits.empty()) {
outNum = (digits.front() == '+') ? digits : ("+" + digits);
}
}
pos += tl;
}
}
::close(sfd);
@@ -1684,6 +1729,7 @@ inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) {
auto formatSlotStrings = [](int s, const std::string& op, const std::string& rat,
const std::string& num, std::string& outLabel,
std::string& outShort) {
(void)num;
std::string base = "SIM " + std::to_string(s);
outShort = op.empty() ? base : (base + " (" + op + ")");
outLabel = base;
@@ -1693,9 +1739,6 @@ inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) {
if (!rat.empty()) {
outLabel += " [" + rat + "]";
}
if (!num.empty()) {
outLabel += " " + num;
}
};
formatSlotStrings(1, info.slot1Operator, info.slot1Rat, info.slot1Number,
@@ -2369,10 +2412,17 @@ inline std::string sendModemUssd(const std::string& input,
}
if (outSessionActive) {
std::string stOut = runCaptureCmd(
"mmcli -m any --3gpp-ussd-status -K 2>/dev/null | grep -E 'modem\\.3gpp\\.ussd\\.(status|state)'");
if (stOut.find("user-response") != std::string::npos) {
std::ifstream stIn("/tmp/sxmo_ussd_session_state");
std::string qmiSt;
if (stIn.is_open() && std::getline(stIn, qmiSt) &&
qmiSt.find("user-response") != std::string::npos) {
*outSessionActive = true;
} else {
std::string stOut = runCaptureCmd(
"mmcli -m any --3gpp-ussd-status -K 2>/dev/null | grep -E 'modem\\.3gpp\\.ussd\\.(status|state)'");
if (stOut.find("user-response") != std::string::npos) {
*outSessionActive = true;
}
}
}