Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
serial_config.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
21
22#ifdef __unix__
23#include <termios.h>
24#endif
25
27 /* Pops flow_control (int), stop_bits (int), parity (int), data_bits (int), fd (int); returns 1/0 */
28 Value flowv = pop_value(vm);
29 Value stopv = pop_value(vm);
30 Value parityv = pop_value(vm);
31 Value datav = pop_value(vm);
32 Value fdv = pop_value(vm);
33 int ok = 0;
34#ifdef __unix__
35 if (flowv.type != VAL_INT || stopv.type != VAL_INT || parityv.type != VAL_INT ||
36 datav.type != VAL_INT || fdv.type != VAL_INT) {
37 fprintf(stderr, "Runtime type error: serial_config expects (int fd, int data_bits, int parity, int stop_bits, int flow_control)\n");
38 } else {
39 int fd = (int)fdv.i;
40 int data_bits = (int)datav.i;
41 int parity = (int)parityv.i;
42 int stop_bits = (int)stopv.i;
43 int flow = (int)flowv.i;
44
45 struct termios options;
46 if (tcgetattr(fd, &options) == 0) {
47 // Data bits
48 options.c_cflag &= ~CSIZE;
49 switch (data_bits) {
50 case 5:
51 options.c_cflag |= CS5;
52 break;
53 case 6:
54 options.c_cflag |= CS6;
55 break;
56 case 7:
57 options.c_cflag |= CS7;
58 break;
59 case 8:
60 default:
61 options.c_cflag |= CS8;
62 break;
63 }
64
65 // Parity
66 switch (parity) {
67 case 0: // None
68 options.c_cflag &= ~PARENB;
69 break;
70 case 1: // Odd
71 options.c_cflag |= PARENB;
72 options.c_cflag |= PARODD;
73 break;
74 case 2: // Even
75 options.c_cflag |= PARENB;
76 options.c_cflag &= ~PARODD;
77 break;
78 }
79
80 // Stop bits
81 if (stop_bits == 2) {
82 options.c_cflag |= CSTOPB;
83 } else {
84 options.c_cflag &= ~CSTOPB;
85 }
86
87 // Flow control
88#ifdef CRTSCTS
89 if (flow == 1) { // Hardware (RTS/CTS)
90 options.c_cflag |= CRTSCTS;
91 } else {
92 options.c_cflag &= ~CRTSCTS;
93 }
94#endif
95
96 if (tcsetattr(fd, TCSANOW, &options) == 0) {
97 ok = 1;
98 }
99 }
100 }
101#endif
102 free_value(flowv);
108 break;
109}
@ OP_SERIAL_CONFIG
Definition bytecode.h:242
int ok
Definition contains.c:38
Value fdv
free_value(flowv)
Value datav
Value parityv
push_value(vm, make_int(ok))
Value stopv
int fd
Definition serial_open.c:92
Tagged union representing a Fun value.
Definition value.h:68
ValueType type
Definition value.h:69
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_INT
Definition value.h:51
#define fprintf
Definition vm.c:200