| // Copyright (C) 2016 The Android Open Source Project |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package device |
| |
| import "net/url" |
| |
| // URL returns the address in URL form. |
| func (i *Information) URL() *url.URL { |
| u, _ := url.Parse(i.Address) |
| return u |
| } |
| |
| // ID returns a short identifier for the device that will be unique to the device and stable if possible. |
| func (i *Information) ID() string { |
| if i.Serial != "" { |
| return i.Serial |
| } |
| if i.Address != "" { |
| return i.Address |
| } |
| if i.Name != "" { |
| return i.Name |
| } |
| return i.Configuration.String() |
| } |
| |
| // SamePhysicalDevice returns true if the two information objects refer to the same physical device. |
| func SamePhysicalDevice(a, b *Information) bool { |
| if a == b { |
| // Same pointer must be same device |
| return true |
| } |
| if a == nil || b == nil { |
| // only one of them is nil no match |
| return false |
| } |
| // If the serial is set, treat it as an authoratitive comparison point |
| if a.Serial != "" && b.Serial != "" { |
| return a.Serial == b.Serial |
| } |
| // No serial, if the address is set and the same, it must be the same device |
| if a.Address != "" && a.Address == b.Address { |
| return true |
| } |
| // If the hardware is different, cannot be the same |
| if !SameHardware(a.Configuration.Hardware, b.Configuration.Hardware) { |
| return false |
| } |
| |
| // If we get here, and the devices have names, we trust the names |
| if a.Name != "" && b.Name != "" { |
| return a.Name == b.Name |
| } |
| |
| // If we get here, we have no strong information that the devices are either different |
| // or the same, assume different for safty |
| return false |
| } |