Support scanning ANSVIS servers device

This commit is contained in:
2026-05-23 18:03:22 +10:00
parent 4678d87e33
commit a2fa2c8ceb
5 changed files with 568 additions and 4 deletions
+78
View File
@@ -2973,3 +2973,81 @@ extern "C" ANSULT_API int DownloadFileStreamANSAWSHandle_V2(uint64_t handleVal,
catch (std::exception& e) { return 0; }
catch (...) { return 0; }
}
// ---------------------------------------------------------------------------
// ScanANSDevices — discover AIBOX / ANSVIS servers on the local network.
// Returns 1 on success (resultJson populated, possibly with empty "[]"), 0
// on internal failure. The static C++ method does its own license/error
// handling; this wrapper just serializes the device list to JSON.
// ---------------------------------------------------------------------------
static std::string JsonEscape(const std::string& s) {
std::string out;
out.reserve(s.size() + 2);
for (char c : s) {
switch (c) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (static_cast<unsigned char>(c) < 0x20) {
char buf[8];
snprintf(buf, sizeof(buf), "\\u%04x", static_cast<unsigned char>(c));
out += buf;
} else {
out += c;
}
}
}
return out;
}
static std::string DevicesToJson(const std::vector<ANSCENTER::ANSDeviceInfo>& devs) {
std::string j = "[";
for (size_t i = 0; i < devs.size(); ++i) {
if (i) j += ',';
j += "{\"ip\":\""; j += JsonEscape(devs[i].ip); j += "\",";
j += "\"name\":\""; j += JsonEscape(devs[i].name); j += "\",";
j += "\"firmware\":\""; j += JsonEscape(devs[i].firmware); j += "\",";
j += "\"source\":"; j += std::to_string(devs[i].source);
j += '}';
}
j += ']';
return j;
}
extern "C" ANSULT_API int ScanANSDevicesCpp(int timeoutMs, std::string& resultJson) {
ANS_DBG("ANSUTIL", "ScanANSDevicesCpp: timeoutMs=%d", timeoutMs);
try {
auto devs = ANSCENTER::ANSUtilities::ScanDevices(timeoutMs);
resultJson = DevicesToJson(devs);
return 1;
}
catch (const std::exception& e) {
ANS_DBG("ANSUTIL", "ScanANSDevicesCpp threw: %s", e.what());
resultJson = "[]";
return 0;
}
catch (...) {
ANS_DBG("ANSUTIL", "ScanANSDevicesCpp threw unknown");
resultJson = "[]";
return 0;
}
}
extern "C" ANSULT_API int ScanANSDevices(int timeoutMs, LStrHandle resultJson) {
ANS_DBG("ANSUTIL", "ScanANSDevices: timeoutMs=%d", timeoutMs);
if (!resultJson) return -1;
try {
std::string j;
ScanANSDevicesCpp(timeoutMs, j);
int size = static_cast<int>(j.size());
MgErr error = DSSetHandleSize(resultJson, sizeof(int32) + size * sizeof(uChar));
if (error != noErr) return -2;
(*resultJson)->cnt = size;
if (size > 0) memcpy((*resultJson)->str, j.data(), size);
return 1;
}
catch (...) { return -1; }
}