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 |
| 40 | @param rate sample clockrate |
| 41 | @param timestamp |
| 42 | */ |
| 43 | smpl_buf(size_t len, double rate); |
| 44 | ~smpl_buf(); |
| 45 | |
| 46 | /** Query number of samples available for reading |
| 47 | @param timestamp time of first sample |
| 48 | @return number of available samples or error |
| 49 | */ |
| 50 | ssize_t avail_smpls(TIMESTAMP timestamp) const; |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 51 | |
| 52 | /** Read and write |
| 53 | @param buf pointer to buffer |
| 54 | @param len number of samples desired to read or write |
| 55 | @param timestamp time of first stample |
| 56 | @return number of actual samples read or written or error |
| 57 | */ |
| 58 | ssize_t read(void *buf, size_t len, TIMESTAMP timestamp); |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 59 | ssize_t write(void *buf, size_t len, TIMESTAMP timestamp); |
Pau Espin Pedrol | 7bef234 | 2019-04-29 17:23:21 +0200 | [diff] [blame] | 60 | |
| 61 | /** Buffer status string |
| 62 | @return a formatted string describing internal buffer state |
| 63 | */ |
| 64 | std::string str_status(size_t ts) const; |
| 65 | |
| 66 | /** Formatted error string |
| 67 | @param code an error code |
| 68 | @return a formatted error string |
| 69 | */ |
| 70 | static std::string str_code(ssize_t code); |
| 71 | |
| 72 | enum err_code { |
| 73 | ERROR_TIMESTAMP = -1, |
| 74 | ERROR_READ = -2, |
| 75 | ERROR_WRITE = -3, |
| 76 | ERROR_OVERFLOW = -4 |
| 77 | }; |
| 78 | |
| 79 | private: |
| 80 | uint32_t *data; |
| 81 | size_t buf_len; |
| 82 | |
| 83 | double clk_rt; |
| 84 | |
| 85 | TIMESTAMP time_start; |
| 86 | TIMESTAMP time_end; |
| 87 | |
| 88 | size_t data_start; |
| 89 | size_t data_end; |
| 90 | }; |