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_INTERNAL_MESSAGE_H 17 #define OHOS_INTERNAL_MESSAGE_H 18 19 #include <cstring> 20 #include <list> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <any> 25 26 namespace OHOS { 27 namespace Wifi { 28 const int MAX_POOL_SIZE_INIT = 50; 29 class MessageBody { 30 public: 31 /** 32 * @Description : Save an Integer Data. 33 * 34 * @param data - Integer Data.[in] 35 */ 36 void SaveIntData(int data); 37 38 /** 39 * @Description : Save a String Data. 40 * 41 * @param data - String Data.[in] 42 */ 43 void SaveStringData(std::string data); 44 45 /** 46 * @Description : Get an Integer Data. 47 * 48 * @return int 49 */ 50 int GetIntData(); 51 52 /** 53 * @Description : Get a String Data. 54 * 55 * @return std::string 56 */ 57 std::string GetStringData(); 58 59 /** 60 * @Description : Clear all Data. 61 * 62 */ 63 void ClearAllData(); 64 65 /** 66 * @Description : Copy a message body. 67 * 68 * @param origBody - Source Message Body.[in] 69 */ 70 void CopyMessageBody(const MessageBody &origBody); 71 72 private: 73 /* Integer data. */ 74 std::list<int> intArray_; 75 /* String data. */ 76 std::list<std::string> stringArray_; 77 }; 78 79 class InternalMessage { 80 public: 81 /** 82 * @Description : Construct a new Internal Message object. 83 * 84 */ 85 InternalMessage(); 86 87 /** 88 * @Description Destroy the Internal Message object. 89 * 90 */ 91 ~InternalMessage(); 92 93 /** 94 * @Description : Get message name. 95 * 96 * @return int 97 */ 98 int GetMessageName() const; 99 100 /** 101 * @Description : Obtains the first parameter in the message body. 102 * 103 * @return int 104 */ 105 int GetParam1() const; 106 107 /** 108 * @Description : Obtains the second parameter in the message body. 109 * 110 * @return int 111 */ 112 int GetParam2() const; 113 114 /** 115 * @Description : Obtains Integer data from message. 116 * 117 * @return int 118 */ 119 int GetIntFromMessage(); 120 121 /** 122 * @Description : Obtains String data from message. 123 * 124 * @return std::string 125 */ 126 std::string GetStringFromMessage(); 127 128 /** 129 * @Description : Obtains message body. 130 * 131 * @return MessageBody& 132 */ 133 const MessageBody &GetMessageBody() const; 134 135 /** 136 * @Description : Copy message body. 137 * 138 * @param origBody - Source Message Body.[in] 139 */ 140 void CopyMessageBody(const MessageBody &origBody); 141 142 /** 143 * @Description : Get next message. 144 * 145 * @return InternalMessagePtr 146 */ 147 std::shared_ptr<InternalMessage> GetNextMsg() const; 148 149 /** 150 * @Description : Obtains time. 151 * 152 * @return int64_t 153 */ 154 int64_t GetHandleTime() const; 155 156 /** 157 * @Description : Set message name. 158 * 159 * @param msgName - Message name.[in] 160 */ 161 void SetMessageName(int msgName); 162 163 /** 164 * @Description : Set the first parameter in the message body. 165 * 166 * @param param1 - The first parameter.[in] 167 */ 168 void SetParam1(int param1); 169 170 /** 171 * @Description : Set the second parameter in the message body. 172 * 173 * @param param2 - The second parameter.[in] 174 */ 175 void SetParam2(int param2); 176 177 /** 178 * @DescriptionSet the Message Obj object - brief 179 * @tparam - T Custom type to be set 180 * @param messageObj - User-defined data to be set 181 */ 182 template<typename T> SetMessageObj(const T & messageObj)183 void SetMessageObj(const T &messageObj) 184 { 185 mMessageObj = messageObj; 186 } 187 188 /** 189 * @DescriptionSet the Message Obj object - brief 190 * @tparam - T Custom type to be set 191 * @param messageObj - User-defined data to be set 192 */ 193 template<typename T> SetMessageObj(T && messageObj)194 void SetMessageObj(T &&messageObj) 195 { 196 mMessageObj = T(messageObj); 197 } 198 SetMessageObj(const std::any & messageObj)199 void SetMessageObj(const std::any &messageObj) 200 { 201 mMessageObj = messageObj; 202 } 203 204 /** 205 * @DescriptionGet the Message Obj object 206 * @tparam - T Custom type to be set 207 * @param messageObj - Gets data of an actual specific object. 208 * @return - bool true:success false:failed 209 */ 210 template<typename T> GetMessageObj(T & messageObj)211 bool GetMessageObj(T &messageObj) const 212 { 213 messageObj = std::any_cast<const T &>(mMessageObj); 214 return true; 215 } 216 GetMessageObj(void)217 const std::any &GetMessageObj(void) const 218 { 219 return mMessageObj; 220 } 221 222 /** 223 * @Description : Release Message Object. 224 * 225 */ 226 void ReleaseMessageObj(); 227 228 /** 229 * @Description : Add integer message body. 230 * 231 * @param data - Integer data.[in] 232 */ 233 void AddIntMessageBody(int data); 234 235 /** 236 * @Description : Add string message body. 237 * 238 * @param data - String data.[in] 239 */ 240 void AddStringMessageBody(std::string data); 241 242 /** 243 * @Description : Clear message body. 244 * 245 */ 246 void ClearMessageBody(); 247 248 /** 249 * @Description : Sets next message. 250 * 251 * @param next - The next message.[in] 252 */ 253 void SetNextMsg(std::shared_ptr<InternalMessage> nextMsg); 254 255 /** 256 * @Description : Set the time. 257 * 258 * @param time - Time.[in] 259 */ 260 void SetHandleTime(int64_t time); 261 262 private: 263 /* Message Name */ 264 int mMsgName; 265 /* Parameter 1 */ 266 int mParam1; 267 /* Parameter 2 */ 268 int mParam2; 269 /* any message obj. */ 270 std::any mMessageObj; 271 /* Message bodies that cannot be directly copied */ 272 MessageBody mMessageBody; 273 /* Next message in the resource pool or message queue */ 274 std::shared_ptr<InternalMessage> pNextMsg; 275 /* Message execution time */ 276 int64_t mHandleTime; 277 }; 278 using InternalMessagePtr = std::shared_ptr<InternalMessage>; 279 class MessageManage { 280 public: 281 /** 282 * @Description : Obtains a single instance. 283 * 284 * @return MessageManage& 285 */ 286 static MessageManage &GetInstance(); 287 288 /** 289 * @Description : Message obtaining function. 290 * 291 * @return InternalMessagePtr 292 */ 293 InternalMessagePtr CreateMessage(); 294 295 /** 296 * @Description : Obtain original messages. 297 * 298 * @param orig - Original messages.[in] 299 * @return InternalMessagePtr 300 */ 301 InternalMessagePtr CreateMessage(const InternalMessagePtr orig); 302 303 /** 304 * @Description : Obtains the message name. 305 * 306 * @param messageName - Message name.[in] 307 * @return InternalMessagePtr 308 */ 309 InternalMessagePtr CreateMessage(int messageName); 310 311 /** 312 * @Description :Obtaining Message Information. 313 * 314 * @param messageName - Message name.[in] 315 * @param messageObj - Message pointer.[in] 316 * @return InternalMessagePtr 317 */ 318 InternalMessagePtr CreateMessage(int messageName, const std::any &messageObj); 319 320 /** 321 * @Description : Obtaining Message Information. 322 * 323 * @param messageName - Message name.[in] 324 * @param param1 - param1.[in] 325 * @param param2 - param2.[in] 326 * @return InternalMessagePtr 327 */ 328 InternalMessagePtr CreateMessage(int messageName, int param1, int param2); 329 330 /** 331 * @Description : Obtaining Message Information. 332 * 333 * @param messageName - Message name.[in] 334 * @param param1 - param1.[in] 335 * @param param2 - param2.[in] 336 * @param messageObj - Message pointer.[in] 337 * @return InternalMessagePtr 338 */ 339 InternalMessagePtr CreateMessage(int messageName, int param1, int param2, const std::any &messageObj); 340 341 /** 342 * @Description :Recycle message. 343 * 344 * @param m - message.[in] 345 */ 346 void ReclaimMsg(InternalMessagePtr m); 347 348 /** 349 * @Description : Construct a new Message Manage object. 350 * 351 */ 352 MessageManage(); 353 354 /** 355 * @Description : Destroy the Message Manage object. 356 * 357 */ 358 ~MessageManage(); 359 360 private: 361 static std::unique_ptr<MessageManage> msgManage; 362 }; 363 } // namespace Wifi 364 } // namespace OHOS 365 #endif 366