blob: 24d3ce912aec42fc3468e549224b4f7e2cacaa86 [file] [log] [blame]
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +02001/*
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 Pedrol7bef2342019-04-29 17:23:21 +020028
29#include "radioDevice.h"
30
31/*
32 Sample Buffer - Allows reading and writing of timed samples using osmo-trx
Pau Espin Pedrol87b7d092019-04-29 17:46:34 +020033 timestamps. Time conversions are handled
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020034 internally or accessable through the static convert calls.
35*/
36class smpl_buf {
37public:
38 /** Sample buffer constructor
39 @param len number of 32-bit samples the buffer should hold
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020040 @param timestamp
41 */
Pau Espin Pedrol580c48b2019-05-03 14:38:36 +020042 smpl_buf(size_t len);
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020043 ~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 Pedrol7bef2342019-04-29 17:23:21 +020050
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 Pedrol7bef2342019-04-29 17:23:21 +020058 ssize_t write(void *buf, size_t len, TIMESTAMP timestamp);
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020059
60 /** Buffer status string
61 @return a formatted string describing internal buffer state
62 */
Pau Espin Pedrol3e9179a2019-04-30 18:13:23 +020063 std::string str_status(TIMESTAMP timestamp) const;
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020064
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
78private:
79 uint32_t *data;
80 size_t buf_len;
81
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020082 TIMESTAMP time_start;
83 TIMESTAMP time_end;
84
85 size_t data_start;
86 size_t data_end;
87};