1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "HidRawSensorDaemon.h"
17 #include "ConnectionDetector.h"
18 #include "DynamicSensorManager.h"
19 #include "HidRawSensorDevice.h"
20 
21 #include <utils/Log.h>
22 #include <utils/SystemClock.h>
23 
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 
29 #include <iomanip>
30 #include <sstream>
31 
32 #define DEV_PATH                "/dev/"
33 #define DEV_NAME_REGEX          "^hidraw[0-9]+$"
34 
35 namespace android {
36 namespace SensorHalExt {
37 
HidRawSensorDaemon(DynamicSensorManager & manager)38 HidRawSensorDaemon::HidRawSensorDaemon(DynamicSensorManager& manager)
39         : BaseDynamicSensorDaemon(manager) {
40     mDetector = new FileConnectionDetector(
41             this, std::string(DEV_PATH), std::string(DEV_NAME_REGEX));
42     mDetector->Init();
43 }
44 
createSensor(const std::string & deviceKey)45 BaseSensorVector HidRawSensorDaemon::createSensor(const std::string &deviceKey) {
46     BaseSensorVector ret;
47     sp<HidRawSensorDevice> device(HidRawSensorDevice::create(deviceKey));
48 
49     if (device != nullptr) {
50         ALOGV("created HidRawSensorDevice(%p) successfully on device %s contains %zu sensors",
51               device.get(), deviceKey.c_str(), device->getSensors().size());
52 
53         // convert type
54         for (auto &i : device->getSensors()) {
55             ret.push_back(i);
56         }
57         mHidRawSensorDevices.emplace(deviceKey, device);
58     } else {
59         ALOGE("failed to create HidRawSensorDevice object");
60     }
61 
62     ALOGE("return %zu sensors", ret.size());
63     return ret;
64 }
65 
removeSensor(const std::string & deviceKey)66 void HidRawSensorDaemon::removeSensor(const std::string &deviceKey) {
67     mHidRawSensorDevices.erase(deviceKey);
68 }
69 
70 } // namespace SensorHalExt
71 } // namespace android
72 
73