NetBurner 3.1
utils.h
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
5 #ifndef _NB_UTILS_H
6 #define _NB_UTILS_H
7 
8 // NB Definitions
9 #include <predef.h>
10 
11 // NB Libs
12 #include <stddef.h>
13 #ifndef NB_NET_TYPES_H
14 #include "nettypes.h"
15 #endif // NB_NET_TYPES_H
16 
17 #ifdef IPV6
18 #include <ipv6/ipv6_addr.h>
19 IPADDR6 AsciiToIp6(const char *p); // Convert an ASCII IP string to an IP address
20 #endif
21 
22 //
23 // Flags to control the debug print functions
24 //
25 #define DB_TCPIP (1)
26 #define DB_HTTP (2)
27 #define DB_ETHER (4)
28 #define DB_RTOS (8)
29 #define DB_BUFFER (16)
30 #define DB_PPP (32)
31 #define DB_AU (64)
32 #define DB_MAIL (128)
33 #define DB_IP (256)
34 #define DB_TCPDATA (512)
35 #define DB_SSL (1024)
36 #define DB_SNMP (2048)
37 #define DB_IPV6_ND (4096)
38 #define DB_IPV6_ICMP (8192)
39 #define DB_IPV6_ROUTE (16384)
40 #define DB_IPV6_FRAG (32768)
41 #define DB_IPV6_ERR (65536)
42 
44 // DIAGNOSTIC FUNCTIONS
45 //
46 void ShowCounters(); // Dump all system counters to stdio
47 void sShowCounters(char *buffer, int slen); // Dump all system counters to a large memory buffer
48 #ifdef FILE
49 void fShowCounters(FILE *fp); // Dump all system counters to a file descriptor
50 #endif // FILE
51 
53 // UTILITY I/O FUNCTIONS
54 //
55 void ShowData(const uint8_t *fromptr, uint16_t len); // Dump a block of data to stdio and show in ASCII and hex
56 void ShowMac(const MACADR *ma); // Dump a MAC address to stdio
57 void fdShowMac(int fd, const MACADR *ma); // Dump a MAC address to stdio
58 
59 inline void ShowMac(const MACADR &ma)
60 {
61  ShowMac(&ma);
62 }; // Dump a MAC address to stdio
63 
64 void snShowMac(char *buf, size_t maxlen, const MACADR *ma); // Dump a MAC address to char buf
65 inline void snShowMac(char *buf, size_t maxlen, const MACADR &ma)
66 {
67  snShowMac(buf, maxlen, &ma);
68 };
69 
70 void MacToID(MACADR *ma, char *IDBuf); // Write 6 character ID string based on unique portion of MAC
71 
72 void outbyte(char c); // Write out a single, unbuffered byte to stdio
73 void print(const char *); // Write out a zero-terminated, unbuffered string
74 void putnum(int i); // Write out a hexadecimal, unbuffered number to stdio
75 void putbytenum(unsigned char c); // Write out a hexadecimal, unbuffered byte to stdio
76 IPADDR4 AsciiToIp4(const char *p); // Convert an ASCII IP string to an IP address
77 MACADR AsciiToMac(const char *p);
78 BOOL ValidIPv4(const char *p);
79 
80 #ifndef IPV6
81 #define AsciiToIp AsciiToIp4
82 #else
83 #define AsciiToIp AsciiToIp6
84 #endif
85 
86 void ShowIP4(const IPADDR4 ia); // Dump an IP address in ASCII IP string format to stdio
87 int snShowIP4(char *buf, size_t maxlen, const IPADDR4 ia); // Dump an IP address in ASCII IP string format to char buf
88 
89 #ifdef IPV6
90 // Dump an IP address in ASCII IP string format to stdio
91 inline void ShowIP6(const IPADDR6 &ia)
92 {
93  ia.print();
94 };
95 inline int snShowIP6(char *buf, size_t maxlen, const IPADDR6 &ia)
96 {
97  return ia.sprintf(buf, maxlen);
98 };
99 #define ShowIP ShowIP6
100 #define snShowIP snShowIP6
101 #else
102 #define ShowIP ShowIP4
103 #define snShowIP snShowIP4
104 #endif
105 
106 // char *itoa( int value, char *buffer, int radix ); // Converts an integer to ASCII (adds the stdlib itoa)
107 
109 // HIGHER RESOLUTION COUNTER READ FUNCTION
110 //
111 extern uint32_t GetPreciseTime(void); // Gets the time tick since system start at a higher
112  // resolution, depending on the platform: 0.868-us for
113  // MOD5234/70, and 1.929-us for MOD5282
114 
115 extern "C"
116 {
117  int kill(int pid, int sig);
118  void _exit(int i);
119  int _fini(void);
120 }
121 
122 #ifdef _STDIO_H_
123 #ifndef FILE
124 void fShowCounters(FILE *fp);
125 #endif // FILE
126 void fShowMac(FILE *fp, const MACADR *ma);
127 
128 void fShowIP4(FILE *fp, const IPADDR4 ia);
129 
130 #ifdef IPV6
131 void fShowIP6(FILE *fp, const IPADDR6 &ia);
132 #define fShowIP fShowIP6
133 #else
134 #define fShowIP fShowIP4
135 #endif
136 
137 #endif // _STDIO_H_
138 
139 /*
140  Convert Binary to/from Hexadecimal ASCII
141 
142  Parameters:
143  fromBufferPtr - Source data
144  fromByteCount - Source byte count
145  toBufferPtr - Converted data
146  toByteCount - Converted byte count
147 
148  Return:
149  Pointer to next free byte in converted buffer
150 
151  Notes:
152  Hexadecimal ASCII buffer must be at least twice the size of the binary
153  buffer size.
154 
155 */
156 unsigned char *convertBinaryToHexAscii(unsigned char *fromBufferPtr,
157  unsigned int fromByteCount,
158  unsigned char *toBufferPtr,
159  unsigned int toByteCount);
160 
161 unsigned char *convertHexAsciiToBinary(unsigned char *fromBufferPtr,
162  unsigned int fromByteCount,
163  unsigned char *toBufferPtr,
164  unsigned int toByteCount);
165 
166 /* Search for a C string in an arbitrary memory blob that may contain NULL
167  * chars
168  *
169  * Paramemeters:
170  * seacrh - pointer to the buffer to search
171  * target - the C string being searched for
172  * len - length of the search buffer
173  *
174  * Return:
175  * pointer to the start of the target string in the search buffer
176  * or
177  * NULL if target not found in search buffer
178  */
179 const char *bufnstr(const char *search, const char *target, size_t len);
180 
182 // DEBUG FUNCTIONS
183 //
184 #if ((defined _DEBUG) || (defined _DEBUG_PRINT))
185 extern unsigned int DB_FLAGS;
186 void SetLogLevel();
187 #ifdef COLDFIRE
188 #define ASSERT(x) \
189  if (!(x)) \
190  { \
191  iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
192  asm("Halt"); \
193  }
194 #elif defined CORTEX_M7
195 #define ASSERT(x) \
196  if (!(x)) \
197  { \
198  iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
199  while (1) \
200  asm("wfi"); \
201  }
202 #endif
203 #define DBPRINT(x, y) \
204  if (DB_FLAGS & x) printf(y);
205 #define DBNUM(x, y) \
206  if (DB_FLAGS & x) putnum(y);
207 #define DBBYTE(x, y) \
208  if (DB_FLAGS & x) putbytenum(y);
209 #define DBPRINTF(x, ...) \
210  if (DB_FLAGS & x) printf(__VA_ARGS__);
211 #define DBIPRINTF(x, ...) \
212  if (DB_FLAGS & x) iprintf(__VA_ARGS__);
213 #else
214 #define ASSERT(x) ((void)0)
215 #define DBPRINT(x, y) ((void)0)
216 #define DBNUM(x, y) ((void)0)
217 #define DBBYTE(x, y) ((void)0)
218 #define DBPRINTF(x, ...) ((void)0)
219 #define DBIPRINTF(x, ...) ((void)0)
220 #endif // _DEBUG
221 
222 #if ((defined _DEBUG) || (defined _DEBUG_PRINT))
223 #define PPPDBPRINT(x) DBPRINT(DB_PPP, (x))
224 #define PPPDBPRINTF(...) DBPRINTF(DB_PPP, __VA_ARGS__)
225 #define PPPDBIPRINTF(...) DBIPRINTF(DB_PPP, __VA_ARGS__)
226 #define PPPDBNUM(x) DBNUM(DB_PPP, (x))
227 #define PPPDBBYTE(x) DBBYTE(DB_PPP, (x))
228 #else
229 #define PPPDBPRINT(x)
230 #define PPPDBPRINTF(...)
231 #define PPPDBIPRINTF(...)
232 #define PPPDBNUM(x)
233 #define PPPDBBYTE(x)
234 #endif
235 
237 // DEVELOPMENT BOARD I/O FUNCTIONS
238 //
239 BOOL OnModDev70(void); // Return TRUE if on MOD-DEV-70, otherwise FALSE
240 void putleds(unsigned char c); // Set the LEDs on the MOD-DEV-70/100
241 unsigned char getleds(); // Get the LEDs state from CPLD, MOD-DEV-100 only
242 void putdisp(unsigned short w); // Set the 7-segment LEDs on the MOD-DEV-100, BCD input, CPLD controls segmnet switching
243 unsigned short getdisp(); // Get the 7-segment LEDs on the MOD-DEV-100, BCD
244 unsigned char getdipsw(); // Read the DIP switch values on the MOD-DEV-70/100
245 
246 //--- The following functions are only valid on v1.12+ revisions of the dev board
247 //--- CPLD programming files are available to upgrade older board revs
248 // Set the 7-segment LEDs on the MOD-DEV-100
249 void putSegments(unsigned char DisplayMask,
250  unsigned char DisplayData); // Display Mask - 4bit value indicates which display shows value, DisplayData - Segment bit
251  // 7 is decimal point, bits 0-6 = Segments A-G
252 void putDecimal(unsigned char DisplayMask); // Set the 7-segment decimal point on the MOD-DEV-100, Display Mask - 4bit value indicates
253  // which display has decimal point
254 unsigned char getCPLDver(); // returns the version # of the CPLD
255 unsigned char getCPLDrev(); // returns the revision # of the CPLD
256 
257 // Predefined segments for the putSegments display function
258 #define SevenSeg_0 (0x3F)
259 #define SevenSeg_1 (0x06)
260 #define SevenSeg_2 (0x5B)
261 #define SevenSeg_3 (0x4F)
262 #define SevenSeg_4 (0x66)
263 #define SevenSeg_5 (0x6D)
264 #define SevenSeg_6 (0x7C)
265 #define SevenSeg_7 (0x07)
266 #define SevenSeg_8 (0x7F)
267 #define SevenSeg_9 (0x67)
268 #define SevenSeg_A (0x77)
269 #define SevenSeg_B (0x7F)
270 #define SevenSeg_C (0x39)
271 #define SevenSeg_D (0x3F)
272 #define SevenSeg_E (0x79)
273 #define SevenSeg_F (0x71)
274 #define SevenSeg_H (0x76)
275 #define SevenSeg_I (0x06)
276 #define SevenSeg_J (0x1E)
277 #define SevenSeg_L (0x38)
278 #define SevenSeg_O (0x3F)
279 #define SevenSeg_P (0x73)
280 #define SevenSeg_S (0x6D)
281 #define SevenSeg_U (0x3E)
282 #define SevenSeg_b (0x7C)
283 #define SevenSeg_c (0x58)
284 #define SevenSeg_d (0x5E)
285 #define SevenSeg_h (0x74)
286 #define SevenSeg_l (0x06)
287 #define SevenSeg_n (0x54)
288 #define SevenSeg_o (0x53)
289 #define SevenSeg_r (0x50)
290 #define SevenSeg_u (0x1C)
291 #define SevenSeg_Dash (0x40)
292 #define SevenSeg_Decimal (0x80)
293 #define SevenSeg_OFF (0x00)
294 
295 #endif /* #ifndef _NB_UTILS_H */
NetBurner IPADDR6 Class.
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition: ipv6_addr.h:28
void print(bool bCompact=true, bool bShowV4Raw=false) const
Print the IP address value to stdout.
Definition: ipv6_addr.cpp:101
int sprintf(char *cp, int maxl, bool bCompact=true, bool bShowV4Raw=false) const
Print the IP address to the specified buffer.
Definition: ipv6_addr.cpp:138