Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BDT Example and readme update #21

Merged
merged 15 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions BACnetServerExample/BACnetServerExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/

#include "CASBACnetStackAdapter.h"
// !!!!!! This file is part of the CAS BACnet Stack. Please contact Chipkin for more information.
// !!!!!! https://github.com/chipkin/BACnetServerExampleCPP/issues/8

#include "CASBACnetStackExampleConstants.h"
#include "CASBACnetStackExampleDatabase.h"
#include "CIBuildSettings.h"
Expand Down Expand Up @@ -50,15 +53,14 @@

#endif // __GNUC__


// Globals
// =======================================
CSimpleUDP g_udp; // UDP resource
ExampleDatabase g_database; // The example database that stores current values.

// Constants
// =======================================
const std::string APPLICATION_VERSION = "0.0.12"; // See CHANGELOG.md for a full list of changes.
const std::string APPLICATION_VERSION = "0.0.14"; // See CHANGELOG.md for a full list of changes.
const uint32_t MAX_RENDER_BUFFER_LENGTH = 1024 * 20;


Expand Down Expand Up @@ -108,6 +110,8 @@ bool HookTextMessage(const uint32_t sourceDeviceIdentifier, const bool useMessag
bool DoUserInput();
bool GetObjectName(const uint32_t deviceInstance, const uint16_t objectType, const uint32_t objectInstance, char* value, uint32_t* valueElementCount, const uint32_t maxElementCount);

// Debug Message Function
void CallbackLogDebugMessage(const char* message, const uint16_t messageLength, const uint8_t messageType);

int main(int argc, char** argv)
{
Expand Down Expand Up @@ -195,6 +199,10 @@ int main(int argc, char** argv)
fpRegisterCallbackDeviceCommunicationControl(CallbackDeviceCommunicationControl);
fpRegisterHookTextMessage(HookTextMessage);

// Get Debug Message Function
fpRegisterCallbackLogDebugMessage(CallbackLogDebugMessage);


// 4. Setup the BACnet device
// ---------------------------------------------------------------------------

Expand Down Expand Up @@ -566,7 +574,45 @@ int main(int argc, char** argv)
std::cerr << "Failed to add NetworkPort" << std::endl;
return -1;
}
std::cout << "OK" << std::endl;
fpSetPropertyEnabled(g_database.device.instance, CASBACnetStackExampleConstants::OBJECT_TYPE_NETWORK_PORT, g_database.networkPort.instance, CASBACnetStackExampleConstants::PROPERTY_IDENTIFIER_BBMD_ACCEPT_FD_REGISTRATIONS, true);
fpSetPropertyEnabled(g_database.device.instance, CASBACnetStackExampleConstants::OBJECT_TYPE_NETWORK_PORT, g_database.networkPort.instance, CASBACnetStackExampleConstants::PROPERTY_IDENTIFIER_BBMD_BROADCAST_DISTRIBUTION_TABLE, true);
fpSetPropertyEnabled(g_database.device.instance, CASBACnetStackExampleConstants::OBJECT_TYPE_NETWORK_PORT, g_database.networkPort.instance, CASBACnetStackExampleConstants::PROPERTY_IDENTIFIER_BBMD_FOREIGN_DEVICE_TABLE, true);

// Ask for BBMD Address, Port, and Mask
justin-wong-ce marked this conversation as resolved.
Show resolved Hide resolved
std::string bbmdIpStr, bbmdPortStr, bbmdIpMaskStr;
std::cout << "\nEnter BBMD IP Address (Format: WWW.XXX.YYY.ZZZ) (Enter empty string to use default value [192.168.1.208]):";
std::cin >> bbmdIpStr;
std::cout << "Enter BBMD IP Port (Enter [N] to use default value [47808])";
std::cin >> bbmdPortStr;
std::cout << "Enter BBMD IP Mask (Format: WWW.XXX.YYY.ZZZ) (Enter [N] to use default value [255.255.255.0]):";
std::cin >> bbmdIpMaskStr;
uint8_t bbmdIpAddress[6] = {192, 168, 1, 208, 0xBA, 0xC0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't used hard-coded variables in the example

uint8_t bbmdIpMask[4] = {255, 255, 255, 0};
uint8_t periodIndex;
if (bbmdIpStr != "N" && bbmdIpStr != "n") {
for (uint8_t i = 0; i < 3; i++) {
periodIndex = bbmdIpStr.find(".");
bbmdIpAddress[i] = std::atoi(bbmdIpStr.substr(0, periodIndex).c_str());
bbmdIpStr = bbmdIpStr.substr(periodIndex + 1);
}
bbmdIpAddress[3] = std::atoi(bbmdIpStr.c_str());
}
if (bbmdPortStr != "N" && bbmdPortStr != "n") {
bbmdIpAddress[4] = std::atoi(bbmdPortStr.c_str()) / 256;
bbmdIpAddress[5] = std::atoi(bbmdPortStr.c_str()) % 256;
}
if (bbmdIpMaskStr != "N" && bbmdIpMaskStr != "n") {
for (uint8_t i = 0; i < 3; i++) {
periodIndex = bbmdIpMaskStr.find(".");
bbmdIpMask[i] = std::atoi(bbmdIpMaskStr.substr(0, periodIndex).c_str());
bbmdIpMaskStr = bbmdIpMaskStr.substr(periodIndex + 1);
}
bbmdIpMask[3] = std::atoi(bbmdIpMaskStr.c_str());
}

fpAddBDTEntry(bbmdIpAddress, 6, bbmdIpMask, 4);
fpSetBBMD(g_database.device.instance, g_database.networkPort.instance);
std::cout << "... OK" << std::endl;

// 5. Send I-Am of this device
// ---------------------------------------------------------------------------
Expand All @@ -593,6 +639,7 @@ int main(int argc, char** argv)
// 6. Start the main loop
// ---------------------------------------------------------------------------
std::cout << "FYI: Entering main loop..." << std::endl ;
std::string debugMessage = "";
for (;;) {
// Call the DLLs loop function which checks for messages and processes them.
fpLoop();
Expand All @@ -610,6 +657,13 @@ int main(int argc, char** argv)
// Update values in the example database
g_database.Loop();

// Check for debug messages
if (debugMessage.c_str() != g_database.debugMessage && g_database.debugMessage != "") {
justin-wong-ce marked this conversation as resolved.
Show resolved Hide resolved
std::cout << "Debug message received! Message type: " << (g_database.debugMessageType == CASBACnetStackExampleConstants::BACNET_DEBUG_LOG_TYPE_ERROR ? "Error message" : "Info message") << std::endl;
std::cout << "Debug message: \n" << g_database.debugMessage << std::endl;
debugMessage = g_database.debugMessage;
}

// Call Sleep to give some time back to the system
Sleep(0); // Windows
}
Expand Down Expand Up @@ -2075,6 +2129,16 @@ bool CallbackDeviceCommunicationControl(const uint32_t deviceInstance, const uin
return true;
}

void CallbackLogDebugMessage(const char* message, const uint16_t messageLength, const uint8_t messageType) {
// This callback is called when the CAS BACnet Stack logs an error or info message
justin-wong-ce marked this conversation as resolved.
Show resolved Hide resolved
// In this callback, you will be able to access this debug message. This callback is optional.
if (message != NULL && messageLength != 0) {
g_database.debugMessageType = messageType;
g_database.debugMessage = std::string(message, messageLength);
}
return;
}

bool HookTextMessage(const uint32_t sourceDeviceIdentifier, const bool useMessageClass, const uint32_t messageClassUnsigned, const char* messageClassString, const uint32_t messageClassStringLength, const uint8_t messagePriority, const char* message, const uint32_t messageLength, const uint8_t* connectionString, const uint8_t connectionStringLength, const uint8_t networkType, const uint16_t sourceNetwork, const uint8_t* sourceAddress, const uint8_t sourceAddressLength, uint16_t* errorClass, uint16_t* errorCode) {
// Configured to respond to Client example Confirmed Text Message Requests
uint32_t expectedSourceDeviceIdentifier = 389002;
Expand Down
Loading