2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20 * @brief Sample service implementation.
22 #include <service-thread.h>
23 #include <generic-socket-manager.h>
24 #include <connection-info.h>
25 #include <message-buffer.h>
26 #include <protocols.h>
28 #include <dpl/serialization.h>
29 #include <dpl/log/log.h>
31 #include <ckm-service.h>
32 #include <ckm-logic.h>
35 const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
36 const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
37 } // namespace anonymous
41 CKMService::CKMService()
42 : m_logic(new CKMLogic)
45 CKMService::~CKMService() {
49 GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
51 return ServiceDescriptionVector {
52 {SERVICE_SOCKET_CKM_CONTROL, "ckm::api-control", SOCKET_ID_CONTROL},
53 {SERVICE_SOCKET_CKM_STORAGE, "ckm::api-storage", SOCKET_ID_STORAGE}
57 void CKMService::accept(const AcceptEvent &event) {
58 LogDebug("Accept event");
59 auto &info = m_connectionInfoMap[event.connectionID.counter];
60 info.interfaceID = event.interfaceID;
63 void CKMService::write(const WriteEvent &event) {
64 LogDebug("Write event (" << event.size << " bytes)");
67 void CKMService::process(const ReadEvent &event) {
68 LogDebug("Read event");
69 auto &info = m_connectionInfoMap[event.connectionID.counter];
70 info.buffer.Push(event.rawBuffer);
71 while(processOne(event.connectionID, info.buffer, info.interfaceID));
74 bool CKMService::processOne(
75 const ConnectionID &conn,
76 MessageBuffer &buffer,
77 InterfaceID interfaceID)
79 LogDebug ("process One");
86 if (interfaceID == SOCKET_ID_CONTROL)
87 response = processControl(buffer);
89 response = processStorage(conn, buffer);
91 m_serviceManager->Write(conn, response);
94 } Catch (MessageBuffer::Exception::Base) {
95 LogError("Broken protocol. Closing socket.");
97 LogError("Unknown exception. Closing socket.");
100 m_serviceManager->Close(conn);
104 RawBuffer CKMService::processControl(MessageBuffer &buffer) {
108 RawBuffer newPass, oldPass;
110 Deserialization::Deserialize(buffer, command);
111 Deserialization::Deserialize(buffer, user);
113 cc = static_cast<ControlCommand>(command);
116 case ControlCommand::UNLOCK_USER_KEY:
117 Deserialization::Deserialize(buffer, newPass);
118 return m_logic->unlockUserKey(user, newPass);
119 case ControlCommand::LOCK_USER_KEY:
120 return m_logic->lockUserKey(user);
121 case ControlCommand::REMOVE_USER_DATA:
122 return m_logic->removeUserData(user);
123 case ControlCommand::CHANGE_USER_PASSWORD:
124 Deserialization::Deserialize(buffer, oldPass);
125 Deserialization::Deserialize(buffer, newPass);
126 return m_logic->changeUserPassword(user, oldPass, newPass);
127 case ControlCommand::RESET_USER_PASSWORD:
128 Deserialization::Deserialize(buffer, newPass);
129 return m_logic->resetUserPassword(user, newPass);
132 throw 1; // broken protocol
136 RawBuffer CKMService::processStorage(const ConnectionID &conn, MessageBuffer &buffer){
143 void CKMService::close(const CloseEvent &event) {
144 LogDebug("Close event");
145 m_connectionInfoMap.erase(event.connectionID.counter);