1 /*
2  * Copyright (C) 2016 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 
17 #ifndef ANDROIDCONTEXTHUB_H_
18 #define ANDROIDCONTEXTHUB_H_
19 
20 #include "contexthub.h"
21 
22 #include <poll.h>
23 #include <unistd.h>
24 
25 namespace android {
26 
27 /*
28  * Communicates with a context hub via the /dev/nanohub interface
29  */
30 class AndroidContextHub : public ContextHub {
31   public:
32     ~AndroidContextHub();
33 
34     // Performs system resource cleanup in the event that the program is
35     // terminated abnormally (via std::terminate)
36     static void TerminateHandler();
37 
38     bool Initialize() override;
39     bool LoadCalibration() override;
40     void SetLoggingEnabled(bool logging_enabled) override;
41 
42   protected:
43     ContextHub::TransportResult WriteEvent(
44         const std::vector<uint8_t>& request) override;
45     ContextHub::TransportResult ReadEvent(std::vector<uint8_t>& response,
46         int timeout_ms) override;
47     bool FlashSensorHub(const std::vector<uint8_t>& bytes) override;
48 
49     bool SetCalibration(SensorType sensor_type, int32_t data) override;
50     bool SetCalibration(SensorType sensor_type, float data) override;
51     bool SetCalibration(SensorType sensor_type, int32_t x, int32_t y, int32_t z)
52         override;
53     bool SetCalibration(SensorType sensor_type, int32_t x, int32_t y,
54         int32_t z, int32_t w) override;
55     bool SaveCalibration() override;
56 
57   private:
58     int sensor_fd_ = -1;
59     int comms_fd_ = -1;
60 
61     ContextHub::TransportResult ReadEventFromFd(int fd,
62         std::vector<uint8_t>& message);
63     int ResetPollFds(struct pollfd *pfds, size_t count);
64     static const char *SensorTypeToCalibrationKey(SensorType sensor_type);
65 };
66 
67 }  // namespace android
68 
69 #endif  // ANDROIDCONTEXTHUB_H_
70