akern-gkgoat-fork/kernel/src/holeybytes/kernel_services/service_definition_service.rs

75 lines
2.2 KiB
Rust

use {
crate::{
alloc::string::ToString,
arch::hardware_random_u64,
holeybytes::{ecah::LogError, kernel_services::block_read, Vm},
ipc::{protocol, protocol::Protocol},
},
alloc::string::String,
hashbrown::HashMap,
log::info,
spin::{lazy::Lazy, Mutex},
};
pub struct Services(HashMap<u64, Protocol>);
pub static SERVICES: Lazy<Mutex<Services>> = Lazy::new(|| {
let mut dt = Services(HashMap::new());
dt.0.insert(0, Protocol::void());
Mutex::new(dt)
});
pub fn sds_msg_handler(vm: &mut Vm, mem_addr: u64, length: usize) -> Result<(), LogError> {
let mut msg_vec = block_read(mem_addr, length);
let sds_event_type: ServiceEventType = msg_vec[0].into();
msg_vec.remove(0);
use ServiceEventType::*;
match sds_event_type {
CreateService => {
let string = String::from_utf8(msg_vec).expect("Our bytes should be valid utf8");
sds_create_service(string);
}
DeleteService => todo!(),
SearchServices => todo!(),
}
// let buffer_id_raw = &msg_vec[0..8];
// let mut arr = [0u8; 8];
// arr.copy_from_slice(&buffer_id_raw);
// let buffer_id = u64::from_le_bytes(arr);
// info!("BufferID({:x?})", buffer_id);
// let mut services = SERVICES.lock();
// let string = String::from_utf8(msg_vec).expect("Our bytes should be valid utf8");
// use core::borrow::BorrowMut;
// services.borrow_mut().0.insert(buffer_id, string);
Ok(())
}
enum ServiceEventType {
CreateService = 0,
// UpdateService = 1,
DeleteService = 2,
SearchServices = 3,
}
impl From<u8> for ServiceEventType {
fn from(value: u8) -> Self {
use self::*;
match value {
0 => Self::CreateService,
// 1 =>
2 => Self::DeleteService,
3 => Self::SearchServices,
1_u8 | 4_u8..=u8::MAX => todo!(),
}
}
}
fn sds_create_service(protocol: String) -> u64 {
let buff_id = hardware_random_u64();
let mut services = SERVICES.lock();
services.0.insert(buff_id, protocol.clone().into());
info!("BufferID({}) => {}", buff_id, protocol);
let a: protocol::Protocol = protocol.into();
buff_id
}