2 * PROJECT: Ports installer library
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll\win32\msports\comdb.c
5 * PURPOSE: COM port database
6 * COPYRIGHT: Copyright 2011 Eric Kohl
12 #include <wine/debug.h>
14 WINE_DEFAULT_DEBUG_CHANNEL(msports
);
16 #define BITS_PER_BYTE 8
26 ComDBClaimNextFreePort(IN HCOMDB hComDB
,
27 OUT LPDWORD ComNumber
)
38 TRACE("ComDBClaimNextFreePort(%p %p)\n", hComDB
, ComNumber
);
40 if (hComDB
== INVALID_HANDLE_VALUE
||
43 return ERROR_INVALID_PARAMETER
;
45 pComDB
= (PCOMDB
)hComDB
;
47 /* Get the required bitmap size */
48 lError
= RegQueryValueExW(pComDB
->hKey
,
54 if (lError
!= ERROR_SUCCESS
)
57 /* Allocate the bitmap */
58 pBitmap
= HeapAlloc(GetProcessHeap(),
63 ERR("Failed to allocate the bitmap!\n");
68 lError
= RegQueryValueExW(pComDB
->hKey
,
74 if (lError
!= ERROR_SUCCESS
)
77 lError
= ERROR_INVALID_PARAMETER
;
78 for (dwBitIndex
= 0; dwBitIndex
< (dwSize
* BITS_PER_BYTE
); dwBitIndex
++)
80 /* Calculate the byte index and a mask for the affected bit */
81 dwByteIndex
= dwBitIndex
/ BITS_PER_BYTE
;
82 cMask
= 1 << (dwBitIndex
% BITS_PER_BYTE
);
84 if ((pBitmap
[dwByteIndex
] & cMask
) == 0)
86 pBitmap
[dwByteIndex
] |= cMask
;
87 *ComNumber
= dwBitIndex
+ 1;
88 lError
= ERROR_SUCCESS
;
93 /* Save the bitmap if it was modified */
94 if (lError
== ERROR_SUCCESS
)
96 lError
= RegSetValueExW(pComDB
->hKey
,
106 HeapFree(GetProcessHeap(), 0, pBitmap
);
114 ComDBClaimPort(IN HCOMDB hComDB
,
124 PBYTE pBitmap
= NULL
;
128 TRACE("ComDBClaimPort(%p %lu)\n", hComDB
, ComNumber
);
130 if (hComDB
== INVALID_HANDLE_VALUE
||
133 ComNumber
> COMDB_MAX_PORTS_ARBITRATED
)
134 return ERROR_INVALID_PARAMETER
;
136 pComDB
= (PCOMDB
)hComDB
;
138 /* Get the required bitmap size */
139 lError
= RegQueryValueExW(pComDB
->hKey
,
145 if (lError
!= ERROR_SUCCESS
)
148 /* Allocate the bitmap */
149 pBitmap
= HeapAlloc(GetProcessHeap(),
154 ERR("Failed to allocate the bitmap!\n");
158 /* Read the bitmap */
159 lError
= RegQueryValueExW(pComDB
->hKey
,
165 if (lError
!= ERROR_SUCCESS
)
168 /* Get the bit index */
169 dwBitIndex
= ComNumber
- 1;
171 /* Check if the bit to set fits into the bitmap */
172 if (dwBitIndex
>= (dwSize
* BITS_PER_BYTE
))
174 FIXME("Resize the bitmap\n");
176 lError
= ERROR_INVALID_PARAMETER
;
180 /* Calculate the byte index and a mask for the affected bit */
181 dwByteIndex
= dwBitIndex
/ BITS_PER_BYTE
;
182 cMask
= 1 << (dwBitIndex
% BITS_PER_BYTE
);
184 lError
= ERROR_SHARING_VIOLATION
;
186 /* Check if the bit is not set */
187 if ((pBitmap
[dwByteIndex
] & cMask
) == 0)
190 pBitmap
[dwByteIndex
] |= cMask
;
191 lError
= ERROR_SUCCESS
;
194 /* Save the bitmap if it was modified */
195 if (lError
== ERROR_SUCCESS
)
197 lError
= RegSetValueExW(pComDB
->hKey
,
207 HeapFree(GetProcessHeap(), 0, pBitmap
);
215 ComDBClose(IN HCOMDB hComDB
)
219 TRACE("ComDBClose(%p)\n", hComDB
);
221 if (hComDB
== HCOMDB_INVALID_HANDLE_VALUE
|| hComDB
== NULL
)
222 return ERROR_INVALID_PARAMETER
;
224 pComDB
= (PCOMDB
)hComDB
;
226 /* Close the registry key */
227 if (pComDB
->hKey
!= NULL
)
228 RegCloseKey(pComDB
->hKey
);
230 /* Release the database */
231 HeapFree(GetProcessHeap(), 0, pComDB
);
233 return ERROR_SUCCESS
;
239 ComDBGetCurrentPortUsage(IN HCOMDB hComDB
,
243 OUT LPDWORD MaxPortsReported
)
245 FIXME("ComDBGetCurrentPortUsage(%p)\n", hComDB
);
246 return ERROR_CALL_NOT_IMPLEMENTED
;
252 ComDBOpen(OUT HCOMDB
*phComDB
)
261 TRACE("ComDBOpen(%p)\n", phComDB
);
263 /* Allocate a new database */
264 pComDB
= HeapAlloc(GetProcessHeap(),
269 ERR("Failed to allocate the database!\n");
270 return ERROR_ACCESS_DENIED
;
273 /* Create or open the database key */
274 lError
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
275 L
"System\\CurrentControlSet\\Control\\COM Name Arbiter",
283 if (lError
!= ERROR_SUCCESS
)
286 /* Get the required bitmap size */
287 lError
= RegQueryValueExW(pComDB
->hKey
,
293 if (lError
== ERROR_FILE_NOT_FOUND
)
295 /* Allocate a new bitmap */
296 dwSize
= COMDB_MIN_PORTS_ARBITRATED
/ BITS_PER_BYTE
;
297 pBitmap
= HeapAlloc(GetProcessHeap(),
302 ERR("Failed to allocate the bitmap!\n");
303 lError
= ERROR_ACCESS_DENIED
;
307 /* Write the bitmap to the registry */
308 lError
= RegSetValueExW(pComDB
->hKey
,
315 HeapFree(GetProcessHeap(), 0, pBitmap
);
319 if (lError
!= ERROR_SUCCESS
)
321 /* Clean up in case of failure */
322 if (pComDB
->hKey
!= NULL
)
323 RegCloseKey(pComDB
->hKey
);
325 HeapFree(GetProcessHeap(), 0, pComDB
);
327 *phComDB
= HCOMDB_INVALID_HANDLE_VALUE
;
331 /* Return the database handle */
332 *phComDB
= (HCOMDB
)pComDB
;
335 TRACE("done (Error %lu)\n", lError
);
343 ComDBReleasePort(IN HCOMDB hComDB
,
351 PBYTE pBitmap
= NULL
;
355 TRACE("ComDBReleasePort(%p %lu)\n", hComDB
, ComNumber
);
357 if (hComDB
== INVALID_HANDLE_VALUE
||
359 ComNumber
> COMDB_MAX_PORTS_ARBITRATED
)
360 return ERROR_INVALID_PARAMETER
;
362 pComDB
= (PCOMDB
)hComDB
;
364 /* Get the required bitmap size */
365 lError
= RegQueryValueExW(pComDB
->hKey
,
371 if (lError
!= ERROR_SUCCESS
)
374 /* Allocate the bitmap */
375 pBitmap
= HeapAlloc(GetProcessHeap(),
380 ERR("Failed to allocate the bitmap!\n");
384 /* Read the bitmap */
385 lError
= RegQueryValueExW(pComDB
->hKey
,
391 if (lError
!= ERROR_SUCCESS
)
394 /* Get the bit index */
395 dwBitIndex
= ComNumber
- 1;
397 /* Check if the bit to set fits into the bitmap */
398 if (dwBitIndex
>= (dwSize
* BITS_PER_BYTE
))
400 lError
= ERROR_INVALID_PARAMETER
;
404 /* Calculate the byte index and a mask for the affected bit */
405 dwByteIndex
= dwBitIndex
/ BITS_PER_BYTE
;
406 cMask
= 1 << (dwBitIndex
% BITS_PER_BYTE
);
408 /* Release the port */
409 pBitmap
[dwByteIndex
] &= ~cMask
;
411 lError
= RegSetValueExW(pComDB
->hKey
,
420 HeapFree(GetProcessHeap(), 0, pBitmap
);
428 ComDBResizeDatabase(IN HCOMDB hComDB
,
431 FIXME("ComDBResizeDatabase(%p %lu)\n", hComDB
, NewSize
);
432 return ERROR_CALL_NOT_IMPLEMENTED
;