rllib  1
Functions
rlcanopendaemon.cpp File Reference
#include "rlcanopendaemon.h"
Include dependency graph for rlcanopendaemon.cpp:

Go to the source code of this file.

Functions

void sdo_read (rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
 
void sdo_write (rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
 
void pdo_receive (rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
 
void pdo_transmit (rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
 
void nmt_transmit (rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
 
void restart_board (rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
 
void getNodeState (rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
 
static void * clientconnection (void *arg)
 
static void * listenerthread (void *arg)
 

Function Documentation

◆ clientconnection()

static void* clientconnection ( void *  arg)
static

handels a new client connection established by listener thread. Saves socket descriptor in new socket object. Runs a loop which reads IPCMSGSTRUCTs containing client requests and calls appropiate handler functions which communicate with the canopen device and send answers back to the client. clientconnection Thread is terminated when client disconnects.

Definition at line 172 of file rlcanopendaemon.cpp.

173 {
174  //cout<<"\n client connection established...\n";
175 
176  rlCanOpenTypes cifmsg;
177  THREAD_PARAM *p = (THREAD_PARAM *) arg;
179  IPCMSGSTRUCT message;
180  int socketdescr = tr->socketdescr;
181  rlCanOpenDaemon *daemonptr = tr->daemonptr;
182  int ret;
183  rlSocket socket(socketdescr);
184 
185  rlDebugPrintf("client connection established...\n");
186  while(socket.isConnected())
187  {
188  ret = socket.read((void*) &message, sizeof(message));
189  if(ret <= 0) break;
190  if ((message.msgtype==MSG_SDO_READ)&&(message.transfertype==MSG_SEND)){
191  sdo_read(daemonptr, &socket, &message);
192  }
193  if ((message.msgtype==MSG_SDO_WRITE)&&(message.transfertype==MSG_SEND)){
194  sdo_write(daemonptr, &socket, &message);
195  }
196  if ((message.msgtype==MSG_PDO_RECEIVE)&&(message.transfertype==MSG_SEND)){
197  pdo_receive(daemonptr, &socket, &message);
198  }
199  if ((message.msgtype==MSG_PDO_TRANSMIT)&&(message.transfertype==MSG_SEND)){
200  pdo_transmit(daemonptr, &socket, &message);
201  }
202  if ((message.msgtype==MSG_NMT_TRANSMIT)&&(message.transfertype==MSG_SEND)){
203  nmt_transmit(daemonptr, &socket, &message);
204  }
205  if ((message.msgtype==MSG_RESTART_BOARD)&&(message.transfertype==MSG_SEND)){
206  restart_board(daemonptr, &socket, &message);
207  }
208  if ((message.msgtype==MSG_GET_NODE_STATE)&&(message.transfertype==MSG_SEND)){
209  getNodeState(daemonptr, &socket, &message);
210  }
211 
212  }
213  rlDebugPrintf("client disconnect...\n");
214  //cout<<"\n client disconnect...\n";
215  return NULL;
216 }
int rlDebugPrintf(const char *format,...)
Definition: rlcutil.cpp:61
the IPCMSGSTRUCT is the transfer buffer which is send trough TCP sockets
canopen tcp/ip interface for concurrent device-access of multiple clientprocesses ...
void * user
Definition: rlthread.h:30
void pdo_receive(rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
void restart_board(rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
class to handle CANopen types
void getNodeState(rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
void nmt_transmit(rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
void sdo_read(rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
void sdo_write(rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)
rlCanOpenDaemon * daemonptr
void pdo_transmit(rlCanOpenDaemon *_daemonptr, rlSocket *_socket, IPCMSGSTRUCT *_message)

◆ getNodeState()

void getNodeState ( rlCanOpenDaemon _daemonptr,
rlSocket _socket,
IPCMSGSTRUCT _message 
)

gets called from clientconnection thread. performs a getNodeState command on the canopen interface. received data is stored in an IPCMSGSTRUCT and sent back to the clientprocess

Definition at line 149 of file rlcanopendaemon.cpp.

151  {
152  int returnvalue;
153  rlCanOpenTypes cifmsg;
154  cifmsg.getIpcMsg(*_message);
155  _daemonptr->daemon_thread.lock();
156  returnvalue=_daemonptr->nodes->getNodeState(_message->boardid,
157  _message->nodeid,
158  cifmsg);
159  _daemonptr->daemon_thread.unlock();
160  *_message = cifmsg.createIpcMsg();
161  _message->msgtype=MSG_GET_NODE_STATE; _message->transfertype=returnvalue;
162  _socket->write((void*) _message, sizeof(*_message));
163 }
rlThread daemon_thread
int write(const void *buf, int len)
Definition: rlsocket.cpp:292
void getIpcMsg(IPCMSGSTRUCT _myIpcMsg)
overwrites all data with IPCMSGSTRUCT data
int unlock()
Definition: rlthread.cpp:52
class to handle CANopen types
rlCanOpen * nodes
IPCMSGSTRUCT createIpcMsg()
returns a IPCMSGSTRUCT filled with current object data
int getNodeState(int _boardnr, int _nodeid, rlCanOpenTypes &_data)
use this function to get information about a specific node
Definition: rlcanopen.cpp:1045
int lock()
Definition: rlthread.cpp:47

◆ listenerthread()

static void* listenerthread ( void *  arg)
static

listenerthread waits for incoming client connections on the confugured port and spawns a new clientthread for each of them.

Definition at line 221 of file rlcanopendaemon.cpp.

222 {
223 
224  // socketdescriptor;
225  int socketdescr;
226  // parameter struct for accessing function parameters
227  THREAD_PARAM *p = (THREAD_PARAM *) arg;
228  // new parameter struct for passing parameters to handler thread
229  THREADTRANSFER tr;
230  rlCanOpenDaemon *daemonptr = (rlCanOpenDaemon*) p->user;
231  tr.daemonptr = daemonptr;
232  int port = daemonptr->getPort();
233  // wait for connections with accept();
234  rlDebugPrintf("\n starting listener on port %d...\n", port);
235  // initialize socket with daemonport
236  rlSocket s("localhost",port,0);
237  // listener main loop
238  while(1){
239  socketdescr = s.connect();
240  if(socketdescr == -1) break;
241  tr.socketdescr = socketdescr;
242  daemonptr->daemon_thread.create(clientconnection, &tr);
243  // wait for clientconnection to save socketdescriptor
244  rlsleep(100);
245  }
246  rlDebugPrintf("\n killing listener...\n");
247  return NULL;
248 }
rlThread daemon_thread
int rlDebugPrintf(const char *format,...)
Definition: rlcutil.cpp:61
canopen tcp/ip interface for concurrent device-access of multiple clientprocesses ...
void * user
Definition: rlthread.h:30
static void * clientconnection(void *arg)
int create(void *(*func)(void *), void *argument)
Definition: rlthread.cpp:35
rlCanOpenDaemon * daemonptr
int getPort()
returns value of current port
void rlsleep(long msec)
Definition: rlwthread.cpp:396

◆ nmt_transmit()

void nmt_transmit ( rlCanOpenDaemon _daemonptr,
rlSocket _socket,
IPCMSGSTRUCT _message 
)

gets called from clientconnection thread. performs a nmt_transmit command on the canopen interface. received data is stored in an IPCMSGSTRUCT and sent back to the clientprocess

Definition at line 118 of file rlcanopendaemon.cpp.

120  {
121  char returnvalue;
122  _daemonptr->daemon_thread.lock();
123  returnvalue=_daemonptr->nodes->sendNMTCommand(_message->boardid,
124  _message->nodeid,
125  _message->mtext[0]);
126  _daemonptr->daemon_thread.unlock();
127  _message->msgtype=MSG_PDO_TRANSMIT; _message->transfertype=returnvalue;
128  _socket->write((void*) _message, sizeof(*_message));
129 }
rlThread daemon_thread
int write(const void *buf, int len)
Definition: rlsocket.cpp:292
char mtext[247]
int unlock()
Definition: rlthread.cpp:52
rlCanOpen * nodes
int sendNMTCommand(int _boardnr, int _nodeid, unsigned char _cmd)
send a NMT Command to one or all nodes (of one board)
Definition: rlcanopen.cpp:1132
int lock()
Definition: rlthread.cpp:47

◆ pdo_receive()

void pdo_receive ( rlCanOpenDaemon _daemonptr,
rlSocket _socket,
IPCMSGSTRUCT _message 
)

gets called from clientconnection thread. performs a pdo_receive command on the canopen interface. received data is stored in an IPCMSGSTRUCT and sent back to the clientprocess

Definition at line 58 of file rlcanopendaemon.cpp.

60  {
61  int returnvalue;
62  rlCanOpenTypes cifmsg;
63  cifmsg.getIpcMsg(*_message);
64  _daemonptr->daemon_thread.lock();
65  if (_message->mappingid==-1){
66  returnvalue=_daemonptr->nodes->pdo_receive(_message->boardid,
67  _message->nodeid,
68  _message->pdoid,
69  cifmsg);
70  }
71  else
72  {
73  returnvalue=_daemonptr->nodes->pdo_receive(_message->boardid,
74  _message->nodeid,
75  _message->pdoid,
76  _message->mappingid,
77  cifmsg);
78  }
79  _daemonptr->daemon_thread.unlock();
80  *_message = cifmsg.createIpcMsg();
81  _message->msgtype=MSG_PDO_RECEIVE; _message->transfertype=returnvalue;
82  _socket->write((void*) _message, sizeof(*_message));
83 }
rlThread daemon_thread
int write(const void *buf, int len)
Definition: rlsocket.cpp:292
void getIpcMsg(IPCMSGSTRUCT _myIpcMsg)
overwrites all data with IPCMSGSTRUCT data
int unlock()
Definition: rlthread.cpp:52
class to handle CANopen types
rlCanOpen * nodes
int pdo_receive(int _boardnr, int _nodeid, int _pdonr, int _mappingnr, rlCanOpenTypes &_pdo_data)
using the pdo_receive function you can receive a mapped object within PDO
Definition: rlcanopen.cpp:544
IPCMSGSTRUCT createIpcMsg()
returns a IPCMSGSTRUCT filled with current object data
int lock()
Definition: rlthread.cpp:47

◆ pdo_transmit()

void pdo_transmit ( rlCanOpenDaemon _daemonptr,
rlSocket _socket,
IPCMSGSTRUCT _message 
)

gets called from clientconnection thread. performs a pdo_transmit command on the canopen interface. received data is stored in an IPCMSGSTRUCT and sent back to the clientprocess

Definition at line 88 of file rlcanopendaemon.cpp.

90  {
91  int returnvalue;
92  rlCanOpenTypes cifmsg;
93  cifmsg.getIpcMsg(*_message);
94  _daemonptr->daemon_thread.lock();
95  if (_message->mappingid==-1){
96  returnvalue=_daemonptr->nodes->pdo_transmit(_message->boardid,
97  _message->nodeid,
98  _message->pdoid,
99  cifmsg);
100  }
101  else
102  {
103  returnvalue=_daemonptr->nodes->pdo_transmit(_message->boardid,
104  _message->nodeid,
105  _message->pdoid,
106  _message->mappingid,
107  cifmsg);
108  }
109  _daemonptr->daemon_thread.unlock();
110  *_message = cifmsg.createIpcMsg();
111  _message->msgtype=MSG_PDO_TRANSMIT; _message->transfertype=returnvalue;
112  _socket->write((void*) _message, sizeof(*_message));
113 }
rlThread daemon_thread
int write(const void *buf, int len)
Definition: rlsocket.cpp:292
void getIpcMsg(IPCMSGSTRUCT _myIpcMsg)
overwrites all data with IPCMSGSTRUCT data
int unlock()
Definition: rlthread.cpp:52
class to handle CANopen types
rlCanOpen * nodes
int pdo_transmit(int _boardnr, int _nodeid, int _pdonr, int _mappingnr, rlCanOpenTypes &_pdo_data)
transmit a mapped object within a PDO to a specific node
Definition: rlcanopen.cpp:703
IPCMSGSTRUCT createIpcMsg()
returns a IPCMSGSTRUCT filled with current object data
int lock()
Definition: rlthread.cpp:47

◆ restart_board()

void restart_board ( rlCanOpenDaemon _daemonptr,
rlSocket _socket,
IPCMSGSTRUCT _message 
)

gets called from clientconnection thread. performs a restartBoard command on the canopen interface. received data is stored in an IPCMSGSTRUCT and sent back to the clientprocess

Definition at line 134 of file rlcanopendaemon.cpp.

136  {
137  char returnvalue;
138  _daemonptr->daemon_thread.lock();
139  returnvalue=_daemonptr->nodes->restartBoard(_message->boardid,
140  (int) _message->mtext[0]);
141  _daemonptr->daemon_thread.unlock();
142  _message->msgtype=MSG_PDO_TRANSMIT; _message->transfertype=returnvalue;
143  _socket->write((void*) _message, sizeof(*_message));
144 }
rlThread daemon_thread
int write(const void *buf, int len)
Definition: rlsocket.cpp:292
char mtext[247]
int unlock()
Definition: rlthread.cpp:52
rlCanOpen * nodes
int restartBoard(int _boardnr, int _restarttype)
using this function you are able to restart a CanOpenMaster board
Definition: rlcanopen.cpp:1114
int lock()
Definition: rlthread.cpp:47

◆ sdo_read()

void sdo_read ( rlCanOpenDaemon _daemonptr,
rlSocket _socket,
IPCMSGSTRUCT _message 
)

gets called from clientconnection thread. performs a sdo_read command on the canopen interface. received data is stored in an IPCMSGSTRUCT and sent back to the clientprocess

Definition at line 21 of file rlcanopendaemon.cpp.

23  {
24  rlCanOpenTypes cifmsg;
25  _daemonptr->daemon_thread.lock();
26  _daemonptr->nodes->sdo_read(_message->boardid,
27  _message->nodeid,
28  _message->objectindex,
29  _message->subindex, cifmsg);
30  _daemonptr->daemon_thread.unlock();
31  *_message = cifmsg.createIpcMsg();
32  _message->msgtype=MSG_SDO_READ; _message->transfertype=MSG_RECEIVE;
33  _socket->write((void*) _message, sizeof(*_message));
34 }
rlThread daemon_thread
int write(const void *buf, int len)
Definition: rlsocket.cpp:292
int unlock()
Definition: rlthread.cpp:52
class to handle CANopen types
int sdo_read(int _boardnr, int _nodeid, int _objectindex, int _subindex, rlCanOpenTypes &_sdo_data)
read a certain object from the object dictionary of a node
Definition: rlcanopen.cpp:332
rlCanOpen * nodes
IPCMSGSTRUCT createIpcMsg()
returns a IPCMSGSTRUCT filled with current object data
int lock()
Definition: rlthread.cpp:47

◆ sdo_write()

void sdo_write ( rlCanOpenDaemon _daemonptr,
rlSocket _socket,
IPCMSGSTRUCT _message 
)

gets called from clientconnection thread. performs a sdo_write command on the canopen interface. received data is stored in an IPCMSGSTRUCT and sent back to the clientprocess

Definition at line 39 of file rlcanopendaemon.cpp.

41  {
42  rlCanOpenTypes cifmsg;
43  cifmsg.getIpcMsg(*_message);
44  _daemonptr->daemon_thread.lock();
45  _daemonptr->nodes->sdo_write(_message->boardid,
46  _message->nodeid,
47  _message->objectindex,
48  _message->subindex, cifmsg);
49  _daemonptr->daemon_thread.unlock();
50  *_message = cifmsg.createIpcMsg();
51  _message->msgtype=MSG_SDO_WRITE; _message->transfertype=MSG_RECEIVE;
52  _socket->write((void*) _message, sizeof(*_message));
53 }
rlThread daemon_thread
int write(const void *buf, int len)
Definition: rlsocket.cpp:292
void getIpcMsg(IPCMSGSTRUCT _myIpcMsg)
overwrites all data with IPCMSGSTRUCT data
int unlock()
Definition: rlthread.cpp:52
class to handle CANopen types
rlCanOpen * nodes
int sdo_write(int _boardnr, int _nodeid, int _objectindex, int _subindex, rlCanOpenTypes &_sdo_data)
write data into a certain object from the object dictionary of a node
Definition: rlcanopen.cpp:431
IPCMSGSTRUCT createIpcMsg()
returns a IPCMSGSTRUCT filled with current object data
int lock()
Definition: rlthread.cpp:47