blob: e1a8572ac701c923a58c15e99a8ab00698b14441 [file] [log] [blame]
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +01001/*
2 * Copyright (C) 2015 by Sysmocom s.f.m.c. GmbH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
Pau Espin Pedrol5447f3a2021-03-11 17:01:25 +010018#pragma once
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010019#ifdef __cplusplus
Max20c7c462017-12-22 14:20:05 +010020extern "C" {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010021#endif
Max20c7c462017-12-22 14:20:05 +010022#include <osmocom/gsm/gsm_utils.h>
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010023#ifdef __cplusplus
Max20c7c462017-12-22 14:20:05 +010024}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010025#endif
26
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010027#include <time.h>
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010028#include <stdint.h>
Max20c7c462017-12-22 14:20:05 +010029
Pau Espin Pedrol3a42d172021-03-10 12:19:04 +010030#define FN_UNSET 0xFFFFFFFF
31
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010032static inline int msecs_to_frames(int msecs) {
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +010033 return (msecs * (1024 * 1000 / 4615)) / 1024;
34}
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010035
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010036static inline uint32_t next_fn(uint32_t fn, uint32_t offset)
Max9dabfa22017-05-16 16:10:45 +020037{
38 return (fn + offset) % GSM_MAX_FN;
39}
40
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010041static inline void csecs_to_timespec(unsigned csecs, struct timespec *ts) {
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010042 ts->tv_sec = csecs / 100;
43 ts->tv_nsec = (csecs % 100) * 10000000;
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010044}
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010045
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010046static inline uint32_t rts_next_fn(uint32_t rts_fn, uint8_t block_nr)
47{
48 uint32_t fn = rts_fn + 4;
49 if ((block_nr % 3) == 2)
50 fn++;
51 fn = fn % GSM_MAX_FN;
52 return fn;
53}
54
Pau Espin Pedrol3a42d172021-03-10 12:19:04 +010055static inline unsigned fn2bn(unsigned fn)
56{
57 return (fn % 52) / 4;
58}
59
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +020060static inline bool fn_valid(uint32_t fn)
61{
62 uint32_t f = fn % 13;
63 return f == 0 || f == 4 || f == 8;
64}
65
Pau Espin Pedrol3a42d172021-03-10 12:19:04 +010066static inline unsigned fn_next_block(unsigned fn)
67{
68 unsigned bn = fn2bn(fn) + 1;
69 fn = fn - (fn % 52);
70 fn += bn * 4 + bn / 3;
71 return fn % GSM_MAX_FN;
72}
73
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010074#ifdef __cplusplus
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010075template <typename T>
76inline unsigned int pcu_bitcount(T x)
77{
78 unsigned int count = 0;
79 for (count = 0; x; count += 1)
80 x &= x - 1;
81
82 return count;
83}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010084#endif
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010085
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010086static inline uint8_t pcu_lsb(uint8_t x)
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010087{
88 return x & -x;
89}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010090
91/* Used to store a C++ class in a llist used by C code */
92struct llist_item {
93 struct llist_head list; /* item used by llist */
94 void *entry;
95};