oc_endpoint.i 5.01 KB
Newer Older
1 2 3
/* File oc_endpoint.i */
%module OCEndpointUtil

George Nash's avatar
George Nash committed
4 5
%include "stdint.i"
%include "arrays_java.i"
6 7
%include "iotivity.swg"

George Nash's avatar
George Nash committed
8 9
%import "oc_uuid.i"

10 11 12 13 14 15 16 17 18 19 20 21 22
%pragma(java) jniclasscode=%{
  static {
    try {
        System.loadLibrary("iotivity-lite-jni");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load. \n" + e);
      System.exit(1);
    }
  }
%}

%{
#include "oc_endpoint.h"
George Nash's avatar
George Nash committed
23
#include "oc_iotivity_lite_jni.h"
24 25 26
%}

/*******************Begin oc_endpoint.h*********************/
27 28 29 30 31 32 33 34 35 36 37 38
%extend oc_endpoint_t {
  oc_endpoint_t() {
    OC_DBG("JNI: %s\n", __func__);
    return oc_new_endpoint();
  }

  ~oc_endpoint_t() {
   OC_DBG("JNI: %s\n", __func__);
   oc_free_endpoint($self);
   $self = NULL;
  }
}
39
%rename(OCEndpoint) oc_endpoint_t;
George Nash's avatar
George Nash committed
40 41
// must use the oc_endpoint_set_di function to set di.
%immutable oc_endpoint_t::di;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// transport flags are pulled from hand generated class as `int` not `enum`
%ignore transport_flags;
//%rename (OCTransportFlags) transport_flags;
%rename(DevAddr) dev_addr;
//if uncommented the following apply lines will cause the output to be byte[] vs short[]
//%apply signed char[ANY] { uint8_t address[4] };
//%apply signed char[ANY] { uint8_t address[16] };
%rename(OCIPv6Addr) oc_ipv6_addr_t;
%rename(OCIPv4Addr) oc_ipv4_addr_t;
%rename(OCLEAddr) oc_le_addr_t;
%rename(addrLocal) addr_local;
%rename(OCFVersion) ocf_version_t;
%rename(interfaceIndex) interface_index;
// look into exposing oc_make_ipv4_endpoint and oc_make_ipv6_endpoint
%rename(newEndpoint) oc_new_endpoint;
57 58 59 60 61 62 63 64
%ignore oc_free_endpoint;
%rename(freeEndpoint) jni_free_endpoint;
%inline %{
void jni_free_endpoint(oc_endpoint_t *endpoint) {
  oc_free_endpoint(endpoint);
  endpoint = NULL;
}
%}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
%ignore oc_endpoint_set_di;
%exception oc_endpoint_set_di {
  /* The `oc_endpoint_t *endpoint` parameter is jarg1, the name is generated by SWIG. */
  if(jarg1 == NULL) {
    OC_DBG("JNI: OCEndpoint cannot be null.\n");
    jclass clazz = (*jenv)->FindClass(jenv, "java/lang/NullPointerException");
    (*jenv)->ThrowNew(jenv, clazz, "OCEndpoint cannot be null.");
    return;
  }
  /* The `oc_uuid_t *di` parameter is jarg2, the name is generated by SWIG. */
  if(jarg2 == NULL) {
    OC_DBG("JNI: OCUuid cannot be null.\n");
    jclass clazz = (*jenv)->FindClass(jenv, "java/lang/NullPointerException");
    (*jenv)->ThrowNew(jenv, clazz, "OCUuid cannot be null.");
    return;
  }
  $action
}
83
%rename(setDi) oc_endpoint_set_di;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

/*
%inline %{
void jni_endpoint_set_di(oc_endpoint_t *endpoint, oc_uuid_t *di) {
  if(endpoint == NULL) {
  OC_DBG("JNI: OCEndpoint cannot be null.\n", __func__);
    jclass clazz = (*jenv)->FindClass(jenv, "java/lang/IllegalArgumentException");
    (*jenv)->ThrowNew(jenv, clazz, "OCEndpoint cannot be null.");
    return;
  }
  if(di == NULL) {
  OC_DBG("JNI: OCUuid cannot be null.\n", __func__);
    jclass clazz = (*jenv)->FindClass(jenv, "java/lang/IllegalArgumentException");
    (*jenv)->ThrowNew(jenv, clazz, "OCUuid cannot be null.");
    return;
  }
  oc_endpoint_set_di(endpoint, di);
}
%}
*/
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
%ignore oc_endpoint_to_string;

%typemap(jni)    jobject toString "jobject";
%typemap(jtype)  jobject toString "String";
%typemap(jstype) jobject toString "String";
%typemap(javain) jobject toString "$javainput";
%pragma(java) jniclassimports="import java.lang.String;"
%native (toString) jobject toString(oc_endpoint_t *endpoint);
%{
#ifdef __cplusplus
extern "C"
#endif
SWIGEXPORT jobject JNICALL Java_org_iotivity_OCEndpointUtilJNI_toString(JNIEnv *jenv,
                                                                      jclass jcls,
                                                                      jlong jendpoint,
                                                                      jobject jendpoint_)
{
  jobject jresult = 0;
  oc_endpoint_t *endpoint = (oc_endpoint_t *)0;
  jobject result;

  (void)jenv;
  (void)jcls;
  (void)jendpoint_;
  endpoint = *(oc_endpoint_t **)&jendpoint;

  oc_string_t ep;
  int r = oc_endpoint_to_string(endpoint, &ep);
  if(r < 0) {
    return NULL;
  }

  result = JCALL1(NewStringUTF, jenv, oc_string(ep));
  oc_free_string(&ep);

  jresult = result;
  return jresult;
}
%}


145 146
%apply oc_string_t *INPUT { oc_string_t *endpoint_str };
%apply oc_string_t *OUTPUT { oc_string_t *uri };
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/* TODO figure out a clean way to return the uri param not as an array value */
%ignore oc_string_to_endpoint;
%rename(stringToEndpoint) jni_string_to_endpoint;
%inline %{
oc_endpoint_t * jni_string_to_endpoint(oc_string_t *endpoint_str, oc_string_t *uri) {
  OC_DBG("JNI: %s\n", __func__);
  oc_endpoint_t *ep = oc_new_endpoint();
  if(oc_string_to_endpoint(endpoint_str, ep, uri) < 0) {
    OC_DBG("JNI: oc_string_to_endpoint failed to parse %s\n", oc_string(*endpoint_str));
    oc_free_endpoint(ep);
    return NULL;
  }
  return ep;
}
%}
162 163 164
%rename(ipv6EndpointIsLinkLocal) oc_ipv6_endpoint_is_link_local;
%rename(compare) oc_endpoint_compare;
%rename(compareAddress) oc_endpoint_compare_address;
165
%rename(setLocalAddress) oc_endpoint_set_local_address;
166 167
%include "oc_endpoint.h"
/*******************End oc_endpoint.h***********************/