ProviewR Programmer's Reference Manual  V7.0.0
rt_appl.cpp
Go to the documentation of this file.
1 /*
2  * ProviewR Open Source Process Control.
3  * Copyright (C) 2005-2026 SSAB EMEA AB.
4  *
5  * This file is part of ProviewR.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation, either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with ProviewR. If not, see <http://www.gnu.org/licenses/>
19  *
20  * Linking ProviewR statically or dynamically with other modules is
21  * making a combined work based on ProviewR. Thus, the terms and
22  * conditions of the GNU General Public License cover the whole
23  * combination.
24  *
25  * In addition, as a special exception, the copyright holders of
26  * ProviewR give you permission to, from the build function in the
27  * ProviewR Configurator, combine ProviewR with modules generated by the
28  * ProviewR PLC Editor to a PLC program, regardless of the license
29  * terms of these modules. You may copy and distribute the resulting
30  * combined work under the terms of your choice, provided that every
31  * copy of the combined work is accompanied by a complete copy of
32  * the source code of ProviewR (the version used to produce the
33  * combined work), being distributed under the terms of the GNU
34  * General Public License plus this exception.
35  */
36 
37 //
38 // Example of a proview application program
39 //
40 
45 #include "rt_appl.h"
46 #include "rt_gdh.h"
47 #include "rt_aproc.h"
48 #include "rt_pwr_msg.h"
49 #include "rt_qcom_msg.h"
50 #include "rt_ini_event.h"
51 #include "co_error.h"
52 #include "pwr_baseclasses.h"
53 
55  const char* name, errh_eAnix anix, double scantime, qcom_sQid qid)
56  : m_anix(anix), m_scantime(scantime), m_maxdelay(5), m_qid(qid)
57 {
58  strcpy(m_name, name);
59 }
60 
61 void rt_appl::init()
62 {
63  qcom_sQid qini;
64  qcom_sQattr qAttr;
65  pwr_tStatus sts;
66 
67  // Init error and status logger with a unique application index per node.
68  errh_Init(m_name, m_anix);
69  errh_SetStatus(PWR__APPLSTARTUP);
70 
71  // Init database
72  sts = gdh_Init("rs_appl");
73  if (EVEN(sts)) {
74  errh_Fatal("gdh_Init, %m", sts);
75  errh_SetStatus(PWR__APPLTERM);
76  exit(sts);
77  }
78 
79  // Create a queue to receive stop and restart events
80  if (!qcom_Init(&sts, 0, "rs_appl")) {
81  errh_Fatal("qcom_Init, %m", sts);
82  errh_SetStatus(PWR__APPLTERM);
83  exit(sts);
84  }
85 
86  qAttr.type = qcom_eQtype_private;
87  qAttr.quota = 100;
88  if (!qcom_CreateQ(&sts, &m_qid, &qAttr, "events")) {
89  errh_Fatal("qcom_CreateQ, %m", sts);
90  errh_SetStatus(PWR__APPLTERM);
91  exit(sts);
92  }
93 
94  qini = qcom_cQini;
95  if (!qcom_Bind(&sts, &m_qid, &qini)) {
96  errh_Fatal("qcom_Bind(Qini), %m", sts);
97  errh_SetStatus(PWR__APPLTERM);
98  exit(-1);
99  }
100 }
101 
102 void rt_appl::register_appl(const char* name)
103 {
104  pwr_tStatus sts;
105  pwr_sClass_Application* op;
106 
107  // Get configuration object
108  sts = gdh_NameToObjid(name, &m_apploid);
109  if (EVEN(sts))
110  throw co_error(sts);
111 
112  aproc_RegisterObject(m_apploid);
113 
114  sts = gdh_ObjidToPointer(m_apploid, (void**)&op);
115  if (ODD(sts))
116  op->Anix = m_anix;
117 }
118 
119 void rt_appl::mainloop()
120 {
121  pwr_tStatus sts;
122  int tmo = 0;
123  char mp[2000];
124  qcom_sGet get;
125  int swap = 0;
126  bool first_scan = true;
127 
128  try {
129  open();
130  } catch (co_error& e) {
131  errh_Error((char*)e.what().c_str());
132  errh_Fatal("rs_appl aborting");
133  errh_SetStatus(PWR__APPLTERM);
134  exit(0);
135  }
136 
137  aproc_TimeStamp(m_scantime, m_maxdelay);
138  errh_SetStatus(PWR__ARUN);
139 
140  first_scan = true;
141  for (;;) {
142  if (first_scan) {
143  tmo = (int)(m_scantime * 1000 - 1);
144  }
145 
146  get.maxSize = sizeof(mp);
147  get.data = mp;
148  qcom_Get(&sts, &m_qid, &get, tmo);
149  if (sts == QCOM__TMO || sts == QCOM__QEMPTY) {
150  if (!swap) {
151  aproc_TimeStamp(m_scantime, m_maxdelay);
152  scan();
153  }
154  } else {
155  ini_mEvent new_event;
156  qcom_sEvent* ep = (qcom_sEvent*)get.data;
157 
158  new_event.m = ep->mask;
159  if (new_event.b.oldPlcStop && !swap) {
160  errh_SetStatus(PWR__APPLRESTART);
161  swap = 1;
162  close();
163  } else if (new_event.b.swapDone && swap) {
164  swap = 0;
165  open();
166  errh_SetStatus(PWR__ARUN);
167  } else if (new_event.b.terminate) {
168  exit(0);
169  }
170  }
171  first_scan = false;
172  }
173 }
174 
175 double rt_appl::scantime()
176 {
177  return m_scantime;
178 }
179 
180 void rt_appl::set_scantime(double time)
181 {
182  m_scantime = time;
183 }
184 
185 pwr_tOid& rt_appl::apploid()
186 {
187  return m_apploid;
188 }
189 
191 {
192 }
193 
195 {
196 }
197 
199 {
200 }
201 
202 rt_appl::~rt_appl()
203 {
204 }
virtual void close()
Closes the application.
Definition: rt_appl.cpp:194
virtual void open()
Initialize the application.
Definition: rt_appl.cpp:190
rt_appl(const char *name, errh_eAnix anix, double scantime=1.0, qcom_sQid qid=qcom_cNQid)
Constructor.
Definition: rt_appl.cpp:54
virtual void scan()
Cyclic function.
Definition: rt_appl.cpp:198
pwr_tStatus aproc_TimeStamp(float scantime, float maxdelay)
Set application timestamp. The timestamp is supervised by the event monitor and if the timestamp is d...
Definition: rt_aproc.c:90
pwr_tStatus aproc_RegisterObject(pwr_tOid)
Register Application object. The Application object is stored in the $Node object and be opened from ...
Definition: rt_aproc.c:57
errh_eAnix
Definition: rt_errh.h:99
void errh_Error(const char *msg,...)
Log an error message. The function has a variable argument list similar to sprintf.
Definition: rt_errh.c:336
pwr_tStatus errh_Init(const char *programName, errh_eAnix anix)
Initialize errh. Set up a queue to write to, set application name.
Definition: rt_errh.c:171
void errh_SetStatus(pwr_tStatus sts)
Set application status. The application status is showed in the Node graph. The application can set t...
Definition: rt_errh.c:195
void errh_Fatal(const char *msg,...)
Log a fatal message. The function has a variable argument list similar to sprintf.
Definition: rt_errh.c:357
pwr_tStatus gdh_ObjidToPointer(pwr_tObjid object, void **objectData)
Gets the address of the data of an object, given its object identity.
Definition: rt_gdh.c:2431
pwr_tStatus gdh_Init(const char *name)
Initialize the process to the Proview runtime environment.
Definition: rt_gdh.c:2065
pwr_tStatus gdh_NameToObjid(const char *objectName, pwr_tObjid *objid)
Get the object identity of an object with name 'name'.
Definition: rt_gdh.c:2202
pwr_tBoolean qcom_Bind(pwr_tStatus *sts, const qcom_sQid *myQ, const qcom_sQid *toQ)
Bind one queue to another.
Definition: rt_qcom.c:171
pwr_tBoolean qcom_CreateQ(pwr_tStatus *sts, qcom_sQid *myQ, qcom_sQattr *attr, const char *qname)
Create a queue and make an implicit connect to it.
Definition: rt_qcom.c:213
void * qcom_Get(pwr_tStatus *sts, const qcom_sQid *myQ, qcom_sGet *get, int tmo_ms)
Get a new message.
Definition: rt_qcom.c:398
pwr_tBoolean qcom_Init(pwr_tStatus *sts, qcom_sAid *aid, const char *aname)
Connect to QCom.
Definition: rt_qcom.c:643
#define EVEN(a)
Check if value is even.
Definition: pwr.h:627
int pwr_tStatus
Status type.
Definition: pwr.h:284
#define ODD(a)
Check if value is odd.
Definition: pwr.h:622
Include file for the rt_appl class.
Include file for AProc Application process interface.
Include file for Gdh Global Data Handler.
Object identity type.
Definition: pwr.h:263
Qcom event.
Definition: rt_qcom.h:155
Get data structure.
Definition: rt_qcom.h:185
Queue attributes.
Definition: rt_qcom.h:162
Queue identity.
Definition: rt_qcom.h:125