1

I am very new to this question answer forum and same in the swig. I am not sure if I am following correct steps to generate wrappers for java and C using swig interface file.

my header example.h file looks like below

#ifndef INCLUDE_ARTIK_WEBSOCKET_H_
#define INCLUDE_ARTIK_WEBSOCKET_H_

#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
    SAMPLE_WEBSOCKET_CLOSED = 1,
    SAMPLE_WEBSOCKET_CONNECTED,
    SAMPLE_WEBSOCKET_HANDSHAKE_ERROR
} sample_websocket_connection_state;

typedef void *sample_websocket_handle;

typedef struct {
    char *uri;
void *private_data;
} sample_websocket_config;


typedef struct {
    void(*websocket_request) (
                sample_websocket_handle * handle,
                sample_websocket_config * config
                );

} sample_websocket_module;

extern const sample_websocket_module websocket_module;

#ifdef __cplusplus
}
#endif
#endif

And my interface file test_app.i looks like below:

%module  test_app 
%{
#include "example.h"
%}

%include "example.h"

And when I generate wrappers using command swig -java test_app.i which generates following files

  1. sample_websocket_config.java
  2. sample_websocket_module.java
  3. SWIGTYPE_p_void.java
  4. test_app.java
  5. test_app_wrap.c
  6. sample_websocket_connection_state.java
  7. SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void.java
  8. test_appJNI.java

    typedef struct {
    void(*websocket_request) (
                sample_websocket_handle * handle,
                sample_websocket_config * config
                );
    

    } sample_websocket_module;

above code generates sample_websocket_module.java class which looks like below

public class sample_websocket_module {
  private transient long swigCPtr;
  protected transient boolean swigCMemOwn;

  protected sample_websocket_module(long cPtr, boolean cMemoryOwn) {
    swigCMemOwn = cMemoryOwn;
    swigCPtr = cPtr;
  }

  protected static long getCPtr(sample_websocket_module obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        test_appJNI.delete_sample_websocket_module(swigCPtr);
      }
      swigCPtr = 0;
    }
  }

  public void setWebsocket_request(SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void value) {
    test_appJNI.sample_websocket_module_websocket_request_set(swigCPtr, this, SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void.getCPtr(value));
  }

  public SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void getWebsocket_request() {
    long cPtr = test_appJNI.sample_websocket_module_websocket_request_get(swigCPtr, this);
    return (cPtr == 0) ? null : new SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void(cPtr, false);
  }

  public sample_websocket_module() {
    this(test_appJNI.new_sample_websocket_module(), true);
  }

}

So I am not sure how to provide arguments to this function as it has merged both the arguments and created a separate class SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void.java.

    public class SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void {
  private transient long swigCPtr;

  protected SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void(long cPtr, @SuppressWarnings("unused") boolean futureUse) {
    swigCPtr = cPtr;
  }

  protected SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void() {
    swigCPtr = 0;
  }

  protected static long getCPtr(SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }
}

and this is the function which is using above class. So how can I provide values to this function.

public void setWebsocket_request(SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void value) {
    test_appJNI.sample_websocket_module_websocket_request_set(swigCPtr, this, SWIGTYPE_p_f_p_p_void_p_sample_websocket_config__void.getCPtr(value));
  }

Thanks in advance.

1 Answer 1

1

I think you will need to add some C code that will do the pointer dereference that you could then call. You can add a function in the interface file that will get pasted into the test_app_wrap.c.

So test_app.i becomes:

%module  test_app 
%{
#include "example.h"

    void callwebsocketreq(sample_websocket_module *mod, 
               sample_websocket_handle * handle,
                sample_websocket_config * config) {
        (*mod->websocket_request)(handle,config);
    }

%}

%include "example.h"
void callwebsocketreq(sample_websocket_module *mod, 
               sample_websocket_handle * handle,
                sample_websocket_config * config);

Now there will be a callwebsocketreq method in the test_app.java that you can call to pass in those arguments.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.