1 /*
2  * Copyright (C) 2018 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 #define DEBUG false
17 #include "Log.h"
18 
19 #include "IncidentdReporter.h"
20 #include "frameworks/base/libs/incident/proto/android/os/header.pb.h"
21 
22 #include <android/os/IIncidentManager.h>
23 #include <android/os/IncidentReportArgs.h>
24 #include <binder/IBinder.h>
25 #include <binder/IServiceManager.h>
26 
27 namespace android {
28 namespace os {
29 namespace statsd {
30 
GenerateIncidentReport(const IncidentdDetails & config,const int64_t & rule_id,const ConfigKey & configKey)31 bool GenerateIncidentReport(const IncidentdDetails& config, const int64_t& rule_id,
32                             const ConfigKey& configKey) {
33     if (config.section_size() == 0) {
34         VLOG("The alert %lld contains zero section in config(%d,%lld)", (unsigned long long)rule_id,
35             configKey.GetUid(), (long long) configKey.GetId());
36         return false;
37     }
38 
39     IncidentReportArgs incidentReport;
40 
41     android::os::IncidentHeaderProto header;
42     header.set_alert_id(rule_id);
43     header.mutable_config_key()->set_uid(configKey.GetUid());
44     header.mutable_config_key()->set_id(configKey.GetId());
45     incidentReport.addHeader(header);
46 
47     for (int i = 0; i < config.section_size(); i++) {
48         incidentReport.addSection(config.section(i));
49     }
50 
51     uint8_t dest;
52     switch (config.dest()) {
53         case IncidentdDetails_Destination_AUTOMATIC:
54             dest = android::os::DEST_AUTOMATIC;
55             break;
56         case IncidentdDetails_Destination_EXPLICIT:
57             dest = android::os::DEST_EXPLICIT;
58             break;
59         default:
60             dest = android::os::DEST_AUTOMATIC;
61     }
62     incidentReport.setDest(dest);
63 
64     sp<IIncidentManager> service = interface_cast<IIncidentManager>(
65             defaultServiceManager()->getService(android::String16("incident")));
66     if (service == nullptr) {
67         ALOGW("Failed to fetch incident service.");
68         return false;
69     }
70     VLOG("Calling incidentd %p", service.get());
71     binder::Status s = service->reportIncident(incidentReport);
72     VLOG("Report incident status: %s", s.toString8().string());
73     return s.isOk();
74 }
75 
76 }  // namespace statsd
77 }  // namespace os
78 }  // namespace android
79