microsoft/mu_feature_ffa
Publicmirrored from https://github.com/microsoft/mu_feature_ffaAvailable
FfaFeaturePkg/Library/TestServiceLibRust/src/test_svc.rs
78lines · modeblame
aa115865kuqin1211 months ago | 1 | use ec_service_lib::{Result, Service}; |
| 2 | use log::{debug, error}; | |
09d65a16Raymond-MS4 months ago | 3 | use odp_ffa::{DirectMessagePayload, HasRegisterPayload, MsgSendDirectReq2, MsgSendDirectResp2}; |
aa115865kuqin1211 months ago | 4 | use odp_ffa::{Function, NotificationSet}; |
| 5 | use uuid::{uuid, Uuid}; | |
| 6 | | |
| 7 | // Protocol CMD definitions for Test | |
33e335eekuqin1210 months ago | 8 | #[allow(dead_code)] |
aa115865kuqin1211 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 | | |
09d65a16Raymond-MS4 months ago | 20 | impl From<GenericRsp> for DirectMessagePayload { |
aa115865kuqin1211 months ago | 21 | fn from(value: GenericRsp) -> Self { |
09d65a16Raymond-MS4 months ago | 22 | DirectMessagePayload::from_iter(value.status.to_le_bytes()) |
aa115865kuqin1211 months ago | 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 | |
09d65a16Raymond-MS4 months ago | 36 | let payload = msg.payload(); |
| 37 | let sender_uuid = Uuid::from_u128_le(((payload.register_at(2) as u128) << 64) | (payload.register_at(1) as u128)); | |
| 38 | let cookie = payload.register_at(3); | |
aa115865kuqin1211 months ago | 39 | let flag = 1 << DELAYED_SRI_BIT_POS; |
| 40 | let bit_pos = 1 << cookie; | |
| 41 | | |
| 42 | debug!("notification_handler for {:?}", sender_uuid); | |
| 43 | | |
| 44 | // Set up notification through the Notify service | |
| 45 | // TODO; | |
| 46 | // let notify_msg = MsgSendDirectReq2::new( | |
| 47 | // Function::Notify, | |
| 48 | // 0) | |
42c9efa3Raymond-MS2 months ago | 49 | NotificationSet::new(msg.destination_id(), msg.source_id(), flag, bit_pos) |
aa115865kuqin1211 months ago | 50 | .exec() |
| 51 | .unwrap(); | |
| 52 | | |
| 53 | GenericRsp { | |
| 54 | status: 0x0, | |
| 55 | } | |
| 56 | } | |
| 57 | } | |
| 58 | | |
| 59 | impl Service for Test { | |
09d65a16Raymond-MS4 months ago | 60 | const UUID: Uuid = uuid!("e0fad9b3-7f5c-42c5-b2ee-b7a82313cdb2"); |
| 61 | const NAME: &'static str = "Test"; | |
aa115865kuqin1211 months ago | 62 | |
09d65a16Raymond-MS4 months ago | 63 | fn ffa_msg_send_direct_req2(&mut self, msg: MsgSendDirectReq2) -> Result<MsgSendDirectResp2> { |
| 64 | let payload = msg.payload(); | |
| 65 | let cmd = payload.u64_at(0); | |
aa115865kuqin1211 months ago | 66 | debug!("Received Test command 0x{:x}", cmd); |
| 67 | | |
| 68 | let payload = match cmd { | |
09d65a16Raymond-MS4 months ago | 69 | TEST_OPCODE_TEST_NOTIFICATION => DirectMessagePayload::from(self.notification_handler(&msg)), |
aa115865kuqin1211 months ago | 70 | _ => { |
| 71 | error!("Unknown Test Command: {}", cmd); | |
| 72 | return Err(odp_ffa::Error::Other("Unknown Test Command")); | |
| 73 | } | |
| 74 | }; | |
| 75 | | |
| 76 | Ok(MsgSendDirectResp2::from_req_with_payload(&msg, payload)) | |
| 77 | } | |
| 78 | } |