Skip to content

Commit

Permalink
Rsdk 9697 support multiple nvs (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
npmenard authored Jan 13, 2025
1 parent 792a62a commit 0cbf5b1
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 80 deletions.
28 changes: 20 additions & 8 deletions examples/esp-idf-component/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"connect to the AP fail");
ESP_LOGI(TAG, "connect to the AP fail");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
Expand Down Expand Up @@ -274,7 +274,7 @@ void app_main(void)
viam_server_register_c_generic_sensor(viam_ctx, "sensorA", config_A);

if (ret != VIAM_OK) {
ESP_LOGE(TAG,"couldn't register sensorA model, error : %i", ret);
ESP_LOGE(TAG, "couldn't register sensorA model, error : %i", ret);
return;
}

Expand All @@ -286,35 +286,47 @@ void app_main(void)
ret = viam_server_register_c_generic_sensor(viam_ctx, "sensorB", config_B);

if (ret != VIAM_OK) {
ESP_LOGE(TAG,"couldn't register sensorB model, error : %i", ret);
ESP_LOGE(TAG, "couldn't register sensorB model, error : %i", ret);
return;
}

ret = viam_server_set_provisioning_manufacturer(viam_ctx, "viam-example");
if (ret != VIAM_OK) {
ESP_LOGE(TAG,"couldn't set manufacturer, error : %i", ret);
ESP_LOGE(TAG, "couldn't set manufacturer, error : %i", ret);
return;
}

uint8_t mac[8];
esp_err_t esp_err = esp_efuse_mac_get_default(mac);
if (esp_err != ESP_OK){
ESP_LOGE(TAG,"couldn't get default mac, error : %i", esp_err);
ESP_LOGE(TAG, "couldn't get default mac, error : %i", esp_err);
return;
}
char model[50];
snprintf(model, 50, "esp32-%02X%02X", mac[6],mac[7]);
ret = viam_server_set_provisioning_model(viam_ctx, model);
if (ret != VIAM_OK) {
ESP_LOGE(TAG,"couldn't set model, error : %i", ret);
ESP_LOGE(TAG, "couldn't set model, error : %i", ret);
return;
}

ESP_LOGI(TAG,"starting viam server\r\n");
ret = viam_server_add_nvs_storage(viam_ctx, "nvs");
if (ret != VIAM_OK) {
ESP_LOGE(TAG, "couldn't set add nvs partition, error : %i", ret);
return;
}

ret = viam_server_add_nvs_storage(viam_ctx, "nvs_other");
if (ret != VIAM_OK) {
ESP_LOGE(TAG, "couldn't set add nvs partition, error : %i", ret);
return;
}

ESP_LOGI(TAG, "starting viam server\r\n");

xTaskCreatePinnedToCore((void*)viam_server_start, "viam", CONFIG_MICRO_RDK_TASK_STACK_SIZE, viam_ctx, 6, NULL, CONFIG_MICRO_RDK_TASK_PINNED_TO_CORE_1);
#else
ESP_LOGE(TAG,"enable MICRO_RDK_ENABLE_BUILD_LIBRARY ");
ESP_LOGE(TAG, "enable MICRO_RDK_ENABLE_BUILD_LIBRARY ");
#endif

}
6 changes: 6 additions & 0 deletions micro-rdk-ffi/include/micrordk.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ enum viam_code viam_server_register_c_generic_sensor(struct viam_server_context
const char *model,
struct generic_c_sensor_config *sensor);

/*
Add an nvs partition to the storage collection
*/
enum viam_code viam_server_add_nvs_storage(struct viam_server_context *ctx,
const char *storage_name);

/*
Starts the viam server, the function will take ownership of `ctx` therefore future call
*/
Expand Down
51 changes: 48 additions & 3 deletions micro-rdk-ffi/src/ffi/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct viam_server_context {
registry: Box<ComponentRegistry>,
provisioning_info: ProvisioningInfo,
_marker: PhantomData<(*mut u8, PhantomPinned)>, // Non Send, Non Sync
storage: Vec<String>,
}

#[cfg(target_os = "espidf")]
Expand All @@ -49,6 +50,7 @@ pub extern "C" fn init_viam_server_context() -> *mut viam_server_context {
registry,
provisioning_info,
_marker: Default::default(),
storage: Default::default(),
}))
}

Expand Down Expand Up @@ -188,6 +190,35 @@ pub unsafe extern "C" fn viam_server_register_c_generic_sensor(
viam_code::VIAM_OK
}

/// Add an nvs partition to the storage collection
///
/// When the viam server starts each partitions (if they exists) will be added to a storage collection
/// and made available to the server
///
/// # Safety
/// `ctx`, `storage_name` must be valid pointers
/// may panic if the storage_name cannot be pushed on the vector
#[no_mangle]
pub unsafe extern "C" fn viam_server_add_nvs_storage(
ctx: *mut viam_server_context,
storage_name: *const c_char,
) -> viam_code {
if ctx.is_null() || storage_name.is_null() {
return viam_code::VIAM_INVALID_ARG;
}

let ctx = unsafe { &mut *ctx };
let name = if let Ok(s) = unsafe { CStr::from_ptr(storage_name) }.to_str() {
s
} else {
return viam_code::VIAM_INVALID_ARG;
};

ctx.storage.push(name.to_owned());

viam_code::VIAM_OK
}

#[allow(dead_code)]
const ROBOT_ID: Option<&str> = option_env!("MICRO_RDK_ROBOT_ID");
#[allow(dead_code)]
Expand Down Expand Up @@ -257,7 +288,7 @@ pub unsafe extern "C" fn viam_server_start(ctx: *mut viam_server_context) -> via
use micro_rdk::common::credentials_storage::RAMStorage;
use micro_rdk::common::credentials_storage::RobotConfigurationStorage;
use micro_rdk::proto::provisioning::v1::CloudConfig;

//TODO(RSDK-9715)
let ram_storage = RAMStorage::new();
let cloud_conf = if ROBOT_ID.is_some() && ROBOT_SECRET.is_some() {
Some(CloudConfig {
Expand All @@ -269,7 +300,7 @@ pub unsafe extern "C" fn viam_server_start(ctx: *mut viam_server_context) -> via
None
}.expect("has_robot_config set in cfg, but build-time configuration failed to set robot credentials");
ram_storage
.store_robot_credentials(cloud_conf)
.store_robot_credentials(&cloud_conf)
.expect("Failed to store cloud config");
if ROBOT_APP_ADDRESS.is_some() {
ram_storage
Expand All @@ -284,7 +315,21 @@ pub unsafe extern "C" fn viam_server_start(ctx: *mut viam_server_context) -> via
#[cfg(not(target_os = "espidf"))]
let storage = micro_rdk::common::credentials_storage::RAMStorage::default();
#[cfg(target_os = "espidf")]
let storage = micro_rdk::esp32::nvs_storage::NVSStorage::new("nvs").unwrap();
let storage: Vec<micro_rdk::esp32::nvs_storage::NVSStorage> = ctx
.storage
.iter()
.map(|s| {
micro_rdk::esp32::nvs_storage::NVSStorage::new(s).inspect_err(|err| {
log::error!(
"storage {} cannot be built reason {} continuing without",
s,
err
)
})
})
.filter(|r| r.is_ok())
.flatten()
.collect();
storage
};

Expand Down
4 changes: 2 additions & 2 deletions micro-rdk-server/esp32/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod esp32 {
if SSID.is_some() && PASS.is_some() {
log::info!("Storing static values from build time wifi configuration to NVS");
storage
.store_wifi_credentials(WifiCredentials::new(
.store_wifi_credentials(&WifiCredentials::new(
SSID.unwrap().to_string(),
PASS.unwrap().to_string(),
))
Expand All @@ -96,7 +96,7 @@ mod esp32 {
log::info!("Storing static values from build time robot configuration to NVS");
storage
.store_robot_credentials(
RobotCredentials::new(
&RobotCredentials::new(
ROBOT_ID.unwrap().to_string(),
ROBOT_SECRET.unwrap().to_string(),
)
Expand Down
2 changes: 1 addition & 1 deletion micro-rdk-server/native/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod native {
log::info!("Storing static values from build time robot configuration");
storage
.store_robot_credentials(
RobotCredentials::new(
&RobotCredentials::new(
ROBOT_ID.unwrap().to_string(),
ROBOT_SECRET.unwrap().to_string(),
)
Expand Down
10 changes: 5 additions & 5 deletions micro-rdk/src/common/conn/viam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ where
.await
.map(|cert_resp| {
let cert: TlsCertificate = cert_resp.into();
match self.storage.store_tls_certificate(cert.clone()) {
match self.storage.store_tls_certificate(&cert) {
Ok(_) => {
log::debug!("stored TLS certificate received by app");
}
Expand Down Expand Up @@ -1290,7 +1290,7 @@ mod tests {
secret: "".to_string(),
app_address: LOCALHOST_URI.to_owned(),
};
assert!(ram_storage.store_robot_credentials(creds).is_ok());
assert!(ram_storage.store_robot_credentials(&creds).is_ok());

let mdns = NativeMdns::new("".to_owned(), network.get_ip());
assert!(mdns.is_ok());
Expand Down Expand Up @@ -1350,7 +1350,7 @@ mod tests {
secret: "".to_string(),
app_address: LOCALHOST_URI.to_owned(),
};
assert!(ram_storage.store_robot_credentials(creds).is_ok());
assert!(ram_storage.store_robot_credentials(&creds).is_ok());

let mdns = NativeMdns::new("".to_owned(), network.get_ip());
assert!(mdns.is_ok());
Expand Down Expand Up @@ -1460,7 +1460,7 @@ mod tests {
app_address: LOCALHOST_URI.to_owned(),
};

assert!(ram_storage.store_robot_credentials(creds).is_ok());
assert!(ram_storage.store_robot_credentials(&creds).is_ok());

let mdns = NativeMdns::new("".to_owned(), network.get_ip());
assert!(mdns.is_ok());
Expand Down Expand Up @@ -1787,7 +1787,7 @@ mod tests {
_ => panic!("oops expected ipv4"),
};

ram_storage.store_robot_credentials(creds).unwrap();
ram_storage.store_robot_credentials(&creds).unwrap();

let mut a = ViamServerBuilder::new(ram_storage);
let mdns = NativeMdns::new("".to_owned(), Ipv4Addr::new(0, 0, 0, 0)).unwrap();
Expand Down
Loading

0 comments on commit 0cbf5b1

Please sign in to comment.