1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "commander_factory.h"
17
18 #include "erase_commander.h"
19 #include "flash_commander.h"
20 #include "flashd_define.h"
21 #include "format_commander.h"
22 #include "update_commander.h"
23
24 namespace Flashd {
25 const std::unordered_map<std::string, std::function<std::unique_ptr<Commander>(callbackFun callback)>> COMMANDERS = {
__anon638a673d0102() 26 { Hdc::CMDSTR_FLASH_PARTITION, [](callbackFun callback) { return std::make_unique<FlashCommander>(callback); } },
__anon638a673d0202() 27 { Hdc::CMDSTR_ERASE_PARTITION, [](callbackFun callback) { return std::make_unique<EraseCommander>(callback); } },
__anon638a673d0302() 28 { Hdc::CMDSTR_UPDATE_SYSTEM, [](callbackFun callback) { return std::make_unique<UpdateCommander>(callback); } },
__anon638a673d0402() 29 { Hdc::CMDSTR_FORMAT_PARTITION, [](callbackFun callback) { return std::make_unique<FormatCommander>(callback); } }
30 };
31
GetInstance()32 CommanderFactory &CommanderFactory::GetInstance()
33 {
34 static CommanderFactory instance;
35 return instance;
36 }
37
CreateCommander(const std::string & cmd,callbackFun callback) const38 std::unique_ptr<Commander> CommanderFactory::CreateCommander(const std::string &cmd, callbackFun callback) const
39 {
40 auto iter = COMMANDERS.find(cmd);
41 if (iter != COMMANDERS.end()) {
42 return iter->second(callback);
43 }
44 return nullptr;
45 }
46 } // namespace Flashd