|
| 1 | +// Copyright 2020 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "org_ros2_rcljava_graph_EndpointInfo.h" |
| 16 | + |
| 17 | +#include <jni.h> |
| 18 | +#include <limits> |
| 19 | + |
| 20 | +#include "rcl/graph.h" |
| 21 | +#include "rcljava_common/exceptions.hpp" |
| 22 | + |
| 23 | +using rcljava_common::exceptions::rcljava_throw_exception; |
| 24 | + |
| 25 | +JNIEXPORT void JNICALL |
| 26 | +Java_org_ros2_rcljava_graph_EndpointInfo_nativeFromRCL(JNIEnv * env, jobject self, jlong handle) |
| 27 | +{ |
| 28 | + auto * p = reinterpret_cast<rcl_topic_endpoint_info_t *>(handle); |
| 29 | + if (!p) { |
| 30 | + rcljava_throw_exception( |
| 31 | + env, "java/lang/IllegalArgumentException", "passed rcl endpoint info handle is NULL"); |
| 32 | + return; |
| 33 | + } |
| 34 | + const char * endpoint_type_enum_path = |
| 35 | + "Lorg/ros2/rcljava/graph/EndpointInfo$EndpointType;"; |
| 36 | + // TODO(ivanpauno): class and field lookup could be done at startup time |
| 37 | + jclass endpoint_type_clazz = env->FindClass("org/ros2/rcljava/graph/EndpointInfo$EndpointType"); |
| 38 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 39 | + jclass clazz = env->GetObjectClass(self); |
| 40 | + jfieldID node_name_fid = env->GetFieldID(clazz, "nodeName", "Ljava/lang/String;"); |
| 41 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 42 | + jfieldID node_namespace_fid = env->GetFieldID(clazz, "nodeNamespace", "Ljava/lang/String;"); |
| 43 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 44 | + jfieldID topic_type_fid = env->GetFieldID(clazz, "topicType", "Ljava/lang/String;"); |
| 45 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 46 | + jfieldID endpoint_type_fid = env->GetFieldID(clazz, "endpointType", endpoint_type_enum_path); |
| 47 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 48 | + jfieldID endpoint_gid_fid = env->GetFieldID(clazz, "endpointGID", "[B"); |
| 49 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 50 | + jfieldID qos_fid = env->GetFieldID(clazz, "qos", "Lorg/ros2/rcljava/qos/QoSProfile;"); |
| 51 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 52 | + |
| 53 | + jstring jnode_name = env->NewStringUTF(p->node_name); |
| 54 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 55 | + env->SetObjectField(self, node_name_fid, jnode_name); |
| 56 | + jstring jnode_namespace = env->NewStringUTF(p->node_namespace); |
| 57 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 58 | + env->SetObjectField(self, node_namespace_fid, jnode_namespace); |
| 59 | + jstring jtopic_type = env->NewStringUTF(p->topic_type); |
| 60 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 61 | + env->SetObjectField(self, topic_type_fid, jtopic_type); |
| 62 | + jfieldID enum_value_fid; |
| 63 | + switch (p->endpoint_type) { |
| 64 | + case RMW_ENDPOINT_INVALID: |
| 65 | + enum_value_fid = env->GetStaticFieldID( |
| 66 | + endpoint_type_clazz, "INVALID", endpoint_type_enum_path); |
| 67 | + break; |
| 68 | + case RMW_ENDPOINT_PUBLISHER: |
| 69 | + enum_value_fid = env->GetStaticFieldID( |
| 70 | + endpoint_type_clazz, "PUBLISHER", endpoint_type_enum_path); |
| 71 | + break; |
| 72 | + case RMW_ENDPOINT_SUBSCRIPTION: |
| 73 | + enum_value_fid = env->GetStaticFieldID( |
| 74 | + endpoint_type_clazz, "SUBSCRIPTION", endpoint_type_enum_path); |
| 75 | + break; |
| 76 | + default: |
| 77 | + rcljava_throw_exception( |
| 78 | + env, "java/lang/IllegalArgumentException", "unknown endpoint type"); |
| 79 | + break; |
| 80 | + } |
| 81 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 82 | + jobject enum_value = env->GetStaticObjectField(endpoint_type_clazz, enum_value_fid); |
| 83 | + env->SetObjectField(self, endpoint_type_fid, enum_value); |
| 84 | + jbyteArray jgid = env->NewByteArray(RMW_GID_STORAGE_SIZE); |
| 85 | + if (jgid == NULL) { |
| 86 | + rcljava_throw_exception( |
| 87 | + env, "java/lang/OutOfMemoryError", "cannot allocate java gid byte array"); |
| 88 | + return; |
| 89 | + } |
| 90 | + jbyte * gid_content = env->GetByteArrayElements(jgid, nullptr); |
| 91 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 92 | + for (size_t i = 0; i < RMW_GID_STORAGE_SIZE; ++i) { |
| 93 | + gid_content[i] = p->endpoint_gid[i]; |
| 94 | + } |
| 95 | + env->ReleaseByteArrayElements(jgid, gid_content, 0); |
| 96 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 97 | + env->SetObjectField(self, endpoint_gid_fid, jgid); |
| 98 | + jclass qos_clazz = env->FindClass("org/ros2/rcljava/qos/QoSProfile"); |
| 99 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 100 | + jmethodID qos_init_mid = env->GetMethodID(qos_clazz, "<init>", "()V"); |
| 101 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 102 | + jobject jqos = env->NewObject(qos_clazz, qos_init_mid); |
| 103 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 104 | + jmethodID qos_from_rcl_mid = env->GetMethodID(qos_clazz, "nativeFromRCL", "(J)V"); |
| 105 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 106 | + env->CallObjectMethod(jqos, qos_from_rcl_mid, &p->qos_profile); |
| 107 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env); |
| 108 | + env->SetObjectField(self, qos_fid, jqos); |
| 109 | +} |
0 commit comments