/* ** ** Copyright 2016, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ #ifndef SYSTEM_VOLD_KEYSTORE_HIDL_SUPPORT_H_ #define SYSTEM_VOLD_KEYSTORE_HIDL_SUPPORT_H_ #include #include #include #include #include "keymaster_tags.h" namespace keystore { inline static std::ostream& formatArgs(std::ostream& out) { return out; } template inline static std::ostream& formatArgs(std::ostream& out, First&& first, Args&&... args) { out << first; return formatArgs(out, args...); } template inline static std::string argsToString(Args&&... args) { std::stringstream s; formatArgs(s, args...); return s.str(); } template inline static ErrorCode ksHandleHidlError(const Return& error, Msgs&&... msgs) { if (!error.isOk()) { ALOGE("HIDL call failed with %s @ %s", error.description().c_str(), argsToString(msgs...).c_str()); return ErrorCode::UNKNOWN_ERROR; } return ErrorCode(error); } template inline static ErrorCode ksHandleHidlError(const Return& error, Msgs&&... msgs) { if (!error.isOk()) { ALOGE("HIDL call failed with %s @ %s", error.description().c_str(), argsToString(msgs...).c_str()); return ErrorCode::UNKNOWN_ERROR; } return ErrorCode::OK; } #define KS_HANDLE_HIDL_ERROR(rc) \ ::keystore::ksHandleHidlError(rc, __FILE__, ":", __LINE__, ":", __PRETTY_FUNCTION__) inline static hidl_vec blob2hidlVec(const uint8_t* data, const size_t length, bool inPlace = true) { hidl_vec result; if (inPlace) result.setToExternal(const_cast(data), length); else { result.resize(length); memcpy(&result[0], data, length); } return result; } inline static hidl_vec blob2hidlVec(const std::string& value) { hidl_vec result; result.setToExternal( reinterpret_cast(const_cast(value.data())), static_cast(value.size())); return result; } inline static hidl_vec blob2hidlVec(const std::vector& blob) { hidl_vec result; result.setToExternal(const_cast(blob.data()), static_cast(blob.size())); return result; } template inline static OutIter copy_bytes_to_iterator(const T& value, OutIter dest) { const uint8_t* value_ptr = reinterpret_cast(&value); return std::copy(value_ptr, value_ptr + sizeof(value), dest); } inline std::string hidlVec2String(const hidl_vec& value) { return std::string(reinterpret_cast(&value[0]), value.size()); } } // namespace keystore #endif // SYSTEM_VOLD_KEYSTORE_HIDL_SUPPORT_H_