• R/O
  • SSH

libctools: Commit

This library contains code that extends and simplifies different operations
for C language based programs.


Commit MetaInfo

Revisiónc137b058d1fdf2effa357f963201ddf8a3e3141e (tree)
Tiempo2014-10-02 17:46:21
Autors.gusarov
Commiters.gusarov

Log Message

Initial commit.
Added files from the digital_phone/firmware repository.
trace.h and trace.cpp added from the maska_router_all/le920d repository.

Cambiar Resumen

Diferencia incremental

diff -r 000000000000 -r c137b058d1fd include/c_tools/asserts.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/include/c_tools/asserts.h Thu Oct 02 12:46:21 2014 +0400
@@ -0,0 +1,52 @@
1+/*
2+ * @author Sergey Gusarov <sgusarow@mail.ru>
3+ * @section LICENSE
4+ * Copyright (c) 2013 by SPT Ltd
5+ *
6+ * This software is copyrighted by and is the sole property of SPT Ltd.
7+ * All rights, title, ownership, or other interests
8+ * in the software remain the property of SPT Ltd. This
9+ * software may only be used in accordance with the corresponding
10+ * license agreement. Any unauthorized use, duplication, transmission,
11+ * distribution, or disclosure of this software is forbidden.
12+ *
13+ * This Copyright notice may not be removed or modified without prior
14+ * written consent of SPT Ltd.
15+ *
16+ * SPT Ltd reserves the right to modify this software without notice
17+ *
18+ * @section DESCRIPTION
19+ *
20+ */
21+
22+#pragma once
23+
24+#include <stdbool.h>
25+
26+/*
27+ We don't use c99 asserts because they don't provide infinite loop and
28+ don't signal hereby to watchdog timer
29+*/
30+
31+#if USE_SPT_ASSERTS
32+# define SPT_ASSERT(condition, ...)\
33+ do\
34+ {\
35+ if (!(condition))\
36+ {\
37+ TRACE_PERM_WP("ASSERT failed: " __VA_ARGS__);\
38+ while (1);\
39+ }\
40+ }\
41+ while (false)
42+# define RETURN_WITHOUT_ASSERT(result)
43+#else
44+# define SPT_ASSERT(expr, ...) do {((void)(expr));} while (0)
45+# define RETURN_WITHOUT_ASSERT(result) return result
46+#endif
47+
48+#define UNIQUE_NAME MAKE_NAME(__LINE__)
49+#define MAKE_NAME(line) MAKE_NAME2(line)
50+#define MAKE_NAME2(line) constraint_ ## line
51+
52+#define COMPILE_TIME_ASSERT(expr) struct UNIQUE_NAME {unsigned int bf : expr;}
diff -r 000000000000 -r c137b058d1fd include/c_tools/binary_const.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/include/c_tools/binary_const.h Thu Oct 02 12:46:21 2014 +0400
@@ -0,0 +1,39 @@
1+#pragma once
2+
3+#include <stdint.h>
4+
5+/*
6+ * Binary constant generator macros evaluating to compile-time constants
7+ *
8+ * Based on work by Tom Torfs donated to the public domain
9+ *
10+ * Sample usage:
11+ *
12+ * B8(01010101) = 85
13+ * B16(10101010,01010101) = 43605
14+ * B32(10000000,11111111,10101010,01010101) = 2164238933
15+ */
16+
17+/*
18+ * For up to 8-bit binary constants
19+ */
20+#define B8(b) ((uint8_t)B8__(HEX__(b)))
21+
22+/*
23+ * For up to 16-bit binary constants, MSB first
24+ */
25+#define B16(b1, b0) (((uint16_t)B8(b1) << 8) | B8(b0))
26+
27+/*
28+ * For up to 32-bit binary constants, MSB first
29+ */
30+#define B32(b3, b2, b1, b0) (((uint32_t)B8(b3) << 24) | ((uint32_t)B8(b2) << 16) |\
31+ ((uint32_t)B8(b1) << 8) | B8(b0))
32+
33+/*
34+ * Helper macros not to be used directly
35+ */
36+#define HEX__(n) 0x##n##UL
37+#define B8__(x) (((x&0x0000000FUL)?0x01:0) | ((x&0x000000F0UL)?0x02:0) | ((x&0x00000F00UL)?0x04:0) |\
38+ ((x&0x0000F000UL)?0x08:0) | ((x&0x000F0000UL)?0x10:0) | ((x&0x00F00000UL)?0x20:0) |\
39+ ((x&0x0F000000UL)?0x40:0) | ((x&0xF0000000UL)?0x80:0))
diff -r 000000000000 -r c137b058d1fd include/c_tools/bits.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/include/c_tools/bits.h Thu Oct 02 12:46:21 2014 +0400
@@ -0,0 +1,22 @@
1+/*
2+ * Source: http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c
3+ * [01.10.2013]
4+ * With modifications
5+ */
6+
7+#pragma once
8+
9+/* a = target variable, b = bit number to act upon 0-n */
10+#define BIT_SET(a, b) ((a) |= (1 << (b)))
11+#define BIT_CLEAR(a, b) ((a) &= ~(1 << (b)))
12+#define BIT_FLIP(a, b) ((a) ^= (1 << (b)))
13+#define BIT_CHECK(a, b) ((a) & (1 << (b)))
14+
15+/* x = target variable, y = mask */
16+#define BITMASK_SET(x, y) ((x) |= (y))
17+#define BITMASK_CLEAR(x, y) ((x) &= (~(y)))
18+#define BITMASK_FLIP(x, y) ((x) ^= (y))
19+#define BITMASK_CHECK(x, y) ((x) & (y))
20+
21+/* x = target variable, y = mask, z = new bits */
22+#define BITS_REPLACE(x, y, z) (((x) & (~(y))) | ((z) & (y)))
diff -r 000000000000 -r c137b058d1fd include/c_tools/c_callstack.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/include/c_tools/c_callstack.h Thu Oct 02 12:46:21 2014 +0400
@@ -0,0 +1,46 @@
1+#ifndef _NANOLAT_C_CALLSTACK_H_
2+#define _NANOLAT_C_CALLSTACK_H_ (1)
3+
4+/**************************************************************************
5+//
6+// Copyright 2013 Kangmo Kim, Nanolat Software.
7+//
8+// e-mail : kangmo@nanolat.com
9+//
10+// Licensed under the Apache License, Version 2.0 (the "License");
11+// you may not use this file except in compliance with the License.
12+// You may obtain a copy of the License at
13+//
14+// http://www.apache.org/licenses/LICENSE-2.0
15+//
16+// Unless required by applicable law or agreed to in writing, software
17+// distributed under the License is distributed on an "AS IS" BASIS,
18+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+// See the License for the specific language governing permissions and
20+// limitations under the License.
21+//
22+// c-callstack.h : Show Java-like callstack in C/C++ projects.
23+//
24+***************************************************************************/
25+
26+#include "trace.h"
27+
28+#if defined(NDEBUG) /* Release Mode */
29+
30+#define NL_RETURN(rc) do {return (rc);} while (false)
31+
32+#else /* Debug Mode */
33+
34+#define NL_RETURN(rc) \
35+ { \
36+ if ((rc)) { \
37+ TRACE_ERROR( \
38+ "Error(code:%d) at : %s (%s:%d)\n", \
39+ (rc), __PRETTY_FUNCTION__, __FILE__, __LINE__); \
40+ } \
41+ return (rc); \
42+ }
43+
44+#endif /* NDEBUG */
45+
46+#endif /* _NANOLAT_C_CALLSTACK_H_ */
diff -r 000000000000 -r c137b058d1fd include/c_tools/c_extensions.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/include/c_tools/c_extensions.h Thu Oct 02 12:46:21 2014 +0400
@@ -0,0 +1,30 @@
1+/*
2+ * @author Sergey Gusarov <sgusarow@mail.ru>
3+ * @section LICENSE
4+ * Copyright (c) 2013 by SPT Ltd
5+ *
6+ * This software is copyrighted by and is the sole property of SPT Ltd.
7+ * All rights, title, ownership, or other interests
8+ * in the software remain the property of SPT Ltd. This
9+ * software may only be used in accordance with the corresponding
10+ * license agreement. Any unauthorized use, duplication, transmission,
11+ * distribution, or disclosure of this software is forbidden.
12+ *
13+ * This Copyright notice may not be removed or modified without prior
14+ * written consent of SPT Ltd.
15+ *
16+ * SPT Ltd reserves the right to modify this software without notice
17+ *
18+ * @section DESCRIPTION
19+ *
20+ */
21+
22+#pragma once
23+
24+#include "binary_const.h"
25+
26+typedef unsigned int uint;
27+
28+#ifndef __GNUC__
29+typedef long ssize_t;
30+#endif
diff -r 000000000000 -r c137b058d1fd include/c_tools/trace.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/include/c_tools/trace.h Thu Oct 02 12:46:21 2014 +0400
@@ -0,0 +1,388 @@
1+/*
2+ * @author Sergey Gusarov <sgusarow@mail.ru>
3+ * @section LICENSE
4+ * Copyright (c) 2013 by SPT Ltd
5+ *
6+ * This software is copyrighted by and is the sole property of SPT Ltd.
7+ * All rights, title, ownership, or other interests
8+ * in the software remain the property of SPT Ltd. This
9+ * software may only be used in accordance with the corresponding
10+ * license agreement. Any unauthorized use, duplication, transmission,
11+ * distribution, or disclosure of this software is forbidden.
12+ *
13+ * This Copyright notice may not be removed or modified without prior
14+ * written consent of SPT Ltd.
15+ *
16+ * SPT Ltd reserves the right to modify this software without notice
17+ *
18+ * @section DESCRIPTION
19+ * This file was taken from the at91lib_20100901_softpack_1_9_v_1_0_svn_v15011.zip file
20+ * (latest available at91lib from Atmel's site). Many things are not actual for new projects.
21+ * Because of this file was completely reworked. C++ support is added.
22+ * Now it requires C99/C++98 compalible compiler.
23+ */
24+
25+/* ---------------------------------------------------------------------------------------------------------------------
26+ * ATMEL Microcontroller Software Support
27+ * ---------------------------------------------------------------------------------------------------------------------
28+ * Copyright (c) 2008, Atmel Corporation
29+ *
30+ * All rights reserved.
31+ *
32+ * Redistribution and use in source and binary forms, with or without
33+ * modification, are permitted provided that the following conditions are met:
34+ *
35+ * - Redistributions of source code must retain the above copyright notice,
36+ * this list of conditions and the disclaimer below.
37+ *
38+ * Atmel's name may not be used to endorse or promote products derived from
39+ * this software without specific prior written permission.
40+ *
41+ * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
42+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
44+ * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
45+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
47+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
50+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51+ * ---------------------------------------------------------------------------------------------------------------------
52+ */
53+
54+#pragma once
55+
56+// ---------------------------------------------------------------------------------------------------------------------
57+/// \unit
58+///
59+/// !Purpose
60+///
61+/// Standard output methods for reporting debug information, warnings and
62+/// errors, which can be easily be turned on/off.
63+///
64+/// !Usage
65+/// -# Uses the TRACE_DEBUG(), TRACE_INFO(), TRACE_WARNING(), TRACE_ERROR()
66+/// TRACE_FATAL() macros to output traces throughout the program.
67+/// -# Each type of trace has a level : Debug 5, Info 4, Warning 3, Error 2
68+/// and Fatal 1. Disable a group of traces by changing the value of
69+/// TRACE_LEVEL during compilation; traces with a level bigger than TRACE_LEVEL
70+/// are not generated. To generate no trace, use the reserved value 0.
71+/// -# Trace disabling can be static or dynamic. If dynamic disabling is selected
72+/// the trace level can be modified in runtime. If static disabling is selected
73+/// the disabled traces are not compiled.
74+///
75+/// !Trace level description
76+/// -# TRACE_DEBUG (5): Traces whose only purpose is for debugging the program,
77+/// and which do not produce meaningful information otherwise.
78+/// -# TRACE_INFO (4): Informational trace about the program execution. Should
79+/// enable the user to see the execution flow.
80+/// -# TRACE_WARNING (3): Indicates that a minor error has happened. In most case
81+/// it can be discarded safely; it may even be expected.
82+/// -# TRACE_ERROR (2): Indicates an error which may not stop the program execution,
83+/// but which indicates there is a problem with the code.
84+/// -# TRACE_FATAL (1): Indicates a major error which prevents the program from going
85+/// any further.
86+///
87+/// Traces compile time setup
88+/// TRACE_LEVEL_NO_TRACE can be defined to turn off all the traces.
89+/// TRACE_SYSLOG or TRACE_TERMINAL or TRACE_SYSLOG_AND_TERMINAL must be defined to point the traces direction.
90+/// TRACE_PLACEMENT can be defined if trace location (file name and line) is needed.
91+
92+#if defined(linux) || defined(__linux__)
93+# define TRACE_LINUX
94+#else
95+# undef TRACE_LINUX
96+#endif
97+
98+#ifdef TRACE_LINUX
99+# include <syslog.h>
100+#endif
101+
102+#ifndef TRACE_LEVEL_NO_TRACE
103+# if !defined(TRACE_SYSLOG) && !defined(TRACE_TERMINAL) && !defined(TRACE_SYSLOG_AND_TERMINAL)
104+# error TRACE_SYSLOG or TRACE_TERMINAL or TRACE_SYSLOG_AND_TERMINAL must be defined
105+# endif
106+
107+# if defined(TRACE_SYSLOG) && defined(TRACE_TERMINAL)
108+# error TRACE_SYSLOG or TRACE_TERMINAL must be defined, but not both
109+# endif
110+
111+# if defined(TRACE_SYSLOG_AND_TERMINAL) && defined(TRACE_TERMINAL)
112+# error TRACE_SYSLOG_AND_TERMINAL or TRACE_TERMINAL must be defined, but not both
113+# endif
114+
115+# if defined(TRACE_SYSLOG_AND_TERMINAL) && defined(TRACE_SYSLOG)
116+# error TRACE_SYSLOG_AND_TERMINAL or TRACE_SYSLOG must be defined, but not both
117+# endif
118+#endif
119+
120+#ifdef __cplusplus
121+
122+# include <cstdio>
123+# include <cstdbool>
124+
125+static inline void traceFatalAction(void) noexcept
126+{
127+}
128+
129+/* *INDENT-OFF* */
130+
131+namespace trace {
132+ extern "C" {
133+
134+/* *INDENT-ON* */
135+
136+#else
137+
138+# include <stdio.h>
139+# include <stdbool.h>
140+
141+static inline void traceFatalAction(void)
142+{
143+ while (true)
144+ ;
145+}
146+
147+#endif /* ifdef __cplusplus */
148+
149+#define ENDL "\r\n"
150+
151+#define TRACE_LEVEL_DEBUG 5
152+#define TRACE_LEVEL_INFO 4
153+#define TRACE_LEVEL_WARNING 3
154+#define TRACE_LEVEL_ERROR 2
155+#define TRACE_LEVEL_FATAL 1
156+#define TRACE_LEVEL_NO_TRACE 0
157+
158+// By default, all traces are output except the debug one.
159+#if !defined (TRACE_LEVEL)
160+# define TRACE_LEVEL TRACE_LEVEL_INFO
161+#endif
162+
163+// By default, trace level is static (not dynamic)
164+#if !defined (DYN_TRACES)
165+# define DYN_TRACES 0
166+#endif
167+
168+#if defined (NOTRACE)
169+# error "Error: NOTRACE has to be not defined!"
170+#endif
171+
172+#undef NOTRACE
173+#if (DYN_TRACES == 0)
174+# if (TRACE_LEVEL == TRACE_LEVEL_NO_TRACE)
175+# define NOTRACE
176+# endif
177+#endif
178+
179+/*
180+ * There are linking troubles when fprintf, flush and other such functions are used with Keil armcc
181+ */
182+#if defined (__arm__) && !defined(TRACE_LINUX)
183+# define TRACE_FATAL_STREAM
184+# define TRACE_ERROR_STREAM
185+# define TRACE_WARNING_STREAM
186+# define TRACE_INFO_STREAM
187+# define TRACE_DEBUG_STREAM
188+# define TRACE_PERM_STREAM
189+# if defined(TRACE_TERMINAL) || defined(TRACE_SYSLOG_AND_TERMINAL)
190+# define streamedPrintf(stream, ...) do {printf(__VA_ARGS__);} while (false)
191+# else
192+# define streamedPrintf(stream, ...) do {} while (false)
193+# endif
194+#else
195+/* !(defined (__arm__) && !defined(TRACE_LINUX)) */
196+# define TRACE_FATAL_STREAM stderr
197+# define TRACE_ERROR_STREAM stderr
198+# define TRACE_WARNING_STREAM stderr
199+# define TRACE_INFO_STREAM stdout
200+# define TRACE_DEBUG_STREAM stdout
201+# define TRACE_PERM_STREAM stdout
202+# if defined(TRACE_TERMINAL) || defined(TRACE_SYSLOG_AND_TERMINAL)
203+# define streamedPrintf(stream, ...) do {fprintf(stream, __VA_ARGS__); fflush(stream);} while (false)
204+# else
205+# define streamedPrintf(stream, ...) do {} while (false)
206+# endif
207+#endif
208+
209+/*
210+ * Wrapper over the printf().
211+ * One gcc extension is used here.
212+ * It seems that armcc is also supports it.
213+ */
214+
215+#ifdef TRACE_PLACEMENT
216+# define dprintf(stream, arg1, arg2, ...)\
217+ do {\
218+ streamedPrintf(stream, ENDL arg1 "%s:%d " arg2, __FILE__, __LINE__, ## __VA_ARGS__);\
219+ }\
220+ while (false)
221+#else
222+# define dprintf(stream, arg1, arg2, ...)\
223+ do {\
224+ streamedPrintf(stream, ENDL arg1 arg2, ## __VA_ARGS__);\
225+ }\
226+ while (false)
227+#endif
228+
229+/*
230+ * It is supposed that only daemons should write to syslog (if they want).
231+ * This feature is available in POSIX but so far supported only for Linux
232+ * @param level: one of LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG.
233+ * Associations:
234+ * TRACE_DEBUG ~ LOG_DEBUG
235+ * TRACE_INFO ~ LOG_INFO
236+ * TRACE_WARNING ~ LOG_WARNING
237+ * TRACE_ERROR ~ LOG_ERR
238+ * TRACE_FATAL ~ LOG_EMERG
239+ *
240+ * TODO: add associations checking
241+ */
242+#if (defined(TRACE_SYSLOG) || defined(TRACE_SYSLOG_AND_TERMINAL)) && defined(TRACE_LINUX)
243+# ifdef TRACE_PLACEMENT
244+# define dsyslog(level, arg1, ...)\
245+ do {\
246+ syslog(LOG_DAEMON | level, "%s:%d " arg1, __FILE__, __LINE__, ## __VA_ARGS__);\
247+ }\
248+ while (false)
249+# else
250+# define dsyslog(level, arg1, ...)\
251+ do {\
252+ syslog(LOG_DAEMON | level, arg1, ## __VA_ARGS__);\
253+ }\
254+ while (false)
255+# endif
256+#else
257+# define dsyslog(level, message, ...) do {} while (false)
258+#endif
259+
260+// ---------------------------------------------------------------------------------------------------------------------
261+/// Outputs a formatted string using <printf> if the log level is high
262+/// enough. Can be disabled by defining TRACE_LEVEL = 0 during compilation.
263+/// \param format Formatted string to output.
264+/// \param ... Additional parameters depending on formatted string.
265+// ---------------------------------------------------------------------------------------------------------------------
266+#if defined (NOTRACE)
267+
268+// Empty macro
269+#define TRACE_DEBUG(...) do {} while (false)
270+#define TRACE_INFO(...) do {} while (false)
271+#define TRACE_WARNING(...) do {} while (false)
272+#define TRACE_ERROR(...) do {} while (false)
273+#define TRACE_FATAL(...) do {traceFatalAction();} while (false)
274+#define TRACE_PERM(...) do {} while (false)
275+
276+#define TRACE_DEBUG_WP(...) do {} while (false)
277+#define TRACE_INFO_WP(...) do {} while (false)
278+#define TRACE_WARNING_WP(...) do {} while (false)
279+#define TRACE_ERROR_WP(...) do {} while (false)
280+#define TRACE_FATAL_WP(...) do {traceFatalAction();} while (false)
281+#define TRACE_PERM_WP(...) do {} while (false)
282+
283+#define TRACE_FUNC(arg1) do {} while (false)
284+
285+#elif (DYN_TRACES == 1)
286+
287+// Trace output depends on traceLevel value
288+#define TRACE_DEBUG(...) do {if (traceLevel >= TRACE_LEVEL_DEBUG) {dsyslog(LOG_DEBUG, __VA_ARGS__); dprintf(TRACE_DEBUG_STREAM, "-D- ", __VA_ARGS__);}} while (false)
289+
290+#define TRACE_INFO(...) do {if (traceLevel >= TRACE_LEVEL_INFO) {dsyslog(LOG_INFO, __VA_ARGS__); dprintf(TRACE_INFO_STREAM, "-I- ", __VA_ARGS__);}} while (false)
291+#define TRACE_WARNING(...) do {if (traceLevel >= TRACE_LEVEL_WARNING) {dsyslog(LOG_WARNING, __VA_ARGS__); dprintf(TRACE_WARNING_STREAM, "-W- ", __VA_ARGS__);}} while (false)
292+#define TRACE_ERROR(...) do {if (traceLevel >= TRACE_LEVEL_ERROR) {dsyslog(LOG_ERR, __VA_ARGS__); dprintf(TRACE_ERROR_STREAM, "-E- ", __VA_ARGS__);}} while (false)
293+#define TRACE_FATAL(...) do {if (traceLevel >= TRACE_LEVEL_FATAL) {dsyslog(LOG_EMERG, __VA_ARGS__); dprintf(TRACE_FATAL_STREAM, "-F- ", __VA_ARGS__); traceFatalAction();}} while (false)
294+#define TRACE_PERM(...) do {dsyslog(LOG_NOTICE, __VA_ARGS__); dprintf(TRACE_PERM_STREAM, "-P- ", __VA_ARGS__);} while (false)
295+
296+#define TRACE_DEBUG_WP(...) do {if (traceLevel >= TRACE_LEVEL_DEBUG) {dsyslog(LOG_DEBUG, __VA_ARGS__); dprintf(TRACE_DEBUG_STREAM, "", __VA_ARGS__);}} while (false)
297+#define TRACE_INFO_WP(...) do {if (traceLevel >= TRACE_LEVEL_INFO) {dsyslog(LOG_INFO, __VA_ARGS__); dprintf(TRACE_INFO_STREAM, "", __VA_ARGS__);}} while (false)
298+#define TRACE_WARNING_WP(...) do {if (traceLevel >= TRACE_LEVEL_WARNING) {dsyslog(LOG_WARNING, __VA_ARGS__); dprintf(TRACE_WARNING_STREAM, "", __VA_ARGS__);}} while (false)
299+#define TRACE_ERROR_WP(...) do {if (traceLevel >= TRACE_LEVEL_ERROR) {dsyslog(LOG_ERR, __VA_ARGS__); dprintf(TRACE_ERROR_STREAM, "", __VA_ARGS__);}} while (false)
300+#define TRACE_FATAL_WP(...) do {if (traceLevel >= TRACE_LEVEL_FATAL) {dsyslog(LOG_EMERG, __VA_ARGS__); dprintf(TRACE_FATAL_STREAM, "", __VA_ARGS__); traceFatalAction();}} while (false)
301+#define TRACE_PERM_WP(...) do {dsyslog(LOG_NOTICE, __VA_ARGS__); dprintf(TRACE_PERM_STREAM, "", __VA_ARGS__);} while (false)
302+
303+#define TRACE_FUNC(arg1) do {if (traceLevel >= TRACE_LEVEL_DEBUG) {dsyslog(LOG_DEBUG, arg1); streamedPrintf(TRACE_DEBUG_STREAM, "%s: %s" ENDL, __PRETTY_FUNCTION__, arg1);}} while (false)
304+
305+#else
306+/* !defined (NOTRACE) */
307+
308+// Trace compilation depends on TRACE_LEVEL value
309+#if (TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
310+#define TRACE_DEBUG(...) do {dsyslog(LOG_DEBUG, __VA_ARGS__); dprintf(TRACE_DEBUG_STREAM, "-D- ", __VA_ARGS__);} while (false)
311+#define TRACE_DEBUG_WP(...) do {dsyslog(LOG_DEBUG, __VA_ARGS__); dprintf(TRACE_DEBUG_STREAM, "", __VA_ARGS__);} while (false)
312+#define TRACE_FUNC(arg1) do {dsyslog(LOG_DEBUG, arg1); streamedPrintf(TRACE_DEBUG_STREAM, "%s: %s" ENDL, __PRETTY_FUNCTION__, arg1);} while (false)
313+#else
314+#define TRACE_DEBUG(...) do {} while (false)
315+#define TRACE_DEBUG_WP(...) do {} while (false)
316+#define TRACE_FUNC(arg1) do {} while (false)
317+#endif
318+
319+#if (TRACE_LEVEL >= TRACE_LEVEL_INFO)
320+#define TRACE_INFO(...) do {dsyslog(LOG_INFO, __VA_ARGS__); dprintf(TRACE_INFO_STREAM, "-I- ", __VA_ARGS__);} while (false)
321+#define TRACE_INFO_WP(...) do {dsyslog(LOG_INFO, __VA_ARGS__); dprintf(TRACE_INFO_STREAM, "", __VA_ARGS__);} while (false)
322+#else
323+#define TRACE_INFO(...) do {} while (false)
324+#define TRACE_INFO_WP(...) do {} while (false)
325+#endif
326+
327+#if (TRACE_LEVEL >= TRACE_LEVEL_WARNING)
328+#define TRACE_WARNING(...) do {dsyslog(LOG_WARNING, __VA_ARGS__); dprintf(TRACE_WARNING_STREAM, "-W- ", __VA_ARGS__);} while (false)
329+#define TRACE_WARNING_WP(...) do {dsyslog(LOG_WARNING, __VA_ARGS__); dprintf(TRACE_WARNING_STREAM, "", __VA_ARGS__);} while (false)
330+#else
331+#define TRACE_WARNING(...) do {} while (false)
332+#define TRACE_WARNING_WP(...) do {} while (false)
333+#endif
334+
335+#if (TRACE_LEVEL >= TRACE_LEVEL_ERROR)
336+#define TRACE_ERROR(...) do {dsyslog(LOG_ERR, __VA_ARGS__); dprintf(TRACE_ERROR_STREAM, "-E- ", __VA_ARGS__);} while (false)
337+#define TRACE_ERROR_WP(...) do {dsyslog(LOG_ERR, __VA_ARGS__); dprintf(TRACE_ERROR_STREAM, "", __VA_ARGS__);} while (false)
338+#else
339+#define TRACE_ERROR(...) do {} while (false)
340+#define TRACE_ERROR_WP(...) do {} while (false)
341+#endif
342+
343+#if (TRACE_LEVEL >= TRACE_LEVEL_FATAL)
344+#define TRACE_FATAL(...) do {dsyslog(LOG_EMERG, __VA_ARGS__); dprintf(TRACE_FATAL_STREAM, "-F- ", __VA_ARGS__); traceFatalAction();} while (false)
345+#define TRACE_FATAL_WP(...) do {dsyslog(LOG_EMERG, __VA_ARGS__); dprintf(TRACE_FATAL_STREAM, "", __VA_ARGS__); traceFatalAction();} while (false)
346+#else
347+#define TRACE_FATAL(...) do {traceFatalAction();} while (false)
348+#define TRACE_FATAL_WP(...) do {traceFatalAction();} while (false)
349+#endif
350+
351+#define TRACE_PERM(...) do {dsyslog(LOG_NOTICE, __VA_ARGS__); dprintf(TRACE_PERM_STREAM, "-P ", __VA_ARGS__);} while (false)
352+#define TRACE_PERM_WP(...) do {dsyslog(LOG_NOTICE, __VA_ARGS__); dprintf(TRACE_PERM_STREAM, "", __VA_ARGS__);} while (false)
353+
354+#endif /* defined (NOTRACE) */
355+
356+#undef NOTRACE
357+
358+// ---------------------------------------------------------------------------------------------------------------------
359+// Exported variables
360+// ---------------------------------------------------------------------------------------------------------------------
361+// Depending on DYN_TRACES, traceLevel is a modifiable runtime variable
362+// or a define
363+#if !defined (NOTRACE) && (DYN_TRACES == 1)
364+extern unsigned int traceLevel;
365+#endif
366+
367+// ---------------------------------------------------------------------------------------------------------------------
368+// Global Functions
369+// ---------------------------------------------------------------------------------------------------------------------
370+
371+extern void TRACE_DumpFrame(const unsigned char* const pFrame, unsigned int size);
372+
373+extern void TRACE_DumpMemory(unsigned char* pBuffer, unsigned int size, unsigned int address);
374+
375+extern unsigned char TRACE_GetInteger(unsigned int* pValue);
376+
377+extern unsigned char TRACE_GetIntegerMinMax(unsigned int* pValue, unsigned int min, unsigned int max);
378+
379+extern unsigned char TRACE_GetHexa32(unsigned int* pValue);
380+
381+/* *INDENT-OFF* */
382+
383+#ifdef __cplusplus
384+ } /* extern "C" */
385+} /* namespace trace */
386+#endif /* ifdef __cplusplus */
387+
388+/* *INDENT-ON* */
diff -r 000000000000 -r c137b058d1fd include/c_tools/unused.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/include/c_tools/unused.h Thu Oct 02 12:46:21 2014 +0400
@@ -0,0 +1,29 @@
1+/*
2+ * @author Sergey Gusarov <sgusarow@mail.ru>
3+ * @section LICENSE
4+ * Copyright (c) 2013 by SPT Ltd
5+ *
6+ * This software is copyrighted by and is the sole property of SPT Ltd.
7+ * All rights, title, ownership, or other interests
8+ * in the software remain the property of SPT Ltd. This
9+ * software may only be used in accordance with the corresponding
10+ * license agreement. Any unauthorized use, duplication, transmission,
11+ * distribution, or disclosure of this software is forbidden.
12+ *
13+ * This Copyright notice may not be removed or modified without prior
14+ * written consent of SPT Ltd.
15+ *
16+ * SPT Ltd reserves the right to modify this software without notice
17+ *
18+ * @section DESCRIPTION
19+ *
20+ */
21+
22+#pragma once
23+
24+#define SPT_UNUSED(var)\
25+ do\
26+ {\
27+ (void)var;\
28+ }\
29+ while (0)
diff -r 000000000000 -r c137b058d1fd readme.rst
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/readme.rst Thu Oct 02 12:46:21 2014 +0400
@@ -0,0 +1,27 @@
1+===================
2+Репозиторий c_tools
3+===================
4+
5+Общие сведения
6+--------------
7+
8+Содержит код, расширяющий и упрощающий различные операции в программах на языке C.
9+То есть содержит программные инструменты, которые можно использовать в любой развитой
10+программе на языке C. Весь код по возможности совместим со стандартом C99,
11+поэтому легко может также использоваться в c++ программах.
12+
13+
14+Целевые конфигурации
15+--------------------
16+
17+Любые, на которых работают компиляторы C99. Библиотека должна работать как на ограниченных
18+в ресурсах платформах (микроконтроллерах без ОС) так и под управлением современных ОС.
19+
20+Сборка
21+------
22+
23+В рамках данного репозитория не осуществляется, т.к. она практически
24+является header-only библиотекой.
25+Сборкой должен заниматься тот проект, который использует эту библиотеку.
26+Для этого он должен указать путь для поиска заголовочных файлов include
27+и линковать все файлы из src.
diff -r 000000000000 -r c137b058d1fd src/trace.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/trace.c Thu Oct 02 12:46:21 2014 +0400
@@ -0,0 +1,283 @@
1+/*
2+ * @author Sergey Gusarov <sgusarow@mail.ru>
3+ * @section LICENSE
4+ * Copyright (c) 2013 by SPT Ltd
5+ *
6+ * This software is copyrighted by and is the sole property of SPT Ltd.
7+ * All rights, title, ownership, or other interests
8+ * in the software remain the property of SPT Ltd. This
9+ * software may only be used in accordance with the corresponding
10+ * license agreement. Any unauthorized use, duplication, transmission,
11+ * distribution, or disclosure of this software is forbidden.
12+ *
13+ * This Copyright notice may not be removed or modified without prior
14+ * written consent of SPT Ltd.
15+ *
16+ * SPT Ltd reserves the right to modify this software without notice
17+ *
18+ * @section DESCRIPTION
19+ * This file was taken from the at91lib_20100901_softpack_1_9_v_1_0_svn_v15011.zip file
20+ * (latest available at91lib from Atmel's site). Many things are not actual for new projects.
21+ * Because of this file was completely reworked. C++ support is added.
22+ * Now it requires C99/C++98 compalible compiler.
23+ */
24+
25+/* ---------------------------------------------------------------------------------------------------------------------
26+ * ATMEL Microcontroller Software Support
27+ * ---------------------------------------------------------------------------------------------------------------------
28+ * Copyright (c) 2008, Atmel Corporation
29+ *
30+ * All rights reserved.
31+ *
32+ * Redistribution and use in source and binary forms, with or without
33+ * modification, are permitted provided that the following conditions are met:
34+ *
35+ * - Redistributions of source code must retain the above copyright notice,
36+ * this list of conditions and the disclaimer below.
37+ *
38+ * Atmel's name may not be used to endorse or promote products derived from
39+ * this software without specific prior written permission.
40+ *
41+ * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
42+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
44+ * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
45+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
47+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
50+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51+ * ---------------------------------------------------------------------------------------------------------------------
52+ */
53+
54+// ---------------------------------------------------------------------------------------------------------------------
55+// Headers
56+// ---------------------------------------------------------------------------------------------------------------------
57+
58+#include "core/trace.h"
59+
60+/* *INDENT-OFF* */
61+
62+#ifdef __cplusplus
63+namespace trace {
64+ extern "C" {
65+#endif /* ifdef __cplusplus */
66+
67+/* *INDENT-ON* */
68+
69+// ---------------------------------------------------------------------------------------------------------------------
70+// Internal variables
71+// ---------------------------------------------------------------------------------------------------------------------
72+
73+/// Trace level can be set at applet initialization
74+#if !defined (NOTRACE) && (DYN_TRACES == 1)
75+unsigned int traceLevel = TRACE_LEVEL;
76+#endif
77+
78+// ---------------------------------------------------------------------------------------------------------------------
79+// Local Functions
80+// ---------------------------------------------------------------------------------------------------------------------
81+
82+// ---------------------------------------------------------------------------------------------------------------------
83+/// Print char if printable. If not print a point
84+/// \param c char to
85+// ---------------------------------------------------------------------------------------------------------------------
86+static inline void printChar(const unsigned char c)
87+{
88+ if ((/*c >= 0x00 && */ c <= 0x1F) || (c >= 0xB0 && c <= 0xDF))
89+ printf(".");
90+ else
91+ printf("%c", c);
92+}
93+
94+// ---------------------------------------------------------------------------------------------------------------------
95+// Global Functions
96+// ---------------------------------------------------------------------------------------------------------------------
97+
98+// ---------------------------------------------------------------------------------------------------------------------
99+/// Displays the content of the given frame on the Trace interface.
100+/// \param pBuffer Pointer to the frame to dump.
101+/// \param size Buffer size in bytes.
102+// ---------------------------------------------------------------------------------------------------------------------
103+void TRACE_DumpFrame(const unsigned char* const pFrame, const unsigned int size)
104+{
105+ unsigned int i;
106+
107+ for (i = 0; i < size; i++)
108+ {
109+ printf("%02X ", pFrame[i]);
110+ }
111+
112+ printf(ENDL);
113+}
114+
115+// ---------------------------------------------------------------------------------------------------------------------
116+/// Displays the content of the given buffer on the Trace interface.
117+/// \param pBuffer Pointer to the buffer to dump.
118+/// \param size Buffer size in bytes.
119+/// \param address Start address to display
120+// ---------------------------------------------------------------------------------------------------------------------
121+void TRACE_DumpMemory(unsigned char* pBuffer, unsigned int size, unsigned int address)
122+{
123+ unsigned int i, j;
124+ unsigned int lastLineStart;
125+ unsigned char* pTmp;
126+
127+ for (i = 0; i < (size / 16); i++)
128+ {
129+ printf("0x%08X (%04x): ", address + (i * 16), (i * 16));
130+ pTmp = (unsigned char*)&pBuffer[i * 16];
131+ for (j = 0; j < 4; j++)
132+ {
133+ printf("%02X%02X%02X%02X ", pTmp[0], pTmp[1], pTmp[2], pTmp[3]);
134+ pTmp += 4;
135+ }
136+
137+ pTmp = (unsigned char*)&pBuffer[i * 16];
138+ for (j = 0; j < 16; j++)
139+ {
140+ printChar(*pTmp++);
141+ }
142+
143+ printf(ENDL);
144+ }
145+
146+ if ((size % 16) != 0)
147+ {
148+ lastLineStart = size - (size % 16);
149+ printf("0x%08X: ", address + lastLineStart);
150+
151+ for (j = lastLineStart; j < lastLineStart + 16; j++)
152+ {
153+ if ((j != lastLineStart) && (j % 4 == 0))
154+ printf(" ");
155+ if (j < size)
156+ printf("%02X", pBuffer[j]);
157+ else
158+ printf(" ");
159+ }
160+
161+ printf(" ");
162+ for (j = lastLineStart; j < size; j++)
163+ {
164+ printChar(pBuffer[j]);
165+ }
166+
167+ printf(ENDL);
168+ }
169+}
170+
171+// ---------------------------------------------------------------------------------------------------------------------
172+/// Reads an integer
173+// ---------------------------------------------------------------------------------------------------------------------
174+unsigned char TRACE_GetInteger(unsigned int* pValue)
175+{
176+ unsigned char key;
177+ unsigned char nbNb = 0;
178+ unsigned int value = 0;
179+
180+ while (1)
181+ {
182+ key = getchar();
183+ // putchar(key); // echo
184+ if (key >= '0' && key <= '9')
185+ {
186+ value = (value * 10) + (key - '0');
187+ nbNb++;
188+ }
189+ else if (key == 0x0D || key == ' ')
190+ {
191+ if (nbNb == 0)
192+ {
193+ printf(ENDL "Write a number and press ENTER or SPACE!" ENDL);
194+
195+ return 0;
196+ }
197+ else
198+ {
199+ printf(ENDL);
200+ *pValue = value;
201+
202+ return 1;
203+ }
204+ }
205+ else
206+ {
207+ printf(ENDL "'%c' not a number!" ENDL, key);
208+
209+ return 0;
210+ }
211+ }
212+}
213+
214+// ---------------------------------------------------------------------------------------------------------------------
215+/// Reads an integer and check the value
216+// ---------------------------------------------------------------------------------------------------------------------
217+unsigned char TRACE_GetIntegerMinMax(unsigned int* pValue, unsigned int min, unsigned int max)
218+{
219+ unsigned int value = 0;
220+
221+ if (TRACE_GetInteger(&value) == 0)
222+ return 0;
223+
224+ if (value < min || value > max)
225+ {
226+ printf(ENDL "The number have to be between %d and %d" ENDL, (int)min, (int)max);
227+
228+ return 0;
229+ }
230+
231+ printf(ENDL);
232+ *pValue = value;
233+
234+ return 1;
235+}
236+
237+// ---------------------------------------------------------------------------------------------------------------------
238+/// Reads an hexadecimal number
239+// ---------------------------------------------------------------------------------------------------------------------
240+unsigned char TRACE_GetHexa32(unsigned int* pValue)
241+{
242+ unsigned char key;
243+ unsigned int i = 0;
244+ unsigned int value = 0;
245+
246+ for (i = 0; i < 8; i++)
247+ {
248+ key = getchar();
249+ putchar(key);
250+ if (key >= '0' && key <= '9')
251+ {
252+ value = (value * 16) + (key - '0');
253+ }
254+ else if (key >= 'A' && key <= 'F')
255+ {
256+ value = (value * 16) + (key - 'A' + 10);
257+ }
258+ else if (key >= 'a' && key <= 'f')
259+ {
260+ value = (value * 16) + (key - 'a' + 10);
261+ }
262+ else
263+ {
264+ printf(ENDL "It is not a hexa character!" ENDL);
265+
266+ return 0;
267+ }
268+ }
269+
270+ printf(ENDL);
271+ *pValue = value;
272+
273+ return 1;
274+}
275+
276+/* *INDENT-OFF* */
277+
278+#ifdef __cplusplus
279+ } /* extern "C" */
280+} /* namespace trace */
281+#endif /* ifdef __cplusplus */
282+
283+/* *INDENT-ON* */
Show on old repository browser