Skip to content

Commit 948b1b7

Browse files
committed
osdpctl: Fix create and start workflows
Signed-off-by: Siddharth Chandrasekaran <[email protected]>
1 parent f21b21e commit 948b1b7

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

osdpctl/config/pd-1.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = pd1
22
address = 2
3-
channel = unix::conn-pd0
3+
channel = unix::conn-pd1
44
scbk = 2a2ec2e2ab95345eaa80577948daf5bb
55
flags = EnforceSecure
66
log_level = INFO

osdpctl/src/config.rs

+29-22
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,23 @@ fn vec_to_array<T, const N: usize>(v: Vec<T>) -> [T; N] {
2626
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2727
pub struct KeyStore {
2828
store: PathBuf,
29+
pub key: [u8; 16],
2930
}
3031

3132
impl KeyStore {
3233
pub fn create(store: PathBuf, key: &str) -> Result<Self> {
33-
_ = KeyStore::str_to_key(key)?;
34-
std::fs::write(&store, key)?;
35-
Ok(Self { store })
34+
let key = KeyStore::str_to_key(key)?;
35+
std::fs::write(&store, key)
36+
.expect("Unable to write to keystore");
37+
Ok(Self { store, key })
3638
}
3739

3840
pub fn _new(store: PathBuf) -> Result<Self> {
39-
let key = KeyStore::key_to_str(&KeyStore::_random_key());
40-
std::fs::write(&store, key)?;
41-
Ok(Self { store })
41+
let key = KeyStore::_random_key();
42+
let key_str = KeyStore::key_to_str(&key);
43+
std::fs::write(&store, key_str)
44+
.expect("Unable to write to keystore");
45+
Ok(Self { store, key })
4246
}
4347

4448
pub fn _random_key() -> [u8; 16] {
@@ -68,16 +72,15 @@ impl KeyStore {
6872
}
6973

7074
pub fn load(&self) -> Result<[u8; 16]> {
71-
if !self.store.exists() {
72-
anyhow::bail!("Store does not exist");
73-
}
74-
let s = std::fs::read_to_string(self.store.as_path())?;
75-
75+
let s = std::fs::read_to_string(&self.store)
76+
.context(format!("keystore {} not found", self.store.display()))?;
7677
KeyStore::str_to_key(&s)
7778
}
7879

79-
pub fn store(&self, key: [u8; 16]) -> Result<()> {
80-
std::fs::write(&self.store, KeyStore::key_to_str(&key))?;
80+
pub fn store(&mut self, key: [u8; 16]) -> Result<()> {
81+
std::fs::write(&self.store, KeyStore::key_to_str(&key))
82+
.expect("Unable to write to keystore");
83+
self.key = key;
8184
Ok(())
8285
}
8386
}
@@ -103,8 +106,7 @@ impl CpConfig {
103106
pub fn new(config: &Ini, runtime_dir: &Path) -> Result<Self> {
104107
let num_pd = config.getuint("default", "num_pd").unwrap().unwrap() as usize;
105108
let name = config.get("default", "name").unwrap();
106-
let mut runtime_dir = runtime_dir.to_owned();
107-
runtime_dir.push(&name);
109+
let runtime_dir = runtime_dir.to_owned();
108110
let mut pd_data = Vec::new();
109111
for pd in 0..num_pd {
110112
let section = format!("pd-{pd}");
@@ -113,7 +115,7 @@ impl CpConfig {
113115
name: config.get(&section, "name").unwrap(),
114116
channel: config.get(&section, "channel").unwrap(),
115117
address: config.getuint(&section, "address").unwrap().unwrap() as i32,
116-
key_store: KeyStore::create(runtime_dir.join("key.store"), key)?,
118+
key_store: KeyStore::create(runtime_dir.join(format!("pd-{}-key.store", pd)), key)?,
117119
flags: OsdpFlag::empty(),
118120
});
119121
}
@@ -149,7 +151,7 @@ impl CpConfig {
149151
.address(d.address)?
150152
.baud_rate(115200)?
151153
.flag(d.flags)
152-
.secure_channel_key(d.key_store.load()?);
154+
.secure_channel_key(d.key_store.key);
153155
cp = cp.add_channel(Box::new(channel), vec![pd_info]);
154156
}
155157
Ok(cp)
@@ -217,8 +219,7 @@ impl PdConfig {
217219
};
218220
let key = &config.get("default", "scbk").unwrap();
219221
let name = config.get("default", "name").unwrap();
220-
let mut runtime_dir = runtime_dir.to_owned();
221-
runtime_dir.push(&name);
222+
let runtime_dir = runtime_dir.to_owned();
222223
let key_store = KeyStore::create(runtime_dir.join("key.store"), key)?;
223224
Ok(Self {
224225
name,
@@ -247,7 +248,7 @@ impl PdConfig {
247248
.flag(self.flags)
248249
.capabilities(&self.pd_cap)
249250
.id(&self.pd_id)
250-
.secure_channel_key(self.key_store.load()?);
251+
.secure_channel_key(self.key_store.key);
251252
Ok((Box::new(channel), pd_info))
252253
}
253254
}
@@ -287,9 +288,15 @@ impl DeviceConfig {
287288
bail!("Config {} does not exist!", cfg.display())
288289
}
289290
config.load(cfg).unwrap();
291+
292+
let mut runtime_dir = runtime_dir.to_owned();
293+
let name = config.get("default", "name").unwrap();
294+
runtime_dir.push(&name);
295+
_ = std::fs::create_dir_all(&runtime_dir);
296+
290297
let config = match config.get("default", "num_pd") {
291-
Some(_) => DeviceConfig::CpConfig(CpConfig::new(&config, runtime_dir)?),
292-
None => DeviceConfig::PdConfig(PdConfig::new(&config, runtime_dir)?),
298+
Some(_) => DeviceConfig::CpConfig(CpConfig::new(&config, &runtime_dir)?),
299+
None => DeviceConfig::PdConfig(PdConfig::new(&config, &runtime_dir)?),
293300
};
294301
Ok(config)
295302
}

osdpctl/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ fn osdpctl_config_dir() -> Result<PathBuf> {
9191
}
9292

9393
fn device_runtime_dir() -> Result<PathBuf> {
94-
let runtime_dir = dirs::runtime_dir().unwrap_or(PathBuf::from_str("/tmp")?);
94+
let mut runtime_dir = dirs::runtime_dir().unwrap_or(PathBuf::from_str("/tmp")?);
95+
runtime_dir.push("osdp");
9596
std::fs::create_dir_all(&runtime_dir)?;
9697
Ok(runtime_dir)
9798
}

osdpctl/src/pd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn setup(dev: &PdConfig, daemonize: bool) -> Result<()> {
2727
Ok(())
2828
}
2929

30-
pub fn main(dev: PdConfig, daemonize: bool) -> Result<()> {
30+
pub fn main(mut dev: PdConfig, daemonize: bool) -> Result<()> {
3131
setup(&dev, daemonize)?;
3232
let (channel, pd_info) = dev.pd_info().context("Failed to create PD info")?;
3333
let mut pd = PeripheralDevice::new(pd_info, channel)?;

0 commit comments

Comments
 (0)