• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // Thread-safe initialization.
28     static ::android::sp<OemNetdListener> listener = ::android::sp<OemNetdListener>::make();
29     static ::android::sp<::android::IBinder> sIBinder = ::android::IInterface::asBinder(listener);
30     return sIBinder;
31 }
32 
isAlive(bool * alive)33 ::android::binder::Status OemNetdListener::isAlive(bool* alive) {
34     *alive = true;
35     return ::android::binder::Status::ok();
36 }
37 
registerOemUnsolicitedEventListener(const::android::sp<IOemNetdUnsolicitedEventListener> & listener)38 ::android::binder::Status OemNetdListener::registerOemUnsolicitedEventListener(
39         const ::android::sp<IOemNetdUnsolicitedEventListener>& listener) {
40     registerOemUnsolicitedEventListenerInternal(listener);
41     listener->onRegistered();
42     return ::android::binder::Status::ok();
43 }
44 
registerOemUnsolicitedEventListenerInternal(const::android::sp<IOemNetdUnsolicitedEventListener> & listener)45 void OemNetdListener::registerOemUnsolicitedEventListenerInternal(
46         const ::android::sp<IOemNetdUnsolicitedEventListener>& listener) {
47     std::lock_guard lock(mOemUnsolicitedMutex);
48 
49     // Create the death listener.
50     class DeathRecipient : public ::android::IBinder::DeathRecipient {
51       public:
52         DeathRecipient(OemNetdListener* oemNetdListener,
53                        ::android::sp<IOemNetdUnsolicitedEventListener> listener)
54             : mOemNetdListener(oemNetdListener), mListener(std::move(listener)) {}
55         ~DeathRecipient() override = default;
56         void binderDied(const ::android::wp<::android::IBinder>& /* who */) override {
57             mOemNetdListener->unregisterOemUnsolicitedEventListenerInternal(mListener);
58         }
59 
60       private:
61         OemNetdListener* mOemNetdListener;
62         ::android::sp<IOemNetdUnsolicitedEventListener> mListener;
63     };
64     ::android::sp<::android::IBinder::DeathRecipient> deathRecipient =
65             new DeathRecipient(this, listener);
66 
67     ::android::IInterface::asBinder(listener)->linkToDeath(deathRecipient);
68 
69     mOemUnsolListenerMap.insert({listener, deathRecipient});
70 }
71 
unregisterOemUnsolicitedEventListenerInternal(const::android::sp<IOemNetdUnsolicitedEventListener> & listener)72 void OemNetdListener::unregisterOemUnsolicitedEventListenerInternal(
73         const ::android::sp<IOemNetdUnsolicitedEventListener>& listener) {
74     std::lock_guard lock(mOemUnsolicitedMutex);
75     mOemUnsolListenerMap.erase(listener);
76 }
77 
78 }  // namespace net
79 }  // namespace internal
80 }  // namespace android
81 }  // namespace com
82