blob: ee886280c6e7bf2b80ebc9d46647fc24db7d47f1 [file] [log] [blame]
Ruchir Rastogi6baeca52019-10-14 16:18:34 -07001/*
2 * Copyright (C) 2019 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#ifndef ANDROID_STATS_LOG_STATS_EVENT_H
18#define ANDROID_STATS_LOG_STATS_EVENT_H
19
20#include <stdbool.h>
21#include <stddef.h>
22#include <stdint.h>
23
24/*
25 * Functionality to build and store the buffer sent over the statsd socket.
26 * This code defines and encapsulates the socket protocol.
27 *
28 * Usage:
29 * struct stats_event* event = stats_event_obtain();
30 *
Ruchir Rastogi6baeca52019-10-14 16:18:34 -070031 * stats_event_set_atom_id(event, atomId);
32 * stats_event_write_int32(event, 24);
33 * stats_event_add_bool_annotation(event, 1, true); // annotations apply to the previous field
34 * stats_event_add_int32_annotation(event, 2, 128);
35 * stats_event_write_float(event, 2.0);
36 *
37 * stats_event_write(event);
38 * stats_event_release(event);
39 *
40 * Notes:
41 * (a) write_<type>() and add_<type>_annotation() should be called in the order that fields
42 * and annotations are defined in the atom.
Ruchir Rastogi6c7cbc42019-10-22 10:56:58 -070043 * (b) set_atom_id() can be called anytime before stats_event_write().
Ruchir Rastogi6baeca52019-10-14 16:18:34 -070044 * (c) add_<type>_annotation() calls apply to the previous field.
45 * (d) If errors occur, stats_event_write() will write a bitmask of the errors to the socket.
46 * (e) Strings should be encoded using UTF8 and written using stats_event_write_string8().
47 */
48
49struct stats_event;
50
51/* ERRORS */
52#define ERROR_NO_TIMESTAMP 0x1
53#define ERROR_NO_ATOM_ID 0x2
54#define ERROR_OVERFLOW 0x4
55#define ERROR_ATTRIBUTION_CHAIN_TOO_LONG 0x8
56#define ERROR_ANNOTATION_DOES_NOT_FOLLOW_FIELD 0x10
57#define ERROR_INVALID_ANNOTATION_ID 0x20
58#define ERROR_ANNOTATION_ID_TOO_LARGE 0x40
59#define ERROR_TOO_MANY_ANNOTATIONS 0x80
60#define ERROR_TOO_MANY_FIELDS 0x100
61
Ruchir Rastogi6c7cbc42019-10-22 10:56:58 -070062/* SYSTEM API */
Ruchir Rastogi6baeca52019-10-14 16:18:34 -070063struct stats_event* stats_event_obtain();
64void stats_event_write(struct stats_event* event);
65void stats_event_release(struct stats_event* event);
66
67void stats_event_set_atom_id(struct stats_event* event, const uint32_t atomId);
Ruchir Rastogi6baeca52019-10-14 16:18:34 -070068
69void stats_event_write_int32(struct stats_event* event, int32_t value);
70void stats_event_write_int64(struct stats_event* event, int64_t value);
71void stats_event_write_float(struct stats_event* event, float value);
72void stats_event_write_bool(struct stats_event* event, bool value);
73void stats_event_write_byte_array(struct stats_event* event, uint8_t* buf, uint32_t numBytes);
74void stats_event_write_string8(struct stats_event* event, char* buf, uint32_t numBytes);
75void stats_event_write_attribution_chain(struct stats_event* event, uint32_t* uids, char** tags,
76 uint32_t* tagLengths, uint32_t numNodes);
77
78void stats_event_add_bool_annotation(struct stats_event* event, uint32_t annotationId, bool value);
79void stats_event_add_int32_annotation(struct stats_event* event, uint32_t annotationId,
80 int32_t value);
81
82uint32_t stats_event_get_errors(struct stats_event* event);
83
Ruchir Rastogi6c7cbc42019-10-22 10:56:58 -070084/* TESTING ONLY */
85void stats_event_set_timestamp_ns(struct stats_event* event, const uint64_t timestampNs);
86
Ruchir Rastogi6baeca52019-10-14 16:18:34 -070087#endif // ANDROID_STATS_LOG_STATS_EVENT_H