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 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 00117 /** 00118 * @ingroup fifos_ipc 00119 * Extended fifo handler. 00120 * 00121 * The usage of X_FIFO_HANDLER(handler) allows to install an extended handler, 00122 * i.e. one prototyped as: 00123 * @code 00124 * int (*handler)(unsigned int fifo, int rw); 00125 * @endcode 00126 * to allow the user to easily understand if the handler was called at fifo 00127 * read (@a rw is 'r') or write (rw is 'w'). 00128 */ 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 long 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 long 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 long fifo; void *buf; long 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 long fifo; void *buf; long 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 long fifo; void *buf; long 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 long fifo; void *buf; long 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 long 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 long 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 long 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 long 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 long 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 long 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; long 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 long fifo; void *buf; long 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 00445 /** 00446 * @ingroup fifos_ipc 00447 * Suspend a process for some time 00448 * 00449 * rtf_suspend_timed suspends a Linux process according to @a delay. 00450 * 00451 * @param fd is the file descriptor returned at fifo open, rtf_suspend_timed 00452 * needs a fifo support. 00453 * @param ms_delay is the timeout time in milliseconds. 00454 * 00455 * @note The standard, clumsy, way to achieve the same result is to use select 00456 * with null file arguments, for long sleeps, with seconds resolution, sleep is 00457 * also available. 00458 */ 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 00465 /** 00466 * @ingroup fifos_ipc 00467 * Create a real-time FIFO 00468 * 00469 * rtf_open_sized is the equivalent of rtf_create() in user space; it creates a 00470 * real-time fifo (RT-FIFO) of initial size @a size. 00471 * 00472 * @param size is the requested size for the fifo. 00473 * 00474 * The RT-FIFO is a character based mechanism to communicate among real-time 00475 * tasks and ordinary Linux processes. The rtf_* functions are used by the 00476 * real-time tasks; Linux processes use standard character device access 00477 * functions such as read, write, and select. 00478 * 00479 * If this function finds an existing fifo of lower size it resizes it to the 00480 * larger new size. Note that the same condition apply to the standard Linux 00481 * device open, except that when it does not find any already existing fifo it 00482 * creates it with a default size of 1K bytes. 00483 * 00484 * It must be remarked that practically any fifo size can be asked for. In 00485 * fact if @a size is within the constraint allowed by kmalloc such a function 00486 * is used, otherwise vmalloc is called, thus allowing any size that can fit 00487 * into the available core memory. 00488 * 00489 * Multiple calls of this function are allowed, a counter is kept internally to 00490 * track their number, and avoid destroying/closing a fifo that is still used. 00491 * 00492 * @return the usual Unix file descriptor on succes, to be used in standard reads 00493 * and writes. 00494 * @retval -ENOMEM if the necessary size could not be allocated for the RT-FIFO. 00495 * 00496 * @note In user space, the standard UNIX open acts like rtf_open_sized with a 00497 * default 1K size. 00498 */ 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; long count; } args = { buf, count }; 00516 int ret = ioctl(fd, EAVESDROP, &args); 00517 return ret < 0 ? -errno : ret; 00518 } 00519 00520 /** 00521 * @ingroup fifos_ipc 00522 * Read data from FIFO in user space, waiting for all of them 00523 * 00524 * rtf_read_all_at_once reads a block of data from a real-time fifo identified 00525 * by the file descriptor @a fd blocking till all waiting at most @a count 00526 * bytes are available, whichever option was used at the related device 00527 * opening. 00528 * 00529 * @param fd is the file descriptor returned at fifo open. 00530 * @param buf points the block of data to be written. 00531 * @param count is the size in bytes of the buffer. 00532 * 00533 * @return the number of bytes read on success. 00534 * @retval -EINVAL if @a fd refers to a not opened fifo. 00535 */ 00536 RTAI_PROTO(int, rtf_read_all_at_once,(int fd, void *buf, int count)) 00537 { 00538 struct { void *buf; long count; } args = { buf, count }; 00539 int ret = ioctl(fd, READ_ALL_AT_ONCE, &args); 00540 return ret < 0 ? -errno : ret; 00541 } 00542 00543 /** 00544 * @ingroup fifos_ipc 00545 * Read data from FIFO in user space, with timeout. 00546 * 00547 * rtf_read_timed reads a block of data from a real-time fifo identified by the 00548 * file descriptor @a fd waiting at most @a delay milliseconds to complete the 00549 * operation. 00550 * 00551 * @param fd is the file descriptor returned at fifo open. 00552 * @param buf points the block of data to be written. 00553 * @param count is the size of the block in bytes. 00554 * @param ms_delay is the timeout time in milliseconds. 00555 * 00556 * @return the number of bytes read is returned on success or timeout. Note that 00557 * this value may be less than @a count if @a count bytes of free space is not 00558 * available in the fifo or a timeout occured. 00559 * @retval -EINVAL if @a fd refers to a not opened fifo. 00560 * 00561 * @note The standard, clumsy, Unix way to achieve the same result is to use 00562 * select. 00563 */ 00564 RTAI_PROTO(int, rtf_read_timed,(int fd, void *buf, int count, int ms_delay)) 00565 { 00566 struct { void *buf; long 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; long count; } args = { buf, count }; 00574 int ret = ioctl(fd, READ_IF, &args); 00575 return ret < 0 ? -errno : ret; 00576 } 00577 00578 /** 00579 * @ingroup fifos_ipc 00580 * Write data to FIFO in user space, with timeout. 00581 * 00582 * rtf_write_timed writes a block of data to a real-time fifo identified by the 00583 * file descriptor @a fd waiting at most @æ delay milliseconds to complete the 00584 * operation. 00585 * 00586 * @param fd is the file descriptor returned at fifo open. 00587 * @param buf points the block of data to be written. 00588 * @param count is the size of the block in bytes. 00589 * @param ms_delay is the timeout time in milliseconds. 00590 * 00591 * @return the number of bytes written on succes. Note that this value may 00592 * be less than @a count if @a count bytes of free space is not available in the 00593 * fifo. 00594 * @retval -EINVAL if @a fd refers to a not opened fifo. 00595 * 00596 * @note The standard, clumsy, Unix way to achieve the same result is to use 00597 * select. 00598 */ 00599 RTAI_PROTO(int, rtf_write_timed,(int fd, void *buf, int count, int ms_delay)) 00600 { 00601 struct { void *buf; long 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; long 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; long 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 00626 /** 00627 * @ingroup fifos_sem 00628 * Take a semaphore. 00629 * 00630 * rtf_sem_wait waits for a event to be posted (signaled) to a semaphore. The 00631 * semaphore value is set to tested and set to zero. If it was one 00632 * rtf_sem_wait returns immediately. Otherwise the caller process is blocked and 00633 * queued up in a priority order based on is POSIX real time priority. 00634 * 00635 * A process blocked on a semaphore returns when: 00636 * - the caller task is in the first place of the waiting queue and somebody 00637 * issues a rtf_sem_post; 00638 * - an error occurs (e.g. the semaphore is destroyed). 00639 * 00640 * @param fd is the file descriptor returned by standard UNIX open in user space 00641 * 00642 * Since it is blocking rtf_sem_waitcannot be used both in kernel and user 00643 * space. 00644 * 00645 * @retval 0 on success. 00646 * @retval -EINVAL if @a fd_fifo refers to an invalid file descriptor or fifo. 00647 */ 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 00660 /** 00661 * @ingroup fifos_sem 00662 * Wait a semaphore with timeout 00663 * 00664 * rtf_sem_timed_wait is a timed version of the standard semaphore wait 00665 * call. The semaphore value is tested and set to zero. If it was one 00666 * rtf_sem_timed_wait returns immediately. Otherwise the caller process is 00667 * blocked and queued up in a priority order based on is POSIX real time 00668 * priority. 00669 * 00670 * A process blocked on a semaphore returns when: 00671 * - the caller task is in the first place of the waiting queue and somebody 00672 * issues a rtf_sem_post; 00673 * - timeout occurs; 00674 * - an error occurs (e.g. the semaphore is destroyed). 00675 * 00676 * @param fd is the file descriptor returned by standard UNIX open in user 00677 * space. In case of timeout the semaphore value is set to one before return. 00678 * @param ms_delay is in milliseconds and is relative to the Linux current time. 00679 * 00680 * Since it is blocking rtf_sem_timed_wait cannot be used both in kernel and 00681 * user space. 00682 * 00683 * @retval 0 on success. 00684 * @retval -EINVAL if fd_fifo refers to an invalid file descriptor or fifo. 00685 */ 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 00704 /** 00705 * @ingroup fifos_ipc 00706 * Activate asynchronous notification of data availability 00707 * 00708 * rtf_set_async_sig activate an asynchronous signals to notify data 00709 * availability by catching a user set signal signum. 00710 * 00711 * @param signum is a user chosen signal number to be used, default is SIGIO. 00712 * 00713 * @retval -EINVAL if fd refers to a not opened fifo. 00714 */ 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 */

Generated on Thu Nov 20 11:49:49 2008 for RTAI API by doxygen 1.3.8