summaryrefslogtreecommitdiff
path: root/src/rust/uci/uci_hmsgs.rs
blob: 9b2f57db17488f0163e8f57332e6779a315a51ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use crate::uci::UwbErr;
use bytes::Bytes;
use log::error;
use num_traits::FromPrimitive;
use uwb_uci_packets::{
    AndroidSetCountryCodeCmdBuilder, AppConfigTlv, Controlee, DeviceResetCmdBuilder, GroupId,
    ResetConfig, SessionInitCmdBuilder, SessionSetAppConfigCmdBuilder, SessionType,
    SessionUpdateControllerMulticastListCmdBuilder, UciCommandPacket, UciVendor_9_CommandBuilder,
    UciVendor_A_CommandBuilder, UciVendor_B_CommandBuilder, UciVendor_E_CommandBuilder,
    UciVendor_F_CommandBuilder, UpdateMulticastListAction,
};

pub fn build_session_init_cmd(
    session_id: u32,
    session_type: u8,
) -> Result<SessionInitCmdBuilder, UwbErr> {
    Ok(SessionInitCmdBuilder {
        session_id,
        session_type: SessionType::from_u8(session_type).ok_or(UwbErr::InvalidArgs)?,
    })
}

pub fn build_set_country_code_cmd(code: &[u8]) -> Result<AndroidSetCountryCodeCmdBuilder, UwbErr> {
    Ok(AndroidSetCountryCodeCmdBuilder { country_code: code.try_into()? })
}

pub fn build_multicast_list_update_cmd(
    session_id: u32,
    action: u8,
    no_of_controlee: u8,
    address_list: &[i16],
    sub_session_id_list: &[i32],
) -> Result<SessionUpdateControllerMulticastListCmdBuilder, UwbErr> {
    if usize::from(no_of_controlee) != address_list.len()
        || usize::from(no_of_controlee) != sub_session_id_list.len()
    {
        return Err(UwbErr::InvalidArgs);
    }
    let mut controlees = Vec::new();
    for i in 0..no_of_controlee {
        controlees.push(Controlee {
            short_address: address_list[i as usize] as u16,
            subsession_id: sub_session_id_list[i as usize] as u32,
        });
    }
    Ok(SessionUpdateControllerMulticastListCmdBuilder {
        session_id,
        action: UpdateMulticastListAction::from_u8(action).ok_or(UwbErr::InvalidArgs)?,
        controlees,
    })
}

pub fn build_set_app_config_cmd(
    session_id: u32,
    no_of_params: u32,
    mut app_configs: &[u8],
) -> Result<SessionSetAppConfigCmdBuilder, UwbErr> {
    if no_of_params != app_configs.len().try_into()? {
        return Err(UwbErr::InvalidArgs);
    }
    let mut tlvs = Vec::new();
    for _ in 0..no_of_params {
        let tlv = AppConfigTlv::parse(app_configs)?;
        app_configs = &app_configs[tlv.v.len() + 2..];
        tlvs.push(tlv);
    }
    Ok(SessionSetAppConfigCmdBuilder { session_id, tlvs })
}

pub fn build_uci_vendor_cmd_packet(
    gid: u32,
    oid: u32,
    payload: Vec<u8>,
) -> Result<UciCommandPacket, UwbErr> {
    use GroupId::*;
    let group_id: GroupId = GroupId::from_u32(gid).ok_or(UwbErr::InvalidArgs)?;
    let payload = if payload.is_empty() { None } else { Some(Bytes::from(payload)) };
    let opcode: u8 = oid.try_into()?;
    let packet: UciCommandPacket = match group_id {
        VendorReserved9 => UciVendor_9_CommandBuilder { opcode, payload }.build().into(),
        VendorReservedA => UciVendor_A_CommandBuilder { opcode, payload }.build().into(),
        VendorReservedB => UciVendor_B_CommandBuilder { opcode, payload }.build().into(),
        VendorReservedE => UciVendor_E_CommandBuilder { opcode, payload }.build().into(),
        VendorReservedF => UciVendor_F_CommandBuilder { opcode, payload }.build().into(),
        _ => {
            error!("Invalid vendor gid {:?}", gid);
            return Err(UwbErr::InvalidArgs);
        }
    };
    Ok(packet)
}

pub fn build_device_reset_cmd(reset_config: u8) -> Result<DeviceResetCmdBuilder, UwbErr> {
    Ok(DeviceResetCmdBuilder {
        reset_config: ResetConfig::from_u8(reset_config).ok_or(UwbErr::InvalidArgs)?,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use num_traits::ToPrimitive;
    use uwb_uci_packets::*;

    #[test]
    fn test_build_uci_vendor_cmd_packet() {
        let oid: u8 = 6;
        let gid = GroupId::VendorReserved9;
        let payload = vec![0x5, 0x5, 0x5, 0x5];
        assert_eq!(
            build_uci_vendor_cmd_packet(gid.to_u32().unwrap(), oid.into(), payload.clone())
                .unwrap()
                .to_bytes(),
            UciVendor_9_CommandBuilder { opcode: oid, payload: Some(Bytes::from(payload)) }
                .build()
                .to_bytes()
        );
    }
}