added clang format C and H files.
This commit is contained in:
+61
-69
@@ -50,8 +50,8 @@
|
||||
#include "version.h"
|
||||
|
||||
#ifndef max
|
||||
#define max(a,b) (((a) (b)) ? (a) : (b))
|
||||
#define min(a,b) (((a) < (b)) ? (a) : (b))
|
||||
#define max(a, b) (((a)(b)) ? (a) : (b))
|
||||
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
/* buffer needed by CRC functions */
|
||||
@@ -65,17 +65,16 @@ static bool MSTP_Cap = false;
|
||||
static bool MSTP_Text_File = false;
|
||||
static char Capture_Filename[64] = "mstp_20090123091200.cap";
|
||||
static FILE *pFile = NULL; /* stream pointer */
|
||||
static FILE *pText_File = NULL; /* stream pointer */
|
||||
static FILE *pText_File = NULL; /* stream pointer */
|
||||
|
||||
/******************************************************************
|
||||
* DESCRIPTION: Takes one of the arguments passed by the main function
|
||||
* and converts it into a buffer value.
|
||||
* argi - single argument in string form.
|
||||
* RETURN: nothing
|
||||
* NOTES: none
|
||||
******************************************************************/
|
||||
static void Parse_Number(
|
||||
char *argi)
|
||||
* DESCRIPTION: Takes one of the arguments passed by the main function
|
||||
* and converts it into a buffer value.
|
||||
* argi - single argument in string form.
|
||||
* RETURN: nothing
|
||||
* NOTES: none
|
||||
******************************************************************/
|
||||
static void Parse_Number(char *argi)
|
||||
{
|
||||
long long_value = 0;
|
||||
|
||||
@@ -84,21 +83,19 @@ static void Parse_Number(
|
||||
} else {
|
||||
long_value = strtol(argi, NULL, 16);
|
||||
}
|
||||
CRC_Buffer[CRC_Buffer_Len] = (uint8_t) long_value;
|
||||
CRC_Buffer[CRC_Buffer_Len] = (uint8_t)long_value;
|
||||
CRC_Buffer_Len++;
|
||||
}
|
||||
|
||||
/******************************************************************
|
||||
* DESCRIPTION: Takes one of the arguments passed by the main function
|
||||
* and sets flags if it matches one of the predefined args.
|
||||
* PARAMETERS: argc (IN) number of arguments.
|
||||
* argv (IN) an array of arguments in string form.
|
||||
* RETURN: number of arguments parsed
|
||||
* NOTES: none
|
||||
******************************************************************/
|
||||
static void Parse_Arguments(
|
||||
int argc,
|
||||
char *argv[])
|
||||
* DESCRIPTION: Takes one of the arguments passed by the main function
|
||||
* and sets flags if it matches one of the predefined args.
|
||||
* PARAMETERS: argc (IN) number of arguments.
|
||||
* argv (IN) an array of arguments in string form.
|
||||
* RETURN: number of arguments parsed
|
||||
* NOTES: none
|
||||
******************************************************************/
|
||||
static void Parse_Arguments(int argc, char *argv[])
|
||||
{
|
||||
int i = 0;
|
||||
long long_value = 0;
|
||||
@@ -156,8 +153,7 @@ static void Parse_Arguments(
|
||||
}
|
||||
}
|
||||
|
||||
static void filename_create(
|
||||
char *filename)
|
||||
static void filename_create(char *filename)
|
||||
{
|
||||
time_t my_time;
|
||||
struct tm *today;
|
||||
@@ -166,70 +162,65 @@ static void filename_create(
|
||||
my_time = time(NULL);
|
||||
today = localtime(&my_time);
|
||||
sprintf(filename, "mstp_%04d%02d%02d%02d%02d%02d.cap",
|
||||
1900 + today->tm_year, 1 + today->tm_mon, today->tm_mday,
|
||||
today->tm_hour, today->tm_min, today->tm_sec);
|
||||
1900 + today->tm_year, 1 + today->tm_mon, today->tm_mday,
|
||||
today->tm_hour, today->tm_min, today->tm_sec);
|
||||
}
|
||||
}
|
||||
|
||||
/* write packet to file in libpcap format */
|
||||
static void write_global_header(
|
||||
const char *filename)
|
||||
static void write_global_header(const char *filename)
|
||||
{
|
||||
uint32_t magic_number = 0xa1b2c3d4; /* magic number */
|
||||
uint16_t version_major = 2; /* major version number */
|
||||
uint16_t version_minor = 4; /* minor version number */
|
||||
int32_t thiszone = 0; /* GMT to local correction */
|
||||
uint32_t sigfigs = 0; /* accuracy of timestamps */
|
||||
uint32_t snaplen = 65535; /* max length of captured packets, in octets */
|
||||
uint32_t network = 165; /* data link type - BACNET_MS_TP */
|
||||
uint16_t version_major = 2; /* major version number */
|
||||
uint16_t version_minor = 4; /* minor version number */
|
||||
int32_t thiszone = 0; /* GMT to local correction */
|
||||
uint32_t sigfigs = 0; /* accuracy of timestamps */
|
||||
uint32_t snaplen = 65535; /* max length of captured packets, in octets */
|
||||
uint32_t network = 165; /* data link type - BACNET_MS_TP */
|
||||
|
||||
/* create a new file. */
|
||||
pFile = fopen(filename, "wb");
|
||||
if (pFile) {
|
||||
(void) fwrite(&magic_number, sizeof(magic_number), 1, pFile);
|
||||
(void) fwrite(&version_major, sizeof(version_major), 1, pFile);
|
||||
(void) fwrite(&version_minor, sizeof(version_minor), 1, pFile);
|
||||
(void) fwrite(&thiszone, sizeof(thiszone), 1, pFile);
|
||||
(void) fwrite(&sigfigs, sizeof(sigfigs), 1, pFile);
|
||||
(void) fwrite(&snaplen, sizeof(snaplen), 1, pFile);
|
||||
(void) fwrite(&network, sizeof(network), 1, pFile);
|
||||
(void)fwrite(&magic_number, sizeof(magic_number), 1, pFile);
|
||||
(void)fwrite(&version_major, sizeof(version_major), 1, pFile);
|
||||
(void)fwrite(&version_minor, sizeof(version_minor), 1, pFile);
|
||||
(void)fwrite(&thiszone, sizeof(thiszone), 1, pFile);
|
||||
(void)fwrite(&sigfigs, sizeof(sigfigs), 1, pFile);
|
||||
(void)fwrite(&snaplen, sizeof(snaplen), 1, pFile);
|
||||
(void)fwrite(&network, sizeof(network), 1, pFile);
|
||||
fflush(pFile);
|
||||
fprintf(stdout, "mstpcap: saving capture to %s\n", filename);
|
||||
} else {
|
||||
fprintf(stderr, "mstpcap[header]: failed to open %s: %s\n", filename,
|
||||
strerror(errno));
|
||||
strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
static void write_received_packet(
|
||||
uint8_t * buffer,
|
||||
unsigned length)
|
||||
static void write_received_packet(uint8_t *buffer, unsigned length)
|
||||
{
|
||||
uint32_t ts_sec; /* timestamp seconds */
|
||||
uint32_t ts_usec; /* timestamp microseconds */
|
||||
uint32_t incl_len; /* number of octets of packet saved in file */
|
||||
uint32_t orig_len; /* actual length of packet */
|
||||
uint32_t ts_sec; /* timestamp seconds */
|
||||
uint32_t ts_usec; /* timestamp microseconds */
|
||||
uint32_t incl_len; /* number of octets of packet saved in file */
|
||||
uint32_t orig_len; /* actual length of packet */
|
||||
struct timeval tv;
|
||||
|
||||
if (pFile) {
|
||||
gettimeofday(&tv, NULL);
|
||||
ts_sec = tv.tv_sec;
|
||||
ts_usec = tv.tv_usec;
|
||||
(void) fwrite(&ts_sec, sizeof(ts_sec), 1, pFile);
|
||||
(void) fwrite(&ts_usec, sizeof(ts_usec), 1, pFile);
|
||||
(void)fwrite(&ts_sec, sizeof(ts_sec), 1, pFile);
|
||||
(void)fwrite(&ts_usec, sizeof(ts_usec), 1, pFile);
|
||||
orig_len = incl_len = length;
|
||||
(void) fwrite(&incl_len, sizeof(incl_len), 1, pFile);
|
||||
(void) fwrite(&orig_len, sizeof(orig_len), 1, pFile);
|
||||
(void) fwrite(buffer, length, 1, pFile);
|
||||
(void)fwrite(&incl_len, sizeof(incl_len), 1, pFile);
|
||||
(void)fwrite(&orig_len, sizeof(orig_len), 1, pFile);
|
||||
(void)fwrite(buffer, length, 1, pFile);
|
||||
} else {
|
||||
fprintf(stderr, "mstpcrc[packet]: failed to open %s: %s\n",
|
||||
Capture_Filename, strerror(errno));
|
||||
Capture_Filename, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
static void Write_Pcap(
|
||||
uint8_t * buffer,
|
||||
unsigned length)
|
||||
static void Write_Pcap(uint8_t *buffer, unsigned length)
|
||||
{
|
||||
filename_create(&Capture_Filename[0]);
|
||||
write_global_header(&Capture_Filename[0]);
|
||||
@@ -239,7 +230,7 @@ static void Write_Pcap(
|
||||
}
|
||||
}
|
||||
/* hold 3 ASCII characters per byte of data */
|
||||
static char Text_Buffer[1024*3];
|
||||
static char Text_Buffer[1024 * 3];
|
||||
static void Process_Text_File(void)
|
||||
{
|
||||
char *argi = NULL;
|
||||
@@ -269,9 +260,7 @@ static void Process_Text_File(void)
|
||||
}
|
||||
|
||||
/* simple program to CRC the data and print the CRC */
|
||||
int main(
|
||||
int argc,
|
||||
char *argv[])
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/* accumulates the crc value */
|
||||
uint8_t crc8 = 0xff;
|
||||
@@ -280,8 +269,10 @@ int main(
|
||||
|
||||
/* initialize our interface */
|
||||
if ((argc > 1) && (strcmp(argv[1], "--help") == 0)) {
|
||||
printf("mstpcrc [options] <05 03 01 0D...>\r\n"
|
||||
"perform MS/TP CRC on data bytes.\r\n" "options:\r\n"
|
||||
printf(
|
||||
"mstpcrc [options] <05 03 01 0D...>\r\n"
|
||||
"perform MS/TP CRC on data bytes.\r\n"
|
||||
"options:\r\n"
|
||||
"[-x] interprete the arguments as ascii hex (default)\r\n"
|
||||
"[-d] interprete the argument as ascii decimal\r\n"
|
||||
"[-m] Write the bytes to Wireshark capture file\r\n"
|
||||
@@ -294,7 +285,8 @@ int main(
|
||||
}
|
||||
if ((argc > 1) && (strcmp(argv[1], "--version") == 0)) {
|
||||
printf("mstpcap %s\r\n", BACNET_VERSION_TEXT);
|
||||
printf("Copyright (C) 2012 by Steve Karg\r\n"
|
||||
printf(
|
||||
"Copyright (C) 2012 by Steve Karg\r\n"
|
||||
"This is free software; see the source for copying conditions.\r\n"
|
||||
"There is NO warranty; not even for MERCHANTABILITY or\r\n"
|
||||
"FITNESS FOR A PARTICULAR PURPOSE.\r\n");
|
||||
@@ -314,7 +306,7 @@ int main(
|
||||
crc16 = CRC_Calc_Data(CRC_Buffer[i], crc16);
|
||||
}
|
||||
if (ASCII_Decimal) {
|
||||
printf("%u\r\n", (unsigned) CRC_Buffer[i]);
|
||||
printf("%u\r\n", (unsigned)CRC_Buffer[i]);
|
||||
} else {
|
||||
printf("0x%02X\r\n", CRC_Buffer[i]);
|
||||
}
|
||||
@@ -322,15 +314,15 @@ int main(
|
||||
if (CRC_Size == 8) {
|
||||
crc8 = ~crc8;
|
||||
if (ASCII_Decimal) {
|
||||
printf("%u Header CRC\r\n", (unsigned) crc8);
|
||||
printf("%u Header CRC\r\n", (unsigned)crc8);
|
||||
} else {
|
||||
printf("0x%02X Header CRC\r\n", crc8);
|
||||
}
|
||||
} else if (CRC_Size == 16) {
|
||||
crc16 = ~crc16;
|
||||
if (ASCII_Decimal) {
|
||||
printf("%u Data CRC\r\n", (unsigned) (crc16 & 0xFF));
|
||||
printf("%u Data CRC\r\n", (unsigned) (crc16 >> 8));
|
||||
printf("%u Data CRC\r\n", (unsigned)(crc16 & 0xFF));
|
||||
printf("%u Data CRC\r\n", (unsigned)(crc16 >> 8));
|
||||
} else {
|
||||
printf("0x%02X Data CRC\r\n", (crc16 & 0xFF));
|
||||
printf("0x%02X Data CRC\r\n", (crc16 >> 8));
|
||||
|
||||
Reference in New Issue
Block a user