oc_endpoint.i 5.17 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
%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");
George Nash's avatar
George Nash committed
70 71
    jclass clazz = JCALL1(FindClass, jenv, "java/lang/NullPointerException");
    JCALL2(ThrowNew, jenv, clazz, "OCEndpoint cannot be null.");
72 73 74 75 76
    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");
George Nash's avatar
George Nash committed
77 78
    jclass clazz = JCALL1(FindClass, jenv, "java/lang/NullPointerException");
    JCALL2(ThrowNew, jenv, clazz, "OCUuid cannot be null.");
79 80 81 82
    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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
%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;
}
%}


125 126
%apply oc_string_t *INPUT { oc_string_t *endpoint_str };
%apply oc_string_t *OUTPUT { oc_string_t *uri };
George Nash's avatar
George Nash committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

%javaexception("OCEndpointParseException") jni_string_to_endpoint {
  $action
  if(result == NULL) {
    OC_DBG("JNI: String can not be parsed.");
    jclass cls_OCEndpointParseException = JCALL1(FindClass, jenv, "org/iotivity/OCEndpointParseException");
    assert(cls_OCEndpointParseException);
    oc_string_t exception_message_part1;
    oc_concat_strings(&exception_message_part1, "The \"", oc_string(*arg1));
    oc_string_t exception_message;
    oc_concat_strings(&exception_message, oc_string(exception_message_part1), "\" string cannot be parsed.");
    JCALL2(ThrowNew, jenv, cls_OCEndpointParseException, ((char *)oc_string(exception_message)));
    oc_free_string(&exception_message_part1);
    oc_free_string(&exception_message);
    return $null;
  }
}
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
/* 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;
}
%}
159 160 161
%rename(ipv6EndpointIsLinkLocal) oc_ipv6_endpoint_is_link_local;
%rename(compare) oc_endpoint_compare;
%rename(compareAddress) oc_endpoint_compare_address;
162
%rename(setLocalAddress) oc_endpoint_set_local_address;
163 164
%include "oc_endpoint.h"
/*******************End oc_endpoint.h***********************/