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 
17 #ifndef ANDROID_SENSORHAL_EXT_CONNECTION_DETECTOR_H
18 #define ANDROID_SENSORHAL_EXT_CONNECTION_DETECTOR_H
19 
20 #include "BaseDynamicSensorDaemon.h"
21 #include <utils/Thread.h>
22 #include <utils/Looper.h>
23 
24 #include <regex>
25 
26 #include <poll.h>
27 
28 namespace android {
29 namespace SensorHalExt {
30 
31 // Abstraction of connection detector: an entity that calls
32 // BaseDynamicSensorDaemon::onConnectionChange() when necessary.
33 class ConnectionDetector : virtual public RefBase {
34 public:
ConnectionDetector(BaseDynamicSensorDaemon * d)35     ConnectionDetector(BaseDynamicSensorDaemon *d) : mDaemon(d) { }
36     virtual ~ConnectionDetector() = default;
Init()37     virtual void Init() {}
38 protected:
39     BaseDynamicSensorDaemon* mDaemon;
40 };
41 
42 // Open a socket that listen to localhost:port and notify sensor daemon of connection and
43 // disconnection event when socket is connected or disconnected, respectively. Only one concurrent
44 // client is accepted.
45 class SocketConnectionDetector : public ConnectionDetector, public Thread {
46 public:
47     SocketConnectionDetector(BaseDynamicSensorDaemon *d, int port);
48     virtual ~SocketConnectionDetector();
49     void Init() override;
50 private:
51     // implement virtual of Thread
52     virtual bool threadLoop();
53     int waitForConnection();
54     static void waitForDisconnection(int connFd);
55 
56     int mListenFd;
57     std::string mDevice;
58 };
59 
60 // Detect file change under path and notify sensor daemon of connection and disconnection event when
61 // file is created in or removed from the directory, respectively.
62 class FileConnectionDetector : public ConnectionDetector, public Thread {
63 public:
64     FileConnectionDetector(
65             BaseDynamicSensorDaemon *d, const std::string &path, const std::string &regex);
66     virtual ~FileConnectionDetector();
67     void Init() override;
68 private:
69     static constexpr int POLL_IDENT = 1;
70     // implement virtual of Thread
71     virtual bool threadLoop();
72 
73     bool matches(const std::string &name) const;
74     void processExistingFiles() const;
75     void handleInotifyData(ssize_t len, const char *data);
76     bool readInotifyData();
77     std::string getFullName(const std::string name) const;
78 
79     std::string mPath;
80     std::regex mRegex;
81     sp<Looper> mLooper;
82     int mInotifyFd;
83 };
84 
85 } // namespace SensorHalExt
86 } // namespace android
87 
88 #endif // ANDROID_SENSORHAL_EXT_DYNAMIC_SENSOR_DAEMON_H
89