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 #ifndef HDI_DEVICE_INTELL_VOICE_TRIGGER_ADAPTER_IMPL_H
17 #define HDI_DEVICE_INTELL_VOICE_TRIGGER_ADAPTER_IMPL_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 #include <unordered_map>
23 #include "iremote_object.h"
24 #include "v1_1/iintell_voice_trigger_adapter.h"
25 #include "i_trigger.h"
26 
27 namespace OHOS {
28 namespace IntelligentVoice {
29 namespace Trigger {
30 using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IIntellVoiceTriggerCallback;
31 using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IntellVoiceRecognitionEvent;
32 using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IntellVoiceTriggerModel;
33 using OHOS::HDI::IntelligentVoice::Trigger::V1_0::IntellVoiceTriggerProperties;
34 
35 class IntellVoiceTriggerCallbackDevice : public ITriggerCallback {
36 public:
37     explicit IntellVoiceTriggerCallbackDevice(OHOS::sptr<IIntellVoiceTriggerCallback> callback);
38     ~IntellVoiceTriggerCallbackDevice();
39     void OnRecognitionHdiEvent(const IntellVoiceRecognitionEvent &event, int32_t cookie) override;
40 
41 private:
42     OHOS::sptr<IIntellVoiceTriggerCallback> callback_ = nullptr;
43 };
44 
45 class IntellVoiceDeathRecipient : public IRemoteObject::DeathRecipient {
46 public:
47     using ServiceDiedCallback = std::function<void(IRemoteObject *)>;
IntellVoiceDeathRecipient(ServiceDiedCallback callback,IRemoteObject * remote)48     IntellVoiceDeathRecipient(ServiceDiedCallback callback, IRemoteObject *remote)
49         : callback_(callback), remote_(remote) {};
50     ~IntellVoiceDeathRecipient() override = default;
51 
OnRemoteDied(const wptr<IRemoteObject> & remote)52     void OnRemoteDied(const wptr<IRemoteObject> &remote) override
53     {
54         (void)remote;
55         if (callback_ != nullptr) {
56             callback_(remote_);
57         }
58     }
59 
60 private:
61     ServiceDiedCallback callback_ = nullptr;
62     IRemoteObject *remote_ = nullptr;
63 };
64 
65 class IntellVoiceTriggerAdapterImpl : public OHOS::HDI::IntelligentVoice::Trigger::V1_1::IIntellVoiceTriggerAdapter {
66 public:
67     explicit IntellVoiceTriggerAdapterImpl(std::unique_ptr<ITrigger> adapter);
68     ~IntellVoiceTriggerAdapterImpl();
69 
70     int32_t GetProperties(IntellVoiceTriggerProperties &properties) override;
71     int32_t LoadModel(const IntellVoiceTriggerModel &model, const sptr<IIntellVoiceTriggerCallback> &triggerCallback,
72         int32_t cookie, int32_t &handle) override;
73     int32_t UnloadModel(int32_t handle) override;
74     int32_t Start(int32_t handle) override;
75     int32_t Stop(int32_t handle) override;
76     int32_t SetParams(const std::string &key, const std::string &value) override;
77     int32_t GetParams(const std::string &key, std::string &value) override;
78 
79 private:
80     int32_t GetModelDataFromAshmem(sptr<Ashmem> ashmem, std::vector<uint8_t> &modelData);
81     bool RegisterDeathRecipient(int32_t handle, const sptr<IIntellVoiceTriggerCallback> &triggerCallback);
82     void DeregisterDeathRecipient(int32_t handle);
83     void Clean(IRemoteObject *remote);
84 
85 private:
86     std::unique_ptr<ITrigger> adapter_ = nullptr;
87     std::mutex mutex_;
88     std::unordered_map<int32_t, sptr<IIntellVoiceTriggerCallback>> handleToCallbackMap_;
89     std::unordered_map<IRemoteObject *, std::set<int32_t>> callbackToHandleMap_;
90     std::unordered_map<IRemoteObject *, sptr<IRemoteObject::DeathRecipient>> deathRecipientMap_;
91 };
92 }  // namespace Trigger
93 }  // namespace IntelligentVoice
94 }  // namespace OHOS
95 #endif  // HDI_DEVICE_INTELL_VOICE_TRIGGER_ADAPTER_IMPL_H