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