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 #define LOG_TAG "bt_storage"
17
18 #include "storage/legacy.h"
19 #include "storage/legacy_osi_config.h"
20
21 namespace bluetooth {
22 namespace storage {
23
24 struct LegacyModule::impl {
config_readbluetooth::storage::LegacyModule::impl25 void config_read(const std::string filename, LegacyReadConfigCallback callback, os::Handler* handler) {
26 std::unique_ptr<config_t> config = legacy::osi::config::config_new(filename.c_str());
27 if (config && !legacy::osi::config::config_has_section(*config, "Adapter")) {
28 LOG_ERROR("Config is missing adapter section");
29 config = nullptr;
30 }
31 if (!config) {
32 config = legacy::osi::config::config_new_empty();
33 }
34 handler->Post(common::BindOnce(std::move(callback), filename, std::move(config)));
35 }
36
config_writebluetooth::storage::LegacyModule::impl37 void config_write(const std::string filename, const config_t config, LegacyWriteConfigCallback callback,
38 os::Handler* handler) {
39 handler->Post(common::BindOnce(std::move(callback), filename, legacy::osi::config::config_save(config, filename)));
40 }
41
checksum_readbluetooth::storage::LegacyModule::impl42 void checksum_read(const std::string filename, LegacyReadChecksumCallback callback, os::Handler* handler) {
43 handler->Post(
44 common::BindOnce(std::move(callback), filename, legacy::osi::config::checksum_read(filename.c_str())));
45 }
46
checksum_writebluetooth::storage::LegacyModule::impl47 void checksum_write(const std::string filename, const std::string checksum, LegacyWriteChecksumCallback callback,
48 os::Handler* handler) {
49 handler->Post(
50 common::BindOnce(std::move(callback), filename, legacy::osi::config::checksum_save(checksum, filename)));
51 }
52
53 void Start();
54 void Stop();
55
56 impl(const LegacyModule& name_module);
57
58 private:
59 const LegacyModule& module_;
60 os::Handler* handler_;
61 };
62
__anon1f9e022a0102() 63 const ModuleFactory storage::LegacyModule::Factory = ModuleFactory([]() { return new storage::LegacyModule(); });
64
impl(const storage::LegacyModule & module)65 storage::LegacyModule::impl::impl(const storage::LegacyModule& module) : module_(module) {}
66
Start()67 void storage::LegacyModule::impl::Start() {
68 handler_ = module_.GetHandler();
69 }
70
Stop()71 void storage::LegacyModule::impl::Stop() {}
72
ConfigRead(const std::string filename,LegacyReadConfigCallback callback,os::Handler * handler)73 void storage::LegacyModule::ConfigRead(const std::string filename, LegacyReadConfigCallback callback,
74 os::Handler* handler) {
75 GetHandler()->Post(common::BindOnce(&LegacyModule::impl::config_read, common::Unretained(pimpl_.get()), filename,
76 std::move(callback), handler));
77 }
78
ConfigWrite(const std::string filename,const config_t & config,LegacyWriteConfigCallback callback,os::Handler * handler)79 void storage::LegacyModule::ConfigWrite(const std::string filename, const config_t& config,
80 LegacyWriteConfigCallback callback, os::Handler* handler) {
81 GetHandler()->Post(common::BindOnce(&LegacyModule::impl::config_write, common::Unretained(pimpl_.get()), filename,
82 config, std::move(callback), handler));
83 }
84
ChecksumRead(const std::string filename,LegacyReadChecksumCallback callback,os::Handler * handler)85 void storage::LegacyModule::ChecksumRead(const std::string filename, LegacyReadChecksumCallback callback,
86 os::Handler* handler) {
87 GetHandler()->Post(common::BindOnce(&LegacyModule::impl::checksum_read, common::Unretained(pimpl_.get()), filename,
88 std::move(callback), handler));
89 }
90
ChecksumWrite(const std::string filename,const std::string checksum,LegacyWriteChecksumCallback callback,os::Handler * handler)91 void storage::LegacyModule::ChecksumWrite(const std::string filename, const std::string checksum,
92 LegacyWriteChecksumCallback callback, os::Handler* handler) {
93 GetHandler()->Post(common::BindOnce(&LegacyModule::impl::checksum_write, common::Unretained(pimpl_.get()), filename,
94 checksum, std::move(callback), handler));
95 }
96
97 /**
98 * General API here
99 */
LegacyModule()100 storage::LegacyModule::LegacyModule() : pimpl_(std::make_unique<impl>(*this)) {}
101
~LegacyModule()102 storage::LegacyModule::~LegacyModule() {
103 pimpl_.reset();
104 }
105
106 /**
107 * Module methods here
108 */
ListDependencies(ModuleList * list)109 void storage::LegacyModule::ListDependencies(ModuleList* list) {}
110
Start()111 void storage::LegacyModule::Start() {
112 pimpl_->Start();
113 }
114
Stop()115 void storage::LegacyModule::Stop() {
116 pimpl_->Stop();
117 }
118
119 } // namespace storage
120 } // namespace bluetooth
121