microsoft/mu_feature_ffa
Publicmirrored from https://github.com/microsoft/mu_feature_ffaAvailable
FfaFeaturePkg/Library/TestServiceLibRust/src/test_svc.rs
83lines · modeblame
aa115865kuqin1210 months ago | 1 | use ec_service_lib::{Result, Service}; |
| 2 | use log::{debug, error}; | |
| 3 | use odp_ffa::{MsgSendDirectReq2, MsgSendDirectResp2, Payload, RegisterPayload}; | |
| 4 | use odp_ffa::{Function, NotificationSet}; | |
| 5 | use uuid::{uuid, Uuid}; | |
| 6 | | |
| 7 | // Protocol CMD definitions for Test | |
33e335eekuqin129 months ago | 8 | #[allow(dead_code)] |
aa115865kuqin1210 months ago | 9 | const TEST_OPCODE_BASE: u64 = 0xDEF0; |
| 10 | const TEST_OPCODE_TEST_NOTIFICATION: u64 = 0xDEF1; | |
| 11 | | |
| 12 | /* Test Service Defines */ | |
| 13 | const DELAYED_SRI_BIT_POS: u64 = 1; | |
| 14 | | |
| 15 | #[derive(Default)] | |
| 16 | struct GenericRsp { | |
| 17 | status: i64, | |
| 18 | } | |
| 19 | | |
| 20 | impl From<GenericRsp> for RegisterPayload { | |
| 21 | fn from(value: GenericRsp) -> Self { | |
| 22 | RegisterPayload::from_iter(value.status.to_le_bytes()) | |
| 23 | } | |
| 24 | } | |
| 25 | | |
| 26 | #[derive(Default)] | |
| 27 | pub struct Test {} | |
| 28 | | |
| 29 | impl Test { | |
| 30 | pub fn new() -> Self { | |
| 31 | Self::default() | |
| 32 | } | |
| 33 | | |
| 34 | fn notification_handler(&self, msg: &MsgSendDirectReq2) -> GenericRsp { | |
| 35 | // Grab the uuid from the message, they will be at x5 and x6 | |
| 36 | let sender_uuid = Uuid::from_u128_le(((msg.register_at(2) as u128) << 64) | (msg.register_at(1) as u128)); | |
| 37 | let cookie = msg.register_at(3); | |
| 38 | let flag = 1 << DELAYED_SRI_BIT_POS; | |
| 39 | let bit_pos = 1 << cookie; | |
| 40 | | |
| 41 | debug!("notification_handler for {:?}", sender_uuid); | |
| 42 | | |
| 43 | // Set up notification through the Notify service | |
| 44 | // TODO; | |
| 45 | // let notify_msg = MsgSendDirectReq2::new( | |
| 46 | // Function::Notify, | |
| 47 | // 0) | |
| 48 | NotificationSet::new(msg.source_id(), msg.destination_id(), flag, bit_pos) | |
| 49 | .exec() | |
| 50 | .unwrap(); | |
| 51 | | |
| 52 | GenericRsp { | |
| 53 | status: 0x0, | |
| 54 | } | |
| 55 | } | |
| 56 | } | |
| 57 | | |
| 58 | const UUID: Uuid = uuid!("e0fad9b3-7f5c-42c5-b2ee-b7a82313cdb2"); | |
| 59 | | |
| 60 | impl Service for Test { | |
| 61 | fn service_name(&self) -> &'static str { | |
| 62 | "Test" | |
| 63 | } | |
| 64 | | |
| 65 | fn service_uuid(&self) -> Uuid { | |
| 66 | UUID | |
| 67 | } | |
| 68 | | |
| 69 | async fn ffa_msg_send_direct_req2(&mut self, msg: MsgSendDirectReq2) -> Result<MsgSendDirectResp2> { | |
| 70 | let cmd = msg.u64_at(0); | |
| 71 | debug!("Received Test command 0x{:x}", cmd); | |
| 72 | | |
| 73 | let payload = match cmd { | |
| 74 | TEST_OPCODE_TEST_NOTIFICATION => RegisterPayload::from(self.notification_handler(&msg)), | |
| 75 | _ => { | |
| 76 | error!("Unknown Test Command: {}", cmd); | |
| 77 | return Err(odp_ffa::Error::Other("Unknown Test Command")); | |
| 78 | } | |
| 79 | }; | |
| 80 | | |
| 81 | Ok(MsgSendDirectResp2::from_req_with_payload(&msg, payload)) | |
| 82 | } | |
| 83 | } |