OpenCores
URL https://opencores.org/ocsvn/w11/w11/trunk

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.7/] [tools/] [src/] [librlinktpp/] [RtclAttnShuttle.cpp] - Blame information for rev 33

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 29 wfjm
// $Id: RtclAttnShuttle.cpp 631 2015-01-09 21:36:51Z mueller $
2 19 wfjm
//
3 27 wfjm
// Copyright 2013-2014 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 19 wfjm
//
5
// This program is free software; you may redistribute and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, either version 2, or at your option any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
// for complete details.
13
// 
14
// Revision History: 
15
// Date         Rev Version  Comment
16 28 wfjm
// 2014-12-30   625   1.1    adopt to Rlink V4 attn logic
17 27 wfjm
// 2014-11-08   602   1.0.3  cast int first to ptrdiff_t, than to ClientData
18
// 2014-08-22   584   1.0.2  use nullptr
19 22 wfjm
// 2013-05-20   521   1.0.1  Setup proper Tcl channel options
20 19 wfjm
// 2013-03-01   493   1.0    Initial version
21
// 2013-01-12   475   0.5    First draft
22
// ---------------------------------------------------------------------------
23
 
24
/*!
25
  \file
26 29 wfjm
  \version $Id: RtclAttnShuttle.cpp 631 2015-01-09 21:36:51Z mueller $
27 19 wfjm
  \brief   Implemenation of class RtclAttnShuttle.
28
 */
29
 
30 22 wfjm
#include <errno.h>
31 19 wfjm
 
32
#include "boost/bind.hpp"
33
 
34
#include "librtools/Rexception.hpp"
35
 
36
#include "RtclAttnShuttle.hpp"
37
 
38
using namespace std;
39
 
40
/*!
41
  \class Retro::RtclAttnShuttle
42
  \brief FIXME_docs
43
*/
44
 
45
// all method definitions in namespace Retro
46
namespace Retro {
47
 
48
//------------------------------------------+-----------------------------------
49
//! constructor
50
 
51
RtclAttnShuttle::RtclAttnShuttle(uint16_t mask, Tcl_Obj* pobj)
52 29 wfjm
  : fpServ(nullptr),
53
    fpInterp(nullptr),
54 22 wfjm
    fFdPipeRead(-1),
55
    fFdPipeWrite(-1),
56 19 wfjm
    fShuttleChn(0),
57
    fMask(mask),
58
    fpScript(pobj)
59
{
60
  int pipefd[2];
61 22 wfjm
  int irc = ::pipe(pipefd);
62 19 wfjm
  if (irc < 0)
63
    throw Rexception("RtclAttnShuttle::<ctor>", "pipe() failed: ", errno);
64
  fFdPipeRead  = pipefd[0];
65
  fFdPipeWrite = pipefd[1];
66
}
67
 
68
//------------------------------------------+-----------------------------------
69
//! Destructor
70
 
71
RtclAttnShuttle::~RtclAttnShuttle()
72
{
73
  Remove();
74 22 wfjm
  ::close(fFdPipeWrite);
75
  ::close(fFdPipeRead);
76 19 wfjm
}
77
 
78
//------------------------------------------+-----------------------------------
79
//! FIXME_docs
80
 
81
void RtclAttnShuttle::Add(RlinkServer* pserv, Tcl_Interp* interp)
82
{
83
  // connect to RlinkServer
84
  pserv->AddAttnHandler(boost::bind(&RtclAttnShuttle::AttnHandler, this, _1),
85
                        fMask, (void*)this);
86
  fpServ = pserv;
87
 
88
  // connect to Tcl
89 27 wfjm
  // cast first to ptrdiff_t to promote to proper int size
90
  fShuttleChn = Tcl_MakeFileChannel((ClientData)(ptrdiff_t)fFdPipeRead,
91
                                    TCL_READABLE);
92 22 wfjm
 
93 27 wfjm
  Tcl_SetChannelOption(nullptr, fShuttleChn, "-buffersize", "64");
94
  Tcl_SetChannelOption(nullptr, fShuttleChn, "-encoding", "binary");
95
  Tcl_SetChannelOption(nullptr, fShuttleChn, "-translation", "binary");
96 22 wfjm
 
97 19 wfjm
  Tcl_CreateChannelHandler(fShuttleChn, TCL_READABLE,
98
                           (Tcl_FileProc*) ThunkTclChannelHandler,
99
                           (ClientData) this);
100 22 wfjm
 
101 19 wfjm
  fpInterp = interp;
102
  return;
103
}
104
 
105
//------------------------------------------+-----------------------------------
106
//! FIXME_docs
107
 
108
void RtclAttnShuttle::Remove()
109
{
110
  // disconnect from RlinkServer
111
  if (fpServ) {
112
    fpServ->RemoveAttnHandler(fMask, (void*)this);
113 29 wfjm
    fpServ = nullptr;
114 19 wfjm
  }
115
  // disconnect from Tcl
116
  if (fpInterp) {
117
    Tcl_DeleteChannelHandler(fShuttleChn,
118
                             (Tcl_FileProc*) ThunkTclChannelHandler,
119
                             (ClientData) this);
120
    Tcl_Close(fpInterp, fShuttleChn);
121 29 wfjm
    fpInterp = nullptr;
122 19 wfjm
  }
123
 
124
  return;
125
}
126
 
127
//------------------------------------------+-----------------------------------
128
//! FIXME_docs
129
 
130 28 wfjm
int RtclAttnShuttle::AttnHandler(RlinkServer::AttnArgs& args)
131 19 wfjm
{
132 28 wfjm
  fpServ->GetAttnInfo(args);
133
 
134 19 wfjm
  uint16_t apat = args.fAttnPatt & args.fAttnMask;
135 22 wfjm
  int irc = ::write(fFdPipeWrite, (void*) &apat, sizeof(apat));
136 19 wfjm
  if (irc < 0)
137
    throw Rexception("RtclAttnShuttle::AttnHandler()",
138
                     "write() failed: ", errno);
139 28 wfjm
 
140 19 wfjm
  return 0;
141
}
142
 
143
//------------------------------------------+-----------------------------------
144
//! FIXME_docs
145
 
146
void RtclAttnShuttle::TclChannelHandler(int mask)
147
{
148
  uint16_t apat;
149
  Tcl_ReadRaw(fShuttleChn, (char*) &apat, sizeof(apat));
150
  // FIXME_code: handle return code
151
 
152 27 wfjm
  Tcl_SetVar2Ex(fpInterp, "Rlink_attnbits", nullptr,
153
                Tcl_NewIntObj((int)apat), 0);
154 19 wfjm
  // FIXME_code: handle return code
155
 
156
  Tcl_EvalObjEx(fpInterp, fpScript, TCL_EVAL_GLOBAL);
157
  // FIXME_code: handle return code
158
  return;
159
}
160
 
161
//------------------------------------------+-----------------------------------
162
//! FIXME_docs
163
 
164
void RtclAttnShuttle::ThunkTclChannelHandler(ClientData cdata, int mask)
165
{
166
  ((RtclAttnShuttle*) cdata)->TclChannelHandler(mask);
167
  return;
168
}
169
 
170
} // end namespace Retro

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.