unique_id.h
Go to the documentation of this file.
00001 /* -*- mode: C++ -*- */
00002 /*********************************************************************
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (C) 2012 Jack O'Quin
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the author nor other contributors may be
00019 *     used to endorse or promote products derived from this software
00020 *     without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *********************************************************************/
00035 
00036 #ifndef _IMPL_UNIQUE_ID_H_
00037 #define _IMPL_UNIQUE_ID_H_ 1
00038 
00046 #include <ros/ros.h>
00047 
00048 #include <boost/uuid/uuid_generators.hpp>
00049 
00050 namespace unique_id
00051 {
00052 
00054   namespace impl
00055   {
00056     /* Instantiate boost string UUID generator. */
00057     static boost::uuids::string_generator genString;
00058 
00059     /* Instantiate boost random UUID generator. */
00060     static boost::uuids::random_generator genRandom;
00061 
00062     /* RFC 4122 namespace for URL identifiers. */
00063     static const std::string url_namespace =
00064       "6ba7b811-9dad-11d1-80b4-00c04fd430c8";
00065 
00066     /* RFC 4122 UUID for URL identifiers. */
00067     static const boost::uuids::uuid url_namespace_uuid =
00068       genString(url_namespace);
00069 
00070     /* Instantiate boost URL name UUID generator. */
00071     static boost::uuids::name_generator genURL(url_namespace_uuid);
00072 
00073     /* Generate RFC4122 Version 1 Time-Based UUID */
00074     static boost::uuids::uuid genTime(ros::Time uuid_time, uint64_t hw_addr)
00075     {
00076       // Get no. of 100 nanosecond intervals since 00:00:00 October 15, 1582
00077       // The RFC4122 standard only requires the least significant 60 bits for timestamp
00078 
00079       // No. of 100 ns intervals between 00:00:00 Oct 15, 1582 and 00:00:00 Jan 1, 1970
00080       uint64_t offset = 122192928000000000;
00081 
00082       uint64_t num_hundred_epoch = static_cast<uint64_t>(uuid_time.sec) / (1e-9 * 100) +
00083                                    static_cast<uint64_t>(uuid_time.nsec) / 100;
00084       // Note: Adding offset in the same line as the above statement may yield incorrect
00085       //       values on some systems
00086       uint64_t num_hundred_rfc = num_hundred_epoch + offset;
00087 
00088       uint32_t time_low = static_cast<uint32_t>(num_hundred_rfc);
00089       uint16_t time_mid = static_cast<uint16_t>(num_hundred_rfc >> 32);
00090       uint16_t version = 0x1000;
00091       uint16_t time_hi_and_version = static_cast<uint16_t>(num_hundred_rfc >> (32 + 16)) | version;
00092 
00093       // Generate Clock Sequence ID based on random number
00094       uint16_t clock_seq_and_reserved = (rand() % (1 << 14)) | 0x8000;
00095 
00096       boost::uuids::uuid uu = {
00097                                 static_cast<uint8_t>(time_low >> 24), static_cast<uint8_t>(time_low >> 16),
00098                                 static_cast<uint8_t>(time_low >> 8),  static_cast<uint8_t>(time_low),
00099                                 static_cast<uint8_t>(time_mid >> 8),  static_cast<uint8_t>(time_mid),
00100                                 static_cast<uint8_t>(time_hi_and_version >> 8),
00101                                 static_cast<uint8_t>(time_hi_and_version),
00102                                 static_cast<uint8_t>(clock_seq_and_reserved >> 8),
00103                                 static_cast<uint8_t>(clock_seq_and_reserved),
00104                                 static_cast<uint8_t>(hw_addr >> 40), static_cast<uint8_t>(hw_addr >> 32),
00105                                 static_cast<uint8_t>(hw_addr >> 24), static_cast<uint8_t>(hw_addr >> 16),
00106                                 static_cast<uint8_t>(hw_addr >> 8),  static_cast<uint8_t>(hw_addr)
00107                               };
00108       return uu;
00109     }
00110 
00111   } // end namespace impl
00112 
00113 } // end namespace unique_id
00114 
00115 #endif // _IMPL_UNIQUE_ID_H_


unique_id
Author(s): Jack O'Quin
autogenerated on Thu Jun 6 2019 20:57:45