58 lines
1.3 KiB
C
58 lines
1.3 KiB
C
#include "device.h"
|
|
#include "str.h"
|
|
|
|
i32 vm_register_device(VM *vm, const char *path, const char *type, void *data,
|
|
DeviceOps *ops) {
|
|
Device *dev;
|
|
|
|
if (vm->dc >= DEVICES_SIZE)
|
|
return -1;
|
|
|
|
dev = &vm->devices[vm->dc];
|
|
dev->handle = vm->dc++;
|
|
strcopy(dev->path, path, DEVICE_PATH_MAX_LENGTH);
|
|
dev->path[DEVICE_PATH_MAX_LENGTH - 1] = '\0';
|
|
|
|
strcopy(dev->type, type, DEVICE_TYPE_MAX_LENGTH);
|
|
dev->type[DEVICE_TYPE_MAX_LENGTH - 1] = '\0';
|
|
|
|
dev->data = data;
|
|
dev->ops = ops;
|
|
dev->flags = 0;
|
|
return dev->handle;
|
|
}
|
|
|
|
/* Find device by path */
|
|
Device *find_device_by_path(VM *vm, const char *path) {
|
|
u32 i;
|
|
for (i = 0; i < vm->dc; i++) {
|
|
if (streq(vm->devices[i].path, path)) {
|
|
return &vm->devices[i];
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* Find device by type (useful for checking capabilities) */
|
|
Device *find_device_by_type(VM *vm, const char *type) {
|
|
u32 i;
|
|
for (i = 0; i < vm->dc; i++) {
|
|
if (streq(vm->devices[i].type, type)) {
|
|
return &vm->devices[i];
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* Find all devices of a type */
|
|
i32 find_devices_by_type(VM *vm, const char *type, Device **results,
|
|
u32 max_results) {
|
|
u32 i, count = 0;
|
|
for (i = 0; i < vm->dc && count < max_results; i++) {
|
|
if (streq(vm->devices[i].type, type)) {
|
|
results[count++] = &vm->devices[i];
|
|
}
|
|
}
|
|
return count;
|
|
}
|