NetBurner 3.5.0
PDF Version
 
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>
19IPADDR 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//
46void ShowCounters(); // Dump all system counters to stdio
47void sShowCounters(char *buffer, int slen); // Dump all system counters to a large memory buffer
48#ifdef FILE
49void fShowCounters(FILE *fp); // Dump all system counters to a file descriptor
50#endif // FILE
51
53// UTILITY I/O FUNCTIONS
54//
55void FdShowRingData(int fd, const uint8_t *ringBuf, uint32_t bufLen, // Dump part of a ring buffer to file descriptor
56 uint32_t start, uint32_t end, const char *indent=NULL);
57void ShowRingData(const uint8_t *ringBuf, uint32_t bufLen, // Dump part of a ring buffer to stdio
58 uint32_t start, uint32_t end, const char *indent=NULL);
59void FdShowData(int fd, const uint8_t *fromptr, uint16_t len); // Dump a block of data to file descriptor and show in ASCII and hex
60void ShowData(const uint8_t *fromptr, uint16_t len); // Dump a block of data to stdio and show in ASCII and hex
61void ShowMac(const MACADR *ma); // Dump a MAC address to stdio
62void fdShowMac(int fd, const MACADR *ma); // Dump a MAC address to stdio
63
64inline void ShowMac(const MACADR &ma)
65{
66 ShowMac(&ma);
67}; // Dump a MAC address to stdio
68
69void snShowMac(char *buf, size_t maxlen, const MACADR *ma); // Dump a MAC address to char buf
70inline void snShowMac(char *buf, size_t maxlen, const MACADR &ma)
71{
72 snShowMac(buf, maxlen, &ma);
73};
74
75void MacToID(MACADR *ma, char *IDBuf); // Write 6 character ID string based on unique portion of MAC
76
77void outbyte(char c); // Write out a single, unbuffered byte to stdio
78void print(const char *); // Write out a zero-terminated, unbuffered string
79void putnum(int i); // Write out a hexadecimal, unbuffered number to stdio
80void putbytenum(unsigned char c); // Write out a hexadecimal, unbuffered byte to stdio
81IPADDR4 AsciiToIp4(const char *p); // Convert an ASCII IP string to an IP address
82MACADR AsciiToMac(const char *p);
83BOOL ValidIPv4(const char *p);
84
85#ifndef IPV6
86#define AsciiToIp AsciiToIp4
87#else
88#define AsciiToIp AsciiToIp6
89#endif
90
91void ShowIP4(const IPADDR4 ia); // Dump an IP address in ASCII IP string format to stdio
92int snShowIP4(char *buf, size_t maxlen, const IPADDR4 ia); // Dump an IP address in ASCII IP string format to char buf
93
94#ifdef IPV6
95// Dump an IP address in ASCII IP string format to stdio
96inline void ShowIP6(const IPADDR &ia)
97{
98 ia.print();
99};
100inline int snShowIP6(char *buf, size_t maxlen, const IPADDR &ia)
101{
102 return ia.sprintf(buf, maxlen);
103};
104#define ShowIP ShowIP6
105#define snShowIP snShowIP6
106#else
107#define ShowIP ShowIP4
108#define snShowIP snShowIP4
109#endif
110
111// char *itoa( int value, char *buffer, int radix ); // Converts an integer to ASCII (adds the stdlib itoa)
112
114// HIGHER RESOLUTION COUNTER READ FUNCTION
115//
116extern uint32_t GetPreciseTime(void); // Gets the time tick since system start at a higher
117 // resolution, depending on the platform: 0.868-us for
118 // MOD5234/70, and 1.929-us for MOD5282
119
125unsigned long long Get_msec();
126
127extern "C"
128{
129 int kill(int pid, int sig);
130 void _exit(int i);
131 int _fini(void);
132}
133
134#ifdef _STDIO_H_
135#ifndef FILE
136void fShowCounters(FILE *fp);
137#endif // FILE
138void fShowMac(FILE *fp, const MACADR *ma);
139
140void fShowIP4(FILE *fp, const IPADDR4 ia);
141
142#ifdef IPV6
143void fShowIP6(FILE *fp, const IPADDR &ia);
144#define fShowIP fShowIP6
145#else
146#define fShowIP fShowIP4
147#endif
148
149#endif // _STDIO_H_
150
151/*
152 Convert Binary to/from Hexadecimal ASCII
153
154 Parameters:
155 fromBufferPtr - Source data
156 fromByteCount - Source byte count
157 toBufferPtr - Converted data
158 toByteCount - Converted byte count
159
160 Return:
161 Pointer to next free byte in converted buffer
162
163 Notes:
164 Hexadecimal ASCII buffer must be at least twice the size of the binary
165 buffer size.
166
167*/
168unsigned char *convertBinaryToHexAscii(unsigned char *fromBufferPtr,
169 unsigned int fromByteCount,
170 unsigned char *toBufferPtr,
171 unsigned int toByteCount);
172
173unsigned char *convertHexAsciiToBinary(unsigned char *fromBufferPtr,
174 unsigned int fromByteCount,
175 unsigned char *toBufferPtr,
176 unsigned int toByteCount);
177
178/* Search for a C string in an arbitrary memory blob that may contain NULL
179 * chars
180 *
181 * Paramemeters:
182 * seacrh - pointer to the buffer to search
183 * target - the C string being searched for
184 * len - length of the search buffer
185 *
186 * Return:
187 * pointer to the start of the target string in the search buffer
188 * or
189 * NULL if target not found in search buffer
190 */
191const char *bufnstr(const char *search, const char *target, size_t len);
192
194// DEBUG FUNCTIONS
195//
196#if ((defined _DEBUG) || (defined _DEBUG_PRINT))
197extern unsigned int DB_FLAGS;
198void SetLogLevel();
199#ifdef COLDFIRE
200#define ASSERT(x) \
201 if (!(x)) \
202 { \
203 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
204 asm("Halt"); \
205 }
206#elif defined CORTEX_M7
207#define ASSERT(x) \
208 if (!(x)) \
209 { \
210 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
211 while (1) \
212 asm("wfi"); \
213 }
214#else
215#define ASSERT(x) \
216 if (!(x)) \
217 { \
218 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
219 while (1) \
220 ; \
221 }
222#endif
223#define DBPRINT(x, y) \
224 if (DB_FLAGS & x) printf(y);
225#define DBNUM(x, y) \
226 if (DB_FLAGS & x) putnum(y);
227#define DBBYTE(x, y) \
228 if (DB_FLAGS & x) putbytenum(y);
229#define DBPRINTF(x, ...) \
230 if (DB_FLAGS & x) printf(__VA_ARGS__);
231#define DBIPRINTF(x, ...) \
232 if (DB_FLAGS & x) iprintf(__VA_ARGS__);
233#else
234#define ASSERT(x) ((void)0)
235#define DBPRINT(x, y) ((void)0)
236#define DBNUM(x, y) ((void)0)
237#define DBBYTE(x, y) ((void)0)
238#define DBPRINTF(x, ...) ((void)0)
239#define DBIPRINTF(x, ...) ((void)0)
240#endif // _DEBUG
241
242#if ((defined _DEBUG) || (defined _DEBUG_PRINT))
243#define PPPDBPRINT(x) DBPRINT(DB_PPP, (x))
244#define PPPDBPRINTF(...) DBPRINTF(DB_PPP, __VA_ARGS__)
245#define PPPDBIPRINTF(...) DBIPRINTF(DB_PPP, __VA_ARGS__)
246#define PPPDBNUM(x) DBNUM(DB_PPP, (x))
247#define PPPDBBYTE(x) DBBYTE(DB_PPP, (x))
248#else
249/*
250 #define PPPDBPRINT(x) printf(x);
251 #define PPPDBPRINTF(...) printf(__VA_ARGS__)
252 #define PPPDBIPRINTF(...) printf(__VA_ARGS__)
253 #define PPPDBNUM(x) printf("%08X",x)
254 #define PPPDBBYTE(x) printf("%02X",x)
255*/
256#define PPPDBPRINT(x)
257#define PPPDBPRINTF(...)
258#define PPPDBIPRINTF(...)
259#define PPPDBNUM(x)
260#define PPPDBBYTE(x)
261
262#endif
263
264void LocalOutByte(char c);
265extern "C"
266{
267 void Local_OutString(const char *cp);
268}
270// USER_ENTER_CRITICAL ASSERTION
271//
272#if ((defined _DEBUG) || (defined USER_CRITICAL_SANITY))
273#ifdef COLDFIRE
274#define USER_CRITICAL_ASSERT() \
275 if (critical_count != 0) \
276 { \
277 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
278 while (1) \
279 asm("Halt"); \
280 }
281#elif (defined CORTEX_M7)
282#define USER_CRITICAL_ASSERT() \
283 if (critical_count != 0) \
284 { \
285 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
286 while (1) \
287 asm("wfi"); \
288 }
289#else
290#define USER_CRITICAL_ASSERT() \
291 if (critical_count != 0) \
292 { \
293 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
294 while (1) \
295 ; \
296 }
297#endif
298#else // !((defined _DEBUG) || (defined USER_CRITICAL_SANITY))
299#define USER_CRITICAL_ASSERT() ((void)0)
300#endif
301
303// DEVELOPMENT BOARD I/O FUNCTIONS
304//
305BOOL OnModDev70(void); // Return TRUE if on MOD-DEV-70, otherwise FALSE
306void putleds(unsigned char c); // Set the LEDs on the MOD-DEV-70/100
307unsigned char getleds(); // Get the LEDs state from CPLD, MOD-DEV-100 only
308void putdisp(unsigned short w); // Set the 7-segment LEDs on the MOD-DEV-100, BCD input, CPLD controls segmnet switching
309unsigned short getdisp(); // Get the 7-segment LEDs on the MOD-DEV-100, BCD
310unsigned char getdipsw(); // Read the DIP switch values on the MOD-DEV-70/100
311
312//--- The following functions are only valid on v1.12+ revisions of the dev board
313//--- CPLD programming files are available to upgrade older board revs
314// Set the 7-segment LEDs on the MOD-DEV-100
315void putSegments(unsigned char DisplayMask,
316 unsigned char DisplayData); // Display Mask - 4bit value indicates which display shows value, DisplayData - Segment bit
317 // 7 is decimal point, bits 0-6 = Segments A-G
318void putDecimal(unsigned char DisplayMask); // Set the 7-segment decimal point on the MOD-DEV-100, Display Mask - 4bit value indicates
319 // which display has decimal point
320unsigned char getCPLDver(); // returns the version # of the CPLD
321unsigned char getCPLDrev(); // returns the revision # of the CPLD
322
323// Predefined segments for the putSegments display function
324#define SevenSeg_0 (0x3F)
325#define SevenSeg_1 (0x06)
326#define SevenSeg_2 (0x5B)
327#define SevenSeg_3 (0x4F)
328#define SevenSeg_4 (0x66)
329#define SevenSeg_5 (0x6D)
330#define SevenSeg_6 (0x7C)
331#define SevenSeg_7 (0x07)
332#define SevenSeg_8 (0x7F)
333#define SevenSeg_9 (0x67)
334#define SevenSeg_A (0x77)
335#define SevenSeg_B (0x7F)
336#define SevenSeg_C (0x39)
337#define SevenSeg_D (0x3F)
338#define SevenSeg_E (0x79)
339#define SevenSeg_F (0x71)
340#define SevenSeg_H (0x76)
341#define SevenSeg_I (0x06)
342#define SevenSeg_J (0x1E)
343#define SevenSeg_L (0x38)
344#define SevenSeg_O (0x3F)
345#define SevenSeg_P (0x73)
346#define SevenSeg_S (0x6D)
347#define SevenSeg_U (0x3E)
348#define SevenSeg_b (0x7C)
349#define SevenSeg_c (0x58)
350#define SevenSeg_d (0x5E)
351#define SevenSeg_h (0x74)
352#define SevenSeg_l (0x06)
353#define SevenSeg_n (0x54)
354#define SevenSeg_o (0x53)
355#define SevenSeg_r (0x50)
356#define SevenSeg_u (0x1C)
357#define SevenSeg_Dash (0x40)
358#define SevenSeg_Decimal (0x80)
359#define SevenSeg_OFF (0x00)
360
361#endif /* #ifndef _NB_UTILS_H */
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:208
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
void print(bool bCompact=true, bool bShowV4Raw=false) const
Print the IP address value to stdout.
int sprintf(char *cp, int maxl, bool bCompact=true, bool bShowV4Raw=false) const
Print the IP address to the specified buffer.
Used to store and manipulate MAC addresses.
Definition nettypes.h:69
NetBurner IPADDR6 Class.
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.