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 #ifndef ANDROID_SENSORHAL_EXT_HIDRAW_DEVICE_H
17 #define ANDROID_SENSORHAL_EXT_HIDRAW_DEVICE_H
18 
19 #include "HidDevice.h"
20 
21 #include <HidParser.h>
22 #include <string>
23 #include <vector>
24 #include <unordered_set>
25 #include <unordered_map>
26 
27 namespace android {
28 namespace SensorHalExt {
29 
30 using HidUtil::HidParser;
31 using HidUtil::HidReport;
32 
33 class HidRawDevice : public HidDevice {
34     friend class HidRawDeviceTest;
35 public:
36     HidRawDevice(const std::string &devName, const std::unordered_set<unsigned int> &usageSet);
37     virtual ~HidRawDevice();
38 
39     // test if the device initialized successfully
40     bool isValid();
41 
42     // implement HidDevice pure virtuals
getDeviceInfo()43     virtual HidDeviceInfo& getDeviceInfo() override { return mDeviceInfo; }
44     virtual bool getFeature(uint8_t id, std::vector<uint8_t> *out) override;
45     virtual bool setFeature(uint8_t id, const std::vector<uint8_t> &in) override;
46     virtual bool sendReport(uint8_t id, std::vector<uint8_t> &data) override;
47     virtual bool receiveReport(uint8_t *id, std::vector<uint8_t> *data) override;
48 
49 protected:
50     bool populateDeviceInfo();
51     size_t getReportSize(int type, uint8_t id);
52     bool generateDigest(const std::unordered_set<uint32_t> &usage);
53     size_t calculateReportBitSize(const std::vector<HidReport> &reportItems);
54     const HidParser::ReportPacket *getReportPacket(unsigned int type, unsigned int id);
55 
56     typedef std::pair<unsigned int, unsigned int> ReportTypeIdPair;
57     struct UnsignedIntPairHash {
operatorUnsignedIntPairHash58         size_t operator()(const ReportTypeIdPair& v) const {
59             std::hash<unsigned int> hash;
60             return hash(v.first) ^ hash(v.second);
61         }
62     };
63 
64     std::unordered_map<ReportTypeIdPair, const HidParser::ReportPacket *, UnsignedIntPairHash>
65             mReportTypeIdMap;
66 
67     HidParser::DigestVector mDigestVector;
68 private:
69     std::mutex mIoBufferLock;
70     std::vector<uint8_t> mIoBuffer;
71 
72     int mDevFd;
73     HidDeviceInfo mDeviceInfo;
74     bool mMultiIdDevice;
75     int mValid;
76 
77     HidRawDevice(const HidRawDevice &) = delete;
78     void operator=(const HidRawDevice &) = delete;
79 };
80 
81 } // namespace SensorHalExt
82 } // namespace android
83 
84 #endif // ANDROID_SENSORHAL_EXT_HIDRAW_DEVICE_H
85 
86