use crate::tlv;
use anyhow;
use serde_json;
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum AssociationFailureCause {
Unknown = 0,
Associationfailed = 1,
Authenticationfailed = 2,
Ssidnotfound = 3,
}
impl AssociationFailureCause {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(AssociationFailureCause::Unknown),
1 => Some(AssociationFailureCause::Associationfailed),
2 => Some(AssociationFailureCause::Authenticationfailed),
3 => Some(AssociationFailureCause::Ssidnotfound),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<AssociationFailureCause> for u8 {
fn from(val: AssociationFailureCause) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum ConnectionStatus {
Connected = 0,
Notconnected = 1,
}
impl ConnectionStatus {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(ConnectionStatus::Connected),
1 => Some(ConnectionStatus::Notconnected),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<ConnectionStatus> for u8 {
fn from(val: ConnectionStatus) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum SecurityType {
Unspecified = 0,
None = 1,
Wep = 2,
Wpa = 3,
Wpa2 = 4,
Wpa3 = 5,
}
impl SecurityType {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(SecurityType::Unspecified),
1 => Some(SecurityType::None),
2 => Some(SecurityType::Wep),
3 => Some(SecurityType::Wpa),
4 => Some(SecurityType::Wpa2),
5 => Some(SecurityType::Wpa3),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<SecurityType> for u8 {
fn from(val: SecurityType) -> Self {
val as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[repr(u8)]
pub enum WiFiVersion {
A = 0,
B = 1,
G = 2,
N = 3,
Ac = 4,
Ax = 5,
Ah = 6,
}
impl WiFiVersion {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(WiFiVersion::A),
1 => Some(WiFiVersion::B),
2 => Some(WiFiVersion::G),
3 => Some(WiFiVersion::N),
4 => Some(WiFiVersion::Ac),
5 => Some(WiFiVersion::Ax),
6 => Some(WiFiVersion::Ah),
_ => None,
}
}
pub fn to_u8(self) -> u8 {
self as u8
}
}
impl From<WiFiVersion> for u8 {
fn from(val: WiFiVersion) -> Self {
val as u8
}
}
pub fn decode_bssid(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<Vec<u8>>> {
if let tlv::TlvItemValue::OctetString(v) = inp {
Ok(Some(v.clone()))
} else {
Ok(None)
}
}
pub fn decode_security_type(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<SecurityType>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(SecurityType::from_u8(*v as u8))
} else {
Ok(None)
}
}
pub fn decode_wifi_version(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<WiFiVersion>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(WiFiVersion::from_u8(*v as u8))
} else {
Ok(None)
}
}
pub fn decode_channel_number(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u16>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u16))
} else {
Ok(None)
}
}
pub fn decode_rssi(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<i8>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as i8))
} else {
Ok(None)
}
}
pub fn decode_beacon_lost_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_beacon_rx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_packet_multicast_rx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_packet_multicast_tx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_packet_unicast_rx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_packet_unicast_tx_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u32>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v as u32))
} else {
Ok(None)
}
}
pub fn decode_current_max_rate(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u64>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v))
} else {
Ok(None)
}
}
pub fn decode_overrun_count(inp: &tlv::TlvItemValue) -> anyhow::Result<Option<u64>> {
if let tlv::TlvItemValue::Int(v) = inp {
Ok(Some(*v))
} else {
Ok(None)
}
}
pub fn decode_attribute_json(cluster_id: u32, attribute_id: u32, tlv_value: &crate::tlv::TlvItemValue) -> String {
if cluster_id != 0x0036 {
return format!("{{\"error\": \"Invalid cluster ID. Expected 0x0036, got {}\"}}", cluster_id);
}
match attribute_id {
0x0000 => {
match decode_bssid(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0001 => {
match decode_security_type(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0002 => {
match decode_wifi_version(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0003 => {
match decode_channel_number(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0004 => {
match decode_rssi(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0005 => {
match decode_beacon_lost_count(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0006 => {
match decode_beacon_rx_count(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0007 => {
match decode_packet_multicast_rx_count(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0008 => {
match decode_packet_multicast_tx_count(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x0009 => {
match decode_packet_unicast_rx_count(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000A => {
match decode_packet_unicast_tx_count(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000B => {
match decode_current_max_rate(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
0x000C => {
match decode_overrun_count(tlv_value) {
Ok(value) => serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()),
Err(e) => format!("{{\"error\": \"{}\"}}", e),
}
}
_ => format!("{{\"error\": \"Unknown attribute ID: {}\"}}", attribute_id),
}
}
pub fn get_attribute_list() -> Vec<(u32, &'static str)> {
vec![
(0x0000, "BSSID"),
(0x0001, "SecurityType"),
(0x0002, "WiFiVersion"),
(0x0003, "ChannelNumber"),
(0x0004, "RSSI"),
(0x0005, "BeaconLostCount"),
(0x0006, "BeaconRxCount"),
(0x0007, "PacketMulticastRxCount"),
(0x0008, "PacketMulticastTxCount"),
(0x0009, "PacketUnicastRxCount"),
(0x000A, "PacketUnicastTxCount"),
(0x000B, "CurrentMaxRate"),
(0x000C, "OverrunCount"),
]
}
#[derive(Debug, serde::Serialize)]
pub struct DisconnectionEvent {
pub reason_code: Option<u16>,
}
#[derive(Debug, serde::Serialize)]
pub struct AssociationFailureEvent {
pub association_failure_cause: Option<AssociationFailureCause>,
pub status: Option<u16>,
}
#[derive(Debug, serde::Serialize)]
pub struct ConnectionStatusEvent {
pub connection_status: Option<ConnectionStatus>,
}
pub fn decode_disconnection_event(inp: &tlv::TlvItemValue) -> anyhow::Result<DisconnectionEvent> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(DisconnectionEvent {
reason_code: item.get_int(&[0]).map(|v| v as u16),
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}
pub fn decode_association_failure_event(inp: &tlv::TlvItemValue) -> anyhow::Result<AssociationFailureEvent> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(AssociationFailureEvent {
association_failure_cause: item.get_int(&[0]).and_then(|v| AssociationFailureCause::from_u8(v as u8)),
status: item.get_int(&[1]).map(|v| v as u16),
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}
pub fn decode_connection_status_event(inp: &tlv::TlvItemValue) -> anyhow::Result<ConnectionStatusEvent> {
if let tlv::TlvItemValue::List(_fields) = inp {
let item = tlv::TlvItem { tag: 0, value: inp.clone() };
Ok(ConnectionStatusEvent {
connection_status: item.get_int(&[0]).and_then(|v| ConnectionStatus::from_u8(v as u8)),
})
} else {
Err(anyhow::anyhow!("Expected struct fields"))
}
}