You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScReader.cpp 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // Created by robin on 6/28/15.
  3. //
  4. #include <winscard.h>
  5. #include "ScReader.h"
  6. ScReader::~ScReader()
  7. {
  8. freeContext();
  9. }
  10. bool ScReader::establishContext()
  11. {
  12. if (!freeContext())
  13. return false;
  14. _context = new SCARDCONTEXT[1];
  15. auto res = SCardEstablishContext(SCARD_SCOPE_SYSTEM, nullptr, nullptr, _context);
  16. return res == SCARD_S_SUCCESS;
  17. }
  18. bool ScReader::freeContext()
  19. {
  20. if (!_context)
  21. return true;
  22. auto res = SCardReleaseContext(*_context);
  23. delete[] _context;
  24. _context = nullptr;
  25. return res == SCARD_S_SUCCESS;
  26. }
  27. bool ScReader::connect(DWORD shareMode, DWORD protocols, DWORD disposition)
  28. {
  29. if (!disconnect(disposition))
  30. return false;
  31. _card = new SCARDCONTEXT[1];
  32. _proto = new DWORD[1];
  33. auto res = SCardConnect(*_context, _name, shareMode, protocols, _card, _proto);
  34. return res == SCARD_S_SUCCESS;
  35. }
  36. bool ScReader::disconnect(DWORD disposition)
  37. {
  38. if (!_card)
  39. return true;
  40. auto res = SCardDisconnect(*_card, disposition);
  41. delete[] _proto;
  42. _proto = nullptr;
  43. delete[] _card;
  44. _card = nullptr;
  45. return res == SCARD_S_SUCCESS;
  46. }
  47. std::shared_ptr<ScResult> ScReader::transmit(const ScCommand& command, DWORD size)
  48. {
  49. auto data = command.getData();
  50. auto resBuffer = new BYTE[size];
  51. auto res = SCardTransmit(*_card, _sendPci, data.getData(), data.getSize(), nullptr, resBuffer, &size);
  52. if (res == SCARD_S_SUCCESS)
  53. return std::make_shared<ScResult>(ScByteArray(resBuffer, size));
  54. return std::make_shared<ScResult>(ScByteArray());
  55. }