mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-09-01 09:51:22 +02:00
- add support for radiotext (needs testing) -> http://www.dbox2world.net/board293-coolstream-hd1/board296-coolstream-software/10635-radiotext-einbauen/
git-svn-id: file:///home/bas/coolstream_public_svn/THIRDPARTY/applications/neutrino-experimental@1501 e54a6e83-5905-42d5-8d5c-058d10e6a962
This commit is contained in:
@@ -33,7 +33,10 @@ libneutrino_driver_a_SOURCES = \
|
||||
shutdown_count.cpp \
|
||||
genpsi.c \
|
||||
streamts.cpp \
|
||||
rfmod.cpp
|
||||
rfmod.cpp \
|
||||
radiotools.cpp \
|
||||
radiotext.cpp \
|
||||
ringbuffer.c
|
||||
|
||||
if BOXTYPE_COOL
|
||||
libneutrino_driver_a_SOURCES += \
|
||||
|
2674
src/driver/radiotext.cpp
Normal file
2674
src/driver/radiotext.cpp
Normal file
File diff suppressed because it is too large
Load Diff
298
src/driver/radiotext.h
Normal file
298
src/driver/radiotext.h
Normal file
@@ -0,0 +1,298 @@
|
||||
/*
|
||||
$Id: radiotext.h,v 1.4 2009/10/31 10:11:02 seife Exp $
|
||||
|
||||
Neutrino-GUI - DBoxII-Project
|
||||
|
||||
License: GPL
|
||||
|
||||
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
|
||||
|
||||
ripped from:
|
||||
*/
|
||||
|
||||
/*
|
||||
* radioaudio.h: A plugin for the Video Disk Recorder
|
||||
*
|
||||
* See the README file for copyright information and how to reach the author.
|
||||
*
|
||||
* This is a "plugin" for the Video Disk Recorder (VDR).
|
||||
*
|
||||
* Written by: Lars Tegeler <email@host.dom>
|
||||
*
|
||||
* Project's homepage: www.math.uni-paderborn.de/~tegeler/vdr
|
||||
*
|
||||
* Latest version available at: URL
|
||||
*
|
||||
* See the file COPYING for license information.
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* This Plugin display an background image while the vdr is switcht to radio channels.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __RADIO_AUDIO_H
|
||||
#define __RADIO_AUDIO_H
|
||||
|
||||
#include <driver/framebuffer.h>
|
||||
#include <driver/fontrenderer.h>
|
||||
|
||||
#if 0
|
||||
#include <vdr/player.h>
|
||||
#include <vdr/device.h>
|
||||
#include <vdr/audio.h>
|
||||
#include <vdr/osd.h>
|
||||
#include <vdr/menu.h>
|
||||
#include <vdr/receiver.h>
|
||||
#endif
|
||||
|
||||
#include <dmx_cs.h>
|
||||
|
||||
//#define ENABLE_RASS
|
||||
|
||||
typedef unsigned char uchar;
|
||||
typedef unsigned int uint;
|
||||
|
||||
extern const char *ConfigDir;
|
||||
extern const char *DataDir;
|
||||
extern char *ReplayFile;
|
||||
|
||||
|
||||
#if 0
|
||||
// RDS-Receiver for seperate Data-Pids
|
||||
class cRDSReceiver : public cReceiver {
|
||||
private:
|
||||
int pid;
|
||||
bool rt_start;
|
||||
bool rt_bstuff;
|
||||
protected:
|
||||
virtual void Receive(uchar *Data, int Length);
|
||||
public:
|
||||
cRDSReceiver(int Pid);
|
||||
virtual ~cRDSReceiver(void);
|
||||
};
|
||||
#endif
|
||||
|
||||
#define RT_MEL 65
|
||||
#define tr(a) a
|
||||
|
||||
class CRadioText {
|
||||
|
||||
public:
|
||||
typedef struct {
|
||||
CRadioText *rt_object;
|
||||
int fd;
|
||||
} s_rt_thread;
|
||||
|
||||
private:
|
||||
bool enabled;
|
||||
bool have_radiotext;
|
||||
char *imagepath;
|
||||
bool imageShown;
|
||||
int imagedelay;
|
||||
void send_pes_packet(unsigned char *data, int len, int timestamp);
|
||||
void ShowImage (const char *file);
|
||||
int first_packets;
|
||||
|
||||
//Radiotext
|
||||
// cDevice *rdsdevice;
|
||||
void RadiotextCheckPES(const uchar *Data, int Length);
|
||||
void RadioStatusMsg(void);
|
||||
void AudioRecorderService(void);
|
||||
void RassDecode(uchar *Data, int Length);
|
||||
bool DividePes(char *data, int length, int *substart, int *subend);
|
||||
|
||||
uint pid;
|
||||
pthread_t threadRT;
|
||||
int dmxfd;
|
||||
|
||||
public:
|
||||
CRadioText(void);
|
||||
~CRadioText(void);
|
||||
int PES_Receive(char *data, int len);
|
||||
int RassImage(int QArchiv, int QKey, bool DirUp);
|
||||
void EnableRadioTextProcessing(const char *Titel, bool replay = false);
|
||||
void DisableRadioTextProcessing();
|
||||
void RadiotextDecode(uchar *Data, int Length);
|
||||
void RDS_PsPtynDecode(bool PTYN, uchar *Data, int Length);
|
||||
void ShowText(void);
|
||||
char* ptynr2string(int nr);
|
||||
char *rds_entitychar(char *text);
|
||||
|
||||
void setPid(uint inPid);
|
||||
uint getPid(){ return pid; }
|
||||
int run(void);
|
||||
int getDMXfd(void) { return dmxfd; }
|
||||
// s_rt_thread& getThreadParams(void) { return rt; }
|
||||
pthread_t getThread(void) { return threadRT; }
|
||||
void radiotext_stop(void);
|
||||
bool haveRadiotext(void) {return have_radiotext; }
|
||||
|
||||
cDemux *audioDemux;
|
||||
s_rt_thread rt;
|
||||
|
||||
//Setup-Params
|
||||
int S_RtFunc;
|
||||
int S_RtOsd;
|
||||
int S_RtOsdTitle;
|
||||
int S_RtOsdTags;
|
||||
int S_RtOsdPos;
|
||||
int S_RtOsdRows;
|
||||
int S_RtOsdLoop;
|
||||
int S_RtOsdTO;
|
||||
int S_RtSkinColor;
|
||||
int S_RtBgCol;
|
||||
int S_RtBgTra;
|
||||
int S_RtFgCol;
|
||||
int S_RtDispl;
|
||||
int S_RassText;
|
||||
int S_RtMsgItems;
|
||||
// uint32_t rt_color[9];
|
||||
int S_Verbose;
|
||||
|
||||
// Radiotext
|
||||
int RTP_ItemToggle, RTP_TToggle;
|
||||
bool RT_MsgShow, RT_PlusShow;
|
||||
bool RT_Replay, RT_ReOpen;
|
||||
char RT_Text[5][RT_MEL];
|
||||
char RTP_Artist[RT_MEL], RTP_Title[RT_MEL];
|
||||
int RT_Info, RT_Index, RT_PTY;
|
||||
time_t RTP_Starttime;
|
||||
bool RT_OsdTO, RTplus_Osd;
|
||||
int RT_OsdTOTemp;
|
||||
char RDS_PTYN[9];
|
||||
char *RT_Titel, *RTp_Titel;
|
||||
|
||||
#if ENABLE_RASS
|
||||
// Rass ...
|
||||
int Rass_Show; // -1=No, 0=Yes, 1=display
|
||||
int Rass_Archiv; // -1=Off, 0=Index, 1000-9990=Slidenr.
|
||||
bool Rass_Flags[11][4]; // Slides+Gallery existent
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
#if 0
|
||||
class cRadioTextOsd : public cOsdObject {
|
||||
private:
|
||||
cOsd *osd;
|
||||
cOsd *qosd;
|
||||
cOsd *qiosd;
|
||||
const cFont *ftitel;
|
||||
const cFont *ftext;
|
||||
int fheight;
|
||||
int bheight;
|
||||
eKeys LastKey;
|
||||
cTimeMs osdtimer;
|
||||
void rtp_print(void);
|
||||
bool rtclosed;
|
||||
bool rassclosed;
|
||||
static cBitmap rds, arec, rass;
|
||||
static cBitmap index, marker, page1, pages2, pages3, pages4, pageE;
|
||||
static cBitmap no0, no1, no2, no3, no4, no5, no6, no7, no8, no9, bok;
|
||||
public:
|
||||
cRadioTextOsd();
|
||||
~cRadioTextOsd();
|
||||
virtual void Hide(void);
|
||||
virtual void Show(void);
|
||||
virtual void ShowText(void);
|
||||
virtual void RTOsdClose(void);
|
||||
int RassImage(int QArchiv, int QKey, bool DirUp);
|
||||
virtual void RassOsd(void);
|
||||
virtual void RassOsdTip(void);
|
||||
virtual void RassOsdClose(void);
|
||||
virtual void RassImgSave(char *size, int pos);
|
||||
virtual eOSState ProcessKey(eKeys Key);
|
||||
virtual bool IsInteractive(void) { return false; }
|
||||
};
|
||||
|
||||
class cRTplusOsd : public cOsdMenu {
|
||||
private:
|
||||
int bcount;
|
||||
int helpmode;
|
||||
const char *listtyp[7];
|
||||
char *btext[7];
|
||||
int rtptyp(char *btext);
|
||||
void rtp_fileprint(void);
|
||||
public:
|
||||
cRTplusOsd(void);
|
||||
virtual ~cRTplusOsd();
|
||||
virtual void Load(void);
|
||||
virtual void Update(void);
|
||||
virtual eOSState ProcessKey(eKeys Key);
|
||||
};
|
||||
|
||||
class cRTplusList : public cOsdMenu {
|
||||
private:
|
||||
int typ;
|
||||
bool refresh;
|
||||
public:
|
||||
cRTplusList(int Typ = 0);
|
||||
~cRTplusList();
|
||||
virtual void Load(void);
|
||||
virtual void Update(void);
|
||||
virtual eOSState ProcessKey(eKeys Key);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
// Radiotext-Memory
|
||||
#define MAX_RTPC 50
|
||||
struct rtp_classes {
|
||||
time_t start;
|
||||
char temptext[RT_MEL];
|
||||
char *radiotext[2*MAX_RTPC];
|
||||
int rt_Index;
|
||||
// Item
|
||||
bool item_New;
|
||||
char *item_Title[MAX_RTPC]; // 1
|
||||
char *item_Artist[MAX_RTPC]; // 4
|
||||
time_t item_Start[MAX_RTPC];
|
||||
int item_Index;
|
||||
// Info
|
||||
char *info_News; // 12
|
||||
char *info_NewsLocal; // 13
|
||||
char *info_Stock[MAX_RTPC]; // 14
|
||||
int info_StockIndex;
|
||||
char *info_Sport[MAX_RTPC]; // 15
|
||||
int info_SportIndex;
|
||||
char *info_Lottery[MAX_RTPC]; // 16
|
||||
int info_LotteryIndex;
|
||||
char *info_DateTime; // 24
|
||||
char *info_Weather[MAX_RTPC]; // 25
|
||||
int info_WeatherIndex;
|
||||
char *info_Traffic; // 26
|
||||
char *info_Alarm; // 27
|
||||
char *info_Advert; // 28
|
||||
char *info_Url; // 29
|
||||
char *info_Other[MAX_RTPC]; // 30
|
||||
int info_OtherIndex;
|
||||
// Programme
|
||||
char *prog_Station; // 31
|
||||
char *prog_Now; // 32
|
||||
char *prog_Next; // 33
|
||||
char *prog_Part; // 34
|
||||
char *prog_Host; // 35
|
||||
char *prog_EditStaff; // 36
|
||||
char *prog_Homepage; // 38
|
||||
// Interactivity
|
||||
char *phone_Hotline; // 39
|
||||
char *phone_Studio; // 40
|
||||
char *email_Hotline; // 44
|
||||
char *email_Studio; // 45
|
||||
// to be continue...
|
||||
};
|
||||
|
||||
#endif //__RADIO_AUDIO_H
|
101
src/driver/radiotools.cpp
Normal file
101
src/driver/radiotools.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* radiotools.c: A plugin for the Video Disk Recorder
|
||||
*
|
||||
* See the README file for copyright information and how to reach the author.
|
||||
*
|
||||
* This is a "plugin" for the Video Disk Recorder (VDR).
|
||||
*
|
||||
* Written by: Lars Tegeler <email@host.dom>
|
||||
*
|
||||
* Project's homepage: www.math.uni-paderborn.de/~tegeler/vdr
|
||||
*
|
||||
* Latest version available at: URL
|
||||
*
|
||||
* See the file COPYING for license information.
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* This Plugin display an background image while the vdr is switcht to radio channels.
|
||||
*
|
||||
* $Id: radiotools.cpp,v 1.1 2009/08/07 07:22:31 rhabarber1848 Exp $
|
||||
|
||||
*/
|
||||
|
||||
#include "radiotools.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
/* for timetest */
|
||||
//#include <time.h>
|
||||
//#include <sys/time.h>
|
||||
unsigned short crc16_ccitt(unsigned char *daten, int len, bool skipfirst)
|
||||
{
|
||||
/* timetest */
|
||||
//struct timeval t;
|
||||
//unsigned long long tstart = 0;
|
||||
//if (gettimeofday(&t, NULL) == 0)
|
||||
// tstart = t.tv_sec*1000000 + t.tv_usec;
|
||||
|
||||
// CRC16-CCITT: x^16 + x^12 + x^5 + 1
|
||||
// with start 0xffff and result invers
|
||||
register unsigned short crc = 0xffff;
|
||||
|
||||
if (skipfirst) *daten++;
|
||||
while (len--) {
|
||||
crc = (crc >> 8) | (crc << 8);
|
||||
crc ^= *daten++;
|
||||
crc ^= (crc & 0xff) >> 4;
|
||||
crc ^= (crc << 8) << 4;
|
||||
crc ^= ((crc & 0xff) << 4) << 1;
|
||||
}
|
||||
|
||||
/* timetest */
|
||||
//if (tstart > 0 && gettimeofday(&t, NULL) == 0)
|
||||
// printf("vdr-radio: crc-calctime = %d usec\n", (int)((t.tv_sec*1000000 + t.tv_usec) - tstart));
|
||||
|
||||
return ~(crc);
|
||||
}
|
||||
|
||||
char *rtrim(char *text)
|
||||
{
|
||||
char *s = text + strlen(text) - 1;
|
||||
while (s >= text && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
|
||||
*s-- = 0;
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
// --- cTimeMs ---------------------------------------------------------------
|
||||
|
||||
cTimeMs::cTimeMs(void)
|
||||
{
|
||||
Set();
|
||||
}
|
||||
|
||||
#if 0
|
||||
uint64_t cTimeMs::Now(void)
|
||||
{
|
||||
struct timeval t;
|
||||
if (gettimeofday(&t, NULL) == 0)
|
||||
return (uint64_t(t.tv_sec)) * 1000 + t.tv_usec / 1000;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void cTimeMs::Set(int Ms)
|
||||
{
|
||||
begin = Now() + Ms;
|
||||
}
|
||||
|
||||
bool cTimeMs::TimedOut(void)
|
||||
{
|
||||
return Now() >= begin;
|
||||
}
|
||||
|
||||
uint64_t cTimeMs::Elapsed(void)
|
||||
{
|
||||
return Now() - begin;
|
||||
}
|
||||
|
||||
#endif
|
||||
//--------------- End -----------------------------------------------------------------
|
46
src/driver/radiotools.h
Normal file
46
src/driver/radiotools.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* radiotools.h: A plugin for the Video Disk Recorder
|
||||
*
|
||||
* See the README file for copyright information and how to reach the author.
|
||||
*
|
||||
* This is a "plugin" for the Video Disk Recorder (VDR).
|
||||
*
|
||||
* Written by: Lars Tegeler <email@host.dom>
|
||||
*
|
||||
* Project's homepage: www.math.uni-paderborn.de/~tegeler/vdr
|
||||
*
|
||||
* Latest version available at: URL
|
||||
*
|
||||
* See the file COPYING for license information.
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* This Plugin display an background image while the vdr is switcht to radio channels.
|
||||
*
|
||||
* $Id: radiotools.h,v 1.1 2009/08/07 07:22:31 rhabarber1848 Exp $
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __RADIO_TOOLS_H
|
||||
#define __RADIO_TOOLS_H
|
||||
|
||||
|
||||
unsigned short crc16_ccitt(unsigned char *daten, int len, bool skipfirst);
|
||||
|
||||
char *rtrim(char *text);
|
||||
|
||||
typedef long long unsigned int uint64_t;
|
||||
|
||||
class cTimeMs {
|
||||
private:
|
||||
uint64_t begin;
|
||||
|
||||
public:
|
||||
cTimeMs(void);
|
||||
static uint64_t Now(void);
|
||||
void Set(int Ms = 0);
|
||||
bool TimedOut(void);
|
||||
uint64_t Elapsed(void);
|
||||
};
|
||||
|
||||
#endif //__RADIO_TOOLS_H
|
378
src/driver/ringbuffer.c
Normal file
378
src/driver/ringbuffer.c
Normal file
@@ -0,0 +1,378 @@
|
||||
/*
|
||||
* Copyright (C) 2000 Paul Davis
|
||||
* Copyright (C) 2003 Rohan Drape
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* ISO/POSIX C version of Paul Davis's lock free ringbuffer C++ code.
|
||||
* This is safe for the case of one read thread and one write thread.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include "ringbuffer.h"
|
||||
|
||||
|
||||
/* Create a new ringbuffer to hold at least `sz' bytes of data. The
|
||||
* actual buffer size is rounded up to the next power of two.
|
||||
*/
|
||||
ringbuffer_t * ringbuffer_create (int sz)
|
||||
{
|
||||
int power_of_two;
|
||||
ringbuffer_t *rb;
|
||||
|
||||
rb = malloc (sizeof (ringbuffer_t));
|
||||
|
||||
for(power_of_two = 1; 1 << power_of_two < sz; power_of_two++)
|
||||
;
|
||||
|
||||
rb->size = 1 << power_of_two;
|
||||
rb->size_mask = rb->size;
|
||||
rb->size_mask -= 1;
|
||||
rb->write_ptr = 0;
|
||||
rb->read_ptr = 0;
|
||||
rb->buf = malloc (rb->size);
|
||||
rb->mlocked = 0;
|
||||
rb->helpbufsize = 1;
|
||||
rb->helpbuf = malloc (rb->helpbufsize);
|
||||
|
||||
if( rb->buf )
|
||||
return rb;
|
||||
|
||||
free( rb );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* Free all data associated with the ringbuffer `rb'.
|
||||
*/
|
||||
void ringbuffer_free (ringbuffer_t * rb)
|
||||
{
|
||||
if (rb->mlocked)
|
||||
munlock (rb->buf, rb->size);
|
||||
|
||||
free (rb->buf);
|
||||
rb->buf=0;
|
||||
free (rb->helpbuf);
|
||||
rb->helpbuf=0;
|
||||
free (rb);
|
||||
rb=0;
|
||||
}
|
||||
|
||||
/* Lock the data block of `rb' using the system call 'mlock'. */
|
||||
int ringbuffer_mlock (ringbuffer_t * rb)
|
||||
{
|
||||
if (mlock (rb->buf, rb->size))
|
||||
return -1;
|
||||
|
||||
rb->mlocked = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Reset the read and write pointers to zero. This is not thread
|
||||
* safe.
|
||||
*/
|
||||
void ringbuffer_reset (ringbuffer_t * rb)
|
||||
{
|
||||
rb->read_ptr = 0;
|
||||
rb->write_ptr = 0;
|
||||
}
|
||||
|
||||
/* Return the number of bytes available for reading. This is the
|
||||
* number of bytes in front of the read pointer and behind the write
|
||||
* pointer.
|
||||
*/
|
||||
size_t ringbuffer_read_space (ringbuffer_t * rb)
|
||||
{
|
||||
size_t w, r;
|
||||
|
||||
w = rb->write_ptr;
|
||||
r = rb->read_ptr;
|
||||
|
||||
if (w > r)
|
||||
return w - r;
|
||||
else
|
||||
return (w - r + rb->size) & rb->size_mask;
|
||||
}
|
||||
|
||||
/* Return the number of bytes available for writing. This is the
|
||||
* number of bytes in front of the write pointer and behind the read
|
||||
* pointer.
|
||||
*/
|
||||
size_t ringbuffer_write_space (ringbuffer_t * rb)
|
||||
{
|
||||
size_t w, r;
|
||||
|
||||
w = rb->write_ptr;
|
||||
r = rb->read_ptr;
|
||||
|
||||
if (w > r)
|
||||
return ((r - w + rb->size) & rb->size_mask) - 1;
|
||||
else if (w < r)
|
||||
return (r - w) - 1;
|
||||
else
|
||||
return rb->size - 1;
|
||||
}
|
||||
|
||||
/* The copying data reader. Copy at most `cnt' bytes from `rb' to
|
||||
* `dest'. Returns the actual number of bytes copied.
|
||||
*/
|
||||
size_t ringbuffer_read (ringbuffer_t * rb, char *dest, size_t cnt)
|
||||
{
|
||||
size_t free_cnt;
|
||||
size_t cnt2;
|
||||
size_t to_read;
|
||||
size_t n1, n2;
|
||||
|
||||
if ((free_cnt = ringbuffer_read_space (rb)) == 0)
|
||||
return 0;
|
||||
|
||||
to_read = cnt > free_cnt ? free_cnt : cnt;
|
||||
|
||||
cnt2 = rb->read_ptr + to_read;
|
||||
|
||||
if (cnt2 > rb->size)
|
||||
{
|
||||
n1 = rb->size - rb->read_ptr;
|
||||
n2 = cnt2 & rb->size_mask;
|
||||
}
|
||||
else
|
||||
{
|
||||
n1 = to_read;
|
||||
n2 = 0;
|
||||
}
|
||||
|
||||
memcpy (dest, &(rb->buf[rb->read_ptr]), n1);
|
||||
rb->read_ptr += n1;
|
||||
rb->read_ptr &= rb->size_mask;
|
||||
|
||||
if (n2)
|
||||
{
|
||||
memcpy (dest + n1, &(rb->buf[rb->read_ptr]), n2);
|
||||
rb->read_ptr += n2;
|
||||
rb->read_ptr &= rb->size_mask;
|
||||
}
|
||||
|
||||
return to_read;
|
||||
}
|
||||
|
||||
/* The copying data writer. Copy at most `cnt' bytes to `rb' from
|
||||
* `src'. Returns the actual number of bytes copied.
|
||||
*/
|
||||
size_t ringbuffer_write (ringbuffer_t * rb, char *src, size_t cnt)
|
||||
{
|
||||
size_t free_cnt;
|
||||
size_t cnt2;
|
||||
size_t to_write;
|
||||
size_t n1, n2;
|
||||
|
||||
if ((free_cnt = ringbuffer_write_space (rb)) == 0)
|
||||
return 0;
|
||||
|
||||
to_write = cnt > free_cnt ? free_cnt : cnt;
|
||||
|
||||
cnt2 = rb->write_ptr + to_write;
|
||||
|
||||
if (cnt2 > rb->size) {
|
||||
n1 = rb->size - rb->write_ptr;
|
||||
n2 = cnt2 & rb->size_mask;
|
||||
}
|
||||
else
|
||||
{
|
||||
n1 = to_write;
|
||||
n2 = 0;
|
||||
}
|
||||
|
||||
memcpy (&(rb->buf[rb->write_ptr]), src, n1);
|
||||
rb->write_ptr += n1;
|
||||
rb->write_ptr &= rb->size_mask;
|
||||
|
||||
if (n2)
|
||||
{
|
||||
memcpy (&(rb->buf[rb->write_ptr]), src + n1, n2);
|
||||
rb->write_ptr += n2;
|
||||
rb->write_ptr &= rb->size_mask;
|
||||
}
|
||||
|
||||
return to_write;
|
||||
}
|
||||
|
||||
/* Advance the read pointer `cnt' places.
|
||||
*/
|
||||
void ringbuffer_read_advance (ringbuffer_t * rb, size_t cnt)
|
||||
{
|
||||
rb->read_ptr += cnt;
|
||||
rb->read_ptr &= rb->size_mask;
|
||||
}
|
||||
|
||||
/* Advance the write pointer `cnt' places.
|
||||
*/
|
||||
void ringbuffer_write_advance (ringbuffer_t * rb, size_t cnt)
|
||||
{
|
||||
rb->write_ptr += cnt;
|
||||
rb->write_ptr &= rb->size_mask;
|
||||
}
|
||||
|
||||
/* The non-copying data reader. `vec' is an array of two places. Set
|
||||
* the values at `vec' to hold the current readable data at `rb'. If
|
||||
* the readable data is in one segment the second segment has zero
|
||||
* length.
|
||||
*/
|
||||
void ringbuffer_get_read_vector (ringbuffer_t * rb, ringbuffer_data_t * vec)
|
||||
{
|
||||
size_t free_cnt;
|
||||
size_t cnt2;
|
||||
size_t w, r;
|
||||
|
||||
w = rb->write_ptr;
|
||||
r = rb->read_ptr;
|
||||
|
||||
if (w > r)
|
||||
free_cnt = w - r;
|
||||
else
|
||||
free_cnt = (w - r + rb->size) & rb->size_mask;
|
||||
|
||||
cnt2 = r + free_cnt;
|
||||
|
||||
if (cnt2 > rb->size)
|
||||
{
|
||||
/* Two part vector: the rest of the buffer after the current write
|
||||
* ptr, plus some from the start of the buffer.
|
||||
*/
|
||||
vec[0].buf = &(rb->buf[r]);
|
||||
vec[0].len = rb->size - r;
|
||||
vec[1].buf = rb->buf;
|
||||
vec[1].len = cnt2 & rb->size_mask;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Single part vector: just the rest of the buffer */
|
||||
vec[0].buf = &(rb->buf[r]);
|
||||
vec[0].len = free_cnt;
|
||||
vec[1].len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* The non-copying data writer. `vec' is an array of two places. Set
|
||||
* the values at `vec' to hold the current writeable data at `rb'. If
|
||||
* the writeable data is in one segment the second segment has zero
|
||||
* length.
|
||||
*/
|
||||
void ringbuffer_get_write_vector (ringbuffer_t * rb, ringbuffer_data_t * vec)
|
||||
{
|
||||
size_t free_cnt;
|
||||
size_t cnt2;
|
||||
size_t w, r;
|
||||
|
||||
w = rb->write_ptr;
|
||||
r = rb->read_ptr;
|
||||
|
||||
if (w > r)
|
||||
free_cnt = ((r - w + rb->size) & rb->size_mask) - 1;
|
||||
else if (w < r)
|
||||
free_cnt = (r - w) - 1;
|
||||
else
|
||||
free_cnt = rb->size - 1;
|
||||
|
||||
cnt2 = w + free_cnt;
|
||||
|
||||
if (cnt2 > rb->size)
|
||||
{
|
||||
/* Two part vector: the rest of the buffer after the current write
|
||||
* ptr, plus some from the start of the buffer.
|
||||
*/
|
||||
vec[0].buf = &(rb->buf[w]);
|
||||
vec[0].len = rb->size - w;
|
||||
vec[1].buf = rb->buf;
|
||||
vec[1].len = cnt2 & rb->size_mask;
|
||||
}
|
||||
else
|
||||
{
|
||||
vec[0].buf = &(rb->buf[w]);
|
||||
vec[0].len = free_cnt;
|
||||
vec[1].len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Get read pointer at most `cnt' bytes from `rb' to
|
||||
`dest'. Returns the actual readable number of bytes . */
|
||||
size_t ringbuffer_get_readpointer (ringbuffer_t * rb, char **dest, size_t cnt)
|
||||
{
|
||||
size_t free_cnt;
|
||||
size_t cnt2;
|
||||
size_t to_read;
|
||||
size_t n1, n2;
|
||||
size_t tmp_read_ptr = rb->read_ptr;
|
||||
|
||||
if ((free_cnt = ringbuffer_read_space (rb)) == 0)
|
||||
return 0;
|
||||
|
||||
to_read = cnt > free_cnt ? free_cnt : cnt;
|
||||
|
||||
cnt2 = rb->read_ptr + to_read;
|
||||
|
||||
if (cnt2 > rb->size)
|
||||
{
|
||||
n1 = rb->size - rb->read_ptr;
|
||||
n2 = cnt2 & rb->size_mask;
|
||||
}
|
||||
else
|
||||
{
|
||||
n1 = to_read;
|
||||
n2 = 0;
|
||||
}
|
||||
if (n2)
|
||||
{
|
||||
if (to_read > rb->helpbufsize)
|
||||
{
|
||||
rb->helpbufsize = to_read;
|
||||
rb->helpbuf = realloc (rb->helpbuf, rb->helpbufsize);
|
||||
}
|
||||
memcpy (rb->helpbuf, &(rb->buf[rb->read_ptr]), n1);
|
||||
tmp_read_ptr += n1;
|
||||
tmp_read_ptr &= rb->size_mask;
|
||||
memcpy (rb->helpbuf + n1, &(rb->buf[tmp_read_ptr]), n2);
|
||||
*dest = rb->helpbuf;
|
||||
}
|
||||
else
|
||||
*dest = &(rb->buf[rb->read_ptr]);
|
||||
|
||||
return to_read;
|
||||
}
|
||||
|
||||
|
||||
/* Get write pointer at most `cnt' bytes to `rb' from
|
||||
`src'. Returns the actual number of bytes can insert. */
|
||||
size_t ringbuffer_get_writepointer (ringbuffer_t * rb, char **src, size_t cnt)
|
||||
{
|
||||
size_t free_cnt;
|
||||
size_t cnt2;
|
||||
size_t to_write;
|
||||
|
||||
if ((free_cnt = ringbuffer_write_space (rb)) == 0)
|
||||
return 0;
|
||||
|
||||
to_write = cnt > free_cnt ? free_cnt : cnt;
|
||||
|
||||
cnt2 = rb->write_ptr + to_write;
|
||||
|
||||
if (cnt2 > rb->size)
|
||||
return 0;
|
||||
else
|
||||
*src = &(rb->buf[rb->write_ptr]);
|
||||
|
||||
return to_write;
|
||||
}
|
45
src/driver/ringbuffer.h
Normal file
45
src/driver/ringbuffer.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef _RINGBUFFER_H
|
||||
#define _RINGBUFFER_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *buf;
|
||||
size_t len;
|
||||
} ringbuffer_data_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *buf;
|
||||
volatile size_t write_ptr;
|
||||
volatile size_t read_ptr;
|
||||
size_t size;
|
||||
size_t size_mask;
|
||||
int mlocked;
|
||||
char *helpbuf;
|
||||
size_t helpbufsize;
|
||||
} ringbuffer_t;
|
||||
|
||||
ringbuffer_t *ringbuffer_create(int sz);
|
||||
void ringbuffer_free(ringbuffer_t *rb);
|
||||
|
||||
int ringbuffer_mlock(ringbuffer_t *rb);
|
||||
void ringbuffer_reset(ringbuffer_t *rb);
|
||||
|
||||
void ringbuffer_write_advance(ringbuffer_t *rb, size_t cnt);
|
||||
void ringbuffer_read_advance(ringbuffer_t *rb, size_t cnt);
|
||||
|
||||
size_t ringbuffer_write_space(ringbuffer_t *rb);
|
||||
size_t ringbuffer_read_space(ringbuffer_t *rb);
|
||||
|
||||
size_t ringbuffer_read(ringbuffer_t *rb, char *dest, size_t cnt);
|
||||
size_t ringbuffer_write(ringbuffer_t *rb, char *src, size_t cnt);
|
||||
|
||||
void ringbuffer_get_read_vector(ringbuffer_t *rb, ringbuffer_data_t *vec);
|
||||
void ringbuffer_get_write_vector(ringbuffer_t *rb, ringbuffer_data_t *vec);
|
||||
|
||||
size_t ringbuffer_get_readpointer(ringbuffer_t * rb, char **dest, size_t cnt);
|
||||
size_t ringbuffer_get_writepointer(ringbuffer_t * rb, char **src, size_t cnt);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user