xref: /aosp_15_r20/hardware/interfaces/wifi/aidl/default/aidl_struct_util.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
2  * Copyright (C) 2022 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 #include <android-base/logging.h>
18 #include <utils/SystemClock.h>
19 
20 #include "aidl_struct_util.h"
21 
22 namespace aidl {
23 namespace android {
24 namespace hardware {
25 namespace wifi {
26 namespace aidl_struct_util {
27 
28 WifiChannelWidthInMhz convertLegacyWifiChannelWidthToAidl(legacy_hal::wifi_channel_width type);
29 bool convertAidlWifiChannelInfoToLegacy(const WifiChannelInfo& aidl_info,
30                                         legacy_hal::wifi_channel_info* legacy_info);
31 RttBw convertLegacyRttBwToAidl(legacy_hal::wifi_rtt_bw type);
32 
safeConvertChar(const char * str,size_t max_len)33 std::string safeConvertChar(const char* str, size_t max_len) {
34     const char* c = str;
35     size_t size = 0;
36     while (*c && (unsigned char)*c < 128 && size < max_len) {
37         ++size;
38         ++c;
39     }
40     return std::string(str, size);
41 }
42 
uintToIntVec(const std::vector<uint32_t> & in)43 inline std::vector<int32_t> uintToIntVec(const std::vector<uint32_t>& in) {
44     return std::vector<int32_t>(in.begin(), in.end());
45 }
46 
convertLegacyChipFeatureToAidl(uint64_t feature)47 IWifiChip::FeatureSetMask convertLegacyChipFeatureToAidl(uint64_t feature) {
48     switch (feature) {
49         case WIFI_FEATURE_SET_TX_POWER_LIMIT:
50             return IWifiChip::FeatureSetMask::SET_TX_POWER_LIMIT;
51         case WIFI_FEATURE_USE_BODY_HEAD_SAR:
52             return IWifiChip::FeatureSetMask::USE_BODY_HEAD_SAR;
53         case WIFI_FEATURE_D2D_RTT:
54             return IWifiChip::FeatureSetMask::D2D_RTT;
55         case WIFI_FEATURE_D2AP_RTT:
56             return IWifiChip::FeatureSetMask::D2AP_RTT;
57         case WIFI_FEATURE_INFRA_60G:
58             return IWifiChip::FeatureSetMask::WIGIG;
59         case WIFI_FEATURE_SET_LATENCY_MODE:
60             return IWifiChip::FeatureSetMask::SET_LATENCY_MODE;
61         case WIFI_FEATURE_P2P_RAND_MAC:
62             return IWifiChip::FeatureSetMask::P2P_RAND_MAC;
63         case WIFI_FEATURE_AFC_CHANNEL:
64             return IWifiChip::FeatureSetMask::SET_AFC_CHANNEL_ALLOWANCE;
65         case WIFI_FEATURE_SET_VOIP_MODE:
66             return IWifiChip::FeatureSetMask::SET_VOIP_MODE;
67         case WIFI_FEATURE_MLO_SAP:
68             return IWifiChip::FeatureSetMask::MLO_SAP;
69     };
70     CHECK(false) << "Unknown legacy feature: " << feature;
71     return {};
72 }
73 
convertLegacyStaIfaceFeatureToAidl(uint64_t feature)74 IWifiStaIface::FeatureSetMask convertLegacyStaIfaceFeatureToAidl(uint64_t feature) {
75     switch (feature) {
76         case WIFI_FEATURE_GSCAN:
77             return IWifiStaIface::FeatureSetMask::BACKGROUND_SCAN;
78         case WIFI_FEATURE_LINK_LAYER_STATS:
79             return IWifiStaIface::FeatureSetMask::LINK_LAYER_STATS;
80         case WIFI_FEATURE_RSSI_MONITOR:
81             return IWifiStaIface::FeatureSetMask::RSSI_MONITOR;
82         case WIFI_FEATURE_CONTROL_ROAMING:
83             return IWifiStaIface::FeatureSetMask::CONTROL_ROAMING;
84         case WIFI_FEATURE_IE_WHITELIST:
85             return IWifiStaIface::FeatureSetMask::PROBE_IE_ALLOWLIST;
86         case WIFI_FEATURE_SCAN_RAND:
87             return IWifiStaIface::FeatureSetMask::SCAN_RAND;
88         case WIFI_FEATURE_INFRA_5G:
89             return IWifiStaIface::FeatureSetMask::STA_5G;
90         case WIFI_FEATURE_HOTSPOT:
91             return IWifiStaIface::FeatureSetMask::HOTSPOT;
92         case WIFI_FEATURE_PNO:
93             return IWifiStaIface::FeatureSetMask::PNO;
94         case WIFI_FEATURE_TDLS:
95             return IWifiStaIface::FeatureSetMask::TDLS;
96         case WIFI_FEATURE_TDLS_OFFCHANNEL:
97             return IWifiStaIface::FeatureSetMask::TDLS_OFFCHANNEL;
98         case WIFI_FEATURE_CONFIG_NDO:
99             return IWifiStaIface::FeatureSetMask::ND_OFFLOAD;
100         case WIFI_FEATURE_MKEEP_ALIVE:
101             return IWifiStaIface::FeatureSetMask::KEEP_ALIVE;
102         case WIFI_FEATURE_ROAMING_MODE_CONTROL:
103             return IWifiStaIface::FeatureSetMask::ROAMING_MODE_CONTROL;
104         case WIFI_FEATURE_CACHED_SCAN_RESULTS:
105             return IWifiStaIface::FeatureSetMask::CACHED_SCAN_DATA;
106     };
107     CHECK(false) << "Unknown legacy feature: " << feature;
108     return {};
109 }
110 
convertLegacyChipFeaturesToAidl(uint64_t legacy_feature_set,uint32_t * aidl_feature_set)111 bool convertLegacyChipFeaturesToAidl(uint64_t legacy_feature_set, uint32_t* aidl_feature_set) {
112     if (!aidl_feature_set) {
113         return false;
114     }
115     *aidl_feature_set = 0;
116     std::vector<uint64_t> features = {WIFI_FEATURE_SET_TX_POWER_LIMIT,
117                                       WIFI_FEATURE_USE_BODY_HEAD_SAR,
118                                       WIFI_FEATURE_D2D_RTT,
119                                       WIFI_FEATURE_D2AP_RTT,
120                                       WIFI_FEATURE_INFRA_60G,
121                                       WIFI_FEATURE_SET_LATENCY_MODE,
122                                       WIFI_FEATURE_P2P_RAND_MAC,
123                                       WIFI_FEATURE_AFC_CHANNEL,
124                                       WIFI_FEATURE_SET_VOIP_MODE};
125     for (const auto feature : features) {
126         if (feature & legacy_feature_set) {
127             *aidl_feature_set |= static_cast<uint32_t>(convertLegacyChipFeatureToAidl(feature));
128         }
129     }
130 
131     return true;
132 }
133 
convertLegacyDebugRingBufferFlagsToAidl(uint32_t flag)134 WifiDebugRingBufferFlags convertLegacyDebugRingBufferFlagsToAidl(uint32_t flag) {
135     switch (flag) {
136         case WIFI_RING_BUFFER_FLAG_HAS_BINARY_ENTRIES:
137             return WifiDebugRingBufferFlags::HAS_BINARY_ENTRIES;
138         case WIFI_RING_BUFFER_FLAG_HAS_ASCII_ENTRIES:
139             return WifiDebugRingBufferFlags::HAS_ASCII_ENTRIES;
140     };
141     CHECK(false) << "Unknown legacy flag: " << flag;
142     return {};
143 }
144 
convertLegacyDebugRingBufferStatusToAidl(const legacy_hal::wifi_ring_buffer_status & legacy_status,WifiDebugRingBufferStatus * aidl_status)145 bool convertLegacyDebugRingBufferStatusToAidl(
146         const legacy_hal::wifi_ring_buffer_status& legacy_status,
147         WifiDebugRingBufferStatus* aidl_status) {
148     if (!aidl_status) {
149         return false;
150     }
151     *aidl_status = {};
152     aidl_status->ringName = safeConvertChar(reinterpret_cast<const char*>(legacy_status.name),
153                                             sizeof(legacy_status.name));
154     aidl_status->flags = 0;
155     for (const auto flag :
156          {WIFI_RING_BUFFER_FLAG_HAS_BINARY_ENTRIES, WIFI_RING_BUFFER_FLAG_HAS_ASCII_ENTRIES}) {
157         if (flag & legacy_status.flags) {
158             aidl_status->flags |= static_cast<std::underlying_type<WifiDebugRingBufferFlags>::type>(
159                     convertLegacyDebugRingBufferFlagsToAidl(flag));
160         }
161     }
162     aidl_status->ringId = legacy_status.ring_id;
163     aidl_status->sizeInBytes = legacy_status.ring_buffer_byte_size;
164     // Calculate free size of the ring the buffer. We don't need to send the
165     // exact read/write pointers that were there in the legacy HAL interface.
166     if (legacy_status.written_bytes >= legacy_status.read_bytes) {
167         aidl_status->freeSizeInBytes = legacy_status.ring_buffer_byte_size -
168                                        (legacy_status.written_bytes - legacy_status.read_bytes);
169     } else {
170         aidl_status->freeSizeInBytes = legacy_status.read_bytes - legacy_status.written_bytes;
171     }
172     aidl_status->verboseLevel = legacy_status.verbose_level;
173     return true;
174 }
175 
convertLegacyVectorOfDebugRingBufferStatusToAidl(const std::vector<legacy_hal::wifi_ring_buffer_status> & legacy_status_vec,std::vector<WifiDebugRingBufferStatus> * aidl_status_vec)176 bool convertLegacyVectorOfDebugRingBufferStatusToAidl(
177         const std::vector<legacy_hal::wifi_ring_buffer_status>& legacy_status_vec,
178         std::vector<WifiDebugRingBufferStatus>* aidl_status_vec) {
179     if (!aidl_status_vec) {
180         return false;
181     }
182     *aidl_status_vec = {};
183     for (const auto& legacy_status : legacy_status_vec) {
184         WifiDebugRingBufferStatus aidl_status;
185         if (!convertLegacyDebugRingBufferStatusToAidl(legacy_status, &aidl_status)) {
186             return false;
187         }
188         aidl_status_vec->push_back(aidl_status);
189     }
190     return true;
191 }
192 
convertLegacyWakeReasonStatsToAidl(const legacy_hal::WakeReasonStats & legacy_stats,WifiDebugHostWakeReasonStats * aidl_stats)193 bool convertLegacyWakeReasonStatsToAidl(const legacy_hal::WakeReasonStats& legacy_stats,
194                                         WifiDebugHostWakeReasonStats* aidl_stats) {
195     if (!aidl_stats) {
196         return false;
197     }
198     *aidl_stats = {};
199     aidl_stats->totalCmdEventWakeCnt = legacy_stats.wake_reason_cnt.total_cmd_event_wake;
200     aidl_stats->cmdEventWakeCntPerType = uintToIntVec(legacy_stats.cmd_event_wake_cnt);
201     aidl_stats->totalDriverFwLocalWakeCnt = legacy_stats.wake_reason_cnt.total_driver_fw_local_wake;
202     aidl_stats->driverFwLocalWakeCntPerType = uintToIntVec(legacy_stats.driver_fw_local_wake_cnt);
203     aidl_stats->totalRxPacketWakeCnt = legacy_stats.wake_reason_cnt.total_rx_data_wake;
204     aidl_stats->rxPktWakeDetails.rxUnicastCnt =
205             legacy_stats.wake_reason_cnt.rx_wake_details.rx_unicast_cnt;
206     aidl_stats->rxPktWakeDetails.rxMulticastCnt =
207             legacy_stats.wake_reason_cnt.rx_wake_details.rx_multicast_cnt;
208     aidl_stats->rxPktWakeDetails.rxBroadcastCnt =
209             legacy_stats.wake_reason_cnt.rx_wake_details.rx_broadcast_cnt;
210     aidl_stats->rxMulticastPkWakeDetails.ipv4RxMulticastAddrCnt =
211             legacy_stats.wake_reason_cnt.rx_multicast_wake_pkt_info.ipv4_rx_multicast_addr_cnt;
212     aidl_stats->rxMulticastPkWakeDetails.ipv6RxMulticastAddrCnt =
213             legacy_stats.wake_reason_cnt.rx_multicast_wake_pkt_info.ipv6_rx_multicast_addr_cnt;
214     aidl_stats->rxMulticastPkWakeDetails.otherRxMulticastAddrCnt =
215             legacy_stats.wake_reason_cnt.rx_multicast_wake_pkt_info.other_rx_multicast_addr_cnt;
216     aidl_stats->rxIcmpPkWakeDetails.icmpPkt =
217             legacy_stats.wake_reason_cnt.rx_wake_pkt_classification_info.icmp_pkt;
218     aidl_stats->rxIcmpPkWakeDetails.icmp6Pkt =
219             legacy_stats.wake_reason_cnt.rx_wake_pkt_classification_info.icmp6_pkt;
220     aidl_stats->rxIcmpPkWakeDetails.icmp6Ra =
221             legacy_stats.wake_reason_cnt.rx_wake_pkt_classification_info.icmp6_ra;
222     aidl_stats->rxIcmpPkWakeDetails.icmp6Na =
223             legacy_stats.wake_reason_cnt.rx_wake_pkt_classification_info.icmp6_na;
224     aidl_stats->rxIcmpPkWakeDetails.icmp6Ns =
225             legacy_stats.wake_reason_cnt.rx_wake_pkt_classification_info.icmp6_ns;
226     return true;
227 }
228 
convertAidlTxPowerScenarioToLegacy(IWifiChip::TxPowerScenario aidl_scenario)229 legacy_hal::wifi_power_scenario convertAidlTxPowerScenarioToLegacy(
230         IWifiChip::TxPowerScenario aidl_scenario) {
231     switch (aidl_scenario) {
232         case IWifiChip::TxPowerScenario::VOICE_CALL:
233             return legacy_hal::WIFI_POWER_SCENARIO_VOICE_CALL;
234         case IWifiChip::TxPowerScenario::ON_HEAD_CELL_OFF:
235             return legacy_hal::WIFI_POWER_SCENARIO_ON_HEAD_CELL_OFF;
236         case IWifiChip::TxPowerScenario::ON_HEAD_CELL_ON:
237             return legacy_hal::WIFI_POWER_SCENARIO_ON_HEAD_CELL_ON;
238         case IWifiChip::TxPowerScenario::ON_BODY_CELL_OFF:
239             return legacy_hal::WIFI_POWER_SCENARIO_ON_BODY_CELL_OFF;
240         case IWifiChip::TxPowerScenario::ON_BODY_CELL_ON:
241             return legacy_hal::WIFI_POWER_SCENARIO_ON_BODY_CELL_ON;
242     };
243     CHECK(false);
244 }
245 
convertAidlLatencyModeToLegacy(IWifiChip::LatencyMode aidl_latency_mode)246 legacy_hal::wifi_latency_mode convertAidlLatencyModeToLegacy(
247         IWifiChip::LatencyMode aidl_latency_mode) {
248     switch (aidl_latency_mode) {
249         case IWifiChip::LatencyMode::NORMAL:
250             return legacy_hal::WIFI_LATENCY_MODE_NORMAL;
251         case IWifiChip::LatencyMode::LOW:
252             return legacy_hal::WIFI_LATENCY_MODE_LOW;
253     }
254     CHECK(false);
255 }
256 
convertLegacyWifiMacInfoToAidl(const legacy_hal::WifiMacInfo & legacy_mac_info,IWifiChipEventCallback::RadioModeInfo * aidl_radio_mode_info)257 bool convertLegacyWifiMacInfoToAidl(const legacy_hal::WifiMacInfo& legacy_mac_info,
258                                     IWifiChipEventCallback::RadioModeInfo* aidl_radio_mode_info) {
259     if (!aidl_radio_mode_info) {
260         return false;
261     }
262     *aidl_radio_mode_info = {};
263 
264     aidl_radio_mode_info->radioId = legacy_mac_info.wlan_mac_id;
265     // Convert from bitmask of bands in the legacy HAL to enum value in
266     // the AIDL interface.
267     if (legacy_mac_info.mac_band & legacy_hal::WLAN_MAC_6_0_BAND &&
268         legacy_mac_info.mac_band & legacy_hal::WLAN_MAC_5_0_BAND &&
269         legacy_mac_info.mac_band & legacy_hal::WLAN_MAC_2_4_BAND) {
270         aidl_radio_mode_info->bandInfo = WifiBand::BAND_24GHZ_5GHZ_6GHZ;
271     } else if (legacy_mac_info.mac_band & legacy_hal::WLAN_MAC_6_0_BAND &&
272                legacy_mac_info.mac_band & legacy_hal::WLAN_MAC_5_0_BAND) {
273         aidl_radio_mode_info->bandInfo = WifiBand::BAND_5GHZ_6GHZ;
274     } else if (legacy_mac_info.mac_band & legacy_hal::WLAN_MAC_6_0_BAND) {
275         aidl_radio_mode_info->bandInfo = WifiBand::BAND_6GHZ;
276     } else if (legacy_mac_info.mac_band & legacy_hal::WLAN_MAC_2_4_BAND &&
277                legacy_mac_info.mac_band & legacy_hal::WLAN_MAC_5_0_BAND) {
278         aidl_radio_mode_info->bandInfo = WifiBand::BAND_24GHZ_5GHZ;
279     } else if (legacy_mac_info.mac_band & legacy_hal::WLAN_MAC_2_4_BAND) {
280         aidl_radio_mode_info->bandInfo = WifiBand::BAND_24GHZ;
281     } else if (legacy_mac_info.mac_band & legacy_hal::WLAN_MAC_5_0_BAND) {
282         aidl_radio_mode_info->bandInfo = WifiBand::BAND_5GHZ;
283     } else {
284         aidl_radio_mode_info->bandInfo = WifiBand::BAND_UNSPECIFIED;
285     }
286     std::vector<IWifiChipEventCallback::IfaceInfo> iface_info_vec;
287     for (const auto& legacy_iface_info : legacy_mac_info.iface_infos) {
288         IWifiChipEventCallback::IfaceInfo iface_info;
289         iface_info.name = legacy_iface_info.name;
290         iface_info.channel = legacy_iface_info.channel;
291         iface_info_vec.push_back(iface_info);
292     }
293     aidl_radio_mode_info->ifaceInfos = iface_info_vec;
294     return true;
295 }
296 
convertAidlWifiBandToLegacyMacBand(WifiBand aidl_band)297 uint32_t convertAidlWifiBandToLegacyMacBand(WifiBand aidl_band) {
298     switch (aidl_band) {
299         case WifiBand::BAND_24GHZ:
300             return legacy_hal::WLAN_MAC_2_4_BAND;
301         case WifiBand::BAND_5GHZ:
302         case WifiBand::BAND_5GHZ_DFS:
303         case WifiBand::BAND_5GHZ_WITH_DFS:
304             return legacy_hal::WLAN_MAC_5_0_BAND;
305         case WifiBand::BAND_24GHZ_5GHZ:
306         case WifiBand::BAND_24GHZ_5GHZ_WITH_DFS:
307             return (legacy_hal::WLAN_MAC_2_4_BAND | legacy_hal::WLAN_MAC_5_0_BAND);
308         case WifiBand::BAND_6GHZ:
309             return legacy_hal::WLAN_MAC_6_0_BAND;
310         case WifiBand::BAND_5GHZ_6GHZ:
311             return (legacy_hal::WLAN_MAC_5_0_BAND | legacy_hal::WLAN_MAC_6_0_BAND);
312         case WifiBand::BAND_24GHZ_5GHZ_6GHZ:
313         case WifiBand::BAND_24GHZ_5GHZ_WITH_DFS_6GHZ:
314             return (legacy_hal::WLAN_MAC_2_4_BAND | legacy_hal::WLAN_MAC_5_0_BAND |
315                     legacy_hal::WLAN_MAC_6_0_BAND);
316         case WifiBand::BAND_60GHZ:
317             return legacy_hal::WLAN_MAC_60_0_BAND;
318         default:
319             return (legacy_hal::WLAN_MAC_2_4_BAND | legacy_hal::WLAN_MAC_5_0_BAND |
320                     legacy_hal::WLAN_MAC_6_0_BAND | legacy_hal::WLAN_MAC_60_0_BAND);
321     }
322 }
323 
convertLegacyMacBandToAidlWifiBand(uint32_t band)324 WifiBand convertLegacyMacBandToAidlWifiBand(uint32_t band) {
325     switch (band) {
326         case legacy_hal::WLAN_MAC_2_4_BAND:
327             return WifiBand::BAND_24GHZ;
328         case legacy_hal::WLAN_MAC_5_0_BAND:
329             return WifiBand::BAND_5GHZ;
330         case legacy_hal::WLAN_MAC_6_0_BAND:
331             return WifiBand::BAND_6GHZ;
332         case legacy_hal::WLAN_MAC_60_0_BAND:
333             return WifiBand::BAND_60GHZ;
334         default:
335             return WifiBand::BAND_UNSPECIFIED;
336     }
337 }
338 
convertAidlWifiIfaceModeToLegacy(uint32_t aidl_iface_mask)339 uint32_t convertAidlWifiIfaceModeToLegacy(uint32_t aidl_iface_mask) {
340     uint32_t legacy_iface_mask = 0;
341     if (aidl_iface_mask & static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_STA)) {
342         legacy_iface_mask |= (1 << legacy_hal::WIFI_INTERFACE_STA);
343     }
344     if (aidl_iface_mask & static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_SOFTAP)) {
345         legacy_iface_mask |= (1 << legacy_hal::WIFI_INTERFACE_SOFTAP);
346     }
347     if (aidl_iface_mask & static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_P2P_CLIENT)) {
348         legacy_iface_mask |= (1 << legacy_hal::WIFI_INTERFACE_P2P_CLIENT);
349     }
350     if (aidl_iface_mask & static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_P2P_GO)) {
351         legacy_iface_mask |= (1 << legacy_hal::WIFI_INTERFACE_P2P_GO);
352     }
353     if (aidl_iface_mask & static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_NAN)) {
354         legacy_iface_mask |= (1 << legacy_hal::WIFI_INTERFACE_NAN);
355     }
356     if (aidl_iface_mask & static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_TDLS)) {
357         legacy_iface_mask |= (1 << legacy_hal::WIFI_INTERFACE_TDLS);
358     }
359     if (aidl_iface_mask & static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_MESH)) {
360         legacy_iface_mask |= (1 << legacy_hal::WIFI_INTERFACE_MESH);
361     }
362     if (aidl_iface_mask & static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_IBSS)) {
363         legacy_iface_mask |= (1 << legacy_hal::WIFI_INTERFACE_IBSS);
364     }
365     return legacy_iface_mask;
366 }
367 
convertLegacyWifiInterfaceModeToAidl(uint32_t legacy_iface_mask)368 uint32_t convertLegacyWifiInterfaceModeToAidl(uint32_t legacy_iface_mask) {
369     uint32_t aidl_iface_mask = 0;
370     if (legacy_iface_mask & (1 << legacy_hal::WIFI_INTERFACE_STA)) {
371         aidl_iface_mask |= static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_STA);
372     }
373     if (legacy_iface_mask & (1 << legacy_hal::WIFI_INTERFACE_SOFTAP)) {
374         aidl_iface_mask |= static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_SOFTAP);
375     }
376     if (legacy_iface_mask & (1 << legacy_hal::WIFI_INTERFACE_P2P_CLIENT)) {
377         aidl_iface_mask |= static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_P2P_CLIENT);
378     }
379     if (legacy_iface_mask & (1 << legacy_hal::WIFI_INTERFACE_P2P_GO)) {
380         aidl_iface_mask |= static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_P2P_GO);
381     }
382     if (legacy_iface_mask & (1 << legacy_hal::WIFI_INTERFACE_NAN)) {
383         aidl_iface_mask |= static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_NAN);
384     }
385     if (legacy_iface_mask & (1 << legacy_hal::WIFI_INTERFACE_TDLS)) {
386         aidl_iface_mask |= static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_TDLS);
387     }
388     if (legacy_iface_mask & (1 << legacy_hal::WIFI_INTERFACE_MESH)) {
389         aidl_iface_mask |= static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_MESH);
390     }
391     if (legacy_iface_mask & (1 << legacy_hal::WIFI_INTERFACE_IBSS)) {
392         aidl_iface_mask |= static_cast<int32_t>(WifiIfaceMode::IFACE_MODE_IBSS);
393     }
394     return aidl_iface_mask;
395 }
396 
convertAidlUsableChannelFilterToLegacy(uint32_t aidl_filter_mask)397 uint32_t convertAidlUsableChannelFilterToLegacy(uint32_t aidl_filter_mask) {
398     uint32_t legacy_filter_mask = 0;
399     if (aidl_filter_mask &
400         static_cast<int32_t>(IWifiChip::UsableChannelFilter::CELLULAR_COEXISTENCE)) {
401         legacy_filter_mask |= legacy_hal::WIFI_USABLE_CHANNEL_FILTER_CELLULAR_COEXISTENCE;
402     }
403     if (aidl_filter_mask & static_cast<int32_t>(IWifiChip::UsableChannelFilter::CONCURRENCY)) {
404         legacy_filter_mask |= legacy_hal::WIFI_USABLE_CHANNEL_FILTER_CONCURRENCY;
405     }
406     if (aidl_filter_mask & static_cast<int32_t>(IWifiChip::UsableChannelFilter::NAN_INSTANT_MODE)) {
407         legacy_filter_mask |= WIFI_USABLE_CHANNEL_FILTER_NAN_INSTANT_MODE;
408     }
409     return legacy_filter_mask;
410 }
411 
convertLegacyWifiUsableChannelToAidl(const legacy_hal::wifi_usable_channel & legacy_usable_channel,WifiUsableChannel * aidl_usable_channel)412 bool convertLegacyWifiUsableChannelToAidl(
413         const legacy_hal::wifi_usable_channel& legacy_usable_channel,
414         WifiUsableChannel* aidl_usable_channel) {
415     if (!aidl_usable_channel) {
416         return false;
417     }
418     *aidl_usable_channel = {};
419     aidl_usable_channel->channel = legacy_usable_channel.freq;
420     aidl_usable_channel->channelBandwidth =
421             convertLegacyWifiChannelWidthToAidl(legacy_usable_channel.width);
422     aidl_usable_channel->ifaceModeMask =
423             convertLegacyWifiInterfaceModeToAidl(legacy_usable_channel.iface_mode_mask);
424 
425     return true;
426 }
427 
convertLegacyWifiUsableChannelsToAidl(const std::vector<legacy_hal::wifi_usable_channel> & legacy_usable_channels,std::vector<WifiUsableChannel> * aidl_usable_channels)428 bool convertLegacyWifiUsableChannelsToAidl(
429         const std::vector<legacy_hal::wifi_usable_channel>& legacy_usable_channels,
430         std::vector<WifiUsableChannel>* aidl_usable_channels) {
431     if (!aidl_usable_channels) {
432         return false;
433     }
434     *aidl_usable_channels = {};
435     for (const auto& legacy_usable_channel : legacy_usable_channels) {
436         WifiUsableChannel aidl_usable_channel;
437         if (!convertLegacyWifiUsableChannelToAidl(legacy_usable_channel, &aidl_usable_channel)) {
438             return false;
439         }
440         aidl_usable_channels->push_back(aidl_usable_channel);
441     }
442     return true;
443 }
444 
convertLegacyWifiMacInfosToAidl(const std::vector<legacy_hal::WifiMacInfo> & legacy_mac_infos,std::vector<IWifiChipEventCallback::RadioModeInfo> * aidl_radio_mode_infos)445 bool convertLegacyWifiMacInfosToAidl(
446         const std::vector<legacy_hal::WifiMacInfo>& legacy_mac_infos,
447         std::vector<IWifiChipEventCallback::RadioModeInfo>* aidl_radio_mode_infos) {
448     if (!aidl_radio_mode_infos) {
449         return false;
450     }
451     *aidl_radio_mode_infos = {};
452 
453     for (const auto& legacy_mac_info : legacy_mac_infos) {
454         IWifiChipEventCallback::RadioModeInfo aidl_radio_mode_info;
455         if (!convertLegacyWifiMacInfoToAidl(legacy_mac_info, &aidl_radio_mode_info)) {
456             return false;
457         }
458         aidl_radio_mode_infos->push_back(aidl_radio_mode_info);
459     }
460     return true;
461 }
462 
convertLegacyStaIfaceFeaturesToAidl(uint64_t legacy_feature_set,uint32_t * aidl_feature_set)463 bool convertLegacyStaIfaceFeaturesToAidl(uint64_t legacy_feature_set, uint32_t* aidl_feature_set) {
464     if (!aidl_feature_set) {
465         return false;
466     }
467     *aidl_feature_set = 0;
468     for (const auto feature :
469          {WIFI_FEATURE_GSCAN, WIFI_FEATURE_LINK_LAYER_STATS, WIFI_FEATURE_RSSI_MONITOR,
470           WIFI_FEATURE_CONTROL_ROAMING, WIFI_FEATURE_IE_WHITELIST, WIFI_FEATURE_SCAN_RAND,
471           WIFI_FEATURE_INFRA_5G, WIFI_FEATURE_HOTSPOT, WIFI_FEATURE_PNO, WIFI_FEATURE_TDLS,
472           WIFI_FEATURE_TDLS_OFFCHANNEL, WIFI_FEATURE_CONFIG_NDO, WIFI_FEATURE_MKEEP_ALIVE,
473           WIFI_FEATURE_ROAMING_MODE_CONTROL, WIFI_FEATURE_CACHED_SCAN_RESULTS}) {
474         if (feature & legacy_feature_set) {
475             *aidl_feature_set |= static_cast<uint32_t>(convertLegacyStaIfaceFeatureToAidl(feature));
476         }
477     }
478     // There is no flag for this one in the legacy feature set. Adding it to the
479     // set because all the current devices support it.
480     *aidl_feature_set |= static_cast<uint32_t>(IWifiStaIface::FeatureSetMask::APF);
481     return true;
482 }
483 
convertLegacyApfCapabilitiesToAidl(const legacy_hal::PacketFilterCapabilities & legacy_caps,StaApfPacketFilterCapabilities * aidl_caps)484 bool convertLegacyApfCapabilitiesToAidl(const legacy_hal::PacketFilterCapabilities& legacy_caps,
485                                         StaApfPacketFilterCapabilities* aidl_caps) {
486     if (!aidl_caps) {
487         return false;
488     }
489     *aidl_caps = {};
490     aidl_caps->version = legacy_caps.version;
491     aidl_caps->maxLength = legacy_caps.max_len;
492     return true;
493 }
494 
convertAidlGscanReportEventFlagToLegacy(StaBackgroundScanBucketEventReportSchemeMask aidl_flag)495 uint8_t convertAidlGscanReportEventFlagToLegacy(
496         StaBackgroundScanBucketEventReportSchemeMask aidl_flag) {
497     using AidlFlag = StaBackgroundScanBucketEventReportSchemeMask;
498     switch (aidl_flag) {
499         case AidlFlag::EACH_SCAN:
500             return REPORT_EVENTS_EACH_SCAN;
501         case AidlFlag::FULL_RESULTS:
502             return REPORT_EVENTS_FULL_RESULTS;
503         case AidlFlag::NO_BATCH:
504             return REPORT_EVENTS_NO_BATCH;
505     };
506     CHECK(false);
507 }
508 
convertLegacyGscanDataFlagToAidl(uint8_t legacy_flag)509 StaScanDataFlagMask convertLegacyGscanDataFlagToAidl(uint8_t legacy_flag) {
510     switch (legacy_flag) {
511         case legacy_hal::WIFI_SCAN_FLAG_INTERRUPTED:
512             return StaScanDataFlagMask::INTERRUPTED;
513     };
514     CHECK(false) << "Unknown legacy flag: " << legacy_flag;
515     // To silence the compiler warning about reaching the end of non-void
516     // function.
517     return {};
518 }
519 
convertLegacyGscanCapabilitiesToAidl(const legacy_hal::wifi_gscan_capabilities & legacy_caps,StaBackgroundScanCapabilities * aidl_caps)520 bool convertLegacyGscanCapabilitiesToAidl(const legacy_hal::wifi_gscan_capabilities& legacy_caps,
521                                           StaBackgroundScanCapabilities* aidl_caps) {
522     if (!aidl_caps) {
523         return false;
524     }
525     *aidl_caps = {};
526     aidl_caps->maxCacheSize = legacy_caps.max_scan_cache_size;
527     aidl_caps->maxBuckets = legacy_caps.max_scan_buckets;
528     aidl_caps->maxApCachePerScan = legacy_caps.max_ap_cache_per_scan;
529     aidl_caps->maxReportingThreshold = legacy_caps.max_scan_reporting_threshold;
530     return true;
531 }
532 
533 // Only use to prepare parameters for Gscan.
convertAidlWifiBandToLegacy(WifiBand band)534 legacy_hal::wifi_band convertAidlWifiBandToLegacy(WifiBand band) {
535     switch (band) {
536         case WifiBand::BAND_UNSPECIFIED:
537             return legacy_hal::WIFI_BAND_UNSPECIFIED;
538         case WifiBand::BAND_24GHZ:
539             return legacy_hal::WIFI_BAND_BG;
540         case WifiBand::BAND_5GHZ:
541             return legacy_hal::WIFI_BAND_A;
542         case WifiBand::BAND_5GHZ_DFS:
543             return legacy_hal::WIFI_BAND_A_DFS;
544         case WifiBand::BAND_5GHZ_WITH_DFS:
545             return legacy_hal::WIFI_BAND_A_WITH_DFS;
546         case WifiBand::BAND_24GHZ_5GHZ:
547             return legacy_hal::WIFI_BAND_ABG;
548         case WifiBand::BAND_24GHZ_5GHZ_WITH_DFS:
549             return legacy_hal::WIFI_BAND_ABG_WITH_DFS;
550         case WifiBand::BAND_6GHZ:
551         case WifiBand::BAND_60GHZ:
552         case WifiBand::BAND_5GHZ_6GHZ:
553         case WifiBand::BAND_24GHZ_5GHZ_6GHZ:
554         case WifiBand::BAND_24GHZ_5GHZ_6GHZ_60GHZ:
555         case WifiBand::BAND_24GHZ_5GHZ_WITH_DFS_6GHZ:
556         case WifiBand::BAND_24GHZ_5GHZ_WITH_DFS_6GHZ_60GHZ:
557             LOG(INFO) << "WifiBand mapping may be incorrect, since 6GHz is not supported by legacy";
558             return legacy_hal::WIFI_BAND_UNSPECIFIED;
559         default:
560             CHECK(false);
561             return {};
562     };
563 }
564 
convertAidlGscanParamsToLegacy(const StaBackgroundScanParameters & aidl_scan_params,legacy_hal::wifi_scan_cmd_params * legacy_scan_params)565 bool convertAidlGscanParamsToLegacy(const StaBackgroundScanParameters& aidl_scan_params,
566                                     legacy_hal::wifi_scan_cmd_params* legacy_scan_params) {
567     if (!legacy_scan_params) {
568         return false;
569     }
570     *legacy_scan_params = {};
571     legacy_scan_params->base_period = aidl_scan_params.basePeriodInMs;
572     legacy_scan_params->max_ap_per_scan = aidl_scan_params.maxApPerScan;
573     legacy_scan_params->report_threshold_percent = aidl_scan_params.reportThresholdPercent;
574     legacy_scan_params->report_threshold_num_scans = aidl_scan_params.reportThresholdNumScans;
575     if (aidl_scan_params.buckets.size() > MAX_BUCKETS) {
576         return false;
577     }
578     legacy_scan_params->num_buckets = aidl_scan_params.buckets.size();
579     for (uint32_t bucket_idx = 0; bucket_idx < aidl_scan_params.buckets.size(); bucket_idx++) {
580         const StaBackgroundScanBucketParameters& aidl_bucket_spec =
581                 aidl_scan_params.buckets[bucket_idx];
582         legacy_hal::wifi_scan_bucket_spec& legacy_bucket_spec =
583                 legacy_scan_params->buckets[bucket_idx];
584         if (aidl_bucket_spec.bucketIdx >= MAX_BUCKETS) {
585             return false;
586         }
587         legacy_bucket_spec.bucket = aidl_bucket_spec.bucketIdx;
588         legacy_bucket_spec.band = convertAidlWifiBandToLegacy(aidl_bucket_spec.band);
589         legacy_bucket_spec.period = aidl_bucket_spec.periodInMs;
590         legacy_bucket_spec.max_period = aidl_bucket_spec.exponentialMaxPeriodInMs;
591         legacy_bucket_spec.base = aidl_bucket_spec.exponentialBase;
592         legacy_bucket_spec.step_count = aidl_bucket_spec.exponentialStepCount;
593         legacy_bucket_spec.report_events = 0;
594         using AidlFlag = StaBackgroundScanBucketEventReportSchemeMask;
595         for (const auto flag : {AidlFlag::EACH_SCAN, AidlFlag::FULL_RESULTS, AidlFlag::NO_BATCH}) {
596             if (aidl_bucket_spec.eventReportScheme &
597                 static_cast<std::underlying_type<AidlFlag>::type>(flag)) {
598                 legacy_bucket_spec.report_events |= convertAidlGscanReportEventFlagToLegacy(flag);
599             }
600         }
601         if (aidl_bucket_spec.frequencies.size() > MAX_CHANNELS) {
602             return false;
603         }
604         legacy_bucket_spec.num_channels = aidl_bucket_spec.frequencies.size();
605         for (uint32_t freq_idx = 0; freq_idx < aidl_bucket_spec.frequencies.size(); freq_idx++) {
606             legacy_bucket_spec.channels[freq_idx].channel = aidl_bucket_spec.frequencies[freq_idx];
607         }
608     }
609     return true;
610 }
611 
convertLegacyIeToAidl(const legacy_hal::wifi_information_element & legacy_ie,WifiInformationElement * aidl_ie)612 bool convertLegacyIeToAidl(const legacy_hal::wifi_information_element& legacy_ie,
613                            WifiInformationElement* aidl_ie) {
614     if (!aidl_ie) {
615         return false;
616     }
617     *aidl_ie = {};
618     aidl_ie->id = legacy_ie.id;
619     aidl_ie->data = std::vector<uint8_t>(legacy_ie.data, legacy_ie.data + legacy_ie.len);
620     return true;
621 }
622 
convertLegacyIeBlobToAidl(const uint8_t * ie_blob,uint32_t ie_blob_len,std::vector<WifiInformationElement> * aidl_ies)623 bool convertLegacyIeBlobToAidl(const uint8_t* ie_blob, uint32_t ie_blob_len,
624                                std::vector<WifiInformationElement>* aidl_ies) {
625     if (!ie_blob || !aidl_ies) {
626         return false;
627     }
628     *aidl_ies = {};
629     const uint8_t* ies_begin = ie_blob;
630     const uint8_t* ies_end = ie_blob + ie_blob_len;
631     const uint8_t* next_ie = ies_begin;
632     using wifi_ie = legacy_hal::wifi_information_element;
633     constexpr size_t kIeHeaderLen = sizeof(wifi_ie);
634     // Each IE should at least have the header (i.e |id| & |len| fields).
635     while (next_ie + kIeHeaderLen <= ies_end) {
636         const wifi_ie& legacy_ie = (*reinterpret_cast<const wifi_ie*>(next_ie));
637         uint32_t curr_ie_len = kIeHeaderLen + legacy_ie.len;
638         if (next_ie + curr_ie_len > ies_end) {
639             LOG(ERROR) << "Error parsing IE blob. Next IE: " << (void*)next_ie
640                        << ", Curr IE len: " << curr_ie_len << ", IEs End: " << (void*)ies_end;
641             break;
642         }
643         WifiInformationElement aidl_ie;
644         if (!convertLegacyIeToAidl(legacy_ie, &aidl_ie)) {
645             LOG(ERROR) << "Error converting IE. Id: " << legacy_ie.id << ", len: " << legacy_ie.len;
646             break;
647         }
648         aidl_ies->push_back(std::move(aidl_ie));
649         next_ie += curr_ie_len;
650     }
651     // Check if the blob has been fully consumed.
652     if (next_ie != ies_end) {
653         LOG(ERROR) << "Failed to fully parse IE blob. Next IE: " << (void*)next_ie
654                    << ", IEs End: " << (void*)ies_end;
655     }
656     return true;
657 }
658 
convertLegacyGscanResultToAidl(const legacy_hal::wifi_scan_result & legacy_scan_result,bool has_ie_data,StaScanResult * aidl_scan_result)659 bool convertLegacyGscanResultToAidl(const legacy_hal::wifi_scan_result& legacy_scan_result,
660                                     bool has_ie_data, StaScanResult* aidl_scan_result) {
661     if (!aidl_scan_result) {
662         return false;
663     }
664     *aidl_scan_result = {};
665     aidl_scan_result->timeStampInUs = legacy_scan_result.ts;
666     aidl_scan_result->ssid = std::vector<uint8_t>(
667             legacy_scan_result.ssid,
668             legacy_scan_result.ssid +
669                     strnlen(legacy_scan_result.ssid, sizeof(legacy_scan_result.ssid) - 1));
670     aidl_scan_result->bssid = std::array<uint8_t, 6>();
671     std::copy(legacy_scan_result.bssid, legacy_scan_result.bssid + 6,
672               std::begin(aidl_scan_result->bssid));
673     aidl_scan_result->frequency = legacy_scan_result.channel;
674     aidl_scan_result->rssi = legacy_scan_result.rssi;
675     aidl_scan_result->beaconPeriodInMs = legacy_scan_result.beacon_period;
676     aidl_scan_result->capability = legacy_scan_result.capability;
677     if (has_ie_data) {
678         std::vector<WifiInformationElement> ies;
679         if (!convertLegacyIeBlobToAidl(reinterpret_cast<const uint8_t*>(legacy_scan_result.ie_data),
680                                        legacy_scan_result.ie_length, &ies)) {
681             return false;
682         }
683         aidl_scan_result->informationElements = std::move(ies);
684     }
685     return true;
686 }
687 
convertLegacyCachedGscanResultsToAidl(const legacy_hal::wifi_cached_scan_results & legacy_cached_scan_result,StaScanData * aidl_scan_data)688 bool convertLegacyCachedGscanResultsToAidl(
689         const legacy_hal::wifi_cached_scan_results& legacy_cached_scan_result,
690         StaScanData* aidl_scan_data) {
691     if (!aidl_scan_data) {
692         return false;
693     }
694     *aidl_scan_data = {};
695     int32_t flags = 0;
696     for (const auto flag : {legacy_hal::WIFI_SCAN_FLAG_INTERRUPTED}) {
697         if (legacy_cached_scan_result.flags & flag) {
698             flags |= static_cast<std::underlying_type<StaScanDataFlagMask>::type>(
699                     convertLegacyGscanDataFlagToAidl(flag));
700         }
701     }
702     aidl_scan_data->flags = flags;
703     aidl_scan_data->bucketsScanned = legacy_cached_scan_result.buckets_scanned;
704 
705     CHECK(legacy_cached_scan_result.num_results >= 0 &&
706           legacy_cached_scan_result.num_results <= MAX_AP_CACHE_PER_SCAN);
707     std::vector<StaScanResult> aidl_scan_results;
708     for (int32_t result_idx = 0; result_idx < legacy_cached_scan_result.num_results; result_idx++) {
709         StaScanResult aidl_scan_result;
710         if (!convertLegacyGscanResultToAidl(legacy_cached_scan_result.results[result_idx], false,
711                                             &aidl_scan_result)) {
712             return false;
713         }
714         aidl_scan_results.push_back(aidl_scan_result);
715     }
716     aidl_scan_data->results = std::move(aidl_scan_results);
717     return true;
718 }
719 
convertLegacyVectorOfCachedGscanResultsToAidl(const std::vector<legacy_hal::wifi_cached_scan_results> & legacy_cached_scan_results,std::vector<StaScanData> * aidl_scan_datas)720 bool convertLegacyVectorOfCachedGscanResultsToAidl(
721         const std::vector<legacy_hal::wifi_cached_scan_results>& legacy_cached_scan_results,
722         std::vector<StaScanData>* aidl_scan_datas) {
723     if (!aidl_scan_datas) {
724         return false;
725     }
726     *aidl_scan_datas = {};
727     for (const auto& legacy_cached_scan_result : legacy_cached_scan_results) {
728         StaScanData aidl_scan_data;
729         if (!convertLegacyCachedGscanResultsToAidl(legacy_cached_scan_result, &aidl_scan_data)) {
730             return false;
731         }
732         aidl_scan_datas->push_back(aidl_scan_data);
733     }
734     return true;
735 }
736 
convertLegacyDebugTxPacketFateToAidl(legacy_hal::wifi_tx_packet_fate fate)737 WifiDebugTxPacketFate convertLegacyDebugTxPacketFateToAidl(legacy_hal::wifi_tx_packet_fate fate) {
738     switch (fate) {
739         case legacy_hal::TX_PKT_FATE_ACKED:
740             return WifiDebugTxPacketFate::ACKED;
741         case legacy_hal::TX_PKT_FATE_SENT:
742             return WifiDebugTxPacketFate::SENT;
743         case legacy_hal::TX_PKT_FATE_FW_QUEUED:
744             return WifiDebugTxPacketFate::FW_QUEUED;
745         case legacy_hal::TX_PKT_FATE_FW_DROP_INVALID:
746             return WifiDebugTxPacketFate::FW_DROP_INVALID;
747         case legacy_hal::TX_PKT_FATE_FW_DROP_NOBUFS:
748             return WifiDebugTxPacketFate::FW_DROP_NOBUFS;
749         case legacy_hal::TX_PKT_FATE_FW_DROP_OTHER:
750             return WifiDebugTxPacketFate::FW_DROP_OTHER;
751         case legacy_hal::TX_PKT_FATE_DRV_QUEUED:
752             return WifiDebugTxPacketFate::DRV_QUEUED;
753         case legacy_hal::TX_PKT_FATE_DRV_DROP_INVALID:
754             return WifiDebugTxPacketFate::DRV_DROP_INVALID;
755         case legacy_hal::TX_PKT_FATE_DRV_DROP_NOBUFS:
756             return WifiDebugTxPacketFate::DRV_DROP_NOBUFS;
757         case legacy_hal::TX_PKT_FATE_DRV_DROP_OTHER:
758             return WifiDebugTxPacketFate::DRV_DROP_OTHER;
759     };
760     CHECK(false) << "Unknown legacy fate type: " << fate;
761 }
762 
convertLegacyDebugRxPacketFateToAidl(legacy_hal::wifi_rx_packet_fate fate)763 WifiDebugRxPacketFate convertLegacyDebugRxPacketFateToAidl(legacy_hal::wifi_rx_packet_fate fate) {
764     switch (fate) {
765         case legacy_hal::RX_PKT_FATE_SUCCESS:
766             return WifiDebugRxPacketFate::SUCCESS;
767         case legacy_hal::RX_PKT_FATE_FW_QUEUED:
768             return WifiDebugRxPacketFate::FW_QUEUED;
769         case legacy_hal::RX_PKT_FATE_FW_DROP_FILTER:
770             return WifiDebugRxPacketFate::FW_DROP_FILTER;
771         case legacy_hal::RX_PKT_FATE_FW_DROP_INVALID:
772             return WifiDebugRxPacketFate::FW_DROP_INVALID;
773         case legacy_hal::RX_PKT_FATE_FW_DROP_NOBUFS:
774             return WifiDebugRxPacketFate::FW_DROP_NOBUFS;
775         case legacy_hal::RX_PKT_FATE_FW_DROP_OTHER:
776             return WifiDebugRxPacketFate::FW_DROP_OTHER;
777         case legacy_hal::RX_PKT_FATE_DRV_QUEUED:
778             return WifiDebugRxPacketFate::DRV_QUEUED;
779         case legacy_hal::RX_PKT_FATE_DRV_DROP_FILTER:
780             return WifiDebugRxPacketFate::DRV_DROP_FILTER;
781         case legacy_hal::RX_PKT_FATE_DRV_DROP_INVALID:
782             return WifiDebugRxPacketFate::DRV_DROP_INVALID;
783         case legacy_hal::RX_PKT_FATE_DRV_DROP_NOBUFS:
784             return WifiDebugRxPacketFate::DRV_DROP_NOBUFS;
785         case legacy_hal::RX_PKT_FATE_DRV_DROP_OTHER:
786             return WifiDebugRxPacketFate::DRV_DROP_OTHER;
787     };
788     CHECK(false) << "Unknown legacy fate type: " << fate;
789 }
790 
convertLegacyDebugPacketFateFrameTypeToAidl(legacy_hal::frame_type type)791 WifiDebugPacketFateFrameType convertLegacyDebugPacketFateFrameTypeToAidl(
792         legacy_hal::frame_type type) {
793     switch (type) {
794         case legacy_hal::FRAME_TYPE_UNKNOWN:
795             return WifiDebugPacketFateFrameType::UNKNOWN;
796         case legacy_hal::FRAME_TYPE_ETHERNET_II:
797             return WifiDebugPacketFateFrameType::ETHERNET_II;
798         case legacy_hal::FRAME_TYPE_80211_MGMT:
799             return WifiDebugPacketFateFrameType::MGMT_80211;
800     };
801     CHECK(false) << "Unknown legacy frame type: " << type;
802 }
803 
convertLegacyDebugPacketFateFrameToAidl(const legacy_hal::frame_info & legacy_frame,WifiDebugPacketFateFrameInfo * aidl_frame)804 bool convertLegacyDebugPacketFateFrameToAidl(const legacy_hal::frame_info& legacy_frame,
805                                              WifiDebugPacketFateFrameInfo* aidl_frame) {
806     if (!aidl_frame) {
807         return false;
808     }
809     *aidl_frame = {};
810     aidl_frame->frameType = convertLegacyDebugPacketFateFrameTypeToAidl(legacy_frame.payload_type);
811     aidl_frame->frameLen = legacy_frame.frame_len;
812     aidl_frame->driverTimestampUsec = legacy_frame.driver_timestamp_usec;
813     aidl_frame->firmwareTimestampUsec = legacy_frame.firmware_timestamp_usec;
814     const uint8_t* frame_begin =
815             reinterpret_cast<const uint8_t*>(legacy_frame.frame_content.ethernet_ii_bytes);
816     aidl_frame->frameContent =
817             std::vector<uint8_t>(frame_begin, frame_begin + legacy_frame.frame_len);
818     return true;
819 }
820 
convertLegacyDebugTxPacketFateToAidl(const legacy_hal::wifi_tx_report & legacy_fate,WifiDebugTxPacketFateReport * aidl_fate)821 bool convertLegacyDebugTxPacketFateToAidl(const legacy_hal::wifi_tx_report& legacy_fate,
822                                           WifiDebugTxPacketFateReport* aidl_fate) {
823     if (!aidl_fate) {
824         return false;
825     }
826     *aidl_fate = {};
827     aidl_fate->fate = convertLegacyDebugTxPacketFateToAidl(legacy_fate.fate);
828     return convertLegacyDebugPacketFateFrameToAidl(legacy_fate.frame_inf, &aidl_fate->frameInfo);
829 }
830 
convertLegacyVectorOfDebugTxPacketFateToAidl(const std::vector<legacy_hal::wifi_tx_report> & legacy_fates,std::vector<WifiDebugTxPacketFateReport> * aidl_fates)831 bool convertLegacyVectorOfDebugTxPacketFateToAidl(
832         const std::vector<legacy_hal::wifi_tx_report>& legacy_fates,
833         std::vector<WifiDebugTxPacketFateReport>* aidl_fates) {
834     if (!aidl_fates) {
835         return false;
836     }
837     *aidl_fates = {};
838     for (const auto& legacy_fate : legacy_fates) {
839         WifiDebugTxPacketFateReport aidl_fate;
840         if (!convertLegacyDebugTxPacketFateToAidl(legacy_fate, &aidl_fate)) {
841             return false;
842         }
843         aidl_fates->push_back(aidl_fate);
844     }
845     return true;
846 }
847 
convertLegacyDebugRxPacketFateToAidl(const legacy_hal::wifi_rx_report & legacy_fate,WifiDebugRxPacketFateReport * aidl_fate)848 bool convertLegacyDebugRxPacketFateToAidl(const legacy_hal::wifi_rx_report& legacy_fate,
849                                           WifiDebugRxPacketFateReport* aidl_fate) {
850     if (!aidl_fate) {
851         return false;
852     }
853     *aidl_fate = {};
854     aidl_fate->fate = convertLegacyDebugRxPacketFateToAidl(legacy_fate.fate);
855     return convertLegacyDebugPacketFateFrameToAidl(legacy_fate.frame_inf, &aidl_fate->frameInfo);
856 }
857 
convertLegacyVectorOfDebugRxPacketFateToAidl(const std::vector<legacy_hal::wifi_rx_report> & legacy_fates,std::vector<WifiDebugRxPacketFateReport> * aidl_fates)858 bool convertLegacyVectorOfDebugRxPacketFateToAidl(
859         const std::vector<legacy_hal::wifi_rx_report>& legacy_fates,
860         std::vector<WifiDebugRxPacketFateReport>* aidl_fates) {
861     if (!aidl_fates) {
862         return false;
863     }
864     *aidl_fates = {};
865     for (const auto& legacy_fate : legacy_fates) {
866         WifiDebugRxPacketFateReport aidl_fate;
867         if (!convertLegacyDebugRxPacketFateToAidl(legacy_fate, &aidl_fate)) {
868             return false;
869         }
870         aidl_fates->push_back(aidl_fate);
871     }
872     return true;
873 }
874 
convertLegacyLinkLayerRadioStatsToAidl(const legacy_hal::LinkLayerRadioStats & legacy_radio_stat,StaLinkLayerRadioStats * aidl_radio_stat)875 bool convertLegacyLinkLayerRadioStatsToAidl(
876         const legacy_hal::LinkLayerRadioStats& legacy_radio_stat,
877         StaLinkLayerRadioStats* aidl_radio_stat) {
878     if (!aidl_radio_stat) {
879         return false;
880     }
881     *aidl_radio_stat = {};
882 
883     aidl_radio_stat->radioId = legacy_radio_stat.stats.radio;
884     aidl_radio_stat->onTimeInMs = legacy_radio_stat.stats.on_time;
885     aidl_radio_stat->txTimeInMs = legacy_radio_stat.stats.tx_time;
886     aidl_radio_stat->rxTimeInMs = legacy_radio_stat.stats.rx_time;
887     aidl_radio_stat->onTimeInMsForScan = legacy_radio_stat.stats.on_time_scan;
888     aidl_radio_stat->txTimeInMsPerLevel = uintToIntVec(legacy_radio_stat.tx_time_per_levels);
889     aidl_radio_stat->onTimeInMsForNanScan = legacy_radio_stat.stats.on_time_nbd;
890     aidl_radio_stat->onTimeInMsForBgScan = legacy_radio_stat.stats.on_time_gscan;
891     aidl_radio_stat->onTimeInMsForRoamScan = legacy_radio_stat.stats.on_time_roam_scan;
892     aidl_radio_stat->onTimeInMsForPnoScan = legacy_radio_stat.stats.on_time_pno_scan;
893     aidl_radio_stat->onTimeInMsForHs20Scan = legacy_radio_stat.stats.on_time_hs20;
894 
895     std::vector<WifiChannelStats> aidl_channel_stats;
896 
897     for (const auto& channel_stat : legacy_radio_stat.channel_stats) {
898         WifiChannelStats aidl_channel_stat;
899         aidl_channel_stat.onTimeInMs = channel_stat.on_time;
900         aidl_channel_stat.ccaBusyTimeInMs = channel_stat.cca_busy_time;
901         aidl_channel_stat.channel.width = WifiChannelWidthInMhz::WIDTH_20;
902         aidl_channel_stat.channel.centerFreq = channel_stat.channel.center_freq;
903         aidl_channel_stat.channel.centerFreq0 = channel_stat.channel.center_freq0;
904         aidl_channel_stat.channel.centerFreq1 = channel_stat.channel.center_freq1;
905         aidl_channel_stats.push_back(aidl_channel_stat);
906     }
907 
908     aidl_radio_stat->channelStats = aidl_channel_stats;
909 
910     return true;
911 }
912 
convertLegacyMlLinkStateToAidl(wifi_link_state state)913 StaLinkLayerLinkStats::StaLinkState convertLegacyMlLinkStateToAidl(wifi_link_state state) {
914     if (state == wifi_link_state::WIFI_LINK_STATE_NOT_IN_USE) {
915         return StaLinkLayerLinkStats::StaLinkState::NOT_IN_USE;
916     } else if (state == wifi_link_state::WIFI_LINK_STATE_IN_USE) {
917         return StaLinkLayerLinkStats::StaLinkState::IN_USE;
918     }
919     return StaLinkLayerLinkStats::StaLinkState::UNKNOWN;
920 }
921 
convertLegacyLinkLayerMlStatsToAidl(const legacy_hal::LinkLayerMlStats & legacy_ml_stats,StaLinkLayerStats * aidl_stats)922 bool convertLegacyLinkLayerMlStatsToAidl(const legacy_hal::LinkLayerMlStats& legacy_ml_stats,
923                                          StaLinkLayerStats* aidl_stats) {
924     if (!aidl_stats) {
925         return false;
926     }
927     *aidl_stats = {};
928     std::vector<StaLinkLayerLinkStats> links;
929     // Iterate over each links
930     for (const auto& link : legacy_ml_stats.links) {
931         StaLinkLayerLinkStats linkStats = {};
932         linkStats.linkId = link.stat.link_id;
933         linkStats.state = convertLegacyMlLinkStateToAidl(link.stat.state);
934         linkStats.radioId = link.stat.radio;
935         linkStats.frequencyMhz = link.stat.frequency;
936         linkStats.beaconRx = link.stat.beacon_rx;
937         linkStats.avgRssiMgmt = link.stat.rssi_mgmt;
938         linkStats.wmeBePktStats.rxMpdu = link.stat.ac[legacy_hal::WIFI_AC_BE].rx_mpdu;
939         linkStats.wmeBePktStats.txMpdu = link.stat.ac[legacy_hal::WIFI_AC_BE].tx_mpdu;
940         linkStats.wmeBePktStats.lostMpdu = link.stat.ac[legacy_hal::WIFI_AC_BE].mpdu_lost;
941         linkStats.wmeBePktStats.retries = link.stat.ac[legacy_hal::WIFI_AC_BE].retries;
942         linkStats.wmeBeContentionTimeStats.contentionTimeMinInUsec =
943                 link.stat.ac[legacy_hal::WIFI_AC_BE].contention_time_min;
944         linkStats.wmeBeContentionTimeStats.contentionTimeMaxInUsec =
945                 link.stat.ac[legacy_hal::WIFI_AC_BE].contention_time_max;
946         linkStats.wmeBeContentionTimeStats.contentionTimeAvgInUsec =
947                 link.stat.ac[legacy_hal::WIFI_AC_BE].contention_time_avg;
948         linkStats.wmeBeContentionTimeStats.contentionNumSamples =
949                 link.stat.ac[legacy_hal::WIFI_AC_BE].contention_num_samples;
950         linkStats.wmeBkPktStats.rxMpdu = link.stat.ac[legacy_hal::WIFI_AC_BK].rx_mpdu;
951         linkStats.wmeBkPktStats.txMpdu = link.stat.ac[legacy_hal::WIFI_AC_BK].tx_mpdu;
952         linkStats.wmeBkPktStats.lostMpdu = link.stat.ac[legacy_hal::WIFI_AC_BK].mpdu_lost;
953         linkStats.wmeBkPktStats.retries = link.stat.ac[legacy_hal::WIFI_AC_BK].retries;
954         linkStats.wmeBkContentionTimeStats.contentionTimeMinInUsec =
955                 link.stat.ac[legacy_hal::WIFI_AC_BK].contention_time_min;
956         linkStats.wmeBkContentionTimeStats.contentionTimeMaxInUsec =
957                 link.stat.ac[legacy_hal::WIFI_AC_BK].contention_time_max;
958         linkStats.wmeBkContentionTimeStats.contentionTimeAvgInUsec =
959                 link.stat.ac[legacy_hal::WIFI_AC_BK].contention_time_avg;
960         linkStats.wmeBkContentionTimeStats.contentionNumSamples =
961                 link.stat.ac[legacy_hal::WIFI_AC_BK].contention_num_samples;
962         linkStats.wmeViPktStats.rxMpdu = link.stat.ac[legacy_hal::WIFI_AC_VI].rx_mpdu;
963         linkStats.wmeViPktStats.txMpdu = link.stat.ac[legacy_hal::WIFI_AC_VI].tx_mpdu;
964         linkStats.wmeViPktStats.lostMpdu = link.stat.ac[legacy_hal::WIFI_AC_VI].mpdu_lost;
965         linkStats.wmeViPktStats.retries = link.stat.ac[legacy_hal::WIFI_AC_VI].retries;
966         linkStats.wmeViContentionTimeStats.contentionTimeMinInUsec =
967                 link.stat.ac[legacy_hal::WIFI_AC_VI].contention_time_min;
968         linkStats.wmeViContentionTimeStats.contentionTimeMaxInUsec =
969                 link.stat.ac[legacy_hal::WIFI_AC_VI].contention_time_max;
970         linkStats.wmeViContentionTimeStats.contentionTimeAvgInUsec =
971                 link.stat.ac[legacy_hal::WIFI_AC_VI].contention_time_avg;
972         linkStats.wmeViContentionTimeStats.contentionNumSamples =
973                 link.stat.ac[legacy_hal::WIFI_AC_VI].contention_num_samples;
974         linkStats.wmeVoPktStats.rxMpdu = link.stat.ac[legacy_hal::WIFI_AC_VO].rx_mpdu;
975         linkStats.wmeVoPktStats.txMpdu = link.stat.ac[legacy_hal::WIFI_AC_VO].tx_mpdu;
976         linkStats.wmeVoPktStats.lostMpdu = link.stat.ac[legacy_hal::WIFI_AC_VO].mpdu_lost;
977         linkStats.wmeVoPktStats.retries = link.stat.ac[legacy_hal::WIFI_AC_VO].retries;
978         linkStats.wmeVoContentionTimeStats.contentionTimeMinInUsec =
979                 link.stat.ac[legacy_hal::WIFI_AC_VO].contention_time_min;
980         linkStats.wmeVoContentionTimeStats.contentionTimeMaxInUsec =
981                 link.stat.ac[legacy_hal::WIFI_AC_VO].contention_time_max;
982         linkStats.wmeVoContentionTimeStats.contentionTimeAvgInUsec =
983                 link.stat.ac[legacy_hal::WIFI_AC_VO].contention_time_avg;
984         linkStats.wmeVoContentionTimeStats.contentionNumSamples =
985                 link.stat.ac[legacy_hal::WIFI_AC_VO].contention_num_samples;
986         linkStats.timeSliceDutyCycleInPercent = link.stat.time_slicing_duty_cycle_percent;
987         // peer info legacy_stats conversion.
988         std::vector<StaPeerInfo> aidl_peers_info_stats;
989         for (const auto& legacy_peer_info_stats : link.peers) {
990             StaPeerInfo aidl_peer_info_stats;
991             if (!convertLegacyPeerInfoStatsToAidl(legacy_peer_info_stats, &aidl_peer_info_stats)) {
992                 return false;
993             }
994             aidl_peers_info_stats.push_back(aidl_peer_info_stats);
995         }
996         linkStats.peers = aidl_peers_info_stats;
997         // Push link stats to aidl stats.
998         links.push_back(linkStats);
999     }
1000     aidl_stats->iface.links = links;
1001     // radio legacy_stats conversion.
1002     std::vector<StaLinkLayerRadioStats> aidl_radios_stats;
1003     for (const auto& legacy_radio_stats : legacy_ml_stats.radios) {
1004         StaLinkLayerRadioStats aidl_radio_stats;
1005         if (!convertLegacyLinkLayerRadioStatsToAidl(legacy_radio_stats, &aidl_radio_stats)) {
1006             return false;
1007         }
1008         aidl_radios_stats.push_back(aidl_radio_stats);
1009     }
1010     aidl_stats->radios = aidl_radios_stats;
1011     aidl_stats->timeStampInMs = ::android::uptimeMillis();
1012 
1013     return true;
1014 }
1015 
convertLegacyLinkLayerStatsToAidl(const legacy_hal::LinkLayerStats & legacy_stats,StaLinkLayerStats * aidl_stats)1016 bool convertLegacyLinkLayerStatsToAidl(const legacy_hal::LinkLayerStats& legacy_stats,
1017                                        StaLinkLayerStats* aidl_stats) {
1018     if (!aidl_stats) {
1019         return false;
1020     }
1021     *aidl_stats = {};
1022     std::vector<StaLinkLayerLinkStats> links;
1023     StaLinkLayerLinkStats linkStats = {};
1024     // iface legacy_stats conversion.
1025     linkStats.linkId = 0;
1026     linkStats.beaconRx = legacy_stats.iface.beacon_rx;
1027     linkStats.avgRssiMgmt = legacy_stats.iface.rssi_mgmt;
1028     linkStats.wmeBePktStats.rxMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_BE].rx_mpdu;
1029     linkStats.wmeBePktStats.txMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_BE].tx_mpdu;
1030     linkStats.wmeBePktStats.lostMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_BE].mpdu_lost;
1031     linkStats.wmeBePktStats.retries = legacy_stats.iface.ac[legacy_hal::WIFI_AC_BE].retries;
1032     linkStats.wmeBeContentionTimeStats.contentionTimeMinInUsec =
1033             legacy_stats.iface.ac[legacy_hal::WIFI_AC_BE].contention_time_min;
1034     linkStats.wmeBeContentionTimeStats.contentionTimeMaxInUsec =
1035             legacy_stats.iface.ac[legacy_hal::WIFI_AC_BE].contention_time_max;
1036     linkStats.wmeBeContentionTimeStats.contentionTimeAvgInUsec =
1037             legacy_stats.iface.ac[legacy_hal::WIFI_AC_BE].contention_time_avg;
1038     linkStats.wmeBeContentionTimeStats.contentionNumSamples =
1039             legacy_stats.iface.ac[legacy_hal::WIFI_AC_BE].contention_num_samples;
1040     linkStats.wmeBkPktStats.rxMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_BK].rx_mpdu;
1041     linkStats.wmeBkPktStats.txMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_BK].tx_mpdu;
1042     linkStats.wmeBkPktStats.lostMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_BK].mpdu_lost;
1043     linkStats.wmeBkPktStats.retries = legacy_stats.iface.ac[legacy_hal::WIFI_AC_BK].retries;
1044     linkStats.wmeBkContentionTimeStats.contentionTimeMinInUsec =
1045             legacy_stats.iface.ac[legacy_hal::WIFI_AC_BK].contention_time_min;
1046     linkStats.wmeBkContentionTimeStats.contentionTimeMaxInUsec =
1047             legacy_stats.iface.ac[legacy_hal::WIFI_AC_BK].contention_time_max;
1048     linkStats.wmeBkContentionTimeStats.contentionTimeAvgInUsec =
1049             legacy_stats.iface.ac[legacy_hal::WIFI_AC_BK].contention_time_avg;
1050     linkStats.wmeBkContentionTimeStats.contentionNumSamples =
1051             legacy_stats.iface.ac[legacy_hal::WIFI_AC_BK].contention_num_samples;
1052     linkStats.wmeViPktStats.rxMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_VI].rx_mpdu;
1053     linkStats.wmeViPktStats.txMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_VI].tx_mpdu;
1054     linkStats.wmeViPktStats.lostMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_VI].mpdu_lost;
1055     linkStats.wmeViPktStats.retries = legacy_stats.iface.ac[legacy_hal::WIFI_AC_VI].retries;
1056     linkStats.wmeViContentionTimeStats.contentionTimeMinInUsec =
1057             legacy_stats.iface.ac[legacy_hal::WIFI_AC_VI].contention_time_min;
1058     linkStats.wmeViContentionTimeStats.contentionTimeMaxInUsec =
1059             legacy_stats.iface.ac[legacy_hal::WIFI_AC_VI].contention_time_max;
1060     linkStats.wmeViContentionTimeStats.contentionTimeAvgInUsec =
1061             legacy_stats.iface.ac[legacy_hal::WIFI_AC_VI].contention_time_avg;
1062     linkStats.wmeViContentionTimeStats.contentionNumSamples =
1063             legacy_stats.iface.ac[legacy_hal::WIFI_AC_VI].contention_num_samples;
1064     linkStats.wmeVoPktStats.rxMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_VO].rx_mpdu;
1065     linkStats.wmeVoPktStats.txMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_VO].tx_mpdu;
1066     linkStats.wmeVoPktStats.lostMpdu = legacy_stats.iface.ac[legacy_hal::WIFI_AC_VO].mpdu_lost;
1067     linkStats.wmeVoPktStats.retries = legacy_stats.iface.ac[legacy_hal::WIFI_AC_VO].retries;
1068     linkStats.wmeVoContentionTimeStats.contentionTimeMinInUsec =
1069             legacy_stats.iface.ac[legacy_hal::WIFI_AC_VO].contention_time_min;
1070     linkStats.wmeVoContentionTimeStats.contentionTimeMaxInUsec =
1071             legacy_stats.iface.ac[legacy_hal::WIFI_AC_VO].contention_time_max;
1072     linkStats.wmeVoContentionTimeStats.contentionTimeAvgInUsec =
1073             legacy_stats.iface.ac[legacy_hal::WIFI_AC_VO].contention_time_avg;
1074     linkStats.wmeVoContentionTimeStats.contentionNumSamples =
1075             legacy_stats.iface.ac[legacy_hal::WIFI_AC_VO].contention_num_samples;
1076     linkStats.timeSliceDutyCycleInPercent = legacy_stats.iface.info.time_slicing_duty_cycle_percent;
1077     // peer info legacy_stats conversion.
1078     std::vector<StaPeerInfo> aidl_peers_info_stats;
1079     for (const auto& legacy_peer_info_stats : legacy_stats.peers) {
1080         StaPeerInfo aidl_peer_info_stats;
1081         if (!convertLegacyPeerInfoStatsToAidl(legacy_peer_info_stats, &aidl_peer_info_stats)) {
1082             return false;
1083         }
1084         aidl_peers_info_stats.push_back(aidl_peer_info_stats);
1085     }
1086     linkStats.peers = aidl_peers_info_stats;
1087     links.push_back(linkStats);
1088     aidl_stats->iface.links = links;
1089     // radio legacy_stats conversion.
1090     std::vector<StaLinkLayerRadioStats> aidl_radios_stats;
1091     for (const auto& legacy_radio_stats : legacy_stats.radios) {
1092         StaLinkLayerRadioStats aidl_radio_stats;
1093         if (!convertLegacyLinkLayerRadioStatsToAidl(legacy_radio_stats, &aidl_radio_stats)) {
1094             return false;
1095         }
1096         aidl_radios_stats.push_back(aidl_radio_stats);
1097     }
1098     aidl_stats->radios = aidl_radios_stats;
1099     aidl_stats->timeStampInMs = ::android::uptimeMillis();
1100     return true;
1101 }
1102 
1103 // TODO (b/324519882): Remove logs after validating the structure size.
logAidlLinkLayerStatsSize(StaLinkLayerStats & aidl_stats)1104 void logAidlLinkLayerStatsSize(StaLinkLayerStats& aidl_stats) {
1105     unsigned long expectedMaxRadios = 5;
1106     unsigned long expectedMaxLinks = 5;
1107     unsigned long expectedMaxChannelStats = 512;
1108     unsigned long expectedMaxPeers = 3;
1109     unsigned long expectedMaxRateStats = 1024;
1110 
1111     unsigned long maxChannelStats = 0, maxPeers = 0, maxRateStats = 0;
1112     for (size_t i = 0; i < aidl_stats.radios.size(); i++) {
1113         maxChannelStats =
1114                 std::max(maxChannelStats, (unsigned long)aidl_stats.radios[i].channelStats.size());
1115     }
1116     for (size_t i = 0; i < aidl_stats.iface.links.size(); i++) {
1117         maxPeers = std::max(maxPeers, (unsigned long)aidl_stats.iface.links[i].peers.size());
1118         for (size_t j = 0; j < aidl_stats.iface.links[i].peers.size(); j++) {
1119             maxRateStats =
1120                     std::max(maxRateStats,
1121                              (unsigned long)aidl_stats.iface.links[i].peers[j].rateStats.size());
1122         }
1123     }
1124 
1125     if (aidl_stats.radios.size() > expectedMaxRadios ||
1126         aidl_stats.iface.links.size() > expectedMaxLinks ||
1127         maxChannelStats > expectedMaxChannelStats || maxPeers > expectedMaxPeers ||
1128         maxRateStats > expectedMaxRateStats) {
1129         LOG(INFO) << "StaLinkLayerStats exceeds expected vector size";
1130         LOG(INFO) << "  numRadios: " << aidl_stats.radios.size();
1131         LOG(INFO) << "  numLinks: " << aidl_stats.iface.links.size();
1132         LOG(INFO) << "  maxChannelStats: " << maxChannelStats;
1133         LOG(INFO) << "  maxPeers: " << maxPeers;
1134         LOG(INFO) << "  maxRateStats: " << maxRateStats;
1135     }
1136 }
1137 
convertLegacyPeerInfoStatsToAidl(const legacy_hal::WifiPeerInfo & legacy_peer_info_stats,StaPeerInfo * aidl_peer_info_stats)1138 bool convertLegacyPeerInfoStatsToAidl(const legacy_hal::WifiPeerInfo& legacy_peer_info_stats,
1139                                       StaPeerInfo* aidl_peer_info_stats) {
1140     if (!aidl_peer_info_stats) {
1141         return false;
1142     }
1143     *aidl_peer_info_stats = {};
1144     aidl_peer_info_stats->staCount = legacy_peer_info_stats.peer_info.bssload.sta_count;
1145     aidl_peer_info_stats->chanUtil = legacy_peer_info_stats.peer_info.bssload.chan_util;
1146 
1147     std::vector<StaRateStat> aidlRateStats;
1148     for (const auto& legacy_rate_stats : legacy_peer_info_stats.rate_stats) {
1149         StaRateStat rateStat;
1150         if (!convertLegacyWifiRateInfoToAidl(legacy_rate_stats.rate, &rateStat.rateInfo)) {
1151             return false;
1152         }
1153         rateStat.txMpdu = legacy_rate_stats.tx_mpdu;
1154         rateStat.rxMpdu = legacy_rate_stats.rx_mpdu;
1155         rateStat.mpduLost = legacy_rate_stats.mpdu_lost;
1156         rateStat.retries = legacy_rate_stats.retries;
1157         aidlRateStats.push_back(rateStat);
1158     }
1159     aidl_peer_info_stats->rateStats = aidlRateStats;
1160     return true;
1161 }
1162 
convertLegacyRoamingCapabilitiesToAidl(const legacy_hal::wifi_roaming_capabilities & legacy_caps,StaRoamingCapabilities * aidl_caps)1163 bool convertLegacyRoamingCapabilitiesToAidl(
1164         const legacy_hal::wifi_roaming_capabilities& legacy_caps,
1165         StaRoamingCapabilities* aidl_caps) {
1166     if (!aidl_caps) {
1167         return false;
1168     }
1169     *aidl_caps = {};
1170     aidl_caps->maxBlocklistSize = legacy_caps.max_blacklist_size;
1171     aidl_caps->maxAllowlistSize = legacy_caps.max_whitelist_size;
1172     return true;
1173 }
1174 
convertAidlRoamingConfigToLegacy(const StaRoamingConfig & aidl_config,legacy_hal::wifi_roaming_config * legacy_config)1175 bool convertAidlRoamingConfigToLegacy(const StaRoamingConfig& aidl_config,
1176                                       legacy_hal::wifi_roaming_config* legacy_config) {
1177     if (!legacy_config) {
1178         return false;
1179     }
1180     *legacy_config = {};
1181     if (aidl_config.bssidBlocklist.size() > MAX_BLACKLIST_BSSID ||
1182         aidl_config.ssidAllowlist.size() > MAX_WHITELIST_SSID) {
1183         return false;
1184     }
1185     legacy_config->num_blacklist_bssid = aidl_config.bssidBlocklist.size();
1186     uint32_t i = 0;
1187     for (const auto& bssid : aidl_config.bssidBlocklist) {
1188         CHECK(bssid.data.size() == sizeof(legacy_hal::mac_addr));
1189         memcpy(legacy_config->blacklist_bssid[i++], bssid.data.data(), bssid.data.size());
1190     }
1191     legacy_config->num_whitelist_ssid = aidl_config.ssidAllowlist.size();
1192     i = 0;
1193     for (const auto& ssid : aidl_config.ssidAllowlist) {
1194         CHECK(ssid.data.size() <= sizeof(legacy_hal::ssid_t::ssid_str));
1195         legacy_config->whitelist_ssid[i].length = ssid.data.size();
1196         memcpy(legacy_config->whitelist_ssid[i].ssid_str, ssid.data.data(), ssid.data.size());
1197         i++;
1198     }
1199     return true;
1200 }
1201 
convertAidlRoamingStateToLegacy(StaRoamingState state)1202 legacy_hal::fw_roaming_state_t convertAidlRoamingStateToLegacy(StaRoamingState state) {
1203     switch (state) {
1204         case StaRoamingState::ENABLED:
1205             return legacy_hal::ROAMING_ENABLE;
1206         case StaRoamingState::DISABLED:
1207             return legacy_hal::ROAMING_DISABLE;
1208         case StaRoamingState::AGGRESSIVE:
1209             return legacy_hal::ROAMING_AGGRESSIVE;
1210     };
1211     CHECK(false);
1212 }
1213 
convertAidlNanMatchAlgToLegacy(NanMatchAlg type)1214 legacy_hal::NanMatchAlg convertAidlNanMatchAlgToLegacy(NanMatchAlg type) {
1215     switch (type) {
1216         case NanMatchAlg::MATCH_ONCE:
1217             return legacy_hal::NAN_MATCH_ALG_MATCH_ONCE;
1218         case NanMatchAlg::MATCH_CONTINUOUS:
1219             return legacy_hal::NAN_MATCH_ALG_MATCH_CONTINUOUS;
1220         case NanMatchAlg::MATCH_NEVER:
1221             return legacy_hal::NAN_MATCH_ALG_MATCH_NEVER;
1222     }
1223     CHECK(false);
1224 }
1225 
convertAidlNanPublishTypeToLegacy(NanPublishType type)1226 legacy_hal::NanPublishType convertAidlNanPublishTypeToLegacy(NanPublishType type) {
1227     switch (type) {
1228         case NanPublishType::UNSOLICITED:
1229             return legacy_hal::NAN_PUBLISH_TYPE_UNSOLICITED;
1230         case NanPublishType::SOLICITED:
1231             return legacy_hal::NAN_PUBLISH_TYPE_SOLICITED;
1232         case NanPublishType::UNSOLICITED_SOLICITED:
1233             return legacy_hal::NAN_PUBLISH_TYPE_UNSOLICITED_SOLICITED;
1234     }
1235     CHECK(false);
1236 }
1237 
convertAidlNanTxTypeToLegacy(NanTxType type)1238 legacy_hal::NanTxType convertAidlNanTxTypeToLegacy(NanTxType type) {
1239     switch (type) {
1240         case NanTxType::BROADCAST:
1241             return legacy_hal::NAN_TX_TYPE_BROADCAST;
1242         case NanTxType::UNICAST:
1243             return legacy_hal::NAN_TX_TYPE_UNICAST;
1244     }
1245     CHECK(false);
1246 }
1247 
convertAidlNanSubscribeTypeToLegacy(NanSubscribeType type)1248 legacy_hal::NanSubscribeType convertAidlNanSubscribeTypeToLegacy(NanSubscribeType type) {
1249     switch (type) {
1250         case NanSubscribeType::PASSIVE:
1251             return legacy_hal::NAN_SUBSCRIBE_TYPE_PASSIVE;
1252         case NanSubscribeType::ACTIVE:
1253             return legacy_hal::NAN_SUBSCRIBE_TYPE_ACTIVE;
1254     }
1255     CHECK(false);
1256 }
1257 
convertAidlNanSrfTypeToLegacy(NanSrfType type)1258 legacy_hal::NanSRFType convertAidlNanSrfTypeToLegacy(NanSrfType type) {
1259     switch (type) {
1260         case NanSrfType::BLOOM_FILTER:
1261             return legacy_hal::NAN_SRF_ATTR_BLOOM_FILTER;
1262         case NanSrfType::PARTIAL_MAC_ADDR:
1263             return legacy_hal::NAN_SRF_ATTR_PARTIAL_MAC_ADDR;
1264     }
1265     CHECK(false);
1266 }
1267 
convertAidlNanDataPathChannelCfgToLegacy(NanDataPathChannelCfg type)1268 legacy_hal::NanDataPathChannelCfg convertAidlNanDataPathChannelCfgToLegacy(
1269         NanDataPathChannelCfg type) {
1270     switch (type) {
1271         case NanDataPathChannelCfg::CHANNEL_NOT_REQUESTED:
1272             return legacy_hal::NAN_DP_CHANNEL_NOT_REQUESTED;
1273         case NanDataPathChannelCfg::REQUEST_CHANNEL_SETUP:
1274             return legacy_hal::NAN_DP_REQUEST_CHANNEL_SETUP;
1275         case NanDataPathChannelCfg::FORCE_CHANNEL_SETUP:
1276             return legacy_hal::NAN_DP_FORCE_CHANNEL_SETUP;
1277     }
1278     CHECK(false);
1279 }
1280 
convertAidlNanPairingRequestTypeToLegacy(NanPairingRequestType type)1281 legacy_hal::NanPairingRequestType convertAidlNanPairingRequestTypeToLegacy(
1282         NanPairingRequestType type) {
1283     switch (type) {
1284         case NanPairingRequestType::NAN_PAIRING_SETUP:
1285             return legacy_hal::NAN_PAIRING_SETUP;
1286         case NanPairingRequestType::NAN_PAIRING_VERIFICATION:
1287             return legacy_hal::NAN_PAIRING_VERIFICATION;
1288     }
1289     LOG(FATAL);
1290 }
1291 
convertLegacyNanPairingRequestTypeToAidl(legacy_hal::NanPairingRequestType type)1292 NanPairingRequestType convertLegacyNanPairingRequestTypeToAidl(
1293         legacy_hal::NanPairingRequestType type) {
1294     switch (type) {
1295         case legacy_hal::NAN_PAIRING_SETUP:
1296             return NanPairingRequestType::NAN_PAIRING_SETUP;
1297         case legacy_hal::NAN_PAIRING_VERIFICATION:
1298             return NanPairingRequestType::NAN_PAIRING_VERIFICATION;
1299     }
1300     LOG(FATAL);
1301 }
1302 
convertAidlAkmTypeToLegacy(NanPairingAkm type)1303 legacy_hal::NanAkm convertAidlAkmTypeToLegacy(NanPairingAkm type) {
1304     switch (type) {
1305         case NanPairingAkm::SAE:
1306             return legacy_hal::SAE;
1307         case NanPairingAkm::PASN:
1308             return legacy_hal::PASN;
1309     }
1310     LOG(FATAL);
1311 }
1312 
convertLegacyAkmTypeToAidl(legacy_hal::NanAkm type)1313 NanPairingAkm convertLegacyAkmTypeToAidl(legacy_hal::NanAkm type) {
1314     switch (type) {
1315         case legacy_hal::SAE:
1316             return NanPairingAkm::SAE;
1317         case legacy_hal::PASN:
1318             return NanPairingAkm::PASN;
1319     }
1320     LOG(FATAL);
1321 }
1322 
convertAidlBootstrappingMethodToLegacy(NanBootstrappingMethod type)1323 uint16_t convertAidlBootstrappingMethodToLegacy(NanBootstrappingMethod type) {
1324     switch (type) {
1325         case NanBootstrappingMethod::BOOTSTRAPPING_OPPORTUNISTIC_MASK:
1326             return NAN_PAIRING_BOOTSTRAPPING_OPPORTUNISTIC_MASK;
1327         case NanBootstrappingMethod::BOOTSTRAPPING_PIN_CODE_DISPLAY_MASK:
1328             return NAN_PAIRING_BOOTSTRAPPING_PIN_CODE_DISPLAY_MASK;
1329         case NanBootstrappingMethod::BOOTSTRAPPING_PASSPHRASE_DISPLAY_MASK:
1330             return NAN_PAIRING_BOOTSTRAPPING_PASSPHRASE_DISPLAY_MASK;
1331         case NanBootstrappingMethod::BOOTSTRAPPING_QR_DISPLAY_MASK:
1332             return NAN_PAIRING_BOOTSTRAPPING_QR_DISPLAY_MASK;
1333         case NanBootstrappingMethod::BOOTSTRAPPING_NFC_TAG_MASK:
1334             return NAN_PAIRING_BOOTSTRAPPING_NFC_TAG_MASK;
1335         case NanBootstrappingMethod::BOOTSTRAPPING_PIN_CODE_KEYPAD_MASK:
1336             return NAN_PAIRING_BOOTSTRAPPING_PIN_CODE_KEYPAD_MASK;
1337         case NanBootstrappingMethod::BOOTSTRAPPING_PASSPHRASE_KEYPAD_MASK:
1338             return NAN_PAIRING_BOOTSTRAPPING_PASSPHRASE_KEYPAD_MASK;
1339         case NanBootstrappingMethod::BOOTSTRAPPING_QR_SCAN_MASK:
1340             return NAN_PAIRING_BOOTSTRAPPING_QR_SCAN_MASK;
1341         case NanBootstrappingMethod::BOOTSTRAPPING_NFC_READER_MASK:
1342             return NAN_PAIRING_BOOTSTRAPPING_NFC_READER_MASK;
1343         case NanBootstrappingMethod::BOOTSTRAPPING_SERVICE_MANAGED_MASK:
1344             return NAN_PAIRING_BOOTSTRAPPING_SERVICE_MANAGED_MASK;
1345         case NanBootstrappingMethod::BOOTSTRAPPING_HANDSHAKE_SHIP_MASK:
1346             return NAN_PAIRING_BOOTSTRAPPING_HANDSHAKE_SHIP_MASK;
1347     }
1348     LOG(FATAL);
1349 }
1350 
convertLegacyBootstrappingMethodToAidl(uint16_t type)1351 NanBootstrappingMethod convertLegacyBootstrappingMethodToAidl(uint16_t type) {
1352     switch (type) {
1353         case NAN_PAIRING_BOOTSTRAPPING_OPPORTUNISTIC_MASK:
1354             return NanBootstrappingMethod::BOOTSTRAPPING_OPPORTUNISTIC_MASK;
1355         case NAN_PAIRING_BOOTSTRAPPING_PIN_CODE_DISPLAY_MASK:
1356             return NanBootstrappingMethod::BOOTSTRAPPING_PIN_CODE_DISPLAY_MASK;
1357         case NAN_PAIRING_BOOTSTRAPPING_PASSPHRASE_DISPLAY_MASK:
1358             return NanBootstrappingMethod::BOOTSTRAPPING_PASSPHRASE_DISPLAY_MASK;
1359         case NAN_PAIRING_BOOTSTRAPPING_QR_DISPLAY_MASK:
1360             return NanBootstrappingMethod::BOOTSTRAPPING_QR_DISPLAY_MASK;
1361         case NAN_PAIRING_BOOTSTRAPPING_NFC_TAG_MASK:
1362             return NanBootstrappingMethod::BOOTSTRAPPING_NFC_TAG_MASK;
1363         case NAN_PAIRING_BOOTSTRAPPING_PIN_CODE_KEYPAD_MASK:
1364             return NanBootstrappingMethod::BOOTSTRAPPING_PIN_CODE_KEYPAD_MASK;
1365         case NAN_PAIRING_BOOTSTRAPPING_PASSPHRASE_KEYPAD_MASK:
1366             return NanBootstrappingMethod::BOOTSTRAPPING_PASSPHRASE_KEYPAD_MASK;
1367         case NAN_PAIRING_BOOTSTRAPPING_QR_SCAN_MASK:
1368             return NanBootstrappingMethod::BOOTSTRAPPING_QR_SCAN_MASK;
1369         case NAN_PAIRING_BOOTSTRAPPING_NFC_READER_MASK:
1370             return NanBootstrappingMethod::BOOTSTRAPPING_NFC_READER_MASK;
1371         case NAN_PAIRING_BOOTSTRAPPING_SERVICE_MANAGED_MASK:
1372             return NanBootstrappingMethod::BOOTSTRAPPING_SERVICE_MANAGED_MASK;
1373         case NAN_PAIRING_BOOTSTRAPPING_HANDSHAKE_SHIP_MASK:
1374             return NanBootstrappingMethod::BOOTSTRAPPING_HANDSHAKE_SHIP_MASK;
1375     }
1376     LOG(FATAL);
1377     return {};
1378 }
1379 
covertAidlPairingConfigToLegacy(const NanPairingConfig & aidl_config,legacy_hal::NanPairingConfig * legacy_config)1380 bool covertAidlPairingConfigToLegacy(const NanPairingConfig& aidl_config,
1381                                      legacy_hal::NanPairingConfig* legacy_config) {
1382     if (!legacy_config) {
1383         LOG(ERROR) << "covertAidlPairingConfigToLegacy: legacy_config is null";
1384         return false;
1385     }
1386     legacy_config->enable_pairing_setup = aidl_config.enablePairingSetup ? 0x1 : 0x0;
1387     legacy_config->enable_pairing_cache = aidl_config.enablePairingCache ? 0x1 : 0x0;
1388     legacy_config->enable_pairing_verification = aidl_config.enablePairingVerification ? 0x1 : 0x0;
1389     legacy_config->supported_bootstrapping_methods = aidl_config.supportedBootstrappingMethods;
1390     return true;
1391 }
1392 
convertLegacyPairingConfigToAidl(const legacy_hal::NanPairingConfig & legacy_config,NanPairingConfig * aidl_config)1393 bool convertLegacyPairingConfigToAidl(const legacy_hal::NanPairingConfig& legacy_config,
1394                                       NanPairingConfig* aidl_config) {
1395     if (!aidl_config) {
1396         LOG(ERROR) << "convertLegacyPairingConfigToAidl: aidl_nira is null";
1397         return false;
1398     }
1399     *aidl_config = {};
1400     aidl_config->enablePairingSetup = legacy_config.enable_pairing_setup == 0x1;
1401     aidl_config->enablePairingCache = legacy_config.enable_pairing_cache == 0x1;
1402     aidl_config->enablePairingVerification = legacy_config.enable_pairing_verification == 0x1;
1403     aidl_config->supportedBootstrappingMethods = legacy_config.supported_bootstrapping_methods;
1404     return true;
1405 }
1406 
convertLegacyNiraToAidl(const legacy_hal::NanIdentityResolutionAttribute & legacy_nira,NanIdentityResolutionAttribute * aidl_nira)1407 bool convertLegacyNiraToAidl(const legacy_hal::NanIdentityResolutionAttribute& legacy_nira,
1408                              NanIdentityResolutionAttribute* aidl_nira) {
1409     if (!aidl_nira) {
1410         LOG(ERROR) << "convertLegacyNiraToAidl: aidl_nira is null";
1411         return false;
1412     }
1413     *aidl_nira = {};
1414     aidl_nira->nonce = std::array<uint8_t, 8>();
1415     std::copy(legacy_nira.nonce, legacy_nira.nonce + 8, std::begin(aidl_nira->nonce));
1416     aidl_nira->tag = std::array<uint8_t, 8>();
1417     std::copy(legacy_nira.tag, legacy_nira.tag + 8, std::begin(aidl_nira->tag));
1418     return true;
1419 }
1420 
convertLegacyNpsaToAidl(const legacy_hal::NpkSecurityAssociation & legacy_npsa,NpkSecurityAssociation * aidl_npsa)1421 bool convertLegacyNpsaToAidl(const legacy_hal::NpkSecurityAssociation& legacy_npsa,
1422                              NpkSecurityAssociation* aidl_npsa) {
1423     if (!aidl_npsa) {
1424         LOG(ERROR) << "convertLegacyNiraToAidl: aidl_nira is null";
1425         return false;
1426     }
1427     *aidl_npsa = {};
1428     aidl_npsa->peerNanIdentityKey = std::array<uint8_t, 16>();
1429     std::copy(legacy_npsa.peer_nan_identity_key, legacy_npsa.peer_nan_identity_key + 16,
1430               std::begin(aidl_npsa->peerNanIdentityKey));
1431     aidl_npsa->localNanIdentityKey = std::array<uint8_t, 16>();
1432     std::copy(legacy_npsa.local_nan_identity_key, legacy_npsa.local_nan_identity_key + 16,
1433               std::begin(aidl_npsa->localNanIdentityKey));
1434     aidl_npsa->npk = std::array<uint8_t, 32>();
1435     std::copy(legacy_npsa.npk.pmk, legacy_npsa.npk.pmk + 32, std::begin(aidl_npsa->npk));
1436     aidl_npsa->akm = convertLegacyAkmTypeToAidl(legacy_npsa.akm);
1437     aidl_npsa->cipherType = (NanCipherSuiteType)legacy_npsa.cipher_type;
1438     return true;
1439 }
1440 
convertLegacyNanStatusTypeToAidl(legacy_hal::NanStatusType type)1441 NanStatusCode convertLegacyNanStatusTypeToAidl(legacy_hal::NanStatusType type) {
1442     switch (type) {
1443         case legacy_hal::NAN_STATUS_SUCCESS:
1444             return NanStatusCode::SUCCESS;
1445         case legacy_hal::NAN_STATUS_INTERNAL_FAILURE:
1446             return NanStatusCode::INTERNAL_FAILURE;
1447         case legacy_hal::NAN_STATUS_PROTOCOL_FAILURE:
1448             return NanStatusCode::PROTOCOL_FAILURE;
1449         case legacy_hal::NAN_STATUS_INVALID_PUBLISH_SUBSCRIBE_ID:
1450             return NanStatusCode::INVALID_SESSION_ID;
1451         case legacy_hal::NAN_STATUS_NO_RESOURCE_AVAILABLE:
1452             return NanStatusCode::NO_RESOURCES_AVAILABLE;
1453         case legacy_hal::NAN_STATUS_INVALID_PARAM:
1454             return NanStatusCode::INVALID_ARGS;
1455         case legacy_hal::NAN_STATUS_INVALID_REQUESTOR_INSTANCE_ID:
1456             return NanStatusCode::INVALID_PEER_ID;
1457         case legacy_hal::NAN_STATUS_INVALID_NDP_ID:
1458             return NanStatusCode::INVALID_NDP_ID;
1459         case legacy_hal::NAN_STATUS_NAN_NOT_ALLOWED:
1460             return NanStatusCode::NAN_NOT_ALLOWED;
1461         case legacy_hal::NAN_STATUS_NO_OTA_ACK:
1462             return NanStatusCode::NO_OTA_ACK;
1463         case legacy_hal::NAN_STATUS_ALREADY_ENABLED:
1464             return NanStatusCode::ALREADY_ENABLED;
1465         case legacy_hal::NAN_STATUS_FOLLOWUP_QUEUE_FULL:
1466             return NanStatusCode::FOLLOWUP_TX_QUEUE_FULL;
1467         case legacy_hal::NAN_STATUS_UNSUPPORTED_CONCURRENCY_NAN_DISABLED:
1468             return NanStatusCode::UNSUPPORTED_CONCURRENCY_NAN_DISABLED;
1469         case legacy_hal::NAN_STATUS_INVALID_PAIRING_ID:
1470             return NanStatusCode::INVALID_PAIRING_ID;
1471         case legacy_hal::NAN_STATUS_INVALID_BOOTSTRAPPING_ID:
1472             return NanStatusCode::INVALID_BOOTSTRAPPING_ID;
1473         case legacy_hal::NAN_STATUS_REDUNDANT_REQUEST:
1474             return NanStatusCode::REDUNDANT_REQUEST;
1475         case legacy_hal::NAN_STATUS_NOT_SUPPORTED:
1476             return NanStatusCode::NOT_SUPPORTED;
1477         case legacy_hal::NAN_STATUS_NO_CONNECTION:
1478             return NanStatusCode::NO_CONNECTION;
1479     }
1480     CHECK(false);
1481 }
1482 
convertToNanStatus(legacy_hal::NanStatusType type,const char * str,size_t max_len,NanStatus * nanStatus)1483 void convertToNanStatus(legacy_hal::NanStatusType type, const char* str, size_t max_len,
1484                         NanStatus* nanStatus) {
1485     nanStatus->status = convertLegacyNanStatusTypeToAidl(type);
1486     nanStatus->description = safeConvertChar(str, max_len);
1487 }
1488 
convertAidlNanEnableRequestToLegacy(const NanEnableRequest & aidl_request1,const NanConfigRequestSupplemental & aidl_request2,legacy_hal::NanEnableRequest * legacy_request)1489 bool convertAidlNanEnableRequestToLegacy(const NanEnableRequest& aidl_request1,
1490                                          const NanConfigRequestSupplemental& aidl_request2,
1491                                          legacy_hal::NanEnableRequest* legacy_request) {
1492     if (!legacy_request) {
1493         LOG(ERROR) << "convertAidlNanEnableRequestToLegacy: null legacy_request";
1494         return false;
1495     }
1496     *legacy_request = {};
1497 
1498     legacy_request->config_2dot4g_support = 1;
1499     legacy_request->support_2dot4g_val =
1500             aidl_request1.operateInBand[(size_t)NanBandIndex::NAN_BAND_24GHZ];
1501     legacy_request->config_support_5g = 1;
1502     legacy_request->support_5g_val =
1503             aidl_request1.operateInBand[(size_t)NanBandIndex::NAN_BAND_5GHZ];
1504     legacy_request->config_hop_count_limit = 1;
1505     legacy_request->hop_count_limit_val = aidl_request1.hopCountMax;
1506     legacy_request->master_pref = aidl_request1.configParams.masterPref;
1507     legacy_request->discovery_indication_cfg = 0;
1508     legacy_request->discovery_indication_cfg |=
1509             aidl_request1.configParams.disableDiscoveryAddressChangeIndication ? 0x1 : 0x0;
1510     legacy_request->discovery_indication_cfg |=
1511             aidl_request1.configParams.disableStartedClusterIndication ? 0x2 : 0x0;
1512     legacy_request->discovery_indication_cfg |=
1513             aidl_request1.configParams.disableJoinedClusterIndication ? 0x4 : 0x0;
1514     legacy_request->config_sid_beacon = 1;
1515     if (aidl_request1.configParams.numberOfPublishServiceIdsInBeacon < 0) {
1516         LOG(ERROR) << "convertAidlNanEnableRequestToLegacy: "
1517                       "numberOfPublishServiceIdsInBeacon < 0";
1518         return false;
1519     }
1520     legacy_request->sid_beacon_val =
1521             (aidl_request1.configParams.includePublishServiceIdsInBeacon ? 0x1 : 0x0) |
1522             (aidl_request1.configParams.numberOfPublishServiceIdsInBeacon << 1);
1523     legacy_request->config_subscribe_sid_beacon = 1;
1524     if (aidl_request1.configParams.numberOfSubscribeServiceIdsInBeacon < 0) {
1525         LOG(ERROR) << "convertAidlNanEnableRequestToLegacy: "
1526                       "numberOfSubscribeServiceIdsInBeacon < 0";
1527         return false;
1528     }
1529     legacy_request->subscribe_sid_beacon_val =
1530             (aidl_request1.configParams.includeSubscribeServiceIdsInBeacon ? 0x1 : 0x0) |
1531             (aidl_request1.configParams.numberOfSubscribeServiceIdsInBeacon << 1);
1532     legacy_request->config_rssi_window_size = 1;
1533     legacy_request->rssi_window_size_val = aidl_request1.configParams.rssiWindowSize;
1534     legacy_request->config_disc_mac_addr_randomization = 1;
1535     legacy_request->disc_mac_addr_rand_interval_sec =
1536             aidl_request1.configParams.macAddressRandomizationIntervalSec;
1537     legacy_request->config_2dot4g_rssi_close = 1;
1538     if (aidl_request1.configParams.bandSpecificConfig.size() != 3) {
1539         LOG(ERROR) << "convertAidlNanEnableRequestToLegacy: "
1540                       "bandSpecificConfig.size() != 3";
1541         return false;
1542     }
1543     legacy_request->rssi_close_2dot4g_val =
1544             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ]
1545                     .rssiClose;
1546     legacy_request->config_2dot4g_rssi_middle = 1;
1547     legacy_request->rssi_middle_2dot4g_val =
1548             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ]
1549                     .rssiMiddle;
1550     legacy_request->config_2dot4g_rssi_proximity = 1;
1551     legacy_request->rssi_proximity_2dot4g_val =
1552             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ]
1553                     .rssiCloseProximity;
1554     legacy_request->config_scan_params = 1;
1555     legacy_request->scan_params_val.dwell_time[legacy_hal::NAN_CHANNEL_24G_BAND] =
1556             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ]
1557                     .dwellTimeMs;
1558     legacy_request->scan_params_val.scan_period[legacy_hal::NAN_CHANNEL_24G_BAND] =
1559             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ]
1560                     .scanPeriodSec;
1561     legacy_request->config_dw.config_2dot4g_dw_band =
1562             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ]
1563                     .validDiscoveryWindowIntervalVal;
1564     legacy_request->config_dw.dw_2dot4g_interval_val =
1565             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ]
1566                     .discoveryWindowIntervalVal;
1567     legacy_request->config_5g_rssi_close = 1;
1568     legacy_request->rssi_close_5g_val =
1569             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1570                     .rssiClose;
1571     legacy_request->config_5g_rssi_middle = 1;
1572     legacy_request->rssi_middle_5g_val =
1573             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1574                     .rssiMiddle;
1575     legacy_request->config_5g_rssi_close_proximity = 1;
1576     legacy_request->rssi_close_proximity_5g_val =
1577             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1578                     .rssiCloseProximity;
1579     legacy_request->scan_params_val.dwell_time[legacy_hal::NAN_CHANNEL_5G_BAND_LOW] =
1580             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1581                     .dwellTimeMs;
1582     legacy_request->scan_params_val.scan_period[legacy_hal::NAN_CHANNEL_5G_BAND_LOW] =
1583             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1584                     .scanPeriodSec;
1585     legacy_request->scan_params_val.dwell_time[legacy_hal::NAN_CHANNEL_5G_BAND_HIGH] =
1586             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1587                     .dwellTimeMs;
1588     legacy_request->scan_params_val.scan_period[legacy_hal::NAN_CHANNEL_5G_BAND_HIGH] =
1589             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1590                     .scanPeriodSec;
1591     legacy_request->config_dw.config_5g_dw_band =
1592             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1593                     .validDiscoveryWindowIntervalVal;
1594     legacy_request->config_dw.dw_5g_interval_val =
1595             aidl_request1.configParams.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1596                     .discoveryWindowIntervalVal;
1597     if (aidl_request1.debugConfigs.validClusterIdVals) {
1598         legacy_request->cluster_low = aidl_request1.debugConfigs.clusterIdBottomRangeVal;
1599         legacy_request->cluster_high = aidl_request1.debugConfigs.clusterIdTopRangeVal;
1600     } else {  // need 'else' since not configurable in legacy HAL
1601         legacy_request->cluster_low = 0x0000;
1602         legacy_request->cluster_high = 0xFFFF;
1603     }
1604     legacy_request->config_intf_addr = aidl_request1.debugConfigs.validIntfAddrVal;
1605     memcpy(legacy_request->intf_addr_val, aidl_request1.debugConfigs.intfAddrVal.data(), 6);
1606     legacy_request->config_oui = aidl_request1.debugConfigs.validOuiVal;
1607     legacy_request->oui_val = aidl_request1.debugConfigs.ouiVal;
1608     legacy_request->config_random_factor_force =
1609             aidl_request1.debugConfigs.validRandomFactorForceVal;
1610     legacy_request->random_factor_force_val = aidl_request1.debugConfigs.randomFactorForceVal;
1611     legacy_request->config_hop_count_force = aidl_request1.debugConfigs.validHopCountForceVal;
1612     legacy_request->hop_count_force_val = aidl_request1.debugConfigs.hopCountForceVal;
1613     legacy_request->config_24g_channel = aidl_request1.debugConfigs.validDiscoveryChannelVal;
1614     legacy_request->channel_24g_val =
1615             aidl_request1.debugConfigs.discoveryChannelMhzVal[(size_t)NanBandIndex::NAN_BAND_24GHZ];
1616     legacy_request->config_5g_channel = aidl_request1.debugConfigs.validDiscoveryChannelVal;
1617     legacy_request->channel_5g_val =
1618             aidl_request1.debugConfigs.discoveryChannelMhzVal[(size_t)NanBandIndex::NAN_BAND_5GHZ];
1619     legacy_request->config_2dot4g_beacons = aidl_request1.debugConfigs.validUseBeaconsInBandVal;
1620     legacy_request->beacon_2dot4g_val =
1621             aidl_request1.debugConfigs.useBeaconsInBandVal[(size_t)NanBandIndex::NAN_BAND_24GHZ];
1622     legacy_request->config_5g_beacons = aidl_request1.debugConfigs.validUseBeaconsInBandVal;
1623     legacy_request->beacon_5g_val =
1624             aidl_request1.debugConfigs.useBeaconsInBandVal[(size_t)NanBandIndex::NAN_BAND_5GHZ];
1625     legacy_request->config_2dot4g_sdf = aidl_request1.debugConfigs.validUseSdfInBandVal;
1626     legacy_request->sdf_2dot4g_val =
1627             aidl_request1.debugConfigs.useSdfInBandVal[(size_t)NanBandIndex::NAN_BAND_24GHZ];
1628     legacy_request->config_5g_sdf = aidl_request1.debugConfigs.validUseSdfInBandVal;
1629     legacy_request->sdf_5g_val =
1630             aidl_request1.debugConfigs.useSdfInBandVal[(size_t)NanBandIndex::NAN_BAND_5GHZ];
1631 
1632     legacy_request->config_discovery_beacon_int = 1;
1633     legacy_request->discovery_beacon_interval = aidl_request2.discoveryBeaconIntervalMs;
1634     legacy_request->config_nss = 1;
1635     legacy_request->nss = aidl_request2.numberOfSpatialStreamsInDiscovery;
1636     legacy_request->config_dw_early_termination = 1;
1637     legacy_request->enable_dw_termination = aidl_request2.enableDiscoveryWindowEarlyTermination;
1638     legacy_request->config_enable_ranging = 1;
1639     legacy_request->enable_ranging = aidl_request2.enableRanging;
1640 
1641     legacy_request->config_enable_instant_mode = 1;
1642     legacy_request->enable_instant_mode = aidl_request2.enableInstantCommunicationMode;
1643     legacy_request->config_instant_mode_channel = 1;
1644     legacy_request->instant_mode_channel = aidl_request2.instantModeChannel;
1645 
1646     return true;
1647 }
1648 
convertAidlNanConfigRequestToLegacy(const NanConfigRequest & aidl_request1,const NanConfigRequestSupplemental & aidl_request2,legacy_hal::NanConfigRequest * legacy_request)1649 bool convertAidlNanConfigRequestToLegacy(const NanConfigRequest& aidl_request1,
1650                                          const NanConfigRequestSupplemental& aidl_request2,
1651                                          legacy_hal::NanConfigRequest* legacy_request) {
1652     if (!legacy_request) {
1653         LOG(ERROR) << "convertAidlNanConfigRequestToLegacy: null legacy_request";
1654         return false;
1655     }
1656     *legacy_request = {};
1657 
1658     legacy_request->master_pref = aidl_request1.masterPref;
1659     legacy_request->discovery_indication_cfg = 0;
1660     legacy_request->discovery_indication_cfg |=
1661             aidl_request1.disableDiscoveryAddressChangeIndication ? 0x1 : 0x0;
1662     legacy_request->discovery_indication_cfg |=
1663             aidl_request1.disableStartedClusterIndication ? 0x2 : 0x0;
1664     legacy_request->discovery_indication_cfg |=
1665             aidl_request1.disableJoinedClusterIndication ? 0x4 : 0x0;
1666     legacy_request->config_sid_beacon = 1;
1667     if (aidl_request1.numberOfPublishServiceIdsInBeacon < 0) {
1668         LOG(ERROR) << "convertAidlNanConfigRequestToLegacy: "
1669                       "numberOfPublishServiceIdsInBeacon < 0";
1670         return false;
1671     }
1672     legacy_request->sid_beacon = (aidl_request1.includePublishServiceIdsInBeacon ? 0x1 : 0x0) |
1673                                  (aidl_request1.numberOfPublishServiceIdsInBeacon << 1);
1674     legacy_request->config_subscribe_sid_beacon = 1;
1675     if (aidl_request1.numberOfSubscribeServiceIdsInBeacon < 0) {
1676         LOG(ERROR) << "convertAidlNanConfigRequestToLegacy: "
1677                       "numberOfSubscribeServiceIdsInBeacon < 0";
1678         return false;
1679     }
1680     legacy_request->subscribe_sid_beacon_val =
1681             (aidl_request1.includeSubscribeServiceIdsInBeacon ? 0x1 : 0x0) |
1682             (aidl_request1.numberOfSubscribeServiceIdsInBeacon << 1);
1683     legacy_request->config_rssi_window_size = 1;
1684     legacy_request->rssi_window_size_val = aidl_request1.rssiWindowSize;
1685     legacy_request->config_disc_mac_addr_randomization = 1;
1686     legacy_request->disc_mac_addr_rand_interval_sec =
1687             aidl_request1.macAddressRandomizationIntervalSec;
1688 
1689     legacy_request->config_scan_params = 1;
1690     legacy_request->scan_params_val.dwell_time[legacy_hal::NAN_CHANNEL_24G_BAND] =
1691             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ].dwellTimeMs;
1692     legacy_request->scan_params_val.scan_period[legacy_hal::NAN_CHANNEL_24G_BAND] =
1693             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ].scanPeriodSec;
1694     legacy_request->config_dw.config_2dot4g_dw_band =
1695             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ]
1696                     .validDiscoveryWindowIntervalVal;
1697     legacy_request->config_dw.dw_2dot4g_interval_val =
1698             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_24GHZ]
1699                     .discoveryWindowIntervalVal;
1700 
1701     legacy_request->config_5g_rssi_close_proximity = 1;
1702     legacy_request->rssi_close_proximity_5g_val =
1703             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1704                     .rssiCloseProximity;
1705     legacy_request->scan_params_val.dwell_time[legacy_hal::NAN_CHANNEL_5G_BAND_LOW] =
1706             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ].dwellTimeMs;
1707     legacy_request->scan_params_val.scan_period[legacy_hal::NAN_CHANNEL_5G_BAND_LOW] =
1708             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ].scanPeriodSec;
1709     legacy_request->scan_params_val.dwell_time[legacy_hal::NAN_CHANNEL_5G_BAND_HIGH] =
1710             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ].dwellTimeMs;
1711     legacy_request->scan_params_val.scan_period[legacy_hal::NAN_CHANNEL_5G_BAND_HIGH] =
1712             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ].scanPeriodSec;
1713     legacy_request->config_dw.config_5g_dw_band =
1714             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1715                     .validDiscoveryWindowIntervalVal;
1716     legacy_request->config_dw.dw_5g_interval_val =
1717             aidl_request1.bandSpecificConfig[(size_t)NanBandIndex::NAN_BAND_5GHZ]
1718                     .discoveryWindowIntervalVal;
1719 
1720     legacy_request->config_discovery_beacon_int = 1;
1721     legacy_request->discovery_beacon_interval = aidl_request2.discoveryBeaconIntervalMs;
1722     legacy_request->config_nss = 1;
1723     legacy_request->nss = aidl_request2.numberOfSpatialStreamsInDiscovery;
1724     legacy_request->config_dw_early_termination = 1;
1725     legacy_request->enable_dw_termination = aidl_request2.enableDiscoveryWindowEarlyTermination;
1726     legacy_request->config_enable_ranging = 1;
1727     legacy_request->enable_ranging = aidl_request2.enableRanging;
1728 
1729     legacy_request->config_enable_instant_mode = 1;
1730     legacy_request->enable_instant_mode = aidl_request2.enableInstantCommunicationMode;
1731     legacy_request->config_instant_mode_channel = 1;
1732     legacy_request->instant_mode_channel = aidl_request2.instantModeChannel;
1733     legacy_request->config_cluster_id = 1;
1734     legacy_request->cluster_id_val = aidl_request2.clusterId;
1735 
1736     return true;
1737 }
1738 
convertAidlNanPublishRequestToLegacy(const NanPublishRequest & aidl_request,legacy_hal::NanPublishRequest * legacy_request)1739 bool convertAidlNanPublishRequestToLegacy(const NanPublishRequest& aidl_request,
1740                                           legacy_hal::NanPublishRequest* legacy_request) {
1741     if (!legacy_request) {
1742         LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: null legacy_request";
1743         return false;
1744     }
1745     *legacy_request = {};
1746 
1747     legacy_request->publish_id = static_cast<uint8_t>(aidl_request.baseConfigs.sessionId);
1748     legacy_request->ttl = aidl_request.baseConfigs.ttlSec;
1749     legacy_request->period = aidl_request.baseConfigs.discoveryWindowPeriod;
1750     legacy_request->publish_count = aidl_request.baseConfigs.discoveryCount;
1751     legacy_request->service_name_len = aidl_request.baseConfigs.serviceName.size();
1752     if (legacy_request->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
1753         LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: service_name_len "
1754                       "too large";
1755         return false;
1756     }
1757     memcpy(legacy_request->service_name, aidl_request.baseConfigs.serviceName.data(),
1758            legacy_request->service_name_len);
1759     legacy_request->publish_match_indicator =
1760             convertAidlNanMatchAlgToLegacy(aidl_request.baseConfigs.discoveryMatchIndicator);
1761     legacy_request->service_specific_info_len = aidl_request.baseConfigs.serviceSpecificInfo.size();
1762     if (legacy_request->service_specific_info_len > NAN_MAX_SERVICE_SPECIFIC_INFO_LEN) {
1763         LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: "
1764                       "service_specific_info_len too large";
1765         return false;
1766     }
1767     memcpy(legacy_request->service_specific_info,
1768            aidl_request.baseConfigs.serviceSpecificInfo.data(),
1769            legacy_request->service_specific_info_len);
1770     legacy_request->sdea_service_specific_info_len =
1771             aidl_request.baseConfigs.extendedServiceSpecificInfo.size();
1772     if (legacy_request->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
1773         LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: "
1774                       "sdea_service_specific_info_len too large";
1775         return false;
1776     }
1777     memcpy(legacy_request->sdea_service_specific_info,
1778            aidl_request.baseConfigs.extendedServiceSpecificInfo.data(),
1779            legacy_request->sdea_service_specific_info_len);
1780     legacy_request->rx_match_filter_len = aidl_request.baseConfigs.rxMatchFilter.size();
1781     if (legacy_request->rx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
1782         LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: "
1783                       "rx_match_filter_len too large";
1784         return false;
1785     }
1786     memcpy(legacy_request->rx_match_filter, aidl_request.baseConfigs.rxMatchFilter.data(),
1787            legacy_request->rx_match_filter_len);
1788     legacy_request->tx_match_filter_len = aidl_request.baseConfigs.txMatchFilter.size();
1789     if (legacy_request->tx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
1790         LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: "
1791                       "tx_match_filter_len too large";
1792         return false;
1793     }
1794     memcpy(legacy_request->tx_match_filter, aidl_request.baseConfigs.txMatchFilter.data(),
1795            legacy_request->tx_match_filter_len);
1796     legacy_request->rssi_threshold_flag = aidl_request.baseConfigs.useRssiThreshold;
1797     legacy_request->recv_indication_cfg = 0;
1798     legacy_request->recv_indication_cfg |=
1799             aidl_request.baseConfigs.disableDiscoveryTerminationIndication ? 0x1 : 0x0;
1800     legacy_request->recv_indication_cfg |=
1801             aidl_request.baseConfigs.disableMatchExpirationIndication ? 0x2 : 0x0;
1802     legacy_request->recv_indication_cfg |=
1803             aidl_request.baseConfigs.disableFollowupReceivedIndication ? 0x4 : 0x0;
1804     legacy_request->recv_indication_cfg |= 0x8;
1805     legacy_request->cipher_type = (unsigned int)aidl_request.baseConfigs.securityConfig.cipherType;
1806 
1807     legacy_request->scid_len = aidl_request.baseConfigs.securityConfig.scid.size();
1808     if (legacy_request->scid_len > NAN_MAX_SCID_BUF_LEN) {
1809         LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: scid_len too large";
1810         return false;
1811     }
1812     memcpy(legacy_request->scid, aidl_request.baseConfigs.securityConfig.scid.data(),
1813            legacy_request->scid_len);
1814 
1815     if (aidl_request.baseConfigs.securityConfig.securityType == NanDataPathSecurityType::PMK) {
1816         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PMK;
1817         legacy_request->key_info.body.pmk_info.pmk_len =
1818                 aidl_request.baseConfigs.securityConfig.pmk.size();
1819         if (legacy_request->key_info.body.pmk_info.pmk_len != NAN_PMK_INFO_LEN) {
1820             LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: invalid pmk_len";
1821             return false;
1822         }
1823         memcpy(legacy_request->key_info.body.pmk_info.pmk,
1824                aidl_request.baseConfigs.securityConfig.pmk.data(),
1825                legacy_request->key_info.body.pmk_info.pmk_len);
1826     }
1827     if (aidl_request.baseConfigs.securityConfig.securityType ==
1828         NanDataPathSecurityType::PASSPHRASE) {
1829         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PASSPHRASE;
1830         legacy_request->key_info.body.passphrase_info.passphrase_len =
1831                 aidl_request.baseConfigs.securityConfig.passphrase.size();
1832         if (legacy_request->key_info.body.passphrase_info.passphrase_len <
1833             NAN_SECURITY_MIN_PASSPHRASE_LEN) {
1834             LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: "
1835                           "passphrase_len too small";
1836             return false;
1837         }
1838         if (legacy_request->key_info.body.passphrase_info.passphrase_len >
1839             NAN_SECURITY_MAX_PASSPHRASE_LEN) {
1840             LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: "
1841                           "passphrase_len too large";
1842             return false;
1843         }
1844         memcpy(legacy_request->key_info.body.passphrase_info.passphrase,
1845                aidl_request.baseConfigs.securityConfig.passphrase.data(),
1846                legacy_request->key_info.body.passphrase_info.passphrase_len);
1847     }
1848     legacy_request->sdea_params.security_cfg =
1849             (aidl_request.baseConfigs.securityConfig.securityType != NanDataPathSecurityType::OPEN)
1850                     ? legacy_hal::NAN_DP_CONFIG_SECURITY
1851                     : legacy_hal::NAN_DP_CONFIG_NO_SECURITY;
1852 
1853     legacy_request->sdea_params.ranging_state = aidl_request.baseConfigs.rangingRequired
1854                                                         ? legacy_hal::NAN_RANGING_ENABLE
1855                                                         : legacy_hal::NAN_RANGING_DISABLE;
1856     legacy_request->ranging_cfg.ranging_interval_msec = aidl_request.baseConfigs.rangingIntervalMs;
1857     legacy_request->ranging_cfg.config_ranging_indications =
1858             aidl_request.baseConfigs.configRangingIndications;
1859     legacy_request->ranging_cfg.distance_ingress_mm =
1860             aidl_request.baseConfigs.distanceIngressCm * 10;
1861     legacy_request->ranging_cfg.distance_egress_mm = aidl_request.baseConfigs.distanceEgressCm * 10;
1862     legacy_request->ranging_auto_response = aidl_request.baseConfigs.rangingRequired
1863                                                     ? legacy_hal::NAN_RANGING_AUTO_RESPONSE_ENABLE
1864                                                     : legacy_hal::NAN_RANGING_AUTO_RESPONSE_DISABLE;
1865     legacy_request->sdea_params.range_report = legacy_hal::NAN_DISABLE_RANGE_REPORT;
1866     legacy_request->publish_type = convertAidlNanPublishTypeToLegacy(aidl_request.publishType);
1867     legacy_request->tx_type = convertAidlNanTxTypeToLegacy(aidl_request.txType);
1868     legacy_request->service_responder_policy = aidl_request.autoAcceptDataPathRequests
1869                                                        ? legacy_hal::NAN_SERVICE_ACCEPT_POLICY_ALL
1870                                                        : legacy_hal::NAN_SERVICE_ACCEPT_POLICY_NONE;
1871     memcpy(legacy_request->nan_identity_key, aidl_request.identityKey.data(), NAN_IDENTITY_KEY_LEN);
1872     if (!covertAidlPairingConfigToLegacy(aidl_request.pairingConfig,
1873                                          &legacy_request->nan_pairing_config)) {
1874         LOG(ERROR) << "convertAidlNanPublishRequestToLegacy: invalid pairing config";
1875         return false;
1876     }
1877     legacy_request->enable_suspendability = aidl_request.baseConfigs.enableSessionSuspendability;
1878 
1879     return true;
1880 }
1881 
convertAidlNanSubscribeRequestToLegacy(const NanSubscribeRequest & aidl_request,legacy_hal::NanSubscribeRequest * legacy_request)1882 bool convertAidlNanSubscribeRequestToLegacy(const NanSubscribeRequest& aidl_request,
1883                                             legacy_hal::NanSubscribeRequest* legacy_request) {
1884     if (!legacy_request) {
1885         LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: legacy_request is null";
1886         return false;
1887     }
1888     *legacy_request = {};
1889 
1890     legacy_request->subscribe_id = static_cast<uint8_t>(aidl_request.baseConfigs.sessionId);
1891     legacy_request->ttl = aidl_request.baseConfigs.ttlSec;
1892     legacy_request->period = aidl_request.baseConfigs.discoveryWindowPeriod;
1893     legacy_request->subscribe_count = aidl_request.baseConfigs.discoveryCount;
1894     legacy_request->service_name_len = aidl_request.baseConfigs.serviceName.size();
1895     if (legacy_request->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
1896         LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: "
1897                       "service_name_len too large";
1898         return false;
1899     }
1900     memcpy(legacy_request->service_name, aidl_request.baseConfigs.serviceName.data(),
1901            legacy_request->service_name_len);
1902     legacy_request->subscribe_match_indicator =
1903             convertAidlNanMatchAlgToLegacy(aidl_request.baseConfigs.discoveryMatchIndicator);
1904     legacy_request->service_specific_info_len = aidl_request.baseConfigs.serviceSpecificInfo.size();
1905     if (legacy_request->service_specific_info_len > NAN_MAX_SERVICE_SPECIFIC_INFO_LEN) {
1906         LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: "
1907                       "service_specific_info_len too large";
1908         return false;
1909     }
1910     memcpy(legacy_request->service_specific_info,
1911            aidl_request.baseConfigs.serviceSpecificInfo.data(),
1912            legacy_request->service_specific_info_len);
1913     legacy_request->sdea_service_specific_info_len =
1914             aidl_request.baseConfigs.extendedServiceSpecificInfo.size();
1915     if (legacy_request->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
1916         LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: "
1917                       "sdea_service_specific_info_len too large";
1918         return false;
1919     }
1920     memcpy(legacy_request->sdea_service_specific_info,
1921            aidl_request.baseConfigs.extendedServiceSpecificInfo.data(),
1922            legacy_request->sdea_service_specific_info_len);
1923     legacy_request->rx_match_filter_len = aidl_request.baseConfigs.rxMatchFilter.size();
1924     if (legacy_request->rx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
1925         LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: "
1926                       "rx_match_filter_len too large";
1927         return false;
1928     }
1929     memcpy(legacy_request->rx_match_filter, aidl_request.baseConfigs.rxMatchFilter.data(),
1930            legacy_request->rx_match_filter_len);
1931     legacy_request->tx_match_filter_len = aidl_request.baseConfigs.txMatchFilter.size();
1932     if (legacy_request->tx_match_filter_len > NAN_MAX_MATCH_FILTER_LEN) {
1933         LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: "
1934                       "tx_match_filter_len too large";
1935         return false;
1936     }
1937     memcpy(legacy_request->tx_match_filter, aidl_request.baseConfigs.txMatchFilter.data(),
1938            legacy_request->tx_match_filter_len);
1939     legacy_request->rssi_threshold_flag = aidl_request.baseConfigs.useRssiThreshold;
1940     legacy_request->recv_indication_cfg = 0;
1941     legacy_request->recv_indication_cfg |=
1942             aidl_request.baseConfigs.disableDiscoveryTerminationIndication ? 0x1 : 0x0;
1943     legacy_request->recv_indication_cfg |=
1944             aidl_request.baseConfigs.disableMatchExpirationIndication ? 0x2 : 0x0;
1945     legacy_request->recv_indication_cfg |=
1946             aidl_request.baseConfigs.disableFollowupReceivedIndication ? 0x4 : 0x0;
1947     legacy_request->cipher_type = (unsigned int)aidl_request.baseConfigs.securityConfig.cipherType;
1948     if (aidl_request.baseConfigs.securityConfig.securityType == NanDataPathSecurityType::PMK) {
1949         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PMK;
1950         legacy_request->key_info.body.pmk_info.pmk_len =
1951                 aidl_request.baseConfigs.securityConfig.pmk.size();
1952         if (legacy_request->key_info.body.pmk_info.pmk_len != NAN_PMK_INFO_LEN) {
1953             LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: invalid pmk_len";
1954             return false;
1955         }
1956         memcpy(legacy_request->key_info.body.pmk_info.pmk,
1957                aidl_request.baseConfigs.securityConfig.pmk.data(),
1958                legacy_request->key_info.body.pmk_info.pmk_len);
1959     }
1960     if (aidl_request.baseConfigs.securityConfig.securityType ==
1961         NanDataPathSecurityType::PASSPHRASE) {
1962         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PASSPHRASE;
1963         legacy_request->key_info.body.passphrase_info.passphrase_len =
1964                 aidl_request.baseConfigs.securityConfig.passphrase.size();
1965         if (legacy_request->key_info.body.passphrase_info.passphrase_len <
1966             NAN_SECURITY_MIN_PASSPHRASE_LEN) {
1967             LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: "
1968                           "passphrase_len too small";
1969             return false;
1970         }
1971         if (legacy_request->key_info.body.passphrase_info.passphrase_len >
1972             NAN_SECURITY_MAX_PASSPHRASE_LEN) {
1973             LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: "
1974                           "passphrase_len too large";
1975             return false;
1976         }
1977         memcpy(legacy_request->key_info.body.passphrase_info.passphrase,
1978                aidl_request.baseConfigs.securityConfig.passphrase.data(),
1979                legacy_request->key_info.body.passphrase_info.passphrase_len);
1980     }
1981     legacy_request->sdea_params.security_cfg =
1982             (aidl_request.baseConfigs.securityConfig.securityType != NanDataPathSecurityType::OPEN)
1983                     ? legacy_hal::NAN_DP_CONFIG_SECURITY
1984                     : legacy_hal::NAN_DP_CONFIG_NO_SECURITY;
1985     legacy_request->sdea_params.ranging_state = aidl_request.baseConfigs.rangingRequired
1986                                                         ? legacy_hal::NAN_RANGING_ENABLE
1987                                                         : legacy_hal::NAN_RANGING_DISABLE;
1988     legacy_request->ranging_cfg.ranging_interval_msec = aidl_request.baseConfigs.rangingIntervalMs;
1989     legacy_request->ranging_cfg.config_ranging_indications =
1990             aidl_request.baseConfigs.configRangingIndications;
1991     legacy_request->ranging_cfg.distance_ingress_mm =
1992             aidl_request.baseConfigs.distanceIngressCm * 10;
1993     legacy_request->ranging_cfg.distance_egress_mm = aidl_request.baseConfigs.distanceEgressCm * 10;
1994     legacy_request->ranging_auto_response = aidl_request.baseConfigs.rangingRequired
1995                                                     ? legacy_hal::NAN_RANGING_AUTO_RESPONSE_ENABLE
1996                                                     : legacy_hal::NAN_RANGING_AUTO_RESPONSE_DISABLE;
1997     legacy_request->sdea_params.range_report = legacy_hal::NAN_DISABLE_RANGE_REPORT;
1998     legacy_request->subscribe_type =
1999             convertAidlNanSubscribeTypeToLegacy(aidl_request.subscribeType);
2000     legacy_request->serviceResponseFilter = convertAidlNanSrfTypeToLegacy(aidl_request.srfType);
2001     legacy_request->serviceResponseInclude = aidl_request.srfRespondIfInAddressSet
2002                                                      ? legacy_hal::NAN_SRF_INCLUDE_RESPOND
2003                                                      : legacy_hal::NAN_SRF_INCLUDE_DO_NOT_RESPOND;
2004     legacy_request->useServiceResponseFilter =
2005             aidl_request.shouldUseSrf ? legacy_hal::NAN_USE_SRF : legacy_hal::NAN_DO_NOT_USE_SRF;
2006     legacy_request->ssiRequiredForMatchIndication =
2007             aidl_request.isSsiRequiredForMatch ? legacy_hal::NAN_SSI_REQUIRED_IN_MATCH_IND
2008                                                : legacy_hal::NAN_SSI_NOT_REQUIRED_IN_MATCH_IND;
2009     legacy_request->num_intf_addr_present = aidl_request.intfAddr.size();
2010     if (legacy_request->num_intf_addr_present > NAN_MAX_SUBSCRIBE_MAX_ADDRESS) {
2011         LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: "
2012                       "num_intf_addr_present - too many";
2013         return false;
2014     }
2015     for (int i = 0; i < legacy_request->num_intf_addr_present; i++) {
2016         memcpy(legacy_request->intf_addr[i], aidl_request.intfAddr[i].data.data(), 6);
2017     }
2018     memcpy(legacy_request->nan_identity_key, aidl_request.identityKey.data(), NAN_IDENTITY_KEY_LEN);
2019     if (!covertAidlPairingConfigToLegacy(aidl_request.pairingConfig,
2020                                          &legacy_request->nan_pairing_config)) {
2021         LOG(ERROR) << "convertAidlNanSubscribeRequestToLegacy: invalid pairing config";
2022         return false;
2023     }
2024     legacy_request->enable_suspendability = aidl_request.baseConfigs.enableSessionSuspendability;
2025 
2026     return true;
2027 }
2028 
convertAidlNanTransmitFollowupRequestToLegacy(const NanTransmitFollowupRequest & aidl_request,legacy_hal::NanTransmitFollowupRequest * legacy_request)2029 bool convertAidlNanTransmitFollowupRequestToLegacy(
2030         const NanTransmitFollowupRequest& aidl_request,
2031         legacy_hal::NanTransmitFollowupRequest* legacy_request) {
2032     if (!legacy_request) {
2033         LOG(ERROR) << "convertAidlNanTransmitFollowupRequestToLegacy: "
2034                       "legacy_request is null";
2035         return false;
2036     }
2037     *legacy_request = {};
2038 
2039     legacy_request->publish_subscribe_id = static_cast<uint8_t>(aidl_request.discoverySessionId);
2040     legacy_request->requestor_instance_id = aidl_request.peerId;
2041     memcpy(legacy_request->addr, aidl_request.addr.data(), 6);
2042     legacy_request->priority = aidl_request.isHighPriority ? legacy_hal::NAN_TX_PRIORITY_HIGH
2043                                                            : legacy_hal::NAN_TX_PRIORITY_NORMAL;
2044     legacy_request->dw_or_faw = aidl_request.shouldUseDiscoveryWindow
2045                                         ? legacy_hal::NAN_TRANSMIT_IN_DW
2046                                         : legacy_hal::NAN_TRANSMIT_IN_FAW;
2047     legacy_request->service_specific_info_len = aidl_request.serviceSpecificInfo.size();
2048     if (legacy_request->service_specific_info_len > NAN_MAX_SERVICE_SPECIFIC_INFO_LEN) {
2049         LOG(ERROR) << "convertAidlNanTransmitFollowupRequestToLegacy: "
2050                       "service_specific_info_len too large";
2051         return false;
2052     }
2053     memcpy(legacy_request->service_specific_info, aidl_request.serviceSpecificInfo.data(),
2054            legacy_request->service_specific_info_len);
2055     legacy_request->sdea_service_specific_info_len =
2056             aidl_request.extendedServiceSpecificInfo.size();
2057     if (legacy_request->sdea_service_specific_info_len > NAN_MAX_SDEA_SERVICE_SPECIFIC_INFO_LEN) {
2058         LOG(ERROR) << "convertAidlNanTransmitFollowupRequestToLegacy: "
2059                       "sdea_service_specific_info_len too large";
2060         return false;
2061     }
2062     memcpy(legacy_request->sdea_service_specific_info,
2063            aidl_request.extendedServiceSpecificInfo.data(),
2064            legacy_request->sdea_service_specific_info_len);
2065     legacy_request->recv_indication_cfg = aidl_request.disableFollowupResultIndication ? 0x1 : 0x0;
2066 
2067     return true;
2068 }
2069 
convertAidlNanDataPathInitiatorRequestToLegacy(const NanInitiateDataPathRequest & aidl_request,legacy_hal::NanDataPathInitiatorRequest * legacy_request)2070 bool convertAidlNanDataPathInitiatorRequestToLegacy(
2071         const NanInitiateDataPathRequest& aidl_request,
2072         legacy_hal::NanDataPathInitiatorRequest* legacy_request) {
2073     if (!legacy_request) {
2074         LOG(ERROR) << "convertAidlNanDataPathInitiatorRequestToLegacy: "
2075                       "legacy_request is null";
2076         return false;
2077     }
2078     *legacy_request = {};
2079 
2080     legacy_request->requestor_instance_id = aidl_request.peerId;
2081     memcpy(legacy_request->peer_disc_mac_addr, aidl_request.peerDiscMacAddr.data(), 6);
2082     legacy_request->channel_request_type =
2083             convertAidlNanDataPathChannelCfgToLegacy(aidl_request.channelRequestType);
2084     legacy_request->channel = aidl_request.channel;
2085     if (strnlen(aidl_request.ifaceName.c_str(), IFNAMSIZ + 1) == IFNAMSIZ + 1) {
2086         LOG(ERROR) << "convertAidlNanDataPathInitiatorRequestToLegacy: "
2087                       "ifaceName too long";
2088         return false;
2089     }
2090     strlcpy(legacy_request->ndp_iface, aidl_request.ifaceName.c_str(), IFNAMSIZ + 1);
2091     legacy_request->ndp_cfg.security_cfg =
2092             (aidl_request.securityConfig.securityType != NanDataPathSecurityType::OPEN)
2093                     ? legacy_hal::NAN_DP_CONFIG_SECURITY
2094                     : legacy_hal::NAN_DP_CONFIG_NO_SECURITY;
2095     legacy_request->app_info.ndp_app_info_len = aidl_request.appInfo.size();
2096     if (legacy_request->app_info.ndp_app_info_len > NAN_DP_MAX_APP_INFO_LEN) {
2097         LOG(ERROR) << "convertAidlNanDataPathInitiatorRequestToLegacy: "
2098                       "ndp_app_info_len too large";
2099         return false;
2100     }
2101     memcpy(legacy_request->app_info.ndp_app_info, aidl_request.appInfo.data(),
2102            legacy_request->app_info.ndp_app_info_len);
2103     legacy_request->cipher_type = (unsigned int)aidl_request.securityConfig.cipherType;
2104     if (aidl_request.securityConfig.securityType == NanDataPathSecurityType::PMK) {
2105         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PMK;
2106         legacy_request->key_info.body.pmk_info.pmk_len = aidl_request.securityConfig.pmk.size();
2107         if (legacy_request->key_info.body.pmk_info.pmk_len != NAN_PMK_INFO_LEN) {
2108             LOG(ERROR) << "convertAidlNanDataPathInitiatorRequestToLegacy: "
2109                           "invalid pmk_len";
2110             return false;
2111         }
2112         memcpy(legacy_request->key_info.body.pmk_info.pmk, aidl_request.securityConfig.pmk.data(),
2113                legacy_request->key_info.body.pmk_info.pmk_len);
2114     }
2115     if (aidl_request.securityConfig.securityType == NanDataPathSecurityType::PASSPHRASE) {
2116         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PASSPHRASE;
2117         legacy_request->key_info.body.passphrase_info.passphrase_len =
2118                 aidl_request.securityConfig.passphrase.size();
2119         if (legacy_request->key_info.body.passphrase_info.passphrase_len <
2120             NAN_SECURITY_MIN_PASSPHRASE_LEN) {
2121             LOG(ERROR) << "convertAidlNanDataPathInitiatorRequestToLegacy: "
2122                           "passphrase_len too small";
2123             return false;
2124         }
2125         if (legacy_request->key_info.body.passphrase_info.passphrase_len >
2126             NAN_SECURITY_MAX_PASSPHRASE_LEN) {
2127             LOG(ERROR) << "convertAidlNanDataPathInitiatorRequestToLegacy: "
2128                           "passphrase_len too large";
2129             return false;
2130         }
2131         memcpy(legacy_request->key_info.body.passphrase_info.passphrase,
2132                aidl_request.securityConfig.passphrase.data(),
2133                legacy_request->key_info.body.passphrase_info.passphrase_len);
2134     }
2135     legacy_request->service_name_len = aidl_request.serviceNameOutOfBand.size();
2136     if (legacy_request->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
2137         LOG(ERROR) << "convertAidlNanDataPathInitiatorRequestToLegacy: "
2138                       "service_name_len too large";
2139         return false;
2140     }
2141     memcpy(legacy_request->service_name, aidl_request.serviceNameOutOfBand.data(),
2142            legacy_request->service_name_len);
2143     legacy_request->scid_len = aidl_request.securityConfig.scid.size();
2144     if (legacy_request->scid_len > NAN_MAX_SCID_BUF_LEN) {
2145         LOG(ERROR) << "convertAidlNanDataPathInitiatorRequestToLegacy: scid_len too large";
2146         return false;
2147     }
2148     memcpy(legacy_request->scid, aidl_request.securityConfig.scid.data(), legacy_request->scid_len);
2149     legacy_request->publish_subscribe_id = static_cast<uint8_t>(aidl_request.discoverySessionId);
2150 
2151     legacy_request->csia_capabilities |=
2152             aidl_request.securityConfig.enable16ReplyCountersForTksa ? 0x1 : 0x0;
2153     legacy_request->csia_capabilities |=
2154             aidl_request.securityConfig.enable16ReplyCountersForGtksa ? 0x8 : 0x0;
2155     if (aidl_request.securityConfig.supportGtkAndIgtk) {
2156         legacy_request->csia_capabilities |= aidl_request.securityConfig.supportBigtksa ? 0x4 : 0x2;
2157     }
2158     legacy_request->csia_capabilities |= aidl_request.securityConfig.enableNcsBip256 ? 0x16 : 0x0;
2159     legacy_request->gtk_protection =
2160             aidl_request.securityConfig.requiresEnhancedFrameProtection ? 1 : 0;
2161 
2162     return true;
2163 }
2164 
convertAidlNanDataPathIndicationResponseToLegacy(const NanRespondToDataPathIndicationRequest & aidl_request,legacy_hal::NanDataPathIndicationResponse * legacy_request)2165 bool convertAidlNanDataPathIndicationResponseToLegacy(
2166         const NanRespondToDataPathIndicationRequest& aidl_request,
2167         legacy_hal::NanDataPathIndicationResponse* legacy_request) {
2168     if (!legacy_request) {
2169         LOG(ERROR) << "convertAidlNanDataPathIndicationResponseToLegacy: "
2170                       "legacy_request is null";
2171         return false;
2172     }
2173     *legacy_request = {};
2174 
2175     legacy_request->rsp_code = aidl_request.acceptRequest ? legacy_hal::NAN_DP_REQUEST_ACCEPT
2176                                                           : legacy_hal::NAN_DP_REQUEST_REJECT;
2177     legacy_request->ndp_instance_id = aidl_request.ndpInstanceId;
2178     if (strnlen(aidl_request.ifaceName.c_str(), IFNAMSIZ + 1) == IFNAMSIZ + 1) {
2179         LOG(ERROR) << "convertAidlNanDataPathIndicationResponseToLegacy: "
2180                       "ifaceName too long";
2181         return false;
2182     }
2183     strlcpy(legacy_request->ndp_iface, aidl_request.ifaceName.c_str(), IFNAMSIZ + 1);
2184     legacy_request->ndp_cfg.security_cfg =
2185             (aidl_request.securityConfig.securityType != NanDataPathSecurityType::OPEN)
2186                     ? legacy_hal::NAN_DP_CONFIG_SECURITY
2187                     : legacy_hal::NAN_DP_CONFIG_NO_SECURITY;
2188     legacy_request->app_info.ndp_app_info_len = aidl_request.appInfo.size();
2189     if (legacy_request->app_info.ndp_app_info_len > NAN_DP_MAX_APP_INFO_LEN) {
2190         LOG(ERROR) << "convertAidlNanDataPathIndicationResponseToLegacy: "
2191                       "ndp_app_info_len too large";
2192         return false;
2193     }
2194     memcpy(legacy_request->app_info.ndp_app_info, aidl_request.appInfo.data(),
2195            legacy_request->app_info.ndp_app_info_len);
2196     legacy_request->cipher_type = (unsigned int)aidl_request.securityConfig.cipherType;
2197     if (aidl_request.securityConfig.securityType == NanDataPathSecurityType::PMK) {
2198         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PMK;
2199         legacy_request->key_info.body.pmk_info.pmk_len = aidl_request.securityConfig.pmk.size();
2200         if (legacy_request->key_info.body.pmk_info.pmk_len != NAN_PMK_INFO_LEN) {
2201             LOG(ERROR) << "convertAidlNanDataPathIndicationResponseToLegacy: "
2202                           "invalid pmk_len";
2203             return false;
2204         }
2205         memcpy(legacy_request->key_info.body.pmk_info.pmk, aidl_request.securityConfig.pmk.data(),
2206                legacy_request->key_info.body.pmk_info.pmk_len);
2207     }
2208     if (aidl_request.securityConfig.securityType == NanDataPathSecurityType::PASSPHRASE) {
2209         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PASSPHRASE;
2210         legacy_request->key_info.body.passphrase_info.passphrase_len =
2211                 aidl_request.securityConfig.passphrase.size();
2212         if (legacy_request->key_info.body.passphrase_info.passphrase_len <
2213             NAN_SECURITY_MIN_PASSPHRASE_LEN) {
2214             LOG(ERROR) << "convertAidlNanDataPathIndicationResponseToLegacy: "
2215                           "passphrase_len too small";
2216             return false;
2217         }
2218         if (legacy_request->key_info.body.passphrase_info.passphrase_len >
2219             NAN_SECURITY_MAX_PASSPHRASE_LEN) {
2220             LOG(ERROR) << "convertAidlNanDataPathIndicationResponseToLegacy: "
2221                           "passphrase_len too large";
2222             return false;
2223         }
2224         memcpy(legacy_request->key_info.body.passphrase_info.passphrase,
2225                aidl_request.securityConfig.passphrase.data(),
2226                legacy_request->key_info.body.passphrase_info.passphrase_len);
2227     }
2228     legacy_request->service_name_len = aidl_request.serviceNameOutOfBand.size();
2229     if (legacy_request->service_name_len > NAN_MAX_SERVICE_NAME_LEN) {
2230         LOG(ERROR) << "convertAidlNanDataPathIndicationResponseToLegacy: "
2231                       "service_name_len too large";
2232         return false;
2233     }
2234     memcpy(legacy_request->service_name, aidl_request.serviceNameOutOfBand.data(),
2235            legacy_request->service_name_len);
2236     legacy_request->scid_len = aidl_request.securityConfig.scid.size();
2237     if (legacy_request->scid_len > NAN_MAX_SCID_BUF_LEN) {
2238         LOG(ERROR) << "convertAidlNanDataPathIndicationResponseToLegacy: scid_len too large";
2239         return false;
2240     }
2241     memcpy(legacy_request->scid, aidl_request.securityConfig.scid.data(), legacy_request->scid_len);
2242     legacy_request->publish_subscribe_id = static_cast<uint8_t>(aidl_request.discoverySessionId);
2243 
2244     legacy_request->csia_capabilities |=
2245             aidl_request.securityConfig.enable16ReplyCountersForTksa ? 0x1 : 0x0;
2246     legacy_request->csia_capabilities |=
2247             aidl_request.securityConfig.enable16ReplyCountersForGtksa ? 0x8 : 0x0;
2248     if (aidl_request.securityConfig.supportGtkAndIgtk) {
2249         legacy_request->csia_capabilities |= aidl_request.securityConfig.supportBigtksa ? 0x4 : 0x2;
2250     }
2251     legacy_request->csia_capabilities |= aidl_request.securityConfig.enableNcsBip256 ? 0x16 : 0x0;
2252     legacy_request->gtk_protection =
2253             aidl_request.securityConfig.requiresEnhancedFrameProtection ? 1 : 0;
2254 
2255     return true;
2256 }
2257 
convertLegacyNanResponseHeaderToAidl(const legacy_hal::NanResponseMsg & legacy_response,NanStatus * nanStatus)2258 bool convertLegacyNanResponseHeaderToAidl(const legacy_hal::NanResponseMsg& legacy_response,
2259                                           NanStatus* nanStatus) {
2260     if (!nanStatus) {
2261         LOG(ERROR) << "convertLegacyNanResponseHeaderToAidl: nanStatus is null";
2262         return false;
2263     }
2264     *nanStatus = {};
2265 
2266     convertToNanStatus(legacy_response.status, legacy_response.nan_error,
2267                        sizeof(legacy_response.nan_error), nanStatus);
2268     return true;
2269 }
2270 
convertLegacyNanCapabilitiesResponseToAidl(const legacy_hal::NanCapabilities & legacy_response,NanCapabilities * aidl_response)2271 bool convertLegacyNanCapabilitiesResponseToAidl(const legacy_hal::NanCapabilities& legacy_response,
2272                                                 NanCapabilities* aidl_response) {
2273     if (!aidl_response) {
2274         LOG(ERROR) << "convertLegacyNanCapabilitiesResponseToAidl: "
2275                       "aidl_response is null";
2276         return false;
2277     }
2278     *aidl_response = {};
2279 
2280     aidl_response->maxConcurrentClusters = legacy_response.max_concurrent_nan_clusters;
2281     aidl_response->maxPublishes = legacy_response.max_publishes;
2282     aidl_response->maxSubscribes = legacy_response.max_subscribes;
2283     aidl_response->maxServiceNameLen = legacy_response.max_service_name_len;
2284     aidl_response->maxMatchFilterLen = legacy_response.max_match_filter_len;
2285     aidl_response->maxTotalMatchFilterLen = legacy_response.max_total_match_filter_len;
2286     aidl_response->maxServiceSpecificInfoLen = legacy_response.max_service_specific_info_len;
2287     aidl_response->maxExtendedServiceSpecificInfoLen =
2288             legacy_response.max_sdea_service_specific_info_len;
2289     aidl_response->maxNdiInterfaces = legacy_response.max_ndi_interfaces;
2290     aidl_response->maxNdpSessions = legacy_response.max_ndp_sessions;
2291     aidl_response->maxAppInfoLen = legacy_response.max_app_info_len;
2292     aidl_response->maxQueuedTransmitFollowupMsgs =
2293             legacy_response.max_queued_transmit_followup_msgs;
2294     aidl_response->maxSubscribeInterfaceAddresses = legacy_response.max_subscribe_address;
2295     aidl_response->supportedCipherSuites = legacy_response.cipher_suites_supported;
2296     aidl_response->instantCommunicationModeSupportFlag = legacy_response.is_instant_mode_supported;
2297     aidl_response->supports6g = legacy_response.is_6g_supported;
2298     aidl_response->supportsHe = legacy_response.is_he_supported;
2299     aidl_response->supportsPairing = legacy_response.is_pairing_supported;
2300     aidl_response->supportsSetClusterId = legacy_response.is_set_cluster_id_supported;
2301     aidl_response->supportsSuspension = legacy_response.is_suspension_supported;
2302     // TODO: Retrieve values from the legacy HAL
2303     aidl_response->supportsPeriodicRanging = false;
2304     aidl_response->maxSupportedBandwidth = RttBw::BW_UNSPECIFIED;
2305     aidl_response->maxNumRxChainsSupported = 2;
2306 
2307     return true;
2308 }
2309 
convertLegacyNanMatchIndToAidl(const legacy_hal::NanMatchInd & legacy_ind,NanMatchInd * aidl_ind)2310 bool convertLegacyNanMatchIndToAidl(const legacy_hal::NanMatchInd& legacy_ind,
2311                                     NanMatchInd* aidl_ind) {
2312     if (!aidl_ind) {
2313         LOG(ERROR) << "convertLegacyNanMatchIndToAidl: aidl_ind is null";
2314         return false;
2315     }
2316     *aidl_ind = {};
2317 
2318     aidl_ind->discoverySessionId = legacy_ind.publish_subscribe_id;
2319     aidl_ind->peerId = legacy_ind.requestor_instance_id;
2320     aidl_ind->addr = std::array<uint8_t, 6>();
2321     std::copy(legacy_ind.addr, legacy_ind.addr + 6, std::begin(aidl_ind->addr));
2322     aidl_ind->serviceSpecificInfo = std::vector<uint8_t>(
2323             legacy_ind.service_specific_info,
2324             legacy_ind.service_specific_info + legacy_ind.service_specific_info_len);
2325     aidl_ind->extendedServiceSpecificInfo = std::vector<uint8_t>(
2326             legacy_ind.sdea_service_specific_info,
2327             legacy_ind.sdea_service_specific_info + legacy_ind.sdea_service_specific_info_len);
2328     aidl_ind->matchFilter =
2329             std::vector<uint8_t>(legacy_ind.sdf_match_filter,
2330                                  legacy_ind.sdf_match_filter + legacy_ind.sdf_match_filter_len);
2331     aidl_ind->matchOccurredInBeaconFlag = legacy_ind.match_occured_flag == 1;  // NOTYPO
2332     aidl_ind->outOfResourceFlag = legacy_ind.out_of_resource_flag == 1;
2333     aidl_ind->rssiValue = legacy_ind.rssi_value;
2334     aidl_ind->peerCipherType = (NanCipherSuiteType)legacy_ind.peer_cipher_type;
2335     aidl_ind->peerRequiresSecurityEnabledInNdp =
2336             legacy_ind.peer_sdea_params.security_cfg == legacy_hal::NAN_DP_CONFIG_SECURITY;
2337     aidl_ind->peerRequiresRanging =
2338             legacy_ind.peer_sdea_params.ranging_state == legacy_hal::NAN_RANGING_ENABLE;
2339     aidl_ind->rangingMeasurementInMm = legacy_ind.range_info.range_measurement_mm;
2340     aidl_ind->rangingIndicationType = legacy_ind.range_info.ranging_event_type;
2341     aidl_ind->scid = std::vector<uint8_t>(legacy_ind.scid, legacy_ind.scid + legacy_ind.scid_len);
2342 
2343     if (!convertLegacyNiraToAidl(legacy_ind.nira, &aidl_ind->peerNira)) {
2344         LOG(ERROR) << "convertLegacyNanMatchIndToAidl: invalid NIRA";
2345         return false;
2346     }
2347     if (!convertLegacyPairingConfigToAidl(legacy_ind.peer_pairing_config,
2348                                           &aidl_ind->peerPairingConfig)) {
2349         LOG(ERROR) << "convertLegacyNanMatchIndToAidl: invalid pairing config";
2350         return false;
2351     }
2352     return true;
2353 }
2354 
convertLegacyNanFollowupIndToAidl(const legacy_hal::NanFollowupInd & legacy_ind,NanFollowupReceivedInd * aidl_ind)2355 bool convertLegacyNanFollowupIndToAidl(const legacy_hal::NanFollowupInd& legacy_ind,
2356                                        NanFollowupReceivedInd* aidl_ind) {
2357     if (!aidl_ind) {
2358         LOG(ERROR) << "convertLegacyNanFollowupIndToAidl: aidl_ind is null";
2359         return false;
2360     }
2361     *aidl_ind = {};
2362 
2363     aidl_ind->discoverySessionId = legacy_ind.publish_subscribe_id;
2364     aidl_ind->peerId = legacy_ind.requestor_instance_id;
2365     aidl_ind->addr = std::array<uint8_t, 6>();
2366     std::copy(legacy_ind.addr, legacy_ind.addr + 6, std::begin(aidl_ind->addr));
2367     aidl_ind->receivedInFaw = legacy_ind.dw_or_faw == 1;
2368     aidl_ind->serviceSpecificInfo = std::vector<uint8_t>(
2369             legacy_ind.service_specific_info,
2370             legacy_ind.service_specific_info + legacy_ind.service_specific_info_len);
2371     aidl_ind->extendedServiceSpecificInfo = std::vector<uint8_t>(
2372             legacy_ind.sdea_service_specific_info,
2373             legacy_ind.sdea_service_specific_info + legacy_ind.sdea_service_specific_info_len);
2374 
2375     return true;
2376 }
2377 
convertLegacyNanDataPathRequestIndToAidl(const legacy_hal::NanDataPathRequestInd & legacy_ind,NanDataPathRequestInd * aidl_ind)2378 bool convertLegacyNanDataPathRequestIndToAidl(const legacy_hal::NanDataPathRequestInd& legacy_ind,
2379                                               NanDataPathRequestInd* aidl_ind) {
2380     if (!aidl_ind) {
2381         LOG(ERROR) << "convertLegacyNanDataPathRequestIndToAidl: aidl_ind is null";
2382         return false;
2383     }
2384     *aidl_ind = {};
2385 
2386     aidl_ind->discoverySessionId = legacy_ind.service_instance_id;
2387     aidl_ind->peerDiscMacAddr = std::array<uint8_t, 6>();
2388     std::copy(legacy_ind.peer_disc_mac_addr, legacy_ind.peer_disc_mac_addr + 6,
2389               std::begin(aidl_ind->peerDiscMacAddr));
2390     aidl_ind->ndpInstanceId = legacy_ind.ndp_instance_id;
2391     aidl_ind->securityRequired =
2392             legacy_ind.ndp_cfg.security_cfg == legacy_hal::NAN_DP_CONFIG_SECURITY;
2393     aidl_ind->appInfo = std::vector<uint8_t>(
2394             legacy_ind.app_info.ndp_app_info,
2395             legacy_ind.app_info.ndp_app_info + legacy_ind.app_info.ndp_app_info_len);
2396 
2397     return true;
2398 }
2399 
convertLegacyNdpChannelInfoToAidl(const legacy_hal::NanChannelInfo & legacy_struct,NanDataPathChannelInfo * aidl_struct)2400 bool convertLegacyNdpChannelInfoToAidl(const legacy_hal::NanChannelInfo& legacy_struct,
2401                                        NanDataPathChannelInfo* aidl_struct) {
2402     if (!aidl_struct) {
2403         LOG(ERROR) << "convertLegacyNdpChannelInfoToAidl: aidl_struct is null";
2404         return false;
2405     }
2406     *aidl_struct = {};
2407 
2408     aidl_struct->channelFreq = legacy_struct.channel;
2409     aidl_struct->channelBandwidth = convertLegacyWifiChannelWidthToAidl(
2410             (legacy_hal::wifi_channel_width)legacy_struct.bandwidth);
2411     aidl_struct->numSpatialStreams = legacy_struct.nss;
2412 
2413     return true;
2414 }
2415 
convertLegacyNanDataPathConfirmIndToAidl(const legacy_hal::NanDataPathConfirmInd & legacy_ind,NanDataPathConfirmInd * aidl_ind)2416 bool convertLegacyNanDataPathConfirmIndToAidl(const legacy_hal::NanDataPathConfirmInd& legacy_ind,
2417                                               NanDataPathConfirmInd* aidl_ind) {
2418     if (!aidl_ind) {
2419         LOG(ERROR) << "convertLegacyNanDataPathConfirmIndToAidl: aidl_ind is null";
2420         return false;
2421     }
2422     *aidl_ind = {};
2423 
2424     aidl_ind->ndpInstanceId = legacy_ind.ndp_instance_id;
2425     aidl_ind->dataPathSetupSuccess = legacy_ind.rsp_code == legacy_hal::NAN_DP_REQUEST_ACCEPT;
2426     aidl_ind->peerNdiMacAddr = std::array<uint8_t, 6>();
2427     std::copy(legacy_ind.peer_ndi_mac_addr, legacy_ind.peer_ndi_mac_addr + 6,
2428               std::begin(aidl_ind->peerNdiMacAddr));
2429     aidl_ind->appInfo = std::vector<uint8_t>(
2430             legacy_ind.app_info.ndp_app_info,
2431             legacy_ind.app_info.ndp_app_info + legacy_ind.app_info.ndp_app_info_len);
2432     aidl_ind->status.status = convertLegacyNanStatusTypeToAidl(legacy_ind.reason_code);
2433     aidl_ind->status.description = "";
2434 
2435     std::vector<NanDataPathChannelInfo> channelInfo;
2436     for (unsigned int i = 0; i < legacy_ind.num_channels; ++i) {
2437         NanDataPathChannelInfo aidl_struct;
2438         if (!convertLegacyNdpChannelInfoToAidl(legacy_ind.channel_info[i], &aidl_struct)) {
2439             return false;
2440         }
2441         channelInfo.push_back(aidl_struct);
2442     }
2443     aidl_ind->channelInfo = channelInfo;
2444 
2445     return true;
2446 }
2447 
convertLegacyNanDataPathScheduleUpdateIndToAidl(const legacy_hal::NanDataPathScheduleUpdateInd & legacy_ind,NanDataPathScheduleUpdateInd * aidl_ind)2448 bool convertLegacyNanDataPathScheduleUpdateIndToAidl(
2449         const legacy_hal::NanDataPathScheduleUpdateInd& legacy_ind,
2450         NanDataPathScheduleUpdateInd* aidl_ind) {
2451     if (!aidl_ind) {
2452         LOG(ERROR) << "convertLegacyNanDataPathScheduleUpdateIndToAidl: "
2453                       "aidl_ind is null";
2454         return false;
2455     }
2456     *aidl_ind = {};
2457 
2458     aidl_ind->peerDiscoveryAddress = std::array<uint8_t, 6>();
2459     std::copy(legacy_ind.peer_mac_addr, legacy_ind.peer_mac_addr + 6,
2460               std::begin(aidl_ind->peerDiscoveryAddress));
2461     std::vector<NanDataPathChannelInfo> channelInfo;
2462     for (unsigned int i = 0; i < legacy_ind.num_channels; ++i) {
2463         NanDataPathChannelInfo aidl_struct;
2464         if (!convertLegacyNdpChannelInfoToAidl(legacy_ind.channel_info[i], &aidl_struct)) {
2465             return false;
2466         }
2467         channelInfo.push_back(aidl_struct);
2468     }
2469     aidl_ind->channelInfo = channelInfo;
2470     std::vector<uint32_t> ndpInstanceIds;
2471     for (unsigned int i = 0; i < legacy_ind.num_ndp_instances; ++i) {
2472         ndpInstanceIds.push_back(legacy_ind.ndp_instance_id[i]);
2473     }
2474     aidl_ind->ndpInstanceIds = uintToIntVec(ndpInstanceIds);
2475 
2476     return true;
2477 }
2478 
convertAidlRttTypeToLegacy(RttType type)2479 legacy_hal::wifi_rtt_type convertAidlRttTypeToLegacy(RttType type) {
2480     switch (type) {
2481         case RttType::ONE_SIDED:
2482             return legacy_hal::RTT_TYPE_1_SIDED;
2483         case RttType::TWO_SIDED_11MC:
2484             // Same as RttType::TWO_SIDED
2485             return legacy_hal::RTT_TYPE_2_SIDED_11MC;
2486         case RttType::TWO_SIDED_11AZ_NTB:
2487             return legacy_hal::RTT_TYPE_2_SIDED_11AZ_NTB;
2488         case RttType::TWO_SIDED_11AZ_NTB_SECURE:
2489             return legacy_hal::RTT_TYPE_2_SIDED_11AZ_NTB_SECURE;
2490     };
2491     CHECK(false);
2492 }
2493 
convertLegacyRttTypeToAidl(legacy_hal::wifi_rtt_type type)2494 RttType convertLegacyRttTypeToAidl(legacy_hal::wifi_rtt_type type) {
2495     switch (type) {
2496         case legacy_hal::RTT_TYPE_1_SIDED:
2497             return RttType::ONE_SIDED;
2498         case legacy_hal::RTT_TYPE_2_SIDED_11MC:
2499             // Same as legacy_hal::RTT_TYPE_2_SIDED
2500             return RttType::TWO_SIDED_11MC;
2501         case legacy_hal::RTT_TYPE_2_SIDED_11AZ_NTB:
2502             return RttType::TWO_SIDED_11AZ_NTB;
2503         case legacy_hal::RTT_TYPE_2_SIDED_11AZ_NTB_SECURE:
2504             return RttType::TWO_SIDED_11AZ_NTB_SECURE;
2505     };
2506     CHECK(false) << "Unknown legacy type: " << type;
2507 }
2508 
convertAidlRttPeerTypeToLegacy(RttPeerType type)2509 legacy_hal::rtt_peer_type convertAidlRttPeerTypeToLegacy(RttPeerType type) {
2510     switch (type) {
2511         case RttPeerType::AP:
2512             return legacy_hal::RTT_PEER_AP;
2513         case RttPeerType::STA:
2514             return legacy_hal::RTT_PEER_STA;
2515         case RttPeerType::P2P_GO:
2516             return legacy_hal::RTT_PEER_P2P_GO;
2517         case RttPeerType::P2P_CLIENT:
2518             return legacy_hal::RTT_PEER_P2P_CLIENT;
2519         case RttPeerType::NAN_TYPE:
2520             return legacy_hal::RTT_PEER_NAN;
2521     };
2522     CHECK(false);
2523 }
2524 
convertAidlWifiChannelWidthToLegacy(WifiChannelWidthInMhz type)2525 legacy_hal::wifi_channel_width convertAidlWifiChannelWidthToLegacy(WifiChannelWidthInMhz type) {
2526     switch (type) {
2527         case WifiChannelWidthInMhz::WIDTH_20:
2528             return legacy_hal::WIFI_CHAN_WIDTH_20;
2529         case WifiChannelWidthInMhz::WIDTH_40:
2530             return legacy_hal::WIFI_CHAN_WIDTH_40;
2531         case WifiChannelWidthInMhz::WIDTH_80:
2532             return legacy_hal::WIFI_CHAN_WIDTH_80;
2533         case WifiChannelWidthInMhz::WIDTH_160:
2534             return legacy_hal::WIFI_CHAN_WIDTH_160;
2535         case WifiChannelWidthInMhz::WIDTH_80P80:
2536             return legacy_hal::WIFI_CHAN_WIDTH_80P80;
2537         case WifiChannelWidthInMhz::WIDTH_5:
2538             return legacy_hal::WIFI_CHAN_WIDTH_5;
2539         case WifiChannelWidthInMhz::WIDTH_10:
2540             return legacy_hal::WIFI_CHAN_WIDTH_10;
2541         case WifiChannelWidthInMhz::WIDTH_320:
2542             return legacy_hal::WIFI_CHAN_WIDTH_320;
2543         case WifiChannelWidthInMhz::WIDTH_INVALID:
2544             return legacy_hal::WIFI_CHAN_WIDTH_INVALID;
2545     };
2546     CHECK(false);
2547 }
2548 
convertLegacyWifiChannelWidthToAidl(legacy_hal::wifi_channel_width type)2549 WifiChannelWidthInMhz convertLegacyWifiChannelWidthToAidl(legacy_hal::wifi_channel_width type) {
2550     switch (type) {
2551         case legacy_hal::WIFI_CHAN_WIDTH_20:
2552             return WifiChannelWidthInMhz::WIDTH_20;
2553         case legacy_hal::WIFI_CHAN_WIDTH_40:
2554             return WifiChannelWidthInMhz::WIDTH_40;
2555         case legacy_hal::WIFI_CHAN_WIDTH_80:
2556             return WifiChannelWidthInMhz::WIDTH_80;
2557         case legacy_hal::WIFI_CHAN_WIDTH_160:
2558             return WifiChannelWidthInMhz::WIDTH_160;
2559         case legacy_hal::WIFI_CHAN_WIDTH_80P80:
2560             return WifiChannelWidthInMhz::WIDTH_80P80;
2561         case legacy_hal::WIFI_CHAN_WIDTH_5:
2562             return WifiChannelWidthInMhz::WIDTH_5;
2563         case legacy_hal::WIFI_CHAN_WIDTH_10:
2564             return WifiChannelWidthInMhz::WIDTH_10;
2565         case legacy_hal::WIFI_CHAN_WIDTH_320:
2566             return WifiChannelWidthInMhz::WIDTH_320;
2567         default:
2568             return WifiChannelWidthInMhz::WIDTH_INVALID;
2569     };
2570 }
2571 
convertAidlRttPreambleToLegacy(RttPreamble type)2572 legacy_hal::wifi_rtt_preamble convertAidlRttPreambleToLegacy(RttPreamble type) {
2573     switch (type) {
2574         case RttPreamble::LEGACY:
2575             return legacy_hal::WIFI_RTT_PREAMBLE_LEGACY;
2576         case RttPreamble::HT:
2577             return legacy_hal::WIFI_RTT_PREAMBLE_HT;
2578         case RttPreamble::VHT:
2579             return legacy_hal::WIFI_RTT_PREAMBLE_VHT;
2580         case RttPreamble::HE:
2581             return legacy_hal::WIFI_RTT_PREAMBLE_HE;
2582         case RttPreamble::EHT:
2583             return legacy_hal::WIFI_RTT_PREAMBLE_EHT;
2584         case RttPreamble::INVALID:
2585             return legacy_hal::WIFI_RTT_PREAMBLE_INVALID;
2586     };
2587     CHECK(false);
2588 }
2589 
convertLegacyRttPreambleToAidl(legacy_hal::wifi_rtt_preamble type)2590 RttPreamble convertLegacyRttPreambleToAidl(legacy_hal::wifi_rtt_preamble type) {
2591     switch (type) {
2592         case legacy_hal::WIFI_RTT_PREAMBLE_LEGACY:
2593             return RttPreamble::LEGACY;
2594         case legacy_hal::WIFI_RTT_PREAMBLE_HT:
2595             return RttPreamble::HT;
2596         case legacy_hal::WIFI_RTT_PREAMBLE_VHT:
2597             return RttPreamble::VHT;
2598         case legacy_hal::WIFI_RTT_PREAMBLE_HE:
2599             return RttPreamble::HE;
2600         case legacy_hal::WIFI_RTT_PREAMBLE_EHT:
2601             return RttPreamble::EHT;
2602         case legacy_hal::WIFI_RTT_PREAMBLE_INVALID:
2603             return RttPreamble::INVALID;
2604     };
2605     CHECK(false) << "Unknown legacy type: " << type;
2606 }
2607 
convertAidlRttBwToLegacy(RttBw type)2608 legacy_hal::wifi_rtt_bw convertAidlRttBwToLegacy(RttBw type) {
2609     switch (type) {
2610         case RttBw::BW_5MHZ:
2611             return legacy_hal::WIFI_RTT_BW_5;
2612         case RttBw::BW_10MHZ:
2613             return legacy_hal::WIFI_RTT_BW_10;
2614         case RttBw::BW_20MHZ:
2615             return legacy_hal::WIFI_RTT_BW_20;
2616         case RttBw::BW_40MHZ:
2617             return legacy_hal::WIFI_RTT_BW_40;
2618         case RttBw::BW_80MHZ:
2619             return legacy_hal::WIFI_RTT_BW_80;
2620         case RttBw::BW_160MHZ:
2621             return legacy_hal::WIFI_RTT_BW_160;
2622         case RttBw::BW_320MHZ:
2623             return legacy_hal::WIFI_RTT_BW_320;
2624         case RttBw::BW_UNSPECIFIED:
2625             return legacy_hal::WIFI_RTT_BW_UNSPECIFIED;
2626     };
2627     CHECK(false);
2628 }
2629 
convertLegacyRttBwToAidl(legacy_hal::wifi_rtt_bw type)2630 RttBw convertLegacyRttBwToAidl(legacy_hal::wifi_rtt_bw type) {
2631     switch (type) {
2632         case legacy_hal::WIFI_RTT_BW_5:
2633             return RttBw::BW_5MHZ;
2634         case legacy_hal::WIFI_RTT_BW_10:
2635             return RttBw::BW_10MHZ;
2636         case legacy_hal::WIFI_RTT_BW_20:
2637             return RttBw::BW_20MHZ;
2638         case legacy_hal::WIFI_RTT_BW_40:
2639             return RttBw::BW_40MHZ;
2640         case legacy_hal::WIFI_RTT_BW_80:
2641             return RttBw::BW_80MHZ;
2642         case legacy_hal::WIFI_RTT_BW_160:
2643             return RttBw::BW_160MHZ;
2644         case legacy_hal::WIFI_RTT_BW_320:
2645             return RttBw::BW_320MHZ;
2646         case legacy_hal::WIFI_RTT_BW_UNSPECIFIED:
2647             return RttBw::BW_UNSPECIFIED;
2648     };
2649     CHECK(false) << "Unknown legacy type: " << type;
2650 }
2651 
convertAidlRttMotionPatternToLegacy(RttMotionPattern type)2652 legacy_hal::wifi_motion_pattern convertAidlRttMotionPatternToLegacy(RttMotionPattern type) {
2653     switch (type) {
2654         case RttMotionPattern::NOT_EXPECTED:
2655             return legacy_hal::WIFI_MOTION_NOT_EXPECTED;
2656         case RttMotionPattern::EXPECTED:
2657             return legacy_hal::WIFI_MOTION_EXPECTED;
2658         case RttMotionPattern::UNKNOWN:
2659             return legacy_hal::WIFI_MOTION_UNKNOWN;
2660     };
2661     CHECK(false);
2662 }
2663 
convertLegacyWifiRatePreambleToAidl(uint8_t preamble)2664 WifiRatePreamble convertLegacyWifiRatePreambleToAidl(uint8_t preamble) {
2665     switch (preamble) {
2666         case 0:
2667             return WifiRatePreamble::OFDM;
2668         case 1:
2669             return WifiRatePreamble::CCK;
2670         case 2:
2671             return WifiRatePreamble::HT;
2672         case 3:
2673             return WifiRatePreamble::VHT;
2674         case 4:
2675             return WifiRatePreamble::HE;
2676         case 5:
2677             return WifiRatePreamble::EHT;
2678         default:
2679             return WifiRatePreamble::RESERVED;
2680     };
2681     CHECK(false) << "Unknown legacy preamble: " << preamble;
2682 }
2683 
convertLegacyWifiRateNssToAidl(uint8_t nss)2684 WifiRateNss convertLegacyWifiRateNssToAidl(uint8_t nss) {
2685     switch (nss) {
2686         case 0:
2687             return WifiRateNss::NSS_1x1;
2688         case 1:
2689             return WifiRateNss::NSS_2x2;
2690         case 2:
2691             return WifiRateNss::NSS_3x3;
2692         case 3:
2693             return WifiRateNss::NSS_4x4;
2694     };
2695     CHECK(false) << "Unknown legacy nss: " << nss;
2696     return {};
2697 }
2698 
convertLegacyRttStatusToAidl(legacy_hal::wifi_rtt_status status)2699 RttStatus convertLegacyRttStatusToAidl(legacy_hal::wifi_rtt_status status) {
2700     switch (status) {
2701         case legacy_hal::RTT_STATUS_SUCCESS:
2702             return RttStatus::SUCCESS;
2703         case legacy_hal::RTT_STATUS_FAILURE:
2704             return RttStatus::FAILURE;
2705         case legacy_hal::RTT_STATUS_FAIL_NO_RSP:
2706             return RttStatus::FAIL_NO_RSP;
2707         case legacy_hal::RTT_STATUS_FAIL_REJECTED:
2708             return RttStatus::FAIL_REJECTED;
2709         case legacy_hal::RTT_STATUS_FAIL_NOT_SCHEDULED_YET:
2710             return RttStatus::FAIL_NOT_SCHEDULED_YET;
2711         case legacy_hal::RTT_STATUS_FAIL_TM_TIMEOUT:
2712             return RttStatus::FAIL_TM_TIMEOUT;
2713         case legacy_hal::RTT_STATUS_FAIL_AP_ON_DIFF_CHANNEL:
2714             return RttStatus::FAIL_AP_ON_DIFF_CHANNEL;
2715         case legacy_hal::RTT_STATUS_FAIL_NO_CAPABILITY:
2716             return RttStatus::FAIL_NO_CAPABILITY;
2717         case legacy_hal::RTT_STATUS_ABORTED:
2718             return RttStatus::ABORTED;
2719         case legacy_hal::RTT_STATUS_FAIL_INVALID_TS:
2720             return RttStatus::FAIL_INVALID_TS;
2721         case legacy_hal::RTT_STATUS_FAIL_PROTOCOL:
2722             return RttStatus::FAIL_PROTOCOL;
2723         case legacy_hal::RTT_STATUS_FAIL_SCHEDULE:
2724             return RttStatus::FAIL_SCHEDULE;
2725         case legacy_hal::RTT_STATUS_FAIL_BUSY_TRY_LATER:
2726             return RttStatus::FAIL_BUSY_TRY_LATER;
2727         case legacy_hal::RTT_STATUS_INVALID_REQ:
2728             return RttStatus::INVALID_REQ;
2729         case legacy_hal::RTT_STATUS_NO_WIFI:
2730             return RttStatus::NO_WIFI;
2731         case legacy_hal::RTT_STATUS_FAIL_FTM_PARAM_OVERRIDE:
2732             return RttStatus::FAIL_FTM_PARAM_OVERRIDE;
2733         case legacy_hal::RTT_STATUS_NAN_RANGING_PROTOCOL_FAILURE:
2734             return RttStatus::NAN_RANGING_PROTOCOL_FAILURE;
2735         case legacy_hal::RTT_STATUS_NAN_RANGING_CONCURRENCY_NOT_SUPPORTED:
2736             return RttStatus::NAN_RANGING_CONCURRENCY_NOT_SUPPORTED;
2737         case legacy_hal::RTT_STATUS_SECURE_RANGING_FAILURE_INVALID_AKM:
2738             return RttStatus::SECURE_RANGING_FAILURE_INVALID_AKM;
2739         case legacy_hal::RTT_STATUS_SECURE_RANGING_FAILURE_INVALID_CIPHER:
2740             return RttStatus::SECURE_RANGING_FAILURE_INVALID_CIPHER;
2741         case legacy_hal::RTT_STATUS_SECURE_RANGING_FAILURE_INVALID_CONFIG:
2742             return RttStatus::SECURE_RANGING_FAILURE_INVALID_CONFIG;
2743         case legacy_hal::RTT_STATUS_SECURE_RANGING_FAILURE_REJECTED:
2744             return RttStatus::SECURE_RANGING_FAILURE_REJECTED;
2745         case legacy_hal::RTT_STATUS_SECURE_RANGING_FAILURE_UNKNOWN:
2746             return RttStatus::SECURE_RANGING_FAILURE_UNKNOWN;
2747     };
2748     CHECK(false) << "Unknown legacy status: " << status;
2749 }
2750 
convertAidlWifiChannelInfoToLegacy(const WifiChannelInfo & aidl_info,legacy_hal::wifi_channel_info * legacy_info)2751 bool convertAidlWifiChannelInfoToLegacy(const WifiChannelInfo& aidl_info,
2752                                         legacy_hal::wifi_channel_info* legacy_info) {
2753     if (!legacy_info) {
2754         return false;
2755     }
2756     *legacy_info = {};
2757     legacy_info->width = convertAidlWifiChannelWidthToLegacy(aidl_info.width);
2758     legacy_info->center_freq = aidl_info.centerFreq;
2759     legacy_info->center_freq0 = aidl_info.centerFreq0;
2760     legacy_info->center_freq1 = aidl_info.centerFreq1;
2761     return true;
2762 }
2763 
convertLegacyWifiChannelInfoToAidl(const legacy_hal::wifi_channel_info & legacy_info,WifiChannelInfo * aidl_info)2764 bool convertLegacyWifiChannelInfoToAidl(const legacy_hal::wifi_channel_info& legacy_info,
2765                                         WifiChannelInfo* aidl_info) {
2766     if (!aidl_info) {
2767         return false;
2768     }
2769     *aidl_info = {};
2770     aidl_info->width = convertLegacyWifiChannelWidthToAidl(legacy_info.width);
2771     aidl_info->centerFreq = legacy_info.center_freq;
2772     aidl_info->centerFreq0 = legacy_info.center_freq0;
2773     aidl_info->centerFreq1 = legacy_info.center_freq1;
2774     return true;
2775 }
2776 
convertAidlRttConfigToLegacy(const RttConfig & aidl_config,legacy_hal::wifi_rtt_config * legacy_config)2777 bool convertAidlRttConfigToLegacy(const RttConfig& aidl_config,
2778                                   legacy_hal::wifi_rtt_config* legacy_config) {
2779     if (!legacy_config) {
2780         return false;
2781     }
2782     *legacy_config = {};
2783     CHECK(aidl_config.addr.size() == sizeof(legacy_config->addr));
2784     memcpy(legacy_config->addr, aidl_config.addr.data(), aidl_config.addr.size());
2785     legacy_config->type = convertAidlRttTypeToLegacy(aidl_config.type);
2786     legacy_config->peer = convertAidlRttPeerTypeToLegacy(aidl_config.peer);
2787     if (!convertAidlWifiChannelInfoToLegacy(aidl_config.channel, &legacy_config->channel)) {
2788         return false;
2789     }
2790     legacy_config->burst_period = aidl_config.burstPeriod;
2791     legacy_config->num_burst = aidl_config.numBurst;
2792     legacy_config->num_frames_per_burst = aidl_config.numFramesPerBurst;
2793     legacy_config->num_retries_per_rtt_frame = aidl_config.numRetriesPerRttFrame;
2794     legacy_config->num_retries_per_ftmr = aidl_config.numRetriesPerFtmr;
2795     legacy_config->LCI_request = aidl_config.mustRequestLci;
2796     legacy_config->LCR_request = aidl_config.mustRequestLcr;
2797     legacy_config->burst_duration = aidl_config.burstDuration;
2798     legacy_config->preamble = convertAidlRttPreambleToLegacy(aidl_config.preamble);
2799     legacy_config->bw = convertAidlRttBwToLegacy(aidl_config.bw);
2800     return true;
2801 }
2802 
convertAidlRttConfigToLegacyV3(const RttConfig & aidl_config,legacy_hal::wifi_rtt_config_v3 * legacy_config)2803 bool convertAidlRttConfigToLegacyV3(const RttConfig& aidl_config,
2804                                     legacy_hal::wifi_rtt_config_v3* legacy_config) {
2805     if (!legacy_config) {
2806         return false;
2807     }
2808     *legacy_config = {};
2809     if (!convertAidlRttConfigToLegacy(aidl_config, &(legacy_config->rtt_config))) {
2810         return false;
2811     }
2812     legacy_config->ntb_min_measurement_time = aidl_config.ntbMinMeasurementTime;
2813     legacy_config->ntb_max_measurement_time = aidl_config.ntbMaxMeasurementTime;
2814     return true;
2815 }
2816 
convertAidlVectorOfRttConfigToLegacy(const std::vector<RttConfig> & aidl_configs,std::vector<legacy_hal::wifi_rtt_config> * legacy_configs)2817 bool convertAidlVectorOfRttConfigToLegacy(
2818         const std::vector<RttConfig>& aidl_configs,
2819         std::vector<legacy_hal::wifi_rtt_config>* legacy_configs) {
2820     if (!legacy_configs) {
2821         return false;
2822     }
2823     *legacy_configs = {};
2824     for (const auto& aidl_config : aidl_configs) {
2825         legacy_hal::wifi_rtt_config legacy_config;
2826         if (!convertAidlRttConfigToLegacy(aidl_config, &(legacy_config))) {
2827             return false;
2828         }
2829         legacy_configs->push_back(legacy_config);
2830     }
2831     return true;
2832 }
2833 
convertAidlVectorOfRttConfigToLegacyV3(const std::vector<RttConfig> & aidl_configs,std::vector<legacy_hal::wifi_rtt_config_v3> * legacy_configs)2834 bool convertAidlVectorOfRttConfigToLegacyV3(
2835         const std::vector<RttConfig>& aidl_configs,
2836         std::vector<legacy_hal::wifi_rtt_config_v3>* legacy_configs) {
2837     if (!legacy_configs) {
2838         return false;
2839     }
2840     *legacy_configs = {};
2841     for (const auto& aidl_config : aidl_configs) {
2842         legacy_hal::wifi_rtt_config_v3 legacy_config;
2843         if (!convertAidlRttConfigToLegacyV3(aidl_config, &legacy_config)) {
2844             return false;
2845         }
2846         legacy_configs->push_back(legacy_config);
2847     }
2848     return true;
2849 }
2850 
convertAidlRttLciInformationToLegacy(const RttLciInformation & aidl_info,legacy_hal::wifi_lci_information * legacy_info)2851 bool convertAidlRttLciInformationToLegacy(const RttLciInformation& aidl_info,
2852                                           legacy_hal::wifi_lci_information* legacy_info) {
2853     if (!legacy_info) {
2854         return false;
2855     }
2856     *legacy_info = {};
2857     legacy_info->latitude = aidl_info.latitude;
2858     legacy_info->longitude = aidl_info.longitude;
2859     legacy_info->altitude = aidl_info.altitude;
2860     legacy_info->latitude_unc = aidl_info.latitudeUnc;
2861     legacy_info->longitude_unc = aidl_info.longitudeUnc;
2862     legacy_info->altitude_unc = aidl_info.altitudeUnc;
2863     legacy_info->motion_pattern = convertAidlRttMotionPatternToLegacy(aidl_info.motionPattern);
2864     legacy_info->floor = aidl_info.floor;
2865     legacy_info->height_above_floor = aidl_info.heightAboveFloor;
2866     legacy_info->height_unc = aidl_info.heightUnc;
2867     return true;
2868 }
2869 
convertAidlRttLcrInformationToLegacy(const RttLcrInformation & aidl_info,legacy_hal::wifi_lcr_information * legacy_info)2870 bool convertAidlRttLcrInformationToLegacy(const RttLcrInformation& aidl_info,
2871                                           legacy_hal::wifi_lcr_information* legacy_info) {
2872     if (!legacy_info) {
2873         return false;
2874     }
2875     *legacy_info = {};
2876     CHECK(aidl_info.countryCode.size() == sizeof(legacy_info->country_code));
2877     memcpy(legacy_info->country_code, aidl_info.countryCode.data(), aidl_info.countryCode.size());
2878     if (aidl_info.civicInfo.size() > sizeof(legacy_info->civic_info)) {
2879         return false;
2880     }
2881     legacy_info->length = aidl_info.civicInfo.size();
2882     memcpy(legacy_info->civic_info, aidl_info.civicInfo.c_str(), aidl_info.civicInfo.size());
2883     return true;
2884 }
2885 
convertAidlRttResponderToLegacy(const RttResponder & aidl_responder,legacy_hal::wifi_rtt_responder * legacy_responder)2886 bool convertAidlRttResponderToLegacy(const RttResponder& aidl_responder,
2887                                      legacy_hal::wifi_rtt_responder* legacy_responder) {
2888     if (!legacy_responder) {
2889         return false;
2890     }
2891     *legacy_responder = {};
2892     if (!convertAidlWifiChannelInfoToLegacy(aidl_responder.channel, &legacy_responder->channel)) {
2893         return false;
2894     }
2895     legacy_responder->preamble = convertAidlRttPreambleToLegacy(aidl_responder.preamble);
2896     return true;
2897 }
2898 
convertLegacyRttResponderToAidl(const legacy_hal::wifi_rtt_responder & legacy_responder,RttResponder * aidl_responder)2899 bool convertLegacyRttResponderToAidl(const legacy_hal::wifi_rtt_responder& legacy_responder,
2900                                      RttResponder* aidl_responder) {
2901     if (!aidl_responder) {
2902         return false;
2903     }
2904     *aidl_responder = {};
2905     if (!convertLegacyWifiChannelInfoToAidl(legacy_responder.channel, &aidl_responder->channel)) {
2906         return false;
2907     }
2908     aidl_responder->preamble = convertLegacyRttPreambleToAidl(legacy_responder.preamble);
2909     return true;
2910 }
2911 
convertLegacyRttPreambleBitmapToAidl(byte legacyPreambleBitmap)2912 RttPreamble convertLegacyRttPreambleBitmapToAidl(byte legacyPreambleBitmap) {
2913     int32_t aidlPreambleBitmap = 0;
2914     for (const auto flag : {legacy_hal::WIFI_RTT_PREAMBLE_LEGACY, legacy_hal::WIFI_RTT_PREAMBLE_HT,
2915                             legacy_hal::WIFI_RTT_PREAMBLE_VHT, legacy_hal::WIFI_RTT_PREAMBLE_HE,
2916                             legacy_hal::WIFI_RTT_PREAMBLE_EHT}) {
2917         if (legacyPreambleBitmap & flag) {
2918             aidlPreambleBitmap |= static_cast<std::underlying_type<RttPreamble>::type>(
2919                     convertLegacyRttPreambleToAidl(flag));
2920         }
2921     }
2922 
2923     return static_cast<RttPreamble>(aidlPreambleBitmap);
2924 }
2925 
convertLegacyRttBwBitmapToAidl(byte legacyBwBitmap)2926 RttBw convertLegacyRttBwBitmapToAidl(byte legacyBwBitmap) {
2927     int32_t aidlBwBitmap = 0;
2928     for (const auto flag :
2929          {legacy_hal::WIFI_RTT_BW_5, legacy_hal::WIFI_RTT_BW_10, legacy_hal::WIFI_RTT_BW_20,
2930           legacy_hal::WIFI_RTT_BW_40, legacy_hal::WIFI_RTT_BW_80, legacy_hal::WIFI_RTT_BW_160,
2931           legacy_hal::WIFI_RTT_BW_320}) {
2932         if (legacyBwBitmap & flag) {
2933             aidlBwBitmap |=
2934                     static_cast<std::underlying_type<RttBw>::type>(convertLegacyRttBwToAidl(flag));
2935         }
2936     }
2937     return static_cast<RttBw>(aidlBwBitmap);
2938 }
2939 
convertLegacyRttCapabilitiesToAidl(const legacy_hal::wifi_rtt_capabilities & legacy_capabilities,RttCapabilities * aidl_capabilities)2940 bool convertLegacyRttCapabilitiesToAidl(
2941         const legacy_hal::wifi_rtt_capabilities& legacy_capabilities,
2942         RttCapabilities* aidl_capabilities) {
2943     if (!aidl_capabilities) {
2944         return false;
2945     }
2946     *aidl_capabilities = {};
2947     aidl_capabilities->rttOneSidedSupported = legacy_capabilities.rtt_one_sided_supported;
2948     aidl_capabilities->rttFtmSupported = legacy_capabilities.rtt_ftm_supported;
2949     aidl_capabilities->lciSupported = legacy_capabilities.lci_support;
2950     aidl_capabilities->lcrSupported = legacy_capabilities.lcr_support;
2951     aidl_capabilities->responderSupported = legacy_capabilities.responder_supported;
2952     aidl_capabilities->preambleSupport =
2953             convertLegacyRttPreambleBitmapToAidl(legacy_capabilities.preamble_support);
2954     aidl_capabilities->bwSupport = convertLegacyRttBwBitmapToAidl(legacy_capabilities.bw_support);
2955     aidl_capabilities->mcVersion = legacy_capabilities.mc_version;
2956     // Initialize 11az parameters to default
2957     aidl_capabilities->azPreambleSupport = (int)RttPreamble::INVALID;
2958     aidl_capabilities->azBwSupport = (int)RttBw::BW_UNSPECIFIED;
2959     aidl_capabilities->ntbInitiatorSupported = false;
2960     aidl_capabilities->ntbResponderSupported = false;
2961     return true;
2962 }
2963 
convertLegacyRttCapabilitiesV3ToAidl(const legacy_hal::wifi_rtt_capabilities_v3 & legacy_capabilities_v3,RttCapabilities * aidl_capabilities)2964 bool convertLegacyRttCapabilitiesV3ToAidl(
2965         const legacy_hal::wifi_rtt_capabilities_v3& legacy_capabilities_v3,
2966         RttCapabilities* aidl_capabilities) {
2967     if (!aidl_capabilities) {
2968         return false;
2969     }
2970     *aidl_capabilities = {};
2971     aidl_capabilities->rttOneSidedSupported =
2972             legacy_capabilities_v3.rtt_capab.rtt_one_sided_supported;
2973     aidl_capabilities->rttFtmSupported = legacy_capabilities_v3.rtt_capab.rtt_ftm_supported;
2974     aidl_capabilities->lciSupported = legacy_capabilities_v3.rtt_capab.lci_support;
2975     aidl_capabilities->lcrSupported = legacy_capabilities_v3.rtt_capab.lcr_support;
2976     aidl_capabilities->responderSupported = legacy_capabilities_v3.rtt_capab.responder_supported;
2977     aidl_capabilities->preambleSupport =
2978             convertLegacyRttPreambleBitmapToAidl(legacy_capabilities_v3.rtt_capab.preamble_support);
2979     aidl_capabilities->bwSupport =
2980             convertLegacyRttBwBitmapToAidl(legacy_capabilities_v3.rtt_capab.bw_support);
2981     aidl_capabilities->mcVersion = legacy_capabilities_v3.rtt_capab.mc_version;
2982     aidl_capabilities->azPreambleSupport =
2983             (int)convertLegacyRttPreambleBitmapToAidl(legacy_capabilities_v3.az_preamble_support);
2984     aidl_capabilities->azBwSupport =
2985             (int)convertLegacyRttBwBitmapToAidl(legacy_capabilities_v3.az_bw_support);
2986     aidl_capabilities->ntbInitiatorSupported = legacy_capabilities_v3.ntb_initiator_supported;
2987     aidl_capabilities->ntbResponderSupported = legacy_capabilities_v3.ntb_responder_supported;
2988     return true;
2989 }
2990 
convertLegacyWifiRateInfoToAidl(const legacy_hal::wifi_rate & legacy_rate,WifiRateInfo * aidl_rate)2991 bool convertLegacyWifiRateInfoToAidl(const legacy_hal::wifi_rate& legacy_rate,
2992                                      WifiRateInfo* aidl_rate) {
2993     if (!aidl_rate) {
2994         return false;
2995     }
2996     *aidl_rate = {};
2997     aidl_rate->preamble = convertLegacyWifiRatePreambleToAidl(legacy_rate.preamble);
2998     aidl_rate->nss = convertLegacyWifiRateNssToAidl(legacy_rate.nss);
2999     aidl_rate->bw = convertLegacyWifiChannelWidthToAidl(
3000             static_cast<legacy_hal::wifi_channel_width>(legacy_rate.bw));
3001     aidl_rate->rateMcsIdx = legacy_rate.rateMcsIdx;
3002     aidl_rate->bitRateInKbps = legacy_rate.bitrate;
3003     return true;
3004 }
3005 
convertLegacyRttResultToAidl(const legacy_hal::wifi_rtt_result & legacy_result,RttResult * aidl_result)3006 bool convertLegacyRttResultToAidl(const legacy_hal::wifi_rtt_result& legacy_result,
3007                                   RttResult* aidl_result) {
3008     if (!aidl_result) {
3009         return false;
3010     }
3011     *aidl_result = {};
3012     aidl_result->addr = std::array<uint8_t, 6>();
3013     CHECK(sizeof(legacy_result.addr) == aidl_result->addr.size());
3014     std::copy(legacy_result.addr, legacy_result.addr + 6, std::begin(aidl_result->addr));
3015     aidl_result->burstNum = legacy_result.burst_num;
3016     aidl_result->measurementNumber = legacy_result.measurement_number;
3017     aidl_result->successNumber = legacy_result.success_number;
3018     aidl_result->numberPerBurstPeer = legacy_result.number_per_burst_peer;
3019     aidl_result->status = convertLegacyRttStatusToAidl(legacy_result.status);
3020     aidl_result->retryAfterDuration = legacy_result.retry_after_duration;
3021     aidl_result->type = convertLegacyRttTypeToAidl(legacy_result.type);
3022     aidl_result->rssi = legacy_result.rssi;
3023     aidl_result->rssiSpread = legacy_result.rssi_spread;
3024     if (!convertLegacyWifiRateInfoToAidl(legacy_result.tx_rate, &aidl_result->txRate)) {
3025         return false;
3026     }
3027     if (!convertLegacyWifiRateInfoToAidl(legacy_result.rx_rate, &aidl_result->rxRate)) {
3028         return false;
3029     }
3030     aidl_result->rtt = legacy_result.rtt;
3031     aidl_result->rttSd = legacy_result.rtt_sd;
3032     aidl_result->rttSpread = legacy_result.rtt_spread;
3033     aidl_result->distanceInMm = legacy_result.distance_mm;
3034     aidl_result->distanceSdInMm = legacy_result.distance_sd_mm;
3035     aidl_result->distanceSpreadInMm = legacy_result.distance_spread_mm;
3036     aidl_result->timeStampInUs = legacy_result.ts;
3037     aidl_result->burstDurationInMs = legacy_result.burst_duration;
3038     aidl_result->negotiatedBurstNum = legacy_result.negotiated_burst_num;
3039     if (legacy_result.LCI && !convertLegacyIeToAidl(*legacy_result.LCI, &aidl_result->lci)) {
3040         return false;
3041     }
3042     if (legacy_result.LCR && !convertLegacyIeToAidl(*legacy_result.LCR, &aidl_result->lcr)) {
3043         return false;
3044     }
3045     return true;
3046 }
3047 
convertLegacyVectorOfRttResultToAidl(const std::vector<const legacy_hal::wifi_rtt_result * > & legacy_results,std::vector<RttResult> * aidl_results)3048 bool convertLegacyVectorOfRttResultToAidl(
3049         const std::vector<const legacy_hal::wifi_rtt_result*>& legacy_results,
3050         std::vector<RttResult>* aidl_results) {
3051     if (!aidl_results) {
3052         return false;
3053     }
3054     *aidl_results = {};
3055     for (const auto legacy_result : legacy_results) {
3056         RttResult aidl_result;
3057         if (!convertLegacyRttResultToAidl(*legacy_result, &aidl_result)) {
3058             return false;
3059         }
3060         aidl_result.channelFreqMHz = 0;
3061         aidl_result.packetBw = RttBw::BW_UNSPECIFIED;
3062         aidl_result.i2rTxLtfRepetitionCount = 0;
3063         aidl_result.r2iTxLtfRepetitionCount = 0;
3064         aidl_result.ntbMinMeasurementTime = 0;
3065         aidl_result.ntbMaxMeasurementTime = 0;
3066         aidl_result.numTxSpatialStreams = 0;
3067         aidl_result.numRxSpatialStreams = 0;
3068         aidl_results->push_back(aidl_result);
3069     }
3070     return true;
3071 }
3072 
convertLegacyVectorOfRttResultV2ToAidl(const std::vector<const legacy_hal::wifi_rtt_result_v2 * > & legacy_results,std::vector<RttResult> * aidl_results)3073 bool convertLegacyVectorOfRttResultV2ToAidl(
3074         const std::vector<const legacy_hal::wifi_rtt_result_v2*>& legacy_results,
3075         std::vector<RttResult>* aidl_results) {
3076     if (!aidl_results) {
3077         return false;
3078     }
3079     *aidl_results = {};
3080     for (const auto legacy_result : legacy_results) {
3081         RttResult aidl_result;
3082         if (!convertLegacyRttResultToAidl(legacy_result->rtt_result, &aidl_result)) {
3083             return false;
3084         }
3085         aidl_result.channelFreqMHz =
3086                 legacy_result->frequency != UNSPECIFIED ? legacy_result->frequency : 0;
3087         aidl_result.packetBw = convertLegacyRttBwToAidl(legacy_result->packet_bw);
3088         aidl_result.i2rTxLtfRepetitionCount = 0;
3089         aidl_result.r2iTxLtfRepetitionCount = 0;
3090         aidl_result.ntbMinMeasurementTime = 0;
3091         aidl_result.ntbMaxMeasurementTime = 0;
3092         aidl_result.numTxSpatialStreams = 0;
3093         aidl_result.numRxSpatialStreams = 0;
3094         aidl_results->push_back(aidl_result);
3095     }
3096     return true;
3097 }
3098 
convertLegacyVectorOfRttResultV3ToAidl(const std::vector<const legacy_hal::wifi_rtt_result_v3 * > & legacy_results,std::vector<RttResult> * aidl_results)3099 bool convertLegacyVectorOfRttResultV3ToAidl(
3100         const std::vector<const legacy_hal::wifi_rtt_result_v3*>& legacy_results,
3101         std::vector<RttResult>* aidl_results) {
3102     if (!aidl_results) {
3103         return false;
3104     }
3105     *aidl_results = {};
3106     for (const auto legacy_result : legacy_results) {
3107         RttResult aidl_result;
3108         if (!convertLegacyRttResultToAidl(legacy_result->rtt_result.rtt_result, &aidl_result)) {
3109             return false;
3110         }
3111         aidl_result.channelFreqMHz = legacy_result->rtt_result.frequency != UNSPECIFIED
3112                                              ? legacy_result->rtt_result.frequency
3113                                              : 0;
3114         aidl_result.packetBw = convertLegacyRttBwToAidl(legacy_result->rtt_result.packet_bw);
3115         aidl_result.i2rTxLtfRepetitionCount = legacy_result->i2r_tx_ltf_repetition_count;
3116         aidl_result.r2iTxLtfRepetitionCount = legacy_result->r2i_tx_ltf_repetition_count;
3117         aidl_result.ntbMinMeasurementTime = legacy_result->ntb_min_measurement_time;
3118         aidl_result.ntbMaxMeasurementTime = legacy_result->ntb_max_measurement_time;
3119         aidl_result.numTxSpatialStreams = legacy_result->num_tx_sts;
3120         aidl_result.numRxSpatialStreams = legacy_result->num_rx_sts;
3121         aidl_results->push_back(aidl_result);
3122     }
3123     return true;
3124 }
3125 
convertAidlIfaceTypeToLegacy(IfaceType aidl_interface_type)3126 legacy_hal::wifi_interface_type convertAidlIfaceTypeToLegacy(IfaceType aidl_interface_type) {
3127     switch (aidl_interface_type) {
3128         case IfaceType::STA:
3129             return legacy_hal::WIFI_INTERFACE_TYPE_STA;
3130         case IfaceType::AP:
3131             return legacy_hal::WIFI_INTERFACE_TYPE_AP;
3132         case IfaceType::P2P:
3133             return legacy_hal::WIFI_INTERFACE_TYPE_P2P;
3134         case IfaceType::NAN_IFACE:
3135             return legacy_hal::WIFI_INTERFACE_TYPE_NAN;
3136     }
3137     CHECK(false);
3138 }
3139 
convertAidlMultiStaUseCaseToLegacy(IWifiChip::MultiStaUseCase use_case)3140 legacy_hal::wifi_multi_sta_use_case convertAidlMultiStaUseCaseToLegacy(
3141         IWifiChip::MultiStaUseCase use_case) {
3142     switch (use_case) {
3143         case IWifiChip::MultiStaUseCase::DUAL_STA_TRANSIENT_PREFER_PRIMARY:
3144             return legacy_hal::WIFI_DUAL_STA_TRANSIENT_PREFER_PRIMARY;
3145         case IWifiChip::MultiStaUseCase::DUAL_STA_NON_TRANSIENT_UNBIASED:
3146             return legacy_hal::WIFI_DUAL_STA_NON_TRANSIENT_UNBIASED;
3147     }
3148     CHECK(false);
3149 }
3150 
convertAidlCoexUnsafeChannelToLegacy(const IWifiChip::CoexUnsafeChannel & aidl_unsafe_channel,legacy_hal::wifi_coex_unsafe_channel * legacy_unsafe_channel)3151 bool convertAidlCoexUnsafeChannelToLegacy(
3152         const IWifiChip::CoexUnsafeChannel& aidl_unsafe_channel,
3153         legacy_hal::wifi_coex_unsafe_channel* legacy_unsafe_channel) {
3154     if (!legacy_unsafe_channel) {
3155         return false;
3156     }
3157     *legacy_unsafe_channel = {};
3158     switch (aidl_unsafe_channel.band) {
3159         case WifiBand::BAND_24GHZ:
3160             legacy_unsafe_channel->band = legacy_hal::WLAN_MAC_2_4_BAND;
3161             break;
3162         case WifiBand::BAND_5GHZ:
3163             legacy_unsafe_channel->band = legacy_hal::WLAN_MAC_5_0_BAND;
3164             break;
3165         default:
3166             return false;
3167     };
3168     legacy_unsafe_channel->channel = aidl_unsafe_channel.channel;
3169     legacy_unsafe_channel->power_cap_dbm = aidl_unsafe_channel.powerCapDbm;
3170     return true;
3171 }
3172 
convertAidlVectorOfCoexUnsafeChannelToLegacy(const std::vector<IWifiChip::CoexUnsafeChannel> & aidl_unsafe_channels,std::vector<legacy_hal::wifi_coex_unsafe_channel> * legacy_unsafe_channels)3173 bool convertAidlVectorOfCoexUnsafeChannelToLegacy(
3174         const std::vector<IWifiChip::CoexUnsafeChannel>& aidl_unsafe_channels,
3175         std::vector<legacy_hal::wifi_coex_unsafe_channel>* legacy_unsafe_channels) {
3176     if (!legacy_unsafe_channels) {
3177         return false;
3178     }
3179     *legacy_unsafe_channels = {};
3180     for (const auto& aidl_unsafe_channel : aidl_unsafe_channels) {
3181         legacy_hal::wifi_coex_unsafe_channel legacy_unsafe_channel;
3182         if (!aidl_struct_util::convertAidlCoexUnsafeChannelToLegacy(aidl_unsafe_channel,
3183                                                                     &legacy_unsafe_channel)) {
3184             return false;
3185         }
3186         legacy_unsafe_channels->push_back(legacy_unsafe_channel);
3187     }
3188     return true;
3189 }
3190 
convertLegacyAntennaConfigurationToAidl(uint32_t antenna_cfg)3191 WifiAntennaMode convertLegacyAntennaConfigurationToAidl(uint32_t antenna_cfg) {
3192     switch (antenna_cfg) {
3193         case legacy_hal::WIFI_ANTENNA_1X1:
3194             return WifiAntennaMode::WIFI_ANTENNA_MODE_1X1;
3195         case legacy_hal::WIFI_ANTENNA_2X2:
3196             return WifiAntennaMode::WIFI_ANTENNA_MODE_2X2;
3197         case legacy_hal::WIFI_ANTENNA_3X3:
3198             return WifiAntennaMode::WIFI_ANTENNA_MODE_3X3;
3199         case legacy_hal::WIFI_ANTENNA_4X4:
3200             return WifiAntennaMode::WIFI_ANTENNA_MODE_4X4;
3201         default:
3202             return WifiAntennaMode::WIFI_ANTENNA_MODE_UNSPECIFIED;
3203     }
3204 }
3205 
convertLegacyWifiRadioConfigurationToAidl(legacy_hal::wifi_radio_configuration * radio_configuration,WifiRadioConfiguration * aidl_radio_configuration)3206 bool convertLegacyWifiRadioConfigurationToAidl(
3207         legacy_hal::wifi_radio_configuration* radio_configuration,
3208         WifiRadioConfiguration* aidl_radio_configuration) {
3209     if (!aidl_radio_configuration) {
3210         return false;
3211     }
3212     *aidl_radio_configuration = {};
3213     aidl_radio_configuration->bandInfo =
3214             aidl_struct_util::convertLegacyMacBandToAidlWifiBand(radio_configuration->band);
3215     if (aidl_radio_configuration->bandInfo == WifiBand::BAND_UNSPECIFIED) {
3216         LOG(ERROR) << "Unspecified band";
3217         return false;
3218     }
3219     aidl_radio_configuration->antennaMode =
3220             aidl_struct_util::convertLegacyAntennaConfigurationToAidl(
3221                     radio_configuration->antenna_cfg);
3222     return true;
3223 }
3224 
convertLegacyRadioCombinationsMatrixToAidl(legacy_hal::wifi_radio_combination_matrix * legacy_matrix,std::vector<WifiRadioCombination> * aidl_combinations)3225 bool convertLegacyRadioCombinationsMatrixToAidl(
3226         legacy_hal::wifi_radio_combination_matrix* legacy_matrix,
3227         std::vector<WifiRadioCombination>* aidl_combinations) {
3228     if (!aidl_combinations || !legacy_matrix) {
3229         return false;
3230     }
3231     *aidl_combinations = {};
3232 
3233     int num_combinations = legacy_matrix->num_radio_combinations;
3234     if (!num_combinations) {
3235         LOG(ERROR) << "zero radio combinations";
3236         return false;
3237     }
3238     wifi_radio_combination* l_radio_combinations_ptr = legacy_matrix->radio_combinations;
3239     for (int i = 0; i < num_combinations; i++) {
3240         int num_configurations = l_radio_combinations_ptr->num_radio_configurations;
3241         WifiRadioCombination radioCombination;
3242         std::vector<WifiRadioConfiguration> radio_configurations_vec;
3243         if (!num_configurations) {
3244             LOG(ERROR) << "zero radio configurations";
3245             return false;
3246         }
3247         for (int j = 0; j < num_configurations; j++) {
3248             WifiRadioConfiguration radioConfiguration;
3249             wifi_radio_configuration* l_radio_configurations_ptr =
3250                     &l_radio_combinations_ptr->radio_configurations[j];
3251             if (!aidl_struct_util::convertLegacyWifiRadioConfigurationToAidl(
3252                         l_radio_configurations_ptr, &radioConfiguration)) {
3253                 LOG(ERROR) << "Error converting wifi radio configuration";
3254                 return false;
3255             }
3256             radio_configurations_vec.push_back(radioConfiguration);
3257         }
3258         radioCombination.radioConfigurations = radio_configurations_vec;
3259         aidl_combinations->push_back(radioCombination);
3260         l_radio_combinations_ptr =
3261                 (wifi_radio_combination*)((u8*)l_radio_combinations_ptr +
3262                                           sizeof(wifi_radio_combination) +
3263                                           (sizeof(wifi_radio_configuration) * num_configurations));
3264     }
3265     return true;
3266 }
3267 
convertAidlNanPairingInitiatorRequestToLegacy(const NanPairingRequest & aidl_request,legacy_hal::NanPairingRequest * legacy_request)3268 bool convertAidlNanPairingInitiatorRequestToLegacy(const NanPairingRequest& aidl_request,
3269                                                    legacy_hal::NanPairingRequest* legacy_request) {
3270     if (!legacy_request) {
3271         LOG(ERROR) << "convertAidlNanPairingInitiatorRequestToLegacy: "
3272                       "legacy_request is null";
3273         return false;
3274     }
3275     *legacy_request = {};
3276 
3277     legacy_request->requestor_instance_id = aidl_request.peerId;
3278     memcpy(legacy_request->peer_disc_mac_addr, aidl_request.peerDiscMacAddr.data(), 6);
3279     legacy_request->nan_pairing_request_type =
3280             convertAidlNanPairingRequestTypeToLegacy(aidl_request.requestType);
3281     legacy_request->enable_pairing_cache = aidl_request.enablePairingCache;
3282 
3283     memcpy(legacy_request->nan_identity_key, aidl_request.pairingIdentityKey.data(),
3284            NAN_IDENTITY_KEY_LEN);
3285 
3286     legacy_request->is_opportunistic =
3287             aidl_request.securityConfig.securityType == NanPairingSecurityType::OPPORTUNISTIC ? 1
3288                                                                                               : 0;
3289     legacy_request->akm = convertAidlAkmTypeToLegacy(aidl_request.securityConfig.akm);
3290     legacy_request->cipher_type = (unsigned int)aidl_request.securityConfig.cipherType;
3291     if (aidl_request.securityConfig.securityType == NanPairingSecurityType::PMK) {
3292         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PMK;
3293         legacy_request->key_info.body.pmk_info.pmk_len = aidl_request.securityConfig.pmk.size();
3294         if (legacy_request->key_info.body.pmk_info.pmk_len != NAN_PMK_INFO_LEN) {
3295             LOG(ERROR) << "convertAidlNanPairingInitiatorRequestToLegacy: "
3296                           "invalid pmk_len";
3297             return false;
3298         }
3299         memcpy(legacy_request->key_info.body.pmk_info.pmk, aidl_request.securityConfig.pmk.data(),
3300                legacy_request->key_info.body.pmk_info.pmk_len);
3301     }
3302     if (aidl_request.securityConfig.securityType == NanPairingSecurityType::PASSPHRASE) {
3303         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PASSPHRASE;
3304         legacy_request->key_info.body.passphrase_info.passphrase_len =
3305                 aidl_request.securityConfig.passphrase.size();
3306         if (legacy_request->key_info.body.passphrase_info.passphrase_len <
3307             NAN_SECURITY_MIN_PASSPHRASE_LEN) {
3308             LOG(ERROR) << "convertAidlNanPairingInitiatorRequestToLegacy: "
3309                           "passphrase_len too small";
3310             return false;
3311         }
3312         if (legacy_request->key_info.body.passphrase_info.passphrase_len >
3313             NAN_SECURITY_MAX_PASSPHRASE_LEN) {
3314             LOG(ERROR) << "convertAidlNanPairingInitiatorRequestToLegacy: "
3315                           "passphrase_len too large";
3316             return false;
3317         }
3318         memcpy(legacy_request->key_info.body.passphrase_info.passphrase,
3319                aidl_request.securityConfig.passphrase.data(),
3320                legacy_request->key_info.body.passphrase_info.passphrase_len);
3321     }
3322 
3323     return true;
3324 }
3325 
convertAidlNanPairingIndicationResponseToLegacy(const NanRespondToPairingIndicationRequest & aidl_request,legacy_hal::NanPairingIndicationResponse * legacy_request)3326 bool convertAidlNanPairingIndicationResponseToLegacy(
3327         const NanRespondToPairingIndicationRequest& aidl_request,
3328         legacy_hal::NanPairingIndicationResponse* legacy_request) {
3329     if (!legacy_request) {
3330         LOG(ERROR) << "convertAidlNanPairingIndicationResponseToLegacy: "
3331                       "legacy_request is null";
3332         return false;
3333     }
3334     *legacy_request = {};
3335 
3336     legacy_request->pairing_instance_id = aidl_request.pairingInstanceId;
3337     legacy_request->nan_pairing_request_type =
3338             convertAidlNanPairingRequestTypeToLegacy(aidl_request.requestType);
3339     legacy_request->enable_pairing_cache = aidl_request.enablePairingCache;
3340 
3341     memcpy(legacy_request->nan_identity_key, aidl_request.pairingIdentityKey.data(),
3342            NAN_IDENTITY_KEY_LEN);
3343 
3344     legacy_request->is_opportunistic =
3345             aidl_request.securityConfig.securityType == NanPairingSecurityType::OPPORTUNISTIC ? 1
3346                                                                                               : 0;
3347     legacy_request->akm = convertAidlAkmTypeToLegacy(aidl_request.securityConfig.akm);
3348     legacy_request->cipher_type = (unsigned int)aidl_request.securityConfig.cipherType;
3349     legacy_request->rsp_code =
3350             aidl_request.acceptRequest ? NAN_PAIRING_REQUEST_ACCEPT : NAN_PAIRING_REQUEST_REJECT;
3351     if (aidl_request.securityConfig.securityType == NanPairingSecurityType::PMK) {
3352         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PMK;
3353         legacy_request->key_info.body.pmk_info.pmk_len = aidl_request.securityConfig.pmk.size();
3354         if (legacy_request->key_info.body.pmk_info.pmk_len != NAN_PMK_INFO_LEN) {
3355             LOG(ERROR) << "convertAidlNanPairingIndicationResponseToLegacy: "
3356                           "invalid pmk_len";
3357             return false;
3358         }
3359         memcpy(legacy_request->key_info.body.pmk_info.pmk, aidl_request.securityConfig.pmk.data(),
3360                legacy_request->key_info.body.pmk_info.pmk_len);
3361     }
3362     if (aidl_request.securityConfig.securityType == NanPairingSecurityType::PASSPHRASE) {
3363         legacy_request->key_info.key_type = legacy_hal::NAN_SECURITY_KEY_INPUT_PASSPHRASE;
3364         legacy_request->key_info.body.passphrase_info.passphrase_len =
3365                 aidl_request.securityConfig.passphrase.size();
3366         if (legacy_request->key_info.body.passphrase_info.passphrase_len <
3367             NAN_SECURITY_MIN_PASSPHRASE_LEN) {
3368             LOG(ERROR) << "convertAidlNanPairingIndicationResponseToLegacy: "
3369                           "passphrase_len too small";
3370             return false;
3371         }
3372         if (legacy_request->key_info.body.passphrase_info.passphrase_len >
3373             NAN_SECURITY_MAX_PASSPHRASE_LEN) {
3374             LOG(ERROR) << "convertAidlNanPairingIndicationResponseToLegacy: "
3375                           "passphrase_len too large";
3376             return false;
3377         }
3378         memcpy(legacy_request->key_info.body.passphrase_info.passphrase,
3379                aidl_request.securityConfig.passphrase.data(),
3380                legacy_request->key_info.body.passphrase_info.passphrase_len);
3381     }
3382 
3383     return true;
3384 }
3385 
convertAidlNanBootstrappingInitiatorRequestToLegacy(const NanBootstrappingRequest & aidl_request,legacy_hal::NanBootstrappingRequest * legacy_request)3386 bool convertAidlNanBootstrappingInitiatorRequestToLegacy(
3387         const NanBootstrappingRequest& aidl_request,
3388         legacy_hal::NanBootstrappingRequest* legacy_request) {
3389     if (!legacy_request) {
3390         LOG(ERROR) << "convertAidlNanBootstrappingInitiatorRequestToLegacy: "
3391                       "legacy_request is null";
3392         return false;
3393     }
3394     *legacy_request = {};
3395 
3396     legacy_request->requestor_instance_id = aidl_request.peerId;
3397     memcpy(legacy_request->peer_disc_mac_addr, aidl_request.peerDiscMacAddr.data(), 6);
3398     legacy_request->request_bootstrapping_method =
3399             convertAidlBootstrappingMethodToLegacy(aidl_request.requestBootstrappingMethod);
3400     legacy_request->cookie_length = aidl_request.cookie.size();
3401 
3402     memcpy(legacy_request->cookie, aidl_request.cookie.data(), legacy_request->cookie_length);
3403     legacy_request->publish_subscribe_id = static_cast<uint8_t>(aidl_request.discoverySessionId);
3404     legacy_request->comeback = aidl_request.isComeback ? 0x1 : 0x0;
3405 
3406     return true;
3407 }
3408 
convertAidlNanBootstrappingIndicationResponseToLegacy(const NanBootstrappingResponse & aidl_request,legacy_hal::NanBootstrappingIndicationResponse * legacy_request)3409 bool convertAidlNanBootstrappingIndicationResponseToLegacy(
3410         const NanBootstrappingResponse& aidl_request,
3411         legacy_hal::NanBootstrappingIndicationResponse* legacy_request) {
3412     if (!legacy_request) {
3413         LOG(ERROR) << "convertAidlNanBootstrappingIndicationResponseToLegacy: "
3414                       "legacy_request is null";
3415         return false;
3416     }
3417     *legacy_request = {};
3418 
3419     legacy_request->service_instance_id = aidl_request.bootstrappingInstanceId;
3420     legacy_request->bootstrapping_instance_id = aidl_request.bootstrappingInstanceId;
3421     legacy_request->rsp_code = aidl_request.acceptRequest ? NAN_BOOTSTRAPPING_REQUEST_ACCEPT
3422                                                           : NAN_BOOTSTRAPPING_REQUEST_REJECT;
3423     legacy_request->publish_subscribe_id = static_cast<uint8_t>(aidl_request.discoverySessionId);
3424 
3425     return true;
3426 }
3427 
convertLegacyNanPairingRequestIndToAidl(const legacy_hal::NanPairingRequestInd & legacy_ind,NanPairingRequestInd * aidl_ind)3428 bool convertLegacyNanPairingRequestIndToAidl(const legacy_hal::NanPairingRequestInd& legacy_ind,
3429                                              NanPairingRequestInd* aidl_ind) {
3430     if (!aidl_ind) {
3431         LOG(ERROR) << "convertLegacyNanPairingRequestIndToAidl: aidl_ind is null";
3432         return false;
3433     }
3434     *aidl_ind = {};
3435 
3436     aidl_ind->discoverySessionId = legacy_ind.publish_subscribe_id;
3437     aidl_ind->peerId = legacy_ind.requestor_instance_id;
3438     aidl_ind->peerDiscMacAddr = std::array<uint8_t, 6>();
3439     std::copy(legacy_ind.peer_disc_mac_addr, legacy_ind.peer_disc_mac_addr + 6,
3440               std::begin(aidl_ind->peerDiscMacAddr));
3441     aidl_ind->pairingInstanceId = legacy_ind.pairing_instance_id;
3442     aidl_ind->enablePairingCache = legacy_ind.enable_pairing_cache == 1;
3443     aidl_ind->requestType =
3444             convertLegacyNanPairingRequestTypeToAidl(legacy_ind.nan_pairing_request_type);
3445     if (!convertLegacyNiraToAidl(legacy_ind.nira, &aidl_ind->peerNira)) {
3446         return false;
3447     }
3448     return true;
3449 }
3450 
convertLegacyNanPairingConfirmIndToAidl(const legacy_hal::NanPairingConfirmInd & legacy_ind,NanPairingConfirmInd * aidl_ind)3451 bool convertLegacyNanPairingConfirmIndToAidl(const legacy_hal::NanPairingConfirmInd& legacy_ind,
3452                                              NanPairingConfirmInd* aidl_ind) {
3453     if (!aidl_ind) {
3454         LOG(ERROR) << "convertLegacyNanPairingRequestIndToAidl: aidl_ind is null";
3455         return false;
3456     }
3457     *aidl_ind = {};
3458 
3459     aidl_ind->pairingInstanceId = legacy_ind.pairing_instance_id;
3460     aidl_ind->enablePairingCache = legacy_ind.enable_pairing_cache == 1;
3461     aidl_ind->requestType =
3462             convertLegacyNanPairingRequestTypeToAidl(legacy_ind.nan_pairing_request_type);
3463     aidl_ind->pairingSuccess = legacy_ind.rsp_code == NAN_PAIRING_REQUEST_ACCEPT;
3464     aidl_ind->status.status = convertLegacyNanStatusTypeToAidl(legacy_ind.reason_code);
3465     if (!convertLegacyNpsaToAidl(legacy_ind.npk_security_association, &aidl_ind->npksa)) {
3466         return false;
3467     }
3468     return true;
3469 }
3470 
convertLegacyNanBootstrappingRequestIndToAidl(const legacy_hal::NanBootstrappingRequestInd & legacy_ind,NanBootstrappingRequestInd * aidl_ind)3471 bool convertLegacyNanBootstrappingRequestIndToAidl(
3472         const legacy_hal::NanBootstrappingRequestInd& legacy_ind,
3473         NanBootstrappingRequestInd* aidl_ind) {
3474     if (!aidl_ind) {
3475         LOG(ERROR) << "convertLegacyNanBootstrappingRequestIndToAidl: aidl_ind is null";
3476         return false;
3477     }
3478     *aidl_ind = {};
3479 
3480     aidl_ind->discoverySessionId = legacy_ind.publish_subscribe_id;
3481     aidl_ind->peerId = legacy_ind.requestor_instance_id;
3482     aidl_ind->peerDiscMacAddr = std::array<uint8_t, 6>();
3483     std::copy(legacy_ind.peer_disc_mac_addr, legacy_ind.peer_disc_mac_addr + 6,
3484               std::begin(aidl_ind->peerDiscMacAddr));
3485     aidl_ind->bootstrappingInstanceId = legacy_ind.bootstrapping_instance_id;
3486     aidl_ind->requestBootstrappingMethod =
3487             convertLegacyBootstrappingMethodToAidl(legacy_ind.request_bootstrapping_method);
3488     return true;
3489 }
3490 
convertLegacyNanBootstrappingConfirmIndToAidl(const legacy_hal::NanBootstrappingConfirmInd & legacy_ind,NanBootstrappingConfirmInd * aidl_ind)3491 bool convertLegacyNanBootstrappingConfirmIndToAidl(
3492         const legacy_hal::NanBootstrappingConfirmInd& legacy_ind,
3493         NanBootstrappingConfirmInd* aidl_ind) {
3494     if (!aidl_ind) {
3495         LOG(ERROR) << "convertLegacyNanBootstrappingConfirmIndToAidl: aidl_ind is null";
3496         return false;
3497     }
3498     *aidl_ind = {};
3499 
3500     aidl_ind->bootstrappingInstanceId = legacy_ind.bootstrapping_instance_id;
3501     aidl_ind->responseCode = static_cast<NanBootstrappingResponseCode>(legacy_ind.rsp_code);
3502     aidl_ind->reasonCode.status = convertLegacyNanStatusTypeToAidl(legacy_ind.reason_code);
3503     aidl_ind->comeBackDelay = legacy_ind.come_back_delay;
3504     aidl_ind->cookie =
3505             std::vector<uint8_t>(legacy_ind.cookie, legacy_ind.cookie + legacy_ind.cookie_length);
3506     return true;
3507 }
3508 
convertLegacyWifiChipCapabilitiesToAidl(const legacy_hal::wifi_chip_capabilities & legacy_chip_capabilities,WifiChipCapabilities & aidl_chip_capabilities)3509 bool convertLegacyWifiChipCapabilitiesToAidl(
3510         const legacy_hal::wifi_chip_capabilities& legacy_chip_capabilities,
3511         WifiChipCapabilities& aidl_chip_capabilities) {
3512     aidl_chip_capabilities.maxMloStrLinkCount = legacy_chip_capabilities.max_mlo_str_link_count;
3513     aidl_chip_capabilities.maxMloAssociationLinkCount =
3514             legacy_chip_capabilities.max_mlo_association_link_count;
3515     aidl_chip_capabilities.maxConcurrentTdlsSessionCount =
3516             legacy_chip_capabilities.max_concurrent_tdls_session_count;
3517     return true;
3518 }
3519 
convertAidlChannelCategoryToLegacy(uint32_t aidl_channel_category_mask)3520 uint32_t convertAidlChannelCategoryToLegacy(uint32_t aidl_channel_category_mask) {
3521     uint32_t channel_category_mask = 0;
3522     if (aidl_channel_category_mask &
3523         static_cast<int32_t>(IWifiChip::ChannelCategoryMask::INDOOR_CHANNEL)) {
3524         channel_category_mask |= legacy_hal::WIFI_INDOOR_CHANNEL;
3525     }
3526     if (aidl_channel_category_mask &
3527         static_cast<int32_t>(IWifiChip::ChannelCategoryMask::DFS_CHANNEL)) {
3528         channel_category_mask |= legacy_hal::WIFI_DFS_CHANNEL;
3529     }
3530     return channel_category_mask;
3531 }
3532 
convertLegacyIfaceMaskToIfaceConcurrencyType(u32 mask,std::vector<IfaceConcurrencyType> * types)3533 bool convertLegacyIfaceMaskToIfaceConcurrencyType(u32 mask,
3534                                                   std::vector<IfaceConcurrencyType>* types) {
3535     if (!mask) return false;
3536 
3537 #ifndef BIT
3538 #define BIT(x) (1 << (x))
3539 #endif
3540     if (mask & BIT(WIFI_INTERFACE_TYPE_STA)) types->push_back(IfaceConcurrencyType::STA);
3541     if (mask & BIT(WIFI_INTERFACE_TYPE_AP)) types->push_back(IfaceConcurrencyType::AP);
3542     if (mask & BIT(WIFI_INTERFACE_TYPE_AP_BRIDGED))
3543         types->push_back(IfaceConcurrencyType::AP_BRIDGED);
3544     if (mask & BIT(WIFI_INTERFACE_TYPE_P2P)) types->push_back(IfaceConcurrencyType::P2P);
3545     if (mask & BIT(WIFI_INTERFACE_TYPE_NAN)) types->push_back(IfaceConcurrencyType::NAN_IFACE);
3546 
3547     return true;
3548 }
3549 
convertLegacyIfaceCombinationsMatrixToChipMode(legacy_hal::wifi_iface_concurrency_matrix & legacy_matrix,IWifiChip::ChipMode * chip_mode)3550 bool convertLegacyIfaceCombinationsMatrixToChipMode(
3551         legacy_hal::wifi_iface_concurrency_matrix& legacy_matrix, IWifiChip::ChipMode* chip_mode) {
3552     if (!chip_mode) {
3553         LOG(ERROR) << "chip_mode is null";
3554         return false;
3555     }
3556     *chip_mode = {};
3557 
3558     int num_combinations = legacy_matrix.num_iface_combinations;
3559     std::vector<IWifiChip::ChipConcurrencyCombination> driver_Combinations_vec;
3560     if (!num_combinations) {
3561         LOG(ERROR) << "zero iface combinations";
3562         return false;
3563     }
3564 
3565     for (int i = 0; i < num_combinations; i++) {
3566         IWifiChip::ChipConcurrencyCombination chipComb;
3567         std::vector<IWifiChip::ChipConcurrencyCombinationLimit> limits;
3568         wifi_iface_combination* comb = &legacy_matrix.iface_combinations[i];
3569         if (!comb->num_iface_limits) continue;
3570         for (u32 j = 0; j < comb->num_iface_limits; j++) {
3571             IWifiChip::ChipConcurrencyCombinationLimit chipLimit;
3572             chipLimit.maxIfaces = comb->iface_limits[j].max_limit;
3573             std::vector<IfaceConcurrencyType> types;
3574             if (!convertLegacyIfaceMaskToIfaceConcurrencyType(comb->iface_limits[j].iface_mask,
3575                                                               &types)) {
3576                 LOG(ERROR) << "Failed to convert from iface_mask:"
3577                            << comb->iface_limits[j].iface_mask;
3578                 return false;
3579             }
3580             chipLimit.types = types;
3581             limits.push_back(chipLimit);
3582         }
3583         chipComb.limits = limits;
3584         driver_Combinations_vec.push_back(chipComb);
3585     }
3586 
3587     chip_mode->availableCombinations = driver_Combinations_vec;
3588     return true;
3589 }
3590 
convertCachedScanReportToAidl(const legacy_hal::WifiCachedScanReport & report,CachedScanData * aidl_scan_data)3591 bool convertCachedScanReportToAidl(const legacy_hal::WifiCachedScanReport& report,
3592                                    CachedScanData* aidl_scan_data) {
3593     if (!aidl_scan_data) {
3594         return false;
3595     }
3596     *aidl_scan_data = {};
3597 
3598     std::vector<CachedScanResult> aidl_scan_results;
3599     for (const auto& result : report.results) {
3600         CachedScanResult aidl_scan_result;
3601         if (!convertCachedScanResultToAidl(result, report.ts, &aidl_scan_result)) {
3602             return false;
3603         }
3604         aidl_scan_results.push_back(aidl_scan_result);
3605     }
3606     aidl_scan_data->cachedScanResults = aidl_scan_results;
3607 
3608     aidl_scan_data->scannedFrequenciesMhz = report.scanned_freqs;
3609     return true;
3610 }
3611 
convertCachedScanResultToAidl(const legacy_hal::wifi_cached_scan_result & legacy_scan_result,uint64_t ts_us,CachedScanResult * aidl_scan_result)3612 bool convertCachedScanResultToAidl(const legacy_hal::wifi_cached_scan_result& legacy_scan_result,
3613                                    uint64_t ts_us, CachedScanResult* aidl_scan_result) {
3614     if (!aidl_scan_result) {
3615         return false;
3616     }
3617     *aidl_scan_result = {};
3618     aidl_scan_result->timeStampInUs = ts_us - legacy_scan_result.age_ms * 1000;
3619     if (aidl_scan_result->timeStampInUs < 0) {
3620         aidl_scan_result->timeStampInUs = 0;
3621         return false;
3622     }
3623     size_t max_len_excluding_null = sizeof(legacy_scan_result.ssid) - 1;
3624     size_t ssid_len = strnlen((const char*)legacy_scan_result.ssid, max_len_excluding_null);
3625     aidl_scan_result->ssid =
3626             std::vector<uint8_t>(legacy_scan_result.ssid, legacy_scan_result.ssid + ssid_len);
3627     aidl_scan_result->bssid = std::array<uint8_t, 6>();
3628     std::copy(legacy_scan_result.bssid, legacy_scan_result.bssid + 6,
3629               std::begin(aidl_scan_result->bssid));
3630     aidl_scan_result->frequencyMhz = legacy_scan_result.chanspec.primary_frequency;
3631     aidl_scan_result->channelWidthMhz =
3632             convertLegacyWifiChannelWidthToAidl(legacy_scan_result.chanspec.width);
3633     aidl_scan_result->rssiDbm = legacy_scan_result.rssi;
3634     aidl_scan_result->preambleType = convertScanResultFlagsToPreambleType(legacy_scan_result.flags);
3635     return true;
3636 }
3637 
convertScanResultFlagsToPreambleType(int flags)3638 WifiRatePreamble convertScanResultFlagsToPreambleType(int flags) {
3639     if ((flags & WIFI_CACHED_SCAN_RESULT_FLAGS_EHT_OPS_PRESENT) > 0) {
3640         return WifiRatePreamble::EHT;
3641     }
3642     if ((flags & WIFI_CACHED_SCAN_RESULT_FLAGS_HE_OPS_PRESENT) > 0) {
3643         return WifiRatePreamble::HE;
3644     }
3645     if ((flags & WIFI_CACHED_SCAN_RESULT_FLAGS_VHT_OPS_PRESENT) > 0) {
3646         return WifiRatePreamble::VHT;
3647     }
3648     if ((flags & WIFI_CACHED_SCAN_RESULT_FLAGS_HT_OPS_PRESENT) > 0) {
3649         return WifiRatePreamble::HT;
3650     }
3651     return WifiRatePreamble::OFDM;
3652 }
3653 
convertTwtCapabilitiesToAidl(legacy_hal::wifi_twt_capabilities legacy_twt_capabs,TwtCapabilities * aidl_twt_capabs)3654 bool convertTwtCapabilitiesToAidl(legacy_hal::wifi_twt_capabilities legacy_twt_capabs,
3655                                   TwtCapabilities* aidl_twt_capabs) {
3656     if (!aidl_twt_capabs) {
3657         return false;
3658     }
3659     aidl_twt_capabs->isTwtRequesterSupported = legacy_twt_capabs.is_twt_requester_supported;
3660     aidl_twt_capabs->isTwtResponderSupported = legacy_twt_capabs.is_twt_responder_supported;
3661     aidl_twt_capabs->isBroadcastTwtSupported = legacy_twt_capabs.is_flexible_twt_supported;
3662     if (legacy_twt_capabs.min_wake_duration_micros > legacy_twt_capabs.max_wake_duration_micros) {
3663         return false;
3664     }
3665     aidl_twt_capabs->minWakeDurationUs = legacy_twt_capabs.min_wake_duration_micros;
3666     aidl_twt_capabs->maxWakeDurationUs = legacy_twt_capabs.max_wake_duration_micros;
3667     if (legacy_twt_capabs.min_wake_interval_micros > legacy_twt_capabs.max_wake_interval_micros) {
3668         return false;
3669     }
3670     aidl_twt_capabs->minWakeIntervalUs = legacy_twt_capabs.min_wake_interval_micros;
3671     aidl_twt_capabs->maxWakeIntervalUs = legacy_twt_capabs.max_wake_interval_micros;
3672     return true;
3673 }
3674 
convertAidlTwtRequestToLegacy(const TwtRequest aidl_twt_request,legacy_hal::wifi_twt_request * legacy_twt_request)3675 bool convertAidlTwtRequestToLegacy(const TwtRequest aidl_twt_request,
3676                                    legacy_hal::wifi_twt_request* legacy_twt_request) {
3677     if (legacy_twt_request == nullptr) {
3678         return false;
3679     }
3680     legacy_twt_request->mlo_link_id = aidl_twt_request.mloLinkId;
3681     if (aidl_twt_request.minWakeDurationUs > aidl_twt_request.maxWakeDurationUs) {
3682         return false;
3683     }
3684     legacy_twt_request->min_wake_duration_micros = aidl_twt_request.minWakeDurationUs;
3685     legacy_twt_request->max_wake_duration_micros = aidl_twt_request.maxWakeDurationUs;
3686     if (aidl_twt_request.minWakeIntervalUs > aidl_twt_request.maxWakeIntervalUs) {
3687         return false;
3688     }
3689     legacy_twt_request->min_wake_interval_micros = aidl_twt_request.minWakeIntervalUs;
3690     legacy_twt_request->max_wake_interval_micros = aidl_twt_request.maxWakeIntervalUs;
3691     return true;
3692 }
3693 
convertLegacyHalTwtErrorCodeToAidl(legacy_hal::wifi_twt_error_code legacy_error_code)3694 IWifiStaIfaceEventCallback::TwtErrorCode convertLegacyHalTwtErrorCodeToAidl(
3695         legacy_hal::wifi_twt_error_code legacy_error_code) {
3696     switch (legacy_error_code) {
3697         case WIFI_TWT_ERROR_CODE_TIMEOUT:
3698             return IWifiStaIfaceEventCallback::TwtErrorCode::TIMEOUT;
3699         case WIFI_TWT_ERROR_CODE_PEER_REJECTED:
3700             return IWifiStaIfaceEventCallback::TwtErrorCode::PEER_REJECTED;
3701         case WIFI_TWT_ERROR_CODE_PEER_NOT_SUPPORTED:
3702             return IWifiStaIfaceEventCallback::TwtErrorCode::PEER_NOT_SUPPORTED;
3703         case WIFI_TWT_ERROR_CODE_NOT_SUPPORTED:
3704             return IWifiStaIfaceEventCallback::TwtErrorCode::NOT_SUPPORTED;
3705         case WIFI_TWT_ERROR_CODE_NOT_AVAILABLE:
3706             return IWifiStaIfaceEventCallback::TwtErrorCode::NOT_AVAILABLE;
3707         case WIFI_TWT_ERROR_CODE_MAX_SESSION_REACHED:
3708             return IWifiStaIfaceEventCallback::TwtErrorCode::MAX_SESSION_REACHED;
3709         case WIFI_TWT_ERROR_CODE_INVALID_PARAMS:
3710             return IWifiStaIfaceEventCallback::TwtErrorCode::INVALID_PARAMS;
3711         case WIFI_TWT_ERROR_CODE_ALREADY_SUSPENDED:
3712             return IWifiStaIfaceEventCallback::TwtErrorCode::ALREADY_SUSPENDED;
3713         case WIFI_TWT_ERROR_CODE_ALREADY_RESUMED:
3714             return IWifiStaIfaceEventCallback::TwtErrorCode::ALREADY_RESUMED;
3715         default:
3716             return IWifiStaIfaceEventCallback::TwtErrorCode::FAILURE_UNKNOWN;
3717     }
3718 }
3719 
convertLegacyHalTwtReasonCodeToAidl(legacy_hal::wifi_twt_teardown_reason_code legacy_reason_code)3720 IWifiStaIfaceEventCallback::TwtTeardownReasonCode convertLegacyHalTwtReasonCodeToAidl(
3721         legacy_hal::wifi_twt_teardown_reason_code legacy_reason_code) {
3722     switch (legacy_reason_code) {
3723         case WIFI_TWT_TEARDOWN_REASON_CODE_LOCALLY_REQUESTED:
3724             return IWifiStaIfaceEventCallback::TwtTeardownReasonCode::LOCALLY_REQUESTED;
3725         case WIFI_TWT_TEARDOWN_REASON_CODE_INTERNALLY_INITIATED:
3726             return IWifiStaIfaceEventCallback::TwtTeardownReasonCode::INTERNALLY_INITIATED;
3727         case WIFI_TWT_TEARDOWN_REASON_CODE_PEER_INITIATED:
3728             return IWifiStaIfaceEventCallback::TwtTeardownReasonCode::PEER_INITIATED;
3729         default:
3730             return IWifiStaIfaceEventCallback::TwtTeardownReasonCode::UNKNOWN;
3731     }
3732 }
3733 
convertLegacyHalTwtSessionToAidl(legacy_hal::wifi_twt_session twt_session,TwtSession * aidl_twt_session)3734 bool convertLegacyHalTwtSessionToAidl(legacy_hal::wifi_twt_session twt_session,
3735                                       TwtSession* aidl_twt_session) {
3736     if (aidl_twt_session == nullptr) {
3737         return false;
3738     }
3739 
3740     aidl_twt_session->sessionId = twt_session.session_id;
3741     aidl_twt_session->mloLinkId = twt_session.mlo_link_id;
3742     aidl_twt_session->wakeDurationUs = twt_session.wake_duration_micros;
3743     aidl_twt_session->wakeIntervalUs = twt_session.wake_interval_micros;
3744     switch (twt_session.negotiation_type) {
3745         case WIFI_TWT_NEGO_TYPE_INDIVIDUAL:
3746             aidl_twt_session->negotiationType = TwtSession::TwtNegotiationType::INDIVIDUAL;
3747             break;
3748         case WIFI_TWT_NEGO_TYPE_BROADCAST:
3749             aidl_twt_session->negotiationType = TwtSession::TwtNegotiationType::BROADCAST;
3750             break;
3751         default:
3752             return false;
3753     }
3754     aidl_twt_session->isTriggerEnabled = twt_session.is_trigger_enabled;
3755     aidl_twt_session->isAnnounced = twt_session.is_announced;
3756     aidl_twt_session->isImplicit = twt_session.is_implicit;
3757     aidl_twt_session->isProtected = twt_session.is_protected;
3758     aidl_twt_session->isUpdatable = twt_session.is_updatable;
3759     aidl_twt_session->isSuspendable = twt_session.is_suspendable;
3760     aidl_twt_session->isResponderPmModeEnabled = twt_session.is_responder_pm_mode_enabled;
3761     return true;
3762 }
3763 
convertLegacyHalTwtSessionStatsToAidl(legacy_hal::wifi_twt_session_stats twt_stats,TwtSessionStats * aidl_twt_stats)3764 bool convertLegacyHalTwtSessionStatsToAidl(legacy_hal::wifi_twt_session_stats twt_stats,
3765                                            TwtSessionStats* aidl_twt_stats) {
3766     if (aidl_twt_stats == nullptr) {
3767         return false;
3768     }
3769 
3770     aidl_twt_stats->avgTxPktCount = twt_stats.avg_pkt_num_tx;
3771     aidl_twt_stats->avgRxPktCount = twt_stats.avg_pkt_num_rx;
3772     aidl_twt_stats->avgTxPktSize = twt_stats.avg_tx_pkt_size;
3773     aidl_twt_stats->avgRxPktSize = twt_stats.avg_rx_pkt_size;
3774     aidl_twt_stats->avgEospDurationUs = twt_stats.avg_eosp_dur_us;
3775     aidl_twt_stats->eospCount = twt_stats.eosp_count;
3776 
3777     return true;
3778 }
3779 
3780 }  // namespace aidl_struct_util
3781 }  // namespace wifi
3782 }  // namespace hardware
3783 }  // namespace android
3784 }  // namespace aidl
3785