bool checkIsAck(InputStream* inputStream) { char ack = inputStream->readChar(inputStream); if (ack != COMMAND_ACK) { writeError(IO_READER_CHECK_CHAR_PROBLEM); OutputStream* outputStream = getErrorOutputStreamLogger(); appendString(outputStream, "\nCHK:ACK EXP:a \t but \t find:"); append(outputStream, ack); println(outputStream); return false; } return true; }
bool checkIsChar(InputStream* inputStream, char expectedChar) { char readChar = inputStream->readChar(inputStream); if (expectedChar != readChar) { writeError(IO_READER_CHECK_CHAR_PROBLEM); OutputStream* outputStream = getErrorOutputStreamLogger(); appendString(outputStream, "\nCHK:CHAR:EXP:"); append(outputStream, expectedChar); appendString(outputStream, " \t but \t find:"); append(outputStream, readChar); println(outputStream); return false; } return true; }
void bufferWriteChar(Buffer* buffer, char c) { int isFull = isBufferFull(buffer); if (!isFull) { char* sPointer = (char*) buffer->s; // Shift to the right cell index sPointer += buffer->writeIndex; *sPointer = c; buffer->writeIndex++; buffer->writeIndex %= buffer->length; } else { // We must log the problem writeError(IO_BUFFER_FULL); // Print Buffer printDebugBuffer(getErrorOutputStreamLogger(), buffer); } }
bool isEnd(void) { if (doNotEnd) { return false; } if (!matchStarted) { appendString(getErrorOutputStreamLogger(), "You must call startMatch before"); } bool result = currentTimeInSecond >= MATCH_DURATION; if (result) { // If END // TODO : Provide a callback in the Interface and not in the endMatchDetectorDevice ! armDriver2012Up(0); } return result; }
char readFilteredChar(InputStream* inputStream) { char b0 = inputStream->readChar(inputStream); char result; if (filterBinaryToValueChar(b0, &result)) { return result; } else { writeError(IO_READER_READ_FILTERED_CHAR); OutputStream* debugOutputStream = getErrorOutputStreamLogger(); appendString(debugOutputStream, "Char:"); append(debugOutputStream, b0); appendString(debugOutputStream, "Hex:"); appendHex2(debugOutputStream, b0); return FILTERED_RESULT; } }
void writeError(int errorCode) { OutputStream* outputStream = getErrorOutputStreamLogger(); appendString(outputStream, "ERR CODE:"); appendHex4(outputStream, errorCode); println(outputStream); }
void deviceSystemHandleRawData(char header, InputStream* inputStream, OutputStream* outputStream) { if (header == COMMAND_PING) { // data ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_PING); // Read and write in output the pingIndex (to control that it's the right which does the response) unsigned char pingIndex = readHex2(inputStream); appendHex2(outputStream, pingIndex); } // Last Error else if (header == COMMAND_GET_LAST_ERROR) { ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_GET_LAST_ERROR); unsigned int lastError = getLastError(); appendHex4(outputStream, lastError); } else if (header == COMMAND_CLEAR_LAST_ERROR) { ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_CLEAR_LAST_ERROR); clearLastError(); } // Device list else if (header == COMMAND_DEVICE_LIST) { ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_DEVICE_LIST); printDeviceList(getInfoOutputStreamLogger()); // Usage } else if (header == COMMAND_USAGE) { ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_USAGE); printDeviceListUsage(getInfoOutputStreamLogger(), false); } else if (header == COMMAND_USAGE_PROBLEM) { ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_USAGE_PROBLEM); printDeviceListUsage(getInfoOutputStreamLogger(), true); } else if (header == COMMAND_USAGE_SPECIFIC_DEVICE) { ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_USAGE_SPECIFIC_DEVICE); char deviceHeader = readBinaryChar(inputStream); int size = getDeviceCount(); int i; for (i = 0; i < size; i++) { Device* device = getDevice(i); if (deviceHeader == device->deviceInterface->deviceHeader) { println(getInfoOutputStreamLogger()); printDeviceUsage(getInfoOutputStreamLogger(), device, false); return; } } appendString(getErrorOutputStreamLogger(), "Device Not Found ! "); } else if (header == COMMAND_CLS) { ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_CLS); #ifdef PC_COMPILER system("cls"); #else appendString(outputStream, "Unsupported Operation"); #endif // PC_COMPILER } else if (header == COMMAND_RESET) { ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_RESET); #ifdef PC_COMPILER appendString(outputStream, "Unsupported Operation"); #else // goto 0; #endif // PC_COMPILER } // Notifications else if (header == COMMAND_NOTIFICATION) { ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_NOTIFICATION); printDeviceListNotification(getInfoOutputStreamLogger(), false); } else if (header == COMMAND_WAIT) { appendAck(outputStream); int mSec = readHex4(inputStream); delaymSec(mSec); append(outputStream, SYSTEM_DEVICE_HEADER); append(outputStream, COMMAND_WAIT); } else if (header == COMMAND_BOARD_NAME) { appendString(getInfoOutputStreamLogger(), getBoardName()); println(getInfoOutputStreamLogger()); ackCommand(outputStream, SYSTEM_DEVICE_HEADER, COMMAND_BOARD_NAME); } }
bool handleStreamInstruction(Buffer* inputBuffer, Buffer* outputBuffer, OutputStream* outputStream, filterCharFunction* inputFilterChar, filterCharFunction* outputFilterChar) { if (inputBuffer == NULL) { writeError(DRIVER_STREAM_LISTENER_INPUT_BUFFER_NULL); return false; } if (outputBuffer == NULL) { writeError(DRIVER_STREAM_LISTENER_OUTPUT_BUFFER_NULL); return false; } // Try to clear the buffer if needed ('z' char) if (clearBufferIfNeeded(inputBuffer)) { return false; } // We received data int inputBufferCount = getBufferElementsCount(inputBuffer); if (inputBufferCount > 0) { if (filterFirstNextChar(inputBuffer, inputFilterChar)) { return false; } // As there is clear of char filtering, we must reload the size of the buffer int bufferSize = getBufferElementsCount(inputBuffer); if (bufferSize < DEVICE_HEADER_LENGTH) { return false; } // Get the header unsigned char deviceHeader = bufferGetCharAtIndex(inputBuffer, DEVICE_HEADER_INDEX); // Manage the dispatcher specifier (3 chars : Ex j01 before real command ...) unsigned char specifyDispatcherLength = 0; if (deviceHeader == DATA_DISPATCHER_DEVICE_HEADER) { specifyDispatcherLength += DISPATCHER_COMMAND_AND_INDEX_HEADER_LENGTH; } // Check if enough data if (bufferSize < specifyDispatcherLength + DEVICE_AND_COMMAND_HEADER_LENGTH) { return false; } // Reload the deviceHeader to take the dispatcher specifier if any deviceHeader = bufferGetCharAtIndex(inputBuffer, specifyDispatcherLength + DEVICE_HEADER_INDEX); unsigned char commandHeader = bufferGetCharAtIndex(inputBuffer, specifyDispatcherLength + COMMAND_HEADER_INDEX); // find the device corresponding to this header. It must at least be declared in local or in remote ! unsigned char dataSize = bufferSize - specifyDispatcherLength; const Device* device = deviceDataDispatcherFindDevice(deviceHeader, commandHeader, dataSize, DEVICE_MODE_INPUT); // if the device was not found if (device == NULL) { return false; } // At this moment, device Interface is found DeviceInterface* deviceInterface = device->deviceInterface; // We must send device specifyDispatcherLength + Header + commandHeader + data => + 2 int dataToTransferCount = deviceInterface->deviceGetInterface(commandHeader, DEVICE_MODE_INPUT, false) + specifyDispatcherLength + DEVICE_AND_COMMAND_HEADER_LENGTH; if (bufferSize < dataToTransferCount) { return false; } // We must receive ack + device header + command header + data => + 3 int dataToReceiveCount = deviceInterface->deviceGetInterface(commandHeader, DEVICE_MODE_OUTPUT, false) + ACK_LENGTH + DEVICE_AND_COMMAND_HEADER_LENGTH; InputStream* bufferedInputStream = getInputStream(inputBuffer); OutputStream* bufferedOutputStream = getOutputStream(outputBuffer); TransmitMode transmitMode = device->transmitMode; // we handle locally the request if (specifyDispatcherLength == 0 && transmitMode == TRANSMIT_LOCAL) { // We need the implementation for local mode DeviceDescriptor* deviceDescriptor = device->descriptor; if (deviceDescriptor == NULL) { writeError(NO_DEVICE_DESC_FOUND_FOR); append(getErrorOutputStreamLogger(), deviceHeader); append(getErrorOutputStreamLogger(), commandHeader); return false; } // remove the first chars corresponding to the device header and command Header bufferClearLastChars(inputBuffer, DEVICE_AND_COMMAND_HEADER_LENGTH); // Call to the device deviceDescriptor->deviceHandleRawData(commandHeader, bufferedInputStream, bufferedOutputStream); } // we forward the request through Remote Operation with Dispatcher else if (specifyDispatcherLength > 0 || transmitMode == TRANSMIT_I2C || transmitMode == TRANSMIT_UART || transmitMode == TRANSMIT_ZIGBEE) { // Find dispatcher DriverDataDispatcher* dispatcher = NULL; if (specifyDispatcherLength > 0) { bufferClearLastChars(inputBuffer, DEVICE_HEADER_LENGTH); unsigned char dispatcherIndex = readHex2(bufferedInputStream); dispatcher = getDriverDataDispatcherByIndex(dispatcherIndex); if (dispatcher == NULL) { writeError(NO_DISPATCHER_FOUND); OutputStream* errorOutputStream = getErrorOutputStreamLogger(); appendStringAndDec(errorOutputStream, ", dispatcherIndex=", dispatcherIndex); return false; } } else { TransmitMode transmitMode = device->transmitMode; int address = device->address; dispatcher = getDriverDataDispatcherByTransmitMode(transmitMode, address); if (dispatcher == NULL) { writeError(NO_DISPATCHER_FOUND); OutputStream* errorOutputStream = getErrorOutputStreamLogger(); appendStringAndDec(errorOutputStream, ", transmitMode=", transmitMode); append(errorOutputStream, '('); appendString(errorOutputStream, getTransmitModeAsString(transmitMode)); append(errorOutputStream, ')'); appendStringAndDec(errorOutputStream, ", addr=", address); return false; } } // copy Driver buffer with remote Call dispatcher->driverDataDispatcherTransmitData(dispatcher, inputBuffer, outputBuffer, dataToTransferCount, dataToReceiveCount ); } // In All Cases (Local / I2C / UART / Zigbee ...) // Copy the data from bufferOutputStream to the outputStream if (outputStream != NULL) { copyInputToOutputStream(&(outputBuffer->inputStream), outputStream, outputFilterChar, dataToReceiveCount); } return true; } return false; }