Merge branch 'master' of gitee.com:openharmony/msdp_device_status into master

This commit is contained in:
施其昌 2022-06-23 02:56:44 +00:00 committed by Gitee
commit 20fcf49969
19 changed files with 669 additions and 11 deletions

View File

@ -16,13 +16,13 @@
"ram": "~4096KB",
"deps": {
"components": [
"access_token",
"hiviewdfx_hilog_native",
"ipc",
"safwk",
"samgr_standard",
"utils_base",
"appexecfwk_standard",
"permission_standard",
"napi"
],
"third_party": []

View File

@ -40,6 +40,7 @@ ohos_shared_library("deviceagent") {
]
external_deps = [
"hitrace_native:hitrace_meter",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
]

View File

@ -0,0 +1,55 @@
/*
* 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 "bytrace_adapter.h"
namespace OHOS {
namespace Msdp {
namespace {
std::string clientSubscribe = "ClientSubscribe";
std::string clientUnSubscribe = "ClientUnSubscribe";
std::string serviceSubscribe = "ServiceSubscribe";
std::string serviceUnSubscribe = "ServiceUnSubscribe";
}
void BytraceAdapter::StartBytrace(TraceBtn traceBtn, SubscribeType isSubscribe, SubscribeObject subscribeObject)
{
if (isSubscribe) {
if (traceBtn == TRACE_START) {
if (subscribeObject == CLIENT) {
StartTrace(HITRACE_TAG_MSDP, clientSubscribe);
HITRACE_METER_NAME(HITRACE_TAG_MSDP, "client start subsvribe");
} else {
StartTrace(HITRACE_TAG_MSDP, serviceSubscribe);
HITRACE_METER_NAME(HITRACE_TAG_MSDP, "service start subsvribe");
}
} else {
FinishTrace(HITRACE_TAG_MSDP);
}
} else {
if (traceBtn == TRACE_START) {
if (subscribeObject == CLIENT) {
StartTrace(HITRACE_TAG_MSDP, clientUnSubscribe);
HITRACE_METER_NAME(HITRACE_TAG_MSDP, "client start unSubsvribe");
} else {
StartTrace(HITRACE_TAG_MSDP, serviceUnSubscribe);
HITRACE_METER_NAME(HITRACE_TAG_MSDP, "service start unSubsvribe");
}
} else {
FinishTrace(HITRACE_TAG_MSDP);
}
}
}
} // namespace MSDP
} // namespace OHOS

View File

@ -21,6 +21,7 @@
#include "idevicestatus_callback.h"
#include "devicestatus_common.h"
#include "bytrace_adapter.h"
namespace OHOS {
namespace Msdp {
@ -28,6 +29,7 @@ void DevicestatusSrvProxy::Subscribe(const DevicestatusDataUtils::DevicestatusTy
const sptr<IdevicestatusCallback>& callback)
{
DEV_HILOGD(INNERKIT, "Enter");
BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_START, BytraceAdapter::SUBSCRIBE, BytraceAdapter::CLIENT);
sptr<IRemoteObject> remote = Remote();
DEVICESTATUS_RETURN_IF((remote == nullptr) || (callback == nullptr));
@ -48,6 +50,7 @@ void DevicestatusSrvProxy::Subscribe(const DevicestatusDataUtils::DevicestatusTy
DEV_HILOGE(INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
return;
}
BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_STOP, BytraceAdapter::SUBSCRIBE, BytraceAdapter::CLIENT);
DEV_HILOGD(INNERKIT, "Exit");
}
@ -55,6 +58,7 @@ void DevicestatusSrvProxy::UnSubscribe(const DevicestatusDataUtils::Devicestatus
const sptr<IdevicestatusCallback>& callback)
{
DEV_HILOGD(INNERKIT, "Enter");
BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_START, BytraceAdapter::UNSUBSCRIBE, BytraceAdapter::CLIENT);
sptr<IRemoteObject> remote = Remote();
DEVICESTATUS_RETURN_IF((remote == nullptr) || (callback == nullptr));
@ -76,6 +80,7 @@ void DevicestatusSrvProxy::UnSubscribe(const DevicestatusDataUtils::Devicestatus
DEV_HILOGE(INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
return;
}
BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_STOP, BytraceAdapter::UNSUBSCRIBE, BytraceAdapter::CLIENT);
DEV_HILOGD(INNERKIT, "Exit");
}

View File

@ -26,6 +26,7 @@ config("devicestatus_public_config") {
ohos_shared_library("devicestatus_client") {
sources = [
"${device_status_frameworks_path}/native/src/bytrace_adapter.cpp",
"${device_status_frameworks_path}/native/src/devicestatus_callback_proxy.cpp",
"${device_status_frameworks_path}/native/src/devicestatus_client.cpp",
"${device_status_frameworks_path}/native/src/devicestatus_srv_proxy.cpp",
@ -42,6 +43,7 @@ ohos_shared_library("devicestatus_client") {
deps = [ "//utils/native/base:utils" ]
external_deps = [
"hitrace_native:hitrace_meter",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"safwk:system_ability_fwk",

View File

@ -0,0 +1,43 @@
/*
* 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_MSDP_BYTRACE_ADAPTER_H
#define OHOS_MSDP_BYTRACE_ADAPTER_H
#include <string.h>
#include "hitrace_meter.h"
namespace OHOS {
namespace Msdp {
class BytraceAdapter {
public:
enum TraceBtn {
TRACE_STOP = 0,
TRACE_START = 1
};
enum SubscribeType {
SUBSCRIBE,
UNSUBSCRIBE
};
enum SubscribeObject {
CLIENT,
SERVICE
};
static void StartBytrace(TraceBtn traceBtn, SubscribeType subscribeType, SubscribeObject subscribeObject);
};
} // namespace MSDP
} // namespace OHOS
#endif // BYTRACE_ADAPTER_H

View File

@ -23,9 +23,9 @@ config("devicestatus_srv_public_config") {
"interface",
"${device_status_interfaces_path}/innerkits/include",
"//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include/",
"//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/rdb/include/",
"//foundation/distributeddatamgr/appdatamgr/relational_store/interfaces/inner_api/rdb/include/",
"${aafwk_path}/interfaces/kits/native/ability/native/",
"//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/appdatafwk/include/",
"//foundation/distributeddatamgr/appdatamgr/relational_store/interfaces/inner_api/appdatafwk/include/",
"//base/sensors/sensor/interfaces/native/include/",
]
}
@ -51,7 +51,6 @@ ohos_shared_library("devicestatus_sensorhdi") {
"ability_base:base",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"permission_standard:libpermissionsdk_standard",
"relational_store:native_rdb",
"safwk:system_ability_fwk",
"samgr_standard:samgr_proxy",
@ -82,7 +81,6 @@ ohos_shared_library("devicestatus_msdp") {
"ability_base:base",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"permission_standard:libpermissionsdk_standard",
"relational_store:native_rdb",
"safwk:system_ability_fwk",
"samgr_standard:samgr_proxy",

View File

@ -23,15 +23,16 @@ config("devicestatus_srv_public_config") {
"${device_status_interfaces_path}/innerkits/include",
"//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include/",
"//base/msdp/device_status/libs/interface",
"//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/rdb/include/",
"//foundation/distributeddatamgr/appdatamgr/relational_store/interfaces/inner_api/rdb/include/",
"${aafwk_path}/interfaces/kits/native/ability/native/",
"//foundation/distributeddatamgr/appdatamgr/interfaces/inner_api/native/appdatafwk/include/",
"//foundation/distributeddatamgr/appdatamgr/relational_store/interfaces/inner_api/appdatafwk/include/",
]
}
ohos_shared_library("devicestatus_service") {
sources = [
"native/src/devicestatus_callback_stub.cpp",
"native/src/devicestatus_dumper.cpp",
"native/src/devicestatus_manager.cpp",
"native/src/devicestatus_msdp_client_impl.cpp",
"native/src/devicestatus_service.cpp",
@ -54,9 +55,11 @@ ohos_shared_library("devicestatus_service") {
external_deps = [
"ability_base:base",
"access_token:libaccesstoken_sdk",
"hisysevent_native:libhisysevent",
"hitrace_native:hitrace_meter",
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
"permission_standard:libpermissionsdk_standard",
"relational_store:native_rdb",
"safwk:system_ability_fwk",
"samgr_standard:samgr_proxy",

View File

@ -0,0 +1,85 @@
/*
* 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 DEVICESTATUS_DUMPER_H
#define DEVICESTATUS_DUMPER_H
#include <refbase.h>
#include <map>
#include <memory>
#include <mutex>
#include <queue>
#include <set>
#include <singleton.h>
#include <string>
#include <vector>
#include "accesstoken_kit.h"
#include "devicestatus_data_utils.h"
#include "idevicestatus_callback.h"
namespace OHOS {
namespace Msdp {
const std::string ARG_DUMP_HELP = "-h";
const std::string ARG_DUMP_DEVICESTATUS_SUBSCRIBER = "-s";
const std::string ARG_DUMP_DEVICESTATUS_CHANGES = "-l";
const std::string ARG_DUMP_DEVICESTATUS_CURRENT_STATE = "-c";
constexpr int32_t RET_OK = 0;
constexpr int32_t RET_NG = -1;
constexpr uint32_t MAX_DEVICE_STATUS_SIZE = 10;
constexpr uint32_t BASE_YEAR = 1900;
constexpr uint32_t BASE_MON = 1;
struct AppInfo {
std::string startTime;
int32_t uid = 0;
int32_t pid = 0;
Security::AccessToken::AccessTokenID tokenId;
std::string packageName;
DevicestatusDataUtils::DevicestatusType type;
sptr<IdevicestatusCallback> callback;
};
struct DeviceStatusRecord {
std::string startTime;
DevicestatusDataUtils::DevicestatusData data;
};
class DevicestatusDumper final : public RefBase,
public Singleton<DevicestatusDumper> {
public:
DevicestatusDumper() = default;
~DevicestatusDumper() = default;
void ParseCommand(int32_t fd, const std::vector<std::string> &args,
const std::vector<DevicestatusDataUtils::DevicestatusData> &datas);
void DumpHelpInfo(int32_t fd) const;
void DumpDevicestatusSubscriber(int32_t fd);
void DumpDevicestatusChanges(int32_t fd);
void DumpDevicestatusCurrentStatus(int32_t fd,
const std::vector<DevicestatusDataUtils::DevicestatusData> &datas) const;
void SaveAppInfo(std::shared_ptr<AppInfo> appInfo);
void RemoveAppInfo(std::shared_ptr<AppInfo> appInfo);
void pushDeviceStatus(const DevicestatusDataUtils::DevicestatusData& data);
std::string GetPackageName(Security::AccessToken::AccessTokenID tokenId);
private:
DISALLOW_COPY_AND_MOVE(DevicestatusDumper);
void DumpCurrentTime(std::string &startTime) const;
std::string GetStatusType(const DevicestatusDataUtils::DevicestatusType &type) const;
std::string GetDeviceState(const DevicestatusDataUtils::DevicestatusValue &type) const;
std::map<DevicestatusDataUtils::DevicestatusType, std::set<std::shared_ptr<AppInfo>>> \
appInfoMap_;
std::queue<std::shared_ptr<DeviceStatusRecord>> deviceStatusQueue_;
std::mutex mutex_;
};
} // namespace Msdp
} // namespace OHOS
#endif // DEVICESTATUS_DUMPER_H

View File

@ -25,9 +25,11 @@
#include "idevicestatus_callback.h"
#include "devicestatus_common.h"
#include "devicestatus_msdp_client_impl.h"
#include "accesstoken_kit.h"
namespace OHOS {
namespace Msdp {
using namespace Security::AccessToken;
class DevicestatusService;
class DevicestatusManager {
public:
@ -58,6 +60,7 @@ public:
int32_t MsdpDataCallback(const DevicestatusDataUtils::DevicestatusData& data);
int32_t LoadAlgorithm(bool bCreate);
int32_t UnloadAlgorithm(bool bCreate);
void GetPackageName(AccessTokenID tokenId, std::string &packageName);
private:
struct classcomp {

View File

@ -32,6 +32,7 @@
#include "result_set.h"
#include "devicestatus_data_utils.h"
#include "devicestatus_delayed_sp_singleton.h"
#include "devicestatus_dumper.h"
#include "devicestatus_msdp_interface.h"
#include "devicestatus_sensor_interface.h"

View File

@ -23,6 +23,7 @@
#include "devicestatus_srv_stub.h"
#include "idevicestatus_callback.h"
#include "devicestatus_data_utils.h"
#include "devicestatus_dumper.h"
#include "devicestatus_manager.h"
#include "devicestatus_delayed_sp_singleton.h"
@ -32,6 +33,7 @@ class DevicestatusService final : public SystemAbility, public DevicestatusSrvSt
DECLARE_SYSTEM_ABILITY(DevicestatusService)
DECLARE_DELAYED_SP_SINGLETON(DevicestatusService);
public:
virtual void OnDump() override;
virtual void OnStart() override;
virtual void OnStop() override;
@ -42,6 +44,8 @@ public:
DevicestatusDataUtils::DevicestatusData GetCache(const DevicestatusDataUtils::DevicestatusType& type) override;
bool IsServiceReady();
std::shared_ptr<DevicestatusManager> GetDevicestatusManager();
int Dump(int fd, const std::vector<std::u16string>& args) override;
void ReportSensorSysEvent(const DevicestatusDataUtils::DevicestatusType& type, bool enable);
private:
bool Init();
bool ready_ = false;

View File

@ -0,0 +1,330 @@
/*
* 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 "devicestatus_dumper.h"
#include <cinttypes>
#include <csignal>
#include <cstring>
#include <getopt.h>
#include <iomanip>
#include <map>
#include <sstream>
#include "securec.h"
#include "string_ex.h"
#include "unique_fd.h"
#include "devicestatus_common.h"
namespace OHOS {
namespace Msdp {
namespace {
constexpr uint32_t MS_NS = 1000000;
}
void DevicestatusDumper::ParseCommand(int32_t fd, const std::vector<std::string> &args,
const std::vector<DevicestatusDataUtils::DevicestatusData> &datas)
{
int32_t optionIndex = 0;
struct option dumpOptions[] = {
{"help", no_argument, 0, 'h'},
{"subscribe", no_argument, 0, 's'},
{"list", no_argument, 0, 'l'},
{"current", no_argument, 0, 'c'},
{NULL, 0, 0, 0}
};
char **argv = new char *[args.size()];
for (size_t i = 0; i < args.size(); ++i) {
argv[i] = new char[args[i].size() + 1];
if (strcpy_s(argv[i], args[i].size() + 1, args[i].c_str()) != RET_OK) {
DEV_HILOGE(SERVICE, "strcpy_s error");
goto RELEASE_RES;
return;
}
}
optind = 1;
int32_t c;
while ((c = getopt_long(args.size(), argv, "hslc", dumpOptions, &optionIndex)) != -1) {
switch (c) {
case 'h': {
DumpHelpInfo(fd);
break;
}
case 's': {
DumpDevicestatusSubscriber(fd);
break;
}
case 'l': {
DumpDevicestatusChanges(fd);
break;
}
case 'c': {
DumpDevicestatusCurrentStatus(fd, datas);
break;
}
default: {
dprintf(fd, "cmd param is error\n");
DumpHelpInfo(fd);
break;
}
}
}
RELEASE_RES:
for (size_t i = 0; i < args.size(); ++i) {
delete[] argv[i];
}
delete[] argv;
}
void DevicestatusDumper::DumpDevicestatusSubscriber(int32_t fd)
{
DEV_HILOGI(SERVICE, "start");
if (appInfoMap_.empty()) {
DEV_HILOGI(SERVICE, "appInfoMap_ is empty");
return;
}
std::string startTime;
DumpCurrentTime(startTime);
dprintf(fd, "Current time: %s \n", startTime.c_str());
for (const auto &item : appInfoMap_) {
for (auto appInfo : item.second) {
dprintf(fd, "startTime:%s | uid:%d | pid:%d | type:%s | packageName:%s\n",
appInfo->startTime.c_str(), appInfo->uid, appInfo->pid, GetStatusType(appInfo->type).c_str(),
appInfo->packageName.c_str());
}
}
}
void DevicestatusDumper::DumpDevicestatusChanges(int32_t fd)
{
DEV_HILOGI(SERVICE, "start");
if (deviceStatusQueue_.empty()) {
DEV_HILOGI(SERVICE, "deviceStatusQueue_ is empty");
return;
}
std::string startTime;
DumpCurrentTime(startTime);
dprintf(fd, "Current time: %s \n", startTime.c_str());
size_t length = deviceStatusQueue_.size() > MAX_DEVICE_STATUS_SIZE ? \
MAX_DEVICE_STATUS_SIZE : deviceStatusQueue_.size();
for (size_t i = 0; i < length; ++i) {
auto record = deviceStatusQueue_.front();
if (record == nullptr) {
DEV_HILOGI(SERVICE, "deviceStatusQueue_ is is null");
continue;
}
deviceStatusQueue_.push(record);
deviceStatusQueue_.pop();
dprintf(fd, "startTime:%s | type:%s | value:%s \n",
record->startTime.c_str(), GetStatusType(record->data.type).c_str(),
GetDeviceState(record->data.value).c_str());
}
}
void DevicestatusDumper::DumpDevicestatusCurrentStatus(int32_t fd,
const std::vector<DevicestatusDataUtils::DevicestatusData> &datas) const
{
DEV_HILOGI(SERVICE, "start");
std::string startTime;
DumpCurrentTime(startTime);
dprintf(fd, "Current time: %s \n", startTime.c_str());
dprintf(fd, "Current device status: \n");
if (datas.empty()) {
dprintf(fd, "No device status available\n");
return;
}
for (auto it = datas.begin(); it != datas.end(); ++it) {
if (it->value == DevicestatusDataUtils::VALUE_INVALID) {
continue;
}
dprintf(fd, "Device status Type is %s , current type state is %s .\n",
GetStatusType(it->type).c_str(), GetDeviceState(it->value).c_str());
}
}
std::string DevicestatusDumper::GetDeviceState(const DevicestatusDataUtils::DevicestatusValue &value) const
{
std::string state;
switch (value) {
case DevicestatusDataUtils::VALUE_ENTER: {
state = "enter";
break;
}
case DevicestatusDataUtils::VALUE_EXIT: {
state = "exit";
break;
}
case DevicestatusDataUtils::VALUE_INVALID: {
state = "invalid";
break;
}
default: {
state = "unknown";
break;
}
}
return state;
}
std::string DevicestatusDumper::GetStatusType(const DevicestatusDataUtils::DevicestatusType &type) const
{
std::string stateType;
switch (type) {
case DevicestatusDataUtils::TYPE_HIGH_STILL: {
stateType = "high still";
break;
}
case DevicestatusDataUtils::TYPE_FINE_STILL: {
stateType = "fine still";
break;
}
case DevicestatusDataUtils::TYPE_CAR_BLUETOOTH: {
stateType = "car bluetooth";
break;
}
case DevicestatusDataUtils::TYPE_LID_OPEN: {
stateType = "lid open";
break;
}
default: {
stateType = "unknown";
break;
}
}
return stateType;
}
void DevicestatusDumper::DumpCurrentTime(std::string &startTime) const
{
timespec curTime;
clock_gettime(CLOCK_REALTIME, &curTime);
struct tm *timeinfo = localtime(&(curTime.tv_sec));
if (timeinfo == nullptr) {
DEV_HILOGI(SERVICE, "get localtime failed");
return;
}
startTime.append(std::to_string(timeinfo->tm_year + BASE_YEAR)).append("-")
.append(std::to_string(timeinfo->tm_mon + BASE_MON)).append("-").append(std::to_string(timeinfo->tm_mday))
.append(" ").append(std::to_string(timeinfo->tm_hour)).append(":").append(std::to_string(timeinfo->tm_min))
.append(":").append(std::to_string(timeinfo->tm_sec)).append(".")
.append(std::to_string(curTime.tv_nsec / MS_NS));
}
void DevicestatusDumper::DumpHelpInfo(int32_t fd) const
{
dprintf(fd, "Usage:\n");
dprintf(fd, " -h: dump help\n");
dprintf(fd, " -s: dump the device_status subscribers\n");
dprintf(fd, " -l: dump the last 10 device status change\n");
dprintf(fd, " -c: dump the device_status current device status\n");
}
void DevicestatusDumper::SaveAppInfo(std::shared_ptr<AppInfo> appInfo)
{
DEV_HILOGI(SERVICE, "Enter");
DumpCurrentTime(appInfo->startTime);
std::set<std::shared_ptr<AppInfo>> appInfos;
auto iter = appInfoMap_.find(appInfo->type);
if (iter == appInfoMap_.end()) {
if (appInfos.insert(appInfo).second) {
appInfoMap_.insert(std::make_pair(appInfo->type, appInfos));
}
} else {
if (!appInfoMap_[iter->first].insert(appInfo).second) {
DEV_HILOGW(SERVICE, "duplicated app info");
return;
}
}
}
void DevicestatusDumper::RemoveAppInfo(std::shared_ptr<AppInfo> appInfo)
{
DEV_HILOGI(SERVICE, "Enter");
if (appInfo->callback == nullptr) {
DEV_HILOGW(SERVICE, "callback is null");
return;
}
DEV_HILOGI(SERVICE, "appInfoMap_ size=%{public}zu", appInfoMap_.size());
auto appInfoSetIter = appInfoMap_.find(appInfo->type);
if (appInfoSetIter == appInfoMap_.end()) {
DEV_HILOGE(SERVICE, "not exist %d type appInfo", appInfo->type);
return;
}
DEV_HILOGI(SERVICE, "callbacklist type=%d size=%{public}zu",
appInfo->type, appInfoMap_[appInfoSetIter->first].size());
auto iter = appInfoMap_.find(appInfo->type);
if (iter == appInfoMap_.end()) {
DEV_HILOGW(SERVICE, "Remove app info is not exists");
return;
}
for (const auto &item : iter->second) {
if (item->pid == appInfo->pid) {
iter->second.erase(item);
break;
}
}
}
void DevicestatusDumper::pushDeviceStatus(const DevicestatusDataUtils::DevicestatusData& data)
{
DEV_HILOGI(SERVICE, "Enter");
std::unique_lock lock(mutex_);
auto record = std::make_shared<DeviceStatusRecord>();
if (record == nullptr) {
DEV_HILOGI(SERVICE, "record is null");
return;
}
DumpCurrentTime(record->startTime);
record->data = data;
deviceStatusQueue_.push(record);
if (deviceStatusQueue_.size() > MAX_DEVICE_STATUS_SIZE) {
deviceStatusQueue_.pop();
}
}
std::string DevicestatusDumper::GetPackageName(Security::AccessToken::AccessTokenID tokenId)
{
DEV_HILOGI(SERVICE, "Enter");
std::string packageName = "unknown";
int32_t tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
switch (tokenType) {
case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE: {
Security::AccessToken::NativeTokenInfo tokenInfo;
if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) {
DEV_HILOGI(SERVICE, "get native token info fail");
return packageName;
}
packageName = tokenInfo.processName;
break;
}
case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: {
Security::AccessToken::HapTokenInfo hapInfo;
if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != 0) {
DEV_HILOGI(SERVICE, "get hap token info fail");
return packageName;
}
packageName = hapInfo.bundleName;
break;
}
default: {
DEV_HILOGI(SERVICE, "token type not match");
break;
}
}
return packageName;
}
} // namespace Msdp
} // namespace OHOS

View File

@ -14,9 +14,11 @@
*/
#include "devicestatus_manager.h"
#include "bytrace_adapter.h"
namespace OHOS {
namespace Msdp {
using namespace OHOS::HiviewDFX;
namespace {
constexpr int32_t ERR_OK = 0;
constexpr int32_t ERR_NG = -1;
@ -214,6 +216,7 @@ void DevicestatusManager::Subscribe(const DevicestatusDataUtils::DevicestatusTyp
}
}
}
BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_STOP, BytraceAdapter::SUBSCRIBE, BytraceAdapter::SERVICE);
DEV_HILOGI(SERVICE, "Subscribe success,Exit");
}
@ -249,6 +252,7 @@ void DevicestatusManager::UnSubscribe(const DevicestatusDataUtils::DevicestatusT
} else {
DEV_HILOGI(SERVICE, "other subscribe exist");
}
BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_STOP, BytraceAdapter::UNSUBSCRIBE, BytraceAdapter::SERVICE);
DEV_HILOGI(SERVICE, "UnSubscribe success,Exit");
}
@ -273,5 +277,34 @@ int32_t DevicestatusManager::UnloadAlgorithm(bool bCreate)
return ERR_OK;
}
void DevicestatusManager::GetPackageName(AccessTokenID tokenId, std::string &packageName)
{
int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId);
switch (tokenType) {
case ATokenTypeEnum::TOKEN_NATIVE: {
NativeTokenInfo tokenInfo;
if (AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) {
DEV_HILOGE(SERVICE, "get native token info fail");
return;
}
packageName = tokenInfo.processName;
break;
}
case ATokenTypeEnum::TOKEN_HAP: {
HapTokenInfo hapInfo;
if (AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != 0) {
DEV_HILOGE(SERVICE, "get hap token info fail");
return;
}
packageName = hapInfo.bundleName;
break;
}
default: {
DEV_HILOGE(SERVICE, "token type not match");
break;
}
}
}
} // namespace Msdp
} // namespace OHOS

View File

@ -210,12 +210,14 @@ ErrCode DevicestatusMsdpClientImpl::UnregisterMsdp(void)
int32_t DevicestatusMsdpClientImpl::MsdpCallback(const DevicestatusDataUtils::DevicestatusData& data)
{
DEV_HILOGI(SERVICE, "Enter");
DevicestatusDumper::GetInstance().pushDeviceStatus(data);
SaveObserverData(data);
if (notifyManagerFlag_) {
ImplCallback(data);
notifyManagerFlag_ = false;
}
DEV_HILOGI(SERVICE, "Exit");
return ERR_OK;
}

View File

@ -15,15 +15,21 @@
#include "devicestatus_service.h"
#include <vector>
#include <ipc_skeleton.h>
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "string_ex.h"
#include "system_ability_definition.h"
#include "devicestatus_permission.h"
#include "devicestatus_common.h"
#include "devicestatus_dumper.h"
#include "hisysevent.h"
#include "bytrace_adapter.h"
namespace OHOS {
namespace Msdp {
using namespace OHOS::HiviewDFX;
namespace {
auto ms = DelayedSpSingleton<DevicestatusService>::GetInstance();
const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(ms.GetRefPtr());
@ -35,6 +41,11 @@ DevicestatusService::DevicestatusService() : SystemAbility(MSDP_DEVICESTATUS_SER
DevicestatusService::~DevicestatusService() {}
void DevicestatusService::OnDump()
{
DEV_HILOGI(SERVICE, "OnDump");
}
void DevicestatusService::OnStart()
{
DEV_HILOGI(SERVICE, "Enter");
@ -71,6 +82,41 @@ void DevicestatusService::OnStop()
DEV_HILOGI(SERVICE, "unload algorithm library exit");
}
int DevicestatusService::Dump(int fd, const std::vector<std::u16string>& args)
{
DEV_HILOGI(SERVICE, "dump DeviceStatusServiceInfo");
if (fd < 0) {
DEV_HILOGE(SERVICE, "fd is invalid");
return RET_NG;
}
DevicestatusDumper &deviceStatusDumper = DevicestatusDumper::GetInstance();
if (args.empty()) {
DEV_HILOGE(SERVICE, "param cannot be empty");
dprintf(fd, "param cannot be empty\n");
deviceStatusDumper.DumpHelpInfo(fd);
return RET_NG;
}
std::vector<std::string> argList = { "" };
std::transform(args.begin(), args.end(), std::back_inserter(argList),
[](const std::u16string &arg) {
return Str16ToStr8(arg);
});
DevicestatusDataUtils::DevicestatusType type;
std::vector<DevicestatusDataUtils::DevicestatusData> datas;
for (type = DevicestatusDataUtils::TYPE_HIGH_STILL;
type <= DevicestatusDataUtils::TYPE_LID_OPEN;
type = (DevicestatusDataUtils::DevicestatusType)(type+1)) {
DevicestatusDataUtils::DevicestatusData data = GetCache(type);
if (data.value != DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID) {
datas.emplace_back(data);
}
}
deviceStatusDumper.ParseCommand(fd, argList, datas);
return RET_OK;
}
bool DevicestatusService::Init()
{
DEV_HILOGI(SERVICE, "Enter");
@ -106,7 +152,23 @@ void DevicestatusService::Subscribe(const DevicestatusDataUtils::DevicestatusTyp
DEV_HILOGI(SERVICE, "UnSubscribe func is nullptr");
return;
}
auto appInfo = std::make_shared<AppInfo>();
if (appInfo == nullptr) {
DEV_HILOGI(SERVICE, "appInfo is null");
return;
}
appInfo->uid = GetCallingUid();
appInfo->pid = GetCallingPid();
appInfo->tokenId = GetCallingTokenID();
appInfo->packageName = DevicestatusDumper::GetInstance().GetPackageName(appInfo->tokenId);
appInfo->type = type;
appInfo->callback = callback;
DevicestatusDumper::GetInstance().SaveAppInfo(appInfo);
BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_START, BytraceAdapter::SUBSCRIBE, BytraceAdapter::SERVICE);
devicestatusManager_->Subscribe(type, callback);
ReportSensorSysEvent(type, true);
}
void DevicestatusService::UnSubscribe(const DevicestatusDataUtils::DevicestatusType& type,
@ -117,7 +179,22 @@ void DevicestatusService::UnSubscribe(const DevicestatusDataUtils::DevicestatusT
DEV_HILOGI(SERVICE, "UnSubscribe func is nullptr");
return;
}
auto appInfo = std::make_shared<AppInfo>();
if (appInfo == nullptr) {
DEV_HILOGI(SERVICE, "appInfo is null");
return;
}
appInfo->uid = GetCallingUid();
appInfo->pid = GetCallingPid();
appInfo->tokenId = GetCallingTokenID();
appInfo->packageName = DevicestatusDumper::GetInstance().GetPackageName(appInfo->tokenId);
appInfo->type = type;
appInfo->callback = callback;
DevicestatusDumper::GetInstance().RemoveAppInfo(appInfo);
BytraceAdapter::StartBytrace(BytraceAdapter::TRACE_START, BytraceAdapter::UNSUBSCRIBE, BytraceAdapter::SERVICE);
devicestatusManager_->UnSubscribe(type, callback);
ReportSensorSysEvent(type, false);
}
DevicestatusDataUtils::DevicestatusData DevicestatusService::GetCache(const \
@ -132,5 +209,21 @@ DevicestatusDataUtils::DevicestatusData DevicestatusService::GetCache(const \
}
return devicestatusManager_->GetLatestDevicestatusData(type);
}
void DevicestatusService::ReportSensorSysEvent(const DevicestatusDataUtils::DevicestatusType& type, bool enable)
{
auto uid = this->GetCallingUid();
auto callerToken = this->GetCallingTokenID();
std::string packageName("");
devicestatusManager_->GetPackageName(callerToken, packageName);
std::string message;
if (enable) {
HiSysEvent::Write(HiSysEvent::Domain::MSDP, "Subscribe", HiSysEvent::EventType::STATISTIC,
"UID", uid, "PKGNAME", packageName, "TYPE", type);
} else {
HiSysEvent::Write(HiSysEvent::Domain::MSDP, "UnSubscribe", HiSysEvent::EventType::STATISTIC,
"UID", uid, "PKGNAME", packageName, "TYPE", type);
}
}
} // namespace Msdp
} // namespace OHOS

View File

@ -48,6 +48,7 @@ ohos_moduletest("test_devicestatus_module") {
external_deps = [
"ability_base:base",
"ability_base:want",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"common_event_service:cesfwk_innerkits",

View File

@ -48,6 +48,7 @@ ohos_unittest("test_devicestatus_service") {
external_deps = [
"ability_base:base",
"ability_base:want",
"access_token:libaccesstoken_sdk",
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"common_event_service:cesfwk_innerkits",

View File

@ -18,8 +18,6 @@
#include <string>
#include "permission/permission_kit.h"
namespace OHOS {
namespace Msdp {
class DevicestatusPermission {