blob: 1c698ca415ad95b5e2a0f19138319546851752fd [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.
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +010013 */
Pau Espin Pedrol5447f3a2021-03-11 17:01:25 +010014#pragma once
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010015#ifdef __cplusplus
Max20c7c462017-12-22 14:20:05 +010016extern "C" {
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010017#endif
Max20c7c462017-12-22 14:20:05 +010018#include <osmocom/gsm/gsm_utils.h>
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010019#ifdef __cplusplus
Max20c7c462017-12-22 14:20:05 +010020}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010021#endif
22
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010023#include <time.h>
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010024#include <stdint.h>
Max20c7c462017-12-22 14:20:05 +010025
Pau Espin Pedrol3a42d172021-03-10 12:19:04 +010026#define FN_UNSET 0xFFFFFFFF
27
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010028static inline int msecs_to_frames(int msecs) {
Jacob Erlbeck3bed5d12015-03-19 11:22:38 +010029 return (msecs * (1024 * 1000 / 4615)) / 1024;
30}
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010031
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010032static inline uint32_t next_fn(uint32_t fn, uint32_t offset)
Max9dabfa22017-05-16 16:10:45 +020033{
34 return (fn + offset) % GSM_MAX_FN;
35}
36
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010037static inline void csecs_to_timespec(unsigned csecs, struct timespec *ts) {
Pau Espin Pedrol1de68732020-03-11 14:04:52 +010038 ts->tv_sec = csecs / 100;
39 ts->tv_nsec = (csecs % 100) * 10000000;
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010040}
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010041
Pau Espin Pedrol15c58ac2021-03-08 14:57:58 +010042static inline uint32_t rts_next_fn(uint32_t rts_fn, uint8_t block_nr)
43{
44 uint32_t fn = rts_fn + 4;
45 if ((block_nr % 3) == 2)
46 fn++;
47 fn = fn % GSM_MAX_FN;
48 return fn;
49}
50
Pau Espin Pedrol3a42d172021-03-10 12:19:04 +010051static inline unsigned fn2bn(unsigned fn)
52{
53 return (fn % 52) / 4;
54}
55
Pau Espin Pedrol50a1ede2021-03-29 13:49:43 +020056static inline bool fn_valid(uint32_t fn)
57{
58 uint32_t f = fn % 13;
59 return f == 0 || f == 4 || f == 8;
60}
61
Pau Espin Pedrol3a42d172021-03-10 12:19:04 +010062static inline unsigned fn_next_block(unsigned fn)
63{
64 unsigned bn = fn2bn(fn) + 1;
65 fn = fn - (fn % 52);
66 fn += bn * 4 + bn / 3;
67 return fn % GSM_MAX_FN;
68}
69
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010070#ifdef __cplusplus
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010071template <typename T>
72inline unsigned int pcu_bitcount(T x)
73{
74 unsigned int count = 0;
75 for (count = 0; x; count += 1)
76 x &= x - 1;
77
78 return count;
79}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010080#endif
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010081
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010082static inline uint8_t pcu_lsb(uint8_t x)
Jacob Erlbeck8cba7e92016-01-19 15:48:03 +010083{
84 return x & -x;
85}
Pau Espin Pedrolda971ee2020-12-16 15:59:45 +010086
87/* Used to store a C++ class in a llist used by C code */
88struct llist_item {
89 struct llist_head list; /* item used by llist */
90 void *entry;
91};