浏览代码

Neue Version zum Testen

Rind 7 年之前
父节点
当前提交
dfa693f864
共有 6 个文件被更改,包括 223 次插入50 次删除
  1. 7 4
      QmlTest/main.qml
  2. 28 0
      debug.h
  3. 35 4
      libgfanet/interfaces.cpp
  4. 114 32
      qtplugin/netinterfaces.cpp
  5. 35 6
      qtplugin/netinterfaces.h
  6. 4 4
      qtplugin/pdump.sh

+ 7 - 4
QmlTest/main.qml

@@ -19,9 +19,9 @@ Window {
 
     NetInterfaces {
         id: idItf
-        itfFilterName: "eth0"
+//        itfFilterName: "eth0"
         itfFilterAF: Interface.AF_Inet
-        itfFilterMethod: Interface.IM_Dhcp
+        itfFilterMethod: Interface.IM_Static | Interface.IM_Manual
         onSigError:
         {
             console.error(msg);
@@ -44,8 +44,11 @@ Window {
                 console.warn("No interface found!");
             }
 
-            idItf.itfFilterName = "eth1";
-            idItf.itfFilterMethod = Interface.IM_Static;
+            idItf.itfFilterMethod |= Interface.IM_Dhcp;
+//            idItf.itfFilterName = "eth1";
+            idItf.itfFilterMethod &= ~Interface.IM_Static;
+
+            idItf.filteredInterfaces[0].af = Interface.AF_Unknown;
 //            idItf.saveAs("/home/wrk/share/gfanet/libgfanet/res/testitf");
         }
     }

+ 28 - 0
debug.h

@@ -0,0 +1,28 @@
+// debug.h :
+//
+
+#if !defined(AGD_DEBUG_H__39391BF3_0394_4846_BFCB_028512359FF5__INCLUDED_)
+#define AGD_DEBUG_H__39391BF3_0394_4846_BFCB_028512359FF5__INCLUDED_
+
+#include <assert.h>
+#include <stdio.h>
+
+/////////////////////////////////////////////////////////////////////////////
+// debug.h - Declarations:
+
+#ifdef _DEBUG
+#define TRACE(...)					printf(__VA_ARGS__), fflush(stdout)
+#define ETRACE(...)					fprintf(stderr, __VA_ARGS__), fflush(stderr)
+#define ASSERT						assert
+#else	//	_DEBUG
+#define TRACE(...)					(void)0
+#define ETRACE(...)					(void)0
+#define ASSERT(e)					(void)0
+#endif	//	_DEBUG
+
+#ifndef UNUSED
+#define UNUSED(p)					(void)p
+#endif	//	UNUSED
+
+/////////////////////////////////////////////////////////////////////////////
+#endif	//	!defined(AGD_DEBUG_H__39391BF3_0394_4846_BFCB_028512359FF5__INCLUDED_)

+ 35 - 4
libgfanet/interfaces.cpp

@@ -2,10 +2,16 @@
 #include <regex>
 #include "util.h"
 #include "interfaces.h"
+#include "../debug.h"
 
 #define _ETC_NETWORK_INTERFACES			"/etc/network/interfaces"
 //#define _ETC_NETWORK_INTERFACES			"/home/wrk/share/gfanet/libgfanet/res/interfaces"
 
+#if 1
+#undef TRACE
+#define TRACE(...)						(void)0
+#endif
+
 /////////////////////////////////////////////////////////////////////////////
 // iface block
 
@@ -294,6 +300,7 @@ bool ParseIncludes(const std::vector<std::string> &v, ITF_INCLUDE_LIST &inc)
 
 void ProcessIface(ETC_NETWORK_INTERFACES &eni)
 {
+	TRACE("%s - enter\n", __FUNCTION__);
 	for(auto iti = eni.ibl.begin(); iti != eni.ibl.end(); iti++)
 	{
 		ITF_IFACE_BLOCK &ib = *iti;
@@ -308,12 +315,14 @@ void ProcessIface(ETC_NETWORK_INTERFACES &eni)
 				ib.inet4s.netprefix = 24;
 		}
 	}
+	TRACE("%s - leave\n", __FUNCTION__);
 }
 
 /////////////////////////////////////////////////////////////////////////////
 
 void ProcessGroupMembers(ETC_NETWORK_INTERFACES &eni)
 {
+	TRACE("%s - enter\n", __FUNCTION__);
 	// remove double configuration names in a single group
 	for(auto cIt = eni.cgl.begin(); cIt != eni.cgl.end(); cIt++)
 	{
@@ -327,7 +336,7 @@ void ProcessGroupMembers(ETC_NETWORK_INTERFACES &eni)
 	for(auto cIt1 = eni.cgl.begin(); cIt1 != eni.cgl.end(); cIt1++)
 	{
 		ITF_CONFIG_GROUP &icg1 = *cIt1;
-        for(auto cIt2 = ++cIt1; cIt2 != eni.cgl.end(); cIt2++)
+        for(auto cIt2 = std::next(cIt1); cIt2 != eni.cgl.end(); cIt2++)
 		{
 			ITF_CONFIG_GROUP &icg2 = *cIt2;
 			if(!icg1.cfgName.compare(icg2.cfgName))
@@ -361,6 +370,7 @@ void ProcessGroupMembers(ETC_NETWORK_INTERFACES &eni)
 			}
 		}
 	}
+	TRACE("%s - leave\n", __FUNCTION__);
 }
 
 /////////////////////////////////////////////////////////////////////////////
@@ -368,6 +378,8 @@ void ProcessGroupMembers(ETC_NETWORK_INTERFACES &eni)
 
 bool ParseEtcNetworkInterfaces(ETC_NETWORK_INTERFACES &eni, const char *pszPath)
 {
+	TRACE("%s\n", __FUNCTION__);
+
 	typedef enum ParseStates
 	{
 		PS_Root,
@@ -375,19 +387,28 @@ bool ParseEtcNetworkInterfaces(ETC_NETWORK_INTERFACES &eni, const char *pszPath)
 		PS_Mapping
 	}ParseStates;
 
+	TRACE("  initializing regex ...\n");
+
 	static const std::string strRegExComt = "^\\s*#.*";
 	static const std::string strRegExLine = "([^\\s]+)";
 	static const std::regex regc(strRegExComt, std::regex_constants::ECMAScript | std::regex_constants::optimize);
 	static const std::regex regl(strRegExLine, std::regex_constants::ECMAScript | std::regex_constants::optimize);
 	std::string buf;
 
+	TRACE("  complete.\n");
+
 	if(!pszPath)
 		pszPath = _ETC_NETWORK_INTERFACES;
+
+	TRACE("  file path: %s\n", pszPath);
 		
 	eni._reset();
 
+	TRACE("  reading file ...\n");
+
 	if(ReadFile(pszPath, buf) > 0)
 	{
+		TRACE("  success.\n");
 		ParseStates ps = PS_Root;
 		std::string line;
 		std::istringstream iss(buf);
@@ -397,11 +418,17 @@ bool ParseEtcNetworkInterfaces(ETC_NETWORK_INTERFACES &eni, const char *pszPath)
 		ITF_IFACE_BLOCK ib;
 		ITF_MAPPING_BLOCK mab;
 
-        while(std::getline(stm, line))
+        while(!(stm >> std::ws).eof())
 		{
+			if(std::getline(stm, line).fail())
+			{
+				TRACE("%s: failed to read stream!\n", __FUNCTION__);
+				return false;
+			}
 			trim(line);
+			TRACE("%zu\n", line.size());
 
-			if(line.size() > 0)
+			if(!line.empty())
 			{
 				if(std::regex_match(line, regc))
 					continue;
@@ -449,7 +476,7 @@ bool ParseEtcNetworkInterfaces(ETC_NETWORK_INTERFACES &eni, const char *pszPath)
 				else
 				{
 					eni.unparsed.push_back(line);
-					printf("%d - %s\n", ps, line.c_str());
+					TRACE("%d - %s\n", ps, line.c_str());
 				}
 			}
 		}
@@ -458,6 +485,10 @@ bool ParseEtcNetworkInterfaces(ETC_NETWORK_INTERFACES &eni, const char *pszPath)
 		ProcessGroupMembers(eni);
 		return true;
 	}
+	else
+	{
+		TRACE("  error!\n");
+	}
 
 	return false;
 }

+ 114 - 32
qtplugin/netinterfaces.cpp

@@ -1,4 +1,10 @@
 #include "netinterfaces.h"
+#include "../debug.h"
+
+#if 1
+#undef TRACE
+#define TRACE(...)					(void)0
+#endif
 
 #ifndef _countof
 #define _countof(a)					(sizeof(a) / sizeof(*a))
@@ -6,34 +12,80 @@
 
 #define _IS_VALID_BYTE_VALUE(b)		(((b) >= 0) && ((b) <= 255))
 
+
+template<typename T>
+static bool _IsPowerOf2(T x)
+{
+	return x && !(x & (x - 1));
+}
+
+template<typename T>
+static unsigned int _BitCount(T n)
+{
+	unsigned int count = 0;
+	while(n)
+	{
+		count++;
+		n &= (n - 1);
+	}
+	return count;
+}
+
+template<typename T>
+static int _BitNumber(T n)
+{
+	if(!_IsPowerOf2(n))
+		return -1;
+	int count = 0;
+	while(n)
+	{
+		count++;
+		n >>= 1;
+	}
+	return count;
+}
+
+
+#define _FLAG_TO_ENUM(f)		(_BitNumber(f))
+#define _ENUM_TO_FLAG(e)		(0x00000001 << e)
+
 /////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////
 
 NetInterfaces::NetInterfaces(QObject *pParent) : QObject(pParent)
 {
 	setObjectName("NetInterfaces");
-	m_itfFilterAF = Interface::AF_Inet;
-	m_itfFilterMethod = Interface::IM_Static;
+	m_itfFilterAF		= Interface::AF_Unknown;
+	m_itfFilterMethod	= Interface::IM_Unknown;
+	m_itfFilterName.clear();
+	TRACE("%s\n", __FUNCTION__);
 }
 
 NetInterfaces::~NetInterfaces(void)
 {
 	reset();
+	TRACE("%s\n", __FUNCTION__);
 }
 
 void NetInterfaces::classBegin()
 {
+	TRACE("%s\n", __FUNCTION__);
+	if(!initialize())
+	{
+		TRACE("initialize failed!\n");
+		emitError("NetInterfaces::initialize failed!");
+	}
 }
 
 void NetInterfaces::componentComplete()
 {
-	if(!initialize())
-		emitError("NetInterfaces::initialize failed!");
+	TRACE("%s\n", __FUNCTION__);
 }
 
 void NetInterfaces::reset(void)
 {
 	Interface *pItf;
+	TRACE("%s\n", __FUNCTION__);
 	
     for(int i = 0; i < m_interfaces.count(); i++)
     {
@@ -50,13 +102,18 @@ bool NetInterfaces::initialize(void)
 {
 	bool bRet;
 	reset();
+	TRACE("%s\n", __FUNCTION__);
 
-	if((bRet = ::ParseEtcNetworkInterfaces(m_eni)))
+    if((bRet = ::ParseEtcNetworkInterfaces(m_eni)))
 	{
+		TRACE("ParseEtcNetworkInterfaces success.\n");
+
 		for(auto it = m_eni.ibl.begin(); it != m_eni.ibl.end(); it++)
 		{
 			ITF_IFACE_BLOCK &ibl = *it;
+			TRACE("  appending interface ...\n");
 			m_interfaces.append(new Interface(ibl, static_cast<const Emitter&>(*this), this));
+			TRACE("  interface appended.\n");
 		}
 
 		emit interfaces_Changed();
@@ -88,7 +145,7 @@ void NetInterfaces::emitError(const char *pszFormatStr, ...) const
     emit sigError(qs);
 }
 
-QVariantList NetInterfaces::getInterface(const QString &itfName)
+/*QVariantList NetInterfaces::getInterface(const QString &itfName)
 {
 	QVariantList list;
     list.clear();
@@ -111,17 +168,34 @@ QVariantList NetInterfaces::getInterface(const QString &itfName)
 	}
 
 	return list;
-}
+}*/
 
-QVariant NetInterfaces::newInterface(QString name, int af, int method, QString cfg)
+QVariant NetInterfaces::newInterface(QString name, int af, int method)
 {
+	if(name.isNull() || name.isEmpty())
+	{
+		emitError("Invalid or empty interface name!");
+		return QVariant();
+	}
+	
+	if(!_IsPowerOf2(af) || (af <= Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
+	{
+		emitError("Invalid address family: %d!", af);
+		return QVariant();
+	}
+
+	if(!_IsPowerOf2(method) || (method <= Interface::IM_Unknown) || (method >= Interface::IM_Invalid))
+	{
+		emitError("Invalid method: %d!", method);
+		return QVariant();
+	}
+
 	m_eni.ibl.emplace_back();
 	ITF_IFACE_BLOCK &rib = m_eni.ibl.back();
     rib.cfgName = name.toStdString();
-	rib.proto = (IfaceProtos)af;
-	rib.method = (IfaceMethods)method;
+	rib.proto = (IfaceProtos)_FLAG_TO_ENUM(af);
+	rib.method = (IfaceMethods)_FLAG_TO_ENUM(method);
 	Interface *pi = new Interface(rib, static_cast<const Emitter&>(*this), this);
-
 	m_interfaces.append(pi);
 	emit interfaces_Changed();
 	return QVariant::fromValue(pi);
@@ -141,9 +215,9 @@ QQmlListProperty<Interface> NetInterfaces::filteredInterfaces(void)
         Interface *pi = m_interfaces.at(i);
 		const ITF_IFACE_BLOCK &ibl = pi->getIface();
 
-		if(	m_itfFilterName == ibl.cfgName.c_str() &&
-			(int)ibl.proto == m_itfFilterAF &&
-			(int)ibl.method == m_itfFilterMethod)
+		if(	(m_itfFilterName.isNull() || m_itfFilterName.isEmpty() || (m_itfFilterName == ibl.cfgName.c_str())) &&
+			(_ENUM_TO_FLAG(ibl.proto) & m_itfFilterAF) &&
+			(_ENUM_TO_FLAG(ibl.method) & m_itfFilterMethod))
 		{
 			m_filteredInterfaces.append(pi);
 		}
@@ -165,6 +239,7 @@ void NetInterfaces::set_itfFilterName(const QString &val)
 		emit itfFilterName_Changed(m_itfFilterName);
 		emit filteredInterfaces_Changed();
 	}
+	TRACE("%s\n", __FUNCTION__);
 }
 
 int NetInterfaces::itfFilterAF(void) const
@@ -174,7 +249,9 @@ int NetInterfaces::itfFilterAF(void) const
 
 void NetInterfaces::set_itfFilterAF(int af)
 {
-	if(af <= Interface::AF_Unknown || af >= Interface::AF_Invalid)
+	TRACE("%s\n", __FUNCTION__);
+
+	if(af < Interface::AF_Unknown || af >= Interface::AF_Invalid)
 	{
 		emitError("Invalid address family filter: %d!", af);
 		return;
@@ -195,7 +272,9 @@ int NetInterfaces::itfFilterMethod(void) const
 
 void NetInterfaces::set_itfFilterMethod(int method)
 {
-	if(method <= Interface::IM_Unknown || method >= Interface::IM_Invalid)
+	TRACE("%s\n", __FUNCTION__);
+
+	if(method < Interface::IM_Unknown || method >= Interface::IM_Invalid)
 	{
 		emitError("Invalid method filter: %d!", method);
 		return;
@@ -226,21 +305,7 @@ Interface::Interface(ITF_IFACE_BLOCK &ifb, const Emitter &errHandler, QObject *p
 		IPv4Address *addr = new IPv4Address(m_ifb.inet4s.namesvr[i], errHandler, this);
 		m_dnsList.append(addr);
 	}
-#if 0
-	for(size_t i = 0; i < _countof(m_ifb.inet4s.namesvr); i++)
-	{
-		if(m_ifb.inet4s.namesvr[i].s_addr)
-		{
-			QString qs(inet_ntoa(m_ifb.inet4s.namesvr[i]));
-			m_dnsList.append(qs);
-		}
-		else
-		{
-			QString qs("");
-			m_dnsList.append(qs);
-		}
-	}
-#endif
+	TRACE("%s\n", __FUNCTION__);
 }
 
 Interface::~Interface(void)
@@ -252,6 +317,7 @@ Interface::~Interface(void)
         if((addr = m_dnsList.at(i)))
         	delete addr;
     }
+	TRACE("%s\n", __FUNCTION__);
 }
 
 QString Interface::name(void) const
@@ -259,7 +325,7 @@ QString Interface::name(void) const
 	return QString::fromStdString(m_ifb.cfgName);
 }
 
-QString Interface::family(void) const
+QString Interface::afName(void) const
 {
 	return ::GetIfaceProtoStr(m_ifb.proto);
 }
@@ -269,6 +335,22 @@ int Interface::af(void) const
 	return (int)m_ifb.proto;
 }
 
+void Interface::set_af(int af)
+{
+	if(!_IsPowerOf2(af) || (af < Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
+	{
+		m_errHandler.emitError("Invalid address family: %d!", af);
+		return;
+	}
+
+	if(m_ifb.proto != (IfaceProtos)_FLAG_TO_ENUM(af))
+	{
+		m_ifb.proto = (IfaceProtos)_FLAG_TO_ENUM(af);
+		emit af_Changed(af);
+		emit afName_Changed();
+	}
+}
+
 QString Interface::method(void) const
 {
 	return ::GetIfaceMethodStr(m_ifb.method);

+ 35 - 6
qtplugin/netinterfaces.h

@@ -66,8 +66,8 @@ class Interface : public QObject
 {
     Q_OBJECT
     Q_PROPERTY(QString name READ name CONSTANT)
-    Q_PROPERTY(QString family READ family)
-    Q_PROPERTY(int af READ af)
+    Q_PROPERTY(QString afName READ afName NOTIFY afName_Changed)
+    Q_PROPERTY(int af READ af WRITE set_af NOTIFY af_Changed)
     Q_PROPERTY(QString method READ method)
     Q_PROPERTY(int itfMethod READ itfMethod)
 	// static
@@ -88,18 +88,28 @@ public:
 
 	enum AddressFamily
 	{
+#if 0
 		AF_Unknown = -1,
 		AF_Inet,
 		AF_Inet6,
 		AF_Ipx,
 		AF_Can,
 		AF_Invalid
+#else
+		AF_Unknown	= 0,
+		AF_Inet		= 0x0001,
+		AF_Inet6	= 0x0002,
+		AF_Ipx		= 0x0004,
+		AF_Can		= 0x0008,
+		AF_Invalid	= 0x0010
+#endif
 	};
 	Q_ENUMS(AddressFamily)
 
 	enum ItfMethod
 	{
-		IM_Unknown = -1,
+#if 0
+		IM_Unknown	= -1,
 		IM_Static,
 		IM_Dhcp,
 		IM_Manual,
@@ -111,13 +121,28 @@ public:
 		IM_Loopback,
 		IM_Auto,
 		IM_Invalid
+#else
+		IM_Unknown	= 0,
+		IM_Static	= 0x0001,
+		IM_Dhcp		= 0x0002,
+		IM_Manual	= 0x0004,
+		IM_BootP	= 0x0008,
+		IM_Tunnel	= 0x0010,
+		IM_Ppp		= 0x0020,
+		IM_WvDial	= 0x0040,
+		IM_IpV4ll	= 0x0080,
+		IM_Loopback	= 0x0100,
+		IM_Auto		= 0x0200,
+		IM_Invalid	= 0x0400
+#endif
 	};
 	Q_ENUMS(ItfMethod)
 
 private:
     QString name(void) const;
-    QString family(void) const;
+    QString afName(void) const;
     int af(void) const;
+    void set_af(int af);
     QString method(void) const;
     int itfMethod(void) const;
     IPv4Address* ipAddress(void);
@@ -127,6 +152,10 @@ private:
     IPv4Address* ptpAddress(void);
     QQmlListProperty<IPv4Address> dnsServer(void);
 
+signals:
+	void afName_Changed(void);
+	void af_Changed(int af);
+
 private:
 	ITF_IFACE_BLOCK &m_ifb;
 	IPv4Address m_ipAddr;
@@ -159,8 +188,8 @@ public:
 	Q_INVOKABLE bool initialize(void);
 	Q_INVOKABLE bool save(void);
 	Q_INVOKABLE bool saveAs(const QString &path);
-	Q_INVOKABLE QVariantList getInterface(const QString &itfName);
-	Q_INVOKABLE QVariant newInterface(QString name, int af, int method, QString cfg);
+//	Q_INVOKABLE QVariantList getInterface(const QString &itfName);
+	Q_INVOKABLE QVariant newInterface(QString name, int af, int method);
 
 	virtual void classBegin();
 	virtual void componentComplete();

+ 4 - 4
qtplugin/pdump.sh

@@ -6,11 +6,11 @@ URI="gfa.plugins.qml.net"
 VERSION=1.0
 
 # Desktop:
-#OUTFILE="/home/wrk/Qt5.7.0/5.7/gcc_64/qml/gfa/plugins/qml/net/net.qmltypes"
-#IMPORTPATH="/home/wrk/share/gfanet/qtplugin/Debug/Desktop_Qt_5_7_0_GCC_64bit"
+OUTFILE="/home/wrk/Qt5.7.0/5.7/gcc_64/qml/gfa/plugins/qml/net/net.qmltypes"
+IMPORTPATH="/home/wrk/Qt5.7.0/5.7/gcc_64/qml/gfa/plugins/qml/net"
 
 # Target:
-OUTFILE="/opt/GfA/TC_L312_C493_QT57/usr/arm-buildroot-linux-gnueabihf/sysroot/usr/qml/gfa/plugins/qml/net/net.qmltypes"
-IMPORTPATH="/home/wrk/share/gfanet/qtplugin/Debug/GfA_Device"
+#OUTFILE="/opt/GfA/TC_L312_C493_QT57/usr/arm-buildroot-linux-gnueabihf/sysroot/usr/qml/gfa/plugins/qml/net/net.qmltypes"
+#IMPORTPATH="/opt/GfA/TC_L312_C493_QT57/usr/arm-buildroot-linux-gnueabihf/sysroot/usr/qml/gfa/plugins/qml/net"
 
 $PDUMP $URI $VERSION $IMPORTPATH > $OUTFILE