Jacob Erlbeck | 3bed5d1 | 2015-03-19 11:22:38 +0100 | [diff] [blame] | 1 | /* |
| 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 Erlbeck | 3bed5d1 | 2015-03-19 11:22:38 +0100 | [diff] [blame] | 13 | */ |
Pau Espin Pedrol | 5447f3a | 2021-03-11 17:01:25 +0100 | [diff] [blame] | 14 | #pragma once |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 15 | #ifdef __cplusplus |
Max | 20c7c46 | 2017-12-22 14:20:05 +0100 | [diff] [blame] | 16 | extern "C" { |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 17 | #endif |
Max | 20c7c46 | 2017-12-22 14:20:05 +0100 | [diff] [blame] | 18 | #include <osmocom/gsm/gsm_utils.h> |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 19 | #ifdef __cplusplus |
Max | 20c7c46 | 2017-12-22 14:20:05 +0100 | [diff] [blame] | 20 | } |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 21 | #endif |
| 22 | |
Pau Espin Pedrol | 1de6873 | 2020-03-11 14:04:52 +0100 | [diff] [blame] | 23 | #include <time.h> |
Pau Espin Pedrol | 15c58ac | 2021-03-08 14:57:58 +0100 | [diff] [blame] | 24 | #include <stdint.h> |
Max | 20c7c46 | 2017-12-22 14:20:05 +0100 | [diff] [blame] | 25 | |
Pau Espin Pedrol | 3a42d17 | 2021-03-10 12:19:04 +0100 | [diff] [blame] | 26 | #define FN_UNSET 0xFFFFFFFF |
| 27 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 28 | static inline int msecs_to_frames(int msecs) { |
Jacob Erlbeck | 3bed5d1 | 2015-03-19 11:22:38 +0100 | [diff] [blame] | 29 | return (msecs * (1024 * 1000 / 4615)) / 1024; |
| 30 | } |
Jacob Erlbeck | 0c1c877 | 2015-03-20 12:02:42 +0100 | [diff] [blame] | 31 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 32 | static inline uint32_t next_fn(uint32_t fn, uint32_t offset) |
Max | 9dabfa2 | 2017-05-16 16:10:45 +0200 | [diff] [blame] | 33 | { |
| 34 | return (fn + offset) % GSM_MAX_FN; |
| 35 | } |
| 36 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 37 | static inline void csecs_to_timespec(unsigned csecs, struct timespec *ts) { |
Pau Espin Pedrol | 1de6873 | 2020-03-11 14:04:52 +0100 | [diff] [blame] | 38 | ts->tv_sec = csecs / 100; |
| 39 | ts->tv_nsec = (csecs % 100) * 10000000; |
Jacob Erlbeck | 0c1c877 | 2015-03-20 12:02:42 +0100 | [diff] [blame] | 40 | } |
Jacob Erlbeck | 8cba7e9 | 2016-01-19 15:48:03 +0100 | [diff] [blame] | 41 | |
Pau Espin Pedrol | 15c58ac | 2021-03-08 14:57:58 +0100 | [diff] [blame] | 42 | static 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 Pedrol | 3a42d17 | 2021-03-10 12:19:04 +0100 | [diff] [blame] | 51 | static inline unsigned fn2bn(unsigned fn) |
| 52 | { |
| 53 | return (fn % 52) / 4; |
| 54 | } |
| 55 | |
Pau Espin Pedrol | 50a1ede | 2021-03-29 13:49:43 +0200 | [diff] [blame] | 56 | static 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 Pedrol | 3a42d17 | 2021-03-10 12:19:04 +0100 | [diff] [blame] | 62 | static 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 Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 70 | #ifdef __cplusplus |
Jacob Erlbeck | 8cba7e9 | 2016-01-19 15:48:03 +0100 | [diff] [blame] | 71 | template <typename T> |
| 72 | inline 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 Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 80 | #endif |
Jacob Erlbeck | 8cba7e9 | 2016-01-19 15:48:03 +0100 | [diff] [blame] | 81 | |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 82 | static inline uint8_t pcu_lsb(uint8_t x) |
Jacob Erlbeck | 8cba7e9 | 2016-01-19 15:48:03 +0100 | [diff] [blame] | 83 | { |
| 84 | return x & -x; |
| 85 | } |
Pau Espin Pedrol | da971ee | 2020-12-16 15:59:45 +0100 | [diff] [blame] | 86 | |
| 87 | /* Used to store a C++ class in a llist used by C code */ |
| 88 | struct llist_item { |
| 89 | struct llist_head list; /* item used by llist */ |
| 90 | void *entry; |
| 91 | }; |