1c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#ifndef DEVICE_BLUETOOTH_BLUETOOTH_UUID_H_
6c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#define DEVICE_BLUETOOTH_BLUETOOTH_UUID_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace device {
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Opaque wrapper around a Bluetooth UUID. Instances of UUID represent the
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 128-bit universally unique identifiers (UUIDs) of profiles and attributes
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// used in Bluetooth based communication, such as a peripheral's services,
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// characteristics, and characteristic descriptors. An instance are
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// constructed using a string representing 16, 32, or 128 bit UUID formats.
17c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass BluetoothUUID {
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Possible representation formats used during construction.
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  enum Format {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    kFormatInvalid,
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    kFormat16Bit,
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    kFormat32Bit,
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    kFormat128Bit
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Single argument constructor. |uuid| can be a 16, 32, or 128 bit UUID
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // represented as a 4, 8, or 36 character string with the following
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // formats:
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   xxxx
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   0xxxxx
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   xxxxxxxx
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   0xxxxxxxxx
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // 16 and 32 bit UUIDs will be internally converted to a 128 bit UUID using
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the base UUID defined in the Bluetooth specification, hence custom UUIDs
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // should be provided in the 128-bit format. If |uuid| is in an unsupported
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // format, the result might be invalid. Use IsValid to check for validity
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // after construction.
41c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  explicit BluetoothUUID(const std::string& uuid);
42c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
43c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Default constructor does nothing. Since BluetoothUUID is copyable, this
44c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // constructor is useful for initializing member variables and assigning a
45c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // value to them later. The default constructor will initialize an invalid
46c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // UUID by definition and the string accessors will return an empty string.
47c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  BluetoothUUID();
48c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual ~BluetoothUUID();
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns true, if the UUID is in a valid canonical format.
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool IsValid() const;
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns the representation format of the UUID. This reflects the format
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // that was provided during construction.
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Format format() const { return format_; }
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns the value of the UUID as a string. The representation format is
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // based on what was passed in during construction. For the supported sizes,
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // this representation can have the following formats:
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   - 16 bit:  xxxx
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   - 32 bit:  xxxxxxxx
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // where x is a lowercase hex digit.
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::string& value() const { return value_; }
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns the underlying 128-bit value as a string in the following format:
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // where x is a lowercase hex digit.
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const std::string& canonical_value() const { return canonical_value_; }
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Permit sufficient comparison to allow a UUID to be used as a key in a
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // std::map.
73c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool operator<(const BluetoothUUID& uuid) const;
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Equality operators.
76c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool operator==(const BluetoothUUID& uuid) const;
77c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool operator!=(const BluetoothUUID& uuid) const;
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // String representation of the UUID that was used during construction. For
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the supported sizes, this representation can have the following formats:
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   - 16 bit:  xxxx
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   - 32 bit:  xxxxxxxx
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  //   - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Format format_;
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string value_;
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The 128-bit string representation of the UUID.
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string canonical_value_;
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
92a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// This is required by gtest to print a readable output on test failures.
93a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochvoid PrintTo(const BluetoothUUID& uuid, std::ostream* out);
94a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace device
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
97c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#endif  // DEVICE_BLUETOOTH_BLUETOOTH_UUID_H_
98