动态拉起SA

Signed-off-by: wangchaole <wangchaole1@huawei.com>
This commit is contained in:
wangchaole
2022-04-20 09:17:14 +08:00
parent e9fb8da21e
commit 80f0236842
16 changed files with 316 additions and 13 deletions
@@ -33,6 +33,7 @@ ohos_shared_library("distributed_camera_sink_sdk") {
sources = [
"src/dcamera_sink_handler.cpp",
"src/dcamera_sink_handler_ipc.cpp",
"src/dcamera_sink_load_callback.cpp",
"src/distributed_camera_sink_proxy.cpp",
]
@@ -16,6 +16,9 @@
#ifndef OHOS_DCAMERA_SINK_HANDLER_H
#define OHOS_DCAMERA_SINK_HANDLER_H
#include <mutex>
#include <condition_variable>
#include "idistributed_hardware_sink.h"
#include "single_instance.h"
@@ -28,10 +31,19 @@ public:
int32_t ReleaseSink() override;
int32_t SubscribeLocalHardware(const std::string& dhId, const std::string& parameters) override;
int32_t UnsubscribeLocalHardware(const std::string& dhId) override;
void FinishStartSA(const std::string &params);
void FinishStartSAFailed(int32_t systemAbilityId);
private:
typedef enum {
DCAMERA_SA_STATE_STOP = 0,
DCAMERA_SA_STATE_START = 1,
} DCameraSAState;
DCameraSinkHandler() = default;
~DCameraSinkHandler();
private:
std::condition_variable producerCon_;
std::mutex producerMutex_;
DCameraSAState state_ = DCAMERA_SA_STATE_STOP;
};
#ifdef __cplusplus
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2022 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 OHOS_DCAMERA_SINk_LOAD_CALLBACK_H
#define OHOS_DCAMERA_SINk_LOAD_CALLBACK_H
#include "system_ability_load_callback_stub.h"
namespace OHOS {
namespace DistributedHardware {
class DCameraSinkLoadCallback : public SystemAbilityLoadCallbackStub {
public:
explicit DCameraSinkLoadCallback(const std::string params);
void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject);
void OnLoadSystemAbilityFail(int32_t systemAbilityId);
private:
std::string params;
};
}
}
#endif
@@ -14,11 +14,15 @@
*/
#include "dcamera_sink_handler.h"
#include "dcamera_sink_load_callback.h"
#include "anonymous_string.h"
#include "dcamera_sink_handler_ipc.h"
#include "distributed_camera_constants.h"
#include "distributed_camera_errno.h"
#include "distributed_hardware_log.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
namespace OHOS {
namespace DistributedHardware {
@@ -32,13 +36,50 @@ DCameraSinkHandler::~DCameraSinkHandler()
int32_t DCameraSinkHandler::InitSink(const std::string& params)
{
DHLOGI("DCameraSinkHandler::InitSink");
sptr<DCameraSinkLoadCallback> loadCallback = new DCameraSinkLoadCallback(params);
sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (sm == nullptr) {
DHLOGE("GetSourceLocalDHMS GetSystemAbilityManager failed");
return DCAMERA_INIT_ERR;
}
int32_t ret = sm->LoadSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, loadCallback);
if (ret != DCAMERA_OK) {
DHLOGE("systemAbilityId: %d load filed,result code: %d.", DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, ret);
return DCAMERA_INIT_ERR;
}
uint32_t interval = 1;
std::unique_lock<std::mutex> lock(producerMutex_);
producerCon_.wait_for(lock, std::chrono::minutes(interval), [this] {
return (this->state_ == DCAMERA_SA_STATE_START);
});
if (state_ == DCAMERA_SA_STATE_STOP) {
DHLOGE("SinkSA Start failed!");
return DCAMERA_INIT_ERR;
}
DHLOGI("DCameraSinkHandler InitSink end, result: %d", ret);
return DCAMERA_OK;
}
void DCameraSinkHandler::FinishStartSA(const std::string &params)
{
DCameraSinkHandlerIpc::GetInstance().Init();
sptr<IDistributedCameraSink> dCameraSinkSrv = DCameraSinkHandlerIpc::GetInstance().GetSinkLocalDHMS();
if (dCameraSinkSrv == nullptr) {
DHLOGE("DCameraSinkHandler::InitSink get Service failed");
return DCAMERA_INIT_ERR;
return;
}
return dCameraSinkSrv->InitSink(params);
dCameraSinkSrv->InitSink(params);
std::unique_lock<std::mutex> lock(producerMutex_);
state_ = DCAMERA_SA_STATE_START;
producerCon_.notify_one();
}
void DCameraSinkHandler::FinishStartSAFailed(int32_t systemAbilityId)
{
DHLOGI("SinkSA Start failed, systemAbilityId: %d.", systemAbilityId);
std::unique_lock<std::mutex> lock(producerMutex_);
state_ = DCAMERA_SA_STATE_STOP;
producerCon_.notify_one();
}
int32_t DCameraSinkHandler::ReleaseSink()
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022 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 "dcamera_sink_load_callback.h"
#include "dcamera_sink_handler.h"
#include "distributed_hardware_log.h"
#include "distributed_camera_constants.h"
namespace OHOS {
namespace DistributedHardware {
DCameraSinkLoadCallback::DCameraSinkLoadCallback(const std::string params) : params(params) {}
void DCameraSinkLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
const sptr<IRemoteObject>& remoteObject)
{
DHLOGI("OnLoadSystemAbilitySuccess start systemAbilityId: %d.", systemAbilityId);
if (systemAbilityId != DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID) {
DHLOGE("start aystemabilityId is not sinkSAId!");
return;
}
DHLOGE("OnLoadSystemAbilitySuccess systemAbilityId: %d, IRmoteObject result: %s",
systemAbilityId, (remoteObject != nullptr) ? "true" : "false");
if (remoteObject == nullptr) {
DHLOGE("remoteObject is null.");
return;
}
DCameraSinkHandler::GetInstance().FinishStartSA(params);
}
void DCameraSinkLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
{
DHLOGE("OnLoadSystemAbilityFail systemAbilityId: %d.", systemAbilityId);
if (systemAbilityId != DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID) {
DHLOGE("start aystemabilityId is not sinkSAId!");
return;
}
DCameraSinkHandler::GetInstance().FinishStartSAFailed(systemAbilityId);
}
}
}
@@ -36,6 +36,7 @@ ohos_shared_library("distributed_camera_source_sdk") {
"src/callback/dcamera_source_callback_stub.cpp",
"src/dcamera_source_handler.cpp",
"src/dcamera_source_handler_ipc.cpp",
"src/dcamera_source_load_callback.cpp",
"src/distributed_camera_source_proxy.cpp",
]
@@ -17,6 +17,7 @@
#define OHOS_DCAMERA_SOURCE_HANDLER_H
#include <mutex>
#include <condition_variable>
#include "iremote_proxy.h"
#include "iremote_broker.h"
@@ -39,14 +40,22 @@ public:
std::shared_ptr<UnregisterCallback> callback) override;
int32_t ConfigDistributedHardware(const std::string& devId, const std::string& dhId, const std::string& key,
const std::string& value) override;
void FinishStartSA(const std::string &params);
void FinishStartSAFailed(int32_t systemAbilityId);
private:
typedef enum {
DCAMERA_SA_STATE_STOP = 0,
DCAMERA_SA_STATE_START = 1,
} DCameraSAState;
DCameraSourceHandler() = default;
~DCameraSourceHandler();
private:
std::mutex optLock_;
sptr<DCameraSourceCallback> callback_;
std::condition_variable producerCon_;
std::mutex producerMutex_;
DCameraSAState state_ = DCAMERA_SA_STATE_STOP;
};
#ifdef __cplusplus
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2022 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 OHOS_DCAMERA_SOURCE_LOAD_CALLBACK_H
#define OHOS_DCAMERA_SOURCE_LOAD_CALLBACK_H
#include "system_ability_load_callback_stub.h"
namespace OHOS {
namespace DistributedHardware {
class DCameraSourceLoadCallback : public SystemAbilityLoadCallbackStub {
public:
explicit DCameraSourceLoadCallback(const std::string params);
void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject>& remoteObject);
void OnLoadSystemAbilityFail(int32_t systemAbilityId);
private:
std::string params;
};
}
}
#endif
@@ -15,10 +15,14 @@
#include "dcamera_source_handler.h"
#include "dcamera_source_callback.h"
#include "dcamera_source_load_callback.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "anonymous_string.h"
#include "dcamera_source_handler_ipc.h"
#include "dh_utils_tool.h"
#include "distributed_camera_constants.h"
#include "distributed_camera_errno.h"
#include "distributed_hardware_log.h"
@@ -34,21 +38,56 @@ DCameraSourceHandler::~DCameraSourceHandler()
int32_t DCameraSourceHandler::InitSource(const std::string& params)
{
DHLOGI("DCameraSourceHandler InitSource Start");
sptr<DCameraSourceLoadCallback> loadCallback = new DCameraSourceLoadCallback(params);
sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (sm == nullptr) {
DHLOGE("GetSourceLocalDHMS GetSystemAbilityManager failed");
return DCAMERA_INIT_ERR;
}
int32_t ret = sm->LoadSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID, loadCallback);
if (ret != DCAMERA_OK) {
DHLOGE("systemAbilityId: %d load filed,result code: %d.", DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID, ret);
return DCAMERA_INIT_ERR;
}
uint32_t interval = 1;
std::unique_lock<std::mutex> lock(producerMutex_);
producerCon_.wait_for(lock, std::chrono::minutes(interval), [this] {
return (this->state_ == DCAMERA_SA_STATE_START);
});
if (state_ == DCAMERA_SA_STATE_STOP) {
DHLOGE("SourceSA Start failed!");
return DCAMERA_INIT_ERR;
}
DHLOGI("DCameraSourceHandler InitSource end, result: %d", ret);
return DCAMERA_OK;
}
void DCameraSourceHandler::FinishStartSA(const std::string &params)
{
DCameraSourceHandlerIpc::GetInstance().Init();
sptr<IDistributedCameraSource> dCameraSourceSrv = DCameraSourceHandlerIpc::GetInstance().GetSourceLocalDHMS();
if (dCameraSourceSrv == nullptr) {
DHLOGE("DCameraSourceHandler InitSource get Service failed");
return DCAMERA_INIT_ERR;
return;
}
callback_ = new DCameraSourceCallback();
if (callback_ == nullptr) {
DHLOGE("DCameraSourceHandler InitSource init callback failed");
return DCAMERA_INIT_ERR;
return;
}
int32_t ret = dCameraSourceSrv->InitSource(params, callback_);
DHLOGI("DCameraSourceHandler InitSource end, ret: %d", ret);
return ret;
dCameraSourceSrv->InitSource(params, callback_);
std::unique_lock<std::mutex> lock(producerMutex_);
state_ = DCAMERA_SA_STATE_START;
producerCon_.notify_one();
}
void DCameraSourceHandler::FinishStartSAFailed(int32_t systemAbilityId)
{
DHLOGE("SourceSA Start failed, systemAbilityId: %d.", systemAbilityId);
std::unique_lock<std::mutex> lock(producerMutex_);
state_ = DCAMERA_SA_STATE_STOP;
producerCon_.notify_one();
}
int32_t DCameraSourceHandler::ReleaseSource()
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022 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 "dcamera_source_load_callback.h"
#include "dcamera_source_handler.h"
#include "distributed_hardware_log.h"
#include "distributed_camera_constants.h"
namespace OHOS {
namespace DistributedHardware {
DCameraSourceLoadCallback::DCameraSourceLoadCallback(const std::string params) : params(params) {}
void DCameraSourceLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
const sptr<IRemoteObject>& remoteObject)
{
DHLOGI("OnLoadSystemAbilitySuccess start systemAbilityId: %d.", systemAbilityId);
if (systemAbilityId != DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID) {
DHLOGE("start systemabilityId is not sourceSAId!");
return;
}
DHLOGE("OnLoadSystemAbilitySuccess systemAbilityId: %d, IRmoteObject result: %s",
systemAbilityId, (remoteObject != nullptr) ? "true" : "false");
if (remoteObject == nullptr) {
DHLOGE("remoteObject is null.");
return;
}
DCameraSourceHandler::GetInstance().FinishStartSA(params);
}
void DCameraSourceLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
{
DHLOGE("OnLoadSystemAbilityFail systemAbilityId: %d.", systemAbilityId);
if (systemAbilityId != DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID) {
DHLOGE("start systemabilityId is not sourceSAId!");
return;
}
DCameraSourceHandler::GetInstance().FinishStartSAFailed(systemAbilityId);
}
}
}
+2 -2
View File
@@ -14,13 +14,13 @@
* limitations under the License.
-->
<info>
<process>dhardware</process>
<process>dcamerasrc</process>
<systemability> <!-- Declare a system ability and its profile -->
<name>4803</name> <!-- Declare the name of system ability -->
<libpath>libdistributed_camera_source.z.so</libpath> <!-- Declare the path of .so file which includes the system ability; Note: 1 .so file can have 1 to N system abilities. -->
<!--<depend></depend> --> <!-- Declare the name of system abilities which the system ability depends on, using ";" as separator among names. If there are dependencies, it needs to check if all those dependencies are avliable in service manager before starting the system ability. -->
<!--<depend-time-out></depend-time-out> --> <!-- Check all dependencies are avaliable before the timeout period ended. The MAX_DEPENDENCY_TIMEOUT is 60s. -->
<run-on-create>true</run-on-create> <!-- "true" means the system ability would start imediately, "false" means the system ability would start on demand. -->
<run-on-create>false</run-on-create> <!-- "true" means the system ability would start imediately, "false" means the system ability would start on demand. -->
<distributed>true</distributed> <!-- "true" means the system ability supports distributed scheduling while "false" is not. -->
<dump-level>1</dump-level> <!-- Declare the dump level. 1-high; 2-media; 3-low -->
</systemability>
+2 -2
View File
@@ -14,13 +14,13 @@
* limitations under the License.
-->
<info>
<process>dhardware</process>
<process>dcamerasink</process>
<systemability> <!-- Declare a system ability and its profile -->
<name>4804</name> <!-- Declare the name of system ability -->
<libpath>libdistributed_camera_sink.z.so</libpath> <!-- Declare the path of .so file which includes the system ability; Note: 1 .so file can have 1 to N system abilities. -->
<!--<depend></depend> --> <!-- Declare the name of system abilities which the system ability depends on, using ";" as separator among names. If there are dependencies, it needs to check if all those dependencies are avliable in service manager before starting the system ability. -->
<!--<depend-time-out></depend-time-out> --> <!-- Check all dependencies are avaliable before the timeout period ended. The MAX_DEPENDENCY_TIMEOUT is 60s. -->
<run-on-create>true</run-on-create> <!-- "true" means the system ability would start imediately, "false" means the system ability would start on demand. -->
<run-on-create>false</run-on-create> <!-- "true" means the system ability would start imediately, "false" means the system ability would start on demand. -->
<distributed>true</distributed> <!-- "true" means the system ability supports distributed scheduling while "false" is not. -->
<dump-level>1</dump-level> <!-- Declare the dump level. 1-high; 2-media; 3-low -->
</systemability>
@@ -108,6 +108,7 @@ ohos_shared_library("distributed_camera_sink") {
"${services_path}/data_process:distributed_camera_data_process",
"//third_party/jsoncpp:jsoncpp",
"//utils/native/base:utils",
":dcamerasink.cfg",
]
defines = [
@@ -127,4 +128,11 @@ ohos_shared_library("distributed_camera_sink") {
subsystem_name = "distributedhardware"
part_name = "distributed_camera"
}
ohos_prebuilt_etc("dcamerasink.cfg") {
relative_install_dir = "init"
source = "dcamerasink.cfg"
part_name = "distributed_camera"
subsystem_name = "distributedhardware"
}
@@ -0,0 +1,9 @@
{
"services" : [{
"name" : "dcamerasink",
"path" : ["/system/bin/sa_main","/system/profile/dcamerasink.xml"],
"uid" : "system",
"gid" : ["system"],
"ondemand" : true
}]
}
@@ -93,6 +93,7 @@ ohos_shared_library("distributed_camera_source") {
"${services_path}/data_process:distributed_camera_data_process",
"//third_party/jsoncpp:jsoncpp",
"//utils/native/base:utils",
":dcamerasrc.cfg",
]
defines = [
@@ -112,4 +113,11 @@ ohos_shared_library("distributed_camera_source") {
subsystem_name = "distributedhardware"
part_name = "distributed_camera"
}
ohos_prebuilt_etc("dcamerasrc.cfg") {
relative_install_dir = "init"
source = "dcamerasrc.cfg"
part_name = "distributed_camera"
subsystem_name = "distributedhardware"
}
@@ -0,0 +1,9 @@
{
"services" : [{
"name" : "dcamerasrc",
"path" : ["/system/bin/sa_main","/system/profile/dcamerasrc.xml"],
"uid" : "system",
"gid" : ["system"],
"ondemand" : true
}]
}