Orocos Real-Time Toolkit  2.8.3
IRQActivity.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:08 CEST 2009 IRQActivity.cpp
3 
4  IRQActivity.cpp - description
5  -------------------
6  begin : Thu October 22 2009
7  copyright : (C) 2009 Peter Soetens
8  email : peter@thesourcworks.com
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #include "IRQActivity.hpp"
40 #include "../ExecutionEngine.hpp"
41 #include "../base/TaskCore.hpp"
42 #include "../Logger.hpp"
43 
44 #include <iostream>
45 
46 using namespace RTT;
47 using namespace extras;
48 using namespace base;
49 
50 IRQActivity::IRQActivity(int priority, RunnableInterface* _r, const std::string& name )
51  : Activity(priority, 0.0, _r, name)
52  , m_irq(-1)
53  , m_runner(_r) {}
54 
55 IRQActivity::IRQActivity(int scheduler, int priority, RunnableInterface* _r, const std::string& name )
56  : Activity(scheduler, priority, 0.0, _r, name)
57  , m_irq(-1)
58  , m_runner(_r) {}
59 
61 {
62  stop();
63 }
64 
65 int IRQActivity::getIRQ() const { return m_irq; }
66 void IRQActivity::setIRQ(int irq) { m_irq = irq; }
67 
68 #ifndef OROPKG_OS_XENOMAI
70  Logger::log() << Logger::Error << "IRQActivity is only usable on Xenomai" << Logger::endl;
71  return false;
72 }
73 #else
74 
75 #include <sys/select.h>
76 #include <unistd.h>
77 
78 bool IRQActivity::start()
79 {
80  if (m_irq == -1)
81  { // no FD explicitely set. Try to find one.
82  ExecutionEngine* engine = dynamic_cast<ExecutionEngine*>(m_runner);
83  if (engine)
84  {
85  Provider* provider = dynamic_cast<Provider*>(engine->getParent());
86  if (provider)
87  m_irq = provider->getIRQ();
88  }
89  }
90 
91  if (m_irq == -1)
92  {
93  Logger::log() << Logger::Error << "no IRQ set for IRQActivity" << Logger::endl;
94  return false;
95  }
96 
97  char name[20];
98  if (snprintf(name, 20, "IRQActivity%d", m_irq) >= 20)
99  {
100  Logger::log() << Logger::Error << "something is wrong with m_irq. Are you trying to do a buffer overflow ?" << strerror(errno) << Logger::endl;
101  return false;
102  }
103 
104  int ret = rt_intr_create(&m_handle, name, m_irq, 0);
105  if (ret != 0)
106  {
107  Logger::log() << Logger::Error << "cannot create interrupt object for IRQ " << m_irq << ": " << strerror(-ret) << Logger::endl;
108  return false;
109  }
110 
111  if (! Activity::start())
112  {
113  rt_intr_delete(&m_handle);
114  return false;
115  }
116 
117  return true;
118 }
119 
120 void IRQActivity::loop()
121 {
122  if (m_irq == -1)
123  return;
124 
125  while(true)
126  {
127  if (rt_intr_wait(&m_handle, TM_INFINITE) > 0)
128  step();
129  else
130  break;
131  // {
132  // sleep(1);
133  // if (errno == -EIDRM) // interrupt handler has been deleted
134  // break;
135  // else
136  // {
137  // std::cout << "ERROR " << errno << std::endl;
138  // break;
139  // }
140  // }
141  // step();
142  }
143 }
144 
146 {
147  rt_intr_delete(&m_handle);
148  return true;
149 }
150 
151 void IRQActivity::step()
152 {
153  if (m_runner != 0)
154  m_runner->step();
155 }
156 
157 #endif // OS is xenomai
158 
virtual void loop()
Definition: Activity.cpp:105
virtual bool stop()
Stop the activity This will stop the activity by removing it from the &#39;run-queue&#39; of a thread or call...
Definition: Activity.cpp:128
If this interface is derived from one of the TaskContext objects which are executed by this activity...
Definition: IRQActivity.hpp:86
A class for running a certain piece of code in a thread.
IRQActivity(int priority, base::RunnableInterface *_r=0, const std::string &name="IRQActivity")
Create a IRQActivity with a given priority and base::RunnableInterface instance.
Definition: IRQActivity.cpp:50
virtual void step()=0
The method that will be periodically executed when this class is run in a periodic thread...
An execution engine serialises (executes one after the other) the execution of all commands...
virtual bool start()
Start the activity.
Definition: IRQActivity.cpp:69
virtual bool breakLoop()
Definition: Activity.cpp:112
virtual int getIRQ() const =0
virtual void step()
Definition: Activity.cpp:100
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
base::TaskCore * getParent()
The base::TaskCore which created this ExecutionEngine.
An Activity is an object that represents a thread.
Definition: Activity.hpp:63
static Logger & log()
As Instance(), but more userfriendly.
Definition: Logger.cpp:117
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
virtual bool start()
Start the activity.
Definition: Activity.cpp:124