TizenRT Libs&Environment  v2.0 M2
hex2bin.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the License.
16  *
17  ****************************************************************************/
18 /****************************************************************************
19  * apps/include/hex2bin.h
20  *
21  * Copyright (C) 2014 Gregory Nutt. All rights reserved.
22  * Author: Gregory Nutt <gnutt@nuttx.org>
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  *
28  * 1. Redistributions of source code must retain the above copyright
29  * notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  * notice, this list of conditions and the following disclaimer in
32  * the documentation and/or other materials provided with the
33  * distribution.
34  * 3. Neither the name NuttX nor the names of its contributors may be
35  * used to endorse or promote products derived from this software
36  * without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
44  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
45  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
46  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
48  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49  * POSSIBILITY OF SUCH DAMAGE.
50  *
51  ****************************************************************************/
52 
53 #ifndef __APPS_INCLUDE_HEX2BIN_H
54 #define __APPS_INCLUDE_HEX2BIN_H
55 
56 /****************************************************************************
57  * Included Files
58  ****************************************************************************/
59 
60 #include <tinyara/config.h>
61 
62 #include <stdint.h>
63 
64 #ifdef CONFIG_SYSTEM_HEX2BIN
65 
66 /****************************************************************************
67  * Pre-processor Definitions
68  ****************************************************************************/
69 /* Configuration ************************************************************/
70 
71 #ifndef CONFIG_SYSTEM_HEX2BIN_BASEADDR
72 #define CONFIG_SYSTEM_HEX2BIN_BASEADDR 0x00000000
73 #endif
74 
75 #ifndef CONFIG_SYSTEM_HEX2BIN_ENDPADDR
76 #define CONFIG_SYSTEM_HEX2BIN_ENDPADDR 0x00000000
77 #endif
78 
79 #ifndef CONFIG_SYSTEM_HEX2BIN_SWAP
80 #define CONFIG_SYSTEM_HEX2BIN_SWAP 0
81 #endif
82 
83 /* Some environments may return CR as end-of-line, others LF, and others
84  * both. If not specified, the logic here assumes either (but not both) as
85  * the default.
86  */
87 
88 #if defined(CONFIG_EOL_IS_CR)
89 #undef CONFIG_EOL_IS_LF
90 #undef CONFIG_EOL_IS_BOTH_CRLF
91 #undef CONFIG_EOL_IS_EITHER_CRLF
92 #elif defined(CONFIG_EOL_IS_LF)
93 #undef CONFIG_EOL_IS_CR
94 #undef CONFIG_EOL_IS_BOTH_CRLF
95 #undef CONFIG_EOL_IS_EITHER_CRLF
96 #elif defined(CONFIG_EOL_IS_BOTH_CRLF)
97 #undef CONFIG_EOL_IS_CR
98 #undef CONFIG_EOL_IS_LF
99 #undef CONFIG_EOL_IS_EITHER_CRLF
100 #elif defined(CONFIG_EOL_IS_EITHER_CRLF)
101 #undef CONFIG_EOL_IS_CR
102 #undef CONFIG_EOL_IS_LF
103 #undef CONFIG_EOL_IS_BOTH_CRLF
104 #else
105 #undef CONFIG_EOL_IS_CR
106 #undef CONFIG_EOL_IS_LF
107 #undef CONFIG_EOL_IS_BOTH_CRLF
108 #define CONFIG_EOL_IS_EITHER_CRLF 1
109 #endif
110 
111 /* Debug from hex2bin code */
112 
113 #ifdef CONFIG_CPP_HAVE_VARARGS
114 #ifdef CONFIG_SYSTEM_HEX2BIN_DEBUG
115 #define hex2bin_debug(format, ...) fprintf(stderr, format, ##__VA_ARGS__)
116 #else
117 #define hex2bin_debug(...)
118 #endif
119 #else
120 #ifdef CONFIG_SYSTEM_HEX2BIN_DEBUG
121 #define hex2bin_debug printf
122 #else
123 #define hex2bin_debug (void)
124 #endif
125 #endif
126 
127 /****************************************************************************
128  * Public Types
129  ****************************************************************************/
130 /* Intel HEX data steams are normally in big endian order. The following
131  * enumeration selects other ordering.
132  */
133 
134 enum hex2bin_swap_e {
135  HEX2BIN_NOSWAP = 0, /* No swap, stream is in the correct byte order */
136  HEX2BIN_SWAP16 = 1, /* Swap bytes in 16-bit values */
137  HEX2BIN_SWAP32 = 2 /* Swap bytes in 32-bit values */
138 };
139 
140 /****************************************************************************
141  * Public Data
142  ****************************************************************************/
143 
144 #ifdef __cplusplus
145 // *INDENT-OFF*
146 #define EXTERN extern "C"
147 extern "C" {
148 #else
149 #define EXTERN extern
150 // *INDENT-ON*
151 #endif
152 
153 /****************************************************************************
154  * Public Function Prototypes
155  ****************************************************************************/
156 
157 /****************************************************************************
158  * Name: hex2bin
159  *
160  * Description:
161  * Read the Intel HEX ASCII data provided on the serial IN stream and write
162  * the binary to the seek-able serial OUT stream.
163  *
164  * These streams may be files or, in another usage example, the IN stream
165  * could be a serial port and the OUT stream could be a memory stream. This
166  * would decode and write the serial input to memory.
167  *
168  * Input Parameters:
169  * instream - The incoming stream from which Intel HEX data will be
170  * received.
171  * outstream - The outgoing stream in which binary data will be written.
172  * baseaddr - The base address of the outgoing stream. Seeking in the
173  * output stream will be relative to this address.
174  * endpaddr - The end address (plus 1) of the outgoing stream. This
175  * value is used only for range checking. endpaddr must
176  * be larger than baseaddr. A zero value for endpaddr
177  * disables range checking.
178  * swap - Controls byte ordering. See enum hex2bin_swap_e for
179  * description of the values.
180  *
181  * Returned Value
182  * Zero (OK) is returned on success; a negated errno value is returned on
183  * failure.
184  *
185  ****************************************************************************/
186 
187 struct lib_instream_s;
188 struct lib_sostream_s;
189 int hex2bin(FAR struct lib_instream_s *instream, FAR struct lib_sostream_s *outstream, uint32_t baseaddr, uint32_t endpaddr, enum hex2bin_swap_e swap);
190 
191 /****************************************************************************
192  * Name hex2mem
193  *
194  * Description:
195  * Read the Intel HEX ASCII data provided on the file descriptor 'fd' and
196  * write the binary to memory.
197  *
198  * If, for example, fd is zero (corresponding to stdin), then the HEX
199  * ASCII data would be taken from the console and written to memory.
200  *
201  * Input Parameters:
202  * fd - The file descriptor from which Intel HEX data will be
203  * received.
204  * baseaddr - The base address of the memory region stream.
205  * endpaddr - The end address (plus 1) of the memory region.
206  * swap - Controls byte ordering. See enum hex2bin_swap_e for
207  * description of the values.
208  *
209  * Returned Value
210  * Zero (OK) is returned on success; a negated errno value is returned on
211  * failure.
212  *
213  ****************************************************************************/
214 
215 int hex2mem(int fd, uint32_t baseaddr, uint32_t endpaddr, enum hex2bin_swap_e swap);
216 
217 /****************************************************************************
218  * Name fhex2mem
219  *
220  * Description:
221  * Read the Intel HEX ASCII data provided on the standard stream
222  * 'instream' and write the binary to memory.
223  *
224  * If, for example, instream is stdin, then the HEX ASCII data would be
225  * taken from the console and written to memory.
226  *
227  * Input Parameters:
228  * instream - The incoming standard stream from which Intel HEX data
229  * will be received.
230  * baseaddr - The base address of the memory region stream.
231  * endpaddr - The end address (plus 1) of the memory region.
232  * swap - Controls byte ordering. See enum hex2bin_swap_e for
233  * description of the values.
234  *
235  * Returned Value
236  * Zero (OK) is returned on success; a negated errno value is returned on
237  * failure.
238  *
239  ****************************************************************************/
240 
241 int fhex2mem(FAR FILE * instream, uint32_t baseaddr, uint32_t endpaddr, enum hex2bin_swap_e swap);
242 
243 /****************************************************************************
244  * Name: hex2bin_main
245  *
246  * Description:
247  * Main entry point when hex2bin is built as an TASH built-in task.
248  *
249  * Input Parameters:
250  * Standard task inputs
251  *
252  * Returned Value
253  * EXIT_SUCESS on success; EXIT_FAILURE on failure
254  *
255  ****************************************************************************/
256 
257 #ifdef CONFIG_SYSTEM_HEX2BIN_BUILTIN
258 int hex2bin_main(int argc, char **argv);
259 #endif /* CONFIG_SYSTEM_HEX2BIN_BUILTIN */
260 
261 /****************************************************************************
262  * Name: hex2mem_main
263  *
264  * Description:
265  * Main entry point when hex2mem is built as an TASH built-in task.
266  *
267  * Input Parameters:
268  * Standard task inputs
269  *
270  * Returned Value
271  * EXIT_SUCESS on success; EXIT_FAILURE on failure
272  *
273  ****************************************************************************/
274 
275 #ifdef CONFIG_SYSTEM_HEX2MEM_BUILTIN
276 int hex2mem_main(int argc, char **argv);
277 #endif /* CONFIG_SYSTEM_HEX2MEM_BUILTIN */
278 
279 #undef EXTERN
280 #ifdef __cplusplus
281 // *INDENT-OFF*
282 }
283 // *INDENT-ON*
284 #endif
285 
286 #endif /* CONFIG_SYSTEM_HEX2BIN */
287 #endif /* __APPS_INCLUDE_HEX2BIN_H */
Structure for getting character from the instream.
Definition: streams.h:88
Structure for putting character to the outstream with seek operation support.
Definition: streams.h:133
struct file_struct FILE
Definition: stdio.h:188