1 /*
2  * Copyright (c) 2023 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 #include "intell_voice_engine_arbitration.h"
16 
17 #include "intell_voice_log.h"
18 
19 #define LOG_TAG "EngineArbitration"
20 
21 using namespace std;
22 
23 namespace OHOS {
24 namespace IntellVoiceEngine {
IntellVoiceEngineArbitration()25 IntellVoiceEngineArbitration::IntellVoiceEngineArbitration()
26 {
27     engineRelationTbl_[INTELL_VOICE_ENROLL][INTELL_VOICE_WAKEUP] = PREEMPTION_TYPE;
28     engineRelationTbl_[INTELL_VOICE_ENROLL][INTELL_VOICE_UPDATE] = REPLACEMENT_TYPE;
29     engineRelationTbl_[INTELL_VOICE_WAKEUP][INTELL_VOICE_ENROLL] = CONCURRENCY_TYPE;
30     engineRelationTbl_[INTELL_VOICE_UPDATE][INTELL_VOICE_ENROLL] = REJECTION_TYPE;
31 }
32 
~IntellVoiceEngineArbitration()33 IntellVoiceEngineArbitration::~IntellVoiceEngineArbitration()
34 {
35 }
36 
ApplyArbitration(IntellVoiceEngineType type,std::map<IntellVoiceEngineType,sptr<EngineBase>> & engines)37 ArbitrationResult IntellVoiceEngineArbitration::ApplyArbitration(IntellVoiceEngineType type,
38     std::map<IntellVoiceEngineType, sptr<EngineBase>> &engines)
39 {
40     if (JudgeRejection(type, engines)) {
41         INTELL_VOICE_LOG_INFO("reject engine, type: %{public}d", type);
42         return ARBITRATION_REJECT;
43     }
44 
45     HandlePreemption(type, engines);
46     HandleReplace(type, engines);
47 
48     return ARBITRATION_OK;
49 }
50 
GetEngine(IntellVoiceEngineType type,const std::map<IntellVoiceEngineType,sptr<EngineBase>> & engines)51 sptr<EngineBase> IntellVoiceEngineArbitration::GetEngine(IntellVoiceEngineType type,
52     const std::map<IntellVoiceEngineType, sptr<EngineBase>> &engines)
53 {
54     auto it = engines.find(type);
55     if (it != engines.end() && it->second != nullptr) {
56         return it->second;
57     }
58 
59     return nullptr;
60 }
61 
JudgeRejection(IntellVoiceEngineType type,const std::map<IntellVoiceEngineType,sptr<EngineBase>> & engines)62 bool IntellVoiceEngineArbitration::JudgeRejection(IntellVoiceEngineType type,
63     const std::map<IntellVoiceEngineType, sptr<EngineBase>> &engines)
64 {
65     auto relation = engineRelationTbl_.find(type);
66     if (relation == engineRelationTbl_.end()) {
67         return false;
68     }
69 
70     for (auto it : relation->second) {
71         if (it.second != REJECTION_TYPE) {
72             continue;
73         }
74         if (GetEngine(it.first, engines) != nullptr) {
75             return true;
76         }
77     }
78     return false;
79 }
80 
HandlePreemption(IntellVoiceEngineType type,const std::map<IntellVoiceEngineType,sptr<EngineBase>> & engines)81 void IntellVoiceEngineArbitration::HandlePreemption(IntellVoiceEngineType type,
82     const std::map<IntellVoiceEngineType, sptr<EngineBase>> &engines)
83 {
84     auto relation = engineRelationTbl_.find(type);
85     if (relation == engineRelationTbl_.end()) {
86         return;
87     }
88 
89     for (auto it : relation->second) {
90         if (it.second != PREEMPTION_TYPE) {
91             continue;
92         }
93         auto engine = GetEngine(it.first, engines);
94         if (engine == nullptr) {
95             continue;
96         }
97 
98         engine->Stop();
99         INTELL_VOICE_LOG_INFO("preemption, type: %{public}d, %{public}d", type, it.first);
100     }
101 }
102 
HandleReplace(IntellVoiceEngineType type,std::map<IntellVoiceEngineType,sptr<EngineBase>> & engines)103 void IntellVoiceEngineArbitration::HandleReplace(IntellVoiceEngineType type,
104     std::map<IntellVoiceEngineType, sptr<EngineBase>> &engines)
105 {
106     auto relation = engineRelationTbl_.find(type);
107     if (relation == engineRelationTbl_.end()) {
108         return;
109     }
110 
111     for (auto it : relation->second) {
112         if (it.second != REPLACEMENT_TYPE) {
113             continue;
114         }
115 
116         auto engineIter = engines.find(it.first);
117         if ((engineIter == engines.end()) || (engineIter->second == nullptr)) {
118             continue;
119         }
120 
121         engineIter->second->Detach();
122         engineIter->second = nullptr;
123         engines.erase(it.first);
124         INTELL_VOICE_LOG_INFO("replace, type: %{public}d, %{public}d", type, it.first);
125     }
126 }
127 }
128 }