1 /**
2  * Copyright (c) 2019, 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 #define LOG_TAG "OemNetd"
18 
19 #include "OemNetdListener.h"
20 
21 namespace com {
22 namespace android {
23 namespace internal {
24 namespace net {
25 
getListener()26 ::android::sp<::android::IBinder> OemNetdListener::getListener() {
27     static OemNetdListener listener;
28     return listener.getIBinder();
29 }
30 
getIBinder()31 ::android::sp<::android::IBinder> OemNetdListener::getIBinder() {
32     std::lock_guard lock(mMutex);
33     if (mIBinder == nullptr) {
34         mIBinder = ::android::IInterface::asBinder(this);
35     }
36     return mIBinder;
37 }
38 
isAlive(bool * alive)39 ::android::binder::Status OemNetdListener::isAlive(bool* alive) {
40     *alive = true;
41     return ::android::binder::Status::ok();
42 }
43 
registerOemUnsolicitedEventListener(const::android::sp<IOemNetdUnsolicitedEventListener> & listener)44 ::android::binder::Status OemNetdListener::registerOemUnsolicitedEventListener(
45         const ::android::sp<IOemNetdUnsolicitedEventListener>& listener) {
46     registerOemUnsolicitedEventListenerInternal(listener);
47     listener->onRegistered();
48     return ::android::binder::Status::ok();
49 }
50 
registerOemUnsolicitedEventListenerInternal(const::android::sp<IOemNetdUnsolicitedEventListener> & listener)51 void OemNetdListener::registerOemUnsolicitedEventListenerInternal(
52         const ::android::sp<IOemNetdUnsolicitedEventListener>& listener) {
53     std::lock_guard lock(mOemUnsolicitedMutex);
54 
55     // Create the death listener.
56     class DeathRecipient : public ::android::IBinder::DeathRecipient {
57       public:
58         DeathRecipient(OemNetdListener* oemNetdListener,
59                        ::android::sp<IOemNetdUnsolicitedEventListener> listener)
60             : mOemNetdListener(oemNetdListener), mListener(std::move(listener)) {}
61         ~DeathRecipient() override = default;
62         void binderDied(const ::android::wp<::android::IBinder>& /* who */) override {
63             mOemNetdListener->unregisterOemUnsolicitedEventListenerInternal(mListener);
64         }
65 
66       private:
67         OemNetdListener* mOemNetdListener;
68         ::android::sp<IOemNetdUnsolicitedEventListener> mListener;
69     };
70     ::android::sp<::android::IBinder::DeathRecipient> deathRecipient =
71             new DeathRecipient(this, listener);
72 
73     ::android::IInterface::asBinder(listener)->linkToDeath(deathRecipient);
74 
75     mOemUnsolListenerMap.insert({listener, deathRecipient});
76 }
77 
unregisterOemUnsolicitedEventListenerInternal(const::android::sp<IOemNetdUnsolicitedEventListener> & listener)78 void OemNetdListener::unregisterOemUnsolicitedEventListenerInternal(
79         const ::android::sp<IOemNetdUnsolicitedEventListener>& listener) {
80     std::lock_guard lock(mOemUnsolicitedMutex);
81     mOemUnsolListenerMap.erase(listener);
82 }
83 
84 }  // namespace net
85 }  // namespace internal
86 }  // namespace android
87 }  // namespace com
88