From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: =?UTF-8?Q?Bj=c3=b6rn_Lundin?= Newsgroups: comp.lang.ada Subject: Re: Read/write access to Unix character devices Date: Mon, 28 Dec 2020 14:26:53 +0100 Organization: A noiseless patient Spider Message-ID: References: <6ece98b8-a82e-40e7-9a0e-37b40a175fb0n@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Mon, 28 Dec 2020 13:26:54 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="162554c92546cd62be75e85cd8bf4dcd"; logging-data="3458"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18uLUnDlrY9flfFPIiXVEXo" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:78.0) Gecko/20100101 Thunderbird/78.5.1 Cancel-Lock: sha1:0VC18Bucb9ztpX/bZsI0O4Eu3vk= In-Reply-To: <6ece98b8-a82e-40e7-9a0e-37b40a175fb0n@googlegroups.com> Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:60964 List-Id: Den 2020-12-21 kl. 05:59, skrev philip...@gmail.com: > Lately I have been working with Unix (really Linux, FreeBSD, and OpenBSD) character devices (these happen to be USB raw HID devices, but the problem is more general than that). The way these work is that each hardware device has a character device node file in /dev/, like /dev/hidraw1. You open the file for both read and write access. Then you can send a command to the device by writing a binary blob and get a response by subsequently reading a binary blob. For what I am doing, it is important not to block on reads forever if there is not response forthcoming, so I need at least read timeouts. > > So far, I have been binding the C library functions open(), close(), read(), write(), and poll() with pragma Import. That works, but I have wondered if there is some way of accomplishing the same thing more portably. The packages GNAT.Sockets and GNAT.Serial_Communicatons can be viewed as special case solutions, but I would like a general solution. > > What I would really like is Ada.Sequential_IO with InOut_File and a timeout mechanism, perhaps like the select() wrapper in GNAT.Sockets. > > So far I haven't found anything in the Ada. or GNAT. that supports InOut_File semantics (other than Direct_IO) let alone timeouts. Does anybody have any suggestions? > > Phil > I'm perhaps too late for a useful suggestion, but I use this pattern when I pass messages through named pipes between unrelated processes. The reading process starts a task T1 which reads the pipe blocking. When data arrives it puts it in a list via a protected object, then it goes back to read blocking again. T1: loop read device PO.Put(data from device) exit when data is special shutdown-message end loop The main of the process does a IO.receive which in turn does a 'Get' on a protected object where 'get' is an entry of the PO with condition list.count > 0 main: loop IO.Receive(Data, Timeout) case Data.id is when timeout => DoSomethingElse when reply => Treat(Data) when shutdown => exit end case end loop; So - the main hangs on receive until a message arrives. And with receive, there is an optional timeout, that if > 0, starts a new task T2 that is a timer. If no message has arrived within time, a timeout message is put by T2 into the list, and the T2 dies. pkg IO procedure Receive (msg out: some type ; timeout : duration := 0.0) is TTP : Timer_Task_Pointer_Type; TO : aliased Duration := Timeout; begin if Timeout > 0.0 then TTP := new Timer_Task_Type(TO'Unchecked_access); PO.Get(msg); <-- we hang here until msg from device or timeout from T2 begin TTP.Stop; loop exit when TTP.all'Terminated; delay 0.0001; --workaround gnat bug end loop; exception when Tasking_Error => null; end; Free(TTP); end if; end receive; T2 task type Timer_Task_Type(Timeout_Time : access Duration) is entry Stop; end Timer_Task_Type; task body Timer_Task_Type is Timeout_Message : Message_Type; begin select delay Timeout_Time.all; Timeout_Message.Msg.Header.Identity := Time_Out_Identity; PO.Put(Timeout_Message); or accept Stop; end select; end Timer_Task_Type; type Timer_Task_Pointer_Type is access all Timer_Task_Type; procedure Free is new Unchecked_Deallocation(Timer_Task_Type,Timer_Task_Pointer_Type); Main is released by this timeout message and can do other things while T1 still hangs on the device waiting for data. To exit the process I post a special exit message that the task puts into the list and then exits its task loop, thus dies. Then the process shuts down when it acts upon the shutdown message. In your case you may not be able to send such a message to the task, but that does perhaps not matter. abort the task when shutting down your process - if you ever shut it down that is. -- Björn