Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic bluetooth support #313

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions example/cpp03/bluetooth/Jamfile.v2
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# 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)
#

lib socket ; # SOLARIS
lib nsl ; # SOLARIS
lib ws2_32 ; # NT
lib mswsock ; # NT
lib ipv6 ; # HPUX
lib network ; # HAIKU

exe bluetooth_client
: bluetooth_client.cpp
/boost/system//boost_system
: <define>BOOST_ALL_NO_LIB=1
<threading>multi
<target-os>solaris:<library>socket
<target-os>solaris:<library>nsl
<target-os>windows:<define>_WIN32_WINNT=0x0501
<target-os>windows,<toolset>gcc:<library>ws2_32
<target-os>windows,<toolset>gcc:<library>mswsock
<target-os>windows,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS
<target-os>hpux,<toolset>gcc:<define>_XOPEN_SOURCE_EXTENDED
<target-os>hpux:<library>ipv6
<target-os>haiku:<library>network
;

exe bluetooth_server
: bluetooth_server.cpp
/boost/system//boost_system
: <define>BOOST_ALL_NO_LIB=1
<threading>multi
<target-os>solaris:<library>socket
<target-os>solaris:<library>nsl
<target-os>windows:<define>_WIN32_WINNT=0x0501
<target-os>windows,<toolset>gcc:<library>ws2_32
<target-os>windows,<toolset>gcc:<library>mswsock
<target-os>windows,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS
<target-os>hpux,<toolset>gcc:<define>_XOPEN_SOURCE_EXTENDED
<target-os>hpux:<library>ipv6
<target-os>haiku:<library>network
;
59 changes: 59 additions & 0 deletions example/cpp03/bluetooth/bluetooth_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// bluetooth_client.cpp
// ~~~~~~~~~~~~~~~~~
//
// 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)
//

#include <cstdio>
#include <iostream>
#include <boost/asio/bluetooth/stream_protocol.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>


#if defined(BOOST_ASIO_HAS_BLUETOOTH_SOCKETS)

using boost::asio::bluetooth::stream_protocol;

int main(int argc, char* argv[])
{
try
{
if (argc != 3)
{
std::cerr << "Usage: bluetooth_client <addr> <channel>\n";
return 1;
}

boost::asio::io_context io_context;

stream_protocol::endpoint ep(boost::asio::bluetooth::make_address(argv[1]), atoi(argv[2]));
stream_protocol::socket s(io_context);
s.connect(ep);
std::cout << "connect to " << s.remote_endpoint() << std::endl;

char buf[6] = "hello";
boost::asio::write(s, boost::asio::buffer(buf, 5));

int n = boost::asio::read(s, boost::asio::buffer(buf, 5));
buf[n] = '\0';
std::cout << "receive [" << buf << "]" << std::endl;

io_context.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}

return 0;
}

#else // defined(BOOST_ASIO_HAS_BLUETOOTH_SOCKETS)
# error Bluetooth sockets not available on this platform.
#endif // defined(BOOST_ASIO_HAS_BLUETOOTH_SOCKETS)
63 changes: 63 additions & 0 deletions example/cpp03/bluetooth/bluetooth_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// bluetooth_server.cpp
// ~~~~~~~~~~~~~~~~~
//
// 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)
//

#include <cstdio>
#include <iostream>
#include <boost/asio/bluetooth/stream_protocol.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>


#if defined(BOOST_ASIO_HAS_BLUETOOTH_SOCKETS)

using boost::asio::bluetooth::stream_protocol;

int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: bluetooth_server <channel>\n";
return 1;
}

boost::asio::io_context io_context;

stream_protocol::endpoint ep(boost::asio::bluetooth::address(), atoi(argv[1]));
stream_protocol::socket s(io_context);
stream_protocol::acceptor acceptor(io_context);
acceptor.open(ep.protocol());
acceptor.bind(ep);
acceptor.listen();
acceptor.accept(s);
std::cout << "connect to " << s.remote_endpoint() << std::endl;

char buf[6];
int n = boost::asio::read(s, boost::asio::buffer(buf, 5));
buf[n] = 0;

std::cout << "receive [" << buf << "]" << std::endl;
boost::asio::write(s, boost::asio::buffer(buf, n));

io_context.run();
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}

return 0;
}

#else // defined(BOOST_ASIO_HAS_BLUETOOTH_SOCKETS)
# error Bluetooth sockets not available on this platform.
#endif // defined(BOOST_ASIO_HAS_BLUETOOTH_SOCKETS)
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
Loading