adjust root folder

This commit is contained in:
Steve Karg
2019-10-08 23:47:53 -05:00
parent b6fc50ddea
commit a42e8f507c
1258 changed files with 26 additions and 214 deletions
+53
View File
@@ -0,0 +1,53 @@
#Makefile to build BACnet Application for the Linux Port
# tools - only if you need them.
# Most platforms have this already defined
# CC = gcc
# Executable file name
TARGET = mstpcrc
TARGET_BIN = ${TARGET}$(TARGET_EXT)
# This demo seems to be a little unique
DEFINES = $(BACNET_DEFINES)
BACNET_SOURCE_DIR = ../../src
#libraries used
LIBRARIES=-lgcc,-lm
#build for release (default) or debug
DEBUGGING =
OPTIMIZATION = -Os
ifeq (${BUILD},debug)
OPTIMIZATION = -O0
DEBUGGING = -g
endif
# search order for included libraries
INCLUDES = -I$(BACNET_INCLUDE_DIR)
SRCS = main.c \
${BACNET_PORT_DIR}/timer.c \
${BACNET_SOURCE_DIR}/crc.c
OBJS = ${SRCS:.c=.o}
all: Makefile ${TARGET_BIN}
${TARGET_BIN}: ${OBJS} Makefile
${CC} ${PFLAGS} ${OBJS} ${LFLAGS} -o $@
size $@
cp $@ ../../bin
.c.o:
${CC} -c ${CFLAGS} $*.c -o $@
depend:
rm -f .depend
${CC} -MM ${CFLAGS} *.c >> .depend
clean:
rm -f core ${TARGET_BIN} ${OBJS} $(TARGET).map
include: .depend
+9
View File
@@ -0,0 +1,9 @@
@echo off
echo Build with MinGW and MSYS: mingw.sourceforge.net
rem set PATH=C:\MinGW\msys\1.0\bin;C:\MinGW\bin
rem assumes rm, cp, size are already in path
set CC=gcc
set AR=ar
set MAKE=make
set TARGET_EXT=.exe
make BACNET_PORT=win32 clean all
+343
View File
@@ -0,0 +1,343 @@
/*####COPYRIGHTBEGIN####
-------------------------------------------
Copyright (C) 2012 Steve Karg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
The Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307
USA.
As a special exception, if other files instantiate templates or
use macros or inline functions from this file, or you compile
this file and link it with other works to produce a work based
on this file, this file does not by itself cause the resulting
work to be covered by the GNU General Public License. However
the source code for this file must still be made available in
accordance with section (3) of the GNU General Public License.
This exception does not invalidate any other reasons why a work
based on this file might be covered by the GNU General Public
License.
-------------------------------------------
####COPYRIGHTEND####*/
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
/* OS specific include*/
#include "net.h"
#include "timer.h"
/* local includes */
#include "bytes.h"
#include "crc.h"
#include "version.h"
#ifndef max
#define max(a,b) (((a) (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
/* buffer needed by CRC functions */
static uint8_t CRC_Buffer[1512];
static unsigned CRC_Buffer_Len = 0;
/* flags needed for options */
static bool ASCII_Decimal = false;
static unsigned CRC_Size = 8;
/* save to capture file for viewing in Wireshark */
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 */
/******************************************************************
* 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;
if (ASCII_Decimal) {
long_value = strtol(argi, NULL, 10);
} else {
long_value = strtol(argi, NULL, 16);
}
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[])
{
int i = 0;
long long_value = 0;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
/* numeric arguments */
if (isdigit(argv[i][1])) {
long_value = strtol(&argv[i][1], NULL, 10);
/* dash arguments */
switch (long_value) {
case 8:
CRC_Size = 8;
break;
case 16:
CRC_Size = 16;
break;
case 32:
CRC_Size = 32;
break;
default:
break;
}
}
/* dash arguments */
switch (argv[i][1]) {
case 'h':
case 'H':
ASCII_Decimal = false;
break;
case 'd':
case 'D':
ASCII_Decimal = true;
break;
case 'm':
case 'M':
MSTP_Cap = true;
break;
case 'f':
case 'F':
MSTP_Text_File = true;
break;
default:
break;
}
} else {
if (MSTP_Text_File) {
/* open existing file. */
pText_File = fopen(argv[i], "r");
} else {
/* should be number values here */
Parse_Number(argv[i]);
}
}
}
}
static void filename_create(
char *filename)
{
time_t my_time;
struct tm *today;
if (filename) {
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);
}
}
/* write packet to file in libpcap format */
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 */
/* 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);
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));
}
}
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 */
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);
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);
} else {
fprintf(stderr, "mstpcrc[packet]: failed to open %s: %s\n",
Capture_Filename, strerror(errno));
}
}
static void Write_Pcap(
uint8_t * buffer,
unsigned length)
{
filename_create(&Capture_Filename[0]);
write_global_header(&Capture_Filename[0]);
write_received_packet(buffer, length);
if (pFile) {
fclose(pFile);
}
}
/* hold 3 ASCII characters per byte of data */
static char Text_Buffer[1024*3];
static void Process_Text_File(void)
{
char *argi = NULL;
filename_create(&Capture_Filename[0]);
write_global_header(&Capture_Filename[0]);
while (fgets(Text_Buffer, sizeof(Text_Buffer), pText_File)) {
CRC_Buffer_Len = 0;
do {
if (!argi) {
argi = strtok(Text_Buffer, " ");
} else {
argi = strtok(NULL, " ");
}
if (argi) {
Parse_Number(argi);
}
} while (argi);
write_received_packet(CRC_Buffer, CRC_Buffer_Len);
}
if (pFile) {
fclose(pFile);
}
if (pText_File) {
fclose(pText_File);
}
}
/* simple program to CRC the data and print the CRC */
int main(
int argc,
char *argv[])
{
/* accumulates the crc value */
uint8_t crc8 = 0xff;
uint16_t crc16 = 0xffff;
unsigned i = 0;
/* 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"
"[-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"
"[-8] calculate the MS/TP 8-bit Header CRC (default)\r\n"
"[-16] calculate the MS/TP 16-bit Data CRC\r\n"
"[-32] calculate the MS/TP 32-bit Extended Frame CRC\r\n"
"[-f filename] read MS/TP capture data from text file\r\n"
"Note: MS/TP Header CRC does not include the 55 FF preamble.\r\n");
return 0;
}
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"
"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");
return 0;
}
Parse_Arguments(argc, argv);
if (MSTP_Text_File) {
Process_Text_File();
} else if (CRC_Buffer_Len) {
if (MSTP_Cap) {
Write_Pcap(CRC_Buffer, CRC_Buffer_Len);
} else {
for (i = 0; i < CRC_Buffer_Len; i++) {
if (CRC_Size == 8) {
crc8 = CRC_Calc_Header(CRC_Buffer[i], crc8);
} else if (CRC_Size == 16) {
crc16 = CRC_Calc_Data(CRC_Buffer[i], crc16);
}
if (ASCII_Decimal) {
printf("%u\r\n", (unsigned) CRC_Buffer[i]);
} else {
printf("0x%02X\r\n", CRC_Buffer[i]);
}
}
if (CRC_Size == 8) {
crc8 = ~crc8;
if (ASCII_Decimal) {
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));
} else {
printf("0x%02X Data CRC\r\n", (crc16 & 0xFF));
printf("0x%02X Data CRC\r\n", (crc16 >> 8));
}
}
}
}
return 1;
}
+139
View File
@@ -0,0 +1,139 @@
#
# Simple makefile to build an executable for Win32
#
# This makefile assumes Borland bcc32 development environment
# on Windows NT/9x/2000/XP
#
!ifndef BORLAND_DIR
BORLAND_DIR_Not_Defined:
@echo .
@echo You must define environment variable BORLAND_DIR to compile.
!endif
# target
PRODUCT = mstpcrc
PRODUCT_EXE = $(PRODUCT).exe
# tools
CC = $(BORLAND_DIR)\bin\bcc32
MAKE=$(BORLAND_DIR)\bin\make.exe
#LINK = $(BORLAND_DIR)\bin\tlink32
LINK = $(BORLAND_DIR)\bin\ilink32
BACNET_LIB_DIR = ..\..\lib
BACNET_LIB = $(BACNET_LIB_DIR)\bacnet.lib
# directories
BACNET_PORT = ..\..\ports\win32
BACNET_INCLUDE = ..\..\include
BACNET_OBJECT = ..\object
BACNET_HANDLER = ..\handler
INCLUDES = \
-I$(BACNET_INCLUDE) \
-I$(BACNET_PORT) \
-I$(BACNET_OBJECT) \
-I$(BACNET_HANDLER) \
-I$(BORLAND_DIR)\include
#
BACNET_DEFINES = -DPRINT_ENABLED=1 -DBACAPP_ALL
#BACDL_DEFINE=-DBACDL_MSTP=1
BACDL_DEFINE=-DBACDL_BIP=1 -DUSE_INADDR=1
DEFINES = $(BACNET_DEFINES) $(BACDL_DEFINE)
SRCS = main.c \
OBJS = $(SRCS:.c=.obj)
#
# Compiler definitions
#
BCC_CFG = bcc32.cfg
#
# Include directories
#
CFLAGS = $(INCLUDES) $(DEFINES)
#
# Libraries
#
C_LIB_DIR = $(BORLAND_DIR)\lib
LIBS = $(BACNET_LIB) \
$(C_LIB_DIR)\IMPORT32.lib \
$(C_LIB_DIR)\CW32MT.lib \
#
# Main target
#
# This should be the first one in the makefile
all : $(BACNET_LIB) $(BCC_CFG) $(OBJS) $(PRODUCT_EXE)
del $(BCC_CFG)
install: $(PRODUCT_EXE)
copy $(PRODUCT_EXE) ..\..\bin\$(PRODUCT_EXE)
# Linker specific: the link below is for BCC linker/compiler. If you link
# with a different linker - please change accordingly.
#
# need a temp response file (@&&| ... |) because command line is too long
# $** lists each dependency
# $< target name
# $* target name without extension
$(PRODUCT_EXE) : $(OBJS)
@echo Running Linker for $(PRODUCT_EXE)
$(LINK) -L$(C_LIB_DIR) -L$(BACNET_LIB_DIR) -m -c -s -v @&&|
$(BORLAND_DIR)\lib\c0x32.obj $**
$<
$*.map
$(LIBS)
|
#
# Utilities
clean :
del $(OBJS)
del $(PRODUCT_EXE)
del $(PRODUCT).map
del $(PRODUCT).ilc
del $(PRODUCT).ild
del $(PRODUCT).ilf
del $(PRODUCT).ils
del $(PRODUCT).tds
del $(BCC_CFG)
#
# Generic rules
#
.SUFFIXES: .cpp .c .sbr .obj
#
# cc generic rule
#
.c.obj:
$(CC) +$(BCC_CFG) -o$@ $<
# Compiler configuration file
$(BCC_CFG) :
Copy &&|
$(CFLAGS)
-c
-y #include line numbers in OBJ's
-v #include debug info
-w+ #turn on all warnings
-Od #disable all optimizations
#-a4 #32 bit data alignment
#-M # generate link map
#-ls # linker options
#-WM- #not multithread
-WM #multithread
-w-aus # ignore warning assigned a value that is never used
-w-sig # ignore warning conversion may lose sig digits
| $@
# EOF: makefile
+20
View File
@@ -0,0 +1,20 @@
BACnet MS/TP CRC Calculator
This tool receives MS/TP bytes and generates a CRC for those bytes
mstpcrc [options] <00 00 00 00...>
perform MS/TP CRC on data bytes.
options:
[-x] interprete the arguments as ascii hex (default)
[-d] interprete the argument as ascii decimal
[-8] calculate the MS/TP 8-bit Header CRC (default)
[-16] calculate the MS/TP 16-bit Data CRC
Here is a sample of the tool running (use CTRL-C to quit):
D:\code\bacnet-stack\demo\mstpcrc>mstpcrc 06 ff 01 00 15
0x06
0xFF
0x01
0x00
0x15
0x8E Header CRC