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_HANDLER_H 17 #define OHOS_HANDLER_H 18 #include "internal_message.h" 19 #ifdef OHOS_ARCH_LITE 20 #include <pthread.h> 21 #include "message_queue.h" 22 #else 23 #include "wifi_event_handler.h" 24 #endif 25 namespace OHOS { 26 namespace Wifi { 27 const int USEC_1000 = 1000; 28 29 class Handler { 30 public: 31 /** 32 * @Description : Construct a new Handler:: Handler object. 33 * 34 */ 35 Handler(); 36 37 /** 38 * @Description : Destroy the Handler:: Handler object. 39 * 40 */ 41 virtual ~Handler(); 42 43 /** 44 * @Description : Initialize Handler 45 * 46 * @return true : Initialize Handler success, false: Initialize Handler failed. 47 */ 48 bool InitialHandler(const std::string &name); 49 50 /** 51 * @Description :Stop the thread for obtaining messages. 52 * 53 */ 54 void StopHandlerThread(); 55 56 /** 57 * @Description : Send a message and place the message in the message queue. 58 * 59 * @param msg - Message to be sent.[in] 60 */ 61 void SendMessage(InternalMessagePtr msg); 62 63 /** 64 * @Description : Send a message, place the message in the message queue, and 65 process the message after delayTimeMs is delayed. 66 * 67 * @param msg - Message to be sent.[in] 68 * @param delayTimeMs - Delay Time.[in] 69 */ 70 void MessageExecutedLater(InternalMessagePtr msg, int64_t delayTimeMs); 71 72 /** 73 * @Description : Send a message, place the message in the message queue, and 74 process the message at the execTime time point. 75 * 76 * @param msg - Message to be sent.[in] 77 * @param execTime - Time when a message is processed.[in] 78 */ 79 void MessageExecutedAtTime(InternalMessagePtr msg, int64_t execTime); 80 81 /** 82 * @Description : Send a message and place the message at the top of the message queue. 83 * 84 * @param msg - Message to be sent.[in] 85 */ 86 void PlaceMessageTopOfQueue(InternalMessagePtr msg); 87 88 /** 89 * @Description : Delete messages from the queue. 90 * 91 * @param messageName - Name of the message to be deleted.[in] 92 */ 93 void DeleteMessageFromQueue(int messageName); 94 95 /** 96 * @Description : Invoke the ExecuteStateMsg interface of the current state 97 to process messages sent to the state machine. The entry/exit 98 of the state machine is also called, and the delayed message 99 is put back into queue when transitioning to a new state. 100 * 101 * @param msg - Messages.[in] 102 */ 103 virtual void ExecuteMessage(InternalMessagePtr msg) = 0; 104 private: 105 #ifdef OHOS_ARCH_LITE 106 /** 107 * @Description : Thread processing function 108 * 109 * @param pInstance - Handler Instance pointer.[in] 110 */ 111 static void *RunHandleThreadFunc(void *pInstance); 112 113 /** 114 * @Description : Distributing Messages. 115 * 116 * @param msg - Messages to be processed.[in] 117 */ 118 void DistributeMessage(InternalMessagePtr msg); 119 120 /** 121 * @Description : Obtains messages from the message queue, distributes the 122 messages, and recycles the messages. 123 * 124 */ 125 void GetAndDistributeMessage(); 126 /* message queue. */ 127 std::unique_ptr<MessageQueue> pMyQueue; 128 /* Thread handle. */ 129 pthread_t handleThread; 130 131 /* Running flag. */ 132 bool isRunning; 133 #else 134 /* task queue. */ 135 std::unique_ptr<WifiEventHandler> pMyTaskQueue; 136 #endif 137 std::string mThreadName = ""; 138 }; 139 } // namespace Wifi 140 } // namespace OHOS 141 #endif