SSB_HighSpeed_Modem/hsmodem/fifo.cpp

194 lines
4.8 KiB
C++
Raw Permalink Normal View History

2020-12-10 13:14:40 -05:00
/*
2021-02-22 09:58:19 -05:00
* Audio Library for Linux and Windows
* ===================================
2020-12-10 13:14:40 -05:00
* Author: DJ0ABR
*
2021-02-22 09:58:19 -05:00
* Author: Kurt Moraw, Ham radio: DJ0ABR, github: dj0abr
* License: GPL-3
*
* compilation:
* Windows ... Visual Studio
* Linux ... make
*
* Documentation see: libkmaudio.h
2020-12-10 13:14:40 -05:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
2021-02-22 09:58:19 -05:00
* fifo.cpp ... thread safe FIFOs
2020-12-10 13:14:40 -05:00
*
*/
#include "hsmodem.h"
2021-02-22 09:58:19 -05:00
#define NUM_OF_FIFOS 20
2021-01-02 18:34:35 -05:00
2021-02-22 09:58:19 -05:00
#ifdef WIN32
CRITICAL_SECTION fifo_crit_sec[NUM_OF_FIFOS];
#define LOCKFIFO(pn) EnterCriticalSection(&(fifo_crit_sec[pn]))
void UNLOCKFIFO(int pn)
2020-12-10 13:14:40 -05:00
{
2021-02-22 09:58:19 -05:00
if (&(fifo_crit_sec[pn]) != NULL)
LeaveCriticalSection(&(fifo_crit_sec[pn]));
2020-12-10 13:14:40 -05:00
}
2021-02-22 09:58:19 -05:00
#else
pthread_mutex_t fifo_crit_sec[NUM_OF_FIFOS];
#define LOCKFIFO(pn) pthread_mutex_lock(&(fifo_crit_sec[pn]))
#define UNLOCKFIFO(pn) pthread_mutex_unlock(&(fifo_crit_sec[pn]))
2020-12-10 13:14:40 -05:00
#endif
2021-02-22 09:58:19 -05:00
#define FIFOBUFLEN 100 // number of fifo buffers
#define FIFOELEMENTLEN 300 // length of one fifo element
2020-12-10 13:14:40 -05:00
2021-02-22 09:58:19 -05:00
int wridx[NUM_OF_FIFOS];
int rdidx[NUM_OF_FIFOS];
int8_t buffer[NUM_OF_FIFOS][FIFOBUFLEN][FIFOELEMENTLEN];
2020-12-10 13:14:40 -05:00
2021-02-22 09:58:19 -05:00
void init_fifos()
2020-12-10 13:14:40 -05:00
{
2021-02-22 09:58:19 -05:00
// init pipes only once
static int f = 1;
if (f)
2020-12-10 13:14:40 -05:00
{
2021-02-22 09:58:19 -05:00
f = 0;
for (int i = 0; i < NUM_OF_FIFOS; i++)
{
#ifdef WIN32
if (&(fifo_crit_sec[i]) != NULL) DeleteCriticalSection(&(fifo_crit_sec[i]));
InitializeCriticalSection(&(fifo_crit_sec[i]));
#else
if (&(fifo_crit_sec[i]) != NULL) pthread_mutex_destroy(&(fifo_crit_sec[i]));
pthread_mutex_init(&(fifo_crit_sec[i]), NULL);
#endif
}
2020-12-10 13:14:40 -05:00
}
2021-02-22 09:58:19 -05:00
for (int i = 0; i < NUM_OF_FIFOS; i++)
fifo_clear(i);
2020-12-10 13:14:40 -05:00
}
2021-02-22 09:58:19 -05:00
// write into the fifo
// ignore data if the fifo is full
void write_fifo(int pipenum, uint8_t *pdata, int len)
2020-12-10 13:14:40 -05:00
{
2021-02-22 09:58:19 -05:00
if (pipenum < 0 || pipenum >= NUM_OF_FIFOS) return;
2020-12-10 13:14:40 -05:00
2021-02-22 09:58:19 -05:00
LOCKFIFO(pipenum);
if (((wridx[pipenum] + 1) % FIFOBUFLEN) == rdidx[pipenum])
2020-12-10 13:14:40 -05:00
{
2021-02-22 09:58:19 -05:00
//printf("cannot WRITE fifo %d full\n",pipenum);
UNLOCKFIFO(pipenum);
return;
2020-12-10 13:14:40 -05:00
}
2021-02-22 09:58:19 -05:00
// as the first 2 bytes store the length, MSB first
buffer[pipenum][wridx[pipenum]][0] = len >> 8;
buffer[pipenum][wridx[pipenum]][1] = len & 0xff;
2020-12-10 13:14:40 -05:00
2021-02-22 09:58:19 -05:00
// followed by the data
memcpy(buffer[pipenum][wridx[pipenum]] + 2, pdata, len);
if (++wridx[pipenum] >= FIFOBUFLEN) wridx[pipenum] = 0;
2020-12-10 13:14:40 -05:00
2021-02-22 09:58:19 -05:00
UNLOCKFIFO(pipenum);
2020-12-10 13:14:40 -05:00
}
2021-02-22 09:58:19 -05:00
// read from the fifo
// return: number of bytes read
int read_fifo(int pipenum, uint8_t* pdata, int maxlen)
2020-12-10 13:14:40 -05:00
{
2021-02-22 09:58:19 -05:00
if (pipenum < 0 || pipenum >= NUM_OF_FIFOS)
2020-12-10 13:14:40 -05:00
{
2021-02-22 09:58:19 -05:00
printf("read_fifo: wrong pipenum:%d (%d ..%d)\n", pipenum, 0, NUM_OF_FIFOS-1);
return 0;
2020-12-10 13:14:40 -05:00
}
2021-02-22 09:58:19 -05:00
LOCKFIFO(pipenum);
2020-12-10 13:14:40 -05:00
2021-02-22 09:58:19 -05:00
if (rdidx[pipenum] == wridx[pipenum])
2020-12-10 13:14:40 -05:00
{
// Fifo empty, no data available
2021-02-22 09:58:19 -05:00
//printf("read: no data\n");
UNLOCKFIFO(pipenum);
2020-12-10 13:14:40 -05:00
return 0;
}
2021-02-22 09:58:19 -05:00
// read length
int len = buffer[pipenum][rdidx[pipenum]][0];
len <<= 8;
len += buffer[pipenum][rdidx[pipenum]][1];
2021-01-02 18:34:35 -05:00
2021-02-22 09:58:19 -05:00
if (len > maxlen)
2020-12-10 13:14:40 -05:00
{
2021-02-22 09:58:19 -05:00
printf("read_fifo: %d, pdata too small. Need:%d has:%d\n", pipenum, len, maxlen);
return 0; // pdata too small
2020-12-10 13:14:40 -05:00
}
2021-02-22 09:58:19 -05:00
// read data
memcpy(pdata, buffer[pipenum][rdidx[pipenum]] + 2, len);
if (++rdidx[pipenum] >= FIFOBUFLEN) rdidx[pipenum] = 0;
UNLOCKFIFO(pipenum);
2021-01-02 18:34:35 -05:00
2021-02-22 09:58:19 -05:00
return len;
2021-01-02 18:34:35 -05:00
}
2021-02-22 09:58:19 -05:00
void fifo_clear(int pipenum)
2021-01-02 18:34:35 -05:00
{
2021-02-22 09:58:19 -05:00
if (pipenum < 0 || pipenum >= NUM_OF_FIFOS) return;
2021-01-02 18:34:35 -05:00
2021-02-22 09:58:19 -05:00
wridx[pipenum] = rdidx[pipenum] = 0;
2021-01-02 18:34:35 -05:00
}
2021-02-22 09:58:19 -05:00
int fifo_freespace(int pipenum)
2021-01-02 18:34:35 -05:00
{
2021-02-22 09:58:19 -05:00
if (pipenum < 0 || pipenum >= NUM_OF_FIFOS) return 0;
2021-01-02 18:34:35 -05:00
2021-02-22 09:58:19 -05:00
int freebuf = 0;
2021-01-02 18:34:35 -05:00
2021-02-22 09:58:19 -05:00
LOCKFIFO(pipenum);
2021-01-02 18:34:35 -05:00
2021-02-22 09:58:19 -05:00
int elemInFifo = (wridx[pipenum] + FIFOBUFLEN - rdidx[pipenum]) % FIFOBUFLEN;
freebuf = FIFOBUFLEN - elemInFifo;
2021-01-02 18:34:35 -05:00
2021-02-22 09:58:19 -05:00
UNLOCKFIFO(pipenum);
return freebuf;
2021-01-02 18:34:35 -05:00
}
2021-02-22 09:58:19 -05:00
int fifo_dataavail(int pipenum)
2021-01-02 18:34:35 -05:00
{
2021-02-22 09:58:19 -05:00
LOCKFIFO(pipenum);
2021-01-02 18:34:35 -05:00
2021-02-22 09:58:19 -05:00
if (rdidx[pipenum] == wridx[pipenum])
2021-01-02 18:34:35 -05:00
{
// Fifo empty, no data available
2021-02-22 09:58:19 -05:00
UNLOCKFIFO(pipenum);
2021-01-02 18:34:35 -05:00
return 0;
}
2021-02-22 09:58:19 -05:00
UNLOCKFIFO(pipenum);
2021-01-02 18:34:35 -05:00
return 1;
}
2021-02-22 09:58:19 -05:00
int fifo_usedspace(int pipenum)
2021-01-02 18:34:35 -05:00
{
2021-02-22 09:58:19 -05:00
int us = FIFOBUFLEN - fifo_freespace(pipenum);
//printf("fifo:%d used space:%d\n", pipenum, us);
return us;
2021-01-02 18:34:35 -05:00
}
2021-02-22 09:58:19 -05:00
int fifo_usedpercent(int pipenum)
2021-01-02 18:34:35 -05:00
{
2021-02-22 09:58:19 -05:00
int used = FIFOBUFLEN - fifo_freespace(pipenum);
int percent = (used * 100) / FIFOBUFLEN;
return percent;
2021-01-02 18:34:35 -05:00
}