Indented.

This commit is contained in:
skarg
2007-11-29 15:56:53 +00:00
parent c585241c03
commit 411d6c1b24
236 changed files with 17864 additions and 15724 deletions
+29 -23
View File
@@ -80,12 +80,14 @@ clockp Clock Prescaler DataRate
*/
bool arcnet_valid(void)
bool arcnet_valid(
void)
{
return (ARCNET_Sock_FD >= 0);
}
void arcnet_cleanup(void)
void arcnet_cleanup(
void)
{
if (arcnet_valid())
close(ARCNET_Sock_FD);
@@ -94,11 +96,12 @@ void arcnet_cleanup(void)
return;
}
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;
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;
/* check to see if we are being run as root */
@@ -116,8 +119,7 @@ static int arcnet_bind(char *interface_name)
/* Then follow it by: # modprobe af_packet */
if ((sock_fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0) {
/* Error occured */
fprintf(stderr,
"arcnet: Error opening socket: %s\n", strerror(errno));
fprintf(stderr, "arcnet: Error opening socket: %s\n", strerror(errno));
fprintf(stderr,
"You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n"
@@ -157,7 +159,7 @@ static int arcnet_bind(char *interface_name)
}
strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name));
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];
/* copy this info into the local copy since bind wiped it out */
ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET;
@@ -176,7 +178,8 @@ static int arcnet_bind(char *interface_name)
return sock_fd;
}
bool arcnet_init(char *interface_name)
bool arcnet_init(
char *interface_name)
{
if (interface_name)
ARCNET_Sock_FD = arcnet_bind(interface_name);
@@ -188,11 +191,12 @@ bool arcnet_init(char *interface_name)
/* function to send a PDU out the socket */
/* 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 */
BACNET_NPDU_DATA * npdu_data, /* network information */
uint8_t * pdu, /* any data to be sent - may be null */
uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len)
{ /* number of bytes of data */
{ /* number of bytes of data */
BACNET_ADDRESS src = { 0 }; /* source address */
int bytes = 0;
uint8_t mtu[512] = { 0 };
@@ -240,19 +244,19 @@ int arcnet_send_pdu(BACNET_ADDRESS * dest, /* destination address */
sizeof(ARCNET_Socket_Address));
/* did it get sent? */
if (bytes < 0)
fprintf(stderr, "arcnet: Error sending packet: %s\n",
strerror(errno));
fprintf(stderr, "arcnet: Error sending packet: %s\n", strerror(errno));
return bytes;
}
/* receives an framed packet */
/* returns the number of octets in the PDU, or zero on failure */
uint16_t arcnet_receive(BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
uint16_t arcnet_receive(
BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout)
{ /* milliseconds to wait for a packet */
{ /* milliseconds to wait for a packet */
int received_bytes;
uint8_t buf[512] = { 0 }; /* data */
uint16_t pdu_len = 0; /* return value */
@@ -347,7 +351,8 @@ uint16_t arcnet_receive(BACNET_ADDRESS * src, /* source address */
return pdu_len;
}
void arcnet_get_my_address(BACNET_ADDRESS * my_address)
void arcnet_get_my_address(
BACNET_ADDRESS * my_address)
{
int i = 0;
@@ -362,15 +367,16 @@ void arcnet_get_my_address(BACNET_ADDRESS * my_address)
return;
}
void arcnet_get_broadcast_address(BACNET_ADDRESS * dest)
{ /* destination address */
int i = 0; /* counter */
void arcnet_get_broadcast_address(
BACNET_ADDRESS * dest)
{ /* destination address */
int i = 0; /* counter */
if (dest) {
dest->mac[0] = ARCNET_BROADCAST;
dest->mac_len = 1;
dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; /* always zero when DNET is broadcast */
dest->len = 0; /* always zero when DNET is broadcast */
for (i = 0; i < MAX_MAC_LEN; i++) {
dest->adr[i] = 0;
}
+18 -14
View File
@@ -32,19 +32,21 @@
-------------------------------------------
####COPYRIGHTEND####*/
#include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> /* for the standard bool type. */
#include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> /* for the standard bool type. */
#include "bacdcode.h"
#include "bip.h"
#include "net.h"
bool BIP_Debug = false;
static int get_local_ifr_ioctl(char *ifname, struct ifreq *ifr,
static int get_local_ifr_ioctl(
char *ifname,
struct ifreq *ifr,
int request)
{
int fd;
int rv; /* return value */
int rv; /* return value */
strncpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
@@ -56,12 +58,14 @@ static int get_local_ifr_ioctl(char *ifname, struct ifreq *ifr,
return rv;
}
static int get_local_address_ioctl(char *ifname,
struct in_addr *addr, int request)
static int get_local_address_ioctl(
char *ifname,
struct in_addr *addr,
int request)
{
struct ifreq ifr = { {{0}} };
struct sockaddr_in *tcpip_address;
int rv; /* return value */
int rv; /* return value */
rv = get_local_ifr_ioctl(ifname, &ifr, request);
if (rv >= 0) {
@@ -73,7 +77,8 @@ static int get_local_address_ioctl(char *ifname,
}
/* on Linux, ifname is eth0, ath0, arc0, and others. */
static void bip_set_interface(char *ifname)
static void bip_set_interface(
char *ifname)
{
struct in_addr local_address;
struct in_addr broadcast_address;
@@ -90,8 +95,7 @@ static void bip_set_interface(char *ifname)
fprintf(stderr, "IP Address: %s\n", inet_ntoa(local_address));
}
/* setup local broadcast address */
rv = get_local_address_ioctl(ifname, &broadcast_address,
SIOCGIFBRDADDR);
rv = get_local_address_ioctl(ifname, &broadcast_address, SIOCGIFBRDADDR);
if (rv < 0) {
broadcast_address.s_addr = ~0;
}
@@ -100,14 +104,14 @@ static void bip_set_interface(char *ifname)
fprintf(stderr, "IP Broadcast Address: %s\n",
inet_ntoa(broadcast_address));
fprintf(stderr, "UDP Port: 0x%04X [%hu]\n",
bip_get_port(),
bip_get_port());
bip_get_port(), bip_get_port());
}
}
bool bip_init(char *ifname)
bool bip_init(
char *ifname)
{
int status = 0; /* return from socket lib calls */
int status = 0; /* return from socket lib calls */
struct sockaddr_in sin;
int sockopt = 0;
int sock_fd = -1;
+153 -150
View File
@@ -63,26 +63,30 @@ static uint8_t RxBuffer[MAX_MPDU];
/* Timer that indicates line silence - and functions */
static uint16_t SilenceTime;
#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;}
static uint16_t Timer_Silence(void)
static uint16_t Timer_Silence(
void)
{
return SilenceTime;
}
static void Timer_Silence_Reset(void)
static void Timer_Silence_Reset(
void)
{
SilenceTime = 0;
}
static void dlmstp_millisecond_timer(void)
static void dlmstp_millisecond_timer(
void)
{
INCREMENT_AND_LIMIT_UINT16(SilenceTime);
}
static void *dlmstp_milliseconds_task(void *pArg)
static void *dlmstp_milliseconds_task(
void *pArg)
{
struct timespec timeOut,remains;
struct timespec timeOut, remains;
timeOut.tv_sec = 0;
timeOut.tv_nsec = 1000000; /* 1 millisecond */
timeOut.tv_nsec = 1000000; /* 1 millisecond */
for (;;) {
nanosleep(&timeOut, &remains);
@@ -92,7 +96,8 @@ static void *dlmstp_milliseconds_task(void *pArg)
return NULL;
}
void dlmstp_reinit(void)
void dlmstp_reinit(
void)
{
/*RS485_Reinit(); */
dlmstp_set_mac_address(DEFAULT_MAC_ADDRESS);
@@ -101,22 +106,21 @@ void dlmstp_reinit(void)
}
/* returns number of bytes sent on success, zero on failure */
int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
int dlmstp_send_pdu(
BACNET_ADDRESS * dest, /* destination address */
BACNET_NPDU_DATA * npdu_data, /* network information */
uint8_t * pdu, /* any data to be sent - may be null */
uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len)
{ /* number of bytes of data */
{ /* number of bytes of data */
DLMSTP_PACKET packet;
int bytes_sent = 0;
ssize_t rc = 0;
if (pdu_len) {
if (npdu_data->data_expecting_reply) {
packet.frame_type =
FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY;
packet.frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY;
} else {
packet.frame_type =
FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY;
packet.frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY;
}
packet.pdu_len = pdu_len;
memmove(&packet.pdu[0], &pdu[0], pdu_len);
@@ -132,11 +136,11 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */
/* copy the packet if one is received.
Return the length of the packet */
uint16_t dlmstp_receive(
BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout) /* milliseconds to wait for a packet */
{
BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout)
{ /* milliseconds to wait for a packet */
uint16_t pdu_len = 0;
int received_bytes = 0;
DLMSTP_PACKET packet;
@@ -161,10 +165,7 @@ uint16_t dlmstp_receive(
max = Receive_Client_SockFD;
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes = read(
Receive_Client_SockFD,
&packet,
sizeof(packet));
received_bytes = read(Receive_Client_SockFD, &packet, sizeof(packet));
} else {
return 0;
}
@@ -198,7 +199,8 @@ uint16_t dlmstp_receive(
return pdu_len;
}
static void *dlmstp_fsm_receive_task(void *pArg)
static void *dlmstp_fsm_receive_task(
void *pArg)
{
bool received_frame = false;
@@ -221,12 +223,13 @@ static void *dlmstp_fsm_receive_task(void *pArg)
return NULL;
}
static void *dlmstp_fsm_master_task(void *pArg)
static void *dlmstp_fsm_master_task(
void *pArg)
{
struct timespec timeOut,remains;
struct timespec timeOut, remains;
timeOut.tv_sec = 0;
timeOut.tv_nsec = 100000; /* .1 millisecond */
timeOut.tv_nsec = 100000; /* .1 millisecond */
for (;;) {
nanosleep(&timeOut, &remains);
@@ -241,7 +244,9 @@ static void *dlmstp_fsm_master_task(void *pArg)
return NULL;
}
void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address)
void dlmstp_fill_bacnet_address(
BACNET_ADDRESS * src,
uint8_t mstp_address)
{
int i = 0;
@@ -266,7 +271,7 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address)
/* for the MS/TP state machine to use for putting received data */
uint16_t MSTP_Put_Receive(
volatile struct mstp_port_struct_t *mstp_port)
volatile struct mstp_port_struct_t *mstp_port)
{
DLMSTP_PACKET packet;
uint16_t pdu_len = mstp_port->DataLength;
@@ -276,11 +281,8 @@ uint16_t MSTP_Put_Receive(
pdu_len = sizeof(packet.pdu);
if (pdu_len) {
MSTP_Packets++;
memmove(&packet.pdu[0],
(void *) & mstp_port->InputBuffer[0],
pdu_len);
dlmstp_fill_bacnet_address(&packet.address,
mstp_port->SourceAddress);
memmove(&packet.pdu[0], (void *) &mstp_port->InputBuffer[0], pdu_len);
dlmstp_fill_bacnet_address(&packet.address, mstp_port->SourceAddress);
packet.pdu_len = pdu_len;
/* ready is not used in this scheme */
packet.ready = true;
@@ -293,10 +295,10 @@ uint16_t MSTP_Put_Receive(
/* for the MS/TP state machine to use for getting data to send */
/* Return: amount of PDU data */
int dlmstp_get_transmit_packet(
DLMSTP_PACKET *packet,
unsigned timeout) /* milliseconds to wait for a packet */
{
int received_bytes = 0; /* return value */
DLMSTP_PACKET * packet,
unsigned timeout)
{ /* milliseconds to wait for a packet */
int received_bytes = 0; /* return value */
struct timeval select_timeout;
fd_set read_fds;
int max = 0;
@@ -318,10 +320,8 @@ int dlmstp_get_transmit_packet(
max = Transmit_Client_SockFD;
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes = read(
Transmit_Client_SockFD,
packet,
sizeof(DLMSTP_PACKET));
received_bytes = read(Transmit_Client_SockFD,
packet, sizeof(DLMSTP_PACKET));
} else {
return 0;
}
@@ -347,10 +347,10 @@ int dlmstp_get_transmit_packet(
/* for the MS/TP state machine to use for getting data to send */
/* Return: amount of PDU data */
uint16_t MSTP_Get_Send(
volatile struct mstp_port_struct_t *mstp_port,
unsigned timeout) /* milliseconds to wait for a packet */
{
uint16_t pdu_len = 0; /* return value */
volatile struct mstp_port_struct_t * mstp_port,
unsigned timeout)
{ /* milliseconds to wait for a packet */
uint16_t pdu_len = 0; /* return value */
int received_bytes = 0;
DLMSTP_PACKET packet;
uint8_t destination = 0; /* destination address */
@@ -368,25 +368,21 @@ uint16_t MSTP_Get_Send(
return 0;
}
/* convert the PDU into the MSTP Frame */
pdu_len = MSTP_Create_Frame(
&mstp_port->OutputBuffer[0], /* <-- loading this */
pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize,
packet.frame_type,
destination,
mstp_port->This_Station,
&packet.pdu[0],
packet.pdu_len);
destination, mstp_port->This_Station, &packet.pdu[0], packet.pdu_len);
return pdu_len;
}
bool dlmstp_compare_data_expecting_reply(
uint8_t *request_pdu,
uint8_t * request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
uint8_t *reply_pdu,
uint8_t * reply_pdu,
uint16_t reply_pdu_len,
BACNET_ADDRESS *dest_address)
BACNET_ADDRESS * dest_address)
{
uint16_t offset;
/* One way to check the message is to compare NPDU
@@ -414,12 +410,12 @@ bool dlmstp_compare_data_expecting_reply(
if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
return false;
}
request.invoke_id = request_pdu[offset+2];
request.invoke_id = request_pdu[offset + 2];
/* segmented message? */
if (request_pdu[offset] & BIT3)
request.service_choice = request_pdu[offset+5];
request.service_choice = request_pdu[offset + 5];
else
request.service_choice = request_pdu[offset+3];
request.service_choice = request_pdu[offset + 3];
/* decode the reply data */
bacnet_address_copy(&reply.address, dest_address);
offset = npdu_decode(&reply_pdu[0],
@@ -432,32 +428,32 @@ bool dlmstp_compare_data_expecting_reply(
reply.pdu_type = reply_pdu[offset] & 0xF0;
switch (reply.pdu_type) {
case PDU_TYPE_CONFIRMED_SERVICE_REQUEST:
reply.invoke_id = reply_pdu[offset+2];
reply.invoke_id = reply_pdu[offset + 2];
/* segmented message? */
if (reply_pdu[offset] & BIT3)
reply.service_choice = reply_pdu[offset+5];
reply.service_choice = reply_pdu[offset + 5];
else
reply.service_choice = reply_pdu[offset+3];
reply.service_choice = reply_pdu[offset + 3];
break;
case PDU_TYPE_SIMPLE_ACK:
reply.invoke_id = reply_pdu[offset+1];
reply.service_choice = reply_pdu[offset+2];
reply.invoke_id = reply_pdu[offset + 1];
reply.service_choice = reply_pdu[offset + 2];
break;
case PDU_TYPE_COMPLEX_ACK:
reply.invoke_id = reply_pdu[offset+1];
reply.invoke_id = reply_pdu[offset + 1];
/* segmented message? */
if (reply_pdu[offset] & BIT3)
reply.service_choice = reply_pdu[offset+4];
reply.service_choice = reply_pdu[offset + 4];
else
reply.service_choice = reply_pdu[offset+2];
reply.service_choice = reply_pdu[offset + 2];
break;
case PDU_TYPE_ERROR:
reply.invoke_id = reply_pdu[offset+1];
reply.service_choice = reply_pdu[offset+2];
reply.invoke_id = reply_pdu[offset + 1];
reply.service_choice = reply_pdu[offset + 2];
break;
case PDU_TYPE_REJECT:
case PDU_TYPE_ABORT:
reply.invoke_id = reply_pdu[offset+1];
reply.invoke_id = reply_pdu[offset + 1];
break;
default:
return false;
@@ -490,12 +486,12 @@ bool dlmstp_compare_data_expecting_reply(
}
uint16_t MSTP_Get_Reply(
volatile struct mstp_port_struct_t *mstp_port,
unsigned timeout) /* milliseconds to wait for a packet */
{
volatile struct mstp_port_struct_t * mstp_port,
unsigned timeout)
{ /* milliseconds to wait for a packet */
int received_bytes = 0;
DLMSTP_PACKET Transmit_Packet;
uint16_t pdu_len = 0; /* return value */
uint16_t pdu_len = 0; /* return value */
uint8_t destination = 0; /* destination address */
bool matched = false;
@@ -508,39 +504,36 @@ uint16_t MSTP_Get_Reply(
} else {
return 0;
}
if ((MAX_HEADER + Transmit_Packet.pdu_len) > MAX_MPDU) {
if ((MAX_HEADER + Transmit_Packet.pdu_len) > MAX_MPDU) {
return 0;
}
/* is this the reply to the DER? */
matched = dlmstp_compare_data_expecting_reply(
&mstp_port->InputBuffer[0],
matched = dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0],
mstp_port->DataLength,
mstp_port->SourceAddress,
&Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len,
&Transmit_Packet.address);
Transmit_Packet.pdu_len, &Transmit_Packet.address);
if (matched) {
/* convert the PDU into the MSTP Frame */
pdu_len = MSTP_Create_Frame(
&mstp_port->OutputBuffer[0], /* <-- loading this */
pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize,
Transmit_Packet.frame_type,
destination,
mstp_port->This_Station,
&Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
&Transmit_Packet.pdu[0], Transmit_Packet.pdu_len);
/* not used here, but setting it anyway */
Transmit_Packet.ready = false;
} else {
/* put it back into the queue */
(void)write(Transmit_Server_SockFD,
&Transmit_Packet, sizeof(Transmit_Packet));
(void) write(Transmit_Server_SockFD,
&Transmit_Packet, sizeof(Transmit_Packet));
}
return pdu_len;
}
void dlmstp_set_mac_address(uint8_t mac_address)
void dlmstp_set_mac_address(
uint8_t mac_address)
{
/* Master Nodes can only have address 0-127 */
if (mac_address <= 127) {
@@ -557,7 +550,8 @@ void dlmstp_set_mac_address(uint8_t mac_address)
return;
}
uint8_t dlmstp_my_address(void)
uint8_t dlmstp_my_address(
void)
{
return MSTP_Port.This_Station;
}
@@ -569,7 +563,8 @@ uint8_t dlmstp_my_address(void)
/* nodes. This may be used to allocate more or less of the available link */
/* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */
/* node, its value shall be 1. */
void dlmstp_set_max_info_frames(uint8_t max_info_frames)
void dlmstp_set_max_info_frames(
uint8_t max_info_frames)
{
if (max_info_frames >= 1) {
MSTP_Port.Nmax_info_frames = max_info_frames;
@@ -583,7 +578,8 @@ void dlmstp_set_max_info_frames(uint8_t max_info_frames)
return;
}
uint8_t dlmstp_max_info_frames(void)
uint8_t dlmstp_max_info_frames(
void)
{
return MSTP_Port.Nmax_info_frames;
}
@@ -593,7 +589,8 @@ uint8_t dlmstp_max_info_frames(void)
/* allowable address for master nodes. The value of Max_Master shall be */
/* less than or equal to 127. If Max_Master is not writable in a node, */
/* its value shall be 127. */
void dlmstp_set_max_master(uint8_t max_master)
void dlmstp_set_max_master(
uint8_t max_master)
{
if (max_master <= 127) {
if (MSTP_Port.This_Station <= max_master) {
@@ -609,14 +606,16 @@ void dlmstp_set_max_master(uint8_t max_master)
return;
}
uint8_t dlmstp_max_master(void)
uint8_t dlmstp_max_master(
void)
{
return MSTP_Port.Nmax_master;
}
void dlmstp_get_my_address(BACNET_ADDRESS * my_address)
void dlmstp_get_my_address(
BACNET_ADDRESS * my_address)
{
int i = 0; /* counter */
int i = 0; /* counter */
my_address->mac_len = 1;
my_address->mac[0] = MSTP_Port.This_Station;
@@ -629,15 +628,16 @@ void dlmstp_get_my_address(BACNET_ADDRESS * my_address)
return;
}
void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest)
{ /* destination address */
int i = 0; /* counter */
void dlmstp_get_broadcast_address(
BACNET_ADDRESS * dest)
{ /* destination address */
int i = 0; /* counter */
if (dest) {
dest->mac_len = 1;
dest->mac[0] = MSTP_BROADCAST_ADDRESS;
dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; /* always zero when DNET is broadcast */
dest->len = 0; /* always zero when DNET is broadcast */
for (i = 0; i < MAX_MAC_LEN; i++) {
dest->adr[i] = 0;
}
@@ -647,84 +647,90 @@ void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest)
}
/* RS485 Baud Rate 9600, 19200, 38400, 57600, 115200 */
void dlmstp_set_baud_rate(uint32_t baud)
void dlmstp_set_baud_rate(
uint32_t baud)
{
RS485_Set_Baud_Rate(baud);
}
uint32_t dlmstp_baud_rate(void)
uint32_t dlmstp_baud_rate(
void)
{
return RS485_Get_Baud_Rate();
return RS485_Get_Baud_Rate();
}
static int create_named_server_socket (const char *filename)
static int create_named_server_socket(
const char *filename)
{
struct sockaddr_un name;
int sock;
size_t size;
/* Create the socket. */
sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
if (sock < 0) {
#if PRINT_ENABLED
perror ("socket");
perror("socket");
#endif
exit (EXIT_FAILURE);
exit(EXIT_FAILURE);
}
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &sock, sizeof(sock));
/* Bind a name to the socket. */
name.sun_family = AF_LOCAL;
strncpy (name.sun_path, filename, sizeof (name.sun_path));
strncpy(name.sun_path, filename, sizeof(name.sun_path));
/* The size of the address is
the offset of the start of the filename,
plus its length,
plus one for the terminating null byte.
Alternatively you can just do:
size = SUN_LEN (&name);
*/
size = (offsetof (struct sockaddr_un, sun_path)
+ strlen (name.sun_path) + 1);
if (bind (sock, (struct sockaddr *) &name, size) < 0) {
the offset of the start of the filename,
plus its length,
plus one for the terminating null byte.
Alternatively you can just do:
size = SUN_LEN (&name);
*/
size = (offsetof(struct sockaddr_un,
sun_path)
+ strlen (name.sun_path) + 1);
if (bind(sock, (struct sockaddr *) &name, size) < 0) {
#if PRINT_ENABLED
perror ("bind");
perror("bind");
#endif
exit (EXIT_FAILURE);
exit(EXIT_FAILURE);
}
return sock;
}
/* used by the client */
static int connect_named_server_socket (const char *filename)
static int connect_named_server_socket(
const char *filename)
{
struct sockaddr_un name;
int sock;
size_t size;
/* Create the socket. */
sock = socket (PF_LOCAL, SOCK_DGRAM, 0);
sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
if (sock < 0) {
#if PRINT_ENABLED
perror ("socket");
perror("socket");
#endif
exit (EXIT_FAILURE);
exit(EXIT_FAILURE);
}
strncpy (name.sun_path, filename, sizeof (name.sun_path));
strncpy(name.sun_path, filename, sizeof(name.sun_path));
/* The size of the address is
the offset of the start of the filename,
plus its length,
plus one for the terminating null byte.
Alternatively you can just do:
size = SUN_LEN (&name);
*/
size = (offsetof (struct sockaddr_un, sun_path)
+ strlen (name.sun_path) + 1);
the offset of the start of the filename,
plus its length,
plus one for the terminating null byte.
Alternatively you can just do:
size = SUN_LEN (&name);
*/
size = (offsetof(struct sockaddr_un,
sun_path)
+ strlen (name.sun_path) + 1);
if (connect(sock, (struct sockaddr *) &name, size) < 0) {
#if PRINT_ENABLED
perror ("client: can't connect to socket");
perror("client: can't connect to socket");
#endif
exit (EXIT_FAILURE);
exit(EXIT_FAILURE);
}
return sock;
@@ -733,7 +739,8 @@ static int connect_named_server_socket (const char *filename)
static char *dlmstp_transmit_socket_name = "/tmp/BACnet_MSTP_Tx";
static char *dlmstp_receive_socket_name = "/tmp/BACnet_MSTP_Rx";
void dlmstp_cleanup(void)
void dlmstp_cleanup(
void)
{
remove(dlmstp_transmit_socket_name);
remove(dlmstp_receive_socket_name);
@@ -741,7 +748,8 @@ void dlmstp_cleanup(void)
/* nothing to do for static buffers */
}
bool dlmstp_init(char *ifname)
bool dlmstp_init(
char *ifname)
{
int rc = 0;
pthread_t hThread;
@@ -763,7 +771,7 @@ bool dlmstp_init(char *ifname)
if (ifname) {
RS485_Set_Interface(ifname);
#if PRINT_ENABLED
fprintf(stderr,"MS/TP Interface: %s\n", ifname);
fprintf(stderr, "MS/TP Interface: %s\n", ifname);
#endif
}
RS485_Initialize();
@@ -790,12 +798,9 @@ bool dlmstp_init(char *ifname)
dlmstp_set_max_info_frames(DEFAULT_MAX_INFO_FRAMES);
#endif
#if PRINT_ENABLED
fprintf(stderr,"MS/TP MAC: %02X\n",
MSTP_Port.This_Station);
fprintf(stderr,"MS/TP Max_Master: %02X\n",
MSTP_Port.Nmax_master);
fprintf(stderr,"MS/TP Max_Info_Frames: %u\n",
MSTP_Port.Nmax_info_frames);
fprintf(stderr, "MS/TP MAC: %02X\n", MSTP_Port.This_Station);
fprintf(stderr, "MS/TP Max_Master: %02X\n", MSTP_Port.Nmax_master);
fprintf(stderr, "MS/TP Max_Info_Frames: %u\n", MSTP_Port.Nmax_info_frames);
#endif
/* start our MilliSec task */
@@ -812,16 +817,18 @@ bool dlmstp_init(char *ifname)
static char *Network_Interface = NULL;
int main(int argc, char *argv[])
int main(
int argc,
char *argv[])
{
uint16_t bytes_received = 0;
BACNET_ADDRESS src; /* source address */
uint8_t pdu[MAX_APDU]; /* PDU data */
uint8_t pdu[MAX_APDU]; /* PDU data */
#if (defined(MSTP_TEST_REQUEST) && MSTP_TEST_REQUEST)
struct timespec timeOut, remains;
timeOut.tv_sec = 1;
timeOut.tv_nsec = 0; /* 1 millisecond */
timeOut.tv_nsec = 0; /* 1 millisecond */
#endif
/* argv has the "/dev/ttyS0" or some other device */
if (argc > 1) {
@@ -835,18 +842,14 @@ int main(int argc, char *argv[])
/* forever task */
for (;;) {
#if (defined(MSTP_TEST_REQUEST) && MSTP_TEST_REQUEST)
MSTP_Create_And_Send_Frame(
&MSTP_Port,
MSTP_Create_And_Send_Frame(&MSTP_Port,
FRAME_TYPE_TEST_REQUEST,
MSTP_Port.SourceAddress,
MSTP_Port.This_Station,
NULL, 0);
MSTP_Port.SourceAddress, MSTP_Port.This_Station, NULL, 0);
nanosleep(&timeOut, &remains);
#endif
bytes_received =
dlmstp_receive(&src, &pdu[0], sizeof(pdu), 10000);
bytes_received = dlmstp_receive(&src, &pdu[0], sizeof(pdu), 10000);
if (bytes_received) {
fprintf(stderr,"Received NPDU!\n");
fprintf(stderr, "Received NPDU!\n");
}
}
return 0;
+47 -34
View File
@@ -32,8 +32,8 @@
-------------------------------------------
####COPYRIGHTEND####*/
#include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> /* for the standard bool type. */
#include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> /* for the standard bool type. */
#include "net.h"
#include "bacdef.h"
@@ -52,12 +52,14 @@ uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0 };
static int eth802_sockfd = -1; /* 802.2 file handle */
static struct sockaddr eth_addr = { 0 }; /* used for binding 802.2 */
bool ethernet_valid(void)
bool ethernet_valid(
void)
{
return (eth802_sockfd >= 0);
}
void ethernet_cleanup(void)
void ethernet_cleanup(
void)
{
if (ethernet_valid())
close(eth802_sockfd);
@@ -75,7 +77,8 @@ void ethernet_cleanup(void)
to EAGAIN (or EWOULDBLOCK).
Thanks to Bjorn Reese for this code.
----------------------------------------------------------------------*/
int setNonblocking(int fd)
int setNonblocking(
int fd)
{
int flags;
@@ -85,9 +88,11 @@ int setNonblocking(int fd)
}
/* 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;
fprintf(stderr, "ethernet: opening \"%s\"\n", interface_name);
@@ -122,15 +127,13 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
/* Clear the memory before copying */
memset(eth_addr->sa_data, '\0', sizeof(eth_addr->sa_data));
/* Strcpy the interface name into the address */
strncpy(eth_addr->sa_data, interface_name,
sizeof(eth_addr->sa_data) - 1);
strncpy(eth_addr->sa_data, interface_name, sizeof(eth_addr->sa_data) - 1);
fprintf(stderr, "ethernet: binding \"%s\"\n", eth_addr->sa_data);
/* Attempt to bind the socket to the interface */
if (bind(sock_fd, eth_addr, sizeof(struct sockaddr)) != 0) {
/* Bind problem, close socket and return */
fprintf(stderr,
"ethernet: Unable to bind 802.2 socket : %s\n",
strerror(errno));
"ethernet: Unable to bind 802.2 socket : %s\n", strerror(errno));
fprintf(stderr,
"You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n"
@@ -148,11 +151,13 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
}
/* function to find the local ethernet MAC address */
static int get_local_hwaddr(const char *ifname, unsigned char *mac)
static int get_local_hwaddr(
const char *ifname,
unsigned char *mac)
{
struct ifreq ifr;
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 */
strcpy(ifr.ifr_name, ifname);
@@ -161,14 +166,15 @@ static int get_local_hwaddr(const char *ifname, unsigned char *mac)
rv = fd;
else {
rv = ioctl(fd, SIOCGIFHWADDR, &ifr);
if (rv >= 0) /* worked okay */
if (rv >= 0) /* worked okay */
memcpy(mac, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
}
return rv;
}
bool ethernet_init(char *interface_name)
bool ethernet_init(
char *interface_name)
{
if (interface_name) {
get_local_hwaddr(interface_name, Ethernet_MAC_Address);
@@ -183,12 +189,13 @@ bool ethernet_init(char *interface_name)
/* function to send a packet out the 802.2 socket */
/* 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 */
BACNET_NPDU_DATA * npdu_data, /* network information */
uint8_t * pdu, /* any data to be sent - may be null */
uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len)
{ /* number of bytes of data */
int i = 0; /* counter */
{ /* number of bytes of data */
int i = 0; /* counter */
int bytes = 0;
BACNET_ADDRESS src = { 0 }; /* source address for npdu */
uint8_t mtu[MAX_MPDU] = { 0 }; /* our buffer */
@@ -226,9 +233,9 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */
return -3;
}
/* Logical PDU portion */
mtu[14] = 0x82; /* DSAP for BACnet */
mtu[15] = 0x82; /* SSAP for BACnet */
mtu[16] = 0x03; /* Control byte in header */
mtu[14] = 0x82; /* DSAP for BACnet */
mtu[15] = 0x82; /* SSAP for BACnet */
mtu[16] = 0x03; /* Control byte in header */
mtu_len = 17;
if ((mtu_len + pdu_len) > MAX_MPDU) {
fprintf(stderr, "ethernet: PDU is too big to send!\n");
@@ -253,11 +260,12 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */
/* receives an 802.2 framed packet */
/* returns the number of octets in the PDU, or zero on failure */
uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
uint16_t ethernet_receive(
BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout)
{ /* number of milliseconds to wait for a packet */
{ /* number of milliseconds to wait for a packet */
int received_bytes;
uint8_t buf[MAX_MPDU] = { 0 }; /* data */
uint16_t pdu_len = 0; /* return value */
@@ -334,7 +342,8 @@ uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */
return pdu_len;
}
void ethernet_set_my_address(BACNET_ADDRESS * my_address)
void ethernet_set_my_address(
BACNET_ADDRESS * my_address)
{
int i = 0;
@@ -345,7 +354,8 @@ void ethernet_set_my_address(BACNET_ADDRESS * my_address)
return;
}
void ethernet_get_my_address(BACNET_ADDRESS * my_address)
void ethernet_get_my_address(
BACNET_ADDRESS * my_address)
{
int i = 0;
@@ -363,9 +373,10 @@ void ethernet_get_my_address(BACNET_ADDRESS * my_address)
return;
}
void ethernet_get_broadcast_address(BACNET_ADDRESS * dest)
{ /* destination address */
int i = 0; /* counter */
void ethernet_get_broadcast_address(
BACNET_ADDRESS * dest)
{ /* destination address */
int i = 0; /* counter */
if (dest) {
for (i = 0; i < 6; i++) {
@@ -373,7 +384,7 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest)
}
dest->mac_len = 6;
dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; /* always zero when DNET is broadcast */
dest->len = 0; /* always zero when DNET is broadcast */
for (i = 0; i < MAX_MAC_LEN; i++) {
dest->adr[i] = 0;
}
@@ -382,9 +393,11 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest)
return;
}
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)
fprintf(stderr, "%s", info);
+29 -22
View File
@@ -50,8 +50,10 @@ bool Who_Is_Request = true;
/* buffers used for receiving */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
static void LocalIAmHandler(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
static void LocalIAmHandler(
uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src)
{
int len = 0;
uint32_t device_id = 0;
@@ -73,7 +75,8 @@ static void LocalIAmHandler(uint8_t * service_request,
return;
}
static void Read_Properties(void)
static void Read_Properties(
void)
{
uint32_t device_id = 0;
bool status = false;
@@ -154,13 +157,12 @@ static void Read_Properties(void)
return;
}
static void Init_Service_Handlers(void)
static void Init_Service_Handlers(
void)
{
/* we need to handle who-is to support dynamic device binding */
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is);
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler);
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is);
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, LocalIAmHandler);
/* set the handler for all the services we don't implement */
/* It is required to send the proper reject message... */
@@ -181,7 +183,8 @@ static void Init_Service_Handlers(void)
handler_atomic_read_file_ack);
}
static void print_address_cache(void)
static void print_address_cache(
void)
{
unsigned i, j;
BACNET_ADDRESS address;
@@ -202,7 +205,8 @@ static void print_address_cache(void)
}
}
static void print_tsm_stats(void)
static void print_tsm_stats(
void)
{
int idle = 0;
int total = 0;
@@ -212,7 +216,8 @@ static void print_tsm_stats(void)
fprintf(stderr, "TSM: %d idle of %d transactions\n", idle, total);
}
static void sig_handler(int signo)
static void sig_handler(
int signo)
{
datalink_cleanup();
print_address_cache();
@@ -221,12 +226,14 @@ static void sig_handler(int signo)
exit(0);
}
int main(int argc, char *argv[])
int main(
int argc,
char *argv[])
{
BACNET_ADDRESS src = { 0 }; /* address where message came from */
uint16_t pdu_len = 0;
unsigned timeout = 100; /* milliseconds */
unsigned count = 0; /* milliseconds */
unsigned count = 0; /* milliseconds */
time_t start_time;
time_t new_time = 0;
@@ -280,15 +287,15 @@ int main(int argc, char *argv[])
/* some round robin task switching */
count++;
switch (count) {
case 1:
/* used for testing, but kind of noisy on the network */
/*Read_Properties(); */
break;
case 2:
break;
default:
count = 0;
break;
case 1:
/* used for testing, but kind of noisy on the network */
/*Read_Properties(); */
break;
case 2:
break;
default:
count = 0;
break;
}
/* blink LEDs, Turn on or off outputs, etc */
+1 -1
View File
@@ -80,7 +80,7 @@
#include <net/route.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <features.h> /* for the glibc version number */
#include <features.h> /* for the glibc version number */
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h> /* the L2 protocols */
+54 -42
View File
@@ -76,7 +76,8 @@ static struct termios RS485_oldtio;
* ALGORITHM: none
* NOTES: none
*********************************************************************/
void RS485_Set_Interface(char *ifname)
void RS485_Set_Interface(
char *ifname)
{
/* note: expects a constant char, or char from the heap */
RS485_Port_Name = ifname;
@@ -88,15 +89,21 @@ void RS485_Set_Interface(char *ifname)
* ALGORITHM: none
* NOTES: none
*****************************************************************************/
uint32_t RS485_Get_Baud_Rate(void)
uint32_t RS485_Get_Baud_Rate(
void)
{
switch (RS485_Baud) {
case B19200: return 19200;
case B38400: return 38400;
case B57600: return 57600;
case B115200: return 115200;
case B19200:
return 19200;
case B38400:
return 38400;
case B57600:
return 57600;
case B115200:
return 115200;
default:
case B9600: return 9600;
case B9600:
return 9600;
}
}
@@ -106,7 +113,8 @@ uint32_t RS485_Get_Baud_Rate(void)
* ALGORITHM: none
* NOTES: none
*****************************************************************************/
bool RS485_Set_Baud_Rate(uint32_t baud)
bool RS485_Set_Baud_Rate(
uint32_t baud)
{
bool valid = true;
@@ -140,10 +148,10 @@ bool RS485_Set_Baud_Rate(uint32_t baud)
/* Transmits a Frame on the wire */
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) */
uint16_t nbytes) /* number of bytes of data (up to 501) */
{
struct mstp_port_struct_t *mstp_port, /* port specific data */
uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
uint16_t nbytes)
{ /* number of bytes of data (up to 501) */
uint8_t turnaround_time;
uint32_t baud;
ssize_t written = 0;
@@ -163,12 +171,12 @@ void RS485_Send_Frame(
};
}
/*
On success, the number of bytes written are returned (zero indicates
nothing was written). On error, -1 is returned, and errno is set
appropriately. If count is zero and the file descriptor refers to a
regular file, 0 will be returned without causing any other effect. For
a special file, the results are not portable.
*/
On success, the number of bytes written are returned (zero indicates
nothing was written). On error, -1 is returned, and errno is set
appropriately. If count is zero and the file descriptor refers to a
regular file, 0 will be returned without causing any other effect. For
a special file, the results are not portable.
*/
written = write(RS485_Handle, buffer, nbytes);
/* per MSTP spec, sort of */
@@ -180,18 +188,19 @@ void RS485_Send_Frame(
}
/* 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)
{
uint8_t buf[1];
int count;
if (mstp_port->ReceiveError == true) {
/* wait for state machine to clear this */
}
/* wait for state machine to read from the DataRegister */
else if (mstp_port->DataAvailable == false) {
/* check for data */
count = read(RS485_Handle,buf,sizeof(buf));
count = read(RS485_Handle, buf, sizeof(buf));
/* if error, */
/* ReceiveError = TRUE; */
/* return; */
@@ -203,23 +212,24 @@ void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port)
}
}
void RS485_Cleanup(void)
void RS485_Cleanup(
void)
{
/* restore the old port settings */
tcsetattr(RS485_Handle,TCSANOW,&RS485_oldtio);
tcsetattr(RS485_Handle, TCSANOW, &RS485_oldtio);
close(RS485_Handle);
}
void RS485_Initialize(void)
void RS485_Initialize(
void)
{
struct termios newtio;
/*
Open device for reading and writing.
*/
RS485_Handle = open(RS485_Port_Name,
O_RDWR | O_NOCTTY | O_NDELAY );
Open device for reading and writing.
*/
RS485_Handle = open(RS485_Port_Name, O_RDWR | O_NOCTTY | O_NDELAY);
if (RS485_Handle < 0) {
perror(RS485_Port_Name);
exit(-1);
@@ -232,17 +242,17 @@ void RS485_Initialize(void)
fcntl(RS485_Handle, F_SETFL, 0);
#endif
/* save current serial port settings */
tcgetattr(RS485_Handle,&RS485_oldtio);
tcgetattr(RS485_Handle, &RS485_oldtio);
/* clear struct for new port settings */
bzero(&newtio, sizeof(newtio));
/*
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/
BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
CRTSCTS : output hardware flow control (only used if the cable has
all necessary lines. See sect. 7 of Serial-HOWTO)
CS8 : 8n1 (8bit,no parity,1 stopbit)
CLOCAL : local connection, no modem contol
CREAD : enable receiving characters
*/
newtio.c_cflag = RS485_Baud | CS8 | CLOCAL | CREAD;
/* Raw input */
newtio.c_iflag = 0;
@@ -251,18 +261,20 @@ void RS485_Initialize(void)
/* no processing */
newtio.c_lflag = 0;
/* activate the settings for the port after flushing I/O */
tcsetattr(RS485_Handle,TCSAFLUSH,&newtio);
tcsetattr(RS485_Handle, TCSAFLUSH, &newtio);
/* destructor */
atexit(RS485_Cleanup);
}
#ifdef TEST_RS485
#include <string.h>
int main(int argc, char *argv[])
int main(
int argc,
char *argv[])
{
uint8_t buf[8];
char *wbuf = {"BACnet!"};
size_t wlen = strlen(wbuf)+1;
char *wbuf = { "BACnet!" };
size_t wlen = strlen(wbuf) + 1;
unsigned i = 0;
size_t written = 0;
int rlen;
@@ -276,11 +288,11 @@ int main(int argc, char *argv[])
for (;;) {
written = write(RS485_Handle, wbuf, wlen);
rlen = read(RS485_Handle,buf,sizeof(buf));
rlen = read(RS485_Handle, buf, sizeof(buf));
/* print any characters received */
if (rlen > 0) {
for (i = 0; i < rlen; i++) {
fprintf(stderr,"%02X ",buf[i]);
fprintf(stderr, "%02X ", buf[i]);
}
}
}
+14 -8
View File
@@ -41,23 +41,29 @@
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */
void RS485_Set_Interface(char *ifname);
void RS485_Set_Interface(
char *ifname);
void RS485_Initialize(void);
void RS485_Initialize(
void);
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) */
uint16_t nbytes); /* number of bytes of data (up to 501) */
void RS485_Check_UART_Data(
volatile struct mstp_port_struct_t *mstp_port); /* port specific data */
uint32_t RS485_Get_Baud_Rate(void);
bool RS485_Set_Baud_Rate(uint32_t baud);
void RS485_Cleanup(void);
uint32_t RS485_Get_Baud_Rate(
void);
bool RS485_Set_Baud_Rate(
uint32_t baud);
void RS485_Cleanup(
void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */
#endif
+37 -35
View File
@@ -58,23 +58,27 @@ static uint8_t RxBuffer[MAX_MPDU];
static uint8_t TxBuffer[MAX_MPDU];
static uint16_t SilenceTime;
#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;}
static uint16_t Timer_Silence(void)
static uint16_t Timer_Silence(
void)
{
return SilenceTime;
}
static void Timer_Silence_Reset(void)
static void Timer_Silence_Reset(
void)
{
SilenceTime = 0;
}
static void dlmstp_millisecond_timer(void)
static void dlmstp_millisecond_timer(
void)
{
INCREMENT_AND_LIMIT_UINT16(SilenceTime);
}
void *milliseconds_task(void *pArg)
void *milliseconds_task(
void *pArg)
{
struct timespec timeOut,remains;
struct timespec timeOut, remains;
timeOut.tv_sec = 0;
timeOut.tv_nsec = 10000000; /* 1 milliseconds */
@@ -89,35 +93,36 @@ void *milliseconds_task(void *pArg)
/* functions used by the MS/TP state machine to put or get data */
uint16_t MSTP_Put_Receive(
volatile struct mstp_port_struct_t *mstp_port)
volatile struct mstp_port_struct_t * mstp_port)
{
(void)mstp_port;
(void) mstp_port;
return 0;
}
/* for the MS/TP state machine to use for getting data to send */
/* Return: amount of PDU data */
uint16_t MSTP_Get_Send(
volatile struct mstp_port_struct_t *mstp_port,
unsigned timeout) /* milliseconds to wait for a packet */
{
(void)mstp_port;
(void)timeout;
volatile struct mstp_port_struct_t * mstp_port,
unsigned timeout)
{ /* milliseconds to wait for a packet */
(void) mstp_port;
(void) timeout;
return 0;
}
uint16_t MSTP_Get_Reply(
volatile struct mstp_port_struct_t *mstp_port,
unsigned timeout) /* milliseconds to wait for a packet */
{
(void)mstp_port;
(void)timeout;
volatile struct mstp_port_struct_t * mstp_port,
unsigned timeout)
{ /* milliseconds to wait for a packet */
(void) mstp_port;
(void) timeout;
return 0;
}
/* returns a delta timestamp */
int timestamp_ms(void)
int timestamp_ms(
void)
{
struct timeval tv;
int delta_ticks = 0;
@@ -125,7 +130,7 @@ int timestamp_ms(void)
static long last_ticks = 0;
int rv = 0;
rv = gettimeofday(&tv,NULL);
rv = gettimeofday(&tv, NULL);
if (rv == -1)
ticks = 0;
else
@@ -144,7 +149,7 @@ static void print_received_packet(
int timestamp = 0;
timestamp = timestamp_ms();
fprintf(stderr,"%03d ",timestamp);
fprintf(stderr, "%03d ", timestamp);
/* Preamble: two octet preamble: X`55', X`FF' */
/* Frame Type: one octet */
/* Destination Address: one octet address */
@@ -161,25 +166,23 @@ static void print_received_packet(
mstp_port->DestinationAddress,
mstp_port->SourceAddress,
HI_BYTE(mstp_port->DataLength),
LO_BYTE(mstp_port->DataLength),
mstp_port->HeaderCRCActual);
LO_BYTE(mstp_port->DataLength), mstp_port->HeaderCRCActual);
if (mstp_port->DataLength) {
for (i = 0; i < mstp_port->DataLength; i++) {
fprintf(stderr,"%02X ",
mstp_port->InputBuffer[i]);
fprintf(stderr, "%02X ", mstp_port->InputBuffer[i]);
}
fprintf(stderr,
"%02X %02X ",
mstp_port->DataCRCActualMSB,
mstp_port->DataCRCActualLSB);
"%02X %02X ",
mstp_port->DataCRCActualMSB, mstp_port->DataCRCActualLSB);
}
fprintf(stderr,"%s",
mstptext_frame_type(mstp_port->FrameType));
fprintf(stderr,"\n");
fprintf(stderr, "%s", mstptext_frame_type(mstp_port->FrameType));
fprintf(stderr, "\n");
}
/* simple test to packetize the data and print it */
int main(int argc, char *argv[])
int main(
int argc,
char *argv[])
{
volatile struct mstp_port_struct_t *mstp_port;
int rc = 0;
@@ -195,7 +198,7 @@ int main(int argc, char *argv[])
if (argc > 2) {
my_mac = strtol(argv[2], NULL, 0);
if (my_mac > 127)
my_mac = 127;
my_mac = 127;
}
RS485_Set_Baud_Rate(38400);
RS485_Initialize();
@@ -229,4 +232,3 @@ int main(int argc, char *argv[])
return 0;
}