Indented.

This commit is contained in:
skarg
2007-11-29 15:56:53 +00:00
parent c585241c03
commit 411d6c1b24
236 changed files with 17864 additions and 15724 deletions
+135 -122
View File
@@ -58,21 +58,25 @@ static uint8_t RxBuffer[MAX_MPDU];
/* Timer that indicates line silence - and functions */
static uint16_t SilenceTime;
#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;}
static uint16_t Timer_Silence(void)
static uint16_t Timer_Silence(
void)
{
return SilenceTime;
}
static void Timer_Silence_Reset(void)
static void Timer_Silence_Reset(
void)
{
SilenceTime = 0;
}
void dlmstp_millisecond_timer(void)
void dlmstp_millisecond_timer(
void)
{
INCREMENT_AND_LIMIT_UINT16(SilenceTime);
}
void dlmstp_reinit(void)
void dlmstp_reinit(
void)
{
/*RS485_Reinit(); */
dlmstp_set_mac_address(DEFAULT_MAC_ADDRESS);
@@ -80,7 +84,8 @@ void dlmstp_reinit(void)
dlmstp_set_max_master(DEFAULT_MAX_MASTER);
}
void dlmstp_cleanup(void)
void dlmstp_cleanup(
void)
{
/* nothing to do for static buffers */
if (Received_Frame_Flag) {
@@ -92,11 +97,12 @@ void dlmstp_cleanup(void)
}
/* returns number of bytes sent on success, zero on failure */
int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
int dlmstp_send_pdu(
BACNET_ADDRESS * dest, /* destination address */
BACNET_NPDU_DATA * npdu_data, /* network information */
uint8_t * pdu, /* any data to be sent - may be null */
uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len)
{ /* number of bytes of data */
{ /* number of bytes of data */
int bytes_sent = 0;
unsigned i = 0;
@@ -121,18 +127,18 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
}
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 */
{
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 pdu_len = 0;
DWORD wait_status = 0;
(void)max_pdu;
(void) max_pdu;
/* see if there is a packet available, and a place
to put the reply (if necessary) and process it */
wait_status = WaitForSingleObject(Receive_Packet_Flag,timeout);
wait_status = WaitForSingleObject(Receive_Packet_Flag, timeout);
if (wait_status == WAIT_OBJECT_0) {
if (Receive_Packet.ready) {
if (Receive_Packet.pdu_len) {
@@ -144,8 +150,7 @@ uint16_t dlmstp_receive(
}
if (pdu) {
memmove(pdu,
&Receive_Packet.pdu,
sizeof(Receive_Packet.pdu));
&Receive_Packet.pdu, sizeof(Receive_Packet.pdu));
}
pdu_len = Receive_Packet.pdu_len;
}
@@ -156,12 +161,14 @@ uint16_t dlmstp_receive(
return pdu_len;
}
static void dlmstp_receive_fsm_task(void *pArg)
static void dlmstp_receive_fsm_task(
void *pArg)
{
bool received_frame;
(void)pArg;
(void)SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
(void) pArg;
(void) SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
while (TRUE) {
/* only do receive state machine while we don't have a frame */
if ((MSTP_Port.ReceivedValidFrame == false) &&
@@ -180,12 +187,14 @@ static void dlmstp_receive_fsm_task(void *pArg)
}
}
static void dlmstp_master_fsm_task(void *pArg)
static void dlmstp_master_fsm_task(
void *pArg)
{
DWORD dwMilliseconds = 0;
(void)pArg;
(void)SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
(void) pArg;
(void) SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
while (TRUE) {
switch (MSTP_Port.master_state) {
case MSTP_MASTER_STATE_IDLE:
@@ -202,22 +211,26 @@ static void dlmstp_master_fsm_task(void *pArg)
break;
}
if (dwMilliseconds)
WaitForSingleObject(Received_Frame_Flag,dwMilliseconds);
WaitForSingleObject(Received_Frame_Flag, dwMilliseconds);
MSTP_Master_Node_FSM(&MSTP_Port);
}
}
static void dlmstp_millisecond_task(void *pArg)
static void dlmstp_millisecond_task(
void *pArg)
{
(void)pArg;
(void)SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
(void) pArg;
(void) SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
for (;;) {
dlmstp_millisecond_timer();
Sleep(1);
}
}
void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address)
void dlmstp_fill_bacnet_address(
BACNET_ADDRESS * src,
uint8_t mstp_address)
{
int i = 0;
@@ -242,7 +255,7 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address)
/* for the MS/TP state machine to use for putting received data */
uint16_t MSTP_Put_Receive(
volatile struct mstp_port_struct_t *mstp_port)
volatile struct mstp_port_struct_t *mstp_port)
{
uint16_t pdu_len = 0;
BOOL rc;
@@ -252,9 +265,8 @@ uint16_t MSTP_Put_Receive(
pdu_len = mstp_port->DataLength;
if (pdu_len > sizeof(Receive_Packet.pdu))
pdu_len = sizeof(Receive_Packet.pdu);
memmove((void *) & Receive_Packet.pdu[0],
(void *) & mstp_port->InputBuffer[0],
pdu_len);
memmove((void *) &Receive_Packet.pdu[0],
(void *) &mstp_port->InputBuffer[0], pdu_len);
dlmstp_fill_bacnet_address(&Receive_Packet.address,
mstp_port->SourceAddress);
Receive_Packet.pdu_len = mstp_port->DataLength;
@@ -268,13 +280,13 @@ uint16_t MSTP_Put_Receive(
/* for the MS/TP state machine to use for getting data to send */
/* Return: amount of PDU data */
uint16_t MSTP_Get_Send(
volatile struct mstp_port_struct_t *mstp_port,
unsigned timeout) /* milliseconds to wait for a packet */
{
volatile struct mstp_port_struct_t * mstp_port,
unsigned timeout)
{ /* milliseconds to wait for a packet */
uint16_t pdu_len = 0;
uint8_t destination = 0; /* destination address */
(void)timeout;
(void) timeout;
if (!Transmit_Packet.ready) {
return 0;
}
@@ -288,26 +300,24 @@ uint16_t MSTP_Get_Send(
return 0;
}
/* convert the PDU into the MSTP Frame */
pdu_len = MSTP_Create_Frame(
&mstp_port->OutputBuffer[0], /* <-- loading this */
pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize,
Transmit_Packet.frame_type,
destination,
mstp_port->This_Station,
&Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
&Transmit_Packet.pdu[0], Transmit_Packet.pdu_len);
Transmit_Packet.ready = false;
return pdu_len;
}
bool dlmstp_compare_data_expecting_reply(
uint8_t *request_pdu,
uint8_t * request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
uint8_t *reply_pdu,
uint8_t * reply_pdu,
uint16_t reply_pdu_len,
BACNET_ADDRESS *dest_address)
BACNET_ADDRESS * dest_address)
{
uint16_t offset;
/* One way to check the message is to compare NPDU
@@ -335,12 +345,12 @@ bool dlmstp_compare_data_expecting_reply(
if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
return false;
}
request.invoke_id = request_pdu[offset+2];
request.invoke_id = request_pdu[offset + 2];
/* segmented message? */
if (request_pdu[offset] & BIT3)
request.service_choice = request_pdu[offset+5];
request.service_choice = request_pdu[offset + 5];
else
request.service_choice = request_pdu[offset+3];
request.service_choice = request_pdu[offset + 3];
/* decode the reply data */
bacnet_address_copy(&reply.address, dest_address);
offset = npdu_decode(&reply_pdu[0],
@@ -353,32 +363,32 @@ bool dlmstp_compare_data_expecting_reply(
reply.pdu_type = reply_pdu[offset] & 0xF0;
switch (reply.pdu_type) {
case PDU_TYPE_CONFIRMED_SERVICE_REQUEST:
reply.invoke_id = reply_pdu[offset+2];
reply.invoke_id = reply_pdu[offset + 2];
/* segmented message? */
if (reply_pdu[offset] & BIT3)
reply.service_choice = reply_pdu[offset+5];
reply.service_choice = reply_pdu[offset + 5];
else
reply.service_choice = reply_pdu[offset+3];
reply.service_choice = reply_pdu[offset + 3];
break;
case PDU_TYPE_SIMPLE_ACK:
reply.invoke_id = reply_pdu[offset+1];
reply.service_choice = reply_pdu[offset+2];
reply.invoke_id = reply_pdu[offset + 1];
reply.service_choice = reply_pdu[offset + 2];
break;
case PDU_TYPE_COMPLEX_ACK:
reply.invoke_id = reply_pdu[offset+1];
reply.invoke_id = reply_pdu[offset + 1];
/* segmented message? */
if (reply_pdu[offset] & BIT3)
reply.service_choice = reply_pdu[offset+4];
reply.service_choice = reply_pdu[offset + 4];
else
reply.service_choice = reply_pdu[offset+2];
reply.service_choice = reply_pdu[offset + 2];
break;
case PDU_TYPE_ERROR:
reply.invoke_id = reply_pdu[offset+1];
reply.service_choice = reply_pdu[offset+2];
reply.invoke_id = reply_pdu[offset + 1];
reply.service_choice = reply_pdu[offset + 2];
break;
case PDU_TYPE_REJECT:
case PDU_TYPE_ABORT:
reply.invoke_id = reply_pdu[offset+1];
reply.invoke_id = reply_pdu[offset + 1];
break;
default:
return false;
@@ -412,14 +422,14 @@ bool dlmstp_compare_data_expecting_reply(
/* Get the reply to a DATA_EXPECTING_REPLY frame, or nothing */
uint16_t MSTP_Get_Reply(
volatile struct mstp_port_struct_t *mstp_port,
unsigned timeout) /* milliseconds to wait for a packet */
{
uint16_t pdu_len = 0; /* return value */
volatile struct mstp_port_struct_t * mstp_port,
unsigned timeout)
{ /* milliseconds to wait for a packet */
uint16_t pdu_len = 0; /* return value */
uint8_t destination = 0; /* destination address */
bool matched = false;
(void)timeout;
(void) timeout;
if (!Transmit_Packet.ready) {
return 0;
}
@@ -433,29 +443,26 @@ uint16_t MSTP_Get_Reply(
return 0;
}
/* is this the reply to the DER? */
matched = dlmstp_compare_data_expecting_reply(
&mstp_port->InputBuffer[0],
matched = dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0],
mstp_port->DataLength,
mstp_port->SourceAddress,
&Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len,
&Transmit_Packet.address);
Transmit_Packet.pdu_len, &Transmit_Packet.address);
if (!matched)
return 0;
/* convert the PDU into the MSTP Frame */
pdu_len = MSTP_Create_Frame(
&mstp_port->OutputBuffer[0], /* <-- loading this */
pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize,
Transmit_Packet.frame_type,
destination, mstp_port->This_Station,
&Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
&Transmit_Packet.pdu[0], Transmit_Packet.pdu_len);
Transmit_Packet.ready = false;
return pdu_len;
}
void dlmstp_set_mac_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) {
@@ -472,7 +479,8 @@ void dlmstp_set_mac_address(uint8_t mac_address)
return;
}
uint8_t dlmstp_mac_address(void)
uint8_t dlmstp_mac_address(
void)
{
return MSTP_Port.This_Station;
}
@@ -484,7 +492,8 @@ uint8_t dlmstp_mac_address(void)
/* 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(uint8_t max_info_frames)
void dlmstp_set_max_info_frames(
uint8_t max_info_frames)
{
if (max_info_frames >= 1) {
MSTP_Port.Nmax_info_frames = max_info_frames;
@@ -498,7 +507,8 @@ void dlmstp_set_max_info_frames(uint8_t max_info_frames)
return;
}
uint8_t dlmstp_max_info_frames(void)
uint8_t dlmstp_max_info_frames(
void)
{
return MSTP_Port.Nmax_info_frames;
}
@@ -508,7 +518,8 @@ uint8_t dlmstp_max_info_frames(void)
/* allowable address for master nodes. The value of Max_Master shall be */
/* less than or equal to 127. If Max_Master is not writable in a node, */
/* its value shall be 127. */
void dlmstp_set_max_master(uint8_t max_master)
void dlmstp_set_max_master(
uint8_t max_master)
{
if (max_master <= 127) {
if (MSTP_Port.This_Station <= max_master) {
@@ -524,25 +535,29 @@ void dlmstp_set_max_master(uint8_t max_master)
return;
}
uint8_t dlmstp_max_master(void)
uint8_t dlmstp_max_master(
void)
{
return MSTP_Port.Nmax_master;
}
/* RS485 Baud Rate 9600, 19200, 38400, 57600, 115200 */
void dlmstp_set_baud_rate(uint32_t baud)
void dlmstp_set_baud_rate(
uint32_t baud)
{
RS485_Set_Baud_Rate(baud);
}
uint32_t dlmstp_baud_rate(void)
uint32_t dlmstp_baud_rate(
void)
{
return RS485_Get_Baud_Rate();
return RS485_Get_Baud_Rate();
}
void dlmstp_get_my_address(BACNET_ADDRESS * my_address)
void dlmstp_get_my_address(
BACNET_ADDRESS * my_address)
{
int i = 0; /* counter */
int i = 0; /* counter */
my_address->mac_len = 1;
my_address->mac[0] = MSTP_Port.This_Station;
@@ -555,15 +570,16 @@ void dlmstp_get_my_address(BACNET_ADDRESS * my_address)
return;
}
void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest)
{ /* destination address */
int i = 0; /* counter */
void dlmstp_get_broadcast_address(
BACNET_ADDRESS * dest)
{ /* destination address */
int i = 0; /* counter */
if (dest) {
dest->mac_len = 1;
dest->mac[0] = MSTP_BROADCAST_ADDRESS;
dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; /* always zero when DNET is broadcast */
dest->len = 0; /* always zero when DNET is broadcast */
for (i = 0; i < MAX_MAC_LEN; i++) {
dest->adr[i] = 0;
}
@@ -572,7 +588,8 @@ void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest)
return;
}
bool dlmstp_init(char *ifname)
bool dlmstp_init(
char *ifname)
{
unsigned long hThread = 0;
uint32_t arg_value = 0;
@@ -580,10 +597,10 @@ bool dlmstp_init(char *ifname)
/* initialize packet queue */
Receive_Packet.ready = false;
Receive_Packet.pdu_len = 0;
Receive_Packet_Flag = CreateSemaphore (NULL,0,1,"dlmstpReceivePacket");
Receive_Packet_Flag = CreateSemaphore(NULL, 0, 1, "dlmstpReceivePacket");
if (Receive_Packet_Flag == NULL)
exit(1);
Received_Frame_Flag = CreateSemaphore (NULL,0,1,"dlsmtpReceiveFrame");
Received_Frame_Flag = CreateSemaphore(NULL, 0, 1, "dlsmtpReceiveFrame");
if (Received_Frame_Flag == NULL) {
CloseHandle(Receive_Packet_Flag);
exit(1);
@@ -593,7 +610,7 @@ bool dlmstp_init(char *ifname)
if (ifname) {
RS485_Set_Interface(ifname);
#if PRINT_ENABLED
fprintf(stderr,"MS/TP Interface: %s\n", ifname);
fprintf(stderr, "MS/TP Interface: %s\n", ifname);
#endif
}
RS485_Initialize();
@@ -608,17 +625,17 @@ bool dlmstp_init(char *ifname)
uint8_t data;
/* FIXME: implement your data storage */
data = 64; /* I2C_Read_Byte(
EEPROM_DEVICE_ADDRESS,
EEPROM_MSTP_MAC_ADDR); */
data = 64; /* I2C_Read_Byte(
EEPROM_DEVICE_ADDRESS,
EEPROM_MSTP_MAC_ADDR); */
if (data <= 127)
MSTP_Port.This_Station = data;
else
dlmstp_set_my_address(DEFAULT_MAC_ADDRESS);
/* FIXME: implement your data storage */
data = 127; /* I2C_Read_Byte(
EEPROM_DEVICE_ADDRESS,
EEPROM_MSTP_MAX_MASTER_ADDR); */
data = 127; /* I2C_Read_Byte(
EEPROM_DEVICE_ADDRESS,
EEPROM_MSTP_MAX_MASTER_ADDR); */
if ((data <= 127) && (data >= MSTP_Port.This_Station))
MSTP_Port.Nmax_master = data;
else
@@ -634,23 +651,20 @@ bool dlmstp_init(char *ifname)
dlmstp_set_max_info_frames(DEFAULT_MAX_INFO_FRAMES);
#endif
#if PRINT_ENABLED
fprintf(stderr,"MS/TP MAC: %02X\n",
MSTP_Port.This_Station);
fprintf(stderr,"MS/TP Max_Master: %02X\n",
MSTP_Port.Nmax_master);
fprintf(stderr,"MS/TP Max_Info_Frames: %u\n",
MSTP_Port.Nmax_info_frames);
fprintf(stderr, "MS/TP MAC: %02X\n", MSTP_Port.This_Station);
fprintf(stderr, "MS/TP Max_Master: %02X\n", MSTP_Port.Nmax_master);
fprintf(stderr, "MS/TP Max_Info_Frames: %u\n", MSTP_Port.Nmax_info_frames);
#endif
/* start the threads */
hThread = _beginthread(dlmstp_millisecond_task,4096,&arg_value);
hThread = _beginthread(dlmstp_millisecond_task, 4096, &arg_value);
if (hThread == 0) {
fprintf(stderr, "Failed to start timer task\n");
}
hThread = _beginthread(dlmstp_receive_fsm_task,4096,&arg_value);
hThread = _beginthread(dlmstp_receive_fsm_task, 4096, &arg_value);
if (hThread == 0) {
fprintf(stderr, "Failed to start recive FSM task\n");
}
hThread = _beginthread(dlmstp_master_fsm_task,4096,&arg_value);
hThread = _beginthread(dlmstp_master_fsm_task, 4096, &arg_value);
if (hThread == 0) {
fprintf(stderr, "Failed to start Master Node FSM task\n");
}
@@ -661,26 +675,26 @@ bool dlmstp_init(char *ifname)
#ifdef TEST_DLMSTP
#include <stdio.h>
void apdu_handler(BACNET_ADDRESS * src, /* source address */
uint8_t * apdu, /* APDU data */
uint16_t pdu_len) /* for confirmed messages */
{
(void)src;
(void)apdu;
(void)pdu_len;
void apdu_handler(
BACNET_ADDRESS * src, /* source address */
uint8_t * apdu, /* APDU data */
uint16_t pdu_len)
{ /* for confirmed messages */
(void) src;
(void) apdu;
(void) pdu_len;
}
/* returns a delta timestamp */
uint32_t timestamp_ms(void)
uint32_t timestamp_ms(
void)
{
DWORD ticks = 0, delta_ticks = 0;
static DWORD last_ticks = 0;
ticks = GetTickCount();
delta_ticks =
(ticks >= last_ticks ?
ticks - last_ticks :
MAXDWORD - last_ticks);
(ticks >= last_ticks ? ticks - last_ticks : MAXDWORD - last_ticks);
last_ticks = ticks;
return delta_ticks;
@@ -688,7 +702,9 @@ uint32_t timestamp_ms(void)
static char *Network_Interface = NULL;
int main(int argc, char *argv[])
int main(
int argc,
char *argv[])
{
uint16_t pdu_len = 0;
@@ -703,14 +719,11 @@ int main(int argc, char *argv[])
dlmstp_init(Network_Interface);
/* forever task */
for (;;) {
pdu_len = dlmstp_receive(NULL,NULL,0,INFINITE);
pdu_len = dlmstp_receive(NULL, NULL, 0, INFINITE);
#if 0
MSTP_Create_And_Send_Frame(
&MSTP_Port,
MSTP_Create_And_Send_Frame(&MSTP_Port,
FRAME_TYPE_TEST_REQUEST,
MSTP_Port.SourceAddress,
MSTP_Port.This_Station,
NULL, 0);
MSTP_Port.SourceAddress, MSTP_Port.This_Station, NULL, 0);
#endif
}