From 758b32de2913b29bee08b886d9ab0425b6f5d2a4 Mon Sep 17 00:00:00 2001 From: skarg Date: Sat, 30 Jun 2007 23:36:54 +0000 Subject: [PATCH] Modified the server to work with MSTP datalink. Still in progress. --- bacnet-stack/demo/server/Makefile | 9 ++++-- bacnet-stack/demo/server/main.c | 8 ++++++ bacnet-stack/dlmstp.h | 13 ++++++--- bacnet-stack/ports/linux/dlmstp.c | 48 +++++++++++++++++++++---------- bacnet-stack/ports/linux/mstp.h | 2 +- 5 files changed, 58 insertions(+), 22 deletions(-) diff --git a/bacnet-stack/demo/server/Makefile b/bacnet-stack/demo/server/Makefile index e2a8063d..65cdd67d 100644 --- a/bacnet-stack/demo/server/Makefile +++ b/bacnet-stack/demo/server/Makefile @@ -10,7 +10,8 @@ BASEDIR = . # Configure the BACnet Datalink Layer #BACDL_DEFINE = -DBACDL_ETHERNET=1 #BACDL_DEFINE = -DBACDL_ARCNET=1 -BACDL_DEFINE = -DBACDL_BIP=1 -DBIP_DEBUG +BACDL_DEFINE = -DBACDL_MSTP=1 +#BACDL_DEFINE = -DBACDL_BIP=1 -DBIP_DEBUG BACNET_DEFINES = -DBACFILE=1 -DPRINT_ENABLED=1 -DBIG_ENDIAN=0 DEFINES = $(BACNET_DEFINES) $(BACDL_DEFINE) @@ -28,6 +29,10 @@ SRCS = main.c \ $(BACNET_PORT)/bip-init.c \ $(BACNET_PORT)/ethernet.c \ $(BACNET_PORT)/arcnet.c \ + $(BACNET_PORT)/dlmstp.c \ + $(BACNET_PORT)/mstp.c \ + $(BACNET_PORT)/rs485.c \ + $(BACNET_ROOT)/crc.c \ $(BACNET_ROOT)/bip.c \ $(BACNET_HANDLER)/txbuf.c \ $(BACNET_HANDLER)/noserv.c \ @@ -83,7 +88,7 @@ OBJS = ${SRCS:.c=.o} all: ${TARGET} ${TARGET}: ${OBJS} - ${CC} -o $@ ${OBJS} + ${CC} -pthread -o $@ ${OBJS} .c.o: ${CC} -c ${CFLAGS} $*.c -o $@ diff --git a/bacnet-stack/demo/server/main.c b/bacnet-stack/demo/server/main.c index a958b40e..6cf33800 100644 --- a/bacnet-stack/demo/server/main.c +++ b/bacnet-stack/demo/server/main.c @@ -107,6 +107,14 @@ int main(int argc, char *argv[]) Network_Interface = argv[2]; if (argc > 3) bip_set_port(strtol(argv[3], NULL, 0)); +#endif +#if defined(BACDL_MSTP) + if (argc > 2) { + Network_Interface = argv[2]; + } + if (argc > 3) { + dlmstp_set_mac_address(strtol(argv[1], NULL, 0)); + } #endif printf("BACnet Server Demo\n" "BACnet Device ID: %u\r\n", diff --git a/bacnet-stack/dlmstp.h b/bacnet-stack/dlmstp.h index c7465bdc..81bb4e52 100644 --- a/bacnet-stack/dlmstp.h +++ b/bacnet-stack/dlmstp.h @@ -57,7 +57,7 @@ typedef struct dlmstp_packet { extern "C" { #endif /* __cplusplus */ - void dlmstp_init(void); + bool dlmstp_init(char *ifname); void dlmstp_cleanup(void); void dlmstp_millisecond_timer(void); @@ -80,8 +80,8 @@ extern "C" { /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ - void dlmstp_set_max_info_frames(unsigned max_info_frames); - unsigned dlmstp_max_info_frames(void); + void dlmstp_set_max_info_frames(uint8_t max_info_frames); + uint8_t dlmstp_max_info_frames(void); /* This parameter represents the value of the Max_Master property of the */ /* node's Device object. The value of Max_Master specifies the highest */ @@ -91,7 +91,10 @@ extern "C" { void dlmstp_set_max_master(uint8_t max_master); uint8_t dlmstp_max_master(void); - void dlmstp_set_my_address(uint8_t my_address); + /* MAC address 0-127 */ + void dlmstp_set_mac_address(uint8_t my_address); + uint8_t dlmstp_mac_address(void); + void dlmstp_get_my_address(BACNET_ADDRESS * my_address); void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest); /* destination address */ @@ -101,6 +104,8 @@ extern "C" { uint16_t pdu_len); + + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/bacnet-stack/ports/linux/dlmstp.c b/bacnet-stack/ports/linux/dlmstp.c index 56608b6e..81184b1a 100644 --- a/bacnet-stack/ports/linux/dlmstp.c +++ b/bacnet-stack/ports/linux/dlmstp.c @@ -77,7 +77,7 @@ static void *dlmstp_milliseconds_task(void *pArg) void dlmstp_reinit(void) { //RS485_Reinit(); - dlmstp_set_my_address(DEFAULT_MAC_ADDRESS); + dlmstp_set_mac_address(DEFAULT_MAC_ADDRESS); dlmstp_set_max_info_frames(DEFAULT_MAX_INFO_FRAMES); dlmstp_set_max_master(DEFAULT_MAX_MASTER); } @@ -156,21 +156,39 @@ static void *dlmstp_master_fsm_task(void *pArg) if (MSTP_Port.receive_state == MSTP_RECEIVE_STATE_IDLE) { while (MSTP_Master_Node_FSM(&MSTP_Port)) { sched_yield(); - }; - } - /* see if there is a packet available, and a place - to put the reply (if necessary) and process it */ - if (Receive_Buffer.ready && !MSTP_Port.TxReady) { - if (Receive_Buffer.pdu_len) { - MSTP_Packets++; - npdu_handler(&Receive_Buffer.address, - &Receive_Buffer.pdu[0], Receive_Buffer.pdu_len); } - Receive_Buffer.ready = false; } } } +/* copy the packet if one is received. + Return the length of the packet */ +uint16_t dlmstp_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ + unsigned timeout) /* milliseconds to wait for a packet */ +{ + uint16_t len = 0; + /* see if there is a packet available, and a place + to put the reply (if necessary) and process it */ + if (Receive_Buffer.ready && !MSTP_Port.TxReady) { + if (Receive_Buffer.pdu_len) { + MSTP_Packets++; + len = Receive_Buffer.pdu_len; + memcpy(src,&Receive_Buffer.address,sizeof(Receive_Buffer.address)); + /* FIXME: check pdu_len and max_pdu */ + memcpy(pdu,&Receive_Buffer.pdu[0],len); + } + Receive_Buffer.ready = false; + } else { + sched_yield(); + } + + return len; +} + + void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) { int i = 0; @@ -207,7 +225,7 @@ uint16_t dlmstp_put_receive(uint8_t src, /* source MS/TP address */ return pdu_len; } -void dlmstp_set_my_address(uint8_t mac_address) +void dlmstp_set_mac_address(uint8_t mac_address) { /* Master Nodes can only have address 0-127 */ if (mac_address <= 127) { @@ -250,7 +268,7 @@ void dlmstp_set_max_info_frames(uint8_t max_info_frames) return; } -unsigned dlmstp_max_info_frames(void) +uint8_t dlmstp_max_info_frames(void) { return MSTP_Port.Nmax_info_frames; } @@ -331,7 +349,7 @@ bool dlmstp_init(char *ifname) if (data <= 127) MSTP_Port.This_Station = data; else - dlmstp_set_my_address(DEFAULT_MAC_ADDRESS); + dlmstp_set_mac_address(DEFAULT_MAC_ADDRESS); if ((data <= 127) && (data >= MSTP_Port.This_Station)) MSTP_Port.Nmax_master = data; else @@ -376,7 +394,7 @@ int main(int argc, char *argv[]) RS485_Set_Interface(argv[1]); } RS485_Set_Baud_Rate(38400); - dlmstp_set_my_address(0x05); + dlmstp_set_mac_address(0x05); dlmstp_set_max_info_frames(DEFAULT_MAX_INFO_FRAMES); dlmstp_set_max_master(DEFAULT_MAX_MASTER); dlmstp_init(); diff --git a/bacnet-stack/ports/linux/mstp.h b/bacnet-stack/ports/linux/mstp.h index bef370b1..76b402c5 100644 --- a/bacnet-stack/ports/linux/mstp.h +++ b/bacnet-stack/ports/linux/mstp.h @@ -194,7 +194,7 @@ struct mstp_port_struct_t { /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ - unsigned Nmax_info_frames; + uint8_t Nmax_info_frames; /* This parameter represents the value of the Max_Master property of the */ /* node's Device object. The value of Max_Master specifies the highest */