2024-03-22 03:58:59 -05:00
|
|
|
use {
|
|
|
|
crate::{
|
2024-03-22 05:13:17 -05:00
|
|
|
arch::hardware_random_u64,
|
2024-09-13 20:50:12 -05:00
|
|
|
holeybytes::{kernel_services::block_read, Vm},
|
2024-09-13 16:41:31 -05:00
|
|
|
ipc::{buffer::IpcBuffer, protocol::Protocol},
|
2024-08-19 13:13:58 -05:00
|
|
|
kmain::IPC_BUFFERS,
|
2024-03-22 03:58:59 -05:00
|
|
|
},
|
|
|
|
hashbrown::HashMap,
|
2024-08-12 08:21:31 -05:00
|
|
|
log::{info, trace},
|
2024-03-22 03:58:59 -05:00
|
|
|
spin::{lazy::Lazy, Mutex},
|
|
|
|
};
|
2024-09-13 16:41:31 -05:00
|
|
|
pub struct Services<'a>(HashMap<u64, Protocol<'a>>);
|
2024-03-22 03:58:59 -05:00
|
|
|
pub static SERVICES: Lazy<Mutex<Services>> = Lazy::new(|| {
|
|
|
|
let mut dt = Services(HashMap::new());
|
2024-03-22 05:13:17 -05:00
|
|
|
dt.0.insert(0, Protocol::void());
|
2024-03-22 03:58:59 -05:00
|
|
|
Mutex::new(dt)
|
|
|
|
});
|
2024-09-14 05:26:32 -05:00
|
|
|
#[derive(Debug)]
|
2024-09-13 20:50:12 -05:00
|
|
|
pub enum ServiceError {
|
|
|
|
InvalidFormat,
|
|
|
|
}
|
|
|
|
pub fn sds_msg_handler(vm: &mut Vm, mem_addr: u64, length: usize) -> Result<(), ServiceError> {
|
2024-09-13 16:41:31 -05:00
|
|
|
let msg_vec = block_read(mem_addr, length);
|
2024-03-22 05:13:17 -05:00
|
|
|
let sds_event_type: ServiceEventType = msg_vec[0].into();
|
|
|
|
|
2024-08-19 13:13:58 -05:00
|
|
|
// info!("Length {}", msg_vec.len());
|
2024-08-12 08:15:50 -05:00
|
|
|
|
2024-03-22 05:13:17 -05:00
|
|
|
use ServiceEventType::*;
|
|
|
|
match sds_event_type {
|
|
|
|
CreateService => {
|
2024-09-13 16:41:31 -05:00
|
|
|
let string =
|
|
|
|
core::str::from_utf8(&msg_vec[1..]).expect("Our bytes should be valid utf8");
|
2024-08-12 08:15:50 -05:00
|
|
|
let ret = sds_create_service(string);
|
|
|
|
vm.registers[1] = hbvm::value::Value(ret as u64);
|
2024-03-22 05:13:17 -05:00
|
|
|
}
|
|
|
|
DeleteService => todo!(),
|
2024-08-12 08:15:50 -05:00
|
|
|
SearchServices => {
|
2024-09-13 16:41:31 -05:00
|
|
|
let string =
|
|
|
|
core::str::from_utf8(&msg_vec[1..]).expect("Our bytes should be valid utf8");
|
2024-08-12 08:15:50 -05:00
|
|
|
let ret = sds_search_service(string);
|
|
|
|
vm.registers[1] = hbvm::value::Value(ret as u64);
|
|
|
|
}
|
2024-03-22 05:13:17 -05:00
|
|
|
}
|
|
|
|
// 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);
|
2024-03-22 03:58:59 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-03-22 05:13:17 -05:00
|
|
|
|
|
|
|
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,
|
2024-08-19 13:13:58 -05:00
|
|
|
10 => {
|
|
|
|
info!("TEST");
|
|
|
|
panic!()
|
|
|
|
}
|
2024-03-22 05:13:17 -05:00
|
|
|
1_u8 | 4_u8..=u8::MAX => todo!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-13 16:41:31 -05:00
|
|
|
fn sds_create_service(protocol: &'static str) -> u64 {
|
2024-03-22 05:13:17 -05:00
|
|
|
let buff_id = hardware_random_u64();
|
|
|
|
let mut services = SERVICES.lock();
|
2024-08-19 13:13:58 -05:00
|
|
|
let mut buffers = IPC_BUFFERS.lock();
|
2024-08-12 08:15:50 -05:00
|
|
|
|
2024-09-13 16:41:31 -05:00
|
|
|
let protocol_ = Protocol::from(protocol);
|
2024-08-19 13:13:58 -05:00
|
|
|
let mut buff = IpcBuffer::new(false, 0);
|
|
|
|
|
|
|
|
services.0.insert(buff_id, protocol_.clone());
|
2024-09-13 16:41:31 -05:00
|
|
|
buff.protocol = protocol_;
|
2024-08-19 13:13:58 -05:00
|
|
|
buffers.insert(buff_id, buff);
|
2024-08-12 08:15:50 -05:00
|
|
|
|
|
|
|
trace!("BufferID({}) => {}", buff_id, protocol);
|
|
|
|
// let a: protocol::Protocol = protocol.into();
|
2024-03-22 05:13:17 -05:00
|
|
|
buff_id
|
|
|
|
}
|
2024-08-12 08:15:50 -05:00
|
|
|
|
2024-09-13 16:41:31 -05:00
|
|
|
fn sds_search_service(protocol: &str) -> u64 {
|
|
|
|
let services = SERVICES.lock();
|
|
|
|
let compare = Protocol::from(protocol);
|
2024-08-12 08:15:50 -05:00
|
|
|
for (bid, protocol_canidate) in &services.0 {
|
2024-08-19 13:13:58 -05:00
|
|
|
trace!("BID-{bid} protocol_canidate {:?}", protocol_canidate);
|
2024-08-12 08:15:50 -05:00
|
|
|
if protocol_canidate == &compare {
|
2024-09-13 16:41:31 -05:00
|
|
|
trace!("BufferID({}) => {}", bid, protocol);
|
2024-08-12 08:15:50 -05:00
|
|
|
return *bid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
0
|
|
|
|
}
|