2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /************************************************************************************
19 * Filename: btif_uid.c
21 * Description: Contains data structures and functions for keeping track of
22 * socket usage per app UID.
24 ***********************************************************************************/
25 #define LOG_TAG "bt_uid_set"
26 #include "bt_common.h"
32 typedef struct uid_set_node_t {
33 struct uid_set_node_t* next;
34 bt_uid_traffic_t data;
37 typedef struct uid_set_t {
42 uid_set_t* uid_set_create(void) {
43 uid_set_t* set = osi_calloc(sizeof(uid_set_t));
44 pthread_mutex_init(&set->lock, NULL);
48 void uid_set_destroy(uid_set_t* set) {
49 pthread_mutex_lock(&set->lock);
50 uid_set_node_t* node = set->head;
52 uid_set_node_t* temp = node;
57 pthread_mutex_unlock(&set->lock);
58 pthread_mutex_destroy(&set->lock);
62 // Lock in uid_set_t must be held.
63 static uid_set_node_t* uid_set_find_or_create_node(uid_set_t* set, int32_t app_uid) {
64 uid_set_node_t* node = set->head;
65 while (node && node->data.app_uid != app_uid) {
70 node = osi_calloc(sizeof(uid_set_node_t));
71 node->data.app_uid = app_uid;
72 node->next = set->head;
78 void uid_set_add_tx(uid_set_t* set, int32_t app_uid, uint64_t bytes) {
79 if (app_uid == -1 || bytes == 0) {
83 pthread_mutex_lock(&set->lock);
84 uid_set_node_t* node = uid_set_find_or_create_node(set, app_uid);
85 node->data.tx_bytes += bytes;
86 pthread_mutex_unlock(&set->lock);
89 void uid_set_add_rx(uid_set_t* set, int32_t app_uid, uint64_t bytes) {
90 if (app_uid == -1 || bytes == 0) {
94 pthread_mutex_lock(&set->lock);
95 uid_set_node_t* node = uid_set_find_or_create_node(set, app_uid);
96 node->data.rx_bytes += bytes;
97 pthread_mutex_unlock(&set->lock);
100 bt_uid_traffic_t* uid_set_read_and_clear(uid_set_t* set) {
101 pthread_mutex_lock(&set->lock);
105 uid_set_node_t* node = set->head;
111 // Allocate an array of elements + 1, to signify the end with app_uid set to -1.
112 bt_uid_traffic_t* result = osi_calloc(sizeof(bt_uid_traffic_t) * (len + 1));
114 bt_uid_traffic_t* data = result;
121 // Clear the counters.
122 node->data.rx_bytes = 0;
123 node->data.tx_bytes = 0;
127 // Mark the last entry
130 pthread_mutex_unlock(&set->lock);