Skip to content

Commit

Permalink
[boost] Add basic bluetooth support
Browse files Browse the repository at this point in the history
  • Loading branch information
huangqinjin committed Aug 4, 2020
1 parent b87450d commit e67ef48
Show file tree
Hide file tree
Showing 8 changed files with 858 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}-targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(DIRECTORY include/${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(DIRECTORY include/${PROJECT_NAME} include/boost DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(EXPORT ${PROJECT_NAME}-targets DESTINATION ${ConfigPackageLocation} NAMESPACE ${PROJECT_NAME}::)

Expand Down
165 changes: 165 additions & 0 deletions include/boost/asio/bluetooth/address.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//
// bluetooth/address.hpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2019 Huang Qinjin (huangqinjin at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef BOOST_ASIO_BLUETOOTH_ADDRESS_HPP
#define BOOST_ASIO_BLUETOOTH_ADDRESS_HPP

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

#include <boost/asio/detail/config.hpp>
#include <string>
#include <boost/asio/detail/string_view.hpp>
#include <boost/system/error_code.hpp>

#if !defined(BOOST_ASIO_NO_IOSTREAM)
# include <iosfwd>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)

#include <boost/asio/detail/push_options.hpp>

namespace boost {
namespace asio {
namespace bluetooth {

/**
* Implements Bluetooth addresses.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
class address
{
public:
/// Get the address as a string.
BOOST_ASIO_DECL std::string to_string() const;

/// Compare two addresses for equality.
BOOST_ASIO_DECL friend bool operator==(const address& a1,
const address& a2) BOOST_ASIO_NOEXCEPT;

/// Compare two addresses for inequality.
friend bool operator!=(const address& a1,
const address& a2) BOOST_ASIO_NOEXCEPT
{
return !(a1 == a2);
}

/// Compare addresses for ordering.
BOOST_ASIO_DECL friend bool operator<(const address& a1,
const address& a2) BOOST_ASIO_NOEXCEPT;

/// Compare addresses for ordering.
friend bool operator>(const address& a1,
const address& a2) BOOST_ASIO_NOEXCEPT
{
return a2 < a1;
}

/// Compare addresses for ordering.
friend bool operator<=(const address& a1,
const address& a2) BOOST_ASIO_NOEXCEPT
{
return !(a2 < a1);
}

/// Compare addresses for ordering.
friend bool operator>=(const address& a1,
const address& a2) BOOST_ASIO_NOEXCEPT
{
return !(a1 < a2);
}

public:
unsigned char data[6];
};

/// Create an address from an Bluetooth address string in colon-separated or hyphen-separated form.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(const char* str);

/// Create an address from an Bluetooth address string in colon-separated or hyphen-separated form.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(const char* str,
boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;

/// Create an address from an Bluetooth address string in colon-separated or hyphen-separated form.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(const std::string& str);

/// Create an address from an Bluetooth address string in colon-separated or hyphen-separated form.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(const std::string& str,
boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;

#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)

/// Create an address from an Bluetooth address string in colon-separated or hyphen-separated form.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(string_view str);

/// Create an address from an Bluetooth address string in colon-separated or hyphen-separated form.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(string_view str,
boost::system::error_code& ec) BOOST_ASIO_NOEXCEPT;

#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)

#if !defined(BOOST_ASIO_NO_IOSTREAM)

/// Output an address as a string.
/**
* Used to output a human-readable string for a specified address.
*
* @param os The output stream to which the string will be written.
*
* @param addr The address to be written.
*
* @return The output stream.
*
* @relates boost::asio::bluetooth::address
*/
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const address& addr)
{
os << addr.to_string();
return os;
}

#endif // !defined(BOOST_ASIO_NO_IOSTREAM)

} // namespace bluetooth
} // namespace asio
} // namespace boost

#include <boost/asio/detail/pop_options.hpp>

#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/bluetooth/detail/impl/address.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)

#endif // BOOST_ASIO_BLUETOOTH_ADDRESS_HPP
212 changes: 212 additions & 0 deletions include/boost/asio/bluetooth/basic_endpoint.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
//
// bluetooth/basic_endpoint.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2019 Huang Qinjin (huangqinjin at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef BOOST_ASIO_BLUETOOTH_BASIC_ENDPOINT_HPP
#define BOOST_ASIO_BLUETOOTH_BASIC_ENDPOINT_HPP

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)

#include <boost/asio/detail/config.hpp>

#if defined(BOOST_ASIO_HAS_BLUETOOTH_SOCKETS) \
|| defined(GENERATING_DOCUMENTATION)

#include <boost/asio/bluetooth/detail/endpoint.hpp>

#if !defined(BOOST_ASIO_NO_IOSTREAM)
# include <iosfwd>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)

#include <boost/asio/detail/push_options.hpp>

namespace boost {
namespace asio {
namespace bluetooth {

/// Describes an endpoint for a Bluetooth socket.
/**
* The boost::asio::bluetooth::basic_endpoint class template describes an endpoint
* that may be associated with a particular Bluetooth socket.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*
* @par Concepts:
* Endpoint.
*/
template <typename Protocol>
class basic_endpoint
{
public:
/// The protocol type associated with the endpoint.
typedef Protocol protocol_type;

/// The type of the endpoint structure. This type is dependent on the
/// underlying implementation of the socket layer.
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined data_type;
#else
typedef boost::asio::detail::socket_addr_type data_type;
#endif

/// Default constructor.
basic_endpoint() BOOST_ASIO_NOEXCEPT
: impl_()
{
}

// Construct an endpoint using an address and channel number.
basic_endpoint(const boost::asio::bluetooth::address& addr,
unsigned short channel_num) BOOST_ASIO_NOEXCEPT
: impl_(addr, channel_num)
{
}

/// The protocol associated with the endpoint.
protocol_type protocol() const BOOST_ASIO_NOEXCEPT
{
return protocol_type();
}

/// Get the underlying endpoint in the native type.
data_type* data() BOOST_ASIO_NOEXCEPT
{
return impl_.data();
}

/// Get the underlying endpoint in the native type.
const data_type* data() const BOOST_ASIO_NOEXCEPT
{
return impl_.data();
}

/// Get the underlying size of the endpoint in the native type.
std::size_t size() const BOOST_ASIO_NOEXCEPT
{
return impl_.size();
}

/// Set the underlying size of the endpoint in the native type.
void resize(std::size_t new_size)
{
impl_.resize(new_size);
}

/// Get the capacity of the endpoint in the native type.
std::size_t capacity() const BOOST_ASIO_NOEXCEPT
{
return impl_.capacity();
}

// Get the channel associated with the endpoint.
unsigned short channel() const BOOST_ASIO_NOEXCEPT
{
return impl_.channel();
}

// Set the channel associated with the endpoint.
void channel(unsigned short channel_num) BOOST_ASIO_NOEXCEPT
{
return impl_.channel(channel_num);
}

// Get the Bluetooth address associated with the endpoint.
boost::asio::bluetooth::address address() const BOOST_ASIO_NOEXCEPT
{
return impl_.address();
}

// Set the Bluetooth address associated with the endpoint.
void address(const boost::asio::bluetooth::address& addr) BOOST_ASIO_NOEXCEPT
{
return impl_.address(addr);
}

/// Compare two endpoints for equality.
friend bool operator==(const basic_endpoint<Protocol>& e1,
const basic_endpoint<Protocol>& e2)
{
return e1.impl_ == e2.impl_;
}

/// Compare two endpoints for inequality.
friend bool operator!=(const basic_endpoint<Protocol>& e1,
const basic_endpoint<Protocol>& e2)
{
return !(e1.impl_ == e2.impl_);
}

/// Compare endpoints for ordering.
friend bool operator<(const basic_endpoint<Protocol>& e1,
const basic_endpoint<Protocol>& e2)
{
return e1.impl_ < e2.impl_;
}

/// Compare endpoints for ordering.
friend bool operator>(const basic_endpoint<Protocol>& e1,
const basic_endpoint<Protocol>& e2)
{
return e2.impl_ < e1.impl_;
}

/// Compare endpoints for ordering.
friend bool operator<=(const basic_endpoint<Protocol>& e1,
const basic_endpoint<Protocol>& e2)
{
return !(e2 < e1);
}

/// Compare endpoints for ordering.
friend bool operator>=(const basic_endpoint<Protocol>& e1,
const basic_endpoint<Protocol>& e2)
{
return !(e1 < e2);
}

private:
// The underlying Bluetooth endpoint.
boost::asio::bluetooth::detail::endpoint impl_;
};

/// Output an endpoint as a string.
/**
* Used to output a human-readable string for a specified endpoint.
*
* @param os The output stream to which the string will be written.
*
* @param endpoint The endpoint to be written.
*
* @return The output stream.
*
* @relates boost::asio::bluetooth::basic_endpoint
*/
template <typename Elem, typename Traits, typename Protocol>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os,
const basic_endpoint<Protocol>& endpoint)
{
os << boost::asio::bluetooth::detail::endpoint(endpoint.address(), endpoint.channel()).to_string();
return os;
}

} // namespace bluetooth
} // namespace asio
} // namespace boost

#include <boost/asio/detail/pop_options.hpp>

#endif // defined(BOOST_ASIO_HAS_BLUETOOTH_SOCKETS)
// || defined(GENERATING_DOCUMENTATION)

#endif // BOOST_ASIO_BLUETOOTH_BASIC_ENDPOINT_HPP
Loading

0 comments on commit e67ef48

Please sign in to comment.