1 //
2 // Copyright (C) 2014 The Android Open Source Project
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 "shill/device_claimer.h"
18 
19 #include "shill/control_interface.h"
20 #include "shill/device_info.h"
21 
22 using std::string;
23 
24 namespace shill {
25 
DeviceClaimer(const std::string & service_name,DeviceInfo * device_info,bool default_claimer)26 DeviceClaimer::DeviceClaimer(
27     const std::string& service_name,
28     DeviceInfo* device_info,
29     bool default_claimer)
30     : service_name_(service_name),
31       device_info_(device_info),
32       default_claimer_(default_claimer) {}
33 
~DeviceClaimer()34 DeviceClaimer::~DeviceClaimer() {
35   // Release claimed devices if there is any.
36   if (DevicesClaimed()) {
37     for (const auto& device : claimed_device_names_) {
38       device_info_->RemoveDeviceFromBlackList(device);
39     }
40     // Clear claimed device list.
41     claimed_device_names_.clear();
42   }
43 }
44 
Claim(const string & device_name,Error * error)45 bool DeviceClaimer::Claim(const string& device_name, Error* error) {
46   // Check if device is claimed already.
47   if (claimed_device_names_.find(device_name) != claimed_device_names_.end()) {
48     Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments,
49                           "Device " + device_name +
50                           " had already been claimed");
51     return false;
52   }
53 
54   // Add device to the black list.
55   device_info_->AddDeviceToBlackList(device_name);
56 
57   claimed_device_names_.insert(device_name);
58   released_device_names_.erase(device_name);
59   return true;
60 }
61 
Release(const std::string & device_name,Error * error)62 bool DeviceClaimer::Release(const std::string& device_name,
63                             Error* error) {
64   // Make sure this is a device that have been claimed.
65   if (claimed_device_names_.find(device_name) == claimed_device_names_.end()) {
66     Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments,
67                           "Device " + device_name +
68                           " have not been claimed");
69     return false;
70   }
71 
72   // Remove the device from the black list.
73   device_info_->RemoveDeviceFromBlackList(device_name);
74 
75   claimed_device_names_.erase(device_name);
76   released_device_names_.insert(device_name);
77   return true;
78 }
79 
DevicesClaimed()80 bool DeviceClaimer::DevicesClaimed() {
81   return !claimed_device_names_.empty();
82 }
83 
IsDeviceReleased(const string & device_name)84 bool DeviceClaimer::IsDeviceReleased(const string& device_name) {
85   return released_device_names_.find(device_name) !=
86       released_device_names_.end();
87 }
88 
89 }  // namespace shill
90