1 2 /* 3 * Copyright (c) 2024 Huawei Device Co., Ltd. 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 #ifndef ENGINE_CALLBACK_MESSAGE_H 17 #define ENGINE_CALLBACK_MESSAGE_H 18 19 #include <functional> 20 #include <map> 21 #include <string> 22 #include <any> 23 #include <optional> 24 #include "nocopyable.h" 25 #include "intell_voice_log.h" 26 27 #undef LOG_TAG 28 #define LOG_TAG "EngineCallbackMessage" 29 30 namespace OHOS { 31 namespace IntellVoiceEngine { 32 enum EngineCbMessageId { 33 HANDLE_CLOSE_WAKEUP_SOURCE, 34 HANDLE_HEADSET_HOST_DIE, 35 HANDLE_CLEAR_WAKEUP_ENGINE_CB, 36 HANDLE_RELEASE_ENGINE, 37 HANDLE_ENGINE_SET_SENSIBILITY, 38 HANDLE_UPDATE_COMPLETE, 39 HANDLE_UPDATE_RETRY, 40 RELEASE_ENGINE, 41 QUERY_SWITCH_STATUS, 42 TRIGGERMGR_GET_PARAMETER, 43 TRIGGERMGR_SET_PARAMETER, 44 TRIGGERMGR_UPDATE_MODEL, 45 }; 46 47 class EngineCallbackMessage { 48 public: 49 EngineCallbackMessage() = default; 50 ~EngineCallbackMessage() = default; 51 52 using Func = std::function<std::optional<std::any>(const std::vector<std::any>&)>; 53 RegisterFunc(EngineCbMessageId id,Func func)54 static void RegisterFunc(EngineCbMessageId id, Func func) 55 { 56 EngineFuncMap[id] = func; 57 } 58 template <typename... Args> CallFunc(EngineCbMessageId id,Args &&...args)59 static std::optional<std::any> CallFunc(EngineCbMessageId id, Args &&... args) 60 { 61 std::vector<std::any> params = { std::forward<Args>(args)... }; 62 if (EngineFuncMap.find(id) != EngineFuncMap.end()) { 63 INTELL_VOICE_LOG_INFO("enter, EngineCbMessageId: %{public}d, Number of arguments: %{public}u", 64 id, static_cast<uint32_t>(params.size())); 65 return EngineFuncMap[id](params); 66 } else { 67 INTELL_VOICE_LOG_ERROR("Engine Callback Function not found"); 68 return std::nullopt; 69 } 70 } 71 72 private: 73 static std::map<EngineCbMessageId, Func> EngineFuncMap; 74 DISALLOW_COPY_AND_MOVE(EngineCallbackMessage); 75 }; 76 } // namespace IntellVoice 77 } // namespace OHOS 78 79 #undef LOG_TAG 80 81 #endif