|
| 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_publisher_statuses_OfferedDeadlineMissed.h" |
| 16 | + |
| 17 | +#include <jni.h> |
| 18 | +#include <stdlib.h> |
| 19 | + |
| 20 | +#include "rmw/types.h" |
| 21 | +#include "rcl/event.h" |
| 22 | +#include "rcljava_common/exceptions.hpp" |
| 23 | + |
| 24 | +using rcljava_common::exceptions::rcljava_throw_exception; |
| 25 | + |
| 26 | +JNIEXPORT jlong JNICALL |
| 27 | +Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeAllocateRCLStatusEvent( |
| 28 | + JNIEnv * env, jclass) |
| 29 | +{ |
| 30 | + void * p = malloc(sizeof(rmw_offered_deadline_missed_status_t)); |
| 31 | + if (!p) { |
| 32 | + rcljava_throw_exception( |
| 33 | + env, "java/lang/OutOfMemoryError", "failed to allocate deadline missed status"); |
| 34 | + } |
| 35 | + return reinterpret_cast<jlong>(p); |
| 36 | +} |
| 37 | + |
| 38 | +JNIEXPORT void JNICALL |
| 39 | +Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeDeallocateRCLStatusEvent( |
| 40 | + JNIEnv *, jclass, jlong handle) |
| 41 | +{ |
| 42 | + free(reinterpret_cast<void *>(handle)); |
| 43 | +} |
| 44 | + |
| 45 | +JNIEXPORT void JNICALL |
| 46 | +Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeFromRCLEvent( |
| 47 | + JNIEnv * env, jobject self, jlong handle) |
| 48 | +{ |
| 49 | + auto * p = reinterpret_cast<rmw_offered_deadline_missed_status_t *>(handle); |
| 50 | + if (!p) { |
| 51 | + rcljava_throw_exception( |
| 52 | + env, "java/lang/IllegalArgumentException", "passed rmw object handle is NULL"); |
| 53 | + } |
| 54 | + // TODO(ivanpauno): class and field lookup could be done at startup time |
| 55 | + jclass clazz = env->GetObjectClass(self); |
| 56 | + jfieldID total_count_fid = env->GetFieldID(clazz, "totalCount", "I"); |
| 57 | + if (env->ExceptionCheck()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + jfieldID total_count_change_fid = env->GetFieldID(clazz, "totalCountChange", "I"); |
| 61 | + if (env->ExceptionCheck()) { |
| 62 | + return; |
| 63 | + } |
| 64 | + env->SetIntField(self, total_count_fid, p->total_count); |
| 65 | + env->SetIntField(self, total_count_change_fid, p->total_count_change); |
| 66 | +} |
| 67 | + |
| 68 | +JNIEXPORT jint JNICALL |
| 69 | +Java_org_ros2_rcljava_publisher_statuses_OfferedDeadlineMissed_nativeGetPublisherEventType( |
| 70 | + JNIEnv *, jclass) |
| 71 | +{ |
| 72 | + return RCL_PUBLISHER_OFFERED_DEADLINE_MISSED; |
| 73 | +} |
0 commit comments