1 /* 2 * Copyright (c) 2023 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 #ifndef P2P_BROADCAST_RECEIVER_H 16 #define P2P_BROADCAST_RECEIVER_H 17 18 #include <map> 19 #include <memory> 20 #include <string> 21 22 #include "kits/c/wifi_p2p.h" 23 24 #include "adapter/p2p_adapter.h" 25 26 namespace OHOS::SoftBus { 27 enum class BroadcastReceiverAction { 28 BROADCAST_RECEIVER_ACTION_INVALID = -1, 29 WIFI_P2P_STATE_CHANGED_ACTION = 0, 30 WIFI_P2P_CONNECTION_CHANGED_ACTION = 1, 31 BROADCAST_RECEIVER_ACTION_MAX, 32 }; 33 34 enum class ListenerPriority { 35 LISTENER_PRIORITY_LOW, 36 LISTENER_PRIORITY_MIDDLE, 37 LISTENER_PRIORITY_HIGH, 38 }; 39 40 struct BroadcastParam { 41 P2pState p2pState; 42 43 WifiP2pLinkedInfo p2pLinkInfo; 44 std::shared_ptr<P2pAdapter::WifiDirectP2pGroupInfo> groupInfo; 45 }; 46 typedef void (*BroadcastListener)(enum BroadcastReceiverAction action, const struct BroadcastParam ¶m); 47 48 struct ActionListener { 49 BroadcastListener listener; 50 std::string name; 51 enum ListenerPriority priority; 52 }; 53 54 class P2pBroadcast { 55 public: 56 static P2pBroadcast *GetInstance(); 57 int RegisterBroadcastListener(const BroadcastReceiverAction *actions, size_t size, const std::string &name, 58 ListenerPriority priority, BroadcastListener listener); 59 void DispatchWorkHandler(BroadcastReceiverAction action, const BroadcastParam ¶m); 60 61 private: 62 std::map<BroadcastReceiverAction, std::vector<ActionListener>> listenerMap_; 63 }; 64 } // namespace OHOS::SoftBus 65 #endif 66