JAVA wrapper not working in this Cisco EnergyWise code, can anyone help

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JAVA wrapper not working in this Cisco EnergyWise code, can anyone help

Jim Cordill

Hi List,

  I’ve been given a  project to resurrect some old Cisco code that retrieves energy consumption data from Cisco switches, it appears the code was abandoned a few years ago, so I don’t know if it ever worked.  I have managed to get the C code working, and now I’m working on the JAVA version which used SWIG to generate the JNI interface files, until I started this project, I had never heard of SWIG…   I’m and old C programmer, so even Java is new to me….

So the problem, in the interface file jarg4 is tested for valid array length and fails, printing out the value of the GetArrayLength() call returns a large number, perhaps a pointer value, and not the size of the array.   I’ve tried several things, but I don’t know what I’m doing.  Any help is appreciated.

The problem occurs in the call to the “energywise_utl_composeKey()” function, this function combines the UUID parameter with the secret string to form a key to be used to authenticate the session with the switch later in the code, the key array is passed as one of the input parameters.

 

When I type on the command line:

$ java -cp CiscoEnergyWiseJni.jar com.cisco.ewapi.jni.Example -h 10.2.0.40 -l 10.2.1.75 -d company -s PASS

I get:

Exception in thread "main" java.lang.IndexOutOfBoundsException: incorrect array size

        at com.cisco.ewapi.jni.EnergywiseJNI.energywise_utl_composeKey(Native Method)

        at com.cisco.ewapi.jni.Energywise.energywise_utl_composeKey(Unknown Source)

        at com.cisco.ewapi.jni.Example.main(Unknown Source)

 

You can find the error msg in the wrapper code below, when its testing jarg4, the length is not returning as 20, but is a larger value, a pointer perhaps?

I have also included a portion of the JAVA code where it calls the C function.

TIA

Jim

 

The SWIG wrapper code generated is:

SWIGEXPORT jint JNICALL Java_com_cisco_ewapi_jni_EnergywiseJNI_energywise_1utl_1composeKey(JNIEnv *jenv, jclass jcls, jstring jarg1, jint jarg2, jlong jarg3, jobject jarg3_, jshortArray jarg4, jint jarg5) {

  jint jresult = 0 ;

  void *arg1 = (void *) 0 ;

  int arg2 ;

  energywise_sender_id_t *arg3 = (energywise_sender_id_t *) 0 ;

  unsigned char *arg4 ;

  int arg5 ;

  jshort *jarr4 ;

  int result;

 

  (void)jenv;

  (void)jcls;

  (void)jarg3_;

  arg1 = 0;

  if (jarg1) {

    arg1 = (void *)(*jenv)->GetStringUTFChars(jenv, jarg1, 0);

    if (!arg1) return 0;

  }

  arg2 = (int)jarg2;

  arg3 = *(energywise_sender_id_t **)&jarg3;

  if (jarg4 && (*jenv)->GetArrayLength(jenv, jarg4) != 20) {

    SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "incorrect array size");

    return 0;

  }

  if (!SWIG_JavaArrayInUchar(jenv, &jarr4, (unsigned char **)&arg4, jarg4)) return 0;

  arg5 = (int)jarg5;

  result = (int)energywise_utl_composeKey((void const *)arg1,arg2,(struct energywise_sender_id_t_ const *)arg3,arg4,arg5);

  jresult = (jint)result;

  SWIG_JavaArrayArgoutUchar(jenv, jarr4, (unsigned char *)arg4, jarg4);

  if (arg1) (*jenv)->ReleaseStringUTFChars(jenv, jarg1, (const char *)arg1);

  free(arg4);

  return jresult;

}

 

 

 

*ewapi.i*

/*

=====================================================================================

  Name        : ewapi.i

  Description : SWIG Interface file used to generate the JNI and Proxy Java Classes

=====================================================================================

 

Copyright (c) 2009-2012 by Cisco Systems, Inc.

*/

%module Energywise

%include "typemaps.i"

%include "stdint.i"

 

%{

#include "EnergyWise.h"

#include "EnergyWiseApi.h"

#include "EnergyWiseUtil.h"

#include "EnergyWiseLog.h"

%}

 

%rename (EWID) energywise_sender_id_t_;

 

typedef unsigned char energywise_id_v1_t[ENERGYWISE_ID_SIZE];

typedef energywise_id_v1_t energywise_id_t;

 

struct energywise_sender_id_t_ {

    energywise_id_t       id;

    uint32_t              phy_idx;

};

 

%rename (EWRecurrence) energywise_recurrence_t_;

 

typedef struct energywise_recurrence_t_ {

    char                               *cron;

    uint8_t                             level;

    uint8_t                             importance;

    int                                 remove;

} energywise_recurrence_t;

 

%typemap(javacode) struct energywise_neighbor_details_t_ %{

  public String getMacAddress() {

    return Energywise.energywise_getNeighborMacAddress(this);

  }

 

   public String getEnergyWiseId() {

    return Energywise.energywise_getNeighborEnergyWiseId(this);

  }

 

   public String getSocketAddress() {

    return Energywise.energywise_getNeighborAddress(this);

  }

 

%};

 

%immutable;

%rename (EWNeighbor) energywise_neighbor_details_t_;

 

typedef struct energywise_neighbor_details_t_ {

    uint16_t                      device_capability_code;

    char                          *name;

    uint32_t                      saddr_len;

    uint8_t                       discovery_flag;

    int                           is_child;

    uint32_t                      connected_interface_ent_phy_idx;

    char                          *connected_interface_name;

} energywise_neighbor_details_t;

 

%mutable;

 

%rename (EWEndpointCache) energywise_endpoint_cache_t;

%rename (EWUsageCaliber) energywise_usage_caliber_t;

%rename (EWUsageCategory) energywise_usage_category_t;

%rename (EWAttributeType) ew_attribute_type_t;

%rename (EWActionAggregate) ew_action_aggregate_t;

%rename (EWActionQuery) ew_action_query_t;

%rename (EWClass) ew_class_t;

%rename (EWSetType) ew_set_type_t;

%rename (EWLogLevel) log_levels_t;

%rename (EWResultRow) SWIGTYPE_p_enw_result_row_t;

 

%include "arrays_java.i"

%apply char * { const void * };

int energywise_utl_composeKey(const void *secret, int secret_len, const energywise_sender_id_t *id, unsigned char key[20], int key_len);

enw_session_t* energywise_createSessionWithPort(const char *targetaddr, int targetport, const char *localaddr, int localport, const energywise_sender_id_t *id, unsigned char key[20], int key_len);

enw_session_t* energywise_createSession(const char *targetaddr, int targetport, const char *localaddr, const energywise_sender_id_t *id, unsigned char key[20], int key_len);

 

%apply int[] { int32_t * }

%apply int32_t *OUTPUT { int32_t *length }

 

%ignore energywise_utl_composeKey;

%ignore energywise_getAcks;

%ignore energywise_getNaks;

%ignore setLogFile;

%ignore enableLogCallback;

%ignore setLogCallback;

%ignore energywise_createSession;

%ignore energywise_createSessionWithPort;

%ignore energywise_addQualifier;

 

 

%include "EnergyWise.h"

%include "EnergyWiseApi.h"

%include "EnergyWiseUtil.h"

%include "EnergyWiseLog.h"

 

%typemap(jstype) jobject getEnergyWiseIdFromRow "com.cisco.ewapi.jni.EnergywiseId"

%typemap(jtype) jobject getEnergyWiseIdFromRow "com.cisco.ewapi.jni.EnergywiseId"

%native(getEnergyWiseIdFromRow) jobject getEnergyWiseIdFromRow(enw_resultrow_t);

 

%typemap(jstype) jobject getBlobFromRow "com.cisco.ewapi.jni.EnergywiseBlob"

%typemap(jtype) jobject getBlobFromRow "com.cisco.ewapi.jni.EnergywiseBlob"

%native(getBlobFromRow) jobject getBlobFromRow(enw_resultrow_t);

 

%typemap(jstype) jint addEnergyWiseIdQualifer "Integer"

%typemap(jtype) jint addEnergyWiseIdQualifer "Integer"

%native(addEnergyWiseIdQualifer) jint addEnergyWiseIdQualifer(SWIGTYPE_p_enw_query, jlong);

 

%typemap(jstype) jint releasePointer "Integer"

%typemap(jtype) jint releasePointer "Integer"

%native(releasePointer) jint releasePointer(EWAttributeType, jlong);

 

The JAVA code is too big to post but here is the portion where it calls the failing function:

*Example.java*

EWID energyWiseId=new EWID();

        short[] key = new short[20];

       

        //creates a 36 byte EnergyWise Id

        Energywise.energywise_utl_createUuid(energyWiseId);

            System.out.println("*** 1 ***");/* PRINTS ON CONSOLE, code reached here! */

        Energywise.energywise_utl_composeKey(secret, secret.getBytes().length, energyWiseId, key, 20); /*<- here is where it fails! */

            System.out.println("*** 2 ***"); /* NOT SEEN ON CONSOLE so fails before it reaches here */

        //initializes the EnergyWise session that is used to run EnergyWise queries on the supplied EnergyWise domain

        SWIGTYPE_p_enw_session session = Energywise.energywise_createSession(host, udpport, localhost, energyWiseId , key, 20);

        if(session == null){

                c.printf("\nEnergywise Session Creation Failure\n");

                System.exit(1);

        }      

 

Jim Cordill
Software Developer

 


Notice: This email message, together with any attachments, contains information of Lynxspring Inc., which may be confidential, proprietary, copyrighted and/or legally privileged. This email is intended solely for the use of the individual or entity named on the message. If you are not the intended recipient, and have received this message in error, please immediately return by email and then delete it.

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. http://sdm.link/zohodev2dev
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...