#include "com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK.h"
#include "general.h"
typedef struct CBData
{
CBData()
{
m_bInit = false;
m_pEnv = NULL;
}
bool m_bInit;
JNIEnv* m_pEnv;
jobject m_objStatusCallBack;
jobject m_objDataCallBack;
int m_nStatusContext;
int m_nDataContext;
jmethodID m_jStatusCBId; //\u72B6\u6001\u56DE\u8C03\u51FD\u6570ID
jmethodID m_jDatasCBId;
JavaVM* m_pJavaVm;
}CBData;
CBData g_cbData;
int AppChannelStatusChange(int lAppChannelID, int lServerHandle, int iChannelStatus, char *pChannelInfo, int iInfoLen, int iContext)
{
#ifdef WIN32
printf("#######AppChanID%d iServerHandle:%d channelstatus: %d %s**************\n", lAppChannelID, lServerHandle, iChannelStatus, pChannelInfo);
#endif
JNIEnv *pJniEnv;
g_cbData.m_pJavaVm->GetEnv((void **)&pJniEnv, 0x00010006);
g_cbData.m_pJavaVm->AttachCurrentThread((void**)&pJniEnv, NULL);
jstring jstrChannelInfo;
if (iChannelStatus == 1)
{
jstrChannelInfo = pJniEnv->NewStringUTF(pChannelInfo);
}
else
{
jstrChannelInfo = pJniEnv->NewStringUTF("");
}
pJniEnv->CallVoidMethod(g_cbData.m_objStatusCallBack, g_cbData.m_jStatusCBId, lAppChannelID, lServerHandle, iChannelStatus, jstrChannelInfo, 200, iContext);
pJniEnv->DeleteLocalRef(jstrChannelInfo);
g_cbData.m_pJavaVm->DetachCurrentThread();
return 0;
}
int DataRecv(int lAppChannelID, char *pszDeviceID, int iMsgType, int iSeqID, void *pXmlData, int iXmlDataLen, void *pPicData, int iPicDataLen, int iConnIndex)
{
#ifdef WIN32
printf("**************AppChanID%d deviceId:%s msgtype %d**************\n", lAppChannelID, pszDeviceID, iMsgType);
#endif
JNIEnv *pJniEnv;
g_cbData.m_pJavaVm->GetEnv((void **)&pJniEnv, 0x00010006); //JavaVM* m_pJavaVm; //在其他有JNIEnv指针的地方调用 pEnv->GetJavaVM(&g_cbData.m_pJavaVm);初始化JavaVM
g_cbData.m_pJavaVm->AttachCurrentThread((void**)&pJniEnv, NULL);
jstring jstrDeviceID = pJniEnv->NewStringUTF(pszDeviceID);
jbyte *cXml = (jbyte*)pXmlData;
jbyteArray chXmlData = pJniEnv->NewByteArray(iXmlDataLen);
pJniEnv->SetByteArrayRegion(chXmlData, 0, iXmlDataLen, cXml);
pJniEnv->CallVoidMethod(g_cbData.m_objDataCallBack,
g_cbData.m_jDatasCBId,
lAppChannelID,
jstrDeviceID,
iMsgType,
iSeqID,
chXmlData,
iXmlDataLen,
NULL,
0,
iConnIndex);
pJniEnv->DeleteLocalRef(jstrDeviceID);
pJniEnv->DeleteLocalRef(chXmlData);
g_cbData.m_pJavaVm->DetachCurrentThread();
return 0;
}
/*
* Class: com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK
* Method: CommStackInit
* Signature: (Ljava/lang/String;III)I
*/
JNIEXPORT jint JNICALL Java_com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK_CommStackInit
(JNIEnv *pJv, jclass Jc, jstring JstrDeviceId, jint nModuleType, jint nRecvThreadNum, jint nMaxLogFileNum)
{
#ifdef WIN32
printf("*****nModuleType = \n");
printf("*****nModuleType = %d\n", nModuleType);
#endif
s8* pszLocalDeviceID = NULL;
pszLocalDeviceID = jstringTostring(pJv, JstrDeviceId);
if ( NULL == pszLocalDeviceID )
{
return JNI_FALSE;
}
return IVHS_CommStackInit(pszLocalDeviceID, nModuleType, nRecvThreadNum, nMaxLogFileNum);
return 0;
}
/*
* Class: com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK
* Method: StartService
* Signature: (II)J
*/
JNIEXPORT jint JNICALL Java_com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK_StartService
(JNIEnv *, jclass, jint nListenPort, jint nModuleType)
{
return IVHS_StartService(nListenPort,nModuleType);
}
/*
* Class: com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK
* Method: ConnToServer
* Signature: (Ljava/lang/String;IIII)J
*/
JNIEXPORT jint JNICALL Java_com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK_ConnToServer
(JNIEnv *pJv, jclass Jc, jstring JstrSerIp, jint nPort, jint nMouleType, jint nSocketNum, jint nChannelIndex)
{
s8* pszSerIp = jstringTostring(pJv,JstrSerIp);
if ( NULL == pszSerIp )
{
return JNI_FALSE;
}
return IVHS_ConnToServer(pszSerIp,nPort,nMouleType,nSocketNum,nChannelIndex);
}
/*
* Class: com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK
* Method: SendDataByAppChannel
* Signature: (JIILjava/lang/String;I[BII)I
*/
JNIEXPORT jint JNICALL Java_com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK_SendDataByAppChannel
(JNIEnv *pJv, jclass Jc, jint dwChId, jint nMsgType, jint nSeqId, jbyteArray chXmlData,
jint nDataLen, jbyteArray chPicData, jint nPicDateLen, jint nConnIndex)
{
#ifdef WIN32
printf("%%%%%%%%%%%%%%%%%%%%%%%SendDataByAppChannel%%%%%%%%%%%%%%%%%%%%\n");
#endif
jbyte *pbyXmlData = pJv->GetByteArrayElements(chXmlData, 0);
int iSendRes = IVHS_SendDataByAppChannel(dwChId, nMsgType, nSeqId, (char *)pbyXmlData, nDataLen, NULL, 0, nConnIndex);
pJv->ReleaseByteArrayElements(chXmlData, pbyXmlData, 0);
return iSendRes;
}
/*
* Class: com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK
* Method: SendDataByAppChannelSync
* Signature: (JIILjava/lang/String;I[BII)Ljava/lang/String;
*/
JNIEXPORT jint JNICALL Java_com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK_SendDataByAppChannelSync
(JNIEnv * pJv, jclass Jc, jint dwChId, jint nMsgType, jint nSeqId, jbyteArray chXmlData,
jint nDataLen, jbyteArray chPicData, jint nPicDateLen, jint nConnIndex, jbyteArray chResInfo)
{
#ifdef WIN32
printf("%%%%%%%%%%%%%%%%%%%%%%% SendDataByAppChannelSync %%%%%%%%%%%%%%%%%%%%\n");
#endif
#if 0
int iResXmlLen = 0;
jbyte *pbyXmlData = pJv->GetByteArrayElements(chXmlData, 0);
jbyte *pbyPicData = pJv->GetByteArrayElements(chPicData, 0);
s8* pResXml = IVHS_SendDataByAppChannelSyncWithLen(dwChId, nMsgType, nSeqId, (char*)pbyXmlData , nDataLen, pbyPicData, nPicDateLen, &iResXmlLen, nConnIndex);
jbyte *cResXml = (jbyte*)pResXml;
pJv->SetByteArrayRegion(chResInfo, 0, iResXmlLen, cResXml);
pJv->ReleaseByteArrayElements(chXmlData, pbyXmlData, 0);
pJv->ReleaseByteArrayElements(chPicData, pbyPicData, 0);
return iResXmlLen;
#endif
return 0;
}
/*
* Class: com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK
* Method: StopServerbyHandle
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK_StopServerbyHandle
(JNIEnv *, jclass, jint);
/*
* Class: com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK
* Method: CloseAppChannel
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK_CloseAppChannel
(JNIEnv *, jclass, jint);
/*
* Class: com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK
* Method: CommStackRelease
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK_CommStackRelease
(JNIEnv *, jclass);
/*
* Class: com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK
* Method: RegAppChannelStatusChangeCBFun
* Signature: (Lcom/kedacom/kdkkdevmgr/commonStatck/CommonStatckSDK/AppChannelStatusChangeCBFun;I)I
*/
JNIEXPORT jint JNICALL Java_com_kedacom_kdkkdevmgr_commonStatck_CommonStatckSDK_RegAppChannelStatusChangeCBFun
(JNIEnv *pJv, jclass Jc, jobject statusCB, jint nStatusContext)
{
#ifdef WIN32
printf("begin to reg status cb\n");
#endif
if (g_cbData.m_bInit == false)
{
g_cbData.m_pEnv = pJv;
g_cbData.m_pEnv->GetJavaVM(&g_cbData.m_pJavaVm);
g_cbData.m_bInit = true;
}
g_cbData.m_objStatusCallBack = pJv->NewGlobalRef(statusCB);
g_cbData.m_nStatusContext = nStatusContext;
jclass cls;
cls = g_cbData.m_pEnv->GetObjectClass(statusCB);
if (0 == cls)
{
printf("class 0\n");
}
g_cbData.m_jStatusCBId = g_cbData.m_pEnv->GetMethodID(cls, "apply", "(IIILjava/lang/String;II)V");
if (g_cbData.m_jStatusCBId == NULL)
{
printf("jmidProcess = NULL\n");
return -2;
}
else
{
printf("get jmidProcess apply sucess.\n");
}
//int i = 10000;
//jstring jstrChannelID = pJv->NewStr