callPN532Function method

List<int> callPN532Function(
  1. int command, {
  2. List<int> parameters = const [],
  3. int responseLength = 0,
  4. int timeout = pn532StandardTimeout,
})

Send the command and given parameters to the PN532 and only wait timeout ms for the PN532 to say it's ready.

Also a responseLength can be given which reads the length from the PN532 and return the response as an List<int>

Can throw todo

Implementation

List<int> callPN532Function(int command,
    {List<int> parameters = const [],
    int responseLength = 0,
    int timeout = pn532StandardTimeout}) {
  final List<int> commandList = [pn532HostToPn532, command, ...parameters];

  // write the command to the board
  try {
    writeCommand(commandList);
  } catch (e) {
    pn532ProtocolImpl.wakeUp();
    rethrow;
  }

  // Wait for chip to say its ready!
  pn532ProtocolImpl.waitReady(timeout: timeout);

  // read acknowledgement
  _readAck();

  // wait for a response or return if no response is expected
  if (responseLength == 0) {
    return [];
  }

  // wait for the response
  pn532ProtocolImpl.waitReady(timeout: timeout);

  final List<int> response = readResponse(responseLength + 2);

  // verify the response
  if (response[0] != pn532Pn532ToHost) {
    throw PN532NotToHostResponse();
  } else if (response[1] != command + 1) {
    throw PN532WrongResponseException(command: command, response: response);
  }

  // return the response without the pre verification bytes
  return response.sublist(2);
}