Leosac  0.8.0
Open Source Access Control
Device.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2017 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "hardware/Device.hpp"
21 #include "HardwareService.hpp"
23 #include <boost/uuid/uuid_generators.hpp>
25 
26 namespace Leosac
27 {
28 namespace Hardware
29 {
30 
33 {
34 }
35 
37  : id_(UUID::random_uuid())
38  , device_class_(device_class)
39  , enabled_(true)
40  , version_(0)
41 {
42 }
43 
44 uint64_t Device::odb_version() const
45 {
46  return version_;
47 }
48 
50 {
51  return id_;
52 }
53 
54 
55 const std::string &Device::name() const
56 {
57  return name_;
58 }
59 
60 void Device::name(const std::string &name)
61 {
62  name_ = name;
63 }
64 
66 {
67  return device_class_;
68 }
69 
71 {
72  device_class_ = d;
73 }
74 
75 bool Device::enabled() const
76 {
77  return enabled_;
78 }
79 
80 void Device::enabled(bool e)
81 {
82  enabled_ = e;
83 }
84 
85 void Device::validation_callback(odb::callback_event e, odb::database &) const
86 {
87  if (e == odb::callback_event::pre_persist ||
88  e == odb::callback_event::pre_update)
89  {
90  // Make sure name is unique.
91  auto hwd_service =
93  ASSERT_LOG(hwd_service, "No hardware service.");
94  auto d = hwd_service->find_device_by_name(name());
95 
96  if (d && d->id() != id())
97  throw ModelException("data/attributes/name", "Device name already used");
98  }
99 
100  if (e == odb::callback_event::post_persist ||
101  e == odb::callback_event::post_update)
102  {
103  if (name().empty())
104  {
105  throw ModelException("data/attributes/name",
106  "Device name must be non empty");
107  }
108  }
109 }
110 }
111 }
Leosac::Hardware::HardwareService
Database aware Hardware Service.
Definition: HardwareService.hpp:39
Leosac::Hardware::DeviceClass
DeviceClass
An enumeration describing the class of the device.
Definition: HardwareFwd.hpp:42
Leosac::Hardware::Device::name_
std::string name_
Name of an hardware device must be unique due to an implementation details.
Definition: Device.hpp:81
Leosac::Hardware::Device::id_
DeviceId id_
Definition: Device.hpp:71
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Hardware::Device::version_
uint64_t version_
Definition: Device.hpp:94
Leosac::Hardware::Device::validation_callback
virtual void validation_callback(odb::callback_event e, odb::database &) const
Definition: Device.cpp:85
Leosac::Hardware::Device
Base class for hardware devices.
Definition: Device.hpp:44
Device.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
GetServiceRegistry.hpp
Leosac::Hardware::Device::id
UUID id() const
Definition: Device.cpp:49
Leosac::Hardware::Device::enabled_
bool enabled_
Is this device supposed to be enabled?
Definition: Device.hpp:91
Leosac::Hardware::Device::device_class
DeviceClass device_class() const
Definition: Device.cpp:65
ModelException.hpp
Leosac::Hardware::Device::Device
Device()
Definition: Device.cpp:31
Leosac::Hardware::Device::name
const std::string & name() const
Definition: Device.cpp:55
ModelException
An exception class for general API error.
Definition: ModelException.hpp:33
HardwareService.hpp
Leosac::APIStatusCode::UNKNOWN
@ UNKNOWN
Unknown status.
Leosac::Hardware::Device::enabled
bool enabled() const
Definition: Device.cpp:75
Leosac::Hardware::Device::odb_version
uint64_t odb_version() const
Definition: Device.cpp:44
Leosac::ServiceRegistry::get_service
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
Definition: ServiceRegistry.hpp:290
Leosac::Hardware::Device::device_class_
DeviceClass device_class_
Definition: Device.hpp:83
Leosac::UUID
Thin wrapper around boost::uuids::uuid.
Definition: Uuid.hpp:35