~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 4a2108ed8808d86eb9c974d911e1b2ee15370deb CTU-FEE-B0B35APO-Semestral-project/lib-pheripherals/src/nonblocking_io.c -rw-r--r-- 1.1 KiB
4a2108ed — František Boháček feat: add wTahoma 13 3 years ago
                                                                                
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
#include "nonblocking_io.h"
#include <stdint.h>
#include <asm-generic/errno-base.h>
#include <asm-generic/errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>

int file_set_blocking(int file, struct termios *old) {
  int oldfl;
  oldfl = fcntl(file, F_GETFL);
  if (oldfl == -1) {
    return oldfl;
  }

  if(tcsetattr(file, TCSANOW, old) == -1) {
    return -1;
  }

  return fcntl(file, F_SETFL, oldfl & ~O_NONBLOCK);
}

int file_set_nonblocking(int file, struct termios *old)
{
  fcntl(file, F_SETFL, O_NONBLOCK);

  struct termios attrs;
  if (tcgetattr(file, &attrs) < 0) {
    return -1;
  }

  if (old != NULL) {
    tcgetattr(file, old);
  }

  cfmakeraw(&attrs);

  tcsetattr(file, TCSANOW, &attrs);
  return 1;
}

int file_read_nonblocking(int file, size_t max_size, uint8_t *data)
{
  int read_bytes = read(file, data, max_size);

  int error = errno;
  if (read_bytes == -1 && error == EAGAIN) {
    read_bytes = 0; // Do not treat EAGAIN as an error.
  } else {
    errno = error;
  }

  return read_bytes;
}
Do not follow this link