Exemple #1
0
/** Append an interface info.
 * @param type type of the interface
 * @param id id of the interface
 * @param hash version hash
 * @param has_writer true if there is a writer, false otherwise
 * @param num_readers number of readers
 * @param serial instance serial
 * @param readers name of readers of interface
 * @param writer name of writer of interface
 * @param timestamp interface timestamp (time of last write or data timestamp)
 */
void
InterfaceInfoList::append(const char *type, const char *id, const unsigned char *hash,
			  unsigned int serial, bool has_writer, unsigned int num_readers,
			  const std::list<std::string> &readers, const std::string &writer,
			  const Time &timestamp)
{
  push_back(InterfaceInfo(type, id, hash, serial, has_writer, num_readers, readers, writer, &timestamp));
}
bool updateSystemInfoP( struct SystemInfo &si )
{
	char line[ 512 ];
	FILE *af;

	uint32	cpu;
	uint64	jiffyUser, jiffyNice, jiffySyst, jiffyIdle;
	uint64	jiffyIOwait, jiffyIRQ, jiffySoftIRQ;

	if ((af = fopen( "/proc/stat", "r" )) == NULL)
	{
		syslog( LOG_ERR, "Couldn't read /proc/stat: %s", strerror( errno ) );
		return false;
	}

	fgets( line, sizeof( line ), af );

	uint64 totalWork, totalIdle;
	uint64 systemIOwait = 0;
	uint64 systemTotalWork = 0;
	for (uint i=0; i < si.nCpus; i++)
	{
		fgets( line, sizeof( line ), af );

		if (hasExtendedStats)
		{
			if (sscanf( line, "cpu%u "PRIu64" "PRIu64" "PRIu64" "PRIu64" "PRIu64" "PRIu64" "PRIu64"",
					&cpu, &jiffyUser, &jiffyNice, &jiffySyst, &jiffyIdle,
					&jiffyIOwait, &jiffyIRQ, &jiffySoftIRQ) != 8)
			{
				syslog( LOG_ERR, "Invalid line in /proc/stat: '%s'", line );
				break;
			}
		}
		else
		{
			if (sscanf( line, "cpu%u "PRIu64" "PRIu64" "PRIu64" "PRIu64,
				&cpu, &jiffyUser, &jiffyNice, &jiffySyst, &jiffyIdle) != 5)
			{
				syslog( LOG_ERR, "Invalid line in /proc/stat: '%s'", line );
				break;
			}
		}

		if (cpu != i)
		{
			syslog( LOG_CRIT, "Line %d of /proc/stat was cpu%u, not cpu%d",
				i, cpu, i );
		}

		totalWork = jiffyUser + jiffyNice + jiffySyst;
		totalIdle = jiffyIdle;

		if (hasExtendedStats)
		{
			totalIdle += jiffyIOwait + jiffyIRQ + jiffySoftIRQ;
		}

		si.cpu[i].val.update( totalWork );
		si.cpu[i].max.update( totalWork + totalIdle );

		systemIOwait += jiffyIOwait;
		systemTotalWork += totalWork + totalIdle;
	}
	si.iowait.val.update( systemIOwait );
	si.iowait.max.update( systemTotalWork );

	fclose(af);

	if ((af = fopen( "/proc/meminfo", "r" )) == NULL)
	{
		syslog( LOG_ERR, "Couldn't read /proc/meminfo: %s", strerror( errno ) );
		return false;
	}

	unsigned long totalmem, freemem, bufmem, cachemem, slabmem;

	for (int i=0; i < 5 && fgets( line, sizeof( line ), af) != NULL;)
	{
		if (sscanf( line, "MemTotal: %lu", &totalmem ) == 1)
		{
			i++; continue;
		}
		if (sscanf( line, "MemFree: %lu", &freemem ) == 1)
		{
			i++; continue;
		}
		if (sscanf( line, "Buffers: %lu", &bufmem ) == 1)
		{
			i++; continue;
		}
		if (sscanf( line, "Cached: %lu", &cachemem ) == 1)
		{
			i++; continue;
		}
		if (sscanf( line, "Slab: %lu", &slabmem ) == 1)
		{
			i++; continue;
		}
	}

	fclose( af );

	si.mem.max.update( uint64( totalmem ) << 10 );
	si.mem.val.update(
		uint64( totalmem - freemem - bufmem - cachemem - slabmem ) << 10 );

	if ((af = fopen( "/proc/net/snmp", "r" )) == NULL)
	{
		syslog( LOG_ERR, "Couldn't read /proc/net/snmp: %s", strerror( errno ) );
		return false;
	}

	fgets( line, sizeof( line ), af );
	if (fscanf( af,"%*s %*d %*d %*d %*d %*d %*d %*d "PRIu64" "PRIu64" "PRIu64" "PRIu64,
		&si.packDropIn.next(), &si.packTotIn.next(),
		&si.packTotOut.next(), &si.packDropOut.next() ) != 4)
	{
		syslog( LOG_ERR, "Failed to read packet loss information from "
				"/proc/net/snmp" );
	}
	fclose( af );

	if ((af = fopen( "/proc/net/dev", "r" )) == NULL)
	{
		syslog( LOG_ERR, "Couldn't open /proc/net/dev: %s", strerror( errno ) );
		return false;
	}

	fgets( line, sizeof( line ), af ); fgets( line, sizeof( line ), af );
	for (unsigned int i=0; fgets( line, sizeof( line ), af ) != NULL; )
	{
		if (i >= si.ifInfo.size())
			si.ifInfo.push_back( InterfaceInfo() );
		struct InterfaceInfo &ifInfo = si.ifInfo[i];
		char ifname[32];

		sscanf( line, " %[^:]", ifname );
		if (strstr( ifname, "lo" ) == NULL)
		{
			ifInfo.name = ifname;
			sscanf( line,
				" %*[^:]:"PRIu64" "PRIu64" %*d %*d %*d %*d %*d %*d "
				PRIu64" "PRIu64,
				&ifInfo.bitsTotIn.next(),
				&ifInfo.packTotIn.next(),
				&ifInfo.bitsTotOut.next(),
				&ifInfo.packTotOut.next() );

			ifInfo.bitsTotIn.cur() *= 8;
			ifInfo.bitsTotOut.cur() *= 8;
			i++;
		}
	}

	fclose( af );
	return true;
}
Exemple #3
0
QList<SerialPortId> enumerateSerialPorts(int loglevel)
{
    QList<QString> eligibleInterfaces;
    QList<InterfaceInfo> eligibleInterfacesInfo;
    QList<SerialPortId> list;

    usb_init();
    usb_find_busses();
    usb_find_devices();

    for (struct usb_bus *bus = usb_get_busses(); bus; bus = bus->next) {
        for (struct usb_device *device = bus->devices; device; device = device->next) {
            for (int n = 0; n < device->descriptor.bNumConfigurations; ++n) {
                struct usb_config_descriptor &usbConfig =device->config[n];
                QList<int> usableInterfaces;
                for (int m = 0; m < usbConfig.bNumInterfaces; ++m) {
                    for (int o = 0; o < usbConfig.interface[m].num_altsetting; ++o) {
                        struct usb_interface_descriptor &descriptor = usbConfig.interface[m].altsetting[o];
                        if (descriptor.bInterfaceClass != 2 // "Communication"
                                || descriptor.bInterfaceSubClass != 2 // Abstract (modem)
                                || descriptor.bInterfaceProtocol != 255) // Vendor Specific
                            continue;

                        unsigned char *buf = descriptor.extra;
                        unsigned int size = descriptor.extralen;
                        while (size >= 2 * sizeof(u_int8_t)) {
                            // for Communication devices there is a slave interface for the actual
                            // data transmission.
                            // the extra info stores that as a index for the interface
                            if (buf[0] >= 5 && buf[1] == 36 && buf[2] == 6) { // CDC Union
                                for (int i = 4; i < buf[0]; i++)
                                    usableInterfaces.append((int) buf[i]);
                            }
                            size -= buf[0];
                            buf += buf[0];
                        }
                    }
                }
                
                if (usableInterfaces.isEmpty())
                    continue;
                
                QString manufacturerString;
                QString productString;
                
                usb_dev_handle *devh = usb_open(device);
                if (devh) {
                    QByteArray buf;
                    buf.resize(256);
                    int err = usb_get_string_simple(devh, device->descriptor.iManufacturer, buf.data(), buf.size());
                    if (err < 0) {
                        if (loglevel > 1)
                            qDebug() << "      can't read manufacturer name, error:" << err;
                    } else {
                        manufacturerString = QString::fromAscii(buf);
                        if (loglevel > 1)
                            qDebug() << "      manufacturer:" << manufacturerString;
                    }

                    buf.resize(256);
                    err = usb_get_string_simple(devh, device->descriptor.iProduct, buf.data(), buf.size());
                    if (err < 0) {
                        if (loglevel > 1)
                            qDebug() << "      can't read product name, error:" << err;
                    } else {
                        productString = QString::fromAscii(buf);
                        if (loglevel > 1)
                            qDebug() << "      product:" << productString;
                    }
                    usb_close(devh);
                } else if (loglevel > 0) {
                    qDebug() << "      can't open usb device";
                }

                // second loop to find the actual data interface.
                foreach (int i, usableInterfaces) {
                    for (int m = 0; m < usbConfig.bNumInterfaces; ++m) {
                        for (int o = 0; o < usbConfig.interface[m].num_altsetting; ++o) {
                            struct usb_interface_descriptor &descriptor = usbConfig.interface[m].altsetting[o];
                            if (descriptor.bInterfaceNumber != i)
                                continue;
                            if (descriptor.bInterfaceClass == 10) { // "CDC Data"
                                if (loglevel > 1) {
                                    qDebug() << "      found the data port"
                                             << "bus:" << bus->dirname
                                             << "device" << device->filename
                                             << "interface" << descriptor.bInterfaceNumber;
                                }
                                // ### manufacturer and product strings are only readable as root :(
                                if (!manufacturerString.isEmpty() && !productString.isEmpty()) {
                                    eligibleInterfaces << QString("usb-%1_%2-if%3")
                                                          .arg(manufacturerString.replace(QChar(' '), QChar('_')))
                                                          .arg(productString.replace(QChar(' '), QChar('_')))
                                                          .arg(i, 2, 16, QChar('0'));
                                } else {
                                    eligibleInterfaces << QString("if%1").arg(i, 2, 16, QChar('0')); // fix!
                                }
                                eligibleInterfacesInfo << InterfaceInfo(manufacturerString, productString, device->descriptor.idVendor, device->descriptor.idProduct);
                            }
                        }
                    }
                }
            }
        }
    }
    
    if (loglevel > 1)
        qDebug() << "      searching for interfaces:" << eligibleInterfaces;

    QDir dir("/dev/serial/by-id/");
    foreach (const QFileInfo &info, dir.entryInfoList()) {
        if (!info.isDir()) {
            bool usable = eligibleInterfaces.isEmpty();
            foreach (const QString &iface, eligibleInterfaces) {
                if (info.fileName().contains(iface)) {
                    if (loglevel > 1)
                        qDebug() << "      found device file:" << info.fileName() << endl;
                    usable = true;
                    break;
                }
            }
            if (!usable)
                continue;

            SerialPortId id;
            id.friendlyName = info.fileName();
            id.portName = info.canonicalFilePath();
            list << id;
        }
    }
Exemple #4
0
/** Append an interface info.
 * @param type type of the interface
 * @param id id of the interface
 * @param hash version hash
 * @param has_writer true if there is a writer, false otherwise
 * @param num_readers number of readers
 * @param serial instance serial
 */
void
InterfaceInfoList::append(const char *type, const char *id, const unsigned char *hash,
			  unsigned int serial, bool has_writer, unsigned int num_readers)
{
  push_back(InterfaceInfo(type, id, hash, serial, has_writer, num_readers));
}
Exemple #5
0
QList<SerialPortId> enumerateSerialPorts(int loglevel)
{
    QList<QString> eligibleInterfaces;
    QList<InterfaceInfo> eligibleInterfacesInfo;
    QList<SerialPortId> list;

    usb_init();
    usb_find_busses();
    usb_find_devices();

    for (struct usb_bus *bus = usb_get_busses(); bus; bus = bus->next) {
        for (struct usb_device *device = bus->devices; device; device = device->next) {
            for (int n = 0; n < device->descriptor.bNumConfigurations && device->config; ++n) {
                struct usb_config_descriptor &usbConfig =device->config[n];
                QList<int> usableInterfaces;
                for (int m = 0; m < usbConfig.bNumInterfaces; ++m) {
                    for (int o = 0; o < usbConfig.interface[m].num_altsetting; ++o) {
                        struct usb_interface_descriptor &descriptor = usbConfig.interface[m].altsetting[o];
                        if (descriptor.bInterfaceClass != 2 // "Communication"
                                || descriptor.bInterfaceSubClass != 2 // Abstract (modem)
                                || descriptor.bInterfaceProtocol != 255) // Vendor Specific
                            continue;

                        unsigned char *buf = descriptor.extra;
                        unsigned int size = descriptor.extralen;
                        while (size >= 2 * sizeof(u_int8_t)) {
                            // for Communication devices there is a slave interface for the actual
                            // data transmission.
                            // the extra info stores that as a index for the interface
                            if (buf[0] >= 5 && buf[1] == 36 && buf[2] == 6) { // CDC Union
                                for (int i = 3; i < buf[0]; i++)
                                    usableInterfaces.append((int) buf[i]);
                            }
                            size -= buf[0];
                            buf += buf[0];
                        }
                    }
                }
                
                if (usableInterfaces.isEmpty())
                    continue;
                
                QString manufacturerString;
                QString productString;
                
                usb_dev_handle *devh = usb_open(device);
                if (devh) {
                    QByteArray buf;
                    buf.resize(256);
                    int err = usb_get_string_simple(devh, device->descriptor.iManufacturer, buf.data(), buf.size());
                    if (err < 0) {
                        if (loglevel > 1)
                            qDebug() << "      can't read manufacturer name, error:" << err;
                    } else {
                        manufacturerString = QString::fromAscii(buf);
                        if (loglevel > 1)
                            qDebug() << "      manufacturer:" << manufacturerString;
                    }

                    buf.resize(256);
                    err = usb_get_string_simple(devh, device->descriptor.iProduct, buf.data(), buf.size());
                    if (err < 0) {
                        if (loglevel > 1)
                            qDebug() << "      can't read product name, error:" << err;
                    } else {
                        productString = QString::fromAscii(buf);
                        if (loglevel > 1)
                            qDebug() << "      product:" << productString;
                    }
                    usb_close(devh);
                } else if (loglevel > 0) {
                    qDebug() << "      can't open usb device";
                }

                // second loop to find the actual data interface.
                foreach (int i, usableInterfaces) {
#ifdef Q_OS_MAC
                    eligibleInterfaces << QString("^cu\\.usbmodem.*%1$")
                        .arg(QString("%1").arg(i, 1, 16).toUpper());
                    eligibleInterfacesInfo << InterfaceInfo(manufacturerString, productString, device->descriptor.idVendor, device->descriptor.idProduct);
#else
                    // ### manufacturer and product strings are only readable as root :(
                    if (!manufacturerString.isEmpty() && !productString.isEmpty()) {
                        eligibleInterfaces << QString("usb-%1_%2-if%3")
                             .arg(manufacturerString.replace(QChar(' '), QChar('_')))
                             .arg(productString.replace(QChar(' '), QChar('_')))
                             .arg(i, 2, 16, QChar('0'));
                    } else {
                        eligibleInterfaces << QString("if%1").arg(i, 2, 16, QChar('0')); // fix!
                    }
#endif
                }
#ifndef Q_OS_MAC
                // On mac, we need a 1-1 mapping between eligibleInterfaces and eligibleInterfacesInfo
                // in order to find the friendly name for an interface
                eligibleInterfacesInfo << InterfaceInfo(manufacturerString, productString, device->descriptor.idVendor, device->descriptor.idProduct);
#endif
            }
        }
    }