1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 use crate::error::UwbErr;
18 use uwb_uci_packets::*;
19
20 #[derive(Debug)]
21 pub enum UciMessage {
22 Response(UciResponse),
23 Notification(UciNotification),
24 }
25
26 #[derive(Debug, Clone)]
27 pub enum UciResponse {
28 GetDeviceInfoRsp(GetDeviceInfoRspPacket),
29 GetCapsInfoRsp(GetCapsInfoRspPacket),
30 SetConfigRsp(SetConfigRspPacket),
31 GetConfigRsp(GetConfigRspPacket),
32 DeviceResetRsp(DeviceResetRspPacket),
33 SessionInitRsp(SessionInitRspPacket),
34 SessionDeinitRsp(SessionDeinitRspPacket),
35 SessionGetAppConfigRsp(SessionGetAppConfigRspPacket),
36 SessionSetAppConfigRsp(SessionSetAppConfigRspPacket),
37 SessionGetStateRsp(SessionGetStateRspPacket),
38 SessionGetCountRsp(SessionGetCountRspPacket),
39 SessionUpdateControllerMulticastListRsp(SessionUpdateControllerMulticastListRspPacket),
40 RangeStartRsp(RangeStartRspPacket),
41 RangeStopRsp(RangeStopRspPacket),
42 RangeGetRangingCountRsp(RangeGetRangingCountRspPacket),
43 AndroidSetCountryCodeRsp(AndroidSetCountryCodeRspPacket),
44 AndroidGetPowerStatsRsp(AndroidGetPowerStatsRspPacket),
45 RawVendorRsp(UciResponsePacket),
46 DisableRsp,
47 }
48
49 #[derive(Debug, Clone)]
50 pub enum UciNotification {
51 GenericError(GenericErrorPacket),
52 DeviceStatusNtf(DeviceStatusNtfPacket),
53 SessionStatusNtf(SessionStatusNtfPacket),
54 SessionUpdateControllerMulticastListNtf(SessionUpdateControllerMulticastListNtfPacket),
55 ShortMacTwoWayRangeDataNtf(ShortMacTwoWayRangeDataNtfPacket),
56 ExtendedMacTwoWayRangeDataNtf(ExtendedMacTwoWayRangeDataNtfPacket),
57 RawVendorNtf(UciNotificationPacket),
58 }
59
uci_message(evt: UciPacketPacket) -> Result<UciMessage, UwbErr>60 pub fn uci_message(evt: UciPacketPacket) -> Result<UciMessage, UwbErr> {
61 match evt.specialize() {
62 UciPacketChild::UciResponse(evt) => Ok(UciMessage::Response(uci_response(evt).unwrap())),
63 UciPacketChild::UciNotification(evt) => {
64 Ok(UciMessage::Notification(uci_notification(evt).unwrap()))
65 }
66 _ => Err(UwbErr::Specialize(evt.to_vec())),
67 }
68 }
69
uci_response(evt: UciResponsePacket) -> Result<UciResponse, UwbErr>70 pub fn uci_response(evt: UciResponsePacket) -> Result<UciResponse, UwbErr> {
71 match evt.specialize() {
72 UciResponseChild::CoreResponse(evt) => core_response(evt),
73 UciResponseChild::SessionResponse(evt) => session_response(evt),
74 UciResponseChild::RangingResponse(evt) => ranging_response(evt),
75 UciResponseChild::AndroidResponse(evt) => android_response(evt),
76 UciResponseChild::UciVendor_9_Response(evt) => vendor_response(evt.into()),
77 UciResponseChild::UciVendor_A_Response(evt) => vendor_response(evt.into()),
78 UciResponseChild::UciVendor_B_Response(evt) => vendor_response(evt.into()),
79 UciResponseChild::UciVendor_E_Response(evt) => vendor_response(evt.into()),
80 UciResponseChild::UciVendor_F_Response(evt) => vendor_response(evt.into()),
81 _ => Err(UwbErr::Specialize(evt.to_vec())),
82 }
83 }
84
uci_notification(evt: UciNotificationPacket) -> Result<UciNotification, UwbErr>85 pub fn uci_notification(evt: UciNotificationPacket) -> Result<UciNotification, UwbErr> {
86 match evt.specialize() {
87 UciNotificationChild::CoreNotification(evt) => core_notification(evt),
88 UciNotificationChild::SessionNotification(evt) => session_notification(evt),
89 UciNotificationChild::RangingNotification(evt) => ranging_notification(evt),
90 UciNotificationChild::AndroidNotification(evt) => android_notification(evt),
91 UciNotificationChild::UciVendor_9_Notification(evt) => vendor_notification(evt.into()),
92 UciNotificationChild::UciVendor_A_Notification(evt) => vendor_notification(evt.into()),
93 UciNotificationChild::UciVendor_B_Notification(evt) => vendor_notification(evt.into()),
94 UciNotificationChild::UciVendor_E_Notification(evt) => vendor_notification(evt.into()),
95 UciNotificationChild::UciVendor_F_Notification(evt) => vendor_notification(evt.into()),
96 _ => Err(UwbErr::Specialize(evt.to_vec())),
97 }
98 }
99
core_response(evt: CoreResponsePacket) -> Result<UciResponse, UwbErr>100 fn core_response(evt: CoreResponsePacket) -> Result<UciResponse, UwbErr> {
101 match evt.specialize() {
102 CoreResponseChild::GetDeviceInfoRsp(evt) => Ok(UciResponse::GetDeviceInfoRsp(evt)),
103 CoreResponseChild::GetCapsInfoRsp(evt) => Ok(UciResponse::GetCapsInfoRsp(evt)),
104 CoreResponseChild::SetConfigRsp(evt) => Ok(UciResponse::SetConfigRsp(evt)),
105 CoreResponseChild::GetConfigRsp(evt) => Ok(UciResponse::GetConfigRsp(evt)),
106 CoreResponseChild::DeviceResetRsp(evt) => Ok(UciResponse::DeviceResetRsp(evt)),
107 _ => Err(UwbErr::Specialize(evt.to_vec())),
108 }
109 }
110
session_response(evt: SessionResponsePacket) -> Result<UciResponse, UwbErr>111 fn session_response(evt: SessionResponsePacket) -> Result<UciResponse, UwbErr> {
112 match evt.specialize() {
113 SessionResponseChild::SessionInitRsp(evt) => Ok(UciResponse::SessionInitRsp(evt)),
114 SessionResponseChild::SessionDeinitRsp(evt) => Ok(UciResponse::SessionDeinitRsp(evt)),
115 SessionResponseChild::SessionSetAppConfigRsp(evt) => {
116 Ok(UciResponse::SessionSetAppConfigRsp(evt))
117 }
118 SessionResponseChild::SessionGetAppConfigRsp(evt) => {
119 Ok(UciResponse::SessionGetAppConfigRsp(evt))
120 }
121 SessionResponseChild::SessionGetStateRsp(evt) => Ok(UciResponse::SessionGetStateRsp(evt)),
122 SessionResponseChild::SessionGetCountRsp(evt) => Ok(UciResponse::SessionGetCountRsp(evt)),
123 SessionResponseChild::SessionUpdateControllerMulticastListRsp(evt) => {
124 Ok(UciResponse::SessionUpdateControllerMulticastListRsp(evt))
125 }
126 _ => Err(UwbErr::Specialize(evt.to_vec())),
127 }
128 }
129
ranging_response(evt: RangingResponsePacket) -> Result<UciResponse, UwbErr>130 fn ranging_response(evt: RangingResponsePacket) -> Result<UciResponse, UwbErr> {
131 match evt.specialize() {
132 RangingResponseChild::RangeStartRsp(evt) => Ok(UciResponse::RangeStartRsp(evt)),
133 RangingResponseChild::RangeStopRsp(evt) => Ok(UciResponse::RangeStopRsp(evt)),
134 RangingResponseChild::RangeGetRangingCountRsp(evt) => {
135 Ok(UciResponse::RangeGetRangingCountRsp(evt))
136 }
137 _ => Err(UwbErr::Specialize(evt.to_vec())),
138 }
139 }
140
android_response(evt: AndroidResponsePacket) -> Result<UciResponse, UwbErr>141 fn android_response(evt: AndroidResponsePacket) -> Result<UciResponse, UwbErr> {
142 match evt.specialize() {
143 AndroidResponseChild::AndroidSetCountryCodeRsp(evt) => {
144 Ok(UciResponse::AndroidSetCountryCodeRsp(evt))
145 }
146 AndroidResponseChild::AndroidGetPowerStatsRsp(evt) => {
147 Ok(UciResponse::AndroidGetPowerStatsRsp(evt))
148 }
149 _ => Err(UwbErr::Specialize(evt.to_vec())),
150 }
151 }
152
vendor_response(evt: UciResponsePacket) -> Result<UciResponse, UwbErr>153 fn vendor_response(evt: UciResponsePacket) -> Result<UciResponse, UwbErr> {
154 Ok(UciResponse::RawVendorRsp(evt))
155 }
156
core_notification(evt: CoreNotificationPacket) -> Result<UciNotification, UwbErr>157 fn core_notification(evt: CoreNotificationPacket) -> Result<UciNotification, UwbErr> {
158 match evt.specialize() {
159 CoreNotificationChild::DeviceStatusNtf(evt) => Ok(UciNotification::DeviceStatusNtf(evt)),
160 CoreNotificationChild::GenericError(evt) => Ok(UciNotification::GenericError(evt)),
161 _ => Err(UwbErr::Specialize(evt.to_vec())),
162 }
163 }
164
session_notification(evt: SessionNotificationPacket) -> Result<UciNotification, UwbErr>165 fn session_notification(evt: SessionNotificationPacket) -> Result<UciNotification, UwbErr> {
166 match evt.specialize() {
167 SessionNotificationChild::SessionStatusNtf(evt) => {
168 Ok(UciNotification::SessionStatusNtf(evt))
169 }
170 SessionNotificationChild::SessionUpdateControllerMulticastListNtf(evt) => {
171 Ok(UciNotification::SessionUpdateControllerMulticastListNtf(evt))
172 }
173 _ => Err(UwbErr::Specialize(evt.to_vec())),
174 }
175 }
176
ranging_notification(evt: RangingNotificationPacket) -> Result<UciNotification, UwbErr>177 fn ranging_notification(evt: RangingNotificationPacket) -> Result<UciNotification, UwbErr> {
178 match evt.specialize() {
179 RangingNotificationChild::RangeDataNtf(evt) => range_data_notification(evt),
180 _ => Err(UwbErr::Specialize(evt.to_vec())),
181 }
182 }
183
range_data_notification(evt: RangeDataNtfPacket) -> Result<UciNotification, UwbErr>184 fn range_data_notification(evt: RangeDataNtfPacket) -> Result<UciNotification, UwbErr> {
185 match evt.specialize() {
186 RangeDataNtfChild::ShortMacTwoWayRangeDataNtf(evt) => {
187 Ok(UciNotification::ShortMacTwoWayRangeDataNtf(evt))
188 }
189 RangeDataNtfChild::ExtendedMacTwoWayRangeDataNtf(evt) => {
190 Ok(UciNotification::ExtendedMacTwoWayRangeDataNtf(evt))
191 }
192 _ => Err(UwbErr::Specialize(evt.to_vec())),
193 }
194 }
195
android_notification(evt: AndroidNotificationPacket) -> Result<UciNotification, UwbErr>196 fn android_notification(evt: AndroidNotificationPacket) -> Result<UciNotification, UwbErr> {
197 Err(UwbErr::Specialize(evt.to_vec()))
198 }
199
vendor_notification(evt: UciNotificationPacket) -> Result<UciNotification, UwbErr>200 fn vendor_notification(evt: UciNotificationPacket) -> Result<UciNotification, UwbErr> {
201 Ok(UciNotification::RawVendorNtf(evt))
202 }
203