Add Dual-SIM Dual Standby (DSDS), outgoing SIM selection, and instant charging icon update
This commit is contained in:
1 parent
5132deb4d8
commit
46f1255905
28 files changed
+2560
-280
No files matched your search
@@ -1421,15 +1421,314 @@ inline bool clearCallLogFile(const std::string& logPath) {
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void placePhoneCall(const std::string& rawNumber) {
|
||||
struct DualSimInfo {
|
||||
bool dualSimActive = false;
|
||||
bool slot1Present = false;
|
||||
bool slot2Present = false;
|
||||
int preferredSlot = 1;
|
||||
int primaryGwSlot = 1;
|
||||
int secondaryGwSlot = 0;
|
||||
std::string slot1Operator;
|
||||
std::string slot2Operator;
|
||||
std::string slot1Number;
|
||||
std::string slot2Number;
|
||||
std::string slot1Rat;
|
||||
std::string slot2Rat;
|
||||
std::string slot1Label;
|
||||
std::string slot2Label;
|
||||
std::string slot1Short;
|
||||
std::string slot2Short;
|
||||
};
|
||||
|
||||
inline DualSimInfo queryDualSimInfo(bool forceRefresh = false) {
|
||||
static DualSimInfo cached;
|
||||
static int64_t lastQueryMs = 0;
|
||||
int64_t now = nowMs();
|
||||
if (!forceRefresh && lastQueryMs > 0 && (now - lastQueryMs) < 3000) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
DualSimInfo info;
|
||||
{
|
||||
std::ifstream conf("/etc/conf.d/msm-modem-uim-selection");
|
||||
std::string line;
|
||||
while (std::getline(conf, line)) {
|
||||
if (line.rfind("preferred_sim_slot=", 0) == 0) {
|
||||
int v = std::atoi(line.c_str() + 19);
|
||||
if (v == 1 || v == 2) info.preferredSlot = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
constexpr int kAfQipcrtr = 42;
|
||||
constexpr uint32_t kQrtrPortCtrl = 0xfffffffeu;
|
||||
constexpr uint32_t kQrtrTypeNewServer = 4;
|
||||
constexpr uint32_t kQrtrTypeNewLookup = 10;
|
||||
constexpr uint32_t kQmiServiceDms = 2;
|
||||
constexpr uint32_t kQmiServiceNas = 3;
|
||||
constexpr uint32_t kQmiServiceUim = 11;
|
||||
|
||||
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));
|
||||
|
||||
auto lookupService = [&](uint32_t sid, uint32_t& outNode, uint32_t& outPort) -> bool {
|
||||
int fd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0);
|
||||
if (fd < 0) return false;
|
||||
SockAddrQrtr localSq {};
|
||||
socklen_t sl = sizeof(localSq);
|
||||
if (::getsockname(fd, reinterpret_cast<struct sockaddr*>(&localSq), &sl) < 0) {
|
||||
::close(fd);
|
||||
return false;
|
||||
}
|
||||
SockAddrQrtr ctrlSq { kAfQipcrtr, localSq.sq_node, kQrtrPortCtrl };
|
||||
QrtrCtrlPkt lookup { kQrtrTypeNewLookup, sid, 0, 0, 0 };
|
||||
if (::sendto(fd, &lookup, sizeof(lookup), 0,
|
||||
reinterpret_cast<const struct sockaddr*>(&ctrlSq),
|
||||
sizeof(ctrlSq)) < 0) {
|
||||
::close(fd);
|
||||
return false;
|
||||
}
|
||||
bool found = false;
|
||||
for (int i = 0; i < 12; ++i) {
|
||||
struct pollfd pfd { fd, POLLIN, 0 };
|
||||
if (::poll(&pfd, 1, 40) <= 0) break;
|
||||
QrtrCtrlPkt pkt {};
|
||||
ssize_t n = ::recv(fd, &pkt, sizeof(pkt), 0);
|
||||
if (n >= static_cast<ssize_t>(sizeof(pkt)) && pkt.cmd == kQrtrTypeNewServer) {
|
||||
if (pkt.service == sid && pkt.port != 0) {
|
||||
outNode = pkt.node;
|
||||
outPort = pkt.port;
|
||||
found = true;
|
||||
}
|
||||
if (pkt.service == 0 && pkt.node == 0 && pkt.port == 0) break;
|
||||
}
|
||||
}
|
||||
::close(fd);
|
||||
return found;
|
||||
};
|
||||
|
||||
uint32_t uimNode = 0, uimPort = 0;
|
||||
if (lookupService(kQmiServiceUim, uimNode, uimPort)) {
|
||||
int sfd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0);
|
||||
if (sfd >= 0) {
|
||||
SockAddrQrtr uimSq { kAfQipcrtr, uimNode, uimPort };
|
||||
uint8_t req[7] = { 0x00, 0x01, 0x00, 0x2F, 0x00, 0x00, 0x00 };
|
||||
if (::sendto(sfd, req, sizeof(req), 0,
|
||||
reinterpret_cast<const struct sockaddr*>(&uimSq),
|
||||
sizeof(uimSq)) >= 0) {
|
||||
struct pollfd pfd { sfd, POLLIN, 0 };
|
||||
if (::poll(&pfd, 1, 60) > 0) {
|
||||
uint8_t buf[1024];
|
||||
ssize_t rlen = ::recv(sfd, buf, sizeof(buf), 0);
|
||||
if (rlen >= 7 && buf[0] == 0x02 && buf[3] == 0x2F) {
|
||||
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 >= 9) {
|
||||
const uint8_t* p = buf + pos;
|
||||
uint16_t gwPri = static_cast<uint16_t>(p[0]) | (static_cast<uint16_t>(p[1]) << 8);
|
||||
uint16_t gwSec = static_cast<uint16_t>(p[4]) | (static_cast<uint16_t>(p[5]) << 8);
|
||||
if (gwPri != 0xFFFF) info.primaryGwSlot = ((gwPri >> 8) & 0xFF) + 1;
|
||||
if (gwSec != 0xFFFF) info.secondaryGwSlot = ((gwSec >> 8) & 0xFF) + 1;
|
||||
uint8_t numSlots = p[8];
|
||||
size_t off = 9;
|
||||
for (uint8_t s = 0; s < numSlots && s < 2; ++s) {
|
||||
if (off + 6 > tl) break;
|
||||
uint8_t cstate = p[off];
|
||||
if (s == 0) info.slot1Present = (cstate == 1);
|
||||
if (s == 1) info.slot2Present = (cstate == 1);
|
||||
uint8_t numApps = p[off + 5];
|
||||
off += 6;
|
||||
for (uint8_t a = 0; a < numApps; ++a) {
|
||||
if (off + 7 > tl) break;
|
||||
uint8_t aidLen = p[off + 6];
|
||||
if (off + 14 + aidLen > tl) break;
|
||||
off += 14 + aidLen;
|
||||
}
|
||||
}
|
||||
}
|
||||
pos += tl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
::close(sfd);
|
||||
}
|
||||
}
|
||||
|
||||
info.dualSimActive = info.slot1Present && info.slot2Present &&
|
||||
(info.primaryGwSlot == 1 || info.primaryGwSlot == 2) &&
|
||||
(info.secondaryGwSlot == 1 || info.secondaryGwSlot == 2);
|
||||
|
||||
uint32_t nasNode = 0, nasPort = 0;
|
||||
uint32_t dmsNode = 0, dmsPort = 0;
|
||||
bool hasNas = lookupService(kQmiServiceNas, nasNode, nasPort);
|
||||
bool hasDms = lookupService(kQmiServiceDms, dmsNode, dmsPort);
|
||||
|
||||
auto querySubMeta = [&](int slot, int sub, std::string& outOp,
|
||||
std::string& outRat, std::string& outNum) {
|
||||
if (sub < 0) return;
|
||||
if (hasNas) {
|
||||
int sfd = ::socket(kAfQipcrtr, SOCK_DGRAM | SOCK_CLOEXEC, 0);
|
||||
if (sfd >= 0) {
|
||||
SockAddrQrtr nasSq { kAfQipcrtr, nasNode, nasPort };
|
||||
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,
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
::close(sfd);
|
||||
}
|
||||
}
|
||||
if (hasDms) {
|
||||
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)
|
||||
};
|
||||
::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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
pos += tl;
|
||||
}
|
||||
}
|
||||
}
|
||||
::close(sfd);
|
||||
}
|
||||
}
|
||||
(void)slot;
|
||||
};
|
||||
|
||||
int sub1 = (info.primaryGwSlot == 1) ? 0 : ((info.secondaryGwSlot == 1) ? 1 : -1);
|
||||
int sub2 = (info.primaryGwSlot == 2) ? 0 : ((info.secondaryGwSlot == 2) ? 1 : -1);
|
||||
querySubMeta(1, sub1, info.slot1Operator, info.slot1Rat, info.slot1Number);
|
||||
querySubMeta(2, sub2, info.slot2Operator, info.slot2Rat, info.slot2Number);
|
||||
|
||||
auto formatSlotStrings = [](int s, const std::string& op, const std::string& rat,
|
||||
const std::string& num, std::string& outLabel,
|
||||
std::string& outShort) {
|
||||
std::string base = "SIM " + std::to_string(s);
|
||||
outShort = op.empty() ? base : (base + " (" + op + ")");
|
||||
outLabel = base;
|
||||
if (!op.empty()) {
|
||||
outLabel += ": " + op;
|
||||
}
|
||||
if (!rat.empty()) {
|
||||
outLabel += " [" + rat + "]";
|
||||
}
|
||||
if (!num.empty()) {
|
||||
outLabel += " " + num;
|
||||
}
|
||||
};
|
||||
|
||||
formatSlotStrings(1, info.slot1Operator, info.slot1Rat, info.slot1Number,
|
||||
info.slot1Label, info.slot1Short);
|
||||
formatSlotStrings(2, info.slot2Operator, info.slot2Rat, info.slot2Number,
|
||||
info.slot2Label, info.slot2Short);
|
||||
|
||||
cached = info;
|
||||
lastQueryMs = now;
|
||||
return info;
|
||||
}
|
||||
|
||||
inline void placePhoneCall(const std::string& rawNumber, int simSlot = 0) {
|
||||
std::string num = normalizePhone(rawNumber);
|
||||
if (num.empty()) {
|
||||
return;
|
||||
}
|
||||
if (!sendPluginCommand("callscreen", "OUTGOING - " + num)) {
|
||||
if (simSlot == 0) {
|
||||
DualSimInfo dsi = queryDualSimInfo();
|
||||
if (dsi.dualSimActive) {
|
||||
if (!sendPluginCommand("callscreen", "PROMPT_SIM " + num)) {
|
||||
runAsyncArgv("sxmo_modemdial.sh", num.c_str());
|
||||
}
|
||||
return;
|
||||
}
|
||||
simSlot = (dsi.primaryGwSlot == 2) ? 2 : 1;
|
||||
}
|
||||
std::string simTag = "SIM" + std::to_string(simSlot);
|
||||
if (!sendPluginCommand("callscreen", "OUTGOING - " + num + " " + simTag)) {
|
||||
runAsyncArgv("xcallscreen", "outgoing", "-", num.c_str());
|
||||
}
|
||||
runAsyncArgv("sxmo_modemdial.sh", num.c_str());
|
||||
std::string simStr = std::to_string(simSlot);
|
||||
runAsyncArgv("sxmo_modemdial.sh", "--sim", simStr.c_str(), num.c_str());
|
||||
}
|
||||
|
||||
inline void closeOtherPlugins(const char* selfPluginName) {
|
||||
@@ -1940,11 +2239,11 @@ inline bool queryQrtrDmsImeis(DeviceImeis& out) {
|
||||
dmsSq.sq_node = dmsNode;
|
||||
dmsSq.sq_port = dmsPort;
|
||||
|
||||
// 1. QMI_DMS_BIND_SUBSCRIPTION (0x0057), TLV 0x01 = uint32_t(subType)
|
||||
// 1. QMI_DMS_BIND_SUBSCRIPTION (0x0054), TLV 0x01 = uint32_t(subType)
|
||||
if (subType > 0) {
|
||||
uint8_t bindReq[14] = {
|
||||
0x00, 0x01, 0x00, // flags=0 (req), txn_id=1
|
||||
0x57, 0x00, // msg_id = 0x0057
|
||||
0x54, 0x00, // msg_id = 0x0054
|
||||
0x07, 0x00, // tlv_len = 7
|
||||
0x01, 0x04, 0x00, // TLV 0x01, len 4
|
||||
static_cast<uint8_t>(subType & 0xFF), 0x00, 0x00, 0x00
|
||||
@@ -2049,16 +2348,20 @@ inline bool isUssdCode(const std::string& code) {
|
||||
|
||||
inline std::string sendModemUssd(const std::string& input,
|
||||
bool isReply = false,
|
||||
bool* outSessionActive = nullptr) {
|
||||
bool* outSessionActive = nullptr,
|
||||
int simSlot = 0) {
|
||||
if (outSessionActive) *outSessionActive = false;
|
||||
std::string clean = trimStr(input);
|
||||
if (clean.empty()) return "Empty USSD request.";
|
||||
|
||||
int rc = 0;
|
||||
std::string mode = (isReply && !isUssdCode(clean)) ? "respond" : "initiate";
|
||||
std::string simArg = (simSlot == 1 || simSlot == 2)
|
||||
? ("--sim " + std::to_string(simSlot) + " ")
|
||||
: "";
|
||||
std::string cmd =
|
||||
"doas -n /usr/libexec/msm-modem-uim-selection ussd " + mode + " " +
|
||||
shellQuote(clean) + " 2>&1";
|
||||
simArg + shellQuote(clean) + " 2>&1";
|
||||
|
||||
std::string out = trimStr(runCaptureCmd(cmd, &rc));
|
||||
if (out.empty()) {
|
||||
|
||||
Reference in new issue
Block a user