diff --git a/core/main/src/firebolt/handlers/account_rpc.rs b/core/main/src/firebolt/handlers/account_rpc.rs index 3e0663934..a16d7f067 100644 --- a/core/main/src/firebolt/handlers/account_rpc.rs +++ b/core/main/src/firebolt/handlers/account_rpc.rs @@ -22,11 +22,9 @@ use jsonrpsee::{ }; use ripple_sdk::{ api::{ - distributor::distributor_encoder::EncoderRequest, gateway::rpc_gateway_api::CallContext, session::{AccountSessionRequest, AccountSessionTokenRequest}, }, - extn::extn_client_message::ExtnResponse, log::error, }; @@ -35,14 +33,16 @@ use crate::{ utils::rpc_utils::rpc_err, }; +const KEY_FIREBOLT_ACCOUNT_UID: &str = "fireboltAccountUid"; + #[rpc(server)] pub trait Account { #[method(name = "account.session")] async fn session(&self, ctx: CallContext, a_t_r: AccountSessionTokenRequest) -> RpcResult<()>; #[method(name = "account.id")] - async fn id_rpc(&self, ctx: CallContext) -> RpcResult; + async fn id(&self, ctx: CallContext) -> RpcResult; #[method(name = "account.uid")] - async fn uid_rpc(&self, ctx: CallContext) -> RpcResult; + async fn uid(&self, ctx: CallContext) -> RpcResult; } #[derive(Debug, Clone)] @@ -68,31 +68,13 @@ impl AccountServer for AccountImpl { Ok(()) } - async fn id_rpc(&self, _ctx: CallContext) -> RpcResult { + async fn id(&self, _ctx: CallContext) -> RpcResult { self.id().await } - async fn uid_rpc(&self, ctx: CallContext) -> RpcResult { - if let Ok(id) = self.id().await { - if self.platform_state.supports_encoding() { - if let Ok(resp) = self - .platform_state - .get_client() - .send_extn_request(EncoderRequest { - reference: id.clone(), - scope: ctx.app_id.clone(), - }) - .await - { - if let Some(ExtnResponse::String(s)) = resp.payload.extract() { - return Ok(s); - } - } - } - return Ok(id); - } - - Err(rpc_err("Account.uid: some failure")) + async fn uid(&self, ctx: CallContext) -> RpcResult { + crate::utils::common::get_uid(&self.platform_state, ctx.app_id, KEY_FIREBOLT_ACCOUNT_UID) + .await } } impl AccountImpl { diff --git a/core/main/src/firebolt/handlers/device_rpc.rs b/core/main/src/firebolt/handlers/device_rpc.rs index 9f6afe7d0..b2e573418 100644 --- a/core/main/src/firebolt/handlers/device_rpc.rs +++ b/core/main/src/firebolt/handlers/device_rpc.rs @@ -28,7 +28,6 @@ use crate::{ use jsonrpsee::{ core::{async_trait, RpcResult}, proc_macros::rpc, - types::error::CallError, RpcModule, }; use ripple_sdk::{ @@ -46,26 +45,21 @@ use ripple_sdk::{ AudioProfile, DeviceVersionResponse, HdcpProfile, HdrProfile, NetworkResponse, }, }, - distributor::distributor_encoder::EncoderRequest, - firebolt::{ - fb_capabilities::CAPABILITY_NOT_SUPPORTED, - fb_general::{ListenRequest, ListenerResponse}, - }, + firebolt::fb_general::{ListenRequest, ListenerResponse}, gateway::rpc_gateway_api::CallContext, session::{AccountSessionRequest, ProvisionRequest}, storage_property::{ - StorageProperty, StoragePropertyData, EVENT_DEVICE_DEVICE_NAME_CHANGED, - EVENT_DEVICE_NAME_CHANGED, KEY_FIREBOLT_DEVICE_UID, SCOPE_DEVICE, + StorageProperty, EVENT_DEVICE_DEVICE_NAME_CHANGED, EVENT_DEVICE_NAME_CHANGED, }, }, extn::extn_client_message::ExtnResponse, log::error, tokio::time::timeout, - uuid::Uuid, }; include!(concat!(env!("OUT_DIR"), "/version.rs")); -pub const DEVICE_UID: &str = "device.uid"; + +const KEY_FIREBOLT_DEVICE_UID: &str = "fireboltDeviceUid"; // #[derive(Serialize, Clone, Debug, Deserialize)] // #[serde(rename_all = "camelCase")] @@ -196,68 +190,6 @@ pub async fn get_device_id(state: &PlatformState) -> RpcResult { } } -pub async fn get_uid(state: &PlatformState, app_id: String) -> RpcResult { - let data = StoragePropertyData { - scope: Some(SCOPE_DEVICE.to_string()), - key: KEY_FIREBOLT_DEVICE_UID, // Static string literal - namespace: app_id.clone(), - value: String::new(), - }; - - // Attempt to get the UID from storage - if let Ok(uid) = StorageManager::get_string_for_scope(state, &data).await { - return Ok(uid); - } - - // check if the app has been migrated - if state - .app_manager_state - .get_persisted_migrated_state_for_app_id(&app_id) - .is_some() - { - let uid = Uuid::new_v4().to_string(); - let mut new_data = data.clone(); - new_data.value = uid.clone(); - StorageManager::set_string_for_scope(state, &new_data, None).await?; - return Ok(uid); - } - - // Fetch and handle the device ID - let device_id = get_device_id(state) - .await - .map_err(|_| rpc_err("parse error"))?; - // check if the state supports encoding - if state.supports_encoding() { - let response = state - .get_client() - .send_extn_request(EncoderRequest { - reference: device_id, - scope: app_id.clone(), - }) - .await; - - if let Ok(resp) = response { - if let Some(ExtnResponse::String(enc_device_id)) = - resp.payload.extract::() - { - let mut new_data = data.clone(); - new_data.value = enc_device_id.clone(); - StorageManager::set_string_for_scope(state, &new_data, None).await?; - state - .app_manager_state - .persist_migrated_state(&app_id, DEVICE_UID.to_string()); - return Ok(enc_device_id); - } - } - } - - Err(jsonrpsee::core::Error::Call(CallError::Custom { - code: CAPABILITY_NOT_SUPPORTED, - message: "capability device:uid is not supported".to_string(), - data: None, - })) -} - pub async fn get_ll_mac_addr(state: PlatformState) -> RpcResult { let resp = state .get_client() @@ -346,7 +278,7 @@ impl DeviceServer for DeviceImpl { } async fn uid(&self, ctx: CallContext) -> RpcResult { - get_uid(&self.state, ctx.app_id).await + crate::utils::common::get_uid(&self.state, ctx.app_id, KEY_FIREBOLT_DEVICE_UID).await } async fn platform(&self, _ctx: CallContext) -> RpcResult { diff --git a/core/main/src/firebolt/rpc_router.rs b/core/main/src/firebolt/rpc_router.rs index efd010c44..a89263eed 100644 --- a/core/main/src/firebolt/rpc_router.rs +++ b/core/main/src/firebolt/rpc_router.rs @@ -133,15 +133,19 @@ async fn resolve_route( impl RpcRouter { pub async fn route( state: PlatformState, - req: RpcRequest, + mut req: RpcRequest, session: Session, timer: Option, ) { let methods = state.router_state.get_methods(); let resources = state.router_state.resources.clone(); + let method = req.method.clone(); + if let Some(overridden_method) = state.get_manifest().has_rpc_override_method(&req.method) { + req.method = overridden_method; + } + tokio::spawn(async move { - let method = req.method.clone(); let app_id = req.ctx.app_id.clone(); let start = Utc::now().timestamp_millis(); let resp = resolve_route(methods, resources, req.clone()).await; diff --git a/core/main/src/utils/common.rs b/core/main/src/utils/common.rs new file mode 100644 index 000000000..6cfc54833 --- /dev/null +++ b/core/main/src/utils/common.rs @@ -0,0 +1,30 @@ +use crate::processor::storage::storage_manager::StorageManager; +use crate::state::platform_state::PlatformState; +use jsonrpsee::core::RpcResult; +use ripple_sdk::{api::storage_property::StoragePropertyData, uuid::Uuid}; + +const UID_SCOPE: &str = "device"; + +pub async fn get_uid( + state: &PlatformState, + app_id: String, + key: &'static str, +) -> RpcResult { + let uid: String; + let mut data = StoragePropertyData { + scope: Some(UID_SCOPE.to_string()), + key, + namespace: app_id.clone(), + value: String::new(), + }; + + if let Ok(id) = StorageManager::get_string_for_scope(state, &data).await { + uid = id; + } else { + // Using app_id as namespace will result in different uid for each app + data.value = Uuid::new_v4().to_string(); + StorageManager::set_string_for_scope(state, &data, None).await?; + uid = data.value; + } + Ok(uid) +} diff --git a/core/main/src/utils/mod.rs b/core/main/src/utils/mod.rs index 9bae75a11..fd25836e9 100644 --- a/core/main/src/utils/mod.rs +++ b/core/main/src/utils/mod.rs @@ -15,6 +15,7 @@ // SPDX-License-Identifier: Apache-2.0 // +pub mod common; pub mod router_utils; pub mod rpc_utils; pub mod serde_utils; diff --git a/core/sdk/src/api/manifest/extn_manifest.rs b/core/sdk/src/api/manifest/extn_manifest.rs index e0553d50e..2453e5ccc 100644 --- a/core/sdk/src/api/manifest/extn_manifest.rs +++ b/core/sdk/src/api/manifest/extn_manifest.rs @@ -32,6 +32,8 @@ pub struct ExtnManifest { pub extns: Vec, pub required_contracts: Vec, pub rpc_aliases: HashMap>, + #[serde(default)] + pub rpc_overrides: HashMap, pub timeout: Option, #[serde(default)] pub rules_path: Vec, @@ -50,6 +52,7 @@ impl Default for ExtnManifest { extns: Vec::new(), required_contracts: Vec::new(), rpc_aliases: HashMap::new(), + rpc_overrides: HashMap::new(), timeout: None, rules_path: Vec::new(), extn_sdks: Vec::new(), @@ -202,6 +205,10 @@ impl ExtnManifest { pub fn get_timeout(&self) -> u64 { self.timeout.unwrap_or(10000) } + + pub fn has_rpc_override_method(&self, method: &str) -> Option { + self.rpc_overrides.get(method).cloned() + } } #[cfg(test)] mod tests { @@ -223,6 +230,7 @@ mod tests { extns: vec![], required_contracts: vec![], rpc_aliases: HashMap::new(), + rpc_overrides: HashMap::new(), timeout: Some(5000), rules_path: Vec::new(), extn_sdks: Vec::new(), @@ -284,7 +292,18 @@ mod tests { } "#; - let expected_manifest = ExtnManifest::default(); + let expected_manifest = ExtnManifest { + default_path: "".to_string(), + default_extension: "".to_string(), + extns: vec![], + required_contracts: vec![], + rpc_aliases: HashMap::new(), + rpc_overrides: HashMap::new(), + timeout: None, + rules_path: Vec::new(), + extn_sdks: Vec::new(), + provider_registrations: default_providers(), + }; assert_eq!( ExtnManifest::load_from_content(contents.to_string()), diff --git a/core/sdk/src/api/storage_property.rs b/core/sdk/src/api/storage_property.rs index f79952df9..d4a53d1d0 100644 --- a/core/sdk/src/api/storage_property.rs +++ b/core/sdk/src/api/storage_property.rs @@ -73,9 +73,6 @@ pub const KEY_PARTNER_EXCLUSIONS: &str = "partnerExclusions"; pub const KEY_SKIP_RESTRICTION: &str = "skipRestriction"; pub const KEY_AUDIO_DESCRIPTION_ENABLED: &str = "audioDescriptionEnabled"; pub const KEY_PREFERRED_AUDIO_LANGUAGES: &str = "preferredAudioLanguages"; -pub const KEY_FIREBOLT_DEVICE_UID: &str = "fireboltDeviceUid"; - -pub const SCOPE_DEVICE: &str = "device"; pub const EVENT_CLOSED_CAPTIONS_SETTINGS_CHANGED: &str = "accessibility.onClosedCaptionsSettingsChanged";