1 /*
2  * Copyright 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 <stdint.h>
20 
21 /**
22  * DOC: Metrics
23  *
24  * Metrics interface provides a way for Android to get Trusty metrics data.
25  *
26  * Currently, only "push" model is supported. Clients are expected to connect to
27  * metrics service, listen for events, e.g. app crash events, and respond to
28  * every event with a &struct metrics_req.
29  *
30  * Communication is driven by metrics service, i.e. requests/responses are all
31  * sent from/to metrics service.
32  *
33  * Note that the type of the event is not known to the client ahead of time.
34  *
35  * In the future, if we need to have Android "pull" metrics data from Trusty,
36  * that can be done by introducing a separate port.
37  *
38  * This interface is shared between Android and Trusty. There is a copy in each
39  * repository. They must be kept in sync.
40  */
41 
42 #define METRICS_PORT "com.android.trusty.metrics"
43 
44 /**
45  * enum metrics_cmd - command identifiers for metrics interface
46  * @METRICS_CMD_RESP_BIT:          message is a response
47  * @METRICS_CMD_REQ_SHIFT:         number of bits used by @METRICS_CMD_RESP_BIT
48  * @METRICS_CMD_REPORT_EVENT_DROP: report gaps in the event stream
49  * @METRICS_CMD_REPORT_CRASH:      report an app crash event
50  */
51 enum metrics_cmd {
52     METRICS_CMD_RESP_BIT = 1,
53     METRICS_CMD_REQ_SHIFT = 1,
54 
55     METRICS_CMD_REPORT_EVENT_DROP = (1 << METRICS_CMD_REQ_SHIFT),
56     METRICS_CMD_REPORT_CRASH = (2 << METRICS_CMD_REQ_SHIFT),
57 };
58 
59 /**
60  * enum metrics_error - metrics error codes
61  * @METRICS_NO_ERROR:        no error
62  * @METRICS_ERR_UNKNOWN_CMD: unknown or not implemented command
63  */
64 enum metrics_error {
65     METRICS_NO_ERROR = 0,
66     METRICS_ERR_UNKNOWN_CMD = 1,
67 };
68 
69 /**
70  * struct metrics_req - common structure for metrics requests
71  * @cmd:      command identifier - one of &enum metrics_cmd
72  * @reserved: must be 0
73  */
74 struct metrics_req {
75     uint32_t cmd;
76     uint32_t reserved;
77 } __attribute__((__packed__));
78 
79 /**
80  * struct metrics_resp - common structure for metrics responses
81  * @cmd: command identifier - %METRICS_CMD_RESP_BIT or'ed with a cmd in
82  *                            one of &enum metrics_cmd
83  * @status: response status, one of &enum metrics_error
84  */
85 struct metrics_resp {
86     uint32_t cmd;
87     uint32_t status;
88 } __attribute__((__packed__));
89 
90 /**
91  * struct metrics_report_crash_req - arguments of %METRICS_CMD_REPORT_CRASH
92  *                                   requests
93  * @app_id_len: length of app ID that follows this structure
94  */
95 struct metrics_report_crash_req {
96     uint32_t app_id_len;
97 } __attribute__((__packed__));
98 
99 #define METRICS_MAX_APP_ID_LEN 256
100 
101 #define METRICS_MAX_MSG_SIZE                                                \
102     (sizeof(struct metrics_req) + sizeof(struct metrics_report_crash_req) + \
103      METRICS_MAX_APP_ID_LEN)
104