pin_pad.cpp
3.01 KB
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
#include "stdafx.h"
#include "etradeclient/hardware/pin_pad.h"
#include "etradeclient/hardware/ex_reference/pin_pad/dcp3.h"
static const uint8_t PWD_LEN_MAX = 16;
// "dcp3" library's error code when get password.
static const int16_t GET_PWD_SUCCESS = 0x0;
static const int16_t GET_PWD_ERROR_CANCELLED = -0xa1;
static const int16_t GET_PWD_ERROR_TIMEOUT = -0xa2;
PINPad::~PINPad()
{
Disconnect();
}
bool PINPad::Connect() const
{
static const uint8_t RETRY_COUNT = 10;
uint8_t conn_count = 0;
do
{
if (DoConnect())
return true;
++conn_count;
} while (conn_count < RETRY_COUNT);
return false;
}
void PINPad::Disconnect() const
{
if ((int32_t)m_handle > 0)
P3_IC_ExitComm(m_handle);
}
bool PINPad::DisplayBuiltInText(const BuiltInText& text) const
{
return 0 == P3_IC_DispLcd(m_handle, static_cast<uint8_t>(text));
}
bool PINPad::PlayVoice(const BuiltInVoice& voice) const
{
return 0 == P3_IC_PlayVoice(m_handle, static_cast<uint8_t>(voice));
}
bool PINPad::SetPWDMaxLen(uint8_t len) const
{
const uint8_t kPWDLenMin = 1;
if (len < kPWDLenMin)
len = kPWDLenMin;
else if (len > PWD_LEN_MAX)
len = PWD_LEN_MAX;
return 0 == P3_IC_SetInPutKeyLength(m_handle, len);
}
bool PINPad::SwitchLCDLight(bool on_off) const
{
uint8_t flag = on_off ? 1 : 0;
return 0 == P3_IC_CtlBackLight(m_handle, flag);
}
PINPad::StatusCode PINPad::ReadUserInputPWD(const DispMode& disp_mode, const DispLine& disp_line, uint8_t timeout, std::string& pwd) const
{
return DoReadUserInputPWD(std::bind(P3_IC_GetInputKey, m_handle, static_cast<uint8_t>(disp_mode), static_cast<uint8_t>(disp_line),
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), timeout, pwd);
}
bool PINPad::ClearScreen() const
{
const uint8_t kTotalLineNum = 2;
return 0 == P3_IC_LcdClrScrn(m_handle, kTotalLineNum);
}
std::string PINPad::GetVersion() const
{
const int kVersionMaxLen = 200;
unsigned char version[kVersionMaxLen] = { '\0' };
P3_IC_ReadVer(m_handle, version);
return reinterpret_cast<char*>(version);
}
bool PINPad::DoConnect() const
{
const int16_t kCOMPortMin = 0, kCOMPortMax = 19, kUSBPort = 100;
const uint32_t kBaudRate = 19200;
do
{
// First try connceting with USB port.
m_handle = P3_IC_InitComm_Baud(kUSBPort, kBaudRate);
if ((int32_t)m_handle > 0)
return true; // Connection established.
for (uint8_t com_port = kCOMPortMin; com_port <= kCOMPortMax; ++com_port)
{
m_handle = P3_IC_InitComm_Baud(com_port, kBaudRate);
if ((int32_t)m_handle > 0)
return true; // Connection established.
}
} while (0);
return false;
}
PINPad::StatusCode PINPad::DoReadUserInputPWD(ReadUserInputPWDFn fn, uint8_t timeout, std::string& pwd) const
{
uint8_t PWD_len = 0;
unsigned char PWD[PWD_LEN_MAX] = { '\0' };
int16_t status = fn(timeout, &PWD_len, PWD);
switch (status)
{
case GET_PWD_SUCCESS:
pwd = reinterpret_cast<char*>(PWD);
return StatusCode::kOK;
case GET_PWD_ERROR_CANCELLED:
return StatusCode::kCancelled;
case GET_PWD_ERROR_TIMEOUT:
return StatusCode::kTimeout;
default:
return StatusCode::kUnknown;
}
}