1 /*
2 * Copyright (c) 2024 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 <sys/time.h>
17 #include "vibrator_client_stub.h"
18 #include "message_parcel.h"
19
20 #include "miscdevice_log.h"
21 #include "vibrator_service_client.h"
22
23 #undef LOG_TAG
24 #define LOG_TAG "VibratorClientStub"
25 namespace OHOS {
26 namespace Sensors {
27 namespace {
28 constexpr int32_t TIME_CONVERSION_UNIT { 1000 };
29 } //unnamespace
30 using OHOS::Sensors::VibratorServiceClient;
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t VibratorClientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
33 MessageOption &option)
34 {
35 std::u16string descriptor = VibratorClientStub::GetDescriptor();
36 std::u16string remoteDescriptor = data.ReadInterfaceToken();
37 if (descriptor != remoteDescriptor) {
38 MISC_HILOGE("Client and service descriptors are inconsistent");
39 return OBJECT_NULL;
40 }
41 MISC_HILOGD("Begin, cmd:%{public}u", code);
42 switch (code) {
43 case TRANS_ID_PLUG_ABILITY: {
44 int32_t eventCode = 0;
45 int32_t deviceId = -1;
46 int32_t vibratorCnt = 0;
47 if (!data.ReadInt32(eventCode)) {
48 MISC_HILOGE("Read eventCode failed.");
49 return PARAMETER_ERROR;
50 }
51 if (!data.ReadInt32(deviceId)) {
52 MISC_HILOGE("Read deviceId failed.");
53 return PARAMETER_ERROR;
54 }
55 if (!data.ReadInt32(vibratorCnt)) {
56 MISC_HILOGE("Read vibratorCnt failed.");
57 return PARAMETER_ERROR;
58 }
59 int result = ProcessPlugEvent(eventCode, deviceId, vibratorCnt);
60 if (result != NO_ERROR) {
61 MISC_HILOGE("ProcessPlugEvent failed");
62 }
63 return NO_ERROR;
64 }
65 default:
66 MISC_HILOGE("Unsupported command, cmd:%{public}u", code);
67 return PARAMETER_ERROR;
68 }
69 return NO_ERROR;
70 }
71
GetSystemTime()72 int64_t VibratorClientStub::GetSystemTime()
73 {
74 struct timeval curTime;
75 curTime.tv_sec = 0;
76 curTime.tv_usec = 0;
77 gettimeofday(&curTime, NULL);
78 int64_t timestamp = static_cast<int64_t>(
79 curTime.tv_sec * TIME_CONVERSION_UNIT + curTime.tv_usec / TIME_CONVERSION_UNIT);
80 return timestamp;
81 }
82
ProcessPlugEvent(int32_t eventCode,int32_t deviceId,int32_t vibratorCnt)83 int VibratorClientStub::ProcessPlugEvent(int32_t eventCode, int32_t deviceId, int32_t vibratorCnt)
84 {
85 MISC_HILOGD("Begin, eventCode=%{public}d, deviceId:%{public}d, vibratorCnt:%{public}d",
86 eventCode, deviceId, vibratorCnt);
87 VibratorStatusEvent statusEvent = {
88 .type = static_cast<VibratorPlugState>(eventCode),
89 .deviceId = deviceId,
90 .vibratorCnt = vibratorCnt,
91 .timestamp = GetSystemTime(),
92 };
93 auto &client = VibratorServiceClient::GetInstance();
94 bool ret = client.HandleVibratorData(statusEvent);
95 if (!ret) {
96 MISC_HILOGE("Handle vibrator data failed, ret:%{public}d", ret);
97 return DEVICE_OPERATION_FAILED;
98 }
99 MISC_HILOGD("Success to process plug event");
100 return NO_ERROR;
101 }
102 } // namespace Sensors
103 } // namespace OHOS