Skip to content

Commit

Permalink
Start coordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
qm3ster committed Apr 22, 2019
1 parent 1615e3a commit 273a560
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 43 deletions.
4 changes: 2 additions & 2 deletions znp-rs/src/cmd/types.rs
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct ShortAddr(u16);
pub struct ShortAddr(pub u16);

#[derive(Serialize, Deserialize, Debug)]
pub struct IEEEAddr(u64);
pub struct IEEEAddr(pub u64);
140 changes: 140 additions & 0 deletions znp-rs/src/cmd/zdo.rs
Expand Up @@ -6,6 +6,138 @@ use crate::znp_codec::{Subsys, ZnpCmd};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

/// ZDO_NODE_DESC_REQ
#[derive(Serialize, Deserialize, Debug)]
pub struct NodeDescReq {
/// Address to respond to
pub dest_addr: ShortAddr,
/// NWKAddrOfInterest - Specifies NWK address of the destination device being queried
pub query_addr: ShortAddr,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct NodeDescReqRsp {
/// Success 0 or Failure 1
pub status: u8,
}
impl Sreq for NodeDescReq {
type Srsp = NodeDescReqRsp;
const SUBSYS: Subsys = Subsys::ZDO;
const CMD_ID: u8 = 0x02;
const MAX_SIZE: usize = 0x04;
}

// #[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
// #[repr(u8)]
// pub enum LogicalType {
// Coordinator = 0x00,
// Router = 0x01,
// EndDevice = 0x02,
// }

/// ZDO_NODE_DESC_RSP
#[derive(Serialize, Deserialize, Debug)]
pub struct NodeDescRsp {
pub src_addr: ShortAddr,
/// Success 0 or Failure 1
pub status: u8,
pub query_addr: ShortAddr,
/// LogicalType/ComplexDescriptorAvailable/UserDescriptorAvailable
pub field1: u8,
/// APSFlags/FrequencyBand
pub field2: u8,
pub mac_capabilities: u8,
pub manuf_code: u16,
pub max_buffer_size: u8,
pub max_in_transfer_size: u16,
pub server_mask: u16,
pub max_out_transfer_size: u16,
pub descriptor_capabilities: u8,
}
impl AreqIn for NodeDescRsp {
const SUBSYS: Subsys = Subsys::ZDO;
const CMD_ID: u8 = 0x82;
}

/// ZDO_ACTIVE_EP_REQ
#[derive(Serialize, Deserialize, Debug)]
pub struct ActiveEpReq {
/// Address to respond to
pub dest_addr: ShortAddr,
/// NWKAddrOfInterest - Specifies NWK address of the destination device being queried
pub query_addr: ShortAddr,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ActiveEpReqRsp {
/// Success 0 or Failure 1
pub status: u8,
}
impl Sreq for ActiveEpReq {
type Srsp = ActiveEpReqRsp;
const SUBSYS: Subsys = Subsys::ZDO;
const CMD_ID: u8 = 0x05;
const MAX_SIZE: usize = 0x04;
}

/// ZDO_ACTIVE_EP_RSP
#[derive(Serialize, Deserialize, Debug)]
pub struct ActiveEpRsp {
pub src_addr: ShortAddr,
/// Success 0 or Failure 1
pub status: u8,
pub query_addr: ShortAddr,
pub active_eps: Vec<u8>,
}
impl AreqIn for ActiveEpRsp {
const SUBSYS: Subsys = Subsys::ZDO;
const CMD_ID: u8 = 0x85;
}

///ZDO_MGMT_PERMIT_JOIN_REQ
#[derive(Serialize, Deserialize, Debug)]
pub struct MgmtPermitJoinReq {
/// Destination address type: 0x02 – Address 16 bit, 0xFF – Broadcast
pub addr_mode: u8,
// TODO: Can be Broadcast
pub dest_addr: ShortAddr,
/// seconds, 0xff = forever
pub duration: u8,
pub tc_significance: u8,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct MgmtPermitJoinReqRsp {
/// Success 0 or Failure 1
pub status: u8,
}
impl Sreq for MgmtPermitJoinReq {
type Srsp = MgmtPermitJoinReqRsp;
const SUBSYS: Subsys = Subsys::ZDO;
const CMD_ID: u8 = 0x36;
const MAX_SIZE: usize = 0x05;
}

/// ZDO_MGMT_PERMIT_JOIN_RSP
#[derive(Serialize, Deserialize, Debug)]
pub struct MgmtPermitJoinRsp {
pub src_addr: ShortAddr,
/// Success 0 or Failure 1
pub status: u8,
}
impl AreqIn for MgmtPermitJoinRsp {
const SUBSYS: Subsys = Subsys::ZDO;
const CMD_ID: u8 = 0xB6;
}

/// ZDO_MGMT_PERMIT_JOIN_IND
#[derive(Serialize, Deserialize, Debug)]
pub struct MgmtPermitJoinInd {
/// seconds
pub duration: u8,
}
impl AreqIn for MgmtPermitJoinInd {
const SUBSYS: Subsys = Subsys::ZDO;
const CMD_ID: u8 = 0xCB;
}

#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
#[repr(u8)]
pub enum StartupFromAppStatus {
Expand Down Expand Up @@ -107,6 +239,10 @@ impl AreqIn for TrustCntDev {

#[derive(Debug)]
pub enum In {
MgmtPermitJoinRsp(MgmtPermitJoinRsp),
MgmtPermitJoinInd(MgmtPermitJoinInd),
NodeDescRsp(NodeDescRsp),
ActiveEpRsp(ActiveEpRsp),
StateChange(StateChange),
SourceRoute(SourceRoute),
EndDevAnnce(EndDevAnnce),
Expand All @@ -116,6 +252,10 @@ pub enum In {
impl In {
pub fn from_cmd(cmd: ZnpCmd) -> Result<Self> {
match cmd.cmd_id() {
MgmtPermitJoinRsp::CMD_ID => Ok(In::MgmtPermitJoinRsp(cmd.parse()?)),
MgmtPermitJoinInd::CMD_ID => Ok(In::MgmtPermitJoinInd(cmd.parse()?)),
NodeDescRsp::CMD_ID => Ok(In::NodeDescRsp(cmd.parse()?)),
ActiveEpRsp::CMD_ID => Ok(In::ActiveEpRsp(cmd.parse()?)),
StateChange::CMD_ID => Ok(In::StateChange(cmd.parse()?)),
SourceRoute::CMD_ID => Ok(In::SourceRoute(cmd.parse()?)),
EndDevAnnce::CMD_ID => Ok(In::EndDevAnnce(cmd.parse()?)),
Expand Down
89 changes: 74 additions & 15 deletions znp-rs/src/init_coord.rs
@@ -1,8 +1,8 @@
use super::cmd;
use super::cmd::af::Register;
use super::cmd::sys::{ResetReq, ResetType};
use super::cmd::zb::{ConfigId, ReadConfig};
use super::znp::Sender;
use cmd::sys::{ResetReq, ResetType};
use cmd::types::ShortAddr;
use cmd::zb::{ConfigId, ReadConfig};
pub async fn init(znp: &mut Sender) {
use cmd::zb::{ZbDeviceInfoProp, ZbGetDeviceInfoReq};
for param in vec![
Expand Down Expand Up @@ -91,27 +91,86 @@ pub async fn init(znp: &mut Sender) {
println!("expected {:x?}", &param.value);
println!("got {:x?}", res.unwrap().value);
}

use cmd::zdo::StartupFromApp;
let cmd = StartupFromApp {
delay: 100, /* this was 100, why? When would you want this? */
};
let res = await!(znp.sreq(cmd));
println!("StartupFromApp {:x?}", res);

use cmd::zdo::NodeDescReq;
let cmd = NodeDescReq {
dest_addr: ShortAddr(0),
query_addr: ShortAddr(0),
};
let res = await!(znp.sreq(cmd));
println!("NodeDescReq {:x?}", res.unwrap().status);

use cmd::zdo::ActiveEpReq;
let cmd = ActiveEpReq {
dest_addr: ShortAddr(0),
query_addr: ShortAddr(0),
};
let res = await!(znp.sreq(cmd));
println!("Active EPs {:x?}", res.unwrap());

use cmd::af::Register;
let endpoint_profile_ids = [0x0104, 0x0101, 0x0105, 0x0107, 0x0108, 0x0109];
for (ep, app_prof) in endpoint_profile_ids.iter().enumerate() {
for (ep, &app_prof) in (1..).zip(&endpoint_profile_ids) {
let cmd = Register {
ep: ep as u8 + 1,
app_prof: *app_prof,
in_clusters: vec![
crate::zcl::ha::general::basic,
crate::zcl::ha::general::on_off,
],
ep,
app_prof,
dev_type: 0x0005,
// in_clusters: vec![0, 1026, 1029, 6],
..Default::default()
};
let res = await!(znp.sreq(cmd));
println!("{:x?}", res.unwrap().status);
println!("Register ep {:x} {:x?}", ep, res.unwrap());
}
let cmd = Register {
ep: 11,
app_prof: 0x0104,
dev_type: 0x0400,
in_clusters: vec![0x0000, 0x0501, 0x0003],
out_clusters: vec![0x0500, 0x0502, 0x0003],
..Default::default()
};
let res = await!(znp.sreq(cmd));
println!("Register ep 11 {:x?}", res.unwrap());

use cmd::zdo::StartupFromApp;
let cmd = StartupFromApp {
delay: 100, /* this was 100, why? When would you want this? */
let cmd = ActiveEpReq {
dest_addr: ShortAddr(0),
query_addr: ShortAddr(0),
};
let res = await!(znp.sreq(cmd));
println!("StartupFromApp {:x?}", res);
println!("Active EPs {:x?}", res.unwrap());

use cmd::zdo::MgmtPermitJoinReq;
let cmd = MgmtPermitJoinReq {
addr_mode: 0x02,
dest_addr: ShortAddr(0x0000),
duration: 0x0,
tc_significance: 0,
};
let res = await!(znp.sreq(cmd));
println!("MgmtPermitJoinReq {:x?}", res.unwrap().status);
let cmd = MgmtPermitJoinReq {
addr_mode: 0x0f,
dest_addr: ShortAddr(0xfffc),
duration: 0xff,
tc_significance: 0,
};
let res = await!(znp.sreq(cmd));
println!("MgmtPermitJoinReq {:x?}", res.unwrap().status);
// let cmd = MgmtPermitJoinReq {
// addr_mode: 0x02,
// dest_addr: ShortAddr(0x0000),
// duration: 0xff,
// tc_significance: 0,
// };
// let res = await!(znp.sreq(cmd));
// println!("MgmtPermitJoinReq {:x?}", res.unwrap().status);
}

pub async fn soft_reset(znp: &mut Sender) {
Expand Down
46 changes: 21 additions & 25 deletions znp-rs/src/main.rs
Expand Up @@ -17,35 +17,31 @@ mod zcl;

mod znp;
fn main() {
tokio::run_async(
async {
let (mut znp, rec) = znp::Sender::from_path("/dev/ttyACM0");
tokio::spawn_async(
async {
let mut rec = rec;
while let Some(areq) = await!(rec.next()) {
println!("AREQ: {:?}", areq);
}
},
);
tokio::run_async(async {
let (mut znp, rec) = znp::Sender::from_path("/dev/ttyACM0");
tokio::spawn_async(async {
let mut rec = rec;
while let Some(areq) = await!(rec.next()) {
println!("AREQ: {:?}", areq);
}
});

await!(init_coord::init(&mut znp));
await!(init_coord::init(&mut znp));

use cmd::sys::StartTimer;
for timer_id in 0..=3 {
let cmd = StartTimer {
timer_id,
timeout: 50 - 10 * timer_id as u16,
};
let res = await!(znp.sreq(cmd));
println!("StartTimer {:x?}", res);
}
use cmd::sys::StartTimer;
for timer_id in 0..=3 {
let cmd = StartTimer {
timer_id,
timeout: 50 - 10 * timer_id as u16,
};
let res = await!(znp.sreq(cmd));
println!("StartTimer {:x?}", res);
}

// await!(init_coord::soft_reset(&mut znp));
// await!(init_coord::soft_reset(&mut znp));

await!(blink_forever(&mut znp));
},
);
await!(blink_forever(&mut znp));
});
}

async fn blink_forever(znp: &mut znp::Sender) {
Expand Down
2 changes: 1 addition & 1 deletion znp-rs/src/znp.rs
Expand Up @@ -58,7 +58,7 @@ async fn receiver(
}
Ok(Async::Ready(Some(Callback { cb, subsys, cmd_id }))) => {
if subsys != frame.subsys() || cmd_id != frame.cmd_id() {
println!("Mismatched SRSP, probably old");
eprintln!("Mismatched SRSP, probably old: {:?}", frame);
continue;
} else {
break cb;
Expand Down

0 comments on commit 273a560

Please sign in to comment.