88
99#include "libnw.h"
1010#include "utils.h"
11+ #include "devtree.h"
12+
13+ typedef struct _DEVTREE_CTX
14+ {
15+ CHAR * ids ;
16+ DWORD idsSize ;
17+ } DEVTREE_CTX ;
1118
1219static WCHAR * GetUsbDiskName (DEVINST usbDevInst )
1320{
@@ -77,82 +84,59 @@ ParseHwClass(PNODE nd, CHAR* ids, DWORD idsSize, LPCWSTR compId)
7784}
7885
7986static void
80- GetDeviceInfo (PNODE nusb , CHAR * ids , DWORD idsSize , DEVINST devInst , LPCWSTR instanceId )
87+ GetDeviceInfoUsb (PNODE node , void * data , DEVINST devInst , LPCWSTR instanceId )
8188{
82- NWL_NodeAttrSet (nusb , "HWID" , NWL_Ucs2ToUtf8 (instanceId ), 0 );
89+ DEVTREE_CTX * ctx = (DEVTREE_CTX * )data ;
90+ NWL_NodeAttrSet (node , "HWID" , NWL_Ucs2ToUtf8 (instanceId ), 0 );
8391
84- NWL_ParseHwid (nusb , ids , idsSize , instanceId , 1 );
92+ NWL_ParseHwid (node , ctx -> ids , ctx -> idsSize , instanceId , 1 );
8593
8694 // Parse hardware class if available
8795 WCHAR * compatibleIds = NWL_GetDevStrProp (devInst , & DEVPKEY_Device_CompatibleIds );
8896 if (compatibleIds )
8997 {
90- ParseHwClass (nusb , ids , idsSize , compatibleIds );
98+ ParseHwClass (node , ctx -> ids , ctx -> idsSize , compatibleIds );
9199 free (compatibleIds );
92100 }
93101
94102 // Get and print device name using DEVPKEY_NAME
95103 WCHAR * name = NWL_GetDevStrProp (devInst , & DEVPKEY_NAME );
96104 if (name )
97105 {
98- NWL_NodeAttrSet (nusb , "Name" , NWL_Ucs2ToUtf8 (name ), 0 );
106+ NWL_NodeAttrSet (node , "Name" , NWL_Ucs2ToUtf8 (name ), 0 );
99107 free (name );
100108 }
101109
102110 // Check if it's a Mass Storage Device and get disk name
103111 WCHAR * diskName = GetUsbDiskName (devInst );
104112 if (diskName )
105113 {
106- NWL_NodeAttrSet (nusb , "Disk" , NWL_Ucs2ToUtf8 (diskName ), 0 );
114+ NWL_NodeAttrSet (node , "Disk" , NWL_Ucs2ToUtf8 (diskName ), 0 );
107115 free (diskName );
108116 }
109117}
110118
111- static inline PNODE AppendUsbHub (PNODE parent )
112- {
113- if (parent -> Flags & NFLG_TABLE )
114- return parent ; // Already a table, return as is
115- PNODE ret = NWL_NodeGetChild (parent , "USB Hub" );
116- if (ret )
117- return ret ; // Found existing USB Hub node
118- // Create a new USB Hub node
119- return NWL_NodeAppendNew (parent , "USB Hub" , NFLG_TABLE );
120- }
121-
122- static void EnumerateUsbDevices (PNODE parent , CHAR * ids , DWORD idsSize , DEVINST devInst )
123- {
124- PNODE nusb = parent ;
125- DEVINST childInst ;
126- WCHAR * instanceId = NWL_GetDevStrProp (devInst , & DEVPKEY_Device_InstanceId );
127- if (instanceId )
128- {
129- if (wcsncmp (instanceId , L"USB\\" , 4 ) == 0 )
130- {
131- nusb = NWL_NodeAppendNew (AppendUsbHub (parent ), "Device" , NFLG_TABLE_ROW );
132- GetDeviceInfo (nusb , ids , idsSize , devInst , instanceId );
133- }
134- free (instanceId );
135- }
136-
137- if (CM_Get_Child (& childInst , devInst , 0 ) == CR_SUCCESS )
138- {
139- EnumerateUsbDevices (nusb , ids , idsSize , childInst );
140- DEVINST siblingInst = childInst ;
141- while (CM_Get_Sibling (& siblingInst , siblingInst , 0 ) == CR_SUCCESS )
142- EnumerateUsbDevices (nusb , ids , idsSize , siblingInst );
143- }
144- }
145-
146119PNODE NW_Usb (VOID )
147120{
148121 DEVINST devRoot ;
149122 CONFIGRET cr ;
150- CHAR * ids = NULL ;
151- DWORD idsSize = 0 ;
123+ DEVTREE_CTX data =
124+ {
125+ .ids = NULL ,
126+ .idsSize = 0 ,
127+ };
128+ DEVTREE_ENUM_CTX ctx =
129+ {
130+ .filter = L"USB\\" ,
131+ .filterLen = 4 , // Length of "USB\\"
132+ .data = & data ,
133+ .hub = "USB Hub" ,
134+ .GetDeviceInfo = GetDeviceInfoUsb ,
135+ };
152136 PNODE node = NWL_NodeAlloc ("USB" , NFLG_TABLE );
153137 if (NWLC -> UsbInfo )
154138 NWL_NodeAppendChild (NWLC -> NwRoot , node );
155- ids = NWL_LoadIdsToMemory (L"usb.ids" , & idsSize );
139+ data . ids = NWL_LoadIdsToMemory (L"usb.ids" , & data . idsSize );
156140
157141 cr = CM_Locate_DevNodeW (& devRoot , NULL , CM_LOCATE_DEVNODE_NORMAL );
158142 if (cr != CR_SUCCESS )
@@ -161,9 +145,9 @@ PNODE NW_Usb(VOID)
161145 goto fail ;
162146 }
163147
164- EnumerateUsbDevices (node , ids , idsSize , devRoot );
148+ NWL_EnumerateDevices (node , & ctx , devRoot );
165149
166150fail :
167- free (ids );
151+ free (data . ids );
168152 return node ;
169- }
153+ }
0 commit comments