Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
serial_open.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 <fcntl.h>
24#include <termios.h>
25#include <unistd.h>
26
27/* Fallbacks for baud rates that might be missing on some systems */
28#ifndef B50
29#define B50 0000001
30#endif
31#ifndef B75
32#define B75 0000002
33#endif
34#ifndef B110
35#define B110 0000003
36#endif
37#ifndef B134
38#define B134 0000004
39#endif
40#ifndef B150
41#define B150 0000005
42#endif
43#ifndef B200
44#define B200 0000006
45#endif
46#ifndef B300
47#define B300 0000007
48#endif
49#ifndef B600
50#define B600 0000010
51#endif
52#ifndef B1200
53#define B1200 0000011
54#endif
55#ifndef B1800
56#define B1800 0000012
57#endif
58#ifndef B2400
59#define B2400 0000013
60#endif
61#ifndef B4800
62#define B4800 0000014
63#endif
64#ifndef B9600
65#define B9600 0000015
66#endif
67#ifndef B19200
68#define B19200 0000016
69#endif
70#ifndef B38400
71#define B38400 0000017
72#endif
73#ifndef B57600
74#define B57600 0010001
75#endif
76#ifndef B115200
77#define B115200 0010002
78#endif
79#ifndef B230400
80#define B230400 0010003
81#endif
82
83#ifndef O_NDELAY
84#define O_NDELAY O_NONBLOCK
85#endif
86#endif
87
89 /* Pops baud_rate (int), path (string); returns fd (int) or 0 */
90 Value baudv = pop_value(vm);
91 Value pathv = pop_value(vm);
92 int fd = 0;
93#ifdef __unix__
94 if (baudv.type != VAL_INT || pathv.type != VAL_STRING) {
95 fprintf(stderr, "Runtime type error: serial_open expects (string path, int baud_rate)\n");
96 free_value(baudv);
98 push_value(vm, make_int(0));
99 break;
100 }
101
102 const char *path = pathv.s ? pathv.s : "";
103 int baud = (int)baudv.i;
104 speed_t speed;
105
106 switch (baud) {
107 case 50:
108 speed = B50;
109 break;
110 case 75:
111 speed = B75;
112 break;
113 case 110:
114 speed = B110;
115 break;
116 case 134:
117 speed = B134;
118 break;
119 case 150:
120 speed = B150;
121 break;
122 case 200:
123 speed = B200;
124 break;
125 case 300:
126 speed = B300;
127 break;
128 case 600:
129 speed = B600;
130 break;
131 case 1200:
132 speed = B1200;
133 break;
134 case 1800:
135 speed = B1800;
136 break;
137 case 2400:
138 speed = B2400;
139 break;
140 case 4800:
141 speed = B4800;
142 break;
143 case 9600:
144 speed = B9600;
145 break;
146 case 19200:
147 speed = B19200;
148 break;
149 case 38400:
150 speed = B38400;
151 break;
152 case 57600:
153 speed = B57600;
154 break;
155 case 115200:
156 speed = B115200;
157 break;
158 case 230400:
159 speed = B230400;
160 break;
161 default:
162 speed = B9600;
163 break;
164 }
165
166 fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY);
167 if (fd != -1) {
168 struct termios options;
169 tcgetattr(fd, &options);
170 cfsetispeed(&options, speed);
171 cfsetospeed(&options, speed);
172 options.c_cflag |= (CLOCAL | CREAD);
173 options.c_cflag &= ~PARENB;
174 options.c_cflag &= ~CSTOPB;
175 options.c_cflag &= ~CSIZE;
176 options.c_cflag |= CS8;
177 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
178 options.c_iflag &= ~(IXON | IXOFF | IXANY);
179 options.c_oflag &= ~OPOST;
180 tcsetattr(fd, TCSANOW, &options);
181 fcntl(fd, F_SETFL, 0); // block on read
182 } else {
183 fd = 0;
184 }
185#endif
186 free_value(baudv);
188 push_value(vm, make_int(fd > 0 ? fd : 0));
189 break;
190}
@ OP_SERIAL_OPEN
Definition bytecode.h:241
free_value(baudv)
push_value(vm, make_int(fd > 0 ? fd :0))
Value pathv
Definition serial_open.c:91
int fd
Definition serial_open.c:92
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
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_STRING
Definition value.h:53
@ VAL_INT
Definition value.h:51
#define fprintf
Definition vm.c:200
Value path
Definition write_file.c:33