]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/chan.c
Major changes. Added metering and power spectrum, other fixes. Rearranged headers...
[dttsp.git] / jDttSP / chan.c
1 /* chan.c
2
3 fast inter-user-process single-reader/single-writer channels
4 using a modified version of jack ringbuffers and memory-mapped files.
5    
6 This file is part of a program that implements a Software-Defined Radio.
7
8 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 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 General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24 The authors can be reached by email at
25
26 ab2kt@arrl.net
27 or
28 rwmcgwier@comcast.net
29
30 or by paper mail at
31
32 The DTTS Microwave Society
33 6 Kathleen Place
34 Bridgewater, NJ 08807
35 */
36
37 #include <chan.h>
38
39 size_t
40 putChan(Chan c, char *data, size_t size) {
41   return ringb_write(c->rb, data, size);
42 }
43
44 size_t
45 getChan(Chan c, char *data, size_t size) {
46   return ringb_read(c->rb, data, size);
47 }
48
49 BOOLEAN
50 putChan_nowait(Chan c, char *data, size_t size) {
51   if (ringb_write_space(c->rb) >= size) {
52     ringb_write(c->rb, data, size);
53     return TRUE;
54   } else return FALSE;
55 }
56
57 size_t
58 putChan_force(Chan c, char *data, size_t size) {
59   if (ringb_write_space(c->rb) >= size) {
60     ringb_write(c->rb, data, size);
61     return size;
62   } else {
63     ringb_reset(c->rb);
64     return ringb_write(c->rb, data, size);
65   }
66 }
67
68 BOOLEAN
69 getChan_nowait(Chan c, char *data, size_t size) {
70   if (ringb_read_space(c->rb) >= size) {
71     ringb_read(c->rb, data, size);
72     return TRUE;
73   } else return FALSE;
74 }
75
76 void
77 resetChan(Chan c) { ringb_reset(c->rb); }
78
79 Chan
80 openChan(char *path, size_t want) {
81   Chan c = (Chan) safealloc(sizeof(ChanDesc), 1, "Chan header");
82   c->size.rib = sizeof(ringb_t);
83   c->size.buf = nblock2(want);
84   c->file.path = path;
85   if ((c->file.desc = open(c->file.path, O_RDWR)) == -1) {
86     fprintf(stderr, "can't open Chan file %s\n", c->file.path);
87     exit(1);
88   }
89   c->size.tot = c->size.rib + c->size.buf;
90   if ((c->size.tru = fdsize(c->file.desc)) < c->size.tot) {
91     fprintf(stderr,
92             "Chan file %s is too small (%d) for required size (%s)\n",
93             c->file.path, c->size.tru, c->size.tot);
94     exit(1);
95   }
96   if (!(c->map.base = (char *) mmap(0,
97                                     c->size.tot,
98                                     PROT_READ | PROT_WRITE,
99                                     MAP_SHARED,
100                                     c->file.desc,
101                                     0))) {
102     fprintf(stderr, "can't memory map %s for Chan\n",
103             c->file.path);
104     exit(1);
105   }
106   if (!(c->rb = ringb_create(c->map.base, c->size.buf))) {
107     fprintf(stderr, "can't map RB for Chan\n");
108     exit(1);
109   }
110   return c;
111 }
112
113 void
114 closeChan(Chan c) {
115   if (c) {
116     close(c->file.desc);
117     munmap(c->map.base, c->size.tot);
118     safefree((char *) c);
119   }
120 }