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
16 #include "trigger_service.h"
17
18 #include <malloc.h>
19
20 #include "intell_voice_log.h"
21
22 #define LOG_TAG "TriggerService"
23 using namespace std;
24
25 namespace OHOS {
26 namespace IntellVoiceTrigger {
TriggerService()27 TriggerService::TriggerService()
28 {
29 dbHelper_ = std::make_shared<TriggerDbHelper>();
30 if (dbHelper_ == nullptr) {
31 INTELL_VOICE_LOG_ERROR("dbHelper_ is nullptr");
32 }
33
34 triggerHelper_ = TriggerHelper::Create();
35 if (triggerHelper_ == nullptr) {
36 INTELL_VOICE_LOG_ERROR("triggerHelper_ is nullptr");
37 }
38 }
39
~TriggerService()40 TriggerService::~TriggerService()
41 {}
42
UpdateGenericTriggerModel(std::shared_ptr<GenericTriggerModel> model)43 void TriggerService::UpdateGenericTriggerModel(std::shared_ptr<GenericTriggerModel> model)
44 {
45 INTELL_VOICE_LOG_INFO("enter");
46 if (dbHelper_ == nullptr) {
47 INTELL_VOICE_LOG_ERROR("dbHelper_ is nullptr");
48 return;
49 }
50
51 if (model == nullptr) {
52 INTELL_VOICE_LOG_ERROR("trigger model is null");
53 return;
54 }
55
56 if (!dbHelper_->UpdateGenericTriggerModel(model)) {
57 INTELL_VOICE_LOG_ERROR("failed to update generic model");
58 }
59 }
60
DeleteGenericTriggerModel(int32_t uuid)61 void TriggerService::DeleteGenericTriggerModel(int32_t uuid)
62 {
63 INTELL_VOICE_LOG_INFO("enter");
64 if (dbHelper_ == nullptr) {
65 INTELL_VOICE_LOG_ERROR("dbHelper_ is nullptr");
66 return;
67 }
68 dbHelper_->DeleteGenericTriggerModel(uuid);
69 }
70
GetGenericTriggerModel(int32_t uuid)71 std::shared_ptr<GenericTriggerModel> TriggerService::GetGenericTriggerModel(int32_t uuid)
72 {
73 INTELL_VOICE_LOG_INFO("enter");
74
75 if (dbHelper_ == nullptr) {
76 INTELL_VOICE_LOG_ERROR("dbHelper_ is nullptr");
77 return nullptr;
78 }
79
80 auto model = dbHelper_->GetGenericTriggerModel(uuid);
81 if (model == nullptr) {
82 INTELL_VOICE_LOG_ERROR("failed to get generic trigger model");
83 return nullptr;
84 }
85
86 return model;
87 }
88
StartRecognition(int32_t uuid,std::shared_ptr<IIntellVoiceTriggerRecognitionCallback> callback)89 int32_t TriggerService::StartRecognition(
90 int32_t uuid, std::shared_ptr<IIntellVoiceTriggerRecognitionCallback> callback)
91 {
92 if (triggerHelper_ == nullptr) {
93 INTELL_VOICE_LOG_ERROR("trigger helper is nullptr");
94 return -1;
95 }
96
97 auto model = GetGenericTriggerModel(uuid);
98 if (model == nullptr) {
99 INTELL_VOICE_LOG_ERROR("trigger model is nullptr, uuid:%{public}d", uuid);
100 return -1;
101 }
102
103 return triggerHelper_->StartGenericRecognition(uuid, model, callback);
104 }
105
StopRecognition(int32_t uuid,std::shared_ptr<IIntellVoiceTriggerRecognitionCallback> callback)106 int32_t TriggerService::StopRecognition(int32_t uuid, std::shared_ptr<IIntellVoiceTriggerRecognitionCallback> callback)
107 {
108 if (triggerHelper_ == nullptr) {
109 INTELL_VOICE_LOG_ERROR("trigger helper is nullptr");
110 return -1;
111 }
112 return triggerHelper_->StopGenericRecognition(uuid, callback);
113 }
114
UnloadTriggerModel(int32_t uuid)115 void TriggerService::UnloadTriggerModel(int32_t uuid)
116 {
117 if (triggerHelper_ == nullptr) {
118 INTELL_VOICE_LOG_ERROR("trigger helper is nullptr");
119 return;
120 }
121
122 triggerHelper_->UnloadGenericTriggerModel(uuid);
123 }
124 } // namespace IntellVoiceTrigger
125 } // namespace OHOS