|
| 1 | +use core::future::Future; |
| 2 | + |
| 3 | +use binary_serde::{BinarySerde, Endianness}; |
| 4 | +use embedded_cfu_protocol::components::CfuComponentTraits; |
| 5 | +use embedded_cfu_protocol::host::{CfuHostStates, CfuUpdater}; |
| 6 | +use embedded_cfu_protocol::protocol_definitions::*; |
| 7 | +use embedded_cfu_protocol::{CfuImage, CfuWriter, CfuWriterDefault, CfuWriterError}; |
| 8 | +use heapless::Vec; |
| 9 | + |
| 10 | +use crate::CfuError; |
| 11 | + |
| 12 | +/// All host side Cfu traits, in some cases this will originate from a OS driver for CFU |
| 13 | +pub trait CfuHost: CfuHostStates { |
| 14 | + /// Get all images |
| 15 | + fn get_cfu_images<I: CfuImage>(&self) -> impl Future<Output = Result<Vec<I, MAX_CMPT_COUNT>, CfuError>>; |
| 16 | + /// Gets the firmware version of all components |
| 17 | + fn get_all_fw_versions<T: CfuWriter>( |
| 18 | + self, |
| 19 | + writer: &mut T, |
| 20 | + primary_cmpt: ComponentId, |
| 21 | + ) -> impl Future<Output = Result<GetFwVersionResponse, CfuError>>; |
| 22 | + /// Goes through the offer list and returns a slice of offer responses |
| 23 | + fn process_cfu_offers<'a, T: CfuWriter>( |
| 24 | + offer_commands: &'a [FwUpdateOfferCommand], |
| 25 | + writer: &mut T, |
| 26 | + ) -> impl Future<Output = Result<&'a [FwUpdateOfferResponse], CfuError>>; |
| 27 | + /// For a specific component, update its content |
| 28 | + fn update_cfu_content<T: CfuWriter>( |
| 29 | + writer: &mut T, |
| 30 | + ) -> impl Future<Output = Result<FwUpdateContentResponse, CfuError>>; |
| 31 | + /// For a specific image that was updated, validate its content |
| 32 | + fn is_cfu_image_valid<I: CfuImage>(image: I) -> impl Future<Output = Result<bool, CfuError>>; |
| 33 | +} |
| 34 | + |
| 35 | +pub struct CfuHostInstance<I: CfuImage, C: CfuComponentTraits> { |
| 36 | + pub updater: CfuUpdater, |
| 37 | + pub images: heapless::Vec<I, MAX_CMPT_COUNT>, |
| 38 | + pub writer: CfuWriterDefault, |
| 39 | + pub primary_cmpt: C, |
| 40 | + pub host_token: HostToken, |
| 41 | +} |
| 42 | + |
| 43 | +impl<I: CfuImage, C: CfuComponentTraits> CfuHostInstance<I, C> { |
| 44 | + #[allow(unused)] |
| 45 | + fn new(primary_cmpt: C) -> Self { |
| 46 | + Self { |
| 47 | + updater: CfuUpdater {}, |
| 48 | + images: Vec::new(), |
| 49 | + writer: CfuWriterDefault::default(), |
| 50 | + primary_cmpt, |
| 51 | + host_token: 0, |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl<I: CfuImage, C: CfuComponentTraits> CfuHostStates for CfuHostInstance<I, C> { |
| 57 | + async fn start_transaction<T: CfuWriter>(self, _writer: &mut T) -> Result<FwUpdateOfferResponse, CfuProtocolError> { |
| 58 | + let component_id = self.primary_cmpt.get_component_id(); |
| 59 | + let _mock_cmd = FwUpdateOfferCommand::new_with_command( |
| 60 | + self.host_token, |
| 61 | + component_id, |
| 62 | + FwVersion::default(), |
| 63 | + 0, |
| 64 | + InformationCodeValues::StartOfferList, |
| 65 | + 0, |
| 66 | + ); |
| 67 | + let mockresponse = FwUpdateOfferResponse::default(); |
| 68 | + Ok(mockresponse) |
| 69 | + } |
| 70 | + async fn notify_start_offer_list<T: CfuWriter>( |
| 71 | + self, |
| 72 | + writer: &mut T, |
| 73 | + ) -> Result<FwUpdateOfferResponse, CfuProtocolError> { |
| 74 | + // Serialize FwUpdateOfferCommand to bytes, pull out componentid, host token |
| 75 | + let component_id = self.primary_cmpt.get_component_id(); |
| 76 | + let mock_cmd = FwUpdateOfferCommand::new_with_command( |
| 77 | + self.host_token, |
| 78 | + component_id, |
| 79 | + FwVersion::default(), |
| 80 | + 0, |
| 81 | + InformationCodeValues::StartOfferList, |
| 82 | + 0, |
| 83 | + ); |
| 84 | + let mut serialized_mock = [0u8; FwUpdateOfferCommand::SERIALIZED_SIZE]; |
| 85 | + FwUpdateOfferCommand::binary_serialize(&mock_cmd, &mut serialized_mock, Endianness::Little); |
| 86 | + let mut read = [0u8; FwUpdateOfferResponse::SERIALIZED_SIZE]; |
| 87 | + //self.primary_cmpt.me.writer.write_read_to_component(Some(component_id), &serialized_mock, &mut read).await; |
| 88 | + if let Ok(_result) = writer.cfu_write_read(None, &serialized_mock, &mut read).await { |
| 89 | + if let Ok(converted) = FwUpdateOfferResponse::binary_deserialize(&read, Endianness::Little) { |
| 90 | + if converted.status != CfuOfferStatus::Accept { |
| 91 | + Err(CfuProtocolError::CfuStatusError(converted.status)) |
| 92 | + } else { |
| 93 | + Ok(converted) |
| 94 | + } |
| 95 | + } else { |
| 96 | + Err(CfuProtocolError::WriterError(CfuWriterError::ByteConversionError)) |
| 97 | + } |
| 98 | + } else { |
| 99 | + Err(CfuProtocolError::WriterError(CfuWriterError::StorageError)) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + async fn notify_end_offer_list<T: CfuWriter>( |
| 104 | + self, |
| 105 | + writer: &mut T, |
| 106 | + ) -> Result<FwUpdateOfferResponse, CfuProtocolError> { |
| 107 | + let component_id = self.primary_cmpt.get_component_id(); |
| 108 | + let mock_cmd = FwUpdateOfferCommand::new_with_command( |
| 109 | + self.host_token, |
| 110 | + component_id, |
| 111 | + FwVersion::default(), |
| 112 | + 0, |
| 113 | + InformationCodeValues::EndOfferList, |
| 114 | + 0, |
| 115 | + ); |
| 116 | + let mut serialized_mock = [0u8; FwUpdateOfferCommand::SERIALIZED_SIZE]; |
| 117 | + FwUpdateOfferCommand::binary_serialize(&mock_cmd, &mut serialized_mock, Endianness::Little); |
| 118 | + let mut read = [0u8; FwUpdateOfferResponse::SERIALIZED_SIZE]; |
| 119 | + if writer.cfu_write_read(None, &serialized_mock, &mut read).await.is_ok() { |
| 120 | + // convert back to FwUpdateOfferResponse |
| 121 | + if let Ok(converted) = FwUpdateOfferResponse::binary_deserialize(&read, Endianness::Little) { |
| 122 | + Ok(converted) |
| 123 | + } else { |
| 124 | + // error deserializing the bytes that were read |
| 125 | + Err(CfuProtocolError::WriterError(CfuWriterError::ByteConversionError)) |
| 126 | + } |
| 127 | + } else { |
| 128 | + // unsuccessful write/read from the storage interface |
| 129 | + // use result.err() eventually |
| 130 | + Err(CfuProtocolError::WriterError(CfuWriterError::StorageError)) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + async fn verify_all_updates_completed(resps: &[FwUpdateOfferResponse]) -> Result<bool, CfuProtocolError> { |
| 135 | + let mut bad_components: heapless::Vec<u8, MAX_CMPT_COUNT> = Vec::new(); |
| 136 | + for (i, r) in resps.iter().enumerate() { |
| 137 | + if r.status != CfuOfferStatus::Reject { |
| 138 | + let _ = bad_components.push(i as u8); |
| 139 | + } |
| 140 | + } |
| 141 | + if bad_components.is_empty() { |
| 142 | + Ok(true) |
| 143 | + } else { |
| 144 | + // probably want to have the component ids that didn't respond properly here too |
| 145 | + Err(CfuProtocolError::BadResponse) |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +impl<I: CfuImage, C: CfuComponentTraits> CfuHost for CfuHostInstance<I, C> { |
| 151 | + async fn get_cfu_images<T: CfuImage>(&self) -> Result<Vec<T, MAX_CMPT_COUNT>, CfuError> { |
| 152 | + Err(CfuError::BadImage) |
| 153 | + } |
| 154 | + |
| 155 | + async fn get_all_fw_versions<T: CfuWriter>( |
| 156 | + self, |
| 157 | + _writer: &mut T, |
| 158 | + primary_cmpt: ComponentId, |
| 159 | + ) -> Result<GetFwVersionResponse, CfuError> { |
| 160 | + let mut vec: Vec<FwVerComponentInfo, MAX_CMPT_COUNT> = Vec::new(); |
| 161 | + let mut component_count: u8 = 0; |
| 162 | + self.primary_cmpt.get_subcomponents().iter().for_each(|x| { |
| 163 | + if x.is_some() { |
| 164 | + component_count += 1 |
| 165 | + } |
| 166 | + }); |
| 167 | + let result = self.primary_cmpt.get_fw_version().await; |
| 168 | + if result.is_ok() { |
| 169 | + // convert bytes back to a GetFwVersionResponse |
| 170 | + let inner = FwVerComponentInfo::new( |
| 171 | + FwVersion { |
| 172 | + major: 0, |
| 173 | + minor: 1, |
| 174 | + variant: 0, |
| 175 | + }, |
| 176 | + primary_cmpt, |
| 177 | + BankType::DualBank, |
| 178 | + ); |
| 179 | + let _ = vec.push(inner); |
| 180 | + let arr = vec.into_array().unwrap(); |
| 181 | + let resp: GetFwVersionResponse = GetFwVersionResponse { |
| 182 | + header: GetFwVersionResponseHeader::new(component_count, GetFwVerRespHeaderByte3::default()), |
| 183 | + component_info: arr, |
| 184 | + misc_and_protocol_version: 0, |
| 185 | + }; |
| 186 | + Ok(resp) |
| 187 | + } else { |
| 188 | + Err(CfuError::ProtocolError(CfuProtocolError::BadResponse)) |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + async fn process_cfu_offers<'a, T: CfuWriter>( |
| 193 | + _offer_commands: &'a [FwUpdateOfferCommand], |
| 194 | + _writer: &mut T, |
| 195 | + ) -> Result<&'a [FwUpdateOfferResponse], CfuError> { |
| 196 | + // TODO |
| 197 | + Err(CfuError::BadImage) |
| 198 | + } |
| 199 | + |
| 200 | + async fn update_cfu_content<T: CfuWriter>(_writer: &mut T) -> Result<FwUpdateContentResponse, CfuError> { |
| 201 | + Err(CfuError::ProtocolError(CfuProtocolError::WriterError( |
| 202 | + CfuWriterError::Other, |
| 203 | + ))) |
| 204 | + } |
| 205 | + |
| 206 | + async fn is_cfu_image_valid<T: CfuImage>(_image: T) -> Result<bool, CfuError> { |
| 207 | + Ok(true) |
| 208 | + } |
| 209 | +} |
0 commit comments