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