xref: /aosp_15_r20/external/aws-crt-java/src/native/mqtt_client.c (revision 3c7ae9de214676c52d19f01067dc1a404272dc11)
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 #include <jni.h>
6 
7 #include <aws/mqtt/client.h>
8 
9 #include "crt.h"
10 #include "java_class_ids.h"
11 
12 /* on 32-bit platforms, casting pointers to longs throws a warning we don't need */
13 #if UINTPTR_MAX == 0xffffffff
14 #    if defined(_MSC_VER)
15 #        pragma warning(push)
16 #        pragma warning(disable : 4305) /* 'type cast': truncation from 'jlong' to 'jni_tls_ctx_options *' */
17 #    else
18 #        pragma GCC diagnostic push
19 #        pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
20 #        pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
21 #    endif
22 #endif
23 
24 JNIEXPORT jlong JNICALL
Java_software_amazon_awssdk_crt_mqtt_MqttClient_mqttClientNew(JNIEnv * env,jclass jni_class,jlong jni_bootstrap)25     Java_software_amazon_awssdk_crt_mqtt_MqttClient_mqttClientNew(JNIEnv *env, jclass jni_class, jlong jni_bootstrap) {
26     (void)jni_class;
27     aws_cache_jni_ids(env);
28 
29     struct aws_client_bootstrap *bootstrap = (struct aws_client_bootstrap *)jni_bootstrap;
30     if (!bootstrap) {
31         aws_jni_throw_runtime_exception(env, "Invalid ClientBootstrap");
32         return (jlong)NULL;
33     }
34 
35     struct aws_allocator *allocator = aws_jni_get_allocator();
36     struct aws_mqtt_client *client = aws_mqtt_client_new(allocator, bootstrap);
37     if (client == NULL) {
38         aws_jni_throw_runtime_exception(env, "MqttClient.mqtt_client_init: aws_mqtt_client_new failed");
39         return (jlong)NULL;
40     }
41 
42     return (jlong)client;
43 }
44 
Java_software_amazon_awssdk_crt_mqtt_MqttClient_mqttClientDestroy(JNIEnv * env,jclass jni_class,jlong jni_mqtt_client)45 JNIEXPORT void JNICALL Java_software_amazon_awssdk_crt_mqtt_MqttClient_mqttClientDestroy(
46     JNIEnv *env,
47     jclass jni_class,
48     jlong jni_mqtt_client) {
49     (void)jni_class;
50     aws_cache_jni_ids(env);
51 
52     struct aws_mqtt_client *client = (struct aws_mqtt_client *)jni_mqtt_client;
53     if (!client) {
54         aws_jni_throw_runtime_exception(env, "MqttClient.mqtt_client_destroy: Invalid/null client");
55         return;
56     }
57 
58     aws_mqtt_client_release(client);
59 }
60 
61 #if UINTPTR_MAX == 0xffffffff
62 #    if defined(_MSC_VER)
63 #        pragma warning(pop)
64 #    else
65 #        pragma GCC diagnostic pop
66 #    endif
67 #endif
68