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