running the indent on the files.

This commit is contained in:
skarg
2006-02-18 23:11:25 +00:00
parent b1d46ffa8c
commit d6a891f0d1
58 changed files with 4781 additions and 5862 deletions
+20 -38
View File
@@ -38,60 +38,42 @@ extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
/* unconfirmed requests */ /* unconfirmed requests */
void Send_WhoIs( void Send_WhoIs(int32_t low_limit, int32_t high_limit);
int32_t low_limit,
int32_t high_limit);
void Send_WhoHas_Object( void Send_WhoHas_Object(int32_t low_limit,
int32_t low_limit,
int32_t high_limit, int32_t high_limit,
BACNET_OBJECT_TYPE object_type, BACNET_OBJECT_TYPE object_type, uint32_t object_instance);
uint32_t object_instance);
void Send_WhoHas_Name( void Send_WhoHas_Name(int32_t low_limit,
int32_t low_limit, int32_t high_limit, char *object_name);
int32_t high_limit,
char *object_name);
void Send_I_Have( void Send_I_Have(uint32_t device_id,
uint32_t device_id,
BACNET_OBJECT_TYPE object_type, BACNET_OBJECT_TYPE object_type,
uint32_t object_instance, uint32_t object_instance, char *object_name);
char *object_name);
/* returns the invoke ID for confirmed request, or 0 if failed */ /* returns the invoke ID for confirmed request, or 0 if failed */
uint8_t Send_Read_Property_Request( uint8_t Send_Read_Property_Request(uint32_t device_id, /* destination device */
uint32_t device_id, /* destination device */ BACNET_OBJECT_TYPE object_type,
uint32_t object_instance,
BACNET_PROPERTY_ID object_property, int32_t array_index);
/* returns the invoke ID for confirmed request, or 0 if failed */
uint8_t Send_Write_Property_Request(uint32_t device_id, // destination device
BACNET_OBJECT_TYPE object_type, BACNET_OBJECT_TYPE object_type,
uint32_t object_instance, uint32_t object_instance,
BACNET_PROPERTY_ID object_property, BACNET_PROPERTY_ID object_property,
int32_t array_index); BACNET_APPLICATION_DATA_VALUE * object_value,
uint8_t priority, int32_t array_index);
/* returns the invoke ID for confirmed request, or 0 if failed */ /* returns the invoke ID for confirmed request, or 0 if failed */
uint8_t Send_Write_Property_Request( uint8_t Send_Reinitialize_Device_Request(uint32_t device_id,
uint32_t device_id, // destination device BACNET_REINITIALIZED_STATE state, char *password);
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance,
BACNET_PROPERTY_ID object_property,
BACNET_APPLICATION_DATA_VALUE *object_value,
uint8_t priority,
int32_t array_index);
/* returns the invoke ID for confirmed request, or 0 if failed */ /* returns the invoke ID for confirmed request, or 0 if failed */
uint8_t Send_Reinitialize_Device_Request( uint8_t Send_Device_Communication_Control_Request(uint32_t device_id, uint16_t timeDuration, /* 0=optional */
uint32_t device_id, BACNET_COMMUNICATION_ENABLE_DISABLE state, char *password); /* NULL=optional */
BACNET_REINITIALIZED_STATE state,
char *password);
/* returns the invoke ID for confirmed request, or 0 if failed */
uint8_t Send_Device_Communication_Control_Request(
uint32_t device_id,
uint16_t timeDuration, /* 0=optional */
BACNET_COMMUNICATION_ENABLE_DISABLE state,
char *password); /* NULL=optional */
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif #endif
+34 -68
View File
@@ -41,11 +41,9 @@
#include "ao.h" #include "ao.h"
#include "bacfile.h" #include "bacfile.h"
void handler_atomic_read_file( void handler_atomic_read_file(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data)
BACNET_CONFIRMED_SERVICE_DATA *service_data)
{ {
BACNET_ATOMIC_READ_FILE_DATA data; BACNET_ATOMIC_READ_FILE_DATA data;
int len = 0; int len = 0;
@@ -57,99 +55,67 @@ void handler_atomic_read_file(
BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT; BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT;
BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT; BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT;
fprintf(stderr,"Received Atomic-Read-File Request!\n"); fprintf(stderr, "Received Atomic-Read-File Request!\n");
len = arf_decode_service_request( len = arf_decode_service_request(service_request, service_len, &data);
service_request,
service_len,
&data);
if (len < 0) if (len < 0)
fprintf(stderr,"Unable to decode Atomic-Read-File Request!\n"); fprintf(stderr, "Unable to decode Atomic-Read-File Request!\n");
// prepare a reply // prepare a reply
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet // encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], src, &my_address, false, // true for confirmed messages
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
// bad decoding - send an abort // bad decoding - send an abort
if (len < 0) if (len < 0) {
{ pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += abort_encode_apdu( service_data->invoke_id, ABORT_REASON_OTHER);
&Handler_Transmit_Buffer[pdu_len], fprintf(stderr, "Sending Abort!\n");
service_data->invoke_id,
ABORT_REASON_OTHER);
fprintf(stderr,"Sending Abort!\n");
send = true; send = true;
} } else if (service_data->segmented_message) {
else if (service_data->segmented_message) pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr,"Sending Abort!\n"); fprintf(stderr, "Sending Abort!\n");
send = true; send = true;
} } else {
else if (data.access == FILE_STREAM_ACCESS) {
{
if (data.access == FILE_STREAM_ACCESS)
{
if (data.type.stream.requestedOctetCount < if (data.type.stream.requestedOctetCount <
octetstring_capacity(&data.fileData)) octetstring_capacity(&data.fileData)) {
{ if (bacfile_read_data(&data)) {
if (bacfile_read_data(&data)) pdu_len +=
{ arf_ack_encode_apdu(&Handler_Transmit_Buffer
pdu_len += arf_ack_encode_apdu( [pdu_len], service_data->invoke_id, &data);
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
&data);
send = true; send = true;
} } else {
else
{
send = true; send = true;
error = true; error = true;
} }
} } else {
else pdu_len +=
{ abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr,"Sending Abort!\n"); fprintf(stderr, "Sending Abort!\n");
send = true; send = true;
} }
} } else {
else
{
error_class = ERROR_CLASS_SERVICES; error_class = ERROR_CLASS_SERVICES;
error_code = ERROR_CODE_INVALID_FILE_ACCESS_METHOD; error_code = ERROR_CODE_INVALID_FILE_ACCESS_METHOD;
send = true; send = true;
error = true; error = true;
} }
} }
if (error) if (error) {
{ pdu_len += bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
SERVICE_CONFIRMED_ATOMIC_READ_FILE, SERVICE_CONFIRMED_ATOMIC_READ_FILE, error_class, error_code);
error_class, fprintf(stderr, "Sending Error!\n");
error_code);
fprintf(stderr,"Sending Error!\n");
send = true; send = true;
} }
if (send) if (send) {
{ bytes_sent = datalink_send_pdu(src, // destination address
bytes_sent = datalink_send_pdu( &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
src, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to send PDU (%s)!\n", strerror(errno)); fprintf(stderr, "Failed to send PDU (%s)!\n", strerror(errno));
} }
return; return;
+16 -28
View File
@@ -47,11 +47,9 @@
// that someone can read from us. It is common to // that someone can read from us. It is common to
// use the description as the file name. // use the description as the file name.
#if BACFILE #if BACFILE
void handler_atomic_read_file_ack( void handler_atomic_read_file_ack(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data)
BACNET_CONFIRMED_SERVICE_ACK_DATA *service_data)
{ {
int len = 0; int len = 0;
BACNET_ATOMIC_READ_FILE_DATA data; BACNET_ATOMIC_READ_FILE_DATA data;
@@ -59,42 +57,32 @@ void handler_atomic_read_file_ack(
char *pFilename = NULL; char *pFilename = NULL;
uint32_t instance = 0; uint32_t instance = 0;
(void)src; (void) src;
// get the file instance from the tsm data before freeing it // get the file instance from the tsm data before freeing it
instance = bacfile_instance_from_tsm(service_data->invoke_id); instance = bacfile_instance_from_tsm(service_data->invoke_id);
len = arf_ack_decode_service_request( len = arf_ack_decode_service_request(service_request,
service_request, service_len, &data);
service_len, fprintf(stderr, "Received Read-File Ack!\n");
&data); if ((len > 0) && (instance <= BACNET_MAX_INSTANCE)) {
fprintf(stderr,"Received Read-File Ack!\n");
if ((len > 0) && (instance <= BACNET_MAX_INSTANCE))
{
// write the data received to the file specified // write the data received to the file specified
if (data.access == FILE_STREAM_ACCESS) if (data.access == FILE_STREAM_ACCESS) {
{
pFilename = bacfile_name(instance); pFilename = bacfile_name(instance);
if (pFilename) if (pFilename) {
{
pFile = fopen(pFilename, "rb"); pFile = fopen(pFilename, "rb");
if (pFile) if (pFile) {
{ (void) fseek(pFile,
(void)fseek(pFile, data.type.stream.fileStartPosition, SEEK_SET);
data.type.stream.fileStartPosition,
SEEK_SET);
if (fwrite(octetstring_value(&data.fileData), if (fwrite(octetstring_value(&data.fileData),
octetstring_length(&data.fileData),1,pFile) != 1) octetstring_length(&data.fileData), 1,
fprintf(stderr,"Failed to write to %s (%u)!\n", pFile) != 1)
fprintf(stderr, "Failed to write to %s (%u)!\n",
pFilename, instance); pFilename, instance);
fclose(pFile); fclose(pFile);
} }
} }
} } else if (data.access == FILE_RECORD_ACCESS) {
else if (data.access == FILE_RECORD_ACCESS)
{
// FIXME: add handling for Record Access // FIXME: add handling for Record Access
} }
} }
} }
#endif #endif
+36 -66
View File
@@ -40,11 +40,9 @@
static char *My_Password = "AnnaRoseKarg"; static char *My_Password = "AnnaRoseKarg";
void handler_device_communication_control( void handler_device_communication_control(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data)
BACNET_CONFIRMED_SERVICE_DATA *service_data)
{ {
uint16_t timeDuration = 0; uint16_t timeDuration = 0;
BACNET_COMMUNICATION_ENABLE_DISABLE state = COMMUNICATION_ENABLE; BACNET_COMMUNICATION_ENABLE_DISABLE state = COMMUNICATION_ENABLE;
@@ -55,91 +53,63 @@ void handler_device_communication_control(
int bytes_sent = 0; int bytes_sent = 0;
// decode the service request only // decode the service request only
len = dcc_decode_service_request( len = dcc_decode_service_request(service_request,
service_request, service_len, &timeDuration, &state, &password);
service_len, fprintf(stderr, "DeviceCommunicationControl!\n");
&timeDuration,
&state,
&password);
fprintf(stderr,"DeviceCommunicationControl!\n");
if (len > 0) if (len > 0)
fprintf(stderr,"DeviceCommunicationControl: " fprintf(stderr, "DeviceCommunicationControl: "
"timeout=%u state=%u password=%s\n", "timeout=%u state=%u password=%s\n",
(unsigned)timeDuration, (unsigned) timeDuration,
(unsigned)state, (unsigned) state, characterstring_value(&password));
characterstring_value(&password));
else else
fprintf(stderr,"DeviceCommunicationControl: " fprintf(stderr, "DeviceCommunicationControl: "
"Unable to decode request!\n"); "Unable to decode request!\n");
// prepare a reply // prepare a reply
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet // encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], src, &my_address, false, // true for confirmed messages
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
// bad decoding or something we didn't understand - send an abort // bad decoding or something we didn't understand - send an abort
if (len == -1) if (len == -1) {
{ pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += abort_encode_apdu( service_data->invoke_id, ABORT_REASON_OTHER);
&Handler_Transmit_Buffer[pdu_len], fprintf(stderr, "DeviceCommunicationControl: "
service_data->invoke_id,
ABORT_REASON_OTHER);
fprintf(stderr,"DeviceCommunicationControl: "
"Sending Abort - could not decode.\n"); "Sending Abort - could not decode.\n");
} } else if (service_data->segmented_message) {
else if (service_data->segmented_message) pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr,"DeviceCommunicationControl: " fprintf(stderr, "DeviceCommunicationControl: "
"Sending Abort - segmented message.\n"); "Sending Abort - segmented message.\n");
} } else if (state >= MAX_BACNET_COMMUNICATION_ENABLE_DISABLE) {
else if (state >= MAX_BACNET_COMMUNICATION_ENABLE_DISABLE) pdu_len += reject_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
{ service_data->invoke_id, REJECT_REASON_UNDEFINED_ENUMERATION);
pdu_len += reject_encode_apdu( fprintf(stderr, "DeviceCommunicationControl: "
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
REJECT_REASON_UNDEFINED_ENUMERATION);
fprintf(stderr,"DeviceCommunicationControl: "
"Sending Reject - undefined enumeration\n"); "Sending Reject - undefined enumeration\n");
} } else {
else if (characterstring_ansi_same(&password, My_Password)) {
{ pdu_len += encode_simple_ack(&Handler_Transmit_Buffer[pdu_len],
if (characterstring_ansi_same(&password,My_Password))
{
pdu_len += encode_simple_ack(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL); SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL);
fprintf(stderr,"DeviceCommunicationControl: " fprintf(stderr, "DeviceCommunicationControl: "
"Sending Simple Ack!\n"); "Sending Simple Ack!\n");
dcc_set_status_duration(state,timeDuration); dcc_set_status_duration(state, timeDuration);
} } else {
else pdu_len +=
{ bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL, SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
ERROR_CLASS_SERVICES, ERROR_CLASS_SERVICES, ERROR_CODE_PASSWORD_FAILURE);
ERROR_CODE_PASSWORD_FAILURE); fprintf(stderr,
fprintf(stderr,"DeviceCommunicationControl: " "DeviceCommunicationControl: "
"Sending Error - password failure.\n"); "Sending Error - password failure.\n");
} }
} }
bytes_sent = datalink_send_pdu( bytes_sent = datalink_send_pdu(src, // destination address
src, // destination address &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"DeviceCommunicationControl: " fprintf(stderr, "DeviceCommunicationControl: "
"Failed to send PDU (%s)!\n", "Failed to send PDU (%s)!\n", strerror(errno));
strerror(errno));
return; return;
} }
+17 -36
View File
@@ -32,10 +32,8 @@
#include "iam.h" #include "iam.h"
#include "address.h" #include "address.h"
void handler_i_am_add( void handler_i_am_add(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src)
uint16_t service_len,
BACNET_ADDRESS *src)
{ {
int len = 0; int len = 0;
uint32_t device_id = 0; uint32_t device_id = 0;
@@ -43,31 +41,21 @@ void handler_i_am_add(
int segmentation = 0; int segmentation = 0;
uint16_t vendor_id = 0; uint16_t vendor_id = 0;
(void)service_len; (void) service_len;
len = iam_decode_service_request( len = iam_decode_service_request(service_request,
service_request, &device_id, &max_apdu, &segmentation, &vendor_id);
&device_id, fprintf(stderr, "Received I-Am Request");
&max_apdu, if (len != -1) {
&segmentation, fprintf(stderr, " from %u!\n", device_id);
&vendor_id); address_add(device_id, max_apdu, src);
fprintf(stderr,"Received I-Am Request"); } else
if (len != -1) fprintf(stderr, "!\n");
{
fprintf(stderr," from %u!\n",device_id);
address_add(device_id,
max_apdu,
src);
}
else
fprintf(stderr,"!\n");
return; return;
} }
void handler_i_am_bind( void handler_i_am_bind(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src)
uint16_t service_len,
BACNET_ADDRESS *src)
{ {
int len = 0; int len = 0;
uint32_t device_id = 0; uint32_t device_id = 0;
@@ -75,18 +63,11 @@ void handler_i_am_bind(
int segmentation = 0; int segmentation = 0;
uint16_t vendor_id = 0; uint16_t vendor_id = 0;
(void)service_len; (void) service_len;
len = iam_decode_service_request( len = iam_decode_service_request(service_request,
service_request, &device_id, &max_apdu, &segmentation, &vendor_id);
&device_id,
&max_apdu,
&segmentation,
&vendor_id);
// only add address if requested to bind // only add address if requested to bind
address_add_binding(device_id, address_add_binding(device_id, max_apdu, src);
max_apdu,
src);
return; return;
} }
+10 -17
View File
@@ -32,31 +32,24 @@
#include "bactext.h" #include "bactext.h"
#include "ihave.h" #include "ihave.h"
void handler_i_have( void handler_i_have(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src)
uint16_t service_len,
BACNET_ADDRESS *src)
{ {
int len = 0; int len = 0;
BACNET_I_HAVE_DATA data; BACNET_I_HAVE_DATA data;
(void)service_len; (void) service_len;
(void)src; (void) src;
len = ihave_decode_service_request( len = ihave_decode_service_request(service_request,
service_request, service_len, &data);
service_len, if (len != -1) {
&data); fprintf(stderr, "I-Have: %s %d from %s %u!\r\n",
if (len != -1)
{
fprintf(stderr,"I-Have: %s %d from %s %u!\r\n",
bactext_object_type_name(data.object_id.type), bactext_object_type_name(data.object_id.type),
data.object_id.instance, data.object_id.instance,
bactext_object_type_name(data.device_id.type), bactext_object_type_name(data.device_id.type),
data.device_id.instance); data.device_id.instance);
} } else
else fprintf(stderr, "I-Have: received, but unable to decode!\n");
fprintf(stderr,"I-Have: received, but unable to decode!\n");
return; return;
} }
+37 -62
View File
@@ -40,11 +40,9 @@
static BACNET_CHARACTER_STRING My_Password; static BACNET_CHARACTER_STRING My_Password;
void handler_reinitialize_device( void handler_reinitialize_device(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data)
BACNET_CONFIRMED_SERVICE_DATA *service_data)
{ {
BACNET_REINITIALIZED_STATE state; BACNET_REINITIALIZED_STATE state;
BACNET_CHARACTER_STRING password; BACNET_CHARACTER_STRING password;
@@ -54,84 +52,61 @@ void handler_reinitialize_device(
int bytes_sent = 0; int bytes_sent = 0;
// decode the service request only // decode the service request only
len = rd_decode_service_request( len = rd_decode_service_request(service_request,
service_request, service_len, &state, &password);
service_len, fprintf(stderr, "ReinitializeDevice!\n");
&state,
&password);
fprintf(stderr,"ReinitializeDevice!\n");
if (len > 0) if (len > 0)
fprintf(stderr,"ReinitializeDevice: state=%u password=%s\n", fprintf(stderr, "ReinitializeDevice: state=%u password=%s\n",
(unsigned)state, (unsigned) state, characterstring_value(&password));
characterstring_value(&password));
else else
fprintf(stderr,"ReinitializeDevice: Unable to decode request!\n"); fprintf(stderr, "ReinitializeDevice: Unable to decode request!\n");
// prepare a reply // prepare a reply
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet // encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], src, &my_address, false, // true for confirmed messages
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
// bad decoding or something we didn't understand - send an abort // bad decoding or something we didn't understand - send an abort
if (len == -1) if (len == -1) {
{ pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += abort_encode_apdu( service_data->invoke_id, ABORT_REASON_OTHER);
&Handler_Transmit_Buffer[pdu_len], fprintf(stderr,
service_data->invoke_id, "ReinitializeDevice: Sending Abort - could not decode.\n");
ABORT_REASON_OTHER); } else if (service_data->segmented_message) {
fprintf(stderr,"ReinitializeDevice: Sending Abort - could not decode.\n"); pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
}
else if (service_data->segmented_message)
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr,"ReinitializeDevice: Sending Abort - segmented message.\n"); fprintf(stderr,
} "ReinitializeDevice: Sending Abort - segmented message.\n");
else if (state >= MAX_BACNET_REINITIALIZED_STATE) } else if (state >= MAX_BACNET_REINITIALIZED_STATE) {
{ pdu_len += reject_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += reject_encode_apdu( service_data->invoke_id, REJECT_REASON_UNDEFINED_ENUMERATION);
&Handler_Transmit_Buffer[pdu_len], fprintf(stderr,
service_data->invoke_id, "ReinitializeDevice: Sending Reject - undefined enumeration\n");
REJECT_REASON_UNDEFINED_ENUMERATION); } else {
fprintf(stderr,"ReinitializeDevice: Sending Reject - undefined enumeration\n"); characterstring_init_ansi(&My_Password, "Jesus");
} if (characterstring_same(&password, &My_Password)) {
else pdu_len += encode_simple_ack(&Handler_Transmit_Buffer[pdu_len],
{
characterstring_init_ansi(&My_Password,"Jesus");
if (characterstring_same(&password,&My_Password))
{
pdu_len += encode_simple_ack(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
SERVICE_CONFIRMED_REINITIALIZE_DEVICE); SERVICE_CONFIRMED_REINITIALIZE_DEVICE);
fprintf(stderr,"ReinitializeDevice: Sending Simple Ack!\n"); fprintf(stderr, "ReinitializeDevice: Sending Simple Ack!\n");
/* FIXME: now you can reboot, restart, quit, or something clever */ /* FIXME: now you can reboot, restart, quit, or something clever */
/* Note: you can use a mix of state and password to do specific stuff */ /* Note: you can use a mix of state and password to do specific stuff */
/* Note: if you don't do something clever like actually restart, /* Note: if you don't do something clever like actually restart,
you probably should clear any DCC status and timeouts */ you probably should clear any DCC status and timeouts */
} } else {
else pdu_len +=
{ bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
SERVICE_CONFIRMED_REINITIALIZE_DEVICE, SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
ERROR_CLASS_SERVICES, ERROR_CLASS_SERVICES, ERROR_CODE_PASSWORD_FAILURE);
ERROR_CODE_PASSWORD_FAILURE); fprintf(stderr,
fprintf(stderr,"ReinitializeDevice: Sending Error - password failure.\n"); "ReinitializeDevice: Sending Error - password failure.\n");
} }
} }
bytes_sent = datalink_send_pdu( bytes_sent = datalink_send_pdu(src, // destination address
src, // destination address &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"ReinitializeDevice: Failed to send PDU (%s)!\n", fprintf(stderr, "ReinitializeDevice: Failed to send PDU (%s)!\n",
strerror(errno)); strerror(errno));
return; return;
+65 -123
View File
@@ -43,13 +43,11 @@
#include "bacfile.h" #include "bacfile.h"
#endif #endif
static uint8_t Temp_Buf[MAX_APDU] = {0}; static uint8_t Temp_Buf[MAX_APDU] = { 0 };
void handler_read_property( void handler_read_property(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data)
BACNET_CONFIRMED_SERVICE_DATA *service_data)
{ {
BACNET_READ_PROPERTY_DATA data; BACNET_READ_PROPERTY_DATA data;
int len = 0; int len = 0;
@@ -61,188 +59,132 @@ void handler_read_property(
BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT; BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT;
BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT; BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT;
len = rp_decode_service_request( len = rp_decode_service_request(service_request, service_len, &data);
service_request,
service_len,
&data);
if (len <= 0) if (len <= 0)
fprintf(stderr,"Unable to decode Read-Property Request!\n"); fprintf(stderr, "Unable to decode Read-Property Request!\n");
// prepare a reply // prepare a reply
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet // encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], src, &my_address, false, // true for confirmed messages
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
// bad decoding - send an abort // bad decoding - send an abort
if (len == -1) if (len == -1) {
{ pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += abort_encode_apdu( service_data->invoke_id, ABORT_REASON_OTHER);
&Handler_Transmit_Buffer[pdu_len], fprintf(stderr, "Sending Abort!\n");
service_data->invoke_id,
ABORT_REASON_OTHER);
fprintf(stderr,"Sending Abort!\n");
send = true; send = true;
} } else if (service_data->segmented_message) {
else if (service_data->segmented_message) pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr,"Sending Abort!\n"); fprintf(stderr, "Sending Abort!\n");
send = true; send = true;
} } else {
else switch (data.object_type) {
{
switch (data.object_type)
{
case OBJECT_DEVICE: case OBJECT_DEVICE:
// FIXME: probably need a length limitation sent with encode // FIXME: probably need a length limitation sent with encode
if (data.object_instance == Device_Object_Instance_Number()) if (data.object_instance == Device_Object_Instance_Number()) {
{ len = Device_Encode_Property_APDU(&Temp_Buf[0],
len = Device_Encode_Property_APDU(
&Temp_Buf[0],
data.object_property, data.object_property,
data.array_index, data.array_index, &error_class, &error_code);
&error_class, if (len >= 0) {
&error_code);
if (len >= 0)
{
// encode the APDU portion of the packet // encode the APDU portion of the packet
data.application_data = &Temp_Buf[0]; data.application_data = &Temp_Buf[0];
data.application_data_len = len; data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode // FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu( pdu_len +=
&Handler_Transmit_Buffer[pdu_len], rp_ack_encode_apdu(&Handler_Transmit_Buffer
service_data->invoke_id, [pdu_len], service_data->invoke_id, &data);
&data); fprintf(stderr, "Sending Read Property Ack!\n");
fprintf(stderr,"Sending Read Property Ack!\n");
send = true; send = true;
} } else
else
error = true; error = true;
} } else
else
error = true; error = true;
break; break;
case OBJECT_ANALOG_INPUT: case OBJECT_ANALOG_INPUT:
if (Analog_Input_Valid_Instance(data.object_instance)) if (Analog_Input_Valid_Instance(data.object_instance)) {
{ len = Analog_Input_Encode_Property_APDU(&Temp_Buf[0],
len = Analog_Input_Encode_Property_APDU(
&Temp_Buf[0],
data.object_instance, data.object_instance,
data.object_property, data.object_property,
data.array_index, data.array_index, &error_class, &error_code);
&error_class, if (len >= 0) {
&error_code);
if (len >= 0)
{
// encode the APDU portion of the packet // encode the APDU portion of the packet
data.application_data = &Temp_Buf[0]; data.application_data = &Temp_Buf[0];
data.application_data_len = len; data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode // FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu( pdu_len +=
&Handler_Transmit_Buffer[pdu_len], rp_ack_encode_apdu(&Handler_Transmit_Buffer
service_data->invoke_id, [pdu_len], service_data->invoke_id, &data);
&data); fprintf(stderr, "Sending Read Property Ack!\n");
fprintf(stderr,"Sending Read Property Ack!\n");
send = true; send = true;
} } else
else
error = true; error = true;
} } else
else
error = true; error = true;
break; break;
case OBJECT_ANALOG_OUTPUT: case OBJECT_ANALOG_OUTPUT:
if (Analog_Output_Valid_Instance(data.object_instance)) if (Analog_Output_Valid_Instance(data.object_instance)) {
{ len = Analog_Output_Encode_Property_APDU(&Temp_Buf[0],
len = Analog_Output_Encode_Property_APDU(
&Temp_Buf[0],
data.object_instance, data.object_instance,
data.object_property, data.object_property,
data.array_index, data.array_index, &error_class, &error_code);
&error_class, if (len >= 0) {
&error_code);
if (len >= 0)
{
// encode the APDU portion of the packet // encode the APDU portion of the packet
data.application_data = &Temp_Buf[0]; data.application_data = &Temp_Buf[0];
data.application_data_len = len; data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode // FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu( pdu_len +=
&Handler_Transmit_Buffer[pdu_len], rp_ack_encode_apdu(&Handler_Transmit_Buffer
service_data->invoke_id, [pdu_len], service_data->invoke_id, &data);
&data); fprintf(stderr, "Sending Read Property Ack!\n");
fprintf(stderr,"Sending Read Property Ack!\n");
send = true; send = true;
} } else
else
error = true; error = true;
} } else
else
error = true; error = true;
break; break;
#if BACFILE #if BACFILE
case OBJECT_FILE: case OBJECT_FILE:
if (bacfile_valid_instance(data.object_instance)) if (bacfile_valid_instance(data.object_instance)) {
{ len = bacfile_encode_property_apdu(&Temp_Buf[0],
len = bacfile_encode_property_apdu(
&Temp_Buf[0],
data.object_instance, data.object_instance,
data.object_property, data.object_property,
data.array_index, data.array_index, &error_class, &error_code);
&error_class, if (len >= 0) {
&error_code);
if (len >= 0)
{
// encode the APDU portion of the packet // encode the APDU portion of the packet
data.application_data = &Temp_Buf[0]; data.application_data = &Temp_Buf[0];
data.application_data_len = len; data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode // FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu( pdu_len +=
&Handler_Transmit_Buffer[pdu_len], rp_ack_encode_apdu(&Handler_Transmit_Buffer
service_data->invoke_id, [pdu_len], service_data->invoke_id, &data);
&data); fprintf(stderr, "Sending Read Property Ack!\n");
fprintf(stderr,"Sending Read Property Ack!\n");
send = true; send = true;
} } else
else
error = true; error = true;
} } else
else
error = true; error = true;
break; break;
#endif #endif
default: default:
error = true; error = true;
break; break;
} }
} }
if (error) if (error) {
{ pdu_len += bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
SERVICE_CONFIRMED_READ_PROPERTY, SERVICE_CONFIRMED_READ_PROPERTY, error_class, error_code);
error_class, fprintf(stderr, "Sending Read Property Error!\n");
error_code);
fprintf(stderr,"Sending Read Property Error!\n");
send = true; send = true;
} }
if (send) if (send) {
{ bytes_sent = datalink_send_pdu(src, // destination address
bytes_sent = datalink_send_pdu( &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
src, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to send PDU (%s)!\n", strerror(errno)); fprintf(stderr, "Failed to send PDU (%s)!\n", strerror(errno));
} }
return; return;
} }
+28 -41
View File
@@ -42,7 +42,7 @@
#include "txbuf.h" #include "txbuf.h"
/* for debugging... */ /* for debugging... */
static void PrintReadPropertyData(BACNET_READ_PROPERTY_DATA *data) static void PrintReadPropertyData(BACNET_READ_PROPERTY_DATA * data)
{ {
BACNET_APPLICATION_DATA_VALUE value; /* for decode value data */ BACNET_APPLICATION_DATA_VALUE value; /* for decode value data */
int len = 0; int len = 0;
@@ -51,76 +51,63 @@ static void PrintReadPropertyData(BACNET_READ_PROPERTY_DATA *data)
bool first_value = true; bool first_value = true;
bool print_brace = false; bool print_brace = false;
if (data) if (data) {
{ #if 0
#if 0
if (data->array_index == BACNET_ARRAY_ALL) if (data->array_index == BACNET_ARRAY_ALL)
fprintf(stderr,"%s #%u %s\n", fprintf(stderr, "%s #%u %s\n",
bactext_object_type_name(data->object_type), bactext_object_type_name(data->object_type),
data->object_instance, data->object_instance,
bactext_property_name(data->object_property)); bactext_property_name(data->object_property));
else else
fprintf(stderr,"%s #%u %s[%d]\n", fprintf(stderr, "%s #%u %s[%d]\n",
bactext_object_type_name(data->object_type), bactext_object_type_name(data->object_type),
data->object_instance, data->object_instance,
bactext_property_name(data->object_property), bactext_property_name(data->object_property),
data->array_index); data->array_index);
#endif #endif
application_data = data->application_data; application_data = data->application_data;
application_data_len = data->application_data_len; application_data_len = data->application_data_len;
/* value? need to loop until all of the len is gone... */ /* value? need to loop until all of the len is gone... */
for (;;) for (;;) {
{ len = bacapp_decode_application_data(application_data,
len = bacapp_decode_application_data( application_data_len, &value);
application_data, if (first_value && (len < application_data_len)) {
application_data_len,
&value);
if (first_value && (len < application_data_len))
{
first_value = false; first_value = false;
fprintf(stdout,"{"); fprintf(stdout, "{");
print_brace = true; print_brace = true;
} }
bacapp_print_value(stdout,&value,data->object_property); bacapp_print_value(stdout, &value, data->object_property);
if (len) if (len) {
{ if (len < application_data_len) {
if (len < application_data_len)
{
application_data += len; application_data += len;
application_data_len -= len; application_data_len -= len;
/* there's more! */ /* there's more! */
fprintf(stdout,","); fprintf(stdout, ",");
} } else
else
break; break;
} } else
else
break; break;
} }
if (print_brace) if (print_brace)
fprintf(stdout,"}"); fprintf(stdout, "}");
fprintf(stdout,"\r\n"); fprintf(stdout, "\r\n");
} }
} }
void handler_read_property_ack( void handler_read_property_ack(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data)
BACNET_CONFIRMED_SERVICE_ACK_DATA *service_data)
{ {
int len = 0; int len = 0;
BACNET_READ_PROPERTY_DATA data; BACNET_READ_PROPERTY_DATA data;
(void)src; (void) src;
(void)service_data; /* we could use these... */ (void) service_data; /* we could use these... */
len = rp_ack_decode_service_request( len = rp_ack_decode_service_request(service_request,
service_request, service_len, &data);
service_len, #if 0
&data); fprintf(stderr, "Received Read-Property Ack!\n");
#if 0 #endif
fprintf(stderr,"Received Read-Property Ack!\n");
#endif
if (len > 0) if (len > 0)
PrintReadPropertyData(&data); PrintReadPropertyData(&data);
} }
+21 -35
View File
@@ -35,10 +35,8 @@
#include "device.h" #include "device.h"
#include "client.h" #include "client.h"
void handler_who_has( void handler_who_has(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src)
uint16_t service_len,
BACNET_ADDRESS *src)
{ {
int len = 0; int len = 0;
BACNET_WHO_HAS_DATA data; BACNET_WHO_HAS_DATA data;
@@ -48,49 +46,37 @@ void handler_who_has(
char *object_name = NULL; char *object_name = NULL;
bool found = false; bool found = false;
(void)src; (void) src;
len = whohas_decode_service_request( len = whohas_decode_service_request(service_request,
service_request, service_len, &data);
service_len, if (len > 0) {
&data);
if (len > 0)
{
if ((data.low_limit == -1) || (data.high_limit == -1)) if ((data.low_limit == -1) || (data.high_limit == -1))
directed_to_me = true; directed_to_me = true;
else if ((Device_Object_Instance_Number() >= (uint32_t)data.low_limit) && else if ((Device_Object_Instance_Number() >=
(Device_Object_Instance_Number() <= (uint32_t)data.high_limit)) (uint32_t) data.low_limit)
&& (Device_Object_Instance_Number() <=
(uint32_t) data.high_limit))
directed_to_me = true; directed_to_me = true;
if (directed_to_me) if (directed_to_me) {
{
/* do we have such an object? If so, send an I-Have. /* do we have such an object? If so, send an I-Have.
note: we should have only 1 of such an object */ note: we should have only 1 of such an object */
if (data.object_name) if (data.object_name) {
{
/* valid name in my device? */ /* valid name in my device? */
object_name = characterstring_value(&data.object.name); object_name = characterstring_value(&data.object.name);
found = Device_Valid_Object_Name( found = Device_Valid_Object_Name(object_name,
object_name, &object_type, &object_instance);
&object_type,
&object_instance);
if (found) if (found)
Send_I_Have( Send_I_Have(Device_Object_Instance_Number(),
Device_Object_Instance_Number(), object_type, object_instance, object_name);
object_type, } else {
object_instance,
object_name);
}
else
{
/* valid object in my device? */ /* valid object in my device? */
object_name = Device_Valid_Object_Id( object_name =
data.object.identifier.type, Device_Valid_Object_Id(data.object.identifier.type,
data.object.identifier.instance); data.object.identifier.instance);
if (object_name) if (object_name)
Send_I_Have( Send_I_Have(Device_Object_Instance_Number(),
Device_Object_Instance_Number(),
data.object.identifier.type, data.object.identifier.type,
data.object.identifier.instance, data.object.identifier.instance, object_name);
object_name);
} }
} }
} }
+8 -14
View File
@@ -37,29 +37,23 @@
/* global flag to send an I-Am */ /* global flag to send an I-Am */
bool I_Am_Request = true; bool I_Am_Request = true;
void handler_who_is( void handler_who_is(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src)
uint16_t service_len,
BACNET_ADDRESS *src)
{ {
int len = 0; int len = 0;
int32_t low_limit = 0; int32_t low_limit = 0;
int32_t high_limit = 0; int32_t high_limit = 0;
(void)src; (void) src;
len = whois_decode_service_request( len = whois_decode_service_request(service_request,
service_request, service_len, &low_limit, &high_limit);
service_len,
&low_limit,
&high_limit);
/* in our simple system, we just set a flag and let the main loop /* in our simple system, we just set a flag and let the main loop
send the I-Am request. */ send the I-Am request. */
if (len == 0) if (len == 0)
I_Am_Request = true; I_Am_Request = true;
else if (len != -1) else if (len != -1) {
{ if ((Device_Object_Instance_Number() >= (uint32_t) low_limit) &&
if ((Device_Object_Instance_Number() >= (uint32_t)low_limit) && (Device_Object_Instance_Number() <= (uint32_t) high_limit))
(Device_Object_Instance_Number() <= (uint32_t)high_limit))
I_Am_Request = true; I_Am_Request = true;
} }
+50 -81
View File
@@ -43,11 +43,9 @@
#include "bacfile.h" #include "bacfile.h"
#endif #endif
void handler_write_property( void handler_write_property(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data)
BACNET_CONFIRMED_SERVICE_DATA *service_data)
{ {
BACNET_WRITE_PROPERTY_DATA wp_data; BACNET_WRITE_PROPERTY_DATA wp_data;
int len = 0; int len = 0;
@@ -58,117 +56,88 @@ void handler_write_property(
int bytes_sent = 0; int bytes_sent = 0;
// decode the service request only // decode the service request only
len = wp_decode_service_request( len = wp_decode_service_request(service_request,
service_request, service_len, &wp_data);
service_len, fprintf(stderr, "Received Write-Property Request!\n");
&wp_data);
fprintf(stderr,"Received Write-Property Request!\n");
if (len > 0) if (len > 0)
fprintf(stderr,"type=%u instance=%u property=%u index=%d\n", fprintf(stderr, "type=%u instance=%u property=%u index=%d\n",
wp_data.object_type, wp_data.object_type,
wp_data.object_instance, wp_data.object_instance,
wp_data.object_property, wp_data.object_property, wp_data.array_index);
wp_data.array_index);
else else
fprintf(stderr,"Unable to decode Write-Property Request!\n"); fprintf(stderr, "Unable to decode Write-Property Request!\n");
// prepare a reply // prepare a reply
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet // encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], src, &my_address, false, // true for confirmed messages
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
// bad decoding or something we didn't understand - send an abort // bad decoding or something we didn't understand - send an abort
if (len == -1) if (len == -1) {
{ pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += abort_encode_apdu( service_data->invoke_id, ABORT_REASON_OTHER);
&Handler_Transmit_Buffer[pdu_len], fprintf(stderr, "Sending Abort!\n");
service_data->invoke_id, } else if (service_data->segmented_message) {
ABORT_REASON_OTHER); pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
fprintf(stderr,"Sending Abort!\n");
}
else if (service_data->segmented_message)
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr,"Sending Abort!\n"); fprintf(stderr, "Sending Abort!\n");
} } else {
else switch (wp_data.object_type) {
{
switch (wp_data.object_type)
{
case OBJECT_DEVICE: case OBJECT_DEVICE:
if (Device_Write_Property(&wp_data,&error_class,&error_code)) if (Device_Write_Property(&wp_data, &error_class, &error_code)) {
{ pdu_len +=
pdu_len += encode_simple_ack( encode_simple_ack(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
SERVICE_CONFIRMED_WRITE_PROPERTY); SERVICE_CONFIRMED_WRITE_PROPERTY);
fprintf(stderr,"Sending Write Property Simple Ack!\n"); fprintf(stderr, "Sending Write Property Simple Ack!\n");
} } else {
else pdu_len +=
{ bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
SERVICE_CONFIRMED_WRITE_PROPERTY, SERVICE_CONFIRMED_WRITE_PROPERTY, error_class,
error_class,
error_code); error_code);
fprintf(stderr,"Sending Write Property Error!\n"); fprintf(stderr, "Sending Write Property Error!\n");
} }
break; break;
case OBJECT_ANALOG_INPUT: case OBJECT_ANALOG_INPUT:
error_class = ERROR_CLASS_PROPERTY; error_class = ERROR_CLASS_PROPERTY;
error_code = ERROR_CODE_WRITE_ACCESS_DENIED; error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
pdu_len += bacerror_encode_apdu( pdu_len +=
&Handler_Transmit_Buffer[pdu_len], bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY,
SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code);
error_class, fprintf(stderr, "Sending Write Access Error!\n");
error_code);
fprintf(stderr,"Sending Write Access Error!\n");
break; break;
case OBJECT_ANALOG_OUTPUT: case OBJECT_ANALOG_OUTPUT:
if (Analog_Output_Write_Property(&wp_data,&error_class,&error_code)) if (Analog_Output_Write_Property(&wp_data, &error_class,
{ &error_code)) {
pdu_len += encode_simple_ack( pdu_len +=
&Handler_Transmit_Buffer[pdu_len], encode_simple_ack(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
SERVICE_CONFIRMED_WRITE_PROPERTY); SERVICE_CONFIRMED_WRITE_PROPERTY);
fprintf(stderr,"Sending Write Property Simple Ack!\n"); fprintf(stderr, "Sending Write Property Simple Ack!\n");
} } else {
else pdu_len +=
{ bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id,
SERVICE_CONFIRMED_WRITE_PROPERTY, SERVICE_CONFIRMED_WRITE_PROPERTY, error_class,
error_class,
error_code); error_code);
fprintf(stderr,"Sending Write Access Error!\n"); fprintf(stderr, "Sending Write Access Error!\n");
} }
break; break;
default: default:
pdu_len += bacerror_encode_apdu( pdu_len +=
&Handler_Transmit_Buffer[pdu_len], bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY,
SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code);
error_class, fprintf(stderr, "Sending Unknown Object Error!\n");
error_code);
fprintf(stderr,"Sending Unknown Object Error!\n");
break; break;
} }
} }
bytes_sent = datalink_send_pdu( bytes_sent = datalink_send_pdu(src, // destination address
src, // destination address &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to send PDU (%s)!\n", strerror(errno)); fprintf(stderr, "Failed to send PDU (%s)!\n", strerror(errno));
return; return;
} }
+34 -53
View File
@@ -39,81 +39,62 @@ extern bool I_Am_Request;
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
void handler_unrecognized_service( void handler_unrecognized_service(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *dest, BACNET_ADDRESS * dest,
BACNET_CONFIRMED_SERVICE_DATA *service_data); BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_who_is( void handler_who_is(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src);
uint16_t service_len,
BACNET_ADDRESS *src);
void handler_who_has( void handler_who_has(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src);
uint16_t service_len,
BACNET_ADDRESS *src);
void handler_i_am_add( void handler_i_am_add(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src);
uint16_t service_len,
BACNET_ADDRESS *src);
void handler_i_am_bind( void handler_i_am_bind(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src);
uint16_t service_len,
BACNET_ADDRESS *src);
void handler_read_property( void handler_read_property(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_DATA *service_data); BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_read_property_ack( void handler_read_property_ack(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_ACK_DATA *service_data); BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data);
void handler_write_property( void handler_write_property(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_DATA *service_data); BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_atomic_read_file( void handler_atomic_read_file(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_DATA *service_data); BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_atomic_read_file_ack( void handler_atomic_read_file_ack(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_ACK_DATA *service_data); BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data);
void handler_reinitialize_device( void handler_reinitialize_device(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_DATA *service_data); BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_device_communication_control( void handler_device_communication_control(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_DATA *service_data); BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_i_have( void handler_i_have(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src);
uint16_t service_len,
BACNET_ADDRESS *src);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif #endif
+11 -21
View File
@@ -34,40 +34,30 @@
#include "npdu.h" #include "npdu.h"
#include "reject.h" #include "reject.h"
void handler_unrecognized_service( void handler_unrecognized_service(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *dest, BACNET_ADDRESS * dest, BACNET_CONFIRMED_SERVICE_DATA * service_data)
BACNET_CONFIRMED_SERVICE_DATA *service_data)
{ {
BACNET_ADDRESS src; BACNET_ADDRESS src;
int pdu_len = 0; int pdu_len = 0;
int bytes_sent = 0; int bytes_sent = 0;
(void)service_request; (void) service_request;
(void)service_len; (void) service_len;
datalink_get_my_address(&src); datalink_get_my_address(&src);
// encode the NPDU portion of the packet // encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], dest, &src, false, // true for confirmed messages
&Handler_Transmit_Buffer[0],
dest,
&src,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
// encode the APDU portion of the packet // encode the APDU portion of the packet
pdu_len += reject_encode_apdu( pdu_len += reject_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, REJECT_REASON_UNRECOGNIZED_SERVICE);
service_data->invoke_id,
REJECT_REASON_UNRECOGNIZED_SERVICE);
bytes_sent = datalink_send_pdu( bytes_sent = datalink_send_pdu(dest, // destination address
dest, // destination address &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent > 0) if (bytes_sent > 0)
fprintf(stderr,"Sent Reject!\n"); fprintf(stderr, "Sent Reject!\n");
else else
fprintf(stderr,"Failed to Send Reject (%s)!\n", strerror(errno)); fprintf(stderr, "Failed to Send Reject (%s)!\n", strerror(errno));
} }
+18 -35
View File
@@ -41,12 +41,9 @@
#include "handlers.h" #include "handlers.h"
#include "txbuf.h" #include "txbuf.h"
uint8_t Send_Device_Communication_Control_Request( uint8_t Send_Device_Communication_Control_Request(uint32_t device_id, uint16_t timeDuration, /* 0=optional */
uint32_t device_id, BACNET_COMMUNICATION_ENABLE_DISABLE state, char *password)
uint16_t timeDuration, /* 0=optional */ { /* NULL=optional */
BACNET_COMMUNICATION_ENABLE_DISABLE state,
char *password) /* NULL=optional */
{
BACNET_ADDRESS dest; BACNET_ADDRESS dest;
BACNET_ADDRESS my_address; BACNET_ADDRESS my_address;
unsigned max_apdu = 0; unsigned max_apdu = 0;
@@ -64,50 +61,36 @@ uint8_t Send_Device_Communication_Control_Request(
/* is there a tsm available? */ /* is there a tsm available? */
if (status) if (status)
status = tsm_transaction_available(); status = tsm_transaction_available();
if (status) if (status) {
{
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID(); invoke_id = tsm_next_free_invokeID();
// load the data for the encoding // load the data for the encoding
characterstring_init_ansi(&password_string,password); characterstring_init_ansi(&password_string, password);
pdu_len += dcc_encode_apdu( pdu_len += dcc_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len],
invoke_id, invoke_id,
timeDuration, timeDuration, state, password ? &password_string : NULL);
state,
password?&password_string:NULL);
/* will it fit in the sender? /* will it fit in the sender?
note: if there is a bottleneck router in between note: if there is a bottleneck router in between
us and the destination, we won't know unless us and the destination, we won't know unless
we have a way to check for that and update the we have a way to check for that and update the
max_apdu in the address binding table. */ max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu) if ((unsigned) pdu_len < max_apdu) {
{ tsm_set_confirmed_unsegmented_transaction(invoke_id,
tsm_set_confirmed_unsegmented_transaction( &dest, &Handler_Transmit_Buffer[0], pdu_len);
invoke_id, bytes_sent = datalink_send_pdu(&dest, // destination address
&dest, &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len);
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send DeviceCommunicationControl Request (%s)!\n", fprintf(stderr,
"Failed to Send DeviceCommunicationControl Request (%s)!\n",
strerror(errno)); strerror(errno));
} } else
else fprintf(stderr,
fprintf(stderr,"Failed to Send DeviceCommunicationControl Request " "Failed to Send DeviceCommunicationControl Request "
"(exceeds destination maximum APDU)!\n"); "(exceeds destination maximum APDU)!\n");
} }
return invoke_id; return invoke_id;
} }
+9 -18
View File
@@ -43,11 +43,9 @@
#include "txbuf.h" #include "txbuf.h"
/* find a specific device, or use -1 for limit if you want unlimited */ /* find a specific device, or use -1 for limit if you want unlimited */
void Send_I_Have( void Send_I_Have(uint32_t device_id,
uint32_t device_id,
BACNET_OBJECT_TYPE object_type, BACNET_OBJECT_TYPE object_type,
uint32_t object_instance, uint32_t object_instance, char *object_name)
char *object_name)
{ {
int pdu_len = 0; int pdu_len = 0;
BACNET_ADDRESS dest; BACNET_ADDRESS dest;
@@ -60,26 +58,19 @@ void Send_I_Have(
/* Who-Has is a global broadcast */ /* Who-Has is a global broadcast */
datalink_get_broadcast_address(&dest); datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */ /* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, /* true for confirmed messages */
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, /* true for confirmed messages */
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */ /* encode the APDU portion of the packet */
data.device_id.type = OBJECT_DEVICE; data.device_id.type = OBJECT_DEVICE;
data.device_id.instance = device_id; data.device_id.instance = device_id;
data.object_id.type = object_type; data.object_id.type = object_type;
data.object_id.instance = object_instance; data.object_id.instance = object_instance;
characterstring_init_ansi(&data.object_name,object_name); characterstring_init_ansi(&data.object_name, object_name);
pdu_len += ihave_encode_apdu( pdu_len += ihave_encode_apdu(&Handler_Transmit_Buffer[pdu_len], &data);
&Handler_Transmit_Buffer[pdu_len],
&data);
/* send the data */ /* send the data */
bytes_sent = datalink_send_pdu( bytes_sent = datalink_send_pdu(&dest, // destination address
&dest, // destination address &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send I-Have Reply (%s)!\n", strerror(errno)); fprintf(stderr, "Failed to Send I-Have Reply (%s)!\n",
strerror(errno));
} }
+16 -33
View File
@@ -42,10 +42,8 @@
#include "handlers.h" #include "handlers.h"
#include "txbuf.h" #include "txbuf.h"
uint8_t Send_Reinitialize_Device_Request( uint8_t Send_Reinitialize_Device_Request(uint32_t device_id,
uint32_t device_id, BACNET_REINITIALIZED_STATE state, char *password)
BACNET_REINITIALIZED_STATE state,
char *password)
{ {
BACNET_ADDRESS dest; BACNET_ADDRESS dest;
BACNET_ADDRESS my_address; BACNET_ADDRESS my_address;
@@ -64,49 +62,34 @@ uint8_t Send_Reinitialize_Device_Request(
/* is there a tsm available? */ /* is there a tsm available? */
if (status) if (status)
status = tsm_transaction_available(); status = tsm_transaction_available();
if (status) if (status) {
{
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID(); invoke_id = tsm_next_free_invokeID();
// load the data for the encoding // load the data for the encoding
characterstring_init_ansi(&password_string,password); characterstring_init_ansi(&password_string, password);
pdu_len += rd_encode_apdu( pdu_len += rd_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len], invoke_id, state, password ? &password_string : NULL);
invoke_id,
state,
password?&password_string:NULL);
/* will it fit in the sender? /* will it fit in the sender?
note: if there is a bottleneck router in between note: if there is a bottleneck router in between
us and the destination, we won't know unless us and the destination, we won't know unless
we have a way to check for that and update the we have a way to check for that and update the
max_apdu in the address binding table. */ max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu) if ((unsigned) pdu_len < max_apdu) {
{ tsm_set_confirmed_unsegmented_transaction(invoke_id,
tsm_set_confirmed_unsegmented_transaction( &dest, &Handler_Transmit_Buffer[0], pdu_len);
invoke_id, bytes_sent = datalink_send_pdu(&dest, // destination address
&dest, &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len);
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send ReinitializeDevice Request (%s)!\n", fprintf(stderr,
"Failed to Send ReinitializeDevice Request (%s)!\n",
strerror(errno)); strerror(errno));
} } else
else fprintf(stderr, "Failed to Send ReinitializeDevice Request "
fprintf(stderr,"Failed to Send ReinitializeDevice Request "
"(exceeds destination maximum APDU)!\n"); "(exceeds destination maximum APDU)!\n");
} }
return invoke_id; return invoke_id;
} }
+15 -30
View File
@@ -43,12 +43,10 @@
#include "txbuf.h" #include "txbuf.h"
/* returns invoke id of 0 if device is not bound or no tsm available */ /* returns invoke id of 0 if device is not bound or no tsm available */
uint8_t Send_Read_Property_Request( uint8_t Send_Read_Property_Request(uint32_t device_id, /* destination device */
uint32_t device_id, /* destination device */
BACNET_OBJECT_TYPE object_type, BACNET_OBJECT_TYPE object_type,
uint32_t object_instance, uint32_t object_instance,
BACNET_PROPERTY_ID object_property, BACNET_PROPERTY_ID object_property, int32_t array_index)
int32_t array_index)
{ {
BACNET_ADDRESS dest; BACNET_ADDRESS dest;
BACNET_ADDRESS my_address; BACNET_ADDRESS my_address;
@@ -67,14 +65,9 @@ uint8_t Send_Read_Property_Request(
/* is there a tsm available? */ /* is there a tsm available? */
if (status) if (status)
status = tsm_transaction_available(); status = tsm_transaction_available();
if (status) if (status) {
{
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID(); invoke_id = tsm_next_free_invokeID();
@@ -83,32 +76,24 @@ uint8_t Send_Read_Property_Request(
data.object_instance = object_instance; data.object_instance = object_instance;
data.object_property = object_property; data.object_property = object_property;
data.array_index = array_index; data.array_index = array_index;
pdu_len += rp_encode_apdu( pdu_len += rp_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len], invoke_id, &data);
invoke_id,
&data);
/* will it fit in the sender? /* will it fit in the sender?
note: if there is a bottleneck router in between note: if there is a bottleneck router in between
us and the destination, we won't know unless us and the destination, we won't know unless
we have a way to check for that and update the we have a way to check for that and update the
max_apdu in the address binding table. */ max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu) if ((unsigned) pdu_len < max_apdu) {
{ tsm_set_confirmed_unsegmented_transaction(invoke_id,
tsm_set_confirmed_unsegmented_transaction( &dest, &Handler_Transmit_Buffer[0], pdu_len);
invoke_id, bytes_sent = datalink_send_pdu(&dest, // destination address
&dest, &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len);
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send ReadProperty Request (%s)!\n", fprintf(stderr,
"Failed to Send ReadProperty Request (%s)!\n",
strerror(errno)); strerror(errno));
} } else
else fprintf(stderr, "Failed to Send ReadProperty Request "
fprintf(stderr,"Failed to Send ReadProperty Request "
"(exceeds destination maximum APDU)!\n"); "(exceeds destination maximum APDU)!\n");
} }
+17 -33
View File
@@ -43,10 +43,8 @@
#include "txbuf.h" #include "txbuf.h"
/* find a specific device, or use -1 for limit if you want unlimited */ /* find a specific device, or use -1 for limit if you want unlimited */
void Send_WhoHas_Name( void Send_WhoHas_Name(int32_t low_limit,
int32_t low_limit, int32_t high_limit, char *object_name)
int32_t high_limit,
char *object_name)
{ {
int pdu_len = 0; int pdu_len = 0;
BACNET_ADDRESS dest; BACNET_ADDRESS dest;
@@ -59,35 +57,27 @@ void Send_WhoHas_Name(
/* Who-Has is a global broadcast */ /* Who-Has is a global broadcast */
datalink_get_broadcast_address(&dest); datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */ /* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, /* true for confirmed messages */
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, /* true for confirmed messages */
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */ /* encode the APDU portion of the packet */
data.low_limit = low_limit; data.low_limit = low_limit;
data.high_limit = high_limit; data.high_limit = high_limit;
data.object_name = true; data.object_name = true;
characterstring_init_ansi(&data.object.name,object_name); characterstring_init_ansi(&data.object.name, object_name);
pdu_len += whohas_encode_apdu( pdu_len += whohas_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len],
&data); &data);
/* send the data */ /* send the data */
bytes_sent = datalink_send_pdu( bytes_sent = datalink_send_pdu(&dest, // destination address
&dest, // destination address &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send Who-Has Request (%s)!\n", strerror(errno)); fprintf(stderr, "Failed to Send Who-Has Request (%s)!\n",
strerror(errno));
} }
/* find a specific device, or use -1 for limit if you want unlimited */ /* find a specific device, or use -1 for limit if you want unlimited */
void Send_WhoHas_Object( void Send_WhoHas_Object(int32_t low_limit,
int32_t low_limit,
int32_t high_limit, int32_t high_limit,
BACNET_OBJECT_TYPE object_type, BACNET_OBJECT_TYPE object_type, uint32_t object_instance)
uint32_t object_instance)
{ {
int pdu_len = 0; int pdu_len = 0;
BACNET_ADDRESS dest; BACNET_ADDRESS dest;
@@ -100,11 +90,7 @@ void Send_WhoHas_Object(
/* Who-Has is a global broadcast */ /* Who-Has is a global broadcast */
datalink_get_broadcast_address(&dest); datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */ /* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, /* true for confirmed messages */
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, /* true for confirmed messages */
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */ /* encode the APDU portion of the packet */
data.low_limit = low_limit; data.low_limit = low_limit;
@@ -112,14 +98,12 @@ void Send_WhoHas_Object(
data.object_name = false; data.object_name = false;
data.object.identifier.type = object_type; data.object.identifier.type = object_type;
data.object.identifier.instance = object_instance; data.object.identifier.instance = object_instance;
pdu_len += whohas_encode_apdu( pdu_len += whohas_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len],
&data); &data);
/* send the data */ /* send the data */
bytes_sent = datalink_send_pdu( bytes_sent = datalink_send_pdu(&dest, // destination address
&dest, // destination address &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send Who-Has Request (%s)!\n", strerror(errno)); fprintf(stderr, "Failed to Send Who-Has Request (%s)!\n",
strerror(errno));
} }
+8 -17
View File
@@ -43,9 +43,7 @@
#include "txbuf.h" #include "txbuf.h"
/* find a specific device, or use -1 for limit if you want unlimited */ /* find a specific device, or use -1 for limit if you want unlimited */
void Send_WhoIs( void Send_WhoIs(int32_t low_limit, int32_t high_limit)
int32_t low_limit,
int32_t high_limit)
{ {
int pdu_len = 0; int pdu_len = 0;
BACNET_ADDRESS dest; BACNET_ADDRESS dest;
@@ -58,23 +56,16 @@ void Send_WhoIs(
datalink_get_broadcast_address(&dest); datalink_get_broadcast_address(&dest);
// encode the NPDU portion of the packet // encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, // true for confirmed messages
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
// encode the APDU portion of the packet // encode the APDU portion of the packet
pdu_len += whois_encode_apdu( pdu_len += whois_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len], low_limit, high_limit);
low_limit,
high_limit);
bytes_sent = datalink_send_pdu( bytes_sent = datalink_send_pdu(&dest, // destination address
&dest, // destination address &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send Who-Is Request (%s)!\n", strerror(errno)); fprintf(stderr, "Failed to Send Who-Is Request (%s)!\n",
strerror(errno));
} }
+17 -33
View File
@@ -43,14 +43,12 @@
#include "txbuf.h" #include "txbuf.h"
/* returns the invoke ID for confirmed request, or zero on failure */ /* returns the invoke ID for confirmed request, or zero on failure */
uint8_t Send_Write_Property_Request( uint8_t Send_Write_Property_Request(uint32_t device_id, // destination device
uint32_t device_id, // destination device
BACNET_OBJECT_TYPE object_type, BACNET_OBJECT_TYPE object_type,
uint32_t object_instance, uint32_t object_instance,
BACNET_PROPERTY_ID object_property, BACNET_PROPERTY_ID object_property,
BACNET_APPLICATION_DATA_VALUE *object_value, BACNET_APPLICATION_DATA_VALUE * object_value,
uint8_t priority, uint8_t priority, int32_t array_index)
int32_t array_index)
{ {
BACNET_ADDRESS dest; BACNET_ADDRESS dest;
BACNET_ADDRESS my_address; BACNET_ADDRESS my_address;
@@ -69,14 +67,9 @@ uint8_t Send_Write_Property_Request(
/* is there a tsm available? */ /* is there a tsm available? */
if (status) if (status)
status = tsm_transaction_available(); status = tsm_transaction_available();
if (status) if (status) {
{
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID(); invoke_id = tsm_next_free_invokeID();
@@ -85,37 +78,28 @@ uint8_t Send_Write_Property_Request(
data.object_instance = object_instance; data.object_instance = object_instance;
data.object_property = object_property; data.object_property = object_property;
data.array_index = array_index; data.array_index = array_index;
bacapp_copy(&data.value,object_value); bacapp_copy(&data.value, object_value);
data.priority = priority; data.priority = priority;
pdu_len += wp_encode_apdu( pdu_len += wp_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len], invoke_id, &data);
invoke_id,
&data);
/* will it fit in the sender? /* will it fit in the sender?
note: if there is a bottleneck router in between note: if there is a bottleneck router in between
us and the destination, we won't know unless us and the destination, we won't know unless
we have a way to check for that and update the we have a way to check for that and update the
max_apdu in the address binding table. */ max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu) if ((unsigned) pdu_len < max_apdu) {
{ tsm_set_confirmed_unsegmented_transaction(invoke_id,
tsm_set_confirmed_unsegmented_transaction( &dest, &Handler_Transmit_Buffer[0], pdu_len);
invoke_id, bytes_sent = datalink_send_pdu(&dest, // destination address
&dest, &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len);
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send WriteProperty Request (%s)!\n", fprintf(stderr,
"Failed to Send WriteProperty Request (%s)!\n",
strerror(errno)); strerror(errno));
} } else
else fprintf(stderr, "Failed to Send WriteProperty Request "
fprintf(stderr,"Failed to Send WriteProperty Request "
"(exceeds destination maximum APDU)!\n"); "(exceeds destination maximum APDU)!\n");
} }
return invoke_id; return invoke_id;
} }
+1 -2
View File
@@ -27,5 +27,4 @@
#include "config.h" #include "config.h"
#include "datalink.h" #include "datalink.h"
uint8_t Handler_Transmit_Buffer[MAX_MPDU] = {0}; uint8_t Handler_Transmit_Buffer[MAX_MPDU] = { 0 };
+14 -23
View File
@@ -65,9 +65,8 @@ char *Analog_Input_Name(uint32_t object_instance)
{ {
static char text_string[32] = ""; /* okay for single thread */ static char text_string[32] = ""; /* okay for single thread */
if (object_instance < MAX_ANALOG_INPUTS) if (object_instance < MAX_ANALOG_INPUTS) {
{ sprintf(text_string, "ANALOG INPUT %u", object_instance);
sprintf(text_string,"ANALOG INPUT %u",object_instance);
return text_string; return text_string;
} }
@@ -76,31 +75,28 @@ char *Analog_Input_Name(uint32_t object_instance)
/* return apdu length, or -1 on error */ /* return apdu length, or -1 on error */
/* assumption - object has already exists */ /* assumption - object has already exists */
int Analog_Input_Encode_Property_APDU( int Analog_Input_Encode_Property_APDU(uint8_t * apdu,
uint8_t *apdu,
uint32_t object_instance, uint32_t object_instance,
BACNET_PROPERTY_ID property, BACNET_PROPERTY_ID property,
int32_t array_index, int32_t array_index,
BACNET_ERROR_CLASS *error_class, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
BACNET_ERROR_CODE *error_code)
{ {
int apdu_len = 0; // return value int apdu_len = 0; // return value
BACNET_BIT_STRING bit_string; BACNET_BIT_STRING bit_string;
BACNET_CHARACTER_STRING char_string; BACNET_CHARACTER_STRING char_string;
float value = 3.14; float value = 3.14;
(void)array_index; (void) array_index;
switch (property) switch (property) {
{
case PROP_OBJECT_IDENTIFIER: case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_ANALOG_INPUT, apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_ANALOG_INPUT,
object_instance); object_instance);
break; break;
case PROP_OBJECT_NAME: case PROP_OBJECT_NAME:
case PROP_DESCRIPTION: case PROP_DESCRIPTION:
characterstring_init_ansi(&char_string, Analog_Input_Name(object_instance)); characterstring_init_ansi(&char_string,
apdu_len = encode_tagged_character_string(&apdu[0], Analog_Input_Name(object_instance));
&char_string); apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break; break;
case PROP_OBJECT_TYPE: case PROP_OBJECT_TYPE:
apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_ANALOG_INPUT); apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_ANALOG_INPUT);
@@ -117,13 +113,13 @@ int Analog_Input_Encode_Property_APDU(
apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string); apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string);
break; break;
case PROP_EVENT_STATE: case PROP_EVENT_STATE:
apdu_len = encode_tagged_enumerated(&apdu[0],EVENT_STATE_NORMAL); apdu_len = encode_tagged_enumerated(&apdu[0], EVENT_STATE_NORMAL);
break; break;
case PROP_OUT_OF_SERVICE: case PROP_OUT_OF_SERVICE:
apdu_len = encode_tagged_boolean(&apdu[0],false); apdu_len = encode_tagged_boolean(&apdu[0], false);
break; break;
case PROP_UNITS: case PROP_UNITS:
apdu_len = encode_tagged_enumerated(&apdu[0],UNITS_PERCENT); apdu_len = encode_tagged_enumerated(&apdu[0], UNITS_PERCENT);
break; break;
default: default:
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
@@ -154,13 +150,10 @@ void testAnalogInput(Test * pTest)
// FIXME: we should do a lot more testing here... // FIXME: we should do a lot more testing here...
len = Analog_Input_Encode_Property_APDU( len = Analog_Input_Encode_Property_APDU(&apdu[0],
&apdu[0],
instance, instance,
PROP_OBJECT_IDENTIFIER, PROP_OBJECT_IDENTIFIER,
BACNET_ARRAY_ALL, BACNET_ARRAY_ALL, &error_class, &error_code);
&error_class,
&error_code);
ct_test(pTest, len >= 0); ct_test(pTest, len >= 0);
len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value);
ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID);
@@ -192,5 +185,3 @@ int main(void)
} }
#endif /* TEST_ANALOG_INPUT */ #endif /* TEST_ANALOG_INPUT */
#endif /* TEST */ #endif /* TEST */
+7 -10
View File
@@ -33,26 +33,23 @@
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
bool Analog_Input_Valid_Instance(uint32_t object_instance); bool Analog_Input_Valid_Instance(uint32_t object_instance);
unsigned Analog_Input_Count(void); unsigned Analog_Input_Count(void);
uint32_t Analog_Input_Index_To_Instance(unsigned index); uint32_t Analog_Input_Index_To_Instance(unsigned index);
char *Analog_Input_Name(uint32_t object_instance); char *Analog_Input_Name(uint32_t object_instance);
int Analog_Input_Encode_Property_APDU( int Analog_Input_Encode_Property_APDU(uint8_t * apdu,
uint8_t *apdu,
uint32_t object_instance, uint32_t object_instance,
BACNET_PROPERTY_ID property, BACNET_PROPERTY_ID property,
int32_t array_index, int32_t array_index,
BACNET_ERROR_CLASS *error_class, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
BACNET_ERROR_CODE *error_code);
#ifdef TEST #ifdef TEST
#include "ctest.h" #include "ctest.h"
void testAnalogInput(Test * pTest); void testAnalogInput(Test * pTest);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif #endif
+65 -103
View File
@@ -46,7 +46,8 @@
// Here is our Priority Array. They are supposed to be Real, but // Here is our Priority Array. They are supposed to be Real, but
// we don't have that kind of memory, so we will use a single byte // we don't have that kind of memory, so we will use a single byte
// and load a Real for returning the value when asked. // and load a Real for returning the value when asked.
static uint8_t Analog_Output_Level[MAX_ANALOG_OUTPUTS][BACNET_MAX_PRIORITY]; static uint8_t
Analog_Output_Level[MAX_ANALOG_OUTPUTS][BACNET_MAX_PRIORITY];
// Writable out-of-service allows others to play with our Present Value // Writable out-of-service allows others to play with our Present Value
// without changing the physical output // without changing the physical output
static bool Analog_Output_Out_Of_Service[MAX_ANALOG_OUTPUTS]; static bool Analog_Output_Out_Of_Service[MAX_ANALOG_OUTPUTS];
@@ -56,17 +57,14 @@ static bool Analog_Ouput_Initialized = false;
void Analog_Output_Init(void) void Analog_Output_Init(void)
{ {
unsigned i,j; unsigned i, j;
if (!Analog_Ouput_Initialized) if (!Analog_Ouput_Initialized) {
{
Analog_Ouput_Initialized = true; Analog_Ouput_Initialized = true;
// initialize all the analog output priority arrays to NULL // initialize all the analog output priority arrays to NULL
for (i = 0; i < MAX_ANALOG_OUTPUTS; i++) for (i = 0; i < MAX_ANALOG_OUTPUTS; i++) {
{ for (j = 0; j < BACNET_MAX_PRIORITY; j++) {
for (j = 0; j < BACNET_MAX_PRIORITY; j++)
{
Analog_Output_Level[i][j] = AO_LEVEL_NULL; Analog_Output_Level[i][j] = AO_LEVEL_NULL;
} }
} }
@@ -126,12 +124,9 @@ static float Analog_Output_Present_Value(uint32_t object_instance)
Analog_Output_Init(); Analog_Output_Init();
index = Analog_Output_Instance_To_Index(object_instance); index = Analog_Output_Instance_To_Index(object_instance);
if (index < MAX_ANALOG_OUTPUTS) if (index < MAX_ANALOG_OUTPUTS) {
{ for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
for (i = 0; i < BACNET_MAX_PRIORITY; i++) if (Analog_Output_Level[index][i] != AO_LEVEL_NULL) {
{
if (Analog_Output_Level[index][i] != AO_LEVEL_NULL)
{
value = Analog_Output_Level[index][i]; value = Analog_Output_Level[index][i];
break; break;
} }
@@ -146,9 +141,8 @@ char *Analog_Output_Name(uint32_t object_instance)
{ {
static char text_string[32] = ""; /* okay for single thread */ static char text_string[32] = ""; /* okay for single thread */
if (object_instance < MAX_ANALOG_OUTPUTS) if (object_instance < MAX_ANALOG_OUTPUTS) {
{ sprintf(text_string, "ANALOG OUTPUT %u", object_instance);
sprintf(text_string,"ANALOG OUTPUT %u",object_instance);
return text_string; return text_string;
} }
@@ -156,13 +150,11 @@ char *Analog_Output_Name(uint32_t object_instance)
} }
/* return apdu len, or -1 on error */ /* return apdu len, or -1 on error */
int Analog_Output_Encode_Property_APDU( int Analog_Output_Encode_Property_APDU(uint8_t * apdu,
uint8_t *apdu,
uint32_t object_instance, uint32_t object_instance,
BACNET_PROPERTY_ID property, BACNET_PROPERTY_ID property,
int32_t array_index, int32_t array_index,
BACNET_ERROR_CLASS *error_class, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
BACNET_ERROR_CODE *error_code)
{ {
int len = 0; int len = 0;
int apdu_len = 0; // return value int apdu_len = 0; // return value
@@ -173,20 +165,20 @@ int Analog_Output_Encode_Property_APDU(
unsigned i = 0; unsigned i = 0;
Analog_Output_Init(); Analog_Output_Init();
switch (property) switch (property) {
{
case PROP_OBJECT_IDENTIFIER: case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_ANALOG_OUTPUT, apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_ANALOG_OUTPUT,
object_instance); object_instance);
break; break;
case PROP_OBJECT_NAME: case PROP_OBJECT_NAME:
case PROP_DESCRIPTION: case PROP_DESCRIPTION:
characterstring_init_ansi(&char_string, Analog_Output_Name(object_instance)); characterstring_init_ansi(&char_string,
apdu_len = encode_tagged_character_string(&apdu[0], Analog_Output_Name(object_instance));
&char_string); apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break; break;
case PROP_OBJECT_TYPE: case PROP_OBJECT_TYPE:
apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_ANALOG_OUTPUT); apdu_len =
encode_tagged_enumerated(&apdu[0], OBJECT_ANALOG_OUTPUT);
break; break;
case PROP_PRESENT_VALUE: case PROP_PRESENT_VALUE:
real_value = Analog_Output_Present_Value(object_instance); real_value = Analog_Output_Present_Value(object_instance);
@@ -201,60 +193,55 @@ int Analog_Output_Encode_Property_APDU(
apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string); apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string);
break; break;
case PROP_EVENT_STATE: case PROP_EVENT_STATE:
apdu_len = encode_tagged_enumerated(&apdu[0],EVENT_STATE_NORMAL); apdu_len = encode_tagged_enumerated(&apdu[0], EVENT_STATE_NORMAL);
break; break;
case PROP_OUT_OF_SERVICE: case PROP_OUT_OF_SERVICE:
apdu_len = encode_tagged_boolean(&apdu[0],false); apdu_len = encode_tagged_boolean(&apdu[0], false);
break; break;
case PROP_UNITS: case PROP_UNITS:
apdu_len = encode_tagged_enumerated(&apdu[0],UNITS_PERCENT); apdu_len = encode_tagged_enumerated(&apdu[0], UNITS_PERCENT);
break; break;
case PROP_PRIORITY_ARRAY: case PROP_PRIORITY_ARRAY:
// Array element zero is the number of elements in the array // Array element zero is the number of elements in the array
if (array_index == BACNET_ARRAY_LENGTH_INDEX) if (array_index == BACNET_ARRAY_LENGTH_INDEX)
apdu_len = encode_tagged_unsigned(&apdu[0], BACNET_MAX_PRIORITY); apdu_len =
encode_tagged_unsigned(&apdu[0], BACNET_MAX_PRIORITY);
// if no index was specified, then try to encode the entire list // if no index was specified, then try to encode the entire list
// into one packet. // into one packet.
else if (array_index == BACNET_ARRAY_ALL) else if (array_index == BACNET_ARRAY_ALL) {
{ object_index =
object_index = Analog_Output_Instance_To_Index(object_instance); Analog_Output_Instance_To_Index(object_instance);
for (i = 0; i < BACNET_MAX_PRIORITY; i++) for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
{
// FIXME: check if we have room before adding it to APDU // FIXME: check if we have room before adding it to APDU
if (Analog_Output_Level[object_index][i] == AO_LEVEL_NULL) if (Analog_Output_Level[object_index][i] == AO_LEVEL_NULL)
len = encode_tagged_null(&apdu[apdu_len]); len = encode_tagged_null(&apdu[apdu_len]);
else else {
{
real_value = Analog_Output_Level[object_index][i]; real_value = Analog_Output_Level[object_index][i];
len = encode_tagged_real(&apdu[apdu_len], real_value); len = encode_tagged_real(&apdu[apdu_len], real_value);
} }
// add it if we have room // add it if we have room
if ((apdu_len + len) < MAX_APDU) if ((apdu_len + len) < MAX_APDU)
apdu_len += len; apdu_len += len;
else else {
{
*error_class = ERROR_CLASS_SERVICES; *error_class = ERROR_CLASS_SERVICES;
*error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT;
apdu_len = -1; apdu_len = -1;
break; break;
} }
} }
} } else {
else object_index =
{ Analog_Output_Instance_To_Index(object_instance);
object_index = Analog_Output_Instance_To_Index(object_instance); if (array_index <= BACNET_MAX_PRIORITY) {
if (array_index <= BACNET_MAX_PRIORITY) if (Analog_Output_Level[object_index][array_index] ==
{ AO_LEVEL_NULL)
if (Analog_Output_Level[object_index][array_index] == AO_LEVEL_NULL)
len = encode_tagged_null(&apdu[apdu_len]); len = encode_tagged_null(&apdu[apdu_len]);
else else {
{ real_value =
real_value = Analog_Output_Level[object_index][array_index]; Analog_Output_Level[object_index][array_index];
len = encode_tagged_real(&apdu[apdu_len], real_value); len = encode_tagged_real(&apdu[apdu_len], real_value);
} }
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_ARRAY_INDEX; *error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
apdu_len = -1; apdu_len = -1;
@@ -277,10 +264,8 @@ int Analog_Output_Encode_Property_APDU(
} }
// returns true if successful // returns true if successful
bool Analog_Output_Write_Property( bool Analog_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
BACNET_WRITE_PROPERTY_DATA *wp_data, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
{ {
bool status = false; // return value bool status = false; // return value
unsigned int object_index = 0; unsigned int object_index = 0;
@@ -288,81 +273,63 @@ bool Analog_Output_Write_Property(
uint8_t level = AO_LEVEL_NULL; uint8_t level = AO_LEVEL_NULL;
Analog_Output_Init(); Analog_Output_Init();
if (!Analog_Output_Valid_Instance(wp_data->object_instance)) if (!Analog_Output_Valid_Instance(wp_data->object_instance)) {
{
*error_class = ERROR_CLASS_OBJECT; *error_class = ERROR_CLASS_OBJECT;
*error_code = ERROR_CODE_UNKNOWN_OBJECT; *error_code = ERROR_CODE_UNKNOWN_OBJECT;
return false; return false;
} }
// decode the some of the request // decode the some of the request
switch (wp_data->object_property) switch (wp_data->object_property) {
{
case PROP_PRESENT_VALUE: case PROP_PRESENT_VALUE:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_REAL) if (wp_data->value.tag == BACNET_APPLICATION_TAG_REAL) {
{
priority = wp_data->priority; priority = wp_data->priority;
if (priority && (priority <= BACNET_MAX_PRIORITY) && if (priority && (priority <= BACNET_MAX_PRIORITY) &&
(priority != 6 /* reserved */) && (priority != 6 /* reserved */ ) &&
(wp_data->value.type.Real >= 0.0) && (wp_data->value.type.Real >= 0.0) &&
(wp_data->value.type.Real <= 100.0)) (wp_data->value.type.Real <= 100.0)) {
{ level = (uint8_t) wp_data->value.type.Real;
level = (uint8_t)wp_data->value.type.Real; object_index =
object_index = Analog_Output_Instance_To_Index( Analog_Output_Instance_To_Index(wp_data->
wp_data->object_instance); object_instance);
priority--; priority--;
Analog_Output_Level[object_index][priority] = level; Analog_Output_Level[object_index][priority] = level;
// if Out of Service is TRUE, then don't set the // if Out of Service is TRUE, then don't set the
// physical output. This comment may apply to the // physical output. This comment may apply to the
// main loop (i.e. check out of service before changing output) // main loop (i.e. check out of service before changing output)
status = true; status = true;
} } else if (priority == 6) {
else if (priority == 6)
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_WRITE_ACCESS_DENIED; *error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
} }
} } else if (wp_data->value.tag == BACNET_APPLICATION_TAG_NULL) {
else if (wp_data->value.tag == BACNET_APPLICATION_TAG_NULL)
{
level = AO_LEVEL_NULL; level = AO_LEVEL_NULL;
object_index = Analog_Output_Instance_To_Index( object_index =
wp_data->object_instance); Analog_Output_Instance_To_Index(wp_data->object_instance);
priority = wp_data->priority; priority = wp_data->priority;
if (priority && (priority <= BACNET_MAX_PRIORITY)) if (priority && (priority <= BACNET_MAX_PRIORITY)) {
{
priority--; priority--;
Analog_Output_Level[object_index][priority] = level; Analog_Output_Level[object_index][priority] = level;
status = true; status = true;
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
} }
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE; *error_code = ERROR_CODE_INVALID_DATA_TYPE;
} }
break; break;
case PROP_OUT_OF_SERVICE: case PROP_OUT_OF_SERVICE:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_BOOLEAN) if (wp_data->value.tag == BACNET_APPLICATION_TAG_BOOLEAN) {
{ object_index =
object_index = Analog_Output_Instance_To_Index( Analog_Output_Instance_To_Index(wp_data->object_instance);
wp_data->object_instance);
Analog_Output_Out_Of_Service[object_index] = Analog_Output_Out_Of_Service[object_index] =
wp_data->value.type.Boolean; wp_data->value.type.Boolean;
status = true; status = true;
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE; *error_code = ERROR_CODE_INVALID_DATA_TYPE;
} }
@@ -395,13 +362,10 @@ void testAnalogOutput(Test * pTest)
BACNET_ERROR_CODE error_code; BACNET_ERROR_CODE error_code;
len = Analog_Output_Encode_Property_APDU( len = Analog_Output_Encode_Property_APDU(&apdu[0],
&apdu[0],
instance, instance,
PROP_OBJECT_IDENTIFIER, PROP_OBJECT_IDENTIFIER,
BACNET_ARRAY_ALL, BACNET_ARRAY_ALL, &error_class, &error_code);
&error_class,
&error_code);
ct_test(pTest, len != 0); ct_test(pTest, len != 0);
len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value);
ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID);
@@ -433,5 +397,3 @@ int main(void)
} }
#endif /* TEST_ANALOG_INPUT */ #endif /* TEST_ANALOG_INPUT */
#endif /* TEST */ #endif /* TEST */
+9 -14
View File
@@ -35,31 +35,26 @@
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
bool Analog_Output_Valid_Instance(uint32_t object_instance); bool Analog_Output_Valid_Instance(uint32_t object_instance);
unsigned Analog_Output_Count(void); unsigned Analog_Output_Count(void);
uint32_t Analog_Output_Index_To_Instance(unsigned index); uint32_t Analog_Output_Index_To_Instance(unsigned index);
char *Analog_Output_Name(uint32_t object_instance); char *Analog_Output_Name(uint32_t object_instance);
int Analog_Output_Encode_Property_APDU( int Analog_Output_Encode_Property_APDU(uint8_t * apdu,
uint8_t *apdu,
uint32_t object_instance, uint32_t object_instance,
BACNET_PROPERTY_ID property, BACNET_PROPERTY_ID property,
int32_t array_index, int32_t array_index,
BACNET_ERROR_CLASS *error_class, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
BACNET_ERROR_CODE *error_code);
bool Analog_Output_Write_Property( bool Analog_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
BACNET_WRITE_PROPERTY_DATA *wp_data, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
#ifdef TEST #ifdef TEST
#include "ctest.h" #include "ctest.h"
void testAnalogOutput(Test * pTest); void testAnalogOutput(Test * pTest);
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif #endif
+58 -105
View File
@@ -39,14 +39,12 @@
#include "arf.h" #include "arf.h"
#include "awf.h" #include "awf.h"
typedef struct typedef struct {
{
uint32_t instance; uint32_t instance;
char *filename; char *filename;
} BACNET_FILE_LISTING; } BACNET_FILE_LISTING;
static BACNET_FILE_LISTING BACnet_File_Listing[] = static BACNET_FILE_LISTING BACnet_File_Listing[] = {
{
{0, "test.log"}, {0, "test.log"},
{1, "script.txt"}, {1, "script.txt"},
{2, "bacenum.h"}, {2, "bacenum.h"},
@@ -59,10 +57,8 @@ char *bacfile_name(uint32_t instance)
char *filename = NULL; char *filename = NULL;
// linear search for file instance match // linear search for file instance match
while (BACnet_File_Listing[index].filename) while (BACnet_File_Listing[index].filename) {
{ if (BACnet_File_Listing[index].instance == instance) {
if (BACnet_File_Listing[index].instance == instance)
{
filename = BACnet_File_Listing[index].filename; filename = BACnet_File_Listing[index].filename;
break; break;
} }
@@ -82,8 +78,7 @@ uint32_t bacfile_count(void)
uint32_t index = 0; uint32_t index = 0;
// linear search for file instance match // linear search for file instance match
while (BACnet_File_Listing[index].filename) while (BACnet_File_Listing[index].filename) {
{
index++; index++;
} }
@@ -96,10 +91,8 @@ uint32_t bacfile_index_to_instance(unsigned find_index)
uint32_t index = 0; uint32_t index = 0;
// bounds checking... // bounds checking...
while (BACnet_File_Listing[index].filename) while (BACnet_File_Listing[index].filename) {
{ if (index == find_index) {
if (index == find_index)
{
instance = BACnet_File_Listing[index].instance; instance = BACnet_File_Listing[index].instance;
break; break;
} }
@@ -109,13 +102,12 @@ uint32_t bacfile_index_to_instance(unsigned find_index)
return instance; return instance;
} }
static long fsize(FILE *pFile) static long fsize(FILE * pFile)
{ {
long size = 0; long size = 0;
long origin = 0; long origin = 0;
if (pFile) if (pFile) {
{
origin = ftell(pFile); origin = ftell(pFile);
fseek(pFile, 0L, SEEK_END); fseek(pFile, 0L, SEEK_END);
size = ftell(pFile); size = ftell(pFile);
@@ -131,11 +123,9 @@ static unsigned bacfile_file_size(uint32_t object_instance)
unsigned file_size = 0; unsigned file_size = 0;
pFilename = bacfile_name(object_instance); pFilename = bacfile_name(object_instance);
if (pFilename) if (pFilename) {
{ pFile = fopen(pFilename, "rb");
pFile = fopen(pFilename,"rb"); if (pFile) {
if (pFile)
{
file_size = fsize(pFile); file_size = fsize(pFile);
fclose(pFile); fclose(pFile);
} }
@@ -145,44 +135,38 @@ static unsigned bacfile_file_size(uint32_t object_instance)
} }
/* return the number of bytes used, or -1 on error */ /* return the number of bytes used, or -1 on error */
int bacfile_encode_property_apdu( int bacfile_encode_property_apdu(uint8_t * apdu,
uint8_t *apdu,
uint32_t object_instance, uint32_t object_instance,
BACNET_PROPERTY_ID property, BACNET_PROPERTY_ID property,
int32_t array_index, int32_t array_index,
BACNET_ERROR_CLASS *error_class, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
BACNET_ERROR_CODE *error_code)
{ {
int apdu_len = 0; // return value int apdu_len = 0; // return value
char text_string[32] = {""}; char text_string[32] = { "" };
BACNET_CHARACTER_STRING char_string; BACNET_CHARACTER_STRING char_string;
(void)array_index; (void) array_index;
switch (property) switch (property) {
{
case PROP_OBJECT_IDENTIFIER: case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0], apdu_len = encode_tagged_object_id(&apdu[0],
OBJECT_FILE, OBJECT_FILE, object_instance);
object_instance);
break; break;
case PROP_OBJECT_NAME: case PROP_OBJECT_NAME:
sprintf(text_string,"FILE %d",object_instance); sprintf(text_string, "FILE %d", object_instance);
characterstring_init_ansi(&char_string, text_string); characterstring_init_ansi(&char_string, text_string);
apdu_len = encode_tagged_character_string(&apdu[0], apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
&char_string);
break; break;
case PROP_OBJECT_TYPE: case PROP_OBJECT_TYPE:
apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_FILE); apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_FILE);
break; break;
case PROP_DESCRIPTION: case PROP_DESCRIPTION:
characterstring_init_ansi(&char_string, bacfile_name(object_instance)); characterstring_init_ansi(&char_string,
apdu_len = encode_tagged_character_string(&apdu[0], bacfile_name(object_instance));
&char_string); apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break; break;
case PROP_FILE_TYPE: case PROP_FILE_TYPE:
characterstring_init_ansi(&char_string, "TEXT"); characterstring_init_ansi(&char_string, "TEXT");
apdu_len = encode_tagged_character_string(&apdu[0], apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
&char_string);
break; break;
case PROP_FILE_SIZE: case PROP_FILE_SIZE:
apdu_len = encode_tagged_unsigned(&apdu[0], apdu_len = encode_tagged_unsigned(&apdu[0],
@@ -191,27 +175,20 @@ int bacfile_encode_property_apdu(
case PROP_MODIFICATION_DATE: case PROP_MODIFICATION_DATE:
// FIXME: get the actual value // FIXME: get the actual value
apdu_len = encode_tagged_date(&apdu[0], apdu_len = encode_tagged_date(&apdu[0],
2005, 2005, 12, 25, 7 /* sunday */ );
12,
25,
7 /* sunday */);
// FIXME: get the actual value // FIXME: get the actual value
apdu_len += encode_tagged_time(&apdu[apdu_len], apdu_len += encode_tagged_time(&apdu[apdu_len], 12, 0, 0, 0);
12,
0,
0,
0);
break; break;
case PROP_ARCHIVE: case PROP_ARCHIVE:
// FIXME: get the actual value: note it may be inverse... // FIXME: get the actual value: note it may be inverse...
apdu_len = encode_tagged_boolean(&apdu[0],true); apdu_len = encode_tagged_boolean(&apdu[0], true);
break; break;
case PROP_READ_ONLY: case PROP_READ_ONLY:
// FIXME: get the actual value // FIXME: get the actual value
apdu_len = encode_tagged_boolean(&apdu[0],true); apdu_len = encode_tagged_boolean(&apdu[0], true);
break; break;
case PROP_FILE_ACCESS_METHOD: case PROP_FILE_ACCESS_METHOD:
apdu_len = encode_tagged_enumerated(&apdu[0],FILE_STREAM_ACCESS); apdu_len = encode_tagged_enumerated(&apdu[0], FILE_STREAM_ACCESS);
break; break;
default: default:
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
@@ -229,10 +206,8 @@ uint32_t bacfile_instance(char *filename)
uint32_t instance = BACNET_MAX_INSTANCE + 1; uint32_t instance = BACNET_MAX_INSTANCE + 1;
// linear search for filename match // linear search for filename match
while (BACnet_File_Listing[index].filename) while (BACnet_File_Listing[index].filename) {
{ if (strcmp(BACnet_File_Listing[index].filename, filename) == 0) {
if (strcmp(BACnet_File_Listing[index].filename,filename) == 0)
{
instance = BACnet_File_Listing[index].instance; instance = BACnet_File_Listing[index].instance;
break; break;
} }
@@ -248,53 +223,39 @@ uint32_t bacfile_instance(char *filename)
// Another way would be to store the // Another way would be to store the
// invokeID and file instance in a list or table // invokeID and file instance in a list or table
// when the request was sent // when the request was sent
uint32_t bacfile_instance_from_tsm( uint32_t bacfile_instance_from_tsm(uint8_t invokeID)
uint8_t invokeID)
{ {
BACNET_NPDU_DATA npdu_data = {0}; // dummy for getting npdu length BACNET_NPDU_DATA npdu_data = { 0 }; // dummy for getting npdu length
BACNET_CONFIRMED_SERVICE_DATA service_data = {0}; BACNET_CONFIRMED_SERVICE_DATA service_data = { 0 };
uint8_t service_choice = 0; uint8_t service_choice = 0;
uint8_t *service_request = NULL; uint8_t *service_request = NULL;
uint16_t service_request_len = 0; uint16_t service_request_len = 0;
BACNET_ADDRESS dest; // where the original packet was destined BACNET_ADDRESS dest; // where the original packet was destined
uint8_t pdu[MAX_PDU] = {0}; // original sent packet uint8_t pdu[MAX_PDU] = { 0 }; // original sent packet
uint16_t pdu_len = 0; // original packet length uint16_t pdu_len = 0; // original packet length
uint16_t len = 0; // apdu header length uint16_t len = 0; // apdu header length
BACNET_ATOMIC_READ_FILE_DATA data = {0}; BACNET_ATOMIC_READ_FILE_DATA data = { 0 };
uint32_t object_instance = BACNET_MAX_INSTANCE + 1; // return value uint32_t object_instance = BACNET_MAX_INSTANCE + 1; // return value
bool found = false; bool found = false;
int apdu_offset = 0; int apdu_offset = 0;
found = tsm_get_transaction_pdu( found = tsm_get_transaction_pdu(invokeID, &dest, &pdu[0], &pdu_len);
invokeID, if (found) {
&dest, apdu_offset = npdu_decode(&pdu[0], // data to decode
&pdu[0],
&pdu_len);
if (found)
{
apdu_offset = npdu_decode(
&pdu[0], // data to decode
NULL, // destination address - get the DNET/DLEN/DADR if in there NULL, // destination address - get the DNET/DLEN/DADR if in there
NULL, // source address - get the SNET/SLEN/SADR if in there NULL, // source address - get the SNET/SLEN/SADR if in there
&npdu_data); // amount of data to decode &npdu_data); // amount of data to decode
if (!npdu_data.network_layer_message && if (!npdu_data.network_layer_message &&
((pdu[apdu_offset] & 0xF0) == PDU_TYPE_CONFIRMED_SERVICE_REQUEST)) ((pdu[apdu_offset] & 0xF0) ==
{ PDU_TYPE_CONFIRMED_SERVICE_REQUEST)) {
len = apdu_decode_confirmed_service_request( len = apdu_decode_confirmed_service_request(&pdu[apdu_offset], // APDU data
&pdu[apdu_offset], // APDU data
pdu_len - apdu_offset, pdu_len - apdu_offset,
&service_data, &service_data,
&service_choice, &service_choice, &service_request, &service_request_len);
&service_request, if (service_choice == SERVICE_CONFIRMED_ATOMIC_READ_FILE) {
&service_request_len); len = arf_decode_service_request(service_request,
if (service_choice == SERVICE_CONFIRMED_ATOMIC_READ_FILE) service_request_len, &data);
{ if (len > 0) {
len = arf_decode_service_request(
service_request,
service_request_len,
&data);
if (len > 0)
{
if (data.object_type == OBJECT_FILE) if (data.object_type == OBJECT_FILE)
object_instance = data.object_instance; object_instance = data.object_instance;
} }
@@ -306,7 +267,7 @@ uint32_t bacfile_instance_from_tsm(
} }
#endif #endif
bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA *data) bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA * data)
{ {
char *pFilename = NULL; char *pFilename = NULL;
bool found = false; bool found = false;
@@ -314,36 +275,28 @@ bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA *data)
size_t len = 0; size_t len = 0;
pFilename = bacfile_name(data->object_instance); pFilename = bacfile_name(data->object_instance);
if (pFilename) if (pFilename) {
{
found = true; found = true;
pFile = fopen(pFilename,"rb"); pFile = fopen(pFilename, "rb");
if (pFile) if (pFile) {
{ (void) fseek(pFile,
(void)fseek(pFile, data->type.stream.fileStartPosition, SEEK_SET);
data->type.stream.fileStartPosition,
SEEK_SET);
len = fread(octetstring_value(&data->fileData), 1, len = fread(octetstring_value(&data->fileData), 1,
data->type.stream.requestedOctetCount, pFile); data->type.stream.requestedOctetCount, pFile);
if (len < data->type.stream.requestedOctetCount) if (len < data->type.stream.requestedOctetCount)
data->endOfFile = true; data->endOfFile = true;
else else
data->endOfFile = false; data->endOfFile = false;
octetstring_truncate(&data->fileData,len); octetstring_truncate(&data->fileData, len);
fclose(pFile); fclose(pFile);
} } else {
else octetstring_truncate(&data->fileData, 0);
{
octetstring_truncate(&data->fileData,0);
data->endOfFile = true; data->endOfFile = true;
} }
} } else {
else octetstring_truncate(&data->fileData, 0);
{
octetstring_truncate(&data->fileData,0);
data->endOfFile = true; data->endOfFile = true;
} }
return found; return found;
} }
+9 -13
View File
@@ -45,35 +45,31 @@
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
char *bacfile_name(uint32_t instance); char *bacfile_name(uint32_t instance);
bool bacfile_valid_instance(uint32_t object_instance); bool bacfile_valid_instance(uint32_t object_instance);
uint32_t bacfile_count(void); uint32_t bacfile_count(void);
uint32_t bacfile_index_to_instance(unsigned find_index); uint32_t bacfile_index_to_instance(unsigned find_index);
uint32_t bacfile_instance(char *filename); uint32_t bacfile_instance(char *filename);
#if TSM_ENABLED #if TSM_ENABLED
// this is one way to match up the invoke ID with // this is one way to match up the invoke ID with
// the file ID from the AtomicReadFile request. // the file ID from the AtomicReadFile request.
// Another way would be to store the // Another way would be to store the
// invokeID and file instance in a list or table // invokeID and file instance in a list or table
// when the request was sent // when the request was sent
uint32_t bacfile_instance_from_tsm( uint32_t bacfile_instance_from_tsm(uint8_t invokeID);
uint8_t invokeID);
#endif #endif
// AtomicReadFile ACK helper // AtomicReadFile ACK helper
bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA *data); bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA * data);
// handling for read property service // handling for read property service
int bacfile_encode_property_apdu( int bacfile_encode_property_apdu(uint8_t * apdu,
uint8_t *apdu,
uint32_t object_instance, uint32_t object_instance,
BACNET_PROPERTY_ID property, BACNET_PROPERTY_ID property,
int32_t array_index, int32_t array_index,
BACNET_ERROR_CLASS *error_class, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
BACNET_ERROR_CODE *error_code);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif #endif
+122 -173
View File
@@ -25,7 +25,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> /* for memmove*/ #include <string.h> /* for memmove */
#include "bacdef.h" #include "bacdef.h"
#include "bacdcode.h" #include "bacdcode.h"
#include "bacenum.h" #include "bacenum.h"
@@ -119,11 +119,10 @@ const char *Device_Object_Name(void)
bool Device_Set_Object_Name(const char *name, size_t length) bool Device_Set_Object_Name(const char *name, size_t length)
{ {
bool status = false; /*return value*/ bool status = false; /*return value */
if (length < sizeof(Object_Name)) if (length < sizeof(Object_Name)) {
{ memmove(Object_Name, name, length);
memmove(Object_Name,name,length);
Object_Name[length] = 0; Object_Name[length] = 0;
status = true; status = true;
} }
@@ -149,11 +148,10 @@ const char *Device_Vendor_Name(void)
bool Device_Set_Vendor_Name(const char *name, size_t length) bool Device_Set_Vendor_Name(const char *name, size_t length)
{ {
bool status = false; /*return value*/ bool status = false; /*return value */
if (length < sizeof(Vendor_Name)) if (length < sizeof(Vendor_Name)) {
{ memmove(Vendor_Name, name, length);
memmove(Vendor_Name,name,length);
Vendor_Name[length] = 0; Vendor_Name[length] = 0;
status = true; status = true;
} }
@@ -178,11 +176,10 @@ const char *Device_Model_Name(void)
bool Device_Set_Model_Name(const char *name, size_t length) bool Device_Set_Model_Name(const char *name, size_t length)
{ {
bool status = false; /*return value*/ bool status = false; /*return value */
if (length < sizeof(Model_Name)) if (length < sizeof(Model_Name)) {
{ memmove(Model_Name, name, length);
memmove(Model_Name,name,length);
Model_Name[length] = 0; Model_Name[length] = 0;
status = true; status = true;
} }
@@ -197,11 +194,10 @@ const char *Device_Firmware_Revision(void)
bool Device_Set_Firmware_Revision(const char *name, size_t length) bool Device_Set_Firmware_Revision(const char *name, size_t length)
{ {
bool status = false; /*return value*/ bool status = false; /*return value */
if (length < sizeof(Firmware_Revision)) if (length < sizeof(Firmware_Revision)) {
{ memmove(Firmware_Revision, name, length);
memmove(Firmware_Revision,name,length);
Firmware_Revision[length] = 0; Firmware_Revision[length] = 0;
status = true; status = true;
} }
@@ -214,13 +210,13 @@ const char *Device_Application_Software_Version(void)
return Application_Software_Version; return Application_Software_Version;
} }
bool Device_Set_Application_Software_Version(const char *name, size_t length) bool Device_Set_Application_Software_Version(const char *name,
size_t length)
{ {
bool status = false; /*return value*/ bool status = false; /*return value */
if (length < sizeof(Application_Software_Version)) if (length < sizeof(Application_Software_Version)) {
{ memmove(Application_Software_Version, name, length);
memmove(Application_Software_Version,name,length);
Application_Software_Version[length] = 0; Application_Software_Version[length] = 0;
status = true; status = true;
} }
@@ -235,11 +231,10 @@ const char *Device_Description(void)
bool Device_Set_Description(const char *name, size_t length) bool Device_Set_Description(const char *name, size_t length)
{ {
bool status = false; /*return value*/ bool status = false; /*return value */
if (length < sizeof(Description)) if (length < sizeof(Description)) {
{ memmove(Description, name, length);
memmove(Description,name,length);
Description[length] = 0; Description[length] = 0;
status = true; status = true;
} }
@@ -254,11 +249,10 @@ const char *Device_Location(void)
bool Device_Set_Location(const char *name, size_t length) bool Device_Set_Location(const char *name, size_t length)
{ {
bool status = false; /*return value*/ bool status = false; /*return value */
if (length < sizeof(Location)) if (length < sizeof(Location)) {
{ memmove(Location, name, length);
memmove(Location,name,length);
Location[length] = 0; Location[length] = 0;
status = true; status = true;
} }
@@ -325,69 +319,58 @@ unsigned Device_Object_List_Count(void)
count += Analog_Input_Count(); count += Analog_Input_Count();
count += Analog_Output_Count(); count += Analog_Output_Count();
#if BACFILE #if BACFILE
count += bacfile_count(); count += bacfile_count();
#endif #endif
return count; return count;
} }
bool Device_Object_List_Identifier(unsigned array_index, bool Device_Object_List_Identifier(unsigned array_index,
int *object_type, int *object_type, uint32_t * instance)
uint32_t *instance)
{ {
bool status = false; bool status = false;
unsigned object_index = 0; unsigned object_index = 0;
if (array_index == 1) if (array_index == 1) {
{
*object_type = OBJECT_DEVICE; *object_type = OBJECT_DEVICE;
*instance = Object_Instance_Number; *instance = Object_Instance_Number;
status = true; status = true;
} }
if (!status) if (!status) {
{
// array index starts at 1, and 1 for the device object // array index starts at 1, and 1 for the device object
object_index = array_index - 2; object_index = array_index - 2;
if (object_index < Analog_Input_Count()) if (object_index < Analog_Input_Count()) {
{
*object_type = OBJECT_ANALOG_INPUT; *object_type = OBJECT_ANALOG_INPUT;
*instance = Analog_Input_Index_To_Instance(object_index); *instance = Analog_Input_Index_To_Instance(object_index);
status = true; status = true;
} }
} }
if (!status) if (!status) {
{
object_index -= Analog_Input_Count(); object_index -= Analog_Input_Count();
if (object_index < Analog_Output_Count()) if (object_index < Analog_Output_Count()) {
{
*object_type = OBJECT_ANALOG_OUTPUT; *object_type = OBJECT_ANALOG_OUTPUT;
*instance = Analog_Output_Index_To_Instance(object_index); *instance = Analog_Output_Index_To_Instance(object_index);
status = true; status = true;
} }
} }
#if BACFILE
#if BACFILE if (!status) {
if (!status)
{
object_index -= Analog_Output_Count(); object_index -= Analog_Output_Count();
if (object_index < bacfile_count()) if (object_index < bacfile_count()) {
{
*object_type = OBJECT_FILE; *object_type = OBJECT_FILE;
*instance = bacfile_index_to_instance(object_index); *instance = bacfile_index_to_instance(object_index);
status = true; status = true;
} }
} }
#endif #endif
return status; return status;
} }
bool Device_Valid_Object_Name( bool Device_Valid_Object_Name(const char *object_name,
const char *object_name, int *object_type, uint32_t * object_instance)
int *object_type,
uint32_t *object_instance)
{ {
bool found = false; bool found = false;
int type = 0; int type = 0;
@@ -397,14 +380,11 @@ bool Device_Valid_Object_Name(
char *name = NULL; char *name = NULL;
max_objects = Device_Object_List_Count(); max_objects = Device_Object_List_Count();
for (i = 0; i < max_objects; i++) for (i = 0; i < max_objects; i++) {
{
check_id = Device_Object_List_Identifier(i, &type, &instance); check_id = Device_Object_List_Identifier(i, &type, &instance);
if (check_id) if (check_id) {
{ name = Device_Valid_Object_Id(type, instance);
name = Device_Valid_Object_Id(type,instance); if (strcmp(name, object_name) == 0) {
if (strcmp(name,object_name) == 0)
{
found = true; found = true;
if (object_type) if (object_type)
*object_type = type; *object_type = type;
@@ -419,14 +399,11 @@ bool Device_Valid_Object_Name(
} }
/* returns the name or NULL if not found */ /* returns the name or NULL if not found */
char *Device_Valid_Object_Id( char *Device_Valid_Object_Id(int object_type, uint32_t object_instance)
int object_type,
uint32_t object_instance)
{ {
char *name = NULL; /* return value */ char *name = NULL; /* return value */
switch (object_type) switch (object_type) {
{
case OBJECT_ANALOG_INPUT: case OBJECT_ANALOG_INPUT:
name = Analog_Input_Name(object_instance); name = Analog_Input_Name(object_instance);
break; break;
@@ -450,12 +427,10 @@ char *Device_Valid_Object_Id(
} }
// return the length of the apdu encoded or -1 for error // return the length of the apdu encoded or -1 for error
int Device_Encode_Property_APDU( int Device_Encode_Property_APDU(uint8_t * apdu,
uint8_t *apdu,
BACNET_PROPERTY_ID property, BACNET_PROPERTY_ID property,
int32_t array_index, int32_t array_index,
BACNET_ERROR_CLASS *error_class, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
BACNET_ERROR_CODE *error_code)
{ {
int apdu_len = 0; // return value int apdu_len = 0; // return value
int len = 0; // apdu len intermediate value int len = 0; // apdu len intermediate value
@@ -466,8 +441,7 @@ int Device_Encode_Property_APDU(
uint32_t instance = 0; uint32_t instance = 0;
unsigned count = 0; unsigned count = 0;
switch (property) switch (property) {
{
case PROP_OBJECT_IDENTIFIER: case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_DEVICE, apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_DEVICE,
Object_Instance_Number); Object_Instance_Number);
@@ -502,7 +476,8 @@ int Device_Encode_Property_APDU(
apdu_len = encode_tagged_character_string(&apdu[0], &char_string); apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break; break;
case PROP_APPLICATION_SOFTWARE_VERSION: case PROP_APPLICATION_SOFTWARE_VERSION:
characterstring_init_ansi(&char_string, Application_Software_Version); characterstring_init_ansi(&char_string,
Application_Software_Version);
apdu_len = encode_tagged_character_string(&apdu[0], &char_string); apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break; break;
// if you support time // if you support time
@@ -526,7 +501,8 @@ int Device_Encode_Property_APDU(
// my_tm->tm_mday, ((my_tm->tm_wday == 0) ? 7 : my_tm->tm_wday)); // my_tm->tm_mday, ((my_tm->tm_wday == 0) ? 7 : my_tm->tm_wday));
//break; //break;
case PROP_PROTOCOL_VERSION: case PROP_PROTOCOL_VERSION:
apdu_len = encode_tagged_unsigned(&apdu[0], Device_Protocol_Version()); apdu_len =
encode_tagged_unsigned(&apdu[0], Device_Protocol_Version());
break; break;
// BACnet Legacy Support // BACnet Legacy Support
case PROP_PROTOCOL_CONFORMANCE_CLASS: case PROP_PROTOCOL_CONFORMANCE_CLASS:
@@ -535,10 +511,10 @@ int Device_Encode_Property_APDU(
case PROP_PROTOCOL_SERVICES_SUPPORTED: case PROP_PROTOCOL_SERVICES_SUPPORTED:
/* Note: list of services that are executed, not initiated. */ /* Note: list of services that are executed, not initiated. */
bitstring_init(&bit_string); bitstring_init(&bit_string);
for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++) for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++) {
{
/* automatic lookup based on handlers set */ /* automatic lookup based on handlers set */
bitstring_set_bit(&bit_string, (uint8_t)i, apdu_service_supported(i)); bitstring_set_bit(&bit_string, (uint8_t) i,
apdu_service_supported(i));
} }
apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string); apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string);
break; break;
@@ -546,18 +522,17 @@ int Device_Encode_Property_APDU(
/* Note: this is the list of objects that can be in this device, /* Note: this is the list of objects that can be in this device,
not a list of objects that this device can access */ not a list of objects that this device can access */
bitstring_init(&bit_string); bitstring_init(&bit_string);
for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++) for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++) {
{
// initialize all the object types to not-supported // initialize all the object types to not-supported
bitstring_set_bit(&bit_string, (uint8_t)i, false); bitstring_set_bit(&bit_string, (uint8_t) i, false);
} }
/* FIXME: indicate the objects that YOU support */ /* FIXME: indicate the objects that YOU support */
bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); bitstring_set_bit(&bit_string, OBJECT_DEVICE, true);
bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true);
bitstring_set_bit(&bit_string, OBJECT_ANALOG_OUTPUT, true); bitstring_set_bit(&bit_string, OBJECT_ANALOG_OUTPUT, true);
#if BACFILE #if BACFILE
bitstring_set_bit(&bit_string, OBJECT_FILE, true); bitstring_set_bit(&bit_string, OBJECT_FILE, true);
#endif #endif
apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string); apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string);
break; break;
case PROP_OBJECT_LIST: case PROP_OBJECT_LIST:
@@ -569,27 +544,23 @@ int Device_Encode_Property_APDU(
// into one packet. Note that more than likely you will have // into one packet. Note that more than likely you will have
// to return an error if the number of encoded objects exceeds // to return an error if the number of encoded objects exceeds
// your maximum APDU size. // your maximum APDU size.
else if (array_index == BACNET_ARRAY_ALL) else if (array_index == BACNET_ARRAY_ALL) {
{ for (i = 1; i <= count; i++) {
for (i = 1; i <= count; i++) if (Device_Object_List_Identifier(i, &object_type,
{ &instance)) {
if (Device_Object_List_Identifier(i,&object_type,&instance)) len =
{ encode_tagged_object_id(&apdu[apdu_len],
len = encode_tagged_object_id(&apdu[apdu_len], object_type, object_type, instance);
instance);
apdu_len += len; apdu_len += len;
// assume next one is the same size as this one // assume next one is the same size as this one
// can we all fit into the APDU? // can we all fit into the APDU?
if ((apdu_len + len) >= MAX_APDU) if ((apdu_len + len) >= MAX_APDU) {
{
*error_class = ERROR_CLASS_SERVICES; *error_class = ERROR_CLASS_SERVICES;
*error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT;
apdu_len = -1; apdu_len = -1;
break; break;
} }
} } else {
else
{
// error: internal error? // error: internal error?
*error_class = ERROR_CLASS_SERVICES; *error_class = ERROR_CLASS_SERVICES;
*error_code = ERROR_CODE_OTHER; *error_code = ERROR_CODE_OTHER;
@@ -597,13 +568,13 @@ int Device_Encode_Property_APDU(
break; break;
} }
} }
} } else {
else if (Device_Object_List_Identifier(array_index, &object_type,
{ &instance))
if (Device_Object_List_Identifier(array_index,&object_type,&instance)) apdu_len =
apdu_len = encode_tagged_object_id(&apdu[0], object_type, instance); encode_tagged_object_id(&apdu[0], object_type,
else instance);
{ else {
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_ARRAY_INDEX; *error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
apdu_len = -1; apdu_len = -1;
@@ -622,7 +593,8 @@ int Device_Encode_Property_APDU(
apdu_len = encode_tagged_unsigned(&apdu[0], APDU_Timeout); apdu_len = encode_tagged_unsigned(&apdu[0], APDU_Timeout);
break; break;
case PROP_NUMBER_OF_APDU_RETRIES: case PROP_NUMBER_OF_APDU_RETRIES:
apdu_len = encode_tagged_unsigned(&apdu[0], Number_Of_APDU_Retries); apdu_len =
encode_tagged_unsigned(&apdu[0], Number_Of_APDU_Retries);
break; break;
case PROP_DEVICE_ADDRESS_BINDING: case PROP_DEVICE_ADDRESS_BINDING:
/* encode the list here, if it exists */ /* encode the list here, if it exists */
@@ -641,125 +613,102 @@ int Device_Encode_Property_APDU(
extern bool I_Am_Request; extern bool I_Am_Request;
// returns true if successful // returns true if successful
bool Device_Write_Property( bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
BACNET_WRITE_PROPERTY_DATA *wp_data, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
{ {
bool status = false; // return value bool status = false; // return value
if (!Device_Valid_Object_Instance_Number(wp_data->object_instance)) if (!Device_Valid_Object_Instance_Number(wp_data->object_instance)) {
{
*error_class = ERROR_CLASS_OBJECT; *error_class = ERROR_CLASS_OBJECT;
*error_code = ERROR_CODE_UNKNOWN_OBJECT; *error_code = ERROR_CODE_UNKNOWN_OBJECT;
return false; return false;
} }
// decode the some of the request // decode the some of the request
switch (wp_data->object_property) switch (wp_data->object_property) {
{
case PROP_OBJECT_IDENTIFIER: case PROP_OBJECT_IDENTIFIER:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_OBJECT_ID) if (wp_data->value.tag == BACNET_APPLICATION_TAG_OBJECT_ID) {
{
if ((wp_data->value.type.Object_Id.type == OBJECT_DEVICE) && if ((wp_data->value.type.Object_Id.type == OBJECT_DEVICE) &&
(Device_Set_Object_Instance_Number( (Device_Set_Object_Instance_Number(wp_data->value.type.
wp_data->value.type.Object_Id.instance))) Object_Id.instance))) {
{
I_Am_Request = true; I_Am_Request = true;
status = true; status = true;
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
} }
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE; *error_code = ERROR_CODE_INVALID_DATA_TYPE;
} }
break; break;
case PROP_NUMBER_OF_APDU_RETRIES: case PROP_NUMBER_OF_APDU_RETRIES:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) if (wp_data->value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) {
{
/* FIXME: bounds check? */ /* FIXME: bounds check? */
Device_Set_Number_Of_APDU_Retries((uint8_t)wp_data->value.type.Unsigned_Int); Device_Set_Number_Of_APDU_Retries((uint8_t) wp_data->value.
type.Unsigned_Int);
status = true; status = true;
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE; *error_code = ERROR_CODE_INVALID_DATA_TYPE;
} }
break; break;
case PROP_APDU_TIMEOUT: case PROP_APDU_TIMEOUT:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) if (wp_data->value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) {
{
/* FIXME: bounds check? */ /* FIXME: bounds check? */
Device_Set_APDU_Timeout((uint16_t)wp_data->value.type.Unsigned_Int); Device_Set_APDU_Timeout((uint16_t) wp_data->value.type.
Unsigned_Int);
status = true; status = true;
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE; *error_code = ERROR_CODE_INVALID_DATA_TYPE;
} }
break; break;
case PROP_VENDOR_IDENTIFIER: case PROP_VENDOR_IDENTIFIER:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) if (wp_data->value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) {
{
/* FIXME: bounds check? */ /* FIXME: bounds check? */
Device_Set_Vendor_Identifier((uint16_t)wp_data->value.type.Unsigned_Int); Device_Set_Vendor_Identifier((uint16_t) wp_data->value.type.
Unsigned_Int);
status = true; status = true;
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE; *error_code = ERROR_CODE_INVALID_DATA_TYPE;
} }
break; break;
case PROP_SYSTEM_STATUS: case PROP_SYSTEM_STATUS:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_ENUMERATED) if (wp_data->value.tag == BACNET_APPLICATION_TAG_ENUMERATED) {
{
Device_Set_System_Status(wp_data->value.type.Enumerated); Device_Set_System_Status(wp_data->value.type.Enumerated);
status = true; status = true;
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE; *error_code = ERROR_CODE_INVALID_DATA_TYPE;
} }
break; break;
case PROP_OBJECT_NAME: case PROP_OBJECT_NAME:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) if (wp_data->value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) {
{
uint8_t encoding; uint8_t encoding;
encoding = characterstring_encoding(&wp_data->value.type.Character_String); encoding =
if (encoding == CHARACTER_ANSI_X34) characterstring_encoding(&wp_data->value.type.
{ Character_String);
status = Device_Set_Object_Name( if (encoding == CHARACTER_ANSI_X34) {
characterstring_value(&wp_data->value.type.Character_String), status =
characterstring_length(&wp_data->value.type.Character_String)); Device_Set_Object_Name(characterstring_value(&wp_data->
if (!status) value.type.Character_String),
{ characterstring_length(&wp_data->value.type.
Character_String));
if (!status) {
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; *error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY;
} }
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; *error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED;
} }
} } else {
else
{
*error_class = ERROR_CLASS_PROPERTY; *error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE; *error_code = ERROR_CODE_INVALID_DATA_TYPE;
} }
@@ -790,25 +739,27 @@ void testDevice(Test * pTest)
status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE); status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE);
ct_test(pTest, Device_Object_Instance_Number() == BACNET_MAX_INSTANCE); ct_test(pTest, Device_Object_Instance_Number() == BACNET_MAX_INSTANCE);
ct_test(pTest, status == true); ct_test(pTest, status == true);
status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE/2); status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE / 2);
ct_test(pTest, Device_Object_Instance_Number() == (BACNET_MAX_INSTANCE/2)); ct_test(pTest,
Device_Object_Instance_Number() == (BACNET_MAX_INSTANCE / 2));
ct_test(pTest, status == true); ct_test(pTest, status == true);
status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE+1); status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE + 1);
ct_test(pTest, Device_Object_Instance_Number() != (BACNET_MAX_INSTANCE+1)); ct_test(pTest,
Device_Object_Instance_Number() != (BACNET_MAX_INSTANCE + 1));
ct_test(pTest, status == false); ct_test(pTest, status == false);
Device_Set_System_Status(STATUS_NON_OPERATIONAL); Device_Set_System_Status(STATUS_NON_OPERATIONAL);
ct_test(pTest, Device_System_Status() == STATUS_NON_OPERATIONAL); ct_test(pTest, Device_System_Status() == STATUS_NON_OPERATIONAL);
Device_Set_Vendor_Name(name,strlen(name)); Device_Set_Vendor_Name(name, strlen(name));
ct_test(pTest, strcmp(Device_Vendor_Name(),name) == 0); ct_test(pTest, strcmp(Device_Vendor_Name(), name) == 0);
Device_Set_Vendor_Identifier(42); Device_Set_Vendor_Identifier(42);
ct_test(pTest, Device_Vendor_Identifier() == 42); ct_test(pTest, Device_Vendor_Identifier() == 42);
Device_Set_Model_Name(name,strlen(name)); Device_Set_Model_Name(name, strlen(name));
ct_test(pTest, strcmp(Device_Model_Name(),name) == 0); ct_test(pTest, strcmp(Device_Model_Name(), name) == 0);
return; return;
} }
@@ -866,5 +817,3 @@ int main(void)
} }
#endif /* TEST_DEVICE */ #endif /* TEST_DEVICE */
#endif /* TEST */ #endif /* TEST */
+40 -49
View File
@@ -44,76 +44,67 @@
extern "C" { extern "C" {
#endif /* __cplusplus */ #endif /* __cplusplus */
uint32_t Device_Object_Instance_Number(void); uint32_t Device_Object_Instance_Number(void);
bool Device_Set_Object_Instance_Number(uint32_t object_id); bool Device_Set_Object_Instance_Number(uint32_t object_id);
bool Device_Valid_Object_Instance_Number(uint32_t object_id); bool Device_Valid_Object_Instance_Number(uint32_t object_id);
unsigned Device_Object_List_Count(void); unsigned Device_Object_List_Count(void);
bool Device_Object_List_Identifier(unsigned array_index, bool Device_Object_List_Identifier(unsigned array_index,
int *object_type, int *object_type, uint32_t * instance);
uint32_t *instance);
BACNET_DEVICE_STATUS Device_System_Status(void); BACNET_DEVICE_STATUS Device_System_Status(void);
void Device_Set_System_Status(BACNET_DEVICE_STATUS status); void Device_Set_System_Status(BACNET_DEVICE_STATUS status);
const char *Device_Vendor_Name(void); const char *Device_Vendor_Name(void);
bool Device_Set_Vendor_Name(const char *name, size_t length); bool Device_Set_Vendor_Name(const char *name, size_t length);
uint16_t Device_Vendor_Identifier(void); uint16_t Device_Vendor_Identifier(void);
void Device_Set_Vendor_Identifier(uint16_t vendor_id); void Device_Set_Vendor_Identifier(uint16_t vendor_id);
const char *Device_Model_Name(void); const char *Device_Model_Name(void);
bool Device_Set_Model_Name(const char *name, size_t length); bool Device_Set_Model_Name(const char *name, size_t length);
const char *Device_Firmware_Revision(void); const char *Device_Firmware_Revision(void);
bool Device_Set_Firmware_Revision(const char *name, size_t length); bool Device_Set_Firmware_Revision(const char *name, size_t length);
const char *Device_Application_Software_Version(void); const char *Device_Application_Software_Version(void);
bool Device_Set_Application_Software_Version(const char *name, size_t length); bool Device_Set_Application_Software_Version(const char *name,
size_t length);
const char *Device_Description(void); const char *Device_Description(void);
bool Device_Set_Description(const char *name, size_t length); bool Device_Set_Description(const char *name, size_t length);
const char *Device_Location(void); const char *Device_Location(void);
bool Device_Set_Location(const char *name, size_t length); bool Device_Set_Location(const char *name, size_t length);
// some stack-centric constant values - no set methods // some stack-centric constant values - no set methods
uint8_t Device_Protocol_Version(void); uint8_t Device_Protocol_Version(void);
uint8_t Device_Protocol_Revision(void); uint8_t Device_Protocol_Revision(void);
uint16_t Device_Max_APDU_Length_Accepted(void); uint16_t Device_Max_APDU_Length_Accepted(void);
BACNET_SEGMENTATION Device_Segmentation_Supported(void); BACNET_SEGMENTATION Device_Segmentation_Supported(void);
uint16_t Device_APDU_Timeout(void); uint16_t Device_APDU_Timeout(void);
void Device_Set_APDU_Timeout(uint16_t timeout); void Device_Set_APDU_Timeout(uint16_t timeout);
uint8_t Device_Number_Of_APDU_Retries(void); uint8_t Device_Number_Of_APDU_Retries(void);
void Device_Set_Number_Of_APDU_Retries(uint8_t retries); void Device_Set_Number_Of_APDU_Retries(uint8_t retries);
uint8_t Device_Database_Revision(void); uint8_t Device_Database_Revision(void);
void Device_Set_Database_Revision(uint8_t revision); void Device_Set_Database_Revision(uint8_t revision);
bool Device_Valid_Object_Name( bool Device_Valid_Object_Name(const char *object_name,
const char *object_name, int *object_type, uint32_t * object_instance);
int *object_type, char *Device_Valid_Object_Id(int object_type,
uint32_t *object_instance);
char *Device_Valid_Object_Id(
int object_type,
uint32_t object_instance); uint32_t object_instance);
int Device_Encode_Property_APDU( int Device_Encode_Property_APDU(uint8_t * apdu,
uint8_t *apdu,
BACNET_PROPERTY_ID property, BACNET_PROPERTY_ID property,
int32_t array_index, int32_t array_index,
BACNET_ERROR_CLASS *error_class, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
BACNET_ERROR_CODE *error_code);
bool Device_Write_Property( bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
BACNET_WRITE_PROPERTY_DATA *wp_data, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif #endif
+100 -175
View File
@@ -49,7 +49,7 @@
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive // buffer used for receive
static uint8_t Rx_Buf[MAX_MPDU] = {0}; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
static uint32_t Target_File_Object_Instance = BACNET_MAX_INSTANCE; static uint32_t Target_File_Object_Instance = BACNET_MAX_INSTANCE;
@@ -60,56 +60,46 @@ static bool End_Of_File_Detected = false;
static bool Error_Detected = false; static bool Error_Detected = false;
static uint8_t Current_Invoke_ID = 0; static uint8_t Current_Invoke_ID = 0;
static void Atomic_Read_File_Error_Handler( static void Atomic_Read_File_Error_Handler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src,
uint8_t invoke_id, uint8_t invoke_id,
BACNET_ERROR_CLASS error_class, BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code)
BACNET_ERROR_CODE error_code)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("\r\nBACnet Error!\r\n"); printf("\r\nBACnet Error!\r\n");
printf("Error Class: %s\r\n", printf("Error Class: %s\r\n", bactext_error_class_name(error_class));
bactext_error_class_name(error_class)); printf("Error Code: %s\r\n", bactext_error_code_name(error_code));
printf("Error Code: %s\r\n",
bactext_error_code_name(error_code));
Error_Detected = true; Error_Detected = true;
} }
void MyAbortHandler( void MyAbortHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t abort_reason)
uint8_t invoke_id,
uint8_t abort_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("\r\nBACnet Abort!\r\n"); printf("\r\nBACnet Abort!\r\n");
printf("Abort Reason: %s\r\n", printf("Abort Reason: %s\r\n",
bactext_abort_reason_name(abort_reason)); bactext_abort_reason_name(abort_reason));
Error_Detected = true; Error_Detected = true;
} }
void MyRejectHandler( void MyRejectHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t reject_reason)
uint8_t invoke_id,
uint8_t reject_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("\r\nBACnet Reject!\r\n"); printf("\r\nBACnet Reject!\r\n");
printf("Reject Reason: %s\r\n", printf("Reject Reason: %s\r\n",
bactext_reject_reason_name(reject_reason)); bactext_reject_reason_name(reject_reason));
Error_Detected = true; Error_Detected = true;
} }
static uint8_t Send_Atomic_Read_File_Stream( static uint8_t Send_Atomic_Read_File_Stream(uint32_t device_id,
uint32_t device_id,
uint32_t file_instance, uint32_t file_instance,
int fileStartPosition, int fileStartPosition, unsigned requestedOctetCount)
unsigned requestedOctetCount)
{ {
BACNET_ADDRESS dest; BACNET_ADDRESS dest;
BACNET_ADDRESS my_address; BACNET_ADDRESS my_address;
@@ -125,14 +115,9 @@ static uint8_t Send_Atomic_Read_File_Stream(
/* is there a tsm available? */ /* is there a tsm available? */
if (status) if (status)
status = tsm_transaction_available(); status = tsm_transaction_available();
if (status) if (status) {
{
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID(); invoke_id = tsm_next_free_invokeID();
@@ -142,32 +127,24 @@ static uint8_t Send_Atomic_Read_File_Stream(
data.access = FILE_STREAM_ACCESS; data.access = FILE_STREAM_ACCESS;
data.type.stream.fileStartPosition = fileStartPosition; data.type.stream.fileStartPosition = fileStartPosition;
data.type.stream.requestedOctetCount = requestedOctetCount; data.type.stream.requestedOctetCount = requestedOctetCount;
pdu_len += arf_encode_apdu( pdu_len += arf_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len], invoke_id, &data);
invoke_id,
&data);
/* will the APDU fit the target device? /* will the APDU fit the target device?
note: if there is a bottleneck router in between note: if there is a bottleneck router in between
us and the destination, we won't know unless us and the destination, we won't know unless
we have a way to check for that and update the we have a way to check for that and update the
max_apdu in the address binding table. */ max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu) if ((unsigned) pdu_len < max_apdu) {
{ tsm_set_confirmed_unsegmented_transaction(invoke_id,
tsm_set_confirmed_unsegmented_transaction( &dest, &Handler_Transmit_Buffer[0], pdu_len);
invoke_id, bytes_sent = datalink_send_pdu(&dest, // destination address
&dest, &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len);
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send AtomicReadFile Request (%s)!\n", fprintf(stderr,
"Failed to Send AtomicReadFile Request (%s)!\n",
strerror(errno)); strerror(errno));
} } else
else fprintf(stderr, "Failed to Send AtomicReadFile Request "
fprintf(stderr,"Failed to Send AtomicReadFile Request "
"(payload exceeds destination maximum APDU)!\n"); "(payload exceeds destination maximum APDU)!\n");
} }
@@ -184,72 +161,57 @@ static void Send_WhoIs(uint32_t device_id)
datalink_get_broadcast_address(&dest); datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */ /* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, // true for confirmed messages
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */ /* encode the APDU portion of the packet */
pdu_len += whois_encode_apdu( pdu_len += whois_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len], device_id, device_id);
device_id,
device_id);
bytes_sent = datalink_send_pdu( bytes_sent = datalink_send_pdu(&dest, /* destination address */
&dest, /* destination address */ &Handler_Transmit_Buffer[0], pdu_len); /* number of bytes of data */
&Handler_Transmit_Buffer[0],
pdu_len); /* number of bytes of data */
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send Who-Is Request (%s)!\n", strerror(errno)); fprintf(stderr, "Failed to Send Who-Is Request (%s)!\n",
strerror(errno));
} }
static void AtomicReadFileAckHandler( static void AtomicReadFileAckHandler(uint8_t * service_request,
uint8_t *service_request,
uint16_t service_len, uint16_t service_len,
BACNET_ADDRESS *src, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data)
BACNET_CONFIRMED_SERVICE_ACK_DATA *service_data)
{ {
int len = 0; int len = 0;
BACNET_ATOMIC_READ_FILE_DATA data; BACNET_ATOMIC_READ_FILE_DATA data;
FILE *pFile = NULL; /* stream pointer */ FILE *pFile = NULL; /* stream pointer */
size_t octets_written = 0; size_t octets_written = 0;
(void)src; /* FIXME: validate the source address matches */ (void) src; /* FIXME: validate the source address matches */
len = arf_ack_decode_service_request( len = arf_ack_decode_service_request(service_request,
service_request, service_len, &data);
service_len, if (len > 0) {
&data);
if (len > 0)
{
/* validate the parameters before storing data */ /* validate the parameters before storing data */
if ((data.access == FILE_STREAM_ACCESS) && if ((data.access == FILE_STREAM_ACCESS) &&
(service_data->invoke_id == Current_Invoke_ID)) (service_data->invoke_id == Current_Invoke_ID)) {
{
if (data.type.stream.fileStartPosition == 0) if (data.type.stream.fileStartPosition == 0)
pFile = fopen(Local_File_Name, "wb"); pFile = fopen(Local_File_Name, "wb");
else else
pFile = fopen(Local_File_Name, "rb+"); pFile = fopen(Local_File_Name, "rb+");
if (pFile) if (pFile) {
{
/* is there anything to do with this? data.stream.requestedOctetCount */ /* is there anything to do with this? data.stream.requestedOctetCount */
(void)fseek(pFile, data.type.stream.fileStartPosition, SEEK_SET); (void) fseek(pFile, data.type.stream.fileStartPosition,
octets_written = fwrite( SEEK_SET);
octetstring_value(&data.fileData), octets_written = fwrite(octetstring_value(&data.fileData), 1, /* unit to write in bytes - in our case, an octet is one byte */
1, /* unit to write in bytes - in our case, an octet is one byte */ octetstring_length(&data.fileData), pFile);
octetstring_length(&data.fileData),
pFile);
if (octets_written != octetstring_length(&data.fileData)) if (octets_written != octetstring_length(&data.fileData))
fprintf(stderr,"Unable to write data to file \"%s\".\n", fprintf(stderr,
"Unable to write data to file \"%s\".\n",
Local_File_Name); Local_File_Name);
else else
printf("\r%u bytes", printf("\r%u bytes",
(data.type.stream.fileStartPosition + octets_written)); (data.type.stream.fileStartPosition +
octets_written));
fclose(pFile); fclose(pFile);
} }
if (data.endOfFile) if (data.endOfFile) {
{
End_Of_File_Detected = true; End_Of_File_Detected = true;
printf("\r\n"); printf("\r\n");
} }
@@ -257,10 +219,8 @@ static void AtomicReadFileAckHandler(
} }
} }
static void LocalIAmHandler( static void LocalIAmHandler(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src)
uint16_t service_len,
BACNET_ADDRESS *src)
{ {
int len = 0; int len = 0;
uint32_t device_id = 0; uint32_t device_id = 0;
@@ -268,22 +228,14 @@ static void LocalIAmHandler(
int segmentation = 0; int segmentation = 0;
uint16_t vendor_id = 0; uint16_t vendor_id = 0;
(void)src; (void) src;
(void)service_len; (void) service_len;
len = iam_decode_service_request( len = iam_decode_service_request(service_request,
service_request, &device_id, &max_apdu, &segmentation, &vendor_id);
&device_id, if (len != -1) {
&max_apdu, address_add(device_id, max_apdu, src);
&segmentation, } else
&vendor_id); fprintf(stderr, "!\n");
if (len != -1)
{
address_add(device_id,
max_apdu,
src);
}
else
fprintf(stderr,"!\n");
return; return;
} }
@@ -292,38 +244,31 @@ static void Init_Service_Handlers(void)
{ {
/* we need to handle who-is /* we need to handle who-is
to support dynamic device binding to us */ to support dynamic device binding to us */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
/* handle i-am to support binding to other devices */ /* handle i-am to support binding to other devices */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler); LocalIAmHandler);
/* set the handler for all the services we don't implement /* set the handler for all the services we don't implement
It is required to send the proper reject message... */ It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler( apdu_set_unrecognized_service_handler_handler
handler_unrecognized_service); (handler_unrecognized_service);
/* we must implement read property - it's required! */ /* we must implement read property - it's required! */
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
/* handle the data coming back from confirmed requests */ /* handle the data coming back from confirmed requests */
apdu_set_confirmed_ack_handler( apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
AtomicReadFileAckHandler); AtomicReadFileAckHandler);
/* handle any errors coming back */ /* handle any errors coming back */
apdu_set_error_handler( apdu_set_error_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
Atomic_Read_File_Error_Handler); Atomic_Read_File_Error_Handler);
apdu_set_abort_handler( apdu_set_abort_handler(MyAbortHandler);
MyAbortHandler); apdu_set_reject_handler(MyRejectHandler);
apdu_set_reject_handler(
MyRejectHandler);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = { 0 }; // address where message came from
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; // milliseconds
unsigned max_apdu = 0; unsigned max_apdu = 0;
@@ -337,27 +282,24 @@ int main(int argc, char *argv[])
bool found = false; bool found = false;
uint16_t my_max_apdu = 0; uint16_t my_max_apdu = 0;
if (argc < 4) if (argc < 4) {
{
/* FIXME: what about access method - record or stream? */ /* FIXME: what about access method - record or stream? */
printf("%s device-instance file-instance local-name\r\n", printf("%s device-instance file-instance local-name\r\n",
filename_remove_path(argv[0])); filename_remove_path(argv[0]));
return 0; return 0;
} }
/* decode the command line parameters */ /* decode the command line parameters */
Target_Device_Object_Instance = strtol(argv[1],NULL,0); Target_Device_Object_Instance = strtol(argv[1], NULL, 0);
Target_File_Object_Instance = strtol(argv[2],NULL,0); Target_File_Object_Instance = strtol(argv[2], NULL, 0);
Local_File_Name = argv[3]; Local_File_Name = argv[3];
if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE) if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE) {
{ fprintf(stderr, "device-instance=%u - it must be less than %u\r\n",
fprintf(stderr,"device-instance=%u - it must be less than %u\r\n", Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
Target_Device_Object_Instance,BACNET_MAX_INSTANCE);
return 1; return 1;
} }
if (Target_File_Object_Instance >= BACNET_MAX_INSTANCE) if (Target_File_Object_Instance >= BACNET_MAX_INSTANCE) {
{ fprintf(stderr, "file-instance=%u - it must be less than %u\r\n",
fprintf(stderr,"file-instance=%u - it must be less than %u\r\n", Target_File_Object_Instance, BACNET_MAX_INSTANCE + 1);
Target_File_Object_Instance,BACNET_MAX_INSTANCE+1);
return 1; return 1;
} }
/* setup my info */ /* setup my info */
@@ -376,45 +318,31 @@ int main(int argc, char *argv[])
/* try to bind with the device */ /* try to bind with the device */
Send_WhoIs(Target_Device_Object_Instance); Send_WhoIs(Target_Device_Object_Instance);
/* loop forever */ /* loop forever */
for (;;) for (;;) {
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
current_seconds = time(NULL); current_seconds = time(NULL);
/* returns 0 bytes on timeout */ /* returns 0 bytes on timeout */
pdu_len = bip_receive( pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
/* process */ /* process */
if (pdu_len) if (pdu_len) {
{ npdu_handler(&src, &Rx_Buf[0], pdu_len);
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
} }
/* at least one second has passed */ /* at least one second has passed */
if (current_seconds != last_seconds) if (current_seconds != last_seconds)
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); tsm_timer_milliseconds(((current_seconds -
last_seconds) * 1000));
if (End_Of_File_Detected || Error_Detected) if (End_Of_File_Detected || Error_Detected)
break; break;
if (I_Am_Request) if (I_Am_Request) {
{
I_Am_Request = false; I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]); iam_send(&Handler_Transmit_Buffer[0]);
} } else {
else
{
/* wait until the device is bound, or timeout and quit */ /* wait until the device is bound, or timeout and quit */
found = address_bind_request( found = address_bind_request(Target_Device_Object_Instance,
Target_Device_Object_Instance, &max_apdu, &Target_Address);
&max_apdu, if (found) {
&Target_Address);
if (found)
{
/* calculate the smaller of our APDU size or theirs /* calculate the smaller of our APDU size or theirs
and remove the overhead of the APDU (about 16 octets max). and remove the overhead of the APDU (about 16 octets max).
note: we could fail if there is a bottle neck (router) note: we could fail if there is a bottle neck (router)
@@ -426,22 +354,19 @@ int main(int argc, char *argv[])
requestedOctetCount = my_max_apdu - 16; requestedOctetCount = my_max_apdu - 16;
/* has the previous invoke id expired or returned? /* has the previous invoke id expired or returned?
note: invoke ID = 0 is invalid, so it will be idle */ note: invoke ID = 0 is invalid, so it will be idle */
if ((invoke_id == 0) || tsm_invoke_id_free(invoke_id)) if ((invoke_id == 0) || tsm_invoke_id_free(invoke_id)) {
{
if (invoke_id != 0) if (invoke_id != 0)
fileStartPosition += requestedOctetCount; fileStartPosition += requestedOctetCount;
/* we'll read the file in chunks /* we'll read the file in chunks
less than max_apdu to keep unsegmented */ less than max_apdu to keep unsegmented */
invoke_id = Send_Atomic_Read_File_Stream( invoke_id =
Target_Device_Object_Instance, Send_Atomic_Read_File_Stream
Target_File_Object_Instance, (Target_Device_Object_Instance,
fileStartPosition, Target_File_Object_Instance, fileStartPosition,
requestedOctetCount); requestedOctetCount);
Current_Invoke_ID = invoke_id; Current_Invoke_ID = invoke_id;
} }
} } else {
else
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds); elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds) if (elapsed_seconds > timeout_seconds)
+63 -98
View File
@@ -50,7 +50,7 @@
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive // buffer used for receive
static uint8_t Rx_Buf[MAX_MPDU] = {0}; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE; static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE;
@@ -62,42 +62,36 @@ static int32_t Target_Object_Index = BACNET_ARRAY_ALL;
static BACNET_ADDRESS Target_Address; static BACNET_ADDRESS Target_Address;
static bool Error_Detected = false; static bool Error_Detected = false;
static void MyErrorHandler( static void MyErrorHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src,
uint8_t invoke_id, uint8_t invoke_id,
BACNET_ERROR_CLASS error_class, BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code)
BACNET_ERROR_CODE error_code)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("BACnet Error: %s: %s\r\n", printf("BACnet Error: %s: %s\r\n",
bactext_error_class_name(error_class), bactext_error_class_name(error_class),
bactext_error_code_name(error_code)); bactext_error_code_name(error_code));
Error_Detected = true; Error_Detected = true;
} }
void MyAbortHandler( void MyAbortHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t abort_reason)
uint8_t invoke_id,
uint8_t abort_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("BACnet Abort: %s\r\n", printf("BACnet Abort: %s\r\n",
bactext_abort_reason_name(abort_reason)); bactext_abort_reason_name(abort_reason));
Error_Detected = true; Error_Detected = true;
} }
void MyRejectHandler( void MyRejectHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t reject_reason)
uint8_t invoke_id,
uint8_t reject_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("BACnet Reject: %s\r\n", printf("BACnet Reject: %s\r\n",
bactext_reject_reason_name(reject_reason)); bactext_reject_reason_name(reject_reason));
Error_Detected = true; Error_Detected = true;
@@ -107,38 +101,31 @@ static void Init_Service_Handlers(void)
{ {
/* we need to handle who-is /* we need to handle who-is
to support dynamic device binding to us */ to support dynamic device binding to us */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
/* handle i-am to support binding to other devices */ /* handle i-am to support binding to other devices */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
SERVICE_UNCONFIRMED_I_AM,
handler_i_am_bind); handler_i_am_bind);
/* set the handler for all the services we don't implement /* set the handler for all the services we don't implement
It is required to send the proper reject message... */ It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler( apdu_set_unrecognized_service_handler_handler
handler_unrecognized_service); (handler_unrecognized_service);
/* we must implement read property - it's required! */ /* we must implement read property - it's required! */
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
/* handle the data coming back from confirmed requests */ /* handle the data coming back from confirmed requests */
apdu_set_confirmed_ack_handler( apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property_ack); handler_read_property_ack);
/* handle any errors coming back */ /* handle any errors coming back */
apdu_set_error_handler( apdu_set_error_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
MyErrorHandler); MyErrorHandler);
apdu_set_abort_handler( apdu_set_abort_handler(MyAbortHandler);
MyAbortHandler); apdu_set_reject_handler(MyRejectHandler);
apdu_set_reject_handler(
MyRejectHandler);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = { 0 }; // address where message came from
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; // milliseconds
unsigned max_apdu = 0; unsigned max_apdu = 0;
@@ -149,41 +136,37 @@ int main(int argc, char *argv[])
uint8_t invoke_id = 0; uint8_t invoke_id = 0;
bool found = false; bool found = false;
if (argc < 5) if (argc < 5) {
{ printf
printf("%s device-instance object-type object-instance property [index]\r\n", ("%s device-instance object-type object-instance property [index]\r\n",
filename_remove_path(argv[0])); filename_remove_path(argv[0]));
return 0; return 0;
} }
/* decode the command line parameters */ /* decode the command line parameters */
Target_Device_Object_Instance = strtol(argv[1],NULL,0); Target_Device_Object_Instance = strtol(argv[1], NULL, 0);
Target_Object_Type = strtol(argv[2],NULL,0); Target_Object_Type = strtol(argv[2], NULL, 0);
Target_Object_Instance = strtol(argv[3],NULL,0); Target_Object_Instance = strtol(argv[3], NULL, 0);
Target_Object_Property = strtol(argv[4],NULL,0); Target_Object_Property = strtol(argv[4], NULL, 0);
if (argc > 5) if (argc > 5)
Target_Object_Index = strtol(argv[5],NULL,0); Target_Object_Index = strtol(argv[5], NULL, 0);
if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE) if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE) {
{ fprintf(stderr, "device-instance=%u - it must be less than %u\r\n",
fprintf(stderr,"device-instance=%u - it must be less than %u\r\n", Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
Target_Device_Object_Instance,BACNET_MAX_INSTANCE);
return 1; return 1;
} }
if (Target_Object_Type > MAX_BACNET_OBJECT_TYPE) if (Target_Object_Type > MAX_BACNET_OBJECT_TYPE) {
{ fprintf(stderr, "object-type=%u - it must be less than %u\r\n",
fprintf(stderr,"object-type=%u - it must be less than %u\r\n", Target_Object_Type, MAX_BACNET_OBJECT_TYPE + 1);
Target_Object_Type,MAX_BACNET_OBJECT_TYPE+1);
return 1; return 1;
} }
if (Target_Object_Instance > BACNET_MAX_INSTANCE) if (Target_Object_Instance > BACNET_MAX_INSTANCE) {
{ fprintf(stderr, "object-instance=%u - it must be less than %u\r\n",
fprintf(stderr,"object-instance=%u - it must be less than %u\r\n", Target_Object_Instance, BACNET_MAX_INSTANCE + 1);
Target_Object_Instance,BACNET_MAX_INSTANCE+1);
return 1; return 1;
} }
if (Target_Object_Property > MAX_BACNET_PROPERTY_ID) if (Target_Object_Property > MAX_BACNET_PROPERTY_ID) {
{ fprintf(stderr, "object-type=%u - it must be less than %u\r\n",
fprintf(stderr,"object-type=%u - it must be less than %u\r\n", Target_Object_Property, MAX_BACNET_PROPERTY_ID + 1);
Target_Object_Property,MAX_BACNET_PROPERTY_ID+1);
return 1; return 1;
} }
@@ -203,61 +186,43 @@ int main(int argc, char *argv[])
/* no need to spam the world */ /* no need to spam the world */
I_Am_Request = false; I_Am_Request = false;
/* try to bind with the device */ /* try to bind with the device */
Send_WhoIs(Target_Device_Object_Instance,Target_Device_Object_Instance); Send_WhoIs(Target_Device_Object_Instance,
Target_Device_Object_Instance);
/* loop forever */ /* loop forever */
for (;;) for (;;) {
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
current_seconds = time(NULL); current_seconds = time(NULL);
/* returns 0 bytes on timeout */ /* returns 0 bytes on timeout */
pdu_len = bip_receive( pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
/* process */ /* process */
if (pdu_len) if (pdu_len) {
{ npdu_handler(&src, &Rx_Buf[0], pdu_len);
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
} }
/* at least one second has passed */ /* at least one second has passed */
if (current_seconds != last_seconds) if (current_seconds != last_seconds)
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); tsm_timer_milliseconds(((current_seconds -
last_seconds) * 1000));
if (Error_Detected) if (Error_Detected)
break; break;
if (I_Am_Request) if (I_Am_Request) {
{
I_Am_Request = false; I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]); iam_send(&Handler_Transmit_Buffer[0]);
} } else {
else
{
/* wait until the device is bound, or timeout and quit */ /* wait until the device is bound, or timeout and quit */
found = address_bind_request( found = address_bind_request(Target_Device_Object_Instance,
Target_Device_Object_Instance, &max_apdu, &Target_Address);
&max_apdu, if (found) {
&Target_Address); if (invoke_id == 0) {
if (found) invoke_id =
{ Send_Read_Property_Request
if (invoke_id == 0) (Target_Device_Object_Instance, Target_Object_Type,
{ Target_Object_Instance, Target_Object_Property,
invoke_id = Send_Read_Property_Request(
Target_Device_Object_Instance,
Target_Object_Type,
Target_Object_Instance,
Target_Object_Property,
Target_Object_Index); Target_Object_Index);
} } else if (tsm_invoke_id_free(invoke_id))
else if (tsm_invoke_id_free(invoke_id))
break; break;
} } else {
else
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds); elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds) if (elapsed_seconds > timeout_seconds)
+67 -104
View File
@@ -51,63 +51,57 @@
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive // buffer used for receive
static uint8_t Rx_Buf[MAX_MPDU] = {0}; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE; static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE;
static BACNET_ADDRESS Target_Address; static BACNET_ADDRESS Target_Address;
static BACNET_REINITIALIZED_STATE Reinitialize_State = BACNET_REINIT_COLDSTART; static BACNET_REINITIALIZED_STATE Reinitialize_State =
BACNET_REINIT_COLDSTART;
static char *Reinitialize_Password = NULL; static char *Reinitialize_Password = NULL;
static bool Error_Detected = false; static bool Error_Detected = false;
static void MyErrorHandler( static void MyErrorHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src,
uint8_t invoke_id, uint8_t invoke_id,
BACNET_ERROR_CLASS error_class, BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code)
BACNET_ERROR_CODE error_code)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("BACnet Error: %s: %s\r\n", printf("BACnet Error: %s: %s\r\n",
bactext_error_class_name(error_class), bactext_error_class_name(error_class),
bactext_error_code_name(error_code)); bactext_error_code_name(error_code));
Error_Detected = true; Error_Detected = true;
} }
void MyAbortHandler( void MyAbortHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t abort_reason)
uint8_t invoke_id,
uint8_t abort_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("BACnet Abort: %s\r\n", printf("BACnet Abort: %s\r\n",
bactext_abort_reason_name(abort_reason)); bactext_abort_reason_name(abort_reason));
Error_Detected = true; Error_Detected = true;
} }
void MyRejectHandler( void MyRejectHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t reject_reason)
uint8_t invoke_id,
uint8_t reject_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("BACnet Reject: %s\r\n", printf("BACnet Reject: %s\r\n",
bactext_reject_reason_name(reject_reason)); bactext_reject_reason_name(reject_reason));
Error_Detected = true; Error_Detected = true;
} }
void MyReinitializeDeviceSimpleAckHandler( void MyReinitializeDeviceSimpleAckHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src,
uint8_t invoke_id) uint8_t invoke_id)
{ {
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("ReinitializeDevice Acknowledged!\r\n"); printf("ReinitializeDevice Acknowledged!\r\n");
} }
@@ -115,48 +109,38 @@ static void Init_Service_Handlers(void)
{ {
/* we need to handle who-is /* we need to handle who-is
to support dynamic device binding to us */ to support dynamic device binding to us */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
/* handle i-am to support binding to other devices */ /* handle i-am to support binding to other devices */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
SERVICE_UNCONFIRMED_I_AM,
handler_i_am_bind); handler_i_am_bind);
/* set the handler for all the services we don't implement /* set the handler for all the services we don't implement
It is required to send the proper reject message... */ It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler( apdu_set_unrecognized_service_handler_handler
handler_unrecognized_service); (handler_unrecognized_service);
/* we must implement read property - it's required! */ /* we must implement read property - it's required! */
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
/* handle the ack coming back */ /* handle the ack coming back */
apdu_set_confirmed_simple_ack_handler( apdu_set_confirmed_simple_ack_handler
SERVICE_CONFIRMED_REINITIALIZE_DEVICE, (SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
MyReinitializeDeviceSimpleAckHandler); MyReinitializeDeviceSimpleAckHandler);
/* handle any errors coming back */ /* handle any errors coming back */
apdu_set_error_handler( apdu_set_error_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
MyErrorHandler); MyErrorHandler);
apdu_set_abort_handler( apdu_set_abort_handler(MyAbortHandler);
MyAbortHandler); apdu_set_reject_handler(MyRejectHandler);
apdu_set_reject_handler(
MyRejectHandler);
} }
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
static void print_address( static void print_address(char *name, BACNET_ADDRESS * dest) // destination address
char *name,
BACNET_ADDRESS *dest) // destination address
{ {
int i = 0; // counter int i = 0; // counter
if (dest) if (dest) {
{ printf("%s: ", name);
printf("%s: ",name); for (i = 0; i < dest->mac_len; i++) {
for (i = 0; i < dest->mac_len; i++) printf("%02X", dest->mac[i]);
{
printf("%02X",dest->mac[i]);
} }
printf("\n"); printf("\n");
} }
@@ -165,7 +149,7 @@ static void print_address(
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = { 0 }; // address where message came from
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; // milliseconds
unsigned max_apdu = 0; unsigned max_apdu = 0;
@@ -175,15 +159,13 @@ int main(int argc, char *argv[])
time_t timeout_seconds = 0; time_t timeout_seconds = 0;
uint8_t invoke_id = 0; uint8_t invoke_id = 0;
bool found = false; bool found = false;
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
BACNET_ADDRESS my_address, broadcast_address; BACNET_ADDRESS my_address, broadcast_address;
#endif #endif
if (argc < 3) if (argc < 3) {
{
/* note: priority 16 and 0 should produce the same end results... */ /* note: priority 16 and 0 should produce the same end results... */
printf( printf("Usage: %s device-instance state [password]\r\n"
"Usage: %s device-instance state [password]\r\n"
"Send BACnet ReinitializeDevice service to device.\r\n" "Send BACnet ReinitializeDevice service to device.\r\n"
"\r\n" "\r\n"
"The device-instance can be 0 to %d.\r\n" "The device-instance can be 0 to %d.\r\n"
@@ -196,21 +178,19 @@ int main(int argc, char *argv[])
" 5=endrestore\r\n" " 5=endrestore\r\n"
" 6=abortrestore\r\n" " 6=abortrestore\r\n"
"The optional password is a character string of 1 to 20 characters.\r\n", "The optional password is a character string of 1 to 20 characters.\r\n",
filename_remove_path(argv[0]), filename_remove_path(argv[0]), BACNET_MAX_INSTANCE - 1);
BACNET_MAX_INSTANCE-1);
return 0; return 0;
} }
/* decode the command line parameters */ /* decode the command line parameters */
Target_Device_Object_Instance = strtol(argv[1],NULL,0); Target_Device_Object_Instance = strtol(argv[1], NULL, 0);
Reinitialize_State = strtol(argv[2],NULL,0); Reinitialize_State = strtol(argv[2], NULL, 0);
/* optional password */ /* optional password */
if (argc > 3) if (argc > 3)
Reinitialize_Password = argv[3]; Reinitialize_Password = argv[3];
if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE) if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE) {
{ fprintf(stderr, "device-instance=%u - it must be less than %u\r\n",
fprintf(stderr,"device-instance=%u - it must be less than %u\r\n", Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
Target_Device_Object_Instance,BACNET_MAX_INSTANCE);
return 1; return 1;
} }
@@ -223,12 +203,12 @@ int main(int argc, char *argv[])
bip_set_port(0xBAC0); bip_set_port(0xBAC0);
if (!bip_init()) if (!bip_init())
return 1; return 1;
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
datalink_get_broadcast_address(&broadcast_address); datalink_get_broadcast_address(&broadcast_address);
print_address("Broadcast",&broadcast_address); print_address("Broadcast", &broadcast_address);
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
print_address("Address",&my_address); print_address("Address", &my_address);
#endif #endif
/* configure the timeout values */ /* configure the timeout values */
last_seconds = time(NULL); last_seconds = time(NULL);
timeout_seconds = (Device_APDU_Timeout() / 1000) * timeout_seconds = (Device_APDU_Timeout() / 1000) *
@@ -236,59 +216,42 @@ int main(int argc, char *argv[])
/* don't send an I-Am unless asked */ /* don't send an I-Am unless asked */
I_Am_Request = false; I_Am_Request = false;
/* try to bind with the device */ /* try to bind with the device */
Send_WhoIs(Target_Device_Object_Instance,Target_Device_Object_Instance); Send_WhoIs(Target_Device_Object_Instance,
Target_Device_Object_Instance);
/* loop forever */ /* loop forever */
for (;;) for (;;) {
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
current_seconds = time(NULL); current_seconds = time(NULL);
/* returns 0 bytes on timeout */ /* returns 0 bytes on timeout */
pdu_len = bip_receive( pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
/* process */ /* process */
if (pdu_len) if (pdu_len) {
{ npdu_handler(&src, &Rx_Buf[0], pdu_len);
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
} }
/* at least one second has passed */ /* at least one second has passed */
if (current_seconds != last_seconds) if (current_seconds != last_seconds)
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); tsm_timer_milliseconds(((current_seconds -
last_seconds) * 1000));
if (Error_Detected) if (Error_Detected)
break; break;
if (I_Am_Request) if (I_Am_Request) {
{
I_Am_Request = false; I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]); iam_send(&Handler_Transmit_Buffer[0]);
} } else {
else
{
/* wait until the device is bound, or timeout and quit */ /* wait until the device is bound, or timeout and quit */
found = address_bind_request( found = address_bind_request(Target_Device_Object_Instance,
Target_Device_Object_Instance, &max_apdu, &Target_Address);
&max_apdu, if (found) {
&Target_Address); if (invoke_id == 0) {
if (found) invoke_id =
{ Send_Reinitialize_Device_Request
if (invoke_id == 0) (Target_Device_Object_Instance, Reinitialize_State,
{
invoke_id = Send_Reinitialize_Device_Request(
Target_Device_Object_Instance,
Reinitialize_State,
Reinitialize_Password); Reinitialize_Password);
} } else if (tsm_invoke_id_free(invoke_id))
else if (tsm_invoke_id_free(invoke_id))
break; break;
} } else {
else
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds); elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds) if (elapsed_seconds > timeout_seconds)
+33 -53
View File
@@ -48,38 +48,32 @@
/* This is an example application using the BACnet Stack */ /* This is an example application using the BACnet Stack */
/* buffers used for receiving */ /* buffers used for receiving */
static uint8_t Rx_Buf[MAX_MPDU] = {0}; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
static void Init_Service_Handlers(void) static void Init_Service_Handlers(void)
{ {
/* we need to handle who-is to support dynamic device binding */ /* we need to handle who-is to support dynamic device binding */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS,
SERVICE_UNCONFIRMED_WHO_HAS,
handler_who_has); handler_who_has);
/* set the handler for all the services we don't implement */ /* set the handler for all the services we don't implement */
/* It is required to send the proper reject message... */ /* It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler( apdu_set_unrecognized_service_handler_handler
handler_unrecognized_service); (handler_unrecognized_service);
/* Set the handlers for any confirmed services that we support. */ /* Set the handlers for any confirmed services that we support. */
/* We must implement read property - it's required! */ /* We must implement read property - it's required! */
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
SERVICE_CONFIRMED_WRITE_PROPERTY,
handler_write_property); handler_write_property);
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
handler_atomic_read_file); handler_atomic_read_file);
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
handler_reinitialize_device); handler_reinitialize_device);
/* handle communication so we can shutup when asked */ /* handle communication so we can shutup when asked */
apdu_set_confirmed_handler( apdu_set_confirmed_handler
SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL, (SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
handler_device_communication_control); handler_device_communication_control);
} }
@@ -88,18 +82,14 @@ static void cleanup(void)
datalink_cleanup(); datalink_cleanup();
} }
static void print_address( static void print_address(char *name, BACNET_ADDRESS * dest) // destination address
char *name,
BACNET_ADDRESS *dest) // destination address
{ {
int i = 0; // counter int i = 0; // counter
if (dest) if (dest) {
{ printf("%s: ", name);
printf("%s: ",name); for (i = 0; i < dest->mac_len; i++) {
for (i = 0; i < dest->mac_len; i++) printf("%02X", dest->mac[i]);
{
printf("%02X",dest->mac[i]);
} }
printf("\n"); printf("\n");
} }
@@ -107,7 +97,7 @@ static void print_address(
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = { 0 }; // address where message came from
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; // milliseconds
BACNET_ADDRESS my_address, broadcast_address; BACNET_ADDRESS my_address, broadcast_address;
@@ -116,63 +106,53 @@ int main(int argc, char *argv[])
/* allow the device ID to be set */ /* allow the device ID to be set */
if (argc > 1) if (argc > 1)
Device_Set_Object_Instance_Number(strtol(argv[1],NULL,0)); Device_Set_Object_Instance_Number(strtol(argv[1], NULL, 0));
if (argc > 2) if (argc > 2)
bip_set_port(strtol(argv[2],NULL,0)); bip_set_port(strtol(argv[2], NULL, 0));
printf("BACnet Server Demo - Device #%u\r\n", printf("BACnet Server Demo - Device #%u\r\n",
Device_Object_Instance_Number()); Device_Object_Instance_Number());
Init_Service_Handlers(); Init_Service_Handlers();
#ifdef BACDL_ETHERNET #ifdef BACDL_ETHERNET
// init the physical layer // init the physical layer
if (!ethernet_init("eth0")) if (!ethernet_init("eth0"))
return 1; return 1;
#endif #endif
#ifdef BACDL_BIP #ifdef BACDL_BIP
bip_set_interface("eth0"); bip_set_interface("eth0");
if (!bip_init()) if (!bip_init())
return 1; return 1;
printf("bip: using port %hu\r\n",bip_get_port()); printf("bip: using port %hu\r\n", bip_get_port());
#endif #endif
#ifdef BACDL_ARCNET #ifdef BACDL_ARCNET
if (!arcnet_init("arc0")) if (!arcnet_init("arc0"))
return 1; return 1;
#endif #endif
datalink_get_broadcast_address(&broadcast_address); datalink_get_broadcast_address(&broadcast_address);
print_address("Broadcast",&broadcast_address); print_address("Broadcast", &broadcast_address);
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
print_address("Address",&my_address); print_address("Address", &my_address);
atexit(cleanup); atexit(cleanup);
/* configure the timeout values */ /* configure the timeout values */
last_seconds = time(NULL); last_seconds = time(NULL);
/* broadcast an I-Am on startup */ /* broadcast an I-Am on startup */
I_Am_Request = true; I_Am_Request = true;
// loop forever // loop forever
for (;;) for (;;) {
{
// input // input
current_seconds = time(NULL); current_seconds = time(NULL);
// returns 0 bytes on timeout // returns 0 bytes on timeout
pdu_len = datalink_receive( pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
// process // process
if (pdu_len) if (pdu_len) {
{ npdu_handler(&src, &Rx_Buf[0], pdu_len);
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
} }
/* at least one second has passed */ /* at least one second has passed */
if (current_seconds != last_seconds) if (current_seconds != last_seconds)
dcc_timer_seconds(current_seconds - last_seconds); dcc_timer_seconds(current_seconds - last_seconds);
/* send out the I-Am if requested */ /* send out the I-Am if requested */
if (I_Am_Request) if (I_Am_Request) {
{
I_Am_Request = false; I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]); iam_send(&Handler_Transmit_Buffer[0]);
} }
+49 -82
View File
@@ -50,7 +50,7 @@
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive // buffer used for receive
static uint8_t Rx_Buf[MAX_MPDU] = {0}; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
static BACNET_OBJECT_TYPE Target_Object_Type = MAX_BACNET_OBJECT_TYPE; static BACNET_OBJECT_TYPE Target_Object_Type = MAX_BACNET_OBJECT_TYPE;
@@ -59,27 +59,23 @@ static char *Target_Object_Name = NULL;
static bool Error_Detected = false; static bool Error_Detected = false;
void MyAbortHandler( void MyAbortHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t abort_reason)
uint8_t invoke_id,
uint8_t abort_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("BACnet Abort: %s\r\n", printf("BACnet Abort: %s\r\n",
bactext_abort_reason_name(abort_reason)); bactext_abort_reason_name(abort_reason));
Error_Detected = true; Error_Detected = true;
} }
void MyRejectHandler( void MyRejectHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t reject_reason)
uint8_t invoke_id,
uint8_t reject_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("BACnet Reject: %s\r\n", printf("BACnet Reject: %s\r\n",
bactext_reject_reason_name(reject_reason)); bactext_reject_reason_name(reject_reason));
Error_Detected = true; Error_Detected = true;
@@ -89,41 +85,32 @@ static void Init_Service_Handlers(void)
{ {
/* we need to handle who-is /* we need to handle who-is
to support dynamic device binding to us */ to support dynamic device binding to us */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
/* set the handler for all the services we don't implement /* set the handler for all the services we don't implement
It is required to send the proper reject message... */ It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler( apdu_set_unrecognized_service_handler_handler
handler_unrecognized_service); (handler_unrecognized_service);
/* we must implement read property - it's required! */ /* we must implement read property - it's required! */
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
/* handle the reply (request) coming back */ /* handle the reply (request) coming back */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_HAVE,
SERVICE_UNCONFIRMED_I_HAVE,
handler_i_have); handler_i_have);
/* handle any errors coming back */ /* handle any errors coming back */
apdu_set_abort_handler( apdu_set_abort_handler(MyAbortHandler);
MyAbortHandler); apdu_set_reject_handler(MyRejectHandler);
apdu_set_reject_handler(
MyRejectHandler);
} }
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
static void print_address( static void print_address(char *name, BACNET_ADDRESS * dest) // destination address
char *name,
BACNET_ADDRESS *dest) // destination address
{ {
int i = 0; // counter int i = 0; // counter
if (dest) if (dest) {
{ printf("%s: ", name);
printf("%s: ",name); for (i = 0; i < dest->mac_len; i++) {
for (i = 0; i < dest->mac_len; i++) printf("%02X", dest->mac[i]);
{
printf("%02X",dest->mac[i]);
} }
printf("\n"); printf("\n");
} }
@@ -132,22 +119,20 @@ static void print_address(
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = { 0 }; // address where message came from
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; // milliseconds
time_t elapsed_seconds = 0; time_t elapsed_seconds = 0;
time_t last_seconds = 0; time_t last_seconds = 0;
time_t current_seconds = 0; time_t current_seconds = 0;
time_t timeout_seconds = 0; time_t timeout_seconds = 0;
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
BACNET_ADDRESS my_address, broadcast_address; BACNET_ADDRESS my_address, broadcast_address;
#endif #endif
if (argc < 2) if (argc < 2) {
{
/* note: priority 16 and 0 should produce the same end results... */ /* note: priority 16 and 0 should produce the same end results... */
printf( printf("Usage: %s <object-type object-instance | object-name>\r\n"
"Usage: %s <object-type object-instance | object-name>\r\n"
"Send BACnet WhoHas request to devices, and wait for responses.\r\n" "Send BACnet WhoHas request to devices, and wait for responses.\r\n"
"\r\n" "\r\n"
"Use either:\r\n" "Use either:\r\n"
@@ -156,29 +141,24 @@ int main(int argc, char *argv[])
"or:\r\n" "or:\r\n"
"The object-name can be any string of characters.\r\n", "The object-name can be any string of characters.\r\n",
filename_remove_path(argv[0]), filename_remove_path(argv[0]),
MAX_BACNET_OBJECT_TYPE-1, MAX_BACNET_OBJECT_TYPE - 1, BACNET_MAX_INSTANCE);
BACNET_MAX_INSTANCE);
return 0; return 0;
} }
/* decode the command line parameters */ /* decode the command line parameters */
if (argc < 3) if (argc < 3) {
{
Target_Object_Name = argv[1]; Target_Object_Name = argv[1];
} } else {
else Target_Object_Type = strtol(argv[1], NULL, 0);
{ Target_Object_Instance = strtol(argv[2], NULL, 0);
Target_Object_Type = strtol(argv[1],NULL,0); if (Target_Object_Instance > BACNET_MAX_INSTANCE) {
Target_Object_Instance = strtol(argv[2],NULL,0); fprintf(stderr,
if (Target_Object_Instance > BACNET_MAX_INSTANCE) "object-instance=%u - it must be less than %u\r\n",
{ Target_Object_Instance, BACNET_MAX_INSTANCE + 1);
fprintf(stderr,"object-instance=%u - it must be less than %u\r\n",
Target_Object_Instance,BACNET_MAX_INSTANCE+1);
return 1; return 1;
} }
if (Target_Object_Type > MAX_BACNET_OBJECT_TYPE) if (Target_Object_Type > MAX_BACNET_OBJECT_TYPE) {
{ fprintf(stderr, "object-type=%u - it must be less than %u\r\n",
fprintf(stderr,"object-type=%u - it must be less than %u\r\n", Target_Object_Type, MAX_BACNET_OBJECT_TYPE + 1);
Target_Object_Type,MAX_BACNET_OBJECT_TYPE+1);
return 1; return 1;
} }
} }
@@ -190,12 +170,12 @@ int main(int argc, char *argv[])
bip_set_port(0xBAC0); bip_set_port(0xBAC0);
if (!bip_init()) if (!bip_init())
return 1; return 1;
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
datalink_get_broadcast_address(&broadcast_address); datalink_get_broadcast_address(&broadcast_address);
print_address("Broadcast",&broadcast_address); print_address("Broadcast", &broadcast_address);
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
print_address("Address",&my_address); print_address("Address", &my_address);
#endif #endif
/* configure the timeout values */ /* configure the timeout values */
last_seconds = time(NULL); last_seconds = time(NULL);
timeout_seconds = Device_APDU_Timeout() / 1000; timeout_seconds = Device_APDU_Timeout() / 1000;
@@ -206,36 +186,23 @@ int main(int argc, char *argv[])
Send_WhoHas_Name(-1, -1, Target_Object_Name); Send_WhoHas_Name(-1, -1, Target_Object_Name);
else else
Send_WhoHas_Object(-1, -1, Send_WhoHas_Object(-1, -1,
Target_Object_Type, Target_Object_Type, Target_Object_Instance);
Target_Object_Instance);
/* loop forever */ /* loop forever */
for (;;) for (;;) {
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
current_seconds = time(NULL); current_seconds = time(NULL);
/* returns 0 bytes on timeout */ /* returns 0 bytes on timeout */
pdu_len = bip_receive( pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
/* process */ /* process */
if (pdu_len) if (pdu_len) {
{ npdu_handler(&src, &Rx_Buf[0], pdu_len);
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
} }
if (Error_Detected) if (Error_Detected)
break; break;
if (I_Am_Request) if (I_Am_Request) {
{
I_Am_Request = false; I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]); iam_send(&Handler_Transmit_Buffer[0]);
} } else {
else
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds); elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds) if (elapsed_seconds > timeout_seconds)
+96 -167
View File
@@ -49,7 +49,7 @@
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive // buffer used for receive
static uint8_t Rx_Buf[MAX_MPDU] = {0}; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
static uint32_t Target_File_Object_Instance = 4194303; static uint32_t Target_File_Object_Instance = 4194303;
@@ -60,56 +60,46 @@ static bool End_Of_File_Detected = false;
static bool Error_Detected = false; static bool Error_Detected = false;
static uint8_t Current_Invoke_ID = 0; static uint8_t Current_Invoke_ID = 0;
static void Atomic_Read_File_Error_Handler( static void Atomic_Read_File_Error_Handler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src,
uint8_t invoke_id, uint8_t invoke_id,
BACNET_ERROR_CLASS error_class, BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code)
BACNET_ERROR_CODE error_code)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("\r\nBACnet Error!\r\n"); printf("\r\nBACnet Error!\r\n");
printf("Error Class: %s\r\n", printf("Error Class: %s\r\n", bactext_error_class_name(error_class));
bactext_error_class_name(error_class)); printf("Error Code: %s\r\n", bactext_error_code_name(error_code));
printf("Error Code: %s\r\n",
bactext_error_code_name(error_code));
Error_Detected = true; Error_Detected = true;
} }
void MyAbortHandler( void MyAbortHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t abort_reason)
uint8_t invoke_id,
uint8_t abort_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("\r\nBACnet Abort!\r\n"); printf("\r\nBACnet Abort!\r\n");
printf("Abort Reason: %s\r\n", printf("Abort Reason: %s\r\n",
bactext_abort_reason_name(abort_reason)); bactext_abort_reason_name(abort_reason));
Error_Detected = true; Error_Detected = true;
} }
void MyRejectHandler( void MyRejectHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t reject_reason)
uint8_t invoke_id,
uint8_t reject_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("\r\nBACnet Reject!\r\n"); printf("\r\nBACnet Reject!\r\n");
printf("Reject Reason: %s\r\n", printf("Reject Reason: %s\r\n",
bactext_reject_reason_name(reject_reason)); bactext_reject_reason_name(reject_reason));
Error_Detected = true; Error_Detected = true;
} }
static uint8_t Send_Atomic_Write_File_Stream( static uint8_t Send_Atomic_Write_File_Stream(uint32_t device_id,
uint32_t device_id,
uint32_t file_instance, uint32_t file_instance,
int fileStartPosition, int fileStartPosition, BACNET_OCTET_STRING * fileData)
BACNET_OCTET_STRING *fileData)
{ {
BACNET_ADDRESS dest; BACNET_ADDRESS dest;
BACNET_ADDRESS my_address; BACNET_ADDRESS my_address;
@@ -125,14 +115,9 @@ static uint8_t Send_Atomic_Write_File_Stream(
/* is there a tsm available? */ /* is there a tsm available? */
if (status) if (status)
status = tsm_transaction_available(); status = tsm_transaction_available();
if (status) if (status) {
{
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID(); invoke_id = tsm_next_free_invokeID();
@@ -142,40 +127,31 @@ static uint8_t Send_Atomic_Write_File_Stream(
data.access = FILE_STREAM_ACCESS; data.access = FILE_STREAM_ACCESS;
data.type.stream.fileStartPosition = fileStartPosition; data.type.stream.fileStartPosition = fileStartPosition;
status = octetstring_copy(&data.fileData, fileData); status = octetstring_copy(&data.fileData, fileData);
if (status) if (status) {
{ pdu_len += awf_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
pdu_len += awf_encode_apdu( invoke_id, &data);
&Handler_Transmit_Buffer[pdu_len],
invoke_id,
&data);
/* will the APDU fit the target device? /* will the APDU fit the target device?
note: if there is a bottleneck router in between note: if there is a bottleneck router in between
us and the destination, we won't know unless us and the destination, we won't know unless
we have a way to check for that and update the we have a way to check for that and update the
max_apdu in the address binding table. */ max_apdu in the address binding table. */
if ((unsigned)pdu_len <= max_apdu) if ((unsigned) pdu_len <= max_apdu) {
{ tsm_set_confirmed_unsegmented_transaction(invoke_id,
tsm_set_confirmed_unsegmented_transaction( &dest, &Handler_Transmit_Buffer[0], pdu_len);
invoke_id, bytes_sent = datalink_send_pdu(&dest, // destination address
&dest, &Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
&Handler_Transmit_Buffer[0],
pdu_len);
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send AtomicWriteFile Request (%s)!\n", fprintf(stderr,
"Failed to Send AtomicWriteFile Request (%s)!\n",
strerror(errno)); strerror(errno));
} } else
else fprintf(stderr, "Failed to Send AtomicWriteFile Request "
fprintf(stderr,"Failed to Send AtomicWriteFile Request "
"(payload [%d] exceeds destination maximum APDU [%u])!\n", "(payload [%d] exceeds destination maximum APDU [%u])!\n",
pdu_len,max_apdu); pdu_len, max_apdu);
} } else
else fprintf(stderr, "Failed to Send AtomicWriteFile Request "
fprintf(stderr,"Failed to Send AtomicWriteFile Request " "(payload [%d] exceeds octet string capacity)!\n",
"(payload [%d] exceeds octet string capacity)!\n",pdu_len); pdu_len);
} }
return invoke_id; return invoke_id;
@@ -191,31 +167,22 @@ static void Send_WhoIs(uint32_t device_id)
datalink_get_broadcast_address(&dest); datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */ /* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu( pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, // true for confirmed messages
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL); MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */ /* encode the APDU portion of the packet */
pdu_len += whois_encode_apdu( pdu_len += whois_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&Handler_Transmit_Buffer[pdu_len], device_id, device_id);
device_id,
device_id);
bytes_sent = datalink_send_pdu( bytes_sent = datalink_send_pdu(&dest, /* destination address */
&dest, /* destination address */ &Handler_Transmit_Buffer[0], pdu_len); /* number of bytes of data */
&Handler_Transmit_Buffer[0],
pdu_len); /* number of bytes of data */
if (bytes_sent <= 0) if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send Who-Is Request (%s)!\n", strerror(errno)); fprintf(stderr, "Failed to Send Who-Is Request (%s)!\n",
strerror(errno));
} }
static void LocalIAmHandler( static void LocalIAmHandler(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src)
uint16_t service_len,
BACNET_ADDRESS *src)
{ {
int len = 0; int len = 0;
uint32_t device_id = 0; uint32_t device_id = 0;
@@ -223,22 +190,14 @@ static void LocalIAmHandler(
int segmentation = 0; int segmentation = 0;
uint16_t vendor_id = 0; uint16_t vendor_id = 0;
(void)src; (void) src;
(void)service_len; (void) service_len;
len = iam_decode_service_request( len = iam_decode_service_request(service_request,
service_request, &device_id, &max_apdu, &segmentation, &vendor_id);
&device_id, if (len != -1) {
&max_apdu, address_add(device_id, max_apdu, src);
&segmentation, } else
&vendor_id); fprintf(stderr, "!\n");
if (len != -1)
{
address_add(device_id,
max_apdu,
src);
}
else
fprintf(stderr,"!\n");
return; return;
} }
@@ -247,34 +206,28 @@ static void Init_Service_Handlers(void)
{ {
/* we need to handle who-is /* we need to handle who-is
to support dynamic device binding to us */ to support dynamic device binding to us */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
/* handle i-am to support binding to other devices */ /* handle i-am to support binding to other devices */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler); LocalIAmHandler);
/* set the handler for all the services we don't implement /* set the handler for all the services we don't implement
It is required to send the proper reject message... */ It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler( apdu_set_unrecognized_service_handler_handler
handler_unrecognized_service); (handler_unrecognized_service);
/* we must implement read property - it's required! */ /* we must implement read property - it's required! */
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
/* handle any errors coming back */ /* handle any errors coming back */
apdu_set_error_handler( apdu_set_error_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
Atomic_Read_File_Error_Handler); Atomic_Read_File_Error_Handler);
apdu_set_abort_handler( apdu_set_abort_handler(MyAbortHandler);
MyAbortHandler); apdu_set_reject_handler(MyRejectHandler);
apdu_set_reject_handler(
MyRejectHandler);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = { 0 }; // address where message came from
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; // milliseconds
unsigned max_apdu = 0; unsigned max_apdu = 0;
@@ -291,27 +244,24 @@ int main(int argc, char *argv[])
static BACNET_OCTET_STRING fileData; static BACNET_OCTET_STRING fileData;
size_t len = 0; size_t len = 0;
if (argc < 4) if (argc < 4) {
{
/* FIXME: what about access method - record or stream? */ /* FIXME: what about access method - record or stream? */
printf("%s device-instance file-instance local-name\r\n", printf("%s device-instance file-instance local-name\r\n",
filename_remove_path(argv[0])); filename_remove_path(argv[0]));
return 0; return 0;
} }
/* decode the command line parameters */ /* decode the command line parameters */
Target_Device_Object_Instance = strtol(argv[1],NULL,0); Target_Device_Object_Instance = strtol(argv[1], NULL, 0);
Target_File_Object_Instance = strtol(argv[2],NULL,0); Target_File_Object_Instance = strtol(argv[2], NULL, 0);
Local_File_Name = argv[3]; Local_File_Name = argv[3];
if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE) if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE) {
{ fprintf(stderr, "device-instance=%u - it must be less than %u\r\n",
fprintf(stderr,"device-instance=%u - it must be less than %u\r\n", Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
Target_Device_Object_Instance,BACNET_MAX_INSTANCE);
return 1; return 1;
} }
if (Target_File_Object_Instance >= BACNET_MAX_INSTANCE) if (Target_File_Object_Instance >= BACNET_MAX_INSTANCE) {
{ fprintf(stderr, "file-instance=%u - it must be less than %u\r\n",
fprintf(stderr,"file-instance=%u - it must be less than %u\r\n", Target_File_Object_Instance, BACNET_MAX_INSTANCE + 1);
Target_File_Object_Instance,BACNET_MAX_INSTANCE+1);
return 1; return 1;
} }
/* setup my info */ /* setup my info */
@@ -330,48 +280,33 @@ int main(int argc, char *argv[])
/* try to bind with the device */ /* try to bind with the device */
Send_WhoIs(Target_Device_Object_Instance); Send_WhoIs(Target_Device_Object_Instance);
/* loop forever */ /* loop forever */
for (;;) for (;;) {
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
current_seconds = time(NULL); current_seconds = time(NULL);
/* returns 0 bytes on timeout */ /* returns 0 bytes on timeout */
pdu_len = bip_receive( pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
/* process */ /* process */
if (pdu_len) if (pdu_len) {
{ npdu_handler(&src, &Rx_Buf[0], pdu_len);
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
} }
/* at least one second has passed */ /* at least one second has passed */
if (current_seconds != last_seconds) if (current_seconds != last_seconds)
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); tsm_timer_milliseconds(((current_seconds -
if (End_Of_File_Detected || Error_Detected) last_seconds) * 1000));
{ if (End_Of_File_Detected || Error_Detected) {
printf("\r\n"); printf("\r\n");
break; break;
} }
if (I_Am_Request) if (I_Am_Request) {
{
I_Am_Request = false; I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]); iam_send(&Handler_Transmit_Buffer[0]);
} } else {
else
{
/* wait until the device is bound, or timeout and quit */ /* wait until the device is bound, or timeout and quit */
found = address_bind_request( found = address_bind_request(Target_Device_Object_Instance,
Target_Device_Object_Instance, &max_apdu, &Target_Address);
&max_apdu, if (found) {
&Target_Address);
if (found)
{
/* calculate the smaller of our APDU size or theirs /* calculate the smaller of our APDU size or theirs
and remove the overhead of the APDU (varies depending on size). and remove the overhead of the APDU (varies depending on size).
note: we could fail if there is a bottle neck (router) note: we could fail if there is a bottle neck (router)
@@ -391,38 +326,32 @@ int main(int argc, char *argv[])
requestedOctetCount = my_max_apdu / 2; requestedOctetCount = my_max_apdu / 2;
/* has the previous invoke id expired or returned? /* has the previous invoke id expired or returned?
note: invoke ID = 0 is invalid, so it will be idle */ note: invoke ID = 0 is invalid, so it will be idle */
if ((invoke_id == 0) || tsm_invoke_id_free(invoke_id)) if ((invoke_id == 0) || tsm_invoke_id_free(invoke_id)) {
{
if (invoke_id != 0) if (invoke_id != 0)
fileStartPosition += requestedOctetCount; fileStartPosition += requestedOctetCount;
/* we'll read the file in chunks /* we'll read the file in chunks
less than max_apdu to keep unsegmented */ less than max_apdu to keep unsegmented */
pFile = fopen(Local_File_Name,"rb"); pFile = fopen(Local_File_Name, "rb");
if (pFile) if (pFile) {
{ (void) fseek(pFile, fileStartPosition, SEEK_SET);
(void)fseek(pFile,
fileStartPosition,
SEEK_SET);
len = fread(octetstring_value(&fileData), 1, len = fread(octetstring_value(&fileData), 1,
requestedOctetCount, pFile); requestedOctetCount, pFile);
if (len < requestedOctetCount) if (len < requestedOctetCount)
End_Of_File_Detected = true; End_Of_File_Detected = true;
octetstring_truncate(&fileData,len); octetstring_truncate(&fileData, len);
fclose(pFile); fclose(pFile);
} } else
else
End_Of_File_Detected = true; End_Of_File_Detected = true;
printf("\rSending %d bytes",(fileStartPosition+len)); printf("\rSending %d bytes",
invoke_id = Send_Atomic_Write_File_Stream( (fileStartPosition + len));
Target_Device_Object_Instance, invoke_id =
Target_File_Object_Instance, Send_Atomic_Write_File_Stream
fileStartPosition, (Target_Device_Object_Instance,
Target_File_Object_Instance, fileStartPosition,
&fileData); &fileData);
Current_Invoke_ID = invoke_id; Current_Invoke_ID = invoke_id;
} }
} } else {
else
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds); elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds) if (elapsed_seconds > timeout_seconds)
+80 -122
View File
@@ -50,7 +50,7 @@
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive // buffer used for receive
static uint8_t Rx_Buf[MAX_MPDU] = {0}; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* global variables used in this file */ /* global variables used in this file */
static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE; static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE;
@@ -59,65 +59,58 @@ static BACNET_OBJECT_TYPE Target_Object_Type = OBJECT_ANALOG_INPUT;
static BACNET_PROPERTY_ID Target_Object_Property = PROP_ACKED_TRANSITIONS; static BACNET_PROPERTY_ID Target_Object_Property = PROP_ACKED_TRANSITIONS;
/* array index value or BACNET_ARRAY_ALL */ /* array index value or BACNET_ARRAY_ALL */
static int32_t Target_Object_Property_Index = BACNET_ARRAY_ALL; static int32_t Target_Object_Property_Index = BACNET_ARRAY_ALL;
static BACNET_APPLICATION_TAG Target_Object_Property_Tag = BACNET_APPLICATION_TAG_NULL; static BACNET_APPLICATION_TAG Target_Object_Property_Tag =
static BACNET_APPLICATION_DATA_VALUE Target_Object_Property_Value = {0}; BACNET_APPLICATION_TAG_NULL;
static BACNET_APPLICATION_DATA_VALUE Target_Object_Property_Value = { 0 };
/* 0 if not set, 1..16 if set */ /* 0 if not set, 1..16 if set */
static uint8_t Target_Object_Property_Priority = 0; static uint8_t Target_Object_Property_Priority = 0;
static BACNET_ADDRESS Target_Address; static BACNET_ADDRESS Target_Address;
static bool Error_Detected = false; static bool Error_Detected = false;
static void MyErrorHandler( static void MyErrorHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src,
uint8_t invoke_id, uint8_t invoke_id,
BACNET_ERROR_CLASS error_class, BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code)
BACNET_ERROR_CODE error_code)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("\r\nBACnet Error!\r\n"); printf("\r\nBACnet Error!\r\n");
printf("Error Class: %s\r\n", printf("Error Class: %s\r\n", bactext_error_class_name(error_class));
bactext_error_class_name(error_class)); printf("Error Code: %s\r\n", bactext_error_code_name(error_code));
printf("Error Code: %s\r\n",
bactext_error_code_name(error_code));
Error_Detected = true; Error_Detected = true;
} }
void MyAbortHandler( void MyAbortHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t abort_reason)
uint8_t invoke_id,
uint8_t abort_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("\r\nBACnet Abort!\r\n"); printf("\r\nBACnet Abort!\r\n");
printf("Abort Reason: %s\r\n", printf("Abort Reason: %s\r\n",
bactext_abort_reason_name(abort_reason)); bactext_abort_reason_name(abort_reason));
Error_Detected = true; Error_Detected = true;
} }
void MyRejectHandler( void MyRejectHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t reject_reason)
uint8_t invoke_id,
uint8_t reject_reason)
{ {
/* FIXME: verify src and invoke id */ /* FIXME: verify src and invoke id */
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("\r\nBACnet Reject!\r\n"); printf("\r\nBACnet Reject!\r\n");
printf("Reject Reason: %s\r\n", printf("Reject Reason: %s\r\n",
bactext_reject_reason_name(reject_reason)); bactext_reject_reason_name(reject_reason));
Error_Detected = true; Error_Detected = true;
} }
void MyWritePropertySimpleAckHandler( void MyWritePropertySimpleAckHandler(BACNET_ADDRESS * src,
BACNET_ADDRESS *src,
uint8_t invoke_id) uint8_t invoke_id)
{ {
(void)src; (void) src;
(void)invoke_id; (void) invoke_id;
printf("\r\nWriteProperty Acknowledged!\r\n"); printf("\r\nWriteProperty Acknowledged!\r\n");
} }
@@ -125,38 +118,31 @@ static void Init_Service_Handlers(void)
{ {
/* we need to handle who-is /* we need to handle who-is
to support dynamic device binding to us */ to support dynamic device binding to us */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
/* handle i-am to support binding to other devices */ /* handle i-am to support binding to other devices */
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
SERVICE_UNCONFIRMED_I_AM,
handler_i_am_bind); handler_i_am_bind);
/* set the handler for all the services we don't implement /* set the handler for all the services we don't implement
It is required to send the proper reject message... */ It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler( apdu_set_unrecognized_service_handler_handler
handler_unrecognized_service); (handler_unrecognized_service);
/* we must implement read property - it's required! */ /* we must implement read property - it's required! */
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
/* handle the ack coming back */ /* handle the ack coming back */
apdu_set_confirmed_simple_ack_handler( apdu_set_confirmed_simple_ack_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
SERVICE_CONFIRMED_WRITE_PROPERTY,
MyWritePropertySimpleAckHandler); MyWritePropertySimpleAckHandler);
/* handle any errors coming back */ /* handle any errors coming back */
apdu_set_error_handler( apdu_set_error_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
SERVICE_CONFIRMED_WRITE_PROPERTY,
MyErrorHandler); MyErrorHandler);
apdu_set_abort_handler( apdu_set_abort_handler(MyAbortHandler);
MyAbortHandler); apdu_set_reject_handler(MyRejectHandler);
apdu_set_reject_handler(
MyRejectHandler);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = { 0 }; // address where message came from
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; // milliseconds
unsigned max_apdu = 0; unsigned max_apdu = 0;
@@ -169,8 +155,7 @@ int main(int argc, char *argv[])
char *value_string = NULL; char *value_string = NULL;
bool status = false; bool status = false;
if (argc < 7) if (argc < 7) {
{
/* note: priority 16 and 0 should produce the same end results... */ /* note: priority 16 and 0 should produce the same end results... */
printf("Usage: %s device-instance object-type object-instance " printf("Usage: %s device-instance object-type object-instance "
"property tag value [priority] [index]\r\n" "property tag value [priority] [index]\r\n"
@@ -240,62 +225,53 @@ int main(int argc, char *argv[])
"You could also send a relinquish command:\r\n" "You could also send a relinquish command:\r\n"
"%s 123 1 0 85 0 0\r\n", "%s 123 1 0 85 0 0\r\n",
filename_remove_path(argv[0]), filename_remove_path(argv[0]),
filename_remove_path(argv[0]), filename_remove_path(argv[0]), filename_remove_path(argv[0]));
filename_remove_path(argv[0]));
return 0; return 0;
} }
/* decode the command line parameters */ /* decode the command line parameters */
Target_Device_Object_Instance = strtol(argv[1],NULL,0); Target_Device_Object_Instance = strtol(argv[1], NULL, 0);
Target_Object_Type = strtol(argv[2],NULL,0); Target_Object_Type = strtol(argv[2], NULL, 0);
Target_Object_Instance = strtol(argv[3],NULL,0); Target_Object_Instance = strtol(argv[3], NULL, 0);
Target_Object_Property = strtol(argv[4],NULL,0); Target_Object_Property = strtol(argv[4], NULL, 0);
Target_Object_Property_Tag = strtol(argv[5],NULL,0); Target_Object_Property_Tag = strtol(argv[5], NULL, 0);
value_string = argv[6]; value_string = argv[6];
/* optional priority */ /* optional priority */
if (argc > 7) if (argc > 7)
Target_Object_Property_Priority = strtol(argv[7],NULL,0); Target_Object_Property_Priority = strtol(argv[7], NULL, 0);
/* optional index */ /* optional index */
if (argc > 8) if (argc > 8)
Target_Object_Property_Index = strtol(argv[8],NULL,0); Target_Object_Property_Index = strtol(argv[8], NULL, 0);
if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE) if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE) {
{ fprintf(stderr, "device-instance=%u - it must be less than %u\r\n",
fprintf(stderr,"device-instance=%u - it must be less than %u\r\n", Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
Target_Device_Object_Instance,BACNET_MAX_INSTANCE);
return 1; return 1;
} }
if (Target_Object_Type > MAX_BACNET_OBJECT_TYPE) if (Target_Object_Type > MAX_BACNET_OBJECT_TYPE) {
{ fprintf(stderr, "object-type=%u - it must be less than %u\r\n",
fprintf(stderr,"object-type=%u - it must be less than %u\r\n", Target_Object_Type, MAX_BACNET_OBJECT_TYPE + 1);
Target_Object_Type,MAX_BACNET_OBJECT_TYPE+1);
return 1; return 1;
} }
if (Target_Object_Instance > BACNET_MAX_INSTANCE) if (Target_Object_Instance > BACNET_MAX_INSTANCE) {
{ fprintf(stderr, "object-instance=%u - it must be less than %u\r\n",
fprintf(stderr,"object-instance=%u - it must be less than %u\r\n", Target_Object_Instance, BACNET_MAX_INSTANCE + 1);
Target_Object_Instance,BACNET_MAX_INSTANCE+1);
return 1; return 1;
} }
if (Target_Object_Property > MAX_BACNET_PROPERTY_ID) if (Target_Object_Property > MAX_BACNET_PROPERTY_ID) {
{ fprintf(stderr, "object-type=%u - it must be less than %u\r\n",
fprintf(stderr,"object-type=%u - it must be less than %u\r\n", Target_Object_Property, MAX_BACNET_PROPERTY_ID + 1);
Target_Object_Property,MAX_BACNET_PROPERTY_ID+1);
return 1; return 1;
} }
if (Target_Object_Property_Tag >= MAX_BACNET_APPLICATION_TAG) if (Target_Object_Property_Tag >= MAX_BACNET_APPLICATION_TAG) {
{ fprintf(stderr, "tag=%u - it must be less than %u\r\n",
fprintf(stderr,"tag=%u - it must be less than %u\r\n", Target_Object_Property_Tag, MAX_BACNET_APPLICATION_TAG);
Target_Object_Property_Tag,MAX_BACNET_APPLICATION_TAG);
return 1; return 1;
} }
status = bacapp_parse_application_data( status = bacapp_parse_application_data(Target_Object_Property_Tag,
Target_Object_Property_Tag, value_string, &Target_Object_Property_Value);
value_string, if (!status) {
&Target_Object_Property_Value);
if (!status)
{
/* FIXME: show the expected entry format for the tag */ /* FIXME: show the expected entry format for the tag */
fprintf(stderr,"unable to parse the tag value\r\n"); fprintf(stderr, "unable to parse the tag value\r\n");
return 1; return 1;
} }
@@ -313,63 +289,45 @@ int main(int argc, char *argv[])
timeout_seconds = (Device_APDU_Timeout() / 1000) * timeout_seconds = (Device_APDU_Timeout() / 1000) *
Device_Number_Of_APDU_Retries(); Device_Number_Of_APDU_Retries();
/* try to bind with the device */ /* try to bind with the device */
Send_WhoIs(Target_Device_Object_Instance,Target_Device_Object_Instance); Send_WhoIs(Target_Device_Object_Instance,
Target_Device_Object_Instance);
/* loop forever */ /* loop forever */
for (;;) for (;;) {
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
current_seconds = time(NULL); current_seconds = time(NULL);
/* returns 0 bytes on timeout */ /* returns 0 bytes on timeout */
pdu_len = bip_receive( pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
/* process */ /* process */
if (pdu_len) if (pdu_len) {
{ npdu_handler(&src, &Rx_Buf[0], pdu_len);
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
} }
/* at least one second has passed */ /* at least one second has passed */
if (current_seconds != last_seconds) if (current_seconds != last_seconds)
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); tsm_timer_milliseconds(((current_seconds -
last_seconds) * 1000));
if (Error_Detected) if (Error_Detected)
break; break;
if (I_Am_Request) if (I_Am_Request) {
{
I_Am_Request = false; I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]); iam_send(&Handler_Transmit_Buffer[0]);
} } else {
else
{
/* wait until the device is bound, or timeout and quit */ /* wait until the device is bound, or timeout and quit */
found = address_bind_request( found = address_bind_request(Target_Device_Object_Instance,
Target_Device_Object_Instance, &max_apdu, &Target_Address);
&max_apdu, if (found) {
&Target_Address); if (invoke_id == 0) {
if (found) invoke_id =
{ Send_Write_Property_Request
if (invoke_id == 0) (Target_Device_Object_Instance, Target_Object_Type,
{ Target_Object_Instance, Target_Object_Property,
invoke_id = Send_Write_Property_Request(
Target_Device_Object_Instance,
Target_Object_Type,
Target_Object_Instance,
Target_Object_Property,
&Target_Object_Property_Value, &Target_Object_Property_Value,
Target_Object_Property_Priority, Target_Object_Property_Priority,
Target_Object_Property_Index); Target_Object_Property_Index);
} } else if (tsm_invoke_id_free(invoke_id))
else if (tsm_invoke_id_free(invoke_id))
break; break;
} } else {
else
{
/* increment timer - exit if timed out */ /* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds); elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds) if (elapsed_seconds > timeout_seconds)
+3 -1
View File
@@ -6,7 +6,9 @@ Don't use C++-style comments (comments beginning with "//" and running
to the end of the line) for modules that are written in C. The module to the end of the line) for modules that are written in C. The module
may run through C rather than C++ compilers, and not all C compilers may run through C rather than C++ compilers, and not all C compilers
support C++-style comments (GCC does, but IBM's C compiler for AIX, for support C++-style comments (GCC does, but IBM's C compiler for AIX, for
example, doesn't do so by default). example, doesn't do so by default). Note: there is a program
called usr/bin/ccmtcnvt in the liwc package that converts the C++
comments to C comments.
Don't initialize variables in their declaration with non-constant Don't initialize variables in their declaration with non-constant
values. Not all compilers support this. E.g. don't use values. Not all compilers support this. E.g. don't use
+1 -2
View File
@@ -576,8 +576,7 @@ void MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t *mstp_port)
// that is not known to this node. // that is not known to this node.
// FIXME: change this if you add a proprietary type // FIXME: change this if you add a proprietary type
else if /*( */ (mstp_port->FrameType >= else if /*( */ (mstp_port->FrameType >=
FRAME_TYPE_PROPRIETARY_MIN) FRAME_TYPE_PROPRIETARY_MIN) { /*&& */
{ /*&& */
/*(FrameType <= FRAME_TYPE_PROPRIETARY_MAX)) */ /*(FrameType <= FRAME_TYPE_PROPRIETARY_MAX)) */
/* unnecessary if FrameType is uint8_t with max of 255 */ /* unnecessary if FrameType is uint8_t with max of 255 */
// an unexpected or unwanted frame was received. // an unexpected or unwanted frame was received.
+48 -72
View File
@@ -108,29 +108,25 @@ static int arcnet_bind(char *interface_name)
"Try running with root priveleges.\n"); "Try running with root priveleges.\n");
return sock_fd; return sock_fd;
} }
fprintf(stderr,"arcnet: opening \"%s\"\n",interface_name); fprintf(stderr, "arcnet: opening \"%s\"\n", interface_name);
// note: on some systems you may have to add or enable in // note: on some systems you may have to add or enable in
// modules.conf (or in modutils/alias on Debian with update-modules) // modules.conf (or in modutils/alias on Debian with update-modules)
// alias net-pf-17 af_packet // alias net-pf-17 af_packet
// Then follow it by: # modprobe af_packet // Then follow it by: # modprobe af_packet
if ((sock_fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0) if ((sock_fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0) {
{
/* Error occured */ /* Error occured */
fprintf(stderr, fprintf(stderr,
"arcnet: Error opening socket: %s\n", "arcnet: Error opening socket: %s\n", strerror(errno));
strerror(errno));
fprintf(stderr, fprintf(stderr,
"You might need to add the following to modules.conf\n" "You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n" "(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n" "alias net-pf-17 af_packet\n"
"Also, add af_packet to /etc/modules.\n" "Also, add af_packet to /etc/modules.\n"
"Then follow it by:\n" "Then follow it by:\n" "# modprobe af_packet\n");
"# modprobe af_packet\n");
exit(-1); exit(-1);
} }
if (ARCNET_Sock_FD >= 0) if (ARCNET_Sock_FD >= 0) {
{
/* Bind the socket to an interface name so we only get packets from it */ /* Bind the socket to an interface name so we only get packets from it */
ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET; ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET;
//ARCNET_Socket_Address.sa_family = PF_INET; //ARCNET_Socket_Address.sa_family = PF_INET;
@@ -139,22 +135,20 @@ static int arcnet_bind(char *interface_name)
sizeof(ARCNET_Socket_Address.sa_data)); sizeof(ARCNET_Socket_Address.sa_data));
/* Strcpy the interface name into the address */ /* Strcpy the interface name into the address */
strncpy(ARCNET_Socket_Address.sa_data, interface_name, strncpy(ARCNET_Socket_Address.sa_data, interface_name,
sizeof(ARCNET_Socket_Address.sa_data)-1); sizeof(ARCNET_Socket_Address.sa_data) - 1);
fprintf(stderr,"arcnet: binding \"%s\"\n",ARCNET_Socket_Address.sa_data); fprintf(stderr, "arcnet: binding \"%s\"\n",
ARCNET_Socket_Address.sa_data);
if (bind(sock_fd, &ARCNET_Socket_Address, if (bind(sock_fd, &ARCNET_Socket_Address,
sizeof(ARCNET_Socket_Address)) != 0) sizeof(ARCNET_Socket_Address)) != 0) {
{
/* Bind problem, close socket and return */ /* Bind problem, close socket and return */
fprintf(stderr, fprintf(stderr,
"arcnet: Unable to bind socket : %s\n", "arcnet: Unable to bind socket : %s\n", strerror(errno));
strerror(errno));
fprintf(stderr, fprintf(stderr,
"You might need to add the following to modules.conf\n" "You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n" "(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n" "alias net-pf-17 af_packet\n"
"Also, add af_packet to /etc/modules.\n" "Also, add af_packet to /etc/modules.\n"
"Then follow it by:\n" "Then follow it by:\n" "# modprobe af_packet\n");
"# modprobe af_packet\n");
/* Close the socket */ /* Close the socket */
close(sock_fd); close(sock_fd);
exit(-1); exit(-1);
@@ -172,10 +166,9 @@ static int arcnet_bind(char *interface_name)
sizeof(ARCNET_Socket_Address.sa_data)); sizeof(ARCNET_Socket_Address.sa_data));
/* Strcpy the interface name into the address */ /* Strcpy the interface name into the address */
strncpy(ARCNET_Socket_Address.sa_data, interface_name, strncpy(ARCNET_Socket_Address.sa_data, interface_name,
sizeof(ARCNET_Socket_Address.sa_data)-1); sizeof(ARCNET_Socket_Address.sa_data) - 1);
fprintf(stderr,"arcnet: MAC=%02Xh iface=\"%s\"\n", fprintf(stderr, "arcnet: MAC=%02Xh iface=\"%s\"\n",
ARCNET_MAC_Address, ARCNET_MAC_Address, ARCNET_Socket_Address.sa_data);
ARCNET_Socket_Address.sa_data);
atexit(arcnet_cleanup); atexit(arcnet_cleanup);
@@ -191,40 +184,35 @@ bool arcnet_init(char *interface_name)
/* function to send a packet out the socket */ /* function to send a packet out the socket */
/* returns number of bytes sent on success, negative on failure */ /* returns number of bytes sent on success, negative on failure */
int arcnet_send( int arcnet_send(BACNET_ADDRESS * dest, // destination address
BACNET_ADDRESS *dest, // destination address BACNET_ADDRESS * src, // source address
BACNET_ADDRESS *src, // source address uint8_t * pdu, // any data to be sent - may be null
uint8_t *pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data unsigned pdu_len) // number of bytes of data
{ {
int bytes = 0; int bytes = 0;
uint8_t mtu[512] = { 0 }; uint8_t mtu[512] = { 0 };
int mtu_len = 0; int mtu_len = 0;
struct archdr *pkt = (struct archdr *)mtu; struct archdr *pkt = (struct archdr *) mtu;
// don't waste time if the socket is not valid // don't waste time if the socket is not valid
if (ARCNET_Sock_FD < 0) if (ARCNET_Sock_FD < 0) {
{
fprintf(stderr, "arcnet: socket is invalid!\n"); fprintf(stderr, "arcnet: socket is invalid!\n");
return -1; return -1;
} }
/* load destination MAC address */ /* load destination MAC address */
if (dest->mac_len == 1) if (dest->mac_len == 1)
pkt->hard.dest = dest->mac[0]; pkt->hard.dest = dest->mac[0];
else else {
{
fprintf(stderr, "arcnet: invalid destination MAC address!\n"); fprintf(stderr, "arcnet: invalid destination MAC address!\n");
return -2; return -2;
} }
if (src->mac_len == 1) if (src->mac_len == 1)
pkt->hard.source = src->mac[0]; pkt->hard.source = src->mac[0];
else else {
{
fprintf(stderr, "arcnet: invalid source MAC address!\n"); fprintf(stderr, "arcnet: invalid source MAC address!\n");
return -3; return -3;
} }
if ((ARC_HDR_SIZE + pdu_len) > 512) if ((ARC_HDR_SIZE + pdu_len) > 512) {
{
fprintf(stderr, "arcnet: PDU is too big to send!\n"); fprintf(stderr, "arcnet: PDU is too big to send!\n");
return -4; return -4;
} }
@@ -235,7 +223,7 @@ int arcnet_send(
pkt->soft.raw[3] = 0x03; /* Control byte in header */ pkt->soft.raw[3] = 0x03; /* Control byte in header */
memcpy(&pkt->soft.raw[4], pdu, pdu_len); memcpy(&pkt->soft.raw[4], pdu, pdu_len);
/* packet length */ /* packet length */
mtu_len = ARC_HDR_SIZE + 4 /*SC,DSAP,SSAP,LLC*/ + pdu_len; mtu_len = ARC_HDR_SIZE + 4 /*SC,DSAP,SSAP,LLC */ + pdu_len;
/* Send the packet */ /* Send the packet */
bytes = bytes =
sendto(ARCNET_Sock_FD, &mtu, mtu_len, 0, sendto(ARCNET_Sock_FD, &mtu, mtu_len, 0,
@@ -243,7 +231,7 @@ int arcnet_send(
sizeof(ARCNET_Socket_Address)); sizeof(ARCNET_Socket_Address));
/* did it get sent? */ /* did it get sent? */
if (bytes < 0) if (bytes < 0)
fprintf(stderr,"arcnet: Error sending packet: %s\n", fprintf(stderr, "arcnet: Error sending packet: %s\n",
strerror(errno)); strerror(errno));
return bytes; return bytes;
@@ -251,12 +239,11 @@ int arcnet_send(
/* function to send a PDU out the socket */ /* function to send a PDU out the socket */
/* returns number of bytes sent on success, negative on failure */ /* returns number of bytes sent on success, negative on failure */
int arcnet_send_pdu( int arcnet_send_pdu(BACNET_ADDRESS * dest, // destination address
BACNET_ADDRESS *dest, // destination address uint8_t * pdu, // any data to be sent - may be null
uint8_t *pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data unsigned pdu_len) // number of bytes of data
{ {
BACNET_ADDRESS src = {0}; // source address BACNET_ADDRESS src = { 0 }; // source address
src.mac[0] = ARCNET_MAC_Address; src.mac[0] = ARCNET_MAC_Address;
src.mac_len = 1; src.mac_len = 1;
@@ -268,19 +255,18 @@ int arcnet_send_pdu(
// receives an framed packet // receives an framed packet
// returns the number of octets in the PDU, or zero on failure // returns the number of octets in the PDU, or zero on failure
uint16_t arcnet_receive( uint16_t arcnet_receive(BACNET_ADDRESS * src, // source address
BACNET_ADDRESS *src, // source address uint8_t * pdu, // PDU data
uint8_t *pdu, // PDU data
uint16_t max_pdu, // amount of space available in the PDU uint16_t max_pdu, // amount of space available in the PDU
unsigned timeout) // milliseconds to wait for a packet unsigned timeout) // milliseconds to wait for a packet
{ {
int received_bytes; int received_bytes;
uint8_t buf[512] = {0}; // data uint8_t buf[512] = { 0 }; // data
uint16_t pdu_len = 0; // return value uint16_t pdu_len = 0; // return value
fd_set read_fds; fd_set read_fds;
int max; int max;
struct timeval select_timeout; struct timeval select_timeout;
struct archdr *pkt = (struct archdr *)buf; struct archdr *pkt = (struct archdr *) buf;
/* Make sure the socket is open */ /* Make sure the socket is open */
if (ARCNET_Sock_FD <= 0) if (ARCNET_Sock_FD <= 0)
@@ -289,14 +275,11 @@ uint16_t arcnet_receive(
/* we could just use a non-blocking socket, but that consumes all /* we could just use a non-blocking socket, but that consumes all
the CPU time. We can use a timeout; it is only supported as the CPU time. We can use a timeout; it is only supported as
a select. */ a select. */
if (timeout >= 1000) if (timeout >= 1000) {
{
select_timeout.tv_sec = timeout / 1000; select_timeout.tv_sec = timeout / 1000;
select_timeout.tv_usec = select_timeout.tv_usec =
1000 * (timeout - select_timeout.tv_sec * 1000); 1000 * (timeout - select_timeout.tv_sec * 1000);
} } else {
else
{
select_timeout.tv_sec = 0; select_timeout.tv_sec = 0;
select_timeout.tv_usec = 1000 * timeout; select_timeout.tv_usec = 1000 * timeout;
} }
@@ -315,7 +298,8 @@ uint16_t arcnet_receive(
// using O_NONBLOCK and no data // using O_NONBLOCK and no data
// was immediately available for reading. // was immediately available for reading.
if (errno != EAGAIN) if (errno != EAGAIN)
fprintf(stderr,"ethernet: Read error in receiving packet: %s\n", fprintf(stderr,
"ethernet: Read error in receiving packet: %s\n",
strerror(errno)); strerror(errno));
return 0; return 0;
} }
@@ -330,7 +314,7 @@ uint16_t arcnet_receive(
*/ */
if (pkt->hard.source == ARCNET_MAC_Address) { if (pkt->hard.source == ARCNET_MAC_Address) {
fprintf(stderr,"arcnet: self sent packet?\n"); fprintf(stderr, "arcnet: self sent packet?\n");
return 0; return 0;
} }
if (pkt->soft.raw[0] != 0xCD) { if (pkt->soft.raw[0] != 0xCD) {
@@ -338,23 +322,20 @@ uint16_t arcnet_receive(
return 0; return 0;
} }
if ((pkt->hard.dest != ARCNET_MAC_Address) && if ((pkt->hard.dest != ARCNET_MAC_Address) &&
(pkt->hard.dest != ARCNET_BROADCAST)) (pkt->hard.dest != ARCNET_BROADCAST)) {
{ fprintf(stderr, "arcnet: This packet is not for us.\n");
fprintf(stderr,"arcnet: This packet is not for us.\n");
return 0; return 0;
} }
if ((pkt->soft.raw[1] != 0x82) || /* DSAP */ if ((pkt->soft.raw[1] != 0x82) || /* DSAP */
(pkt->soft.raw[2] != 0x82) || /* LSAP */ (pkt->soft.raw[2] != 0x82) || /* LSAP */
(pkt->soft.raw[3] != 0x03)) /* LLC Control */ (pkt->soft.raw[3] != 0x03)) { /* LLC Control */
{ fprintf(stderr, "arcnet: BACnet packet has invalid LLC.\n");
fprintf(stderr,"arcnet: BACnet packet has invalid LLC.\n");
return 0; return 0;
} }
/* It must be addressed to us or be a Broadcast */ /* It must be addressed to us or be a Broadcast */
if ((pkt->hard.dest != ARCNET_MAC_Address) && if ((pkt->hard.dest != ARCNET_MAC_Address) &&
(pkt->hard.dest != ARCNET_BROADCAST)) (pkt->hard.dest != ARCNET_BROADCAST)) {
{ fprintf(stderr, "arcnet: This packet is not for us.\n");
fprintf(stderr,"arcnet: This packet is not for us.\n");
return 0; return 0;
} }
/* copy the source address */ /* copy the source address */
@@ -365,7 +346,7 @@ uint16_t arcnet_receive(
pdu_len -= 4 /* SC, DSAP, SSAP, LLC Control */ ; pdu_len -= 4 /* SC, DSAP, SSAP, LLC Control */ ;
// copy the buffer into the PDU // copy the buffer into the PDU
if (pdu_len < max_pdu) if (pdu_len < max_pdu)
memmove(&pdu[0],&pkt->soft.raw[4],pdu_len); memmove(&pdu[0], &pkt->soft.raw[4], pdu_len);
// silently ignore packets that are too large // silently ignore packets that are too large
else else
pdu_len = 0; pdu_len = 0;
@@ -373,7 +354,7 @@ uint16_t arcnet_receive(
return pdu_len; return pdu_len;
} }
void arcnet_get_my_address(BACNET_ADDRESS *my_address) void arcnet_get_my_address(BACNET_ADDRESS * my_address)
{ {
int i = 0; int i = 0;
@@ -381,31 +362,26 @@ void arcnet_get_my_address(BACNET_ADDRESS *my_address)
my_address->mac[0] = ARCNET_MAC_Address; my_address->mac[0] = ARCNET_MAC_Address;
my_address->net = 0; // local only, no routing my_address->net = 0; // local only, no routing
my_address->len = 0; my_address->len = 0;
for (i = 0; i < MAX_MAC_LEN; i++) for (i = 0; i < MAX_MAC_LEN; i++) {
{
my_address->adr[i] = 0; my_address->adr[i] = 0;
} }
return; return;
} }
void arcnet_get_broadcast_address( void arcnet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address
BACNET_ADDRESS *dest) // destination address
{ {
int i = 0; // counter int i = 0; // counter
if (dest) if (dest) {
{
dest->mac[0] = ARCNET_BROADCAST; dest->mac[0] = ARCNET_BROADCAST;
dest->mac_len = 1; dest->mac_len = 1;
dest->net = BACNET_BROADCAST_NETWORK; dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; // len=0 denotes broadcast address dest->len = 0; // len=0 denotes broadcast address
for (i = 0; i < MAX_MAC_LEN; i++) for (i = 0; i < MAX_MAC_LEN; i++) {
{
dest->adr[i] = 0; dest->adr[i] = 0;
} }
} }
return; return;
} }
+16 -20
View File
@@ -39,7 +39,8 @@
#include "net.h" #include "net.h"
static int get_local_ifr_ioctl(char *ifname, struct ifreq *ifr, int request) static int get_local_ifr_ioctl(char *ifname, struct ifreq *ifr,
int request)
{ {
int fd; int fd;
int rv; // return value int rv; // return value
@@ -54,16 +55,14 @@ static int get_local_ifr_ioctl(char *ifname, struct ifreq *ifr, int request)
return rv; return rv;
} }
static int get_local_address_ioctl( static int get_local_address_ioctl(char *ifname,
char *ifname, struct in_addr *addr, int request)
struct in_addr *addr,
int request)
{ {
struct ifreq ifr = { {{0}} }; struct ifreq ifr = { {{0}} };
struct sockaddr_in *tcpip_address; struct sockaddr_in *tcpip_address;
int rv; // return value int rv; // return value
rv = get_local_ifr_ioctl(ifname,&ifr,request); rv = get_local_ifr_ioctl(ifname, &ifr, request);
if (rv >= 0) { if (rv >= 0) {
tcpip_address = (struct sockaddr_in *) &ifr.ifr_addr; tcpip_address = (struct sockaddr_in *) &ifr.ifr_addr;
memcpy(addr, &tcpip_address->sin_addr, sizeof(struct in_addr)); memcpy(addr, &tcpip_address->sin_addr, sizeof(struct in_addr));
@@ -80,15 +79,16 @@ void bip_set_interface(char *ifname)
/* setup local address */ /* setup local address */
get_local_address_ioctl(ifname, &local_address, SIOCGIFADDR); get_local_address_ioctl(ifname, &local_address, SIOCGIFADDR);
bip_set_addr(local_address.s_addr); bip_set_addr(local_address.s_addr);
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
fprintf(stderr,"IP Address: %s\n",inet_ntoa(local_address)); fprintf(stderr, "IP Address: %s\n", inet_ntoa(local_address));
#endif #endif
/* setup local broadcast address */ /* setup local broadcast address */
get_local_address_ioctl(ifname, &broadcast_address, SIOCGIFBRDADDR); get_local_address_ioctl(ifname, &broadcast_address, SIOCGIFBRDADDR);
bip_set_broadcast_addr(broadcast_address.s_addr); bip_set_broadcast_addr(broadcast_address.s_addr);
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
fprintf(stderr,"Broadcast Address: %s\n",inet_ntoa(broadcast_address)); fprintf(stderr, "Broadcast Address: %s\n",
#endif inet_ntoa(broadcast_address));
#endif
} }
bool bip_init(void) bool bip_init(void)
@@ -108,8 +108,7 @@ bool bip_init(void)
sockopt = 1; sockopt = 1;
status = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, status = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR,
&sockopt, sizeof(sockopt)); &sockopt, sizeof(sockopt));
if (status < 0) if (status < 0) {
{
close(sock_fd); close(sock_fd);
bip_set_socket(-1); bip_set_socket(-1);
return status; return status;
@@ -117,22 +116,19 @@ bool bip_init(void)
// allow us to send a broadcast // allow us to send a broadcast
status = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, status = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST,
&sockopt, sizeof(sockopt)); &sockopt, sizeof(sockopt));
if (status < 0) if (status < 0) {
{
close(sock_fd); close(sock_fd);
bip_set_socket(-1); bip_set_socket(-1);
return status; return status;
} }
// bind the socket to the local port number and IP address // bind the socket to the local port number and IP address
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(bip_get_port()); sin.sin_port = htons(bip_get_port());
memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero)); memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
status = bind(sock_fd, status = bind(sock_fd,
(const struct sockaddr*)&sin, sizeof(struct sockaddr)); (const struct sockaddr *) &sin, sizeof(struct sockaddr));
if (status < 0) if (status < 0) {
{
close(sock_fd); close(sock_fd);
bip_set_socket(-1); bip_set_socket(-1);
return false; return false;
+63 -97
View File
@@ -45,6 +45,7 @@ uint8_t Ethernet_Broadcast[MAX_MAC_LEN] =
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
// commonly used empty address for ethernet quick compare // commonly used empty address for ethernet quick compare
uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 }; uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
// my local device data - MAC address // my local device data - MAC address
uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0 }; uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0 };
@@ -89,7 +90,7 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
int sock_fd = -1; // return value int sock_fd = -1; // return value
int uid = 0; int uid = 0;
fprintf(stderr,"ethernet: opening \"%s\"\n",interface_name); fprintf(stderr, "ethernet: opening \"%s\"\n", interface_name);
/* check to see if we are being run as root */ /* check to see if we are being run as root */
uid = getuid(); uid = getuid();
if (uid != 0) { if (uid != 0) {
@@ -104,19 +105,16 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
// Then follow it by: # modprobe af_packet // Then follow it by: # modprobe af_packet
/* Attempt to open the socket for 802.2 ethernet frames */ /* Attempt to open the socket for 802.2 ethernet frames */
if ((sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_802_2))) < 0) if ((sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_802_2))) < 0) {
{
/* Error occured */ /* Error occured */
fprintf(stderr, fprintf(stderr,
"ethernet: Error opening socket: %s\n", "ethernet: Error opening socket: %s\n", strerror(errno));
strerror(errno));
fprintf(stderr, fprintf(stderr,
"You might need to add the following to modules.conf\n" "You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n" "(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n" "alias net-pf-17 af_packet\n"
"Also, add af_packet to /etc/modules.\n" "Also, add af_packet to /etc/modules.\n"
"Then follow it by:\n" "Then follow it by:\n" "# modprobe af_packet\n");
"# modprobe af_packet\n");
exit(-1); exit(-1);
} }
/* Bind the socket to an address */ /* Bind the socket to an address */
@@ -124,11 +122,11 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
/* Clear the memory before copying */ /* Clear the memory before copying */
memset(eth_addr->sa_data, '\0', sizeof(eth_addr->sa_data)); memset(eth_addr->sa_data, '\0', sizeof(eth_addr->sa_data));
/* Strcpy the interface name into the address */ /* 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,
fprintf(stderr,"ethernet: binding \"%s\"\n",eth_addr->sa_data); sizeof(eth_addr->sa_data) - 1);
fprintf(stderr, "ethernet: binding \"%s\"\n", eth_addr->sa_data);
/* Attempt to bind the socket to the interface */ /* Attempt to bind the socket to the interface */
if (bind(sock_fd, eth_addr, sizeof(struct sockaddr)) != 0) if (bind(sock_fd, eth_addr, sizeof(struct sockaddr)) != 0) {
{
/* Bind problem, close socket and return */ /* Bind problem, close socket and return */
fprintf(stderr, fprintf(stderr,
"ethernet: Unable to bind 802.2 socket : %s\n", "ethernet: Unable to bind 802.2 socket : %s\n",
@@ -138,8 +136,7 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
"(or in /etc/modutils/alias on Debian with update-modules):\n" "(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n" "alias net-pf-17 af_packet\n"
"Also, add af_packet to /etc/modules.\n" "Also, add af_packet to /etc/modules.\n"
"Then follow it by:\n" "Then follow it by:\n" "# modprobe af_packet\n");
"# modprobe af_packet\n");
/* Close the socket */ /* Close the socket */
close(sock_fd); close(sock_fd);
exit(-1); exit(-1);
@@ -174,18 +171,16 @@ static int get_local_hwaddr(const char *ifname, unsigned char *mac)
bool ethernet_init(char *interface_name) bool ethernet_init(char *interface_name)
{ {
get_local_hwaddr(interface_name, Ethernet_MAC_Address); get_local_hwaddr(interface_name, Ethernet_MAC_Address);
eth802_sockfd = eth802_sockfd = ethernet_bind(&eth_addr, interface_name);
ethernet_bind(&eth_addr, interface_name);
return ethernet_valid(); return ethernet_valid();
} }
/* function to send a packet out the 802.2 socket */ /* function to send a packet out the 802.2 socket */
/* returns bytes sent success, negative on failure */ /* returns bytes sent success, negative on failure */
int ethernet_send( int ethernet_send(BACNET_ADDRESS * dest, // destination address
BACNET_ADDRESS *dest, // destination address BACNET_ADDRESS * src, // source address
BACNET_ADDRESS *src, // source address uint8_t * pdu, // any data to be sent - may be null
uint8_t *pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data unsigned pdu_len) // number of bytes of data
{ {
int bytes = 0; int bytes = 0;
@@ -194,48 +189,38 @@ int ethernet_send(
int i = 0; int i = 0;
// don't waste time if the socket is not valid // don't waste time if the socket is not valid
if (eth802_sockfd < 0) if (eth802_sockfd < 0) {
{
fprintf(stderr, "ethernet: 802.2 socket is invalid!\n"); fprintf(stderr, "ethernet: 802.2 socket is invalid!\n");
return -1; return -1;
} }
/* load destination ethernet MAC address */ /* load destination ethernet MAC address */
if (dest->mac_len == 6) if (dest->mac_len == 6) {
{ for (i = 0; i < 6; i++) {
for (i = 0; i < 6; i++)
{
mtu[mtu_len] = dest->mac[i]; mtu[mtu_len] = dest->mac[i];
mtu_len++; mtu_len++;
} }
} } else {
else
{
fprintf(stderr, "ethernet: invalid destination MAC address!\n"); fprintf(stderr, "ethernet: invalid destination MAC address!\n");
return -2; return -2;
} }
/* load source ethernet MAC address */ /* load source ethernet MAC address */
if (src->mac_len == 6) if (src->mac_len == 6) {
{ for (i = 0; i < 6; i++) {
for (i = 0; i < 6; i++)
{
mtu[mtu_len] = src->mac[i]; mtu[mtu_len] = src->mac[i];
mtu_len++; mtu_len++;
} }
} } else {
else
{
fprintf(stderr, "ethernet: invalid source MAC address!\n"); fprintf(stderr, "ethernet: invalid source MAC address!\n");
return -3; return -3;
} }
if ((14 + 3 + pdu_len) > MAX_MPDU) if ((14 + 3 + pdu_len) > MAX_MPDU) {
{
fprintf(stderr, "ethernet: PDU is too big to send!\n"); fprintf(stderr, "ethernet: PDU is too big to send!\n");
return -4; return -4;
} }
/* packet length */ /* packet length */
mtu_len += encode_unsigned16(&mtu[12], mtu_len += encode_unsigned16(&mtu[12],
3 /*DSAP,SSAP,LLC*/ + pdu_len); 3 /*DSAP,SSAP,LLC */ + pdu_len);
// Logical PDU portion // Logical PDU portion
mtu[mtu_len++] = 0x82; /* DSAP for BACnet */ mtu[mtu_len++] = 0x82; /* DSAP for BACnet */
mtu[mtu_len++] = 0x82; /* SSAP for BACnet */ mtu[mtu_len++] = 0x82; /* SSAP for BACnet */
@@ -249,7 +234,7 @@ int ethernet_send(
(struct sockaddr *) &eth_addr, sizeof(struct sockaddr)); (struct sockaddr *) &eth_addr, sizeof(struct sockaddr));
/* did it get sent? */ /* did it get sent? */
if (bytes < 0) if (bytes < 0)
fprintf(stderr,"ethernet: Error sending packet: %s\n", fprintf(stderr, "ethernet: Error sending packet: %s\n",
strerror(errno)); strerror(errno));
return bytes; return bytes;
@@ -257,16 +242,14 @@ int ethernet_send(
/* function to send a packet out the 802.2 socket */ /* function to send a packet out the 802.2 socket */
/* returns number of bytes sent on success, negative on failure */ /* returns number of bytes sent on success, negative on failure */
int ethernet_send_pdu( int ethernet_send_pdu(BACNET_ADDRESS * dest, // destination address
BACNET_ADDRESS *dest, // destination address uint8_t * pdu, // any data to be sent - may be null
uint8_t *pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data unsigned pdu_len) // number of bytes of data
{ {
int i = 0; // counter int i = 0; // counter
BACNET_ADDRESS src = {0}; // source address BACNET_ADDRESS src = { 0 }; // source address
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++) {
{
src.mac[i] = Ethernet_MAC_Address[i]; src.mac[i] = Ethernet_MAC_Address[i];
src.mac_len++; src.mac_len++;
} }
@@ -280,14 +263,13 @@ int ethernet_send_pdu(
// receives an 802.2 framed packet // receives an 802.2 framed packet
// returns the number of octets in the PDU, or zero on failure // returns the number of octets in the PDU, or zero on failure
uint16_t ethernet_receive( uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address
BACNET_ADDRESS *src, // source address uint8_t * pdu, // PDU data
uint8_t *pdu, // PDU data
uint16_t max_pdu, // amount of space available in the PDU uint16_t max_pdu, // amount of space available in the PDU
unsigned timeout) // number of milliseconds to wait for a packet unsigned timeout) // number of milliseconds to wait for a packet
{ {
int received_bytes; int received_bytes;
uint8_t buf[MAX_MPDU] = {0}; // data uint8_t buf[MAX_MPDU] = { 0 }; // data
uint16_t pdu_len = 0; // return value uint16_t pdu_len = 0; // return value
fd_set read_fds; fd_set read_fds;
int max; int max;
@@ -300,14 +282,11 @@ uint16_t ethernet_receive(
/* we could just use a non-blocking socket, but that consumes all /* we could just use a non-blocking socket, but that consumes all
the CPU time. We can use a timeout; it is only supported as the CPU time. We can use a timeout; it is only supported as
a select. */ a select. */
if (timeout >= 1000) if (timeout >= 1000) {
{
select_timeout.tv_sec = timeout / 1000; select_timeout.tv_sec = timeout / 1000;
select_timeout.tv_usec = select_timeout.tv_usec =
1000 * (timeout - select_timeout.tv_sec * 1000); 1000 * (timeout - select_timeout.tv_sec * 1000);
} } else {
else
{
select_timeout.tv_sec = 0; select_timeout.tv_sec = 0;
select_timeout.tv_usec = 1000 * timeout; select_timeout.tv_usec = 1000 * timeout;
} }
@@ -326,7 +305,8 @@ uint16_t ethernet_receive(
// using O_NONBLOCK and no data // using O_NONBLOCK and no data
// was immediately available for reading. // was immediately available for reading.
if (errno != EAGAIN) if (errno != EAGAIN)
fprintf(stderr,"ethernet: Read error in receiving packet: %s\n", fprintf(stderr,
"ethernet: Read error in receiving packet: %s\n",
strerror(errno)); strerror(errno));
return 0; return 0;
} }
@@ -345,18 +325,17 @@ uint16_t ethernet_receive(
// check destination address for when // check destination address for when
// the Ethernet card is in promiscious mode // the Ethernet card is in promiscious mode
if ((memcmp(&buf[0], Ethernet_MAC_Address,6) != 0) if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0)
&& (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) && (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) {
{
//fprintf(stderr, "ethernet: This packet isn't for us\n"); //fprintf(stderr, "ethernet: This packet isn't for us\n");
return 0; return 0;
} }
(void)decode_unsigned16(&buf[12],&pdu_len); (void) decode_unsigned16(&buf[12], &pdu_len);
pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ; pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ;
// copy the buffer into the PDU // copy the buffer into the PDU
if (pdu_len < max_pdu) if (pdu_len < max_pdu)
memmove(&pdu[0],&buf[17],pdu_len); memmove(&pdu[0], &buf[17], pdu_len);
// ignore packets that are too large // ignore packets that are too large
else else
pdu_len = 0; pdu_len = 0;
@@ -365,54 +344,47 @@ uint16_t ethernet_receive(
return pdu_len; return pdu_len;
} }
void ethernet_set_my_address(BACNET_ADDRESS *my_address) void ethernet_set_my_address(BACNET_ADDRESS * my_address)
{ {
int i = 0; int i = 0;
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++) {
{
Ethernet_MAC_Address[i] = my_address->mac[i]; Ethernet_MAC_Address[i] = my_address->mac[i];
} }
return; return;
} }
void ethernet_get_my_address(BACNET_ADDRESS *my_address) void ethernet_get_my_address(BACNET_ADDRESS * my_address)
{ {
int i = 0; int i = 0;
my_address->mac_len = 0; my_address->mac_len = 0;
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++) {
{
my_address->mac[i] = Ethernet_MAC_Address[i]; my_address->mac[i] = Ethernet_MAC_Address[i];
my_address->mac_len++; my_address->mac_len++;
} }
my_address->net = 0; // local only, no routing my_address->net = 0; // local only, no routing
my_address->len = 0; my_address->len = 0;
for (i = 0; i < MAX_MAC_LEN; i++) for (i = 0; i < MAX_MAC_LEN; i++) {
{
my_address->adr[i] = 0; my_address->adr[i] = 0;
} }
return; return;
} }
void ethernet_get_broadcast_address( void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address
BACNET_ADDRESS *dest) // destination address
{ {
int i = 0; // counter int i = 0; // counter
if (dest) if (dest) {
{ for (i = 0; i < 6; i++) {
for (i = 0; i < 6; i++)
{
dest->mac[i] = Ethernet_Broadcast[i]; dest->mac[i] = Ethernet_Broadcast[i];
} }
dest->mac_len = 6; dest->mac_len = 6;
dest->net = BACNET_BROADCAST_NETWORK; dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; // denotes broadcast address dest->len = 0; // denotes broadcast address
for (i = 0; i < MAX_MAC_LEN; i++) for (i = 0; i < MAX_MAC_LEN; i++) {
{
dest->adr[i] = 0; dest->adr[i] = 0;
} }
} }
@@ -420,34 +392,28 @@ void ethernet_get_broadcast_address(
return; return;
} }
void ethernet_debug_address( void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest)
const char *info,
BACNET_ADDRESS *dest)
{ {
int i = 0; // counter int i = 0; // counter
if (info) if (info)
fprintf(stderr,"%s",info); fprintf(stderr, "%s", info);
if (dest) if (dest) {
{ fprintf(stderr, "Address:\n");
fprintf(stderr,"Address:\n"); fprintf(stderr, " MAC Length=%d\n", dest->mac_len);
fprintf(stderr," MAC Length=%d\n",dest->mac_len); fprintf(stderr, " MAC Address=");
fprintf(stderr," MAC Address="); for (i = 0; i < MAX_MAC_LEN; i++) {
for (i = 0; i < MAX_MAC_LEN; i++) fprintf(stderr, "%02X ", (unsigned) dest->mac[i]);
{
fprintf(stderr,"%02X ",(unsigned)dest->mac[i]);
} }
fprintf(stderr,"\n"); fprintf(stderr, "\n");
fprintf(stderr," Net=%hu\n",dest->net); fprintf(stderr, " Net=%hu\n", dest->net);
fprintf(stderr," Len=%d\n",dest->len); fprintf(stderr, " Len=%d\n", dest->len);
fprintf(stderr," Adr="); fprintf(stderr, " Adr=");
for (i = 0; i < MAX_MAC_LEN; i++) for (i = 0; i < MAX_MAC_LEN; i++) {
{ fprintf(stderr, "%02X ", (unsigned) dest->adr[i]);
fprintf(stderr,"%02X ",(unsigned)dest->adr[i]);
} }
fprintf(stderr,"\n"); fprintf(stderr, "\n");
} }
return; return;
} }
+58 -100
View File
@@ -48,12 +48,10 @@
bool Who_Is_Request = true; bool Who_Is_Request = true;
// buffers used for receiving // buffers used for receiving
static uint8_t Rx_Buf[MAX_MPDU] = {0}; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
static void LocalIAmHandler( static void LocalIAmHandler(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src)
uint16_t service_len,
BACNET_ADDRESS *src)
{ {
int len = 0; int len = 0;
uint32_t device_id = 0; uint32_t device_id = 0;
@@ -61,24 +59,16 @@ static void LocalIAmHandler(
int segmentation = 0; int segmentation = 0;
uint16_t vendor_id = 0; uint16_t vendor_id = 0;
(void)src; (void) src;
(void)service_len; (void) service_len;
len = iam_decode_service_request( len = iam_decode_service_request(service_request,
service_request, &device_id, &max_apdu, &segmentation, &vendor_id);
&device_id, fprintf(stderr, "Received I-Am Request");
&max_apdu, if (len != -1) {
&segmentation, fprintf(stderr, " from %u!\n", device_id);
&vendor_id); address_add(device_id, max_apdu, src);
fprintf(stderr,"Received I-Am Request"); } else
if (len != -1) fprintf(stderr, "!\n");
{
fprintf(stderr," from %u!\n",device_id);
address_add(device_id,
max_apdu,
src);
}
else
fprintf(stderr,"!\n");
return; return;
} }
@@ -96,8 +86,7 @@ static void Read_Properties(void)
properties in the Device Object. Note that this demo properties in the Device Object. Note that this demo
tests for error messages so that the device doesn't have tests for error messages so that the device doesn't have
to have all the properties listed here. */ to have all the properties listed here. */
const int object_props[] = const int object_props[] = {
{
PROP_OBJECT_IDENTIFIER, PROP_OBJECT_IDENTIFIER,
PROP_OBJECT_NAME, PROP_OBJECT_NAME,
PROP_OBJECT_TYPE, PROP_OBJECT_TYPE,
@@ -131,37 +120,29 @@ static void Read_Properties(void)
rather than trying to read the entire object rather than trying to read the entire object
list in one message. */ list in one message. */
/* some proprietary properties */ /* some proprietary properties */
514,515, 514, 515,
/* end of list */ /* end of list */
-1 -1
}; };
if (address_count()) if (address_count()) {
{ if (address_get_by_index(index, &device_id, &max_apdu, &src)) {
if (address_get_by_index(index, &device_id, &max_apdu, &src))
{
if (object_props[property] < 0) if (object_props[property] < 0)
next_device = true; next_device = true;
else else {
{
/* note: if we wanted to do this synchronously, we would get the /* note: if we wanted to do this synchronously, we would get the
invoke ID from the sending of the request, and wait until we invoke ID from the sending of the request, and wait until we
got the reply with matching invoke ID or the TSM of the got the reply with matching invoke ID or the TSM of the
invoke ID expired. This demo is doing things asynchronously. */ invoke ID expired. This demo is doing things asynchronously. */
status = Send_Read_Property_Request( status = Send_Read_Property_Request(device_id, // destination device
device_id, // destination device
OBJECT_DEVICE, OBJECT_DEVICE,
device_id, device_id, object_props[property], BACNET_ARRAY_ALL);
object_props[property],
BACNET_ARRAY_ALL);
if (status) if (status)
property++; property++;
} }
} } else
else
next_device = true; next_device = true;
if (next_device) if (next_device) {
{
next_device = false; next_device = false;
index++; index++;
if (index >= MAX_ADDRESS_CACHE) if (index >= MAX_ADDRESS_CACHE)
@@ -176,57 +157,47 @@ static void Read_Properties(void)
static void Init_Service_Handlers(void) static void Init_Service_Handlers(void)
{ {
// we need to handle who-is to support dynamic device binding // we need to handle who-is to support dynamic device binding
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler); LocalIAmHandler);
// set the handler for all the services we don't implement // set the handler for all the services we don't implement
// It is required to send the proper reject message... // It is required to send the proper reject message...
apdu_set_unrecognized_service_handler_handler( apdu_set_unrecognized_service_handler_handler
handler_unrecognized_service); (handler_unrecognized_service);
// Set the handlers for any confirmed services that we support. // Set the handlers for any confirmed services that we support.
// We must implement read property - it's required! // We must implement read property - it's required!
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
SERVICE_CONFIRMED_WRITE_PROPERTY,
handler_write_property); handler_write_property);
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
handler_atomic_read_file); handler_atomic_read_file);
// handle the data coming back from confirmed requests // handle the data coming back from confirmed requests
apdu_set_confirmed_ack_handler( apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property_ack); handler_read_property_ack);
apdu_set_confirmed_ack_handler( apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
handler_atomic_read_file_ack); handler_atomic_read_file_ack);
} }
static void print_address_cache(void) static void print_address_cache(void)
{ {
unsigned i,j; unsigned i, j;
BACNET_ADDRESS address; BACNET_ADDRESS address;
uint32_t device_id = 0; uint32_t device_id = 0;
unsigned max_apdu = 0; unsigned max_apdu = 0;
fprintf(stderr,"Device\tMAC\tMaxAPDU\tNet\n"); fprintf(stderr, "Device\tMAC\tMaxAPDU\tNet\n");
for (i = 0; i < MAX_ADDRESS_CACHE; i++) for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
{ if (address_get_by_index(i, &device_id, &max_apdu, &address)) {
if (address_get_by_index(i,&device_id, &max_apdu, &address)) fprintf(stderr, "%u\t", device_id);
{ for (j = 0; j < address.mac_len; j++) {
fprintf(stderr,"%u\t",device_id); fprintf(stderr, "%02X", address.mac[j]);
for (j = 0; j < address.mac_len; j++)
{
fprintf(stderr,"%02X",address.mac[j]);
} }
fprintf(stderr,"\t"); fprintf(stderr, "\t");
fprintf(stderr,"%hu\t",max_apdu); fprintf(stderr, "%hu\t", max_apdu);
fprintf(stderr,"%hu\n",address.net); fprintf(stderr, "%hu\n", address.net);
} }
} }
} }
@@ -238,7 +209,7 @@ static void print_tsm_stats(void)
idle = tsm_transaction_idle_count(); idle = tsm_transaction_idle_count();
total = MAX_TSM_TRANSACTIONS; total = MAX_TSM_TRANSACTIONS;
fprintf(stderr,"TSM: %d idle of %d transactions\n",idle,total); fprintf(stderr, "TSM: %d idle of %d transactions\n", idle, total);
} }
static void sig_handler(int signo) static void sig_handler(int signo)
@@ -252,7 +223,7 @@ static void sig_handler(int signo)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = { 0 }; // address where message came from
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; // milliseconds
unsigned count = 0; // milliseconds unsigned count = 0; // milliseconds
@@ -267,61 +238,48 @@ int main(int argc, char *argv[])
// setup this BACnet Server device // setup this BACnet Server device
Device_Set_Object_Instance_Number(111); Device_Set_Object_Instance_Number(111);
Init_Service_Handlers(); Init_Service_Handlers();
#ifdef BACDL_ETHERNET #ifdef BACDL_ETHERNET
// init the physical layer // init the physical layer
if (!ethernet_init("eth0")) if (!ethernet_init("eth0"))
return 1; return 1;
#endif #endif
#ifdef BACDL_BIP #ifdef BACDL_BIP
bip_set_interface("eth0"); bip_set_interface("eth0");
bip_set_port(0xBAC0); bip_set_port(0xBAC0);
if (!bip_init()) if (!bip_init())
return 1; return 1;
#endif #endif
#ifdef BACDL_ARCNET #ifdef BACDL_ARCNET
if (!arcnet_init("arc0")) if (!arcnet_init("arc0"))
return 1; return 1;
#endif #endif
// loop forever // loop forever
for (;;) for (;;) {
{
// input // input
new_time = time(NULL); new_time = time(NULL);
// returns 0 bytes on timeout // returns 0 bytes on timeout
pdu_len = datalink_receive( pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
// process // process
if (pdu_len) if (pdu_len) {
{ npdu_handler(&src, &Rx_Buf[0], pdu_len);
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
} }
if (new_time > start_time) if (new_time > start_time) {
{
tsm_timer_milliseconds(new_time - start_time * 1000); tsm_timer_milliseconds(new_time - start_time * 1000);
start_time = new_time; start_time = new_time;
} }
if (I_Am_Request) if (I_Am_Request) {
{
I_Am_Request = false; I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]); iam_send(&Handler_Transmit_Buffer[0]);
} else if (Who_Is_Request) } else if (Who_Is_Request) {
{
Who_Is_Request = false; Who_Is_Request = false;
Send_WhoIs(-1,-1); Send_WhoIs(-1, -1);
} }
// output // output
// some round robin task switching // some round robin task switching
count++; count++;
switch (count) switch (count) {
{
case 1: case 1:
// used for testing, but kind of noisy on the network // used for testing, but kind of noisy on the network
//Read_Properties(); //Read_Properties();
+7 -13
View File
@@ -44,23 +44,20 @@
#include "mstp.h" #include "mstp.h"
// Transmits a Frame on the wire // Transmits a Frame on the wire
void RS485_Send_Frame( void RS485_Send_Frame(struct mstp_port_struct_t *mstp_port, // port specific data
struct mstp_port_struct_t *mstp_port, // port specific data uint8_t * buffer, // frame to send (up to 501 bytes of data)
uint8_t *buffer, // frame to send (up to 501 bytes of data)
uint16_t nbytes) // number of bytes of data (up to 501) uint16_t nbytes) // number of bytes of data (up to 501)
{ {
// in order to avoid line contention // in order to avoid line contention
while (mstp_port->Turn_Around_Waiting) while (mstp_port->Turn_Around_Waiting) {
{
// wait, yield, or whatever // wait, yield, or whatever
} }
// Disable the receiver, and enable the transmit line driver. // Disable the receiver, and enable the transmit line driver.
while (nbytes) while (nbytes) {
{ putc(*buffer, stderr);
putc(*buffer,stderr);
buffer++; buffer++;
nbytes--; nbytes--;
} }
@@ -76,13 +73,11 @@ void RS485_Send_Frame(
// called by timer, interrupt(?) or other thread // called by timer, interrupt(?) or other thread
void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port) void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port)
{ {
if (mstp_port->ReceiveError == true) if (mstp_port->ReceiveError == true) {
{
// wait for state machine to clear this // wait for state machine to clear this
} }
// wait for state machine to read from the DataRegister // wait for state machine to read from the DataRegister
else if (mstp_port->DataAvailable == false) else if (mstp_port->DataAvailable == false) {
{
// check for data // check for data
// if error, // if error,
@@ -96,4 +91,3 @@ void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port)
// return; // return;
} }
} }
+36 -31
View File
@@ -47,7 +47,7 @@ void bip_set_interface(char *ifname)
#if (defined(BACDL_ETHERNET) || defined(BACDL_BIP)) #if (defined(BACDL_ETHERNET) || defined(BACDL_BIP))
/*-----------------------------------*/ /*-----------------------------------*/
static void Error(const char * Msg) static void Error(const char *Msg)
{ {
int Code = WSAGetLastError(); int Code = WSAGetLastError();
#ifdef HOST #ifdef HOST
@@ -62,8 +62,7 @@ static void Error(const char * Msg)
/*-----------------------------------*/ /*-----------------------------------*/
void InterfaceCleanup(void) void InterfaceCleanup(void)
{ {
if (interface != SOCKET_ERROR) if (interface != SOCKET_ERROR) {
{
xn_interface_close(interface); xn_interface_close(interface);
interface = SOCKET_ERROR; interface = SOCKET_ERROR;
#if DEVICE_ID == PRISM_PCMCIA_DEVICE #if DEVICE_ID == PRISM_PCMCIA_DEVICE
@@ -85,7 +84,7 @@ static void NetInitialize(void)
if (!RTKDebugVersion()) // switch of all diagnostics and error messages of RTIP-32 if (!RTKDebugVersion()) // switch of all diagnostics and error messages of RTIP-32
xn_callbacks()->cb_wr_screen_string_fnc = NULL; xn_callbacks()->cb_wr_screen_string_fnc = NULL;
CLKSetTimerIntVal(10*1000); // 10 millisecond tick CLKSetTimerIntVal(10 * 1000); // 10 millisecond tick
RTKDelay(1); RTKDelay(1);
RTCMOSSetSystemTime(); // get the right time-of-day RTCMOSSetSystemTime(); // get the right time-of-day
@@ -100,13 +99,12 @@ static void NetInitialize(void)
#ifdef DHCP #ifdef DHCP
XN_REGISTER_DHCP_CLI() // and optionally the DHCP client XN_REGISTER_DHCP_CLI() // and optionally the DHCP client
#endif #endif
Result = xn_rtip_init(); // Initialize the RTIP stack Result = xn_rtip_init(); // Initialize the RTIP stack
if (Result != 0) if (Result != 0)
Error("xn_rtip_init failed"); Error("xn_rtip_init failed");
atexit(InterfaceCleanup); // make sure the driver is shut down properly atexit(InterfaceCleanup); // make sure the driver is shut down properly
RTCallDebugger(RT_DBG_CALLRESET, (DWORD)exit, 0); // even if we get restarted by the debugger RTCallDebugger(RT_DBG_CALLRESET, (DWORD) exit, 0); // even if we get restarted by the debugger
Result = BIND_DRIVER(MINOR_0); // tell RTIP what Ethernet driver we want (see netcfg.h) Result = BIND_DRIVER(MINOR_0); // tell RTIP what Ethernet driver we want (see netcfg.h)
if (Result != 0) if (Result != 0)
@@ -119,25 +117,27 @@ static void NetInitialize(void)
#endif #endif
// Open the interface // Open the interface
interface = xn_interface_open_config(DEVICE_ID, MINOR_0, ED_IO_ADD, ED_IRQ, ED_MEM_ADD); interface =
xn_interface_open_config(DEVICE_ID, MINOR_0, ED_IO_ADD, ED_IRQ,
ED_MEM_ADD);
if (interface == SOCKET_ERROR) if (interface == SOCKET_ERROR)
Error("xn_interface_open_config failed"); Error("xn_interface_open_config failed");
else else {
{
struct _iface_info ii; struct _iface_info ii;
#ifdef BACDL_ETHERNET #ifdef BACDL_ETHERNET
BACNET_ADDRESS my_address; BACNET_ADDRESS my_address;
unsigned i; unsigned i;
#endif #endif
xn_interface_info(interface, &ii); xn_interface_info(interface, &ii);
printf("Interface opened, MAC address: %02x-%02x-%02x-%02x-%02x-%02x\n", printf
ii.my_ethernet_address[0], ii.my_ethernet_address[1], ii.my_ethernet_address[2], ("Interface opened, MAC address: %02x-%02x-%02x-%02x-%02x-%02x\n",
ii.my_ethernet_address[3], ii.my_ethernet_address[4], ii.my_ethernet_address[5]); ii.my_ethernet_address[0], ii.my_ethernet_address[1],
ii.my_ethernet_address[2], ii.my_ethernet_address[3],
ii.my_ethernet_address[4], ii.my_ethernet_address[5]);
#ifdef BACDL_ETHERNET #ifdef BACDL_ETHERNET
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++) {
{
my_address.mac[i] = ii.my_ethernet_address[i]; my_address.mac[i] = ii.my_ethernet_address[i];
} }
ethernet_set_my_address(&my_address); ethernet_set_my_address(&my_address);
#endif #endif
} }
@@ -157,16 +157,20 @@ static void NetInitialize(void)
Result = xn_autoip(interface, MinIP, MaxIP, NetMask, TargetIP); Result = xn_autoip(interface, MinIP, MaxIP, NetMask, TargetIP);
if (Result == SOCKET_ERROR) if (Result == SOCKET_ERROR)
Error("xn_autoip failed"); Error("xn_autoip failed");
else else {
{ printf("Auto-assigned IP address %i.%i.%i.%i\n", TargetIP[0],
printf("Auto-assigned IP address %i.%i.%i.%i\n", TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]); TargetIP[1], TargetIP[2], TargetIP[3]);
// define default gateway and DNS server // define default gateway and DNS server
xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF); xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface,
xn_set_server_list((DWORD*)DNSServer, 1); RT_INF);
} xn_set_server_list((DWORD *) DNSServer, 1);
}
#elif defined(DHCP) // use DHCP #elif defined(DHCP) // use DHCP
{ {
DHCP_param param[] = {{SUBNET_MASK, 1}, {DNS_OP, 1}, {ROUTER_OPTION, 1}}; DHCP_param param[] = { {SUBNET_MASK, 1}
, {DNS_OP, 1}
, {ROUTER_OPTION, 1}
};
DHCP_session DS; DHCP_session DS;
DHCP_conf DC; DHCP_conf DC;
@@ -178,15 +182,17 @@ static void NetInitialize(void)
if (Result == SOCKET_ERROR) if (Result == SOCKET_ERROR)
Error("xn_dhcp failed"); Error("xn_dhcp failed");
memcpy(TargetIP, DS.client_ip, 4); memcpy(TargetIP, DS.client_ip, 4);
printf("My IP address is: %i.%i.%i.%i\n", TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]); printf("My IP address is: %i.%i.%i.%i\n", TargetIP[0], TargetIP[1],
} TargetIP[2], TargetIP[3]);
}
#else #else
// Set the IP address and interface // Set the IP address and interface
printf("Using static IP address %i.%i.%i.%i\n", TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]); printf("Using static IP address %i.%i.%i.%i\n", TargetIP[0],
TargetIP[1], TargetIP[2], TargetIP[3]);
Result = xn_set_ip(interface, TargetIP, NetMask); Result = xn_set_ip(interface, TargetIP, NetMask);
// define default gateway and DNS server // define default gateway and DNS server
xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF); xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF);
xn_set_server_list((DWORD*)DNSServer, 1); xn_set_server_list((DWORD *) DNSServer, 1);
#endif #endif
#else // HOST defined, run on Windows #else // HOST defined, run on Windows
@@ -204,7 +210,7 @@ static void NetInitialize(void)
bool bip_init(void) bool bip_init(void)
{ {
int rv = 0; // return from socket lib calls int rv = 0; // return from socket lib calls
struct sockaddr_in sin = {-1}; struct sockaddr_in sin = { -1 };
int value = 1; int value = 1;
int sock_fd = -1; int sock_fd = -1;
@@ -238,9 +244,8 @@ bool bip_init(void)
sin.sin_port = htons(bip_get_port()); sin.sin_port = htons(bip_get_port());
memset(&(sin.sin_zero), '\0', 8); memset(&(sin.sin_zero), '\0', 8);
rv = bind(sock_fd, rv = bind(sock_fd,
(const struct sockaddr*)&sin, sizeof(struct sockaddr)); (const struct sockaddr *) &sin, sizeof(struct sockaddr));
if (rv < 0) if (rv < 0) {
{
close(sock_fd); close(sock_fd);
bip_set_socket(-1); bip_set_socket(-1);
return false; return false;
+60 -88
View File
@@ -40,6 +40,7 @@ uint8_t Ethernet_Broadcast[MAX_MAC_LEN] =
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
// commonly used empty address for ethernet quick compare // commonly used empty address for ethernet quick compare
uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 }; uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
// my local device data - MAC address // my local device data - MAC address
uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 }; uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
@@ -65,17 +66,17 @@ bool ethernet_init(char *interface_name)
{ {
int value = 1; int value = 1;
(void)interface_name; (void) interface_name;
// setup the socket // setup the socket
Ethernet_Socket = socket(AF_INET, SOCK_RAW, 0); Ethernet_Socket = socket(AF_INET, SOCK_RAW, 0);
//Ethernet_Socket = socket(AF_INET, SOCK_STREAM, 0); //Ethernet_Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Ethernet_Socket < 0) if (Ethernet_Socket < 0)
fprintf(stderr,"ethernet: failed to bind to socket!\r\n"); fprintf(stderr, "ethernet: failed to bind to socket!\r\n");
Ethernet_Address.sa_family = AF_INET; Ethernet_Address.sa_family = AF_INET;
memset(Ethernet_Address.sa_data,0,sizeof(Ethernet_Address.sa_data)); memset(Ethernet_Address.sa_data, 0, sizeof(Ethernet_Address.sa_data));
if (bind(Ethernet_Socket, if (bind(Ethernet_Socket,
&Ethernet_Address, sizeof(Ethernet_Address)) == SOCKET_ERROR) &Ethernet_Address, sizeof(Ethernet_Address)) == SOCKET_ERROR)
fprintf(stderr,"ethernet: failed to bind to socket!\r\n"); fprintf(stderr, "ethernet: failed to bind to socket!\r\n");
//setsockopt(Ethernet_Socket,SOL_SOCKET,SO_802_2,(char *)&value,sizeof(value)); //setsockopt(Ethernet_Socket,SOL_SOCKET,SO_802_2,(char *)&value,sizeof(value));
return ethernet_valid(); return ethernet_valid();
@@ -83,10 +84,9 @@ bool ethernet_init(char *interface_name)
/* function to send a packet out the 802.2 socket */ /* function to send a packet out the 802.2 socket */
/* returns bytes sent on success, negative number on failure */ /* returns bytes sent on success, negative number on failure */
int ethernet_send( int ethernet_send(BACNET_ADDRESS * dest, // destination address
BACNET_ADDRESS *dest, // destination address BACNET_ADDRESS * src, // source address
BACNET_ADDRESS *src, // source address uint8_t * pdu, // any data to be sent - may be null
uint8_t *pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data unsigned pdu_len) // number of bytes of data
{ {
int bytes = 0; int bytes = 0;
@@ -95,48 +95,38 @@ int ethernet_send(
int i = 0; int i = 0;
// don't waste time if the socket is not valid // don't waste time if the socket is not valid
if (Ethernet_Socket < 0) if (Ethernet_Socket < 0) {
{
fprintf(stderr, "ethernet: 802.2 socket is invalid!\n"); fprintf(stderr, "ethernet: 802.2 socket is invalid!\n");
return -1; return -1;
} }
/* load destination ethernet MAC address */ /* load destination ethernet MAC address */
if (dest->mac_len == 6) if (dest->mac_len == 6) {
{ for (i = 0; i < 6; i++) {
for (i = 0; i < 6; i++)
{
mtu[mtu_len] = dest->mac[i]; mtu[mtu_len] = dest->mac[i];
mtu_len++; mtu_len++;
} }
} } else {
else
{
fprintf(stderr, "ethernet: invalid destination MAC address!\n"); fprintf(stderr, "ethernet: invalid destination MAC address!\n");
return -2; return -2;
} }
/* load source ethernet MAC address */ /* load source ethernet MAC address */
if (src->mac_len == 6) if (src->mac_len == 6) {
{ for (i = 0; i < 6; i++) {
for (i = 0; i < 6; i++)
{
mtu[mtu_len] = src->mac[i]; mtu[mtu_len] = src->mac[i];
mtu_len++; mtu_len++;
} }
} } else {
else
{
fprintf(stderr, "ethernet: invalid source MAC address!\n"); fprintf(stderr, "ethernet: invalid source MAC address!\n");
return -3; return -3;
} }
if ((14 + 3 + pdu_len) > MAX_MPDU) if ((14 + 3 + pdu_len) > MAX_MPDU) {
{
fprintf(stderr, "ethernet: PDU is too big to send!\n"); fprintf(stderr, "ethernet: PDU is too big to send!\n");
return -4; return -4;
} }
/* packet length */ /* packet length */
mtu_len += encode_unsigned16(&mtu[12], mtu_len += encode_unsigned16(&mtu[12],
3 /*DSAP,SSAP,LLC*/ + pdu_len); 3 /*DSAP,SSAP,LLC */ + pdu_len);
// Logical PDU portion // Logical PDU portion
mtu[mtu_len++] = 0x82; /* DSAP for BACnet */ mtu[mtu_len++] = 0x82; /* DSAP for BACnet */
mtu[mtu_len++] = 0x82; /* SSAP for BACnet */ mtu[mtu_len++] = 0x82; /* SSAP for BACnet */
@@ -145,11 +135,10 @@ int ethernet_send(
mtu_len += pdu_len; mtu_len += pdu_len;
/* Send the packet */ /* Send the packet */
bytes = bytes = send(Ethernet_Socket, (const char *) &mtu, mtu_len, 0);
send(Ethernet_Socket, (const char *)&mtu, mtu_len, 0);
/* did it get sent? */ /* did it get sent? */
if (bytes < 0) if (bytes < 0)
fprintf(stderr,"ethernet: Error sending packet: %s\n", fprintf(stderr, "ethernet: Error sending packet: %s\n",
strerror(errno)); strerror(errno));
return bytes; return bytes;
@@ -157,16 +146,14 @@ int ethernet_send(
/* function to send a packet out the 802.2 socket */ /* function to send a packet out the 802.2 socket */
/* returns bytes sent on success, negative number on failure */ /* returns bytes sent on success, negative number on failure */
int ethernet_send_pdu( int ethernet_send_pdu(BACNET_ADDRESS * dest, // destination address
BACNET_ADDRESS *dest, // destination address uint8_t * pdu, // any data to be sent - may be null
uint8_t *pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data unsigned pdu_len) // number of bytes of data
{ {
int i = 0; // counter int i = 0; // counter
BACNET_ADDRESS src = {0}; // source address BACNET_ADDRESS src = { 0 }; // source address
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++) {
{
src.mac[i] = Ethernet_MAC_Address[i]; src.mac[i] = Ethernet_MAC_Address[i];
src.mac_len++; src.mac_len++;
} }
@@ -180,14 +167,13 @@ int ethernet_send_pdu(
// receives an 802.2 framed packet // receives an 802.2 framed packet
// returns the number of octets in the PDU, or zero on failure // returns the number of octets in the PDU, or zero on failure
uint16_t ethernet_receive( uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address
BACNET_ADDRESS *src, // source address uint8_t * pdu, // PDU data
uint8_t *pdu, // PDU data
uint16_t max_pdu, // amount of space available in the PDU uint16_t max_pdu, // amount of space available in the PDU
unsigned timeout) // number of milliseconds to wait for a packet unsigned timeout) // number of milliseconds to wait for a packet
{ {
int received_bytes; int received_bytes;
uint8_t buf[MAX_MPDU] = {0}; // data uint8_t buf[MAX_MPDU] = { 0 }; // data
uint16_t pdu_len = 0; // return value uint16_t pdu_len = 0; // return value
fd_set read_fds; fd_set read_fds;
int max; int max;
@@ -200,14 +186,11 @@ uint16_t ethernet_receive(
/* we could just use a non-blocking socket, but that consumes all /* we could just use a non-blocking socket, but that consumes all
the CPU time. We can use a timeout; it is only supported as the CPU time. We can use a timeout; it is only supported as
a select. */ a select. */
if (timeout >= 1000) if (timeout >= 1000) {
{
select_timeout.tv_sec = timeout / 1000; select_timeout.tv_sec = timeout / 1000;
select_timeout.tv_usec = select_timeout.tv_usec =
1000 * (timeout - select_timeout.tv_sec * 1000); 1000 * (timeout - select_timeout.tv_sec * 1000);
} } else {
else
{
select_timeout.tv_sec = 0; select_timeout.tv_sec = 0;
select_timeout.tv_usec = 1000 * timeout; select_timeout.tv_usec = 1000 * timeout;
} }
@@ -216,7 +199,8 @@ uint16_t ethernet_receive(
max = Ethernet_Socket; max = Ethernet_Socket;
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0)
received_bytes = recv(Ethernet_Socket, (char *)&buf[0], MAX_MPDU, 0); received_bytes =
recv(Ethernet_Socket, (char *) &buf[0], MAX_MPDU, 0);
else else
return 0; return 0;
@@ -226,7 +210,8 @@ uint16_t ethernet_receive(
// using O_NONBLOCK and no data // using O_NONBLOCK and no data
// was immediately available for reading. // was immediately available for reading.
if (errno != EAGAIN) if (errno != EAGAIN)
fprintf(stderr,"ethernet: Read error in receiving packet: %s\n", fprintf(stderr,
"ethernet: Read error in receiving packet: %s\n",
strerror(errno)); strerror(errno));
return 0; return 0;
} }
@@ -245,18 +230,17 @@ uint16_t ethernet_receive(
// check destination address for when // check destination address for when
// the Ethernet card is in promiscious mode // the Ethernet card is in promiscious mode
if ((memcmp(&buf[0], Ethernet_MAC_Address,6) != 0) if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0)
&& (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) && (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) {
{
//fprintf(stderr, "ethernet: This packet isn't for us\n"); //fprintf(stderr, "ethernet: This packet isn't for us\n");
return 0; return 0;
} }
(void)decode_unsigned16(&buf[12],&pdu_len); (void) decode_unsigned16(&buf[12], &pdu_len);
pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ; pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ;
// copy the buffer into the PDU // copy the buffer into the PDU
if (pdu_len < max_pdu) if (pdu_len < max_pdu)
memmove(&pdu[0],&buf[17],pdu_len); memmove(&pdu[0], &buf[17], pdu_len);
// ignore packets that are too large // ignore packets that are too large
// client should check my max apdu first // client should check my max apdu first
else else
@@ -265,54 +249,47 @@ uint16_t ethernet_receive(
return pdu_len; return pdu_len;
} }
void ethernet_get_my_address(BACNET_ADDRESS *my_address) void ethernet_get_my_address(BACNET_ADDRESS * my_address)
{ {
int i = 0; int i = 0;
my_address->mac_len = 0; my_address->mac_len = 0;
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++) {
{
my_address->mac[i] = Ethernet_MAC_Address[i]; my_address->mac[i] = Ethernet_MAC_Address[i];
my_address->mac_len++; my_address->mac_len++;
} }
my_address->net = 0; // local only, no routing my_address->net = 0; // local only, no routing
my_address->len = 0; my_address->len = 0;
for (i = 0; i < MAX_MAC_LEN; i++) for (i = 0; i < MAX_MAC_LEN; i++) {
{
my_address->adr[i] = 0; my_address->adr[i] = 0;
} }
return; return;
} }
void ethernet_set_my_address(BACNET_ADDRESS *my_address) void ethernet_set_my_address(BACNET_ADDRESS * my_address)
{ {
int i = 0; int i = 0;
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++) {
{
Ethernet_MAC_Address[i] = my_address->mac[i]; Ethernet_MAC_Address[i] = my_address->mac[i];
} }
return; return;
} }
void ethernet_get_broadcast_address( void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address
BACNET_ADDRESS *dest) // destination address
{ {
int i = 0; // counter int i = 0; // counter
if (dest) if (dest) {
{ for (i = 0; i < 6; i++) {
for (i = 0; i < 6; i++)
{
dest->mac[i] = Ethernet_Broadcast[i]; dest->mac[i] = Ethernet_Broadcast[i];
} }
dest->mac_len = 6; dest->mac_len = 6;
dest->net = BACNET_BROADCAST_NETWORK; dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0; // denotes broadcast address dest->len = 0; // denotes broadcast address
for (i = 0; i < MAX_MAC_LEN; i++) for (i = 0; i < MAX_MAC_LEN; i++) {
{
dest->adr[i] = 0; dest->adr[i] = 0;
} }
} }
@@ -320,32 +297,27 @@ void ethernet_get_broadcast_address(
return; return;
} }
void ethernet_debug_address( void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest)
const char *info,
BACNET_ADDRESS *dest)
{ {
int i = 0; // counter int i = 0; // counter
if (info) if (info)
fprintf(stderr,"%s",info); fprintf(stderr, "%s", info);
if (dest) if (dest) {
{ fprintf(stderr, "Address:\n");
fprintf(stderr,"Address:\n"); fprintf(stderr, " MAC Length=%d\n", dest->mac_len);
fprintf(stderr," MAC Length=%d\n",dest->mac_len); fprintf(stderr, " MAC Address=");
fprintf(stderr," MAC Address="); for (i = 0; i < MAX_MAC_LEN; i++) {
for (i = 0; i < MAX_MAC_LEN; i++) fprintf(stderr, "%02X ", (unsigned) dest->mac[i]);
{
fprintf(stderr,"%02X ",(unsigned)dest->mac[i]);
} }
fprintf(stderr,"\n"); fprintf(stderr, "\n");
fprintf(stderr," Net=%hu\n",dest->net); fprintf(stderr, " Net=%hu\n", dest->net);
fprintf(stderr," Len=%d\n",dest->len); fprintf(stderr, " Len=%d\n", dest->len);
fprintf(stderr," Adr="); fprintf(stderr, " Adr=");
for (i = 0; i < MAX_MAC_LEN; i++) for (i = 0; i < MAX_MAC_LEN; i++) {
{ fprintf(stderr, "%02X ", (unsigned) dest->adr[i]);
fprintf(stderr,"%02X ",(unsigned)dest->adr[i]);
} }
fprintf(stderr,"\n"); fprintf(stderr, "\n");
} }
return; return;
+26 -30
View File
@@ -30,34 +30,33 @@
extern void RTEmuInit(void); extern void RTEmuInit(void);
#ifdef _MSC_VER #ifdef _MSC_VER
#define VOIDEXPORT _declspec(dllexport) void __cdecl #define VOIDEXPORT _declspec(dllexport) void __cdecl
#else #else
#define VOIDEXPORT void __export __cdecl #define VOIDEXPORT void __export __cdecl
#endif #endif
/* DISK SYSTEM */ /* DISK SYSTEM */
#ifdef DOC // include DiskOnChip driver #ifdef DOC // include DiskOnChip driver
#include <rtfiles.h> #include <rtfiles.h>
#define RTF_MAX_FILES 16 // support for more open files (default is 8) #define RTF_MAX_FILES 16 // support for more open files (default is 8)
#define RTF_BUFFERS_IN_BSS // we do not need file I/O before the run-time #define RTF_BUFFERS_IN_BSS // we do not need file I/O before the run-time
#include <rtfdata.c> // system is initialized #include <rtfdata.c> // system is initialized
//#define READ_HEAD_BUFFER_SIZE 2048+4 //#define READ_HEAD_BUFFER_SIZE 2048+4
//static BYTE ReadAheadBuffer[READ_HEAD_BUFFER_SIZE]; //static BYTE ReadAheadBuffer[READ_HEAD_BUFFER_SIZE];
static RTFDrvFLPYData FLPYDriveAData = {0}; static RTFDrvFLPYData FLPYDriveAData = { 0 };
static RTFDrvDOCData DOCDriveData = {0}; static RTFDrvDOCData DOCDriveData = { 0 };
static RTFDrvIDEData IDEDriveData = {0}; static RTFDrvIDEData IDEDriveData = { 0 };
RTFDevice RTFDeviceList[] = RTFDevice RTFDeviceList[] = {
{
/* type,number,flags,driver,driverdata */ /* type,number,flags,driver,driverdata */
{ RTF_DEVICE_FLOPPY, 0, 0, &RTFDrvFloppy, &FLPYDriveAData}, {RTF_DEVICE_FLOPPY, 0, 0, &RTFDrvFloppy, &FLPYDriveAData},
{ RTF_DEVICE_FDISK , 0, 0, &RTFDrvDOC, &DOCDriveData}, {RTF_DEVICE_FDISK, 0, 0, &RTFDrvDOC, &DOCDriveData},
{ RTF_DEVICE_FDISK , 0, 0, &RTFDrvIDE, &IDEDriveData}, {RTF_DEVICE_FDISK, 0, 0, &RTFDrvIDE, &IDEDriveData},
{ 0 , 0, 0, NULL, NULL} {0, 0, 0, NULL, NULL}
}; };
#endif #endif
/* END OF DISK SYSTEM */ /* END OF DISK SYSTEM */
@@ -67,13 +66,13 @@ extern void RTEmuInit(void);
#define MAXOBJECTS 1024 #define MAXOBJECTS 1024
#define MAXTYPES 32 #define MAXTYPES 32
RTW32Handle RTHandleTable[MAXHANDLES] = {{0}}; RTW32Handle RTHandleTable[MAXHANDLES] = { {0} };
int RTHandleCount = MAXHANDLES; int RTHandleCount = MAXHANDLES;
RTW32Object RTObjectTable[MAXOBJECTS] = {{0}}; RTW32Object RTObjectTable[MAXOBJECTS] = { {0} };
int RTObjectCount = MAXOBJECTS; int RTObjectCount = MAXOBJECTS;
RTW32Types RTTypeTable[MAXTYPES] = {{0}}; RTW32Types RTTypeTable[MAXTYPES] = { {0} };
int RTTypeCount = MAXTYPES; int RTTypeCount = MAXTYPES;
#if 0 #if 0
@@ -85,22 +84,19 @@ int RTTypeCount = MAXTYPES;
that here, as well as the LPT, console, and FAT. that here, as well as the LPT, console, and FAT.
From RTFiles-32 manual, ch. 7, "Using RTFiles-32 with From RTFiles-32 manual, ch. 7, "Using RTFiles-32 with
RTTarget-32" */ RTTarget-32" */
RTFileSystem Console = RTFileSystem Console = { RT_FS_CONSOLE, 0, 0, &RTConsoleFileSystem };
{ RT_FS_CONSOLE, 0, 0, &RTConsoleFileSystem };
RTFileSystem LPTFiles = RTFileSystem LPTFiles = { RT_FS_LPT_DEVICE, 0, 0, &RTLPTFileSystem };
{ RT_FS_LPT_DEVICE, 0, 0, &RTLPTFileSystem };
/* logical drive Z: can be used to access the RAM drive */ /* logical drive Z: can be used to access the RAM drive */
RTFileSystem RAMFiles = RTFileSystem RAMFiles =
{ RT_FS_FILE,1 << ('Z'-'A'), 0, &RTRAMFileSystem }; { RT_FS_FILE, 1 << ('Z' - 'A'), 0, &RTRAMFileSystem };
/* logical drive A: through D: are reserved for FAT */ /* logical drive A: through D: are reserved for FAT */
RTFileSystem FATFiles = RTFileSystem FATFiles =
{ RT_FS_FILE | RT_FS_IS_DEFAULT, 0x0F, 0x03, &RTFilesFileSystem }; { RT_FS_FILE | RT_FS_IS_DEFAULT, 0x0F, 0x03, &RTFilesFileSystem };
RTFileSystem *RTFileSystemList[] = RTFileSystem *RTFileSystemList[] = {
{
&Console, &Console,
&LPTFiles, &LPTFiles,
&RAMFiles, &RAMFiles,
@@ -112,8 +108,8 @@ RTFileSystem *RTFileSystemList[] =
/*-----------------------------------*/ /*-----------------------------------*/
VOIDEXPORT Init(void) VOIDEXPORT Init(void)
{ {
(void)RTSetFlags(RT_MM_VIRTUAL, 1); // this is the better method (void) RTSetFlags(RT_MM_VIRTUAL, 1); // this is the better method
(void)RTCMOSExtendHeap(); // get as much memory as we can (void) RTCMOSExtendHeap(); // get as much memory as we can
RTCMOSSetSystemTime(); // get the right date and time RTCMOSSetSystemTime(); // get the right date and time
RTEmuInit(); // set up floating point emulation RTEmuInit(); // set up floating point emulation
// pizza - RTHaltCPL3 appears to cause problems with file handling // pizza - RTHaltCPL3 appears to cause problems with file handling
@@ -124,5 +120,5 @@ VOIDEXPORT Init(void)
RTKConfig.Flags |= RF_WIN32MUTEX_MUTEX; // Win32 mutexes are RTK32 mutexes RTKConfig.Flags |= RF_WIN32MUTEX_MUTEX; // Win32 mutexes are RTK32 mutexes
RTKConfig.Flags |= RF_FPCONTEXT; // saves floating point context for tasks RTKConfig.Flags |= RF_FPCONTEXT; // saves floating point context for tasks
RTKConfig.HookedIRQs |= 1 << 1; // hook the keyboard IRQ RTKConfig.HookedIRQs |= 1 << 1; // hook the keyboard IRQ
RTKConfig.DefaultTaskStackSize = 1024*8; // for Win32 task stacks req = 0 RTKConfig.DefaultTaskStackSize = 1024 * 8; // for Win32 task stacks req = 0
} }
+28 -41
View File
@@ -37,7 +37,8 @@
#include "net.h" #include "net.h"
// buffers used for transmit and receive // buffers used for transmit and receive
static uint8_t Rx_Buf[MAX_MPDU] = {0}; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
#ifdef BACDL_MSTP #ifdef BACDL_MSTP
volatile struct mstp_port_struct_t MSTP_Port; // port data volatile struct mstp_port_struct_t MSTP_Port; // port data
static uint8_t MSTP_MAC_Address = 0x05; // local MAC address static uint8_t MSTP_MAC_Address = 0x05; // local MAC address
@@ -46,85 +47,71 @@ static uint8_t MSTP_MAC_Address = 0x05; // local MAC address
static void Init_Service_Handlers(void) static void Init_Service_Handlers(void)
{ {
// we need to handle who-is to support dynamic device binding // we need to handle who-is to support dynamic device binding
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, WhoIsHandler);
SERVICE_UNCONFIRMED_WHO_IS,
WhoIsHandler);
// set the handler for all the services we don't implement // set the handler for all the services we don't implement
// It is required to send the proper reject message... // It is required to send the proper reject message...
apdu_set_unrecognized_service_handler_handler( apdu_set_unrecognized_service_handler_handler
UnrecognizedServiceHandler); (UnrecognizedServiceHandler);
// we must implement read property - it's required! // we must implement read property - it's required!
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
ReadPropertyHandler); ReadPropertyHandler);
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
SERVICE_CONFIRMED_WRITE_PROPERTY,
WritePropertyHandler); WritePropertyHandler);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = { 0 }; // address where message came from
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; // milliseconds
(void)argc; (void) argc;
(void)argv; (void) argv;
Device_Set_Object_Instance_Number(126); Device_Set_Object_Instance_Number(126);
Init_Service_Handlers(); Init_Service_Handlers();
// init the physical layer // init the physical layer
#ifdef BACDL_BIP #ifdef BACDL_BIP
if (!bip_init()) if (!bip_init())
return 1; return 1;
#endif #endif
#ifdef BACDL_ETHERNET #ifdef BACDL_ETHERNET
if (!ethernet_init(NULL)) if (!ethernet_init(NULL))
return 1; return 1;
#endif #endif
#ifdef BACDL_MSTP #ifdef BACDL_MSTP
RS485_Initialize(); RS485_Initialize();
MSTP_Init(&MSTP_Port,MSTP_MAC_Address); MSTP_Init(&MSTP_Port, MSTP_MAC_Address);
#endif #endif
// loop forever // loop forever
for (;;) for (;;) {
{
// input // input
#ifdef BACDL_MSTP #ifdef BACDL_MSTP
MSTP_Millisecond_Timer(&MSTP_Port); MSTP_Millisecond_Timer(&MSTP_Port);
// note: also called by RS-485 Receive ISR // note: also called by RS-485 Receive ISR
RS485_Check_UART_Data(&MSTP_Port); RS485_Check_UART_Data(&MSTP_Port);
MSTP_Receive_Frame_FSM(&MSTP_Port); MSTP_Receive_Frame_FSM(&MSTP_Port);
#endif #endif
#if (defined(BACDL_ETHERNET) || defined(BACDL_BIP)) #if (defined(BACDL_ETHERNET) || defined(BACDL_BIP))
// returns 0 bytes on timeout // returns 0 bytes on timeout
pdu_len = bacdl_receive( pdu_len = bacdl_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
&src, #endif
&Rx_Buf[0],
MAX_MPDU,
timeout);
#endif
// process // process
if (pdu_len) if (pdu_len) {
{ npdu_handler(&src, &Rx_Buf[0], pdu_len);
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
} }
if (I_Am_Request) if (I_Am_Request) {
{
I_Am_Request = false; I_Am_Request = false;
Send_IAm(); Send_IAm();
} }
// output // output
#ifdef BACDL_MSTP #ifdef BACDL_MSTP
MSTP_Master_Node_FSM(&MSTP_Port); MSTP_Master_Node_FSM(&MSTP_Port);
#endif #endif
// blink LEDs, Turn on or off outputs, etc // blink LEDs, Turn on or off outputs, etc
} }
+11 -11
View File
@@ -35,17 +35,17 @@
#include <process.h> #include <process.h>
#ifdef BACDL_BIP #ifdef BACDL_BIP
#include "bip.h" #include "bip.h"
#ifndef HOST #ifndef HOST
#include "netcfg.h" #include "netcfg.h"
#include <rttarget.h> #include <rttarget.h>
#include <rtk32.h> #include <rtk32.h>
#include <clock.h> #include <clock.h>
#include <socket.h> #include <socket.h>
#else #else
#include <winsock.h> #include <winsock.h>
#endif #endif
#define close closesocket #define close closesocket
#endif #endif
#endif #endif
+34 -34
View File
@@ -32,26 +32,26 @@
// #define DHCP // if you enable this, you must also link library dhcpc.lib // #define DHCP // if you enable this, you must also link library dhcpc.lib
#if defined(AUTO_IP) // use xn_autoip() to get an IP address #if defined(AUTO_IP) // use xn_autoip() to get an IP address
static BYTE TargetIP[] = { 0, 0, 0, 0}; // will be filled at run-time static BYTE TargetIP[] = { 0, 0, 0, 0 }; // will be filled at run-time
static BYTE NetMask[] = {255, 255, 255, 0}; static BYTE NetMask[] = { 255, 255, 255, 0 };
static BYTE MinIP[] = {192, 168, 1, 128}; static BYTE MinIP[] = { 192, 168, 1, 128 };
static BYTE MaxIP[] = {192, 168, 1, 255}; static BYTE MaxIP[] = { 192, 168, 1, 255 };
static BYTE DefaultGateway[] = {192, 168, 1, 1}; // set to zero if not available or required static BYTE DefaultGateway[] = { 192, 168, 1, 1 }; // set to zero if not available or required
static BYTE DNSServer[] = {192, 168, 1, 1}; // ditto static BYTE DNSServer[] = { 192, 168, 1, 1 }; // ditto
#elif defined(DHCP) // use DHCP #elif defined(DHCP) // use DHCP
#include <dhcpcapi.h> #include <dhcpcapi.h>
static BYTE TargetIP[] = { 0, 0, 0, 0}; // will be filled at run-time static BYTE TargetIP[] = { 0, 0, 0, 0 }; // will be filled at run-time
#else // static IP address assignment (default) #else // static IP address assignment (default)
static BYTE TargetIP[] = {192, 168, 0, 50}; static BYTE TargetIP[] = { 192, 168, 0, 50 };
static BYTE NetMask[] = {255, 255, 255, 0}; static BYTE NetMask[] = { 255, 255, 255, 0 };
static BYTE DefaultGateway[] = {192, 168, 0, 1}; // set to zero if not available or required static BYTE DefaultGateway[] = { 192, 168, 0, 1 }; // set to zero if not available or required
static BYTE DNSServer[] = {192, 168, 0, 1}; // ditto static BYTE DNSServer[] = { 192, 168, 0, 1 }; // ditto
#endif #endif
#define DEVICE_ID DAVICOM_DEVICE // define your device type here #define DEVICE_ID DAVICOM_DEVICE // define your device type here
#ifndef DEVICE_ID #ifndef DEVICE_ID
#error You must define Ethernet driver/resources and IP address/net mask here #error You must define Ethernet driver/resources and IP address/net mask here
#endif #endif
// The following values are ignored for PCI devices (the BIOS supplies // The following values are ignored for PCI devices (the BIOS supplies
@@ -65,40 +65,40 @@
// Define function to pull in the required driver // Define function to pull in the required driver
#if DEVICE_ID == NE2000_DEVICE #if DEVICE_ID == NE2000_DEVICE
#define BIND_DRIVER xn_bind_ne2000 #define BIND_DRIVER xn_bind_ne2000
#elif DEVICE_ID == N83815_DEVICE #elif DEVICE_ID == N83815_DEVICE
#define BIND_DRIVER xn_bind_n83815 #define BIND_DRIVER xn_bind_n83815
#elif DEVICE_ID == TC90X_DEVICE #elif DEVICE_ID == TC90X_DEVICE
#define BIND_DRIVER xn_bind_tc90x #define BIND_DRIVER xn_bind_tc90x
#elif DEVICE_ID == SMC91C9X_DEVICE #elif DEVICE_ID == SMC91C9X_DEVICE
#define BIND_DRIVER xn_bind_smc91c9x #define BIND_DRIVER xn_bind_smc91c9x
#elif DEVICE_ID == LANCE_DEVICE #elif DEVICE_ID == LANCE_DEVICE
#define BIND_DRIVER xn_bind_rtlance #define BIND_DRIVER xn_bind_rtlance
#elif DEVICE_ID == LANCE_ISA_DEVICE #elif DEVICE_ID == LANCE_ISA_DEVICE
#define BIND_DRIVER xn_bind_lance_isa #define BIND_DRIVER xn_bind_lance_isa
#elif DEVICE_ID == LAN_CS89X0_DEVICE #elif DEVICE_ID == LAN_CS89X0_DEVICE
#define BIND_DRIVER xn_bind_cs #define BIND_DRIVER xn_bind_cs
#elif DEVICE_ID == I82559_DEVICE #elif DEVICE_ID == I82559_DEVICE
#define BIND_DRIVER xn_bind_i82559 #define BIND_DRIVER xn_bind_i82559
#elif DEVICE_ID == R8139_DEVICE #elif DEVICE_ID == R8139_DEVICE
#define BIND_DRIVER xn_bind_r8139 #define BIND_DRIVER xn_bind_r8139
#elif DEVICE_ID == DAVICOM_DEVICE #elif DEVICE_ID == DAVICOM_DEVICE
#define BIND_DRIVER xn_bind_davicom #define BIND_DRIVER xn_bind_davicom
#elif DEVICE_ID == RHINE_DEVICE #elif DEVICE_ID == RHINE_DEVICE
#define BIND_DRIVER xn_bind_rhine #define BIND_DRIVER xn_bind_rhine
#elif DEVICE_ID == AX172_DEVICE #elif DEVICE_ID == AX172_DEVICE
#include <rtusb.h> // must also link Rtusb.lib and UsbInit.cpp #include <rtusb.h> // must also link Rtusb.lib and UsbInit.cpp
#define BIND_DRIVER xn_bind_ax172 #define BIND_DRIVER xn_bind_ax172
#elif DEVICE_ID == AX772_DEVICE #elif DEVICE_ID == AX772_DEVICE
#include <rtusb.h> // must also link Rtusb.lib and UsbInit.cpp #include <rtusb.h> // must also link Rtusb.lib and UsbInit.cpp
#define BIND_DRIVER xn_bind_ax772 #define BIND_DRIVER xn_bind_ax772
#elif DEVICE_ID == PRISM_DEVICE #elif DEVICE_ID == PRISM_DEVICE
#include <wlanapi.h> // must also link Wlan.lib #include <wlanapi.h> // must also link Wlan.lib
#define BIND_DRIVER xn_bind_prism #define BIND_DRIVER xn_bind_prism
#elif DEVICE_ID == PRISM_PCMCIA_DEVICE #elif DEVICE_ID == PRISM_PCMCIA_DEVICE
#include <rtpcmcia.h> #include <rtpcmcia.h>
#include <wlanapi.h> // must also link Wlan.lib #include <wlanapi.h> // must also link Wlan.lib
#define BIND_DRIVER xn_bind_prism_pcmcia #define BIND_DRIVER xn_bind_prism_pcmcia
#else #else
#error Invalid DEVICE_ID value #error Invalid DEVICE_ID value
#endif #endif
+25 -32
View File
@@ -44,24 +44,24 @@ static long RS485_Base = 0;
/* hardware IRQ number */ /* hardware IRQ number */
static long RS485_IRQ_Number = 0; static long RS485_IRQ_Number = 0;
static void RS485_Standard_Port_Settings(long port, long *pIRQ, long *pBase) static void RS485_Standard_Port_Settings(long port, long *pIRQ,
long *pBase)
{ {
switch (port) switch (port) {
{
case COM1: case COM1:
*pBase = (long)0x3F8; *pBase = (long) 0x3F8;
*pIRQ = 4L; *pIRQ = 4L;
break; break;
case COM2: case COM2:
*pBase = (long)0x2F8; *pBase = (long) 0x2F8;
*pIRQ = 3L; *pIRQ = 3L;
break; break;
case COM3: case COM3:
*pBase = (long)0x3E8; *pBase = (long) 0x3E8;
*pIRQ = 4L; *pIRQ = 4L;
break; break;
case COM4: case COM4:
*pBase = (long)0x2E8; *pBase = (long) 0x2E8;
*pIRQ = 3L; *pIRQ = 3L;
break; break;
default: default:
@@ -69,24 +69,23 @@ static void RS485_Standard_Port_Settings(long port, long *pIRQ, long *pBase)
} }
} }
static RS485_Open_Port( static RS485_Open_Port(int port, /* COM port number - COM1 = 0 */
int port, /* COM port number - COM1 = 0 */
long baud, /* baud rate */ long baud, /* baud rate */
unsigned base, /* io base address */ unsigned base, /* io base address */
int irq) /* hardware IRQ number */ int irq)
{ { /* hardware IRQ number */
/* setup the COM IO */ /* setup the COM IO */
SetIOBase(port, base); SetIOBase(port, base);
SetIRQ(port, irq); SetIRQ(port, irq);
if (irq < 8) if (irq < 8)
RTKIRQTopPriority(irq,9); RTKIRQTopPriority(irq, 9);
InitPort(port, baud, PARITY_NONE, 1, 8); InitPort(port, baud, PARITY_NONE, 1, 8);
if (HasFIFO(port)) if (HasFIFO(port))
EnableFIFO(port,8); EnableFIFO(port, 8);
EnableCOMInterrupt(port, 1024*4); EnableCOMInterrupt(port, 1024 * 4);
/* enable the 485 via the DTR pin */ /* enable the 485 via the DTR pin */
RS485_IO_ENABLE(port); RS485_IO_ENABLE(port);
@@ -97,22 +96,22 @@ static RS485_Open_Port(
void RS485_Initialize(void) void RS485_Initialize(void)
{ {
RS485_Standard_Port_Settings(RS485_Port, &RS485_IRQ_Number, &RS485_Base); RS485_Standard_Port_Settings(RS485_Port, &RS485_IRQ_Number,
&RS485_Base);
RS485_Open_Port(RS485_Port, RS485_Baud, RS485_Base, RS485_IRQ_Number); RS485_Open_Port(RS485_Port, RS485_Baud, RS485_Base, RS485_IRQ_Number);
} }
void RS485_Send_Frame( void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, // port specific data
volatile struct mstp_port_struct_t *mstp_port, // port specific data uint8_t * buffer, // frame to send (up to 501 bytes of data)
uint8_t *buffer, // frame to send (up to 501 bytes of data)
uint16_t nbytes) // number of bytes of data (up to 501) uint16_t nbytes) // number of bytes of data (up to 501)
{ {
bool status = true; // return value bool status = true; // return value
(void)mstp_port; (void) mstp_port;
RS485_TRANSMIT_ENABLE(RS485_Port); RS485_TRANSMIT_ENABLE(RS485_Port);
SendBlock(RS485_Port, (char *)buffer, nbytes); SendBlock(RS485_Port, (char *) buffer, nbytes);
/* need to wait at least 9600 baud * 512 bytes = 54mS */ /* need to wait at least 9600 baud * 512 bytes = 54mS */
(void)WaitSendBufferEmpty(RS485_Port,MilliSecsToTicks(200)); (void) WaitSendBufferEmpty(RS485_Port, MilliSecsToTicks(200));
while (!(LineStatus(RS485_Port) & TX_SHIFT_EMPTY)) while (!(LineStatus(RS485_Port) & TX_SHIFT_EMPTY))
RTKScheduler(); RTKScheduler();
RS485_RECEIVE_ENABLE(RS485_Port); RS485_RECEIVE_ENABLE(RS485_Port);
@@ -120,33 +119,28 @@ void RS485_Send_Frame(
return; return;
} }
void RS485_Check_UART_Data( void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) // port specific data
volatile struct mstp_port_struct_t *mstp_port) // port specific data
{ {
COMData com_data = 0; /* byte from COM driver */ COMData com_data = 0; /* byte from COM driver */
unsigned timeout = 10; // milliseconds to wait for a character unsigned timeout = 10; // milliseconds to wait for a character
Duration ticks; /* duration to wait for data */ Duration ticks; /* duration to wait for data */
if (mstp_port->ReceiveError) if (mstp_port->ReceiveError) {
{
// wait for state machine to clear this // wait for state machine to clear this
} }
// wait for state machine to read from the DataRegister // wait for state machine to read from the DataRegister
else if (!mstp_port->DataAvailable) else if (!mstp_port->DataAvailable) {
{
// check for data // check for data
ticks = MilliSecsToTicks(timeout); ticks = MilliSecsToTicks(timeout);
if (!ticks) if (!ticks)
ticks = 1; ticks = 1;
if (RTKGetTimed(ReceiveBuffer[RS485_Port],&com_data,ticks)) if (RTKGetTimed(ReceiveBuffer[RS485_Port], &com_data, ticks)) {
{
// if error, // if error,
if (com_data & (COM_OVERRUN << 8)) if (com_data & (COM_OVERRUN << 8))
mstp_port->ReceiveError = true; mstp_port->ReceiveError = true;
else if (com_data & (COM_FRAME << 8)) else if (com_data & (COM_FRAME << 8))
mstp_port->ReceiveError = true; mstp_port->ReceiveError = true;
else else {
{
mstp_port->DataRegister = com_data & 0x00FF; mstp_port->DataRegister = com_data & 0x00FF;
mstp_port->DataAvailable = true; mstp_port->DataAvailable = true;
} }
@@ -158,4 +152,3 @@ void RS485_Process_Tx_Message(void)
{ {
// nothing to do // nothing to do
} }
+13 -13
View File
@@ -4,25 +4,25 @@
// C99 Boolean types for compilers without C99 support // C99 Boolean types for compilers without C99 support
#ifndef __cplusplus #ifndef __cplusplus
typedef int _Bool; typedef int _Bool;
#ifndef bool #ifndef bool
#define bool _Bool #define bool _Bool
#endif #endif
#ifndef true #ifndef true
#define true 1 #define true 1
#endif #endif
#ifndef false #ifndef false
#define false 0 #define false 0
#endif #endif
#define __bool_true_false_are_defined 1 #define __bool_true_false_are_defined 1
#endif #endif
#ifndef FALSE #ifndef FALSE
#define FALSE 0 #define FALSE 0
#endif #endif
#ifndef TRUE #ifndef TRUE
#define TRUE 1 #define TRUE 1
#endif #endif
#endif #endif
+39 -46
View File
@@ -51,13 +51,13 @@ static long gethostaddr(void)
if (gethostname(host_name, sizeof(host_name)) != 0) if (gethostname(host_name, sizeof(host_name)) != 0)
return -1; return -1;
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
printf("host name: %s\n",host_name); printf("host name: %s\n", host_name);
#endif #endif
if ((host_ent = gethostbyname(host_name)) == NULL) if ((host_ent = gethostbyname(host_name)) == NULL)
return -1; return -1;
return *(long *)host_ent->h_addr; return *(long *) host_ent->h_addr;
} }
static void set_broadcast_address(uint32_t net_address) static void set_broadcast_address(uint32_t net_address)
@@ -67,22 +67,26 @@ static void set_broadcast_address(uint32_t net_address)
/* Note: sometimes INADDR_BROADCAST does not let me get /* Note: sometimes INADDR_BROADCAST does not let me get
any unicast messages. Not sure why... */ any unicast messages. Not sure why... */
#if USE_INADDR #if USE_INADDR
(void)net_address; (void) net_address;
bip_set_broadcast_addr(INADDR_BROADCAST); bip_set_broadcast_addr(INADDR_BROADCAST);
#else #else
if (IN_CLASSA(ntohl(net_address))) if (IN_CLASSA(ntohl(net_address)))
broadcast_address = (ntohl(net_address) & ~IN_CLASSA_HOST) | IN_CLASSA_HOST; broadcast_address =
(ntohl(net_address) & ~IN_CLASSA_HOST) | IN_CLASSA_HOST;
else if (IN_CLASSB(ntohl(net_address))) else if (IN_CLASSB(ntohl(net_address)))
broadcast_address = (ntohl(net_address) & ~IN_CLASSB_HOST) | IN_CLASSB_HOST; broadcast_address =
(ntohl(net_address) & ~IN_CLASSB_HOST) | IN_CLASSB_HOST;
else if (IN_CLASSC(ntohl(net_address))) else if (IN_CLASSC(ntohl(net_address)))
broadcast_address = (ntohl(net_address) & ~IN_CLASSC_HOST) | IN_CLASSC_HOST; broadcast_address =
(ntohl(net_address) & ~IN_CLASSC_HOST) | IN_CLASSC_HOST;
else if (IN_CLASSD(ntohl(net_address))) else if (IN_CLASSD(ntohl(net_address)))
broadcast_address = (ntohl(net_address) & ~IN_CLASSD_HOST) | IN_CLASSD_HOST; broadcast_address =
(ntohl(net_address) & ~IN_CLASSD_HOST) | IN_CLASSD_HOST;
else else
broadcast_address = INADDR_BROADCAST; broadcast_address = INADDR_BROADCAST;
bip_set_broadcast_addr(htonl(broadcast_address)); bip_set_broadcast_addr(htonl(broadcast_address));
#endif #endif
} }
static void cleanup(void) static void cleanup(void)
@@ -92,14 +96,14 @@ static void cleanup(void)
void bip_set_interface(char *ifname) void bip_set_interface(char *ifname)
{ {
(void)ifname; (void) ifname;
/* dummy function */ /* dummy function */
} }
bool bip_init(void) bool bip_init(void)
{ {
int rv = 0; // return from socket lib calls int rv = 0; // return from socket lib calls
struct sockaddr_in sin = {-1}; struct sockaddr_in sin = { -1 };
int value = 1; int value = 1;
int sock_fd = -1; int sock_fd = -1;
int Result; int Result;
@@ -109,8 +113,7 @@ bool bip_init(void)
Result = WSAStartup((1 << 8) | 1, &wd); Result = WSAStartup((1 << 8) | 1, &wd);
//Result = WSAStartup(MAKEWORD(2,2), &wd); //Result = WSAStartup(MAKEWORD(2,2), &wd);
if (Result != 0) if (Result != 0) {
{
Code = WSAGetLastError(); Code = WSAGetLastError();
printf("TCP/IP stack initialization failed, error code: %i\n", printf("TCP/IP stack initialization failed, error code: %i\n",
Code); Code);
@@ -119,53 +122,46 @@ bool bip_init(void)
atexit(cleanup); atexit(cleanup);
address.s_addr = gethostaddr(); address.s_addr = gethostaddr();
if (address.s_addr == (unsigned)-1) if (address.s_addr == (unsigned) -1) {
{
Code = WSAGetLastError(); Code = WSAGetLastError();
printf("Get host address failed, error code: %i\n", printf("Get host address failed, error code: %i\n", Code);
Code);
exit(1); exit(1);
} }
#ifdef BIP_DEBUG #ifdef BIP_DEBUG
printf("host address: %s\n",inet_ntoa(address)); printf("host address: %s\n", inet_ntoa(address));
#endif #endif
bip_set_addr(address.s_addr); bip_set_addr(address.s_addr);
set_broadcast_address(address.s_addr); set_broadcast_address(address.s_addr);
/* assumes that the driver has already been initialized */ /* assumes that the driver has already been initialized */
sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
bip_set_socket(sock_fd); bip_set_socket(sock_fd);
if (sock_fd < 0) if (sock_fd < 0) {
{ fprintf(stderr, "bip: failed to allocate a socket.\n");
fprintf(stderr,"bip: failed to allocate a socket.\n");
return false; return false;
} }
// Allow us to use the same socket for sending and receiving // Allow us to use the same socket for sending and receiving
// This makes sure that the src port is correct when sending // This makes sure that the src port is correct when sending
rv = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, rv = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR,
(char *)&value, sizeof(value)); (char *) &value, sizeof(value));
if (rv < 0) if (rv < 0) {
{ fprintf(stderr, "bip: failed to set REUSEADDR socket option.\n");
fprintf(stderr,"bip: failed to set REUSEADDR socket option.\n");
close(sock_fd); close(sock_fd);
bip_set_socket(-1); bip_set_socket(-1);
return false; return false;
} }
// allow us to send a broadcast // allow us to send a broadcast
rv = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, rv = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST,
(char *)&value, sizeof(value)); (char *) &value, sizeof(value));
if (rv < 0) if (rv < 0) {
{ fprintf(stderr, "bip: failed to set BROADCAST socket option.\n");
fprintf(stderr,"bip: failed to set BROADCAST socket option.\n");
close(sock_fd); close(sock_fd);
bip_set_socket(-1); bip_set_socket(-1);
return false; return false;
} }
// bind the socket to the local port number and IP address // bind the socket to the local port number and IP address
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
#if USE_INADDR #if USE_INADDR
/* by setting sin.sin_addr.s_addr to INADDR_ANY, /* by setting sin.sin_addr.s_addr to INADDR_ANY,
I am telling the IP stack to automatically fill I am telling the IP stack to automatically fill
in the IP address of the machine the process in the IP address of the machine the process
@@ -182,20 +178,18 @@ bool bip_init(void)
Note: sometimes INADDR_ANY does not let me get Note: sometimes INADDR_ANY does not let me get
any unicast messages. Not sure why... */ any unicast messages. Not sure why... */
sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_addr.s_addr = htonl(INADDR_ANY);
#else #else
/* or we could use the specific adapter address /* or we could use the specific adapter address
note: already in network byte order */ note: already in network byte order */
sin.sin_addr.s_addr = address.s_addr; sin.sin_addr.s_addr = address.s_addr;
#endif #endif
sin.sin_port = htons(bip_get_port()); sin.sin_port = htons(bip_get_port());
memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero)); memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
rv = bind(sock_fd, rv = bind(sock_fd,
(const struct sockaddr*)&sin, sizeof(struct sockaddr)); (const struct sockaddr *) &sin, sizeof(struct sockaddr));
if (rv < 0) if (rv < 0) {
{ fprintf(stderr, "bip: failed to bind to %s port %hd\n",
fprintf(stderr,"bip: failed to bind to %s port %hd\n", inet_ntoa(sin.sin_addr), bip_get_port());
inet_ntoa(sin.sin_addr),
bip_get_port());
close(sock_fd); close(sock_fd);
bip_set_socket(-1); bip_set_socket(-1);
return false; return false;
@@ -203,4 +197,3 @@ bool bip_init(void)
return true; return true;
} }
+58 -101
View File
@@ -43,7 +43,8 @@
#include "txbuf.h" #include "txbuf.h"
// buffer used for receive // buffer used for receive
static uint8_t Rx_Buf[MAX_MPDU] = {0}; static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* send a whois to see who is on the network */ /* send a whois to see who is on the network */
static bool Who_Is_Request = true; static bool Who_Is_Request = true;
@@ -60,8 +61,7 @@ static void Read_Properties(void)
Device Object Device Object
note: you could just loop through note: you could just loop through
all the properties in all the objects. */ all the properties in all the objects. */
const int object_props[] = const int object_props[] = {
{
PROP_OBJECT_IDENTIFIER, PROP_OBJECT_IDENTIFIER,
PROP_OBJECT_NAME, PROP_OBJECT_NAME,
PROP_OBJECT_TYPE, PROP_OBJECT_TYPE,
@@ -92,33 +92,25 @@ static void Read_Properties(void)
we need to get it with an index method since we need to get it with an index method since
the list could be very large */ the list could be very large */
/* some proprietary properties */ /* some proprietary properties */
514,515, 514, 515,
/* end of list */ /* end of list */
-1 -1
}; };
if (address_count()) if (address_count()) {
{ if (address_get_by_index(index, &device_id, &max_apdu, &src)) {
if (address_get_by_index(index, &device_id, &max_apdu, &src))
{
if (object_props[property] < 0) if (object_props[property] < 0)
next_device = true; next_device = true;
else else {
{ status = Send_Read_Property_Request(device_id, // destination device
status = Send_Read_Property_Request(
device_id, // destination device
OBJECT_DEVICE, OBJECT_DEVICE,
device_id, device_id, object_props[property], BACNET_ARRAY_ALL);
object_props[property],
BACNET_ARRAY_ALL);
if (status) if (status)
property++; property++;
} }
} } else
else
next_device = true; next_device = true;
if (next_device) if (next_device) {
{
next_device = false; next_device = false;
index++; index++;
if (index >= MAX_ADDRESS_CACHE) if (index >= MAX_ADDRESS_CACHE)
@@ -130,10 +122,8 @@ static void Read_Properties(void)
return; return;
} }
static void LocalIAmHandler( static void LocalIAmHandler(uint8_t * service_request,
uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS * src)
uint16_t service_len,
BACNET_ADDRESS *src)
{ {
int len = 0; int len = 0;
uint32_t device_id = 0; uint32_t device_id = 0;
@@ -141,24 +131,16 @@ static void LocalIAmHandler(
int segmentation = 0; int segmentation = 0;
uint16_t vendor_id = 0; uint16_t vendor_id = 0;
(void)src; (void) src;
(void)service_len; (void) service_len;
len = iam_decode_service_request( len = iam_decode_service_request(service_request,
service_request, &device_id, &max_apdu, &segmentation, &vendor_id);
&device_id, fprintf(stderr, "Received I-Am Request");
&max_apdu, if (len != -1) {
&segmentation, fprintf(stderr, " from %u!\n", device_id);
&vendor_id); address_add(device_id, max_apdu, src);
fprintf(stderr,"Received I-Am Request"); } else
if (len != -1) fprintf(stderr, "!\n");
{
fprintf(stderr," from %u!\n",device_id);
address_add(device_id,
max_apdu,
src);
}
else
fprintf(stderr,"!\n");
return; return;
} }
@@ -166,42 +148,33 @@ static void LocalIAmHandler(
static void Init_Service_Handlers(void) static void Init_Service_Handlers(void)
{ {
// we need to handle who-is to support dynamic device binding // we need to handle who-is to support dynamic device binding
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is); handler_who_is);
apdu_set_unconfirmed_handler( apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler); LocalIAmHandler);
// set the handler for all the services we don't implement // set the handler for all the services we don't implement
// It is required to send the proper reject message... // It is required to send the proper reject message...
apdu_set_unrecognized_service_handler_handler( apdu_set_unrecognized_service_handler_handler
handler_unrecognized_service); (handler_unrecognized_service);
// we must implement read property - it's required! // we must implement read property - it's required!
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property); handler_read_property);
apdu_set_confirmed_handler( apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
SERVICE_CONFIRMED_WRITE_PROPERTY,
handler_write_property); handler_write_property);
// handle the data coming back from confirmed requests // handle the data coming back from confirmed requests
apdu_set_confirmed_ack_handler( apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY,
SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property_ack); handler_read_property_ack);
} }
static void print_address( static void print_address(char *name, BACNET_ADDRESS * dest) // destination address
char *name,
BACNET_ADDRESS *dest) // destination address
{ {
int i = 0; // counter int i = 0; // counter
if (dest) if (dest) {
{ printf("%s: ", name);
printf("%s: ",name); for (i = 0; i < dest->mac_len; i++) {
for (i = 0; i < dest->mac_len; i++) printf("%02X", dest->mac[i]);
{
printf("%02X",dest->mac[i]);
} }
printf("\n"); printf("\n");
} }
@@ -209,37 +182,34 @@ static void print_address(
static void print_address_cache(void) static void print_address_cache(void)
{ {
int i,j; int i, j;
BACNET_ADDRESS address; BACNET_ADDRESS address;
uint32_t device_id = 0; uint32_t device_id = 0;
unsigned max_apdu = 0; unsigned max_apdu = 0;
fprintf(stderr,"Device\tMAC\tMaxAPDU\tNet\n"); fprintf(stderr, "Device\tMAC\tMaxAPDU\tNet\n");
for (i = 0; i < MAX_ADDRESS_CACHE; i++) for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
{ if (address_get_by_index(i, &device_id, &max_apdu, &address)) {
if (address_get_by_index(i,&device_id, &max_apdu, &address)) fprintf(stderr, "%u\t", device_id);
{ for (j = 0; j < address.mac_len; j++) {
fprintf(stderr,"%u\t",device_id); fprintf(stderr, "%02X", address.mac[j]);
for (j = 0; j < address.mac_len; j++)
{
fprintf(stderr,"%02X",address.mac[j]);
} }
fprintf(stderr,"\t"); fprintf(stderr, "\t");
fprintf(stderr,"%hu\t",max_apdu); fprintf(stderr, "%hu\t", max_apdu);
fprintf(stderr,"%hu\n",address.net); fprintf(stderr, "%hu\n", address.net);
} }
} }
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BACNET_ADDRESS src = {0}; // address where message came from BACNET_ADDRESS src = { 0 }; // address where message came from
uint16_t pdu_len = 0; uint16_t pdu_len = 0;
unsigned timeout = 100; // milliseconds unsigned timeout = 100; // milliseconds
BACNET_ADDRESS my_address, broadcast_address; BACNET_ADDRESS my_address, broadcast_address;
(void)argc; (void) argc;
(void)argv; (void) argv;
Device_Set_Object_Instance_Number(124); Device_Set_Object_Instance_Number(124);
Init_Service_Handlers(); Init_Service_Handlers();
// init the data link layer // init the data link layer
@@ -249,43 +219,30 @@ int main(int argc, char *argv[])
return 1; return 1;
datalink_get_broadcast_address(&broadcast_address); datalink_get_broadcast_address(&broadcast_address);
print_address("Broadcast",&broadcast_address); print_address("Broadcast", &broadcast_address);
datalink_get_my_address(&my_address); datalink_get_my_address(&my_address);
print_address("Address",&my_address); print_address("Address", &my_address);
printf("BACnet stack running...\n"); printf("BACnet stack running...\n");
// loop forever // loop forever
for (;;) for (;;) {
{
// input // input
// returns 0 bytes on timeout // returns 0 bytes on timeout
pdu_len = bip_receive( pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
// process // process
if (pdu_len) if (pdu_len) {
{ npdu_handler(&src, &Rx_Buf[0], pdu_len);
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
} }
if (I_Am_Request) if (I_Am_Request) {
{
I_Am_Request = false; I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]); iam_send(&Handler_Transmit_Buffer[0]);
} else if (Who_Is_Request) } else if (Who_Is_Request) {
{
Who_Is_Request = false; Who_Is_Request = false;
Send_WhoIs(-1,-1); Send_WhoIs(-1, -1);
} } else {
else
{
Read_Properties(); Read_Properties();
} }
+13 -13
View File
@@ -4,25 +4,25 @@
// C99 Boolean types for compilers without C99 support // C99 Boolean types for compilers without C99 support
#ifndef __cplusplus #ifndef __cplusplus
typedef int _Bool; typedef int _Bool;
#ifndef bool #ifndef bool
#define bool _Bool #define bool _Bool
#endif #endif
#ifndef true #ifndef true
#define true 1 #define true 1
#endif #endif
#ifndef false #ifndef false
#define false 0 #define false 0
#endif #endif
#define __bool_true_false_are_defined 1 #define __bool_true_false_are_defined 1
#endif #endif
#ifndef FALSE #ifndef FALSE
#define FALSE 0 #define FALSE 0
#endif #endif
#ifndef TRUE #ifndef TRUE
#define TRUE 1 #define TRUE 1
#endif #endif
#endif #endif