diff --git a/ai_lite/ai_engine_posix/base/BUILD.gn b/ai_lite/ai_engine_posix/base/BUILD.gn index dd7664ba2a292fa5544d0fe38630af115aa24310..a40b4c3f4d51ff93cf4ee62f68f44132a9d4f5ba 100644 --- a/ai_lite/ai_engine_posix/base/BUILD.gn +++ b/ai_lite/ai_engine_posix/base/BUILD.gn @@ -57,9 +57,9 @@ hcpptest_suite("ActsAiEngineTest") { "//foundation/ai/engine/services/client:client", "//foundation/ai/engine/services/common/platform/dl_operation:dlOperation", "//foundation/ai/engine/services/server/plugin_manager:plugin_manager", - "//foundation/ai/engine/test/sample:sample_plugin_1", - "//foundation/ai/engine/test/sample:sample_plugin_2", "//foundation/distributedschedule/samgr_lite/samgr:samgr", + "//test/xts/acts/ai_lite/ai_engine_posix/base/src/sample:sample_plugin_1_sync", + "//test/xts/acts/ai_lite/ai_engine_posix/base/src/sample:sample_plugin_2_async", ] cflags = [ "-Wno-error" ] ldflags = [ "-lstdc++" ] diff --git a/ai_lite/ai_engine_posix/base/src/sample/BUILD.gn b/ai_lite/ai_engine_posix/base/src/sample/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4e8201202d9ed82fe34d380c38550e5d83db3f85 --- /dev/null +++ b/ai_lite/ai_engine_posix/base/src/sample/BUILD.gn @@ -0,0 +1,64 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/lite/config/component/lite_component.gni") + +source_set("syncDemoPluginCode") { + sources = [ "source/sample_plugin_1.cpp" ] + + cflags = [ "-fPIC" ] + cflags_cc = cflags + + include_dirs = [ + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog", + "//foundation/ai/engine/services/common", + "//foundation/ai/engine/services/server", + "//foundation/ai/engine/test", + "//third_party/bounds_checking_function/include", + ] +} + +lite_component("sample_plugin_1_sync") { + target_type = "shared_library" + cflags = [ "-fPIC" ] + cflags_cc = cflags + features = [ ":syncDemoPluginCode" ] + deps = [ + "//foundation/ai/engine/services/common/protocol/data_channel:data_channel", + ] +} + +source_set("asyncDemoPluginCode") { + sources = [ "source/sample_plugin_2.cpp" ] + + cflags = [ "-fPIC" ] + cflags_cc = cflags + + include_dirs = [ + "//base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog", + "//foundation/ai/engine/services/common", + "//foundation/ai/engine/services/server", + "//foundation/ai/engine/test", + "//third_party/bounds_checking_function/include", + ] +} + +lite_component("sample_plugin_2_async") { + target_type = "shared_library" + cflags = [ "-fPIC" ] + cflags_cc = cflags + features = [ ":asyncDemoPluginCode" ] + deps = [ + "//foundation/ai/engine/services/common/protocol/data_channel:data_channel", + ] +} diff --git a/ai_lite/ai_engine_posix/base/src/sample/include/sample_plugin_1.h b/ai_lite/ai_engine_posix/base/src/sample/include/sample_plugin_1.h new file mode 100644 index 0000000000000000000000000000000000000000..9a777acc31ef82e045a7ebe33e113c6ae5c1c4fb --- /dev/null +++ b/ai_lite/ai_engine_posix/base/src/sample/include/sample_plugin_1.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SAMPLE_PLUGIN_1_H +#define SAMPLE_PLUGIN_1_H + +#include + +#include "plugin/i_plugin.h" + +namespace OHOS { +namespace AI { +class SamplePlugin1 : public IPlugin { + const long long ALG_VERSION = 1; + const char *ALG_NAME = "SAMPLE_PLUGIN_1"; + const char * const PLUGIN_INFER_MODEL = "SYNC"; + const char *DEFAULT_OPTION_DATA = "default option data"; + unsigned char *optionData = nullptr; + int optionLength = 0; + +public: + SamplePlugin1(); + + ~SamplePlugin1(); + + const long long GetVersion() const override; + + const char *GetName() const override; + + const char *GetInferMode() const override; + + int SyncProcess(IRequest *request, IResponse *&response) override; + + int AsyncProcess(IRequest *request, IPluginCallback *callback) override; + + int Prepare(long long transactionId, const DataInfo &inputInfo, DataInfo &outputInfo) override; + + int Release(bool isFullUnload, long long transactionId, const DataInfo &inputInfo) override; + + int SetOption(int optionType, const DataInfo &inputInfo) override; + + int GetOption(int optionType, const DataInfo &inputInfo, DataInfo &outputInfo) override; +}; +} +} + +#endif // SAMPLE_PLUGIN_1_H diff --git a/ai_lite/ai_engine_posix/base/src/sample/include/sample_plugin_2.h b/ai_lite/ai_engine_posix/base/src/sample/include/sample_plugin_2.h new file mode 100644 index 0000000000000000000000000000000000000000..a8169e77f49ea4039d80d6d303bcc0f90074d48c --- /dev/null +++ b/ai_lite/ai_engine_posix/base/src/sample/include/sample_plugin_2.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SAMPLE_PLUGIN_2_H +#define SAMPLE_PLUGIN_2_H + +#include + +#include "plugin/i_plugin.h" + +namespace OHOS { +namespace AI { +class SamplePlugin2 : public IPlugin { + const long long ALG_VERSION = 1; + const char *ALG_NAME = "SAMPLE_PLUGIN_2"; + const char * const PLUGIN_INFER_MODEL = "ASYNC"; + const char *DEFAULT_OPTION_DATA = "default option data"; + unsigned char *optionData = nullptr; + int optionLength = 0; + +public: + SamplePlugin2(); + + ~SamplePlugin2(); + + const long long GetVersion() const override; + + const char *GetName() const override; + + const char *GetInferMode() const override; + + int SyncProcess(IRequest *request, IResponse *&response) override; + + int AsyncProcess(IRequest *request, IPluginCallback *callback) override; + + int Prepare(long long transactionId, const DataInfo &inputInfo, DataInfo &outputInfo) override; + + int Release(bool isFullUnload, long long transactionId, const DataInfo &inputInfo) override; + + int SetOption(int optionType, const DataInfo &inputInfo) override; + + int GetOption(int optionType, const DataInfo &inputInfo, DataInfo &outputInfo) override; +}; +} +} + +#endif // SAMPLE_PLUGIN_2_H diff --git a/ai_lite/ai_engine_posix/base/src/sample/source/sample_plugin_1.cpp b/ai_lite/ai_engine_posix/base/src/sample/source/sample_plugin_1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d642b0687335539fa8e57f6cacf6cfd1de8eab5 --- /dev/null +++ b/ai_lite/ai_engine_posix/base/src/sample/source/sample_plugin_1.cpp @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sample/include/sample_plugin_1.h" + +#include + +#include "securec.h" + +#include "protocol/retcode_inner/aie_retcode_inner.h" +#include "utils/log/aie_log.h" + +namespace OHOS { +namespace AI { +SamplePlugin1::SamplePlugin1() = default; + +SamplePlugin1::~SamplePlugin1() +{ + if (optionData != nullptr) { + free(optionData); + optionData = nullptr; + optionLength = 0; + } +} + +const long long SamplePlugin1::GetVersion() const +{ + return ALG_VERSION; +} + +const char *SamplePlugin1::GetName() const +{ + return ALG_NAME; +} + +const char *SamplePlugin1::GetInferMode() const +{ + return PLUGIN_INFER_MODEL; +} + +int SamplePlugin1::SyncProcess(IRequest *request, IResponse *&response) +{ + DataInfo inputInfo = request->GetMsg(); + if (inputInfo.data != nullptr) { + HILOGE("[SamplePlugin2]inputInfo data is %p.", inputInfo.data); + } + response = IResponse::Create(request); + CHK_RET(response == nullptr, RETCODE_FAILURE); + + const char *str = "sample_plugin_1 SyncProcess outputInfo"; + char *outputData = const_cast(str); + int len = strlen(str) + 1; + DataInfo outputInfo = { + .data = (unsigned char*)outputData, + .length = len, + }; + response->SetResult(outputInfo); + response->SetRetCode(RETCODE_SUCCESS); + return RETCODE_SUCCESS; +} + +int SamplePlugin1::AsyncProcess(IRequest *request, IPluginCallback *callback) +{ + DataInfo inputInfo = request->GetMsg(); + if (inputInfo.data != nullptr) { + HILOGE("[SamplePlugin2]inputInfo data is %p.", inputInfo.data); + } + IResponse *response = IResponse::Create(request); + CHK_RET(response == nullptr, RETCODE_FAILURE); + + const char *str = "sample_plugin_1 AsyncProcess outputInfo"; + char *outputData = const_cast(str); + int len = strlen(str) + 1; + DataInfo outputInfo = { + .data = (unsigned char*)outputData, + .length = len, + }; + response->SetResult(outputInfo); + response->SetRetCode(RETCODE_SUCCESS); + int retCode = callback->OnEvent(ON_PLUGIN_SUCCEED, response); + return retCode; +} + +int SamplePlugin1::Prepare(long long transactionId, const DataInfo &inputInfo, DataInfo &outputInfo) +{ + const char *str = "sample_plugin_1 Prepare outputInfo"; + char *outputData = const_cast(str); + int len = strlen(str) + 1; + outputInfo = { + .data = (unsigned char*)outputData, + .length = len, + }; + return RETCODE_SUCCESS; +} + +int SamplePlugin1::Release(bool isFullUnload, long long transactionId, const DataInfo &inputInfo) +{ + if (optionData != nullptr) { + free(optionData); + optionData = nullptr; + optionLength = 0; + } + return RETCODE_SUCCESS; +} + +int SamplePlugin1::SetOption(int optionType, const DataInfo &inputInfo) +{ + if (optionData != nullptr) { + free(optionData); + optionData = nullptr; + optionLength = 0; + } + if (inputInfo.data == nullptr) { + return RETCODE_SUCCESS; + } + optionLength = inputInfo.length; + optionData = (unsigned char *)malloc(sizeof(unsigned char) * optionLength); + if (optionData == nullptr) { + HILOGE("[SamplePlugin1]Failed to request memory."); + return RETCODE_OUT_OF_MEMORY; + } + errno_t retCode = memcpy_s(optionData, optionLength, inputInfo.data, optionLength); + if (retCode != EOK) { + HILOGE("[SamplePlugin1]Failed to memory copy, retCode[%d].", retCode); + free(optionData); + optionData = nullptr; + return RETCODE_MEMORY_COPY_FAILURE; + } + return RETCODE_SUCCESS; +} + +int SamplePlugin1::GetOption(int optionType, const DataInfo &inputInfo, DataInfo &outputInfo) +{ + if (optionData == nullptr) { + optionLength = strlen(DEFAULT_OPTION_DATA) + 1; + optionData = (unsigned char *)malloc(sizeof(unsigned char) * optionLength); + if (optionData == nullptr) { + HILOGE("[SamplePlugin1]Failed to request memory."); + return RETCODE_OUT_OF_MEMORY; + } + errno_t retCode = memcpy_s(optionData, optionLength, DEFAULT_OPTION_DATA, optionLength); + if (retCode != EOK) { + HILOGE("[SamplePlugin1]Failed to memory copy, retCode[%d].", retCode); + free(optionData); + optionData = nullptr; + return RETCODE_MEMORY_COPY_FAILURE; + } + } + outputInfo = { + .data = optionData, + .length = optionLength + }; + return RETCODE_SUCCESS; +} + +PLUGIN_INTERFACE_IMPL(SamplePlugin1); +} +} diff --git a/ai_lite/ai_engine_posix/base/src/sample/source/sample_plugin_2.cpp b/ai_lite/ai_engine_posix/base/src/sample/source/sample_plugin_2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b87c9a28a106088933d5a09768e2a5406a4614a --- /dev/null +++ b/ai_lite/ai_engine_posix/base/src/sample/source/sample_plugin_2.cpp @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sample/include/sample_plugin_2.h" + +#include + +#include "securec.h" + +#include "protocol/retcode_inner/aie_retcode_inner.h" +#include "utils/log/aie_log.h" + +namespace OHOS { +namespace AI { +SamplePlugin2::SamplePlugin2() = default; + +SamplePlugin2::~SamplePlugin2() +{ + if (optionData != nullptr) { + free(optionData); + optionData = nullptr; + optionLength = 0; + } +} + +const long long SamplePlugin2::GetVersion() const +{ + return ALG_VERSION; +} + +const char *SamplePlugin2::GetName() const +{ + return ALG_NAME; +} + +const char *SamplePlugin2::GetInferMode() const +{ + return PLUGIN_INFER_MODEL; +} + +int SamplePlugin2::SyncProcess(IRequest *request, IResponse *&response) +{ + DataInfo inputInfo = request->GetMsg(); + if (inputInfo.data != nullptr) { + HILOGE("[SamplePlugin2]inputInfo data is %p.", inputInfo.data); + } + response = IResponse::Create(request); + CHK_RET(response == nullptr, RETCODE_FAILURE); + + const char *str = "sample_plugin_2 Process outputInfo"; + char *outputData = const_cast(str); + int len = strlen(str) + 1; + DataInfo outputInfo = { + .data = (unsigned char*)outputData, + .length = len, + }; + response->SetResult(outputInfo); + response->SetRetCode(RETCODE_SUCCESS); + return RETCODE_SUCCESS; +} + +int SamplePlugin2::AsyncProcess(IRequest *request, IPluginCallback *callback) +{ + DataInfo inputInfo = request->GetMsg(); + if (inputInfo.data != nullptr) { + HILOGE("[SamplePlugin2]inputInfo data is %p.", inputInfo.data); + } + IResponse *response = IResponse::Create(request); + CHK_RET(response == nullptr, RETCODE_FAILURE); + + const char *str = "sample_plugin_2 AsyncProcess outputInfo"; + char *outputData = const_cast(str); + int len = strlen(str) + 1; + DataInfo outputInfo = { + .data = (unsigned char*)outputData, + .length = len, + }; + response->SetResult(outputInfo); + response->SetRetCode(RETCODE_SUCCESS); + int retCode = callback->OnEvent(ON_PLUGIN_SUCCEED, response); + return retCode; +} + +int SamplePlugin2::Prepare(long long transactionId, const DataInfo &inputInfo, DataInfo &outputInfo) +{ + const char *str = "sample_plugin_2 Prepare outputInfo"; + char *outputData = const_cast(str); + int len = strlen(str) + 1; + outputInfo = { + .data = (unsigned char*)outputData, + .length = len, + }; + return RETCODE_SUCCESS; +} + +int SamplePlugin2::Release(bool isFullUnload, long long transactionId, const DataInfo &inputInfo) +{ + if (optionData != nullptr) { + free(optionData); + optionData = nullptr; + optionLength = 0; + } + return RETCODE_SUCCESS; +} + +int SamplePlugin2::SetOption(int optionType, const DataInfo &inputInfo) +{ + if (optionData != nullptr) { + free(optionData); + optionData = nullptr; + optionLength = 0; + } + if (inputInfo.data == nullptr) { + return RETCODE_SUCCESS; + } + optionLength = inputInfo.length; + optionData = (unsigned char *)malloc(sizeof(unsigned char) * optionLength); + if (optionData == nullptr) { + HILOGE("[SamplePlugin2]Failed to request memory."); + return RETCODE_OUT_OF_MEMORY; + } + errno_t retCode = memcpy_s(optionData, optionLength, inputInfo.data, optionLength); + if (retCode != EOK) { + HILOGE("[SamplePlugin2]Failed to memory copy, retCode[%d].", retCode); + free(optionData); + optionData = nullptr; + return RETCODE_MEMORY_COPY_FAILURE; + } + return RETCODE_SUCCESS; +} + +int SamplePlugin2::GetOption(int optionType, const DataInfo &inputInfo, DataInfo &outputInfo) +{ + if (optionData == nullptr) { + optionLength = strlen(DEFAULT_OPTION_DATA) + 1; + optionData = (unsigned char *)malloc(sizeof(unsigned char) * optionLength); + if (optionData == nullptr) { + HILOGE("[SamplePlugin1]Failed to request memory."); + return RETCODE_OUT_OF_MEMORY; + } + errno_t retCode = memcpy_s(optionData, optionLength, DEFAULT_OPTION_DATA, optionLength); + if (retCode != EOK) { + HILOGE("[SamplePlugin1]Failed to memory copy, retCode[%d].", retCode); + free(optionData); + optionData = nullptr; + return RETCODE_MEMORY_COPY_FAILURE; + } + } + outputInfo = { + .data = optionData, + .length = optionLength + }; + return RETCODE_SUCCESS; +} + +PLUGIN_INTERFACE_IMPL(SamplePlugin2); +} +} diff --git a/build_lite/BUILD.gn b/build_lite/BUILD.gn index bf9134a2e41065d2db7d702b92cc7f480805b8d0..ef334a832ad391583d0ab7da0ddd6ff8e1930a50 100644 --- a/build_lite/BUILD.gn +++ b/build_lite/BUILD.gn @@ -71,7 +71,7 @@ lite_component("acts_component") { "//test/xts/acts/communication_lite/softbus_posix:ActsSoftBusTest", "//test/xts/acts/communication_lite/lwip_posix:ActsLwipTest", - "//test/xts/acts/ai_lite:ActsAiTest", + "//test/xts/acts/ai_lite/ai_engine_posix/base:ActsAiEngineTest", "//test/xts/acts/global_lite:ActsGlobalTest", "//test/xts/acts/sensors_lite:sensorstest", ] @@ -86,7 +86,7 @@ lite_component("acts_component") { "//test/xts/acts/distributed_schedule_lite/system_ability_manager_posix:ActsSamgrTest", "//test/xts/acts/appexecfwk_lite/bundle_mgr_posix:ActsBundleMgrTest", "//test/xts/acts/aafwk_lite/ability_posix:ActsAbilityMgrTest", - "//test/xts/acts/ai_lite:ActsAiTest", + "//test/xts/acts/ai_lite/ai_engine_posix/base:ActsAiEngineTest", ] } }