1  /*
2   * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3   * Licensed under the Apache License, Version 2.0 (the "License");
4   * you may not use this file except in compliance with the License.
5   * You may obtain a copy of the License at
6   *
7   *     http://www.apache.org/licenses/LICENSE-2.0
8   *
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  #ifndef COER_EVENT_SERVER_H
17  #define COER_EVENT_SERVER_H
18  
19  #include <map>
20  #include <memory>
21  #include <string>
22  #include <vector>
23  
24  #include "device_node.h"
25  
26  struct epoll_event;
27  namespace OHOS {
28  namespace HiviewDFX {
29  constexpr int UN_INIT_INT_TYPE_VAL = -1;
30  class SocketDevice : public DeviceNode {
31  public:
SocketDevice()32      SocketDevice() {};
~SocketDevice()33      virtual ~SocketDevice() {};
34      int Close() override;
35      int Open() override;
36      uint32_t GetEvents() override;
37      std::string GetName() override;
38      int ReceiveMsg(std::vector<std::shared_ptr<EventReceiver>> &receivers) override;
39      bool IsValidMsg(char* msg, int32_t len) override;
40  
41  private:
42      void InitSocket(int &socketId);
43      void SetUCredPid(const pid_t pid);
44  
45  private:
46      int socketId_ = UN_INIT_INT_TYPE_VAL;
47      pid_t uCredPid_ = UN_INIT_INT_TYPE_VAL;
48  };
49  
50  class BBoxDevice : public DeviceNode {
51  public:
BBoxDevice()52      BBoxDevice() {};
~BBoxDevice()53      virtual ~BBoxDevice() {};
54      int Close() override;
55      int Open() override;
56      uint32_t GetEvents() override;
57      std::string GetName() override;
58      int ReceiveMsg(std::vector<std::shared_ptr<EventReceiver>> &receivers) override;
59      bool IsValidMsg(char* msg, int32_t len) override;
60  
61  private:
62      int fd_ = UN_INIT_INT_TYPE_VAL;
63      bool hasBbox_ = false;
64  };
65  
66  class EventServer {
67  public:
EventServer()68      EventServer(): isStart_(false) {};
~EventServer()69      ~EventServer() {}
70      void Start();
71      void Stop();
72      void AddReceiver(std::shared_ptr<EventReceiver> receiver);
73  private:
74      void AddDev(std::shared_ptr<DeviceNode> dev);
75      int OpenDevs();
76      void CloseDevs();
77      int AddToMonitor(int pollFd, struct epoll_event pollEvents[]);
78      std::map<int, std::shared_ptr<DeviceNode>> devs_;
79      std::vector<std::shared_ptr<EventReceiver>> receivers_;
80      bool isStart_;
81  };
82  } // namespace HiviewDFX
83  } // namespace OHOS
84  #endif // COER_EVENT_SERVER_H