Proto message for "dumpsys sensorservice --proto"

Add SensorServiceProto for protobuf dumpsys of sensorservice. Primarily
used by incident service to capture an incident report proto.
Also add a gensrc rule to generate cpp headers of frameworks proto to
be used by native services like sensorservice. Added "static" qualifier
to a few generated fields in the header to avoid linker conflict.

Command to invoke (any of the following after lunch and env setup):
$ adb shell dumpsys sensorservice --proto
$ adb shell incident 3053
$ incident_report 3053

Bug: 146086105
Test: Execute the above commands and compare the output against dumpsys
      sensorservice

Change-Id: I282ff37bd3e845464d0693d69fc0d82943cc9855
diff --git a/Android.bp b/Android.bp
index c899179..8705fca 100644
--- a/Android.bp
+++ b/Android.bp
@@ -656,6 +656,33 @@
     output_extension: "srcjar",
 }
 
+gensrcs {
+    name: "framework-cppstream-protos",
+    depfile: true,
+
+    tools: [
+        "aprotoc",
+        "protoc-gen-cppstream",
+    ],
+
+    cmd: "mkdir -p $(genDir) " +
+        "&& $(location aprotoc) " +
+        "  --plugin=$(location protoc-gen-cppstream) " +
+        "  --dependency_out=$(depfile) " +
+        "  --cppstream_out=$(genDir) " +
+        "  -Iexternal/protobuf/src " +
+        "  -I . " +
+        "  $(in)",
+
+    srcs: [
+        ":ipconnectivity-proto-src",
+        "core/proto/**/*.proto",
+        "libs/incident/**/*.proto",
+    ],
+
+    output_extension: "proto.h",
+}
+
 filegroup {
     name: "framework-annotations",
     srcs: [
@@ -1013,43 +1040,6 @@
     },
 }
 
-gensrcs {
-    name: "gen-platform-proto-constants",
-    depfile: true,
-
-    tools: [
-        "aprotoc",
-        "protoc-gen-cppstream",
-    ],
-
-    srcs: [
-        "core/proto/android/os/backtrace.proto",
-        "core/proto/android/os/batterytype.proto",
-        "core/proto/android/os/cpufreq.proto",
-        "core/proto/android/os/cpuinfo.proto",
-        "core/proto/android/os/data.proto",
-        "core/proto/android/os/kernelwake.proto",
-        "core/proto/android/os/pagetypeinfo.proto",
-        "core/proto/android/os/procrank.proto",
-        "core/proto/android/os/ps.proto",
-        "core/proto/android/os/system_properties.proto",
-        "core/proto/android/util/event_log_tags.proto",
-        "core/proto/android/util/log.proto",
-    ],
-
-    // Append protoc-gen-cppstream tool's PATH otherwise aprotoc can't find the plugin tool
-    cmd: "mkdir -p $(genDir) " +
-        "&& $(location aprotoc) " +
-        "  --plugin=$(location protoc-gen-cppstream) " +
-        "  --dependency_out=$(depfile) " +
-        "  --cppstream_out=$(genDir) " +
-        "  -Iexternal/protobuf/src " +
-        "  -I . " +
-        "  $(in)",
-
-    output_extension: "proto.h",
-}
-
 
 subdirs = [
     "cmds/*",
diff --git a/cmds/incident_helper/Android.bp b/cmds/incident_helper/Android.bp
index 64f4c66..f07743e 100644
--- a/cmds/incident_helper/Android.bp
+++ b/cmds/incident_helper/Android.bp
@@ -44,7 +44,7 @@
         "src/ih_util.cpp",
     ],
 
-    generated_headers: ["gen-platform-proto-constants"],
+    generated_headers: ["framework-cppstream-protos"],
 
     shared_libs: [
         "libbase",
diff --git a/cmds/incidentd/Android.bp b/cmds/incidentd/Android.bp
index 25e0328..55bfc32 100644
--- a/cmds/incidentd/Android.bp
+++ b/cmds/incidentd/Android.bp
@@ -43,7 +43,7 @@
     ],
 
     local_include_dirs: ["src"],
-    generated_headers: ["gen-platform-proto-constants"],
+    generated_headers: ["framework-cppstream-protos"],
 
     proto: {
         type: "lite",
@@ -98,7 +98,7 @@
     ],
 
     local_include_dirs: ["src"],
-    generated_headers: ["gen-platform-proto-constants"],
+    generated_headers: ["framework-cppstream-protos"],
 
     srcs: [
         "tests/**/*.cpp",
diff --git a/core/proto/android/os/incident.proto b/core/proto/android/os/incident.proto
index 8adcc9e..bf4cdee 100644
--- a/core/proto/android/os/incident.proto
+++ b/core/proto/android/os/incident.proto
@@ -52,6 +52,7 @@
 import "frameworks/base/core/proto/android/service/print.proto";
 import "frameworks/base/core/proto/android/service/procstats.proto";
 import "frameworks/base/core/proto/android/service/restricted_image.proto";
+import "frameworks/base/core/proto/android/service/sensor_service.proto";
 import "frameworks/base/core/proto/android/service/usb.proto";
 import "frameworks/base/core/proto/android/util/event_log_tags.proto";
 import "frameworks/base/core/proto/android/util/log.proto";
@@ -492,6 +493,11 @@
         (section).args = "contexthub --proto"
     ];
 
+    optional android.service.SensorServiceProto sensor_service = 3053 [
+        (section).type = SECTION_DUMPSYS,
+        (section).args = "sensorservice --proto"
+    ];
+
     // Reserved for OEMs.
     extensions 50000 to 100000;
 }
diff --git a/core/proto/android/server/activitymanagerservice.proto b/core/proto/android/server/activitymanagerservice.proto
index 0a2fd70..2d2ead4 100644
--- a/core/proto/android/server/activitymanagerservice.proto
+++ b/core/proto/android/server/activitymanagerservice.proto
@@ -27,7 +27,6 @@
 import "frameworks/base/core/proto/android/content/configuration.proto";
 import "frameworks/base/core/proto/android/content/intent.proto";
 import "frameworks/base/core/proto/android/content/package_item_info.proto";
-import "frameworks/base/core/proto/android/graphics/rect.proto";
 import "frameworks/base/core/proto/android/internal/processstats.proto";
 import "frameworks/base/core/proto/android/os/bundle.proto";
 import "frameworks/base/core/proto/android/os/looper.proto";
diff --git a/core/proto/android/server/notificationhistory.proto b/core/proto/android/server/notificationhistory.proto
index 1e6ee3f..6749719 100644
--- a/core/proto/android/server/notificationhistory.proto
+++ b/core/proto/android/server/notificationhistory.proto
@@ -17,8 +17,6 @@
 syntax = "proto2";
 package com.android.server.notification;
 
-import "frameworks/base/core/proto/android/server/enums.proto";
-
 option java_multiple_files = true;
 
 // On disk data store for historical notifications
diff --git a/core/proto/android/service/sensor_service.proto b/core/proto/android/service/sensor_service.proto
new file mode 100644
index 0000000..8598f86
--- /dev/null
+++ b/core/proto/android/service/sensor_service.proto
@@ -0,0 +1,246 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+syntax = "proto2";
+package android.service;
+
+import "frameworks/base/core/proto/android/privacy.proto";
+
+option java_multiple_files = true;
+
+/*
+ * Notes:
+ * 1. When using ProtoOutputStream to write this proto message, must call
+ *    token = ProtoOutputStream#start(fieldId) before and ProtoOutputStream#end(token) after
+ *    writing a nested message.
+ * 2. Never reuse a proto field number. When removing a field, mark it as reserved.
+ */
+
+// Proto dump of android::SensorService. dumpsys sensorservice --proto
+message SensorServiceProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    enum OperatingModeEnum {
+        OP_MODE_UNKNOWN = 0;
+        OP_MODE_NORMAL = 1;
+        OP_MODE_RESTRICTED = 2;
+        OP_MODE_DATA_INJECTION = 3;
+    }
+
+    optional int64 current_time_ms = 1;
+    optional SensorDeviceProto sensor_device = 2;
+    optional SensorListProto sensors = 3;
+    optional SensorFusionProto fusion_state = 4;
+    optional SensorEventsProto sensor_events = 5;
+    repeated ActiveSensorProto active_sensors = 6;
+    optional int32 socket_buffer_size = 7;
+    optional int32 socket_buffer_size_in_events = 8;
+    optional bool wake_lock_acquired = 9;
+    optional OperatingModeEnum operating_mode = 10;
+    // Non-empty only if operating_mode is RESTRICTED or DATA_INJECTION.
+    optional string whitelisted_package = 11;
+    optional bool sensor_privacy = 12;
+    repeated SensorEventConnectionProto active_connections = 13;
+    repeated SensorDirectConnectionProto direct_connections = 14;
+    repeated SensorRegistrationInfoProto previous_registrations = 15;
+}
+
+// Proto dump of android::SensorDevice
+message SensorDeviceProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    optional bool initialized = 1;
+    optional int32 total_sensors = 2;
+    optional int32 active_sensors = 3;
+
+    message SensorProto {
+        option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+        optional int32 handle = 1;
+        optional int32 active_count = 2;
+        repeated float sampling_period_ms = 3;
+        optional float sampling_period_selected = 4;
+        repeated float batching_period_ms = 5;
+        optional float batching_period_selected = 6;
+    }
+    repeated SensorProto sensors = 4;
+}
+
+// Proto dump of android::SensorServiceUtil::SensorList
+message SensorListProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    enum ReportingModeEnum {
+        RM_UNKNOWN = 0;
+        RM_CONTINUOUS = 1;
+        RM_ON_CHANGE = 2;
+        RM_ONE_SHOT = 3;
+        RM_SPECIAL_TRIGGER = 4;
+    }
+
+    message SensorProto {
+        option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+        optional int32 handle = 1;
+        optional string name = 2;
+        optional string vendor = 3;
+        optional int32 version = 4;
+        optional string string_type = 5;
+        optional int32 type = 6;
+        optional string required_permission = 7;
+        optional int32 flags = 8;
+        optional ReportingModeEnum reporting_mode = 9;
+        optional int32 max_delay_us = 10;
+        optional int32 min_delay_us = 11;
+        optional int32 fifo_max_event_count = 12;
+        optional int32 fifo_reserved_event_count = 13;
+        optional bool is_wakeup = 14;
+        optional bool data_injection_supported = 15;
+        optional bool is_dynamic = 16;
+        optional bool has_additional_info = 17;
+        optional int32 highest_rate_level = 18;
+        optional bool ashmem = 19;
+        optional bool gralloc = 20;
+        optional float min_value = 21;
+        optional float max_value = 22;
+        optional float resolution = 23;
+        optional float power_usage = 24;
+    }
+    repeated SensorProto sensors = 1;
+}
+
+
+// Proto dump of android::SensorFusion
+message SensorFusionProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    message FusionProto {
+        option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+        optional bool enabled = 1;
+        optional int32 num_clients = 2;
+        optional float estimated_gyro_rate = 3;
+        optional float attitude_x = 4;
+        optional float attitude_y = 5;
+        optional float attitude_z = 6;
+        optional float attitude_w = 7;
+        optional float attitude_length = 8;
+        optional float bias_x = 9;
+        optional float bias_y = 10;
+        optional float bias_z = 11;
+    }
+    optional FusionProto fusion_9axis = 1;
+    optional FusionProto fusion_nomag = 2;
+    optional FusionProto fusion_nogyro = 3;
+}
+
+// Proto dump of android::SensorServiceUtil::RecentEventLogger
+message SensorEventsProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    message Event {
+        option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+        optional float timestamp_sec = 1;
+        optional int64 wall_timestamp_ms = 2;
+        optional bool masked = 3;
+        optional int64 int64_data = 4;
+        repeated float float_array = 5;
+    }
+
+    message RecentEventsLog {
+        option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+        optional string name = 1;
+        optional int32 recent_events_count = 2;
+        repeated Event events = 3;
+    }
+    repeated RecentEventsLog recent_events_logs = 1;
+}
+
+message ActiveSensorProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    optional string name = 1;
+    optional int32 handle = 2;
+    optional int32 num_connections = 3;
+}
+
+// Proto dump of android::SensorService::SensorDirectConnection
+message SensorDirectConnectionProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    message SensorProto {
+        option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+        optional int32 sensor = 1;
+        optional int32 rate = 2;
+    }
+
+    optional string package_name = 1;
+    optional int32 hal_channel_handle = 2;
+    optional int32 num_sensor_activated = 3;
+    repeated SensorProto sensors = 4;
+}
+
+// Proto dump of android::SensorService::SensorEventConnection
+message SensorEventConnectionProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    message FlushInfoProto {
+        option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+        optional string sensor_name = 1;
+        optional int32 sensor_handle = 2;
+        optional bool first_flush_pending = 3;
+        optional int32 pending_flush_events_to_send = 4;
+    }
+
+    enum OperatingModeEnum {
+        OP_MODE_UNKNOWN = 0;
+        OP_MODE_NORMAL = 1;
+        OP_MODE_RESTRICTED = 2;
+        OP_MODE_DATA_INJECTION = 3;
+    }
+
+    optional OperatingModeEnum operating_mode = 1;
+    optional string package_name = 2;
+    optional int32 wake_lock_ref_count = 3;
+    optional int32 uid = 4;
+    optional int32 cache_size = 5;
+    optional int32 max_cache_size = 6;
+    repeated FlushInfoProto flush_infos = 7;
+    optional int32 events_received = 8;
+    optional int32 events_sent = 9;
+    optional int32 events_cache = 10;
+    optional int32 events_dropped = 11;
+    optional int32 total_acks_needed = 12;
+    optional int32 total_acks_received = 13;
+}
+
+// Proto dump of android::SensorService::SensorRegistrationInfo
+message SensorRegistrationInfoProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    optional int64 timestamp_sec = 1;
+    optional int32 sensor_handle = 2;
+    optional string package_name = 3;
+    optional int32 pid = 4;
+    optional int32 uid = 5;
+    optional int64 sampling_rate_us = 6;
+    optional int64 max_report_latency_us = 7;
+    optional bool activated = 8;
+}
diff --git a/tools/streaming_proto/cpp/main.cpp b/tools/streaming_proto/cpp/main.cpp
index d6b9d81..fe9a438 100644
--- a/tools/streaming_proto/cpp/main.cpp
+++ b/tools/streaming_proto/cpp/main.cpp
@@ -33,13 +33,13 @@
     if (GENERATE_MAPPING) {
         string name = make_constant_name(enu.name());
         string prefix = name + "_";
-        text << indent << "const int _ENUM_" << name << "_COUNT = " << N << ";" << endl;
-        text << indent << "const char* _ENUM_" << name << "_NAMES[" << N << "] = {" << endl;
+        text << indent << "static const int _ENUM_" << name << "_COUNT = " << N << ";" << endl;
+        text << indent << "static const char* _ENUM_" << name << "_NAMES[" << N << "] = {" << endl;
         for (int i=0; i<N; i++) {
             text << indent << INDENT << "\"" << stripPrefix(enu.value(i).name(), prefix) << "\"," << endl;
         }
         text << indent << "};" << endl;
-        text << indent << "const int _ENUM_" << name << "_VALUES[" << N << "] = {" << endl;
+        text << indent << "static const int _ENUM_" << name << "_VALUES[" << N << "] = {" << endl;
         for (int i=0; i<N; i++) {
             text << indent << INDENT << make_constant_name(enu.value(i).name()) << "," << endl;
         }
@@ -102,13 +102,13 @@
 
     if (GENERATE_MAPPING) {
         N = message.field_size();
-        text << indented << "const int _FIELD_COUNT = " << N << ";" << endl;
-        text << indented << "const char* _FIELD_NAMES[" << N << "] = {" << endl;
+        text << indented << "static const int _FIELD_COUNT = " << N << ";" << endl;
+        text << indented << "static const char* _FIELD_NAMES[" << N << "] = {" << endl;
         for (int i=0; i<N; i++) {
             text << indented << INDENT << "\"" << message.field(i).name() << "\"," << endl;
         }
         text << indented << "};" << endl;
-        text << indented << "const uint64_t _FIELD_IDS[" << N << "] = {" << endl;
+        text << indented << "static const uint64_t _FIELD_IDS[" << N << "] = {" << endl;
         for (int i=0; i<N; i++) {
             text << indented << INDENT << make_constant_name(message.field(i).name()) << "," << endl;
         }
@@ -152,7 +152,7 @@
         write_message(text, file_descriptor.message_type(i), "");
     }
 
-    for (vector<string>::iterator it = namespaces.begin(); it != namespaces.end(); it++) {
+    for (vector<string>::reverse_iterator it = namespaces.rbegin(); it != namespaces.rend(); it++) {
         text << "} // " << *it << endl;
     }