• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant - Utilities for the mainline supplicant
3  * Copyright (c) 2024, Google Inc. All rights reserved.
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef MAINLINE_SUPPLICANT_UTILS_H
10 #define MAINLINE_SUPPLICANT_UTILS_H
11 
12 #include "callback_manager.h"
13 
14 #include <aidl/android/system/wifi/mainline_supplicant/SupplicantStatusCode.h>
15 
16 extern "C"
17 {
18 #include "utils/common.h"
19 #include "wpabuf.h"
20 }
21 
22 namespace {
23 // Custom deleter for wpabuf
freeWpaBuf(wpabuf * ptr)24 void freeWpaBuf(wpabuf *ptr) { wpabuf_free(ptr); }
25 }
26 
27 using ::aidl::android::system::wifi::mainline_supplicant::SupplicantStatusCode;
28 
29 using wpabuf_unique_ptr = std::unique_ptr<wpabuf, void (*)(wpabuf *)>;
30 
createStatus(SupplicantStatusCode statusCode)31 inline ndk::ScopedAStatus createStatus(SupplicantStatusCode statusCode) {
32     return ndk::ScopedAStatus::fromServiceSpecificError(
33         static_cast<int32_t>(statusCode));
34 }
35 
createStatusWithMsg(SupplicantStatusCode statusCode,std::string msg)36 inline ndk::ScopedAStatus createStatusWithMsg(
37         SupplicantStatusCode statusCode, std::string msg) {
38     return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
39         static_cast<int32_t>(statusCode), msg.c_str());
40 }
41 
42 // Check whether the container is within the maximum size
43 template <typename T>
checkContainerSize(const T & container,int maxSize)44 inline bool checkContainerSize(const T& container, int maxSize) {
45     return container.size() <= maxSize;
46 }
47 
48 // Check whether the enum value is within the specified range
49 template <typename T>
isValidEnumValue(T value,T enumRangeMin,T enumRangeMax)50 inline bool isValidEnumValue(T value, T enumRangeMin, T enumRangeMax) {
51     return static_cast<uint32_t>(value) >= static_cast<uint32_t>(enumRangeMin)
52         && static_cast<uint32_t>(value) <= static_cast<uint32_t>(enumRangeMax);
53 }
54 
55 // Create a unique_ptr for a wpabuf ptr with a custom deleter
createWpaBufUniquePtr(struct wpabuf * raw_ptr)56 inline wpabuf_unique_ptr createWpaBufUniquePtr(struct wpabuf *raw_ptr) {
57     return {raw_ptr, freeWpaBuf};
58 }
59 
60 // Create a wpabuf ptr with a custom deleter, copying the data from the provided vector
convertVectorToWpaBuf(const std::vector<uint8_t> & data)61 inline wpabuf_unique_ptr convertVectorToWpaBuf(const std::vector<uint8_t> &data) {
62     return createWpaBufUniquePtr(wpabuf_alloc_copy(data.data(), data.size()));
63 }
64 
65 // Convert a byte array representation of a MAC address to an std::array
macAddrBytesToArray(const uint8_t * mac_addr)66 inline std::array<uint8_t, ETH_ALEN> macAddrBytesToArray(const uint8_t* mac_addr) {
67     std::array<uint8_t, ETH_ALEN> arr;
68     std::copy(mac_addr, mac_addr + ETH_ALEN, std::begin(arr));
69     return arr;
70 }
71 
72 // Convert a byte array to an std::vector
byteArrToVec(const uint8_t * arr,int len)73 inline std::vector<uint8_t> byteArrToVec(const uint8_t* arr, int len) {
74     return std::vector<uint8_t>{arr, arr + len};
75 }
76 
77 // Wrapper to retrieve a STA iface callback registered with the callback manager
getStaIfaceCallback(std::string ifaceName)78 static std::shared_ptr<IStaInterfaceCallback> getStaIfaceCallback(
79         std::string ifaceName) {
80     CallbackManager* callback_manager = CallbackManager::getInstance();
81     if (!callback_manager) {
82         return nullptr;
83     }
84     return callback_manager->getStaIfaceCallback(ifaceName);
85 }
86 
87 #endif // MAINLINE_SUPPLICANT_UTILS_H
88