NetBurner 3.5.6
PDF Version
utils.h
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
70#define DB_TCPIP (1)
71#define DB_HTTP (2)
72#define DB_ETHER (4)
73#define DB_RTOS (8)
74#define DB_BUFFER (16)
75#define DB_PPP (32)
76#define DB_AU (64)
77#define DB_MAIL (128)
78#define DB_IP (256)
79#define DB_TCPDATA (512)
80#define DB_SSL (1024)
81#define DB_SNMP (2048)
82#define DB_IPV6_ND (4096)
83#define DB_IPV6_ICMP (8192)
84#define DB_IPV6_ROUTE (16384)
85#define DB_IPV6_FRAG (32768)
86#define DB_IPV6_ERR (65536)
87
89// DIAGNOSTIC FUNCTIONS
90//
91
96
103void sShowCounters(char *buffer, int slen);
104
105#ifdef FILE
111void fShowCounters(FILE *fp);
112#endif // FILE
113
115// UTILITY I/O FUNCTIONS
116//
127void FdShowRingData(int fd, const uint8_t *ringBuf, uint32_t bufLen,
128 uint32_t start, uint32_t end, const char *indent=NULL);
138void ShowRingData(const uint8_t *ringBuf, uint32_t bufLen,
139 uint32_t start, uint32_t end, const char *indent=NULL);
147void FdShowData(int fd, const uint8_t *fromptr, uint16_t len);
148
155void ShowData(const uint8_t *fromptr, uint16_t len);
156
162void ShowMac(const MACADR *ma);
163
170void fdShowMac(int fd, const MACADR *ma);
171
177inline void ShowMac(const MACADR &ma)
178{
179 ShowMac(&ma);
180};
181
189void snShowMac(char *buf, size_t maxlen, const MACADR *ma);
190
198inline void snShowMac(char *buf, size_t maxlen, const MACADR &ma)
199{
200 snShowMac(buf, maxlen, &ma);
201};
202
209void MacToID(MACADR *ma, char *IDBuf);
210
216void outbyte(char c);
222void print(const char *str);
228void putnum(int i);
234void putbytenum(unsigned char c);
242IPADDR4 AsciiToIp4(const char *p);
250MACADR AsciiToMac(const char *p);
258BOOL ValidIPv4(const char *p);
259
260#ifndef IPV6
261#define AsciiToIp AsciiToIp4
262#else
263#define AsciiToIp AsciiToIp6
264#endif
265
271void ShowIP4(const IPADDR4 ia);
272
282int snShowIP4(char *buf, size_t maxlen, const IPADDR4 ia);
283
284#ifdef IPV6
290inline void ShowIP6(const IPADDR &ia)
291{
292 ia.print();
293};
294
304inline int snShowIP6(char *buf, size_t maxlen, const IPADDR &ia)
305{
306 return ia.sprintf(buf, maxlen);
307};
308#define ShowIP ShowIP6
309#define snShowIP snShowIP6
310#else
311#define ShowIP ShowIP4
312#define snShowIP snShowIP4
313#endif
314
315/*
316 * @brief Converts an integer to ASCII (adds the stdlib itoa)
317 *
318 * @param value Integer value to convert
319 * @param buffer Buffer to store the ASCII representation
320 * @param radix Base for conversion (e.g., 10 for decimal, 16 for hexadecimal)
321 *
322 * @return Pointer to the converted string
323 */
324// char *itoa( int value, char *buffer, int radix );
325
327// HIGHER RESOLUTION COUNTER READ FUNCTION
328//
329
337extern uint32_t GetPreciseTime(void);
338
344unsigned long long Get_msec();
345
346extern "C"
347{
356 int kill(int pid, int sig);
357
363 void _exit(int i);
364
370 int _fini(void);
371}
372
373#ifdef _STDIO_H_
374#ifndef FILE
380void fShowCounters(FILE *fp);
381#endif // FILE
382
389void fShowMac(FILE *fp, const MACADR *ma);
390
397void fShowIP4(FILE *fp, const IPADDR4 ia);
398
399#ifdef IPV6
406void fShowIP6(FILE *fp, const IPADDR &ia);
407#define fShowIP fShowIP6
408#else
409#define fShowIP fShowIP4
410#endif
411
412#endif // _STDIO_H_
413
691unsigned char *convertBinaryToHexAscii(unsigned char *fromBufferPtr,
692 unsigned int fromByteCount,
693 unsigned char *toBufferPtr,
694 unsigned int toByteCount);
695
1073unsigned char *convertHexAsciiToBinary(unsigned char *fromBufferPtr,
1074 unsigned int fromByteCount,
1075 unsigned char *toBufferPtr,
1076 unsigned int toByteCount);
1086const char *bufnstr(const char *search, const char *target, size_t len);
1087
1089// DEBUG FUNCTIONS
1090//
1091#if ((defined _DEBUG) || (defined _DEBUG_PRINT))
1092extern unsigned int DB_FLAGS;
1093void SetLogLevel();
1094#ifdef COLDFIRE
1095#define ASSERT(x) \
1096 if (!(x)) \
1097 { \
1098 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
1099 asm("Halt"); \
1100 }
1101#elif defined CORTEX_M7
1102#define ASSERT(x) \
1103 if (!(x)) \
1104 { \
1105 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
1106 while (1) \
1107 asm("wfi"); \
1108 }
1109#else
1110#define ASSERT(x) \
1111 if (!(x)) \
1112 { \
1113 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
1114 while (1) \
1115 ; \
1116 }
1117#endif
1118#define DBPRINT(x, y) \
1119 if (DB_FLAGS & x) printf(y);
1120#define DBNUM(x, y) \
1121 if (DB_FLAGS & x) putnum(y);
1122#define DBBYTE(x, y) \
1123 if (DB_FLAGS & x) putbytenum(y);
1124#define DBPRINTF(x, ...) \
1125 if (DB_FLAGS & x) printf(__VA_ARGS__);
1126#define DBIPRINTF(x, ...) \
1127 if (DB_FLAGS & x) iprintf(__VA_ARGS__);
1128#else
1129#define ASSERT(x) ((void)0)
1130#define DBPRINT(x, y) ((void)0)
1131#define DBNUM(x, y) ((void)0)
1132#define DBBYTE(x, y) ((void)0)
1133#define DBPRINTF(x, ...) ((void)0)
1134#define DBIPRINTF(x, ...) ((void)0)
1135#endif // _DEBUG
1136
1137#if ((defined _DEBUG) || (defined _DEBUG_PRINT))
1138#define PPPDBPRINT(x) DBPRINT(DB_PPP, (x))
1139#define PPPDBPRINTF(...) DBPRINTF(DB_PPP, __VA_ARGS__)
1140#define PPPDBIPRINTF(...) DBIPRINTF(DB_PPP, __VA_ARGS__)
1141#define PPPDBNUM(x) DBNUM(DB_PPP, (x))
1142#define PPPDBBYTE(x) DBBYTE(DB_PPP, (x))
1143#else
1144/*
1145 #define PPPDBPRINT(x) printf(x);
1146 #define PPPDBPRINTF(...) printf(__VA_ARGS__)
1147 #define PPPDBIPRINTF(...) printf(__VA_ARGS__)
1148 #define PPPDBNUM(x) printf("%08X",x)
1149 #define PPPDBBYTE(x) printf("%02X",x)
1150*/
1151#define PPPDBPRINT(x)
1152#define PPPDBPRINTF(...)
1153#define PPPDBIPRINTF(...)
1154#define PPPDBNUM(x)
1155#define PPPDBBYTE(x)
1156
1157#endif
1158
1159void LocalOutByte(char c);
1160extern "C"
1161{
1162 void Local_OutString(const char *cp);
1163}
1165// USER_ENTER_CRITICAL ASSERTION
1166//
1167#if ((defined _DEBUG) || (defined USER_CRITICAL_SANITY))
1168#ifdef COLDFIRE
1169#define USER_CRITICAL_ASSERT() \
1170 if (critical_count != 0) \
1171 { \
1172 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
1173 while (1) \
1174 asm("Halt"); \
1175 }
1176#elif (defined CORTEX_M7)
1177#define USER_CRITICAL_ASSERT() \
1178 if (critical_count != 0) \
1179 { \
1180 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
1181 while (1) \
1182 asm("wfi"); \
1183 }
1184#else
1185#define USER_CRITICAL_ASSERT() \
1186 if (critical_count != 0) \
1187 { \
1188 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
1189 while (1) \
1190 ; \
1191 }
1192#endif
1193#else // !((defined _DEBUG) || (defined USER_CRITICAL_SANITY))
1194#define USER_CRITICAL_ASSERT() ((void)0)
1195#endif
1196
1198// DEVELOPMENT BOARD I/O FUNCTIONS
1199//
1200BOOL OnModDev70(void); // Return TRUE if on MOD-DEV-70, otherwise FALSE
1201void putleds(unsigned char c); // Set the LEDs on the MOD-DEV-70/100
1202unsigned char getleds(); // Get the LEDs state from CPLD, MOD-DEV-100 only
1203void putdisp(unsigned short w); // Set the 7-segment LEDs on the MOD-DEV-100, BCD input, CPLD controls segmnet switching
1204unsigned short getdisp(); // Get the 7-segment LEDs on the MOD-DEV-100, BCD
1205unsigned char getdipsw(); // Read the DIP switch values on the MOD-DEV-70/100
1206
1207//--- The following functions are only valid on v1.12+ revisions of the dev board
1208//--- CPLD programming files are available to upgrade older board revs
1209// Set the 7-segment LEDs on the MOD-DEV-100
1210void putSegments(unsigned char DisplayMask,
1211 unsigned char DisplayData); // Display Mask - 4bit value indicates which display shows value, DisplayData - Segment bit
1212 // 7 is decimal point, bits 0-6 = Segments A-G
1213void putDecimal(unsigned char DisplayMask); // Set the 7-segment decimal point on the MOD-DEV-100, Display Mask - 4bit value indicates
1214 // which display has decimal point
1215unsigned char getCPLDver(); // returns the version # of the CPLD
1216unsigned char getCPLDrev(); // returns the revision # of the CPLD
1217
1218// Predefined segments for the putSegments display function
1219#define SevenSeg_0 (0x3F)
1220#define SevenSeg_1 (0x06)
1221#define SevenSeg_2 (0x5B)
1222#define SevenSeg_3 (0x4F)
1223#define SevenSeg_4 (0x66)
1224#define SevenSeg_5 (0x6D)
1225#define SevenSeg_6 (0x7C)
1226#define SevenSeg_7 (0x07)
1227#define SevenSeg_8 (0x7F)
1228#define SevenSeg_9 (0x67)
1229#define SevenSeg_A (0x77)
1230#define SevenSeg_B (0x7F)
1231#define SevenSeg_C (0x39)
1232#define SevenSeg_D (0x3F)
1233#define SevenSeg_E (0x79)
1234#define SevenSeg_F (0x71)
1235#define SevenSeg_H (0x76)
1236#define SevenSeg_I (0x06)
1237#define SevenSeg_J (0x1E)
1238#define SevenSeg_L (0x38)
1239#define SevenSeg_O (0x3F)
1240#define SevenSeg_P (0x73)
1241#define SevenSeg_S (0x6D)
1242#define SevenSeg_U (0x3E)
1243#define SevenSeg_b (0x7C)
1244#define SevenSeg_c (0x58)
1245#define SevenSeg_d (0x5E)
1246#define SevenSeg_h (0x74)
1247#define SevenSeg_l (0x06)
1248#define SevenSeg_n (0x54)
1249#define SevenSeg_o (0x53)
1250#define SevenSeg_r (0x50)
1251#define SevenSeg_u (0x1C)
1252#define SevenSeg_Dash (0x40)
1253#define SevenSeg_Decimal (0x80)
1254#define SevenSeg_OFF (0x00)
1255
1256#endif /* #ifndef _NB_UTILS_H */
1257
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 representation.
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:290
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 string 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:304
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)
Gets the time tick since system start at a higher resolution.