NetBurner 3.5.6
PDF Version
utils.h
Go to the documentation of this file.
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
15#ifndef _NB_UTILS_H
16#define _NB_UTILS_H
17
18// NB Definitions
19#include <predef.h>
20
21// NB Libs
22#include <stddef.h>
23#ifndef NB_NET_TYPES_H
24#include "nettypes.h"
25#endif // NB_NET_TYPES_H
26
27#ifdef IPV6
28#include <ipv6/ipv6_addr.h>
36IPADDR AsciiToIp6(const char *p);
37#endif
38
42#define DB_TCPIP (1)
43#define DB_HTTP (2)
44#define DB_ETHER (4)
45#define DB_RTOS (8)
46#define DB_BUFFER (16)
47#define DB_PPP (32)
48#define DB_AU (64)
49#define DB_MAIL (128)
50#define DB_IP (256)
51#define DB_TCPDATA (512)
52#define DB_SSL (1024)
53#define DB_SNMP (2048)
54#define DB_IPV6_ND (4096)
55#define DB_IPV6_ICMP (8192)
56#define DB_IPV6_ROUTE (16384)
57#define DB_IPV6_FRAG (32768)
58#define DB_IPV6_ERR (65536)
59
61// DIAGNOSTIC FUNCTIONS
62//
63
68
75void sShowCounters(char *buffer, int slen);
76
77#ifdef FILE
83void fShowCounters(FILE *fp);
84#endif // FILE
85
87// UTILITY I/O FUNCTIONS
88//
99void FdShowRingData(int fd, const uint8_t *ringBuf, uint32_t bufLen,
100 uint32_t start, uint32_t end, const char *indent=NULL);
110void ShowRingData(const uint8_t *ringBuf, uint32_t bufLen,
111 uint32_t start, uint32_t end, const char *indent=NULL);
119void FdShowData(int fd, const uint8_t *fromptr, uint16_t len);
120
127void ShowData(const uint8_t *fromptr, uint16_t len);
128
134void ShowMac(const MACADR *ma);
135
142void fdShowMac(int fd, const MACADR *ma);
143
149inline void ShowMac(const MACADR &ma)
150{
151 ShowMac(&ma);
152};
153
161void snShowMac(char *buf, size_t maxlen, const MACADR *ma);
162
170inline void snShowMac(char *buf, size_t maxlen, const MACADR &ma)
171{
172 snShowMac(buf, maxlen, &ma);
173};
174
181void MacToID(MACADR *ma, char *IDBuf);
182
188void outbyte(char c);
194void print(const char *str);
200void putnum(int i);
206void putbytenum(unsigned char c);
214IPADDR4 AsciiToIp4(const char *p);
222MACADR AsciiToMac(const char *p);
230BOOL ValidIPv4(const char *p);
231
232#ifndef IPV6
233#define AsciiToIp AsciiToIp4
234#else
235#define AsciiToIp AsciiToIp6
236#endif
237
243void ShowIP4(const IPADDR4 ia);
244
254int snShowIP4(char *buf, size_t maxlen, const IPADDR4 ia);
255
256#ifdef IPV6
262inline void ShowIP6(const IPADDR &ia)
263{
264 ia.print();
265};
266
276inline int snShowIP6(char *buf, size_t maxlen, const IPADDR &ia)
277{
278 return ia.sprintf(buf, maxlen);
279};
280#define ShowIP ShowIP6
281#define snShowIP snShowIP6
282#else
283#define ShowIP ShowIP4
284#define snShowIP snShowIP4
285#endif
286
296// char *itoa( int value, char *buffer, int radix );
297
299// HIGHER RESOLUTION COUNTER READ FUNCTION
300//
301
309extern uint32_t GetPreciseTime(void);
310
316unsigned long long Get_msec();
317
318extern "C"
319{
328 int kill(int pid, int sig);
329
335 void _exit(int i);
336
342 int _fini(void);
343}
344
345#ifdef _STDIO_H_
346#ifndef FILE
352void fShowCounters(FILE *fp);
353#endif // FILE
354
361void fShowMac(FILE *fp, const MACADR *ma);
362
369void fShowIP4(FILE *fp, const IPADDR4 ia);
370
371#ifdef IPV6
378void fShowIP6(FILE *fp, const IPADDR &ia);
379#define fShowIP fShowIP6
380#else
381#define fShowIP fShowIP4
382#endif
383
384#endif // _STDIO_H_
385
398unsigned char *convertBinaryToHexAscii(unsigned char *fromBufferPtr,
399 unsigned int fromByteCount,
400 unsigned char *toBufferPtr,
401 unsigned int toByteCount);
402
413unsigned char *convertHexAsciiToBinary(unsigned char *fromBufferPtr,
414 unsigned int fromByteCount,
415 unsigned char *toBufferPtr,
416 unsigned int toByteCount);
417
427const char *bufnstr(const char *search, const char *target, size_t len);
428
430// DEBUG FUNCTIONS
431//
432#if ((defined _DEBUG) || (defined _DEBUG_PRINT))
433extern unsigned int DB_FLAGS;
434void SetLogLevel();
435#ifdef COLDFIRE
436#define ASSERT(x) \
437 if (!(x)) \
438 { \
439 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
440 asm("Halt"); \
441 }
442#elif defined CORTEX_M7
443#define ASSERT(x) \
444 if (!(x)) \
445 { \
446 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
447 while (1) \
448 asm("wfi"); \
449 }
450#else
451#define ASSERT(x) \
452 if (!(x)) \
453 { \
454 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
455 while (1) \
456 ; \
457 }
458#endif
459#define DBPRINT(x, y) \
460 if (DB_FLAGS & x) printf(y);
461#define DBNUM(x, y) \
462 if (DB_FLAGS & x) putnum(y);
463#define DBBYTE(x, y) \
464 if (DB_FLAGS & x) putbytenum(y);
465#define DBPRINTF(x, ...) \
466 if (DB_FLAGS & x) printf(__VA_ARGS__);
467#define DBIPRINTF(x, ...) \
468 if (DB_FLAGS & x) iprintf(__VA_ARGS__);
469#else
470#define ASSERT(x) ((void)0)
471#define DBPRINT(x, y) ((void)0)
472#define DBNUM(x, y) ((void)0)
473#define DBBYTE(x, y) ((void)0)
474#define DBPRINTF(x, ...) ((void)0)
475#define DBIPRINTF(x, ...) ((void)0)
476#endif // _DEBUG
477
478#if ((defined _DEBUG) || (defined _DEBUG_PRINT))
479#define PPPDBPRINT(x) DBPRINT(DB_PPP, (x))
480#define PPPDBPRINTF(...) DBPRINTF(DB_PPP, __VA_ARGS__)
481#define PPPDBIPRINTF(...) DBIPRINTF(DB_PPP, __VA_ARGS__)
482#define PPPDBNUM(x) DBNUM(DB_PPP, (x))
483#define PPPDBBYTE(x) DBBYTE(DB_PPP, (x))
484#else
485/*
486 #define PPPDBPRINT(x) printf(x);
487 #define PPPDBPRINTF(...) printf(__VA_ARGS__)
488 #define PPPDBIPRINTF(...) printf(__VA_ARGS__)
489 #define PPPDBNUM(x) printf("%08X",x)
490 #define PPPDBBYTE(x) printf("%02X",x)
491*/
492#define PPPDBPRINT(x)
493#define PPPDBPRINTF(...)
494#define PPPDBIPRINTF(...)
495#define PPPDBNUM(x)
496#define PPPDBBYTE(x)
497
498#endif
499
500void LocalOutByte(char c);
501extern "C"
502{
503 void Local_OutString(const char *cp);
504}
506// USER_ENTER_CRITICAL ASSERTION
507//
508#if ((defined _DEBUG) || (defined USER_CRITICAL_SANITY))
509#ifdef COLDFIRE
510#define USER_CRITICAL_ASSERT() \
511 if (critical_count != 0) \
512 { \
513 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
514 while (1) \
515 asm("Halt"); \
516 }
517#elif (defined CORTEX_M7)
518#define USER_CRITICAL_ASSERT() \
519 if (critical_count != 0) \
520 { \
521 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
522 while (1) \
523 asm("wfi"); \
524 }
525#else
526#define USER_CRITICAL_ASSERT() \
527 if (critical_count != 0) \
528 { \
529 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
530 while (1) \
531 ; \
532 }
533#endif
534#else // !((defined _DEBUG) || (defined USER_CRITICAL_SANITY))
535#define USER_CRITICAL_ASSERT() ((void)0)
536#endif
537
539// DEVELOPMENT BOARD I/O FUNCTIONS
540//
541BOOL OnModDev70(void); // Return TRUE if on MOD-DEV-70, otherwise FALSE
542void putleds(unsigned char c); // Set the LEDs on the MOD-DEV-70/100
543unsigned char getleds(); // Get the LEDs state from CPLD, MOD-DEV-100 only
544void putdisp(unsigned short w); // Set the 7-segment LEDs on the MOD-DEV-100, BCD input, CPLD controls segmnet switching
545unsigned short getdisp(); // Get the 7-segment LEDs on the MOD-DEV-100, BCD
546unsigned char getdipsw(); // Read the DIP switch values on the MOD-DEV-70/100
547
548//--- The following functions are only valid on v1.12+ revisions of the dev board
549//--- CPLD programming files are available to upgrade older board revs
550// Set the 7-segment LEDs on the MOD-DEV-100
551void putSegments(unsigned char DisplayMask,
552 unsigned char DisplayData); // Display Mask - 4bit value indicates which display shows value, DisplayData - Segment bit
553 // 7 is decimal point, bits 0-6 = Segments A-G
554void putDecimal(unsigned char DisplayMask); // Set the 7-segment decimal point on the MOD-DEV-100, Display Mask - 4bit value indicates
555 // which display has decimal point
556unsigned char getCPLDver(); // returns the version # of the CPLD
557unsigned char getCPLDrev(); // returns the revision # of the CPLD
558
559// Predefined segments for the putSegments display function
560#define SevenSeg_0 (0x3F)
561#define SevenSeg_1 (0x06)
562#define SevenSeg_2 (0x5B)
563#define SevenSeg_3 (0x4F)
564#define SevenSeg_4 (0x66)
565#define SevenSeg_5 (0x6D)
566#define SevenSeg_6 (0x7C)
567#define SevenSeg_7 (0x07)
568#define SevenSeg_8 (0x7F)
569#define SevenSeg_9 (0x67)
570#define SevenSeg_A (0x77)
571#define SevenSeg_B (0x7F)
572#define SevenSeg_C (0x39)
573#define SevenSeg_D (0x3F)
574#define SevenSeg_E (0x79)
575#define SevenSeg_F (0x71)
576#define SevenSeg_H (0x76)
577#define SevenSeg_I (0x06)
578#define SevenSeg_J (0x1E)
579#define SevenSeg_L (0x38)
580#define SevenSeg_O (0x3F)
581#define SevenSeg_P (0x73)
582#define SevenSeg_S (0x6D)
583#define SevenSeg_U (0x3E)
584#define SevenSeg_b (0x7C)
585#define SevenSeg_c (0x58)
586#define SevenSeg_d (0x5E)
587#define SevenSeg_h (0x74)
588#define SevenSeg_l (0x06)
589#define SevenSeg_n (0x54)
590#define SevenSeg_o (0x53)
591#define SevenSeg_r (0x50)
592#define SevenSeg_u (0x1C)
593#define SevenSeg_Dash (0x40)
594#define SevenSeg_Decimal (0x80)
595#define SevenSeg_OFF (0x00)
596
597#endif /* #ifndef _NB_UTILS_H */
598
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:225
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
void outbyte(char c)
Write out a single, unbuffered byte to stdio.
unsigned char * convertBinaryToHexAscii(unsigned char *fromBufferPtr, unsigned int fromByteCount, unsigned char *toBufferPtr, unsigned int toByteCount)
Convert binary data to hexadecimal ASCII.
int kill(int pid, int sig)
Send a signal to a process.
unsigned long long Get_msec()
Returns the number of milliseconds as a 64-bit value.
int _fini(void)
Finalization function.
void fdShowMac(int fd, const MACADR *ma)
Dump a MAC address to file descriptor.
void ShowRingData(const uint8_t *ringBuf, uint32_t bufLen, uint32_t start, uint32_t end, const char *indent=NULL)
Dump part of a ring buffer to stdio.
IPADDR AsciiToIp6(const char *p)
Convert an ASCII IPv6 string to an IP address.
void snShowMac(char *buf, size_t maxlen, const MACADR *ma)
Dump a MAC address to character buffer.
void _exit(int i)
Terminate the calling process.
void FdShowRingData(int fd, const uint8_t *ringBuf, uint32_t bufLen, uint32_t start, uint32_t end, const char *indent=NULL)
Dump part of a ring buffer to file descriptor.
void ShowIP4(const IPADDR4 ia)
Dump an IPv4 address in ASCII IP string format to stdio.
void ShowIP6(const IPADDR &ia)
Dump an IPv6 address in ASCII IP string format to stdio.
Definition utils.h:262
void putnum(int i)
Write out a hexadecimal, unbuffered number to stdio.
const char * bufnstr(const char *search, const char *target, size_t len)
Search for a C string in an arbitrary memory blob that may contain NULL chars.
MACADR AsciiToMac(const char *p)
Convert an ASCII MAC address string to a MAC address.
void print(const char *str)
Write out a zero-terminated, unbuffered string.
void putbytenum(unsigned char c)
Write out a hexadecimal, unbuffered byte to stdio.
unsigned char * convertHexAsciiToBinary(unsigned char *fromBufferPtr, unsigned int fromByteCount, unsigned char *toBufferPtr, unsigned int toByteCount)
Convert hexadecimal ASCII to binary data.
int snShowIP4(char *buf, size_t maxlen, const IPADDR4 ia)
Dump an IPv4 address in ASCII IP string format to character buffer.
void ShowData(const uint8_t *fromptr, uint16_t len)
Dump a block of data to stdio and show in ASCII and hex.
void FdShowData(int fd, const uint8_t *fromptr, uint16_t len)
Dump a block of data to file descriptor and show in ASCII and hex.
void MacToID(MACADR *ma, char *IDBuf)
Write 6 character ID string based on unique portion of MAC.
BOOL ValidIPv4(const char *p)
Validate if a string contains a valid IPv4 address.
void ShowMac(const MACADR *ma)
Dump a MAC address to stdio.
int snShowIP6(char *buf, size_t maxlen, const IPADDR &ia)
Dump an IPv6 address in ASCII IP string format to character buffer.
Definition utils.h:276
void sShowCounters(char *buffer, int slen)
Dump all system counters to a memory buffer.
IPADDR4 AsciiToIp4(const char *p)
Convert an ASCII IPv4 string to an IP address.
void ShowCounters()
Dump all system counters to stdio.
uint32_t GetPreciseTime(void)
Converts an integer to ASCII (adds the stdlib itoa)
NetBurner IPADDR6 Class.
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.