1 /*
2  * Copyright (C) 2021 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 #pragma once
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 
23 #include <android-base/result.h>
24 #include <android-base/unique_fd.h>
25 
26 namespace android {
27 namespace trusty {
28 namespace metrics {
29 
30 using android::base::Result;
31 using android::base::unique_fd;
32 
33 class TrustyMetrics {
34   public:
35     /* Wait for next event with a given timeout. Negative timeout means infinite timeout. */
36     Result<void> WaitForEvent(int timeout_ms = -1);
37     /* Attempt to handle an event from Metrics TA in a non-blocking manner. */
38     Result<void> HandleEvent();
39     /* Expose TIPC channel so that client can integrate it into an event loop with other fds. */
GetRawFd()40     int GetRawFd() { return metrics_fd_; };
41 
42   protected:
TrustyMetrics(std::string tipc_dev)43     TrustyMetrics(std::string tipc_dev) : tipc_dev_(std::move(tipc_dev)), metrics_fd_(-1) {}
~TrustyMetrics()44     virtual ~TrustyMetrics(){};
45 
46     Result<void> Open();
47     virtual void HandleCrash(const std::string& app_id) = 0;
48     virtual void HandleEventDrop() = 0;
49 
50   private:
51     std::string tipc_dev_;
52     unique_fd metrics_fd_;
53 };
54 
55 }  // namespace metrics
56 }  // namespace trusty
57 }  // namespace android
58