| 1 |
// -*- C++ -*- |
| 2 |
// $Id$ |
| 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 "LabelManagerLanguageMonitor.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 |
CLabelManagerLanguageMonitor::CLabelManagerLanguageMonitor(IPrintEnvironment& Environment, bool UseSleep, size_t ReadStatusTimeout): |
| 33 |
Environment_(Environment), IsFirstPage_(true), PageData_(), UseSleep_(UseSleep), LastReadStatusResult_(true), ReadStatusTimeout_(ReadStatusTimeout) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
CLabelManagerLanguageMonitor::~CLabelManagerLanguageMonitor() |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
void |
| 42 |
CLabelManagerLanguageMonitor::StartDoc() |
| 43 |
{ |
| 44 |
IsFirstPage_ = true; |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
CLabelManagerLanguageMonitor::EndDoc() |
| 49 |
{ |
| 50 |
fprintf(stderr, "DEBUG: CLabelManagerLanguageMonitor::EndDoc()\n"); |
| 51 |
} |
| 52 |
|
| 53 |
void |
| 54 |
CLabelManagerLanguageMonitor::StartPage() |
| 55 |
{ |
| 56 |
fprintf(stderr, "DEBUG: CLabelManagerLanguageMonitor::StartPage()\n"); |
| 57 |
if (IsFirstPage_) |
| 58 |
{ |
| 59 |
CheckStatus(); |
| 60 |
} |
| 61 |
|
| 62 |
IsFirstPage_ = false; |
| 63 |
} |
| 64 |
|
| 65 |
void |
| 66 |
CLabelManagerLanguageMonitor::EndPage() |
| 67 |
{ |
| 68 |
fprintf(stderr, "DEBUG: CLabelManagerLanguageMonitor::EndPage()\n"); |
| 69 |
} |
| 70 |
|
| 71 |
bool |
| 72 |
CLabelManagerLanguageMonitor::IsLocal() |
| 73 |
{ |
| 74 |
char* uri = getenv("DEVICE_URI"); |
| 75 |
|
| 76 |
return (strncmp(uri, "usb://", 6) == 0); |
| 77 |
} |
| 78 |
|
| 79 |
void |
| 80 |
CLabelManagerLanguageMonitor::CheckStatus() |
| 81 |
{ |
| 82 |
fprintf(stderr, "DEBUG: CLabelManagerLanguageMonitor::CheckStatus()\n"); |
| 83 |
|
| 84 |
// restore good status of the job |
| 85 |
|
| 86 |
SetJobStatus(CASSETTE_PRESENT_BIT); |
| 87 |
|
| 88 |
if(!IsLocal()) |
| 89 |
return; |
| 90 |
|
| 91 |
while (true) |
| 92 |
{ |
| 93 |
fprintf(stderr, "DEBUG: CLabelManagerLanguageMonitor::CheckStatus() 1\n"); |
| 94 |
|
| 95 |
buffer_t Status; |
| 96 |
time_t BeginTime = time(NULL); |
| 97 |
bool StatusOK = ReadStatus(Status); |
| 98 |
|
| 99 |
// request status while good or bad condition or timeout |
| 100 |
int i = 0; |
| 101 |
while ((!StatusOK || (Status[0] & BUSY_BIT)) && (difftime(time(NULL), BeginTime) < ReadStatusTimeout_)) |
| 102 |
{ |
| 103 |
fprintf(stderr, "DEBUG: CLabelManagerLanguageMonitor::CheckStatus() 2 %i\n", i); |
| 104 |
|
| 105 |
StatusOK = ReadStatus(Status); |
| 106 |
|
| 107 |
i++; |
| 108 |
} |
| 109 |
|
| 110 |
if (difftime(time(NULL), BeginTime) >= ReadStatusTimeout_) |
| 111 |
{ |
| 112 |
SetJobStatus(BUSY_BIT); |
| 113 |
|
| 114 |
fprintf(stderr, "DEBUG: CLabelManagerLanguageMonitor::CheckStatus() timeout\n"); |
| 115 |
|
| 116 |
break; |
| 117 |
} |
| 118 |
|
| 119 |
if((Status[0] & CASSETTE_PRESENT_BIT) == CASSETTE_PRESENT_BIT && !CheckTapeSize(Status)) |
| 120 |
Status[0] = INCORRECT_SIZE_BIT; |
| 121 |
|
| 122 |
if ((Status[0] == INCORRECT_SIZE_BIT) || |
| 123 |
(Status[0] & GENERAL_ERROR_BIT) || |
| 124 |
(Status[0] & HEAD_OVERHEAT_BIT) || |
| 125 |
(Status[0] & SLOT_STATUS_BIT) || |
| 126 |
((Status[0] & CASSETTE_PRESENT_BIT) == 0)) |
| 127 |
{ |
| 128 |
SetJobStatus(Status[0]); |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
// restore good status of the job |
| 133 |
|
| 134 |
SetJobStatus(CASSETTE_PRESENT_BIT); |
| 135 |
|
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// clear stored label data |
| 141 |
PageData_.clear(); |
| 142 |
|
| 143 |
fprintf(stderr, "DEBUG: CLabelManagerLanguageMonitor::CheckStatus() return\n"); |
| 144 |
} |
| 145 |
|
| 146 |
bool |
| 147 |
CLabelManagerLanguageMonitor::ReadStatus(buffer_t& Status) |
| 148 |
{ |
| 149 |
time_t t = time(NULL); |
| 150 |
fprintf(stderr, "DEBUG: CLabelManagerLanguageMonitor::ReadStatus() %s\n", ctime(&t)); |
| 151 |
|
| 152 |
bool Result = false; |
| 153 |
Status.clear(); |
| 154 |
|
| 155 |
buffer_t RequestStatusCommand = CLabelManagerDriver::GetRequestStatusCommand(); |
| 156 |
Environment_.WriteData(RequestStatusCommand); |
| 157 |
|
| 158 |
Environment_.ReadData(Status); |
| 159 |
|
| 160 |
if (Status.size() > 0) |
| 161 |
{ |
| 162 |
Result = true; |
| 163 |
} |
| 164 |
|
| 165 |
fprintf(stderr, "DEBUG: ReadStatus() returned %i %i\n", (int)Status.size(), (int)Result); |
| 166 |
return Result; |
| 167 |
} |
| 168 |
|
| 169 |
void |
| 170 |
CLabelManagerLanguageMonitor::SetJobStatus(byte Status) |
| 171 |
{ |
| 172 |
IPrintEnvironment::job_status_t JobStatus = IPrintEnvironment::jsOK; |
| 173 |
|
| 174 |
if (Status == INCORRECT_SIZE_BIT) |
| 175 |
JobStatus = IPrintEnvironment::jsPaperSizeError; |
| 176 |
else if (Status & GENERAL_ERROR_BIT) |
| 177 |
JobStatus = IPrintEnvironment::jsError; |
| 178 |
else if (Status & HEAD_OVERHEAT_BIT) |
| 179 |
JobStatus = IPrintEnvironment::jsHeadOverheat; |
| 180 |
else if (Status & SLOT_STATUS_BIT) |
| 181 |
JobStatus = IPrintEnvironment::jsSlotStatusError; |
| 182 |
else if (Status & BUSY_BIT) |
| 183 |
JobStatus = IPrintEnvironment::jsBusy; |
| 184 |
else if ((Status & CASSETTE_PRESENT_BIT) == 0) |
| 185 |
JobStatus = IPrintEnvironment::jsPaperSizeUndefinedError; |
| 186 |
|
| 187 |
Environment_.SetJobStatus(JobStatus); |
| 188 |
} |
| 189 |
|
| 190 |
bool |
| 191 |
CLabelManagerLanguageMonitor::CheckTapeSize(buffer_t Status) |
| 192 |
{ |
| 193 |
fprintf(stderr, "DEBUG: CheckTapeSize() device %s tape %d\n", DeviceName_.c_str(), TapeWidth_); |
| 194 |
|
| 195 |
if (!strcasecmp(DeviceName_.c_str(), "DYMO LabelWriter DUO Tape 128") || |
| 196 |
!strcasecmp(DeviceName_.c_str(), "DYMO LabelWriter 450 DUO Tape") || |
| 197 |
!strcasecmp(DeviceName_.c_str(), "DYMO LabelMANAGER 400") || |
| 198 |
!strcasecmp(DeviceName_.c_str(), "DYMO LabelMANAGER PC II") || |
| 199 |
!strcasecmp(DeviceName_.c_str(), "DYMO LabelMANAGER PC")) |
| 200 |
{ |
| 201 |
if((Status[0] & CASSETTE_PRESENT_BIT) == CASSETTE_PRESENT_BIT) |
| 202 |
return true; |
| 203 |
else |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
if (!strcasecmp(DeviceName_.c_str(), "DYMO LabelWriter DUO Tape")) |
| 208 |
{ |
| 209 |
if((Status[0] & CASSETTE_PRESENT_BIT) == CASSETTE_PRESENT_BIT) |
| 210 |
{ |
| 211 |
if((((Status[0] & CASSETTE_SIZE_BITS) == 0x00) && TapeWidth_ == CLabelManagerDriver::tw6mm) || |
| 212 |
(((Status[0] & CASSETTE_SIZE_BITS) == 0x01) && (TapeWidth_ == CLabelManagerDriver::tw9mm || TapeWidth_ == CLabelManagerDriver::tw12mm)) || |
| 213 |
(((Status[0] & CASSETTE_SIZE_BITS) == 0x02) && TapeWidth_ == CLabelManagerDriver::tw19mm) || |
| 214 |
(((Status[0] & CASSETTE_SIZE_BITS) == 0x03) && TapeWidth_ == CLabelManagerDriver::tw24mm)) |
| 215 |
return true; |
| 216 |
else |
| 217 |
return false; |
| 218 |
} |
| 219 |
else |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
if (!strcasecmp(DeviceName_.c_str(), "DYMO LabelMANAGER 450")) |
| 224 |
{ |
| 225 |
if(((Status[1] & 0xFF) == 0x00) || |
| 226 |
(((Status[1] & 0xFF) == 0x01) && TapeWidth_ == CLabelManagerDriver::tw6mm) || |
| 227 |
(((Status[1] & 0xFF) == 0x02) && TapeWidth_ == CLabelManagerDriver::tw9mm) || |
| 228 |
(((Status[1] & 0xFF) == 0x03) && TapeWidth_ == CLabelManagerDriver::tw12mm) || |
| 229 |
(((Status[1] & 0xFF) == 0x04) && TapeWidth_ == CLabelManagerDriver::tw19mm) || |
| 230 |
(((Status[1] & 0xFF) == 0x05) && TapeWidth_ == CLabelManagerDriver::tw24mm)) |
| 231 |
return true; |
| 232 |
else |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
if (!strcasecmp(DeviceName_.c_str(), "DYMO LabelPOINT 350")) |
| 237 |
{ |
| 238 |
if((((Status[0] & CASSETTE_SIZE_BITS) == 0x01) && TapeWidth_ == CLabelManagerDriver::tw6mm) || |
| 239 |
(((Status[0] & CASSETTE_SIZE_BITS) == 0x02) && (TapeWidth_ == CLabelManagerDriver::tw19mm || TapeWidth_ == CLabelManagerDriver::tw24mm)) || |
| 240 |
(((Status[0] & CASSETTE_SIZE_BITS) == 0x03) && (TapeWidth_ == CLabelManagerDriver::tw9mm || TapeWidth_ == CLabelManagerDriver::tw12mm))) |
| 241 |
return true; |
| 242 |
else |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
if (!strcasecmp(DeviceName_.c_str(), "DYMO LabelMANAGER PnP") || |
| 247 |
!strcasecmp(DeviceName_.c_str(), "DYMO LabelMANAGER 420P") || |
| 248 |
!strcasecmp(DeviceName_.c_str(), "DYMO LabelManager 500TS")) |
| 249 |
{ |
| 250 |
if((Status[0] & CASSETTE_PRESENT_BIT) == CASSETTE_PRESENT_BIT) |
| 251 |
{ |
| 252 |
if(((Status[1] & 0xFF) == 0x00) || |
| 253 |
(((Status[1] & 0xFF) == 0x01) && TapeWidth_ == CLabelManagerDriver::tw6mm) || |
| 254 |
(((Status[1] & 0xFF) == 0x02) && TapeWidth_ == CLabelManagerDriver::tw9mm) || |
| 255 |
(((Status[1] & 0xFF) == 0x03) && TapeWidth_ == CLabelManagerDriver::tw12mm) || |
| 256 |
(((Status[1] & 0xFF) == 0x04) && TapeWidth_ == CLabelManagerDriver::tw19mm) || |
| 257 |
(((Status[1] & 0xFF) == 0x05) && TapeWidth_ == CLabelManagerDriver::tw24mm)) |
| 258 |
return true; |
| 259 |
else |
| 260 |
return false; |
| 261 |
} |
| 262 |
else |
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
return true; |
| 267 |
} |
| 268 |
|
| 269 |
void |
| 270 |
CLabelManagerLanguageMonitor::ProcessData(const buffer_t& Data) |
| 271 |
{ |
| 272 |
PageData_.insert(PageData_.end(), Data.begin(), Data.end()); |
| 273 |
} |
| 274 |
|
| 275 |
void |
| 276 |
CLabelManagerLanguageMonitor::SetDeviceName(const std::string& Value) |
| 277 |
{ |
| 278 |
DeviceName_ = Value; |
| 279 |
} |
| 280 |
|
| 281 |
void |
| 282 |
CLabelManagerLanguageMonitor::SetTapeWidth(CLabelManagerDriver::tape_width_t Value) |
| 283 |
{ |
| 284 |
TapeWidth_ = Value; |
| 285 |
} |
| 286 |
}; // namespace |
| 287 |
|
| 288 |
|
| 289 |
/* |
| 290 |
* End of "$Id$". |
| 291 |
*/ |
| 292 |
|
| 293 |
|
| 294 |
|