This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
main.cpp
169 lines (145 loc) · 5.71 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright 2013-2020 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/channel/PrintingChannelListener.h>
#include <opendnp3/logging/LogLevels.h>
#include <opendnp3/outstation/DefaultOutstationApplication.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;
};
void AddUpdates(UpdateBuilder& builder, State& state, const std::string& arguments);
int main(int argc, char* argv[])
{
if (argc != 4)
{
std::cout << "usage: master-gprs-tls-demo <peer certificate> <local certificate chain> <private key>" << std::endl;
return -1;
}
std::string peerCertificate(argv[1]);
std::string localCertificate(argv[2]);
std::string privateKey(argv[3]);
std::cout << "Using peer certificate: " << peerCertificate << std::endl;
std::cout << "Using local certificate: " << localCertificate << std::endl;
std::cout << "Using private key file: " << privateKey << std::endl;
// 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::NORMAL | levels::ALL_COMMS;
// This is the main point of interaction with the stack
// Allocate a single thread to the pool since this is a single outstation
DNP3Manager manager(1, ConsoleLogger::Create());
// Create a TCP server (listener)
auto channel = manager.AddTLSServer("server", logLevels, ServerAcceptMode::CloseExisting, IPEndpoint("0.0.0.0", 20001),
TLSConfig(peerCertificate, localCertificate, privateKey),
PrintingChannelListener::Create());
// The main object for a outstation. The defaults are useable,
// but understanding the options are important.
OutstationStackConfig stackConfig(ConfigureDatabase());
// specify the maximum size of the event buffers
stackConfig.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
stackConfig.outstation.params.allowUnsolicited = true;
// You can override the default link layer settings here
// in this example we've changed the default link layer addressing
stackConfig.link.LocalAddr = 10;
stackConfig.link.RemoteAddr = 1;
// 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(), stackConfig);
// 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, 'quit' = exit" << std::endl;
std::cin >> input;
if (input == "quit")
return 0;
else
{
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;
}
default:
break;
}
}
}