1 /*
2  * Copyright (C) 2017 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 DEBUG false
18 #include "Log.h"
19 
20 #include <android/os/IStatsCompanionService.h>
21 #include <binder/IPCThreadState.h>
22 #include <private/android_filesystem_config.h>
23 #include "../stats_log_util.h"
24 #include "../statscompanion_util.h"
25 #include "StatsCompanionServicePuller.h"
26 
27 using namespace android;
28 using namespace android::base;
29 using namespace android::binder;
30 using namespace android::os;
31 using std::make_shared;
32 using std::shared_ptr;
33 using std::vector;
34 
35 namespace android {
36 namespace os {
37 namespace statsd {
38 
39 // The reading and parsing are implemented in Java. It is not difficult to port over. But for now
40 // let StatsCompanionService handle that and send the data back.
StatsCompanionServicePuller(int tagId)41 StatsCompanionServicePuller::StatsCompanionServicePuller(int tagId) : StatsPuller(tagId) {
42 }
43 
SetStatsCompanionService(sp<IStatsCompanionService> statsCompanionService)44 void StatsCompanionServicePuller::SetStatsCompanionService(
45         sp<IStatsCompanionService> statsCompanionService) {
46     AutoMutex _l(mStatsCompanionServiceLock);
47     sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
48     mStatsCompanionService = statsCompanionService;
49 }
50 
PullInternal(vector<shared_ptr<LogEvent>> * data)51 bool StatsCompanionServicePuller::PullInternal(vector<shared_ptr<LogEvent> >* data) {
52     sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
53     if (statsCompanionServiceCopy != nullptr) {
54         vector<StatsLogEventWrapper> returned_value;
55         Status status = statsCompanionServiceCopy->pullData(mTagId, &returned_value);
56         if (!status.isOk()) {
57             ALOGW("StatsCompanionServicePuller::pull failed for %d", mTagId);
58             StatsdStats::getInstance().noteStatsCompanionPullFailed(mTagId);
59             if (status.exceptionCode() == Status::Exception::EX_TRANSACTION_FAILED) {
60                 StatsdStats::getInstance().noteStatsCompanionPullBinderTransactionFailed(mTagId);
61             }
62             return false;
63         }
64         data->clear();
65         for (const StatsLogEventWrapper& it : returned_value) {
66             LogEvent::createLogEvents(it, *data);
67         }
68         VLOG("StatsCompanionServicePuller::pull succeeded for %d", mTagId);
69         return true;
70     } else {
71         ALOGW("statsCompanion not found!");
72         return false;
73     }
74 }
75 
76 }  // namespace statsd
77 }  // namespace os
78 }  // namespace android
79