1 /*
2 * Copyright 2020 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 "dumpsys/reflection_schema.h"
18
19 #include <string>
20
21 #include "bundler_schema_generated.h"
22 #include "flatbuffers/flatbuffers.h"
23 #include "flatbuffers/idl.h"
24 #include "os/log.h"
25
26 using namespace bluetooth;
27
ReflectionSchema(const std::string & pre_bundled_schema)28 dumpsys::ReflectionSchema::ReflectionSchema(const std::string& pre_bundled_schema)
29 : pre_bundled_schema_(pre_bundled_schema) {
30 bundled_schema_ = flatbuffers::GetRoot<bluetooth::dumpsys::BundledSchema>(pre_bundled_schema_.data());
31 ASSERT(bundled_schema_ != nullptr);
32 }
33
GetNumberOfBundledSchemas() const34 int dumpsys::ReflectionSchema::GetNumberOfBundledSchemas() const {
35 return bundled_schema_->map()->size();
36 }
37
GetTitle() const38 std::string dumpsys::ReflectionSchema::GetTitle() const {
39 return bundled_schema_->title()->str();
40 }
41
GetRootName() const42 std::string dumpsys::ReflectionSchema::GetRootName() const {
43 return bundled_schema_->root_name()->str();
44 }
45
GetRootReflectionSchema() const46 const reflection::Schema* dumpsys::ReflectionSchema::GetRootReflectionSchema() const {
47 return FindInReflectionSchema(GetRootName());
48 }
49
FindInReflectionSchema(const std::string & name) const50 const reflection::Schema* dumpsys::ReflectionSchema::FindInReflectionSchema(const std::string& name) const {
51 const flatbuffers::Vector<flatbuffers::Offset<bluetooth::dumpsys::BundledSchemaMap>>* map = bundled_schema_->map();
52
53 for (auto it = map->cbegin(); it != map->cend(); ++it) {
54 if (it->name()->str() == name) {
55 flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t*>(it->data()->Data()), it->data()->size());
56 if (!reflection::VerifySchemaBuffer(verifier)) {
57 LOG_WARN("Unable to verify schema buffer name:%s", name.c_str());
58 return nullptr;
59 }
60 return reflection::GetSchema(it->data()->Data());
61 }
62 }
63 return nullptr;
64 }
65
PrintReflectionSchema() const66 void dumpsys::ReflectionSchema::PrintReflectionSchema() const {
67 const flatbuffers::Vector<flatbuffers::Offset<bluetooth::dumpsys::BundledSchemaMap>>* map = bundled_schema_->map();
68 LOG_INFO(
69 " Bundled schema title:%s root_name:%s",
70 bundled_schema_->title()->c_str(),
71 bundled_schema_->root_name()->c_str());
72 for (auto it = map->cbegin(); it != map->cend(); ++it) {
73 LOG_INFO(" schema:%s", it->name()->c_str());
74 }
75 }
76
VerifyReflectionSchema() const77 bool dumpsys::ReflectionSchema::VerifyReflectionSchema() const {
78 const flatbuffers::Vector<flatbuffers::Offset<bluetooth::dumpsys::BundledSchemaMap>>* map = bundled_schema_->map();
79
80 for (auto it = map->cbegin(); it != map->cend(); ++it) {
81 flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t*>(it->data()->Data()), it->data()->size());
82 if (!reflection::VerifySchemaBuffer(verifier)) {
83 return false;
84 }
85 }
86 return true;
87 }
88