changed c++ comments to c comments using comment.sh script.

This commit is contained in:
skarg
2006-02-19 01:33:30 +00:00
parent dee63d45bc
commit b686fa7ca7
34 changed files with 975 additions and 1030 deletions
+3 -3
View File
@@ -49,7 +49,7 @@
#include "client.h" #include "client.h"
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive /* buffer used for receive */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
@@ -125,9 +125,9 @@ static void Init_Service_Handlers(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = { 0 }; // address where message came from BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; /* milliseconds */
unsigned max_apdu = 0; unsigned max_apdu = 0;
time_t elapsed_seconds = 0; time_t elapsed_seconds = 0;
time_t last_seconds = 0; time_t last_seconds = 0;
+6 -6
View File
@@ -50,7 +50,7 @@
#include "client.h" #include "client.h"
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive /* buffer used for receive */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
@@ -133,9 +133,9 @@ static void Init_Service_Handlers(void)
} }
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
static void print_address(char *name, BACNET_ADDRESS * dest) // destination address static void print_address(char *name, BACNET_ADDRESS * dest)
{ { /* destination address */
int i = 0; // counter int i = 0; /* counter */
if (dest) { if (dest) {
printf("%s: ", name); printf("%s: ", name);
@@ -149,9 +149,9 @@ static void print_address(char *name, BACNET_ADDRESS * dest) // destination a
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = { 0 }; // address where message came from BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; /* milliseconds */
unsigned max_apdu = 0; unsigned max_apdu = 0;
time_t elapsed_seconds = 0; time_t elapsed_seconds = 0;
time_t last_seconds = 0; time_t last_seconds = 0;
+12 -12
View File
@@ -82,9 +82,9 @@ static void cleanup(void)
datalink_cleanup(); datalink_cleanup();
} }
static void print_address(char *name, BACNET_ADDRESS * dest) // destination address static void print_address(char *name, BACNET_ADDRESS * dest)
{ { /* destination address */
int i = 0; // counter int i = 0; /* counter */
if (dest) { if (dest) {
printf("%s: ", name); printf("%s: ", name);
@@ -97,9 +97,9 @@ static void print_address(char *name, BACNET_ADDRESS * dest) // destination a
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = { 0 }; // address where message came from BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; /* milliseconds */
BACNET_ADDRESS my_address, broadcast_address; BACNET_ADDRESS my_address, broadcast_address;
time_t last_seconds = 0; time_t last_seconds = 0;
time_t current_seconds = 0; time_t current_seconds = 0;
@@ -113,7 +113,7 @@ int main(int argc, char *argv[])
Device_Object_Instance_Number()); Device_Object_Instance_Number());
Init_Service_Handlers(); Init_Service_Handlers();
#ifdef BACDL_ETHERNET #ifdef BACDL_ETHERNET
// init the physical layer /* init the physical layer */
if (!ethernet_init("eth0")) if (!ethernet_init("eth0"))
return 1; return 1;
#endif #endif
@@ -136,15 +136,15 @@ int main(int argc, char *argv[])
last_seconds = time(NULL); last_seconds = time(NULL);
/* broadcast an I-Am on startup */ /* broadcast an I-Am on startup */
I_Am_Request = true; I_Am_Request = true;
// loop forever /* loop forever */
for (;;) { for (;;) {
// input /* input */
current_seconds = time(NULL); current_seconds = time(NULL);
// returns 0 bytes on timeout /* returns 0 bytes on timeout */
pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
// process /* process */
if (pdu_len) { if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len); npdu_handler(&src, &Rx_Buf[0], pdu_len);
} }
@@ -156,8 +156,8 @@ int main(int argc, char *argv[])
I_Am_Request = false; I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]); iam_send(&Handler_Transmit_Buffer[0]);
} }
// output /* output */
// blink LEDs, Turn on or off outputs, etc /* blink LEDs, Turn on or off outputs, etc */
} }
} }
+6 -6
View File
@@ -49,7 +49,7 @@
#include "client.h" #include "client.h"
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive /* buffer used for receive */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
@@ -103,9 +103,9 @@ static void Init_Service_Handlers(void)
} }
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
static void print_address(char *name, BACNET_ADDRESS * dest) // destination address static void print_address(char *name, BACNET_ADDRESS * dest)
{ { /* destination address */
int i = 0; // counter int i = 0; /* counter */
if (dest) { if (dest) {
printf("%s: ", name); printf("%s: ", name);
@@ -119,9 +119,9 @@ static void print_address(char *name, BACNET_ADDRESS * dest) // destination a
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = { 0 }; // address where message came from BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; /* milliseconds */
time_t elapsed_seconds = 0; time_t elapsed_seconds = 0;
time_t last_seconds = 0; time_t last_seconds = 0;
time_t current_seconds = 0; time_t current_seconds = 0;
+8 -8
View File
@@ -48,7 +48,7 @@
#include "handlers.h" #include "handlers.h"
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive /* buffer used for receive */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
@@ -117,11 +117,11 @@ static uint8_t Send_Atomic_Write_File_Stream(uint32_t device_id,
status = tsm_transaction_available(); status = tsm_transaction_available();
if (status) { if (status) {
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, /* true for confirmed messages */
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID(); invoke_id = tsm_next_free_invokeID();
// load the data for the encoding /* load the data for the encoding */
data.object_type = OBJECT_FILE; data.object_type = OBJECT_FILE;
data.object_instance = file_instance; data.object_instance = file_instance;
data.access = FILE_STREAM_ACCESS; data.access = FILE_STREAM_ACCESS;
@@ -138,8 +138,8 @@ static uint8_t Send_Atomic_Write_File_Stream(uint32_t device_id,
if ((unsigned) pdu_len <= max_apdu) { if ((unsigned) pdu_len <= max_apdu) {
tsm_set_confirmed_unsegmented_transaction(invoke_id, tsm_set_confirmed_unsegmented_transaction(invoke_id,
&dest, &Handler_Transmit_Buffer[0], pdu_len); &dest, &Handler_Transmit_Buffer[0], pdu_len);
bytes_sent = datalink_send_pdu(&dest, // destination address bytes_sent = datalink_send_pdu(&dest, /* destination address */
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data &Handler_Transmit_Buffer[0], pdu_len); /* number of bytes of data */
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr, fprintf(stderr,
"Failed to Send AtomicWriteFile Request (%s)!\n", "Failed to Send AtomicWriteFile Request (%s)!\n",
@@ -167,7 +167,7 @@ static void Send_WhoIs(uint32_t device_id)
datalink_get_broadcast_address(&dest); datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */ /* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, // true for confirmed messages pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, /* true for confirmed messages */
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */ /* encode the APDU portion of the packet */
@@ -227,9 +227,9 @@ static void Init_Service_Handlers(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = { 0 }; // address where message came from BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; /* milliseconds */
unsigned max_apdu = 0; unsigned max_apdu = 0;
time_t elapsed_seconds = 0; time_t elapsed_seconds = 0;
time_t last_seconds = 0; time_t last_seconds = 0;
+3 -3
View File
@@ -49,7 +49,7 @@
#include "client.h" #include "client.h"
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive /* buffer used for receive */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
@@ -142,9 +142,9 @@ static void Init_Service_Handlers(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = { 0 }; // address where message came from BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; /* milliseconds */
unsigned max_apdu = 0; unsigned max_apdu = 0;
time_t elapsed_seconds = 0; time_t elapsed_seconds = 0;
time_t last_seconds = 0; time_t last_seconds = 0;
+47 -47
View File
@@ -37,13 +37,13 @@
#include "arcnet.h" #include "arcnet.h"
#include "net.h" #include "net.h"
// my local device data - MAC address /* my local device data - MAC address */
uint8_t ARCNET_MAC_Address = 0; uint8_t ARCNET_MAC_Address = 0;
// ARCNET file handle /* ARCNET file handle */
static int ARCNET_Sock_FD = -1; static int ARCNET_Sock_FD = -1;
// ARCNET socket address (has the interface name) /* ARCNET socket address (has the interface name) */
static struct sockaddr ARCNET_Socket_Address; static struct sockaddr ARCNET_Socket_Address;
// Broadcast address /* Broadcast address */
#define ARCNET_BROADCAST 0 #define ARCNET_BROADCAST 0
/* /*
@@ -95,9 +95,9 @@ void arcnet_cleanup(void)
static int arcnet_bind(char *interface_name) static int arcnet_bind(char *interface_name)
{ {
int sock_fd = -1; // return value int sock_fd = -1; /* return value */
struct ifreq ifr; struct ifreq ifr;
int rv; // return value - error value from df or ioctl call int rv; /* return value - error value from df or ioctl call */
int uid = 0; int uid = 0;
/* check to see if we are being run as root */ /* check to see if we are being run as root */
@@ -109,10 +109,10 @@ static int arcnet_bind(char *interface_name)
return sock_fd; return sock_fd;
} }
fprintf(stderr, "arcnet: opening \"%s\"\n", interface_name); fprintf(stderr, "arcnet: opening \"%s\"\n", interface_name);
// note: on some systems you may have to add or enable in /* note: on some systems you may have to add or enable in */
// modules.conf (or in modutils/alias on Debian with update-modules) /* modules.conf (or in modutils/alias on Debian with update-modules) */
// alias net-pf-17 af_packet /* alias net-pf-17 af_packet */
// Then follow it by: # modprobe af_packet /* Then follow it by: # modprobe af_packet */
if ((sock_fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0) { if ((sock_fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0) {
/* Error occured */ /* Error occured */
fprintf(stderr, fprintf(stderr,
@@ -129,7 +129,7 @@ static int arcnet_bind(char *interface_name)
if (ARCNET_Sock_FD >= 0) { if (ARCNET_Sock_FD >= 0) {
/* Bind the socket to an interface name so we only get packets from it */ /* Bind the socket to an interface name so we only get packets from it */
ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET; ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET;
//ARCNET_Socket_Address.sa_family = PF_INET; /*ARCNET_Socket_Address.sa_family = PF_INET; */
/* Clear the memory before copying */ /* Clear the memory before copying */
memset(ARCNET_Socket_Address.sa_data, '\0', memset(ARCNET_Socket_Address.sa_data, '\0',
sizeof(ARCNET_Socket_Address.sa_data)); sizeof(ARCNET_Socket_Address.sa_data));
@@ -158,9 +158,9 @@ static int arcnet_bind(char *interface_name)
rv = ioctl(sock_fd, SIOCGIFHWADDR, &ifr); rv = ioctl(sock_fd, SIOCGIFHWADDR, &ifr);
if (rv != -1) /* worked okay */ if (rv != -1) /* worked okay */
ARCNET_MAC_Address = ifr.ifr_hwaddr.sa_data[0]; ARCNET_MAC_Address = ifr.ifr_hwaddr.sa_data[0];
// copy this info into the local copy since bind wiped it out /* copy this info into the local copy since bind wiped it out */
ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET; ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET;
//ARCNET_Socket_Address.sa_family = PF_INET; /*ARCNET_Socket_Address.sa_family = PF_INET; */
/* Clear the memory before copying */ /* Clear the memory before copying */
memset(ARCNET_Socket_Address.sa_data, '\0', memset(ARCNET_Socket_Address.sa_data, '\0',
sizeof(ARCNET_Socket_Address.sa_data)); sizeof(ARCNET_Socket_Address.sa_data));
@@ -184,17 +184,17 @@ bool arcnet_init(char *interface_name)
/* function to send a packet out the socket */ /* function to send a packet out the socket */
/* returns number of bytes sent on success, negative on failure */ /* returns number of bytes sent on success, negative on failure */
int arcnet_send(BACNET_ADDRESS * dest, // destination address int arcnet_send(BACNET_ADDRESS * dest, /* destination address */
BACNET_ADDRESS * src, // source address BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, // any data to be sent - may be null uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len) // number of bytes of data unsigned pdu_len)
{ { /* number of bytes of data */
int bytes = 0; int bytes = 0;
uint8_t mtu[512] = { 0 }; uint8_t mtu[512] = { 0 };
int mtu_len = 0; int mtu_len = 0;
struct archdr *pkt = (struct archdr *) mtu; struct archdr *pkt = (struct archdr *) mtu;
// don't waste time if the socket is not valid /* don't waste time if the socket is not valid */
if (ARCNET_Sock_FD < 0) { if (ARCNET_Sock_FD < 0) {
fprintf(stderr, "arcnet: socket is invalid!\n"); fprintf(stderr, "arcnet: socket is invalid!\n");
return -1; return -1;
@@ -239,30 +239,30 @@ int arcnet_send(BACNET_ADDRESS * dest, // destination address
/* function to send a PDU out the socket */ /* function to send a PDU out the socket */
/* returns number of bytes sent on success, negative on failure */ /* returns number of bytes sent on success, negative on failure */
int arcnet_send_pdu(BACNET_ADDRESS * dest, // destination address int arcnet_send_pdu(BACNET_ADDRESS * dest, /* destination address */
uint8_t * pdu, // any data to be sent - may be null uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len) // number of bytes of data unsigned pdu_len)
{ { /* number of bytes of data */
BACNET_ADDRESS src = { 0 }; // source address BACNET_ADDRESS src = { 0 }; /* source address */
src.mac[0] = ARCNET_MAC_Address; src.mac[0] = ARCNET_MAC_Address;
src.mac_len = 1; src.mac_len = 1;
return arcnet_send(dest, // destination address return arcnet_send(dest, /* destination address */
&src, // source address &src, /* source address */
pdu, // any data to be sent - may be null pdu, /* any data to be sent - may be null */
pdu_len); // number of bytes of data pdu_len); /* number of bytes of data */
} }
// receives an framed packet /* receives an framed packet */
// returns the number of octets in the PDU, or zero on failure /* returns the number of octets in the PDU, or zero on failure */
uint16_t arcnet_receive(BACNET_ADDRESS * src, // source address uint16_t arcnet_receive(BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, // PDU data uint8_t * pdu, /* PDU data */
uint16_t max_pdu, // amount of space available in the PDU uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout) // milliseconds to wait for a packet unsigned timeout)
{ { /* milliseconds to wait for a packet */
int received_bytes; int received_bytes;
uint8_t buf[512] = { 0 }; // data uint8_t buf[512] = { 0 }; /* data */
uint16_t pdu_len = 0; // return value uint16_t pdu_len = 0; /* return value */
fd_set read_fds; fd_set read_fds;
int max; int max;
struct timeval select_timeout; struct timeval select_timeout;
@@ -294,9 +294,9 @@ uint16_t arcnet_receive(BACNET_ADDRESS * src, // source address
/* See if there is a problem */ /* See if there is a problem */
if (received_bytes < 0) { if (received_bytes < 0) {
// EAGAIN Non-blocking I/O has been selected /* EAGAIN Non-blocking I/O has been selected */
// using O_NONBLOCK and no data /* using O_NONBLOCK and no data */
// was immediately available for reading. /* was immediately available for reading. */
if (errno != EAGAIN) if (errno != EAGAIN)
fprintf(stderr, fprintf(stderr,
"ethernet: Read error in receiving packet: %s\n", "ethernet: Read error in receiving packet: %s\n",
@@ -344,10 +344,10 @@ uint16_t arcnet_receive(BACNET_ADDRESS * src, // source address
/* compute the PDU length */ /* compute the PDU length */
pdu_len = received_bytes - ARC_HDR_SIZE; pdu_len = received_bytes - ARC_HDR_SIZE;
pdu_len -= 4 /* SC, DSAP, SSAP, LLC Control */ ; pdu_len -= 4 /* SC, DSAP, SSAP, LLC Control */ ;
// copy the buffer into the PDU /* copy the buffer into the PDU */
if (pdu_len < max_pdu) if (pdu_len < max_pdu)
memmove(&pdu[0], &pkt->soft.raw[4], pdu_len); memmove(&pdu[0], &pkt->soft.raw[4], pdu_len);
// silently ignore packets that are too large /* silently ignore packets that are too large */
else else
pdu_len = 0; pdu_len = 0;
@@ -360,7 +360,7 @@ void arcnet_get_my_address(BACNET_ADDRESS * my_address)
my_address->mac_len = 1; my_address->mac_len = 1;
my_address->mac[0] = ARCNET_MAC_Address; my_address->mac[0] = ARCNET_MAC_Address;
my_address->net = 0; // local only, no routing my_address->net = 0; /* local only, no routing */
my_address->len = 0; my_address->len = 0;
for (i = 0; i < MAX_MAC_LEN; i++) { for (i = 0; i < MAX_MAC_LEN; i++) {
my_address->adr[i] = 0; my_address->adr[i] = 0;
@@ -369,15 +369,15 @@ void arcnet_get_my_address(BACNET_ADDRESS * my_address)
return; return;
} }
void arcnet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address void arcnet_get_broadcast_address(BACNET_ADDRESS * dest)
{ { /* destination address */
int i = 0; // counter int i = 0; /* counter */
if (dest) { if (dest) {
dest->mac[0] = ARCNET_BROADCAST; dest->mac[0] = ARCNET_BROADCAST;
dest->mac_len = 1; dest->mac_len = 1;
dest->net = BACNET_BROADCAST_NETWORK; dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; // len=0 denotes broadcast address dest->len = 0; /* len=0 denotes broadcast address */
for (i = 0; i < MAX_MAC_LEN; i++) { for (i = 0; i < MAX_MAC_LEN; i++) {
dest->adr[i] = 0; dest->adr[i] = 0;
} }
+10 -10
View File
@@ -32,8 +32,8 @@
------------------------------------------- -------------------------------------------
####COPYRIGHTEND####*/ ####COPYRIGHTEND####*/
#include <stdint.h> // for standard integer types uint8_t etc. #include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> // for the standard bool type. #include <stdbool.h> /* for the standard bool type. */
#include "bacdcode.h" #include "bacdcode.h"
#include "bip.h" #include "bip.h"
#include "net.h" #include "net.h"
@@ -43,7 +43,7 @@ static int get_local_ifr_ioctl(char *ifname, struct ifreq *ifr,
int request) int request)
{ {
int fd; int fd;
int rv; // return value int rv; /* return value */
strncpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name)); strncpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
@@ -60,7 +60,7 @@ static int get_local_address_ioctl(char *ifname,
{ {
struct ifreq ifr = { {{0}} }; struct ifreq ifr = { {{0}} };
struct sockaddr_in *tcpip_address; struct sockaddr_in *tcpip_address;
int rv; // return value int rv; /* return value */
rv = get_local_ifr_ioctl(ifname, &ifr, request); rv = get_local_ifr_ioctl(ifname, &ifr, request);
if (rv >= 0) { if (rv >= 0) {
@@ -93,18 +93,18 @@ void bip_set_interface(char *ifname)
bool bip_init(void) bool bip_init(void)
{ {
int status = 0; // return from socket lib calls int status = 0; /* return from socket lib calls */
struct sockaddr_in sin; struct sockaddr_in sin;
int sockopt = 0; int sockopt = 0;
int sock_fd = -1; int sock_fd = -1;
// assumes that the driver has already been initialized /* assumes that the driver has already been initialized */
sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
bip_set_socket(sock_fd); bip_set_socket(sock_fd);
if (sock_fd < 0) if (sock_fd < 0)
return false; return false;
// Allow us to use the same socket for sending and receiving /* Allow us to use the same socket for sending and receiving */
// This makes sure that the src port is correct when sending /* This makes sure that the src port is correct when sending */
sockopt = 1; sockopt = 1;
status = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, status = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR,
&sockopt, sizeof(sockopt)); &sockopt, sizeof(sockopt));
@@ -113,7 +113,7 @@ bool bip_init(void)
bip_set_socket(-1); bip_set_socket(-1);
return status; return status;
} }
// allow us to send a broadcast /* allow us to send a broadcast */
status = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, status = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST,
&sockopt, sizeof(sockopt)); &sockopt, sizeof(sockopt));
if (status < 0) { if (status < 0) {
@@ -121,7 +121,7 @@ bool bip_init(void)
bip_set_socket(-1); bip_set_socket(-1);
return status; return status;
} }
// bind the socket to the local port number and IP address /* bind the socket to the local port number and IP address */
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(bip_get_port()); sin.sin_port = htons(bip_get_port());
+54 -54
View File
@@ -32,25 +32,25 @@
------------------------------------------- -------------------------------------------
####COPYRIGHTEND####*/ ####COPYRIGHTEND####*/
#include <stdint.h> // for standard integer types uint8_t etc. #include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> // for the standard bool type. #include <stdbool.h> /* for the standard bool type. */
#include "net.h" #include "net.h"
#include "bacdef.h" #include "bacdef.h"
#include "ethernet.h" #include "ethernet.h"
#include "bacdcode.h" #include "bacdcode.h"
// commonly used comparison address for ethernet /* commonly used comparison address for ethernet */
uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = uint8_t Ethernet_Broadcast[MAX_MAC_LEN] =
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
// commonly used empty address for ethernet quick compare /* commonly used empty address for ethernet quick compare */
uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 }; uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
// my local device data - MAC address /* my local device data - MAC address */
uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0 }; uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0 };
static int eth802_sockfd = -1; /* 802.2 file handle */ static int eth802_sockfd = -1; /* 802.2 file handle */
static struct sockaddr eth_addr = { 0 }; // used for binding 802.2 static struct sockaddr eth_addr = { 0 }; /* used for binding 802.2 */
bool ethernet_valid(void) bool ethernet_valid(void)
{ {
@@ -87,7 +87,7 @@ int setNonblocking(int fd)
/* opens an 802.2 socket to receive and send packets */ /* opens an 802.2 socket to receive and send packets */
static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name) static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
{ {
int sock_fd = -1; // return value int sock_fd = -1; /* return value */
int uid = 0; int uid = 0;
fprintf(stderr, "ethernet: opening \"%s\"\n", interface_name); fprintf(stderr, "ethernet: opening \"%s\"\n", interface_name);
@@ -99,10 +99,10 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
"Try running with root priveleges.\n"); "Try running with root priveleges.\n");
return sock_fd; return sock_fd;
} }
// note: on some systems you may have to add or enable in /* note: on some systems you may have to add or enable in */
// modules.conf (or in modutils/alias on Debian with update-modules) /* modules.conf (or in modutils/alias on Debian with update-modules) */
// alias net-pf-17 af_packet /* alias net-pf-17 af_packet */
// Then follow it by: # modprobe af_packet /* Then follow it by: # modprobe af_packet */
/* Attempt to open the socket for 802.2 ethernet frames */ /* Attempt to open the socket for 802.2 ethernet frames */
if ((sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_802_2))) < 0) { if ((sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_802_2))) < 0) {
@@ -152,7 +152,7 @@ static int get_local_hwaddr(const char *ifname, unsigned char *mac)
{ {
struct ifreq ifr; struct ifreq ifr;
int fd; int fd;
int rv; // return value - error value from df or ioctl call int rv; /* return value - error value from df or ioctl call */
/* determine the local MAC address */ /* determine the local MAC address */
strcpy(ifr.ifr_name, ifname); strcpy(ifr.ifr_name, ifname);
@@ -178,17 +178,17 @@ bool ethernet_init(char *interface_name)
/* function to send a packet out the 802.2 socket */ /* function to send a packet out the 802.2 socket */
/* returns bytes sent success, negative on failure */ /* returns bytes sent success, negative on failure */
int ethernet_send(BACNET_ADDRESS * dest, // destination address int ethernet_send(BACNET_ADDRESS * dest, /* destination address */
BACNET_ADDRESS * src, // source address BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, // any data to be sent - may be null uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len) // number of bytes of data unsigned pdu_len)
{ { /* number of bytes of data */
int bytes = 0; int bytes = 0;
uint8_t mtu[MAX_MPDU] = { 0 }; uint8_t mtu[MAX_MPDU] = { 0 };
int mtu_len = 0; int mtu_len = 0;
int i = 0; int i = 0;
// don't waste time if the socket is not valid /* don't waste time if the socket is not valid */
if (eth802_sockfd < 0) { if (eth802_sockfd < 0) {
fprintf(stderr, "ethernet: 802.2 socket is invalid!\n"); fprintf(stderr, "ethernet: 802.2 socket is invalid!\n");
return -1; return -1;
@@ -221,7 +221,7 @@ int ethernet_send(BACNET_ADDRESS * dest, // destination address
/* packet length */ /* packet length */
mtu_len += encode_unsigned16(&mtu[12], mtu_len += encode_unsigned16(&mtu[12],
3 /*DSAP,SSAP,LLC */ + pdu_len); 3 /*DSAP,SSAP,LLC */ + pdu_len);
// Logical PDU portion /* Logical PDU portion */
mtu[mtu_len++] = 0x82; /* DSAP for BACnet */ mtu[mtu_len++] = 0x82; /* DSAP for BACnet */
mtu[mtu_len++] = 0x82; /* SSAP for BACnet */ mtu[mtu_len++] = 0x82; /* SSAP for BACnet */
mtu[mtu_len++] = 0x03; /* Control byte in header */ mtu[mtu_len++] = 0x03; /* Control byte in header */
@@ -242,12 +242,12 @@ int ethernet_send(BACNET_ADDRESS * dest, // destination address
/* function to send a packet out the 802.2 socket */ /* function to send a packet out the 802.2 socket */
/* returns number of bytes sent on success, negative on failure */ /* returns number of bytes sent on success, negative on failure */
int ethernet_send_pdu(BACNET_ADDRESS * dest, // destination address int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */
uint8_t * pdu, // any data to be sent - may be null uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len) // number of bytes of data unsigned pdu_len)
{ { /* number of bytes of data */
int i = 0; // counter int i = 0; /* counter */
BACNET_ADDRESS src = { 0 }; // source address BACNET_ADDRESS src = { 0 }; /* source address */
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
src.mac[i] = Ethernet_MAC_Address[i]; src.mac[i] = Ethernet_MAC_Address[i];
@@ -255,22 +255,22 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, // destination address
} }
/* function to send a packet out the 802.2 socket */ /* function to send a packet out the 802.2 socket */
/* returns 1 on success, 0 on failure */ /* returns 1 on success, 0 on failure */
return ethernet_send(dest, // destination address return ethernet_send(dest, /* destination address */
&src, // source address &src, /* source address */
pdu, // any data to be sent - may be null pdu, /* any data to be sent - may be null */
pdu_len); // number of bytes of data pdu_len); /* number of bytes of data */
} }
// receives an 802.2 framed packet /* receives an 802.2 framed packet */
// returns the number of octets in the PDU, or zero on failure /* returns the number of octets in the PDU, or zero on failure */
uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, // PDU data uint8_t * pdu, /* PDU data */
uint16_t max_pdu, // amount of space available in the PDU uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout) // number of milliseconds to wait for a packet unsigned timeout)
{ { /* number of milliseconds to wait for a packet */
int received_bytes; int received_bytes;
uint8_t buf[MAX_MPDU] = { 0 }; // data uint8_t buf[MAX_MPDU] = { 0 }; /* data */
uint16_t pdu_len = 0; // return value uint16_t pdu_len = 0; /* return value */
fd_set read_fds; fd_set read_fds;
int max; int max;
struct timeval select_timeout; struct timeval select_timeout;
@@ -301,9 +301,9 @@ uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address
/* See if there is a problem */ /* See if there is a problem */
if (received_bytes < 0) { if (received_bytes < 0) {
// EAGAIN Non-blocking I/O has been selected /* EAGAIN Non-blocking I/O has been selected */
// using O_NONBLOCK and no data /* using O_NONBLOCK and no data */
// was immediately available for reading. /* was immediately available for reading. */
if (errno != EAGAIN) if (errno != EAGAIN)
fprintf(stderr, fprintf(stderr,
"ethernet: Read error in receiving packet: %s\n", "ethernet: Read error in receiving packet: %s\n",
@@ -316,27 +316,27 @@ uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address
/* the signature of an 802.2 BACnet packet */ /* the signature of an 802.2 BACnet packet */
if ((buf[14] != 0x82) && (buf[15] != 0x82)) { if ((buf[14] != 0x82) && (buf[15] != 0x82)) {
//fprintf(stderr,"ethernet: Non-BACnet packet\n"); /*fprintf(stderr,"ethernet: Non-BACnet packet\n"); */
return 0; return 0;
} }
// copy the source address /* copy the source address */
src->mac_len = 6; src->mac_len = 6;
memmove(src->mac, &buf[6], 6); memmove(src->mac, &buf[6], 6);
// check destination address for when /* check destination address for when */
// the Ethernet card is in promiscious mode /* the Ethernet card is in promiscious mode */
if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0) if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0)
&& (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) { && (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) {
//fprintf(stderr, "ethernet: This packet isn't for us\n"); /*fprintf(stderr, "ethernet: This packet isn't for us\n"); */
return 0; return 0;
} }
(void) decode_unsigned16(&buf[12], &pdu_len); (void) decode_unsigned16(&buf[12], &pdu_len);
pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ; pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ;
// copy the buffer into the PDU /* copy the buffer into the PDU */
if (pdu_len < max_pdu) if (pdu_len < max_pdu)
memmove(&pdu[0], &buf[17], pdu_len); memmove(&pdu[0], &buf[17], pdu_len);
// ignore packets that are too large /* ignore packets that are too large */
else else
pdu_len = 0; pdu_len = 0;
@@ -364,7 +364,7 @@ void ethernet_get_my_address(BACNET_ADDRESS * my_address)
my_address->mac[i] = Ethernet_MAC_Address[i]; my_address->mac[i] = Ethernet_MAC_Address[i];
my_address->mac_len++; my_address->mac_len++;
} }
my_address->net = 0; // local only, no routing my_address->net = 0; /* local only, no routing */
my_address->len = 0; my_address->len = 0;
for (i = 0; i < MAX_MAC_LEN; i++) { for (i = 0; i < MAX_MAC_LEN; i++) {
my_address->adr[i] = 0; my_address->adr[i] = 0;
@@ -373,9 +373,9 @@ void ethernet_get_my_address(BACNET_ADDRESS * my_address)
return; return;
} }
void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address void ethernet_get_broadcast_address(BACNET_ADDRESS * dest)
{ { /* destination address */
int i = 0; // counter int i = 0; /* counter */
if (dest) { if (dest) {
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
@@ -383,7 +383,7 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination a
} }
dest->mac_len = 6; dest->mac_len = 6;
dest->net = BACNET_BROADCAST_NETWORK; dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; // denotes broadcast address dest->len = 0; /* denotes broadcast address */
for (i = 0; i < MAX_MAC_LEN; i++) { for (i = 0; i < MAX_MAC_LEN; i++) {
dest->adr[i] = 0; dest->adr[i] = 0;
} }
@@ -394,7 +394,7 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination a
void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest) void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest)
{ {
int i = 0; // counter int i = 0; /* counter */
if (info) if (info)
fprintf(stderr, "%s", info); fprintf(stderr, "%s", info);
+24 -24
View File
@@ -44,10 +44,10 @@
#include "net.h" #include "net.h"
#include "txbuf.h" #include "txbuf.h"
// This is an example application using the BACnet Stack on Linux /* This is an example application using the BACnet Stack on Linux */
bool Who_Is_Request = true; bool Who_Is_Request = true;
// buffers used for receiving /* buffers used for receiving */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
static void LocalIAmHandler(uint8_t * service_request, static void LocalIAmHandler(uint8_t * service_request,
@@ -134,7 +134,7 @@ static void Read_Properties(void)
invoke ID from the sending of the request, and wait until we invoke ID from the sending of the request, and wait until we
got the reply with matching invoke ID or the TSM of the got the reply with matching invoke ID or the TSM of the
invoke ID expired. This demo is doing things asynchronously. */ invoke ID expired. This demo is doing things asynchronously. */
status = Send_Read_Property_Request(device_id, // destination device status = Send_Read_Property_Request(device_id, /* destination device */
OBJECT_DEVICE, OBJECT_DEVICE,
device_id, object_props[property], BACNET_ARRAY_ALL); device_id, object_props[property], BACNET_ARRAY_ALL);
if (status) if (status)
@@ -156,25 +156,25 @@ static void Read_Properties(void)
static void Init_Service_Handlers(void) static void Init_Service_Handlers(void)
{ {
// we need to handle who-is to support dynamic device binding /* we need to handle who-is to support dynamic device binding */
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler); LocalIAmHandler);
// set the handler for all the services we don't implement /* set the handler for all the services we don't implement */
// It is required to send the proper reject message... /* It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service); (handler_unrecognized_service);
// Set the handlers for any confirmed services that we support. /* Set the handlers for any confirmed services that we support. */
// We must implement read property - it's required! /* We must implement read property - it's required! */
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
handler_write_property); handler_write_property);
apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE, apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
handler_atomic_read_file); handler_atomic_read_file);
// handle the data coming back from confirmed requests /* handle the data coming back from confirmed requests */
apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY, apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property_ack); handler_read_property_ack);
apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE, apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
@@ -223,23 +223,23 @@ static void sig_handler(int signo)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = { 0 }; // address where message came from BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; /* milliseconds */
unsigned count = 0; // milliseconds unsigned count = 0; /* milliseconds */
time_t start_time; time_t start_time;
time_t new_time = 0; time_t new_time = 0;
start_time = time(NULL); /* get current time */ start_time = time(NULL); /* get current time */
// Linux specials /* Linux specials */
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
signal(SIGHUP, sig_handler); signal(SIGHUP, sig_handler);
signal(SIGTERM, sig_handler); signal(SIGTERM, sig_handler);
// setup this BACnet Server device /* setup this BACnet Server device */
Device_Set_Object_Instance_Number(111); Device_Set_Object_Instance_Number(111);
Init_Service_Handlers(); Init_Service_Handlers();
#ifdef BACDL_ETHERNET #ifdef BACDL_ETHERNET
// init the physical layer /* init the physical layer */
if (!ethernet_init("eth0")) if (!ethernet_init("eth0"))
return 1; return 1;
#endif #endif
@@ -254,14 +254,14 @@ int main(int argc, char *argv[])
return 1; return 1;
#endif #endif
// loop forever /* loop forever */
for (;;) { for (;;) {
// input /* input */
new_time = time(NULL); new_time = time(NULL);
// returns 0 bytes on timeout /* returns 0 bytes on timeout */
pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
// process /* process */
if (pdu_len) { if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len); npdu_handler(&src, &Rx_Buf[0], pdu_len);
} }
@@ -276,13 +276,13 @@ int main(int argc, char *argv[])
Who_Is_Request = false; Who_Is_Request = false;
Send_WhoIs(-1, -1); Send_WhoIs(-1, -1);
} }
// output /* output */
// some round robin task switching /* some round robin task switching */
count++; count++;
switch (count) { switch (count) {
case 1: case 1:
// used for testing, but kind of noisy on the network /* used for testing, but kind of noisy on the network */
//Read_Properties(); /*Read_Properties(); */
break; break;
case 2: case 2:
break; break;
@@ -291,7 +291,7 @@ int main(int argc, char *argv[])
break; break;
} }
// blink LEDs, Turn on or off outputs, etc /* blink LEDs, Turn on or off outputs, etc */
} }
return 0; return 0;
+25 -25
View File
@@ -33,9 +33,9 @@
------------------------------------------- -------------------------------------------
####COPYRIGHTEND####*/ ####COPYRIGHTEND####*/
// The module handles sending data out the RS-485 port /* The module handles sending data out the RS-485 port */
// and handles receiving data from the RS-485 port. /* and handles receiving data from the RS-485 port. */
// Customize this file for your specific hardware /* Customize this file for your specific hardware */
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
@@ -43,18 +43,18 @@
#include "mstp.h" #include "mstp.h"
// Transmits a Frame on the wire /* Transmits a Frame on the wire */
void RS485_Send_Frame(struct mstp_port_struct_t *mstp_port, // port specific data void RS485_Send_Frame(struct mstp_port_struct_t *mstp_port, /* port specific data */
uint8_t * buffer, // frame to send (up to 501 bytes of data) uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
uint16_t nbytes) // number of bytes of data (up to 501) uint16_t nbytes)
{ { /* number of bytes of data (up to 501) */
// in order to avoid line contention /* in order to avoid line contention */
while (mstp_port->Turn_Around_Waiting) { while (mstp_port->Turn_Around_Waiting) {
// wait, yield, or whatever /* wait, yield, or whatever */
} }
// Disable the receiver, and enable the transmit line driver. /* Disable the receiver, and enable the transmit line driver. */
while (nbytes) { while (nbytes) {
putc(*buffer, stderr); putc(*buffer, stderr);
@@ -62,32 +62,32 @@ void RS485_Send_Frame(struct mstp_port_struct_t *mstp_port, // port specific
nbytes--; nbytes--;
} }
// Wait until the final stop bit of the most significant CRC octet /* Wait until the final stop bit of the most significant CRC octet */
// has been transmitted but not more than Tpostdrive. /* has been transmitted but not more than Tpostdrive. */
// Disable the transmit line driver. /* Disable the transmit line driver. */
return; return;
} }
// called by timer, interrupt(?) or other thread /* called by timer, interrupt(?) or other thread */
void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port) void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port)
{ {
if (mstp_port->ReceiveError == true) { if (mstp_port->ReceiveError == true) {
// wait for state machine to clear this /* wait for state machine to clear this */
} }
// wait for state machine to read from the DataRegister /* wait for state machine to read from the DataRegister */
else if (mstp_port->DataAvailable == false) { else if (mstp_port->DataAvailable == false) {
// check for data /* check for data */
// if error, /* if error, */
// ReceiveError = TRUE; /* ReceiveError = TRUE; */
// return; /* return; */
mstp_port->DataRegister = 0; // FIXME: Get this data from UART or buffer mstp_port->DataRegister = 0; /* FIXME: Get this data from UART or buffer */
// if data is ready, /* if data is ready, */
// DataAvailable = TRUE; /* DataAvailable = TRUE; */
// return; /* return; */
} }
} }
+11 -11
View File
@@ -115,13 +115,13 @@
#define SHIFTREG_CLK PORTCbits.RC1 #define SHIFTREG_CLK PORTCbits.RC1
#define SHIFTREG_DATA PORTCbits.RC0 #define SHIFTREG_DATA PORTCbits.RC0
#define NO_ANALOGS 0x06 // None #define NO_ANALOGS 0x06 /* None */
#define ALL_ANALOG 0x00 // RA0 RA1 RA2 RA3 RA5 RE0 RE1 RE2 Ref=Vdd #define ALL_ANALOG 0x00 /* RA0 RA1 RA2 RA3 RA5 RE0 RE1 RE2 Ref=Vdd */
#define ANALOG_RA3_REF 0x01 // RA0 RA1 RA2 RA5 RE0 RE1 RE2 Ref=RA3 #define ANALOG_RA3_REF 0x01 /* RA0 RA1 RA2 RA5 RE0 RE1 RE2 Ref=RA3 */
#define A_ANALOG 0x02 // RA0 RA1 RA2 RA3 RA5 Ref=Vdd #define A_ANALOG 0x02 /* RA0 RA1 RA2 RA3 RA5 Ref=Vdd */
#define A_ANALOG_RA3_REF 0x03 // RA0 RA1 RA2 RA5 Ref=RA3 #define A_ANALOG_RA3_REF 0x03 /* RA0 RA1 RA2 RA5 Ref=RA3 */
#define RA0_RA1_RA3_ANALOG 0x04 // RA0 RA1 RA3 Ref=Vdd #define RA0_RA1_RA3_ANALOG 0x04 /* RA0 RA1 RA3 Ref=Vdd */
#define RA0_RA1_ANALOG_RA3_REF 0x05 // RA0 RA1 Ref=RA3 #define RA0_RA1_ANALOG_RA3_REF 0x05 /* RA0 RA1 Ref=RA3 */
#define ANALOG_RA3_RA2_REF 0x08 #define ANALOG_RA3_RA2_REF 0x08
#define ANALOG_NOT_RE1_RE2 0x09 #define ANALOG_NOT_RE1_RE2 0x09
@@ -132,7 +132,7 @@
#define RA0_ANALOG 0x0E #define RA0_ANALOG 0x0E
#define RA0_ANALOG_RA3_RA2_REF 0x0F #define RA0_ANALOG_RA3_RA2_REF 0x0F
// Constants used for SETUP_ADC() are: /* Constants used for SETUP_ADC() are: */
#define ADC_OFF 0 #define ADC_OFF 0
#define ADC_START 4 #define ADC_START 4
#define ADC_CLOCK_DIV_2 1 #define ADC_CLOCK_DIV_2 1
@@ -242,9 +242,9 @@
#define USART_TRANSMIT(x) TXREG = (x) #define USART_TRANSMIT(x) TXREG = (x)
#define USART_RECEIVE() RCREG #define USART_RECEIVE() RCREG
#define USART_RX_FRAME_ERROR() rcstabits.FERR #define USART_RX_FRAME_ERROR() rcstabits.FERR
// combine the sequence correctly /* combine the sequence correctly */
#define USART_RX_SETUP() PIE1bits.RCIE = 1; RCSTAbits.CREN = 1 #define USART_RX_SETUP() PIE1bits.RCIE = 1; RCSTAbits.CREN = 1
#define USART_TX_SETUP() PIE1bits.TXIE = 1; TXSTAbits.TXEN = 1 #define USART_TX_SETUP() PIE1bits.TXIE = 1; TXSTAbits.TXEN = 1
#endif /* HARDWARE_H */ #endif /* HARDWARE_H */
+48 -49
View File
@@ -29,10 +29,10 @@
#include <stdint.h> #include <stdint.h>
#include "hardware.h" #include "hardware.h"
// define this to enable ICD /* define this to enable ICD */
//#define USE_ICD /*#define USE_ICD */
// Configuration Bits /* Configuration Bits */
#pragma config OSC = HS #pragma config OSC = HS
#pragma config PWRT = ON #pragma config PWRT = ON
#pragma config BOR = ON, BORV = 42 #pragma config BOR = ON, BORV = 42
@@ -59,12 +59,12 @@
#pragma config EBTRB = OFF #pragma config EBTRB = OFF
#ifdef USE_ICD #ifdef USE_ICD
#pragma config WDT = OFF, WDTPS = 128 #pragma config WDT = OFF, WDTPS = 128
#pragma config DEBUG = ON #pragma config DEBUG = ON
#else #else
#pragma config WDT = ON, WDTPS = 128 #pragma config WDT = ON, WDTPS = 128
#pragma config DEBUG = OFF #pragma config DEBUG = OFF
#endif /* USE_ICD */ #endif /* USE_ICD */
#pragma romdata #pragma romdata
@@ -76,56 +76,55 @@
*****************************************************************************/ *****************************************************************************/
void init_hardware(void) void init_hardware(void)
{ {
// If the processor gets a power on reset then we can do something. /* If the processor gets a power on reset then we can do something. */
// We should not get a reset unless there has been /* We should not get a reset unless there has been */
// some kind of power line disturbance. /* some kind of power line disturbance. */
if (RCONbits.POR) if (RCONbits.POR) {
{ /*do something special! */
//do something special! }
}
GLOBAL_INT_DISABLE(); GLOBAL_INT_DISABLE();
/* Setup PORT A */ /* Setup PORT A */
TRISA=PORT_A_TRIS_MASK; TRISA = PORT_A_TRIS_MASK;
/* PORT A can have analog inputs or digital IO */ /* PORT A can have analog inputs or digital IO */
ADCON1 = NO_ANALOGS; ADCON1 = NO_ANALOGS;
/* Setup PORT B */ /* Setup PORT B */
TRISB=PORT_B_TRIS_MASK; TRISB = PORT_B_TRIS_MASK;
/* Setup PORT C */ /* Setup PORT C */
TRISC=PORT_C_TRIS_MASK; TRISC = PORT_C_TRIS_MASK;
/* setup zero cross interrupt to trigger on a low to high edge */ /* setup zero cross interrupt to trigger on a low to high edge */
INTCON2bits.INTEDG0 = 1; INTCON2bits.INTEDG0 = 1;
// setup ABUS /* setup ABUS */
//ABUS_LED_OFF(); /*ABUS_LED_OFF(); */
//SSPADD = ABUS_DEFAULT_ADDR; /*SSPADD = ABUS_DEFAULT_ADDR; */
//SSPCON1 = (ABUS_SLAVE_MASK | ABUS_CLK_ENABLE | ABUS_MODE_SETUP); /*SSPCON1 = (ABUS_SLAVE_MASK | ABUS_CLK_ENABLE | ABUS_MODE_SETUP); */
//ABUS_Clear_SSPBUF(); /*ABUS_Clear_SSPBUF(); */
// setup timer2 to reset every 1ms /* setup timer2 to reset every 1ms */
CloseTimer2(); CloseTimer2();
PR2 = 250; PR2 = 250;
OpenTimer2(T2_PS_1_4 & T2_POST_1_5 & 0x7F); OpenTimer2(T2_PS_1_4 & T2_POST_1_5 & 0x7F);
// Setup our interrupt priorities ---------> all low priority /* Setup our interrupt priorities ---------> all low priority */
RCONbits.IPEN = 1; RCONbits.IPEN = 1;
IPR1 = 0; IPR1 = 0;
IPR2 = 0; IPR2 = 0;
INTCON2bits.TMR0IP = 0; INTCON2bits.TMR0IP = 0;
INTCON2bits.RBIP = 0; INTCON2bits.RBIP = 0;
INTCON3 = 0; INTCON3 = 0;
/* Enable interrupts */ /* Enable interrupts */
TIMER2_INT_ENABLE(); TIMER2_INT_ENABLE();
PERIPHERAL_INT_ENABLE(); PERIPHERAL_INT_ENABLE();
GLOBAL_INT_ENABLE(); GLOBAL_INT_ENABLE();
// Turn on the Zero cross interrupt /* Turn on the Zero cross interrupt */
INTCONbits.INT0F = 0; INTCONbits.INT0F = 0;
INTCONbits.INT0E = 1; INTCONbits.INT0E = 1;
} }
+82 -98
View File
@@ -31,7 +31,7 @@
#include "hardware.h" #include "hardware.h"
#include "timer.h" #include "timer.h"
// interrupt service routines /* interrupt service routines */
extern void RS485_Receive_Interrupt(void); extern void RS485_Receive_Interrupt(void);
extern void RS485_Transmit_Interrupt(void); extern void RS485_Transmit_Interrupt(void);
@@ -45,76 +45,65 @@ extern void RS485_Transmit_Interrupt(void);
#pragma interruptlow InterruptHandlerLow save=PROD, section(".tmpdata") #pragma interruptlow InterruptHandlerLow save=PROD, section(".tmpdata")
void InterruptHandlerLow(void) void InterruptHandlerLow(void)
{ {
// check for timer0 interrupt /* check for timer0 interrupt */
if((INTCONbits.TMR0IF) && (INTCONbits.TMR0IE)) if ((INTCONbits.TMR0IF) && (INTCONbits.TMR0IE)) {
{ /* clear interrupt flag */
// clear interrupt flag INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IF = 0; /* call interrupt handler */
// call interrupt handler }
} /*check for timer1 interrupt */
//check for timer1 interrupt if ((PIR1bits.TMR1IF) && (PIE1bits.TMR1IE)) {
if ((PIR1bits.TMR1IF) && (PIE1bits.TMR1IE)) PIR1bits.TMR1IF = 0;
{ /* call interrupt handler */
PIR1bits.TMR1IF = 0; }
// call interrupt handler /*check for timer2 interrupt */
} if ((PIR1bits.TMR2IF) && (PIE1bits.TMR2IE)) {
//check for timer2 interrupt PIR1bits.TMR2IF = 0;
if ((PIR1bits.TMR2IF) && (PIE1bits.TMR2IE)) Timer_Millisecond_Interrupt();
{ }
PIR1bits.TMR2IF = 0; /*check for timer3 interrupt */
Timer_Millisecond_Interrupt(); if ((PIR2bits.TMR3IF) && (PIE2bits.TMR3IE)) {
} PIR2bits.TMR3IF = 0;
//check for timer3 interrupt /* call interrupt handler */
if ((PIR2bits.TMR3IF) && (PIE2bits.TMR3IE)) }
{ /*check for compare 1 int */
PIR2bits.TMR3IF = 0; if ((PIR1bits.CCP1IF) && (PIE1bits.CCP1IE)) {
// call interrupt handler PIR1bits.CCP1IF = 0;
} /* call interrupt handler */
//check for compare 1 int }
if ((PIR1bits.CCP1IF) && (PIE1bits.CCP1IE)) /*check for compare 2 int */
{ if ((PIR2bits.CCP2IF) && (PIE2bits.CCP2IE)) {
PIR1bits.CCP1IF = 0; PIR2bits.CCP2IF = 0;
// call interrupt handler /* call interrupt handler */
} }
//check for compare 2 int /*check for USART Rx int */
if ((PIR2bits.CCP2IF) && (PIE2bits.CCP2IE)) if ((PIR2bits.EEIF) && (PIE2bits.EEIE)) {
{ PIR2bits.EEIF = 0; /*clear interrupt flag */
PIR2bits.CCP2IF = 0; EECON1bits.WREN = 0; /* disable writes */
// call interrupt handler /* call interrupt handler */
} }
//check for USART Rx int /*check for USART Tx int */
if ((PIR2bits.EEIF) && (PIE2bits.EEIE)) if ((PIR1bits.TXIF) && (PIE1bits.TXIE)) {
{ /* call interrupt handler */
PIR2bits.EEIF = 0; //clear interrupt flag RS485_Transmit_Interrupt();
EECON1bits.WREN = 0; // disable writes }
// call interrupt handler /*check for USART Rx int */
} if ((PIR1bits.RCIF) && (PIE1bits.RCIE)) {
//check for USART Tx int /* call interrupt handler */
if ((PIR1bits.TXIF) && (PIE1bits.TXIE)) RS485_Receive_Interrupt();
{ }
// call interrupt handler /*check for AD int */
RS485_Transmit_Interrupt(); if ((PIR1bits.ADIF) && (PIE1bits.ADIE)) {
} /* call interrupt handler */
//check for USART Rx int PIR1bits.ADIF = 0;
if ((PIR1bits.RCIF) && (PIE1bits.RCIE)) }
{ /*check for I2C receive int (MSSP int) */
// call interrupt handler if ((PIR1bits.SSPIF) && (PIE1bits.SSPIE)) {
RS485_Receive_Interrupt(); PIR1bits.SSPIF = 0;
} /* call interrupt handler */
//check for AD int }
if ((PIR1bits.ADIF) && (PIE1bits.ADIE))
{
// call interrupt handler
PIR1bits.ADIF = 0;
}
//check for I2C receive int (MSSP int)
if ((PIR1bits.SSPIF) && (PIE1bits.SSPIE))
{
PIR1bits.SSPIF = 0;
// call interrupt handler
}
return; return;
} }
/**************************************************************************** /****************************************************************************
@@ -128,26 +117,24 @@ void InterruptHandlerLow(void)
#pragma interrupt InterruptHandlerHigh #pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh(void) void InterruptHandlerHigh(void)
{ {
//check for external int /*check for external int */
if ((INTCONbits.INT0IF) && (INTCONbits.INT0IE)) if ((INTCONbits.INT0IF) && (INTCONbits.INT0IE)) {
{ /* Test to ensure that we are not getting a false trigger on the
/* Test to ensure that we are not getting a false trigger on the falling edge. Only trigger on Rising edge. */
falling edge. Only trigger on Rising edge. */ if (ZERO_CROSS) {
if (ZERO_CROSS) /* timer used to determine when power is gone (no zero crosses) */
{ /* Power_Timeout = 30; */
// timer used to determine when power is gone (no zero crosses) /* if (ABUS_Current_Status.Zerox_Fail) */
// Power_Timeout = 30; /* { */
// if (ABUS_Current_Status.Zerox_Fail) /* ABUS_Flags.SendStatus = TRUE; */
// { /* ABUS_Current_Status.Zerox_Fail = FALSE; */
// ABUS_Flags.SendStatus = TRUE; /* } */
// ABUS_Current_Status.Zerox_Fail = FALSE; /* if we get here it means power is good */
// } /* System_Flags.PowerFail = FALSE; */
// if we get here it means power is good }
// System_Flags.PowerFail = FALSE; INTCONbits.INT0IF = 0;
} }
INTCONbits.INT0IF = 0; return;
}
return;
} }
/**************************************************************************** /****************************************************************************
@@ -160,10 +147,9 @@ void InterruptHandlerHigh(void)
#pragma code InterruptVectorHigh = 0x08 #pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh(void) void InterruptVectorHigh(void)
{ {
_asm _asm goto InterruptHandlerHigh /*jump to interrupt routine */
goto InterruptHandlerHigh //jump to interrupt routine _endasm}
_endasm
}
#pragma code #pragma code
/**************************************************************************** /****************************************************************************
@@ -176,9 +162,7 @@ void InterruptVectorHigh(void)
#pragma code InterruptVectorLow = 0x18 #pragma code InterruptVectorLow = 0x18
void InterruptVectorLow(void) void InterruptVectorLow(void)
{ {
_asm _asm goto InterruptHandlerLow /*jump to interrupt routine */
goto InterruptHandlerLow //jump to interrupt routine _endasm}
_endasm
}
#pragma code
#pragma code
+26 -28
View File
@@ -34,8 +34,8 @@
#include "timer.h" #include "timer.h"
#include "hardware.h" #include "hardware.h"
volatile struct mstp_port_struct_t MSTP_Port; // port data volatile struct mstp_port_struct_t MSTP_Port; /* port data */
static uint8_t MSTP_MAC_Address = 0x05; // local MAC address static uint8_t MSTP_MAC_Address = 0x05; /* local MAC address */
/**************************************************************************** /****************************************************************************
* DESCRIPTION: Handles our calling our module level milisecond counters * DESCRIPTION: Handles our calling our module level milisecond counters
@@ -46,40 +46,38 @@ static uint8_t MSTP_MAC_Address = 0x05; // local MAC address
*****************************************************************************/ *****************************************************************************/
static void Check_Timer_Milliseconds(void) static void Check_Timer_Milliseconds(void)
{ {
// We might have missed some so keep doing it until we have got them all /* We might have missed some so keep doing it until we have got them all */
while (Milliseconds) while (Milliseconds) {
{ MSTP_Millisecond_Timer(&MSTP_Port);
MSTP_Millisecond_Timer(&MSTP_Port); Milliseconds--;
Milliseconds--; }
}
} }
void main(void) void main(void)
{ {
init_hardware(); init_hardware();
RS485_Initialize(); RS485_Initialize();
MSTP_Init(&MSTP_Port,MSTP_MAC_Address); MSTP_Init(&MSTP_Port, MSTP_MAC_Address);
// loop forever /* loop forever */
for (;;) for (;;) {
{ WATCHDOG_TIMER();
WATCHDOG_TIMER();
// input /* input */
Check_Timer_Milliseconds(); Check_Timer_Milliseconds();
// note: also called by RS-485 Receive ISR /* note: also called by RS-485 Receive ISR */
RS485_Check_UART_Data(&MSTP_Port); RS485_Check_UART_Data(&MSTP_Port);
MSTP_Receive_Frame_FSM(&MSTP_Port); MSTP_Receive_Frame_FSM(&MSTP_Port);
// process /* process */
// output /* output */
RS485_Process_Tx_Message(); RS485_Process_Tx_Message();
MSTP_Master_Node_FSM(&MSTP_Port); MSTP_Master_Node_FSM(&MSTP_Port);
} }
return; return;
} }
+222 -243
View File
@@ -23,63 +23,60 @@
* *
*********************************************************************/ *********************************************************************/
// The module handles sending data out the RS-485 port /* The module handles sending data out the RS-485 port */
// and handles receiving data from the RS-485 port. /* and handles receiving data from the RS-485 port. */
// Customize this file for your specific hardware /* Customize this file for your specific hardware */
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include "hardware.h" #include "hardware.h"
#include "mstp.h" #include "mstp.h"
// public port info /* public port info */
extern volatile struct mstp_port_struct_t MSTP_Port; extern volatile struct mstp_port_struct_t MSTP_Port;
static uint32_t RS485_Baud_Rate = 9600; static uint32_t RS485_Baud_Rate = 9600;
// UART transmission buffer and index /* UART transmission buffer and index */
static volatile uint8_t RS485_Tx_Buffer[MAX_MPDU]; static volatile uint8_t RS485_Tx_Buffer[MAX_MPDU];
static volatile uint8_t RS485_Tx_Index = 0; static volatile uint8_t RS485_Tx_Index = 0;
static volatile uint8_t RS485_Tx_Length = 0; static volatile uint8_t RS485_Tx_Length = 0;
static volatile char RS485_Tx_Postdrive_Delay = 0; static volatile char RS485_Tx_Postdrive_Delay = 0;
static struct static struct {
{ unsigned TransmitStart:1; /* TRUE if we are requested to transmit */
unsigned TransmitStart:1; // TRUE if we are requested to transmit unsigned TransmitComplete:1; /* TRUE if we are finished transmitting frame */
unsigned TransmitComplete:1; // TRUE if we are finished transmitting frame
} RS485_Flags; } RS485_Flags;
// Duplicate of the RCSTA reg used due to the double buffering of the /* Duplicate of the RCSTA reg used due to the double buffering of the */
// fifo. Reading the RCREG reg will cause the second RCSTA reg to be /* fifo. Reading the RCREG reg will cause the second RCSTA reg to be */
// loaded if there is one. */ /* loaded if there is one. */ */
struct _rcstabits struct _rcstabits {
{ unsigned char RX9D:1;
unsigned char RX9D:1; unsigned char OERR:1;
unsigned char OERR:1; unsigned char FERR:1;
unsigned char FERR:1; unsigned char ADDEN:1;
unsigned char ADDEN:1; unsigned char CREN:1;
unsigned char CREN:1; unsigned char SREN:1;
unsigned char SREN:1; unsigned char RX9:1;
unsigned char RX9:1; unsigned char SPEN:1;
unsigned char SPEN:1;
}; };
volatile static enum volatile static enum {
{ RS485_STATE_IDLE = 0,
RS485_STATE_IDLE = 0, RS485_STATE_RX_DATA = 1,
RS485_STATE_RX_DATA = 1, RS485_STATE_RX_CHECKSUM = 2,
RS485_STATE_RX_CHECKSUM = 2, RS485_STATE_RX_PROCESS = 3,
RS485_STATE_RX_PROCESS = 3, RS485_STATE_TX_DATA = 4,
RS485_STATE_TX_DATA = 4, RS485_STATE_WAIT_FOR_ACK = 5,
RS485_STATE_WAIT_FOR_ACK = 5, RS485_STATE_WAIT_COMPLETE = 6,
RS485_STATE_WAIT_COMPLETE = 6, RS485_STATE_TX_GLOBAL_ACK = 7,
RS485_STATE_TX_GLOBAL_ACK = 7, RS485_STATE_TX_POSTDRIVE_DELAY = 8,
RS485_STATE_TX_POSTDRIVE_DELAY = 8, RS485_STATE_ERROR = 9,
RS485_STATE_ERROR = 9, RS485_STATE_RX_TEST = 10,
RS485_STATE_RX_TEST = 10, RS485_STATE_RX_TEST_EEPROM = 11,
RS485_STATE_RX_TEST_EEPROM = 11, RS485_STATE_RX_TEST_DELAY = 12,
RS485_STATE_RX_TEST_DELAY = 12, RS485_STATE_TX_TEST_WAIT = 13,
RS485_STATE_TX_TEST_WAIT = 13, RS485_STATE_TX_TEST = 14
RS485_STATE_TX_TEST = 14
} RS485_State; } RS485_State;
/**************************************************************************** /****************************************************************************
@@ -88,45 +85,42 @@ volatile static enum
* ALGORITHM: none * ALGORITHM: none
* NOTES: none * NOTES: none
*****************************************************************************/ *****************************************************************************/
void RS485_Send_Frame( void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */
volatile struct mstp_port_struct_t *mstp_port, // port specific data uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
uint8_t *buffer, // frame to send (up to 501 bytes of data) uint16_t nbytes)
uint16_t nbytes) // number of bytes of data (up to 501) { /* number of bytes of data (up to 501) */
{ /* do we check for tx buffer in-use? */
// do we check for tx buffer in-use? /* Or do we just stop the in-progress transmission? */
// Or do we just stop the in-progress transmission? /* Drop any transmission in progress, but don't worry about */
// Drop any transmission in progress, but don't worry about /* cleaning up the hardware - the start routine will handle that */
// cleaning up the hardware - the start routine will handle that
// Disable the interrupt since it depends on the global transmit buffer. /* Disable the interrupt since it depends on the global transmit buffer. */
USART_TX_INT_DISABLE(); USART_TX_INT_DISABLE();
switch (RS485_State) switch (RS485_State) {
{
case RS485_STATE_TX_DATA: case RS485_STATE_TX_DATA:
case RS485_STATE_WAIT_FOR_ACK: case RS485_STATE_WAIT_FOR_ACK:
case RS485_STATE_WAIT_COMPLETE: case RS485_STATE_WAIT_COMPLETE:
case RS485_STATE_TX_GLOBAL_ACK: case RS485_STATE_TX_GLOBAL_ACK:
RS485_State = RS485_STATE_IDLE; RS485_State = RS485_STATE_IDLE;
break; break;
} }
// load the frame
RS485_Tx_Length = 0;
while (buffer && nbytes)
{
RS485_Tx_Buffer[RS485_Tx_Length] = *buffer;
buffer++;
nbytes--;
RS485_Tx_Length++;
// check bounds - should this error be indicated somehow?
// perhaps not send the message?
if (RS485_Tx_Length >= MAX_MPDU)
break;
}
// signal the task to start sending when it is ready
RS485_Flags.TransmitStart = TRUE;
return; /* load the frame */
RS485_Tx_Length = 0;
while (buffer && nbytes) {
RS485_Tx_Buffer[RS485_Tx_Length] = *buffer;
buffer++;
nbytes--;
RS485_Tx_Length++;
/* check bounds - should this error be indicated somehow? */
/* perhaps not send the message? */
if (RS485_Tx_Length >= MAX_MPDU)
break;
}
/* signal the task to start sending when it is ready */
RS485_Flags.TransmitStart = TRUE;
return;
} }
/**************************************************************************** /****************************************************************************
@@ -137,55 +131,49 @@ void RS485_Send_Frame(
*****************************************************************************/ *****************************************************************************/
void RS485_Transmit_Interrupt(void) void RS485_Transmit_Interrupt(void)
{ {
uint8_t data; // data byte to send uint8_t data; /* data byte to send */
switch (RS485_State) switch (RS485_State) {
{
case RS485_STATE_TX_DATA: case RS485_STATE_TX_DATA:
RS485_Tx_Index++; RS485_Tx_Index++;
if (RS485_Tx_Index < RS485_Tx_Length) if (RS485_Tx_Index < RS485_Tx_Length) {
{ data = RS485_Tx_Buffer[RS485_Tx_Index];
data = RS485_Tx_Buffer[RS485_Tx_Index]; USART_TRANSMIT(data);
USART_TRANSMIT(data); MSTP_Port.SilenceTimer = 0;
MSTP_Port.SilenceTimer = 0; } else {
} /* wait until the last bit is sent */
else while (!USART_TX_EMPTY());
{ RS485_TRANSMIT_DISABLE();
// wait until the last bit is sent /* wait 2 characters after sending (min=15 bit times) */
while (!USART_TX_EMPTY()); RS485_Tx_Postdrive_Delay = 2;
RS485_TRANSMIT_DISABLE(); RS485_State = RS485_STATE_TX_POSTDRIVE_DELAY;
// wait 2 characters after sending (min=15 bit times) USART_TRANSMIT(0);
RS485_Tx_Postdrive_Delay = 2; }
RS485_State = RS485_STATE_TX_POSTDRIVE_DELAY; break;
USART_TRANSMIT(0);
}
break;
case RS485_STATE_TX_POSTDRIVE_DELAY: case RS485_STATE_TX_POSTDRIVE_DELAY:
// after the message is sent, we wait a certain /* after the message is sent, we wait a certain */
// number of character times to get a delay /* number of character times to get a delay */
if (RS485_Tx_Postdrive_Delay) if (RS485_Tx_Postdrive_Delay) {
{ RS485_Tx_Postdrive_Delay--;
RS485_Tx_Postdrive_Delay--; if (RS485_Tx_Postdrive_Delay == 0)
if (RS485_Tx_Postdrive_Delay == 0) RS485_State = RS485_STATE_WAIT_COMPLETE;
RS485_State = RS485_STATE_WAIT_COMPLETE; USART_TRANSMIT(0);
USART_TRANSMIT(0); } else
} RS485_State = RS485_STATE_WAIT_COMPLETE;
else break;
RS485_State = RS485_STATE_WAIT_COMPLETE;
break;
case RS485_STATE_WAIT_COMPLETE: case RS485_STATE_WAIT_COMPLETE:
// wait until the last delay bit is shifted /* wait until the last delay bit is shifted */
while (!USART_TX_EMPTY()); while (!USART_TX_EMPTY());
USART_TX_INT_DISABLE(); USART_TX_INT_DISABLE();
RS485_Flags.TransmitComplete = TRUE; RS485_Flags.TransmitComplete = TRUE;
RS485_State = RS485_STATE_IDLE; RS485_State = RS485_STATE_IDLE;
USART_RX_SETUP(); USART_RX_SETUP();
break; break;
default: default:
break; break;
} }
return; return;
} }
/**************************************************************************** /****************************************************************************
@@ -196,28 +184,27 @@ void RS485_Transmit_Interrupt(void)
*****************************************************************************/ *****************************************************************************/
void RS485_Process_Tx_Message(void) void RS485_Process_Tx_Message(void)
{ {
if (RS485_Flags.TransmitComplete) if (RS485_Flags.TransmitComplete)
RS485_Flags.TransmitComplete = FALSE; RS485_Flags.TransmitComplete = FALSE;
// start a new transmisstion if we are ready /* start a new transmisstion if we are ready */
if (RS485_Flags.TransmitStart && (RS485_State == RS485_STATE_IDLE)) if (RS485_Flags.TransmitStart && (RS485_State == RS485_STATE_IDLE)) {
{ /* Disable the receiver */
// Disable the receiver USART_RX_INT_DISABLE();
USART_RX_INT_DISABLE(); USART_CONTINUOUS_RX_DISABLE();
USART_CONTINUOUS_RX_DISABLE(); /* Enable the transmit line driver and interrupts */
// Enable the transmit line driver and interrupts RS485_TRANSMIT_ENABLE();
RS485_TRANSMIT_ENABLE(); RS485_State = RS485_STATE_TX_DATA;
RS485_State = RS485_STATE_TX_DATA; /* Configure the ISR handler for an outgoing message */
// Configure the ISR handler for an outgoing message RS485_Tx_Index = 0;
RS485_Tx_Index = 0; /* update the flags for beginning a send */
// update the flags for beginning a send RS485_Flags.TransmitComplete = FALSE;
RS485_Flags.TransmitComplete = FALSE; RS485_Flags.TransmitStart = FALSE;
RS485_Flags.TransmitStart = FALSE; /* send the first byte */
// send the first byte USART_TRANSMIT(RS485_Tx_Buffer[0]);
USART_TRANSMIT(RS485_Tx_Buffer[0]); USART_TX_SETUP();
USART_TX_SETUP(); }
}
return;
return;
} }
/**************************************************************************** /****************************************************************************
@@ -228,40 +215,36 @@ void RS485_Process_Tx_Message(void)
*****************************************************************************/ *****************************************************************************/
void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port)
{ {
struct _rcstabits rcstabits; // reading it more than once gets wrong data struct _rcstabits rcstabits; /* reading it more than once gets wrong data */
// check for data /* check for data */
if (USART_RX_COMPLETE()) if (USART_RX_COMPLETE()) {
{ /* Read the data and the Rx status reg */
// Read the data and the Rx status reg rcstabits = USART_RX_STATUS();
rcstabits = USART_RX_STATUS(); mstp_port->DataRegister = USART_RECEIVE();
mstp_port->DataRegister = USART_RECEIVE();
// Check for buffer overrun error /* Check for buffer overrun error */
if (rcstabits.OERR) if (rcstabits.OERR) {
{ /* clear the error */
// clear the error USART_CONTINUOUS_RX_DISABLE();
USART_CONTINUOUS_RX_DISABLE(); USART_CONTINUOUS_RX_ENABLE();
USART_CONTINUOUS_RX_ENABLE(); /* let the state machine know */
// let the state machine know mstp_port->ReceiveError = TRUE;
mstp_port->ReceiveError = TRUE; }
/* Check for framing errors */
else if (USART_RX_FRAME_ERROR()) {
/* let the state machine know */
mstp_port->FramingError = TRUE;
mstp_port->ReceiveError = TRUE;
}
/* We read a good byte */
else {
/* state machine will clear this */
mstp_port->DataAvailable = TRUE;
}
} }
// Check for framing errors
else if (USART_RX_FRAME_ERROR())
{
// let the state machine know
mstp_port->FramingError = TRUE;
mstp_port->ReceiveError = TRUE;
}
// We read a good byte
else
{
// state machine will clear this
mstp_port->DataAvailable = TRUE;
}
}
return; return;
} }
/**************************************************************************** /****************************************************************************
@@ -272,17 +255,16 @@ void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port)
*****************************************************************************/ *****************************************************************************/
void RS485_Receive_Interrupt(void) void RS485_Receive_Interrupt(void)
{ {
// get as many bytes as we can get /* get as many bytes as we can get */
for (;;) for (;;) {
{ RS485_Check_UART_Data(&MSTP_Port);
RS485_Check_UART_Data(&MSTP_Port); if (MSTP_Port.ReceiveError || MSTP_Port.DataAvailable)
if (MSTP_Port.ReceiveError || MSTP_Port.DataAvailable) MSTP_Receive_Frame_FSM(&MSTP_Port);
MSTP_Receive_Frame_FSM(&MSTP_Port); else
else break;
break; }
}
return; return;
} }
/**************************************************************************** /****************************************************************************
@@ -293,7 +275,7 @@ void RS485_Receive_Interrupt(void)
*****************************************************************************/ *****************************************************************************/
uint32_t RS485_Get_Baud_Rate(void) uint32_t RS485_Get_Baud_Rate(void)
{ {
return RS485_Baud_Rate; return RS485_Baud_Rate;
} }
/**************************************************************************** /****************************************************************************
@@ -304,70 +286,69 @@ uint32_t RS485_Get_Baud_Rate(void)
*****************************************************************************/ *****************************************************************************/
void RS485_Set_Baud_Rate(uint32_t baud) void RS485_Set_Baud_Rate(uint32_t baud)
{ {
if (baud < 19200) if (baud < 19200)
RS485_Baud_Rate = 9600; RS485_Baud_Rate = 9600;
else if (baud < 38400) else if (baud < 38400)
RS485_Baud_Rate = 19200; RS485_Baud_Rate = 19200;
else if (baud < 57600) else if (baud < 57600)
RS485_Baud_Rate = 38400; RS485_Baud_Rate = 38400;
else if (baud < 57600) else if (baud < 57600)
RS485_Baud_Rate = 57600; RS485_Baud_Rate = 57600;
else if (baud < 115200) else if (baud < 115200)
RS485_Baud_Rate = 76800; RS485_Baud_Rate = 76800;
else else
RS485_Baud_Rate = 115200; RS485_Baud_Rate = 115200;
} }
void RS485_Initialize_Baud(void) void RS485_Initialize_Baud(void)
{ {
// setup USART Baud Rate Generator /* setup USART Baud Rate Generator */
// see BAUD RATES FOR ASYNCHRONOUS MODE in Data Book /* see BAUD RATES FOR ASYNCHRONOUS MODE in Data Book */
/* Fosc=20MHz /* Fosc=20MHz
BRGH=1 BRGH=0 BRGH=1 BRGH=0
Rate SPBRG Rate SPBRG Rate SPBRG Rate SPBRG
------- ----- ------- ----- ------- ----- ------- -----
9615 129 9469 32 9615 129 9469 32
19230 64 19530 15 19230 64 19530 15
37878 32 78130 3 37878 32 78130 3
56818 21 104200 2 56818 21 104200 2
113630 10 312500 0 113630 10 312500 0
250000 4 250000 4
625000 1 625000 1
1250000 0 1250000 0
*/ */
switch (RS485_Baud_Rate) switch (RS485_Baud_Rate) {
{
case 19200: case 19200:
SPBRG = 64; SPBRG = 64;
TXSTAbits.BRGH=1; TXSTAbits.BRGH = 1;
break; break;
case 38400: case 38400:
SPBRG = 32; SPBRG = 32;
TXSTAbits.BRGH=1; TXSTAbits.BRGH = 1;
break; break;
case 57600: case 57600:
SPBRG = 21; SPBRG = 21;
TXSTAbits.BRGH=1; TXSTAbits.BRGH = 1;
break; break;
case 76800: case 76800:
SPBRG = 3; SPBRG = 3;
TXSTAbits.BRGH=0; TXSTAbits.BRGH = 0;
break; break;
case 115200: case 115200:
SPBRG = 10; SPBRG = 10;
TXSTAbits.BRGH=1; TXSTAbits.BRGH = 1;
break; break;
case 9600: case 9600:
default: default:
SPBRG = 129; SPBRG = 129;
TXSTAbits.BRGH=1; TXSTAbits.BRGH = 1;
break; break;
} }
/* select async mode */ /* select async mode */
TXSTAbits.SYNC=0; TXSTAbits.SYNC = 0;
/* serial port enable */ /* serial port enable */
RCSTAbits.SPEN=1; RCSTAbits.SPEN = 1;
} }
/**************************************************************************** /****************************************************************************
@@ -379,17 +360,15 @@ void RS485_Initialize_Baud(void)
*****************************************************************************/ *****************************************************************************/
void RS485_Initialize(void) void RS485_Initialize(void)
{ {
RS485_Initialize_Baud(); RS485_Initialize_Baud();
/* configure interrupts */ /* configure interrupts */
USART_TX_INT_DISABLE(); USART_TX_INT_DISABLE();
USART_RX_INT_ENABLE(); USART_RX_INT_ENABLE();
// configure USART for receiving /* configure USART for receiving */
// since the TX will handle setting up for transmit /* since the TX will handle setting up for transmit */
USART_CONTINUOUS_RX_ENABLE(); USART_CONTINUOUS_RX_ENABLE();
/* since we are using RS485, /* since we are using RS485,
we need to explicitly say we need to explicitly say
transmit enable or not */ transmit enable or not */
RS485_TRANSMIT_DISABLE(); RS485_TRANSMIT_DISABLE();
} }
+14 -14
View File
@@ -1,28 +1,28 @@
#ifndef STDBOOL_H #ifndef STDBOOL_H
#define STDBOOL_H #define STDBOOL_H
// C99 Boolean types for compilers without C99 support /* C99 Boolean types for compilers without C99 support */
#ifndef __cplusplus #ifndef __cplusplus
typedef int _Bool; typedef int _Bool;
#ifndef bool #ifndef bool
#define bool _Bool #define bool _Bool
#endif #endif
#ifndef true #ifndef true
#define true 1 #define true 1
#endif #endif
#ifndef false #ifndef false
#define false 0 #define false 0
#endif #endif
#define __bool_true_false_are_defined 1 #define __bool_true_false_are_defined 1
#endif #endif
#ifndef FALSE #ifndef FALSE
#define FALSE 0 #define FALSE 0
#endif #endif
#ifndef TRUE #ifndef TRUE
#define TRUE 1 #define TRUE 1
#endif #endif
#endif #endif
+11 -11
View File
@@ -1,18 +1,18 @@
// Defines the standard integer types that are used in code /* Defines the standard integer types that are used in code */
#ifndef STDINT_H #ifndef STDINT_H
#define STDINT_H 1 #define STDINT_H 1
#include <stddef.h> #include <stddef.h>
typedef unsigned char uint8_t; // 1 byte 0 to 255 typedef unsigned char uint8_t; /* 1 byte 0 to 255 */
typedef signed char int8_t; // 1 byte -127 to 127 typedef signed char int8_t; /* 1 byte -127 to 127 */
typedef unsigned short uint16_t; // 2 bytes 0 to 65535 typedef unsigned short uint16_t; /* 2 bytes 0 to 65535 */
typedef signed short int16_t; // 2 bytes -32767 to 32767 typedef signed short int16_t; /* 2 bytes -32767 to 32767 */
//typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215 /*typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215 */
typedef unsigned long uint32_t; // 4 bytes 0 to 4294967295 typedef unsigned long uint32_t; /* 4 bytes 0 to 4294967295 */
typedef signed long int32_t; // 4 bytes -2147483647 to 2147483647 typedef signed long int32_t; /* 4 bytes -2147483647 to 2147483647 */
// typedef signed long long int64_t; /* typedef signed long long int64_t; */
// typedef unsigned long long uint64_t; /* typedef unsigned long long uint64_t; */
#endif // STDINT_H #endif /* STDINT_H */
+5 -5
View File
@@ -25,7 +25,7 @@
*********************************************************************/ *********************************************************************/
#include <stdint.h> #include <stdint.h>
volatile uint8_t Milliseconds; // used for timing stuff - counts up to 0xFF. volatile uint8_t Milliseconds; /* used for timing stuff - counts up to 0xFF. */
/**************************************************************************** /****************************************************************************
* DESCRIPTION: Timer is set to go off every 1ms. We increment the counter, * DESCRIPTION: Timer is set to go off every 1ms. We increment the counter,
@@ -37,9 +37,9 @@ volatile uint8_t Milliseconds; // used for timing stuff - counts up to 0xFF.
*****************************************************************************/ *****************************************************************************/
void Timer_Millisecond_Interrupt(void) void Timer_Millisecond_Interrupt(void)
{ {
// Global Milisecond timer /* Global Milisecond timer */
if (Milliseconds < 0xFF) if (Milliseconds < 0xFF)
Milliseconds++; Milliseconds++;
return; return;
} }
+1 -1
View File
@@ -26,7 +26,7 @@
#ifndef TIMER_H #ifndef TIMER_H
#define TIMER_H #define TIMER_H
// used for timing stuff - counts up to 0xFF. /* used for timing stuff - counts up to 0xFF. */
extern volatile uint8_t Milliseconds; extern volatile uint8_t Milliseconds;
void Timer_Millisecond_Interrupt(void); void Timer_Millisecond_Interrupt(void);
+41 -41
View File
@@ -32,12 +32,12 @@
------------------------------------------- -------------------------------------------
####COPYRIGHTEND####*/ ####COPYRIGHTEND####*/
#include <stdint.h> // for standard integer types uint8_t etc. #include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> // for the standard bool type. #include <stdbool.h> /* for the standard bool type. */
#include "bip.h" #include "bip.h"
#if (defined(BACDL_ETHERNET) || defined(BACDL_BIP)) #if (defined(BACDL_ETHERNET) || defined(BACDL_BIP))
static int interface = SOCKET_ERROR; // SOCKET_ERROR means no open interface static int interface = SOCKET_ERROR; /* SOCKET_ERROR means no open interface */
#endif #endif
void bip_set_interface(char *ifname) void bip_set_interface(char *ifname)
@@ -73,50 +73,50 @@ void InterfaceCleanup(void)
#endif #endif
static void NetInitialize(void) static void NetInitialize(void)
// initialize the TCP/IP stack /* initialize the TCP/IP stack */
{ {
int Result; int Result;
#ifndef HOST #ifndef HOST
RTKernelInit(0); // get the kernel going RTKernelInit(0); /* get the kernel going */
if (!RTKDebugVersion()) // switch of all diagnostics and error messages of RTIP-32 if (!RTKDebugVersion()) /* switch of all diagnostics and error messages of RTIP-32 */
xn_callbacks()->cb_wr_screen_string_fnc = NULL; xn_callbacks()->cb_wr_screen_string_fnc = NULL;
CLKSetTimerIntVal(10 * 1000); // 10 millisecond tick CLKSetTimerIntVal(10 * 1000); /* 10 millisecond tick */
RTKDelay(1); RTKDelay(1);
RTCMOSSetSystemTime(); // get the right time-of-day RTCMOSSetSystemTime(); /* get the right time-of-day */
#ifdef RTUSB_VER #ifdef RTUSB_VER
RTURegisterCallback(USBAX172); // ax172 and ax772 drivers RTURegisterCallback(USBAX172); /* ax172 and ax772 drivers */
RTURegisterCallback(USBAX772); RTURegisterCallback(USBAX772);
RTURegisterCallback(USBKeyboard); // support USB keyboards RTURegisterCallback(USBKeyboard); /* support USB keyboards */
FindUSBControllers(); // install USB host controllers FindUSBControllers(); /* install USB host controllers */
Sleep(2000); // give the USB stack time to enumerate devices Sleep(2000); /* give the USB stack time to enumerate devices */
#endif #endif
#ifdef DHCP #ifdef DHCP
XN_REGISTER_DHCP_CLI() // and optionally the DHCP client XN_REGISTER_DHCP_CLI() /* and optionally the DHCP client */
#endif #endif
Result = xn_rtip_init(); // Initialize the RTIP stack Result = xn_rtip_init(); /* Initialize the RTIP stack */
if (Result != 0) if (Result != 0)
Error("xn_rtip_init failed"); Error("xn_rtip_init failed");
atexit(InterfaceCleanup); // make sure the driver is shut down properly atexit(InterfaceCleanup); /* make sure the driver is shut down properly */
RTCallDebugger(RT_DBG_CALLRESET, (DWORD) exit, 0); // even if we get restarted by the debugger RTCallDebugger(RT_DBG_CALLRESET, (DWORD) exit, 0); /* even if we get restarted by the debugger */
Result = BIND_DRIVER(MINOR_0); // tell RTIP what Ethernet driver we want (see netcfg.h) Result = BIND_DRIVER(MINOR_0); /* tell RTIP what Ethernet driver we want (see netcfg.h) */
if (Result != 0) if (Result != 0)
Error("driver initialization failed"); Error("driver initialization failed");
#if DEVICE_ID == PRISM_PCMCIA_DEVICE #if DEVICE_ID == PRISM_PCMCIA_DEVICE
// if this is a PCMCIA device, start the PCMCIA driver /* if this is a PCMCIA device, start the PCMCIA driver */
if (RTPCInit(-1, 0, 2, NULL) == 0) if (RTPCInit(-1, 0, 2, NULL) == 0)
Error("No PCMCIA controller found"); Error("No PCMCIA controller found");
#endif #endif
// Open the interface /* Open the interface */
interface = interface =
xn_interface_open_config(DEVICE_ID, MINOR_0, ED_IO_ADD, ED_IRQ, xn_interface_open_config(DEVICE_ID, MINOR_0, ED_IO_ADD, ED_IRQ,
ED_MEM_ADD); ED_MEM_ADD);
@@ -143,29 +143,29 @@ static void NetInitialize(void)
} }
#if DEVICE_ID == PRISM_PCMCIA_DEVICE || DEVICE_ID == PRISM_DEVICE #if DEVICE_ID == PRISM_PCMCIA_DEVICE || DEVICE_ID == PRISM_DEVICE
xn_wlan_setup(interface, // iface_no: value returned by xn_interface_open_config() xn_wlan_setup(interface, /* iface_no: value returned by xn_interface_open_config() */
"network name", // SSID : network name set in the access point "network name", /* SSID : network name set in the access point */
"station name", // Name : name of this node "station name", /* Name : name of this node */
0, // Channel : 0 for access points, 1..14 for ad-hoc 0, /* Channel : 0 for access points, 1..14 for ad-hoc */
0, // KeyIndex: 0 .. 3 0, /* KeyIndex: 0 .. 3 */
"12345", // WEP Key : key to use (5 or 13 bytes) "12345", /* WEP Key : key to use (5 or 13 bytes) */
0); // Flags : see manual and Wlanapi.h for details 0); /* Flags : see manual and Wlanapi.h for details */
Sleep(1000); // wireless devices need a little time before they can be used Sleep(1000); /* wireless devices need a little time before they can be used */
#endif // WLAN device #endif /* WLAN device */
#if defined(AUTO_IP) // use xn_autoip() to get an IP address #if defined(AUTO_IP) /* use xn_autoip() to get an IP address */
Result = xn_autoip(interface, MinIP, MaxIP, NetMask, TargetIP); Result = xn_autoip(interface, MinIP, MaxIP, NetMask, TargetIP);
if (Result == SOCKET_ERROR) if (Result == SOCKET_ERROR)
Error("xn_autoip failed"); Error("xn_autoip failed");
else { else {
printf("Auto-assigned IP address %i.%i.%i.%i\n", TargetIP[0], printf("Auto-assigned IP address %i.%i.%i.%i\n", TargetIP[0],
TargetIP[1], TargetIP[2], TargetIP[3]); TargetIP[1], TargetIP[2], TargetIP[3]);
// define default gateway and DNS server /* define default gateway and DNS server */
xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface,
RT_INF); RT_INF);
xn_set_server_list((DWORD *) DNSServer, 1); xn_set_server_list((DWORD *) DNSServer, 1);
} }
#elif defined(DHCP) // use DHCP #elif defined(DHCP) /* use DHCP */
{ {
DHCP_param param[] = { {SUBNET_MASK, 1} DHCP_param param[] = { {SUBNET_MASK, 1}
, {DNS_OP, 1} , {DNS_OP, 1}
@@ -174,11 +174,11 @@ static void NetInitialize(void)
DHCP_session DS; DHCP_session DS;
DHCP_conf DC; DHCP_conf DC;
xn_init_dhcp_conf(&DC); // load default DHCP options xn_init_dhcp_conf(&DC); /* load default DHCP options */
DC.plist = param; // add MASK, DNS, and gateway options DC.plist = param; /* add MASK, DNS, and gateway options */
DC.plist_entries = sizeof(param) / sizeof(param[0]); DC.plist_entries = sizeof(param) / sizeof(param[0]);
printf("Contacting DHCP server, please wait...\n"); printf("Contacting DHCP server, please wait...\n");
Result = xn_dhcp(interface, &DS, &DC); // contact DHCP server Result = xn_dhcp(interface, &DS, &DC); /* contact DHCP server */
if (Result == SOCKET_ERROR) if (Result == SOCKET_ERROR)
Error("xn_dhcp failed"); Error("xn_dhcp failed");
memcpy(TargetIP, DS.client_ip, 4); memcpy(TargetIP, DS.client_ip, 4);
@@ -186,16 +186,16 @@ static void NetInitialize(void)
TargetIP[2], TargetIP[3]); TargetIP[2], TargetIP[3]);
} }
#else #else
// Set the IP address and interface /* Set the IP address and interface */
printf("Using static IP address %i.%i.%i.%i\n", TargetIP[0], printf("Using static IP address %i.%i.%i.%i\n", TargetIP[0],
TargetIP[1], TargetIP[2], TargetIP[3]); TargetIP[1], TargetIP[2], TargetIP[3]);
Result = xn_set_ip(interface, TargetIP, NetMask); Result = xn_set_ip(interface, TargetIP, NetMask);
// define default gateway and DNS server /* define default gateway and DNS server */
xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF); xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF);
xn_set_server_list((DWORD *) DNSServer, 1); xn_set_server_list((DWORD *) DNSServer, 1);
#endif #endif
#else // HOST defined, run on Windows #else /* HOST defined, run on Windows */
WSADATA wd; WSADATA wd;
Result = WSAStartup(0x0101, &wd); Result = WSAStartup(0x0101, &wd);
@@ -209,7 +209,7 @@ static void NetInitialize(void)
bool bip_init(void) bool bip_init(void)
{ {
int rv = 0; // return from socket lib calls int rv = 0; /* return from socket lib calls */
struct sockaddr_in sin = { -1 }; struct sockaddr_in sin = { -1 };
int value = 1; int value = 1;
int sock_fd = -1; int sock_fd = -1;
@@ -218,7 +218,7 @@ bool bip_init(void)
bip_set_address(TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]); bip_set_address(TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]);
// FIXME: /* FIXME: */
#if 0 #if 0
bip_set_address(NetMask[0], NetMask[1], NetMask[2], NetMask[3]); bip_set_address(NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
extern unsigned long bip_get_addr(void); extern unsigned long bip_get_addr(void);
@@ -232,13 +232,13 @@ bool bip_init(void)
bip_set_port(0xBAC0); bip_set_port(0xBAC0);
#endif #endif
// assumes that the driver has already been initialized /* assumes that the driver has already been initialized */
sock_fd = socket(AF_INET, SOCK_DGRAM, IPROTO_UDP); sock_fd = socket(AF_INET, SOCK_DGRAM, IPROTO_UDP);
bip_set_socket(sock_fd); bip_set_socket(sock_fd);
if (sock_fd < 0) if (sock_fd < 0)
return false; return false;
// bind the socket to the local port number and IP address /* bind the socket to the local port number and IP address */
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(bip_get_port()); sin.sin_port = htons(bip_get_port());
+54 -54
View File
@@ -23,10 +23,10 @@
* *
*********************************************************************/ *********************************************************************/
#include <stdint.h> // for standard integer types uint8_t etc. #include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> // for the standard bool type. #include <stdbool.h> /* for the standard bool type. */
#include <stdio.h> // for the standard bool type. #include <stdio.h> /* for the standard bool type. */
#include <stdlib.h> // for the standard bool type. #include <stdlib.h> /* for the standard bool type. */
#include <rttarget.h> #include <rttarget.h>
#include <rtk32.h> #include <rtk32.h>
#include <clock.h> #include <clock.h>
@@ -35,17 +35,17 @@
#include "ethernet.h" #include "ethernet.h"
#include "bacdcode.h" #include "bacdcode.h"
// commonly used comparison address for ethernet /* commonly used comparison address for ethernet */
uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = uint8_t Ethernet_Broadcast[MAX_MAC_LEN] =
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
// commonly used empty address for ethernet quick compare /* commonly used empty address for ethernet quick compare */
uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 }; uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
// my local device data - MAC address /* my local device data - MAC address */
uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 }; uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
static SOCKET Ethernet_Socket = -1; static SOCKET Ethernet_Socket = -1;
// used for binding 802.2 /* used for binding 802.2 */
static struct sockaddr Ethernet_Address = { 0 }; static struct sockaddr Ethernet_Address = { 0 };
bool ethernet_valid(void) bool ethernet_valid(void)
@@ -67,9 +67,9 @@ bool ethernet_init(char *interface_name)
int value = 1; int value = 1;
(void) interface_name; (void) interface_name;
// setup the socket /* setup the socket */
Ethernet_Socket = socket(AF_INET, SOCK_RAW, 0); Ethernet_Socket = socket(AF_INET, SOCK_RAW, 0);
//Ethernet_Socket = socket(AF_INET, SOCK_STREAM, 0); /*Ethernet_Socket = socket(AF_INET, SOCK_STREAM, 0); */
if (Ethernet_Socket < 0) if (Ethernet_Socket < 0)
fprintf(stderr, "ethernet: failed to bind to socket!\r\n"); fprintf(stderr, "ethernet: failed to bind to socket!\r\n");
Ethernet_Address.sa_family = AF_INET; Ethernet_Address.sa_family = AF_INET;
@@ -77,24 +77,24 @@ bool ethernet_init(char *interface_name)
if (bind(Ethernet_Socket, if (bind(Ethernet_Socket,
&Ethernet_Address, sizeof(Ethernet_Address)) == SOCKET_ERROR) &Ethernet_Address, sizeof(Ethernet_Address)) == SOCKET_ERROR)
fprintf(stderr, "ethernet: failed to bind to socket!\r\n"); fprintf(stderr, "ethernet: failed to bind to socket!\r\n");
//setsockopt(Ethernet_Socket,SOL_SOCKET,SO_802_2,(char *)&value,sizeof(value)); /*setsockopt(Ethernet_Socket,SOL_SOCKET,SO_802_2,(char *)&value,sizeof(value)); */
return ethernet_valid(); return ethernet_valid();
} }
/* function to send a packet out the 802.2 socket */ /* function to send a packet out the 802.2 socket */
/* returns bytes sent on success, negative number on failure */ /* returns bytes sent on success, negative number on failure */
int ethernet_send(BACNET_ADDRESS * dest, // destination address int ethernet_send(BACNET_ADDRESS * dest, /* destination address */
BACNET_ADDRESS * src, // source address BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, // any data to be sent - may be null uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len) // number of bytes of data unsigned pdu_len)
{ { /* number of bytes of data */
int bytes = 0; int bytes = 0;
uint8_t mtu[MAX_MPDU] = { 0 }; uint8_t mtu[MAX_MPDU] = { 0 };
int mtu_len = 0; int mtu_len = 0;
int i = 0; int i = 0;
// don't waste time if the socket is not valid /* don't waste time if the socket is not valid */
if (Ethernet_Socket < 0) { if (Ethernet_Socket < 0) {
fprintf(stderr, "ethernet: 802.2 socket is invalid!\n"); fprintf(stderr, "ethernet: 802.2 socket is invalid!\n");
return -1; return -1;
@@ -127,7 +127,7 @@ int ethernet_send(BACNET_ADDRESS * dest, // destination address
/* packet length */ /* packet length */
mtu_len += encode_unsigned16(&mtu[12], mtu_len += encode_unsigned16(&mtu[12],
3 /*DSAP,SSAP,LLC */ + pdu_len); 3 /*DSAP,SSAP,LLC */ + pdu_len);
// Logical PDU portion /* Logical PDU portion */
mtu[mtu_len++] = 0x82; /* DSAP for BACnet */ mtu[mtu_len++] = 0x82; /* DSAP for BACnet */
mtu[mtu_len++] = 0x82; /* SSAP for BACnet */ mtu[mtu_len++] = 0x82; /* SSAP for BACnet */
mtu[mtu_len++] = 0x03; /* Control byte in header */ mtu[mtu_len++] = 0x03; /* Control byte in header */
@@ -146,12 +146,12 @@ int ethernet_send(BACNET_ADDRESS * dest, // destination address
/* function to send a packet out the 802.2 socket */ /* function to send a packet out the 802.2 socket */
/* returns bytes sent on success, negative number on failure */ /* returns bytes sent on success, negative number on failure */
int ethernet_send_pdu(BACNET_ADDRESS * dest, // destination address int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */
uint8_t * pdu, // any data to be sent - may be null uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len) // number of bytes of data unsigned pdu_len)
{ { /* number of bytes of data */
int i = 0; // counter int i = 0; /* counter */
BACNET_ADDRESS src = { 0 }; // source address BACNET_ADDRESS src = { 0 }; /* source address */
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
src.mac[i] = Ethernet_MAC_Address[i]; src.mac[i] = Ethernet_MAC_Address[i];
@@ -159,22 +159,22 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, // destination address
} }
/* function to send a packet out the 802.2 socket */ /* function to send a packet out the 802.2 socket */
/* returns 1 on success, 0 on failure */ /* returns 1 on success, 0 on failure */
return ethernet_send(dest, // destination address return ethernet_send(dest, /* destination address */
&src, // source address &src, /* source address */
pdu, // any data to be sent - may be null pdu, /* any data to be sent - may be null */
pdu_len); // number of bytes of data pdu_len); /* number of bytes of data */
} }
// receives an 802.2 framed packet /* receives an 802.2 framed packet */
// returns the number of octets in the PDU, or zero on failure /* returns the number of octets in the PDU, or zero on failure */
uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, // PDU data uint8_t * pdu, /* PDU data */
uint16_t max_pdu, // amount of space available in the PDU uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout) // number of milliseconds to wait for a packet unsigned timeout)
{ { /* number of milliseconds to wait for a packet */
int received_bytes; int received_bytes;
uint8_t buf[MAX_MPDU] = { 0 }; // data uint8_t buf[MAX_MPDU] = { 0 }; /* data */
uint16_t pdu_len = 0; // return value uint16_t pdu_len = 0; /* return value */
fd_set read_fds; fd_set read_fds;
int max; int max;
struct timeval select_timeout; struct timeval select_timeout;
@@ -206,9 +206,9 @@ uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address
/* See if there is a problem */ /* See if there is a problem */
if (received_bytes < 0) { if (received_bytes < 0) {
// EAGAIN Non-blocking I/O has been selected /* EAGAIN Non-blocking I/O has been selected */
// using O_NONBLOCK and no data /* using O_NONBLOCK and no data */
// was immediately available for reading. /* was immediately available for reading. */
if (errno != EAGAIN) if (errno != EAGAIN)
fprintf(stderr, fprintf(stderr,
"ethernet: Read error in receiving packet: %s\n", "ethernet: Read error in receiving packet: %s\n",
@@ -221,28 +221,28 @@ uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address
/* the signature of an 802.2 BACnet packet */ /* the signature of an 802.2 BACnet packet */
if ((buf[14] != 0x82) && (buf[15] != 0x82)) { if ((buf[14] != 0x82) && (buf[15] != 0x82)) {
//fprintf(stderr,"ethernet: Non-BACnet packet\n"); /*fprintf(stderr,"ethernet: Non-BACnet packet\n"); */
return 0; return 0;
} }
// copy the source address /* copy the source address */
src->mac_len = 6; src->mac_len = 6;
memmove(src->mac, &buf[6], 6); memmove(src->mac, &buf[6], 6);
// check destination address for when /* check destination address for when */
// the Ethernet card is in promiscious mode /* the Ethernet card is in promiscious mode */
if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0) if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0)
&& (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) { && (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) {
//fprintf(stderr, "ethernet: This packet isn't for us\n"); /*fprintf(stderr, "ethernet: This packet isn't for us\n"); */
return 0; return 0;
} }
(void) decode_unsigned16(&buf[12], &pdu_len); (void) decode_unsigned16(&buf[12], &pdu_len);
pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ; pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ;
// copy the buffer into the PDU /* copy the buffer into the PDU */
if (pdu_len < max_pdu) if (pdu_len < max_pdu)
memmove(&pdu[0], &buf[17], pdu_len); memmove(&pdu[0], &buf[17], pdu_len);
// ignore packets that are too large /* ignore packets that are too large */
// client should check my max apdu first /* client should check my max apdu first */
else else
pdu_len = 0; pdu_len = 0;
@@ -258,7 +258,7 @@ void ethernet_get_my_address(BACNET_ADDRESS * my_address)
my_address->mac[i] = Ethernet_MAC_Address[i]; my_address->mac[i] = Ethernet_MAC_Address[i];
my_address->mac_len++; my_address->mac_len++;
} }
my_address->net = 0; // local only, no routing my_address->net = 0; /* local only, no routing */
my_address->len = 0; my_address->len = 0;
for (i = 0; i < MAX_MAC_LEN; i++) { for (i = 0; i < MAX_MAC_LEN; i++) {
my_address->adr[i] = 0; my_address->adr[i] = 0;
@@ -278,9 +278,9 @@ void ethernet_set_my_address(BACNET_ADDRESS * my_address)
return; return;
} }
void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address void ethernet_get_broadcast_address(BACNET_ADDRESS * dest)
{ { /* destination address */
int i = 0; // counter int i = 0; /* counter */
if (dest) { if (dest) {
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
@@ -288,7 +288,7 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination a
} }
dest->mac_len = 6; dest->mac_len = 6;
dest->net = BACNET_BROADCAST_NETWORK; dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; // denotes broadcast address dest->len = 0; /* denotes broadcast address */
for (i = 0; i < MAX_MAC_LEN; i++) { for (i = 0; i < MAX_MAC_LEN; i++) {
dest->adr[i] = 0; dest->adr[i] = 0;
} }
@@ -299,7 +299,7 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination a
void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest) void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest)
{ {
int i = 0; // counter int i = 0; /* counter */
if (info) if (info)
fprintf(stderr, "%s", info); fprintf(stderr, "%s", info);
+19 -19
View File
@@ -36,15 +36,15 @@ extern void RTEmuInit(void);
#endif #endif
/* DISK SYSTEM */ /* DISK SYSTEM */
#ifdef DOC // include DiskOnChip driver #ifdef DOC /* include DiskOnChip driver */
#include <rtfiles.h> #include <rtfiles.h>
#define RTF_MAX_FILES 16 // support for more open files (default is 8) #define RTF_MAX_FILES 16 /* support for more open files (default is 8) */
#define RTF_BUFFERS_IN_BSS // we do not need file I/O before the run-time #define RTF_BUFFERS_IN_BSS /* we do not need file I/O before the run-time */
#include <rtfdata.c> // system is initialized #include <rtfdata.c> /* system is initialized */
//#define READ_HEAD_BUFFER_SIZE 2048+4 /*#define READ_HEAD_BUFFER_SIZE 2048+4 */
//static BYTE ReadAheadBuffer[READ_HEAD_BUFFER_SIZE]; /*static BYTE ReadAheadBuffer[READ_HEAD_BUFFER_SIZE]; */
static RTFDrvFLPYData FLPYDriveAData = { 0 }; static RTFDrvFLPYData FLPYDriveAData = { 0 };
static RTFDrvDOCData DOCDriveData = { 0 }; static RTFDrvDOCData DOCDriveData = { 0 };
@@ -108,17 +108,17 @@ RTFileSystem *RTFileSystemList[] = {
/*-----------------------------------*/ /*-----------------------------------*/
VOIDEXPORT Init(void) VOIDEXPORT Init(void)
{ {
(void) RTSetFlags(RT_MM_VIRTUAL, 1); // this is the better method (void) RTSetFlags(RT_MM_VIRTUAL, 1); /* this is the better method */
(void) RTCMOSExtendHeap(); // get as much memory as we can (void) RTCMOSExtendHeap(); /* get as much memory as we can */
RTCMOSSetSystemTime(); // get the right date and time RTCMOSSetSystemTime(); /* get the right date and time */
RTEmuInit(); // set up floating point emulation RTEmuInit(); /* set up floating point emulation */
// pizza - RTHaltCPL3 appears to cause problems with file handling /* pizza - RTHaltCPL3 appears to cause problems with file handling */
//RTIdleHandler = (void RTTAPI *)RTHaltCPL3; // low power when idle /*RTIdleHandler = (void RTTAPI *)RTHaltCPL3; // low power when idle */
// not needed with pre-emptive /* not needed with pre-emptive */
//RTKTimeSlice(2); // allow same priority task switch /*RTKTimeSlice(2); // allow same priority task switch */
RTKConfig.Flags |= RF_PREEMPTIVE; // preemptive multitasking RTKConfig.Flags |= RF_PREEMPTIVE; /* preemptive multitasking */
RTKConfig.Flags |= RF_WIN32MUTEX_MUTEX; // Win32 mutexes are RTK32 mutexes RTKConfig.Flags |= RF_WIN32MUTEX_MUTEX; /* Win32 mutexes are RTK32 mutexes */
RTKConfig.Flags |= RF_FPCONTEXT; // saves floating point context for tasks RTKConfig.Flags |= RF_FPCONTEXT; /* saves floating point context for tasks */
RTKConfig.HookedIRQs |= 1 << 1; // hook the keyboard IRQ RTKConfig.HookedIRQs |= 1 << 1; /* hook the keyboard IRQ */
RTKConfig.DefaultTaskStackSize = 1024 * 8; // for Win32 task stacks req = 0 RTKConfig.DefaultTaskStackSize = 1024 * 8; /* for Win32 task stacks req = 0 */
} }
+19 -19
View File
@@ -23,8 +23,8 @@
* *
*********************************************************************/ *********************************************************************/
// This is one way to use the embedded BACnet stack under RTOS-32 /* This is one way to use the embedded BACnet stack under RTOS-32 */
// compiled with Borland C++ 5.02 /* compiled with Borland C++ 5.02 */
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@@ -36,23 +36,23 @@
#include "handlers.h" #include "handlers.h"
#include "net.h" #include "net.h"
// buffers used for transmit and receive /* buffers used for transmit and receive */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
#ifdef BACDL_MSTP #ifdef BACDL_MSTP
volatile struct mstp_port_struct_t MSTP_Port; // port data volatile struct mstp_port_struct_t MSTP_Port; /* port data */
static uint8_t MSTP_MAC_Address = 0x05; // local MAC address static uint8_t MSTP_MAC_Address = 0x05; /* local MAC address */
#endif #endif
static void Init_Service_Handlers(void) static void Init_Service_Handlers(void)
{ {
// we need to handle who-is to support dynamic device binding /* we need to handle who-is to support dynamic device binding */
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, WhoIsHandler); apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, WhoIsHandler);
// set the handler for all the services we don't implement /* set the handler for all the services we don't implement */
// It is required to send the proper reject message... /* It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler apdu_set_unrecognized_service_handler_handler
(UnrecognizedServiceHandler); (UnrecognizedServiceHandler);
// we must implement read property - it's required! /* we must implement read property - it's required! */
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
ReadPropertyHandler); ReadPropertyHandler);
apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
@@ -61,15 +61,15 @@ static void Init_Service_Handlers(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = { 0 }; // address where message came from BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; /* milliseconds */
(void) argc; (void) argc;
(void) argv; (void) argv;
Device_Set_Object_Instance_Number(126); Device_Set_Object_Instance_Number(126);
Init_Service_Handlers(); Init_Service_Handlers();
// init the physical layer /* init the physical layer */
#ifdef BACDL_BIP #ifdef BACDL_BIP
if (!bip_init()) if (!bip_init())
return 1; return 1;
@@ -84,22 +84,22 @@ int main(int argc, char *argv[])
#endif #endif
// loop forever /* loop forever */
for (;;) { for (;;) {
// input /* input */
#ifdef BACDL_MSTP #ifdef BACDL_MSTP
MSTP_Millisecond_Timer(&MSTP_Port); MSTP_Millisecond_Timer(&MSTP_Port);
// note: also called by RS-485 Receive ISR /* note: also called by RS-485 Receive ISR */
RS485_Check_UART_Data(&MSTP_Port); RS485_Check_UART_Data(&MSTP_Port);
MSTP_Receive_Frame_FSM(&MSTP_Port); MSTP_Receive_Frame_FSM(&MSTP_Port);
#endif #endif
#if (defined(BACDL_ETHERNET) || defined(BACDL_BIP)) #if (defined(BACDL_ETHERNET) || defined(BACDL_BIP))
// returns 0 bytes on timeout /* returns 0 bytes on timeout */
pdu_len = bacdl_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); pdu_len = bacdl_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
#endif #endif
// process /* process */
if (pdu_len) { if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len); npdu_handler(&src, &Rx_Buf[0], pdu_len);
@@ -108,11 +108,11 @@ int main(int argc, char *argv[])
I_Am_Request = false; I_Am_Request = false;
Send_IAm(); Send_IAm();
} }
// output /* output */
#ifdef BACDL_MSTP #ifdef BACDL_MSTP
MSTP_Master_Node_FSM(&MSTP_Port); MSTP_Master_Node_FSM(&MSTP_Port);
#endif #endif
// blink LEDs, Turn on or off outputs, etc /* blink LEDs, Turn on or off outputs, etc */
} }
} }
+23 -23
View File
@@ -28,41 +28,41 @@
*/ */
// #define AUTO_IP // use xn_autoip() to get an IP address /* #define AUTO_IP // use xn_autoip() to get an IP address */
// #define DHCP // if you enable this, you must also link library dhcpc.lib /* #define DHCP // if you enable this, you must also link library dhcpc.lib */
#if defined(AUTO_IP) // use xn_autoip() to get an IP address #if defined(AUTO_IP) /* use xn_autoip() to get an IP address */
static BYTE TargetIP[] = { 0, 0, 0, 0 }; // will be filled at run-time static BYTE TargetIP[] = { 0, 0, 0, 0 }; /* will be filled at run-time */
static BYTE NetMask[] = { 255, 255, 255, 0 }; static BYTE NetMask[] = { 255, 255, 255, 0 };
static BYTE MinIP[] = { 192, 168, 1, 128 }; static BYTE MinIP[] = { 192, 168, 1, 128 };
static BYTE MaxIP[] = { 192, 168, 1, 255 }; static BYTE MaxIP[] = { 192, 168, 1, 255 };
static BYTE DefaultGateway[] = { 192, 168, 1, 1 }; // set to zero if not available or required static BYTE DefaultGateway[] = { 192, 168, 1, 1 }; /* set to zero if not available or required */
static BYTE DNSServer[] = { 192, 168, 1, 1 }; // ditto static BYTE DNSServer[] = { 192, 168, 1, 1 }; /* ditto */
#elif defined(DHCP) // use DHCP #elif defined(DHCP) /* use DHCP */
#include <dhcpcapi.h> #include <dhcpcapi.h>
static BYTE TargetIP[] = { 0, 0, 0, 0 }; // will be filled at run-time static BYTE TargetIP[] = { 0, 0, 0, 0 }; /* will be filled at run-time */
#else // static IP address assignment (default) #else /* static IP address assignment (default) */
static BYTE TargetIP[] = { 192, 168, 0, 50 }; static BYTE TargetIP[] = { 192, 168, 0, 50 };
static BYTE NetMask[] = { 255, 255, 255, 0 }; static BYTE NetMask[] = { 255, 255, 255, 0 };
static BYTE DefaultGateway[] = { 192, 168, 0, 1 }; // set to zero if not available or required static BYTE DefaultGateway[] = { 192, 168, 0, 1 }; /* set to zero if not available or required */
static BYTE DNSServer[] = { 192, 168, 0, 1 }; // ditto static BYTE DNSServer[] = { 192, 168, 0, 1 }; /* ditto */
#endif #endif
#define DEVICE_ID DAVICOM_DEVICE // define your device type here #define DEVICE_ID DAVICOM_DEVICE /* define your device type here */
#ifndef DEVICE_ID #ifndef DEVICE_ID
#error You must define Ethernet driver/resources and IP address/net mask here #error You must define Ethernet driver/resources and IP address/net mask here
#endif #endif
// The following values are ignored for PCI devices (the BIOS supplies /* The following values are ignored for PCI devices (the BIOS supplies */
// them), but they must be set correctly for ISA/PCMCIA systems and for /* them), but they must be set correctly for ISA/PCMCIA systems and for */
// PCI devices if you do not have a BIOS /* PCI devices if you do not have a BIOS */
#define ED_IO_ADD 0x300 // I/O address of the device #define ED_IO_ADD 0x300 /* I/O address of the device */
#define ED_IRQ 5 // IRQ of the device #define ED_IRQ 5 /* IRQ of the device */
#define ED_MEM_ADD 0 // Memory Window (only some devices) #define ED_MEM_ADD 0 /* Memory Window (only some devices) */
// Define function to pull in the required driver /* Define function to pull in the required driver */
#if DEVICE_ID == NE2000_DEVICE #if DEVICE_ID == NE2000_DEVICE
#define BIND_DRIVER xn_bind_ne2000 #define BIND_DRIVER xn_bind_ne2000
@@ -87,17 +87,17 @@ static BYTE DNSServer[] = { 192, 168, 0, 1 }; // ditto
#elif DEVICE_ID == RHINE_DEVICE #elif DEVICE_ID == RHINE_DEVICE
#define BIND_DRIVER xn_bind_rhine #define BIND_DRIVER xn_bind_rhine
#elif DEVICE_ID == AX172_DEVICE #elif DEVICE_ID == AX172_DEVICE
#include <rtusb.h> // must also link Rtusb.lib and UsbInit.cpp #include <rtusb.h> /* must also link Rtusb.lib and UsbInit.cpp */
#define BIND_DRIVER xn_bind_ax172 #define BIND_DRIVER xn_bind_ax172
#elif DEVICE_ID == AX772_DEVICE #elif DEVICE_ID == AX772_DEVICE
#include <rtusb.h> // must also link Rtusb.lib and UsbInit.cpp #include <rtusb.h> /* must also link Rtusb.lib and UsbInit.cpp */
#define BIND_DRIVER xn_bind_ax772 #define BIND_DRIVER xn_bind_ax772
#elif DEVICE_ID == PRISM_DEVICE #elif DEVICE_ID == PRISM_DEVICE
#include <wlanapi.h> // must also link Wlan.lib #include <wlanapi.h> /* must also link Wlan.lib */
#define BIND_DRIVER xn_bind_prism #define BIND_DRIVER xn_bind_prism
#elif DEVICE_ID == PRISM_PCMCIA_DEVICE #elif DEVICE_ID == PRISM_PCMCIA_DEVICE
#include <rtpcmcia.h> #include <rtpcmcia.h>
#include <wlanapi.h> // must also link Wlan.lib #include <wlanapi.h> /* must also link Wlan.lib */
#define BIND_DRIVER xn_bind_prism_pcmcia #define BIND_DRIVER xn_bind_prism_pcmcia
#else #else
#error Invalid DEVICE_ID value #error Invalid DEVICE_ID value
+14 -14
View File
@@ -29,7 +29,7 @@
#include <itimer.h> #include <itimer.h>
#include "mstp.h" #include "mstp.h"
// note: use the RTKernel-C API so that it can use this library /* note: use the RTKernel-C API so that it can use this library */
#define RS485_IO_ENABLE(p) ModemControl(p,0,DTR); #define RS485_IO_ENABLE(p) ModemControl(p,0,DTR);
#define RS485_TRANSMIT_ENABLE(p) ModemControl(p,1,RTS); #define RS485_TRANSMIT_ENABLE(p) ModemControl(p,1,RTS);
@@ -101,11 +101,11 @@ void RS485_Initialize(void)
RS485_Open_Port(RS485_Port, RS485_Baud, RS485_Base, RS485_IRQ_Number); RS485_Open_Port(RS485_Port, RS485_Baud, RS485_Base, RS485_IRQ_Number);
} }
void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, // port specific data 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) uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
uint16_t nbytes) // number of bytes of data (up to 501) uint16_t nbytes)
{ { /* number of bytes of data (up to 501) */
bool status = true; // return value bool status = true; /* return value */
(void) mstp_port; (void) mstp_port;
RS485_TRANSMIT_ENABLE(RS485_Port); RS485_TRANSMIT_ENABLE(RS485_Port);
@@ -119,23 +119,23 @@ void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, // port
return; return;
} }
void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) // port specific data void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port)
{ { /* port specific data */
COMData com_data = 0; /* byte from COM driver */ COMData com_data = 0; /* byte from COM driver */
unsigned timeout = 10; // milliseconds to wait for a character unsigned timeout = 10; /* milliseconds to wait for a character */
Duration ticks; /* duration to wait for data */ Duration ticks; /* duration to wait for data */
if (mstp_port->ReceiveError) { if (mstp_port->ReceiveError) {
// wait for state machine to clear this /* wait for state machine to clear this */
} }
// wait for state machine to read from the DataRegister /* wait for state machine to read from the DataRegister */
else if (!mstp_port->DataAvailable) { else if (!mstp_port->DataAvailable) {
// check for data /* check for data */
ticks = MilliSecsToTicks(timeout); ticks = MilliSecsToTicks(timeout);
if (!ticks) if (!ticks)
ticks = 1; ticks = 1;
if (RTKGetTimed(ReceiveBuffer[RS485_Port], &com_data, ticks)) { if (RTKGetTimed(ReceiveBuffer[RS485_Port], &com_data, ticks)) {
// if error, /* if error, */
if (com_data & (COM_OVERRUN << 8)) if (com_data & (COM_OVERRUN << 8))
mstp_port->ReceiveError = true; mstp_port->ReceiveError = true;
else if (com_data & (COM_FRAME << 8)) else if (com_data & (COM_FRAME << 8))
@@ -150,5 +150,5 @@ void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port)
void RS485_Process_Tx_Message(void) void RS485_Process_Tx_Message(void)
{ {
// nothing to do /* nothing to do */
} }
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef STDBOOL_H #ifndef STDBOOL_H
#define STDBOOL_H #define STDBOOL_H
// C99 Boolean types for compilers without C99 support /* C99 Boolean types for compilers without C99 support */
#ifndef __cplusplus #ifndef __cplusplus
typedef int _Bool; typedef int _Bool;
+12 -12
View File
@@ -1,19 +1,19 @@
// Defines the standard integer types that are used in code /* Defines the standard integer types that are used in code */
// for the x86 processor and Borland Compiler /* for the x86 processor and Borland Compiler */
#ifndef STDINT_H #ifndef STDINT_H
#define STDINT_H #define STDINT_H
#include <stddef.h> #include <stddef.h>
typedef unsigned char uint8_t; // 1 byte 0 to 255 typedef unsigned char uint8_t; /* 1 byte 0 to 255 */
typedef signed char int8_t; // 1 byte -127 to 127 typedef signed char int8_t; /* 1 byte -127 to 127 */
typedef unsigned short uint16_t; // 2 bytes 0 to 65535 typedef unsigned short uint16_t; /* 2 bytes 0 to 65535 */
typedef signed short int16_t; // 2 bytes -32767 to 32767 typedef signed short int16_t; /* 2 bytes -32767 to 32767 */
//typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215 /*typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215 */
typedef unsigned long uint32_t; // 4 bytes 0 to 4294967295 typedef unsigned long uint32_t; /* 4 bytes 0 to 4294967295 */
typedef signed long int32_t; // 4 bytes -2147483647 to 2147483647 typedef signed long int32_t; /* 4 bytes -2147483647 to 2147483647 */
// typedef signed long long int64_t; /* typedef signed long long int64_t; */
// typedef unsigned long long uint64_t; /* typedef unsigned long long uint64_t; */
#endif // STDINT_H #endif /* STDINT_H */
+8 -8
View File
@@ -37,8 +37,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> // for standard integer types uint8_t etc. #include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> // for the standard bool type. #include <stdbool.h> /* for the standard bool type. */
#include "bacdcode.h" #include "bacdcode.h"
#include "bip.h" #include "bip.h"
#include "net.h" #include "net.h"
@@ -102,7 +102,7 @@ void bip_set_interface(char *ifname)
bool bip_init(void) bool bip_init(void)
{ {
int rv = 0; // return from socket lib calls int rv = 0; /* return from socket lib calls */
struct sockaddr_in sin = { -1 }; struct sockaddr_in sin = { -1 };
int value = 1; int value = 1;
int sock_fd = -1; int sock_fd = -1;
@@ -112,7 +112,7 @@ bool bip_init(void)
struct in_addr address; struct in_addr address;
Result = WSAStartup((1 << 8) | 1, &wd); Result = WSAStartup((1 << 8) | 1, &wd);
//Result = WSAStartup(MAKEWORD(2,2), &wd); /*Result = WSAStartup(MAKEWORD(2,2), &wd); */
if (Result != 0) { if (Result != 0) {
Code = WSAGetLastError(); Code = WSAGetLastError();
printf("TCP/IP stack initialization failed, error code: %i\n", printf("TCP/IP stack initialization failed, error code: %i\n",
@@ -140,8 +140,8 @@ bool bip_init(void)
fprintf(stderr, "bip: failed to allocate a socket.\n"); fprintf(stderr, "bip: failed to allocate a socket.\n");
return false; return false;
} }
// Allow us to use the same socket for sending and receiving /* Allow us to use the same socket for sending and receiving */
// This makes sure that the src port is correct when sending /* This makes sure that the src port is correct when sending */
rv = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, rv = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR,
(char *) &value, sizeof(value)); (char *) &value, sizeof(value));
if (rv < 0) { if (rv < 0) {
@@ -150,7 +150,7 @@ bool bip_init(void)
bip_set_socket(-1); bip_set_socket(-1);
return false; return false;
} }
// allow us to send a broadcast /* allow us to send a broadcast */
rv = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, rv = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST,
(char *) &value, sizeof(value)); (char *) &value, sizeof(value));
if (rv < 0) { if (rv < 0) {
@@ -159,7 +159,7 @@ bool bip_init(void)
bip_set_socket(-1); bip_set_socket(-1);
return false; return false;
} }
// bind the socket to the local port number and IP address /* bind the socket to the local port number and IP address */
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
#if USE_INADDR #if USE_INADDR
/* by setting sin.sin_addr.s_addr to INADDR_ANY, /* by setting sin.sin_addr.s_addr to INADDR_ANY,
+21 -21
View File
@@ -23,8 +23,8 @@
* *
*********************************************************************/ *********************************************************************/
// This is one way to use the embedded BACnet stack under Win32 /* This is one way to use the embedded BACnet stack under Win32 */
// compiled with Borland C++ 5.02 or Visual C++ 6.0 /* compiled with Borland C++ 5.02 or Visual C++ 6.0 */
#include <winsock2.h> #include <winsock2.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@@ -42,7 +42,7 @@
#include "datalink.h" #include "datalink.h"
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive /* buffer used for receive */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* send a whois to see who is on the network */ /* send a whois to see who is on the network */
@@ -102,7 +102,7 @@ static void Read_Properties(void)
if (object_props[property] < 0) if (object_props[property] < 0)
next_device = true; next_device = true;
else { else {
status = Send_Read_Property_Request(device_id, // destination device status = Send_Read_Property_Request(device_id, /* destination device */
OBJECT_DEVICE, OBJECT_DEVICE,
device_id, object_props[property], BACNET_ARRAY_ALL); device_id, object_props[property], BACNET_ARRAY_ALL);
if (status) if (status)
@@ -147,29 +147,29 @@ static void LocalIAmHandler(uint8_t * service_request,
static void Init_Service_Handlers(void) static void Init_Service_Handlers(void)
{ {
// we need to handle who-is to support dynamic device binding /* we need to handle who-is to support dynamic device binding */
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler); LocalIAmHandler);
// set the handler for all the services we don't implement /* set the handler for all the services we don't implement */
// It is required to send the proper reject message... /* It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service); (handler_unrecognized_service);
// we must implement read property - it's required! /* we must implement read property - it's required! */
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
handler_write_property); handler_write_property);
// handle the data coming back from confirmed requests /* handle the data coming back from confirmed requests */
apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY, apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property_ack); handler_read_property_ack);
} }
static void print_address(char *name, BACNET_ADDRESS * dest) // destination address static void print_address(char *name, BACNET_ADDRESS * dest)
{ { /* destination address */
int i = 0; // counter int i = 0; /* counter */
if (dest) { if (dest) {
printf("%s: ", name); printf("%s: ", name);
@@ -203,16 +203,16 @@ static void print_address_cache(void)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = { 0 }; // address where message came from BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; /* milliseconds */
BACNET_ADDRESS my_address, broadcast_address; BACNET_ADDRESS my_address, broadcast_address;
(void) argc; (void) argc;
(void) argv; (void) argv;
Device_Set_Object_Instance_Number(124); Device_Set_Object_Instance_Number(124);
Init_Service_Handlers(); Init_Service_Handlers();
// init the data link layer /* init the data link layer */
/* configure standard BACnet/IP port */ /* configure standard BACnet/IP port */
bip_set_port(0xBAC0); bip_set_port(0xBAC0);
if (!bip_init()) if (!bip_init())
@@ -224,14 +224,14 @@ int main(int argc, char *argv[])
print_address("Address", &my_address); print_address("Address", &my_address);
printf("BACnet stack running...\n"); printf("BACnet stack running...\n");
// loop forever /* loop forever */
for (;;) { for (;;) {
// input /* input */
// returns 0 bytes on timeout /* returns 0 bytes on timeout */
pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
// process /* process */
if (pdu_len) { if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len); npdu_handler(&src, &Rx_Buf[0], pdu_len);
@@ -246,9 +246,9 @@ int main(int argc, char *argv[])
Read_Properties(); Read_Properties();
} }
// output /* output */
// blink LEDs, Turn on or off outputs, etc /* blink LEDs, Turn on or off outputs, etc */
/* wait for ESC from keyboard before quitting */ /* wait for ESC from keyboard before quitting */
if (kbhit() && (getch() == 0x1B)) if (kbhit() && (getch() == 0x1B))
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef STDBOOL_H #ifndef STDBOOL_H
#define STDBOOL_H #define STDBOOL_H
// C99 Boolean types for compilers without C99 support /* C99 Boolean types for compilers without C99 support */
#ifndef __cplusplus #ifndef __cplusplus
typedef int _Bool; typedef int _Bool;
+12 -12
View File
@@ -1,19 +1,19 @@
// Defines the standard integer types that are used in code /* Defines the standard integer types that are used in code */
// for the x86 processor and Borland Compiler /* for the x86 processor and Borland Compiler */
#ifndef STDINT_H #ifndef STDINT_H
#define STDINT_H #define STDINT_H
#include <stddef.h> #include <stddef.h>
typedef unsigned char uint8_t; // 1 byte 0 to 255 typedef unsigned char uint8_t; /* 1 byte 0 to 255 */
typedef signed char int8_t; // 1 byte -127 to 127 typedef signed char int8_t; /* 1 byte -127 to 127 */
typedef unsigned short uint16_t; // 2 bytes 0 to 65535 typedef unsigned short uint16_t; /* 2 bytes 0 to 65535 */
typedef signed short int16_t; // 2 bytes -32767 to 32767 typedef signed short int16_t; /* 2 bytes -32767 to 32767 */
//typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215 /*typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215 */
typedef unsigned long uint32_t; // 4 bytes 0 to 4294967295 typedef unsigned long uint32_t; /* 4 bytes 0 to 4294967295 */
typedef signed long int32_t; // 4 bytes -2147483647 to 2147483647 typedef signed long int32_t; /* 4 bytes -2147483647 to 2147483647 */
// typedef signed long long int64_t; /* typedef signed long long int64_t; */
// typedef unsigned long long uint64_t; /* typedef unsigned long long uint64_t; */
#endif // STDINT_H #endif /* STDINT_H */
+106 -119
View File
@@ -7,119 +7,109 @@
#include <assert.h> #include <assert.h>
/* Number of tests to hold incrementally */ /* Number of tests to hold incrementally */
enum {CHUNK = 10}; enum { CHUNK = 10 };
Test* ct_create(const char* name, void (*init)(Test*)) Test *ct_create(const char *name, void (*init) (Test *))
{ {
int backOutLevel = 0; int backOutLevel = 0;
Test* pTest = malloc(sizeof(Test)); Test *pTest = malloc(sizeof(Test));
if (pTest) if (pTest) {
{ pTest->nPass = pTest->nFail = pTest->nTests = 0;
pTest->nPass = pTest->nFail = pTest->nTests = 0; pTest->pStream = stdout;
pTest->pStream = stdout;
/* Allocate array of fptrs: */ /* Allocate array of fptrs: */
assert(CHUNK); assert(CHUNK);
pTest->pTestFuns = calloc(CHUNK, sizeof(TestFunc)); pTest->pTestFuns = calloc(CHUNK, sizeof(TestFunc));
if (pTest->pTestFuns) if (pTest->pTestFuns) {
{ pTest->maxTests = CHUNK;
pTest->maxTests = CHUNK; /* Allocate test name: */
/* Allocate test name: */ assert(name);
assert(name); pTest->name = malloc(strlen(name) + 1);
pTest->name = malloc(strlen(name) + 1); if (pTest->name)
if (pTest->name) strcpy(pTest->name, name);
strcpy(pTest->name, name); else
else ++backOutLevel;
++backOutLevel; } else
} ++backOutLevel;
else }
++backOutLevel;
}
/* Back-out allocations if memory failed: */ /* Back-out allocations if memory failed: */
if (backOutLevel) if (backOutLevel) {
{ switch (backOutLevel) {
switch(backOutLevel) case 2:
{ free(pTest->pTestFuns);
case 2: pTest->pTestFuns = NULL;
free(pTest->pTestFuns); case 1:
pTest->pTestFuns = NULL; free(pTest);
case 1: pTest = NULL;
free(pTest);
pTest = NULL;
}
} }
else if (init) } else if (init) {
{
assert(pTest);
init(pTest);
}
return pTest;
}
void ct_destroy(Test* pTest)
{
assert(pTest); assert(pTest);
assert(pTest->pTestFuns); init(pTest);
free(pTest->pTestFuns); }
pTest->pTestFuns = NULL; return pTest;
assert(pTest->name);
free(pTest->name);
pTest->name = NULL;
free(pTest);
} }
bool ct_addTestFunction(Test* pTest, TestFunc tfun) void ct_destroy(Test * pTest)
{ {
assert(pTest); assert(pTest);
assert(pTest->pTestFuns); assert(pTest->pTestFuns);
if (pTest->nTests == pTest->maxTests) free(pTest->pTestFuns);
{ pTest->pTestFuns = NULL;
size_t newSize = pTest->nTests + CHUNK; assert(pTest->name);
TestFunc* new_pTestFuns = free(pTest->name);
realloc(pTest->pTestFuns, pTest->name = NULL;
newSize * sizeof(TestFunc)); free(pTest);
if (!new_pTestFuns)
return false;
pTest->pTestFuns = new_pTestFuns;
pTest->maxTests += CHUNK;
}
assert(pTest->nTests < pTest->maxTests);
pTest->pTestFuns[pTest->nTests++] = tfun;
return true;
} }
void ct_setStream(Test* pTest, FILE* pStream) bool ct_addTestFunction(Test * pTest, TestFunc tfun)
{ {
pTest->pStream = pStream; assert(pTest);
assert(pTest->pTestFuns);
if (pTest->nTests == pTest->maxTests) {
size_t newSize = pTest->nTests + CHUNK;
TestFunc *new_pTestFuns = realloc(pTest->pTestFuns,
newSize * sizeof(TestFunc));
if (!new_pTestFuns)
return false;
pTest->pTestFuns = new_pTestFuns;
pTest->maxTests += CHUNK;
}
assert(pTest->nTests < pTest->maxTests);
pTest->pTestFuns[pTest->nTests++] = tfun;
return true;
} }
FILE* ct_getStream(Test* pTest) void ct_setStream(Test * pTest, FILE * pStream)
{ {
return pTest->pStream; pTest->pStream = pStream;
} }
long ct_report(Test* pTest) FILE *ct_getStream(Test * pTest)
{ {
assert(pTest); return pTest->pStream;
if (pTest->pStream)
{
fprintf(pTest->pStream,
"Test \"%s\":\n\tPassed: %ld\n\tFailed: %ld\n",
pTest->name, pTest->nPass, pTest->nFail);
}
return pTest->nFail;
} }
long ct_report(Test * pTest)
void ct_succeed(Test* pTest)
{ {
assert(pTest); assert(pTest);
++pTest->nPass; if (pTest->pStream) {
fprintf(pTest->pStream,
"Test \"%s\":\n\tPassed: %ld\n\tFailed: %ld\n",
pTest->name, pTest->nPass, pTest->nFail);
}
return pTest->nFail;
} }
void ct_do_test(Test* pTest, const char* str,
bool cond, const char* file, long line) void ct_succeed(Test * pTest)
{
assert(pTest);
++pTest->nPass;
}
void ct_do_test(Test * pTest, const char *str,
bool cond, const char *file, long line)
{ {
assert(pTest); assert(pTest);
if (!cond) if (!cond)
@@ -128,55 +118,52 @@ void ct_do_test(Test* pTest, const char* str,
ct_succeed(pTest); ct_succeed(pTest);
} }
void ct_do_fail(Test* pTest, const char* str, void ct_do_fail(Test * pTest, const char *str, const char *file, long line)
const char* file, long line)
{ {
assert(pTest); assert(pTest);
++pTest->nFail; ++pTest->nFail;
if (pTest->pStream) if (pTest->pStream) {
{
fprintf(pTest->pStream, fprintf(pTest->pStream,
"%s failure: (%s), %s (line %ld)\n", "%s failure: (%s), %s (line %ld)\n",
pTest->name, str, file, line); pTest->name, str, file, line);
} }
} }
long ct_getNumPassed(Test* pTest) long ct_getNumPassed(Test * pTest)
{ {
assert(pTest); assert(pTest);
return pTest->nPass; return pTest->nPass;
} }
long ct_getNumFailed(Test* pTest) long ct_getNumFailed(Test * pTest)
{ {
assert(pTest); assert(pTest);
return pTest->nFail; return pTest->nFail;
} }
long ct_run(Test* pTest) long ct_run(Test * pTest)
{ {
size_t testNum; size_t testNum;
assert(pTest); assert(pTest);
for (testNum = 0; testNum < pTest->nTests; ++testNum) for (testNum = 0; testNum < pTest->nTests; ++testNum)
pTest->pTestFuns[testNum](pTest); pTest->pTestFuns[testNum] (pTest);
return pTest->nFail; return pTest->nFail;
} }
void ct_reset(Test* pTest) void ct_reset(Test * pTest)
{ {
assert(pTest); assert(pTest);
pTest->nFail = pTest->nPass = 0; pTest->nFail = pTest->nPass = 0;
} }
const char* ct_getName(Test* pTest) const char *ct_getName(Test * pTest)
{ {
assert(pTest); assert(pTest);
return (pTest->name); return (pTest->name);
} }
long ct_getNumTests(Test* pTest) long ct_getNumTests(Test * pTest)
{ {
assert(pTest); assert(pTest);
return pTest->nTests; return pTest->nTests;
} }
+26 -28
View File
@@ -15,47 +15,45 @@
typedef struct _Test Test; typedef struct _Test Test;
typedef void (*TestFunc)(Test*); typedef void (*TestFunc) (Test *);
struct _Test struct _Test {
{ char *name;
char* name; FILE *pStream;
FILE* pStream; size_t nTests;
size_t nTests; size_t maxTests;
size_t maxTests; TestFunc *pTestFuns;
TestFunc* pTestFuns; long nPass;
long nPass; long nFail;
long nFail;
}; };
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
Test* ct_create(const char* name, void (*init)(Test*)); Test *ct_create(const char *name, void (*init) (Test *));
void ct_destroy(Test* pTest); void ct_destroy(Test * pTest);
const char* ct_getName(Test* pTest); const char *ct_getName(Test * pTest);
long ct_getNumPassed(Test* pTest); long ct_getNumPassed(Test * pTest);
long ct_getNumFailed(Test* pTest); long ct_getNumFailed(Test * pTest);
long ct_getNumTests(Test* pTest); long ct_getNumTests(Test * pTest);
FILE* ct_getStream(Test* pTest); FILE *ct_getStream(Test * pTest);
void ct_setStream(Test* pTest, FILE* stream); void ct_setStream(Test * pTest, FILE * stream);
bool ct_addTestFunction(Test* pTest, TestFunc tfun); bool ct_addTestFunction(Test * pTest, TestFunc tfun);
void ct_succeed(Test* pTest); void ct_succeed(Test * pTest);
long ct_run(Test* pTest); long ct_run(Test * pTest);
long ct_report(Test* pTest); long ct_report(Test * pTest);
void ct_reset(Test* pTest); void ct_reset(Test * pTest);
/* Not intended for end-users: */ /* Not intended for end-users: */
void ct_do_test(Test* pTest, const char* str, void ct_do_test(Test * pTest, const char *str,
bool cond, const char* file, long line); bool cond, const char *file, long line);
void ct_do_fail(Test* pTest, const char* str, void ct_do_fail(Test * pTest, const char *str,
const char* file, long line); const char *file, long line);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif #endif