1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
#include "Ase.h"
/* ASE reader generic command types */
#define ASE_LONG_RESPONSE_PID 0x90
#define ASE_RESPONSE_PID 0x10
#define ASE_ACK_PID 0x20
#define ASE_LONG_RESPONSE_WITH_STATUS_PID 0xF0
#define ASE_RESPONSE_WITH_STATUS_PID 0x70
/*****************************************************************************
*
*****************************************************************************/
//#define ASE_PACKET_TYPE(pid, cnt, dst) (pid | cnt << 2 | dst)
#define ASE_PACKET_TYPE(pid, cnt, dst) (pid | dst)
/*****************************************************************************
*
*****************************************************************************/
/* returns 0 on success, negative number otherwise */
int checkValidity (int retVal, int len, int actual, char* message) {
if (retVal < 0 || actual != len) {
#ifdef ASE_DEBUG
syslog(LOG_INFO, "%s Error = %d\n", message, retVal);
#endif
return retVal;
}
return ASE_OK;
}
/*****************************************************************************
*
*****************************************************************************/
static void cleanResponseBuffer (reader* globalData) {
IO_CleanReadBuffer(globalData);
}
/*****************************************************************************
*
*****************************************************************************/
/* returns 0 on success, negative number otherwise
outBuf should be large enough to contain the close response */
int sendCloseResponseCommand (reader* globalData, char socket, char* cmd, int len,
char* outBuf, int* outBufLen, char ignoreEvents) {
int retVal, actual, i, readLen, retryTimes = 5, withStatus = 0;
uchar oneByte, cs, readcs;
unsigned long cwt = (globalData->cards[(int)socket].cwt > 0 ? globalData->cards[(int)socket].cwt : 1000);
/* send the command */
retVal = writeToReader(globalData, cmd, len, &actual);
if (checkValidity(retVal, len, actual, "sendCloseResponseCommand - Error! in command write.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
/* read the close response */
/* set the timeout for the packet header */
cwt = MAX(cwt * 260, globalData->cards[(int)socket].bwt);
cwt += 200000; // delta of 0.2 seconds
/* read packet header and verify it's ok */
retVal = readResponse(globalData, socket, 1, (char*)(&oneByte), &actual, cwt);
if (checkValidity(retVal, 1, actual, "sendCloseResponseCommand - Error! in packet header read.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
/* loop until we get the start of a response or an error has occured */
while (oneByte != ASE_LONG_RESPONSE_PID && oneByte != ASE_RESPONSE_PID &&
oneByte != ASE_RESPONSE_WITH_STATUS_PID && oneByte != ASE_LONG_RESPONSE_WITH_STATUS_PID &&
retryTimes) {
/* check if it's an acknowledgment */
if (oneByte & ASE_ACK_PID) {
if (parseStatus(oneByte) != ASE_READER_EXTRA_WAITING_TIME) {
#ifdef ASE_DEBUG
syslog(LOG_INFO, "sendCloseResponseCommand - Status 0x%x%x.\n", (oneByte & 0xF0) >> 4, oneByte & 0x0F);
#endif
cleanResponseBuffer(globalData);
return parseStatus(oneByte);
}
else
retryTimes = 5;
}
/* check if it's an event */
else if (isEvent(oneByte)) {
/* this is an event and it's concerning the current card */
parseEvent(globalData, socket, oneByte);
#ifdef ASE_DEBUG
syslog(LOG_INFO, "sendCloseResponseCommand - Event 0x%x%x.\n", (oneByte & 0xF0) >> 4, oneByte & 0x0F);
#endif
retryTimes = 5;
}
/* this must be an error -> send a retry command */
else {
char cmd[4];
cmd[0] = ASE_PACKET_TYPE(0x50, globalData->commandCounter, socket);
globalData->commandCounter++;
globalData->commandCounter %= 4;
cmd[1] = 0x44;
cmd[2] = 0x0;
cmd[3] = cmd[0] ^ cmd[1] ^ cmd[2];
retVal = writeToReader(globalData, cmd, 4, &actual);
if (checkValidity(retVal, 4, actual, "sendControlCommand - Error! in command write.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
retryTimes = 5;
}
/* read packet header and verify it's ok */
retVal = readResponse(globalData, socket, 1, (char*)(&oneByte), &actual, cwt);
if (checkValidity(retVal, 1, actual, "sendCloseResponseCommand - Error! in packet header read.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
retryTimes--;
}
if (retryTimes == 0) {
#ifdef ASE_DEBUG
syslog(LOG_INFO, "sendCloseResponseCommand - Error! retryTimes is 0.\n");
#endif
return ASE_ERROR_RESEND_COMMAND;
}
cs = oneByte;
/* set the timeout for the length */
cwt = 100000; // 0.1 sec
/* check if a status byte appears at the end of the response */
if (oneByte == ASE_LONG_RESPONSE_WITH_STATUS_PID || oneByte == ASE_RESPONSE_WITH_STATUS_PID)
withStatus = 1;
/* this is a response header, so read its length */
if (oneByte == ASE_LONG_RESPONSE_PID || oneByte == ASE_LONG_RESPONSE_WITH_STATUS_PID) {
/* long response */
retVal = readResponse(globalData, socket, 1, (char*)(&oneByte), &actual, cwt);
if (checkValidity(retVal, 1, actual, "sendCloseResponseCommand - Error! in packet header read.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
cs ^= oneByte;
readLen = (oneByte << 8);
readLen &= 0xFF00;
retVal = readResponse(globalData, socket, 1, (char*)(&oneByte), &actual, cwt);
if (checkValidity(retVal, 1, actual, "sendCloseResponseCommand - Error! in packet header read.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
cs ^= oneByte;
readLen |= (oneByte & 0xFF);
}
else {
/* short response */
retVal = readResponse(globalData, socket, 1, (char*)(&oneByte), &actual, cwt);
if (checkValidity(retVal, 1, actual, "sendCloseResponseCommand - Error! in packet header read.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
cs ^= oneByte;
readLen = oneByte;
}
/* set the timeout for the data */
cwt = 100000 * (readLen + 1); // 0.1 sec per byte
/***!!!!!!!!!!!!!!!!!!!!***************************************/
/* read data + checksum */
retVal = readResponse(globalData, socket, readLen + 1, outBuf, outBufLen, cwt);
if (checkValidity(retVal, readLen + 1, *outBufLen, "sendCloseResponseCommand - Error! in data read.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
readcs = outBuf[*outBufLen - 1];
(*outBufLen)--;
for (i = 0 ; i < *outBufLen ; ++i)
cs ^= outBuf[i];
/* check the status byte provided */
if (withStatus) {
/* remove the last byte of outBuf and check the status */
(*outBufLen)--;
if (outBuf[*outBufLen] != ASE_ACK_PID) {
/* read the checksum */
/*retVal = readResponse(globalData, socket, 1, &oneByte, &actual, cwt);*/
/* return error according to the status byte */
cleanResponseBuffer(globalData);
return parseStatus(outBuf[*outBufLen]);
}
}
/* read the checksum */
/*
retVal = readResponse(globalData, socket, 1, &oneByte, &actual);
if (checkValidity(retVal, 1, actual, "sendCloseResponseCommand - Error! in checksum read.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
*/
if (cs != readcs) {
#ifdef ASE_DEBUG
syslog(LOG_INFO, "sendCloseResponseCommand - Error! invalid checksum.\n");
#endif
cleanResponseBuffer(globalData);
return ASE_ERROR_CHECKSUM;
}
return ASE_OK;
}
/*****************************************************************************
*
*****************************************************************************/
/* returns 0 on success, negative number otherwise
outBuf will contain the ACK/NAK byte sent as response */
int sendControlCommand (reader* globalData, char socket, char* cmd, int len,
char* outBuf, int* outBufLen, char ignoreEvents) {
int retVal, actual, retryTimes = 5;
unsigned long cwt = (globalData->cards[(int)socket].cwt > 0 ? globalData->cards[(int)socket].cwt : 1000);
#ifdef ASE_DEBUG
uchar oneByte;
#endif
/* send the command */
retVal = writeToReader(globalData, cmd, len, &actual);
if (checkValidity(retVal, len, actual, "sendControlCommand - Error! in command write.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
/* read the Ack control byte */
/* set the timeout for the packet header */
// cwt = MAX(cwt * 260, globalData->cards[(int)socket].bwt);
cwt = 3000000; // 3 sec
retVal = readResponse(globalData, socket, 1, outBuf, outBufLen, cwt);
if (checkValidity(retVal, 1, *outBufLen, "sendControlCommand - Error! in ack read.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
/* loop until we get an OK acknowledgement */
while (outBuf[0] != ASE_ACK_PID && retryTimes) {
/* check if it's an acknowledgment */
if (outBuf[0] & ASE_ACK_PID) {
if (parseStatus(outBuf[0]) != ASE_READER_EXTRA_WAITING_TIME) {
#ifdef ASE_DEBUG
syslog(LOG_INFO, "sendControlCommand - Error! Status 0x%x%x.\n", (oneByte & 0xF0) >> 4, oneByte & 0x0F);
#endif
cleanResponseBuffer(globalData);
return parseStatus(outBuf[0]);
}
else
retryTimes = 5;
}
/* check if it's an event */
else if (isEvent((uchar)(outBuf[0]))) {
parseEvent(globalData, socket, outBuf[0]);
retryTimes = 5;
}
/* this must be an error -> send a retry command */
else {
char cmd[4];
cmd[0] = ASE_PACKET_TYPE(0x50, globalData->commandCounter, socket);
globalData->commandCounter++;
globalData->commandCounter %= 4;
cmd[1] = 0x44;
cmd[2] = 0x0;
cmd[3] = cmd[0] ^ cmd[1] ^ cmd[2];
retVal = writeToReader(globalData, cmd, 4, &actual);
if (checkValidity(retVal, 4, actual, "sendControlCommand - Error! in command write.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
retryTimes = 5;
}
/* read packet header and verify it's ok */
retVal = readResponse(globalData, socket, 1, outBuf, outBufLen, cwt);
if (checkValidity(retVal, 1, *outBufLen, "sendControlCommand - Error! in ack read.\n")) {
cleanResponseBuffer(globalData);
return retVal;
}
retryTimes--;
}
return ASE_OK;
}
|