OpcUaCanOpen
CANopen OPC-UA server
OpcServer.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** opcserver.cpp
3 **
4 ** Copyright (c) 2006-2016 Unified Automation GmbH. All rights reserved.
5 **
6 ** Software License Agreement ("SLA") Version 2.5
7 **
8 ** Unless explicitly acquired and licensed from Licensor under another
9 ** license, the contents of this file are subject to the Software License
10 ** Agreement ("SLA") Version 2.5, or subsequent versions
11 ** as allowed by the SLA, and You may not copy or use this file in either
12 ** source code or executable form, except in compliance with the terms and
13 ** conditions of the SLA.
14 **
15 ** All software distributed under the SLA is provided strictly on an
16 ** "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
17 ** AND LICENSOR HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
18 ** LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 ** PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the SLA for specific
20 ** language governing rights and limitations under the SLA.
21 **
22 ** The complete license agreement can be found here:
23 ** http://unifiedautomation.com/License/SLA/2.5/
24 **
25 ** Project: C++ OPC Server SDK sample code
26 **
27 ** Description: Main OPC Server object class.
28 **
29 ******************************************************************************/
30 #include "OpcServer.h"
31 #include "uamodule.h"
32 #include "uasession.h"
33 
34 #include "ControlInterface.h"
35 #include "UaCanTrace.h"
36 
37 using namespace AddressSpace;
38 extern bool createCertificateOnly;
39 
40 
41 #ifdef WIN32
42 extern "C" __declspec(dllexport) void UaTrace_tError(const char * fmt, const char * text)
43 {
44  UaTrace::tError(fmt, text);
45 }
46 #endif
47 
48 #ifndef UA_BUILD_DATE_ZONE
49 #define UA_BUILD_DATE_ZONE 1 // Must match UTC offset and daylight saving time at build date
50 #endif /* UA_BUILD_DATE_ZONE */
51 
54 {
55 public:
58 };
59 
62 {
63  d = new OpcServerPrivate;
64 }
65 
67 OpcServer::OpcServer(int argc, char* argv[], bool bRunAsService, const UaString &sApplicationName)
68 : UaServerApplication(argc, argv, bRunAsService, sApplicationName)
69 {
70  d = new OpcServerPrivate;
71 }
72 
75 {
76 // disconnectInterface();
77 
78  if ( isStarted() != OpcUa_False )
79  {
80  UaLocalizedText reason("en","Application shut down");
81 // stop(0, reason);
82  }
83  delete d;
84  d = NULL;
85 }
86 
88 {
89  UaStatus ret;
90 
91  ret = UaServerApplication::afterStartUp();
92 
94  cout << "Create certificate only!" << endl;
95  if (ret.isNotGood())
96  cout << "Create certificate error " << ret.toString(ret.statusCode()).toUtf8() << endl;
97  exit(0); // out if needed only to create certificate
98  }
99 
100  if ( ret.isGood() )
101  {
102  ret = m_pControlInterface->CreateAddressSpace(m_pMyNodeManager ,m_pTypeCreator->gwtNodeXmlManager(),getDefaultNodeManager());
103  UA_ASSERT(ret.isGood());
104  if (ret.isGood()) {
105  cout << "Initialize Interfaces" << endl;
106 
107  ret = m_pControlInterface->initialize();
108  }
109  else
110  {
111  cout << "Could not create Address Space" << endl;
112  }
113 
114  UaString sRejectedCertificateDirectory;
115  OpcUa_UInt32 nRejectedCertificatesCount;
116  UaEndpointArray uaEndpointArray;
117  getServerConfig()->getEndpointConfiguration(
118  sRejectedCertificateDirectory,
119  nRejectedCertificatesCount,
120  uaEndpointArray);
121  if ( uaEndpointArray.length() > 0 )
122  {
123  printf("***************************************************\n");
124  printf(" Server opened endpoints for following URLs:\n");
125  OpcUa_UInt32 idx;
126  bool bError = false;
127  for ( idx=0; idx<uaEndpointArray.length(); idx++ )
128  {
129  if ( uaEndpointArray[idx]->isOpened() )
130  {
131  printf(" %s\n", uaEndpointArray[idx]->sEndpointUrl().toUtf8());
132  }
133  else
134  {
135  bError = true;
136  }
137  }
138  if ( bError )
139  {
140  printf("\n");
141  printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
142  printf("!!!! The following endpoints URLs failed:\n");
143  for ( idx=0; idx<uaEndpointArray.length(); idx++ )
144  {
145  if ( uaEndpointArray[idx]->isOpened() == false )
146  {
147  printf("!!!! %s\n", uaEndpointArray[idx]->sEndpointUrl().toUtf8());
148  }
149  }
150  printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
151  printf("\n");
152  }
153  printf("***************************************************\n");
154  }
155  }
156  else
157  {
158  cout << "Could not StartUp Server " << std::hex << ret.code() << " "<< ret.toString().toUtf8() << endl;
159 
160  }
161 
162  return ret;
163 }
164 
166 {
167  m_pControlInterface->closeInterface();
168 }
169 
170 //void OpcServer::disconnectInterface()
171 //{
173 //}
174 
175 
179 OpcUa_DateTime OpcServer::getBuildDate() const
180 {
181  static OpcUa_DateTime date;
182  static const char szDate[] = __DATE__; /* "Mon DD YYYY" */
183  static char szISO[] = "YYYY-MM-DDT" __TIME__ "Z";
184  static const char* Months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
185  char mon = 0;
186 
187  /* set year */
188  szISO[0] = szDate[7];
189  szISO[1] = szDate[8];
190  szISO[2] = szDate[9];
191  szISO[3] = szDate[10];
192 
193  /* set month */
194  while ( (strncmp(Months[(int)mon], szDate, 3) != 0) && (mon < 11) )
195  {
196  mon++;
197  }
198  mon++;
199  szISO[5] = '0' + mon / 10;
200  szISO[6] = '0' + mon % 10;
201 
202  /* set day */
203  szISO[8] = szDate[4];
204  szISO[9] = szDate[5];
205 
206  /* convert to UA time */
207  OpcUa_DateTime_GetDateTimeFromString(szISO, &date);
208 
209  /* correct time */
210  UaDateTime buildDate(date);
211  buildDate.addSecs(UA_BUILD_DATE_ZONE * 3600 * -1);
212 
213  return buildDate;
214 }