summaryrefslogtreecommitdiff
path: root/StdLib/LibC/Uefi/InteractiveIO/TerminalFunctions.c
blob: 807ab1fd4cd7e8ee9244d9e980bebef55049e03e (plain)
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
/** @file
    "Terminal" Control functions for Interactive IO.

    Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>
    This program and the accompanying materials are licensed and made available under
    the terms and conditions of the BSD License that accompanies this distribution.
    The full text of the license may be found at
    http://opensource.org/licenses/bsd-license.

    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include  <Uefi.h>
#include  <Library/BaseMemoryLib.h>

#include  <LibConfig.h>

#include  <errno.h>
#include  <sys/termios.h>
#include  <Device/IIO.h>
#include  <MainData.h>

/** Get input baud rate.

    Extracts the input baud rate from the termios structure pointed to by the
    pTermios argument.

    @param[in]  pTermios  A pointer to the termios structure from which to extract
                          the input baud rate.

    @return The value of the input speed is returned exactly as it is contained
            in the termios structure, without interpretation.
**/
speed_t
cfgetispeed (
  const struct termios *pTermios
  )
{
  return pTermios->c_ispeed;
}

/** Get output baud rate.

    Extracts the output baud rate from the termios structure pointed to by the
    pTermios argument.

    @param[in]  pTermios  A pointer to the termios structure from which to extract
                          the output baud rate.

    @return The value of the output speed is returned exactly as it is contained
            in the termios structure, without interpretation.
**/
speed_t
cfgetospeed (
  const struct termios *pTermios
  )
{
  return pTermios->c_ospeed;
}

/** Set input baud rate.

    Replaces the input baud rate, in the termios structure pointed to by the
    pTermios argument, with the value of NewSpeed.

    @param[out]   pTermios  A pointer to the termios structure into which to set
                            the input baud rate.
    @param[in]    NewSpeed  The new input baud rate.

    @retval 0     The operation completed successfully.
    @retval -1    An error occured and errno is set to indicate the error.
                    * EINVAL - The value of NewSpeed is outside the range of
                      possible speed values as specified in <sys/termios.h>.
**/
int
cfsetispeed (
  struct termios *pTermios,
  speed_t         NewSpeed
  )
{
  int RetVal;

  if(NewSpeed < B921600) {
    pTermios->c_ispeed = NewSpeed;
    RetVal = 0;
  }
  else {
    RetVal = -1;
    errno = EINVAL;
  }
  return RetVal;
}

/** Set output baud rate.

    Replaces the output baud rate, in the termios structure pointed to by the
    pTermios argument, with the value of NewSpeed.

    @param[out]   pTermios  A pointer to the termios structure into which to set
                            the output baud rate.
    @param[in]    NewSpeed  The new output baud rate.

    @retval 0     The operation completed successfully.
    @retval -1    An error occured and errno is set to indicate the error.
                    * EINVAL - The value of NewSpeed is outside the range of
                      possible speed values as specified in <sys/termios.h>.
**/
int
cfsetospeed (
  struct termios *pTermios,
  speed_t         NewSpeed
  )
{
  int RetVal;

  if(NewSpeed < B921600) {
    pTermios->c_ospeed = NewSpeed;
    RetVal = 0;
  }
  else {
    RetVal = -1;
    errno = EINVAL;
  }
  return RetVal;
}

/** Get the parameters associated with an interactive IO device.

    Get the parameters associated with the device referred to by
    fd and store them into the termios structure referenced by pTermios.

    @param[in]    fd        The file descriptor for an open interactive IO device.
    @param[out]   pTermios  A pointer to a termios structure into which to store
                            attributes of the interactive IO device.

    @retval 0     The operation completed successfully.
    @retval -1    An error occured and errno is set to indicate the error.
                    * EBADF - The fd argument is not a valid file descriptor.
                    * ENOTTY - The file associated with fd is not an interactive IO device.
**/
int
tcgetattr (
  int             fd,
  struct termios *pTermios
  )
{
  cIIO             *IIO;
  int               RetVal;
  struct __filedes *filp;
  struct termios   *Termio;

  RetVal = 0;
  if(ValidateFD( fd, VALID_OPEN)) {
    filp = &gMD->fdarray[fd];

    if((filp->f_iflags & _S_ITTY) != 0) {
      // fd is for a TTY or "Interactive IO" device
      IIO = (cIIO *)filp->devdata;
      Termio = &IIO->Termio;
      (void)CopyMem((void *)pTermios, (const void *)Termio, sizeof(struct termios));
    }
    else {
      errno   = ENOTTY;
      RetVal  = -1;
    }
  }
  else {
    errno   = EBADF;
    RetVal  = -1;
  }
  return RetVal;
}

/** Set the parameters associated with an interactive IO device.

    Set the parameters associated with the device referred to by
    fd to the values in the termios structure referenced by pTermios.

    Behavior is modified by the value of the OptAct parameter:
      * TCSANOW: The change shall occur immediately.
      * TCSADRAIN: The change shall occur after all output written to fd is
        transmitted. This action should be used when changing parameters which
        affect output.
      * TCSAFLUSH: The change shall occur after all output written to fd is
        transmitted, and all input so far received but not read shall be
        discarded before the change is made.

    @param[in]  fd        The file descriptor for an open interactive IO device.
    @param[in]  OptAct    Currently has no effect.
    @param[in]  pTermios  A pointer to a termios structure into which to retrieve
                          attributes to set in the interactive IO device.

    @retval 0     The operation completed successfully.
    @retval -1    An error occured and errno is set to indicate the error.
                    * EBADF - The fd argument is not a valid file descriptor.
                    * ENOTTY - The file associated with fd is not an interactive IO device.
**/
int
tcsetattr (
  int                   fd,
  int                   OptAct,   // Currently ignored
  const struct termios *pTermios
  )
{
  cIIO             *IIO;
  int               RetVal;
  struct __filedes *filp;
  struct termios   *Termio;

  RetVal = 0;
  if(ValidateFD( fd, VALID_OPEN)) {
    filp = &gMD->fdarray[fd];

    if((filp->f_iflags & _S_ITTY) != 0) {
      // fd is for a TTY or "Interactive IO" device
      IIO = (cIIO *)filp->devdata;
      Termio = &IIO->Termio;
      (void)CopyMem((void *)Termio, (const void *)pTermios, sizeof(struct termios));
    }
    else {
      errno   = ENOTTY;
      RetVal  = -1;
    }
  }
  else {
    errno   = EBADF;
    RetVal  = -1;
  }
  return RetVal;
}

/** Transmit pending output.

    Function is not yet implemented for UEFI.

    @param[in]  fd        Ignored

    @retval -1    This function is not yet supported.  errno is set to ENOTSUP.
**/
int
tcdrain (int fd)
{
  errno = ENOTSUP;
  return -1;
}

/** Suspend or restart the transmission or reception of data.

    This function will suspend or resume transmission or reception of data on
    the file referred to by fd, depending on the value of Action.

    Function is not yet implemented for UEFI.

    @param[in]  fd        Ignored
    @param[in]  Action    Ignored

    @retval -1    This function is not yet supported.  errno is set to ENOTSUP.
**/
int
tcflow (
  int fd,
  int Action)
{
  errno = ENOTSUP;
  return -1;
}

/** Discard non-transmitted output data, non-read input data, or both.

    Function is not yet implemented for UEFI.

    @param[in]  fd              Ignored
    @param[in]  QueueSelector   Ignored

    @retval -1    This function is not yet supported.  errno is set to ENOTSUP.
**/
int
tcflush (
  int fd,
  int QueueSelector)
{
  errno = ENOTSUP;
  return -1;
}