~nitrokey/nitrokey/release

« back to all changes in this revision

Viewing changes to src/device.cpp

  • Committer: GitHub
  • Author(s): szszszsz
  • Date: 2016-09-27 08:54:14 UTC
  • mfrom: (326.1.3)
  • Revision ID: git-v1:7516ce879517d7ea304238184f601efa63b9c969
Merge pull request #153 from Nitrokey/143-windows_change_pin

Ask for device's reinsertion when its locked

Show diffs side-by-side

added added

removed removed

Lines of Context:
785
785
int Device::getStatus() {
786
786
  int res;
787
787
 
788
 
  uint8_t data[1];
789
 
 
790
788
  if (isConnected) {
791
 
    Command cmd(CMD_GET_STATUS, data, 0);
792
 
 
 
789
    Command cmd(CMD_GET_STATUS, Q_NULLPTR, 0);
793
790
    res = sendCommand(&cmd);
794
791
 
795
792
    if (res == -1) {
796
 
      return -1;
 
793
      return -1; //sending error
797
794
    } else { // sending the command was successful
798
795
      Sleep::msleep(100);
799
796
      Response resp;
800
 
 
801
797
      resp.getResponse(this);
802
798
 
803
799
      if (cmd.crc == resp.lastCommandCRC) {
805
801
        memcpy(cardSerial, resp.data + 2, 4);
806
802
        memcpy(generalConfig, resp.data + 6, 3);
807
803
        memcpy(otpPasswordConfig, resp.data + 9, 2);
 
804
      } else {
 
805
          const int maxTries = 5;
 
806
          const int maxTriesToReconnection = 2*maxTries;
 
807
          static int invalidCRCCounter = 0;
 
808
          QString text;
 
809
          text = QString(__FUNCTION__) + QString(": CRC other than expected %1/%2\n").arg(invalidCRCCounter).arg(maxTries);
 
810
          DebugAppendTextGui(text.toLatin1().data());
 
811
          if (++invalidCRCCounter%maxTries == 0){
 
812
              if(invalidCRCCounter>maxTriesToReconnection){
 
813
                  invalidCRCCounter = 0;
 
814
                  return -11; // fatal error, cannot resume communication, ask user for reinsertion
 
815
              }
 
816
              text = QString(__FUNCTION__)+ ": Reconnecting device\n";
 
817
              DebugAppendTextGui(text.toLatin1().data());
 
818
              this->disconnect();
 
819
              return -10; // problems with communication, received CRC other than expected, try to reinitialize
 
820
          }
808
821
      }
809
822
    }
810
 
    return 0;
 
823
    return 0; //OK
811
824
  }
812
 
  return -2;
 
825
  return -2; // device not connected
813
826
}
814
827
 
815
828
/*******************************************************************************