Solved

calling c dll from java using JNI

Posted on 2003-10-22
4
1,110 Views
Last Modified: 2007-12-19
Dear All

I am accessing a dll from java, the dll was written in c and I used jni (Java Native Interface) to wrap my c code so that I can call the functions from java.

I have two native functions, each returning a structure as an object to the java, this is done through traversing through a linked list that stores my data structures.

here is the problem
When ever I call just one function, the results are all fine and everything is ok, I get the structure back as an object and the linked list gets traversed until it reaches the end.
This tells me that my functions and the jni interface is working

however when ever I call both functions, the first one works just fine, while the second one crashes and return the following message

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
An unexpected exception has been detected in native code outside the VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at PC=0x184731E3
Function=[Unknown.]
Library=D:\WINNT\system32\MPS_TrackLib.dll
NOTE: We are unable to locate the function name symbol for the error
      just occurred. Please refer to release documentation for possible
      reason and solutions.
Current Java thread:
        at Test.ROTManager.ReadAosLosSol(Native Method)
        at Test.ROTManager.AosLosPasses(ROTManager.java:144)
        at Test.Wrapper.main(Wrapper.java:28)
Dynamic libraries:
0x00400000 - 0x00406000         G:\APPS\s1studio_jdk\j2sdk1.4.2\jre\bin\java.exe
0x77F60000 - 0x77FBE000         D:\WINNT\System32\ntdll.dll
0x77DC0000 - 0x77DFF000         D:\WINNT\system32\ADVAPI32.dll
0x77F00000 - 0x77F5E000         D:\WINNT\system32\KERNEL32.dll
0x77E70000 - 0x77EC2000         D:\WINNT\system32\USER32.dll
0x77ED0000 - 0x77EFC000         D:\WINNT\system32\GDI32.dll
0x77E10000 - 0x77E67000         D:\WINNT\system32\RPCRT4.dll
0x78000000 - 0x78040000         D:\WINNT\system32\MSVCRT.dll
0x08000000 - 0x08136000         G:\APPS\s1studio_jdk\j2sdk1.4.2\jre\bin\client\jvm.dll
0x77FD0000 - 0x77FFA000         D:\WINNT\System32\WINMM.dll
0x77FC0000 - 0x77FC8000         D:\WINNT\System32\mmdrv.dll
0x6BC00000 - 0x6BC10000         D:\WINNT\System32\SMNT40.dll
0x69F00000 - 0x69FBA000         D:\WINNT\System32\adisynth.dll
0x10000000 - 0x10007000         G:\APPS\s1studio_jdk\j2sdk1.4.2\jre\bin\hpi.dll
0x00910000 - 0x0091E000         G:\APPS\s1studio_jdk\j2sdk1.4.2\jre\bin\verify.dll
0x00920000 - 0x00938000         G:\APPS\s1studio_jdk\j2sdk1.4.2\jre\bin\java.dll
0x00940000 - 0x0094D000         G:\APPS\s1studio_jdk\j2sdk1.4.2\jre\bin\zip.dll
0x18470000 - 0x184BA000         D:\WINNT\system32\MPS_TrackLib.dll
0x185D0000 - 0x185DC000         G:\APPS\s1studio_jdk\j2sdk1.4.2\jre\bin\JdbcOdbc.dll
0x1F7D0000 - 0x1F804000         D:\WINNT\System32\ODBC32.dll
0x77D80000 - 0x77DB2000         D:\WINNT\system32\comdlg32.dll
0x77C40000 - 0x77D7B000         D:\WINNT\system32\SHELL32.dll
0x71710000 - 0x71794000         D:\WINNT\system32\COMCTL32.dll
0x1F8C0000 - 0x1F8D6000         D:\WINNT\System32\odbcint.dll
0x18720000 - 0x18782000         D:\WINNT\System32\myodbc3.dll
0x5F000000 - 0x5F011000         D:\WINNT\System32\CTL3D32.dll
0x776D0000 - 0x776D8000         D:\WINNT\system32\WSOCK32.dll
0x776B0000 - 0x776C4000         D:\WINNT\system32\WS2_32.dll
0x776A0000 - 0x776A7000         D:\WINNT\system32\WS2HELP.dll
0x1F820000 - 0x1F83A000         D:\WINNT\System32\odbccp32.dll
0x77B20000 - 0x77BD1000         D:\WINNT\system32\ole32.dll
0x77A90000 - 0x77A9B000         D:\WINNT\system32\VERSION.dll
0x779C0000 - 0x779C8000         D:\WINNT\system32\LZ32.dll
0x74FF0000 - 0x74FFE000         D:\WINNT\System32\rnr20.dll
0x77BF0000 - 0x77BF7000         D:\WINNT\System32\rpcltc1.dll
0x77660000 - 0x7766F000         D:\WINNT\system32\msafd.dll
0x77690000 - 0x77699000         D:\WINNT\System32\wshtcpip.dll
0x76AC0000 - 0x76ADD000         D:\WINNT\System32\imagehlp.dll
0x71DC0000 - 0x71DCA000         D:\WINNT\System32\PSAPI.DLL
Heap at VM Abort:
Heap
 def new generation   total 576K, used 49K [0x10010000, 0x100b0000, 0x104f0000)
  eden space 512K,   0% used [0x10010000, 0x10011208, 0x10090000)
  from space 64K,  69% used [0x100a0000, 0x100ab2b0, 0x100b0000)
  to   space 64K,   0% used [0x10090000, 0x10090000, 0x100a0000)
 tenured generation   total 1408K, used 234K [0x104f0000, 0x10650000, 0x14010000)
   the space 1408K,  16% used [0x104f0000, 0x1052abd8, 0x1052ac00, 0x10650000)
 compacting perm gen  total 4096K, used 2089K [0x14010000, 0x14410000, 0x18010000)
   the space 4096K,  51% used [0x14010000, 0x1421a5c0, 0x1421a600, 0x14410000)
Local Time = Wed Oct 22 10:31:15 2003
Elapsed Time = 9
#
# The exception above was detected in native code outside the VM
#
# Java VM: Java HotSpot(TM) Client VM (1.4.2-b28 mixed mode)
#
# An error report file has been saved as hs_err_pid262.log.
# Please refer to the file for further information.
#


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

what could be causing this problem.

Any help welcomed.
0
Comment
Question by:Havalchy
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
Ajar earned 30 total points
Comment Utility
Can you send t he C code ... Most probably there is a bug in the C program

.. Try to verify the C dll program in a  C application


0
 
LVL 1

Author Comment

by:Havalchy
Comment Utility
OK now here is the Source code.
I did not comment it out yet I call some other C code that is very well documented and implemented.
I suspect that the way I am freeing my linked list could be wrong or is where my main problems lie I am not sure


/*************************************************************************
Linked List File
************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "jnimain.h"

/* Create a linked list structure */
linked_list *CreateList()
{
linked_list *retval;

     if ((retval = (linked_list*)malloc(sizeof(linked_list))) == NULL)
          return NULL;

     retval->head = retval->tail = NULL;

     return retval;
}

/* Add a node to the list */
node *CreateNode(aoslosResult result, int index)
{
node *retval;

     if ((retval = (node*)malloc(sizeof(node))) == NULL)
          return NULL;

     retval->result = result;
       retval->index = index;
     retval->next = NULL;

     return retval;
}



/* Add the result to the linked list */
void ListAddNode(linked_list *ll,aoslosResult resultData, int index)
{
node *node;
int i = index;
     if ((node = CreateNode(resultData, i)) == NULL)
          return;

     if (ll->head == NULL)
          ll->head = node;
     else
             ll->tail->next = node;

     ll->tail = node;
}

/* Count the links */
int ListLength(linked_list *ll)
{
int retval = 0;
node *n;

     if (ll != NULL)
          for (n = ll->head;n != NULL;n = n->next)
               retval++;

     return retval;
}

void ListTransverse(linked_list *ll)
{
node *n;
int link = 1;

     if (ll != NULL)
          for (n = ll->head;n != NULL;n = n->next)
               printf("\nNode %d: StartTime %ld EndTime %ld"
                    ,n->index
                    ,n->result.aostime
                    ,n->result.lostime
                    );
}

void getListData(linked_list *ll, int i, aoslosResult *res)
{
node *n;
int count = 0;


     if (ll != NULL)
             for (n = ll->head; count < i ; n = n->next)  {
                        count++;
                        res->aostime = n->result.aostime;
                        res->lostime = n->result.lostime;
                        res->aosAz = n->result.aosAz;
                        res->aosEl = n->result.aosEl;
                        res->losAz = n->result.losAz;
                        res->losEl = n->result.losEl;
                        res->AosSunOrEclipse = n->result.AosSunOrEclipse;
                        res->LosSunOrEclipse = n->result.LosSunOrEclipse;
                        res->maxEl = n->result.maxEl;
             }
}

void freeList(linked_list *ll)
{
    node *p, *q;

    p = ll->head;
    while (p != NULL) {
      q = p;
      p = p->next;
      free(q);
    }
    ll->head = NULL;
}




/*eclipse Linked List code */

eclipse_list *CreateEclipseList()
{
eclipse_list *retval;

     if ((retval = (eclipse_list*)malloc(sizeof(eclipse_list))) == NULL)
          return NULL;

     retval->head = retval->tail = NULL;

     return retval;
}

/* Add a node to the list */
eclipseNode *CreateEclipseNode(eclipseResult result, int index)
{
eclipseNode *retval;

     if ((retval = (eclipseNode*)malloc(sizeof(eclipseNode))) == NULL)
          return NULL;

     retval->result = result;
       retval->index = index;
     retval->next = NULL;

     return retval;
}

/* Add the result to the linked list */
void eclipseListAddNode(eclipse_list *ll, eclipseResult resultData, int index)
{
eclipseNode *eclipseNode;
int i = index;
     if ((eclipseNode = CreateEclipseNode(resultData, i)) == NULL)
          return;

     if (ll->head == NULL)
         ll->head = eclipseNode;
     else
             ll->tail->next = eclipseNode;

     ll->tail = eclipseNode;
}

/* Count the links */
int eclipseListLength(eclipse_list *ll)
{
int retval = 0;
eclipseNode *n;

     if (ll != NULL)
          for (n = ll->head;n != NULL;n = n->next)
               retval++;

     return retval;
}

void eclipseListTransverse(eclipse_list *ll)
{
eclipseNode *n;
int link = 1;

     if (ll != NULL)
          for (n = ll->head;n != NULL;n = n->next)
               printf("\nNode %d: EclipseStartTime %ld EclipseEndTime %ld",
                                    n->index,
                                    n->result.eclipseStartTime,
                                    n->result.eclipseEndTime );
}

void getEclipseListData(eclipse_list *ll, int i, eclipseResult *res)
{
eclipseNode *n;
int count = 0;


     if (ll != NULL)
             for (n = ll->head; count < i ; n = n->next)  {
                        count++;
                        res->eclipseStartTime = n->result.eclipseStartTime;
                        res->eclipseEndTime = n->result.eclipseEndTime;
             }
}

void freeEclipseList(eclipse_list *ll)
{
    eclipseNode *p, *q;

    p = ll->head;
    while (p != NULL) {
      q = p;
      p = p->next;
      free(q);
    }
    ll->head = NULL;
}

/******************************************************************************88
The main c file that interfaces with java
*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "jnimain.h"


     
linked_list lltemp;
eclipse_list ecltemp;
int count =0;

JNIEXPORT jint JNICALL Java_Test_ROTManager_isEclipse
(JNIEnv *env, jobject obj, jstring jline1, jstring jline2, jlong junix, jint jTimeWindow){
     
     long unixtime = (long)junix;
     int timewindow = (jTimeWindow);

     eclipseResult eclipseRes = {0,0};

     const char *line2 = (*env)->GetStringUTFChars(env, jline2, 0);
     const char *line1 = (*env)->GetStringUTFChars(env, jline1, 0);

     (*env)->ReleaseStringUTFChars(env, jline1, line1);
     (*env)->ReleaseStringUTFChars(env, jline2, line2);


     isEclipse((char*)line1, (char*)line2, unixtime, timewindow, &eclipseRes, &ecltemp);

return 1;
}

JNIEXPORT jint JNICALL Java_Test_ROTManager_AosLosPredictor
  (JNIEnv *env, jobject obj, jstring jline1, jstring jline2, jlong junix,
     jdouble jlongitude, jdouble jlatitude, jdouble jaltitude, jfloat jminEl, jint jTimeWindow){


     atlas groundstation = {jlongitude*D2R, jlatitude*D2R, jaltitude*D2R};
     float minEl = jminEl;
     long unixtime = (long)junix;
     int timewindow = (jTimeWindow);

     aoslosResult aoslospredict = {0,0,0,0,0,0};

     const char *line2 = (*env)->GetStringUTFChars(env, jline2, 0);
     const char *line1 = (*env)->GetStringUTFChars(env, jline1, 0);

     (*env)->ReleaseStringUTFChars(env, jline1, line1);
     (*env)->ReleaseStringUTFChars(env, jline2, line2);

     aoslosPredict((char*)line1, (char*)line2, unixtime, timewindow, minEl, &groundstation, &aoslospredict, &lltemp);

return 1;
}


JNIEXPORT jint JNICALL Java_Test_ROTManager_EclipsePassCount
(JNIEnv *env, jobject obj){
     int eclipsePassNumber = eclipseListLength(&ecltemp);

     return (int)eclipsePassNumber;
}

JNIEXPORT jint JNICALL Java_Test_ROTManager_PassCount
     (JNIEnv *env, jobject obj){

     int VisiblePassNumber;
     VisiblePassNumber = ListLength(&lltemp);

     return VisiblePassNumber;
}


JNIEXPORT jobject JNICALL Java_Test_ROTManager_ReadEclipseSol
(JNIEnv *env, jobject eclipseObj){

     eclipseResult res = {0,0};
               
     jclass read_cls = (*env)->FindClass(env, "Test/eclipse");
     jfieldID fid;
     count++;
     
     getEclipseListData(&ecltemp, count, &res);

    if(read_cls == 0) return 0;

     eclipseObj = (*env)->AllocObject(env, read_cls);


     
     //set EclipseStartTime
     fid = (*env)->GetFieldID(env, read_cls, "eclipseStartTime", "I");
          (*env)->SetIntField(env, eclipseObj, fid, (int)res.eclipseStartTime);

     //set EclipseEndTime
     fid = (*env)->GetFieldID(env, read_cls, "eclipseEndTime", "I");
          (*env)->SetIntField(env, eclipseObj, fid, (int)res.eclipseEndTime);

     return (jobject) eclipseObj;


}



JNIEXPORT jobject JNICALL Java_Test_ROTManager_ReadAosLosSol
(JNIEnv *env, jobject obj){

     aoslosResult aoslospredict = {0,0,0,0,0,0};
               
     jclass read_cls = (*env)->FindClass(env, "Test/AosLos");
     jfieldID fid;
     count++;

     // to show the counter output so that I can determine the result;
     printf("\nCounter is now at %i " , count);

     getListData(&lltemp, count, &aoslospredict);

    if(read_cls == 0) return 0;

     obj = (*env)->AllocObject(env, read_cls);


     
     //set aostime
     fid = (*env)->GetFieldID(env, read_cls, "aostime", "I");
          (*env)->SetIntField(env, obj, fid, aoslospredict.aostime);

     //set lostime
     fid = (*env)->GetFieldID(env, read_cls, "lostime", "I");
          (*env)->SetIntField(env, obj, fid, aoslospredict.lostime);

     //set aosSunOrEclipse
     fid = (*env)->GetFieldID(env, read_cls, "aosSunOrEclipse", "I");
          (*env)->SetIntField(env, obj, fid, aoslospredict.AosSunOrEclipse);

     //set losSunOrEclipse
     fid = (*env)->GetFieldID(env, read_cls, "losSunOrEclipse", "I");
          (*env)->SetIntField(env, obj, fid, aoslospredict.LosSunOrEclipse);


     //set aosEl
     fid = (*env)->GetFieldID(env, read_cls, "aosel", "F");
          (*env)->SetFloatField(env, obj, fid, aoslospredict.aosEl);

     //set losEl
     fid = (*env)->GetFieldID(env, read_cls, "losel", "F");
          (*env)->SetFloatField(env, obj, fid, aoslospredict.losEl);

     //set aosAz
     fid = (*env)->GetFieldID(env, read_cls, "aosaz", "F");
          (*env)->SetFloatField(env, obj, fid, aoslospredict.aosAz);

     //set losAz
     fid = (*env)->GetFieldID(env, read_cls, "losaz", "F");
          (*env)->SetFloatField(env, obj, fid, aoslospredict.losAz);

     //set losAz
     fid = (*env)->GetFieldID(env, read_cls, "maxEl", "F");
          (*env)->SetFloatField(env, obj, fid, aoslospredict.maxEl);

     return (jobject) obj;


     }

}
/*******************************************************
The header file for it
*******************************************************/

#include <math.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include "tracklib.h"
//#include "AosLosPredict.h"
#include "sgp4.h"
#include "l2decode.h"
//#include "SubSatPredict.h"
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ROTManager */


// structure that will be returned by JNI, added 3/10/03
typedef struct  {int aostime;
                     int lostime;
                     int AosSunOrEclipse;
                     int LosSunOrEclipse;
                     float aosEl;
                     float losEl;
                     float aosAz;
                     float losAz;
                     float maxEl;} aoslosResult;

typedef struct  {int eclipseStartTime;
                     int eclipseEndTime;} eclipseResult;

typedef struct  EclipseNode {/*struct*/ eclipseResult result;
                                    int index;
                                    struct EclipseNode *next;} eclipseNode;

typedef struct Node {/*struct*/ aoslosResult result;
                          int index;
                          struct Node *next;} node;

typedef struct {/*struct*/ node *head;
                    /*struct*/ node *tail;} linked_list;

typedef struct {/*struct*/ eclipseNode *head;
                    /*struct*/ eclipseNode *tail; } eclipse_list;

short     GetPointing (long unix, double utc2ut1, j2000 *j,
                            double *z, attitude *e, atlas *szptn);

void isEclipse(char *line1, char *line2, long unixtime,
                  int timewindow, eclipseResult *res, eclipse_list *ecltemp);

int aosloscalc(long unixtime, int timewindow, float minElAngle,
                  atlas *groundstation, attitude *sat_attitude);

void aoslosPredict(char *line1, char *line2, long unixtime, int timewindow,
                       float minEl, atlas *groundstation, aoslosResult *res, linked_list *lltemp);

linked_list  *CreateList();
void           getListData(linked_list *ll, int i, aoslosResult *res);
int                ListLength(linked_list *ll);
void           ListTransverse(linked_list *ll);
void           ListAddNode(linked_list *ll ,aoslosResult aoslosRes, int index);
node           *CreateNode(aoslosResult aoslosRes, int index);
void           freeList(linked_list *ll);

eclipse_list *CreateEclipseList();
void           getEclipseListData(eclipse_list *ll, int i, eclipseResult *res);
int                eclipseListLength(eclipse_list *ll);
void           eclipseListTransverse(eclipse_list *ll);
void           eclipseListAddNode(eclipse_list *ll, eclipseResult result, int index);
eclipseNode  *CreateEclipseNode(eclipseResult result, int index);
void           freeEclipseList(eclipse_list *ll);

#ifndef TwoPI
#define TwoPI     (2.0*3.14159265358979)
#endif

#ifndef D2R
#define D2R          (TwoPI/360.0)
#endif

#ifndef R2D
#define     R2D          (360.0/TwoPI)
#endif



#ifndef _Included_ROTManager
#define _Included_ROTManager
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Test.ROTManager
 * Method:    AosLosPredictor
 * Signature: (Ljava/lang/String;Ljava/lang/String;JDDDFI)I
 */
JNIEXPORT jint JNICALL Java_Test_ROTManager_AosLosPredictor
  (JNIEnv *, jobject, jstring, jstring, jlong, jdouble, jdouble, jdouble, jfloat, jint);

/*
 * Class:     Test.ROTManager
 * Method:    isEclipse
 * Signature: (Ljava/lang/String;Ljava/lang/String;JI)I
 */
JNIEXPORT jint JNICALL Java_Test_ROTManager_isEclipse
  (JNIEnv *, jobject, jstring, jstring, jlong, jint);


/*
 * Class:     Test.ROTManager
 * Method:    ReadAosLosSol
 * Signature: ()LTest/AosLos;
 */
JNIEXPORT jobject JNICALL Java_Test_ROTManager_ReadAosLosSol
  (JNIEnv *, jobject );

/*
 * Class:     Test.ROTManager
 * Method:    ReadEclipseSol
 * Signature: ()LTest/eclipse;
 */
JNIEXPORT jobject JNICALL Java_Test_ROTManager_ReadEclipseSol
  (JNIEnv *, jobject );


/*
 * Class:     Test.ROTManager
 * Method:    ReadSubSatPointSol
 * Signature: ()LTest/SubSatPoint;
 */
JNIEXPORT jobject JNICALL Java_Test_ROTManager_ReadEclipseSol
  (JNIEnv *, jobject );

/*
 * Class:     Test.ROTManager
 * Method:    PassCount
 * Signature: ()I;
 */
JNIEXPORT jint JNICALL Java_Test_ROTManager_PassCount(JNIEnv *, jobject);

/*
 * Class:     Test.ROTManager
 * Method:    EclipsePassCount
 * Signature: ()I;
 */
JNIEXPORT jint JNICALL Java_Test_ROTManager_EclipsePassCount(JNIEnv *, jobject);


/*
 * Class:     Test.ROTManager
 * Method:    subSatPredict
 * Signature: (Ljava/lang/String;Ljava/lang/String;JDDD)I
 */
JNIEXPORT jobject JNICALL Java_Test_ROTManager_subSatPredict
  (JNIEnv *, jobject, jstring, jstring, jlong, jdouble, jdouble, jdouble);



#ifdef __cplusplus
}
#endif
#endif

/***************************************************
Function1 C code
***************************************************/

#include "jnimain.h"


void aoslosPredict(char *line1, char *line2, long unixtime, int timewindow,
                       float minEl, atlas *groundstation, aoslosResult *aoslospredict, linked_list *lltemp){

     double l2[10];
     linked_list *ll = NULL;
     double utc2ut1 = (double)0;
     j2000 z;
     double v[6];
     atlas subsatpos = {0, 0, 0};
     aoslosResult AosLosRes = {0,0,0,0,0,0};
     tracker poshorting = {0, 0, 0};
     char fmat3[] = "%d, %f, %f, %f, %f, %f, %f\n";
     double tsince = (double) 0.0;     // time elapsed in seconds since 2-line epoch
     int i = (long)0;
     int count1 = 0, count2 = 0, count3 = 0, index = 0;
     double Limit = 86400.00 * timewindow;
     float ElDeg;
     float AzDeg;
     double minElRad = (minEl * D2R);
     int aos, los;
     double mjd;
     double RSun[6];
     double mjdunixtemp;
     short eclipseDetector;
     double maxEl = 0 ;

     



     /* Converts TLE to array to be used in program*/
     l2_converter (line1, line2, l2);

     /* Calculates time elapsed since TLE epoch */
     mjd = l22mjd(l2[0]);
     mjdunixtemp = mjd2unix(mjd);
     tsince = ((double)unixtime - mjdunixtemp);

     /* computes mjd of the unixtime, to be used to calculate j2000 elements */
     mjd = unix2mjd(unixtime);
     
     /* computes j2000 elements*/
     rnpjmm(utc2ut1, mjd, &z);

     /* computes position and velocity vector in m and m/s */
     sgp4(tsince, l2, v);
     
     /* main function of the program, calculates AOS, LOS for given satellite and groundstation*/
     SatelliteTracker(unixtime+(long)i, utc2ut1, &z, v, groundstation, &subsatpos, &poshorting);

     /*creates list of solutions to be returned to Java*/
     ll = CreateList();
     
     /* tracks forward intime to find where AOS, LOS is */
     if (poshorting.elevation < minElRad ){

          for (i = 0; i< Limit; i++){
               mjd = unix2mjd(unixtime+(long)i);
         
               rnpjmm(utc2ut1, mjd, &z);

               sgp4(tsince+i, l2, v);
               
               SatelliteTracker(unixtime+(long)i, utc2ut1, &z, v, groundstation, &subsatpos, &poshorting);
               if(poshorting.elevation > maxEl && count3 == 0){
                         maxEl = poshorting.elevation;
                    }
               if ((poshorting.elevation > minElRad) && count1 ==0) {
                    aos = unixtime+i;
                    count1++;
                    count2 =1;
                    ElDeg = (float)(poshorting.elevation * R2D);
                    AzDeg = (float)(poshorting.azimuth * R2D);
                    sun(mjd, RSun);
                    eclipseDetector = eclipse(v, RSun);

                    AosLosRes.AosSunOrEclipse = eclipseDetector;
                    AosLosRes.aostime = aos;
                    AosLosRes.aosAz = AzDeg;
                    AosLosRes.aosEl = ElDeg;              
               }

               if ((poshorting.elevation < minElRad) && count2 ==1) {
                    los = unixtime+i;
                    count2 ++;
                    count1 = 0;
                    index++;
                    ElDeg = (float)(poshorting.elevation * R2D);
                    AzDeg = (float)(poshorting.azimuth * R2D);
                    sun(mjd, RSun);
                    eclipseDetector = eclipse(v, RSun);

                    AosLosRes.LosSunOrEclipse = eclipseDetector;
                    AosLosRes.lostime = los;
                    AosLosRes.losAz = AzDeg;
                    AosLosRes.losEl = ElDeg;
                    AosLosRes.maxEl = (float)(maxEl*R2D);
                    maxEl = 0;
                    count3 = 0;
                   
                    /* Add aoslosResult to list */
                    ListAddNode(ll, AosLosRes, index);
               }
          }
     }


     else if ( poshorting.elevation > minElRad){

          for (i = 0; i< Limit; i++){
         
               mjd = unix2mjd(unixtime+(long)i);
               
               rnpjmm(utc2ut1, mjd, &z);

               sgp4(tsince+i, l2, v);
               
               SatelliteTracker(unixtime+(long)i, utc2ut1, &z, v, groundstation, &subsatpos, &poshorting);
               if     (poshorting.elevation > maxEl && count3 == 0){
                    maxEl = poshorting.elevation;
               }
               if     ((poshorting.elevation > minElRad) && count1 ==0) {
                    aos = unixtime+i;
                    count1++;
                    count2 =1;
                    ElDeg = (float)(poshorting.elevation * R2D);
                    AzDeg = (float)(poshorting.azimuth * R2D);
                    sun(mjd, RSun);
                    eclipseDetector = eclipse(v, RSun);

                    AosLosRes.AosSunOrEclipse = eclipseDetector;
                    AosLosRes.aostime = aos;
                    AosLosRes.aosAz = AzDeg;
                    AosLosRes.aosEl = ElDeg;
                   
               }

               
               if ((poshorting.elevation < minElRad) && count2 ==1) {
                    los = unixtime+i;
                    count2 ++;
                    count1 = 0;
                    index++;
                    ElDeg = (float)(poshorting.elevation * R2D);
                    AzDeg = (float)(poshorting.azimuth * R2D);
                    sun(mjd, RSun);
                    eclipseDetector = eclipse(v, RSun);

                    AosLosRes.LosSunOrEclipse = eclipseDetector;
                    AosLosRes.lostime = los;
                    AosLosRes.losAz = AzDeg;
                    AosLosRes.losEl = ElDeg;
                    AosLosRes.maxEl = (float)(maxEl*R2D);
                    maxEl = 0;
                    count3 = 0;
               
                    /* Add aoslosResult to list */
                    ListAddNode(ll, AosLosRes, index);

               }              
          }
     }
     /* pointer to the head of the linked list to be returned */
     lltemp->head =  ll->head;
    free(ll);


}

/********************************************************88
function2 C code
********************************************************/

#include "jnimain.h"

void isEclipse(char *line1, char *line2, long unixtime, int timewindow,
                  eclipseResult *res, eclipse_list *ecltemp)
{
     eclipse_list *eclipsell = NULL;
     eclipseResult EclipseRes = {0,0};
     double l2[10];
     double utc2ut1 = 0;
     double v[6];
     double tsince;
     int i = (long)0, count1 = 0, count2 = 0;
     double Limit = 10000;//86400 * timewindow;
     int index = 0;
     double mjd;
     short eclipseDetector;
     double RSun[6];


     l2_converter (line1, line2, l2);
     tsince = ((double)unixtime - mjd2unix(l22mjd(l2[0])));
     mjd = unix2mjd(unixtime);
     sgp4(tsince, l2, v);
     sun(mjd, RSun);

     eclipseDetector = eclipse(v, RSun);
     
     /*creates list of solutions to be returned to Java*/
     eclipsell = CreateEclipseList();

     if (eclipseDetector == 1){
          for (i = 0; i< Limit; i++){

               mjd = unix2mjd(unixtime+(long)i);
               sgp4(tsince+i, l2, v);
               sun(mjd, RSun);
               eclipseDetector = eclipse(v, RSun);                        
               if ((eclipseDetector ==1) && count1 ==0 ) {
                    count1++;
                    count2 =1;
                    EclipseRes.eclipseStartTime = unixtime+i;

               }
               if ((eclipseDetector ==0) && count2 == 1 ) {
                    index++;
                    count2 ++;
                    count1 = 0;
               
                    EclipseRes.eclipseEndTime = unixtime+i;
                         
                    eclipseListAddNode(eclipsell, EclipseRes, index);
                   
               }
          }
     }

     ecltemp->head =  eclipsell->head;
   
//     free(eclipsell);

}


/*************************************************************
java Main() Function
************************************************************/

package Test;

import java.util.*;
import java.text.*;
import java.math.*;

public class Wrapper {
   
   
    static {  System.loadLibrary("MPS_TrackLib"); }
   
    public static void main(String args[]) {
       
        long unix = 1066767060;
         
        int searchTimeWindow = 1;
        float minElevationAngle = 0;
        double gs_long = 0.588, gs_lat = 51.2431, gs_alt = 70;
        double roll = 0, pitch = 0, yaw = 0;

        AosLosList passlist =  new AosLosList();
        ROTManager alp = new ROTManager();

/*Function1*/
        alp.EclipsePass("UK-DMC", unix, searchTimeWindow);

/* Function2*/
       passlist = alp.AosLosPasses("UK-DMC", "Surrey", unix, gs_long, gs_lat, gs_alt, minElevationAngle, searchTimeWindow );

/*Function3, uses jni, but does not return linkedlist object*/
       alp.subSatPointTracker("UK-DMC", unix, roll, pitch, yaw);


/***************************************************************************
Java function where the native methods are defined
***************************************************************************/


package Test;

import java.util.*;
import java.text.*;
import java.sql.*;

public class ROTManager {
    static {  System.loadLibrary("MPS_TrackLib"); }
   
   
    private native AosLos ReadAosLosSol();
    private native eclipse ReadEclipseSol();
    private native int EclipsePassCount();
    private native int PassCount();
    private native int isEclipse(String line1, String line2, long unix, int searchTimeWindow);
    private native int AosLosPredictor(String line1, String line2, long unix, double gs_long,
    double gs_lat, double gs_alt, float minElevationAngle, int searchTimeWindow);
    private native SubSatPoint subSatPredict(String line1, String line2, long unix, double gs_long,
    double gs_lat, double gs_alt);
   
   
   
    private SubSatPoint     sspoint;
    private AosLosList     AosLosSolList;
    private eclipseList    eclipseSolList;
    private int result, count = 0, visiblePasses = 0, eclipsePasses = 0;
   
    String url = "jdbc:odbc:EPHEMERIS";
    Connection con;
    java.sql.Statement stmt;
    java.sql.Statement statement;
    ResultSet rsdb;
    ResultSet rs;
   
    public ROTManager() {
        sspoint = new SubSatPoint();
    }
   
   
    public KEPS getLatestKepsData(String _SatName, long _Time){
        long time = _Time;
        String SatName = _SatName;
        long result = -1;
        String line1 = "", line2 = "";
        String SatelliteName = "";
        int ObjectNr = 0;
        KEPS kp = new KEPS();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }
        try {
            con = DriverManager.getConnection(url, "root", "");
            stmt = con.createStatement();
            statement = con.createStatement();
            rs = stmt.executeQuery("SELECT ObjectNr, SatelliteName FROM satindex WHERE " +
            "(SatelliteName = '"+SatName+"') Order by ObjectNr ASC" );
           
            while (rs.next()) {
                ObjectNr = rs.getInt("ObjectNr");
                SatelliteName = rs.getString("SatelliteName");
                String dbTableTitle = "tle" + ObjectNr;
                System.out.println("\n"+ SatelliteName +" is located in table " + dbTableTitle);
                rsdb = statement.executeQuery("SELECT Line1, Line2, ABS(UNIX_TIMESTAMP(EpochTime)" +
                " - ' "+ time + "') As timeDiff FROM tle"+ObjectNr+" ORDER BY timeDiff ASC");
               
                for (int i=0; i<1;i++){
                    if (rsdb.next() && i==0){
                        line1 = rsdb.getString("Line1");
                        System.out.println("Line 1 is " + line1);
                        line2 = rsdb.getString("Line2");
                        System.out.println("Line 2 is " + line2);
                        kp = StringToKeps(result, line1, line2);
                        System.out.println("Epoch inside KEPS is " +kp.getEpoch());
                    }
                }
            }
        } catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }
       
       
        return kp;
    }
   
    public KEPS StringToKeps(long _epoch, String _line1, String _line2){
        KEPS kep = new KEPS();
        String jline1 = _line1;
        String jline2 = _line2;
       
        if (jline1.length() == 0){
            System.out.println("The database contains an empty Line1");
        }
        else if(jline1.charAt(21) == ' '){
            jline1 = jline1.substring(0, 21) + "0" + jline1.substring(22);
            System.out.println(" Character 21 was " +jline1);
        }
        if(jline1.charAt(22) == ' '){
            jline1 = jline1.substring(0, 22) + "0" + jline1.substring(23);
            System.out.println(" Character 22 was " +jline1);
        }
        if (jline1.charAt(23) == ' '){
            jline1 = jline1.substring(0, 23) + "0" + jline1.substring(24);
            System.out.println(" Character 23 was " +jline1);
        }
       
        kep.setSatID(jline1.substring(2, 7));
        kep.setEpoch(Double.parseDouble(jline1.substring(18, 32)));
        kep.setDmm(Double.parseDouble(jline1.substring(33, 43)));
        kep.setDdmm(Double.parseDouble(jline1.charAt(44) + "." + jline1.substring(45, 50)) *
        Math.pow(10d, Double.parseDouble(jline1.substring(50,52))) );
        kep.setIncl(Double.parseDouble(jline2.substring(8, 16)));
        kep.setRaan(Double.parseDouble(jline2.substring(17, 25)));
        kep.setEccn(Double.parseDouble("." + jline2.substring(26, 33)));
        kep.setArgp(Double.parseDouble(jline2.substring(34, 42)));
        kep.setMa(Double.parseDouble(jline2.substring(43, 51)));
        kep.setMm(Double.parseDouble(jline2.substring(52, 63)));
        kep.setTLE_Line1(jline1);
        kep.setTLE_Line2(jline2);
        if (jline1.charAt(53) == ' '){kep.setBstar(0);}
        else{kep.setBstar(Double.parseDouble(jline1.charAt(53) + "." + jline1.substring(54, 59)) *
        Math.pow(10d, Double.parseDouble(jline1.substring(59,61))) ); }
       
        return kep;
    }
   
    public AosLosList AosLosPasses(String _satName, String _gsnName, long unix, double gs_long, double gs_lat,
    double gs_alt, float minElevationAngle, int searchTimeWindow) {
       
        KEPS k = new KEPS();
        k = getLatestKepsData(_satName, unix);
        DBHandler dbh = new DBHandler();
        AosLosSolList = new AosLosList();
        AosLos AosLosResult = new AosLos();          // IPSTRUCT DATA set by the user.
        result = (int) AosLosPredictor(k.getTLE_Line1(), k.getTLE_Line2(), unix, gs_long, gs_lat, gs_alt, minElevationAngle, searchTimeWindow);
        visiblePasses = (int) PassCount();
       
        System.out.println("\nPasses counter has found " +visiblePasses +" visible Passes");
       
        for(count = 0; count < visiblePasses; count++){
            AosLosResult = (AosLos) ReadAosLosSol();
            AosLosResult.setSatelliteName(_satName);
            AosLosResult.setGroundStationName(_gsnName);
            AosLosSolList.setAosLosData(AosLosResult);
            dbh.addAosLosToDB(AosLosResult);
           
           
            System.out.println("\nAosTime = " + AosLosResult.getAosTimeString() +
            " AosEl = " + AosLosResult.getAosEl() +
            " AosAz = " + AosLosResult.getAosAz() +
            " AosSun/Eclipse = " +AosLosResult.getAosSunOrEclipse() +
            " LosTime = " + AosLosResult.getLosTimeString() +
            " LosEl = " + AosLosResult.getLosEl() +
            " LosAz = " + AosLosResult.getLosAz() +
            " AosSun/Eclipse = " +AosLosResult.getLosSunOrEclipse() +
            " MaxElevation = " +AosLosResult.getMaxEl());
           
           
        }
       
        return (AosLosList)AosLosSolList;
    }
   
    public int EclipsePass(String _satName, long unix, int searchTimeWindow) {
       
        KEPS k = new KEPS();
        k = getLatestKepsData(_satName, unix);
        DBHandler dbh = new DBHandler();
        eclipse eclipseResult = new eclipse();          // IPSTRUCT DATA set by the user.
        eclipse eclipseResultTemp;
        eclipseSolList = new eclipseList();
        result = (int) isEclipse(k.getTLE_Line1(), k.getTLE_Line2(), unix, searchTimeWindow);
        eclipsePasses = (int) EclipsePassCount();
        System.out.println("\n Result is " + result);
        System.out.println("\nPasses counter has found " +eclipsePasses +" eclipse Passes");
       
        for(count =0; count < eclipsePasses; count++){
            eclipseResult = (eclipse) ReadEclipseSol();
            eclipseSolList.setEclipseData(eclipseResult);
            dbh.addEclipseToDB(eclipseResult);
           
            if ( count > 0){
                eclipseResultTemp = (eclipse) eclipseSolList.getEclipseData(count-1);
                dbh.addDaylightToDB(eclipseResultTemp , eclipseResult);
                System.out.println("\n DaylightStartTime = " + eclipseResultTemp.getEclipseEndTimeString() +
                " DaylightEndTime = " + eclipseResult.getEclipseStartTimeString());
            }
            System.out.println("\n EclipseStartTime = " + eclipseResult.getEclipseStartTimeString() +
            " EclipseEndTime = " +eclipseResult.getEclipseEndTimeString());
           
        }
       
        return 1;
    }
   
    public int subSatPointTracker(String _satName, long unix, double roll,
    double pitch, double yaw) {
        KEPS k = new KEPS();
        k = getLatestKepsData(_satName, unix);
        DBHandler dbh = new DBHandler();
        sspoint = subSatPredict(k.getTLE_Line1(), k.getTLE_Line2(), unix, roll, pitch, yaw);
        sspoint.setSatelliteName(_satName);
        System.out.println("sunsatPoint unix " + sspoint.getUnixtime() +
        "\nLongitude " + sspoint.getLongitude() +
        "\nLat "  +sspoint.getLatitude());
        return 1;
    }
    public SubSatPoint getSubSatPoint(){ return sspoint; }
    public void setSubSatPoint(SubSatPoint jSubSatPoint){ sspoint = jSubSatPoint; }
   
}
0
 
LVL 1

Author Comment

by:Havalchy
Comment Utility
Opps, found the error,

it turns out that I was using a counter in the for loop to call the objects from the c code, and I did not reset the counter after calling in in the first function.

Sorry to give you such a headache,
0
 
LVL 6

Expert Comment

by:Ajar
Comment Utility
Havalchy ,
     
    Hey you dont need to be that formal here at experts exchange ... we are here to discover problems ,solve them to our mutual benefit.


Good day .
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now