1 /*
2 * Copyright (c) 2021 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 #include "sensor_service_stub.h"
17
18 #include <string>
19 #include <sys/socket.h>
20 #include <unistd.h>
21 #include <vector>
22
23 #include "hisysevent.h"
24 #include "ipc_skeleton.h"
25 #include "message_parcel.h"
26 #include "permission_util.h"
27 #include "sensor_client_proxy.h"
28 #include "sensors_errors.h"
29
30 namespace OHOS {
31 namespace Sensors {
32 using namespace OHOS::HiviewDFX;
33
34 namespace {
35 constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "SensorServiceStub" };
36 } // namespace
37
SensorServiceStub()38 SensorServiceStub::SensorServiceStub()
39 {
40 CALL_LOG_ENTER;
41 baseFuncs_[ENABLE_SENSOR] = &SensorServiceStub::SensorEnableInner;
42 baseFuncs_[DISABLE_SENSOR] = &SensorServiceStub::SensorDisableInner;
43 baseFuncs_[GET_SENSOR_LIST] = &SensorServiceStub::GetAllSensorsInner;
44 baseFuncs_[TRANSFER_DATA_CHANNEL] = &SensorServiceStub::CreateDataChannelInner;
45 baseFuncs_[DESTROY_SENSOR_CHANNEL] = &SensorServiceStub::DestroyDataChannelInner;
46 }
47
~SensorServiceStub()48 SensorServiceStub::~SensorServiceStub()
49 {
50 CALL_LOG_ENTER;
51 baseFuncs_.clear();
52 }
53
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)54 int32_t SensorServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
55 MessageOption &option)
56 {
57 SEN_HILOGD("begin, cmd : %{public}u", code);
58 std::u16string descriptor = SensorServiceStub::GetDescriptor();
59 std::u16string remoteDescriptor = data.ReadInterfaceToken();
60 if (descriptor != remoteDescriptor) {
61 SEN_HILOGE("client and service descriptors are inconsistent");
62 return OBJECT_NULL;
63 }
64 auto itFunc = baseFuncs_.find(code);
65 if (itFunc != baseFuncs_.end()) {
66 auto memberFunc = itFunc->second;
67 if (memberFunc != nullptr) {
68 return (this->*memberFunc)(data, reply);
69 }
70 }
71 SEN_HILOGD("no member func supporting, applying default process");
72 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
73 }
74
SensorEnableInner(MessageParcel & data,MessageParcel & reply)75 ErrCode SensorServiceStub::SensorEnableInner(MessageParcel &data, MessageParcel &reply)
76 {
77 (void)reply;
78 uint32_t sensorId = data.ReadUint32();
79 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
80 int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId);
81 if (ret != PERMISSION_GRANTED) {
82 HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL",
83 HiSysEvent::EventType::SECURITY, "PKG_NAME", "SensorEnableInner", "ERROR_CODE", ret);
84 SEN_HILOGE("sensorId:%{public}u grant failed,result:%{public}d", sensorId, ret);
85 return PERMISSION_DENIED;
86 }
87 return EnableSensor(sensorId, data.ReadInt64(), data.ReadInt64());
88 }
89
SensorDisableInner(MessageParcel & data,MessageParcel & reply)90 ErrCode SensorServiceStub::SensorDisableInner(MessageParcel &data, MessageParcel &reply)
91 {
92 (void)reply;
93 uint32_t sensorId = data.ReadUint32();
94 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
95 int32_t ret = permissionUtil.CheckSensorPermission(GetCallingTokenID(), sensorId);
96 if (ret != PERMISSION_GRANTED) {
97 HiSysEvent::Write(HiSysEvent::Domain::SENSOR, "SENSOR_VERIFY_ACCESS_TOKEN_FAIL",
98 HiSysEvent::EventType::SECURITY, "PKG_NAME", "SensorDisableInner", "ERROR_CODE", ret);
99 SEN_HILOGE("sensorId:%{public}u grant failed,result:%{public}d", sensorId, ret);
100 return PERMISSION_DENIED;
101 }
102 return DisableSensor(sensorId);
103 }
104
GetAllSensorsInner(MessageParcel & data,MessageParcel & reply)105 ErrCode SensorServiceStub::GetAllSensorsInner(MessageParcel &data, MessageParcel &reply)
106 {
107 (void)data;
108 std::vector<Sensor> sensors(GetSensorList());
109 int32_t sensorCount = int32_t { sensors.size() };
110 reply.WriteInt32(sensorCount);
111 for (int32_t i = 0; i < sensorCount; i++) {
112 bool flag = sensors[i].Marshalling(reply);
113 if (!flag) {
114 SEN_HILOGE("sensor %{public}d failed", i);
115 return GET_SENSOR_LIST_ERR;
116 }
117 }
118 return NO_ERROR;
119 }
120
CreateDataChannelInner(MessageParcel & data,MessageParcel & reply)121 ErrCode SensorServiceStub::CreateDataChannelInner(MessageParcel &data, MessageParcel &reply)
122 {
123 (void)reply;
124 sptr<SensorBasicDataChannel> sensorChannel = new (std::nothrow) SensorBasicDataChannel();
125 CHKPR(sensorChannel, OBJECT_NULL);
126 auto ret = sensorChannel->CreateSensorBasicChannel(data);
127 if (ret != ERR_OK) {
128 SEN_HILOGE("CreateSensorBasicChannel ret:%{public}d", ret);
129 return OBJECT_NULL;
130 }
131 sptr<IRemoteObject> sensorClient = data.ReadRemoteObject();
132 CHKPR(sensorClient, OBJECT_NULL);
133 return TransferDataChannel(sensorChannel, sensorClient);
134 }
135
DestroyDataChannelInner(MessageParcel & data,MessageParcel & reply)136 ErrCode SensorServiceStub::DestroyDataChannelInner(MessageParcel &data, MessageParcel &reply)
137 {
138 sptr<IRemoteObject> sensorClient = data.ReadRemoteObject();
139 CHKPR(sensorClient, OBJECT_NULL);
140 return DestroySensorChannel(sensorClient);
141 }
142 } // namespace Sensors
143 } // namespace OHOS
144