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

Commit 0e465290 authored by Myles Watson's avatar Myles Watson
Browse files

PDL: Add Python support for Address, ClassOfDevice

Add a type_caster for each custom type to and from std::string.

Bug: 143568835
Test: Create packets in Python passing strings:
   $ python3.8
   >>> import bluetooth_packets_python3 as bp3
   >>> pview = bp3.PacketViewLittleEndian([14, 10, 1, 9, 16, 0, 1, 2, 3, 4, 5, 6])
   >>> event = bp3.hci_packets.EventPacketView(pview)
   >>> complete = bp3.hci_packets.CommandCompleteView(event)
   >>> read_complete = bp3.hci_packets.ReadBdAddrCompleteView(complete)
   >>> address = read_complete.GetBdAddr()
   >>> print(address)
   06:05:04:03:02:01
   >>> builder = bp3.hci_packets.ReadBdAddrCompleteBuilder(1, bp3.hci_packets.ErrorCode.SUCCESS, address)
   >>> builder.Serialize()
   [14, 10, 1, 9, 16, 0, 1, 2, 3, 4, 5, 6]
   >>> quit()
Change-Id: If0a1639a4c1a162b68e1d0a0bb1cd5ba552a1190
parent 73ec92e9
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