1 /*!
2 * \copyright Copyright (c) 2016-2021 Governikus GmbH & Co. KG, Germany
3 */
4
5 #include "MsgHandler.h"
6
7 #include "states/StateEditAccessRights.h"
8 #include "states/StateEnterNewPacePin.h"
9 #include "states/StateEnterPacePassword.h"
10 #include "states/StateSelectReader.h"
11
12 #include <QJsonDocument>
13
14 using namespace governikus;
15
16 const MsgLevel MsgHandler::DEFAULT_MSG_LEVEL = MsgLevel::v1;
17
18 const MsgHandler MsgHandler::Void = MsgHandler();
19
20
getStateMsgType(const QString & pState,PacePasswordId pPasswordId)21 MsgType MsgHandler::getStateMsgType(const QString& pState, PacePasswordId pPasswordId)
22 {
23 if (AbstractState::isState<StateEnterPacePassword>(pState))
24 {
25 if (pPasswordId == PacePasswordId::PACE_PIN)
26 {
27 return MsgType::ENTER_PIN;
28 }
29 else if (pPasswordId == PacePasswordId::PACE_CAN)
30 {
31 return MsgType::ENTER_CAN;
32 }
33 else if (pPasswordId == PacePasswordId::PACE_PUK)
34 {
35 return MsgType::ENTER_PUK;
36 }
37 }
38 else if (AbstractState::isState<StateEnterNewPacePin>(pState))
39 {
40 return MsgType::ENTER_NEW_PIN;
41 }
42 else if (AbstractState::isState<StateEditAccessRights>(pState))
43 {
44 return MsgType::ACCESS_RIGHTS;
45 }
46 else if (AbstractState::isState<StateSelectReader>(pState))
47 {
48 return MsgType::INSERT_CARD;
49 }
50
51 // indicates "do not use this" otherwise it is an internal error!
52 return MsgType::INTERNAL_ERROR;
53 }
54
55
MsgHandler()56 MsgHandler::MsgHandler()
57 : MsgHandler(MsgType::INTERNAL_ERROR)
58 {
59 setVoid();
60 }
61
62
MsgHandler(MsgType pType)63 MsgHandler::MsgHandler(MsgType pType)
64 : mType(pType)
65 , mVoid(false)
66 , mJsonObject()
67 {
68 mJsonObject[QLatin1String("msg")] = getEnumName(mType);
69 }
70
71
MsgHandler(MsgType pType,const char * const pKey,const QString & pValue)72 MsgHandler::MsgHandler(MsgType pType, const char* const pKey, const QString& pValue)
73 : MsgHandler(pType)
74 {
75 setValue(pKey, pValue);
76 }
77
78
MsgHandler(MsgType pType,const char * const pKey,const QLatin1String pValue)79 MsgHandler::MsgHandler(MsgType pType, const char* const pKey, const QLatin1String pValue)
80 : MsgHandler(pType)
81 {
82 setValue(pKey, pValue);
83 }
84
85
toJson() const86 QByteArray MsgHandler::toJson() const
87 {
88 Q_ASSERT(mJsonObject[QLatin1String("msg")].isString());
89 return QJsonDocument(mJsonObject).toJson(QJsonDocument::Compact);
90 }
91
92
getOutput() const93 QByteArray MsgHandler::getOutput() const
94 {
95 if (isVoid())
96 {
97 return QByteArray();
98 }
99
100 return toJson();
101 }
102
103
isVoid() const104 bool MsgHandler::isVoid() const
105 {
106 return mVoid;
107 }
108
109
getType() const110 MsgType MsgHandler::getType() const
111 {
112 return mType;
113 }
114
115
setRequest(const QJsonObject & pRequest)116 void MsgHandler::setRequest(const QJsonObject& pRequest)
117 {
118 const QLatin1String requestName("request");
119
120 const auto& requestValue = pRequest[requestName];
121 if (!requestValue.isUndefined())
122 {
123 mJsonObject[requestName] = requestValue;
124 }
125 }
126
127
setValue(const char * const pKey,const QString & pValue)128 void MsgHandler::setValue(const char* const pKey, const QString& pValue)
129 {
130 setValue(QLatin1String(pKey), pValue);
131 }
132
133
setValue(const QLatin1String pKey,const QLatin1String pValue)134 void MsgHandler::setValue(const QLatin1String pKey, const QLatin1String pValue)
135 {
136 if (pValue.size())
137 {
138 mJsonObject[pKey] = pValue;
139 }
140 }
141
142
setValue(const char * const pKey,const QLatin1String pValue)143 void MsgHandler::setValue(const char* const pKey, const QLatin1String pValue)
144 {
145 setValue(QLatin1String(pKey), pValue);
146 }
147
148
setVoid(bool pVoid)149 void MsgHandler::setVoid(bool pVoid)
150 {
151 mVoid = pVoid;
152 }
153
154
setValue(const QLatin1String pKey,const QString & pValue)155 void MsgHandler::setValue(const QLatin1String pKey, const QString& pValue)
156 {
157 if (!pValue.isEmpty())
158 {
159 mJsonObject[pKey] = pValue;
160 }
161 }
162