1 /******************************************************************************
2 @file unix_fdrw.h
4 @brief TIMAC 2.0 API Unix style File descriptor read/write operations
6 Group: WCS LPC
7 $Target Devices: Linux: AM335x, Embedded Devices: CC1310, CC1350, CC1352$
9 ******************************************************************************
10 $License: BSD3 2016 $
12 Copyright (c) 2015, Texas Instruments Incorporated
13 All rights reserved.
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
19 * Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
22 * Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in the
24 documentation and/or other materials provided with the distribution.
26 * Neither the name of Texas Instruments Incorporated nor the names of
27 its contributors may be used to endorse or promote products derived
28 from this software without specific prior written permission.
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
32 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
37 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
39 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 ******************************************************************************
42 $Release Name: TI-15.4Stack Linux x64 SDK$
43 $Release Date: Sept 27, 2017 (2.04.00.13)$
44 *****************************************************************************/
46 #if !defined(UNIX_FDRW_H)
47 #define UNIX_FDRW_H
49 /*!
50 * OVERVIEW
51 * ========
52 *
53 * This file handles RD/WR operations for a Unix File Descriptor using rd/wr
54 * and also handles any FIFO activty with respect to that FIFO
55 *
56 * Use cases:
57 *
58 * <ul>
59 * <li> Case #1 - (No Fifo involved)
60 * No fifo is involved, rd/wr the file descriptor.
61 *
62 * <li> Case #2 - (Read with Fifo)
63 * - for reading, there is a "read thread"
64 * - This read thread is blocked waiting for data
65 * when it gets data, it wakes up and fills the fifo.
66 * - When the APP reads, it reads from the FIFO
67 *
68 * <li> Case #3 - (Write with Fifo)
69 * Same idea as read with fifo
70 * </ul>
71 *
72 * Logic is as follows:
73 * \code
74 * if(fifo_handle == 0){
75 * we read/write the file descriptor directly.
76 * return
77 * } else {
78 * if(v_bytes==NULL){
79 * device rd_thread is active
80 * transfer bytes from fd to fifo
81 * return;
82 * if(c_bytes==NULL){
83 * device wr_thread is active
84 * transfer bytes from fifo to fd
85 * } else {
86 * FD is not used
87 * app thread is active
88 * transfer bytes from buffer to fifo
89 * }
90 * }
91 * \endcode
92 */
94 #include <stdint.h>
95 #include <stdbool.h>
96 #include <stddef.h>
98 /*!
99 * @struct Unix File Descriptor (fd) RW Parameters.
100 */
101 struct unix_fdrw {
102 /*! File descriptor */
103 intptr_t fd; // could be HANDLE on windows
105 /*! are we reading or writing with respect to the file descriptor? */
106 int rw;
108 /*! Are we using a fifo? If so this is the handle */
109 intptr_t fifo_handle;
111 /*! if we make logs, what prefix do we use */
112 const char *log_prefix;
114 /*! log flags for this operation */
115 int64_t log_why;
116 int64_t log_why_raw;
118 /*! if not-null, this is the transfer buffer */
119 const void *c_bytes;
121 /*! if not null this is the transfer buffer */
122 void *v_bytes;
124 /*! how many did we do? */
125 size_t n_done;
127 /*! how many do we need to do? */
128 size_t n_todo;
130 /*! what is the timeout value? */
131 int mSecs_timeout;
133 /*! Did we have an error? */
134 bool is_error;
136 /*! If socket, this is set when we detect a close */
137 bool is_connected;
139 /*! Is this a socket? uart or other
140 * 'f' file
141 * 's' socket
142 * 'u' uart
143 * 'i' i2c
144 * 'p' spi
145 */
146 int type;
148 const char *this_c_buf;
149 char *this_v_buf;
150 size_t this_len;
151 int this_actual;
152 };
154 /*!
155 * @brief Process rd/wr requests against a unix file descriptor, or fifo
156 * @param pRW - the rw details.
157 * @return negative on error, or number of bytes transfered
158 */
159 int UNIX_fdRw(struct unix_fdrw *pRW);
161 /*
162 * @brief Platform specific rd/wr operations at the "this" level
163 * @param pRW - the rw details.
164 */
165 void UNIX_fdWrThis(struct unix_fdrw *pRW);
167 /*
168 * @brief Platform specific rd/wr operations at the "this" level
169 * @param pRW - the rw details.
170 */
171 void UNIX_fdRdThis(struct unix_fdrw *pRW);
173 /*!
174 * @brief determine if a unix file descriptor is readable
175 *
176 * @param pURW - contains file descriptor, and timeout periods
177 *
178 * @returns positive if readable, 0 if not, negative for errors
179 *
180 */
181 int POLL_readable(struct unix_fdrw *pURW);
183 /*!
184 * @brief Determine if a file descriptor is writeable
185 *
186 * @param pURW - contains file descriptor, and timeout periods
187 *
188 * @returns positive if writeable, 0 if not, negative for errors
189 *
190 */
191 int POLL_writable(struct unix_fdrw *pURW);
193 #endif
195 /*
196 * ========================================
197 * Texas Instruments Micro Controller Style
198 * ========================================
199 * Local Variables:
200 * mode: c
201 * c-file-style: "bsd"
202 * tab-width: 4
203 * c-basic-offset: 4
204 * indent-tabs-mode: nil
205 * End:
206 * vim:set filetype=c tabstop=4 shiftwidth=4 expandtab=true
207 */