Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Sample Buffer - Allows reading and writing of timed samples |
| 3 | * |
| 4 | * Copyright 2010,2011 Free Software Foundation, Inc. |
| 5 | * Copyright (C) 2015 Ettus Research LLC |
| 6 | * Copyright 2019 sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 7 | * |
| 8 | * Author: Tom Tsou <tom.tsou@ettus.com> |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Affero General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Affero General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Affero General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * See the COPYING file in the main directory for details. |
| 23 | */ |
| 24 | |
| 25 | #pragma once |
| 26 | |
| 27 | #include <unistd.h> |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 28 | |
| 29 | #include "radioDevice.h" |
| 30 | |
| 31 | /* |
| 32 | Sample Buffer - Allows reading and writing of timed samples using osmo-trx |
Pau Espin Pedrol | 87b7d09 | 2019-04-29 17:46:34 +0200 | [diff] [blame] | 33 | timestamps. Time conversions are handled |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 34 | internally or accessable through the static convert calls. |
| 35 | */ |
| 36 | class smpl_buf { |
| 37 | public: |
| 38 | /** Sample buffer constructor |
| 39 | @param len number of 32-bit samples the buffer should hold |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 40 | @param timestamp |
| 41 | */ |
Pau Espin Pedrol | 580c48b | 2019-05-03 14:38:36 +0200 | [diff] [blame] | 42 | smpl_buf(size_t len); |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 43 | ~smpl_buf(); |
| 44 | |
| 45 | /** Query number of samples available for reading |
| 46 | @param timestamp time of first sample |
| 47 | @return number of available samples or error |
| 48 | */ |
| 49 | ssize_t avail_smpls(TIMESTAMP timestamp) const; |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 50 | |
| 51 | /** Read and write |
| 52 | @param buf pointer to buffer |
| 53 | @param len number of samples desired to read or write |
| 54 | @param timestamp time of first stample |
| 55 | @return number of actual samples read or written or error |
| 56 | */ |
| 57 | ssize_t read(void *buf, size_t len, TIMESTAMP timestamp); |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 58 | ssize_t write(void *buf, size_t len, TIMESTAMP timestamp); |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 59 | |
| 60 | /** Buffer status string |
| 61 | @return a formatted string describing internal buffer state |
| 62 | */ |
Pau Espin Pedrol | 3e9179a | 2019-04-30 18:13:23 +0200 | [diff] [blame] | 63 | std::string str_status(TIMESTAMP timestamp) const; |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 64 | |
| 65 | /** Formatted error string |
| 66 | @param code an error code |
| 67 | @return a formatted error string |
| 68 | */ |
| 69 | static std::string str_code(ssize_t code); |
| 70 | |
| 71 | enum err_code { |
| 72 | ERROR_TIMESTAMP = -1, |
| 73 | ERROR_READ = -2, |
| 74 | ERROR_WRITE = -3, |
| 75 | ERROR_OVERFLOW = -4 |
| 76 | }; |
| 77 | |
| 78 | private: |
| 79 | uint32_t *data; |
| 80 | size_t buf_len; |
| 81 | |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 82 | TIMESTAMP time_start; |
| 83 | TIMESTAMP time_end; |
| 84 | |
| 85 | size_t data_start; |
| 86 | size_t data_end; |
| 87 | }; |