blob: d21e02adb260391554465c6b6e84877695325617 [file] [log] [blame]
Shawn Willdenf4527742017-11-09 15:59:39 -07001/*
2 **
3 ** Copyright 2016, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18#ifndef SYSTEM_VOLD_KEYSTORE_HIDL_SUPPORT_H_
19#define SYSTEM_VOLD_KEYSTORE_HIDL_SUPPORT_H_
20
21#include <ostream>
22#include <sstream>
23#include <string>
24
25#include <hidl/Status.h>
26
27#include "keymaster_tags.h"
28
29namespace keystore {
30
31inline static std::ostream& formatArgs(std::ostream& out) {
32 return out;
33}
34
35template <typename First, typename... Args>
36inline static std::ostream& formatArgs(std::ostream& out, First&& first, Args&&... args) {
37 out << first;
38 return formatArgs(out, args...);
39}
40
41template <typename... Args>
42inline static std::string argsToString(Args&&... args) {
43 std::stringstream s;
44 formatArgs(s, args...);
45 return s.str();
46}
47
48template <typename... Msgs>
49inline static ErrorCode ksHandleHidlError(const Return<ErrorCode>& error, Msgs&&... msgs) {
50 if (!error.isOk()) {
51 ALOGE("HIDL call failed with %s @ %s", error.description().c_str(),
52 argsToString(msgs...).c_str());
53 return ErrorCode::UNKNOWN_ERROR;
54 }
55 return ErrorCode(error);
56}
57template <typename... Msgs>
58inline static ErrorCode ksHandleHidlError(const Return<void>& error, Msgs&&... msgs) {
59 if (!error.isOk()) {
60 ALOGE("HIDL call failed with %s @ %s", error.description().c_str(),
61 argsToString(msgs...).c_str());
62 return ErrorCode::UNKNOWN_ERROR;
63 }
64 return ErrorCode::OK;
65}
66
67#define KS_HANDLE_HIDL_ERROR(rc) \
68 ::keystore::ksHandleHidlError(rc, __FILE__, ":", __LINE__, ":", __PRETTY_FUNCTION__)
69
70inline static hidl_vec<uint8_t> blob2hidlVec(const uint8_t* data, const size_t length,
71 bool inPlace = true) {
72 hidl_vec<uint8_t> result;
73 if (inPlace)
74 result.setToExternal(const_cast<unsigned char*>(data), length);
75 else {
76 result.resize(length);
77 memcpy(&result[0], data, length);
78 }
79 return result;
80}
81
82inline static hidl_vec<uint8_t> blob2hidlVec(const std::string& value) {
83 hidl_vec<uint8_t> result;
84 result.setToExternal(
85 reinterpret_cast<uint8_t*>(const_cast<std::string::value_type*>(value.data())),
86 static_cast<size_t>(value.size()));
87 return result;
88}
89
90inline static hidl_vec<uint8_t> blob2hidlVec(const std::vector<uint8_t>& blob) {
91 hidl_vec<uint8_t> result;
92 result.setToExternal(const_cast<uint8_t*>(blob.data()), static_cast<size_t>(blob.size()));
93 return result;
94}
95
96template <typename T, typename OutIter>
97inline static OutIter copy_bytes_to_iterator(const T& value, OutIter dest) {
98 const uint8_t* value_ptr = reinterpret_cast<const uint8_t*>(&value);
99 return std::copy(value_ptr, value_ptr + sizeof(value), dest);
100}
101
102inline std::string hidlVec2String(const hidl_vec<uint8_t>& value) {
103 return std::string(reinterpret_cast<const std::string::value_type*>(&value[0]), value.size());
104}
105
106} // namespace keystore
107
108#endif // SYSTEM_VOLD_KEYSTORE_HIDL_SUPPORT_H_