• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 OHOS_HRIL_EVENT_H
17 #define OHOS_HRIL_EVENT_H
18 
19 #include <functional>
20 #include <mutex>
21 #include <list>
22 #include <sys/time.h>
23 #include <vector>
24 
25 namespace OHOS {
26 namespace Telephony {
27 typedef std::function<void(int32_t, int16_t, std::shared_ptr<void>)> HRilEventCallback;
28 struct HRilEventMessage {
29     int32_t fd = 0;
30     int32_t index = 0;
31     bool isHolding = false;
32     struct timeval timeout;
33     HRilEventCallback func;
34     std::shared_ptr<void> param = nullptr;
35 };
36 
37 class HRilEvent {
38 public:
HRilEvent()39     HRilEvent() {};
40     virtual ~HRilEvent() = default;
41     const int32_t IVNALID_FD = -1;
42 
43     void TimerEventInit();
44     void AddTimerEvent(HRilEventMessage &eventMsg, const struct timeval &tv);
45     void SetTimerEvent(
46         HRilEventMessage &eventMsg, int32_t fd, bool isHolding, HRilEventCallback func, std::shared_ptr<void> param);
47     void AddEventMessage(const HRilEventMessage &eventMsg);
48     void RemoveEventMessage(HRilEventMessage &eventMsg);
49     void EventMessageLoop();
50     void SetNormalDestory(bool isDestory);
51     bool IsNormalDestory();
52 
53 private:
54     void GetNowTime(struct timeval &tv);
55     bool GetNextTimeOut(struct timeval &tv);
56     void ProcessTimerList();
57     void ProcessPendingList();
58     void EraseListenEvent(HRilEventMessage &eventMsg, int32_t index);
59     void ProcessEvents(fd_set *rfds, int32_t number);
60 
61     std::mutex listLock_;
62     std::list<HRilEventMessage> timerList_;
63     std::list<HRilEventMessage> pendingList_;
64     std::vector<HRilEventMessage *> listenEventTable_;
65     fd_set readFds_;
66     int32_t nfds_;
67     bool isNormalDestory = false;
68     const int32_t DEFAULT_INDEX = -1;
69     const int32_t LISTEN_FD_EVENTS_MAX = 8;
70     const int32_t TIME_UNIT = 1000;
71 };
72 } // namespace Telephony
73 } // namespace OHOS
74 #endif // OHOS_HRIL_EVENT_H