Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b4f5e640 authored by Myles Watson's avatar Myles Watson Committed by android-build-merger
Browse files

Merge "PDL: Add Python support for Address, ClassOfDevice"

am: 01b13d1b

Change-Id: Ibae791f984d805442229cacc649defc36d7d0ae2
parents b1113c64 01b13d1b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -637,6 +637,8 @@ cc_library_host_shared{
    "packet/python3_module.cc",
    "l2cap/fcs.cc",
    ":BluetoothPacketSources",
    "hci/address.cc",
    "hci/class_of_device.cc",
  ],
  generated_headers: [
    "BluetoothGeneratedPackets_h",
+2 −0
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@
#include <sstream>
#include <vector>

#include "os/log.h"

namespace bluetooth {
namespace hci {

+55 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2019 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.
 *
 ******************************************************************************/

#pragma once

#include <cstring>
#include <string>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "hci/address.h"

namespace py = pybind11;

namespace pybind11 {
namespace detail {
template <>
struct type_caster<::bluetooth::hci::Address> {
 public:
  // Set the Python name of Address and declare "value"
  PYBIND11_TYPE_CASTER(::bluetooth::hci::Address, _("Address"));

  // Convert from Python->C++
  bool load(handle src, bool) {
    PyObject* source = src.ptr();
    if (py::isinstance<py::str>(src)) {
      bool conversion_successful = bluetooth::hci::Address::FromString(PyUnicode_AsUTF8(source), value);
      return conversion_successful && !PyErr_Occurred();
    }
    return false;
  }

  // Convert from C++->Python
  static handle cast(bluetooth::hci::Address src, return_value_policy, handle) {
    return PyUnicode_FromString(src.ToString().c_str());
  }
};
}  // namespace detail
}  // namespace pybind11
+55 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2019 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.
 *
 ******************************************************************************/

#pragma once

#include <cstring>
#include <string>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "hci/class_of_device.h"

namespace py = pybind11;

namespace pybind11 {
namespace detail {
template <>
struct type_caster<::bluetooth::hci::ClassOfDevice> {
 public:
  // Set the Python name of ClassOfDevice and declare "value"
  PYBIND11_TYPE_CASTER(::bluetooth::hci::ClassOfDevice, _("ClassOfDevice"));

  // Convert from Python->C++
  bool load(handle src, bool) {
    PyObject* source = src.ptr();
    if (py::isinstance<py::str>(src)) {
      bool conversion_successful = bluetooth::hci::ClassOfDevice::FromString(PyUnicode_AsUTF8(source), value);
      return conversion_successful && !PyErr_Occurred();
    }
    return false;
  }

  // Convert from C++->Python
  static handle cast(bluetooth::hci::ClassOfDevice src, return_value_policy, handle) {
    return PyUnicode_FromString(src.ToString().c_str());
  }
};
}  // namespace detail
}  // namespace pybind11
+4 −0
Original line number Diff line number Diff line
@@ -43,6 +43,10 @@ void CustomFieldDef::GenInclude(std::ostream& s) const {
  s << "#include \"" << include_ << util::CamelCaseToUnderScore(GetTypeName()) << ".h\"\n";
}

void CustomFieldDef::GenPyBind11Include(std::ostream& s) const {
  s << "#include \"" << include_ << util::CamelCaseToUnderScore(GetTypeName()) << "_pybind11_type_caster.h\"\n";
}

void CustomFieldDef::GenUsing(std::ostream& s) const {
  s << "using ::bluetooth::";
  for (const auto& c : include_) {
Loading