Netd unsolicited event porting

Use another buffer to log unsolicited event.

Test: built, flashed, booted
Test: 1. manual test datacall/wifi work
      2. manual test tethering work
      3. run cts StrictModeTest pass
      4. manual test data alert work
      5. manual idletimer work

Change-Id: I1e4ed360b0c9d60c14bc7f0ffdf578fc557d3643
diff --git a/server/EventReporter.cpp b/server/EventReporter.cpp
index 7ada0bf..c9d966a 100644
--- a/server/EventReporter.cpp
+++ b/server/EventReporter.cpp
@@ -17,10 +17,11 @@
 #include "EventReporter.h"
 
 using android::interface_cast;
+using android::net::INetdUnsolicitedEventListener;
 using android::net::metrics::INetdEventListener;
 
 android::sp<INetdEventListener> EventReporter::getNetdEventListener() {
-    std::lock_guard lock(mutex);
+    std::lock_guard lock(mEventMutex);
     if (mNetdEventListener == nullptr) {
         // Use checkService instead of getService because getService waits for 5 seconds for the
         // service to become available. The DNS resolver inside netd is started much earlier in the
@@ -28,9 +29,7 @@
         // for 5 seconds until the DNS listener starts up.
         android::sp<android::IBinder> b = android::defaultServiceManager()->checkService(
                 android::String16("netd_listener"));
-        if (b != nullptr) {
-            mNetdEventListener = interface_cast<INetdEventListener>(b);
-        }
+        mNetdEventListener = interface_cast<INetdEventListener>(b);
     }
     // If the netd listener service is dead, the binder call will just return an error, which should
     // be fine because the only impact is that we can't log netd events. In any case, this should
@@ -38,3 +37,37 @@
     // with it.
     return mNetdEventListener;
 }
+
+std::map<pid_t, const android::sp<INetdUnsolicitedEventListener>>
+EventReporter::getNetdUnsolicitedEventListenerVec() {
+    std::lock_guard lock(mUnsolicitedMutex);
+    return mNetdUnsolicitedEventListenerMap;
+}
+
+void EventReporter::registerUnsolEventListener(
+        pid_t pid, const android::sp<INetdUnsolicitedEventListener>& listener) {
+    std::lock_guard lock(mUnsolicitedMutex);
+    if (mNetdUnsolicitedEventListenerMap.find(pid) != mNetdUnsolicitedEventListenerMap.end()) {
+        return;
+    }
+    mNetdUnsolicitedEventListenerMap.insert({pid, listener});
+
+    // Create the death listener.
+    class DeathRecipient : public android::IBinder::DeathRecipient {
+      public:
+        DeathRecipient(UnsolListenerMap* map, pid_t pid, std::mutex& unsolMutex)
+            : mNetdUnsolicitedEventListenerMap(map), mPid(pid), mMutex(unsolMutex) {}
+
+      private:
+        void binderDied(const android::wp<android::IBinder>& /* who */) override {
+            std::lock_guard lock(mMutex);
+            mNetdUnsolicitedEventListenerMap->erase(mPid);
+        }
+        UnsolListenerMap* mNetdUnsolicitedEventListenerMap;
+        pid_t mPid;
+        std::mutex& mMutex;
+    };
+    android::sp<android::IBinder::DeathRecipient> deathRecipient =
+            new DeathRecipient(&mNetdUnsolicitedEventListenerMap, pid, mUnsolicitedMutex);
+    android::IInterface::asBinder(listener)->linkToDeath(deathRecipient);
+}
\ No newline at end of file