Converted mstpcap to use timer rather than sleep for timing.

This commit is contained in:
skarg
2009-07-27 17:50:09 +00:00
parent f85519c9e1
commit f30a21a032
7 changed files with 438 additions and 97 deletions
+2 -1
View File
@@ -25,7 +25,7 @@ LIBRARIES=-lc,-lgcc,-lrt,-lm
endif
ifeq (${BACNET_PORT},win32)
TARGET_BIN = ${TARGET}.exe
LIBRARIES=-lws2_32,-lgcc,-lm,-liphlpapi
LIBRARIES=-lws2_32,-lgcc,-lm,-liphlpapi,-lwinmm
endif
#build for release (default) or debug
DEBUGGING =
@@ -40,6 +40,7 @@ LFLAGS = -Wl,-Map=$(TARGET).map,$(LIBRARIES),--gc-sections
SRCS = main.c \
${BACNET_PORT_DIR}/rs485.c \
${BACNET_PORT_DIR}/timer.c \
${BACNET_SOURCE_DIR}/mstp.c \
${BACNET_SOURCE_DIR}/mstptext.c \
${BACNET_SOURCE_DIR}/debug.c \
+11 -95
View File
@@ -41,6 +41,7 @@
#include <time.h>
/* OS specific include*/
#include "net.h"
#include "timer.h"
/* local includes */
#include "bytes.h"
#include "rs485.h"
@@ -48,12 +49,6 @@
#include "mstptext.h"
#include "dlmstp.h"
#if defined(__BORLANDC__)
#include <sys/timeb.h>
#define _timeb timeb
#define _ftime(param) ftime(param)
#endif
#ifndef max
#define max(a,b) (((a) (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
@@ -64,86 +59,26 @@ static volatile struct mstp_port_struct_t MSTP_Port;
/* buffers needed by mstp port struct */
static uint8_t RxBuffer[MAX_MPDU];
static uint8_t TxBuffer[MAX_MPDU];
static uint16_t SilenceTime;
#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;}
#if defined (_WIN32)
struct timespec {
time_t tv_sec; /* Seconds */
long tv_nsec; /* Nanoseconds [0 .. 999999999] */
};
static int gettimeofday(
struct timeval *tp,
void *tzp)
{
struct _timeb timebuffer;
(void) tzp;
_ftime(&timebuffer);
tp->tv_sec = timebuffer.time;
tp->tv_usec = timebuffer.millitm * 1000;
return 0;
}
#endif
static uint16_t Timer_Silence(
void)
{
return SilenceTime;
uint32_t delta_time = 0;
delta_time = timer_milliseconds(TIMER_SILENCE);
if (delta_time > 0xFFFF) {
delta_time = 0xFFFF;
}
return (uint16_t)delta_time;
}
static void Timer_Silence_Reset(
void)
{
SilenceTime = 0;
timer_reset(TIMER_SILENCE);
}
static void dlmstp_millisecond_timer(
void)
{
INCREMENT_AND_LIMIT_UINT16(SilenceTime);
}
#if !defined (_WIN32)
void Sleep(
unsigned long milliseconds)
{
struct timespec timeOut, remains;
timeOut.tv_sec = milliseconds / 1000;
timeOut.tv_nsec = (milliseconds - (timeOut.tv_sec * 1000)) * 10000000;
nanosleep(&timeOut, &remains);
}
#endif
void *milliseconds_task(
void *pArg)
{
(void) pArg;
for (;;) {
Sleep(1);
dlmstp_millisecond_timer();
}
return NULL;
}
#if defined(_WIN32)
/*************************************************************************
* Description: Timer task
* Returns: none
* Notes: none
*************************************************************************/
static void milliseconds_task_win32(
void *pArg)
{
milliseconds_task(pArg);
}
#endif
/* functions used by the MS/TP state machine to put or get data */
uint16_t MSTP_Put_Receive(
volatile struct mstp_port_struct_t *mstp_port)
@@ -313,13 +248,6 @@ int main(
volatile struct mstp_port_struct_t *mstp_port;
long my_baud = 38400;
uint32_t packet_count = 0;
#if defined(_WIN32)
unsigned long hThread = 0;
uint32_t arg_value = 0;
#else
int rc = 0;
pthread_t hThread;
#endif
/* mimic our pointer in the state machine */
mstp_port = &MSTP_Port;
@@ -357,18 +285,6 @@ int main(
mstp_port->Lurking = true;
fprintf(stdout, "mstpcap: Using %s for capture at %ld bps.\n",
RS485_Interface(), (long) RS485_Get_Baud_Rate());
#if defined(_WIN32)
hThread = _beginthread(milliseconds_task_win32, 4096, &arg_value);
if (hThread == 0) {
fprintf(stderr, "Failed to start timer task\n");
}
(void) SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
#else
/* start our MilliSec task */
rc = pthread_create(&hThread, NULL, milliseconds_task, NULL);
signal_init();
#endif
atexit(cleanup);
filename_create_new();
/* run forever */
+1 -1
View File
@@ -38,7 +38,7 @@ BACNET_DEFINES = -DPRINT_ENABLED=1 -DBACAPP_ALL
BACDL_DEFINE=-DBACDL_BIP=1 -DUSE_INADDR=1
DEFINES = $(BACNET_DEFINES) $(BACDL_DEFINE)
SRCS = main.c
SRCS = main.c \
OBJS = $(SRCS:.c=.obj)