implement device probe in C

It's an attempt to build auto-completion for the dive-computers
based on unpublished code inside libdivecomputer[1]

[1] -
http://git.libdivecomputer.org/?p=libdivecomputer.git;a=commitdiff;h=d44053a99435fb9fc1f408fb3f1629a54c938afc

Signed-off-by: Danilo Cesar Lemes de Paula <danilo.eu@gmail.com>
This commit is contained in:
Danilo Cesar Lemes de Paula 2013-09-16 18:04:42 -03:00
parent f5b33dede3
commit a8d33f80b0
7 changed files with 171 additions and 5 deletions

View file

@ -333,3 +333,56 @@ gboolean subsurface_os_feature_available(os_feature_t f)
return TRUE;
}
}
int enumerate_devices (device_callback_t callback, void *userdata)
{
// Open the registry key.
HKEY hKey;
int index = -1;
LONG rc = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
if (rc != ERROR_SUCCESS) {
return -1;
}
// Get the number of values.
DWORD count = 0;
rc = RegQueryInfoKey (hKey, NULL, NULL, NULL, NULL, NULL, NULL, &count, NULL, NULL, NULL, NULL);
if (rc != ERROR_SUCCESS) {
RegCloseKey(hKey);
return -1;
}
for (DWORD i = 0; i < count; ++i) {
// Get the value name, data and type.
char name[512], data[512];
DWORD name_len = sizeof (name);
DWORD data_len = sizeof (data);
DWORD type = 0;
rc = RegEnumValue (hKey, i, name, &name_len, NULL, &type, (LPBYTE) data, &data_len);
if (rc != ERROR_SUCCESS) {
RegCloseKey(hKey);
return -1;
}
// Ignore non-string values.
if (type != REG_SZ)
continue;
// Prevent a possible buffer overflow.
if (data_len >= sizeof (data)) {
RegCloseKey(hKey);
return -1;
}
// Null terminate the string.
data[data_len] = 0;
callback (data, userdata);
index++;
if (is_default_dive_computer_device(filename))
index = i;
}
RegCloseKey(hKey);
return index;
}