summaryrefslogtreecommitdiff
path: root/system/gd/dumpsys/reflection_schema.cc
blob: 52ceaa2aaf4e0e92340b8dbc4dbdd83e3b68370f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright 2020 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.
 */

#include "dumpsys/reflection_schema.h"

#include <string>

#include "bundler_schema_generated.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "os/log.h"

using namespace bluetooth;

dumpsys::ReflectionSchema::ReflectionSchema(const std::string& pre_bundled_schema)
    : pre_bundled_schema_(pre_bundled_schema) {
  bundled_schema_ = flatbuffers::GetRoot<bluetooth::dumpsys::BundledSchema>(pre_bundled_schema_.data());
  ASSERT(bundled_schema_ != nullptr);
}

int dumpsys::ReflectionSchema::GetNumberOfBundledSchemas() const {
  return bundled_schema_->map()->size();
}

std::string dumpsys::ReflectionSchema::GetTitle() const {
  return bundled_schema_->title()->str();
}

std::string dumpsys::ReflectionSchema::GetRootName() const {
  return bundled_schema_->root_name()->str();
}

const reflection::Schema* dumpsys::ReflectionSchema::GetRootReflectionSchema() const {
  return FindInReflectionSchema(GetRootName());
}

const reflection::Schema* dumpsys::ReflectionSchema::FindInReflectionSchema(const std::string& name) const {
  const flatbuffers::Vector<flatbuffers::Offset<bluetooth::dumpsys::BundledSchemaMap>>* map = bundled_schema_->map();

  for (auto it = map->cbegin(); it != map->cend(); ++it) {
    if (it->name()->str() == name) {
      flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t*>(it->data()->Data()), it->data()->size());
      if (!reflection::VerifySchemaBuffer(verifier)) {
        LOG_WARN("Unable to verify schema buffer name:%s", name.c_str());
        return nullptr;
      }
      return reflection::GetSchema(it->data()->Data());
    }
  }
  return nullptr;
}

void dumpsys::ReflectionSchema::PrintReflectionSchema() const {
  const flatbuffers::Vector<flatbuffers::Offset<bluetooth::dumpsys::BundledSchemaMap>>* map = bundled_schema_->map();
  LOG_INFO(
      "  Bundled schema title:%s root_name:%s",
      bundled_schema_->title()->c_str(),
      bundled_schema_->root_name()->c_str());
  for (auto it = map->cbegin(); it != map->cend(); ++it) {
    LOG_INFO("    schema:%s", it->name()->c_str());
  }
}

bool dumpsys::ReflectionSchema::VerifyReflectionSchema() const {
  const flatbuffers::Vector<flatbuffers::Offset<bluetooth::dumpsys::BundledSchemaMap>>* map = bundled_schema_->map();

  for (auto it = map->cbegin(); it != map->cend(); ++it) {
    flatbuffers::Verifier verifier(reinterpret_cast<const uint8_t*>(it->data()->Data()), it->data()->size());
    if (!reflection::VerifySchemaBuffer(verifier)) {
      return false;
    }
  }
  return true;
}