Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef NETD_SERVER_EVENT_REPORTER_H |
| 18 | #define NETD_SERVER_EVENT_REPORTER_H |
| 19 | |
Luke Huang | 900f18e | 2019-01-31 16:50:17 +0800 | [diff] [blame^] | 20 | #include <map> |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 21 | #include <mutex> |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 22 | |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 23 | #include <android-base/thread_annotations.h> |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include "android/net/INetd.h" |
| 26 | #include "android/net/INetdUnsolicitedEventListener.h" |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 27 | #include "android/net/metrics/INetdEventListener.h" |
| 28 | |
| 29 | /* |
Bernie Innocenti | cd25764 | 2018-12-20 15:56:40 +0900 | [diff] [blame] | 30 | * This class can be used to get the event listener service. |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 31 | */ |
| 32 | class EventReporter { |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 33 | public: |
Luke Huang | 900f18e | 2019-01-31 16:50:17 +0800 | [diff] [blame^] | 34 | using UnsolListenerMap = |
| 35 | std::map<const android::sp<android::net::INetdUnsolicitedEventListener>, |
| 36 | const android::sp<android::IBinder::DeathRecipient>>; |
Bernie Innocenti | 9ee088d | 2019-02-01 17:14:32 +0900 | [diff] [blame] | 37 | |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 38 | // Returns the binder reference to the netd events listener service, attempting to fetch it if |
Michal Karpinski | 4b9b78a | 2016-10-06 19:33:55 +0100 | [diff] [blame] | 39 | // we do not have it already. This method is threadsafe. |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 40 | android::sp<android::net::metrics::INetdEventListener> getNetdEventListener(); |
Bernie Innocenti | 9ee088d | 2019-02-01 17:14:32 +0900 | [diff] [blame] | 41 | |
| 42 | // Returns a copy of the registered listeners. |
Luke Huang | 900f18e | 2019-01-31 16:50:17 +0800 | [diff] [blame^] | 43 | UnsolListenerMap getNetdUnsolicitedEventListenerMap() const EXCLUDES(mUnsolicitedMutex); |
Bernie Innocenti | 9ee088d | 2019-02-01 17:14:32 +0900 | [diff] [blame] | 44 | |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 45 | void registerUnsolEventListener( |
Bernie Innocenti | 9ee088d | 2019-02-01 17:14:32 +0900 | [diff] [blame] | 46 | const android::sp<android::net::INetdUnsolicitedEventListener>& listener) |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 47 | EXCLUDES(mUnsolicitedMutex); |
Luke Huang | 900f18e | 2019-01-31 16:50:17 +0800 | [diff] [blame^] | 48 | void unregisterUnsolEventListener( |
| 49 | const android::sp<android::net::INetdUnsolicitedEventListener>& listener) |
| 50 | EXCLUDES(mUnsolicitedMutex); |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 51 | |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 52 | private: |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 53 | std::mutex mEventMutex; |
Luke Huang | 900f18e | 2019-01-31 16:50:17 +0800 | [diff] [blame^] | 54 | mutable std::mutex mUnsolicitedMutex; |
Bernie Innocenti | 9ee088d | 2019-02-01 17:14:32 +0900 | [diff] [blame] | 55 | android::sp<android::net::metrics::INetdEventListener> mNetdEventListener |
| 56 | GUARDED_BY(mEventMutex); |
Luke Huang | 900f18e | 2019-01-31 16:50:17 +0800 | [diff] [blame^] | 57 | UnsolListenerMap mUnsolListenerMap GUARDED_BY(mUnsolicitedMutex); |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | #endif // NETD_SERVER_EVENT_REPORTER_H |