iCub-main
Loading...
Searching...
No Matches
SharedCanBus.h
Go to the documentation of this file.
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
15#ifndef __SHARED_CAN_BUS_H__
16#define __SHARED_CAN_BUS_H__
17
18#include <mutex>
19#include <condition_variable>
20
21#include <yarp/os/Time.h>
22#include <yarp/os/Log.h>
23#include <yarp/dev/DeviceDriver.h>
24#include <yarp/dev/CanBusInterface.h>
25
26#include "canControlConstants.h"
27
28#define UNREQ 0
29#define REQST 1
30
31namespace yarp{
32 namespace dev{
33 class CanBusAccessPoint;
34 }
35}
36
37class SharedCanBus;
38
51 public ICanBus,
52 public ICanBufferFactory,
53 public DeviceDriver
54{
55public:
57 {
59
60 reqIds=new char[0x800];
61
62 for (int i=0; i<0x800; ++i) reqIds[i]=UNREQ;
63
64 waitingOnRead=false;
65
67
68 nRecv=0;
69 }
70
72 {
74
75 delete [] reqIds;
76 }
77
78 bool hasId(unsigned int id)
79 {
80 return reqIds[id]==REQST;
81 }
82
83 bool pushReadMsg(CanMessage& msg)
84 {
85 std::lock_guard<std::mutex> lck(synchroMutex);
86
87 if (nRecv>=mBufferSize)
88 {
89 yError("recv buffer overrun (%4d >= %4d)", nRecv, mBufferSize);
90 return false;
91 }
92
93 readBuffer[nRecv++]=msg;
94
95 if (waitingOnRead)
96 {
97 waitingOnRead=false;
98 cv_waitRead.notify_one();
99 }
100
101 return true;
102 }
103
105 // ICanBus
106 virtual bool canGetBaudRate(unsigned int *rate);
107 virtual bool canSetBaudRate(unsigned int rate)
108 {
109 yWarning("Set baud rate not allowed from CanBusAccessPoint implementation\n");
110 return false;
111 }
112
113 virtual bool canIdAdd(unsigned int id);
114 virtual bool canIdDelete(unsigned int id);
115
116 virtual bool canRead(CanBuffer &msgs, unsigned int size, unsigned int *nmsg, bool wait=false)
117 {
118 synchroMutex.lock();
119
120 if (wait && !nRecv)
121 {
122 waitingOnRead=true;
123 synchroMutex.unlock();
124 std::unique_lock<std::mutex> lck(mtx_waitRead);
125 cv_waitRead.wait(lck);
126 synchroMutex.lock();
127 }
128
129 if (nRecv)
130 {
131 for (unsigned int i=0; i<nRecv && i<size; ++i)
132 {
133 msgs[i]=readBuffer[i];
134 }
135
136 *nmsg=nRecv;
137 nRecv=0;
138 synchroMutex.unlock();
139 return true;
140 }
141 else
142 {
143 *nmsg=0;
144 synchroMutex.unlock();
145 return true;
146 }
147 }
148
149 virtual bool canWrite(const CanBuffer &msgs, unsigned int size, unsigned int *sent, bool wait=false);
150 // ICanBus
152
154 // ICanBufferFactory
155 virtual CanBuffer createBuffer(int nmessage);
156 virtual void destroyBuffer(CanBuffer &msgs);
157 // ICanBufferFactory
159
161 // DeviceDriver
162 virtual bool open(yarp::os::Searchable& config);
163 virtual bool close();
164 // DeviceDriver
166
167protected:
168 std::mutex mtx_waitRead;
169 std::condition_variable cv_waitRead;
170 std::mutex synchroMutex;
171
173
174 unsigned int nRecv;
175 CanBuffer readBuffer;
176
177 char *reqIds; //[0x800];
178
179 unsigned int mBufferSize;
180
182};
183
184#endif
185
#define REQST
#define UNREQ
Copyright (C) 2012 RobotCub Consortium.
sharedcan : implements ICanBus interface for multiple access from a single access can driver (for exa...
virtual bool canSetBaudRate(unsigned int rate)
virtual void destroyBuffer(CanBuffer &msgs)
virtual bool open(yarp::os::Searchable &config)
virtual bool canGetBaudRate(unsigned int *rate)
virtual bool canRead(CanBuffer &msgs, unsigned int size, unsigned int *nmsg, bool wait=false)
bool hasId(unsigned int id)
virtual bool canIdAdd(unsigned int id)
bool pushReadMsg(CanMessage &msg)
std::condition_variable cv_waitRead
virtual bool canWrite(const CanBuffer &msgs, unsigned int size, unsigned int *sent, bool wait=false)
virtual bool canIdDelete(unsigned int id)
virtual CanBuffer createBuffer(int nmessage)
Copyright (C) 2008 RobotCub Consortium.