summaryrefslogtreecommitdiffstats
path: root/src/rxlev_stat.c
blob: b474aaa870bfb84bc603e7037e2d225cf346d500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Rx Level statistics */

/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>

#include <osmocore/bitvec.h>
#include <osmocore/rxlev_stat.h>

int bitvec_find_bit_pos(const struct bitvec *bv, unsigned int n, enum bit_value val)
{
	unsigned int i;

	for (i = n; i < bv->data_len*8; i++) {
		if (bitvec_get_bit_pos(bv, i) == val)
			return i;
	}

	return -1;
}

void rxlev_stat_input(struct rxlev_stats *st, uint16_t arfcn, uint8_t rxlev)
{
	struct bitvec bv;

	if (rxlev >= NUM_RXLEVS)
		rxlev = NUM_RXLEVS-1;

	bv.data_len = NUM_ARFCNS/8;
	bv.data = st->rxlev_buckets[rxlev];

	bitvec_set_bit_pos(&bv, arfcn, ONE);
}

/* get the next ARFCN that has the specified Rxlev */
int16_t rxlev_stat_get_next(const struct rxlev_stats *st, uint8_t rxlev, int16_t arfcn)
{
	struct bitvec bv;

	if (rxlev >= NUM_RXLEVS)
		rxlev = NUM_RXLEVS-1;

	bv.data_len = NUM_ARFCNS/8;

	if (arfcn < 0)
		arfcn = -1;

	bv.data = (uint8_t *) st->rxlev_buckets[rxlev];

	return bitvec_find_bit_pos(&bv, arfcn+1, ONE);
}

void rxlev_stat_reset(struct rxlev_stats *st)
{
	memset(st, 0, sizeof(*st));
}

void rxlev_stat_dump(const struct rxlev_stats *st)
{
	int i;

	for (i = NUM_RXLEVS-1; i >= 0; i--) {
		int16_t arfcn = -1;

		printf("ARFCN with RxLev %u: ", i);
		while ((arfcn = rxlev_stat_get_next(st, i, arfcn)) >= 0) {
			printf("%u ", arfcn);
		}
		printf("\n");
	}
}