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; 30 int32_t index; 31 bool isHolding; 32 struct timeval timeout; 33 HRilEventCallback func; 34 std::shared_ptr<void> param; 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 51 private: 52 void GetNowTime(struct timeval &tv); 53 bool GetNextTimeOut(struct timeval &tv); 54 void ProcessTimerList(); 55 void ProcessPendingList(); 56 void EraseListenEvent(HRilEventMessage &eventMsg, int32_t index); 57 void ProcessEvents(fd_set *rfds, int32_t number); 58 59 std::mutex listLock_; 60 std::list<HRilEventMessage> timerList_; 61 std::list<HRilEventMessage> pendingList_; 62 std::vector<HRilEventMessage *> listenEventTable_; 63 fd_set readFds_; 64 int32_t nfds_; 65 const int32_t DEFAULT_INDEX = -1; 66 const int32_t LISTEN_FD_EVENTS_MAX = 8; 67 }; 68 } // namespace Telephony 69 } // namespace OHOS 70 #endif // OHOS_HRIL_EVENT_H