Commit 2db8582b authored by Matheus Andrade's avatar Matheus Andrade Committed by Mart Lubbers

Checking for device errors on TTYAvailable

parent ce8c8653
...@@ -190,18 +190,29 @@ void ttyread(int fd, int *ch, int *fdo) ...@@ -190,18 +190,29 @@ void ttyread(int fd, int *ch, int *fdo)
debug("ttyread done"); debug("ttyread done");
} }
void ttyavailable(int fd, int *r, int *fdo) void ttyavailable(int fd, int *r, int *e, int *fdo)
{ {
// debug("ttyavailable"); // debug("ttyavailable");
fd_set fds; fd_set rfds, efds;
struct timeval tv; struct timeval tv;
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 0; tv.tv_usec = 0;
FD_ZERO(&fds); FD_ZERO(&rfds);
FD_SET(fd, &fds); FD_SET(fd, &rfds);
FD_ZERO(&efds);
FD_SET(fd, &efds);
*e = 0;
*r = select(fd+1, &rfds, NULL, &efds, &tv);
if (FD_ISSET(fd, &efds)){
*e = 1;
*fdo = fd;
return;
}
*r = select(fd+1, &fds, NULL, NULL, &tv);
if(*r == -1) if(*r == -1)
die("select"); die("select");
*fdo = fd; *fdo = fd;
......
...@@ -29,5 +29,12 @@ TTYerror :: !*env -> (!String, !*env) ...@@ -29,5 +29,12 @@ TTYerror :: !*env -> (!String, !*env)
TTYopen :: !TTYSettings !*env -> (!Bool,!*TTY,!*env) TTYopen :: !TTYSettings !*env -> (!Bool,!*TTY,!*env)
TTYread :: !*TTY -> (!Int, !*TTY) TTYread :: !*TTY -> (!Int, !*TTY)
TTYreadline :: !*TTY -> (!String, !*TTY) TTYreadline :: !*TTY -> (!String, !*TTY)
TTYavailable :: !*TTY -> (!Bool, !*TTY) /**
* Checks if the TTY device is available for reading
*
* @param The TTY Device
* @return A 3-tuple containing whether there is data available,
* whether there was an error and the TTY device
*/
TTYavailable :: !*TTY -> (!Bool, !Bool, !*TTY)
TTYwrite :: !String !*TTY -> *TTY TTYwrite :: !String !*TTY -> *TTY
...@@ -90,9 +90,9 @@ TTYwrite _ _ = code { ...@@ -90,9 +90,9 @@ TTYwrite _ _ = code {
ccall ttywrite "SI:I" ccall ttywrite "SI:I"
} }
TTYavailable :: !*TTY -> (!Bool, !*TTY) TTYavailable :: !*TTY -> (!Bool, !Bool, !*TTY)
TTYavailable _ = code { TTYavailable _ = code {
ccall ttyavailable "I:VII" ccall ttyavailable "I:VIII"
} }
TTYerror :: !*env -> (!String, !*env) TTYerror :: !*env -> (!String, !*env)
......
...@@ -72,9 +72,10 @@ where ...@@ -72,9 +72,10 @@ where
, {iworld & resources=[TTYd dp tty:resources]}) , {iworld & resources=[TTYd dp tty:resources]})
(r,s,ss) (r,s,ss)
# tty = foldr TTYwrite tty $ reverse $ map enc s # tty = foldr TTYwrite tty $ reverse $ map enc s
# (newdata, tty) = readWhileAvailable tty # (merr, tty) = readWhileAvailable tty
| isError merr = (exc (fromError merr), iworld)
# iworld = {iworld & resources=[TTYd dp tty:iworld.resources]} # iworld = {iworld & resources=[TTYd dp tty:iworld.resources]}
= case dec (acc +++ toString newdata) of = case dec (acc +++ toString (fromOk merr)) of
(Left err, newacc) = (exc "Error while parsing", iworld) (Left err, newacc) = (exc "Error while parsing", iworld)
(Right msgs, newacc) (Right msgs, newacc)
# (merr, iworld) = if (msgs =: [] && s =: []) # (merr, iworld) = if (msgs =: [] && s =: [])
...@@ -104,10 +105,12 @@ where ...@@ -104,10 +105,12 @@ where
getResource = iworldResource (\t=:(TTYd p _)->(p == opts.devicePath, t)) getResource = iworldResource (\t=:(TTYd p _)->(p == opts.devicePath, t))
exc = ExceptionResult o exception exc = ExceptionResult o exception
readWhileAvailable :: !*TTY -> ([Char], !*TTY) readWhileAvailable :: !*TTY -> (MaybeError String [Char], !*TTY)
readWhileAvailable tty readWhileAvailable tty
# (available, tty) = TTYavailable tty # (available, error, tty) = TTYavailable tty
| not available = ([], tty) | error = (Error "TTY device disconnected", tty)
| not available = (Ok [], tty)
# (c, tty) = TTYread tty # (c, tty) = TTYread tty
# (cs, tty) = readWhileAvailable tty # (merr, tty) = readWhileAvailable tty
= ([toChar c:cs], tty) | isError merr = (merr, tty)
= (Ok [toChar c:fromOk merr], tty)
...@@ -17,7 +17,7 @@ Start w ...@@ -17,7 +17,7 @@ Start w
# (ok, tty, w) = TTYopen {zero & devicePath="/dev/ttyUSB0"} w # (ok, tty, w) = TTYopen {zero & devicePath="/dev/ttyUSB0"} w
| not ok = TTYerrorclose io w | not ok = TTYerrorclose io w
#! tty = TTYwrite "echo123\n" tty #! tty = TTYwrite "echo123\n" tty
#! (av, tty) = TTYavailable tty #! (av, e, tty) = TTYavailable tty
# io = io <<< ("Bytes available: " +++ toString av +++ "\n") # io = io <<< ("Bytes available: " +++ toString av +++ "\n")
#! (l, tty) = TTYreadline tty #! (l, tty) = TTYreadline tty
# io = io <<< ("Line read: " +++ l) # io = io <<< ("Line read: " +++ l)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment