1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
|
/****************************************************************************
* drivers/pipes/pipe_common.c
*
* Copyright (C) 2008-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <semaphore.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/fs.h>
#if CONFIG_DEBUG
# include <nuttx/arch.h>
#endif
#include "pipe_common.h"
#if CONFIG_DEV_PIPE_SIZE > 0
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* CONFIG_DEV_PIPEDUMP will dump the contents of each transfer into and out
* of the pipe.
*/
#ifdef CONFIG_DEV_PIPEDUMP
# define pipe_dumpbuffer(m,a,n) lib_dumpbuffer(m,a,n)
#else
# define pipe_dumpbuffer(m,a,n)
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void pipecommon_semtake(sem_t *sem);
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: pipecommon_semtake
****************************************************************************/
static void pipecommon_semtake(sem_t *sem)
{
while (sem_wait(sem) != 0)
{
/* The only case that an error should occur here is if the wait was
* awakened by a signal.
*/
ASSERT(errno == EINTR);
}
}
/****************************************************************************
* Name: pipecommon_pollnotify
****************************************************************************/
#ifndef CONFIG_DISABLE_POLL
static void pipecommon_pollnotify(FAR struct pipe_dev_s *dev, pollevent_t eventset)
{
int i;
for (i = 0; i < CONFIG_DEV_PIPE_NPOLLWAITERS; i++)
{
struct pollfd *fds = dev->d_fds[i];
if (fds)
{
fds->revents |= (fds->events & eventset);
if (fds->revents != 0)
{
fvdbg("Report events: %02x\n", fds->revents);
sem_post(fds->sem);
}
}
}
}
#else
# define pipecommon_pollnotify(dev,event)
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pipecommon_allocdev
****************************************************************************/
FAR struct pipe_dev_s *pipecommon_allocdev(void)
{
struct pipe_dev_s *dev;
/* Allocate a private structure to manage the pipe */
dev = (struct pipe_dev_s *)malloc(sizeof(struct pipe_dev_s));
if (dev)
{
/* Initialize the private structure */
memset(dev, 0, sizeof(struct pipe_dev_s));
sem_init(&dev->d_bfsem, 0, 1);
sem_init(&dev->d_rdsem, 0, 0);
sem_init(&dev->d_wrsem, 0, 0);
}
return dev;
}
/****************************************************************************
* Name: pipecommon_freedev
****************************************************************************/
void pipecommon_freedev(FAR struct pipe_dev_s *dev)
{
sem_destroy(&dev->d_bfsem);
sem_destroy(&dev->d_rdsem);
sem_destroy(&dev->d_wrsem);
free(dev);
}
/****************************************************************************
* Name: pipecommon_open
****************************************************************************/
int pipecommon_open(FAR struct file *filep)
{
struct inode *inode = filep->f_inode;
struct pipe_dev_s *dev = inode->i_private;
int sval;
int ret;
/* Some sanity checking */
#if CONFIG_DEBUG
if (!dev)
{
return -EBADF;
}
#endif
/* Make sure that we have exclusive access to the device structure. The
* sem_wait() call should fail only if we are awakened by a signal.
*/
ret = sem_wait(&dev->d_bfsem);
if (ret != OK)
{
fdbg("sem_wait failed: %d\n", errno);
DEBUGASSERT(errno > 0);
return -errno;
}
/* If this the first reference on the device, then allocate the buffer */
if (dev->d_refs == 0)
{
dev->d_buffer = (uint8_t*)malloc(CONFIG_DEV_PIPE_SIZE);
if (!dev->d_buffer)
{
(void)sem_post(&dev->d_bfsem);
return -ENOMEM;
}
}
/* Increment the reference count on the pipe instance */
dev->d_refs++;
/* If opened for writing, increment the count of writers on on the pipe instance */
if ((filep->f_oflags & O_WROK) != 0)
{
dev->d_nwriters++;
/* If this this is the first writer, then the read semaphore indicates the
* number of readers waiting for the first writer. Wake them all up.
*/
if (dev->d_nwriters == 1)
{
while (sem_getvalue(&dev->d_rdsem, &sval) == 0 && sval < 0)
{
sem_post(&dev->d_rdsem);
}
}
}
/* If opened for read-only, then wait for at least one writer on the pipe */
sched_lock();
(void)sem_post(&dev->d_bfsem);
if ((filep->f_oflags & O_RDWR) == O_RDONLY && dev->d_nwriters < 1)
{
/* NOTE: d_rdsem is normally used when the read logic waits for more
* data to be written. But until the first writer has opened the
* pipe, the meaning is different: it is used prevent O_RDONLY open
* calls from returning until there is at least one writer on the pipe.
* This is required both by spec and also because it prevents
* subsequent read() calls from returning end-of-file because there is
* no writer on the pipe.
*/
ret = sem_wait(&dev->d_rdsem);
if (ret != OK)
{
/* The sem_wait() call should fail only if we are awakened by
* a signal.
*/
fdbg("sem_wait failed: %d\n", errno);
DEBUGASSERT(errno > 0);
ret = -errno;
/* Immediately close the pipe that we just opened */
(void)pipecommon_close(filep);
}
}
sched_unlock();
return ret;
}
/****************************************************************************
* Name: pipecommon_close
****************************************************************************/
int pipecommon_close(FAR struct file *filep)
{
struct inode *inode = filep->f_inode;
struct pipe_dev_s *dev = inode->i_private;
int sval;
/* Some sanity checking */
#if CONFIG_DEBUG
if (!dev)
{
return -EBADF;
}
#endif
/* Make sure that we have exclusive access to the device structure.
* NOTE: close() is supposed to return EINTR if interrupted, however
* I've never seen anyone check that.
*/
pipecommon_semtake(&dev->d_bfsem);
/* Check if the decremented reference count would go to zero */
if (dev->d_refs > 1)
{
/* No.. then just decrement the reference count */
dev->d_refs--;
/* If opened for writing, decrement the count of writers on on the pipe instance */
if ((filep->f_oflags & O_WROK) != 0)
{
/* If there are no longer any writers on the pipe, then notify all of the
* waiting readers that they must return end-of-file.
*/
if (--dev->d_nwriters <= 0)
{
while (sem_getvalue(&dev->d_rdsem, &sval) == 0 && sval < 0)
{
sem_post(&dev->d_rdsem);
}
}
}
}
else
{
/* Yes... deallocate the buffer */
free(dev->d_buffer);
dev->d_buffer = NULL;
/* And reset all counts and indices */
dev->d_wrndx = 0;
dev->d_rdndx = 0;
dev->d_refs = 0;
dev->d_nwriters = 0;
}
sem_post(&dev->d_bfsem);
return OK;
}
/****************************************************************************
* Name: pipecommon_read
****************************************************************************/
ssize_t pipecommon_read(FAR struct file *filep, FAR char *buffer, size_t len)
{
struct inode *inode = filep->f_inode;
struct pipe_dev_s *dev = inode->i_private;
#ifdef CONFIG_DEV_PIPEDUMP
FAR uint8_t *start = (uint8_t*)buffer;
#endif
ssize_t nread = 0;
int sval;
int ret;
/* Some sanity checking */
#if CONFIG_DEBUG
if (!dev)
{
return -ENODEV;
}
#endif
/* Make sure that we have exclusive access to the device structure */
if (sem_wait(&dev->d_bfsem) < 0)
{
return ERROR;
}
/* If the pipe is empty, then wait for something to be written to it */
while (dev->d_wrndx == dev->d_rdndx)
{
/* If O_NONBLOCK was set, then return EGAIN */
if (filep->f_oflags & O_NONBLOCK)
{
sem_post(&dev->d_bfsem);
return -EAGAIN;
}
/* If there are no writers on the pipe, then return end of file */
if (dev->d_nwriters <= 0)
{
sem_post(&dev->d_bfsem);
return 0;
}
/* Otherwise, wait for something to be written to the pipe */
sched_lock();
sem_post(&dev->d_bfsem);
ret = sem_wait(&dev->d_rdsem);
sched_unlock();
if (ret < 0 || sem_wait(&dev->d_bfsem) < 0)
{
return ERROR;
}
}
/* Then return whatever is available in the pipe (which is at least one byte) */
nread = 0;
while (nread < len && dev->d_wrndx != dev->d_rdndx)
{
*buffer++ = dev->d_buffer[dev->d_rdndx];
if (++dev->d_rdndx >= CONFIG_DEV_PIPE_SIZE)
{
dev->d_rdndx = 0;
}
nread++;
}
/* Notify all waiting writers that bytes have been removed from the buffer */
while (sem_getvalue(&dev->d_wrsem, &sval) == 0 && sval < 0)
{
sem_post(&dev->d_wrsem);
}
/* Notify all poll/select waiters that they can write to the FIFO */
pipecommon_pollnotify(dev, POLLOUT);
sem_post(&dev->d_bfsem);
pipe_dumpbuffer("From PIPE:", start, nread);
return nread;
}
/****************************************************************************
* Name: pipecommon_write
****************************************************************************/
ssize_t pipecommon_write(FAR struct file *filep, FAR const char *buffer, size_t len)
{
struct inode *inode = filep->f_inode;
struct pipe_dev_s *dev = inode->i_private;
ssize_t nwritten = 0;
ssize_t last;
int nxtwrndx;
int sval;
/* Some sanity checking */
#if CONFIG_DEBUG
if (!dev)
{
return -ENODEV;
}
#endif
pipe_dumpbuffer("To PIPE:", (uint8_t*)buffer, len);
/* At present, this method cannot be called from interrupt handlers. That is
* because it calls sem_wait (via pipecommon_semtake below) and sem_wait cannot
* be called from interrupt level. This actually happens fairly commonly
* IF dbg() is called from interrupt handlers and stdout is being redirected
* via a pipe. In that case, the debug output will try to go out the pipe
* (interrupt handlers should use the lldbg() APIs).
*
* On the other hand, it would be very valuable to be able to feed the pipe
* from an interrupt handler! TODO: Consider disabling interrupts instead
* of taking semaphores so that pipes can be written from interupt handlers
*/
DEBUGASSERT(up_interrupt_context() == false)
/* Make sure that we have exclusive access to the device structure */
if (sem_wait(&dev->d_bfsem) < 0)
{
return ERROR;
}
/* Loop until all of the bytes have been written */
last = 0;
for (;;)
{
/* Calculate the write index AFTER the next byte is written */
nxtwrndx = dev->d_wrndx + 1;
if (nxtwrndx >= CONFIG_DEV_PIPE_SIZE)
{
nxtwrndx = 0;
}
/* Would the next write overflow the circular buffer? */
if (nxtwrndx != dev->d_rdndx)
{
/* No... copy the byte */
dev->d_buffer[dev->d_wrndx] = *buffer++;
dev->d_wrndx = nxtwrndx;
/* Is the write complete? */
if (++nwritten >= len)
{
/* Yes.. Notify all of the waiting readers that more data is available */
while (sem_getvalue(&dev->d_rdsem, &sval) == 0 && sval < 0)
{
sem_post(&dev->d_rdsem);
}
/* Notify all poll/select waiters that they can write to the FIFO */
pipecommon_pollnotify(dev, POLLIN);
/* Return the number of bytes written */
sem_post(&dev->d_bfsem);
return len;
}
}
else
{
/* There is not enough room for the next byte. Was anything written in this pass? */
if (last < nwritten)
{
/* Yes.. Notify all of the waiting readers that more data is available */
while (sem_getvalue(&dev->d_rdsem, &sval) == 0 && sval < 0)
{
sem_post(&dev->d_rdsem);
}
}
last = nwritten;
/* If O_NONBLOCK was set, then return partial bytes written or EGAIN */
if (filep->f_oflags & O_NONBLOCK)
{
if (nwritten == 0)
{
nwritten = -EAGAIN;
}
sem_post(&dev->d_bfsem);
return nwritten;
}
/* There is more to be written.. wait for data to be removed from the pipe */
sched_lock();
sem_post(&dev->d_bfsem);
pipecommon_semtake(&dev->d_wrsem);
sched_unlock();
pipecommon_semtake(&dev->d_bfsem);
}
}
}
/****************************************************************************
* Name: pipecommon_poll
****************************************************************************/
#ifndef CONFIG_DISABLE_POLL
int pipecommon_poll(FAR struct file *filep, FAR struct pollfd *fds,
bool setup)
{
FAR struct inode *inode = filep->f_inode;
FAR struct pipe_dev_s *dev = inode->i_private;
pollevent_t eventset;
pipe_ndx_t nbytes;
int ret = OK;
int i;
/* Some sanity checking */
#if CONFIG_DEBUG
if (!dev || !fds)
{
return -ENODEV;
}
#endif
/* Are we setting up the poll? Or tearing it down? */
pipecommon_semtake(&dev->d_bfsem);
if (setup)
{
/* This is a request to set up the poll. Find an available
* slot for the poll structure reference
*/
for (i = 0; i < CONFIG_DEV_PIPE_NPOLLWAITERS; i++)
{
/* Find an available slot */
if (!dev->d_fds[i])
{
/* Bind the poll structure and this slot */
dev->d_fds[i] = fds;
fds->priv = &dev->d_fds[i];
break;
}
}
if (i >= CONFIG_DEV_PIPE_NPOLLWAITERS)
{
fds->priv = NULL;
ret = -EBUSY;
goto errout;
}
/* Should immediately notify on any of the requested events?
* First, determine how many bytes are in the buffer
*/
if (dev->d_wrndx >= dev->d_rdndx)
{
nbytes = dev->d_wrndx - dev->d_rdndx;
}
else
{
nbytes = (CONFIG_DEV_PIPE_SIZE-1) + dev->d_wrndx - dev->d_rdndx;
}
/* Notify the POLLOUT event if the pipe is not full */
eventset = 0;
if (nbytes < (CONFIG_DEV_PIPE_SIZE-1))
{
eventset |= POLLOUT;
}
/* Notify the POLLIN event if the pipe is not empty */
if (nbytes > 0)
{
eventset |= POLLIN;
}
if (eventset)
{
pipecommon_pollnotify(dev, eventset);
}
}
else
{
/* This is a request to tear down the poll. */
struct pollfd **slot = (struct pollfd **)fds->priv;
#ifdef CONFIG_DEBUG
if (!slot)
{
ret = -EIO;
goto errout;
}
#endif
/* Remove all memory of the poll setup */
*slot = NULL;
fds->priv = NULL;
}
errout:
sem_post(&dev->d_bfsem);
return ret;
}
#endif
#endif /* CONFIG_DEV_PIPE_SIZE > 0 */
|