# rtai_fifos.h

Go to the documentation of this file.

    00001
    00026 #ifndef _RTAI_FIFOS_H
    00027 #define _RTAI_FIFOS_H
    00028
    00029 #include <rtai_types.h>
    00030
    00031 #define MAX_FIFOS 64
    00032
    00033 #define RTAI_FIFOS_MAJOR 150
    00034
    00035 #define RESET                   1
    00036 #define RESIZE                  2
    00037 #define RTF_SUSPEND_TIMED       3
    00038 #define OPEN_SIZED              4
    00039 #define READ_ALL_AT_ONCE        5
    00040 #define READ_TIMED              6
    00041 #define WRITE_TIMED             7
    00042 #define RTF_SEM_INIT            8
    00043 #define RTF_SEM_WAIT            9
    00044 #define RTF_SEM_TRYWAIT         10
    00045 #define RTF_SEM_TIMED_WAIT      11
    00046 #define RTF_SEM_POST            12
    00047 #define RTF_SEM_DESTROY         13
    00048 #define SET_ASYNC_SIG           14
    00049 #define EAVESDROP               19
    00050 #define OVRWRITE                20
    00051 #define READ_IF                 21
    00052 #define WRITE_IF                22
    00053 #define RTF_NAMED_CREATE        23
    00054
    00055 #define RTF_GET_N_FIFOS         15
    00056 #define RTF_GET_FIFO_INFO       16
    00057 #define RTF_CREATE_NAMED        17
    00058 #define RTF_NAME_LOOKUP         18
    00059
    00060 #define RTF_NAMELEN 15
    00061
    00062 struct rt_fifo_info_struct{
    00063         unsigned int fifo_number;
    00064         unsigned int size;
    00065         unsigned int opncnt;
    00066         char name[RTF_NAMELEN+1];
    00067 };
    00068
    00069 struct rt_fifo_get_info_struct{
    00070         unsigned int fifo;
    00071         unsigned int n;
    00072         struct rt_fifo_info_struct *ptr;
    00073 };
    00074
    00075 #define  FUN_FIFOS_LXRT_INDX 10
    00076
    00077 #define _CREATE         0
    00078 #define _DESTROY        1
    00079 #define _PUT            2
    00080 #define _GET            3
    00081 #define _RESET          4
    00082 #define _RESIZE         5
    00083 #define _SEM_INIT       6
    00084 #define _SEM_DESTRY     7
    00085 #define _SEM_POST       8
    00086 #define _SEM_TRY        9
    00087 #define _CREATE_NAMED  10
    00088 #define _GETBY_NAME    11
    00089 #define _OVERWRITE     12
    00090 #define _PUT_IF        13
    00091 #define _GET_IF        14
    00092 #define _NAMED_CREATE  15
    00093
    00094 #ifdef __KERNEL__
    00095
    00096 #ifdef __cplusplus
    00097 extern "C" {
    00098 #endif /* __cplusplus */
    00099
    00100 int __rtai_fifos_init(void);
    00101
    00102 void __rtai_fifos_exit(void);
    00103
    00104 int rtf_init(void);
    00105
    00106 /* Attach a handler to an RT-FIFO.
    00107  *
    00108  * Allow function handler to be called when a user process reads or writes to
    00109  * the FIFO. When the function is called, it is passed the fifo number as the
    00110  * argument.
    00111  */
    00112
    00113 int rtf_create_handler(unsigned int fifo,       /* RT-FIFO */
    00114                        int (*handler)(unsigned int fifo)        /* function to be called */);
    00115
    00116
    00129 #define X_FIFO_HANDLER(handler) ((int (*)(unsigned int))(handler))
    00130
    00131 /* Create an RT-FIFO.
    00132  *
    00133  * An RT-FIFO fifo is created with initial size of size.
    00134  * Return value: On success, 0 is returned. On error, -1 is returned.
    00135  */
    00136 #undef rtf_create
    00137 int rtf_create(unsigned int fifo, int size);
    00138
    00139 /* Create an RT-FIFO with a name and size.
    00140  *
    00141  * An RT-FIFO is created with a name of name, it will be allocated
    00142  * the first unused minor number and will have a user assigned size.
    00143  * Return value: On success, the allocated minor number is returned.
    00144  *               On error, -errno is returned.
    00145  */
    00146
    00147 int rtf_named_create(const char *name, int size);
    00148
    00149 /* Create an RT-FIFO with a name.
    00150  *
    00151  * An RT-FIFO is created with a name of name, it will be allocated
    00152  * the first unused minor number and will have a default size.
    00153  * Return value: On success, the allocated minor number is returned.
    00154  *               On error, -errno is returned.
    00155  */
    00156
    00157 int rtf_create_named(const char *name);
    00158
    00159 /* Look up a named RT-FIFO.
    00160  *
    00161  * Find the RT-FIFO with the name name.
    00162  * Return value: On success, the minor number is returned.
    00163  *               On error, -errno is returned.
    00164  */
    00165
    00166 int rtf_getfifobyname(const char *name);
    00167
    00168 /* Reset an RT-FIFO.
    00169  *
    00170  * An RT-FIFO fifo is reset by setting its buffer pointers to zero, so
    00171  * that any existing data is discarded and the fifo started anew like at its
    00172  * creation.
    00173  */
    00174
    00175 int rtf_reset(unsigned int fifo);
    00176
    00177 /* destroy an RT-FIFO.
    00178  *
    00179  * Return value: On success, 0 is returned.
    00180  */
    00181
    00182 int rtf_destroy(unsigned int fifo);
    00183
    00184
    00185 /* Resize an RT-FIFO.
    00186  *
    00187  * Return value: size is returned on success. On error, a negative value
    00188  * is returned.
    00189  */
    00190
    00191 int rtf_resize(unsigned int minor, int size);
    00192
    00193
    00194 /* Write to an RT-FIFO.
    00195  *
    00196  * Try to write count bytes to an FIFO. Returns the number of bytes written.
    00197  */
    00198
    00199 int rtf_put(unsigned int fifo,  /* RT-FIFO */
    00200             void * buf,         /* buffer address */
    00201             int count           /* number of bytes to write */);
    00202
    00203
    00204
    00205 /* Write to an RT-FIFO, over writing if there is not enough space.
    00206  *
    00207  * Try to write count bytes to an FIFO. Returns 0.
    00208  */
    00209
    00210 int rtf_ovrwr_put(unsigned int fifo,    /* RT-FIFO */
    00211                   void * buf,           /* buffer address */
    00212                   int count             /* number of bytes to write */);
    00213
    00214
    00215
    00216 /* Write atomically to an RT-FIFO.
    00217  *
    00218  * Try to write count bytes in block to an FIFO. Returns the number of bytes
    00219  * written.
    00220  */
    00221
    00222 extern int rtf_put_if (unsigned int fifo,       /* RT-FIFO */
    00223                 void * buf,                     /* buffer address */
    00224                 int count                       /* number of bytes to write */);
    00225
    00226 /* Read from an RT-FIFO.
    00227  *
    00228  * Try to read count bytes from a FIFO. Returns the number of bytes read.
    00229  */
    00230
    00231 int rtf_get(unsigned int fifo,  /* RT-FIFO */
    00232             void * buf,                 /* buffer address */
    00233             int count           /* number of bytes to read */);
    00234
    00235
    00236 /* Atomically read from an RT-FIFO.
    00237  *
    00238  * Try to read count bytes in a block from an FIFO. Returns the number of bytes read.
    00239  */
    00240
    00241 int rtf_get_if(unsigned int fifo,       /* RT-FIFO */
    00242             void * buf,                 /* buffer address */
    00243             int count           /* number of bytes to read */);
    00244
    00245
    00246 /*
    00247  * Preview the an RT-FIFO content.
    00248  */
    00249
    00250 int rtf_evdrp(unsigned int fifo,        /* RT-FIFO */
    00251               void * buf,               /* buffer address */
    00252               int count         /* number of bytes to read */);
    00253
    00254 /* Open an RT-FIFO semaphore.
    00255  *
    00256  */
    00257
    00258 int rtf_sem_init(unsigned int fifo,     /* RT-FIFO */
    00259                  int value                      /* initial semaphore value */);
    00260
    00261
    00262 /* Post to an RT-FIFO semaphore.
    00263  *
    00264  */
    00265
    00266 int rtf_sem_post(unsigned int fifo      /* RT-FIFO */);
    00267
    00268
    00269 /* Try to acquire an RT-FIFO semaphore.
    00270  *
    00271  */
    00272
    00273 int rtf_sem_trywait(unsigned int fifo   /* RT-FIFO */);
    00274
    00275
    00276 /* Destroy an RT-FIFO semaphore.
    00277  *
    00278  */
    00279
    00280 int rtf_sem_destroy(unsigned int fifo   /* RT-FIFO */);
    00281
    00282 #define rtf_sem_delete rtf_sem_destroy
    00283
    00284 /* Just for compatibility with earlier rtai_fifos releases. No more bh and user
    00285 buffers. Fifos are now awakened immediately and buffers > 128K are vmalloced */
    00286
    00287 #define rtf_create_using_bh(fifo, size, bh_list) rtf_create(fifo, size)
    00288 #define rtf_create_using_bh_and_usr_buf(fifo, buf, size, bh_list) rtf_create(fifo, size)
    00289 #define rtf_destroy_using_usr_buf(fifo) rtf_destroy(fifo)
    00290
    00291 #ifdef __cplusplus
    00292 }
    00293 #endif /* __cplusplus */
    00294
    00295 #else  /* !__KERNEL__ */
    00296
    00297 #include <sys/types.h>
    00298 #include <sys/stat.h>
    00299 #include <sys/ioctl.h>
    00300 #include <fcntl.h>
    00301 #include <unistd.h>
    00302 #include <stdio.h>
    00303 #include <string.h>
    00304 #include <rtai_lxrt.h>
    00305
    00306 #ifdef __cplusplus
    00307 extern "C" {
    00308 #endif /* __cplusplus */
    00309
    00310 RTAI_PROTO(int, rtf_create,(unsigned int fifo, int size))
    00311 {
    00312         struct { unsigned int fifo, size; } arg = { fifo, size };
    00313         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _CREATE, &arg).i[LOW];
    00314 }
    00315
    00316 RTAI_PROTO(int, rtf_destroy,(unsigned int fifo))
    00317 {
    00318         struct { unsigned int fifo; } arg = { fifo };
    00319         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _DESTROY, &arg).i[LOW];
    00320 }
    00321
    00322 RTAI_PROTO(int, rtf_put,(unsigned int fifo, const void *buf, int count))
    00323 {
    00324         char lbuf[count];
    00325         struct { unsigned int fifo; void *buf; int count; } arg = { fifo, lbuf, count };
    00326         memcpy(lbuf, buf, count);
    00327         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _PUT, &arg).i[LOW];
    00328 }
    00329
    00330 RTAI_PROTO(int, rtf_put_if,(unsigned int fifo, const void *buf, int count))
    00331 {
    00332         char lbuf[count];
    00333         struct { unsigned int fifo; void *buf; int count; } arg = { fifo, lbuf, count };
    00334         memcpy(lbuf, buf, count);
    00335         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _PUT_IF, &arg).i[LOW];
    00336 }
    00337
    00338 RTAI_PROTO(int, rtf_get,(unsigned int fifo, void *buf, int count))
    00339 {
    00340         int retval;
    00341         char lbuf[count];
    00342         struct { unsigned int fifo; void *buf; int count; } arg = { fifo, lbuf, count };
    00343         retval = rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _GET, &arg).i[LOW];
    00344         if (retval > 0) {
    00345                 memcpy(buf, lbuf, retval);
    00346         }
    00347         return retval;
    00348 }
    00349
    00350 RTAI_PROTO(int, rtf_get_if,(unsigned int fifo, void *buf, int count))
    00351 {
    00352         int retval;
    00353         char lbuf[count];
    00354         struct { unsigned int fifo; void *buf; int count; } arg = { fifo, lbuf, count };
    00355         retval = rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _GET_IF, &arg).i[LOW];
    00356         if (retval > 0) {
    00357                 memcpy(buf, lbuf, retval);
    00358         }
    00359         return retval;
    00360 }
    00361
    00362 RTAI_PROTO(int, rtf_reset_lxrt,(unsigned int fifo))
    00363 {
    00364         struct { unsigned int fifo; } arg = { fifo };
    00365         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _RESET, &arg).i[LOW];
    00366 }
    00367
    00368 RTAI_PROTO(int, rtf_resize_lxrt,(unsigned int fifo, int size))
    00369 {
    00370         struct { unsigned int fifo, size; } arg = { fifo, size };
    00371         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _RESIZE, &arg).i[LOW];
    00372 }
    00373
    00374 RTAI_PROTO(int, rtf_sem_init_lxrt,(unsigned int fifo, int value))
    00375 {
    00376         struct { unsigned int fifo, value; } arg = { fifo, value };
    00377         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _SEM_INIT, &arg).i[LOW];
    00378 }
    00379
    00380 RTAI_PROTO(int, rtf_sem_post_lxrt,(unsigned int fifo))
    00381 {
    00382         struct { unsigned int fifo; } arg = { fifo };
    00383         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _SEM_POST, &arg).i[LOW];
    00384 }
    00385
    00386 RTAI_PROTO(int, rtf_sem_trywait_lxrt,(unsigned int fifo))
    00387 {
    00388         struct { unsigned int fifo; } arg = { fifo };
    00389         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _SEM_TRY, &arg).i[LOW];
    00390 }
    00391
    00392 RTAI_PROTO(int, rtf_sem_destroy_lxrt,(unsigned int fifo))
    00393 {
    00394         struct { unsigned int fifo; } arg = { fifo };
    00395         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _SEM_DESTRY, &arg).i[LOW];
    00396 }
    00397
    00398 RTAI_PROTO(int, rtf_named_create_lxrt,(const char *name, int size))
    00399 {
    00400         int len;
    00401         char lname[len = strlen(name)];
    00402         struct { char * name; int size; } arg = { lname, size };
    00403         strncpy(lname, name, len);
    00404         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _NAMED_CREATE, &arg).i[LOW];
    00405 }
    00406
    00407 RTAI_PROTO(int, rtf_create_named_lxrt,(const char *name))
    00408 {
    00409         int len;
    00410         char lname[len = strlen(name)];
    00411         struct { char * name; } arg = { lname };
    00412         strncpy(lname, name, len);
    00413         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _CREATE_NAMED, &arg).i[LOW];
    00414 }
    00415
    00416 RTAI_PROTO(int, rtf_getfifobyname_lxrt,(const char *name))
    00417 {
    00418         int len;
    00419         char lname[len = strlen(name)];
    00420         struct { char * name; } arg = { lname };
    00421         strncpy(lname, name, len);
    00422         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _GETBY_NAME, &arg).i[LOW];
    00423 }
    00424
    00425 RTAI_PROTO(int, rtf_ovrwr_put,(unsigned int fifo, const void *buf, int count))
    00426 {
    00427         char lbuf[count];
    00428         struct { unsigned int fifo; void *buf; int count; } arg = { fifo, lbuf, count };
    00429         memcpy(lbuf, buf, count);
    00430         return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _OVERWRITE, &arg).i[LOW];
    00431 }
    00432
    00433 RTAI_PROTO(int, rtf_reset,(int fd))
    00434 {
    00435         int ret = ioctl(fd, RESET);
    00436         return ret < 0 ? -errno : ret;
    00437 }
    00438
    00439 RTAI_PROTO(int, rtf_resize,(int fd, int size))
    00440 {
    00441         int ret = ioctl(fd, RESIZE, size);
    00442         return ret < 0 ? -errno : ret;
    00443 }
    00444
    00459 RTAI_PROTO(int, rtf_suspend_timed,(int fd, int ms_delay))
    00460 {
    00461         int ret = ioctl(fd, RTF_SUSPEND_TIMED, ms_delay);
    00462         return ret < 0 ? -errno : ret;
    00463 }
    00464
    00499 RTAI_PROTO(int, rtf_open_sized,(const char *dev, int perm, int size))
    00500 {
    00501         int fd;
    00502
    00503         if ((fd = open(dev, perm)) < 0) {
    00504                 return -errno;
    00505         }
    00506         if (ioctl(fd, RESIZE, size) < 0) {
    00507                 close(fd);
    00508                 return -errno;
    00509         }
    00510         return fd;
    00511 }
    00512
    00513 RTAI_PROTO(int, rtf_evdrp,(int fd, void *buf, int count))
    00514 {
    00515         struct { void *buf; int count; } args = { buf, count };
    00516         int ret = ioctl(fd, EAVESDROP, &args);
    00517         return ret < 0 ? -errno : ret;
    00518 }
    00519
    00536 RTAI_PROTO(int, rtf_read_all_at_once,(int fd, void *buf, int count))
    00537 {
    00538         struct { void *buf; int count; } args = { buf, count };
    00539         int ret = ioctl(fd, READ_ALL_AT_ONCE, &args);
    00540         return ret < 0 ? -errno : ret;
    00541 }
    00542
    00564 RTAI_PROTO(int, rtf_read_timed,(int fd, void *buf, int count, int ms_delay))
    00565 {
    00566         struct { void *buf; int count, delay; } args = { buf, count, ms_delay };
    00567         int ret = ioctl(fd, READ_TIMED, &args);
    00568         return ret < 0 ? -errno : ret;
    00569 }
    00570
    00571 RTAI_PROTO(int, rtf_read_if,(int fd, void *buf, int count))
    00572 {
    00573         struct { void *buf; int count; } args = { buf, count };
    00574         int ret = ioctl(fd, READ_IF, &args);
    00575         return ret < 0 ? -errno : ret;
    00576 }
    00577
    00599 RTAI_PROTO(int, rtf_write_timed,(int fd, void *buf, int count, int ms_delay))
    00600 {
    00601         struct { void *buf; int count, delay; } args = { buf, count, ms_delay };
    00602         int ret = ioctl(fd, WRITE_TIMED, &args);
    00603         return ret < 0 ? -errno : ret;
    00604 }
    00605
    00606 RTAI_PROTO(int, rtf_overwrite,(int fd, void *buf, int count))
    00607 {
    00608         struct { void *buf; int count; } args = { buf, count };
    00609         int ret = ioctl(fd, OVRWRITE, &args);
    00610         return ret < 0 ? -errno : ret;
    00611 }
    00612
    00613 RTAI_PROTO(int, rtf_write_if,(int fd, void *buf, int count))
    00614 {
    00615         struct { void *buf; int count; } args = { buf, count };
    00616         int ret = ioctl(fd, WRITE_IF, &args);
    00617         return ret < 0 ? -errno : ret;
    00618 }
    00619
    00620 RTAI_PROTO(int, rtf_sem_init,(int fd, int value))
    00621 {
    00622         int ret = ioctl(fd, RTF_SEM_INIT, value);
    00623         return ret < 0 ? -errno : ret;
    00624 }
    00625
    00648 RTAI_PROTO(int, rtf_sem_wait,(int fd))
    00649 {
    00650         int ret = ioctl(fd, RTF_SEM_WAIT);
    00651         return ret < 0 ? -errno : ret;
    00652 }
    00653
    00654 RTAI_PROTO(int, rtf_sem_trywait,(int fd))
    00655 {
    00656         int ret = ioctl(fd, RTF_SEM_TRYWAIT);
    00657         return ret < 0 ? -errno : ret;
    00658 }
    00659
    00686 RTAI_PROTO(int, rtf_sem_timed_wait,(int fd, int ms_delay))
    00687 {
    00688         int ret = ioctl(fd, RTF_SEM_TIMED_WAIT, ms_delay);
    00689         return ret < 0 ? -errno : ret;
    00690 }
    00691
    00692 RTAI_PROTO(int, rtf_sem_post,(int fd))
    00693 {
    00694         int ret = ioctl(fd, RTF_SEM_POST);
    00695         return ret < 0 ? -errno : ret;
    00696 }
    00697
    00698 RTAI_PROTO(int, rtf_sem_destroy,(int fd))
    00699 {
    00700         int ret = ioctl(fd, RTF_SEM_DESTROY);
    00701         return ret < 0 ? -errno : ret;
    00702 }
    00703
    00715 RTAI_PROTO(int, rtf_set_async_sig,(int fd, int signum))
    00716 {
    00717         int ret = ioctl(fd, SET_ASYNC_SIG, signum);
    00718         return ret < 0 ? -errno : ret;
    00719 }
    00720
    00721 /*
    00722  * Support for named FIFOS : Ian Soanes (ians@zentropix.com)
    00723  * Based on ideas from Stuart Hughes and David Schleef
    00724  */
    00725
    00726 RTAI_PROTO_ALWAYS_INLINE(char *, rtf_getfifobyminor,(int minor, char *buf, int len))
    00727 {
    00728     snprintf(buf,len,CONFIG_RTAI_FIFOS_TEMPLATE,minor);
    00729     return buf;
    00730 }
    00731
    00732 RTAI_PROTO(int, rtf_getfifobyname,(const char *name))
    00733 {
    00734         int fd, minor;
    00735         char nm[RTF_NAMELEN+1];
    00736
    00737         if (strlen(name) > RTF_NAMELEN) {
    00738                 return -1;
    00739         }
    00740         if ((fd = open(rtf_getfifobyminor(0,nm,sizeof(nm)), O_RDONLY)) < 0) {
    00741                 return -errno;
    00742         }
    00743         strncpy(nm, name, RTF_NAMELEN+1);
    00744         minor = ioctl(fd, RTF_NAME_LOOKUP, nm);
    00745         close(fd);
    00746         return minor < 0 ? -errno : minor;
    00747 }
    00748
    00749 RTAI_PROTO(int, rtf_named_create,(const char *name, int size))
    00750 {
    00751         int fd, minor;
    00752         char nm[RTF_NAMELEN+1];
    00753
    00754         if (strlen(name) > RTF_NAMELEN) {
    00755                 return -1;
    00756         }
    00757         if ((fd = open(rtf_getfifobyminor(0,nm,sizeof(nm)), O_RDONLY)) < 0) {
    00758                 return -errno;
    00759         }
    00760         strncpy(nm, name, RTF_NAMELEN+1);
    00761         minor = ioctl(fd, RTF_NAMED_CREATE, nm, size);
    00762         close(fd);
    00763         return minor < 0 ? -errno : minor;
    00764 }
    00765
    00766 RTAI_PROTO(int, rtf_create_named,(const char *name))
    00767 {
    00768         int fd, minor;
    00769         char nm[RTF_NAMELEN+1];
    00770
    00771         if (strlen(name) > RTF_NAMELEN) {
    00772                 return -1;
    00773         }
    00774         if ((fd = open(rtf_getfifobyminor(0,nm,sizeof(nm)), O_RDONLY)) < 0) {
    00775                 return -errno;
    00776         }
    00777         strncpy(nm, name, RTF_NAMELEN+1);
    00778         minor = ioctl(fd, RTF_CREATE_NAMED, nm);
    00779         close(fd);
    00780         return minor < 0 ? -errno : minor;
    00781 }
    00782
    00783 #ifdef __cplusplus
    00784 }
    00785 #endif /* __cplusplus */
    00786
    00787 #endif /* __KERNEL__ */
    00788
    00789 #endif /* !_RTAI_FIFOS_H */