Use dbus for event notification
[platform/core/security/ode.git] / server / event.cpp
1 /*
2  *  Copyright (c) 2015 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 #include "event.h"
18 #include "logger.h"
19
20 #include <klay/dbus/signal.h>
21 #include <klay/dbus/introspection.h>
22 #include <klay/exception.h>
23
24 const std::string SIGNAL_OBJECT_PATH = "/org/tizen/OnDeviceEncryption";
25 const std::string SIGNAL_EVENT_INTERFACE = "org.tizen.OnDeviceEncryption.Event";
26
27 const std::string manifest =
28     "<node>"
29     "   <interface name='" + SIGNAL_EVENT_INTERFACE + "'>"
30     "       <signal name='" + SIGNAL_OBJECT_PATH + "'>"
31     "           <arg type='s' name='argument'/>"
32     "       </signal>"
33     "   </interface>"
34     "</node>";
35
36 void EventNotifier::init(void) noexcept
37 {
38         try {
39                 dbus::Connection& conn = dbus::Connection::getSystem();
40                 conn.registerObject(SIGNAL_OBJECT_PATH, manifest, nullptr, nullptr);
41                 WARN(SINK, "Success to init event-notifier.");
42         } catch(runtime::Exception& e) {
43                 ERROR(SINK, e.what());
44         }
45 }
46
47 void EventNotifier::emit(const std::string& name, const std::string& state) noexcept
48 {
49         try {
50                 dbus::signal::Sender sender(SIGNAL_OBJECT_PATH, SIGNAL_EVENT_INTERFACE);
51                 sender.emit(name, "(s)", state.c_str());
52                 WARN(SINK, "Event '" << name << "' has notified with state '" << state << "'");
53         } catch(runtime::Exception& e) {
54                 ERROR(SINK, e.what() << ", name: " << name << ", state: " << state);
55         }
56 }