NetBurner 3.5.7
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//
117
130void FdShowRingData(int fd, const uint8_t *ringBuf, uint32_t bufLen,
131 uint32_t start, uint32_t end, const char *indent=NULL,
132 uint32_t repAddr = 0xFFFFFFFF, uint8_t addrWidth=4);
144void ShowRingData(const uint8_t *ringBuf, uint32_t bufLen,
145 uint32_t start, uint32_t end, const char *indent=NULL,
146 uint32_t repAddr = 0xFFFFFFFF, uint8_t addrWidth=4);
157void FdShowData(int fd, const uint8_t *fromptr, uint16_t len,
158 const char *indent=NULL, uint32_t repAddr = 0xFFFFFFFF,
159 uint8_t addrWidth=4 );
160
170void ShowData(const uint8_t *fromptr, uint16_t len,
171 const char *indent=NULL, uint32_t repAddr = 0xFFFFFFFF,
172 uint8_t addrWidth=4 );
173
179void ShowMac(const MACADR *ma);
180
187void fdShowMac(int fd, const MACADR *ma);
188
194inline void ShowMac(const MACADR &ma)
195{
196 ShowMac(&ma);
197};
198
206void snShowMac(char *buf, size_t maxlen, const MACADR *ma);
207
215inline void snShowMac(char *buf, size_t maxlen, const MACADR &ma)
216{
217 snShowMac(buf, maxlen, &ma);
218};
219
226void MacToID(MACADR *ma, char *IDBuf);
227
233void outbyte(char c);
239void print(const char *str);
245void putnum(int i);
251void putbytenum(unsigned char c);
259IPADDR4 AsciiToIp4(const char *p);
267MACADR AsciiToMac(const char *p);
275BOOL ValidIPv4(const char *p);
276
277#ifndef IPV6
278#define AsciiToIp AsciiToIp4
279#else
280#define AsciiToIp AsciiToIp6
281#endif
282
288void ShowIP4(const IPADDR4 ia);
289
299int snShowIP4(char *buf, size_t maxlen, const IPADDR4 ia);
300
301#ifdef IPV6
307inline void ShowIP6(const IPADDR &ia)
308{
309 ia.print();
310};
311
321inline int snShowIP6(char *buf, size_t maxlen, const IPADDR &ia)
322{
323 return ia.sprintf(buf, maxlen);
324};
325#define ShowIP ShowIP6
326#define snShowIP snShowIP6
327#else
328#define ShowIP ShowIP4
329#define snShowIP snShowIP4
330#endif
331
332/*
333 * @brief Converts an integer to ASCII (adds the stdlib itoa)
334 *
335 * @param value Integer value to convert
336 * @param buffer Buffer to store the ASCII representation
337 * @param radix Base for conversion (e.g., 10 for decimal, 16 for hexadecimal)
338 *
339 * @return Pointer to the converted string
340 */
341// char *itoa( int value, char *buffer, int radix );
342
344// HIGHER RESOLUTION COUNTER READ FUNCTION
345//
346
354extern uint32_t GetPreciseTime(void);
355
361unsigned long long Get_msec();
362
363extern "C"
364{
373 int kill(int pid, int sig);
374
380 void _exit(int i);
381
387 int _fini(void);
388}
389
390#ifdef _STDIO_H_
391#ifndef FILE
397void fShowCounters(FILE *fp);
398#endif // FILE
399
406void fShowMac(FILE *fp, const MACADR *ma);
407
414void fShowIP4(FILE *fp, const IPADDR4 ia);
415
416#ifdef IPV6
423void fShowIP6(FILE *fp, const IPADDR &ia);
424#define fShowIP fShowIP6
425#else
426#define fShowIP fShowIP4
427#endif
428
429#endif // _STDIO_H_
430
708unsigned char *convertBinaryToHexAscii(unsigned char *fromBufferPtr,
709 unsigned int fromByteCount,
710 unsigned char *toBufferPtr,
711 unsigned int toByteCount);
712
1090unsigned char *convertHexAsciiToBinary(unsigned char *fromBufferPtr,
1091 unsigned int fromByteCount,
1092 unsigned char *toBufferPtr,
1093 unsigned int toByteCount);
1103const char *bufnstr(const char *search, const char *target, size_t len);
1104
1106// DEBUG FUNCTIONS
1107//
1108#if ((defined _DEBUG) || (defined _DEBUG_PRINT))
1109extern unsigned int DB_FLAGS;
1110void SetLogLevel();
1111#ifdef COLDFIRE
1112#define ASSERT(x) \
1113 if (!(x)) \
1114 { \
1115 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
1116 asm("Halt"); \
1117 }
1118#elif defined CORTEX_M7
1119#define ASSERT(x) \
1120 if (!(x)) \
1121 { \
1122 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
1123 while (1) \
1124 asm("wfi"); \
1125 }
1126#else
1127#define ASSERT(x) \
1128 if (!(x)) \
1129 { \
1130 iprintf("ASSERT failed at %d of %s\n", __LINE__, __FILE__); \
1131 while (1) \
1132 ; \
1133 }
1134#endif
1135#define DBPRINT(x, y) \
1136 if (DB_FLAGS & x) printf(y);
1137#define DBNUM(x, y) \
1138 if (DB_FLAGS & x) putnum(y);
1139#define DBBYTE(x, y) \
1140 if (DB_FLAGS & x) putbytenum(y);
1141#define DBPRINTF(x, ...) \
1142 if (DB_FLAGS & x) printf(__VA_ARGS__);
1143#define DBIPRINTF(x, ...) \
1144 if (DB_FLAGS & x) iprintf(__VA_ARGS__);
1145#else
1146#define ASSERT(x) ((void)0)
1147#define DBPRINT(x, y) ((void)0)
1148#define DBNUM(x, y) ((void)0)
1149#define DBBYTE(x, y) ((void)0)
1150#define DBPRINTF(x, ...) ((void)0)
1151#define DBIPRINTF(x, ...) ((void)0)
1152#endif // _DEBUG
1153
1154#if ((defined _DEBUG) || (defined _DEBUG_PRINT))
1155#define PPPDBPRINT(x) DBPRINT(DB_PPP, (x))
1156#define PPPDBPRINTF(...) DBPRINTF(DB_PPP, __VA_ARGS__)
1157#define PPPDBIPRINTF(...) DBIPRINTF(DB_PPP, __VA_ARGS__)
1158#define PPPDBNUM(x) DBNUM(DB_PPP, (x))
1159#define PPPDBBYTE(x) DBBYTE(DB_PPP, (x))
1160#else
1161/*
1162 #define PPPDBPRINT(x) printf(x);
1163 #define PPPDBPRINTF(...) printf(__VA_ARGS__)
1164 #define PPPDBIPRINTF(...) printf(__VA_ARGS__)
1165 #define PPPDBNUM(x) printf("%08X",x)
1166 #define PPPDBBYTE(x) printf("%02X",x)
1167*/
1168#define PPPDBPRINT(x)
1169#define PPPDBPRINTF(...)
1170#define PPPDBIPRINTF(...)
1171#define PPPDBNUM(x)
1172#define PPPDBBYTE(x)
1173
1174#endif
1175
1176void LocalOutByte(char c);
1177extern "C"
1178{
1179 void Local_OutString(const char *cp);
1180}
1182// USER_ENTER_CRITICAL ASSERTION
1183//
1184#if ((defined _DEBUG) || (defined USER_CRITICAL_SANITY))
1185#ifdef COLDFIRE
1186#define USER_CRITICAL_ASSERT() \
1187 if (critical_count != 0) \
1188 { \
1189 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
1190 while (1) \
1191 asm("Halt"); \
1192 }
1193#elif (defined CORTEX_M7)
1194#define USER_CRITICAL_ASSERT() \
1195 if (critical_count != 0) \
1196 { \
1197 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
1198 while (1) \
1199 asm("wfi"); \
1200 }
1201#else
1202#define USER_CRITICAL_ASSERT() \
1203 if (critical_count != 0) \
1204 { \
1205 Local_OutString("Illegal pend after USER_ENTER_CRITICAL(). Halting.\r\n"); \
1206 while (1) \
1207 ; \
1208 }
1209#endif
1210#else // !((defined _DEBUG) || (defined USER_CRITICAL_SANITY))
1211#define USER_CRITICAL_ASSERT() ((void)0)
1212#endif
1213
1215// DEVELOPMENT BOARD I/O FUNCTIONS
1216//
1217BOOL OnModDev70(void); // Return TRUE if on MOD-DEV-70, otherwise FALSE
1218void putleds(unsigned char c); // Set the LEDs on the MOD-DEV-70/100
1219unsigned char getleds(); // Get the LEDs state from CPLD, MOD-DEV-100 only
1220void putdisp(unsigned short w); // Set the 7-segment LEDs on the MOD-DEV-100, BCD input, CPLD controls segmnet switching
1221unsigned short getdisp(); // Get the 7-segment LEDs on the MOD-DEV-100, BCD
1222unsigned char getdipsw(); // Read the DIP switch values on the MOD-DEV-70/100
1223
1224//--- The following functions are only valid on v1.12+ revisions of the dev board
1225//--- CPLD programming files are available to upgrade older board revs
1226// Set the 7-segment LEDs on the MOD-DEV-100
1227void putSegments(unsigned char DisplayMask,
1228 unsigned char DisplayData); // Display Mask - 4bit value indicates which display shows value, DisplayData - Segment bit
1229 // 7 is decimal point, bits 0-6 = Segments A-G
1230void putDecimal(unsigned char DisplayMask); // Set the 7-segment decimal point on the MOD-DEV-100, Display Mask - 4bit value indicates
1231 // which display has decimal point
1232unsigned char getCPLDver(); // returns the version # of the CPLD
1233unsigned char getCPLDrev(); // returns the revision # of the CPLD
1234
1235// Predefined segments for the putSegments display function
1236#define SevenSeg_0 (0x3F)
1237#define SevenSeg_1 (0x06)
1238#define SevenSeg_2 (0x5B)
1239#define SevenSeg_3 (0x4F)
1240#define SevenSeg_4 (0x66)
1241#define SevenSeg_5 (0x6D)
1242#define SevenSeg_6 (0x7C)
1243#define SevenSeg_7 (0x07)
1244#define SevenSeg_8 (0x7F)
1245#define SevenSeg_9 (0x67)
1246#define SevenSeg_A (0x77)
1247#define SevenSeg_B (0x7F)
1248#define SevenSeg_C (0x39)
1249#define SevenSeg_D (0x3F)
1250#define SevenSeg_E (0x79)
1251#define SevenSeg_F (0x71)
1252#define SevenSeg_H (0x76)
1253#define SevenSeg_I (0x06)
1254#define SevenSeg_J (0x1E)
1255#define SevenSeg_L (0x38)
1256#define SevenSeg_O (0x3F)
1257#define SevenSeg_P (0x73)
1258#define SevenSeg_S (0x6D)
1259#define SevenSeg_U (0x3E)
1260#define SevenSeg_b (0x7C)
1261#define SevenSeg_c (0x58)
1262#define SevenSeg_d (0x5E)
1263#define SevenSeg_h (0x74)
1264#define SevenSeg_l (0x06)
1265#define SevenSeg_n (0x54)
1266#define SevenSeg_o (0x53)
1267#define SevenSeg_r (0x50)
1268#define SevenSeg_u (0x1C)
1269#define SevenSeg_Dash (0x40)
1270#define SevenSeg_Decimal (0x80)
1271#define SevenSeg_OFF (0x00)
1272
1273#endif /* #ifndef _NB_UTILS_H */
1274
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.
void FdShowRingData(int fd, const uint8_t *ringBuf, uint32_t bufLen, uint32_t start, uint32_t end, const char *indent=NULL, uint32_t repAddr=0xFFFFFFFF, uint8_t addrWidth=4)
Dump part of a ring buffer to file descriptor.
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.
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 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:307
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 ShowRingData(const uint8_t *ringBuf, uint32_t bufLen, uint32_t start, uint32_t end, const char *indent=NULL, uint32_t repAddr=0xFFFFFFFF, uint8_t addrWidth=4)
Dump part of a ring buffer to stdio.
void MacToID(MACADR *ma, char *IDBuf)
Write 6 character ID string based on unique portion of MAC.
void FdShowData(int fd, const uint8_t *fromptr, uint16_t len, const char *indent=NULL, uint32_t repAddr=0xFFFFFFFF, uint8_t addrWidth=4)
Dump a block of data to file descriptor and show in ASCII and hex.
void ShowData(const uint8_t *fromptr, uint16_t len, const char *indent=NULL, uint32_t repAddr=0xFFFFFFFF, uint8_t addrWidth=4)
Dump a block of data to stdio and show in ASCII and hex.
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:321
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.