|
| 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_events_publisher_statuses_OfferedQosIncompatible.h" |
| 16 | + |
| 17 | +#include <jni.h> |
| 18 | +#include <stdlib.h> |
| 19 | + |
| 20 | +#include "rmw/incompatible_qos_events_statuses.h" |
| 21 | +#include "rmw/types.h" |
| 22 | +#include "rcl/event.h" |
| 23 | +#include "rcljava_common/exceptions.hpp" |
| 24 | + |
| 25 | +using rcljava_common::exceptions::rcljava_throw_exception; |
| 26 | + |
| 27 | +JNIEXPORT jlong JNICALL |
| 28 | +Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nAllocateRCLStatusEvent( |
| 29 | + JNIEnv * env, jclass) |
| 30 | +{ |
| 31 | + void * p = malloc(sizeof(rmw_offered_qos_incompatible_event_status_t)); |
| 32 | + if (!p) { |
| 33 | + rcljava_throw_exception( |
| 34 | + env, "java/lang/OutOfMemoryError", "failed to allocate offered qos incompatible status"); |
| 35 | + } |
| 36 | + return reinterpret_cast<jlong>(p); |
| 37 | +} |
| 38 | + |
| 39 | +JNIEXPORT void JNICALL |
| 40 | +Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nDeallocateRCLStatusEvent( |
| 41 | + JNIEnv *, jclass, jlong handle) |
| 42 | +{ |
| 43 | + free(reinterpret_cast<void *>(handle)); |
| 44 | +} |
| 45 | + |
| 46 | +JNIEXPORT void JNICALL |
| 47 | +Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nFromRCLEvent( |
| 48 | + JNIEnv * env, jobject self, jlong handle) |
| 49 | +{ |
| 50 | + auto * p = reinterpret_cast<rmw_offered_qos_incompatible_event_status_t *>(handle); |
| 51 | + if (!p) { |
| 52 | + rcljava_throw_exception( |
| 53 | + env, "java/lang/IllegalArgumentException", "passed rmw object handle is NULL"); |
| 54 | + } |
| 55 | + // TODO(ivanpauno): class and field lookup could be done at startup time |
| 56 | + jclass clazz = env->GetObjectClass(self); |
| 57 | + jclass qos_kind_clazz = env->FindClass( |
| 58 | + "org/ros2/rcljava/events/publisher_statuses/OfferedQosIncompatible$PolicyKind"); |
| 59 | + if (env->ExceptionCheck()) { |
| 60 | + return; |
| 61 | + } |
| 62 | + jfieldID total_count_fid = env->GetFieldID(clazz, "totalCount", "I"); |
| 63 | + if (env->ExceptionCheck()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + jfieldID total_count_change_fid = env->GetFieldID(clazz, "totalCountChange", "I"); |
| 67 | + if (env->ExceptionCheck()) { |
| 68 | + return; |
| 69 | + } |
| 70 | + const char * enum_class_path = |
| 71 | + "Lorg/ros2/rcljava/events/publisher_statuses/OfferedQosIncompatible$PolicyKind;"; |
| 72 | + jfieldID policy_kind_fid = env->GetFieldID(clazz, "lastPolicyKind", enum_class_path); |
| 73 | + if (env->ExceptionCheck()) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + jfieldID enum_value_fid; |
| 78 | + switch (p->last_policy_kind) { |
| 79 | + case RMW_QOS_POLICY_INVALID: |
| 80 | + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "INVALID", enum_class_path); |
| 81 | + break; |
| 82 | + case RMW_QOS_POLICY_DURABILITY: |
| 83 | + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "DURABILITY", enum_class_path); |
| 84 | + break; |
| 85 | + case RMW_QOS_POLICY_DEADLINE: |
| 86 | + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "DEADLINE", enum_class_path); |
| 87 | + break; |
| 88 | + case RMW_QOS_POLICY_LIVELINESS: |
| 89 | + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "LIVELINESS", enum_class_path); |
| 90 | + break; |
| 91 | + case RMW_QOS_POLICY_RELIABILITY: |
| 92 | + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "RELIABILITY", enum_class_path); |
| 93 | + break; |
| 94 | + case RMW_QOS_POLICY_HISTORY: |
| 95 | + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "HISTORY", enum_class_path); |
| 96 | + break; |
| 97 | + case RMW_QOS_POLICY_LIFESPAN: |
| 98 | + enum_value_fid = env->GetStaticFieldID(qos_kind_clazz, "LIFESPAN", enum_class_path); |
| 99 | + break; |
| 100 | + default: |
| 101 | + rcljava_throw_exception( |
| 102 | + env, "java/lang/IllegalStateException", "unknown rmw qos policy kind"); |
| 103 | + break; |
| 104 | + } |
| 105 | + if (env->ExceptionCheck()) { |
| 106 | + return; |
| 107 | + } |
| 108 | + jobject enum_value = env->GetStaticObjectField(qos_kind_clazz, enum_value_fid); |
| 109 | + |
| 110 | + env->SetIntField(self, total_count_fid, p->total_count); |
| 111 | + env->SetIntField(self, total_count_change_fid, p->total_count_change); |
| 112 | + env->SetObjectField(self, policy_kind_fid, enum_value); |
| 113 | +} |
| 114 | + |
| 115 | +JNIEXPORT jint JNICALL |
| 116 | +Java_org_ros2_rcljava_events_publisher_1statuses_OfferedQosIncompatible_nGetPublisherEventType( |
| 117 | + JNIEnv *, jclass) |
| 118 | +{ |
| 119 | + return RCL_PUBLISHER_OFFERED_INCOMPATIBLE_QOS; |
| 120 | +} |
0 commit comments