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 CHRE_HOST_SOCKET_SERVER_H_
18 #define CHRE_HOST_SOCKET_SERVER_H_
19 
20 #include <poll.h>
21 
22 #include <atomic>
23 #include <functional>
24 #include <map>
25 #include <mutex>
26 
27 #include <android-base/macros.h>
28 #include <cutils/sockets.h>
29 
30 namespace android {
31 namespace chre {
32 
33 class SocketServer {
34  public:
35   SocketServer();
36 
37   /**
38    * Defines the function signature of the callback given to run() which
39    * receives message data sent in by a client.
40    *
41    * @param clientId A unique identifier for the client that sent this request
42    *        (assigned locally)
43    * @param data Pointer to buffer containing the raw message data
44    * @param len Number of bytes of data received
45    */
46   typedef std::function<void(uint16_t clientId, void *data, size_t len)>
47       ClientMessageCallback;
48 
49   /**
50    * Opens the socket, and runs the receive loop until an error is encountered,
51    * or SIGINT/SIGTERM is received. Masks off all other signals.
52    *
53    * @param socketName Android socket name to use when listening
54    * @param allowSocketCreation If true, allow creation of the socket rather
55    *        than strictly inheriting it from init (used primarily for
56    *        development purposes)
57    * @param clientMessageCallback Callback to be invoked when a message is
58    *        received from a client
59    */
60   void run(const char *socketName, bool allowSocketCreation,
61            ClientMessageCallback clientMessageCallback);
62 
63   /**
64    * Delivers data to all connected clients. This method is thread-safe.
65    *
66    * @param data Pointer to buffer containing message data
67    * @param length Number of bytes of data to send
68    */
69   void sendToAllClients(const void *data, size_t length);
70 
71   /**
72    * Sends a message to one client, specified via its unique client ID. This
73    * method is thread-safe.
74    *
75    * @param data
76    * @param length
77    * @param clientId
78    *
79    * @return true if the message was successfully sent to the specified client
80    */
81   bool sendToClientById(const void *data, size_t length, uint16_t clientId);
82 
83  private:
84   DISALLOW_COPY_AND_ASSIGN(SocketServer);
85 
86   static constexpr int kMaxPendingConnectionRequests = 4;
87   static constexpr size_t kMaxActiveClients = 4;
88   static constexpr size_t kMaxPacketSize = 1024 * 1024;
89 
90   int mSockFd = INVALID_SOCKET;
91   uint16_t mNextClientId = 1;
92   // TODO: std::vector-ify this
93   struct pollfd mPollFds[1 + kMaxActiveClients] = {};
94 
95   struct ClientData {
96     uint16_t clientId;
97   };
98 
99   // Maps from socket FD to ClientData
100   std::map<int, ClientData> mClients;
101 
102   // Ensures that mClients can be safely iterated over from other threads
103   // without worrying about potential modification from the RX thread
104   std::mutex mClientsMutex;
105 
106   ClientMessageCallback mClientMessageCallback;
107 
108   void acceptClientConnection();
109   void disconnectClient(int clientSocket);
110   void handleClientData(int clientSocket);
111   bool sendToClientSocket(const void *data, size_t length, int clientSocket,
112                           uint16_t clientId);
113   void serviceSocket();
114 
115   static std::atomic<bool> sSignalReceived;
116   static void signalHandler(int signal);
117 };
118 
119 }  // namespace chre
120 }  // namespace android
121 
122 #endif  // CHRE_HOST_SOCKET_SERVER_H_
123