BACnet router added.
This commit is contained in:
@@ -78,7 +78,7 @@ static int get_local_ifr_ioctl(
|
||||
return rv;
|
||||
}
|
||||
|
||||
static int get_local_address_ioctl(
|
||||
int get_local_address_ioctl(
|
||||
char *ifname,
|
||||
struct in_addr *addr,
|
||||
int request)
|
||||
|
||||
@@ -100,7 +100,7 @@ static uint8_t Tusage_timeout = 60;
|
||||
static struct timeval start;
|
||||
|
||||
static uint32_t Timer_Silence(
|
||||
void)
|
||||
void * pArg)
|
||||
{
|
||||
struct timeval now, tmp_diff;
|
||||
int32_t res;
|
||||
@@ -113,7 +113,7 @@ static uint32_t Timer_Silence(
|
||||
}
|
||||
|
||||
static void Timer_Silence_Reset(
|
||||
void)
|
||||
void * pArg)
|
||||
{
|
||||
gettimeofday(&start, NULL);
|
||||
}
|
||||
@@ -221,7 +221,7 @@ static void *dlmstp_master_fsm_task(
|
||||
if (MSTP_Port.ReceivedValidFrame || MSTP_Port.ReceivedInvalidFrame) {
|
||||
run_master = true;
|
||||
} else {
|
||||
silence = MSTP_Port.SilenceTimer();
|
||||
silence = MSTP_Port.SilenceTimer(NULL);
|
||||
switch (MSTP_Port.master_state) {
|
||||
case MSTP_MASTER_STATE_IDLE:
|
||||
if (silence >= Tno_token)
|
||||
|
||||
@@ -0,0 +1,978 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2008 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*********************************************************************/
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include "bacdef.h"
|
||||
#include "bacaddr.h"
|
||||
#include "mstp.h"
|
||||
//#include "dlmstp.h"
|
||||
#include "dlmstp_linux.h"
|
||||
#include "rs485.h"
|
||||
#include "npdu.h"
|
||||
#include "bits.h"
|
||||
/* OS Specific include */
|
||||
#include "net.h"
|
||||
#include "ringbuf.h"
|
||||
|
||||
/** @file linux/dlmstp.c Provides Linux-specific DataLink functions for MS/TP. */
|
||||
|
||||
#define BACNET_PDU_CONTROL_BYTE_OFFSET 1
|
||||
#define BACNET_DATA_EXPECTING_REPLY_BIT 2
|
||||
#define BACNET_DATA_EXPECTING_REPLY(control) ( (control & (1 << BACNET_DATA_EXPECTING_REPLY_BIT) ) > 0 )
|
||||
|
||||
#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;}
|
||||
uint32_t Timer_Silence(
|
||||
void * poPort)
|
||||
{
|
||||
struct timeval now, tmp_diff;
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int32_t res;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
timersub(&poSharedData->start, &now, &tmp_diff);
|
||||
res = ((tmp_diff.tv_sec) * 1000 + (tmp_diff.tv_usec) / 1000);
|
||||
|
||||
return (res >= 0 ? res : -res);
|
||||
}
|
||||
|
||||
void Timer_Silence_Reset(
|
||||
void * poPort)
|
||||
{
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return;
|
||||
}
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
gettimeofday(&poSharedData->start, NULL);
|
||||
}
|
||||
|
||||
void get_abstime(
|
||||
struct timespec *abstime,
|
||||
unsigned long milliseconds)
|
||||
{
|
||||
struct timeval now, offset, result;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
offset.tv_sec = 0;
|
||||
offset.tv_usec = milliseconds * 1000;
|
||||
timeradd(&now, &offset, &result);
|
||||
abstime->tv_sec = result.tv_sec;
|
||||
abstime->tv_nsec = result.tv_usec * 1000;
|
||||
}
|
||||
|
||||
void dlmstp_cleanup(
|
||||
void * poPort)
|
||||
{
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return;
|
||||
}
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* restore the old port settings */
|
||||
tcsetattr(poSharedData->RS485_Handle, TCSANOW, &poSharedData->RS485_oldtio);
|
||||
close(poSharedData->RS485_Handle);
|
||||
|
||||
pthread_cond_destroy(&poSharedData->Received_Frame_Flag);
|
||||
pthread_cond_destroy(&poSharedData->Receive_Packet_Flag);
|
||||
pthread_cond_destroy(&poSharedData->Master_Done_Flag);
|
||||
pthread_mutex_destroy(&poSharedData->Received_Frame_Mutex);
|
||||
pthread_mutex_destroy(&poSharedData->Receive_Packet_Mutex);
|
||||
pthread_mutex_destroy(&poSharedData->Master_Done_Mutex);
|
||||
}
|
||||
|
||||
/* returns number of bytes sent on success, zero on failure */
|
||||
int dlmstp_send_pdu(
|
||||
void * poPort,
|
||||
BACNET_ADDRESS * dest, /* destination address */
|
||||
uint8_t * pdu, /* any data to be sent - may be null */
|
||||
unsigned pdu_len)
|
||||
{ /* number of bytes of data */
|
||||
int bytes_sent = 0;
|
||||
struct mstp_pdu_packet *pkt;
|
||||
unsigned i = 0;
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Alloc(&poSharedData->PDU_Queue);
|
||||
if (pkt) {
|
||||
pkt->data_expecting_reply = BACNET_DATA_EXPECTING_REPLY(pdu[BACNET_PDU_CONTROL_BYTE_OFFSET]);
|
||||
for (i = 0; i < pdu_len; i++) {
|
||||
pkt->buffer[i] = pdu[i];
|
||||
}
|
||||
pkt->length = pdu_len;
|
||||
pkt->destination_mac = dest->mac[0];
|
||||
bytes_sent = pdu_len;
|
||||
}
|
||||
|
||||
return bytes_sent;
|
||||
}
|
||||
|
||||
uint16_t dlmstp_receive(
|
||||
void * poPort,
|
||||
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;
|
||||
struct timespec abstime;
|
||||
int rv = 0;
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
(void) max_pdu;
|
||||
/* see if there is a packet available, and a place
|
||||
to put the reply (if necessary) and process it */
|
||||
get_abstime(&abstime, timeout);
|
||||
rv = pthread_cond_timedwait(&poSharedData->Receive_Packet_Flag, &poSharedData->Receive_Packet_Mutex,
|
||||
&abstime);
|
||||
if (rv == 0) {
|
||||
if (poSharedData->Receive_Packet.ready) {
|
||||
if (poSharedData->Receive_Packet.pdu_len) {
|
||||
poSharedData->MSTP_Packets++;
|
||||
if (src) {
|
||||
memmove(src, &poSharedData->Receive_Packet.address,
|
||||
sizeof(poSharedData->Receive_Packet.address));
|
||||
}
|
||||
if (pdu) {
|
||||
memmove(pdu, &poSharedData->Receive_Packet.pdu,
|
||||
sizeof(poSharedData->Receive_Packet.pdu));
|
||||
}
|
||||
pdu_len = poSharedData->Receive_Packet.pdu_len;
|
||||
}
|
||||
poSharedData->Receive_Packet.ready = false;
|
||||
}
|
||||
}
|
||||
|
||||
return pdu_len;
|
||||
}
|
||||
|
||||
void *dlmstp_receive_fsm_task(
|
||||
void *pArg)
|
||||
{
|
||||
bool received_frame;
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t*)pArg;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
poSharedData = (SHARED_MSTP_DATA *) ((struct mstp_port_struct_t*)pArg)->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
/* only do receive state machine while we don't have a frame */
|
||||
if ((mstp_port->ReceivedValidFrame == false) &&
|
||||
(mstp_port->ReceivedInvalidFrame == false)) {
|
||||
do {
|
||||
RS485_Check_UART_Data(mstp_port);
|
||||
MSTP_Receive_Frame_FSM((volatile struct mstp_port_struct_t*)pArg);
|
||||
received_frame = mstp_port->ReceivedValidFrame ||
|
||||
mstp_port->ReceivedInvalidFrame;
|
||||
if (received_frame) {
|
||||
pthread_cond_signal(&poSharedData->Received_Frame_Flag);
|
||||
break;
|
||||
}
|
||||
} while (mstp_port->DataAvailable);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *dlmstp_master_fsm_task(
|
||||
void *pArg)
|
||||
{
|
||||
uint32_t silence = 0;
|
||||
bool run_master = false;
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t*)pArg;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
poSharedData = (SHARED_MSTP_DATA *) ((struct mstp_port_struct_t*)pArg)->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
if (mstp_port->ReceivedValidFrame == false &&
|
||||
mstp_port->ReceivedInvalidFrame == false) {
|
||||
RS485_Check_UART_Data(mstp_port);
|
||||
MSTP_Receive_Frame_FSM(mstp_port);
|
||||
}
|
||||
if (mstp_port->ReceivedValidFrame || mstp_port->ReceivedInvalidFrame) {
|
||||
run_master = true;
|
||||
} else {
|
||||
silence = mstp_port->SilenceTimer(NULL);
|
||||
switch (mstp_port->master_state) {
|
||||
case MSTP_MASTER_STATE_IDLE:
|
||||
if (silence >= Tno_token)
|
||||
run_master = true;
|
||||
break;
|
||||
case MSTP_MASTER_STATE_WAIT_FOR_REPLY:
|
||||
if (silence >= poSharedData->Treply_timeout)
|
||||
run_master = true;
|
||||
break;
|
||||
case MSTP_MASTER_STATE_POLL_FOR_MASTER:
|
||||
if (silence >= poSharedData->Tusage_timeout)
|
||||
run_master = true;
|
||||
break;
|
||||
default:
|
||||
run_master = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (run_master) {
|
||||
if (mstp_port->This_Station <= DEFAULT_MAX_MASTER) {
|
||||
while (MSTP_Master_Node_FSM(mstp_port)) {
|
||||
/* do nothing while immediate transitioning */
|
||||
}
|
||||
} else if (mstp_port->This_Station < 255) {
|
||||
MSTP_Slave_Node_FSM(mstp_port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void dlmstp_fill_bacnet_address(
|
||||
BACNET_ADDRESS * src,
|
||||
uint8_t mstp_address)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
if (mstp_address == MSTP_BROADCAST_ADDRESS) {
|
||||
/* mac_len = 0 if broadcast address */
|
||||
src->mac_len = 0;
|
||||
src->mac[0] = 0;
|
||||
} else {
|
||||
src->mac_len = 1;
|
||||
src->mac[0] = mstp_address;
|
||||
}
|
||||
/* fill with 0's starting with index 1; index 0 filled above */
|
||||
for (i = 1; i < MAX_MAC_LEN; i++) {
|
||||
src->mac[i] = 0;
|
||||
}
|
||||
src->net = 0;
|
||||
src->len = 0;
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
src->adr[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
uint16_t pdu_len = 0;
|
||||
SHARED_MSTP_DATA * poSharedData = (SHARED_MSTP_DATA*)mstp_port->UserData;
|
||||
|
||||
if(!poSharedData)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!poSharedData->Receive_Packet.ready) {
|
||||
/* bounds check - maybe this should send an abort? */
|
||||
pdu_len = mstp_port->DataLength;
|
||||
if (pdu_len > sizeof(poSharedData->Receive_Packet.pdu))
|
||||
pdu_len = sizeof(poSharedData->Receive_Packet.pdu);
|
||||
memmove((void *) &poSharedData->Receive_Packet.pdu[0],
|
||||
(void *) &mstp_port->InputBuffer[0], pdu_len);
|
||||
dlmstp_fill_bacnet_address(&poSharedData->Receive_Packet.address,
|
||||
mstp_port->SourceAddress);
|
||||
poSharedData->Receive_Packet.pdu_len = mstp_port->DataLength;
|
||||
poSharedData->Receive_Packet.ready = true;
|
||||
pthread_cond_signal(&poSharedData->Receive_Packet_Flag);
|
||||
}
|
||||
|
||||
return pdu_len;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
uint8_t frame_type = 0;
|
||||
struct mstp_pdu_packet *pkt;
|
||||
SHARED_MSTP_DATA * poSharedData = (SHARED_MSTP_DATA*)mstp_port->UserData;
|
||||
|
||||
if(!poSharedData)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
(void) timeout;
|
||||
if (Ringbuf_Empty(&poSharedData->PDU_Queue)) {
|
||||
return 0;
|
||||
}
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Pop_Front(&poSharedData->PDU_Queue);
|
||||
if (pkt->data_expecting_reply) {
|
||||
frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY;
|
||||
} else {
|
||||
frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY;
|
||||
}
|
||||
/* convert the PDU into the MSTP Frame */
|
||||
pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
|
||||
mstp_port->OutputBufferSize, frame_type, pkt->destination_mac,
|
||||
mstp_port->This_Station, (uint8_t *) & pkt->buffer[0], pkt->length);
|
||||
|
||||
return pdu_len;
|
||||
}
|
||||
|
||||
bool dlmstp_compare_data_expecting_reply(
|
||||
uint8_t * request_pdu,
|
||||
uint16_t request_pdu_len,
|
||||
uint8_t src_address,
|
||||
uint8_t * reply_pdu,
|
||||
uint16_t reply_pdu_len,
|
||||
uint8_t dest_address)
|
||||
{
|
||||
uint16_t offset;
|
||||
/* One way to check the message is to compare NPDU
|
||||
src, dest, along with the APDU type, invoke id.
|
||||
Seems a bit overkill */
|
||||
struct DER_compare_t {
|
||||
BACNET_NPDU_DATA npdu_data;
|
||||
BACNET_ADDRESS address;
|
||||
uint8_t pdu_type;
|
||||
uint8_t invoke_id;
|
||||
uint8_t service_choice;
|
||||
};
|
||||
struct DER_compare_t request;
|
||||
struct DER_compare_t reply;
|
||||
|
||||
/* unused parameters */
|
||||
request_pdu_len = request_pdu_len;
|
||||
reply_pdu_len = reply_pdu_len;
|
||||
/* decode the request data */
|
||||
request.address.mac[0] = src_address;
|
||||
request.address.mac_len = 1;
|
||||
offset =
|
||||
npdu_decode(&request_pdu[0], NULL, &request.address,
|
||||
&request.npdu_data);
|
||||
if (request.npdu_data.network_layer_message) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,
|
||||
"DLMSTP: DER Compare failed: " "Request is Network message.\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
request.pdu_type = request_pdu[offset] & 0xF0;
|
||||
if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,
|
||||
"DLMSTP: DER Compare failed: " "Not Confirmed Request.\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
request.invoke_id = request_pdu[offset + 2];
|
||||
/* segmented message? */
|
||||
if (request_pdu[offset] & BIT3) {
|
||||
request.service_choice = request_pdu[offset + 5];
|
||||
} else {
|
||||
request.service_choice = request_pdu[offset + 3];
|
||||
}
|
||||
/* decode the reply data */
|
||||
reply.address.mac[0] = dest_address;
|
||||
reply.address.mac_len = 1;
|
||||
offset =
|
||||
npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data);
|
||||
if (reply.npdu_data.network_layer_message) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,
|
||||
"DLMSTP: DER Compare failed: " "Reply is Network message.\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
/* reply could be a lot of things:
|
||||
confirmed, simple ack, abort, reject, error */
|
||||
reply.pdu_type = reply_pdu[offset] & 0xF0;
|
||||
switch (reply.pdu_type) {
|
||||
case PDU_TYPE_CONFIRMED_SERVICE_REQUEST:
|
||||
reply.invoke_id = reply_pdu[offset + 2];
|
||||
/* segmented message? */
|
||||
if (reply_pdu[offset] & BIT3) {
|
||||
reply.service_choice = reply_pdu[offset + 5];
|
||||
} else {
|
||||
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];
|
||||
break;
|
||||
case PDU_TYPE_COMPLEX_ACK:
|
||||
reply.invoke_id = reply_pdu[offset + 1];
|
||||
/* segmented message? */
|
||||
if (reply_pdu[offset] & BIT3) {
|
||||
reply.service_choice = reply_pdu[offset + 4];
|
||||
} else {
|
||||
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];
|
||||
break;
|
||||
case PDU_TYPE_REJECT:
|
||||
case PDU_TYPE_ABORT:
|
||||
reply.invoke_id = reply_pdu[offset + 1];
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
/* these don't have service choice included */
|
||||
if ((reply.pdu_type == PDU_TYPE_REJECT) ||
|
||||
(reply.pdu_type == PDU_TYPE_ABORT)) {
|
||||
if (request.invoke_id != reply.invoke_id) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,
|
||||
"DLMSTP: DER Compare failed: " "Invoke ID mismatch.\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (request.invoke_id != reply.invoke_id) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,
|
||||
"DLMSTP: DER Compare failed: " "Invoke ID mismatch.\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
if (request.service_choice != reply.service_choice) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,
|
||||
"DLMSTP: DER Compare failed: " "Service choice mismatch.\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,
|
||||
"DLMSTP: DER Compare failed: "
|
||||
"NPDU Protocol Version mismatch.\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
if (request.npdu_data.priority != reply.npdu_data.priority) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,
|
||||
"DLMSTP: DER Compare failed: " "NPDU Priority mismatch.\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
if (!bacnet_address_same(&request.address, &reply.address)) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr,
|
||||
"DLMSTP: DER Compare failed: " "BACnet Address mismatch.\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Get the reply to a DATA_EXPECTING_REPLY frame, or nothing */
|
||||
uint16_t MSTP_Get_Reply(
|
||||
volatile struct mstp_port_struct_t * mstp_port,
|
||||
unsigned timeout)
|
||||
{ /* milliseconds to wait for a packet */
|
||||
uint16_t pdu_len = 0; /* return value */
|
||||
bool matched = false;
|
||||
uint8_t frame_type = 0;
|
||||
struct mstp_pdu_packet *pkt;
|
||||
SHARED_MSTP_DATA * poSharedData = (SHARED_MSTP_DATA*)mstp_port->UserData;
|
||||
|
||||
if(!poSharedData)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (Ringbuf_Empty(&poSharedData->PDU_Queue)) {
|
||||
return 0;
|
||||
}
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Get_Front(&poSharedData->PDU_Queue);
|
||||
/* is this the reply to the DER? */
|
||||
matched =
|
||||
dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0],
|
||||
mstp_port->DataLength, mstp_port->SourceAddress,
|
||||
(uint8_t *) & pkt->buffer[0], pkt->length, pkt->destination_mac);
|
||||
if (!matched) {
|
||||
return 0;
|
||||
}
|
||||
pkt = (struct mstp_pdu_packet *) Ringbuf_Pop_Front(&poSharedData->PDU_Queue);
|
||||
if (pkt->data_expecting_reply) {
|
||||
frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY;
|
||||
} else {
|
||||
frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY;
|
||||
}
|
||||
/* convert the PDU into the MSTP Frame */
|
||||
pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
|
||||
mstp_port->OutputBufferSize, frame_type, pkt->destination_mac,
|
||||
mstp_port->This_Station, (uint8_t *) & pkt->buffer[0], pkt->length);
|
||||
|
||||
return pdu_len;
|
||||
}
|
||||
|
||||
void dlmstp_set_mac_address(
|
||||
void * poPort,
|
||||
uint8_t mac_address)
|
||||
{
|
||||
// SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return;
|
||||
}
|
||||
/*
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
*/
|
||||
/* Master Nodes can only have address 0-127 */
|
||||
if (mac_address <= 127) {
|
||||
mstp_port->This_Station = mac_address;
|
||||
/* FIXME: implement your data storage */
|
||||
/* I2C_Write_Byte(
|
||||
EEPROM_DEVICE_ADDRESS,
|
||||
mac_address,
|
||||
EEPROM_MSTP_MAC_ADDR); */
|
||||
if (mac_address > mstp_port->Nmax_master)
|
||||
dlmstp_set_max_master(mstp_port, mac_address);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t dlmstp_mac_address(
|
||||
void * poPort)
|
||||
{
|
||||
// SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
return mstp_port->This_Station;
|
||||
}
|
||||
|
||||
/* This parameter represents the value of the Max_Info_Frames property of */
|
||||
/* the node's Device object. The value of Max_Info_Frames specifies the */
|
||||
/* maximum number of information frames the node may send before it must */
|
||||
/* pass the token. Max_Info_Frames may have different values on different */
|
||||
/* 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(
|
||||
void * poPort,
|
||||
uint8_t max_info_frames)
|
||||
{
|
||||
// SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return;
|
||||
}
|
||||
/*
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
*/
|
||||
if (max_info_frames >= 1) {
|
||||
mstp_port->Nmax_info_frames = max_info_frames;
|
||||
/* FIXME: implement your data storage */
|
||||
/* I2C_Write_Byte(
|
||||
EEPROM_DEVICE_ADDRESS,
|
||||
(uint8_t)max_info_frames,
|
||||
EEPROM_MSTP_MAX_INFO_FRAMES_ADDR); */
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t dlmstp_max_info_frames(
|
||||
void * poPort)
|
||||
{
|
||||
// SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
return mstp_port->Nmax_info_frames;
|
||||
}
|
||||
|
||||
/* This parameter represents the value of the Max_Master property of the */
|
||||
/* node's Device object. The value of Max_Master specifies the highest */
|
||||
/* 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(
|
||||
void * poPort,
|
||||
uint8_t max_master)
|
||||
{
|
||||
// SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return;
|
||||
}
|
||||
/*
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
*/
|
||||
if (max_master <= 127) {
|
||||
if (mstp_port->This_Station <= max_master) {
|
||||
mstp_port->Nmax_master = max_master;
|
||||
/* FIXME: implement your data storage */
|
||||
/* I2C_Write_Byte(
|
||||
EEPROM_DEVICE_ADDRESS,
|
||||
max_master,
|
||||
EEPROM_MSTP_MAX_MASTER_ADDR); */
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t dlmstp_max_master(
|
||||
void * poPort)
|
||||
{
|
||||
// SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
return mstp_port->Nmax_master;
|
||||
}
|
||||
|
||||
/* RS485 Baud Rate 9600, 19200, 38400, 57600, 115200 */
|
||||
void dlmstp_set_baud_rate(
|
||||
void * poPort,
|
||||
uint32_t baud)
|
||||
{
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return;
|
||||
}
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (baud) {
|
||||
case 9600:
|
||||
poSharedData->RS485_Baud = B9600;
|
||||
break;
|
||||
case 19200:
|
||||
poSharedData->RS485_Baud = B19200;
|
||||
break;
|
||||
case 38400:
|
||||
poSharedData->RS485_Baud = B38400;
|
||||
break;
|
||||
case 57600:
|
||||
poSharedData->RS485_Baud = B57600;
|
||||
break;
|
||||
case 115200:
|
||||
poSharedData->RS485_Baud = B115200;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t dlmstp_baud_rate(
|
||||
void * poPort)
|
||||
{
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (poSharedData->RS485_Baud) {
|
||||
case B19200:
|
||||
return 19200;
|
||||
case B38400:
|
||||
return 38400;
|
||||
case B57600:
|
||||
return 57600;
|
||||
case B115200:
|
||||
return 115200;
|
||||
default:
|
||||
case B9600:
|
||||
return 9600;
|
||||
}
|
||||
}
|
||||
|
||||
void dlmstp_get_my_address(
|
||||
void * poPort,
|
||||
BACNET_ADDRESS * my_address)
|
||||
{
|
||||
int i = 0; /* counter */
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t *) poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return;
|
||||
}
|
||||
poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
my_address->mac_len = 1;
|
||||
my_address->mac[0] = mstp_port->This_Station;
|
||||
my_address->net = 0; /* local only, no routing */
|
||||
my_address->len = 0;
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
my_address->adr[i] = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
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 */
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
dest->adr[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool dlmstp_init(
|
||||
void * poPort,
|
||||
char *ifname)
|
||||
{
|
||||
unsigned long hThread = 0;
|
||||
int rv = 0;
|
||||
SHARED_MSTP_DATA * poSharedData;
|
||||
struct mstp_port_struct_t * mstp_port = (struct mstp_port_struct_t*)poPort;
|
||||
if(!mstp_port)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
poSharedData = (SHARED_MSTP_DATA *) ((struct mstp_port_struct_t*)mstp_port)->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
poSharedData->RS485_Port_Name = ifname;
|
||||
/* initialize PDU queue */
|
||||
Ringbuf_Init(&poSharedData->PDU_Queue, (uint8_t *) & poSharedData->PDU_Buffer,
|
||||
sizeof(struct mstp_pdu_packet), MSTP_PDU_PACKET_COUNT);
|
||||
/* initialize packet queue */
|
||||
poSharedData->Receive_Packet.ready = false;
|
||||
poSharedData->Receive_Packet.pdu_len = 0;
|
||||
rv = pthread_cond_init(&poSharedData->Receive_Packet_Flag, NULL);
|
||||
if (rv != 0) {
|
||||
fprintf(stderr,
|
||||
"MS/TP Interface: %s\n cannot allocate PThread Condition.\n",
|
||||
ifname);
|
||||
exit(1);
|
||||
}
|
||||
rv = pthread_mutex_init(&poSharedData->Receive_Packet_Mutex, NULL);
|
||||
if (rv != 0) {
|
||||
fprintf(stderr,
|
||||
"MS/TP Interface: %s\n cannot allocate PThread Mutex.\n", ifname);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
struct termios newtio;
|
||||
printf("RS485: Initializing %s", poSharedData->RS485_Port_Name);
|
||||
/*
|
||||
Open device for reading and writing.
|
||||
Blocking mode - more CPU effecient
|
||||
*/
|
||||
poSharedData->RS485_Handle = open(poSharedData->RS485_Port_Name, O_RDWR | O_NOCTTY | O_NONBLOCK/*| O_NDELAY */ );
|
||||
if (poSharedData->RS485_Handle < 0) {
|
||||
perror(poSharedData->RS485_Port_Name);
|
||||
exit(-1);
|
||||
}
|
||||
#if 0
|
||||
/* non blocking for the read */
|
||||
fcntl(poSharedData->RS485_Handle, F_SETFL, FNDELAY);
|
||||
#else
|
||||
/* efficient blocking for the read */
|
||||
fcntl(poSharedData->RS485_Handle, F_SETFL, 0);
|
||||
#endif
|
||||
/* save current serial port settings */
|
||||
tcgetattr(poSharedData->RS485_Handle, &poSharedData->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)
|
||||
CLOCAL : local connection, no modem contol
|
||||
CREAD : enable receiving characters
|
||||
*/
|
||||
newtio.c_cflag = poSharedData->RS485_Baud | poSharedData->RS485MOD | CLOCAL | CREAD;
|
||||
/* Raw input */
|
||||
newtio.c_iflag = 0;
|
||||
/* Raw output */
|
||||
newtio.c_oflag = 0;
|
||||
/* no processing */
|
||||
newtio.c_lflag = 0;
|
||||
/* activate the settings for the port after flushing I/O */
|
||||
tcsetattr(poSharedData->RS485_Handle, TCSAFLUSH, &newtio);
|
||||
/* flush any data waiting */
|
||||
usleep(200000);
|
||||
tcflush(poSharedData->RS485_Handle, TCIOFLUSH);
|
||||
/* ringbuffer */
|
||||
FIFO_Init(&poSharedData->Rx_FIFO, poSharedData->Rx_Buffer, sizeof(poSharedData->Rx_Buffer));
|
||||
printf("=success!\n");
|
||||
mstp_port->InputBuffer = &poSharedData->RxBuffer[0];
|
||||
mstp_port->InputBufferSize = sizeof(poSharedData->RxBuffer);
|
||||
mstp_port->OutputBuffer = &poSharedData->TxBuffer[0];
|
||||
mstp_port->OutputBufferSize = sizeof(poSharedData->TxBuffer);
|
||||
gettimeofday(&poSharedData->start, NULL);
|
||||
mstp_port->SilenceTimer = Timer_Silence;
|
||||
mstp_port->SilenceTimerReset = Timer_Silence_Reset;
|
||||
MSTP_Init(mstp_port);
|
||||
#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);
|
||||
#endif
|
||||
|
||||
rv = pthread_create(&hThread, NULL, dlmstp_master_fsm_task, mstp_port);
|
||||
if (rv != 0) {
|
||||
fprintf(stderr, "Failed to start Master Node FSM task\n");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2012 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*********************************************************************/
|
||||
#ifndef DLMSTP_LINUX_H
|
||||
#define DLMSTP_LINUX_H
|
||||
|
||||
#include "mstp.h"
|
||||
//#include "dlmstp.h"
|
||||
#include "bits/pthreadtypes.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "bacdef.h"
|
||||
#include "npdu.h"
|
||||
#include <termios.h>
|
||||
#include "fifo.h"
|
||||
#include "ringbuf.h"
|
||||
/* defines specific to MS/TP */
|
||||
/* preamble+type+dest+src+len+crc8+crc16 */
|
||||
#define MAX_HEADER (2+1+1+1+2+1+2)
|
||||
#define MAX_MPDU (MAX_HEADER+MAX_PDU)
|
||||
|
||||
/* count must be a power of 2 for ringbuf library */
|
||||
#ifndef MSTP_PDU_PACKET_COUNT
|
||||
#define MSTP_PDU_PACKET_COUNT 8
|
||||
#endif
|
||||
|
||||
typedef struct dlmstp_packet {
|
||||
bool ready; /* true if ready to be sent or received */
|
||||
BACNET_ADDRESS address; /* source address */
|
||||
uint8_t frame_type; /* type of message */
|
||||
uint16_t pdu_len; /* packet length */
|
||||
uint8_t pdu[MAX_MPDU]; /* packet */
|
||||
} DLMSTP_PACKET;
|
||||
|
||||
/* data structure for MS/TP PDU Queue */
|
||||
struct mstp_pdu_packet {
|
||||
bool data_expecting_reply;
|
||||
uint8_t destination_mac;
|
||||
uint16_t length;
|
||||
uint8_t buffer[MAX_MPDU];
|
||||
};
|
||||
|
||||
typedef struct shared_mstp_data
|
||||
{
|
||||
/* Number of MS/TP Packets Rx/Tx */
|
||||
uint16_t MSTP_Packets;
|
||||
|
||||
/* packet queues */
|
||||
DLMSTP_PACKET Receive_Packet;
|
||||
DLMSTP_PACKET Transmit_Packet;
|
||||
/*
|
||||
RT_COND Receive_Packet_Flag;
|
||||
RT_MUTEX Receive_Packet_Mutex;
|
||||
*/
|
||||
pthread_cond_t Receive_Packet_Flag;
|
||||
pthread_mutex_t Receive_Packet_Mutex;
|
||||
/* mechanism to wait for a frame in state machine */
|
||||
/*
|
||||
RT_COND Received_Frame_Flag;
|
||||
RT_MUTEX Received_Frame_Mutex;
|
||||
*/
|
||||
pthread_cond_t Received_Frame_Flag;
|
||||
pthread_mutex_t Received_Frame_Mutex;
|
||||
pthread_cond_t Master_Done_Flag;
|
||||
pthread_mutex_t Master_Done_Mutex;
|
||||
/* buffers needed by mstp port struct */
|
||||
uint8_t TxBuffer[MAX_MPDU];
|
||||
uint8_t RxBuffer[MAX_MPDU];
|
||||
/* The minimum time without a DataAvailable or ReceiveError event */
|
||||
/* that a node must wait for a station to begin replying to a */
|
||||
/* confirmed request: 255 milliseconds. (Implementations may use */
|
||||
/* larger values for this timeout, not to exceed 300 milliseconds.) */
|
||||
uint16_t Treply_timeout;
|
||||
/* The minimum time without a DataAvailable or ReceiveError event that a */
|
||||
/* node must wait for a remote node to begin using a token or replying to */
|
||||
/* a Poll For Master frame: 20 milliseconds. (Implementations may use */
|
||||
/* larger values for this timeout, not to exceed 100 milliseconds.) */
|
||||
uint8_t Tusage_timeout;
|
||||
/* Timer that indicates line silence - and functions */
|
||||
uint16_t SilenceTime;
|
||||
|
||||
/* handle returned from open() */
|
||||
int RS485_Handle;
|
||||
/* baudrate settings are defined in <asm/termbits.h>, which is
|
||||
included by <termios.h> */
|
||||
unsigned int RS485_Baud;
|
||||
/* serial port name, /dev/ttyS0,
|
||||
/dev/ttyUSB0 for USB->RS485 from B&B Electronics USOPTL4 */
|
||||
char *RS485_Port_Name;
|
||||
/* serial I/O settings */
|
||||
struct termios RS485_oldtio;
|
||||
/* some terminal I/O have RS-485 specific functionality */
|
||||
tcflag_t RS485MOD;
|
||||
/* Ring buffer for incoming bytes, in order to speed up the receiving. */
|
||||
FIFO_BUFFER Rx_FIFO;
|
||||
/* buffer size needs to be a power of 2 */
|
||||
uint8_t Rx_Buffer[4096];
|
||||
struct timeval start;
|
||||
|
||||
RING_BUFFER PDU_Queue;
|
||||
|
||||
struct mstp_pdu_packet PDU_Buffer[MSTP_PDU_PACKET_COUNT];
|
||||
|
||||
}SHARED_MSTP_DATA;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
bool dlmstp_init(
|
||||
void * poShared,
|
||||
char *ifname);
|
||||
void dlmstp_reset(
|
||||
void * poShared);
|
||||
void dlmstp_cleanup(
|
||||
void * poShared);
|
||||
|
||||
/* returns number of bytes sent on success, negative on failure */
|
||||
int dlmstp_send_pdu(
|
||||
void * poShared,
|
||||
BACNET_ADDRESS * dest, /* destination address */
|
||||
uint8_t * pdu, /* any data to be sent - may be null */
|
||||
unsigned pdu_len); /* number of bytes of data */
|
||||
|
||||
/* returns the number of octets in the PDU, or zero on failure */
|
||||
uint16_t dlmstp_receive(
|
||||
void * poShared,
|
||||
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 */
|
||||
|
||||
/* This parameter represents the value of the Max_Info_Frames property of */
|
||||
/* the node's Device object. The value of Max_Info_Frames specifies the */
|
||||
/* maximum number of information frames the node may send before it must */
|
||||
/* pass the token. Max_Info_Frames may have different values on different */
|
||||
/* 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(
|
||||
void * poShared,
|
||||
uint8_t max_info_frames);
|
||||
uint8_t dlmstp_max_info_frames(
|
||||
void * poShared);
|
||||
|
||||
/* This parameter represents the value of the Max_Master property of the */
|
||||
/* node's Device object. The value of Max_Master specifies the highest */
|
||||
/* 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(
|
||||
void * poShared,
|
||||
uint8_t max_master);
|
||||
uint8_t dlmstp_max_master(
|
||||
void * poShared);
|
||||
|
||||
/* MAC address 0-127 */
|
||||
void dlmstp_set_mac_address(
|
||||
void * poShared,
|
||||
uint8_t my_address);
|
||||
uint8_t dlmstp_mac_address(
|
||||
void * poShared);
|
||||
|
||||
void dlmstp_get_my_address(
|
||||
void * poShared,
|
||||
BACNET_ADDRESS * my_address);
|
||||
void dlmstp_get_broadcast_address(
|
||||
BACNET_ADDRESS * dest); /* destination address */
|
||||
|
||||
/* RS485 Baud Rate 9600, 19200, 38400, 57600, 115200 */
|
||||
void dlmstp_set_baud_rate(
|
||||
void * poShared,
|
||||
uint32_t baud);
|
||||
uint32_t dlmstp_baud_rate(
|
||||
void * poShared);
|
||||
|
||||
void dlmstp_fill_bacnet_address(
|
||||
BACNET_ADDRESS * src,
|
||||
uint8_t mstp_address);
|
||||
|
||||
bool dlmstp_sole_master(
|
||||
void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /*DLMSTP_LINUX_H*/
|
||||
@@ -63,6 +63,8 @@
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "dlmstp_linux.h"
|
||||
|
||||
/* Posix serial programming reference:
|
||||
http://www.easysw.com/~mike/serial/serial.html */
|
||||
|
||||
@@ -195,6 +197,87 @@ uint32_t RS485_Get_Baud_Rate(
|
||||
return baud;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Returns the baud rate that we are currently running at
|
||||
* RETURN: none
|
||||
* ALGORITHM: none
|
||||
* NOTES: none
|
||||
*****************************************************************************/
|
||||
uint32_t RS485_Get_Port_Baud_Rate(
|
||||
volatile struct mstp_port_struct_t *mstp_port)
|
||||
{
|
||||
uint32_t baud = 0;
|
||||
SHARED_MSTP_DATA * poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
switch (poSharedData->RS485_Baud) {
|
||||
case B0:
|
||||
baud = 0;
|
||||
break;
|
||||
case B50:
|
||||
baud = 50;
|
||||
break;
|
||||
case B75:
|
||||
baud = 75;
|
||||
break;
|
||||
case B110:
|
||||
baud = 110;
|
||||
break;
|
||||
case B134:
|
||||
baud = 134;
|
||||
break;
|
||||
case B150:
|
||||
baud = 150;
|
||||
break;
|
||||
case B200:
|
||||
baud = 200;
|
||||
break;
|
||||
case B300:
|
||||
baud = 300;
|
||||
break;
|
||||
case B600:
|
||||
baud = 600;
|
||||
break;
|
||||
case B1200:
|
||||
baud = 1200;
|
||||
break;
|
||||
case B1800:
|
||||
baud = 1800;
|
||||
break;
|
||||
case B2400:
|
||||
baud = 2400;
|
||||
break;
|
||||
case B4800:
|
||||
baud = 4800;
|
||||
break;
|
||||
case B9600:
|
||||
baud = 9600;
|
||||
break;
|
||||
case B19200:
|
||||
baud = 19200;
|
||||
break;
|
||||
case B38400:
|
||||
baud = 38400;
|
||||
break;
|
||||
case B57600:
|
||||
baud = 57600;
|
||||
break;
|
||||
case B115200:
|
||||
baud = 115200;
|
||||
break;
|
||||
case B230400:
|
||||
baud = 230400;
|
||||
break;
|
||||
default:
|
||||
baud = 9600;
|
||||
break;
|
||||
}
|
||||
|
||||
return baud;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* DESCRIPTION: Sets the baud rate for the chip USART
|
||||
* RETURN: none
|
||||
@@ -286,35 +369,66 @@ 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) */
|
||||
{ /* number of bytes of data (up to 501) */
|
||||
uint32_t turnaround_time = Tturnaround * 1000;
|
||||
uint32_t baud = RS485_Get_Baud_Rate();
|
||||
uint32_t baud;
|
||||
ssize_t written = 0;
|
||||
int greska;
|
||||
|
||||
/* sleeping for turnaround time is necessary to give other devices
|
||||
time to change from sending to receiving state. */
|
||||
usleep(turnaround_time / baud);
|
||||
/*
|
||||
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);
|
||||
greska = errno;
|
||||
if (written <= 0) {
|
||||
printf("write error: %s\n", strerror(greska));
|
||||
} else {
|
||||
/* wait until all output has been transmitted. */
|
||||
tcdrain(RS485_Handle);
|
||||
}
|
||||
/* tcdrain(RS485_Handle); */
|
||||
/* per MSTP spec, sort of */
|
||||
if (mstp_port) {
|
||||
mstp_port->SilenceTimerReset();
|
||||
}
|
||||
SHARED_MSTP_DATA * poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
baud = RS485_Get_Baud_Rate();
|
||||
/* sleeping for turnaround time is necessary to give other devices
|
||||
time to change from sending to receiving state. */
|
||||
usleep(turnaround_time / baud);
|
||||
/*
|
||||
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);
|
||||
greska = errno;
|
||||
if (written <= 0) {
|
||||
printf("write error: %s\n", strerror(greska));
|
||||
} else {
|
||||
/* wait until all output has been transmitted. */
|
||||
tcdrain(RS485_Handle);
|
||||
}
|
||||
/* tcdrain(RS485_Handle); */
|
||||
/* per MSTP spec, sort of */
|
||||
if (mstp_port) {
|
||||
mstp_port->SilenceTimerReset((void*)mstp_port);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
baud = RS485_Get_Port_Baud_Rate(mstp_port);
|
||||
/* sleeping for turnaround time is necessary to give other devices
|
||||
time to change from sending to receiving state. */
|
||||
usleep(turnaround_time / baud);
|
||||
/*
|
||||
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(poSharedData->RS485_Handle, buffer, nbytes);
|
||||
greska = errno;
|
||||
if (written <= 0) {
|
||||
printf("write error: %s\n", strerror(greska));
|
||||
} else {
|
||||
/* wait until all output has been transmitted. */
|
||||
tcdrain(poSharedData->RS485_Handle);
|
||||
}
|
||||
/* tcdrain(RS485_Handle); */
|
||||
/* per MSTP spec, sort of */
|
||||
if (mstp_port) {
|
||||
mstp_port->SilenceTimerReset((void*)mstp_port);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -333,37 +447,75 @@ void RS485_Check_UART_Data(
|
||||
uint8_t buf[2048];
|
||||
int n;
|
||||
|
||||
if (mstp_port->ReceiveError == true) {
|
||||
/* do nothing but wait for state machine to clear the error */
|
||||
/* burning time, so wait a longer time */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
} else if (mstp_port->DataAvailable == false) {
|
||||
/* wait for state machine to read from the DataRegister */
|
||||
if (FIFO_Count(&Rx_FIFO) > 0) {
|
||||
/* data is available */
|
||||
mstp_port->DataRegister = FIFO_Get(&Rx_FIFO);
|
||||
mstp_port->DataAvailable = true;
|
||||
/* FIFO is giving data - don't wait very long */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 10;
|
||||
} else {
|
||||
/* FIFO is empty - wait a longer time */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
}
|
||||
}
|
||||
/* grab bytes and stuff them into the FIFO every time */
|
||||
FD_ZERO(&input);
|
||||
FD_SET(RS485_Handle, &input);
|
||||
n = select(RS485_Handle + 1, &input, NULL, NULL, &waiter);
|
||||
if (n < 0) {
|
||||
return;
|
||||
}
|
||||
if (FD_ISSET(RS485_Handle, &input)) {
|
||||
n = read(RS485_Handle, buf, sizeof(buf));
|
||||
FIFO_Add(&Rx_FIFO, &buf[0], n);
|
||||
}
|
||||
SHARED_MSTP_DATA * poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData;
|
||||
if(!poSharedData)
|
||||
{
|
||||
if (mstp_port->ReceiveError == true) {
|
||||
/* do nothing but wait for state machine to clear the error */
|
||||
/* burning time, so wait a longer time */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
} else if (mstp_port->DataAvailable == false) {
|
||||
/* wait for state machine to read from the DataRegister */
|
||||
if (FIFO_Count(&Rx_FIFO) > 0) {
|
||||
/* data is available */
|
||||
mstp_port->DataRegister = FIFO_Get(&Rx_FIFO);
|
||||
mstp_port->DataAvailable = true;
|
||||
/* FIFO is giving data - don't wait very long */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 10;
|
||||
} else {
|
||||
/* FIFO is empty - wait a longer time */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
}
|
||||
}
|
||||
/* grab bytes and stuff them into the FIFO every time */
|
||||
FD_ZERO(&input);
|
||||
FD_SET(RS485_Handle, &input);
|
||||
n = select(RS485_Handle + 1, &input, NULL, NULL, &waiter);
|
||||
if (n < 0) {
|
||||
return;
|
||||
}
|
||||
if (FD_ISSET(RS485_Handle, &input)) {
|
||||
n = read(RS485_Handle, buf, sizeof(buf));
|
||||
FIFO_Add(&Rx_FIFO, &buf[0], n);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mstp_port->ReceiveError == true) {
|
||||
/* do nothing but wait for state machine to clear the error */
|
||||
/* burning time, so wait a longer time */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
} else if (mstp_port->DataAvailable == false) {
|
||||
/* wait for state machine to read from the DataRegister */
|
||||
if (FIFO_Count(&poSharedData->Rx_FIFO) > 0) {
|
||||
/* data is available */
|
||||
mstp_port->DataRegister = FIFO_Get(&poSharedData->Rx_FIFO);
|
||||
mstp_port->DataAvailable = true;
|
||||
/* FIFO is giving data - don't wait very long */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 10;
|
||||
} else {
|
||||
/* FIFO is empty - wait a longer time */
|
||||
waiter.tv_sec = 0;
|
||||
waiter.tv_usec = 5000;
|
||||
}
|
||||
}
|
||||
/* grab bytes and stuff them into the FIFO every time */
|
||||
FD_ZERO(&input);
|
||||
FD_SET(poSharedData->RS485_Handle, &input);
|
||||
n = select(poSharedData->RS485_Handle + 1, &input, NULL, NULL, &waiter);
|
||||
if (n < 0) {
|
||||
return;
|
||||
}
|
||||
if (FD_ISSET(poSharedData->RS485_Handle, &input)) {
|
||||
n = read(poSharedData->RS485_Handle, buf, sizeof(buf));
|
||||
FIFO_Add(&poSharedData->Rx_FIFO, &buf[0], n);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RS485_Cleanup(
|
||||
|
||||
Reference in New Issue
Block a user