• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hidl interface for wpa_supplicant daemon
3  * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
4  * Copyright (c) 2004-2016, Roshan Pius <rpius@google.com>
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #ifndef MISC_UTILS_H_
11 #define MISC_UTILS_H_
12 
13 extern "C"
14 {
15 #include "wpabuf.h"
16 }
17 
18 namespace {
19 constexpr size_t kWpsPinNumDigits = 8;
20 // Custom deleter for wpabuf.
freeWpaBuf(wpabuf * ptr)21 void freeWpaBuf(wpabuf *ptr) { wpabuf_free(ptr); }
22 }  // namespace
23 
24 namespace android {
25 namespace hardware {
26 namespace wifi {
27 namespace supplicant {
28 namespace V1_2 {
29 namespace implementation {
30 namespace misc_utils {
31 using wpabuf_unique_ptr = std::unique_ptr<wpabuf, void (*)(wpabuf *)>;
32 
33 // Creates a unique_ptr for wpabuf ptr with a custom deleter.
createWpaBufUniquePtr(struct wpabuf * raw_ptr)34 inline wpabuf_unique_ptr createWpaBufUniquePtr(struct wpabuf *raw_ptr)
35 {
36 	return {raw_ptr, freeWpaBuf};
37 }
38 
39 // Creates a wpabuf ptr with a custom deleter copying the data from the provided
40 // vector.
convertVectorToWpaBuf(const std::vector<uint8_t> & data)41 inline wpabuf_unique_ptr convertVectorToWpaBuf(const std::vector<uint8_t> &data)
42 {
43 	return createWpaBufUniquePtr(
44 	    wpabuf_alloc_copy(data.data(), data.size()));
45 }
46 
47 // Copies the provided wpabuf contents to a std::vector.
convertWpaBufToVector(const struct wpabuf * buf)48 inline std::vector<uint8_t> convertWpaBufToVector(const struct wpabuf *buf)
49 {
50 	if (buf) {
51 		return std::vector<uint8_t>(
52 		    wpabuf_head_u8(buf), wpabuf_head_u8(buf) + wpabuf_len(buf));
53 	} else {
54 		return std::vector<uint8_t>();
55 	}
56 }
57 
58 // Returns a string holding the wps pin.
convertWpsPinToString(int pin)59 inline std::string convertWpsPinToString(int pin)
60 {
61 	char pin_str[kWpsPinNumDigits + 1];
62 	snprintf(pin_str, sizeof(pin_str), "%08d", pin);
63 	return pin_str;
64 }
65 
66 }  // namespace misc_utils
67 }  // namespace implementation
68 }  // namespace V1_2
69 }  // namespace supplicant
70 }  // namespace wifi
71 }  // namespace hardware
72 }  // namespace android
73 #endif  // MISC_UTILS_H_
74