Skip to content

Commit

Permalink
Fix linter errors (#30)
Browse files Browse the repository at this point in the history
Signed-off-by: Gonzo de Pedro <[email protected]>
  • Loading branch information
gonzodepedro authored Jan 2, 2024
1 parent 7a664a2 commit d0a26a9
Show file tree
Hide file tree
Showing 19 changed files with 241 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ else()
"${rosidl_adapter_proto_TEMPLATE_DIR}")

include("${rosidl_adapter_proto_DIR}/rosidl_adapt_proto_interfaces.cmake")

add_compile_definitions("ROSIDL_ADAPTER_PROTO_BUILDING_DLL__${PROJECT_NAME}")

# There is no clean way to include additional files into protobuf headers
if(NOT WIN32)
add_compile_options("-include${rosidl_adapter_proto_VISIBILITY_CONTROL_HEADER}")
else()
add_compile_options( "/FI\"${rosidl_adapter_proto_VISIBILITY_CONTROL_HEADER}\"")
add_compile_options("/FI\"${rosidl_adapter_proto_VISIBILITY_CONTROL_HEADER}\"")
endif()

foreach(_pkg_name ${rosidl_generate_interfaces_DEPENDENCY_PACKAGE_NAMES})
set(_proto_dir "${${_pkg_name}_DIR}/../../../include/${_pkg_name}/${_pkg_name}/rosidl_adapter_proto__visibility_control.h")
normalize_path(_proto_dir "${_proto_dir}")
if(NOT WIN32)
add_compile_options("-include${_proto_dir}")
else()
add_compile_options( "/FI\"${_proto_dir}\"")
add_compile_options("/FI\"${_proto_dir}\"")
endif()
endforeach()

Expand Down
2 changes: 1 addition & 1 deletion rosidl_adapter_proto/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<exec_depend>python3-empy</exec_depend>
<exec_depend>rosidl_cli</exec_depend>
<exec_depend>rosidl_parser</exec_depend>

<member_of_group>rosidl_interface_packages</member_of_group>

<export>
Expand Down
49 changes: 30 additions & 19 deletions rosidl_adapter_proto/rosidl_adapter_proto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
# limitations under the License.
#
# ================================= Apache 2.0 =================================
from rosidl_cmake import generate_files

import rosidl_parser.definition as rosidl

import subprocess
import zlib

from rosidl_cmake import generate_files
import rosidl_parser.definition as rosidl

# A postfix for the protobuf package name / the c++ namespace
PROTO_PACKAGE_POSTFIX = "pb"
PROTO_PACKAGE_POSTFIX = 'pb'

# The rpc-function name for service calls. As ros services can only offer a
# single function, this function gets a static name in the protobuf service
PROTO_SERVICE_CALL_NAME = "Call"
PROTO_SERVICE_CALL_NAME = 'Call'

# The rpc function name for sending an action goal
PROTO_ACTION_SEND_GOAL_CALL_NAME = "SendGoal"
PROTO_ACTION_SEND_GOAL_CALL_NAME = 'SendGoal'

# The rpc function name for retrieving the action result
PROTO_ACTION_GET_RESULT_CALL_NAME = "GetResult"
PROTO_ACTION_GET_RESULT_CALL_NAME = 'GetResult'

# A Mapping from IDL -> Protobuf type
MSG_TYPE_TO_PROTO = {
Expand All @@ -58,25 +58,33 @@

field_val = 0


def compute_proto_field_number(variable_name):
# Field number rules (https://developers.google.com/protocol-buffers/docs/proto#assigning_field_numbers)
# Field number rules (https://developers.google.com/protocol-buffers/docs/
# proto#assigning_field_numbers)
#
# Smallest: 1
# Largest: 536870911 (= 2^29 - 1)
#
# Reserved Range: 19000 to 19999 (=> 1000 values)

field_number = zlib.crc32(bytearray(variable_name, 'utf8')) # Create a 32 bit hash from the variable name
field_number = (field_number % (536870911 - 1000)) # Reduce to the correct amount of values
field_number += 1 # Account for the fact that we must not use 0
# Create a 32 bit hash from the variable name
field_number = zlib.crc32(bytearray(variable_name, 'utf8'))
# Reduce to the correct amount of values
field_number = (field_number % (536870911 - 1000))
# Account for the fact that we must not use 0
field_number += 1
# Account for the fact that we must not use 19000 to 19999
if field_number >= 19000:
field_number += 1000 # Account for the fact that we must not use 19000 to 19999
field_number += 1000

return field_number


def to_proto_import(namespaced_type):
assert isinstance(namespaced_type, rosidl.NamespacedType)
return "/".join(namespaced_type.namespaces + [namespaced_type.name]) + ".proto"
return '/'.join(namespaced_type.namespaces + [namespaced_type.name]) + '.proto'


def collect_proto_imports(rosidl_message):
assert isinstance(rosidl_message, rosidl.Message)
Expand All @@ -90,25 +98,28 @@ def collect_proto_imports(rosidl_message):
namespaced_type = member.type.value_type
else:
continue

import_set.add(to_proto_import(namespaced_type))

return import_set


def generate_proto(generator_arguments_file):
mapping = {
'idl.proto.em': '%s.proto',
}
generate_files(generator_arguments_file, mapping, keep_case=True)
return 0


def compile_proto(protoc_path, proto_path_list, cpp_out_dir, proto_files, package_name):
protoc_cmd = [protoc_path]

for path in proto_path_list:
protoc_cmd.append("--proto_path=" + path)

protoc_cmd.append(f"--cpp_out=dllexport_decl=ROSIDL_ADAPTER_PROTO_PUBLIC__{package_name}:{cpp_out_dir}")
protoc_cmd.append('--proto_path=' + path)

protoc_cmd.append(
f'--cpp_out=dllexport_decl=ROSIDL_ADAPTER_PROTO_PUBLIC__{package_name}:{cpp_out_dir}')
protoc_cmd = protoc_cmd + proto_files

subprocess.check_call(protoc_cmd)
subprocess.check_call(protoc_cmd)
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/* ================================ Apache 2.0 =================================
*
* Copyright (C) 2021 Continental
*
*
* 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.
*
*
* ================================ Apache 2.0 =================================
*/

Expand All @@ -29,11 +29,9 @@ typedef struct message_type_support_t
const char * message_namespace;
const char * message_name;

bool (* serialize)(const void * ros_message, std::string& serialized_message);
bool (* deserialize)(void * ros_message, const void *serialized_data, size_t size);
std::string (*get_descriptor)();

bool (* serialize)(const void * ros_message, std::string & serialized_message);
bool (* deserialize)(void * ros_message, const void * serialized_data, size_t size);
std::string (* get_descriptor)();
} message_type_support_t;

}

} // namespace rosidl_typesupport_protobuf
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/* ================================ Apache 2.0 =================================
*
* Copyright (C) 2021 Continental
*
*
* 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.
*
*
* ================================ Apache 2.0 =================================
*/

#pragma once

#include <rosidl_typesupport_protobuf/visibility_control.h>

//rosidl_message_type_support_t
// rosidl_message_type_support_t
#include "rosidl_typesupport_protobuf/rosidl_generator_c_pkg_adapter.hpp"

namespace rosidl_typesupport_protobuf
Expand All @@ -32,4 +32,4 @@ template<typename T>
ROSIDL_TYPESUPPORT_PROTOBUF_PUBLIC
const rosidl_message_type_support_t * get_message_type_support_handle();

} // namespace rosidl_typesupport_protobuf_cpp
} // namespace rosidl_typesupport_protobuf
Original file line number Diff line number Diff line change
@@ -1,101 +1,108 @@
/* ================================ Apache 2.0 =================================
*
* Copyright (C) 2021 Continental
*
* 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.
*
* ================================ Apache 2.0 =================================
*/
#pragma once

#include <string>
#include <algorithm>
#include <string>
#include <vector>

// protobuf includes
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4100 4127 4146 4800) // disable proto warnings
#pragma warning(disable: 4100 4127 4146 4800) // disable proto warnings
#endif
#include <google/protobuf/descriptor.pb.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif


inline bool HasFile(const google::protobuf::FileDescriptorSet& fset_, const std::string& fname_)
inline bool HasFile(const google::protobuf::FileDescriptorSet & fset_, const std::string & fname_)
{
for (auto findex = 0; findex < fset_.file_size(); ++findex)
{
if (fset_.file(findex).name() == fname_)
{
return(true);
for (auto findex = 0; findex < fset_.file_size(); ++findex) {
if (fset_.file(findex).name() == fname_) {
return true;
}
}
return(false);
return false;
}

inline bool GetFileDescriptor(const google::protobuf::Descriptor* desc_, google::protobuf::FileDescriptorSet& fset_)
inline bool GetFileDescriptor(
const google::protobuf::Descriptor * desc_,
google::protobuf::FileDescriptorSet & fset_)
{

if (desc_ == nullptr) return(false);
const google::protobuf::FileDescriptor* fdesc = desc_->file();

for (auto dep = 0; dep < fdesc->dependency_count(); ++dep)
{
if (desc_ == nullptr) {return false;}
const google::protobuf::FileDescriptor * fdesc = desc_->file();

for (auto dep = 0; dep < fdesc->dependency_count(); ++dep) {
// iterate containing messages
const google::protobuf::FileDescriptor* sfdesc = fdesc->dependency(dep);

for (auto mtype = 0; mtype < sfdesc->message_type_count(); ++mtype)
{
const google::protobuf::Descriptor* desc = sfdesc->message_type(mtype);
const google::protobuf::FileDescriptor * sfdesc = fdesc->dependency(dep);

for (auto mtype = 0; mtype < sfdesc->message_type_count(); ++mtype) {
const google::protobuf::Descriptor * desc = sfdesc->message_type(mtype);
GetFileDescriptor(desc, fset_);
}

// containing enums ?
if (sfdesc->enum_type_count() > 0)
{
const google::protobuf::EnumDescriptor* edesc = sfdesc->enum_type(0);
const google::protobuf::FileDescriptor* efdesc = edesc->file();
if (sfdesc->enum_type_count() > 0) {
const google::protobuf::EnumDescriptor * edesc = sfdesc->enum_type(0);
const google::protobuf::FileDescriptor * efdesc = edesc->file();

if (!HasFile(fset_, efdesc->name()))
{
google::protobuf::FileDescriptorProto* epdesc = fset_.add_file();
if (!HasFile(fset_, efdesc->name())) {
google::protobuf::FileDescriptorProto * epdesc = fset_.add_file();
efdesc->CopyTo(epdesc);
}
}


// containing services ?
if (sfdesc->service_count() > 0)
{
const google::protobuf::ServiceDescriptor* svdesc = sfdesc->service(0);
const google::protobuf::FileDescriptor* svfdesc = svdesc->file();
if (sfdesc->service_count() > 0) {
const google::protobuf::ServiceDescriptor * svdesc = sfdesc->service(0);
const google::protobuf::FileDescriptor * svfdesc = svdesc->file();

if (!HasFile(fset_, svfdesc->name()))
{
google::protobuf::FileDescriptorProto* svpdesc = fset_.add_file();
if (!HasFile(fset_, svfdesc->name())) {
google::protobuf::FileDescriptorProto * svpdesc = fset_.add_file();
svfdesc->CopyTo(svpdesc);
}
}
}

if (HasFile(fset_, fdesc->name())) return(true);
if (HasFile(fset_, fdesc->name())) {return true;}

google::protobuf::FileDescriptorProto* pdesc = fset_.add_file();
google::protobuf::FileDescriptorProto * pdesc = fset_.add_file();
fdesc->CopyTo(pdesc);
for (auto field = 0; field < desc_->field_count(); ++field)
{
const google::protobuf::FieldDescriptor* fddesc = desc_->field(field);
const google::protobuf::Descriptor* desc = fddesc->message_type();
for (auto field = 0; field < desc_->field_count(); ++field) {
const google::protobuf::FieldDescriptor * fddesc = desc_->field(field);
const google::protobuf::Descriptor * desc = fddesc->message_type();
GetFileDescriptor(desc, fset_);
}

return(true);
return true;
}

template<typename T>
inline std::string GetProtoMessageDescription()
{
const google::protobuf::Descriptor* desc = T::descriptor();
const google::protobuf::Descriptor * desc = T::descriptor();
google::protobuf::FileDescriptorSet pset;
if (GetFileDescriptor(desc, pset))
{
if (GetFileDescriptor(desc, pset)) {
std::string desc_s = pset.SerializeAsString();
return(desc_s);
return desc_s;
}
return("");
}
return "";
}
Loading

0 comments on commit d0a26a9

Please sign in to comment.