update the latest source
[platform/core/connectivity/smartcard-service.git] / common / include / APDUHelper.h
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 #ifndef APDUHELPER_H_
19 #define APDUHELPER_H_
20
21 /* standard library header */
22
23 /* SLP library header */
24
25 /* local header */
26 #include "ByteArray.h"
27
28 namespace smartcard_service_api
29 {
30         class ResponseHelper
31         {
32         private:
33                 ByteArray response;
34                 unsigned char sw[2];
35                 int status;
36                 ByteArray dataField;
37
38                 static int parseStatusWord(unsigned char *sw);
39         public:
40                 ResponseHelper();
41                 ResponseHelper(const ByteArray &response);
42                 ~ResponseHelper();
43
44                 bool setResponse(const ByteArray &response);
45                 int getStatus();
46 //              char *getErrorString();
47                 ByteArray getDataField();
48
49                 static int getStatus(const ByteArray &response);
50                 static ByteArray getDataField(const ByteArray &response);
51 //              static char *getErrorString();
52         };
53
54         class APDUCommand
55         {
56         private:
57                 typedef struct _command_header_t
58                 {
59                         unsigned char cla;
60                         unsigned char ins;
61                         unsigned char param[2];
62                 } command_header_t;
63
64                 command_header_t header;
65                 ByteArray commandData;
66                 unsigned int maxResponseSize;
67                 bool isExtendedLength;
68
69         public:
70                 static const unsigned char INS_DEACTIVATE_FILE = (unsigned char)0x04;
71                 static const unsigned char INS_TERMINAL_PROFILE = (unsigned char)0x10;
72                 static const unsigned char INS_TERMINAL_RESPONSE = (unsigned char)0x14;
73                 static const unsigned char INS_VERIFY = (unsigned char)0x20;
74                 static const unsigned char INS_CHANGE_PIN = (unsigned char)0x24;
75                 static const unsigned char INS_DISABLE_PIN = (unsigned char)0x26;
76                 static const unsigned char INS_ENABLE_PIN = (unsigned char)0x28;
77                 static const unsigned char INS_UNBLOCK_PIN = (unsigned char)0x2C;
78                 static const unsigned char INS_INCREASE = (unsigned char)0x32;
79                 static const unsigned char INS_ACTIVATE_FILE = (unsigned char)0x44;
80                 static const unsigned char INS_GET_CHALLENGE = (unsigned char)0x84;
81                 static const unsigned char INS_AUTHENTICATE = (unsigned char)0x88;
82                 static const unsigned char INS_AUTHENTICATE2 = (unsigned char)0x89;
83                 static const unsigned char INS_MANAGE_CHANNEL = (unsigned char)0x70;
84                 static const unsigned char INS_SEARCH_RECORD = (unsigned char)0xA2;
85                 static const unsigned char INS_SELECT_FILE = (unsigned char)0xA4;
86                 static const unsigned char INS_TERMINAL_CAPABILITY = (unsigned char)0xAA;
87                 static const unsigned char INS_READ_BINARY = (unsigned char)0xB0;
88                 static const unsigned char INS_READ_RECORD = (unsigned char)0xB2;
89                 static const unsigned char INS_GET_RESPONSE = (unsigned char)0xC0;
90                 static const unsigned char INS_RETRIEVE_DATA = (unsigned char)0xCB;
91                 static const unsigned char INS_WRITE_BINARY = (unsigned char)0xD0;
92                 static const unsigned char INS_UPDATE_BINARY = (unsigned char)0xD6;
93                 static const unsigned char INS_UPDATE_RECORD = (unsigned char)0xDC;
94                 static const unsigned char INS_SET_DATA = (unsigned char)0xDB;
95                 static const unsigned char INS_CREATE_FILE = (unsigned char)0xE0;
96                 static const unsigned char INS_APPEND_RECORD = (unsigned char)0xE2;
97                 static const unsigned char INS_DELETE_FILE = (unsigned char)0xE4;
98                 static const unsigned char INS_STATUS = (unsigned char)0xF2;
99 //              static const unsigned char INS_ = (unsigned char)0x;
100
101                 static const unsigned char P1_SELECT_BY_ID = (unsigned char)0x00;
102                 static const unsigned char P1_SELECT_PARENT_DF = (unsigned char)0x03;
103                 static const unsigned char P1_SELECT_BY_DF_NAME = (unsigned char)0x04;
104                 static const unsigned char P1_SELECT_BY_PATH = (unsigned char)0x08;
105                 static const unsigned char P1_SELECT_BY_PATH_FROM_CURRENT_DF = (unsigned char)0x09;
106 //              static const unsigned char P1_ = (unsigned char)0x;
107
108                 static const unsigned char P2_SELECT_GET_FCP = (unsigned char)0x04;
109 //              static const unsigned char P2_ = (unsigned char)0x;
110
111                 APDUCommand();
112                 ~APDUCommand();
113
114                 bool setCommand(unsigned char cla, unsigned char ins, unsigned char p1, unsigned char p2, ByteArray commandData, unsigned int maxResponseSize);
115                 bool setCommand(const ByteArray &command);
116
117                 bool setChannel(int type, int channelNum);
118
119                 void setCLA(unsigned char cla);
120                 unsigned char getCLA();
121
122                 void setINS(unsigned char ins);
123                 unsigned char getINS();
124
125                 void setP1(unsigned char p1);
126                 unsigned char getP1();
127
128                 void setP2(unsigned char p2);
129                 unsigned char getP2();
130
131                 void setCommandData(const ByteArray &data);
132                 ByteArray getCommandData();
133
134                 void setMaxResponseSize(unsigned int maxResponseSize);
135                 unsigned int setMaxResponseSize();
136
137                 bool getBuffer(ByteArray &array);
138         };
139
140         class APDUHelper
141         {
142         public:
143                 static const int COMMAND_OPEN_LOGICAL_CHANNEL = 1;
144                 static const int COMMAND_CLOSE_LOGICAL_CHANNEL = 2;
145                 static const int COMMAND_SELECT_BY_ID = 3;
146                 static const int COMMAND_SELECT_PARENT_DF = 4;
147                 static const int COMMAND_SELECT_BY_DF_NAME = 5;
148                 static const int COMMAND_SELECT_BY_PATH = 6;
149                 static const int COMMAND_SELECT_BY_PATH_FROM_CURRENT_DF = 7;
150                 static const int COMMAND_READ_BINARY = 8;
151                 static const int COMMAND_READ_RECORD = 9;
152                 static const int COMMAND_WRITE_BINARY = 10;
153                 static const int COMMAND_WRITE_RECORD = 11;
154
155                 static ByteArray generateAPDU(int command, int channel, ByteArray data);
156         };
157
158 } /* namespace smartcard_service_api */
159 #endif /* APDUHELPER_H_ */