2023-10-30 20:03:38 +01:00
|
|
|
#include "io-device.hpp"
|
|
|
|
|
#include "orbis/KernelAllocator.hpp"
|
|
|
|
|
#include "orbis/file.hpp"
|
2023-10-31 19:28:40 +01:00
|
|
|
#include "orbis/uio.hpp"
|
2023-10-30 20:03:38 +01:00
|
|
|
#include "orbis/utils/Logs.hpp"
|
2023-10-31 19:28:40 +01:00
|
|
|
#include "orbis/utils/SharedMutex.hpp"
|
2023-10-31 12:22:22 +01:00
|
|
|
#include <chrono>
|
2023-10-31 19:28:40 +01:00
|
|
|
#include <cstddef>
|
|
|
|
|
#include <mutex>
|
2023-10-31 12:22:22 +01:00
|
|
|
#include <thread>
|
2023-10-30 20:03:38 +01:00
|
|
|
|
|
|
|
|
struct NotificationFile : orbis::File {};
|
|
|
|
|
struct NotificationDevice : IoDevice {
|
|
|
|
|
int index;
|
2023-10-31 19:28:40 +01:00
|
|
|
orbis::shared_mutex mutex;
|
|
|
|
|
orbis::kvector<std::byte> data;
|
2023-10-30 20:03:38 +01:00
|
|
|
|
|
|
|
|
NotificationDevice(int index) : index(index) {}
|
|
|
|
|
orbis::ErrorCode open(orbis::Ref<orbis::File> *file, const char *path,
|
|
|
|
|
std::uint32_t flags, std::uint32_t mode,
|
|
|
|
|
orbis::Thread *thread) override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static orbis::ErrorCode notification_ioctl(orbis::File *file, std::uint64_t request,
|
|
|
|
|
void *argp, orbis::Thread *thread) {
|
|
|
|
|
|
|
|
|
|
ORBIS_LOG_FATAL("Unhandled notification ioctl", request);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 12:22:22 +01:00
|
|
|
static orbis::ErrorCode notification_read(orbis::File *file, orbis::Uio *uio, orbis::Thread *thread) {
|
2023-10-31 19:28:40 +01:00
|
|
|
auto dev = dynamic_cast<NotificationDevice *>(file->device.get());
|
|
|
|
|
ORBIS_LOG_FATAL(__FUNCTION__, dev->index);
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
if (dev->data.empty()) {
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::lock_guard lock(dev->mutex);
|
|
|
|
|
|
|
|
|
|
if (dev->data.empty()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto vec : std::span(uio->iov, uio->iovcnt)) {
|
|
|
|
|
auto size = std::min<std::size_t>(dev->data.size(), vec.len);
|
|
|
|
|
uio->offset += size;
|
|
|
|
|
std::memcpy(vec.base, dev->data.data(), size);
|
|
|
|
|
|
|
|
|
|
if (dev->data.size() == size) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::memmove(dev->data.data(), dev->data.data() + size, dev->data.size() - size);
|
|
|
|
|
dev->data.resize(dev->data.size() - size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static orbis::ErrorCode notification_write(orbis::File *file, orbis::Uio *uio, orbis::Thread *thread) {
|
|
|
|
|
auto dev = dynamic_cast<NotificationDevice *>(file->device.get());
|
|
|
|
|
ORBIS_LOG_FATAL(__FUNCTION__, dev->index);
|
|
|
|
|
|
|
|
|
|
std::lock_guard lock(dev->mutex);
|
|
|
|
|
|
|
|
|
|
for (auto vec : std::span(uio->iov, uio->iovcnt)) {
|
|
|
|
|
auto offset = dev->data.size();
|
|
|
|
|
dev->data.resize(offset + vec.len);
|
|
|
|
|
std::memcpy(dev->data.data(), vec.base, vec.len);
|
|
|
|
|
}
|
|
|
|
|
uio->resid = 0;
|
2023-10-31 12:22:22 +01:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 20:03:38 +01:00
|
|
|
static const orbis::FileOps fileOps = {
|
|
|
|
|
.ioctl = notification_ioctl,
|
2023-10-31 12:22:22 +01:00
|
|
|
.read = notification_read,
|
2023-10-31 19:28:40 +01:00
|
|
|
.write = notification_write,
|
2023-10-30 20:03:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
orbis::ErrorCode NotificationDevice::open(orbis::Ref<orbis::File> *file, const char *path,
|
|
|
|
|
std::uint32_t flags, std::uint32_t mode,
|
|
|
|
|
orbis::Thread *thread) {
|
|
|
|
|
auto newFile = orbis::knew<NotificationFile>();
|
|
|
|
|
newFile->ops = &fileOps;
|
|
|
|
|
newFile->device = this;
|
|
|
|
|
|
|
|
|
|
*file = newFile;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IoDevice *createNotificationCharacterDevice(int index) { return orbis::knew<NotificationDevice>(index); }
|