source: rtems/cpukit/libmisc/mw-fb/mw_uid.c @ c8b0f1cb

4.104.114.84.95
Last change on this file since c8b0f1cb was c8b0f1cb, checked in by Ralf Corsepius <ralf.corsepius@…>, on 02/06/07 at 05:57:07

Use size_t for sizes.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 *
3 * Copyright (c) 2000 - Rosimildo da Silva
4 *
5 * MODULE DESCRIPTION:
6 * This module implements the input devices interface used by MicroWindows
7 * in an embedded system environment.
8 * It uses the RTEMS message queue as the repository for the messages posted
9 * by the devices registered.
10 *
11 *  $Id$
12 */
13
14#ifdef HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <stdio.h>
19#include <fcntl.h>
20#include <sys/ioctl.h>
21#include <errno.h>
22#include <rtems.h>
23
24#include <rtems/mw_uid.h>
25#include <rtems/seterr.h>
26
27static rtems_id   queue_id = 0;
28static int open_count = 0;
29
30/*
31#define MW_DEBUG_ON     1
32*/
33
34/* open a message queue with the kernel */
35int uid_open_queue( const char *q_name, int flags, size_t max_msgs )
36{
37   static rtems_name queue_name;
38
39   /*
40    * For the first device calling this function we would create the queue.
41    * It is assumed that this call is done at initialization, and no concerns
42    * regarding multi-threading is taken in consideration here.
43    */
44   if( !open_count )
45   {
46      rtems_status_code status;
47      queue_name = rtems_build_name( q_name[0],
48                                     q_name[1],
49                                     q_name[2],
50                                     q_name[3] );
51      status = rtems_message_queue_create( queue_name,
52                                           max_msgs,
53                                           sizeof( struct MW_UID_MESSAGE ),
54                                           RTEMS_FIFO | RTEMS_LOCAL,
55                                           &queue_id );
56      if( status != RTEMS_SUCCESSFUL )
57      {
58#ifdef MW_DEBUG_ON
59        printk( "UID_Queue: error creating queue: %d\n", status );
60#endif
61        return -1;
62      }
63#ifdef MW_DEBUG_ON
64      printk( "UID_Queue: id=%X\n", queue_id );
65#endif
66   }
67   open_count++;
68   return 0;
69}
70
71
72/* close message queue */
73int uid_close_queue( void )
74{
75  if( open_count == 1 )
76  {
77     rtems_message_queue_delete( queue_id );
78     queue_id = 0;
79  }
80  open_count--;
81  return 0;
82}
83
84/* reads for a message from the device */
85int uid_read_message( struct MW_UID_MESSAGE *m, unsigned long timeout )
86{
87  rtems_status_code status;
88  size_t            size = 0;
89  unsigned long micro_secs = timeout*1000;
90  int wait = ( timeout != 0 );
91
92  status = rtems_message_queue_receive( queue_id,
93                                       (void*)m,
94                                       &size,
95                                       wait ? RTEMS_WAIT : RTEMS_NO_WAIT,
96                                       TOD_MICROSECONDS_TO_TICKS(micro_secs ) );
97
98  if( status == RTEMS_SUCCESSFUL )
99  {
100     return size;
101  }
102  else if( ( status == RTEMS_UNSATISFIED ) || ( status == RTEMS_TIMEOUT ) )
103  {
104     /* this macro returns -1 */
105     rtems_set_errno_and_return_minus_one( ETIMEDOUT );
106  }
107  /* Here we have one error condition */
108#ifdef MW_DEBUG_ON
109  printk( "UID_Queue: error reading queue: %d\n", status );
110#endif
111  return -1;
112}
113
114
115/*
116 * add a message to the queue of events. This method cna be used to
117 * simulate hardware events, and it can be very handy during development
118 * a new interface.
119 */
120int uid_send_message( struct MW_UID_MESSAGE *m )
121{
122  rtems_status_code status;
123  status = rtems_message_queue_send( queue_id, ( void * )m,
124                                    sizeof( struct MW_UID_MESSAGE ) );
125  return status == RTEMS_SUCCESSFUL ? 0 : -1;
126}
127
128/*
129 * register the device to insert events to the message
130 * queue named as the value passed in q_name
131 */
132int uid_register_device( int fd, const char *q_name )
133{
134  return ioctl( fd, MW_UID_REGISTER_DEVICE, q_name );
135}
136
137/* tell this device to stop adding events to the queue */
138int uid_unregister_device( int fd )
139{
140  return ioctl( fd, MW_UID_UNREGISTER_DEVICE, NULL );
141}
142
143/* set the keyboard */
144int uid_set_kbd_mode( int fd, int mode, int *old_mode )
145{
146   if (ioctl( fd, MV_KDGKBMODE, old_mode) < 0)
147   {
148      return -1;
149   }
150   if (ioctl(fd, MV_KDSKBMODE, mode ) < 0 )
151   {
152      return -1;
153   }
154   return 0;
155}
Note: See TracBrowser for help on using the repository browser.