#include "global.h"

QSettings* g_networkSettingInfo;

void getLocalIP(QStringList& localIPList) {
    QHostInfo hostInfo = QHostInfo::fromName(QHostInfo::localHostName());
    QList<QHostAddress> ipAddressesList;
    foreach (QHostAddress address, hostInfo.addresses()) {
        if (address.protocol() == QAbstractSocket::IPv4Protocol) {
            localIPList.append(address.toString());
        }
    }
}

void getLocalSerialPorts(QStringList& localCOMList) {
    foreach (const QSerialPortInfo& info, QSerialPortInfo::availablePorts()) {
        localCOMList.append(info.portName());
    }
}

bool isValidIPAddress(const QString& ip) {
    QHostAddress address;
    return address.setAddress(ip);
}

bool isMulticastAddress(const QString& ip) {
    QHostAddress address;
    if (!address.setAddress(ip)) {
        return false;  // 如果不是有效的IP地址，直接返回false
    }

    if (address.protocol() == QAbstractSocket::IPv4Protocol) {
        quint32 ip4 = address.toIPv4Address();
        return ip4 >= QHostAddress("224.0.0.0").toIPv4Address() &&
               ip4 <= QHostAddress("239.255.255.255").toIPv4Address();
    } else if (address.protocol() == QAbstractSocket::IPv6Protocol) {
        QString ipv6 = address.toString();
        return ipv6.startsWith("ff",
                               Qt::CaseInsensitive);  // IPv6组播地址以"ff"开头
    }

    return false;
}

Global::Global() {}
