Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Add UDP channel support. #314

Merged
merged 7 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ if(DNP3_EXAMPLES)
add_subdirectory(./cpp/examples/decoder)
add_subdirectory(./cpp/examples/master)
add_subdirectory(./cpp/examples/master-gprs)
add_subdirectory(./cpp/examples/master-udp)
add_subdirectory(./cpp/examples/outstation)
add_subdirectory(./cpp/examples/outstation-udp)

if(DNP3_TLS)
add_subdirectory(./cpp/examples/tls/master)
Expand Down
3 changes: 3 additions & 0 deletions cpp/examples/master-udp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(master-udp-demo ./main.cpp)
target_link_libraries (master-udp-demo PRIVATE opendnp3)
set_target_properties(master-udp-demo PROPERTIES FOLDER cpp/examples)
157 changes: 157 additions & 0 deletions cpp/examples/master-udp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright 2013-2019 Automatak, LLC
*
* Licensed to Green Energy Corp (www.greenenergycorp.com) and Automatak
* LLC (www.automatak.com) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership. Green Energy Corp and Automatak LLC license
* this file to you under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may obtain
* a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <opendnp3/ConsoleLogger.h>
#include <opendnp3/DNP3Manager.h>
#include <opendnp3/LogLevels.h>
#include <opendnp3/channel/PrintingChannelListener.h>
#include <opendnp3/master/DefaultMasterApplication.h>
#include <opendnp3/master/PrintingCommandResultCallback.h>
#include <opendnp3/master/PrintingSOEHandler.h>

using namespace std;
using namespace opendnp3;

int main(int argc, char* argv[])
{
// Specify what log levels to use. NORMAL is warning and above
// You can add all the comms logging by uncommenting below
const auto logLevels = levels::ALL;

// This is the main point of interaction with the stack
DNP3Manager manager(1, ConsoleLogger::Create());

// Connect via a UDP socket to a outstation
auto channel = manager.AddUDPChannel("udpclient", logLevels, ChannelRetry::Default(), IPEndpoint("0.0.0.0", 20000),
IPEndpoint("192.168.0.106", 19999), PrintingChannelListener::Create());

// The master config object for a master. The default are
// useable, but understanding the options are important.
MasterStackConfig stackConfig;

// you can override application layer settings for the master here
// in this example, we've change the application layer timeout to 2 seconds
stackConfig.master.responseTimeout = TimeDuration::Seconds(2);
stackConfig.master.disableUnsolOnStartup = true;

// You can override the default link layer settings here
// in this example we've changed the default link layer addressing
stackConfig.link.LocalAddr = 1;
stackConfig.link.RemoteAddr = 10;

// Create a new master on a previously declared port, with a
// name, log level, command acceptor, and config info. This
// returns a thread-safe interface used for sending commands.
auto master = channel->AddMaster("master", // id for logging
PrintingSOEHandler::Create(), // callback for data processing
DefaultMasterApplication::Create(), // master application instance
stackConfig // stack configuration
);

// do an integrity poll (Class 3/2/1/0) once per minute
auto integrityScan = master->AddClassScan(ClassField::AllClasses(), TimeDuration::Minutes(1));

// do a Class 1 exception poll every 5 seconds
auto exceptionScan = master->AddClassScan(ClassField(ClassField::CLASS_1), TimeDuration::Seconds(2));

// Enable the master. This will start communications.
master->Enable();

bool channelCommsLoggingEnabled = true;
bool masterCommsLoggingEnabled = true;

while (true)
{
std::cout << "Enter a command" << std::endl;
std::cout << "x - exits program" << std::endl;
std::cout << "a - performs an ad-hoc range scan" << std::endl;
std::cout << "i - integrity demand scan" << std::endl;
std::cout << "e - exception demand scan" << std::endl;
std::cout << "d - disable unsolicited" << std::endl;
std::cout << "r - cold restart" << std::endl;
std::cout << "c - send crob" << std::endl;
std::cout << "t - toggle channel logging" << std::endl;
std::cout << "u - toggle master logging" << std::endl;

char cmd;
std::cin >> cmd;
switch (cmd)
{
case ('a'):
master->ScanRange(GroupVariationID(1, 2), 0, 3);
break;
case ('d'):
master->PerformFunction("disable unsol", FunctionCode::DISABLE_UNSOLICITED,
{Header::AllObjects(60, 2), Header::AllObjects(60, 3), Header::AllObjects(60, 4)});
break;
case ('r'):
{
auto print = [](const RestartOperationResult& result) {
if (result.summary == TaskCompletion::SUCCESS)
{
std::cout << "Success, Time: " << result.restartTime.ToString() << std::endl;
}
else
{
std::cout << "Failure: " << TaskCompletionSpec::to_string(result.summary) << std::endl;
}
};
master->Restart(RestartType::COLD, print);
break;
}
case ('x'):
// C++ destructor on DNP3Manager cleans everything up for you
return 0;
case ('i'):
integrityScan->Demand();
break;
case ('e'):
exceptionScan->Demand();
break;
case ('c'):
{
ControlRelayOutputBlock crob(ControlCode::LATCH_ON);
master->SelectAndOperate(crob, 0, PrintingCommandResultCallback::Get());
break;
}
case ('t'):
{
channelCommsLoggingEnabled = !channelCommsLoggingEnabled;
auto levels = channelCommsLoggingEnabled ? levels::ALL_COMMS : levels::NORMAL;
channel->SetLogFilters(levels);
std::cout << "Channel logging set to: " << levels.get_value() << std::endl;
break;
}
case ('u'):
{
masterCommsLoggingEnabled = !masterCommsLoggingEnabled;
auto levels = masterCommsLoggingEnabled ? levels::ALL_COMMS : levels::NORMAL;
master->SetLogFilters(levels);
std::cout << "Master logging set to: " << levels.get_value() << std::endl;
break;
}
default:
std::cout << "Unknown action: " << cmd << std::endl;
break;
}
}

return 0;
}
3 changes: 3 additions & 0 deletions cpp/examples/outstation-udp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
add_executable(outstation-udp-demo ./main.cpp)
target_link_libraries (outstation-udp-demo PRIVATE opendnp3)
set_target_properties(outstation-udp-demo PROPERTIES FOLDER cpp/examples)
166 changes: 166 additions & 0 deletions cpp/examples/outstation-udp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2013-2019 Automatak, LLC
*
* Licensed to Green Energy Corp (www.greenenergycorp.com) and Automatak
* LLC (www.automatak.com) under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership. Green Energy Corp and Automatak LLC license
* this file to you under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may obtain
* a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <opendnp3/ConsoleLogger.h>
#include <opendnp3/DNP3Manager.h>
#include <opendnp3/LogLevels.h>
#include <opendnp3/channel/PrintingChannelListener.h>
#include <opendnp3/outstation/IUpdateHandler.h>
#include <opendnp3/outstation/SimpleCommandHandler.h>
#include <opendnp3/outstation/UpdateBuilder.h>

#include <iostream>
#include <string>
#include <thread>

using namespace std;
using namespace opendnp3;

DatabaseConfig ConfigureDatabase()
{
DatabaseConfig config(10); // 10 of each type

config.analog_input[0].clazz = PointClass::Class2;
config.analog_input[0].svariation = StaticAnalogVariation::Group30Var5;
config.analog_input[0].evariation = EventAnalogVariation::Group32Var7;

return config;
}

struct State
{
uint32_t count = 0;
double value = 0;
bool binary = false;
DoubleBit dbit = DoubleBit::DETERMINED_OFF;
uint8_t octetStringValue = 1;
};

void AddUpdates(UpdateBuilder& builder, State& state, const std::string& arguments);

int main(int argc, char* argv[])
{

// Specify what log levels to use. NORMAL is warning and above
// You can add all the comms logging by uncommenting below.
const auto logLevels = levels::ALL;

// This is the main point of interaction with the stack
// Allocate a single thread to the pool since this is a single outstation
// Log messages to the console
DNP3Manager manager(1, ConsoleLogger::Create());

// Create a UDP socket
auto channel = manager.AddUDPChannel("server", logLevels, ChannelRetry::Default(), IPEndpoint("192.168.0.106", 19999),
IPEndpoint("192.168.0.193", 20000), PrintingChannelListener::Create());

// The main object for a outstation. The defaults are useable,
// but understanding the options are important.
OutstationStackConfig config(ConfigureDatabase());

// Specify the maximum size of the event buffers
config.outstation.eventBufferConfig = EventBufferConfig::AllTypes(10);

// you can override an default outstation parameters here
// in this example, we've enabled the oustation to use unsolicted reporting
// if the master enables it
config.outstation.params.allowUnsolicited = true;

// You can override the default link layer settings here
// in this example we've changed the default link layer addressing
config.link.LocalAddr = 10;
config.link.RemoteAddr = 1;
config.link.KeepAliveTimeout = TimeDuration::Max();

// Create a new outstation with a log level, command handler, and
// config info this returns a thread-safe interface used for
// updating the outstation's database.
auto outstation = channel->AddOutstation("outstation", SuccessCommandHandler::Create(),
DefaultOutstationApplication::Create(), config);

// Enable the outstation and start communications
outstation->Enable();

// variables used in example loop
string input;
State state;

while (true)
{
std::cout << "Enter one or more measurement changes then press <enter>" << std::endl;
std::cout << "c = counter, b = binary, d = doublebit, a = analog, o = octet string, 'quit' = exit" << std::endl;
std::cin >> input;

if (input == "quit")
return 0; // DNP3Manager destructor cleanups up everything automatically
else
{
// update measurement values based on input string
UpdateBuilder builder;
AddUpdates(builder, state, input);
outstation->Apply(builder.Build());
}
}

return 0;
}

void AddUpdates(UpdateBuilder& builder, State& state, const std::string& arguments)
{
for (const char& c : arguments)
{
switch (c)
{
case ('c'):
{
builder.Update(Counter(state.count), 0);
++state.count;
break;
}
case ('a'):
{
builder.Update(Analog(state.value), 0);
state.value += 1;
break;
}
case ('b'):
{
builder.Update(Binary(state.binary), 0);
state.binary = !state.binary;
break;
}
case ('d'):
{
builder.Update(DoubleBitBinary(state.dbit), 0);
state.dbit
= (state.dbit == DoubleBit::DETERMINED_OFF) ? DoubleBit::DETERMINED_ON : DoubleBit::DETERMINED_OFF;
break;
}
case ('o'):
{
OctetString value(Buffer(&state.octetStringValue, 1));
builder.Update(value, 0);
state.octetStringValue += 1;
break;
}
default:
break;
}
}
}
2 changes: 1 addition & 1 deletion cpp/examples/outstation/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int main(int argc, char* argv[])
DNP3Manager manager(1, ConsoleLogger::Create());

// Create a TCP server (listener)
auto channel = manager.AddTCPServer("server", logLevels, ServerAcceptMode::CloseExisting, "0.0.0.0", 20000,
auto channel = manager.AddTCPServer("server", logLevels, ServerAcceptMode::CloseExisting, IPEndpoint("0.0.0.0", 20000),
PrintingChannelListener::Create());

// The main object for a outstation. The defaults are useable,
Expand Down
2 changes: 1 addition & 1 deletion cpp/examples/tls/outstation/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ int main(int argc, char* argv[])
std::error_code ec;

// Create a TCP server (listener)
auto channel = manager.AddTLSServer("server", logLevels, ServerAcceptMode::CloseExisting, "0.0.0.0", 20001,
auto channel = manager.AddTLSServer("server", logLevels, ServerAcceptMode::CloseExisting, IPEndpoint("0.0.0.0", 20001),
TLSConfig(caCertificate, certificateChain, privateKey, 2),
PrintingChannelListener::Create(), ec);

Expand Down
10 changes: 8 additions & 2 deletions cpp/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,15 @@ set(opendnp3_private_headers
./src/channel/LoggingConnectionCondition.h
./src/channel/SerialChannel.h
./src/channel/SerialIOHandler.h
./src/channel/SocketChannel.h
./src/channel/SocketHelpers.h
./src/channel/TCPClient.h
./src/channel/TCPClientIOHandler.h
./src/channel/TCPServer.h
./src/channel/TCPServerIOHandler.h
./src/channel/TCPSocketChannel.h
./src/channel/UDPClient.h
./src/channel/UDPClientIOHandler.h
./src/channel/UDPSocketChannel.h

./src/decoder/DecoderImpl.h
./src/decoder/Indent.h
Expand Down Expand Up @@ -468,11 +471,14 @@ set(opendnp3_src
./src/channel/IPEndpointsList.cpp
./src/channel/SerialChannel.cpp
./src/channel/SerialIOHandler.cpp
./src/channel/SocketChannel.cpp
./src/channel/TCPClient.cpp
./src/channel/TCPClientIOHandler.cpp
./src/channel/TCPServer.cpp
./src/channel/TCPServerIOHandler.cpp
./src/channel/TCPSocketChannel.cpp
./src/channel/UDPClient.cpp
./src/channel/UDPClientIOHandler.cpp
./src/channel/UDPSocketChannel.cpp

./src/decoder/Decoder.cpp
./src/decoder/DecoderImpl.cpp
Expand Down