blob: 383c8143d5e53d3692bde40fd9ee99225a9d5999 [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 *
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +020010 * SPDX-License-Identifier: AGPL-3.0+
11 *
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020012 * 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 Pedrol7bef2342019-04-29 17:23:21 +020030
31#include "radioDevice.h"
32
33/*
34 Sample Buffer - Allows reading and writing of timed samples using osmo-trx
Pau Espin Pedrol87b7d092019-04-29 17:46:34 +020035 timestamps. Time conversions are handled
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020036 internally or accessable through the static convert calls.
37*/
38class smpl_buf {
39public:
40 /** Sample buffer constructor
41 @param len number of 32-bit samples the buffer should hold
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020042 @param timestamp
43 */
Pau Espin Pedrol580c48b2019-05-03 14:38:36 +020044 smpl_buf(size_t len);
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020045 ~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 Pedrol7bef2342019-04-29 17:23:21 +020052
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 Pedrol7bef2342019-04-29 17:23:21 +020060 ssize_t write(void *buf, size_t len, TIMESTAMP timestamp);
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020061
62 /** Buffer status string
63 @return a formatted string describing internal buffer state
64 */
Pau Espin Pedrol3e9179a2019-04-30 18:13:23 +020065 std::string str_status(TIMESTAMP timestamp) const;
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020066
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
80private:
81 uint32_t *data;
82 size_t buf_len;
83
Pau Espin Pedrol7bef2342019-04-29 17:23:21 +020084 TIMESTAMP time_start;
85 TIMESTAMP time_end;
86
87 size_t data_start;
88 size_t data_end;
89};