• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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(const std::string & socketName,uint8_t eventCountPerCycle)32     SocketDevice(const std::string& socketName, uint8_t eventCountPerCycle)
33         : socketName_(socketName), eventCountPerCycle_(eventCountPerCycle) {};
~SocketDevice()34     virtual ~SocketDevice() {};
35     int Close() override;
36     int Open() override;
37     uint32_t GetEvents() override;
38     std::string GetName() override;
39     int ReceiveMsg(std::vector<std::shared_ptr<EventReceiver>> &receivers) override;
40     bool IsValidMsg(char* msg, int32_t len) override;
41 
42 private:
43     void InitSocket(int &socketId);
44     void SetUCredPid(const pid_t pid);
45 
46 private:
47     int socketId_ = UN_INIT_INT_TYPE_VAL;
48     pid_t uCredPid_ = UN_INIT_INT_TYPE_VAL;
49     std::string socketName_;
50     uint8_t eventCountPerCycle_;
51 };
52 
53 class BBoxDevice : public DeviceNode {
54 public:
BBoxDevice()55     BBoxDevice() {};
~BBoxDevice()56     virtual ~BBoxDevice() {};
57     int Close() override;
58     int Open() override;
59     uint32_t GetEvents() override;
60     std::string GetName() override;
61     int ReceiveMsg(std::vector<std::shared_ptr<EventReceiver>> &receivers) override;
62     bool IsValidMsg(char* msg, int32_t len) override;
63 
64 private:
65     int fd_ = UN_INIT_INT_TYPE_VAL;
66     bool hasBbox_ = false;
67 };
68 
69 class EventServer {
70 public:
EventServer()71     EventServer(): isStart_(false) {};
~EventServer()72     ~EventServer() {}
73     void Start();
74     void Stop();
75     void AddReceiver(std::shared_ptr<EventReceiver> receiver);
76 private:
77     void AddDev(std::shared_ptr<DeviceNode> dev);
78     int OpenDevs();
79     void CloseDevs();
80     int AddToMonitor(int pollFd, struct epoll_event pollEvents[]);
81     std::map<int, std::shared_ptr<DeviceNode>> devs_;
82     std::vector<std::shared_ptr<EventReceiver>> receivers_;
83     bool isStart_;
84 };
85 } // namespace HiviewDFX
86 } // namespace OHOS
87 #endif // COER_EVENT_SERVER_H