Skip to content

Commit

Permalink
Bind SDK ChipError and ErrorStr (#9117)
Browse files Browse the repository at this point in the history
* Bind SDK ChipError and ErrorStr

* Update PR comments.
  • Loading branch information
Josh V [Apple] authored and pull[bot] committed Aug 20, 2021
1 parent 05f043d commit 8372436
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 202 deletions.
35 changes: 4 additions & 31 deletions src/pybindings/pycontroller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ shared_library("CHIPController") {
}

sources = [
"${root_gen_dir}/include/pybindings/pycontroller/CHIPErrorToExceptionBindings.cpp",
"${root_gen_dir}/include/pybindings/pycontroller/CHIPErrorToExceptionBindings.h",
"ControllerBindings/PyChip_ChipError.cpp",
"ControllerBindings/PyChip_ErrorStr.cpp",
"ControllerBindings/PyChip_Main.cpp",
]

public_deps = [
":build_exceptions",
"${chip_root}/src/app",
"${chip_root}/src/controller/data_model",
"${chip_root}/src/lib",
Expand All @@ -82,37 +81,14 @@ shared_library("CHIPController") {
libs = [ "python3.9" ]
}
}
pw_python_action("build_exceptions") {
script = "create_error_wrapper.py"

header_file = "${root_gen_dir}/include/pybindings/pycontroller/CHIPErrorToExceptionBindings.h"
cpp_file = "${root_gen_dir}/include/pybindings/pycontroller/CHIPErrorToExceptionBindings.cpp"
inputs = [
"${chip_root}/src/lib/core/CHIPError.h",
"${chip_root}/src/lib/core/CHIPConfig.h",
]
outputs = [
header_file,
cpp_file,
]

args = [
"--output_cpp_file=" + rebase_path(cpp_file, root_build_dir),
"--output_header_file=" + rebase_path(header_file, root_build_dir),
"--error_header=" +
rebase_path("${chip_root}/src/lib/core/CHIPError.h", root_build_dir),
"--config_header=" +
rebase_path("${chip_root}/src/lib/core/CHIPConfig.h", root_build_dir),
]
}

pw_python_action("pycontroller") {
script = "build-chip-wheel.py"

_py_manifest_files = [
{
src_dir = "."
sources = []
sources = [ "pychip/__init__.py" ]
},
{
src_dir = target_out_dir
Expand All @@ -126,10 +102,7 @@ pw_python_action("pycontroller") {

_py_manifest_file = "${target_gen_dir}/${target_name}.py_manifest.json"

inputs = [
"${root_gen_dir}/include/pybindings/pycontroller/CHIPErrorToExceptionBindings.cpp",
"${root_gen_dir}/include/pybindings/pycontroller/CHIPErrorToExceptionBindings.h",
]
inputs = []
_py_manifest_files_rebased = []
foreach(_manifest_entry, _py_manifest_files) {
inputs += _manifest_entry.sources
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* 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.
*/
#include <core/CHIPError.h>
#include <sstream>
#include <support/TypeTraits.h>

#include <functional>
#include <pybind11/pybind11.h>
#include <string>

#ifndef BINDER_PYBIND11_TYPE_CASTER
#define BINDER_PYBIND11_TYPE_CASTER
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)
PYBIND11_DECLARE_HOLDER_TYPE(T, T *)
PYBIND11_MAKE_OPAQUE(std::shared_ptr<void>)
#endif

void bind_PyChip_ChipError(std::function<pybind11::module &(std::string const & namespace_)> & M)
{
{
pybind11::class_<chip::ChipError, std::shared_ptr<chip::ChipError>> cl(
M("chip"), "ChipError",
"This is a helper class for managing `CHIP_ERROR` numbers.\n\n At the top level, an error belongs to a `Range` and has "
"an integral Value whose meaning depends on the `Range`.\n One, `Range::kSDK`, is used for the CHIP SDK's own errors; "
"others encapsulate error codes from external sources\n (e.g. libraries, OS) into a `CHIP_ERROR`.\n\n CHIP SDK errors "
"inside `Range::kSDK` consist of a component identifier given by `SdkPart` and an arbitrary small\n integer Code.");
cl.def(pybind11::init([]() { return new chip::ChipError(); }));
cl.def(pybind11::init<unsigned int>(), pybind11::arg("error"));

cl.def(pybind11::init([](chip::ChipError const & o) { return new chip::ChipError(o); }));

pybind11::enum_<chip::ChipError::Range>(cl, "Range", "Top-level error classification.")
.value("kSDK", chip::ChipError::Range::kSDK)
.value("kOS", chip::ChipError::Range::kOS)
.value("kPOSIX", chip::ChipError::Range::kPOSIX)
.value("kLwIP", chip::ChipError::Range::kLwIP)
.value("kOpenThread", chip::ChipError::Range::kOpenThread)
.value("kPlatform", chip::ChipError::Range::kPlatform);

pybind11::enum_<chip::ChipError::SdkPart>(cl, "SdkPart", "Secondary classification of errors in `Range::kSDK`.")
.value("kCore", chip::ChipError::SdkPart::kCore)
.value("kInet", chip::ChipError::SdkPart::kInet)
.value("kDevice", chip::ChipError::SdkPart::kDevice)
.value("kASN1", chip::ChipError::SdkPart::kASN1)
.value("kBLE", chip::ChipError::SdkPart::kBLE)
.value("kApplication", chip::ChipError::SdkPart::kApplication);

cl.def("__eq__", (bool (chip::ChipError::*)(const class chip::ChipError &) const) & chip::ChipError::operator==,
"C++: chip::ChipError::operator==(const class chip::ChipError &) const --> bool", pybind11::arg("other"));
cl.def("__ne__", (bool (chip::ChipError::*)(const class chip::ChipError &) const) & chip::ChipError::operator!=,
"C++: chip::ChipError::operator!=(const class chip::ChipError &) const --> bool", pybind11::arg("other"));
cl.def_static("IsSuccess", (bool (*)(unsigned int)) & chip::ChipError::IsSuccess,
"C++: chip::ChipError::IsSuccess(unsigned int) --> bool", pybind11::arg("error"));
cl.def_static("IsSuccess", (bool (*)(class chip::ChipError)) & chip::ChipError::IsSuccess,
"C++: chip::ChipError::IsSuccess(class chip::ChipError) --> bool", pybind11::arg("error"));
}
M("chip").def("RegisterCHIPLayerErrorFormatter", (void (*)()) & chip::RegisterCHIPLayerErrorFormatter,
"C++: chip::RegisterCHIPLayerErrorFormatter() --> void");

M("chip").def("FormatCHIPError", (bool (*)(char *, unsigned short, unsigned int)) & chip::FormatCHIPError,
"C++: chip::FormatCHIPError(char *, unsigned short, unsigned int) --> bool", pybind11::arg("buf"),
pybind11::arg("bufSize"), pybind11::arg("err"));
}
53 changes: 53 additions & 0 deletions src/pybindings/pycontroller/ControllerBindings/PyChip_ErrorStr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* 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.
*/
#include <core/CHIPError.h>
#include <sstream>
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>

#include <functional>
#include <pybind11/pybind11.h>
#include <string>

#ifndef BINDER_PYBIND11_TYPE_CASTER
#define BINDER_PYBIND11_TYPE_CASTER
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)
PYBIND11_DECLARE_HOLDER_TYPE(T, T *)
PYBIND11_MAKE_OPAQUE(std::shared_ptr<void>)
#endif

void bind_PyChip_ErrorStr(std::function<pybind11::module &(std::string const & namespace_)> & M)
{
{
pybind11::class_<chip::ErrorFormatter, std::shared_ptr<chip::ErrorFormatter>> cl(M("chip"), "ErrorFormatter", "");
cl.def(pybind11::init([]() { return new chip::ErrorFormatter(); }));
}
M("chip").def("ErrorStr", (const char * (*) (CHIP_ERROR)) & chip::ErrorStr,
"C++: chip::ErrorStr(unsigned int) --> const char *", pybind11::return_value_policy::automatic,
pybind11::arg("err"));

M("chip").def("RegisterErrorFormatter", (void (*)(struct chip::ErrorFormatter *)) & chip::RegisterErrorFormatter,
"C++: chip::RegisterErrorFormatter(struct chip::ErrorFormatter *) --> void", pybind11::arg("errFormatter"));

M("chip").def("DeregisterErrorFormatter", (void (*)(struct chip::ErrorFormatter *)) & chip::DeregisterErrorFormatter,
"C++: chip::DeregisterErrorFormatter(struct chip::ErrorFormatter *) --> void", pybind11::arg("errFormatter"));

M("chip").def("FormatError", (void (*)(char *, unsigned short, const char *, unsigned int, const char *)) & chip::FormatError,
"C++: chip::FormatError(char *, unsigned short, const char *, unsigned int, const char *) --> void",
pybind11::arg("buf"), pybind11::arg("bufSize"), pybind11::arg("subsys"), pybind11::arg("err"),
pybind11::arg("desc"));
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

typedef std::function<pybind11::module &(std::string const &)> ModuleGetter;

void bind_CHIPController_ChipExceptions(std::function<pybind11::module &(std::string const & namespace_)> & M);
void bind_PyChip_ErrorStr(std::function<pybind11::module &(std::string const & namespace_)> & M);
void bind_PyChip_ChipError(std::function<pybind11::module &(std::string const & namespace_)> & M);

PYBIND11_MODULE(PyChip, root_module)
{
Expand All @@ -40,10 +41,11 @@ PYBIND11_MODULE(PyChip, root_module)

modules[""] = root_module;

std::vector<std::pair<std::string, std::string>> sub_modules{ { "", "ChipExceptions" } };
std::vector<std::pair<std::string, std::string>> sub_modules{ { "", "chip" } };
for (auto & p : sub_modules)
modules[p.first.size() ? p.first + "::" + p.second : p.second] =
modules[p.first].def_submodule(p.second.c_str(), ("Bindings for " + p.first + "::" + p.second + " namespace").c_str());

bind_CHIPController_ChipExceptions(M);
bind_PyChip_ErrorStr(M);
bind_PyChip_ChipError(M);
}
168 changes: 0 additions & 168 deletions src/pybindings/pycontroller/create_error_wrapper.py

This file was deleted.

1 change: 1 addition & 0 deletions src/pybindings/pycontroller/pychip/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .PyChip import chip

0 comments on commit 8372436

Please sign in to comment.