Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPPL-2654 Merge RPPL-2593 changes to 1.13 #661

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 8 additions & 26 deletions core/main/src/firebolt/handlers/account_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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<String>;
async fn id(&self, ctx: CallContext) -> RpcResult<String>;
#[method(name = "account.uid")]
async fn uid_rpc(&self, ctx: CallContext) -> RpcResult<String>;
async fn uid(&self, ctx: CallContext) -> RpcResult<String>;
}

#[derive(Debug, Clone)]
Expand All @@ -68,31 +68,13 @@ impl AccountServer for AccountImpl {
Ok(())
}

async fn id_rpc(&self, _ctx: CallContext) -> RpcResult<String> {
async fn id(&self, _ctx: CallContext) -> RpcResult<String> {
self.id().await
}

async fn uid_rpc(&self, ctx: CallContext) -> RpcResult<String> {
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<String> {
crate::utils::common::get_uid(&self.platform_state, ctx.app_id, KEY_FIREBOLT_ACCOUNT_UID)
.await
}
}
impl AccountImpl {
Expand Down
78 changes: 5 additions & 73 deletions core/main/src/firebolt/handlers/device_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use crate::{
use jsonrpsee::{
core::{async_trait, RpcResult},
proc_macros::rpc,
types::error::CallError,
RpcModule,
};
use ripple_sdk::{
Expand All @@ -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")]
Expand Down Expand Up @@ -196,68 +190,6 @@ pub async fn get_device_id(state: &PlatformState) -> RpcResult<String> {
}
}

pub async fn get_uid(state: &PlatformState, app_id: String) -> RpcResult<String> {
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::<ExtnResponse>()
{
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<String> {
let resp = state
.get_client()
Expand Down Expand Up @@ -346,7 +278,7 @@ impl DeviceServer for DeviceImpl {
}

async fn uid(&self, ctx: CallContext) -> RpcResult<String> {
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<String> {
Expand Down
8 changes: 6 additions & 2 deletions core/main/src/firebolt/rpc_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Timer>,
) {
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;
Expand Down
30 changes: 30 additions & 0 deletions core/main/src/utils/common.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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)
}
1 change: 1 addition & 0 deletions core/main/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 20 additions & 1 deletion core/sdk/src/api/manifest/extn_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct ExtnManifest {
pub extns: Vec<ExtnManifestEntry>,
pub required_contracts: Vec<String>,
pub rpc_aliases: HashMap<String, Vec<String>>,
#[serde(default)]
pub rpc_overrides: HashMap<String, String>,
pub timeout: Option<u64>,
#[serde(default)]
pub rules_path: Vec<String>,
Expand All @@ -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(),
Expand Down Expand Up @@ -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<String> {
self.rpc_overrides.get(method).cloned()
}
}
#[cfg(test)]
mod tests {
Expand All @@ -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(),
Expand Down Expand Up @@ -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()),
Expand Down
3 changes: 0 additions & 3 deletions core/sdk/src/api/storage_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading