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
+15 -33
View File
@@ -38,60 +38,42 @@ extern "C" {
#endif /* __cplusplus */
/* unconfirmed requests */
void Send_WhoIs(
int32_t low_limit,
int32_t high_limit);
void Send_WhoIs(int32_t low_limit, int32_t high_limit);
void Send_WhoHas_Object(
int32_t low_limit,
void Send_WhoHas_Object(int32_t low_limit,
int32_t high_limit,
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance);
BACNET_OBJECT_TYPE object_type, uint32_t object_instance);
void Send_WhoHas_Name(
int32_t low_limit,
int32_t high_limit,
char *object_name);
void Send_WhoHas_Name(int32_t low_limit,
int32_t high_limit, char *object_name);
void Send_I_Have(
uint32_t device_id,
void Send_I_Have(uint32_t device_id,
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance,
char *object_name);
uint32_t object_instance, char *object_name);
/* returns the invoke ID for confirmed request, or 0 if failed */
uint8_t Send_Read_Property_Request(
uint32_t device_id, /* destination device */
uint8_t Send_Read_Property_Request(uint32_t device_id, /* destination device */
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance,
BACNET_PROPERTY_ID object_property,
int32_t array_index);
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
uint8_t Send_Write_Property_Request(uint32_t device_id, // destination device
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);
uint8_t priority, int32_t array_index);
/* returns the invoke ID for confirmed request, or 0 if failed */
uint8_t Send_Reinitialize_Device_Request(
uint32_t device_id,
BACNET_REINITIALIZED_STATE state,
char *password);
uint8_t Send_Reinitialize_Device_Request(uint32_t device_id,
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 */
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
}
#endif /* __cplusplus */
#endif
+27 -61
View File
@@ -41,11 +41,9 @@
#include "ao.h"
#include "bacfile.h"
void handler_atomic_read_file(
uint8_t *service_request,
void handler_atomic_read_file(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_DATA *service_data)
BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data)
{
BACNET_ATOMIC_READ_FILE_DATA data;
int len = 0;
@@ -58,96 +56,64 @@ void handler_atomic_read_file(
BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT;
fprintf(stderr, "Received Atomic-Read-File Request!\n");
len = arf_decode_service_request(
service_request,
service_len,
&data);
len = arf_decode_service_request(service_request, service_len, &data);
if (len < 0)
fprintf(stderr, "Unable to decode Atomic-Read-File Request!\n");
// prepare a reply
datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], src, &my_address, false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
// bad decoding - send an abort
if (len < 0)
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
ABORT_REASON_OTHER);
if (len < 0) {
pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_OTHER);
fprintf(stderr, "Sending Abort!\n");
send = true;
}
else if (service_data->segmented_message)
{
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,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr, "Sending Abort!\n");
send = true;
}
else
{
if (data.access == FILE_STREAM_ACCESS)
{
} else {
if (data.access == FILE_STREAM_ACCESS) {
if (data.type.stream.requestedOctetCount <
octetstring_capacity(&data.fileData))
{
if (bacfile_read_data(&data))
{
pdu_len += arf_ack_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
&data);
octetstring_capacity(&data.fileData)) {
if (bacfile_read_data(&data)) {
pdu_len +=
arf_ack_encode_apdu(&Handler_Transmit_Buffer
[pdu_len], service_data->invoke_id, &data);
send = true;
}
else
{
} else {
send = true;
error = true;
}
}
else
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
} else {
pdu_len +=
abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr, "Sending Abort!\n");
send = true;
}
}
else
{
} else {
error_class = ERROR_CLASS_SERVICES;
error_code = ERROR_CODE_INVALID_FILE_ACCESS_METHOD;
send = true;
error = true;
}
}
if (error)
{
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
if (error) {
pdu_len += bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
error_class,
error_code);
SERVICE_CONFIRMED_ATOMIC_READ_FILE, error_class, error_code);
fprintf(stderr, "Sending Error!\n");
send = true;
}
if (send)
{
bytes_sent = datalink_send_pdu(
src, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (send) {
bytes_sent = datalink_send_pdu(src, // destination address
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
if (bytes_sent <= 0)
fprintf(stderr, "Failed to send PDU (%s)!\n", strerror(errno));
}
+12 -24
View File
@@ -47,11 +47,9 @@
// that someone can read from us. It is common to
// use the description as the file name.
#if BACFILE
void handler_atomic_read_file_ack(
uint8_t *service_request,
void handler_atomic_read_file_ack(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_ACK_DATA *service_data)
BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data)
{
int len = 0;
BACNET_ATOMIC_READ_FILE_DATA data;
@@ -62,39 +60,29 @@ void handler_atomic_read_file_ack(
(void) src;
// get the file instance from the tsm data before freeing it
instance = bacfile_instance_from_tsm(service_data->invoke_id);
len = arf_ack_decode_service_request(
service_request,
service_len,
&data);
len = arf_ack_decode_service_request(service_request,
service_len, &data);
fprintf(stderr, "Received Read-File Ack!\n");
if ((len > 0) && (instance <= BACNET_MAX_INSTANCE))
{
if ((len > 0) && (instance <= BACNET_MAX_INSTANCE)) {
// write the data received to the file specified
if (data.access == FILE_STREAM_ACCESS)
{
if (data.access == FILE_STREAM_ACCESS) {
pFilename = bacfile_name(instance);
if (pFilename)
{
if (pFilename) {
pFile = fopen(pFilename, "rb");
if (pFile)
{
if (pFile) {
(void) fseek(pFile,
data.type.stream.fileStartPosition,
SEEK_SET);
data.type.stream.fileStartPosition, SEEK_SET);
if (fwrite(octetstring_value(&data.fileData),
octetstring_length(&data.fileData),1,pFile) != 1)
octetstring_length(&data.fileData), 1,
pFile) != 1)
fprintf(stderr, "Failed to write to %s (%u)!\n",
pFilename, instance);
fclose(pFile);
}
}
}
else if (data.access == FILE_RECORD_ACCESS)
{
} else if (data.access == FILE_RECORD_ACCESS) {
// FIXME: add handling for Record Access
}
}
}
#endif
+26 -56
View File
@@ -40,11 +40,9 @@
static char *My_Password = "AnnaRoseKarg";
void handler_device_communication_control(
uint8_t *service_request,
void handler_device_communication_control(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_DATA *service_data)
BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data)
{
uint16_t timeDuration = 0;
BACNET_COMMUNICATION_ENABLE_DISABLE state = COMMUNICATION_ENABLE;
@@ -55,91 +53,63 @@ void handler_device_communication_control(
int bytes_sent = 0;
// decode the service request only
len = dcc_decode_service_request(
service_request,
service_len,
&timeDuration,
&state,
&password);
len = dcc_decode_service_request(service_request,
service_len, &timeDuration, &state, &password);
fprintf(stderr, "DeviceCommunicationControl!\n");
if (len > 0)
fprintf(stderr, "DeviceCommunicationControl: "
"timeout=%u state=%u password=%s\n",
(unsigned) timeDuration,
(unsigned)state,
characterstring_value(&password));
(unsigned) state, characterstring_value(&password));
else
fprintf(stderr, "DeviceCommunicationControl: "
"Unable to decode request!\n");
// prepare a reply
datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], src, &my_address, false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
// bad decoding or something we didn't understand - send an abort
if (len == -1)
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
ABORT_REASON_OTHER);
if (len == -1) {
pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_OTHER);
fprintf(stderr, "DeviceCommunicationControl: "
"Sending Abort - could not decode.\n");
}
else if (service_data->segmented_message)
{
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,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr, "DeviceCommunicationControl: "
"Sending Abort - segmented message.\n");
}
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);
} 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);
fprintf(stderr, "DeviceCommunicationControl: "
"Sending Reject - undefined enumeration\n");
}
else
{
if (characterstring_ansi_same(&password,My_Password))
{
pdu_len += encode_simple_ack(
&Handler_Transmit_Buffer[pdu_len],
} else {
if (characterstring_ansi_same(&password, My_Password)) {
pdu_len += encode_simple_ack(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL);
fprintf(stderr, "DeviceCommunicationControl: "
"Sending Simple Ack!\n");
dcc_set_status_duration(state, timeDuration);
}
else
{
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
} else {
pdu_len +=
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
ERROR_CLASS_SERVICES,
ERROR_CODE_PASSWORD_FAILURE);
fprintf(stderr,"DeviceCommunicationControl: "
ERROR_CLASS_SERVICES, ERROR_CODE_PASSWORD_FAILURE);
fprintf(stderr,
"DeviceCommunicationControl: "
"Sending Error - password failure.\n");
}
}
bytes_sent = datalink_send_pdu(
src, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
bytes_sent = datalink_send_pdu(src, // destination address
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
if (bytes_sent <= 0)
fprintf(stderr, "DeviceCommunicationControl: "
"Failed to send PDU (%s)!\n",
strerror(errno));
"Failed to send PDU (%s)!\n", strerror(errno));
return;
}
+12 -31
View File
@@ -32,10 +32,8 @@
#include "iam.h"
#include "address.h"
void handler_i_am_add(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src)
void handler_i_am_add(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
{
int len = 0;
uint32_t device_id = 0;
@@ -44,30 +42,20 @@ void handler_i_am_add(
uint16_t vendor_id = 0;
(void) service_len;
len = iam_decode_service_request(
service_request,
&device_id,
&max_apdu,
&segmentation,
&vendor_id);
len = iam_decode_service_request(service_request,
&device_id, &max_apdu, &segmentation, &vendor_id);
fprintf(stderr, "Received I-Am Request");
if (len != -1)
{
if (len != -1) {
fprintf(stderr, " from %u!\n", device_id);
address_add(device_id,
max_apdu,
src);
}
else
address_add(device_id, max_apdu, src);
} else
fprintf(stderr, "!\n");
return;
}
void handler_i_am_bind(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src)
void handler_i_am_bind(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
{
int len = 0;
uint32_t device_id = 0;
@@ -76,17 +64,10 @@ void handler_i_am_bind(
uint16_t vendor_id = 0;
(void) service_len;
len = iam_decode_service_request(
service_request,
&device_id,
&max_apdu,
&segmentation,
&vendor_id);
len = iam_decode_service_request(service_request,
&device_id, &max_apdu, &segmentation, &vendor_id);
// only add address if requested to bind
address_add_binding(device_id,
max_apdu,
src);
address_add_binding(device_id, max_apdu, src);
return;
}
+6 -13
View File
@@ -32,31 +32,24 @@
#include "bactext.h"
#include "ihave.h"
void handler_i_have(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src)
void handler_i_have(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
{
int len = 0;
BACNET_I_HAVE_DATA data;
(void) service_len;
(void) src;
len = ihave_decode_service_request(
service_request,
service_len,
&data);
if (len != -1)
{
len = ihave_decode_service_request(service_request,
service_len, &data);
if (len != -1) {
fprintf(stderr, "I-Have: %s %d from %s %u!\r\n",
bactext_object_type_name(data.object_id.type),
data.object_id.instance,
bactext_object_type_name(data.device_id.type),
data.device_id.instance);
}
else
} else
fprintf(stderr, "I-Have: received, but unable to decode!\n");
return;
}
+31 -56
View File
@@ -40,11 +40,9 @@
static BACNET_CHARACTER_STRING My_Password;
void handler_reinitialize_device(
uint8_t *service_request,
void handler_reinitialize_device(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_DATA *service_data)
BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data)
{
BACNET_REINITIALIZED_STATE state;
BACNET_CHARACTER_STRING password;
@@ -54,59 +52,40 @@ void handler_reinitialize_device(
int bytes_sent = 0;
// decode the service request only
len = rd_decode_service_request(
service_request,
service_len,
&state,
&password);
len = rd_decode_service_request(service_request,
service_len, &state, &password);
fprintf(stderr, "ReinitializeDevice!\n");
if (len > 0)
fprintf(stderr, "ReinitializeDevice: state=%u password=%s\n",
(unsigned)state,
characterstring_value(&password));
(unsigned) state, characterstring_value(&password));
else
fprintf(stderr, "ReinitializeDevice: Unable to decode request!\n");
// prepare a reply
datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], src, &my_address, false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
// bad decoding or something we didn't understand - send an abort
if (len == -1)
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
ABORT_REASON_OTHER);
fprintf(stderr,"ReinitializeDevice: Sending Abort - could not decode.\n");
}
else if (service_data->segmented_message)
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
if (len == -1) {
pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_OTHER);
fprintf(stderr,
"ReinitializeDevice: Sending Abort - could not decode.\n");
} else if (service_data->segmented_message) {
pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr,"ReinitializeDevice: Sending Abort - segmented message.\n");
}
else if (state >= MAX_BACNET_REINITIALIZED_STATE)
{
pdu_len += reject_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
REJECT_REASON_UNDEFINED_ENUMERATION);
fprintf(stderr,"ReinitializeDevice: Sending Reject - undefined enumeration\n");
}
else
{
fprintf(stderr,
"ReinitializeDevice: Sending Abort - segmented message.\n");
} else if (state >= MAX_BACNET_REINITIALIZED_STATE) {
pdu_len += reject_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, REJECT_REASON_UNDEFINED_ENUMERATION);
fprintf(stderr,
"ReinitializeDevice: Sending Reject - undefined enumeration\n");
} else {
characterstring_init_ansi(&My_Password, "Jesus");
if (characterstring_same(&password,&My_Password))
{
pdu_len += encode_simple_ack(
&Handler_Transmit_Buffer[pdu_len],
if (characterstring_same(&password, &My_Password)) {
pdu_len += encode_simple_ack(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_REINITIALIZE_DEVICE);
fprintf(stderr, "ReinitializeDevice: Sending Simple Ack!\n");
@@ -114,22 +93,18 @@ void handler_reinitialize_device(
/* 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,
you probably should clear any DCC status and timeouts */
}
else
{
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
} else {
pdu_len +=
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
ERROR_CLASS_SERVICES,
ERROR_CODE_PASSWORD_FAILURE);
fprintf(stderr,"ReinitializeDevice: Sending Error - password failure.\n");
ERROR_CLASS_SERVICES, ERROR_CODE_PASSWORD_FAILURE);
fprintf(stderr,
"ReinitializeDevice: Sending Error - password failure.\n");
}
}
bytes_sent = datalink_send_pdu(
src, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
bytes_sent = datalink_send_pdu(src, // destination address
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
if (bytes_sent <= 0)
fprintf(stderr, "ReinitializeDevice: Failed to send PDU (%s)!\n",
strerror(errno));
+53 -111
View File
@@ -45,11 +45,9 @@
static uint8_t Temp_Buf[MAX_APDU] = { 0 };
void handler_read_property(
uint8_t *service_request,
void handler_read_property(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_DATA *service_data)
BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data)
{
BACNET_READ_PROPERTY_DATA data;
int len = 0;
@@ -61,159 +59,111 @@ void handler_read_property(
BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT;
BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT;
len = rp_decode_service_request(
service_request,
service_len,
&data);
len = rp_decode_service_request(service_request, service_len, &data);
if (len <= 0)
fprintf(stderr, "Unable to decode Read-Property Request!\n");
// prepare a reply
datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], src, &my_address, false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
// bad decoding - send an abort
if (len == -1)
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
ABORT_REASON_OTHER);
if (len == -1) {
pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_OTHER);
fprintf(stderr, "Sending Abort!\n");
send = true;
}
else if (service_data->segmented_message)
{
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,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr, "Sending Abort!\n");
send = true;
}
else
{
switch (data.object_type)
{
} else {
switch (data.object_type) {
case OBJECT_DEVICE:
// FIXME: probably need a length limitation sent with encode
if (data.object_instance == Device_Object_Instance_Number())
{
len = Device_Encode_Property_APDU(
&Temp_Buf[0],
if (data.object_instance == Device_Object_Instance_Number()) {
len = Device_Encode_Property_APDU(&Temp_Buf[0],
data.object_property,
data.array_index,
&error_class,
&error_code);
if (len >= 0)
{
data.array_index, &error_class, &error_code);
if (len >= 0) {
// encode the APDU portion of the packet
data.application_data = &Temp_Buf[0];
data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
&data);
pdu_len +=
rp_ack_encode_apdu(&Handler_Transmit_Buffer
[pdu_len], service_data->invoke_id, &data);
fprintf(stderr, "Sending Read Property Ack!\n");
send = true;
}
else
} else
error = true;
}
else
} else
error = true;
break;
case OBJECT_ANALOG_INPUT:
if (Analog_Input_Valid_Instance(data.object_instance))
{
len = Analog_Input_Encode_Property_APDU(
&Temp_Buf[0],
if (Analog_Input_Valid_Instance(data.object_instance)) {
len = Analog_Input_Encode_Property_APDU(&Temp_Buf[0],
data.object_instance,
data.object_property,
data.array_index,
&error_class,
&error_code);
if (len >= 0)
{
data.array_index, &error_class, &error_code);
if (len >= 0) {
// encode the APDU portion of the packet
data.application_data = &Temp_Buf[0];
data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
&data);
pdu_len +=
rp_ack_encode_apdu(&Handler_Transmit_Buffer
[pdu_len], service_data->invoke_id, &data);
fprintf(stderr, "Sending Read Property Ack!\n");
send = true;
}
else
} else
error = true;
}
else
} else
error = true;
break;
case OBJECT_ANALOG_OUTPUT:
if (Analog_Output_Valid_Instance(data.object_instance))
{
len = Analog_Output_Encode_Property_APDU(
&Temp_Buf[0],
if (Analog_Output_Valid_Instance(data.object_instance)) {
len = Analog_Output_Encode_Property_APDU(&Temp_Buf[0],
data.object_instance,
data.object_property,
data.array_index,
&error_class,
&error_code);
if (len >= 0)
{
data.array_index, &error_class, &error_code);
if (len >= 0) {
// encode the APDU portion of the packet
data.application_data = &Temp_Buf[0];
data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
&data);
pdu_len +=
rp_ack_encode_apdu(&Handler_Transmit_Buffer
[pdu_len], service_data->invoke_id, &data);
fprintf(stderr, "Sending Read Property Ack!\n");
send = true;
}
else
} else
error = true;
}
else
} else
error = true;
break;
#if BACFILE
case OBJECT_FILE:
if (bacfile_valid_instance(data.object_instance))
{
len = bacfile_encode_property_apdu(
&Temp_Buf[0],
if (bacfile_valid_instance(data.object_instance)) {
len = bacfile_encode_property_apdu(&Temp_Buf[0],
data.object_instance,
data.object_property,
data.array_index,
&error_class,
&error_code);
if (len >= 0)
{
data.array_index, &error_class, &error_code);
if (len >= 0) {
// encode the APDU portion of the packet
data.application_data = &Temp_Buf[0];
data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
&data);
pdu_len +=
rp_ack_encode_apdu(&Handler_Transmit_Buffer
[pdu_len], service_data->invoke_id, &data);
fprintf(stderr, "Sending Read Property Ack!\n");
send = true;
}
else
} else
error = true;
}
else
} else
error = true;
break;
#endif
@@ -222,27 +172,19 @@ void handler_read_property(
break;
}
}
if (error)
{
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
if (error) {
pdu_len += bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_READ_PROPERTY,
error_class,
error_code);
SERVICE_CONFIRMED_READ_PROPERTY, error_class, error_code);
fprintf(stderr, "Sending Read Property Error!\n");
send = true;
}
if (send)
{
bytes_sent = datalink_send_pdu(
src, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (send) {
bytes_sent = datalink_send_pdu(src, // destination address
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
if (bytes_sent <= 0)
fprintf(stderr, "Failed to send PDU (%s)!\n", strerror(errno));
}
return;
}
+13 -26
View File
@@ -51,8 +51,7 @@ static void PrintReadPropertyData(BACNET_READ_PROPERTY_DATA *data)
bool first_value = true;
bool print_brace = false;
if (data)
{
if (data) {
#if 0
if (data->array_index == BACNET_ARRAY_ALL)
fprintf(stderr, "%s #%u %s\n",
@@ -69,32 +68,24 @@ static void PrintReadPropertyData(BACNET_READ_PROPERTY_DATA *data)
application_data = data->application_data;
application_data_len = data->application_data_len;
/* value? need to loop until all of the len is gone... */
for (;;)
{
len = bacapp_decode_application_data(
application_data,
application_data_len,
&value);
if (first_value && (len < application_data_len))
{
for (;;) {
len = bacapp_decode_application_data(application_data,
application_data_len, &value);
if (first_value && (len < application_data_len)) {
first_value = false;
fprintf(stdout, "{");
print_brace = true;
}
bacapp_print_value(stdout, &value, data->object_property);
if (len)
{
if (len < application_data_len)
{
if (len) {
if (len < application_data_len) {
application_data += len;
application_data_len -= len;
/* there's more! */
fprintf(stdout, ",");
}
else
} else
break;
}
else
} else
break;
}
if (print_brace)
@@ -103,21 +94,17 @@ static void PrintReadPropertyData(BACNET_READ_PROPERTY_DATA *data)
}
}
void handler_read_property_ack(
uint8_t *service_request,
void handler_read_property_ack(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_ACK_DATA *service_data)
BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data)
{
int len = 0;
BACNET_READ_PROPERTY_DATA data;
(void) src;
(void) service_data; /* we could use these... */
len = rp_ack_decode_service_request(
service_request,
service_len,
&data);
len = rp_ack_decode_service_request(service_request,
service_len, &data);
#if 0
fprintf(stderr, "Received Read-Property Ack!\n");
#endif
+20 -34
View File
@@ -35,10 +35,8 @@
#include "device.h"
#include "client.h"
void handler_who_has(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src)
void handler_who_has(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
{
int len = 0;
BACNET_WHO_HAS_DATA data;
@@ -49,48 +47,36 @@ void handler_who_has(
bool found = false;
(void) src;
len = whohas_decode_service_request(
service_request,
service_len,
&data);
if (len > 0)
{
len = whohas_decode_service_request(service_request,
service_len, &data);
if (len > 0) {
if ((data.low_limit == -1) || (data.high_limit == -1))
directed_to_me = true;
else if ((Device_Object_Instance_Number() >= (uint32_t)data.low_limit) &&
(Device_Object_Instance_Number() <= (uint32_t)data.high_limit))
else if ((Device_Object_Instance_Number() >=
(uint32_t) data.low_limit)
&& (Device_Object_Instance_Number() <=
(uint32_t) data.high_limit))
directed_to_me = true;
if (directed_to_me)
{
if (directed_to_me) {
/* do we have such an object? If so, send an I-Have.
note: we should have only 1 of such an object */
if (data.object_name)
{
if (data.object_name) {
/* valid name in my device? */
object_name = characterstring_value(&data.object.name);
found = Device_Valid_Object_Name(
object_name,
&object_type,
&object_instance);
found = Device_Valid_Object_Name(object_name,
&object_type, &object_instance);
if (found)
Send_I_Have(
Device_Object_Instance_Number(),
object_type,
object_instance,
object_name);
}
else
{
Send_I_Have(Device_Object_Instance_Number(),
object_type, object_instance, object_name);
} else {
/* valid object in my device? */
object_name = Device_Valid_Object_Id(
data.object.identifier.type,
object_name =
Device_Valid_Object_Id(data.object.identifier.type,
data.object.identifier.instance);
if (object_name)
Send_I_Have(
Device_Object_Instance_Number(),
Send_I_Have(Device_Object_Instance_Number(),
data.object.identifier.type,
data.object.identifier.instance,
object_name);
data.object.identifier.instance, object_name);
}
}
}
+5 -11
View File
@@ -37,27 +37,21 @@
/* global flag to send an I-Am */
bool I_Am_Request = true;
void handler_who_is(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src)
void handler_who_is(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
{
int len = 0;
int32_t low_limit = 0;
int32_t high_limit = 0;
(void) src;
len = whois_decode_service_request(
service_request,
service_len,
&low_limit,
&high_limit);
len = whois_decode_service_request(service_request,
service_len, &low_limit, &high_limit);
/* in our simple system, we just set a flag and let the main loop
send the I-Am request. */
if (len == 0)
I_Am_Request = true;
else if (len != -1)
{
else if (len != -1) {
if ((Device_Object_Instance_Number() >= (uint32_t) low_limit) &&
(Device_Object_Instance_Number() <= (uint32_t) high_limit))
I_Am_Request = true;
+38 -69
View File
@@ -43,11 +43,9 @@
#include "bacfile.h"
#endif
void handler_write_property(
uint8_t *service_request,
void handler_write_property(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_DATA *service_data)
BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data)
{
BACNET_WRITE_PROPERTY_DATA wp_data;
int len = 0;
@@ -58,65 +56,45 @@ void handler_write_property(
int bytes_sent = 0;
// decode the service request only
len = wp_decode_service_request(
service_request,
service_len,
&wp_data);
len = wp_decode_service_request(service_request,
service_len, &wp_data);
fprintf(stderr, "Received Write-Property Request!\n");
if (len > 0)
fprintf(stderr, "type=%u instance=%u property=%u index=%d\n",
wp_data.object_type,
wp_data.object_instance,
wp_data.object_property,
wp_data.array_index);
wp_data.object_property, wp_data.array_index);
else
fprintf(stderr, "Unable to decode Write-Property Request!\n");
// prepare a reply
datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], src, &my_address, false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
// bad decoding or something we didn't understand - send an abort
if (len == -1)
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
ABORT_REASON_OTHER);
if (len == -1) {
pdu_len += abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, ABORT_REASON_OTHER);
fprintf(stderr, "Sending Abort!\n");
}
else if (service_data->segmented_message)
{
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,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr, "Sending Abort!\n");
}
else
{
switch (wp_data.object_type)
{
} else {
switch (wp_data.object_type) {
case OBJECT_DEVICE:
if (Device_Write_Property(&wp_data,&error_class,&error_code))
{
pdu_len += encode_simple_ack(
&Handler_Transmit_Buffer[pdu_len],
if (Device_Write_Property(&wp_data, &error_class, &error_code)) {
pdu_len +=
encode_simple_ack(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_WRITE_PROPERTY);
fprintf(stderr, "Sending Write Property Simple Ack!\n");
}
else
{
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
} else {
pdu_len +=
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_WRITE_PROPERTY,
error_class,
SERVICE_CONFIRMED_WRITE_PROPERTY, error_class,
error_code);
fprintf(stderr, "Sending Write Property Error!\n");
}
@@ -124,49 +102,40 @@ void handler_write_property(
case OBJECT_ANALOG_INPUT:
error_class = ERROR_CLASS_PROPERTY;
error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_WRITE_PROPERTY,
error_class,
error_code);
pdu_len +=
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY,
error_class, error_code);
fprintf(stderr, "Sending Write Access Error!\n");
break;
case OBJECT_ANALOG_OUTPUT:
if (Analog_Output_Write_Property(&wp_data,&error_class,&error_code))
{
pdu_len += encode_simple_ack(
&Handler_Transmit_Buffer[pdu_len],
if (Analog_Output_Write_Property(&wp_data, &error_class,
&error_code)) {
pdu_len +=
encode_simple_ack(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_WRITE_PROPERTY);
fprintf(stderr, "Sending Write Property Simple Ack!\n");
}
else
{
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
} else {
pdu_len +=
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_WRITE_PROPERTY,
error_class,
SERVICE_CONFIRMED_WRITE_PROPERTY, error_class,
error_code);
fprintf(stderr, "Sending Write Access Error!\n");
}
break;
default:
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_WRITE_PROPERTY,
error_class,
error_code);
pdu_len +=
bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY,
error_class, error_code);
fprintf(stderr, "Sending Unknown Object Error!\n");
break;
}
}
bytes_sent = datalink_send_pdu(
src, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
bytes_sent = datalink_send_pdu(src, // destination address
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
if (bytes_sent <= 0)
fprintf(stderr, "Failed to send PDU (%s)!\n", strerror(errno));
+18 -37
View File
@@ -39,81 +39,62 @@ extern bool I_Am_Request;
extern "C" {
#endif /* __cplusplus */
void handler_unrecognized_service(
uint8_t *service_request,
void handler_unrecognized_service(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * dest,
BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_who_is(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src);
void handler_who_is(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src);
void handler_who_has(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src);
void handler_who_has(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src);
void handler_i_am_add(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src);
void handler_i_am_add(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src);
void handler_i_am_bind(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src);
void handler_i_am_bind(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src);
void handler_read_property(
uint8_t *service_request,
void handler_read_property(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_read_property_ack(
uint8_t *service_request,
void handler_read_property_ack(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data);
void handler_write_property(
uint8_t *service_request,
void handler_write_property(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_atomic_read_file(
uint8_t *service_request,
void handler_atomic_read_file(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_atomic_read_file_ack(
uint8_t *service_request,
void handler_atomic_read_file_ack(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data);
void handler_reinitialize_device(
uint8_t *service_request,
void handler_reinitialize_device(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_device_communication_control(
uint8_t *service_request,
void handler_device_communication_control(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src,
BACNET_CONFIRMED_SERVICE_DATA * service_data);
void handler_i_have(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src);
void handler_i_have(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+7 -17
View File
@@ -34,11 +34,9 @@
#include "npdu.h"
#include "reject.h"
void handler_unrecognized_service(
uint8_t *service_request,
void handler_unrecognized_service(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS *dest,
BACNET_CONFIRMED_SERVICE_DATA *service_data)
BACNET_ADDRESS * dest, BACNET_CONFIRMED_SERVICE_DATA * service_data)
{
BACNET_ADDRESS src;
int pdu_len = 0;
@@ -49,23 +47,15 @@ void handler_unrecognized_service(
datalink_get_my_address(&src);
// encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
dest,
&src,
false, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], dest, &src, false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
// encode the APDU portion of the packet
pdu_len += reject_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
REJECT_REASON_UNRECOGNIZED_SERVICE);
pdu_len += reject_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id, REJECT_REASON_UNRECOGNIZED_SERVICE);
bytes_sent = datalink_send_pdu(
dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
bytes_sent = datalink_send_pdu(dest, // destination address
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
if (bytes_sent > 0)
fprintf(stderr, "Sent Reject!\n");
else
+17 -34
View File
@@ -41,12 +41,9 @@
#include "handlers.h"
#include "txbuf.h"
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 */
{
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 */
BACNET_ADDRESS dest;
BACNET_ADDRESS my_address;
unsigned max_apdu = 0;
@@ -64,50 +61,36 @@ uint8_t Send_Device_Communication_Control_Request(
/* is there a tsm available? */
if (status)
status = tsm_transaction_available();
if (status)
{
if (status) {
datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID();
// load the data for the encoding
characterstring_init_ansi(&password_string, password);
pdu_len += dcc_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
pdu_len += dcc_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
invoke_id,
timeDuration,
state,
password?&password_string:NULL);
timeDuration, state, password ? &password_string : NULL);
/* will it fit in the sender?
note: if there is a bottleneck router in between
us and the destination, we won't know unless
we have a way to check for that and update the
max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu)
{
tsm_set_confirmed_unsegmented_transaction(
invoke_id,
&dest,
&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 ((unsigned) pdu_len < max_apdu) {
tsm_set_confirmed_unsegmented_transaction(invoke_id,
&dest, &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)
fprintf(stderr,"Failed to Send DeviceCommunicationControl Request (%s)!\n",
fprintf(stderr,
"Failed to Send DeviceCommunicationControl Request (%s)!\n",
strerror(errno));
}
else
fprintf(stderr,"Failed to Send DeviceCommunicationControl Request "
} else
fprintf(stderr,
"Failed to Send DeviceCommunicationControl Request "
"(exceeds destination maximum APDU)!\n");
}
return invoke_id;
}
+8 -17
View File
@@ -43,11 +43,9 @@
#include "txbuf.h"
/* find a specific device, or use -1 for limit if you want unlimited */
void Send_I_Have(
uint32_t device_id,
void Send_I_Have(uint32_t device_id,
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance,
char *object_name)
uint32_t object_instance, char *object_name)
{
int pdu_len = 0;
BACNET_ADDRESS dest;
@@ -60,11 +58,7 @@ void Send_I_Have(
/* Who-Has is a global broadcast */
datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, /* true for confirmed messages */
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, /* true for confirmed messages */
MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */
data.device_id.type = OBJECT_DEVICE;
@@ -72,14 +66,11 @@ void Send_I_Have(
data.object_id.type = object_type;
data.object_id.instance = object_instance;
characterstring_init_ansi(&data.object_name, object_name);
pdu_len += ihave_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
&data);
pdu_len += ihave_encode_apdu(&Handler_Transmit_Buffer[pdu_len], &data);
/* send the data */
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
bytes_sent = datalink_send_pdu(&dest, // destination address
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
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));
}
+14 -31
View File
@@ -42,10 +42,8 @@
#include "handlers.h"
#include "txbuf.h"
uint8_t Send_Reinitialize_Device_Request(
uint32_t device_id,
BACNET_REINITIALIZED_STATE state,
char *password)
uint8_t Send_Reinitialize_Device_Request(uint32_t device_id,
BACNET_REINITIALIZED_STATE state, char *password)
{
BACNET_ADDRESS dest;
BACNET_ADDRESS my_address;
@@ -64,49 +62,34 @@ uint8_t Send_Reinitialize_Device_Request(
/* is there a tsm available? */
if (status)
status = tsm_transaction_available();
if (status)
{
if (status) {
datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID();
// load the data for the encoding
characterstring_init_ansi(&password_string, password);
pdu_len += rd_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
invoke_id,
state,
password?&password_string:NULL);
pdu_len += rd_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
invoke_id, state, password ? &password_string : NULL);
/* will it fit in the sender?
note: if there is a bottleneck router in between
us and the destination, we won't know unless
we have a way to check for that and update the
max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu)
{
tsm_set_confirmed_unsegmented_transaction(
invoke_id,
&dest,
&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 ((unsigned) pdu_len < max_apdu) {
tsm_set_confirmed_unsegmented_transaction(invoke_id,
&dest, &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)
fprintf(stderr,"Failed to Send ReinitializeDevice Request (%s)!\n",
fprintf(stderr,
"Failed to Send ReinitializeDevice Request (%s)!\n",
strerror(errno));
}
else
} else
fprintf(stderr, "Failed to Send ReinitializeDevice Request "
"(exceeds destination maximum APDU)!\n");
}
return invoke_id;
}
+14 -29
View File
@@ -43,12 +43,10 @@
#include "txbuf.h"
/* returns invoke id of 0 if device is not bound or no tsm available */
uint8_t Send_Read_Property_Request(
uint32_t device_id, /* destination device */
uint8_t Send_Read_Property_Request(uint32_t device_id, /* destination device */
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance,
BACNET_PROPERTY_ID object_property,
int32_t array_index)
BACNET_PROPERTY_ID object_property, int32_t array_index)
{
BACNET_ADDRESS dest;
BACNET_ADDRESS my_address;
@@ -67,14 +65,9 @@ uint8_t Send_Read_Property_Request(
/* is there a tsm available? */
if (status)
status = tsm_transaction_available();
if (status)
{
if (status) {
datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID();
@@ -83,31 +76,23 @@ uint8_t Send_Read_Property_Request(
data.object_instance = object_instance;
data.object_property = object_property;
data.array_index = array_index;
pdu_len += rp_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
invoke_id,
&data);
pdu_len += rp_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
invoke_id, &data);
/* will it fit in the sender?
note: if there is a bottleneck router in between
us and the destination, we won't know unless
we have a way to check for that and update the
max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu)
{
tsm_set_confirmed_unsegmented_transaction(
invoke_id,
&dest,
&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 ((unsigned) pdu_len < max_apdu) {
tsm_set_confirmed_unsegmented_transaction(invoke_id,
&dest, &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)
fprintf(stderr,"Failed to Send ReadProperty Request (%s)!\n",
fprintf(stderr,
"Failed to Send ReadProperty Request (%s)!\n",
strerror(errno));
}
else
} else
fprintf(stderr, "Failed to Send ReadProperty Request "
"(exceeds destination maximum APDU)!\n");
}
+16 -32
View File
@@ -43,10 +43,8 @@
#include "txbuf.h"
/* find a specific device, or use -1 for limit if you want unlimited */
void Send_WhoHas_Name(
int32_t low_limit,
int32_t high_limit,
char *object_name)
void Send_WhoHas_Name(int32_t low_limit,
int32_t high_limit, char *object_name)
{
int pdu_len = 0;
BACNET_ADDRESS dest;
@@ -59,35 +57,27 @@ void Send_WhoHas_Name(
/* Who-Has is a global broadcast */
datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, /* true for confirmed messages */
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, /* true for confirmed messages */
MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */
data.low_limit = low_limit;
data.high_limit = high_limit;
data.object_name = true;
characterstring_init_ansi(&data.object.name, object_name);
pdu_len += whohas_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
pdu_len += whohas_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&data);
/* send the data */
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
bytes_sent = datalink_send_pdu(&dest, // destination address
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
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 */
void Send_WhoHas_Object(
int32_t low_limit,
void Send_WhoHas_Object(int32_t low_limit,
int32_t high_limit,
BACNET_OBJECT_TYPE object_type,
uint32_t object_instance)
BACNET_OBJECT_TYPE object_type, uint32_t object_instance)
{
int pdu_len = 0;
BACNET_ADDRESS dest;
@@ -100,11 +90,7 @@ void Send_WhoHas_Object(
/* Who-Has is a global broadcast */
datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, /* true for confirmed messages */
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, /* true for confirmed messages */
MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */
data.low_limit = low_limit;
@@ -112,14 +98,12 @@ void Send_WhoHas_Object(
data.object_name = false;
data.object.identifier.type = object_type;
data.object.identifier.instance = object_instance;
pdu_len += whohas_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
pdu_len += whohas_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
&data);
/* send the data */
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
bytes_sent = datalink_send_pdu(&dest, // destination address
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
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"
/* find a specific device, or use -1 for limit if you want unlimited */
void Send_WhoIs(
int32_t low_limit,
int32_t high_limit)
void Send_WhoIs(int32_t low_limit, int32_t high_limit)
{
int pdu_len = 0;
BACNET_ADDRESS dest;
@@ -58,23 +56,16 @@ void Send_WhoIs(
datalink_get_broadcast_address(&dest);
// encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
// encode the APDU portion of the packet
pdu_len += whois_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
low_limit,
high_limit);
pdu_len += whois_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
low_limit, high_limit);
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
bytes_sent = datalink_send_pdu(&dest, // destination address
&Handler_Transmit_Buffer[0], pdu_len); // number of bytes of data
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));
}
+14 -30
View File
@@ -43,14 +43,12 @@
#include "txbuf.h"
/* returns the invoke ID for confirmed request, or zero on failure */
uint8_t Send_Write_Property_Request(
uint32_t device_id, // destination device
uint8_t Send_Write_Property_Request(uint32_t device_id, // destination device
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)
uint8_t priority, int32_t array_index)
{
BACNET_ADDRESS dest;
BACNET_ADDRESS my_address;
@@ -69,14 +67,9 @@ uint8_t Send_Write_Property_Request(
/* is there a tsm available? */
if (status)
status = tsm_transaction_available();
if (status)
{
if (status) {
datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID();
@@ -87,35 +80,26 @@ uint8_t Send_Write_Property_Request(
data.array_index = array_index;
bacapp_copy(&data.value, object_value);
data.priority = priority;
pdu_len += wp_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
invoke_id,
&data);
pdu_len += wp_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
invoke_id, &data);
/* will it fit in the sender?
note: if there is a bottleneck router in between
us and the destination, we won't know unless
we have a way to check for that and update the
max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu)
{
tsm_set_confirmed_unsegmented_transaction(
invoke_id,
&dest,
&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 ((unsigned) pdu_len < max_apdu) {
tsm_set_confirmed_unsegmented_transaction(invoke_id,
&dest, &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)
fprintf(stderr,"Failed to Send WriteProperty Request (%s)!\n",
fprintf(stderr,
"Failed to Send WriteProperty Request (%s)!\n",
strerror(errno));
}
else
} else
fprintf(stderr, "Failed to Send WriteProperty Request "
"(exceeds destination maximum APDU)!\n");
}
return invoke_id;
}
-1
View File
@@ -28,4 +28,3 @@
#include "datalink.h"
uint8_t Handler_Transmit_Buffer[MAX_MPDU] = { 0 };
+9 -18
View File
@@ -65,8 +65,7 @@ char *Analog_Input_Name(uint32_t object_instance)
{
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);
return text_string;
}
@@ -76,13 +75,11 @@ char *Analog_Input_Name(uint32_t object_instance)
/* return apdu length, or -1 on error */
/* assumption - object has already exists */
int Analog_Input_Encode_Property_APDU(
uint8_t *apdu,
int Analog_Input_Encode_Property_APDU(uint8_t * apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
{
int apdu_len = 0; // return value
BACNET_BIT_STRING bit_string;
@@ -90,17 +87,16 @@ int Analog_Input_Encode_Property_APDU(
float value = 3.14;
(void) array_index;
switch (property)
{
switch (property) {
case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_ANALOG_INPUT,
object_instance);
break;
case PROP_OBJECT_NAME:
case PROP_DESCRIPTION:
characterstring_init_ansi(&char_string, Analog_Input_Name(object_instance));
apdu_len = encode_tagged_character_string(&apdu[0],
&char_string);
characterstring_init_ansi(&char_string,
Analog_Input_Name(object_instance));
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
case PROP_OBJECT_TYPE:
apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_ANALOG_INPUT);
@@ -154,13 +150,10 @@ void testAnalogInput(Test * pTest)
// FIXME: we should do a lot more testing here...
len = Analog_Input_Encode_Property_APDU(
&apdu[0],
len = Analog_Input_Encode_Property_APDU(&apdu[0],
instance,
PROP_OBJECT_IDENTIFIER,
BACNET_ARRAY_ALL,
&error_class,
&error_code);
BACNET_ARRAY_ALL, &error_class, &error_code);
ct_test(pTest, len >= 0);
len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value);
ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID);
@@ -192,5 +185,3 @@ int main(void)
}
#endif /* TEST_ANALOG_INPUT */
#endif /* TEST */
+2 -5
View File
@@ -38,13 +38,11 @@ unsigned Analog_Input_Count(void);
uint32_t Analog_Input_Index_To_Instance(unsigned index);
char *Analog_Input_Name(uint32_t object_instance);
int Analog_Input_Encode_Property_APDU(
uint8_t *apdu,
int Analog_Input_Encode_Property_APDU(uint8_t * apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
#ifdef TEST
#include "ctest.h"
@@ -54,5 +52,4 @@ void testAnalogInput(Test * pTest);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+58 -96
View File
@@ -46,7 +46,8 @@
// 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
// 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
// without changing the physical output
static bool Analog_Output_Out_Of_Service[MAX_ANALOG_OUTPUTS];
@@ -58,15 +59,12 @@ void Analog_Output_Init(void)
{
unsigned i, j;
if (!Analog_Ouput_Initialized)
{
if (!Analog_Ouput_Initialized) {
Analog_Ouput_Initialized = true;
// initialize all the analog output priority arrays to NULL
for (i = 0; i < MAX_ANALOG_OUTPUTS; i++)
{
for (j = 0; j < BACNET_MAX_PRIORITY; j++)
{
for (i = 0; i < MAX_ANALOG_OUTPUTS; i++) {
for (j = 0; j < BACNET_MAX_PRIORITY; j++) {
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();
index = Analog_Output_Instance_To_Index(object_instance);
if (index < MAX_ANALOG_OUTPUTS)
{
for (i = 0; i < BACNET_MAX_PRIORITY; i++)
{
if (Analog_Output_Level[index][i] != AO_LEVEL_NULL)
{
if (index < MAX_ANALOG_OUTPUTS) {
for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
if (Analog_Output_Level[index][i] != AO_LEVEL_NULL) {
value = Analog_Output_Level[index][i];
break;
}
@@ -146,8 +141,7 @@ char *Analog_Output_Name(uint32_t object_instance)
{
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);
return text_string;
}
@@ -156,13 +150,11 @@ char *Analog_Output_Name(uint32_t object_instance)
}
/* return apdu len, or -1 on error */
int Analog_Output_Encode_Property_APDU(
uint8_t *apdu,
int Analog_Output_Encode_Property_APDU(uint8_t * apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
{
int len = 0;
int apdu_len = 0; // return value
@@ -173,20 +165,20 @@ int Analog_Output_Encode_Property_APDU(
unsigned i = 0;
Analog_Output_Init();
switch (property)
{
switch (property) {
case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_ANALOG_OUTPUT,
object_instance);
break;
case PROP_OBJECT_NAME:
case PROP_DESCRIPTION:
characterstring_init_ansi(&char_string, Analog_Output_Name(object_instance));
apdu_len = encode_tagged_character_string(&apdu[0],
&char_string);
characterstring_init_ansi(&char_string,
Analog_Output_Name(object_instance));
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
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;
case PROP_PRESENT_VALUE:
real_value = Analog_Output_Present_Value(object_instance);
@@ -212,49 +204,44 @@ int Analog_Output_Encode_Property_APDU(
case PROP_PRIORITY_ARRAY:
// Array element zero is the number of elements in the array
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
// into one packet.
else if (array_index == BACNET_ARRAY_ALL)
{
object_index = Analog_Output_Instance_To_Index(object_instance);
for (i = 0; i < BACNET_MAX_PRIORITY; i++)
{
else if (array_index == BACNET_ARRAY_ALL) {
object_index =
Analog_Output_Instance_To_Index(object_instance);
for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
// FIXME: check if we have room before adding it to APDU
if (Analog_Output_Level[object_index][i] == AO_LEVEL_NULL)
len = encode_tagged_null(&apdu[apdu_len]);
else
{
else {
real_value = Analog_Output_Level[object_index][i];
len = encode_tagged_real(&apdu[apdu_len], real_value);
}
// add it if we have room
if ((apdu_len + len) < MAX_APDU)
apdu_len += len;
else
{
else {
*error_class = ERROR_CLASS_SERVICES;
*error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT;
apdu_len = -1;
break;
}
}
}
else
{
object_index = Analog_Output_Instance_To_Index(object_instance);
if (array_index <= BACNET_MAX_PRIORITY)
{
if (Analog_Output_Level[object_index][array_index] == AO_LEVEL_NULL)
} else {
object_index =
Analog_Output_Instance_To_Index(object_instance);
if (array_index <= BACNET_MAX_PRIORITY) {
if (Analog_Output_Level[object_index][array_index] ==
AO_LEVEL_NULL)
len = encode_tagged_null(&apdu[apdu_len]);
else
{
real_value = Analog_Output_Level[object_index][array_index];
else {
real_value =
Analog_Output_Level[object_index][array_index];
len = encode_tagged_real(&apdu[apdu_len], real_value);
}
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
apdu_len = -1;
@@ -277,10 +264,8 @@ int Analog_Output_Encode_Property_APDU(
}
// returns true if successful
bool Analog_Output_Write_Property(
BACNET_WRITE_PROPERTY_DATA *wp_data,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
bool Analog_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
{
bool status = false; // return value
unsigned int object_index = 0;
@@ -288,81 +273,63 @@ bool Analog_Output_Write_Property(
uint8_t level = AO_LEVEL_NULL;
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_code = ERROR_CODE_UNKNOWN_OBJECT;
return false;
}
// decode the some of the request
switch (wp_data->object_property)
{
switch (wp_data->object_property) {
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;
if (priority && (priority <= BACNET_MAX_PRIORITY) &&
(priority != 6 /* reserved */ ) &&
(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;
object_index = Analog_Output_Instance_To_Index(
wp_data->object_instance);
object_index =
Analog_Output_Instance_To_Index(wp_data->
object_instance);
priority--;
Analog_Output_Level[object_index][priority] = level;
// if Out of Service is TRUE, then don't set the
// physical output. This comment may apply to the
// main loop (i.e. check out of service before changing output)
status = true;
}
else if (priority == 6)
{
} else if (priority == 6) {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*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;
object_index = Analog_Output_Instance_To_Index(
wp_data->object_instance);
object_index =
Analog_Output_Instance_To_Index(wp_data->object_instance);
priority = wp_data->priority;
if (priority && (priority <= BACNET_MAX_PRIORITY))
{
if (priority && (priority <= BACNET_MAX_PRIORITY)) {
priority--;
Analog_Output_Level[object_index][priority] = level;
status = true;
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
}
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
case PROP_OUT_OF_SERVICE:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_BOOLEAN)
{
object_index = Analog_Output_Instance_To_Index(
wp_data->object_instance);
if (wp_data->value.tag == BACNET_APPLICATION_TAG_BOOLEAN) {
object_index =
Analog_Output_Instance_To_Index(wp_data->object_instance);
Analog_Output_Out_Of_Service[object_index] =
wp_data->value.type.Boolean;
status = true;
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
@@ -395,13 +362,10 @@ void testAnalogOutput(Test * pTest)
BACNET_ERROR_CODE error_code;
len = Analog_Output_Encode_Property_APDU(
&apdu[0],
len = Analog_Output_Encode_Property_APDU(&apdu[0],
instance,
PROP_OBJECT_IDENTIFIER,
BACNET_ARRAY_ALL,
&error_class,
&error_code);
BACNET_ARRAY_ALL, &error_class, &error_code);
ct_test(pTest, len != 0);
len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value);
ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID);
@@ -433,5 +397,3 @@ int main(void)
}
#endif /* TEST_ANALOG_INPUT */
#endif /* TEST */
+4 -9
View File
@@ -40,18 +40,14 @@ unsigned Analog_Output_Count(void);
uint32_t Analog_Output_Index_To_Instance(unsigned index);
char *Analog_Output_Name(uint32_t object_instance);
int Analog_Output_Encode_Property_APDU(
uint8_t *apdu,
int Analog_Output_Encode_Property_APDU(uint8_t * apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
bool Analog_Output_Write_Property(
BACNET_WRITE_PROPERTY_DATA *wp_data,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
bool Analog_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
#ifdef TEST
#include "ctest.h"
@@ -61,5 +57,4 @@ void testAnalogOutput(Test * pTest);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+40 -87
View File
@@ -39,14 +39,12 @@
#include "arf.h"
#include "awf.h"
typedef struct
{
typedef struct {
uint32_t instance;
char *filename;
} BACNET_FILE_LISTING;
static BACNET_FILE_LISTING BACnet_File_Listing[] =
{
static BACNET_FILE_LISTING BACnet_File_Listing[] = {
{0, "test.log"},
{1, "script.txt"},
{2, "bacenum.h"},
@@ -59,10 +57,8 @@ char *bacfile_name(uint32_t instance)
char *filename = NULL;
// linear search for file instance match
while (BACnet_File_Listing[index].filename)
{
if (BACnet_File_Listing[index].instance == instance)
{
while (BACnet_File_Listing[index].filename) {
if (BACnet_File_Listing[index].instance == instance) {
filename = BACnet_File_Listing[index].filename;
break;
}
@@ -82,8 +78,7 @@ uint32_t bacfile_count(void)
uint32_t index = 0;
// linear search for file instance match
while (BACnet_File_Listing[index].filename)
{
while (BACnet_File_Listing[index].filename) {
index++;
}
@@ -96,10 +91,8 @@ uint32_t bacfile_index_to_instance(unsigned find_index)
uint32_t index = 0;
// bounds checking...
while (BACnet_File_Listing[index].filename)
{
if (index == find_index)
{
while (BACnet_File_Listing[index].filename) {
if (index == find_index) {
instance = BACnet_File_Listing[index].instance;
break;
}
@@ -114,8 +107,7 @@ static long fsize(FILE *pFile)
long size = 0;
long origin = 0;
if (pFile)
{
if (pFile) {
origin = ftell(pFile);
fseek(pFile, 0L, SEEK_END);
size = ftell(pFile);
@@ -131,11 +123,9 @@ static unsigned bacfile_file_size(uint32_t object_instance)
unsigned file_size = 0;
pFilename = bacfile_name(object_instance);
if (pFilename)
{
if (pFilename) {
pFile = fopen(pFilename, "rb");
if (pFile)
{
if (pFile) {
file_size = fsize(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 */
int bacfile_encode_property_apdu(
uint8_t *apdu,
int bacfile_encode_property_apdu(uint8_t * apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
{
int apdu_len = 0; // return value
char text_string[32] = { "" };
BACNET_CHARACTER_STRING char_string;
(void) array_index;
switch (property)
{
switch (property) {
case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0],
OBJECT_FILE,
object_instance);
OBJECT_FILE, object_instance);
break;
case PROP_OBJECT_NAME:
sprintf(text_string, "FILE %d", object_instance);
characterstring_init_ansi(&char_string, text_string);
apdu_len = encode_tagged_character_string(&apdu[0],
&char_string);
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
case PROP_OBJECT_TYPE:
apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_FILE);
break;
case PROP_DESCRIPTION:
characterstring_init_ansi(&char_string, bacfile_name(object_instance));
apdu_len = encode_tagged_character_string(&apdu[0],
&char_string);
characterstring_init_ansi(&char_string,
bacfile_name(object_instance));
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
case PROP_FILE_TYPE:
characterstring_init_ansi(&char_string, "TEXT");
apdu_len = encode_tagged_character_string(&apdu[0],
&char_string);
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
case PROP_FILE_SIZE:
apdu_len = encode_tagged_unsigned(&apdu[0],
@@ -191,16 +175,9 @@ int bacfile_encode_property_apdu(
case PROP_MODIFICATION_DATE:
// FIXME: get the actual value
apdu_len = encode_tagged_date(&apdu[0],
2005,
12,
25,
7 /* sunday */);
2005, 12, 25, 7 /* sunday */ );
// FIXME: get the actual value
apdu_len += encode_tagged_time(&apdu[apdu_len],
12,
0,
0,
0);
apdu_len += encode_tagged_time(&apdu[apdu_len], 12, 0, 0, 0);
break;
case PROP_ARCHIVE:
// FIXME: get the actual value: note it may be inverse...
@@ -229,10 +206,8 @@ uint32_t bacfile_instance(char *filename)
uint32_t instance = BACNET_MAX_INSTANCE + 1;
// linear search for filename match
while (BACnet_File_Listing[index].filename)
{
if (strcmp(BACnet_File_Listing[index].filename,filename) == 0)
{
while (BACnet_File_Listing[index].filename) {
if (strcmp(BACnet_File_Listing[index].filename, filename) == 0) {
instance = BACnet_File_Listing[index].instance;
break;
}
@@ -248,8 +223,7 @@ uint32_t bacfile_instance(char *filename)
// Another way would be to store the
// invokeID and file instance in a list or table
// when the request was sent
uint32_t bacfile_instance_from_tsm(
uint8_t invokeID)
uint32_t bacfile_instance_from_tsm(uint8_t invokeID)
{
BACNET_NPDU_DATA npdu_data = { 0 }; // dummy for getting npdu length
BACNET_CONFIRMED_SERVICE_DATA service_data = { 0 };
@@ -265,36 +239,23 @@ uint32_t bacfile_instance_from_tsm(
bool found = false;
int apdu_offset = 0;
found = tsm_get_transaction_pdu(
invokeID,
&dest,
&pdu[0],
&pdu_len);
if (found)
{
apdu_offset = npdu_decode(
&pdu[0], // data to decode
found = tsm_get_transaction_pdu(invokeID, &dest, &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, // source address - get the SNET/SLEN/SADR if in there
&npdu_data); // amount of data to decode
if (!npdu_data.network_layer_message &&
((pdu[apdu_offset] & 0xF0) == PDU_TYPE_CONFIRMED_SERVICE_REQUEST))
{
len = apdu_decode_confirmed_service_request(
&pdu[apdu_offset], // APDU data
((pdu[apdu_offset] & 0xF0) ==
PDU_TYPE_CONFIRMED_SERVICE_REQUEST)) {
len = apdu_decode_confirmed_service_request(&pdu[apdu_offset], // APDU data
pdu_len - apdu_offset,
&service_data,
&service_choice,
&service_request,
&service_request_len);
if (service_choice == SERVICE_CONFIRMED_ATOMIC_READ_FILE)
{
len = arf_decode_service_request(
service_request,
service_request_len,
&data);
if (len > 0)
{
&service_choice, &service_request, &service_request_len);
if (service_choice == SERVICE_CONFIRMED_ATOMIC_READ_FILE) {
len = arf_decode_service_request(service_request,
service_request_len, &data);
if (len > 0) {
if (data.object_type == OBJECT_FILE)
object_instance = data.object_instance;
}
@@ -314,15 +275,12 @@ bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA *data)
size_t len = 0;
pFilename = bacfile_name(data->object_instance);
if (pFilename)
{
if (pFilename) {
found = true;
pFile = fopen(pFilename, "rb");
if (pFile)
{
if (pFile) {
(void) fseek(pFile,
data->type.stream.fileStartPosition,
SEEK_SET);
data->type.stream.fileStartPosition, SEEK_SET);
len = fread(octetstring_value(&data->fileData), 1,
data->type.stream.requestedOctetCount, pFile);
if (len < data->type.stream.requestedOctetCount)
@@ -331,19 +289,14 @@ bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA *data)
data->endOfFile = false;
octetstring_truncate(&data->fileData, len);
fclose(pFile);
}
else
{
} else {
octetstring_truncate(&data->fileData, 0);
data->endOfFile = true;
}
}
else
{
} else {
octetstring_truncate(&data->fileData, 0);
data->endOfFile = true;
}
return found;
}
+3 -7
View File
@@ -56,24 +56,20 @@ uint32_t bacfile_instance(char *filename);
// Another way would be to store the
// invokeID and file instance in a list or table
// when the request was sent
uint32_t bacfile_instance_from_tsm(
uint8_t invokeID);
uint32_t bacfile_instance_from_tsm(uint8_t invokeID);
#endif
// AtomicReadFile ACK helper
bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA * data);
// handling for read property service
int bacfile_encode_property_apdu(
uint8_t *apdu,
int bacfile_encode_property_apdu(uint8_t * apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+93 -144
View File
@@ -121,8 +121,7 @@ bool Device_Set_Object_Name(const char *name, size_t length)
{
bool status = false; /*return value */
if (length < sizeof(Object_Name))
{
if (length < sizeof(Object_Name)) {
memmove(Object_Name, name, length);
Object_Name[length] = 0;
status = true;
@@ -151,8 +150,7 @@ bool Device_Set_Vendor_Name(const char *name, size_t length)
{
bool status = false; /*return value */
if (length < sizeof(Vendor_Name))
{
if (length < sizeof(Vendor_Name)) {
memmove(Vendor_Name, name, length);
Vendor_Name[length] = 0;
status = true;
@@ -180,8 +178,7 @@ bool Device_Set_Model_Name(const char *name, size_t length)
{
bool status = false; /*return value */
if (length < sizeof(Model_Name))
{
if (length < sizeof(Model_Name)) {
memmove(Model_Name, name, length);
Model_Name[length] = 0;
status = true;
@@ -199,8 +196,7 @@ bool Device_Set_Firmware_Revision(const char *name, size_t length)
{
bool status = false; /*return value */
if (length < sizeof(Firmware_Revision))
{
if (length < sizeof(Firmware_Revision)) {
memmove(Firmware_Revision, name, length);
Firmware_Revision[length] = 0;
status = true;
@@ -214,12 +210,12 @@ const char *Device_Application_Software_Version(void)
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 */
if (length < sizeof(Application_Software_Version))
{
if (length < sizeof(Application_Software_Version)) {
memmove(Application_Software_Version, name, length);
Application_Software_Version[length] = 0;
status = true;
@@ -237,8 +233,7 @@ bool Device_Set_Description(const char *name, size_t length)
{
bool status = false; /*return value */
if (length < sizeof(Description))
{
if (length < sizeof(Description)) {
memmove(Description, name, length);
Description[length] = 0;
status = true;
@@ -256,8 +251,7 @@ bool Device_Set_Location(const char *name, size_t length)
{
bool status = false; /*return value */
if (length < sizeof(Location))
{
if (length < sizeof(Location)) {
memmove(Location, name, length);
Location[length] = 0;
status = true;
@@ -333,47 +327,38 @@ unsigned Device_Object_List_Count(void)
}
bool Device_Object_List_Identifier(unsigned array_index,
int *object_type,
uint32_t *instance)
int *object_type, uint32_t * instance)
{
bool status = false;
unsigned object_index = 0;
if (array_index == 1)
{
if (array_index == 1) {
*object_type = OBJECT_DEVICE;
*instance = Object_Instance_Number;
status = true;
}
if (!status)
{
if (!status) {
// array index starts at 1, and 1 for the device object
object_index = array_index - 2;
if (object_index < Analog_Input_Count())
{
if (object_index < Analog_Input_Count()) {
*object_type = OBJECT_ANALOG_INPUT;
*instance = Analog_Input_Index_To_Instance(object_index);
status = true;
}
}
if (!status)
{
if (!status) {
object_index -= Analog_Input_Count();
if (object_index < Analog_Output_Count())
{
if (object_index < Analog_Output_Count()) {
*object_type = OBJECT_ANALOG_OUTPUT;
*instance = Analog_Output_Index_To_Instance(object_index);
status = true;
}
}
#if BACFILE
if (!status)
{
if (!status) {
object_index -= Analog_Output_Count();
if (object_index < bacfile_count())
{
if (object_index < bacfile_count()) {
*object_type = OBJECT_FILE;
*instance = bacfile_index_to_instance(object_index);
status = true;
@@ -384,10 +369,8 @@ bool Device_Object_List_Identifier(unsigned array_index,
return status;
}
bool Device_Valid_Object_Name(
const char *object_name,
int *object_type,
uint32_t *object_instance)
bool Device_Valid_Object_Name(const char *object_name,
int *object_type, uint32_t * object_instance)
{
bool found = false;
int type = 0;
@@ -397,14 +380,11 @@ bool Device_Valid_Object_Name(
char *name = NULL;
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);
if (check_id)
{
if (check_id) {
name = Device_Valid_Object_Id(type, instance);
if (strcmp(name,object_name) == 0)
{
if (strcmp(name, object_name) == 0) {
found = true;
if (object_type)
*object_type = type;
@@ -419,14 +399,11 @@ bool Device_Valid_Object_Name(
}
/* returns the name or NULL if not found */
char *Device_Valid_Object_Id(
int object_type,
uint32_t object_instance)
char *Device_Valid_Object_Id(int object_type, uint32_t object_instance)
{
char *name = NULL; /* return value */
switch (object_type)
{
switch (object_type) {
case OBJECT_ANALOG_INPUT:
name = Analog_Input_Name(object_instance);
break;
@@ -450,12 +427,10 @@ char *Device_Valid_Object_Id(
}
// return the length of the apdu encoded or -1 for error
int Device_Encode_Property_APDU(
uint8_t *apdu,
int Device_Encode_Property_APDU(uint8_t * apdu,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
{
int apdu_len = 0; // return value
int len = 0; // apdu len intermediate value
@@ -466,8 +441,7 @@ int Device_Encode_Property_APDU(
uint32_t instance = 0;
unsigned count = 0;
switch (property)
{
switch (property) {
case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_DEVICE,
Object_Instance_Number);
@@ -502,7 +476,8 @@ int Device_Encode_Property_APDU(
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
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);
break;
// 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));
//break;
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;
// BACnet Legacy Support
case PROP_PROTOCOL_CONFORMANCE_CLASS:
@@ -535,10 +511,10 @@ int Device_Encode_Property_APDU(
case PROP_PROTOCOL_SERVICES_SUPPORTED:
/* Note: list of services that are executed, not initiated. */
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 */
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);
break;
@@ -546,8 +522,7 @@ int Device_Encode_Property_APDU(
/* Note: this is the list of objects that can be in this device,
not a list of objects that this device can access */
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
bitstring_set_bit(&bit_string, (uint8_t) i, false);
}
@@ -569,27 +544,23 @@ int Device_Encode_Property_APDU(
// into one packet. Note that more than likely you will have
// to return an error if the number of encoded objects exceeds
// your maximum APDU size.
else if (array_index == BACNET_ARRAY_ALL)
{
for (i = 1; i <= count; i++)
{
if (Device_Object_List_Identifier(i,&object_type,&instance))
{
len = encode_tagged_object_id(&apdu[apdu_len], object_type,
instance);
else if (array_index == BACNET_ARRAY_ALL) {
for (i = 1; i <= count; i++) {
if (Device_Object_List_Identifier(i, &object_type,
&instance)) {
len =
encode_tagged_object_id(&apdu[apdu_len],
object_type, instance);
apdu_len += len;
// assume next one is the same size as this one
// 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_code = ERROR_CODE_NO_SPACE_FOR_OBJECT;
apdu_len = -1;
break;
}
}
else
{
} else {
// error: internal error?
*error_class = ERROR_CLASS_SERVICES;
*error_code = ERROR_CODE_OTHER;
@@ -597,13 +568,13 @@ int Device_Encode_Property_APDU(
break;
}
}
}
else
{
if (Device_Object_List_Identifier(array_index,&object_type,&instance))
apdu_len = encode_tagged_object_id(&apdu[0], object_type, instance);
else
{
} else {
if (Device_Object_List_Identifier(array_index, &object_type,
&instance))
apdu_len =
encode_tagged_object_id(&apdu[0], object_type,
instance);
else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
apdu_len = -1;
@@ -622,7 +593,8 @@ int Device_Encode_Property_APDU(
apdu_len = encode_tagged_unsigned(&apdu[0], APDU_Timeout);
break;
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;
case PROP_DEVICE_ADDRESS_BINDING:
/* encode the list here, if it exists */
@@ -641,125 +613,102 @@ int Device_Encode_Property_APDU(
extern bool I_Am_Request;
// returns true if successful
bool Device_Write_Property(
BACNET_WRITE_PROPERTY_DATA *wp_data,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
{
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_code = ERROR_CODE_UNKNOWN_OBJECT;
return false;
}
// decode the some of the request
switch (wp_data->object_property)
{
switch (wp_data->object_property) {
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) &&
(Device_Set_Object_Instance_Number(
wp_data->value.type.Object_Id.instance)))
{
(Device_Set_Object_Instance_Number(wp_data->value.type.
Object_Id.instance))) {
I_Am_Request = true;
status = true;
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
}
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
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? */
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;
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
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? */
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;
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
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? */
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;
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
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);
status = true;
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
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;
encoding = characterstring_encoding(&wp_data->value.type.Character_String);
if (encoding == CHARACTER_ANSI_X34)
{
status = Device_Set_Object_Name(
characterstring_value(&wp_data->value.type.Character_String),
characterstring_length(&wp_data->value.type.Character_String));
if (!status)
{
encoding =
characterstring_encoding(&wp_data->value.type.
Character_String);
if (encoding == CHARACTER_ANSI_X34) {
status =
Device_Set_Object_Name(characterstring_value(&wp_data->
value.type.Character_String),
characterstring_length(&wp_data->value.type.
Character_String));
if (!status) {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY;
}
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED;
}
}
else
{
} else {
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
@@ -791,10 +740,12 @@ void testDevice(Test * pTest)
ct_test(pTest, Device_Object_Instance_Number() == BACNET_MAX_INSTANCE);
ct_test(pTest, status == true);
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);
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);
@@ -866,5 +817,3 @@ int main(void)
}
#endif /* TEST_DEVICE */
#endif /* TEST */
+10 -19
View File
@@ -49,8 +49,7 @@ bool Device_Set_Object_Instance_Number(uint32_t object_id);
bool Device_Valid_Object_Instance_Number(uint32_t object_id);
unsigned Device_Object_List_Count(void);
bool Device_Object_List_Identifier(unsigned array_index,
int *object_type,
uint32_t *instance);
int *object_type, uint32_t * instance);
BACNET_DEVICE_STATUS Device_System_Status(void);
void Device_Set_System_Status(BACNET_DEVICE_STATUS status);
@@ -68,7 +67,8 @@ const char *Device_Firmware_Revision(void);
bool Device_Set_Firmware_Revision(const char *name, size_t length);
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);
bool Device_Set_Description(const char *name, size_t length);
@@ -91,29 +91,20 @@ void Device_Set_Number_Of_APDU_Retries(uint8_t retries);
uint8_t Device_Database_Revision(void);
void Device_Set_Database_Revision(uint8_t revision);
bool Device_Valid_Object_Name(
const char *object_name,
int *object_type,
uint32_t *object_instance);
char *Device_Valid_Object_Id(
int object_type,
bool Device_Valid_Object_Name(const char *object_name,
int *object_type, uint32_t * object_instance);
char *Device_Valid_Object_Id(int object_type,
uint32_t object_instance);
int Device_Encode_Property_APDU(
uint8_t *apdu,
int Device_Encode_Property_APDU(uint8_t * apdu,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
bool Device_Write_Property(
BACNET_WRITE_PROPERTY_DATA *wp_data,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+81 -156
View File
@@ -60,27 +60,21 @@ static bool End_Of_File_Detected = false;
static bool Error_Detected = false;
static uint8_t Current_Invoke_ID = 0;
static void Atomic_Read_File_Error_Handler(
BACNET_ADDRESS *src,
static void Atomic_Read_File_Error_Handler(BACNET_ADDRESS * src,
uint8_t invoke_id,
BACNET_ERROR_CLASS error_class,
BACNET_ERROR_CODE error_code)
BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code)
{
/* FIXME: verify src and invoke id */
(void) src;
(void) invoke_id;
printf("\r\nBACnet Error!\r\n");
printf("Error Class: %s\r\n",
bactext_error_class_name(error_class));
printf("Error Code: %s\r\n",
bactext_error_code_name(error_code));
printf("Error Class: %s\r\n", bactext_error_class_name(error_class));
printf("Error Code: %s\r\n", bactext_error_code_name(error_code));
Error_Detected = true;
}
void MyAbortHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t abort_reason)
void MyAbortHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t abort_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -91,10 +85,8 @@ void MyAbortHandler(
Error_Detected = true;
}
void MyRejectHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t reject_reason)
void MyRejectHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t reject_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -105,11 +97,9 @@ void MyRejectHandler(
Error_Detected = true;
}
static uint8_t Send_Atomic_Read_File_Stream(
uint32_t device_id,
static uint8_t Send_Atomic_Read_File_Stream(uint32_t device_id,
uint32_t file_instance,
int fileStartPosition,
unsigned requestedOctetCount)
int fileStartPosition, unsigned requestedOctetCount)
{
BACNET_ADDRESS dest;
BACNET_ADDRESS my_address;
@@ -125,14 +115,9 @@ static uint8_t Send_Atomic_Read_File_Stream(
/* is there a tsm available? */
if (status)
status = tsm_transaction_available();
if (status)
{
if (status) {
datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID();
@@ -142,31 +127,23 @@ static uint8_t Send_Atomic_Read_File_Stream(
data.access = FILE_STREAM_ACCESS;
data.type.stream.fileStartPosition = fileStartPosition;
data.type.stream.requestedOctetCount = requestedOctetCount;
pdu_len += arf_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
invoke_id,
&data);
pdu_len += arf_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
invoke_id, &data);
/* will the APDU fit the target device?
note: if there is a bottleneck router in between
us and the destination, we won't know unless
we have a way to check for that and update the
max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu)
{
tsm_set_confirmed_unsegmented_transaction(
invoke_id,
&dest,
&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 ((unsigned) pdu_len < max_apdu) {
tsm_set_confirmed_unsegmented_transaction(invoke_id,
&dest, &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)
fprintf(stderr,"Failed to Send AtomicReadFile Request (%s)!\n",
fprintf(stderr,
"Failed to Send AtomicReadFile Request (%s)!\n",
strerror(errno));
}
else
} else
fprintf(stderr, "Failed to Send AtomicReadFile Request "
"(payload exceeds destination maximum APDU)!\n");
}
@@ -184,32 +161,23 @@ static void Send_WhoIs(uint32_t device_id)
datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */
pdu_len += whois_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
device_id,
device_id);
pdu_len += whois_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
device_id, device_id);
bytes_sent = datalink_send_pdu(
&dest, /* destination address */
&Handler_Transmit_Buffer[0],
pdu_len); /* number of bytes of data */
bytes_sent = datalink_send_pdu(&dest, /* destination address */
&Handler_Transmit_Buffer[0], pdu_len); /* number of bytes of data */
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(
uint8_t *service_request,
static void AtomicReadFileAckHandler(uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_ACK_DATA *service_data)
BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data)
{
int len = 0;
BACNET_ATOMIC_READ_FILE_DATA data;
@@ -217,39 +185,33 @@ static void AtomicReadFileAckHandler(
size_t octets_written = 0;
(void) src; /* FIXME: validate the source address matches */
len = arf_ack_decode_service_request(
service_request,
service_len,
&data);
if (len > 0)
{
len = arf_ack_decode_service_request(service_request,
service_len, &data);
if (len > 0) {
/* validate the parameters before storing data */
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)
pFile = fopen(Local_File_Name, "wb");
else
pFile = fopen(Local_File_Name, "rb+");
if (pFile)
{
if (pFile) {
/* is there anything to do with this? data.stream.requestedOctetCount */
(void)fseek(pFile, data.type.stream.fileStartPosition, SEEK_SET);
octets_written = fwrite(
octetstring_value(&data.fileData),
1, /* unit to write in bytes - in our case, an octet is one byte */
octetstring_length(&data.fileData),
pFile);
(void) fseek(pFile, data.type.stream.fileStartPosition,
SEEK_SET);
octets_written = fwrite(octetstring_value(&data.fileData), 1, /* unit to write in bytes - in our case, an octet is one byte */
octetstring_length(&data.fileData), pFile);
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);
else
printf("\r%u bytes",
(data.type.stream.fileStartPosition + octets_written));
(data.type.stream.fileStartPosition +
octets_written));
fclose(pFile);
}
if (data.endOfFile)
{
if (data.endOfFile) {
End_Of_File_Detected = true;
printf("\r\n");
}
@@ -257,10 +219,8 @@ static void AtomicReadFileAckHandler(
}
}
static void LocalIAmHandler(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src)
static void LocalIAmHandler(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
{
int len = 0;
uint32_t device_id = 0;
@@ -270,19 +230,11 @@ static void LocalIAmHandler(
(void) src;
(void) service_len;
len = iam_decode_service_request(
service_request,
&device_id,
&max_apdu,
&segmentation,
&vendor_id);
if (len != -1)
{
address_add(device_id,
max_apdu,
src);
}
else
len = iam_decode_service_request(service_request,
&device_id, &max_apdu, &segmentation, &vendor_id);
if (len != -1) {
address_add(device_id, max_apdu, src);
} else
fprintf(stderr, "!\n");
return;
@@ -292,33 +244,26 @@ static void Init_Service_Handlers(void)
{
/* we need to handle who-is
to support dynamic device binding to us */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_IS,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is);
/* handle i-am to support binding to other devices */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_I_AM,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler);
/* set the handler for all the services we don't implement
It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler(
handler_unrecognized_service);
apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service);
/* we must implement read property - it's required! */
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property);
/* handle the data coming back from confirmed requests */
apdu_set_confirmed_ack_handler(
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
AtomicReadFileAckHandler);
/* handle any errors coming back */
apdu_set_error_handler(
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
apdu_set_error_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
Atomic_Read_File_Error_Handler);
apdu_set_abort_handler(
MyAbortHandler);
apdu_set_reject_handler(
MyRejectHandler);
apdu_set_abort_handler(MyAbortHandler);
apdu_set_reject_handler(MyRejectHandler);
}
int main(int argc, char *argv[])
@@ -337,8 +282,7 @@ int main(int argc, char *argv[])
bool found = false;
uint16_t my_max_apdu = 0;
if (argc < 4)
{
if (argc < 4) {
/* FIXME: what about access method - record or stream? */
printf("%s device-instance file-instance local-name\r\n",
filename_remove_path(argv[0]));
@@ -348,14 +292,12 @@ int main(int argc, char *argv[])
Target_Device_Object_Instance = strtol(argv[1], NULL, 0);
Target_File_Object_Instance = strtol(argv[2], NULL, 0);
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",
Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
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",
Target_File_Object_Instance, BACNET_MAX_INSTANCE + 1);
return 1;
@@ -376,45 +318,31 @@ int main(int argc, char *argv[])
/* try to bind with the device */
Send_WhoIs(Target_Device_Object_Instance);
/* loop forever */
for (;;)
{
for (;;) {
/* increment timer - exit if timed out */
current_seconds = time(NULL);
/* returns 0 bytes on timeout */
pdu_len = bip_receive(
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
/* process */
if (pdu_len)
{
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
/* at least one second has passed */
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)
break;
if (I_Am_Request)
{
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
}
else
{
} else {
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(
Target_Device_Object_Instance,
&max_apdu,
&Target_Address);
if (found)
{
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
/* calculate the smaller of our APDU size or theirs
and remove the overhead of the APDU (about 16 octets max).
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;
/* has the previous invoke id expired or returned?
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)
fileStartPosition += requestedOctetCount;
/* we'll read the file in chunks
less than max_apdu to keep unsegmented */
invoke_id = Send_Atomic_Read_File_Stream(
Target_Device_Object_Instance,
Target_File_Object_Instance,
fileStartPosition,
invoke_id =
Send_Atomic_Read_File_Stream
(Target_Device_Object_Instance,
Target_File_Object_Instance, fileStartPosition,
requestedOctetCount);
Current_Invoke_ID = invoke_id;
}
}
else
{
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
+42 -77
View File
@@ -62,11 +62,9 @@ static int32_t Target_Object_Index = BACNET_ARRAY_ALL;
static BACNET_ADDRESS Target_Address;
static bool Error_Detected = false;
static void MyErrorHandler(
BACNET_ADDRESS *src,
static void MyErrorHandler(BACNET_ADDRESS * src,
uint8_t invoke_id,
BACNET_ERROR_CLASS error_class,
BACNET_ERROR_CODE error_code)
BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -77,10 +75,8 @@ static void MyErrorHandler(
Error_Detected = true;
}
void MyAbortHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t abort_reason)
void MyAbortHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t abort_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -90,10 +86,8 @@ void MyAbortHandler(
Error_Detected = true;
}
void MyRejectHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t reject_reason)
void MyRejectHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t reject_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -107,33 +101,26 @@ static void Init_Service_Handlers(void)
{
/* we need to handle who-is
to support dynamic device binding to us */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_IS,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is);
/* handle i-am to support binding to other devices */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_I_AM,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
handler_i_am_bind);
/* set the handler for all the services we don't implement
It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler(
handler_unrecognized_service);
apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service);
/* we must implement read property - it's required! */
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property);
/* handle the data coming back from confirmed requests */
apdu_set_confirmed_ack_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property_ack);
/* handle any errors coming back */
apdu_set_error_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_error_handler(SERVICE_CONFIRMED_READ_PROPERTY,
MyErrorHandler);
apdu_set_abort_handler(
MyAbortHandler);
apdu_set_reject_handler(
MyRejectHandler);
apdu_set_abort_handler(MyAbortHandler);
apdu_set_reject_handler(MyRejectHandler);
}
int main(int argc, char *argv[])
@@ -149,9 +136,9 @@ int main(int argc, char *argv[])
uint8_t invoke_id = 0;
bool found = false;
if (argc < 5)
{
printf("%s device-instance object-type object-instance property [index]\r\n",
if (argc < 5) {
printf
("%s device-instance object-type object-instance property [index]\r\n",
filename_remove_path(argv[0]));
return 0;
}
@@ -162,26 +149,22 @@ int main(int argc, char *argv[])
Target_Object_Property = strtol(argv[4], NULL, 0);
if (argc > 5)
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",
Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
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",
Target_Object_Type, MAX_BACNET_OBJECT_TYPE + 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",
Target_Object_Instance, BACNET_MAX_INSTANCE + 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",
Target_Object_Property, MAX_BACNET_PROPERTY_ID + 1);
return 1;
@@ -203,61 +186,43 @@ int main(int argc, char *argv[])
/* no need to spam the world */
I_Am_Request = false;
/* 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 */
for (;;)
{
for (;;) {
/* increment timer - exit if timed out */
current_seconds = time(NULL);
/* returns 0 bytes on timeout */
pdu_len = bip_receive(
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
/* process */
if (pdu_len)
{
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
/* at least one second has passed */
if (current_seconds != last_seconds)
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000));
tsm_timer_milliseconds(((current_seconds -
last_seconds) * 1000));
if (Error_Detected)
break;
if (I_Am_Request)
{
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
}
else
{
} else {
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(
Target_Device_Object_Instance,
&max_apdu,
&Target_Address);
if (found)
{
if (invoke_id == 0)
{
invoke_id = Send_Read_Property_Request(
Target_Device_Object_Instance,
Target_Object_Type,
Target_Object_Instance,
Target_Object_Property,
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
if (invoke_id == 0) {
invoke_id =
Send_Read_Property_Request
(Target_Device_Object_Instance, Target_Object_Type,
Target_Object_Instance, Target_Object_Property,
Target_Object_Index);
}
else if (tsm_invoke_id_free(invoke_id))
} else if (tsm_invoke_id_free(invoke_id))
break;
}
else
{
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
+45 -82
View File
@@ -56,16 +56,15 @@ static uint8_t Rx_Buf[MAX_MPDU] = {0};
/* global variables used in this file */
static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE;
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 bool Error_Detected = false;
static void MyErrorHandler(
BACNET_ADDRESS *src,
static void MyErrorHandler(BACNET_ADDRESS * src,
uint8_t invoke_id,
BACNET_ERROR_CLASS error_class,
BACNET_ERROR_CODE error_code)
BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -76,10 +75,8 @@ static void MyErrorHandler(
Error_Detected = true;
}
void MyAbortHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t abort_reason)
void MyAbortHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t abort_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -89,10 +86,8 @@ void MyAbortHandler(
Error_Detected = true;
}
void MyRejectHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t reject_reason)
void MyRejectHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t reject_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -102,8 +97,7 @@ void MyRejectHandler(
Error_Detected = true;
}
void MyReinitializeDeviceSimpleAckHandler(
BACNET_ADDRESS *src,
void MyReinitializeDeviceSimpleAckHandler(BACNET_ADDRESS * src,
uint8_t invoke_id)
{
(void) src;
@@ -115,47 +109,37 @@ static void Init_Service_Handlers(void)
{
/* we need to handle who-is
to support dynamic device binding to us */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_IS,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is);
/* handle i-am to support binding to other devices */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_I_AM,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
handler_i_am_bind);
/* set the handler for all the services we don't implement
It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler(
handler_unrecognized_service);
apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service);
/* we must implement read property - it's required! */
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property);
/* handle the ack coming back */
apdu_set_confirmed_simple_ack_handler(
SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
apdu_set_confirmed_simple_ack_handler
(SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
MyReinitializeDeviceSimpleAckHandler);
/* handle any errors coming back */
apdu_set_error_handler(
SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
apdu_set_error_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
MyErrorHandler);
apdu_set_abort_handler(
MyAbortHandler);
apdu_set_reject_handler(
MyRejectHandler);
apdu_set_abort_handler(MyAbortHandler);
apdu_set_reject_handler(MyRejectHandler);
}
#ifdef BIP_DEBUG
static void print_address(
char *name,
BACNET_ADDRESS *dest) // destination address
static void print_address(char *name, BACNET_ADDRESS * dest) // destination address
{
int i = 0; // counter
if (dest)
{
if (dest) {
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("\n");
@@ -179,11 +163,9 @@ int main(int argc, char *argv[])
BACNET_ADDRESS my_address, broadcast_address;
#endif
if (argc < 3)
{
if (argc < 3) {
/* note: priority 16 and 0 should produce the same end results... */
printf(
"Usage: %s device-instance state [password]\r\n"
printf("Usage: %s device-instance state [password]\r\n"
"Send BACnet ReinitializeDevice service to device.\r\n"
"\r\n"
"The device-instance can be 0 to %d.\r\n"
@@ -196,8 +178,7 @@ int main(int argc, char *argv[])
" 5=endrestore\r\n"
" 6=abortrestore\r\n"
"The optional password is a character string of 1 to 20 characters.\r\n",
filename_remove_path(argv[0]),
BACNET_MAX_INSTANCE-1);
filename_remove_path(argv[0]), BACNET_MAX_INSTANCE - 1);
return 0;
}
/* decode the command line parameters */
@@ -207,8 +188,7 @@ int main(int argc, char *argv[])
if (argc > 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",
Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
return 1;
@@ -236,59 +216,42 @@ int main(int argc, char *argv[])
/* don't send an I-Am unless asked */
I_Am_Request = false;
/* 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 */
for (;;)
{
for (;;) {
/* increment timer - exit if timed out */
current_seconds = time(NULL);
/* returns 0 bytes on timeout */
pdu_len = bip_receive(
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
/* process */
if (pdu_len)
{
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
/* at least one second has passed */
if (current_seconds != last_seconds)
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000));
tsm_timer_milliseconds(((current_seconds -
last_seconds) * 1000));
if (Error_Detected)
break;
if (I_Am_Request)
{
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
}
else
{
} else {
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(
Target_Device_Object_Instance,
&max_apdu,
&Target_Address);
if (found)
{
if (invoke_id == 0)
{
invoke_id = Send_Reinitialize_Device_Request(
Target_Device_Object_Instance,
Reinitialize_State,
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
if (invoke_id == 0) {
invoke_id =
Send_Reinitialize_Device_Request
(Target_Device_Object_Instance, Reinitialize_State,
Reinitialize_Password);
}
else if (tsm_invoke_id_free(invoke_id))
} else if (tsm_invoke_id_free(invoke_id))
break;
}
else
{
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
+18 -38
View File
@@ -53,33 +53,27 @@ static uint8_t Rx_Buf[MAX_MPDU] = {0};
static void Init_Service_Handlers(void)
{
/* we need to handle who-is to support dynamic device binding */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_IS,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is);
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_HAS,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS,
handler_who_has);
/* set the handler for all the services we don't implement */
/* It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler(
handler_unrecognized_service);
apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service);
/* Set the handlers for any confirmed services that we support. */
/* We must implement read property - it's required! */
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property);
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_WRITE_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
handler_write_property);
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
handler_atomic_read_file);
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE,
handler_reinitialize_device);
/* handle communication so we can shutup when asked */
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
apdu_set_confirmed_handler
(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL,
handler_device_communication_control);
}
@@ -88,17 +82,13 @@ static void cleanup(void)
datalink_cleanup();
}
static void print_address(
char *name,
BACNET_ADDRESS *dest) // destination address
static void print_address(char *name, BACNET_ADDRESS * dest) // destination address
{
int i = 0; // counter
if (dest)
{
if (dest) {
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("\n");
@@ -147,32 +137,22 @@ int main(int argc, char *argv[])
/* broadcast an I-Am on startup */
I_Am_Request = true;
// loop forever
for (;;)
{
for (;;) {
// input
current_seconds = time(NULL);
// returns 0 bytes on timeout
pdu_len = datalink_receive(
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
// process
if (pdu_len)
{
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
/* at least one second has passed */
if (current_seconds != last_seconds)
dcc_timer_seconds(current_seconds - last_seconds);
/* send out the I-Am if requested */
if (I_Am_Request)
{
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
}
+30 -63
View File
@@ -59,10 +59,8 @@ static char *Target_Object_Name = NULL;
static bool Error_Detected = false;
void MyAbortHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t abort_reason)
void MyAbortHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t abort_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -72,10 +70,8 @@ void MyAbortHandler(
Error_Detected = true;
}
void MyRejectHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t reject_reason)
void MyRejectHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t reject_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -89,40 +85,31 @@ static void Init_Service_Handlers(void)
{
/* we need to handle who-is
to support dynamic device binding to us */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_IS,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is);
/* set the handler for all the services we don't implement
It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler(
handler_unrecognized_service);
apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service);
/* we must implement read property - it's required! */
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property);
/* handle the reply (request) coming back */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_I_HAVE,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_HAVE,
handler_i_have);
/* handle any errors coming back */
apdu_set_abort_handler(
MyAbortHandler);
apdu_set_reject_handler(
MyRejectHandler);
apdu_set_abort_handler(MyAbortHandler);
apdu_set_reject_handler(MyRejectHandler);
}
#ifdef BIP_DEBUG
static void print_address(
char *name,
BACNET_ADDRESS *dest) // destination address
static void print_address(char *name, BACNET_ADDRESS * dest) // destination address
{
int i = 0; // counter
if (dest)
{
if (dest) {
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("\n");
@@ -143,11 +130,9 @@ int main(int argc, char *argv[])
BACNET_ADDRESS my_address, broadcast_address;
#endif
if (argc < 2)
{
if (argc < 2) {
/* note: priority 16 and 0 should produce the same end results... */
printf(
"Usage: %s <object-type object-instance | object-name>\r\n"
printf("Usage: %s <object-type object-instance | object-name>\r\n"
"Send BACnet WhoHas request to devices, and wait for responses.\r\n"
"\r\n"
"Use either:\r\n"
@@ -156,27 +141,22 @@ int main(int argc, char *argv[])
"or:\r\n"
"The object-name can be any string of characters.\r\n",
filename_remove_path(argv[0]),
MAX_BACNET_OBJECT_TYPE-1,
BACNET_MAX_INSTANCE);
MAX_BACNET_OBJECT_TYPE - 1, BACNET_MAX_INSTANCE);
return 0;
}
/* decode the command line parameters */
if (argc < 3)
{
if (argc < 3) {
Target_Object_Name = argv[1];
}
else
{
} else {
Target_Object_Type = strtol(argv[1], NULL, 0);
Target_Object_Instance = strtol(argv[2], NULL, 0);
if (Target_Object_Instance > BACNET_MAX_INSTANCE)
{
fprintf(stderr,"object-instance=%u - it must be less than %u\r\n",
if (Target_Object_Instance > BACNET_MAX_INSTANCE) {
fprintf(stderr,
"object-instance=%u - it must be less than %u\r\n",
Target_Object_Instance, BACNET_MAX_INSTANCE + 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",
Target_Object_Type, MAX_BACNET_OBJECT_TYPE + 1);
return 1;
@@ -206,36 +186,23 @@ int main(int argc, char *argv[])
Send_WhoHas_Name(-1, -1, Target_Object_Name);
else
Send_WhoHas_Object(-1, -1,
Target_Object_Type,
Target_Object_Instance);
Target_Object_Type, Target_Object_Instance);
/* loop forever */
for (;;)
{
for (;;) {
/* increment timer - exit if timed out */
current_seconds = time(NULL);
/* returns 0 bytes on timeout */
pdu_len = bip_receive(
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
/* process */
if (pdu_len)
{
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
if (Error_Detected)
break;
if (I_Am_Request)
{
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
}
else
{
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
+74 -145
View File
@@ -60,27 +60,21 @@ static bool End_Of_File_Detected = false;
static bool Error_Detected = false;
static uint8_t Current_Invoke_ID = 0;
static void Atomic_Read_File_Error_Handler(
BACNET_ADDRESS *src,
static void Atomic_Read_File_Error_Handler(BACNET_ADDRESS * src,
uint8_t invoke_id,
BACNET_ERROR_CLASS error_class,
BACNET_ERROR_CODE error_code)
BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code)
{
/* FIXME: verify src and invoke id */
(void) src;
(void) invoke_id;
printf("\r\nBACnet Error!\r\n");
printf("Error Class: %s\r\n",
bactext_error_class_name(error_class));
printf("Error Code: %s\r\n",
bactext_error_code_name(error_code));
printf("Error Class: %s\r\n", bactext_error_class_name(error_class));
printf("Error Code: %s\r\n", bactext_error_code_name(error_code));
Error_Detected = true;
}
void MyAbortHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t abort_reason)
void MyAbortHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t abort_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -91,10 +85,8 @@ void MyAbortHandler(
Error_Detected = true;
}
void MyRejectHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t reject_reason)
void MyRejectHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t reject_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -105,11 +97,9 @@ void MyRejectHandler(
Error_Detected = true;
}
static uint8_t Send_Atomic_Write_File_Stream(
uint32_t device_id,
static uint8_t Send_Atomic_Write_File_Stream(uint32_t device_id,
uint32_t file_instance,
int fileStartPosition,
BACNET_OCTET_STRING *fileData)
int fileStartPosition, BACNET_OCTET_STRING * fileData)
{
BACNET_ADDRESS dest;
BACNET_ADDRESS my_address;
@@ -125,14 +115,9 @@ static uint8_t Send_Atomic_Write_File_Stream(
/* is there a tsm available? */
if (status)
status = tsm_transaction_available();
if (status)
{
if (status) {
datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, &my_address, true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID();
@@ -142,40 +127,31 @@ static uint8_t Send_Atomic_Write_File_Stream(
data.access = FILE_STREAM_ACCESS;
data.type.stream.fileStartPosition = fileStartPosition;
status = octetstring_copy(&data.fileData, fileData);
if (status)
{
pdu_len += awf_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
invoke_id,
&data);
if (status) {
pdu_len += awf_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
invoke_id, &data);
/* will the APDU fit the target device?
note: if there is a bottleneck router in between
us and the destination, we won't know unless
we have a way to check for that and update the
max_apdu in the address binding table. */
if ((unsigned)pdu_len <= max_apdu)
{
tsm_set_confirmed_unsegmented_transaction(
invoke_id,
&dest,
&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 ((unsigned) pdu_len <= max_apdu) {
tsm_set_confirmed_unsegmented_transaction(invoke_id,
&dest, &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)
fprintf(stderr,"Failed to Send AtomicWriteFile Request (%s)!\n",
fprintf(stderr,
"Failed to Send AtomicWriteFile Request (%s)!\n",
strerror(errno));
}
else
} else
fprintf(stderr, "Failed to Send AtomicWriteFile Request "
"(payload [%d] exceeds destination maximum APDU [%u])!\n",
pdu_len, max_apdu);
}
else
} else
fprintf(stderr, "Failed to Send AtomicWriteFile Request "
"(payload [%d] exceeds octet string capacity)!\n",pdu_len);
"(payload [%d] exceeds octet string capacity)!\n",
pdu_len);
}
return invoke_id;
@@ -191,31 +167,22 @@ static void Send_WhoIs(uint32_t device_id)
datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, // true for confirmed messages
pdu_len = npdu_encode_apdu(&Handler_Transmit_Buffer[0], &dest, NULL, false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */
pdu_len += whois_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
device_id,
device_id);
pdu_len += whois_encode_apdu(&Handler_Transmit_Buffer[pdu_len],
device_id, device_id);
bytes_sent = datalink_send_pdu(
&dest, /* destination address */
&Handler_Transmit_Buffer[0],
pdu_len); /* number of bytes of data */
bytes_sent = datalink_send_pdu(&dest, /* destination address */
&Handler_Transmit_Buffer[0], pdu_len); /* number of bytes of data */
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(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src)
static void LocalIAmHandler(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
{
int len = 0;
uint32_t device_id = 0;
@@ -225,19 +192,11 @@ static void LocalIAmHandler(
(void) src;
(void) service_len;
len = iam_decode_service_request(
service_request,
&device_id,
&max_apdu,
&segmentation,
&vendor_id);
if (len != -1)
{
address_add(device_id,
max_apdu,
src);
}
else
len = iam_decode_service_request(service_request,
&device_id, &max_apdu, &segmentation, &vendor_id);
if (len != -1) {
address_add(device_id, max_apdu, src);
} else
fprintf(stderr, "!\n");
return;
@@ -247,29 +206,23 @@ static void Init_Service_Handlers(void)
{
/* we need to handle who-is
to support dynamic device binding to us */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_IS,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is);
/* handle i-am to support binding to other devices */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_I_AM,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler);
/* set the handler for all the services we don't implement
It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler(
handler_unrecognized_service);
apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service);
/* we must implement read property - it's required! */
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property);
/* handle any errors coming back */
apdu_set_error_handler(
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
apdu_set_error_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
Atomic_Read_File_Error_Handler);
apdu_set_abort_handler(
MyAbortHandler);
apdu_set_reject_handler(
MyRejectHandler);
apdu_set_abort_handler(MyAbortHandler);
apdu_set_reject_handler(MyRejectHandler);
}
int main(int argc, char *argv[])
@@ -291,8 +244,7 @@ int main(int argc, char *argv[])
static BACNET_OCTET_STRING fileData;
size_t len = 0;
if (argc < 4)
{
if (argc < 4) {
/* FIXME: what about access method - record or stream? */
printf("%s device-instance file-instance local-name\r\n",
filename_remove_path(argv[0]));
@@ -302,14 +254,12 @@ int main(int argc, char *argv[])
Target_Device_Object_Instance = strtol(argv[1], NULL, 0);
Target_File_Object_Instance = strtol(argv[2], NULL, 0);
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",
Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
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",
Target_File_Object_Instance, BACNET_MAX_INSTANCE + 1);
return 1;
@@ -330,48 +280,33 @@ int main(int argc, char *argv[])
/* try to bind with the device */
Send_WhoIs(Target_Device_Object_Instance);
/* loop forever */
for (;;)
{
for (;;) {
/* increment timer - exit if timed out */
current_seconds = time(NULL);
/* returns 0 bytes on timeout */
pdu_len = bip_receive(
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
/* process */
if (pdu_len)
{
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
/* at least one second has passed */
if (current_seconds != last_seconds)
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000));
if (End_Of_File_Detected || Error_Detected)
{
tsm_timer_milliseconds(((current_seconds -
last_seconds) * 1000));
if (End_Of_File_Detected || Error_Detected) {
printf("\r\n");
break;
}
if (I_Am_Request)
{
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
}
else
{
} else {
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(
Target_Device_Object_Instance,
&max_apdu,
&Target_Address);
if (found)
{
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
/* calculate the smaller of our APDU size or theirs
and remove the overhead of the APDU (varies depending on size).
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;
/* has the previous invoke id expired or returned?
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)
fileStartPosition += requestedOctetCount;
/* we'll read the file in chunks
less than max_apdu to keep unsegmented */
pFile = fopen(Local_File_Name, "rb");
if (pFile)
{
(void)fseek(pFile,
fileStartPosition,
SEEK_SET);
if (pFile) {
(void) fseek(pFile, fileStartPosition, SEEK_SET);
len = fread(octetstring_value(&fileData), 1,
requestedOctetCount, pFile);
if (len < requestedOctetCount)
End_Of_File_Detected = true;
octetstring_truncate(&fileData, len);
fclose(pFile);
}
else
} else
End_Of_File_Detected = true;
printf("\rSending %d bytes",(fileStartPosition+len));
invoke_id = Send_Atomic_Write_File_Stream(
Target_Device_Object_Instance,
Target_File_Object_Instance,
fileStartPosition,
printf("\rSending %d bytes",
(fileStartPosition + len));
invoke_id =
Send_Atomic_Write_File_Stream
(Target_Device_Object_Instance,
Target_File_Object_Instance, fileStartPosition,
&fileData);
Current_Invoke_ID = invoke_id;
}
}
else
{
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
+51 -93
View File
@@ -59,35 +59,31 @@ static BACNET_OBJECT_TYPE Target_Object_Type = OBJECT_ANALOG_INPUT;
static BACNET_PROPERTY_ID Target_Object_Property = PROP_ACKED_TRANSITIONS;
/* array index value or 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 =
BACNET_APPLICATION_TAG_NULL;
static BACNET_APPLICATION_DATA_VALUE Target_Object_Property_Value = { 0 };
/* 0 if not set, 1..16 if set */
static uint8_t Target_Object_Property_Priority = 0;
static BACNET_ADDRESS Target_Address;
static bool Error_Detected = false;
static void MyErrorHandler(
BACNET_ADDRESS *src,
static void MyErrorHandler(BACNET_ADDRESS * src,
uint8_t invoke_id,
BACNET_ERROR_CLASS error_class,
BACNET_ERROR_CODE error_code)
BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code)
{
/* FIXME: verify src and invoke id */
(void) src;
(void) invoke_id;
printf("\r\nBACnet Error!\r\n");
printf("Error Class: %s\r\n",
bactext_error_class_name(error_class));
printf("Error Code: %s\r\n",
bactext_error_code_name(error_code));
printf("Error Class: %s\r\n", bactext_error_class_name(error_class));
printf("Error Code: %s\r\n", bactext_error_code_name(error_code));
Error_Detected = true;
}
void MyAbortHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t abort_reason)
void MyAbortHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t abort_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -98,10 +94,8 @@ void MyAbortHandler(
Error_Detected = true;
}
void MyRejectHandler(
BACNET_ADDRESS *src,
uint8_t invoke_id,
uint8_t reject_reason)
void MyRejectHandler(BACNET_ADDRESS * src,
uint8_t invoke_id, uint8_t reject_reason)
{
/* FIXME: verify src and invoke id */
(void) src;
@@ -112,8 +106,7 @@ void MyRejectHandler(
Error_Detected = true;
}
void MyWritePropertySimpleAckHandler(
BACNET_ADDRESS *src,
void MyWritePropertySimpleAckHandler(BACNET_ADDRESS * src,
uint8_t invoke_id)
{
(void) src;
@@ -125,33 +118,26 @@ static void Init_Service_Handlers(void)
{
/* we need to handle who-is
to support dynamic device binding to us */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_IS,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is);
/* handle i-am to support binding to other devices */
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_I_AM,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
handler_i_am_bind);
/* set the handler for all the services we don't implement
It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler(
handler_unrecognized_service);
apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service);
/* we must implement read property - it's required! */
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property);
/* handle the ack coming back */
apdu_set_confirmed_simple_ack_handler(
SERVICE_CONFIRMED_WRITE_PROPERTY,
apdu_set_confirmed_simple_ack_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
MyWritePropertySimpleAckHandler);
/* handle any errors coming back */
apdu_set_error_handler(
SERVICE_CONFIRMED_WRITE_PROPERTY,
apdu_set_error_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
MyErrorHandler);
apdu_set_abort_handler(
MyAbortHandler);
apdu_set_reject_handler(
MyRejectHandler);
apdu_set_abort_handler(MyAbortHandler);
apdu_set_reject_handler(MyRejectHandler);
}
int main(int argc, char *argv[])
@@ -169,8 +155,7 @@ int main(int argc, char *argv[])
char *value_string = NULL;
bool status = false;
if (argc < 7)
{
if (argc < 7) {
/* note: priority 16 and 0 should produce the same end results... */
printf("Usage: %s device-instance object-type object-instance "
"property tag value [priority] [index]\r\n"
@@ -240,8 +225,7 @@ int main(int argc, char *argv[])
"You could also send a relinquish command:\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]));
return 0;
}
/* decode the command line parameters */
@@ -258,42 +242,34 @@ int main(int argc, char *argv[])
if (argc > 8)
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",
Target_Device_Object_Instance, BACNET_MAX_INSTANCE);
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",
Target_Object_Type, MAX_BACNET_OBJECT_TYPE + 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",
Target_Object_Instance, BACNET_MAX_INSTANCE + 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",
Target_Object_Property, MAX_BACNET_PROPERTY_ID + 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",
Target_Object_Property_Tag, MAX_BACNET_APPLICATION_TAG);
return 1;
}
status = bacapp_parse_application_data(
Target_Object_Property_Tag,
value_string,
&Target_Object_Property_Value);
if (!status)
{
status = bacapp_parse_application_data(Target_Object_Property_Tag,
value_string, &Target_Object_Property_Value);
if (!status) {
/* FIXME: show the expected entry format for the tag */
fprintf(stderr, "unable to parse the tag value\r\n");
return 1;
@@ -313,63 +289,45 @@ int main(int argc, char *argv[])
timeout_seconds = (Device_APDU_Timeout() / 1000) *
Device_Number_Of_APDU_Retries();
/* 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 */
for (;;)
{
for (;;) {
/* increment timer - exit if timed out */
current_seconds = time(NULL);
/* returns 0 bytes on timeout */
pdu_len = bip_receive(
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
/* process */
if (pdu_len)
{
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
/* at least one second has passed */
if (current_seconds != last_seconds)
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000));
tsm_timer_milliseconds(((current_seconds -
last_seconds) * 1000));
if (Error_Detected)
break;
if (I_Am_Request)
{
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
}
else
{
} else {
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(
Target_Device_Object_Instance,
&max_apdu,
&Target_Address);
if (found)
{
if (invoke_id == 0)
{
invoke_id = Send_Write_Property_Request(
Target_Device_Object_Instance,
Target_Object_Type,
Target_Object_Instance,
Target_Object_Property,
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
if (invoke_id == 0) {
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_Priority,
Target_Object_Property_Index);
}
else if (tsm_invoke_id_free(invoke_id))
} else if (tsm_invoke_id_free(invoke_id))
break;
}
else
{
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_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
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
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
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.
// FIXME: change this if you add a proprietary type
else if /*( */ (mstp_port->FrameType >=
FRAME_TYPE_PROPRIETARY_MIN)
{ /*&& */
FRAME_TYPE_PROPRIETARY_MIN) { /*&& */
/*(FrameType <= FRAME_TYPE_PROPRIETARY_MAX)) */
/* unnecessary if FrameType is uint8_t with max of 255 */
// an unexpected or unwanted frame was received.
+28 -52
View File
@@ -113,24 +113,20 @@ static int arcnet_bind(char *interface_name)
// modules.conf (or in modutils/alias on Debian with update-modules)
// alias net-pf-17 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 */
fprintf(stderr,
"arcnet: Error opening socket: %s\n",
strerror(errno));
"arcnet: Error opening socket: %s\n", strerror(errno));
fprintf(stderr,
"You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n"
"Also, add af_packet to /etc/modules.\n"
"Then follow it by:\n"
"# modprobe af_packet\n");
"Then follow it by:\n" "# modprobe af_packet\n");
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 */
ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET;
//ARCNET_Socket_Address.sa_family = PF_INET;
@@ -140,21 +136,19 @@ static int arcnet_bind(char *interface_name)
/* Strcpy the interface name into the address */
strncpy(ARCNET_Socket_Address.sa_data, interface_name,
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,
sizeof(ARCNET_Socket_Address)) != 0)
{
sizeof(ARCNET_Socket_Address)) != 0) {
/* Bind problem, close socket and return */
fprintf(stderr,
"arcnet: Unable to bind socket : %s\n",
strerror(errno));
"arcnet: Unable to bind socket : %s\n", strerror(errno));
fprintf(stderr,
"You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n"
"Also, add af_packet to /etc/modules.\n"
"Then follow it by:\n"
"# modprobe af_packet\n");
"Then follow it by:\n" "# modprobe af_packet\n");
/* Close the socket */
close(sock_fd);
exit(-1);
@@ -174,8 +168,7 @@ static int arcnet_bind(char *interface_name)
strncpy(ARCNET_Socket_Address.sa_data, interface_name,
sizeof(ARCNET_Socket_Address.sa_data) - 1);
fprintf(stderr, "arcnet: MAC=%02Xh iface=\"%s\"\n",
ARCNET_MAC_Address,
ARCNET_Socket_Address.sa_data);
ARCNET_MAC_Address, ARCNET_Socket_Address.sa_data);
atexit(arcnet_cleanup);
@@ -191,8 +184,7 @@ bool arcnet_init(char *interface_name)
/* function to send a packet out the socket */
/* returns number of bytes sent on success, negative on failure */
int arcnet_send(
BACNET_ADDRESS *dest, // destination address
int arcnet_send(BACNET_ADDRESS * dest, // destination address
BACNET_ADDRESS * src, // source address
uint8_t * pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data
@@ -203,28 +195,24 @@ int arcnet_send(
struct archdr *pkt = (struct archdr *) mtu;
// 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");
return -1;
}
/* load destination MAC address */
if (dest->mac_len == 1)
pkt->hard.dest = dest->mac[0];
else
{
else {
fprintf(stderr, "arcnet: invalid destination MAC address!\n");
return -2;
}
if (src->mac_len == 1)
pkt->hard.source = src->mac[0];
else
{
else {
fprintf(stderr, "arcnet: invalid source MAC address!\n");
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");
return -4;
}
@@ -251,8 +239,7 @@ int arcnet_send(
/* function to send a PDU out the socket */
/* returns number of bytes sent on success, negative on failure */
int arcnet_send_pdu(
BACNET_ADDRESS *dest, // destination address
int arcnet_send_pdu(BACNET_ADDRESS * dest, // destination address
uint8_t * pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data
{
@@ -268,8 +255,7 @@ int arcnet_send_pdu(
// receives an framed packet
// returns the number of octets in the PDU, or zero on failure
uint16_t arcnet_receive(
BACNET_ADDRESS *src, // source address
uint16_t arcnet_receive(BACNET_ADDRESS * src, // source address
uint8_t * pdu, // PDU data
uint16_t max_pdu, // amount of space available in the PDU
unsigned timeout) // milliseconds to wait for a packet
@@ -289,14 +275,11 @@ uint16_t arcnet_receive(
/* 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
a select. */
if (timeout >= 1000)
{
if (timeout >= 1000) {
select_timeout.tv_sec = timeout / 1000;
select_timeout.tv_usec =
1000 * (timeout - select_timeout.tv_sec * 1000);
}
else
{
} else {
select_timeout.tv_sec = 0;
select_timeout.tv_usec = 1000 * timeout;
}
@@ -315,7 +298,8 @@ uint16_t arcnet_receive(
// using O_NONBLOCK and no data
// was immediately available for reading.
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));
return 0;
}
@@ -338,22 +322,19 @@ uint16_t arcnet_receive(
return 0;
}
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");
return 0;
}
if ((pkt->soft.raw[1] != 0x82) || /* DSAP */
(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");
return 0;
}
/* It must be addressed to us or be a Broadcast */
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");
return 0;
}
@@ -381,31 +362,26 @@ void arcnet_get_my_address(BACNET_ADDRESS *my_address)
my_address->mac[0] = ARCNET_MAC_Address;
my_address->net = 0; // local only, no routing
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;
}
return;
}
void arcnet_get_broadcast_address(
BACNET_ADDRESS *dest) // destination address
void arcnet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address
{
int i = 0; // counter
if (dest)
{
if (dest) {
dest->mac[0] = ARCNET_BROADCAST;
dest->mac_len = 1;
dest->net = BACNET_BROADCAST_NETWORK;
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;
}
}
return;
}
+9 -13
View File
@@ -39,7 +39,8 @@
#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 rv; // return value
@@ -54,10 +55,8 @@ static int get_local_ifr_ioctl(char *ifname, struct ifreq *ifr, int request)
return rv;
}
static int get_local_address_ioctl(
char *ifname,
struct in_addr *addr,
int request)
static int get_local_address_ioctl(char *ifname,
struct in_addr *addr, int request)
{
struct ifreq ifr = { {{0}} };
struct sockaddr_in *tcpip_address;
@@ -87,7 +86,8 @@ void bip_set_interface(char *ifname)
get_local_address_ioctl(ifname, &broadcast_address, SIOCGIFBRDADDR);
bip_set_broadcast_addr(broadcast_address.s_addr);
#ifdef BIP_DEBUG
fprintf(stderr,"Broadcast Address: %s\n",inet_ntoa(broadcast_address));
fprintf(stderr, "Broadcast Address: %s\n",
inet_ntoa(broadcast_address));
#endif
}
@@ -108,8 +108,7 @@ bool bip_init(void)
sockopt = 1;
status = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR,
&sockopt, sizeof(sockopt));
if (status < 0)
{
if (status < 0) {
close(sock_fd);
bip_set_socket(-1);
return status;
@@ -117,13 +116,11 @@ bool bip_init(void)
// allow us to send a broadcast
status = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST,
&sockopt, sizeof(sockopt));
if (status < 0)
{
if (status < 0) {
close(sock_fd);
bip_set_socket(-1);
return status;
}
// bind the socket to the local port number and IP address
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
@@ -131,8 +128,7 @@ bool bip_init(void)
memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
status = bind(sock_fd,
(const struct sockaddr *) &sin, sizeof(struct sockaddr));
if (status < 0)
{
if (status < 0) {
close(sock_fd);
bip_set_socket(-1);
return false;
+37 -71
View File
@@ -45,6 +45,7 @@ uint8_t Ethernet_Broadcast[MAX_MAC_LEN] =
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
// commonly used empty address for ethernet quick compare
uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
// my local device data - MAC address
uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0 };
@@ -104,19 +105,16 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
// Then follow it by: # modprobe af_packet
/* 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 */
fprintf(stderr,
"ethernet: Error opening socket: %s\n",
strerror(errno));
"ethernet: Error opening socket: %s\n", strerror(errno));
fprintf(stderr,
"You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n"
"Also, add af_packet to /etc/modules.\n"
"Then follow it by:\n"
"# modprobe af_packet\n");
"Then follow it by:\n" "# modprobe af_packet\n");
exit(-1);
}
/* 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 */
memset(eth_addr->sa_data, '\0', sizeof(eth_addr->sa_data));
/* Strcpy the interface name into the address */
strncpy(eth_addr->sa_data, interface_name, sizeof(eth_addr->sa_data)-1);
strncpy(eth_addr->sa_data, interface_name,
sizeof(eth_addr->sa_data) - 1);
fprintf(stderr, "ethernet: binding \"%s\"\n", eth_addr->sa_data);
/* Attempt to bind the socket to the interface */
if (bind(sock_fd, eth_addr, sizeof(struct sockaddr)) != 0)
{
if (bind(sock_fd, eth_addr, sizeof(struct sockaddr)) != 0) {
/* Bind problem, close socket and return */
fprintf(stderr,
"ethernet: Unable to bind 802.2 socket : %s\n",
@@ -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"
"alias net-pf-17 af_packet\n"
"Also, add af_packet to /etc/modules.\n"
"Then follow it by:\n"
"# modprobe af_packet\n");
"Then follow it by:\n" "# modprobe af_packet\n");
/* Close the socket */
close(sock_fd);
exit(-1);
@@ -174,16 +171,14 @@ static int get_local_hwaddr(const char *ifname, unsigned char *mac)
bool ethernet_init(char *interface_name)
{
get_local_hwaddr(interface_name, Ethernet_MAC_Address);
eth802_sockfd =
ethernet_bind(&eth_addr, interface_name);
eth802_sockfd = ethernet_bind(&eth_addr, interface_name);
return ethernet_valid();
}
/* function to send a packet out the 802.2 socket */
/* returns bytes sent success, negative on failure */
int ethernet_send(
BACNET_ADDRESS *dest, // destination address
int ethernet_send(BACNET_ADDRESS * dest, // destination address
BACNET_ADDRESS * src, // source address
uint8_t * pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data
@@ -194,42 +189,32 @@ int ethernet_send(
int i = 0;
// 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");
return -1;
}
/* load destination ethernet MAC address */
if (dest->mac_len == 6)
{
for (i = 0; i < 6; i++)
{
if (dest->mac_len == 6) {
for (i = 0; i < 6; i++) {
mtu[mtu_len] = dest->mac[i];
mtu_len++;
}
}
else
{
} else {
fprintf(stderr, "ethernet: invalid destination MAC address!\n");
return -2;
}
/* load source ethernet MAC address */
if (src->mac_len == 6)
{
for (i = 0; i < 6; i++)
{
if (src->mac_len == 6) {
for (i = 0; i < 6; i++) {
mtu[mtu_len] = src->mac[i];
mtu_len++;
}
}
else
{
} else {
fprintf(stderr, "ethernet: invalid source MAC address!\n");
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");
return -4;
}
@@ -257,16 +242,14 @@ int ethernet_send(
/* function to send a packet out the 802.2 socket */
/* returns number of bytes sent on success, negative on failure */
int ethernet_send_pdu(
BACNET_ADDRESS *dest, // destination address
int ethernet_send_pdu(BACNET_ADDRESS * dest, // destination address
uint8_t * pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data
{
int i = 0; // counter
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_len++;
}
@@ -280,8 +263,7 @@ int ethernet_send_pdu(
// receives an 802.2 framed packet
// returns the number of octets in the PDU, or zero on failure
uint16_t ethernet_receive(
BACNET_ADDRESS *src, // source address
uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address
uint8_t * pdu, // PDU data
uint16_t max_pdu, // amount of space available in the PDU
unsigned timeout) // number of milliseconds to wait for a packet
@@ -300,14 +282,11 @@ uint16_t ethernet_receive(
/* 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
a select. */
if (timeout >= 1000)
{
if (timeout >= 1000) {
select_timeout.tv_sec = timeout / 1000;
select_timeout.tv_usec =
1000 * (timeout - select_timeout.tv_sec * 1000);
}
else
{
} else {
select_timeout.tv_sec = 0;
select_timeout.tv_usec = 1000 * timeout;
}
@@ -326,7 +305,8 @@ uint16_t ethernet_receive(
// using O_NONBLOCK and no data
// was immediately available for reading.
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));
return 0;
}
@@ -346,8 +326,7 @@ uint16_t ethernet_receive(
// check destination address for when
// the Ethernet card is in promiscious mode
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");
return 0;
}
@@ -369,8 +348,7 @@ void ethernet_set_my_address(BACNET_ADDRESS *my_address)
{
int i = 0;
for (i = 0; i < 6; i++)
{
for (i = 0; i < 6; i++) {
Ethernet_MAC_Address[i] = my_address->mac[i];
}
@@ -382,37 +360,31 @@ void ethernet_get_my_address(BACNET_ADDRESS *my_address)
int i = 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_len++;
}
my_address->net = 0; // local only, no routing
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;
}
return;
}
void ethernet_get_broadcast_address(
BACNET_ADDRESS *dest) // destination address
void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address
{
int i = 0; // counter
if (dest)
{
for (i = 0; i < 6; i++)
{
if (dest) {
for (i = 0; i < 6; i++) {
dest->mac[i] = Ethernet_Broadcast[i];
}
dest->mac_len = 6;
dest->net = BACNET_BROADCAST_NETWORK;
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;
}
}
@@ -420,29 +392,24 @@ void ethernet_get_broadcast_address(
return;
}
void ethernet_debug_address(
const char *info,
BACNET_ADDRESS *dest)
void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest)
{
int i = 0; // counter
if (info)
fprintf(stderr, "%s", info);
if (dest)
{
if (dest) {
fprintf(stderr, "Address:\n");
fprintf(stderr, " MAC Length=%d\n", dest->mac_len);
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, "\n");
fprintf(stderr, " Net=%hu\n", dest->net);
fprintf(stderr, " Len=%d\n", dest->len);
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, "\n");
@@ -450,4 +417,3 @@ void ethernet_debug_address(
return;
}
+35 -77
View File
@@ -50,10 +50,8 @@ bool Who_Is_Request = true;
// buffers used for receiving
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
static void LocalIAmHandler(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src)
static void LocalIAmHandler(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
{
int len = 0;
uint32_t device_id = 0;
@@ -63,21 +61,13 @@ static void LocalIAmHandler(
(void) src;
(void) service_len;
len = iam_decode_service_request(
service_request,
&device_id,
&max_apdu,
&segmentation,
&vendor_id);
len = iam_decode_service_request(service_request,
&device_id, &max_apdu, &segmentation, &vendor_id);
fprintf(stderr, "Received I-Am Request");
if (len != -1)
{
if (len != -1) {
fprintf(stderr, " from %u!\n", device_id);
address_add(device_id,
max_apdu,
src);
}
else
address_add(device_id, max_apdu, src);
} else
fprintf(stderr, "!\n");
return;
@@ -96,8 +86,7 @@ static void Read_Properties(void)
properties in the Device Object. Note that this demo
tests for error messages so that the device doesn't have
to have all the properties listed here. */
const int object_props[] =
{
const int object_props[] = {
PROP_OBJECT_IDENTIFIER,
PROP_OBJECT_NAME,
PROP_OBJECT_TYPE,
@@ -136,32 +125,24 @@ static void Read_Properties(void)
-1
};
if (address_count())
{
if (address_get_by_index(index, &device_id, &max_apdu, &src))
{
if (address_count()) {
if (address_get_by_index(index, &device_id, &max_apdu, &src)) {
if (object_props[property] < 0)
next_device = true;
else
{
else {
/* note: if we wanted to do this synchronously, we would get the
invoke ID from the sending of the request, and wait until we
got the reply with matching invoke ID or the TSM of the
invoke ID expired. This demo is doing things asynchronously. */
status = Send_Read_Property_Request(
device_id, // destination device
status = Send_Read_Property_Request(device_id, // destination device
OBJECT_DEVICE,
device_id,
object_props[property],
BACNET_ARRAY_ALL);
device_id, object_props[property], BACNET_ARRAY_ALL);
if (status)
property++;
}
}
else
} else
next_device = true;
if (next_device)
{
if (next_device) {
next_device = false;
index++;
if (index >= MAX_ADDRESS_CACHE)
@@ -176,34 +157,27 @@ static void Read_Properties(void)
static void Init_Service_Handlers(void)
{
// we need to handle who-is to support dynamic device binding
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_IS,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is);
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_I_AM,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler);
// set the handler for all the services we don't implement
// It is required to send the proper reject message...
apdu_set_unrecognized_service_handler_handler(
handler_unrecognized_service);
apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service);
// Set the handlers for any confirmed services that we support.
// We must implement read property - it's required!
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property);
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_WRITE_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
handler_write_property);
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
handler_atomic_read_file);
// handle the data coming back from confirmed requests
apdu_set_confirmed_ack_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property_ack);
apdu_set_confirmed_ack_handler(
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE,
handler_atomic_read_file_ack);
}
@@ -215,13 +189,10 @@ static void print_address_cache(void)
unsigned max_apdu = 0;
fprintf(stderr, "Device\tMAC\tMaxAPDU\tNet\n");
for (i = 0; i < MAX_ADDRESS_CACHE; i++)
{
if (address_get_by_index(i,&device_id, &max_apdu, &address))
{
for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
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++)
{
for (j = 0; j < address.mac_len; j++) {
fprintf(stderr, "%02X", address.mac[j]);
}
fprintf(stderr, "\t");
@@ -284,44 +255,31 @@ int main(int argc, char *argv[])
#endif
// loop forever
for (;;)
{
for (;;) {
// input
new_time = time(NULL);
// returns 0 bytes on timeout
pdu_len = datalink_receive(
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
// process
if (pdu_len)
{
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
if (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);
start_time = new_time;
}
if (I_Am_Request)
{
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
} else if (Who_Is_Request)
{
} else if (Who_Is_Request) {
Who_Is_Request = false;
Send_WhoIs(-1, -1);
}
// output
// some round robin task switching
count++;
switch (count)
{
switch (count) {
case 1:
// used for testing, but kind of noisy on the network
//Read_Properties();
+5 -11
View File
@@ -44,22 +44,19 @@
#include "mstp.h"
// Transmits a Frame on the wire
void RS485_Send_Frame(
struct mstp_port_struct_t *mstp_port, // port specific data
void RS485_Send_Frame(struct mstp_port_struct_t *mstp_port, // port specific data
uint8_t * buffer, // frame to send (up to 501 bytes of data)
uint16_t nbytes) // number of bytes of data (up to 501)
{
// in order to avoid line contention
while (mstp_port->Turn_Around_Waiting)
{
while (mstp_port->Turn_Around_Waiting) {
// wait, yield, or whatever
}
// Disable the receiver, and enable the transmit line driver.
while (nbytes)
{
while (nbytes) {
putc(*buffer, stderr);
buffer++;
nbytes--;
@@ -76,13 +73,11 @@ void RS485_Send_Frame(
// called by timer, interrupt(?) or other thread
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 read from the DataRegister
else if (mstp_port->DataAvailable == false)
{
else if (mstp_port->DataAvailable == false) {
// check for data
// if error,
@@ -96,4 +91,3 @@ void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port)
// return;
}
}
+25 -20
View File
@@ -62,8 +62,7 @@ static void Error(const char * Msg)
/*-----------------------------------*/
void InterfaceCleanup(void)
{
if (interface != SOCKET_ERROR)
{
if (interface != SOCKET_ERROR) {
xn_interface_close(interface);
interface = SOCKET_ERROR;
#if DEVICE_ID == PRISM_PCMCIA_DEVICE
@@ -100,7 +99,6 @@ static void NetInitialize(void)
#ifdef DHCP
XN_REGISTER_DHCP_CLI() // and optionally the DHCP client
#endif
Result = xn_rtip_init(); // Initialize the RTIP stack
if (Result != 0)
Error("xn_rtip_init failed");
@@ -119,23 +117,25 @@ static void NetInitialize(void)
#endif
// 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)
Error("xn_interface_open_config failed");
else
{
else {
struct _iface_info ii;
#ifdef BACDL_ETHERNET
BACNET_ADDRESS my_address;
unsigned i;
#endif
xn_interface_info(interface, &ii);
printf("Interface opened, MAC address: %02x-%02x-%02x-%02x-%02x-%02x\n",
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]);
printf
("Interface opened, MAC address: %02x-%02x-%02x-%02x-%02x-%02x\n",
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
for (i = 0; i < 6; i++)
{
for (i = 0; i < 6; i++) {
my_address.mac[i] = ii.my_ethernet_address[i];
}
ethernet_set_my_address(&my_address);
@@ -157,16 +157,20 @@ static void NetInitialize(void)
Result = xn_autoip(interface, MinIP, MaxIP, NetMask, TargetIP);
if (Result == SOCKET_ERROR)
Error("xn_autoip failed");
else
{
printf("Auto-assigned IP address %i.%i.%i.%i\n", TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]);
else {
printf("Auto-assigned IP address %i.%i.%i.%i\n", TargetIP[0],
TargetIP[1], TargetIP[2], TargetIP[3]);
// 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);
}
#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_conf DC;
@@ -178,11 +182,13 @@ static void NetInitialize(void)
if (Result == SOCKET_ERROR)
Error("xn_dhcp failed");
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
// 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);
// define default gateway and DNS server
xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF);
@@ -239,8 +245,7 @@ bool bip_init(void)
memset(&(sin.sin_zero), '\0', 8);
rv = bind(sock_fd,
(const struct sockaddr *) &sin, sizeof(struct sockaddr));
if (rv < 0)
{
if (rv < 0) {
close(sock_fd);
bip_set_socket(-1);
return false;
+32 -60
View File
@@ -40,6 +40,7 @@ uint8_t Ethernet_Broadcast[MAX_MAC_LEN] =
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
// commonly used empty address for ethernet quick compare
uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
// my local device data - MAC address
uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
@@ -83,8 +84,7 @@ bool ethernet_init(char *interface_name)
/* function to send a packet out the 802.2 socket */
/* returns bytes sent on success, negative number on failure */
int ethernet_send(
BACNET_ADDRESS *dest, // destination address
int ethernet_send(BACNET_ADDRESS * dest, // destination address
BACNET_ADDRESS * src, // source address
uint8_t * pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data
@@ -95,42 +95,32 @@ int ethernet_send(
int i = 0;
// 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");
return -1;
}
/* load destination ethernet MAC address */
if (dest->mac_len == 6)
{
for (i = 0; i < 6; i++)
{
if (dest->mac_len == 6) {
for (i = 0; i < 6; i++) {
mtu[mtu_len] = dest->mac[i];
mtu_len++;
}
}
else
{
} else {
fprintf(stderr, "ethernet: invalid destination MAC address!\n");
return -2;
}
/* load source ethernet MAC address */
if (src->mac_len == 6)
{
for (i = 0; i < 6; i++)
{
if (src->mac_len == 6) {
for (i = 0; i < 6; i++) {
mtu[mtu_len] = src->mac[i];
mtu_len++;
}
}
else
{
} else {
fprintf(stderr, "ethernet: invalid source MAC address!\n");
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");
return -4;
}
@@ -145,8 +135,7 @@ int ethernet_send(
mtu_len += pdu_len;
/* Send the packet */
bytes =
send(Ethernet_Socket, (const char *)&mtu, mtu_len, 0);
bytes = send(Ethernet_Socket, (const char *) &mtu, mtu_len, 0);
/* did it get sent? */
if (bytes < 0)
fprintf(stderr, "ethernet: Error sending packet: %s\n",
@@ -157,16 +146,14 @@ int ethernet_send(
/* function to send a packet out the 802.2 socket */
/* returns bytes sent on success, negative number on failure */
int ethernet_send_pdu(
BACNET_ADDRESS *dest, // destination address
int ethernet_send_pdu(BACNET_ADDRESS * dest, // destination address
uint8_t * pdu, // any data to be sent - may be null
unsigned pdu_len) // number of bytes of data
{
int i = 0; // counter
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_len++;
}
@@ -180,8 +167,7 @@ int ethernet_send_pdu(
// receives an 802.2 framed packet
// returns the number of octets in the PDU, or zero on failure
uint16_t ethernet_receive(
BACNET_ADDRESS *src, // source address
uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address
uint8_t * pdu, // PDU data
uint16_t max_pdu, // amount of space available in the PDU
unsigned timeout) // number of milliseconds to wait for a packet
@@ -200,14 +186,11 @@ uint16_t ethernet_receive(
/* 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
a select. */
if (timeout >= 1000)
{
if (timeout >= 1000) {
select_timeout.tv_sec = timeout / 1000;
select_timeout.tv_usec =
1000 * (timeout - select_timeout.tv_sec * 1000);
}
else
{
} else {
select_timeout.tv_sec = 0;
select_timeout.tv_usec = 1000 * timeout;
}
@@ -216,7 +199,8 @@ uint16_t ethernet_receive(
max = Ethernet_Socket;
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
return 0;
@@ -226,7 +210,8 @@ uint16_t ethernet_receive(
// using O_NONBLOCK and no data
// was immediately available for reading.
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));
return 0;
}
@@ -246,8 +231,7 @@ uint16_t ethernet_receive(
// check destination address for when
// the Ethernet card is in promiscious mode
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");
return 0;
}
@@ -270,15 +254,13 @@ void ethernet_get_my_address(BACNET_ADDRESS *my_address)
int i = 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_len++;
}
my_address->net = 0; // local only, no routing
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;
}
@@ -289,30 +271,25 @@ void ethernet_set_my_address(BACNET_ADDRESS *my_address)
{
int i = 0;
for (i = 0; i < 6; i++)
{
for (i = 0; i < 6; i++) {
Ethernet_MAC_Address[i] = my_address->mac[i];
}
return;
}
void ethernet_get_broadcast_address(
BACNET_ADDRESS *dest) // destination address
void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address
{
int i = 0; // counter
if (dest)
{
for (i = 0; i < 6; i++)
{
if (dest) {
for (i = 0; i < 6; i++) {
dest->mac[i] = Ethernet_Broadcast[i];
}
dest->mac_len = 6;
dest->net = BACNET_BROADCAST_NETWORK;
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;
}
}
@@ -320,29 +297,24 @@ void ethernet_get_broadcast_address(
return;
}
void ethernet_debug_address(
const char *info,
BACNET_ADDRESS *dest)
void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest)
{
int i = 0; // counter
if (info)
fprintf(stderr, "%s", info);
if (dest)
{
if (dest) {
fprintf(stderr, "Address:\n");
fprintf(stderr, " MAC Length=%d\n", dest->mac_len);
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, "\n");
fprintf(stderr, " Net=%hu\n", dest->net);
fprintf(stderr, " Len=%d\n", dest->len);
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, "\n");
+4 -8
View File
@@ -50,8 +50,7 @@ extern void RTEmuInit(void);
static RTFDrvDOCData DOCDriveData = { 0 };
static RTFDrvIDEData IDEDriveData = { 0 };
RTFDevice RTFDeviceList[] =
{
RTFDevice RTFDeviceList[] = {
/* type,number,flags,driver,driverdata */
{RTF_DEVICE_FLOPPY, 0, 0, &RTFDrvFloppy, &FLPYDriveAData},
{RTF_DEVICE_FDISK, 0, 0, &RTFDrvDOC, &DOCDriveData},
@@ -85,11 +84,9 @@ int RTTypeCount = MAXTYPES;
that here, as well as the LPT, console, and FAT.
From RTFiles-32 manual, ch. 7, "Using RTFiles-32 with
RTTarget-32" */
RTFileSystem Console =
{ RT_FS_CONSOLE, 0, 0, &RTConsoleFileSystem };
RTFileSystem Console = { RT_FS_CONSOLE, 0, 0, &RTConsoleFileSystem };
RTFileSystem LPTFiles =
{ RT_FS_LPT_DEVICE, 0, 0, &RTLPTFileSystem };
RTFileSystem LPTFiles = { RT_FS_LPT_DEVICE, 0, 0, &RTLPTFileSystem };
/* logical drive Z: can be used to access the RAM drive */
RTFileSystem RAMFiles =
@@ -99,8 +96,7 @@ RTFileSystem RAMFiles =
RTFileSystem FATFiles =
{ RT_FS_FILE | RT_FS_IS_DEFAULT, 0x0F, 0x03, &RTFilesFileSystem };
RTFileSystem *RTFileSystemList[] =
{
RTFileSystem *RTFileSystemList[] = {
&Console,
&LPTFiles,
&RAMFiles,
+11 -24
View File
@@ -38,6 +38,7 @@
// buffers used for transmit and receive
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
#ifdef BACDL_MSTP
volatile struct mstp_port_struct_t MSTP_Port; // port data
static uint8_t MSTP_MAC_Address = 0x05; // local MAC address
@@ -46,19 +47,15 @@ static uint8_t MSTP_MAC_Address = 0x05; // local MAC address
static void Init_Service_Handlers(void)
{
// we need to handle who-is to support dynamic device binding
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_IS,
WhoIsHandler);
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, WhoIsHandler);
// set the handler for all the services we don't implement
// It is required to send the proper reject message...
apdu_set_unrecognized_service_handler_handler(
UnrecognizedServiceHandler);
apdu_set_unrecognized_service_handler_handler
(UnrecognizedServiceHandler);
// we must implement read property - it's required!
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
ReadPropertyHandler);
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_WRITE_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
WritePropertyHandler);
}
@@ -88,8 +85,7 @@ int main(int argc, char *argv[])
// loop forever
for (;;)
{
for (;;) {
// input
#ifdef BACDL_MSTP
MSTP_Millisecond_Timer(&MSTP_Port);
@@ -100,24 +96,15 @@ int main(int argc, char *argv[])
#if (defined(BACDL_ETHERNET) || defined(BACDL_BIP))
// returns 0 bytes on timeout
pdu_len = bacdl_receive(
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
pdu_len = bacdl_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
#endif
// process
if (pdu_len)
{
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
if (I_Am_Request)
{
if (I_Am_Request) {
I_Am_Request = false;
Send_IAm();
}
+14 -21
View File
@@ -44,10 +44,10 @@ static long RS485_Base = 0;
/* hardware IRQ number */
static long RS485_IRQ_Number = 0;
static void RS485_Standard_Port_Settings(long port, long *pIRQ, long *pBase)
{
switch (port)
static void RS485_Standard_Port_Settings(long port, long *pIRQ,
long *pBase)
{
switch (port) {
case COM1:
*pBase = (long) 0x3F8;
*pIRQ = 4L;
@@ -69,12 +69,11 @@ static void RS485_Standard_Port_Settings(long port, long *pIRQ, long *pBase)
}
}
static RS485_Open_Port(
int port, /* COM port number - COM1 = 0 */
static RS485_Open_Port(int port, /* COM port number - COM1 = 0 */
long baud, /* baud rate */
unsigned base, /* io base address */
int irq) /* hardware IRQ number */
{
int irq)
{ /* hardware IRQ number */
/* setup the COM IO */
SetIOBase(port, base);
SetIRQ(port, irq);
@@ -97,12 +96,12 @@ static RS485_Open_Port(
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);
}
void RS485_Send_Frame(
volatile struct mstp_port_struct_t *mstp_port, // port specific data
void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, // port specific data
uint8_t * buffer, // frame to send (up to 501 bytes of data)
uint16_t nbytes) // number of bytes of data (up to 501)
{
@@ -120,33 +119,28 @@ void RS485_Send_Frame(
return;
}
void RS485_Check_UART_Data(
volatile struct mstp_port_struct_t *mstp_port) // port specific data
void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) // port specific data
{
COMData com_data = 0; /* byte from COM driver */
unsigned timeout = 10; // milliseconds to wait for a character
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 read from the DataRegister
else if (!mstp_port->DataAvailable)
{
else if (!mstp_port->DataAvailable) {
// check for data
ticks = MilliSecsToTicks(timeout);
if (!ticks)
ticks = 1;
if (RTKGetTimed(ReceiveBuffer[RS485_Port],&com_data,ticks))
{
if (RTKGetTimed(ReceiveBuffer[RS485_Port], &com_data, ticks)) {
// if error,
if (com_data & (COM_OVERRUN << 8))
mstp_port->ReceiveError = true;
else if (com_data & (COM_FRAME << 8))
mstp_port->ReceiveError = true;
else
{
else {
mstp_port->DataRegister = com_data & 0x00FF;
mstp_port->DataAvailable = true;
}
@@ -158,4 +152,3 @@ void RS485_Process_Tx_Message(void)
{
// nothing to do
}
+16 -23
View File
@@ -72,13 +72,17 @@ static void set_broadcast_address(uint32_t net_address)
bip_set_broadcast_addr(INADDR_BROADCAST);
#else
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)))
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)))
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)))
broadcast_address = (ntohl(net_address) & ~IN_CLASSD_HOST) | IN_CLASSD_HOST;
broadcast_address =
(ntohl(net_address) & ~IN_CLASSD_HOST) | IN_CLASSD_HOST;
else
broadcast_address = INADDR_BROADCAST;
bip_set_broadcast_addr(htonl(broadcast_address));
@@ -109,8 +113,7 @@ bool bip_init(void)
Result = WSAStartup((1 << 8) | 1, &wd);
//Result = WSAStartup(MAKEWORD(2,2), &wd);
if (Result != 0)
{
if (Result != 0) {
Code = WSAGetLastError();
printf("TCP/IP stack initialization failed, error code: %i\n",
Code);
@@ -119,11 +122,9 @@ bool bip_init(void)
atexit(cleanup);
address.s_addr = gethostaddr();
if (address.s_addr == (unsigned)-1)
{
if (address.s_addr == (unsigned) -1) {
Code = WSAGetLastError();
printf("Get host address failed, error code: %i\n",
Code);
printf("Get host address failed, error code: %i\n", Code);
exit(1);
}
#ifdef BIP_DEBUG
@@ -135,18 +136,15 @@ bool bip_init(void)
/* assumes that the driver has already been initialized */
sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
bip_set_socket(sock_fd);
if (sock_fd < 0)
{
if (sock_fd < 0) {
fprintf(stderr, "bip: failed to allocate a socket.\n");
return false;
}
// Allow us to use the same socket for sending and receiving
// This makes sure that the src port is correct when sending
rv = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR,
(char *) &value, sizeof(value));
if (rv < 0)
{
if (rv < 0) {
fprintf(stderr, "bip: failed to set REUSEADDR socket option.\n");
close(sock_fd);
bip_set_socket(-1);
@@ -155,14 +153,12 @@ bool bip_init(void)
// allow us to send a broadcast
rv = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST,
(char *) &value, sizeof(value));
if (rv < 0)
{
if (rv < 0) {
fprintf(stderr, "bip: failed to set BROADCAST socket option.\n");
close(sock_fd);
bip_set_socket(-1);
return false;
}
// bind the socket to the local port number and IP address
sin.sin_family = AF_INET;
#if USE_INADDR
@@ -191,11 +187,9 @@ bool bip_init(void)
memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
rv = bind(sock_fd,
(const struct sockaddr *) &sin, sizeof(struct sockaddr));
if (rv < 0)
{
if (rv < 0) {
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);
bip_set_socket(-1);
return false;
@@ -203,4 +197,3 @@ bool bip_init(void)
return true;
}
+36 -79
View File
@@ -44,6 +44,7 @@
// buffer used for receive
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* send a whois to see who is on the network */
static bool Who_Is_Request = true;
@@ -60,8 +61,7 @@ static void Read_Properties(void)
Device Object
note: you could just loop through
all the properties in all the objects. */
const int object_props[] =
{
const int object_props[] = {
PROP_OBJECT_IDENTIFIER,
PROP_OBJECT_NAME,
PROP_OBJECT_TYPE,
@@ -97,28 +97,20 @@ static void Read_Properties(void)
-1
};
if (address_count())
{
if (address_get_by_index(index, &device_id, &max_apdu, &src))
{
if (address_count()) {
if (address_get_by_index(index, &device_id, &max_apdu, &src)) {
if (object_props[property] < 0)
next_device = true;
else
{
status = Send_Read_Property_Request(
device_id, // destination device
else {
status = Send_Read_Property_Request(device_id, // destination device
OBJECT_DEVICE,
device_id,
object_props[property],
BACNET_ARRAY_ALL);
device_id, object_props[property], BACNET_ARRAY_ALL);
if (status)
property++;
}
}
else
} else
next_device = true;
if (next_device)
{
if (next_device) {
next_device = false;
index++;
if (index >= MAX_ADDRESS_CACHE)
@@ -130,10 +122,8 @@ static void Read_Properties(void)
return;
}
static void LocalIAmHandler(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src)
static void LocalIAmHandler(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
{
int len = 0;
uint32_t device_id = 0;
@@ -143,21 +133,13 @@ static void LocalIAmHandler(
(void) src;
(void) service_len;
len = iam_decode_service_request(
service_request,
&device_id,
&max_apdu,
&segmentation,
&vendor_id);
len = iam_decode_service_request(service_request,
&device_id, &max_apdu, &segmentation, &vendor_id);
fprintf(stderr, "Received I-Am Request");
if (len != -1)
{
if (len != -1) {
fprintf(stderr, " from %u!\n", device_id);
address_add(device_id,
max_apdu,
src);
}
else
address_add(device_id, max_apdu, src);
} else
fprintf(stderr, "!\n");
return;
@@ -166,41 +148,32 @@ static void LocalIAmHandler(
static void Init_Service_Handlers(void)
{
// we need to handle who-is to support dynamic device binding
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_WHO_IS,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is);
apdu_set_unconfirmed_handler(
SERVICE_UNCONFIRMED_I_AM,
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM,
LocalIAmHandler);
// set the handler for all the services we don't implement
// It is required to send the proper reject message...
apdu_set_unrecognized_service_handler_handler(
handler_unrecognized_service);
apdu_set_unrecognized_service_handler_handler
(handler_unrecognized_service);
// we must implement read property - it's required!
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property);
apdu_set_confirmed_handler(
SERVICE_CONFIRMED_WRITE_PROPERTY,
apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
handler_write_property);
// handle the data coming back from confirmed requests
apdu_set_confirmed_ack_handler(
SERVICE_CONFIRMED_READ_PROPERTY,
apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY,
handler_read_property_ack);
}
static void print_address(
char *name,
BACNET_ADDRESS *dest) // destination address
static void print_address(char *name, BACNET_ADDRESS * dest) // destination address
{
int i = 0; // counter
if (dest)
{
if (dest) {
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("\n");
@@ -215,13 +188,10 @@ static void print_address_cache(void)
unsigned max_apdu = 0;
fprintf(stderr, "Device\tMAC\tMaxAPDU\tNet\n");
for (i = 0; i < MAX_ADDRESS_CACHE; i++)
{
if (address_get_by_index(i,&device_id, &max_apdu, &address))
{
for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
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++)
{
for (j = 0; j < address.mac_len; j++) {
fprintf(stderr, "%02X", address.mac[j]);
}
fprintf(stderr, "\t");
@@ -255,37 +225,24 @@ int main(int argc, char *argv[])
printf("BACnet stack running...\n");
// loop forever
for (;;)
{
for (;;) {
// input
// returns 0 bytes on timeout
pdu_len = bip_receive(
&src,
&Rx_Buf[0],
MAX_MPDU,
timeout);
pdu_len = bip_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
// process
if (pdu_len)
{
npdu_handler(
&src,
&Rx_Buf[0],
pdu_len);
if (pdu_len) {
npdu_handler(&src, &Rx_Buf[0], pdu_len);
}
if (I_Am_Request)
{
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
} else if (Who_Is_Request)
{
} else if (Who_Is_Request) {
Who_Is_Request = false;
Send_WhoIs(-1, -1);
}
else
{
} else {
Read_Properties();
}