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

Features: Set defaults from manifest on boot (#478) #489

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/Build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main", "1.2.0.rc","1.3.0.rc" ]
branches: [ "main", "1.4.0.rc" ]

env:
CARGO_TERM_COLOR: always
Expand Down
12 changes: 4 additions & 8 deletions core/main/src/firebolt/firebolt_ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,12 @@ impl FireboltWs {
error!("Error registering the connection {:?}", e);
return;
}
if !gateway_secure {
state
.session_state
.add_pending_session(app_id.clone(), None);
if PermissionHandler::fetch_and_store(&state, &app_id, false)
if !gateway_secure
&& PermissionHandler::fetch_and_store(&state, &app_id, false)
.await
.is_err()
{
error!("Couldnt pre cache permissions");
}
{
error!("Couldnt pre cache permissions");
}

let (mut sender, mut receiver) = ws_stream.split();
Expand Down
11 changes: 10 additions & 1 deletion core/main/src/service/context_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
//

use crate::state::platform_state::PlatformState;
use ripple_sdk::api::context::RippleContextUpdateRequest;
use ripple_sdk::api::config::FEATURE_CLOUD_PERMISSIONS;
use ripple_sdk::api::context::{FeatureUpdate, RippleContextUpdateRequest};
use ripple_sdk::api::device::device_events::{
DeviceEvent, DeviceEventCallback, DeviceEventRequest,
};
Expand Down Expand Up @@ -93,6 +94,14 @@ impl ContextManager {

// Asynchronously get context and update the state
tokio::spawn(async move {
// Set default cloud permissions value
ps_c.get_client().get_extn_client().context_update(
RippleContextUpdateRequest::UpdateFeatures(vec![FeatureUpdate::new(
FEATURE_CLOUD_PERMISSIONS.into(),
ps_c.get_device_manifest().get_features().cloud_permissions,
)]),
);

// Get Initial power state
if let Ok(resp) = ps_c
.get_client()
Expand Down
12 changes: 6 additions & 6 deletions core/main/src/state/cap/permitted_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::{

use ripple_sdk::{
api::{
config::FEATURE_CLOUD_PERMISISONS,
config::FEATURE_CLOUD_PERMISSIONS,
device::device_apps::AppsRequest,
//config::Config,
distributor::distributor_permissions::{PermissionRequest, PermissionResponse},
Expand Down Expand Up @@ -150,11 +150,15 @@ impl PermissionHandler {
app_id: &str,
allow_cached: bool,
) -> RippleResponse {
if state.open_rpc_state.is_app_excluded(app_id) {
return Ok(());
}

if state
.get_client()
.get_extn_client()
.get_features()
.contains(&String::from(FEATURE_CLOUD_PERMISISONS))
.contains(&String::from(FEATURE_CLOUD_PERMISSIONS))
{
if allow_cached {
if let Some(permissions) =
Expand Down Expand Up @@ -206,10 +210,6 @@ impl PermissionHandler {
}

pub async fn device_fetch_and_store(state: &PlatformState, app_id: &str) -> RippleResponse {
if state.open_rpc_state.is_app_excluded(app_id) {
return Ok(());
}

let mut client = state.get_client().get_extn_client();
let resp = client
.request(AppsRequest::GetFireboltPermissions(app_id.to_string()))
Expand Down
2 changes: 1 addition & 1 deletion core/sdk/src/api/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use super::manifest::{

use super::manifest::device_manifest::AppLibraryEntry;

pub const FEATURE_CLOUD_PERMISISONS: &str = "cloud_permissions";
pub const FEATURE_CLOUD_PERMISSIONS: &str = "cloud_permissions";

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum Config {
Expand Down
143 changes: 138 additions & 5 deletions core/sdk/src/api/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ pub enum RippleContextUpdateType {
}

impl RippleContext {
pub fn new(
activation_status: Option<ActivationStatus>,
internet_connectivity: Option<InternetConnectionStatus>,
system_power_state: Option<SystemPowerState>,
time_zone: Option<TimeZone>,
update_type: Option<RippleContextUpdateType>,
features: Vec<String>,
metrics_context: Option<MetricsContext>,
) -> RippleContext {
RippleContext {
activation_status,
internet_connectivity,
system_power_state,
time_zone,
update_type,
features,
metrics_context,
}
}

pub fn is_ripple_context(msg: &ExtnPayload) -> Option<Self> {
RippleContext::get_from_payload(msg.clone())
}
Expand Down Expand Up @@ -141,14 +161,25 @@ impl RippleContext {
false
// This is not an update request so need not to honour it
}
RippleContextUpdateRequest::Features(features) => {
RippleContextUpdateRequest::UpdateFeatures(features) => {
let mut changed = false;
for feature in features {
if !self.features.contains(&feature) {
self.features.push(feature);
changed = true;
match feature.enabled {
true => {
if !self.features.contains(&feature.name) {
self.features.push(feature.name);
changed = true;
}
}
false => {
if self.features.contains(&feature.name) {
self.features.retain(|f| !f.eq(&feature.name));
changed = true;
}
}
}
}

if changed {
self.update_type = Some(RippleContextUpdateType::FeaturesChanged);
}
Expand Down Expand Up @@ -216,14 +247,26 @@ impl ExtnPayloadProvider for RippleContext {
}
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct FeatureUpdate {
name: String,
enabled: bool,
}

impl FeatureUpdate {
pub fn new(name: String, enabled: bool) -> FeatureUpdate {
FeatureUpdate { name, enabled }
}
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum RippleContextUpdateRequest {
Activation(bool),
Token(AccountToken),
InternetStatus(InternetConnectionStatus),
PowerState(SystemPowerState),
TimeZone(TimeZone),
Features(Vec<String>),
UpdateFeatures(Vec<FeatureUpdate>),
MetricsContext(MetricsContext),
RefreshContext(Option<RippleContextUpdateType>),
}
Expand Down Expand Up @@ -286,4 +329,94 @@ mod tests {
let contract_type: RippleContract = RippleContract::RippleContext;
test_extn_payload_provider(ripple_context, contract_type);
}

#[test]
fn test_update_features_enabled_not_exists() {
let name = String::from("foo");
let some_other_feature = String::from("bar");
let mut ripple_context = RippleContext::new(
None,
None,
None,
None,
None,
vec![some_other_feature.clone()],
None,
);
let changed = ripple_context.update(RippleContextUpdateRequest::UpdateFeatures(vec![
FeatureUpdate::new(name.clone(), true),
]));
assert!(changed);
assert!(ripple_context.features.contains(&name));
assert!(ripple_context.features.contains(&some_other_feature));
}

#[test]
fn test_update_features_enabled_exists() {
let name = String::from("foo");
let some_other_feature = String::from("bar");
let mut ripple_context = RippleContext::new(
None,
None,
None,
None,
None,
vec![some_other_feature.clone()],
None,
);
ripple_context.update(RippleContextUpdateRequest::UpdateFeatures(vec![
FeatureUpdate::new(name.clone(), true),
]));
let changed = ripple_context.update(RippleContextUpdateRequest::UpdateFeatures(vec![
FeatureUpdate::new(name.clone(), true),
]));
assert!(!changed);
assert!(ripple_context.features.contains(&name));
assert!(ripple_context.features.contains(&some_other_feature));
}

#[test]
fn test_update_features_disabled_not_exists() {
let name = String::from("foo");
let some_other_feature = String::from("bar");
let mut ripple_context = RippleContext::new(
None,
None,
None,
None,
None,
vec![some_other_feature.clone()],
None,
);
let changed = ripple_context.update(RippleContextUpdateRequest::UpdateFeatures(vec![
FeatureUpdate::new(name.clone(), false),
]));
assert!(!changed);
assert!(!ripple_context.features.contains(&name));
assert!(ripple_context.features.contains(&some_other_feature));
}

#[test]
fn test_update_features_disabled_exists() {
let name = String::from("foo");
let some_other_feature = String::from("bar");
let mut ripple_context = RippleContext::new(
None,
None,
None,
None,
None,
vec![some_other_feature.clone()],
None,
);
ripple_context.update(RippleContextUpdateRequest::UpdateFeatures(vec![
FeatureUpdate::new(name.clone(), true),
]));
let changed = ripple_context.update(RippleContextUpdateRequest::UpdateFeatures(vec![
FeatureUpdate::new(name.clone(), false),
]));
assert!(changed);
assert!(!ripple_context.features.contains(&name));
assert!(ripple_context.features.contains(&some_other_feature));
}
}
Loading