Adding EventReporter class
This class stores the reporting level and can be used to get
the event listener service.
Bug: 29748723
(cherry picked from commit be581e228bd1a97efd179edcdd3e744622fb99b8)
Change-Id: I98bfa97cb502f4d19ccf8c79f32d347e7e168492
diff --git a/server/EventReporter.cpp b/server/EventReporter.cpp
new file mode 100644
index 0000000..fd09b8a
--- /dev/null
+++ b/server/EventReporter.cpp
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "Netd"
+
+#include "EventReporter.h"
+#include "log/log.h"
+
+using android::interface_cast;
+using android::net::metrics::INetdEventListener;
+
+int EventReporter::setMetricsReportingLevel(const int level) {
+ if (level < INetdEventListener::REPORTING_LEVEL_NONE
+ || level > INetdEventListener::REPORTING_LEVEL_FULL) {
+ ALOGE("Invalid metrics reporting level %d", level);
+ return -EINVAL;
+ }
+ mReportingLevel = level;
+ return 0;
+}
+
+int EventReporter::getMetricsReportingLevel() const {
+ return mReportingLevel;
+}
+
+android::sp<INetdEventListener> EventReporter::getNetdEventListener() {
+ 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
+ // boot sequence than the framework DNS listener, and we don't want to delay all DNS lookups
+ // 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);
+ }
+ }
+ // 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
+ // only happen if the system server is going down, which means it will shortly be taking us down
+ // with it.
+ return mNetdEventListener;
+}