Debian Codegraph @

src/lw/LabelWriterLanguageMonitor.cpp

raw
1 // -*- C++ -*-
2 // $Id: LabelWriterLanguageMonitor.cpp 15965 2011-09-02 14:48:46Z pineichen $
3
4 // DYMO LabelWriter Drivers
5 // Copyright (C) 2008 Sanford L.P.
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
9 // as published by the Free Software Foundation; either version 2
10 // of 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 this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21
22 #include "LabelWriterLanguageMonitor.h"
23 #include <assert.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29
30 namespace DymoPrinterDriver
31 {
32
33 //const byte ESC = 0x1B;
34 CLabelWriterLanguageMonitor::CLabelWriterLanguageMonitor(IPrintEnvironment& Environment, bool UseSleep, size_t ReadStatusTimeout):
35 Environment_(Environment), PaperType_(CLabelWriterDriver::ptRegular), Roll_(CLabelWriterDriverTwinTurbo::rtAuto), RollUsed_(false), IsFirstPage_(true), PageData_(), UseSleep_(UseSleep), LastStatus_(0), LastReadStatusResult_(true), ReadStatusTimeout_(ReadStatusTimeout)
36 {
37 }
38
39 CLabelWriterLanguageMonitor::~CLabelWriterLanguageMonitor()
40 {
41 }
42
43 void
44 CLabelWriterLanguageMonitor::StartDoc()
45 {
46 IsFirstPage_ = true;
47 ResetPrinter();
48
49 if (RollUsed_)
50 SynchronizeRoll();
51 }
52
53 void
54 CLabelWriterLanguageMonitor::EndDoc()
55 {
56 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::EndDoc()\n");
57 //CheckStatusAndReprint();
58 }
59
60 void
61 CLabelWriterLanguageMonitor::StartPage()
62 {
63 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::StartPage()\n");
64 if (IsFirstPage_)
65 {
66 CheckStatusAndReprint();
67 }
68
69 IsFirstPage_ = false;
70 }
71
72 void
73 CLabelWriterLanguageMonitor::EndPage()
74 {
75 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::EndPage()\n");
76 CheckStatusAndReprint();
77 }
78
79 bool
80 CLabelWriterLanguageMonitor::IsLocal()
81 {
82 bool bIsLocal = true;
83
84 char* uri = getenv("DEVICE_URI");
85
86 if(uri != NULL)
87 bIsLocal = (strncmp(uri, "usb://", 6) == 0);
88
89 return bIsLocal;
90 }
91
92 void
93 CLabelWriterLanguageMonitor::SynchronizeRoll()
94 {
95 buffer_t buf = CLabelWriterDriverTwinTurbo::GetRollSelectCommand(Roll_);
96 Environment_.WriteData(buf);
97 }
98
99 void
100 CLabelWriterLanguageMonitor::ResetPrinter()
101 {
102 buffer_t buf = CLabelWriterDriver::GetResetCommand();
103 Environment_.WriteData(buf);
104 }
105
106
107 void
108 CLabelWriterLanguageMonitor::CheckStatusAndReprint()
109 {
110 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::CheckStatusAndReprint()\n");
111
112 // restore good status of the job
113 SetJobStatus(TOF_BIT);
114
115 if(!IsLocal())
116 return;
117
118 while (true) // reprint also can fail, so don't forget to recheck status after reprint
119 {
120 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::CheckStatusAndReprint() 1\n");
121
122 byte Status = 0;
123 time_t BeginTime = time(NULL);
124 bool StatusOK = ReadStatus(Status);
125
126 // request status while good or bad condition or timeout
127 int i = 0;
128 while (
129 //StatusOK
130 !((Status & TOF_BIT) || (Status & ERROR_BIT) || (Status & ROLL_CHANGED_BIT))
131 && (difftime(time(NULL), BeginTime) < ReadStatusTimeout_))
132 {
133 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::CheckStatusAndReprint() 2 %i\n", i);
134 StatusOK = ReadStatus(Status);
135 //usleep(100000);
136 i++;
137 }
138
139 if (difftime(time(NULL), BeginTime) >= ReadStatusTimeout_)
140 {
141 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::CheckStatusAndReprint() timeout\n");
142 break;
143 }
144
145 //if (!StatusOK)
146 //{
147 // fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::CheckStatusAndReprint() 3\n");
148 // break;
149 //}
150
151 // error - needs reprint
152 if ((Status & ERROR_BIT) || (Status & ROLL_CHANGED_BIT) || !(Status & TOF_BIT))
153 {
154 // force error bit in case of timeout
155 if (!(Status & TOF_BIT) && !(Status & ROLL_CHANGED_BIT))
156 Status |= ERROR_BIT;
157
158 SetJobStatus(Status);
159 if (PollUntilPaperIn())
160 {
161 // restore good status of the job
162 SetJobStatus(TOF_BIT);
163
164 ReprintLabel();
165 }
166 }
167 else
168 break;
169 }
170
171 // clear stored label data
172 PageData_.clear();
173
174 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::CheckStatusAndReprint() return\n");
175 }
176
177 bool
178 CLabelWriterLanguageMonitor::ReadStatus(byte& Status)
179 {
180 time_t t = time(NULL);
181 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::ReadStatus() %s\n", ctime(&t));
182
183 bool Result = false;
184 Status = 0; // default
185 LastStatus_ = 0;
186
187 //if (LastReadStatusResult_)
188 {
189 buffer_t RequestStatusCommand = CLabelWriterDriver::GetRequestStatusCommand();
190 Environment_.WriteData(RequestStatusCommand);
191 }
192 //Environment_.WriteData(buffer_t(128, 0));
193 //Environment_.WriteData(RequestStatusCommand);
194
195 //byte b[] = {
196 // 0x1b, 'A', 0x1b, 'A', 0x1b, 'A', 0x1b, 'A', 0x1b, 'A', 0x1b, 'A', 0x1b, 'A', 0x1b, 'A',
197 // 0x1b, 'A', 0x1b, 'A', 0x1b, 'A', 0x1b, 'A', 0x1b, 'A', 0x1b, 'A', 0x1b, 'A', 0x1b, 'A'};
198 //byte b[] = { 0x1b, 'A', 0x1b, 'A'};
199 //Environment_.WriteData(buffer_t(b, b + sizeof(b)));
200
201 buffer_t buf;
202 Environment_.ReadData(buf);
203
204 if (buf.size() > 0)
205 {
206
207 Status = buf[0];
208
209 if (PaperType_ == CLabelWriterDriver::ptContinuous)
210 Status |= TOF_BIT;
211
212 Result = true;
213 }
214
215 //if (!LastReadStatusResult_ && Result)
216 //{
217 // LastReadStatusResult_ = true;
218 // Result = false;
219 // }
220 //else
221 // LastReadStatusResult_ = Result;
222
223 LastStatus_ = Status;
224
225 fprintf(stderr, "DEBUG: ReadStatus() returned %x %i\n", Status, (int)Result);
226 return Result;
227 }
228
229 bool
230 CLabelWriterLanguageMonitor::PollUntilPaperIn()
231 {
232 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::PollUntilPaperIn()\n");
233
234 byte Status = 0;
235 for(;;)
236 {
237 // TODO: use platform-undependend call
238 if (UseSleep_)
239 {
240 //sleep(2);
241 timespec interval;
242 interval.tv_sec = 0;
243 interval.tv_nsec = 200000000; // 0.2 second
244 nanosleep(&interval, NULL);
245 }
246
247 if (Environment_.GetJobStatus() == IPrintEnvironment::jsDeleted)
248 return false;
249
250 if (!ReadStatus(Status))
251 return false;
252
253 SetJobStatus(Status); // update status
254
255 if ((Status & TOF_BIT) && !(Status & ERROR_BIT))
256 return true;
257 }
258 }
259
260 void
261 CLabelWriterLanguageMonitor::SetJobStatus(byte Status)
262 {
263 IPrintEnvironment::job_status_t JobStatus = IPrintEnvironment::jsOK;
264
265 if ((Status & PAPER_OUT_BIT) || (Status & PAPER_FEED_BIT))
266 JobStatus = IPrintEnvironment::jsPaperOut;
267 else if (Status & ERROR_BIT)
268 JobStatus = IPrintEnvironment::jsError;
269
270 Environment_.SetJobStatus(JobStatus);
271 }
272
273 void
274 CLabelWriterLanguageMonitor::ReprintLabel()
275 {
276 fprintf(stderr, "DEBUG: CLabelWriterLanguageMonitor::ReprintLabel()\n");
277
278 // send form feed first
279 if (!(LastStatus_ & ROLL_CHANGED_BIT))
280 {
281 buffer_t ShortFormFeedCommand = CLabelWriterDriver400::GetShortFormFeedCommand();
282 Environment_.WriteData(ShortFormFeedCommand);
283 }
284
285 Environment_.WriteData(PageData_);
286 }
287
288 void
289 CLabelWriterLanguageMonitor::ProcessData(const buffer_t& Data)
290 {
291 PageData_.insert(PageData_.end(), Data.begin(), Data.end());
292 }
293
294 void
295 CLabelWriterLanguageMonitor::SetPaperType(CLabelWriterDriver::paper_type_t Value)
296 {
297 PaperType_ = Value;
298 }
299
300 void
301 CLabelWriterLanguageMonitor::SetRoll(CLabelWriterDriverTwinTurbo::roll_t Value)
302 {
303 Roll_ = Value;
304 RollUsed_ = true;
305 }
306
307
308 }; // namespace
309
310
311 /*
312 * End of "$Id: LabelWriterLanguageMonitor.cpp 15965 2011-09-02 14:48:46Z pineichen $".
313 */
314
315
316