diff --git a/bacnet-stack/demo/mstpcap/main.c b/bacnet-stack/demo/mstpcap/main.c index 173f1751..c6d05d17 100644 --- a/bacnet-stack/demo/mstpcap/main.c +++ b/bacnet-stack/demo/mstpcap/main.c @@ -117,61 +117,61 @@ uint16_t MSTP_Get_Reply( } static const char *Capture_Filename = "mstp.cap"; -static FILE *pFile = NULL; /* stream pointer */ +static FILE *pFile = NULL; /* stream pointer */ /* write packet to file in libpcap format */ -static void write_global_header(void) +static void write_global_header( + void) { 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 */ + 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(Capture_Filename, "wb"); if (pFile) { - fwrite(&magic_number,sizeof(magic_number),1,pFile); - fwrite(&version_major,sizeof(version_major),1,pFile); - fwrite(&version_minor,sizeof(version_minor),1,pFile); - fwrite(&thiszone,sizeof(thiszone),1,pFile); - fwrite(&sigfigs,sizeof(sigfigs),1,pFile); - fwrite(&snaplen,sizeof(snaplen),1,pFile); - fwrite(&network,sizeof(network),1,pFile); + fwrite(&magic_number, sizeof(magic_number), 1, pFile); + fwrite(&version_major, sizeof(version_major), 1, pFile); + fwrite(&version_minor, sizeof(version_minor), 1, pFile); + fwrite(&thiszone, sizeof(thiszone), 1, pFile); + fwrite(&sigfigs, sizeof(sigfigs), 1, pFile); + fwrite(&snaplen, sizeof(snaplen), 1, pFile); + fwrite(&network, sizeof(network), 1, pFile); fflush(pFile); - fprintf(stdout,"mstpcap: saving capture to %s\n", - Capture_Filename); + fprintf(stdout, "mstpcap: saving capture to %s\n", Capture_Filename); } else { - fprintf(stderr,"mstpcap: failed to open %s: %s\n", - Capture_Filename, strerror(errno)); + fprintf(stderr, "mstpcap: failed to open %s: %s\n", Capture_Filename, + strerror(errno)); } } static void write_received_packet( volatile struct mstp_port_struct_t *mstp_port) { - 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 */ - uint8_t header[8]; /* MS/TP header */ + 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 */ + uint8_t header[8]; /* MS/TP header */ struct timeval tv; if (pFile) { gettimeofday(&tv, NULL); ts_sec = tv.tv_sec; ts_usec = tv.tv_usec; - fwrite(&ts_sec,sizeof(ts_sec),1,pFile); - fwrite(&ts_usec,sizeof(ts_usec),1,pFile); + fwrite(&ts_sec, sizeof(ts_sec), 1, pFile); + fwrite(&ts_usec, sizeof(ts_usec), 1, pFile); if (mstp_port->DataLength) { incl_len = orig_len = 8 + mstp_port->DataLength + 2; } else { incl_len = orig_len = 8; } - fwrite(&incl_len,sizeof(incl_len),1,pFile); - fwrite(&orig_len,sizeof(orig_len),1,pFile); + fwrite(&incl_len, sizeof(incl_len), 1, pFile); + fwrite(&orig_len, sizeof(orig_len), 1, pFile); header[0] = 0x55; header[1] = 0xFF; header[2] = mstp_port->FrameType; @@ -180,37 +180,40 @@ static void write_received_packet( header[5] = HI_BYTE(mstp_port->DataLength); header[6] = LO_BYTE(mstp_port->DataLength); header[7] = mstp_port->HeaderCRCActual; - fwrite(header,sizeof(header),1,pFile); + fwrite(header, sizeof(header), 1, pFile); if (mstp_port->DataLength) { - fwrite(mstp_port->InputBuffer,mstp_port->DataLength,1,pFile); - fwrite((char *)&mstp_port->DataCRCActualMSB,1,1,pFile); - fwrite((char *)&mstp_port->DataCRCActualLSB,1,1,pFile); + fwrite(mstp_port->InputBuffer, mstp_port->DataLength, 1, pFile); + fwrite((char *) &mstp_port->DataCRCActualMSB, 1, 1, pFile); + fwrite((char *) &mstp_port->DataCRCActualLSB, 1, 1, pFile); } } else { - fprintf(stderr,"mstpcap: failed to open %s: %s\n", - Capture_Filename, strerror(errno)); + fprintf(stderr, "mstpcap: failed to open %s: %s\n", Capture_Filename, + strerror(errno)); } } -static void cleanup(void) +static void cleanup( + void) { if (pFile) { - fflush(pFile); /* stream pointer */ - fclose(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ + fclose(pFile); /* stream pointer */ } pFile = NULL; } #if (!defined(_WIN32)) -static void sig_int(int signo) +static void sig_int( + int signo) { - (void)signo; + (void) signo; cleanup(); exit(0); } -void signal_init(void) +void signal_init( + void) { signal(SIGINT, sig_int); signal(SIGHUP, sig_int); @@ -256,8 +259,8 @@ int main( MSTP_Port.SilenceTimerReset = Timer_Silence_Reset; MSTP_Init(mstp_port); mstp_port->Lurking = true; - fprintf(stdout,"mstpcap: Using %s for capture at %ld bps.\n", - RS485_Interface(), (long)RS485_Get_Baud_Rate()); + 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, 4096, &arg_value); if (hThread == 0) { @@ -288,7 +291,7 @@ int main( packet_count++; } if (!(packet_count % 100)) { - fprintf(stdout,"\r%hu packets",packet_count); + fprintf(stdout, "\r%hu packets", packet_count); } } diff --git a/bacnet-stack/demo/ucov/main.c b/bacnet-stack/demo/ucov/main.c index f32d3cd4..3bf3b5ba 100644 --- a/bacnet-stack/demo/ucov/main.c +++ b/bacnet-stack/demo/ucov/main.c @@ -212,8 +212,8 @@ int main(int argc, char *argv[]) { filename_remove_path(argv[0]), filename_remove_path(argv[0])); return 0; } - /* decode the command line parameters */ - cov_data.subscriberProcessIdentifier = strtol(argv[1], NULL, 0); + /* decode the command line parameters */ cov_data. + subscriberProcessIdentifier = strtol(argv[1], NULL, 0); cov_data.initiatingDeviceIdentifier = strtol(argv[2], NULL, 0); cov_data.monitoredObjectIdentifier.type = strtol(argv[3], NULL, 0); cov_data.monitoredObjectIdentifier.instance = strtol(argv[4], NULL, 0); diff --git a/bacnet-stack/ports/linux/dlmstp.c b/bacnet-stack/ports/linux/dlmstp.c index 60ca1e6a..5ca8d855 100644 --- a/bacnet-stack/ports/linux/dlmstp.c +++ b/bacnet-stack/ports/linux/dlmstp.c @@ -127,7 +127,8 @@ int dlmstp_send_pdu( packet.pdu_len = pdu_len; memmove(&packet.pdu[0], &pdu[0], pdu_len); memmove(&packet.address, dest, sizeof(packet.address)); - rc = mq_send(NPDU_Transmit_Queue, (const char *)&packet, sizeof(packet), 0); + rc = mq_send(NPDU_Transmit_Queue, (const char *) &packet, + sizeof(packet), 0); if (rc > 0) bytes_sent = rc; } @@ -146,10 +147,10 @@ uint16_t dlmstp_receive( uint16_t pdu_len = 0; mqd_t received_bytes = 0; DLMSTP_PACKET packet; - struct timespec queue_timeout = {0}; + struct timespec queue_timeout = { 0 }; time_t epoch_time = 0; unsigned msg_prio = 0; - char buffer[sizeof(struct dlmstp_packet)+1]; + char buffer[sizeof(struct dlmstp_packet) + 1]; if (NPDU_Receive_Queue == -1) { return 0; @@ -166,13 +167,10 @@ uint16_t dlmstp_receive( } /* get current time */ epoch_time = time(NULL); - queue_timeout.tv_sec += epoch_time; + queue_timeout.tv_sec += epoch_time; - received_bytes = mq_timedreceive( - NPDU_Receive_Queue, - buffer, - sizeof(buffer), - &msg_prio, + received_bytes = + mq_timedreceive(NPDU_Receive_Queue, buffer, sizeof(buffer), &msg_prio, &queue_timeout); /* See if there is a problem */ @@ -182,10 +180,9 @@ uint16_t dlmstp_receive( /* was immediately available for reading. */ if ((errno != EAGAIN) && (errno != ETIMEDOUT)) { #if PRINT_ENABLED - fprintf(stderr, "MS/TP: NPDU Receive: %s\n", - strerror(errno)); + fprintf(stderr, "MS/TP: NPDU Receive: %s\n", strerror(errno)); #endif - } + } return 0; } @@ -193,7 +190,7 @@ uint16_t dlmstp_receive( return 0; /* copy the buffer into the PDU */ - memmove(&packet,buffer,sizeof(packet)); + memmove(&packet, buffer, sizeof(packet)); pdu_len = packet.pdu_len; memmove(&pdu[0], &packet.pdu[0], pdu_len); memmove(src, &packet.address, sizeof(packet.address)); @@ -285,7 +282,7 @@ uint16_t MSTP_Put_Receive( pdu_len = sizeof(packet.pdu); if (pdu_len) { #if PRINT_ENABLED - fprintf(stderr,"MSTP: packet from FSM.\n"); + fprintf(stderr, "MSTP: packet from FSM.\n"); #endif MSTP_Packets++; memmove(&packet.pdu[0], (void *) &mstp_port->InputBuffer[0], pdu_len); @@ -293,7 +290,7 @@ uint16_t MSTP_Put_Receive( packet.pdu_len = pdu_len; /* ready is not used in this scheme */ packet.ready = true; - mq_send(NPDU_Receive_Queue, (const char *)&packet, sizeof(packet), 0); + mq_send(NPDU_Receive_Queue, (const char *) &packet, sizeof(packet), 0); } return pdu_len; @@ -306,10 +303,10 @@ int dlmstp_get_transmit_packet( unsigned timeout) { /* milliseconds to wait for a packet */ int received_bytes = 0; /* return value */ - struct timespec queue_timeout = {0}; + struct timespec queue_timeout = { 0 }; time_t epoch_time = 0; unsigned msg_prio = 0; - char buffer[sizeof(struct dlmstp_packet)+1]; + char buffer[sizeof(struct dlmstp_packet) + 1]; /* Make sure the socket is open */ if (NPDU_Transmit_Queue == -1) @@ -325,13 +322,10 @@ int dlmstp_get_transmit_packet( } /* get current time */ epoch_time = time(NULL); - queue_timeout.tv_sec += epoch_time; + queue_timeout.tv_sec += epoch_time; - received_bytes = mq_timedreceive( - NPDU_Transmit_Queue, - buffer, - sizeof(buffer), - &msg_prio, + received_bytes = + mq_timedreceive(NPDU_Transmit_Queue, buffer, sizeof(buffer), &msg_prio, &queue_timeout); /* See if there is a problem */ @@ -341,13 +335,14 @@ int dlmstp_get_transmit_packet( /* was immediately available for reading. */ if ((errno != EAGAIN) && (errno != ETIMEDOUT)) { #if PRINT_ENABLED - fprintf(stderr, "MS/TP: Read error in Transmit_Client packet: %s\n", + fprintf(stderr, + "MS/TP: Read error in Transmit_Client packet: %s\n", strerror(errno)); #endif } return 0; } - memmove(packet,buffer,sizeof(packet)); + memmove(packet, buffer, sizeof(packet)); return (received_bytes); } @@ -376,7 +371,7 @@ uint16_t MSTP_Get_Send( return 0; } #if PRINT_ENABLED - fprintf(stderr,"MS/TP: sending packet to FSM.\n"); + fprintf(stderr, "MS/TP: sending packet to FSM.\n"); #endif /* convert the PDU into the MSTP Frame */ pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ @@ -521,23 +516,23 @@ uint16_t MSTP_Get_Reply( /* is this the reply to the DER? */ matched = dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0], - mstp_port->DataLength, mstp_port->SourceAddress, - &packet.pdu[0], packet.pdu_len, - &packet.address); + mstp_port->DataLength, mstp_port->SourceAddress, &packet.pdu[0], + packet.pdu_len, &packet.address); if (matched) { #if PRINT_ENABLED - fprintf(stderr,"MSTP: sending packet to FSM.\n"); + fprintf(stderr, "MSTP: sending packet to FSM.\n"); #endif /* convert the PDU into the MSTP Frame */ - pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], - mstp_port->OutputBufferSize, packet.frame_type, - destination, mstp_port->This_Station, &packet.pdu[0], - packet.pdu_len); + pdu_len = + MSTP_Create_Frame(&mstp_port->OutputBuffer[0], + mstp_port->OutputBufferSize, packet.frame_type, destination, + mstp_port->This_Station, &packet.pdu[0], packet.pdu_len); /* not used here, but setting it anyway */ packet.ready = false; } else { /* put it back into the queue */ - (void)mq_send(NPDU_Transmit_Queue, (char *)&packet, sizeof(packet), 1); + (void) mq_send(NPDU_Transmit_Queue, (char *) &packet, sizeof(packet), + 1); } return pdu_len; @@ -687,27 +682,23 @@ bool dlmstp_init( char mqname[32]; struct mq_attr mqattr; - mqattr.mq_flags = 0; - mqattr.mq_maxmsg = 5; + mqattr.mq_flags = 0; + mqattr.mq_maxmsg = 5; mqattr.mq_msgsize = sizeof(struct dlmstp_packet); /* create a queue for the NDPU data between MS/TP threads */ snprintf(mqname, sizeof(mqname), "/MSTP_Rx_%d", getpid()); - NPDU_Transmit_Queue = mq_open(mqname, - O_RDWR | O_CREAT, 0600, &mqattr); + NPDU_Transmit_Queue = mq_open(mqname, O_RDWR | O_CREAT, 0600, &mqattr); if (NPDU_Transmit_Queue == -1) { #if PRINT_ENABLED - fprintf(stderr, "MS/TP: Create NPDU Transmit Queue %s: %s\n", - mqname, + fprintf(stderr, "MS/TP: Create NPDU Transmit Queue %s: %s\n", mqname, strerror(errno)); #endif } snprintf(mqname, sizeof(mqname), "/MSTP_Tx_%d", getpid()); - NPDU_Receive_Queue = mq_open(mqname, - O_RDWR | O_CREAT, 0600, &mqattr); + NPDU_Receive_Queue = mq_open(mqname, O_RDWR | O_CREAT, 0600, &mqattr); if (NPDU_Receive_Queue == -1) { #if PRINT_ENABLED - fprintf(stderr, "MS/TP: Create NPDU Receive Queue %s: %s\n", - mqname, + fprintf(stderr, "MS/TP: Create NPDU Receive Queue %s: %s\n", mqname, strerror(errno)); #endif } diff --git a/bacnet-stack/ports/linux/rs485.c b/bacnet-stack/ports/linux/rs485.c index 22c32978..8237e16f 100644 --- a/bacnet-stack/ports/linux/rs485.c +++ b/bacnet-stack/ports/linux/rs485.c @@ -91,7 +91,8 @@ void RS485_Set_Interface( * ALGORITHM: none * NOTES: none *********************************************************************/ -const char *RS485_Interface(void) +const char *RS485_Interface( + void) { return RS485_Port_Name; } diff --git a/bacnet-stack/ports/linux/rs485.h b/bacnet-stack/ports/linux/rs485.h index 8fd25050..949e1b53 100644 --- a/bacnet-stack/ports/linux/rs485.h +++ b/bacnet-stack/ports/linux/rs485.h @@ -45,7 +45,8 @@ extern "C" { void RS485_Set_Interface( char *ifname); - const char * RS485_Interface(void); + const char *RS485_Interface( + void); void RS485_Initialize( void); diff --git a/bacnet-stack/ports/linux/rx_fsm.c b/bacnet-stack/ports/linux/rx_fsm.c index 7176e055..b54b5960 100644 --- a/bacnet-stack/ports/linux/rx_fsm.c +++ b/bacnet-stack/ports/linux/rx_fsm.c @@ -45,7 +45,7 @@ #include #include #include -#include /* signal handling functions */ +#include /* signal handling functions */ /* local includes */ #include "bytes.h" @@ -146,58 +146,59 @@ int timestamp_ms( } static const char *Capture_Filename = "mstp.cap"; -static FILE *pFile = NULL; /* stream pointer */ +static FILE *pFile = NULL; /* stream pointer */ /* write packet to file in libpcap format */ -static void write_global_header(void) +static void write_global_header( + void) { 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 */ + 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(Capture_Filename, "wb"); if (pFile) { - fwrite(&magic_number,sizeof(magic_number),1,pFile); - fwrite(&version_major,sizeof(version_major),1,pFile); - fwrite(&version_minor,sizeof(version_minor),1,pFile); - fwrite(&thiszone,sizeof(thiszone),1,pFile); - fwrite(&sigfigs,sizeof(sigfigs),1,pFile); - fwrite(&snaplen,sizeof(snaplen),1,pFile); - fwrite(&network,sizeof(network),1,pFile); + fwrite(&magic_number, sizeof(magic_number), 1, pFile); + fwrite(&version_major, sizeof(version_major), 1, pFile); + fwrite(&version_minor, sizeof(version_minor), 1, pFile); + fwrite(&thiszone, sizeof(thiszone), 1, pFile); + fwrite(&sigfigs, sizeof(sigfigs), 1, pFile); + fwrite(&snaplen, sizeof(snaplen), 1, pFile); + fwrite(&network, sizeof(network), 1, pFile); } else { - fprintf(stderr,"rx_fsm: failed to open %s: %s\n", - Capture_Filename, strerror(errno)); + fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename, + strerror(errno)); } } static void write_received_packet( volatile struct mstp_port_struct_t *mstp_port) { - 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 */ - uint8_t header[8]; /* MS/TP header */ + 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 */ + uint8_t header[8]; /* MS/TP header */ struct timeval tv; if (pFile) { gettimeofday(&tv, NULL); ts_sec = tv.tv_sec; ts_usec = tv.tv_usec; - fwrite(&ts_sec,sizeof(ts_sec),1,pFile); - fwrite(&ts_usec,sizeof(ts_usec),1,pFile); + fwrite(&ts_sec, sizeof(ts_sec), 1, pFile); + fwrite(&ts_usec, sizeof(ts_usec), 1, pFile); if (mstp_port->DataLength) { incl_len = orig_len = 8 + mstp_port->DataLength + 2; } else { incl_len = orig_len = 8; } - fwrite(&incl_len,sizeof(incl_len),1,pFile); - fwrite(&orig_len,sizeof(orig_len),1,pFile); + fwrite(&incl_len, sizeof(incl_len), 1, pFile); + fwrite(&orig_len, sizeof(orig_len), 1, pFile); header[0] = 0x55; header[1] = 0xFF; header[2] = mstp_port->FrameType; @@ -206,23 +207,24 @@ static void write_received_packet( header[5] = HI_BYTE(mstp_port->DataLength); header[6] = LO_BYTE(mstp_port->DataLength); header[7] = mstp_port->HeaderCRCActual; - fwrite(header,sizeof(header),1,pFile); + fwrite(header, sizeof(header), 1, pFile); if (mstp_port->DataLength) { - fwrite(mstp_port->InputBuffer,mstp_port->DataLength,1,pFile); - fwrite(&(mstp_port->DataCRCActualMSB),1,1,pFile); - fwrite(&(mstp_port->DataCRCActualLSB),1,1,pFile); + fwrite(mstp_port->InputBuffer, mstp_port->DataLength, 1, pFile); + fwrite(&(mstp_port->DataCRCActualMSB), 1, 1, pFile); + fwrite(&(mstp_port->DataCRCActualLSB), 1, 1, pFile); } } else { - fprintf(stderr,"rx_fsm: failed to open %s: %s\n", - Capture_Filename, strerror(errno)); + fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename, + strerror(errno)); } } -static void cleanup(void) +static void cleanup( + void) { if (pFile) { - fflush(pFile); /* stream pointer */ - fclose(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ + fclose(pFile); /* stream pointer */ } pFile = NULL; } @@ -262,15 +264,17 @@ static void print_received_packet( } #endif -static void sig_int(int signo) +static void sig_int( + int signo) { - (void)signo; + (void) signo; cleanup(); exit(0); } -void signal_init(void) +void signal_init( + void) { signal(SIGINT, sig_int); signal(SIGHUP, sig_int); @@ -319,7 +323,7 @@ int main( rc = pthread_create(&hThread, NULL, milliseconds_task, NULL); atexit(cleanup); write_global_header(); - fflush(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ /* run forever */ for (;;) { RS485_Check_UART_Data(mstp_port); @@ -331,7 +335,7 @@ int main( print_received_packet(mstp_port); #endif write_received_packet(mstp_port); - fflush(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ } else if (mstp_port->ReceivedInvalidFrame) { mstp_port->ReceivedInvalidFrame = false; fprintf(stderr, "ReceivedInvalidFrame\n"); @@ -339,7 +343,7 @@ int main( print_received_packet(mstp_port); #endif write_received_packet(mstp_port); - fflush(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ } } diff --git a/bacnet-stack/ports/win32/net.h b/bacnet-stack/ports/win32/net.h index 372ce2a3..19e99dd0 100644 --- a/bacnet-stack/ports/win32/net.h +++ b/bacnet-stack/ports/win32/net.h @@ -49,11 +49,13 @@ typedef HANDLE sem_t; struct timespec { time_t tv_sec; /* Seconds */ - long tv_nsec; /* Nanoseconds [0 .. 999999999] */ + long tv_nsec; /* Nanoseconds [0 .. 999999999] */ }; -static inline int gettimeofday(struct timeval *tp, void *tzp) +static inline int gettimeofday( + struct timeval *tp, + void *tzp) { struct _timeb timebuffer; @@ -65,20 +67,21 @@ static inline int gettimeofday(struct timeval *tp, void *tzp) } /* FIXME: not a complete implementation of the posix function */ -static inline int sem_timedwait(sem_t *sem, +static inline int sem_timedwait( + sem_t * sem, const struct timespec *abs_timeout) { struct timeval tp; DWORD wait_status = 0; - DWORD dwMilliseconds = (abs_timeout->tv_sec * 1000) + - (abs_timeout->tv_nsec / 1000); + DWORD dwMilliseconds = + (abs_timeout->tv_sec * 1000) + (abs_timeout->tv_nsec / 1000); - gettimeofday(&tp,NULL); + gettimeofday(&tp, NULL); if (abs_timeout->tv_sec >= tp.tv_sec) { dwMilliseconds = (abs_timeout->tv_sec - tp.tv_sec) * 1000; - if (abs_timeout->tv_nsec >= (tp.tv_usec*1000)) { + if (abs_timeout->tv_nsec >= (tp.tv_usec * 1000)) { dwMilliseconds += - ((abs_timeout->tv_nsec - (tp.tv_usec*1000)) / (1000*1000)); + ((abs_timeout->tv_nsec - (tp.tv_usec * 1000)) / (1000 * 1000)); } } else { dwMilliseconds = 0; @@ -91,14 +94,16 @@ static inline int sem_timedwait(sem_t *sem, return -1; } -static inline int sem_init(sem_t *sem, int pshared, unsigned int value) +static inline int sem_init( + sem_t * sem, + int pshared, + unsigned int value) { - (void)pshared; - *sem = CreateSemaphore( - NULL/*lpSecurityDescriptor*/, - value /* lInitialCount */, - 1 /* lMaximumCount */, - NULL /* lpName */); + (void) pshared; + *sem = CreateSemaphore(NULL /*lpSecurityDescriptor */ , + value /* lInitialCount */ , + 1 /* lMaximumCount */ , + NULL /* lpName */ ); if ((*sem) == NULL) { return -1; } @@ -106,13 +111,14 @@ static inline int sem_init(sem_t *sem, int pshared, unsigned int value) return 0; } -static inline int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) +static inline int nanosleep( + const struct timespec *rqtp, + struct timespec *rmtp) { - DWORD dwMilliseconds = (rqtp->tv_sec * 1000) + - (rqtp->tv_nsec / 1000); + DWORD dwMilliseconds = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000); Sleep(dwMilliseconds); - + return 0; } diff --git a/bacnet-stack/ports/win32/rs485.c b/bacnet-stack/ports/win32/rs485.c index ee068b72..32df287d 100644 --- a/bacnet-stack/ports/win32/rs485.c +++ b/bacnet-stack/ports/win32/rs485.c @@ -97,7 +97,8 @@ void RS485_Set_Interface( } } -const char *RS485_Interface(void) +const char *RS485_Interface( + void) { return RS485_Port_Name; } diff --git a/bacnet-stack/ports/win32/rs485.h b/bacnet-stack/ports/win32/rs485.h index fdff55bc..c4f161ec 100644 --- a/bacnet-stack/ports/win32/rs485.h +++ b/bacnet-stack/ports/win32/rs485.h @@ -45,7 +45,8 @@ extern "C" { void RS485_Set_Interface( char *ifname); - const char * RS485_Interface(void); + const char *RS485_Interface( + void); void RS485_Initialize( void); diff --git a/bacnet-stack/ports/win32/rx_fsm.c b/bacnet-stack/ports/win32/rx_fsm.c index ab423347..52f2593b 100644 --- a/bacnet-stack/ports/win32/rx_fsm.c +++ b/bacnet-stack/ports/win32/rx_fsm.c @@ -55,20 +55,20 @@ /* file format for libpcap/winpcap */ /* from http://wiki.wireshark.org/Development/LibpcapFileFormat */ typedef struct pcap_hdr_s { - uint32_t magic_number; /* magic number */ - uint16_t version_major; /* major version number */ - uint16_t version_minor; /* minor version number */ - int32_t thiszone; /* GMT to local correction */ - uint32_t sigfigs; /* accuracy of timestamps */ - uint32_t snaplen; /* max length of captured packets, in octets */ - uint32_t network; /* data link type */ + uint32_t magic_number; /* magic number */ + uint16_t version_major; /* major version number */ + uint16_t version_minor; /* minor version number */ + int32_t thiszone; /* GMT to local correction */ + uint32_t sigfigs; /* accuracy of timestamps */ + uint32_t snaplen; /* max length of captured packets, in octets */ + uint32_t network; /* data link type */ } pcap_hdr_t; typedef struct pcaprec_hdr_s { - 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 */ } pcaprec_hdr_t; /* local port data - shared with RS-485 */ @@ -169,9 +169,9 @@ static void print_received_packet( /* returns a delta timestamp */ void timestamp( - uint32_t *ts_sec, /* timestamp seconds since epoch (Unix) */ - uint32_t *ts_usec) /* timestamp microseconds (unix) */ -{ + uint32_t * ts_sec, /* timestamp seconds since epoch (Unix) */ + uint32_t * ts_usec) +{ /* timestamp microseconds (unix) */ DWORD ticks = 0; static DWORD initial_ticks = 0; static time_t initial_seconds = 0; @@ -185,7 +185,7 @@ void timestamp( } ticks = GetTickCount(); /* how much total time has passed? */ - elapsed_ticks = ticks - initial_ticks; + elapsed_ticks = ticks - initial_ticks; seconds = elapsed_ticks / 1000; milliseconds = elapsed_ticks - (seconds * 1000); *ts_sec = initial_seconds + seconds; @@ -195,55 +195,56 @@ void timestamp( } static const char *Capture_Filename = "mstp.cap"; -static FILE *pFile = NULL; /* stream pointer */ +static FILE *pFile = NULL; /* stream pointer */ /* write packet to file in libpcap format */ -static void write_global_header(void) +static void write_global_header( + void) { 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 */ + 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 */ /* create a new file. */ pFile = fopen(Capture_Filename, "wb"); if (pFile) { - fwrite(&magic_number,sizeof(magic_number),1,pFile); - fwrite(&version_major,sizeof(version_major),1,pFile); - fwrite(&version_minor,sizeof(version_minor),1,pFile); - fwrite(&thiszone,sizeof(thiszone),1,pFile); - fwrite(&sigfigs,sizeof(sigfigs),1,pFile); - fwrite(&snaplen,sizeof(snaplen),1,pFile); - fwrite(&network,sizeof(network),1,pFile); + fwrite(&magic_number, sizeof(magic_number), 1, pFile); + fwrite(&version_major, sizeof(version_major), 1, pFile); + fwrite(&version_minor, sizeof(version_minor), 1, pFile); + fwrite(&thiszone, sizeof(thiszone), 1, pFile); + fwrite(&sigfigs, sizeof(sigfigs), 1, pFile); + fwrite(&snaplen, sizeof(snaplen), 1, pFile); + fwrite(&network, sizeof(network), 1, pFile); } else { - fprintf(stderr,"rx_fsm: failed to open %s: %s\n", - Capture_Filename, strerror(errno)); + fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename, + strerror(errno)); } } static void write_received_packet( volatile struct mstp_port_struct_t *mstp_port) { - 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 */ - uint8_t header[8]; /* MS/TP header */ + 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 */ + uint8_t header[8]; /* MS/TP header */ if (pFile) { timestamp(&ts_sec, &ts_usec); - fwrite(&ts_sec,sizeof(ts_sec),1,pFile); - fwrite(&ts_usec,sizeof(ts_usec),1,pFile); + fwrite(&ts_sec, sizeof(ts_sec), 1, pFile); + fwrite(&ts_usec, sizeof(ts_usec), 1, pFile); if (mstp_port->DataLength) { incl_len = orig_len = 8 + mstp_port->DataLength + 2; } else { incl_len = orig_len = 8; } - fwrite(&incl_len,sizeof(incl_len),1,pFile); - fwrite(&orig_len,sizeof(orig_len),1,pFile); + fwrite(&incl_len, sizeof(incl_len), 1, pFile); + fwrite(&orig_len, sizeof(orig_len), 1, pFile); header[0] = 0x55; header[1] = 0xFF; header[2] = mstp_port->FrameType; @@ -252,25 +253,26 @@ static void write_received_packet( header[5] = HI_BYTE(mstp_port->DataLength); header[6] = LO_BYTE(mstp_port->DataLength); header[7] = mstp_port->HeaderCRCActual; - fwrite(header,sizeof(header),1,pFile); + fwrite(header, sizeof(header), 1, pFile); if (mstp_port->DataLength) { - fwrite(mstp_port->InputBuffer,mstp_port->DataLength,1,pFile); - fwrite(&(mstp_port->DataCRCActualMSB),1,1,pFile); - fwrite(&(mstp_port->DataCRCActualLSB),1,1,pFile); + fwrite(mstp_port->InputBuffer, mstp_port->DataLength, 1, pFile); + fwrite(&(mstp_port->DataCRCActualMSB), 1, 1, pFile); + fwrite(&(mstp_port->DataCRCActualLSB), 1, 1, pFile); } } else { - fprintf(stderr,"rx_fsm: failed to open %s: %s\n", - Capture_Filename, strerror(errno)); + fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename, + strerror(errno)); } } static char *Network_Interface = NULL; -static void cleanup(void) +static void cleanup( + void) { if (pFile) { - fflush(pFile); /* stream pointer */ - fclose(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ + fclose(pFile); /* stream pointer */ } pFile = NULL; } diff --git a/bacnet-stack/src/bactext.c b/bacnet-stack/src/bactext.c index b4a7a32e..821359a0 100644 --- a/bacnet-stack/src/bactext.c +++ b/bacnet-stack/src/bactext.c @@ -663,7 +663,7 @@ INDTEXT_DATA bacnet_property_names[] = { {PROP_TRIGGER, "trigger"} , {PROP_UTC_TIME_SYNCHRONIZATION_RECIPIENTS, - "utc-time-synchronization-recipients"} + "utc-time-synchronization-recipients"} , {PROP_NODE_SUBTYPE, "node-subtype"} , @@ -748,7 +748,7 @@ INDTEXT_DATA bacnet_property_names[] = { {PROP_ACCESS_EVENT, "access-event"} , {PROP_ACCESS_EVENT_AUTHENTICATION_FACTOR, - "access-event-authentication-factor"} + "access-event-authentication-factor"} , {PROP_ACCESS_EVENT_CREDENTIAL, "access-event-credential"} , @@ -835,7 +835,7 @@ INDTEXT_DATA bacnet_property_names[] = { {PROP_MASTER_POINT, "muster-point"} , {PROP_NUMBER_OF_AUTHENTICATION_POLICIES, - "number-of-authentication-policies"} + "number-of-authentication-policies"} , {PROP_OCCUPANCY_COUNT, "occupancy-count"} , @@ -846,7 +846,7 @@ INDTEXT_DATA bacnet_property_names[] = { {PROP_OCCUPANCY_LOWER_THRESHOLD, "occupancy-lower-threshold"} , {PROP_OCCUPANCY_LOWER_THRESHOLD_ENFORCED, - "occupancy-lower-threshold-enforced"} + "occupancy-lower-threshold-enforced"} , {PROP_OCCUPANCY_STATE, "occupancy-state"} , @@ -1522,13 +1522,13 @@ INDTEXT_DATA bacnet_error_code_names[] = { {ERROR_CODE_ABORT_BUFFER_OVERFLOW, "abort-buffer-overflow"} , {ERROR_CODE_ABORT_INVALID_APDU_IN_THIS_STATE, - "abort-invalid-apdu-in-this-state"} + "abort-invalid-apdu-in-this-state"} , {ERROR_CODE_ABORT_PREEMPTED_BY_HIGHER_PRIORITY_TASK, - "abort-preempted-by-higher-priority-task"} + "abort-preempted-by-higher-priority-task"} , {ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED, - "abort-segmentation-not-supported"} + "abort-segmentation-not-supported"} , {ERROR_CODE_ABORT_PROPRIETARY, "abort-proprietary"} , @@ -1541,15 +1541,15 @@ INDTEXT_DATA bacnet_error_code_names[] = { {ERROR_CODE_REJECT_BUFFER_OVERFLOW, "reject-buffer-overflow"} , {ERROR_CODE_REJECT_INCONSISTENT_PARAMETERS, - "reject-inconsistent-parameters"} + "reject-inconsistent-parameters"} , {ERROR_CODE_REJECT_INVALID_PARAMETER_DATA_TYPE, - "reject-invalid-parameter-data-type"} + "reject-invalid-parameter-data-type"} , {ERROR_CODE_REJECT_INVALID_TAG, "reject-invalid-tag"} , {ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER, - "reject-missing-required-parameter"} + "reject-missing-required-parameter"} , {ERROR_CODE_REJECT_PARAMETER_OUT_OF_RANGE, "reject-parameter-out-of-range"} , @@ -1580,7 +1580,7 @@ INDTEXT_DATA bacnet_error_code_names[] = { {ERROR_CODE_NO_PROPERTY_SPECIFIED, "no-property-specified"} , {ERROR_CODE_NOT_CONFIGURED_FOR_TRIGGERED_LOGGING, - "not-configured-for-triggered-logging"} + "not-configured-for-triggered-logging"} , {ERROR_CODE_UNKNOWN_SUBSCRIPTION, "unknown-subscription"} , diff --git a/bacnet-stack/src/dlmstp.c b/bacnet-stack/src/dlmstp.c index 5b95f4fa..e5833d1a 100644 --- a/bacnet-stack/src/dlmstp.c +++ b/bacnet-stack/src/dlmstp.c @@ -135,18 +135,18 @@ uint16_t dlmstp_receive( (void) max_pdu; /* see if there is a packet available, and a place to put the reply (if necessary) and process it */ - gettimeofday(&tp,NULL); + gettimeofday(&tp, NULL); if (timeout < 1000) { tp.tv_nsec += timeout * 1000; - while (tp.tv_nsec > (1000*1000)) { + while (tp.tv_nsec > (1000 * 1000)) { tp.tv_sec++; - tp.tv_nsec-= (1000*1000); + tp.tv_nsec -= (1000 * 1000); } } else { tp.tv_sec += timeout / 1000; tp.tv_nsec += timeout - (tp.tv_sec * 1000); } - if (!sem_timedwait(&Receive_Packet_Flag,tp)) { + if (!sem_timedwait(&Receive_Packet_Flag, tp)) { if (Receive_Packet.ready) { if (Receive_Packet.pdu_len) { MSTP_Packets++; @@ -621,8 +621,7 @@ bool dlmstp_init( rv = sem_init(&Receive_Packet_Flag, 0, 0); Receive_Packet_Flag = CreateSemaphore(NULL, 0, 1, "dlmstpReceivePacket"); if (rv) { - fprintf(stderr, - "MS/TP Interface: %s\n cannot allocate semaphore.", + fprintf(stderr, "MS/TP Interface: %s\n cannot allocate semaphore.", ifname); exit(1); } diff --git a/bacnet-stack/src/mstp.c b/bacnet-stack/src/mstp.c index 64c03b04..a2437d42 100644 --- a/bacnet-stack/src/mstp.c +++ b/bacnet-stack/src/mstp.c @@ -254,8 +254,8 @@ void MSTP_Receive_Frame_FSM( volatile struct mstp_port_struct_t *mstp_port) { static MSTP_RECEIVE_STATE receive_state = MSTP_RECEIVE_STATE_IDLE; - printf_receive( - "MSTP Rx: State=%s Data=%02X hCRC=%02X Index=%u EC=%u DateLen=%u Silence=%u\n", + printf_receive + ("MSTP Rx: State=%s Data=%02X hCRC=%02X Index=%u EC=%u DateLen=%u Silence=%u\n", mstptext_receive_state(mstp_port->receive_state), mstp_port->DataRegister, mstp_port->HeaderCRC, mstp_port->Index, mstp_port->EventCount, mstp_port->DataLength, @@ -269,21 +269,22 @@ void MSTP_Receive_Frame_FSM( mstp_port->SilenceTimerReset(); INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); /* wait for the start of a frame. */ + } else if (mstp_port->DataAvailable == true) { + printf_receive_data("MSTP Rx: %02X ", mstp_port->DataRegister); + /* Preamble1 */ + if (mstp_port->DataRegister == 0x55) { + /* receive the remainder of the frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; + } + /* EatAnOctet */ + else { + printf_receive_data("\n"); + /* wait for the start of a frame. */ + } + mstp_port->DataAvailable = false; + mstp_port->SilenceTimerReset(); + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); } - else - if (mstp_port->DataAvailable == true) { - printf_receive_data("MSTP Rx: %02X ", mstp_port->DataRegister); - /* Preamble1 */ - if (mstp_port->DataRegister == 0x55) { - /* receive the remainder of the frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE;} - /* EatAnOctet */ - else { - printf_receive_data("\n"); - /* wait for the start of a frame. */ - } - mstp_port->DataAvailable = false; mstp_port->SilenceTimerReset(); - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);} break; /* In the PREAMBLE state, the node waits for the second octet of the preamble. */ case MSTP_RECEIVE_STATE_PREAMBLE: @@ -291,35 +292,39 @@ void MSTP_Receive_Frame_FSM( if (mstp_port->SilenceTimer() > Tframe_abort) { /* a correct preamble has not been received */ /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} - /* Error */ - else - if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; mstp_port->SilenceTimerReset(); - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} - else - if (mstp_port->DataAvailable == true) { - printf_receive_data("%02X ", mstp_port->DataRegister); - /* Preamble2 */ - if (mstp_port->DataRegister == 0xFF) { - mstp_port->Index = 0; mstp_port->HeaderCRC = 0xFF; - /* receive the remainder of the frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER;} - /* ignore RepeatedPreamble1 */ - else - if (mstp_port->DataRegister == 0x55) { - /* wait for the second preamble octet. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; } - /* NotPreamble */ - else { + /* Error */ + else if (mstp_port->ReceiveError == true) { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimerReset(); + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} - mstp_port->DataAvailable = false; mstp_port->SilenceTimerReset(); - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);} + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else if (mstp_port->DataAvailable == true) { + printf_receive_data("%02X ", mstp_port->DataRegister); + /* Preamble2 */ + if (mstp_port->DataRegister == 0xFF) { + mstp_port->Index = 0; + mstp_port->HeaderCRC = 0xFF; + /* receive the remainder of the frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* ignore RepeatedPreamble1 */ + else if (mstp_port->DataRegister == 0x55) { + /* wait for the second preamble octet. */ + } + /* NotPreamble */ + else { + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + mstp_port->DataAvailable = false; + mstp_port->SilenceTimerReset(); + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + } break; - /* In the HEADER state, the node waits for the fixed message header. */ + /* In the HEADER state, the node waits for the fixed message header. */ case MSTP_RECEIVE_STATE_HEADER: /* Timeout */ if (mstp_port->SilenceTimer() > Tframe_abort) { @@ -331,146 +336,159 @@ void MSTP_Receive_Frame_FSM( mstp_port->SilenceTimer(), Tframe_abort); } /* Error */ - else - if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; mstp_port->SilenceTimerReset(); - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - printf_receive_error("MSTP: Rx Header: ReceiveError\n"); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} - else - if (mstp_port->DataAvailable == true) { - printf_receive_data("%02X ", mstp_port->DataRegister); - /* FrameType */ - if (mstp_port->Index == 0) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->FrameType = mstp_port->DataRegister; - mstp_port->Index = 1;} - /* Destination */ - else - if (mstp_port->Index == 1) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, mstp_port->HeaderCRC); - mstp_port->DestinationAddress = mstp_port->DataRegister; - mstp_port->Index = 2;} - /* Source */ - else - if (mstp_port->Index == 2) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, mstp_port->HeaderCRC); - mstp_port->SourceAddress = mstp_port->DataRegister; - mstp_port->Index = 3;} - /* Length1 */ - else - if (mstp_port->Index == 3) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, mstp_port->HeaderCRC); - mstp_port->DataLength = mstp_port->DataRegister * 256; - mstp_port->Index = 4;} - /* Length2 */ - else - if (mstp_port->Index == 4) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, mstp_port->HeaderCRC); - mstp_port->DataLength += mstp_port->DataRegister; - mstp_port->Index = 5;} - /* HeaderCRC */ - else - if (mstp_port->Index == 5) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, mstp_port->HeaderCRC); - mstp_port->HeaderCRCActual = mstp_port->DataRegister; - /* don't wait for next state - do it here */ - /* MSTP_RECEIVE_STATE_HEADER_CRC */ - if (mstp_port->HeaderCRC != 0x55) { - /* BadCRC */ - /* indicate that an error has occurred during - the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - printf_receive_error("MSTP: Rx Header: BadCRC [%02X]\n", - mstp_port->DataRegister); - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} - else { - /* Note: proposed change to BACnet MSTP state machine! - If we don't decode data that is not for us, we could - get confused about the start if the Preamble 55 FF - is part of the data. */ - /* Data */ - if ((mstp_port->DataLength) && - (mstp_port->DataLength <= mstp_port->InputBufferSize)) { - /* Data - decode anyway to keep from false */ - mstp_port->Index = 0; mstp_port->DataCRC = 0xFFFF; - /* receive the data portion of the frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA;} - else { - /* FrameTooLong */ - if (mstp_port->DataLength) { - printf_receive_error("MSTP: Rx Header: FrameTooLong %d\n", - mstp_port->DataLength); - /* indicate that a frame with an illegal or */ - /* unacceptable data length has been received */ - mstp_port->ReceivedInvalidFrame = true;} - /* NoData */ - else - if (mstp_port->DataLength == 0) { - printf_receive_data("%s", - mstptext_frame_type(mstp_port->FrameType)); - if ((mstp_port->DestinationAddress == - mstp_port->This_Station) - || (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS) - || (mstp_port->Lurking)) { - /* ForUs */ - /* indicate that a frame with no data has been received */ - mstp_port->ReceivedValidFrame = true;} - else { - /* NotForUs - drop */ - } - } - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} - } - } - /* not per MS/TP standard, but it is a case not covered */ - else { + else if (mstp_port->ReceiveError == true) { mstp_port->ReceiveError = false; - /* indicate that an error has occurred during */ - /* the reception of a frame */ + mstp_port->SilenceTimerReset(); + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* indicate that an error has occurred during the reception of a frame */ mstp_port->ReceivedInvalidFrame = true; - printf_receive_error("MSTP: Rx Data: BadIndex %d\n", - mstp_port->Index); + printf_receive_error("MSTP: Rx Header: ReceiveError\n"); /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else if (mstp_port->DataAvailable == true) { + printf_receive_data("%02X ", mstp_port->DataRegister); + /* FrameType */ + if (mstp_port->Index == 0) { + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->FrameType = mstp_port->DataRegister; + mstp_port->Index = 1; + } + /* Destination */ + else if (mstp_port->Index == 1) { + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DestinationAddress = mstp_port->DataRegister; + mstp_port->Index = 2; + } + /* Source */ + else if (mstp_port->Index == 2) { + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->SourceAddress = mstp_port->DataRegister; + mstp_port->Index = 3; + } + /* Length1 */ + else if (mstp_port->Index == 3) { + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DataLength = mstp_port->DataRegister * 256; + mstp_port->Index = 4; + } + /* Length2 */ + else if (mstp_port->Index == 4) { + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DataLength += mstp_port->DataRegister; + mstp_port->Index = 5; + } + /* HeaderCRC */ + else if (mstp_port->Index == 5) { + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->HeaderCRCActual = mstp_port->DataRegister; + /* don't wait for next state - do it here */ + /* MSTP_RECEIVE_STATE_HEADER_CRC */ + if (mstp_port->HeaderCRC != 0x55) { + /* BadCRC */ + /* indicate that an error has occurred during + the reception of a frame */ + mstp_port->ReceivedInvalidFrame = true; + printf_receive_error + ("MSTP: Rx Header: BadCRC [%02X]\n", + mstp_port->DataRegister); + /* wait for the start of the next frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else { + /* Note: proposed change to BACnet MSTP state machine! + If we don't decode data that is not for us, we could + get confused about the start if the Preamble 55 FF + is part of the data. */ + /* Data */ + if ((mstp_port->DataLength) && + (mstp_port->DataLength <= + mstp_port->InputBufferSize)) { + /* Data - decode anyway to keep from false */ + mstp_port->Index = 0; + mstp_port->DataCRC = 0xFFFF; + /* receive the data portion of the frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; + } else { + /* FrameTooLong */ + if (mstp_port->DataLength) { + printf_receive_error + ("MSTP: Rx Header: FrameTooLong %d\n", + mstp_port->DataLength); + /* indicate that a frame with an illegal or */ + /* unacceptable data length has been received */ + mstp_port->ReceivedInvalidFrame = true; + } + /* NoData */ + else if (mstp_port->DataLength == 0) { + printf_receive_data("%s", + mstptext_frame_type(mstp_port->FrameType)); + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) + || (mstp_port->DestinationAddress == + MSTP_BROADCAST_ADDRESS) + || (mstp_port->Lurking)) { + /* ForUs */ + /* indicate that a frame with no data has been received */ + mstp_port->ReceivedValidFrame = true; + } else { + /* NotForUs - drop */ + } + } + /* wait for the start of the next frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + } + } + /* not per MS/TP standard, but it is a case not covered */ + else { + mstp_port->ReceiveError = false; + /* indicate that an error has occurred during */ + /* the reception of a frame */ + mstp_port->ReceivedInvalidFrame = true; + printf_receive_error("MSTP: Rx Data: BadIndex %d\n", + mstp_port->Index); + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } mstp_port->SilenceTimerReset(); INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); mstp_port->DataAvailable = false; } break; - /* In the HEADER_CRC state, the node validates the CRC on the fixed */ - /* message header. */ + /* In the HEADER_CRC state, the node validates the CRC on the fixed */ + /* message header. */ case MSTP_RECEIVE_STATE_HEADER_CRC: /* note: we should never get to this state since we shortcut it earlier in the state machine, and never set this state */ mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; break; - /* In the DATA state, the node waits for the data portion of a frame. */ + /* In the DATA state, the node waits for the data portion of a frame. */ case MSTP_RECEIVE_STATE_DATA: /* Timeout */ if (mstp_port->SilenceTimer() > Tframe_abort) { /* indicate that an error has occurred during the reception of a frame */ mstp_port->ReceivedInvalidFrame = true; - printf_receive_error("MSTP: Rx Data: SilenceTimer %dms > %dms\n", + printf_receive_error + ("MSTP: Rx Data: SilenceTimer %dms > %dms\n", mstp_port->SilenceTimer(), Tframe_abort); - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} + /* wait for the start of the next frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } /* Error */ else if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; mstp_port->SilenceTimerReset(); + mstp_port->ReceiveError = false; + mstp_port->SilenceTimerReset(); /* indicate that an error has occurred during the reception of a frame */ mstp_port->ReceivedInvalidFrame = true; printf_receive_error("MSTP: Rx Data: ReceiveError\n"); @@ -481,27 +499,33 @@ void MSTP_Receive_Frame_FSM( if (mstp_port->Index < mstp_port->DataLength) { /* DataOctet */ mstp_port->DataCRC = - CRC_Calc_Data(mstp_port->DataRegister, mstp_port->DataCRC); + CRC_Calc_Data(mstp_port->DataRegister, + mstp_port->DataCRC); mstp_port->InputBuffer[mstp_port->Index] = - mstp_port->DataRegister; mstp_port->Index++; + mstp_port->DataRegister; + mstp_port->Index++; mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; } else if (mstp_port->Index == mstp_port->DataLength) { /* CRC1 */ mstp_port->DataCRC = - CRC_Calc_Data(mstp_port->DataRegister, mstp_port->DataCRC); + CRC_Calc_Data(mstp_port->DataRegister, + mstp_port->DataCRC); mstp_port->DataCRCActualMSB = mstp_port->DataRegister; mstp_port->Index++; mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; } else if (mstp_port->Index == (mstp_port->DataLength + 1)) { /* CRC2 */ mstp_port->DataCRC = - CRC_Calc_Data(mstp_port->DataRegister, mstp_port->DataCRC); + CRC_Calc_Data(mstp_port->DataRegister, + mstp_port->DataCRC); mstp_port->DataCRCActualLSB = mstp_port->DataRegister; - printf_receive_data("%s", mstptext_frame_type(mstp_port->FrameType)); + printf_receive_data("%s", + mstptext_frame_type(mstp_port->FrameType)); /* STATE DATA CRC - no need for new state */ /* indicate the complete reception of a valid frame */ if (mstp_port->DataCRC == 0xF0B8) { - if ((mstp_port->DestinationAddress == mstp_port->This_Station) + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) || (mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS) || (mstp_port->Lurking)) { @@ -537,7 +561,9 @@ void MSTP_Receive_Frame_FSM( } /* returns true if we need to transition immediately */ -bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { +bool MSTP_Master_Node_FSM( + volatile struct mstp_port_struct_t * mstp_port) +{ unsigned length = 0; uint8_t next_poll_station = 0; uint8_t next_this_station = 0; @@ -549,20 +575,19 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { /* some calculations that several states need */ next_poll_station = - (mstp_port->Poll_Station + 1) % (mstp_port->Nmax_master + 1); + (mstp_port->Poll_Station + 1) % (mstp_port->Nmax_master + 1); next_this_station = - (mstp_port->This_Station + 1) % (mstp_port->Nmax_master + 1); + (mstp_port->This_Station + 1) % (mstp_port->Nmax_master + 1); next_next_station = - (mstp_port->Next_Station + 1) % (mstp_port->Nmax_master + 1); + (mstp_port->Next_Station + 1) % (mstp_port->Nmax_master + 1); if (mstp_port->master_state != master_state) { master_state = mstp_port->master_state; - printf_master( - "MSTP: TS=%02X[%02X] NS=%02X[%02X] PS=%02X[%02X] EC=%u TC=%u ST=%u %s\n", + printf_master + ("MSTP: TS=%02X[%02X] NS=%02X[%02X] PS=%02X[%02X] EC=%u TC=%u ST=%u %s\n", mstp_port->This_Station, next_this_station, mstp_port->Next_Station, next_next_station, - mstp_port->Poll_Station, next_poll_station, - mstp_port->EventCount, mstp_port->TokenCount, - mstp_port->SilenceTimer(), + mstp_port->Poll_Station, next_poll_station, mstp_port->EventCount, + mstp_port->TokenCount, mstp_port->SilenceTimer(), mstptext_master_state(mstp_port->master_state)); } switch (mstp_port->master_state) { @@ -573,7 +598,8 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { mstp_port->Poll_Station = mstp_port->This_Station; /* cause a Poll For Master to be sent when this node first */ /* receives the token */ - mstp_port->TokenCount = Npoll; mstp_port->SoleMaster = false; + mstp_port->TokenCount = Npoll; + mstp_port->SoleMaster = false; mstp_port->master_state = MSTP_MASTER_STATE_IDLE; transition_now = true; break; @@ -591,18 +617,16 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { mstp_port->ReceivedInvalidFrame = false; } else if (mstp_port->ReceivedValidFrame == true) { /* wait for the next frame - remain in IDLE */ - printf_master( - "MSTP: ReceivedValidFrame " - "Src=%02X Dest=%02X DataLen=%u " - "FC=%u ST=%u Type=%s\n", - mstp_port->SourceAddress, - mstp_port->DestinationAddress, + printf_master("MSTP: ReceivedValidFrame " + "Src=%02X Dest=%02X DataLen=%u " "FC=%u ST=%u Type=%s\n", + mstp_port->SourceAddress, mstp_port->DestinationAddress, mstp_port->DataLength, mstp_port->FrameCount, mstp_port->SilenceTimer(), mstptext_frame_type(mstp_port->FrameType)); /* destined for me! */ - if ((mstp_port->DestinationAddress == mstp_port->This_Station) || - (mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS)) { + if ((mstp_port->DestinationAddress == mstp_port->This_Station) + || (mstp_port->DestinationAddress == + MSTP_BROADCAST_ADDRESS)) { switch (mstp_port->FrameType) { case FRAME_TYPE_TOKEN: /* ReceivedToken */ @@ -614,18 +638,17 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { mstp_port->ReceivedValidFrame = false; mstp_port->FrameCount = 0; mstp_port->SoleMaster = false; - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; + mstp_port->master_state = + MSTP_MASTER_STATE_USE_TOKEN; transition_now = true; break; case FRAME_TYPE_POLL_FOR_MASTER: /* ReceivedPFM */ - MSTP_Create_And_Send_Frame( - mstp_port, + MSTP_Create_And_Send_Frame(mstp_port, FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, mstp_port->SourceAddress, - mstp_port->This_Station, - NULL, 0); - break; + mstp_port->This_Station, NULL, 0); + break; case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: /* indicate successful reception to the higher layers */ MSTP_Put_Receive(mstp_port); @@ -642,13 +665,11 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { } break; case FRAME_TYPE_TEST_REQUEST: - MSTP_Create_And_Send_Frame( - mstp_port, + MSTP_Create_And_Send_Frame(mstp_port, FRAME_TYPE_TEST_RESPONSE, mstp_port->SourceAddress, mstp_port->This_Station, - mstp_port->InputBuffer, - mstp_port->DataLength); + mstp_port->InputBuffer, mstp_port->DataLength); break; case FRAME_TYPE_TEST_RESPONSE: default: @@ -656,7 +677,7 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { } } /* For DATA_EXPECTING_REPLY, we will keep the Rx Frame for - reference, and the flag will be cleared in the next state */ + reference, and the flag will be cleared in the next state */ if (mstp_port->master_state != MSTP_MASTER_STATE_ANSWER_DATA_REQUEST) { mstp_port->ReceivedValidFrame = false; @@ -672,8 +693,7 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { if (length < 1) { /* NothingToSend */ mstp_port->FrameCount = mstp_port->Nmax_info_frames; - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; transition_now = true; } else { uint8_t frame_type = mstp_port->OutputBuffer[2]; @@ -725,10 +745,11 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { /* error in frame reception */ mstp_port->ReceivedInvalidFrame = false; mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; + MSTP_MASTER_STATE_DONE_WITH_TOKEN; transition_now = true; } else if (mstp_port->ReceivedValidFrame == true) { - if (mstp_port->DestinationAddress == mstp_port->This_Station) { + if (mstp_port->DestinationAddress == + mstp_port->This_Station) { switch (mstp_port->FrameType) { case FRAME_TYPE_REPLY_POSTPONED: /* ReceivedReplyPostponed */ @@ -744,12 +765,14 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { /* or a proprietary type that indicates a reply */ /* indicate successful reception to the higher layers */ MSTP_Put_Receive(mstp_port); - mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; break; default: /* if proprietary frame was expected, you might - need to transition to DONE WITH TOKEN */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + need to transition to DONE WITH TOKEN */ + mstp_port->master_state = + MSTP_MASTER_STATE_IDLE; break; } } else { @@ -774,8 +797,7 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { /* before passing the token. */ mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; transition_now = true; - } - else if (mstp_port->TokenCount < (Npoll - 1)) { + } else if (mstp_port->TokenCount < (Npoll - 1)) { /* Npoll changed in Errata SSPC-135-2004 */ if ((mstp_port->SoleMaster == true) && (mstp_port->Next_Station != next_this_station)) { @@ -815,13 +837,15 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { mstp_port->TokenCount = 1; /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ /* find a new successor to TS */ - mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER; + mstp_port->master_state = + MSTP_MASTER_STATE_POLL_FOR_MASTER; } else { /* ResetMaintenancePFM */ mstp_port->Poll_Station = mstp_port->This_Station; /* transmit a Token frame to NS */ MSTP_Create_And_Send_Frame(mstp_port, FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, NULL, 0); + mstp_port->Next_Station, mstp_port->This_Station, NULL, + 0); mstp_port->RetryCount = 0; /* changed in Errata SSPC-135-2004 */ mstp_port->TokenCount = 1; @@ -870,18 +894,20 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { mstp_port->This_Station, NULL, 0); /* no known successor node */ mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; mstp_port->TokenCount = 0; + mstp_port->RetryCount = 0; + mstp_port->TokenCount = 0; /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ /* find a new successor to TS */ - mstp_port->master_state = - MSTP_MASTER_STATE_POLL_FOR_MASTER;} + mstp_port->master_state = + MSTP_MASTER_STATE_POLL_FOR_MASTER; + } } break; case MSTP_MASTER_STATE_NO_TOKEN: - /* The NO_TOKEN state is entered if mstp_port->SilenceTimer() becomes greater */ - /* than Tno_token, indicating that there has been no network activity */ - /* for that period of time. The timeout is continued to determine */ - /* whether or not this node may create a token. */ + /* The NO_TOKEN state is entered if mstp_port->SilenceTimer() becomes greater */ + /* than Tno_token, indicating that there has been no network activity */ + /* for that period of time. The timeout is continued to determine */ + /* whether or not this node may create a token. */ my_timeout = Tno_token + (Tslot * mstp_port->This_Station); if (mstp_port->SilenceTimer() < my_timeout) { if (mstp_port->EventCount > Nmin_octets) { @@ -905,7 +931,8 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { mstp_port->This_Station, NULL, 0); /* indicate that the next station is unknown */ mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; mstp_port->TokenCount = 0; + mstp_port->RetryCount = 0; + mstp_port->TokenCount = 0; /* mstp_port->EventCount = 0; removed Addendum 135-2004d-8 */ /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ mstp_port->master_state = @@ -927,10 +954,11 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { mstp_port->EventCount = 0; /* Transmit a Token frame to NS */ MSTP_Create_And_Send_Frame(mstp_port, FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, - NULL, 0); + mstp_port->Next_Station, mstp_port->This_Station, NULL, + 0); mstp_port->Poll_Station = mstp_port->This_Station; - mstp_port->TokenCount = 0; mstp_port->RetryCount = 0; + mstp_port->TokenCount = 0; + mstp_port->RetryCount = 0; mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; } else { /* ReceivedUnexpectedFrame */ @@ -971,8 +999,8 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { /* Transmit a Poll For Master frame to PS. */ MSTP_Create_And_Send_Frame(mstp_port, FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, mstp_port->This_Station, - NULL, 0); + mstp_port->Poll_Station, + mstp_port->This_Station, NULL, 0); mstp_port->RetryCount = 0; /* Re-enter the current state. */ } else { @@ -994,7 +1022,8 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { /* BACnet Data Expecting Reply, a Test_Request, or */ /* a proprietary frame that expects a reply is received. */ /* FIXME: we could wait for up to Treply_delay */ - length = MSTP_Get_Reply(mstp_port, 0); if (length > 0) { + length = MSTP_Get_Reply(mstp_port, 0); + if (length > 0) { /* Reply */ /* If a reply is available from the higher layers */ /* within Treply_delay after the reception of the */ @@ -1037,23 +1066,34 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { /* note: InputBuffer and InputBufferSize assumed to be set */ /* note: OutputBuffer and OutputBufferSize assumed to be set */ /* note: SilenceTimer and SilenceTimerReset assumed to be set */ -void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port) { +void MSTP_Init( + volatile struct mstp_port_struct_t *mstp_port) +{ if (mstp_port) { mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; mstp_port->master_state = MSTP_MASTER_STATE_INITIALIZE; - mstp_port->ReceiveError = false; mstp_port->DataAvailable = false; - mstp_port->DataRegister = 0; mstp_port->DataCRC = 0; - mstp_port->DataCRC = 0; mstp_port->DataLength = 0; - mstp_port->DestinationAddress = 0; mstp_port->EventCount = 0; - mstp_port->FrameType = FRAME_TYPE_TOKEN; mstp_port->FrameCount = 0; - mstp_port->HeaderCRC = 0; mstp_port->Index = 0; + mstp_port->ReceiveError = false; + mstp_port->DataAvailable = false; + mstp_port->DataRegister = 0; + mstp_port->DataCRC = 0; + mstp_port->DataCRC = 0; + mstp_port->DataLength = 0; + mstp_port->DestinationAddress = 0; + mstp_port->EventCount = 0; + mstp_port->FrameType = FRAME_TYPE_TOKEN; + mstp_port->FrameCount = 0; + mstp_port->HeaderCRC = 0; + mstp_port->Index = 0; mstp_port->Index = 0; mstp_port->Next_Station = mstp_port->This_Station; mstp_port->Poll_Station = mstp_port->This_Station; mstp_port->ReceivedInvalidFrame = false; - mstp_port->ReceivedValidFrame = false; mstp_port->RetryCount = 0; - mstp_port->SilenceTimerReset(); mstp_port->SoleMaster = false; - mstp_port->SourceAddress = 0; mstp_port->TokenCount = 0; + mstp_port->ReceivedValidFrame = false; + mstp_port->RetryCount = 0; + mstp_port->SilenceTimerReset(); + mstp_port->SoleMaster = false; + mstp_port->SourceAddress = 0; + mstp_port->TokenCount = 0; mstp_port->Lurking = false; #if 0 /* FIXME: you must point these buffers to actual byte buckets @@ -1078,393 +1118,491 @@ void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port) { #include "ringbuf.h" #include "ctest.h" - static uint8_t RxBuffer[MAX_MPDU]; static uint8_t TxBuffer[MAX_MPDU]; +static uint8_t RxBuffer[MAX_MPDU]; +static uint8_t TxBuffer[MAX_MPDU]; /* test stub functions */ - void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ - uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ - uint16_t nbytes) { /* number of bytes of data (up to 501) */ - (void) mstp_port; (void) buffer; (void) nbytes;} +void RS485_Send_Frame( + volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ + uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ + uint16_t nbytes) +{ /* number of bytes of data (up to 501) */ + (void) mstp_port; + (void) buffer; + (void) nbytes; +} #define RING_BUFFER_DATA_SIZE 1 #define RING_BUFFER_SIZE MAX_MPDU - static RING_BUFFER Test_Buffer; - static uint8_t Test_Buffer_Data[RING_BUFFER_DATA_SIZE * RING_BUFFER_SIZE]; - static void Load_Input_Buffer(uint8_t * buffer, - size_t len) { - static bool initialized = false; /* tracks our init */ - if (!initialized) { +static RING_BUFFER Test_Buffer; +static uint8_t Test_Buffer_Data[RING_BUFFER_DATA_SIZE * RING_BUFFER_SIZE]; +static void Load_Input_Buffer( + uint8_t * buffer, + size_t len) +{ + static bool initialized = false; /* tracks our init */ + if (!initialized) { initialized = true; - Ringbuf_Init(&Test_Buffer, (char *) Test_Buffer_Data, - RING_BUFFER_DATA_SIZE, RING_BUFFER_SIZE);} - /* empty any the existing data */ - while (!Ringbuf_Empty(&Test_Buffer)) { - (void) Ringbuf_Pop_Front(&Test_Buffer);} - - if (buffer) { - while (len) { - (void) Ringbuf_Put(&Test_Buffer, (char *) buffer); len--; - buffer++;} - } + Ringbuf_Init(&Test_Buffer, (char *) Test_Buffer_Data, + RING_BUFFER_DATA_SIZE, RING_BUFFER_SIZE); + } + /* empty any the existing data */ + while (!Ringbuf_Empty(&Test_Buffer)) { + (void) Ringbuf_Pop_Front(&Test_Buffer); } - void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) { /* port specific data */ - char *data; - if (!Ringbuf_Empty(&Test_Buffer) && mstp_port && - (mstp_port->DataAvailable == false)) { - data = Ringbuf_Pop_Front(&Test_Buffer); if (data) { + if (buffer) { + while (len) { + (void) Ringbuf_Put(&Test_Buffer, (char *) buffer); + len--; + buffer++; + } + } +} + +void RS485_Check_UART_Data( + volatile struct mstp_port_struct_t *mstp_port) +{ /* port specific data */ + char *data; + if (!Ringbuf_Empty(&Test_Buffer) && mstp_port && + (mstp_port->DataAvailable == false)) { + data = Ringbuf_Pop_Front(&Test_Buffer); + if (data) { mstp_port->DataRegister = *data; - mstp_port->DataAvailable = true;} + mstp_port->DataAvailable = true; } } +} - uint16_t MSTP_Put_Receive(volatile struct mstp_port_struct_t *mstp_port) { - return mstp_port->DataLength;} +uint16_t MSTP_Put_Receive( + volatile struct mstp_port_struct_t *mstp_port) +{ + return mstp_port->DataLength; +} /* 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 */ - return 0;} +uint16_t MSTP_Get_Send( + volatile struct mstp_port_struct_t * mstp_port, + unsigned timeout) +{ /* milliseconds to wait for a packet */ + return 0; +} - uint16_t MSTP_Get_Reply(volatile struct mstp_port_struct_t * mstp_port, unsigned timeout) { /* milliseconds to wait for a packet */ - return 0;} +uint16_t MSTP_Get_Reply( + volatile struct mstp_port_struct_t * mstp_port, + unsigned timeout) +{ /* milliseconds to wait for a packet */ + return 0; +} - uint16_t SilenceTime = 0; static uint16_t Timer_Silence(void) { - return SilenceTime;} - static void Timer_Silence_Reset(void) { - SilenceTime = 0;} +uint16_t SilenceTime = 0; +static uint16_t Timer_Silence( + void) +{ + return SilenceTime; +} +static void Timer_Silence_Reset( + void) +{ + SilenceTime = 0; +} - void testReceiveNodeFSM(Test * pTest) { - volatile struct mstp_port_struct_t mstp_port; /* port data */ - unsigned EventCount = 0; /* local counter */ - uint8_t my_mac = 0x05; /* local MAC address */ - uint8_t HeaderCRC = 0; /* for local CRC calculation */ - uint8_t FrameType = 0; /* type of packet that was sent */ - unsigned len; /* used for the size of the message packet */ - size_t i; /* used to loop through the message bytes */ - uint8_t buffer[MAX_MPDU] = { - 0}; uint8_t data[MAX_PDU] = { - 0}; mstp_port.InputBuffer = &RxBuffer[0]; - mstp_port.InputBufferSize = sizeof(RxBuffer); - mstp_port.OutputBuffer = &TxBuffer[0]; - mstp_port.OutputBufferSize = sizeof(TxBuffer); - mstp_port.SilenceTimer = Timer_Silence; - mstp_port.SilenceTimerReset = Timer_Silence_Reset; - mstp_port.This_Station = my_mac; mstp_port.Nmax_info_frames = 1; - mstp_port.Nmax_master = 127; MSTP_Init(&mstp_port); - /* check the receive error during idle */ - mstp_port.receive_state = MSTP_RECEIVE_STATE_IDLE; - mstp_port.ReceiveError = true; SilenceTime = 255; - mstp_port.EventCount = 0; INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.ReceiveError == false); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - /* check for bad packet header */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x11; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - /* check for good packet header, but timeout */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - /* force the timeout */ - SilenceTime = Tframe_abort + 1; MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - /* check for good packet header preamble, but receive error */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - /* force the error */ - mstp_port.ReceiveError = true; INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.ReceiveError == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - /* check for good packet header preamble1, but bad preamble2 */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - MSTP_Receive_Frame_FSM(&mstp_port); - /* no change of state if no data yet */ - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - /* repeated preamble1 */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - /* repeated preamble1 */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - /* bad data */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x11; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.ReceiveError == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - /* check for good packet header preamble, but timeout in packet */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - MSTP_Receive_Frame_FSM(&mstp_port); - /* preamble2 */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0xFF; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 0); - ct_test(pTest, mstp_port.HeaderCRC == 0xFF); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - /* force the timeout */ - SilenceTime = Tframe_abort + 1; MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); - /* check for good packet header preamble, but error in packet */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - MSTP_Receive_Frame_FSM(&mstp_port); - /* preamble2 */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0xFF; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 0); - ct_test(pTest, mstp_port.HeaderCRC == 0xFF); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - /* force the error */ - mstp_port.ReceiveError = true; INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.ReceiveError == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - /* check for good packet header preamble */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - MSTP_Receive_Frame_FSM(&mstp_port); - /* preamble2 */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0xFF; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 0); - ct_test(pTest, mstp_port.HeaderCRC == 0xFF); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - /* no change of state if no data yet */ - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - /* Data is received - index is incremented */ - /* FrameType */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = FRAME_TYPE_TOKEN; HeaderCRC = 0xFF; - HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 1); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - ct_test(pTest, FrameType == FRAME_TYPE_TOKEN); - /* Destination */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0x10; - HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 2); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - ct_test(pTest, mstp_port.DestinationAddress == 0x10); - /* Source */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = my_mac; - HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 3); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - ct_test(pTest, mstp_port.SourceAddress == my_mac); - /* Length1 = length*256 */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0; - HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 4); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - ct_test(pTest, mstp_port.DataLength == 0); - /* Length2 */ - mstp_port.DataAvailable = true; mstp_port.DataRegister = 0; - HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 5); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - ct_test(pTest, mstp_port.DataLength == 0); - /* HeaderCRC */ - mstp_port.DataAvailable = true; ct_test(pTest, HeaderCRC == 0x73); /* per Annex G example */ - mstp_port.DataRegister = ~HeaderCRC; /* one's compliment of CRC is sent */ - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 5); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - ct_test(pTest, mstp_port.HeaderCRC == 0x55); - /* BadCRC in header check */ - mstp_port.ReceivedInvalidFrame = false; mstp_port.ReceivedValidFrame = false; len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, 0x10, /* destination */ - my_mac, /* source */ - NULL, /* data */ - 0); /* data size */ - ct_test(pTest, len > 0); - /* make the header CRC bad */ - buffer[7] = 0x00; Load_Input_Buffer(buffer, len); for (i = 0; i < len; - i++) { +void testReceiveNodeFSM( + Test * pTest) +{ + volatile struct mstp_port_struct_t mstp_port; /* port data */ + unsigned EventCount = 0; /* local counter */ + uint8_t my_mac = 0x05; /* local MAC address */ + uint8_t HeaderCRC = 0; /* for local CRC calculation */ + uint8_t FrameType = 0; /* type of packet that was sent */ + unsigned len; /* used for the size of the message packet */ + size_t i; /* used to loop through the message bytes */ + uint8_t buffer[MAX_MPDU] = { + 0 + }; + uint8_t data[MAX_PDU] = { + 0 + }; + mstp_port.InputBuffer = &RxBuffer[0]; + mstp_port.InputBufferSize = sizeof(RxBuffer); + mstp_port.OutputBuffer = &TxBuffer[0]; + mstp_port.OutputBufferSize = sizeof(TxBuffer); + mstp_port.SilenceTimer = Timer_Silence; + mstp_port.SilenceTimerReset = Timer_Silence_Reset; + mstp_port.This_Station = my_mac; + mstp_port.Nmax_info_frames = 1; + mstp_port.Nmax_master = 127; + MSTP_Init(&mstp_port); + /* check the receive error during idle */ + mstp_port.receive_state = MSTP_RECEIVE_STATE_IDLE; + mstp_port.ReceiveError = true; + SilenceTime = 255; + mstp_port.EventCount = 0; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.ReceiveError == false); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for bad packet header */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x11; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for good packet header, but timeout */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + /* force the timeout */ + SilenceTime = Tframe_abort + 1; + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for good packet header preamble, but receive error */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + /* force the error */ + mstp_port.ReceiveError = true; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.ReceiveError == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for good packet header preamble1, but bad preamble2 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + MSTP_Receive_Frame_FSM(&mstp_port); + /* no change of state if no data yet */ + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + /* repeated preamble1 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + /* repeated preamble1 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + /* bad data */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x11; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.ReceiveError == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for good packet header preamble, but timeout in packet */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + MSTP_Receive_Frame_FSM(&mstp_port); + /* preamble2 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0xFF; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 0); + ct_test(pTest, mstp_port.HeaderCRC == 0xFF); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + /* force the timeout */ + SilenceTime = Tframe_abort + 1; + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); + /* check for good packet header preamble, but error in packet */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + MSTP_Receive_Frame_FSM(&mstp_port); + /* preamble2 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0xFF; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 0); + ct_test(pTest, mstp_port.HeaderCRC == 0xFF); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + /* force the error */ + mstp_port.ReceiveError = true; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.ReceiveError == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for good packet header preamble */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + MSTP_Receive_Frame_FSM(&mstp_port); + /* preamble2 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0xFF; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 0); + ct_test(pTest, mstp_port.HeaderCRC == 0xFF); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + /* no change of state if no data yet */ + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + /* Data is received - index is incremented */ + /* FrameType */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = FRAME_TYPE_TOKEN; + HeaderCRC = 0xFF; + HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 1); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + ct_test(pTest, FrameType == FRAME_TYPE_TOKEN); + /* Destination */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x10; + HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 2); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + ct_test(pTest, mstp_port.DestinationAddress == 0x10); + /* Source */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = my_mac; + HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 3); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + ct_test(pTest, mstp_port.SourceAddress == my_mac); + /* Length1 = length*256 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0; + HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 4); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + ct_test(pTest, mstp_port.DataLength == 0); + /* Length2 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0; + HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 5); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + ct_test(pTest, mstp_port.DataLength == 0); + /* HeaderCRC */ + mstp_port.DataAvailable = true; + ct_test(pTest, HeaderCRC == 0x73); /* per Annex G example */ + mstp_port.DataRegister = ~HeaderCRC; /* one's compliment of CRC is sent */ + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 5); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + ct_test(pTest, mstp_port.HeaderCRC == 0x55); + /* BadCRC in header check */ + mstp_port.ReceivedInvalidFrame = false; + mstp_port.ReceivedValidFrame = false; + len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, 0x10, /* destination */ + my_mac, /* source */ + NULL, /* data */ + 0); /* data size */ + ct_test(pTest, len > 0); + /* make the header CRC bad */ + buffer[7] = 0x00; + Load_Input_Buffer(buffer, len); + for (i = 0; i < len; i++) { RS485_Check_UART_Data(&mstp_port); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount);} - ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); - ct_test(pTest, mstp_port.ReceivedValidFrame == false); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - /* NoData for us */ - mstp_port.ReceivedInvalidFrame = false; mstp_port.ReceivedValidFrame = false; len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, my_mac, /* destination */ - my_mac, /* source */ - NULL, /* data */ - 0); /* data size */ - ct_test(pTest, len > 0); Load_Input_Buffer(buffer, len); for (i = 0; - i < len; i++) { - RS485_Check_UART_Data(&mstp_port); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount);} - ct_test(pTest, mstp_port.ReceivedInvalidFrame == false); - ct_test(pTest, mstp_port.ReceivedValidFrame == true); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - /* FrameTooLong */ - mstp_port.ReceivedInvalidFrame = false; mstp_port.ReceivedValidFrame = false; len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, my_mac, /* destination */ - my_mac, /* source */ - NULL, /* data */ - 0); /* data size */ - ct_test(pTest, len > 0); - /* make the header data length bad */ - buffer[5] = 0x02; Load_Input_Buffer(buffer, len); for (i = 0; i < len; - i++) { - RS485_Check_UART_Data(&mstp_port); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount);} - ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); - ct_test(pTest, mstp_port.ReceivedValidFrame == false); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - /* Data */ - mstp_port.ReceivedInvalidFrame = false; - mstp_port.ReceivedValidFrame = false; memset(data, 0, sizeof(data)); - len = - MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_PROPRIETARY_MIN, - my_mac, my_mac, data, sizeof(data)); ct_test(pTest, len > 0); - Load_Input_Buffer(buffer, len); RS485_Check_UART_Data(&mstp_port); + INCREMENT_AND_LIMIT_UINT8(EventCount); MSTP_Receive_Frame_FSM(&mstp_port); - while (mstp_port.receive_state != MSTP_RECEIVE_STATE_IDLE) { - RS485_Check_UART_Data(&mstp_port); - MSTP_Receive_Frame_FSM(&mstp_port);} - ct_test(pTest, mstp_port.DataLength == sizeof(data)); - ct_test(pTest, mstp_port.ReceivedInvalidFrame == false); - ct_test(pTest, mstp_port.ReceivedValidFrame == true); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - return;} - - void testMasterNodeFSM(Test * pTest) { - volatile struct mstp_port_struct_t MSTP_Port; /* port data */ - uint8_t my_mac = 0x05; /* local MAC address */ - MSTP_Port.InputBuffer = &RxBuffer[0]; - MSTP_Port.InputBufferSize = sizeof(RxBuffer); - MSTP_Port.OutputBuffer = &TxBuffer[0]; - MSTP_Port.OutputBufferSize = sizeof(TxBuffer); - MSTP_Port.This_Station = my_mac; MSTP_Port.Nmax_info_frames = 1; - MSTP_Port.Nmax_master = 127; MSTP_Port.SilenceTimer = Timer_Silence; - MSTP_Port.SilenceTimerReset = Timer_Silence_Reset; - MSTP_Init(&MSTP_Port); - ct_test(pTest, MSTP_Port.master_state == MSTP_MASTER_STATE_INITIALIZE); - /* FIXME: write a unit test for the Master Node State Machine */ + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); } + ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); + ct_test(pTest, mstp_port.ReceivedValidFrame == false); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* NoData for us */ + mstp_port.ReceivedInvalidFrame = false; + mstp_port.ReceivedValidFrame = false; + len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, my_mac, /* destination */ + my_mac, /* source */ + NULL, /* data */ + 0); /* data size */ + ct_test(pTest, len > 0); + Load_Input_Buffer(buffer, len); + for (i = 0; i < len; i++) { + RS485_Check_UART_Data(&mstp_port); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + } + ct_test(pTest, mstp_port.ReceivedInvalidFrame == false); + ct_test(pTest, mstp_port.ReceivedValidFrame == true); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* FrameTooLong */ + mstp_port.ReceivedInvalidFrame = false; + mstp_port.ReceivedValidFrame = false; + len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, my_mac, /* destination */ + my_mac, /* source */ + NULL, /* data */ + 0); /* data size */ + ct_test(pTest, len > 0); + /* make the header data length bad */ + buffer[5] = 0x02; + Load_Input_Buffer(buffer, len); + for (i = 0; i < len; i++) { + RS485_Check_UART_Data(&mstp_port); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + } + ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); + ct_test(pTest, mstp_port.ReceivedValidFrame == false); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* Data */ + mstp_port.ReceivedInvalidFrame = false; + mstp_port.ReceivedValidFrame = false; + memset(data, 0, sizeof(data)); + len = + MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_PROPRIETARY_MIN, + my_mac, my_mac, data, sizeof(data)); + ct_test(pTest, len > 0); + Load_Input_Buffer(buffer, len); + RS485_Check_UART_Data(&mstp_port); + MSTP_Receive_Frame_FSM(&mstp_port); + while (mstp_port.receive_state != MSTP_RECEIVE_STATE_IDLE) { + RS485_Check_UART_Data(&mstp_port); + MSTP_Receive_Frame_FSM(&mstp_port); + } + ct_test(pTest, mstp_port.DataLength == sizeof(data)); + ct_test(pTest, mstp_port.ReceivedInvalidFrame == false); + ct_test(pTest, mstp_port.ReceivedValidFrame == true); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + return; +} + +void testMasterNodeFSM( + Test * pTest) +{ + volatile struct mstp_port_struct_t MSTP_Port; /* port data */ + uint8_t my_mac = 0x05; /* local MAC address */ + MSTP_Port.InputBuffer = &RxBuffer[0]; + MSTP_Port.InputBufferSize = sizeof(RxBuffer); + MSTP_Port.OutputBuffer = &TxBuffer[0]; + MSTP_Port.OutputBufferSize = sizeof(TxBuffer); + MSTP_Port.This_Station = my_mac; + MSTP_Port.Nmax_info_frames = 1; + MSTP_Port.Nmax_master = 127; + MSTP_Port.SilenceTimer = Timer_Silence; + MSTP_Port.SilenceTimerReset = Timer_Silence_Reset; + MSTP_Init(&MSTP_Port); + ct_test(pTest, MSTP_Port.master_state == MSTP_MASTER_STATE_INITIALIZE); + /* FIXME: write a unit test for the Master Node State Machine */ +} #endif #ifdef TEST_MSTP - int main(void) { - Test * pTest; bool rc; pTest = ct_create("mstp", NULL); - /* individual tests */ - rc = ct_addTestFunction(pTest, testReceiveNodeFSM); assert(rc); - rc = ct_addTestFunction(pTest, testMasterNodeFSM); assert(rc); - ct_setStream(pTest, stdout); ct_run(pTest); - (void) ct_report(pTest); ct_destroy(pTest); return 0;} +int main( + void) +{ + Test *pTest; + bool rc; + pTest = ct_create("mstp", NULL); + /* individual tests */ + rc = ct_addTestFunction(pTest, testReceiveNodeFSM); + assert(rc); + rc = ct_addTestFunction(pTest, testMasterNodeFSM); + assert(rc); + ct_setStream(pTest, stdout); + ct_run(pTest); + (void) ct_report(pTest); + ct_destroy(pTest); + return 0; +} #endif