base/include/rtai_fifos.h

Go to the documentation of this file.
00001 /**
00002  * @ingroup fifos
00003  * @ingroup fifos_ipc
00004  * @ingroup fifos_sem
00005  * @file
00006  *
00007  * Interface of the @ref fifos "RTAI FIFO module".
00008  *
00009  * @note Copyright &copy; 1999-2003 Paolo Mantegazza <mantegazza@aero.polimi.it>
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License as
00013  * published by the Free Software Foundation; either version 2 of the
00014  * License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024  */
00025 
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     int avbs, frbs;
00067     char name[RTF_NAMELEN+1];
00068 };
00069 
00070 struct rt_fifo_get_info_struct{
00071         unsigned int fifo;
00072     unsigned int n;
00073     struct rt_fifo_info_struct *ptr;
00074 };
00075 
00076 #define  FUN_FIFOS_LXRT_INDX 10
00077 
00078 #define _CREATE         0
00079 #define _DESTROY        1
00080 #define _PUT            2
00081 #define _GET            3
00082 #define _RESET          4
00083 #define _RESIZE         5
00084 #define _SEM_INIT       6
00085 #define _SEM_DESTRY     7
00086 #define _SEM_POST       8
00087 #define _SEM_TRY        9
00088 #define _CREATE_NAMED  10
00089 #define _GETBY_NAME    11
00090 #define _OVERWRITE     12
00091 #define _PUT_IF        13
00092 #define _GET_IF        14
00093 #define _NAMED_CREATE  15
00094 #define _AVBS          16
00095 #define _FRBS          17
00096 
00097 #ifdef __KERNEL__
00098 
00099 #include <rtai.h>
00100 
00101 #ifdef __cplusplus
00102 extern "C" {
00103 #endif /* __cplusplus */
00104 
00105 int __rtai_fifos_init(void);
00106 
00107 void __rtai_fifos_exit(void);
00108 
00109 int rtf_init(void);
00110 
00111 typedef int (*rtf_handler_t)(unsigned int fifo, int rw);
00112 
00113 /* Attach a handler to an RT-FIFO.
00114  *
00115  * Allow function handler to be called when a user process reads or writes to 
00116  * the FIFO. When the function is called, it is passed the fifo number as the 
00117  * argument.
00118  */
00119 
00120 int rtf_create_handler(unsigned int fifo,   /* RT-FIFO */
00121                void *handler        /* function to be called */);
00122 
00123 
00124 /**
00125  * @ingroup fifos_ipc
00126  * Extended fifo handler.
00127  *
00128  * The usage of X_FIFO_HANDLER(handler) allows to install an extended handler,
00129  * i.e. one prototyped as:
00130  * @code
00131  * int (*handler)(unsigned int fifo, int rw);
00132  * @endcode
00133  * to allow the user to easily understand if the handler was called at fifo
00134  * read (@a rw is 'r') or write (rw is 'w').
00135  */
00136 #define X_FIFO_HANDLER(handler) ((int (*)(unsigned int, int rw))(handler))
00137 
00138 /* Create an RT-FIFO.
00139  * 
00140  * An RT-FIFO fifo is created with initial size of size.
00141  * Return value: On success, 0 is returned. On error, -1 is returned.
00142  */
00143 #undef rtf_create
00144 RTAI_SYSCALL_MODE int rtf_create(unsigned int fifo, int size);
00145 
00146 /* Create an RT-FIFO with a name and size.
00147  *
00148  * An RT-FIFO is created with a name of name, it will be allocated
00149  * the first unused minor number and will have a user assigned size.
00150  * Return value: On success, the allocated minor number is returned.
00151  *               On error, -errno is returned.
00152  */
00153 
00154 int rtf_named_create(const char *name, int size);
00155 
00156 /* Create an RT-FIFO with a name.
00157  *
00158  * An RT-FIFO is created with a name of name, it will be allocated
00159  * the first unused minor number and will have a default size.
00160  * Return value: On success, the allocated minor number is returned. 
00161  *               On error, -errno is returned.
00162  */
00163 
00164 RTAI_SYSCALL_MODE int rtf_create_named(const char *name);
00165 
00166 /* Look up a named RT-FIFO.
00167  *
00168  * Find the RT-FIFO with the name name.
00169  * Return value: On success, the minor number is returned. 
00170  *               On error, -errno is returned.
00171  */
00172 
00173 RTAI_SYSCALL_MODE int rtf_getfifobyname(const char *name);
00174 
00175 /* Reset an RT-FIFO.
00176  * 
00177  * An RT-FIFO fifo is reset by setting its buffer pointers to zero, so
00178  * that any existing data is discarded and the fifo started anew like at its
00179  * creation.
00180  */
00181 
00182 RTAI_SYSCALL_MODE int rtf_reset(unsigned int fifo);
00183 
00184 /* destroy an RT-FIFO.
00185  * 
00186  * Return value: On success, 0 is returned.
00187  */
00188 
00189 RTAI_SYSCALL_MODE int rtf_destroy(unsigned int fifo);
00190 
00191 
00192 /* Resize an RT-FIFO.
00193  * 
00194  * Return value: size is returned on success. On error, a negative value
00195  * is returned.
00196  */
00197 
00198 RTAI_SYSCALL_MODE int rtf_resize(unsigned int minor, int size);
00199 
00200 
00201 /* Write to an RT-FIFO.
00202  *
00203  * Try to write count bytes to an FIFO. Returns the number of bytes written.
00204  */
00205 
00206 RTAI_SYSCALL_MODE int rtf_put(unsigned int fifo,    /* RT-FIFO */
00207         void * buf,     /* buffer address */
00208         int count       /* number of bytes to write */);
00209 
00210 
00211 
00212 /* Write to an RT-FIFO, over writing if there is not enough space.
00213  *
00214  * Try to write count bytes to an FIFO. Returns 0.
00215  */
00216 
00217 RTAI_SYSCALL_MODE int rtf_ovrwr_put(unsigned int fifo,  /* RT-FIFO */
00218           void * buf,       /* buffer address */
00219           int count     /* number of bytes to write */);
00220 
00221 
00222 
00223 /* Write atomically to an RT-FIFO.
00224  *
00225  * Try to write count bytes in block to an FIFO. Returns the number of bytes
00226  * written.
00227  */
00228 
00229 extern RTAI_SYSCALL_MODE int rtf_put_if (unsigned int fifo, /* RT-FIFO */
00230         void * buf,         /* buffer address */
00231             int count           /* number of bytes to write */);
00232 
00233 /* Read from an RT-FIFO.
00234  *
00235  * Try to read count bytes from a FIFO. Returns the number of bytes read.
00236  */
00237 
00238 RTAI_SYSCALL_MODE int rtf_get(unsigned int fifo,    /* RT-FIFO */
00239         void * buf,         /* buffer address */
00240         int count       /* number of bytes to read */);
00241 
00242 
00243 /* Atomically read from an RT-FIFO.
00244  *
00245  * Try to read count bytes in a block from an FIFO. Returns the number of bytes read.
00246  */
00247 
00248 RTAI_SYSCALL_MODE int rtf_get_if(unsigned int fifo, /* RT-FIFO */
00249         void * buf,         /* buffer address */
00250         int count       /* number of bytes to read */);
00251 
00252 
00253 /* 
00254  * Preview the an RT-FIFO content.
00255  */
00256 
00257 int rtf_evdrp(unsigned int fifo,    /* RT-FIFO */
00258           void * buf,       /* buffer address */
00259           int count     /* number of bytes to read */);
00260 
00261 /* Open an RT-FIFO semaphore.
00262  *
00263  */
00264 
00265 RTAI_SYSCALL_MODE int rtf_sem_init(unsigned int fifo,   /* RT-FIFO */
00266          int value          /* initial semaphore value */);
00267 
00268 
00269 /* Post to an RT-FIFO semaphore.
00270  *
00271  */
00272 
00273 RTAI_SYSCALL_MODE int rtf_sem_post(unsigned int fifo    /* RT-FIFO */);
00274 
00275 
00276 /* Try to acquire an RT-FIFO semaphore.
00277  *
00278  */
00279 
00280 RTAI_SYSCALL_MODE int rtf_sem_trywait(unsigned int fifo /* RT-FIFO */);
00281 
00282 
00283 /* Destroy an RT-FIFO semaphore.
00284  *
00285  */
00286 
00287 RTAI_SYSCALL_MODE int rtf_sem_destroy(unsigned int fifo /* RT-FIFO */);
00288 
00289 #define rtf_sem_delete rtf_sem_destroy
00290 
00291 
00292 /* Get an RT-FIFO free bytes in buffer.
00293  *
00294  */
00295 
00296 RTAI_SYSCALL_MODE int rtf_get_frbs(unsigned int fifo /* RT-FIFO */);
00297 
00298 /* Just for compatibility with earlier rtai_fifos releases. No more bh and user
00299 buffers. Fifos are now awakened immediately and buffers > 128K are vmalloced */
00300 
00301 #define rtf_create_using_bh(fifo, size, bh_list) rtf_create(fifo, size)
00302 #define rtf_create_using_bh_and_usr_buf(fifo, buf, size, bh_list) rtf_create(fifo, size)
00303 #define rtf_destroy_using_usr_buf(fifo) rtf_destroy(fifo)
00304 
00305 #ifdef __cplusplus
00306 }
00307 #endif /* __cplusplus */
00308 
00309 #else  /* !__KERNEL__ */
00310 
00311 #include <sys/types.h>
00312 #include <sys/stat.h>
00313 #include <sys/ioctl.h>
00314 #include <fcntl.h>
00315 #include <unistd.h>
00316 #include <stdio.h>
00317 #include <string.h>
00318 #include <rtai_lxrt.h>
00319 
00320 #ifdef __cplusplus
00321 extern "C" {
00322 #endif /* __cplusplus */
00323 
00324 RTAI_PROTO(int, rtf_create,(unsigned int fifo, int size))
00325 {
00326     struct { unsigned long fifo, size; } arg = { fifo, size }; 
00327     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _CREATE, &arg).i[LOW];
00328 }
00329 
00330 RTAI_PROTO(int, rtf_destroy,(unsigned int fifo))
00331 {
00332     struct { unsigned long fifo; } arg = { fifo };
00333     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _DESTROY, &arg).i[LOW];
00334 }
00335 
00336 RTAI_PROTO(int, rtf_put,(unsigned int fifo, const void *buf, int count))
00337 {
00338     char lbuf[count];
00339     struct { unsigned long fifo; void *buf; long count; } arg = { fifo, lbuf, count };
00340     memcpy(lbuf, buf, count);
00341     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _PUT, &arg).i[LOW];
00342 }
00343 
00344 RTAI_PROTO(int, rtf_put_if,(unsigned int fifo, const void *buf, int count))
00345 {
00346     char lbuf[count];
00347     struct { unsigned long fifo; void *buf; long count; } arg = { fifo, lbuf, count };
00348     memcpy(lbuf, buf, count);
00349     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _PUT_IF, &arg).i[LOW];
00350 }
00351 
00352 RTAI_PROTO(int, rtf_get,(unsigned int fifo, void *buf, int count))
00353 {
00354     int retval;
00355     char lbuf[count];
00356     struct { unsigned long fifo; void *buf; long count; } arg = { fifo, lbuf, count };
00357     retval = rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _GET, &arg).i[LOW];
00358     if (retval > 0) {
00359         memcpy(buf, lbuf, retval);
00360     }
00361     return retval;
00362 }
00363 
00364 RTAI_PROTO(int, rtf_get_if,(unsigned int fifo, void *buf, int count))
00365 {
00366     int retval;
00367     char lbuf[count];
00368     struct { unsigned long fifo; void *buf; long count; } arg = { fifo, lbuf, count };
00369     retval = rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _GET_IF, &arg).i[LOW];
00370     if (retval > 0) {
00371         memcpy(buf, lbuf, retval);
00372     }
00373     return retval;
00374 }
00375 
00376 RTAI_PROTO(int, rtf_get_avbs, (unsigned int fifo))
00377 {
00378     struct { unsigned long fifo; } arg = { fifo };
00379     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _AVBS, &arg).i[LOW];
00380 }
00381 
00382 RTAI_PROTO(int, rtf_get_frbs, (unsigned int fifo))
00383 {
00384     struct { unsigned long fifo; } arg = { fifo };
00385     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _FRBS, &arg).i[LOW];
00386 }
00387 
00388 RTAI_PROTO(int, rtf_reset_lxrt,(unsigned int fifo))
00389 {
00390     struct { unsigned long fifo; } arg = { fifo };
00391     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _RESET, &arg).i[LOW];
00392 }
00393 
00394 RTAI_PROTO(int, rtf_resize_lxrt,(unsigned int fifo, int size))
00395 {
00396     struct { unsigned long fifo, size; } arg = { fifo, size }; 
00397     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _RESIZE, &arg).i[LOW];
00398 }
00399 
00400 RTAI_PROTO(int, rtf_sem_init_lxrt,(unsigned int fifo, int value))
00401 {
00402     struct { unsigned long fifo, value; } arg = { fifo, value }; 
00403     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _SEM_INIT, &arg).i[LOW];
00404 }
00405 
00406 RTAI_PROTO(int, rtf_sem_post_lxrt,(unsigned int fifo))
00407 {
00408     struct { unsigned long fifo; } arg = { fifo }; 
00409     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _SEM_POST, &arg).i[LOW];
00410 }
00411 
00412 RTAI_PROTO(int, rtf_sem_trywait_lxrt,(unsigned int fifo))
00413 {
00414     struct { unsigned long fifo; } arg = { fifo }; 
00415     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _SEM_TRY, &arg).i[LOW];
00416 }
00417 
00418 RTAI_PROTO(int, rtf_sem_destroy_lxrt,(unsigned int fifo))
00419 {
00420     struct { unsigned long fifo; } arg = { fifo }; 
00421     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _SEM_DESTRY, &arg).i[LOW];
00422 }
00423 
00424 RTAI_PROTO(int, rtf_named_create_lxrt,(const char *name, int size))
00425 {
00426     int len;
00427     char lname[len = strlen(name)];
00428     struct { char * name; long size; } arg = { lname, size };
00429     strncpy(lname, name, len);
00430     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _NAMED_CREATE, &arg).i[LOW];
00431 }
00432 
00433 RTAI_PROTO(int, rtf_create_named_lxrt,(const char *name))
00434 {
00435     int len;
00436     char lname[len = strlen(name)];
00437     struct { char * name; } arg = { lname };
00438     strncpy(lname, name, len);
00439     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _CREATE_NAMED, &arg).i[LOW];
00440 }
00441 
00442 RTAI_PROTO(int, rtf_getfifobyname_lxrt,(const char *name))
00443 {
00444     int len;
00445     char lname[len = strlen(name)];
00446     struct { char * name; } arg = { lname };
00447     strncpy(lname, name, len);
00448     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _GETBY_NAME, &arg).i[LOW];
00449 }
00450 
00451 RTAI_PROTO(int, rtf_ovrwr_put,(unsigned int fifo, const void *buf, int count))
00452 {
00453     char lbuf[count];
00454     struct { unsigned long fifo; void *buf; long count; } arg = { fifo, lbuf, count };
00455     memcpy(lbuf, buf, count);
00456     return rtai_lxrt(FUN_FIFOS_LXRT_INDX, SIZARG, _OVERWRITE, &arg).i[LOW];
00457 }
00458 
00459 RTAI_PROTO(int, rtf_reset,(int fd))
00460 {
00461     int ret = ioctl(fd, RESET);
00462     return ret < 0 ? -errno : ret;
00463 }
00464 
00465 RTAI_PROTO(int, rtf_resize,(int fd, int size))
00466 {
00467     int ret = ioctl(fd, RESIZE, size);
00468     return ret < 0 ? -errno : ret;
00469 }
00470 
00471 /**
00472  * @ingroup fifos_ipc
00473  * Suspend a process for some time
00474  *
00475  * rtf_suspend_timed suspends a Linux process according to @a delay.
00476  *
00477  * @param fd is the file descriptor returned at fifo open, rtf_suspend_timed
00478  * needs a fifo support.
00479  * @param ms_delay is the timeout time in milliseconds.
00480  *
00481  * @note The standard, clumsy, way to achieve the same result is to use select
00482  * with null file arguments, for long sleeps, with seconds resolution, sleep is
00483  * also available.
00484  */
00485 RTAI_PROTO(int, rtf_suspend_timed,(int fd, int ms_delay))
00486 {
00487     int ret = ioctl(fd, RTF_SUSPEND_TIMED, ms_delay);
00488     return ret < 0 ? -errno : ret;
00489 }
00490 
00491 /**
00492  * @ingroup fifos_ipc
00493  * Create a real-time FIFO
00494  *
00495  * rtf_open_sized is the equivalent of rtf_create() in user space; it creates a
00496  * real-time fifo (RT-FIFO) of initial size @a size.
00497  *
00498  * @param size is the requested size for the fifo. 
00499  * 
00500  * The RT-FIFO is a character based mechanism to communicate among real-time
00501  * tasks and ordinary Linux processes. The rtf_* functions are used by the
00502  * real-time tasks; Linux processes use standard character device access
00503  * functions such as read, write, and select.
00504  *
00505  * If this function finds an existing fifo of lower size it resizes it to the
00506  * larger new size. Note that the same condition apply to the standard Linux
00507  * device open, except that when it does not find any already existing fifo it
00508  * creates it with a default size of 1K bytes.
00509  *
00510  * It must be remarked that practically any fifo size can be asked for. In
00511  * fact if @a size is within the constraint allowed by kmalloc such a function
00512  * is used, otherwise vmalloc is called, thus allowing any size that can fit
00513  * into the available core memory.
00514  *
00515  * Multiple calls of this function are allowed, a counter is kept internally to
00516  * track their number, and avoid destroying/closing a fifo that is still used.
00517  *
00518  * @return the usual Unix file descriptor on succes, to be used in standard reads
00519  * and writes. 
00520  * @retval -ENOMEM if the necessary size could not be allocated for the RT-FIFO.
00521  *
00522  * @note In user space, the standard UNIX open acts like rtf_open_sized with a
00523  * default 1K size.
00524  */
00525 RTAI_PROTO(int, rtf_open_sized,(const char *dev, int perm, int size))
00526 {
00527     int fd;
00528 
00529     if ((fd = open(dev, perm)) < 0) { 
00530         return -errno;
00531     }
00532     if (ioctl(fd, RESIZE, size) < 0) {
00533             close(fd);
00534         return -errno;
00535     }
00536     return fd; 
00537 }
00538 
00539 RTAI_PROTO(int, rtf_evdrp,(int fd, void *buf, int count))
00540 {
00541     struct { void *buf; long count; } args = { buf, count };
00542     int ret = ioctl(fd, EAVESDROP, &args);
00543     return ret < 0 ? -errno : ret;
00544 }
00545 
00546 /**
00547  * @ingroup fifos_ipc
00548  * Read data from FIFO in user space, waiting for all of them
00549  *
00550  * rtf_read_all_at_once reads a block of data from a real-time fifo identified
00551  * by the file descriptor @a fd blocking till all waiting at most @a count
00552  * bytes are available, whichever option was used at the related device
00553  * opening.
00554  *
00555  * @param fd is the file descriptor returned at fifo open.
00556  * @param buf points the block of data to be written.
00557  * @param count is the size in bytes of the buffer.
00558  *
00559  * @return the number of bytes read on success.
00560  * @retval -EINVAL if @a fd refers to a not opened fifo.
00561  */
00562 RTAI_PROTO(int, rtf_read_all_at_once,(int fd, void *buf, int count))
00563 {
00564     struct { void *buf; long count; } args = { buf, count };
00565     int ret = ioctl(fd, READ_ALL_AT_ONCE, &args);
00566     return ret < 0 ? -errno : ret;
00567 }
00568 
00569 /**
00570  * @ingroup fifos_ipc
00571  * Read data from FIFO in user space, with timeout.
00572  *
00573  * rtf_read_timed reads a block of data from a real-time fifo identified by the
00574  * file descriptor @a fd waiting at most @a delay milliseconds to complete the
00575  * operation.
00576  *
00577  * @param fd is the file descriptor returned at fifo open.
00578  * @param buf points the block of data to be written.
00579  * @param count is the size of the block in bytes.
00580  * @param ms_delay is the timeout time in milliseconds.
00581  *
00582  * @return the number of bytes read is returned on success or timeout. Note that
00583  * this value may be less than @a count if @a count bytes of free space is not
00584  * available in the fifo or a timeout occured.
00585  * @retval -EINVAL if @a fd refers to a not opened fifo.
00586  *
00587  * @note The standard, clumsy, Unix way to achieve the same result is to use
00588  * select.
00589  */
00590 RTAI_PROTO(int, rtf_read_timed,(int fd, void *buf, int count, int ms_delay))
00591 {
00592     struct { void *buf; long count, delay; } args = { buf, count, ms_delay };
00593     int ret = ioctl(fd, READ_TIMED, &args);
00594     return ret < 0 ? -errno : ret;
00595 }
00596 
00597 RTAI_PROTO(int, rtf_read_if,(int fd, void *buf, int count))
00598 {
00599     struct { void *buf; long count; } args = { buf, count };
00600     int ret = ioctl(fd, READ_IF, &args);
00601     return ret < 0 ? -errno : ret;
00602 }
00603 
00604 /**
00605  * @ingroup fifos_ipc
00606  * Write data to FIFO in user space, with timeout.
00607  *
00608  * rtf_write_timed writes a block of data to a real-time fifo identified by the
00609  * file descriptor @a fd waiting at most @æ delay milliseconds to complete the
00610  * operation.
00611  *
00612  * @param fd is the file descriptor returned at fifo open.
00613  * @param buf points the block of data to be written.
00614  * @param count is the size of the block in bytes.
00615  * @param ms_delay is the timeout time in milliseconds.
00616  *
00617  * @return the number of bytes written on succes. Note that this value may
00618  * be less than @a count if @a count bytes of free space is not available in the
00619  * fifo.
00620  * @retval -EINVAL if @a fd refers to a not opened fifo.
00621  *
00622  * @note The standard, clumsy, Unix way to achieve the same result is to use
00623  * select.
00624  */
00625 RTAI_PROTO(int, rtf_write_timed,(int fd, void *buf, int count, int ms_delay))
00626 {
00627     struct { void *buf; long count, delay; } args = { buf, count, ms_delay };
00628     int ret = ioctl(fd, WRITE_TIMED, &args);
00629     return ret < 0 ? -errno : ret;
00630 }
00631 
00632 RTAI_PROTO(int, rtf_overwrite,(int fd, void *buf, int count))
00633 {
00634     struct { void *buf; long count; } args = { buf, count };
00635     int ret = ioctl(fd, OVRWRITE, &args);
00636     return ret < 0 ? -errno : ret;
00637 }
00638 
00639 RTAI_PROTO(int, rtf_write_if,(int fd, void *buf, int count))
00640 {
00641     struct { void *buf; long count; } args = { buf, count };
00642     int ret = ioctl(fd, WRITE_IF, &args);
00643     return ret < 0 ? -errno : ret;
00644 }
00645 
00646 RTAI_PROTO(int, rtf_sem_init,(int fd, int value))
00647 {
00648     int ret = ioctl(fd, RTF_SEM_INIT, value);
00649     return ret < 0 ? -errno : ret;
00650 }
00651 
00652 /**
00653  * @ingroup fifos_sem
00654  * Take a semaphore.
00655  *
00656  * rtf_sem_wait waits for a event to be posted (signaled) to a semaphore. The
00657  * semaphore value is set to tested and set to zero.   If it was one
00658  * rtf_sem_wait returns immediately. Otherwise the caller process is blocked and
00659  * queued up in a priority order based on is POSIX real time priority.
00660  *
00661  * A process blocked on a semaphore returns when:
00662  * - the caller task is in the first place of the waiting queue and somebody
00663  *   issues a rtf_sem_post; 
00664  * - an error occurs (e.g. the semaphore is destroyed).
00665  *
00666  * @param fd is the file descriptor returned by standard UNIX open in user space
00667  * 
00668  * Since it is blocking rtf_sem_waitcannot be used both in kernel and user
00669  * space.
00670  *
00671  * @retval 0 on success.
00672  * @retval -EINVAL if @a fd_fifo  refers to an invalid file descriptor or fifo.
00673  */
00674 RTAI_PROTO(int, rtf_sem_wait,(int fd))
00675 {
00676     int ret = ioctl(fd, RTF_SEM_WAIT);
00677     return ret < 0 ? -errno : ret;
00678 }
00679 
00680 RTAI_PROTO(int, rtf_sem_trywait,(int fd))
00681 {
00682     int ret = ioctl(fd, RTF_SEM_TRYWAIT);
00683     return ret < 0 ? -errno : ret;
00684 }
00685 
00686 /**
00687  * @ingroup fifos_sem
00688  * Wait a semaphore with timeout
00689  *
00690  * rtf_sem_timed_wait is a timed version of the standard semaphore wait
00691  * call. The semaphore value is tested and set to zero. If it was one
00692  * rtf_sem_timed_wait returns immediately. Otherwise the caller process is
00693  * blocked and queued up in a priority order based on is POSIX real time
00694  * priority.
00695  *
00696  * A process blocked on a semaphore returns when:
00697  * - the caller task is in the first place of the waiting queue and somebody
00698  *   issues a rtf_sem_post;
00699  * - timeout occurs;
00700  * - an error occurs (e.g. the semaphore is destroyed).
00701  *
00702  * @param fd is the file descriptor returned by standard UNIX open in user
00703  * space. In case of timeout the semaphore value is set to one before return.
00704  * @param ms_delay is in milliseconds and is relative to the Linux current time.
00705  *
00706  * Since it is blocking rtf_sem_timed_wait cannot be used both in kernel and
00707  * user space.
00708  *
00709  * @retval 0 on success.
00710  * @retval -EINVAL if fd_fifo refers to an invalid file descriptor or fifo.
00711  */
00712 RTAI_PROTO(int, rtf_sem_timed_wait,(int fd, int ms_delay))
00713 {
00714     int ret = ioctl(fd, RTF_SEM_TIMED_WAIT, ms_delay);
00715     return ret < 0 ? -errno : ret;
00716 }
00717 
00718 RTAI_PROTO(int, rtf_sem_post,(int fd))
00719 {
00720     int ret = ioctl(fd, RTF_SEM_POST);
00721     return ret < 0 ? -errno : ret;
00722 }
00723 
00724 RTAI_PROTO(int, rtf_sem_destroy,(int fd))
00725 {
00726     int ret = ioctl(fd, RTF_SEM_DESTROY);
00727     return ret < 0 ? -errno : ret;
00728 }
00729 
00730 /**
00731  * @ingroup fifos_ipc
00732  * Activate asynchronous notification of data availability
00733  *
00734  * rtf_set_async_sig activate an asynchronous signals to notify data
00735  * availability by catching a user set signal signum.
00736  *
00737  * @param signum is a user chosen signal number to be used, default is SIGIO.
00738  *
00739  * @retval -EINVAL if fd refers to a not opened fifo.
00740  */
00741 RTAI_PROTO(int, rtf_set_async_sig,(int fd, int signum))
00742 {
00743     int ret = ioctl(fd, SET_ASYNC_SIG, signum);
00744     return ret < 0 ? -errno : ret;
00745 }
00746 
00747 /*
00748  * Support for named FIFOS : Ian Soanes (ians@zentropix.com)
00749  * Based on ideas from Stuart Hughes and David Schleef
00750  */
00751 
00752 RTAI_PROTO_ALWAYS_INLINE(char *, rtf_getfifobyminor,(int minor, char *buf, int len))
00753 {
00754     snprintf(buf,len,CONFIG_RTAI_FIFOS_TEMPLATE,minor);
00755     return buf;
00756 }
00757 
00758 RTAI_PROTO(int, rtf_getfifobyname,(const char *name))
00759 {
00760         int fd, minor;
00761     char nm[RTF_NAMELEN+1];
00762 
00763     if (strlen(name) > RTF_NAMELEN) {
00764             return -1;
00765     }
00766     if ((fd = open(rtf_getfifobyminor(0,nm,sizeof(nm)), O_RDONLY)) < 0) {
00767             return -errno;
00768     }
00769     strncpy(nm, name, RTF_NAMELEN+1);
00770     minor = ioctl(fd, RTF_NAME_LOOKUP, nm);
00771     close(fd);
00772     return minor < 0 ? -errno : minor;
00773 }
00774 
00775 RTAI_PROTO(int, rtf_named_create,(const char *name, int size))
00776 {
00777     int fd, minor;
00778     char nm[RTF_NAMELEN+1];
00779 
00780     if (strlen(name) > RTF_NAMELEN) {
00781         return -1;
00782     }
00783     if ((fd = open(rtf_getfifobyminor(0,nm,sizeof(nm)), O_RDONLY)) < 0) {
00784         return -errno;
00785     }
00786     strncpy(nm, name, RTF_NAMELEN+1);
00787     minor = ioctl(fd, RTF_NAMED_CREATE, nm, size);
00788     close(fd);
00789     return minor < 0 ? -errno : minor;
00790 }
00791 
00792 RTAI_PROTO(int, rtf_create_named,(const char *name))
00793 {
00794     int fd, minor;
00795     char nm[RTF_NAMELEN+1];
00796 
00797     if (strlen(name) > RTF_NAMELEN) {
00798             return -1;
00799     }
00800     if ((fd = open(rtf_getfifobyminor(0,nm,sizeof(nm)), O_RDONLY)) < 0) { 
00801         return -errno;
00802     }
00803     strncpy(nm, name, RTF_NAMELEN+1);
00804     minor = ioctl(fd, RTF_CREATE_NAMED, nm);
00805     close(fd);
00806     return minor < 0 ? -errno : minor;
00807 }
00808 
00809 #ifdef __cplusplus
00810 }
00811 #endif /* __cplusplus */
00812 
00813 #endif /* __KERNEL__ */
00814 
00815 #endif /* !_RTAI_FIFOS_H */

Generated on Tue Feb 2 17:46:05 2010 for RTAI API by  doxygen 1.4.7