Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 7ce1fc70de8bf8ea52b8eb9a57de668b89efc018 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*******************************************************************************
 * Copyright (c) 2008 Radoslav Gerganov
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Radoslav Gerganov - initial API and implementation
 *******************************************************************************/

#include <WinSock2.h>
#include <rapi2.h>

#include "org_eclipse_tm_internal_rapi_RapiDevice.h"

#define RAPI_NATIVE(func) Java_org_eclipse_tm_internal_rapi_RapiDevice_##func

struct RAPI_DEVICEINFO_FID_CACHE
{
	int cached;
	jclass clazz;
	jfieldID id, versionMajor, versionMinor, name, platform;
};

struct RAPI_CONNECTIONINFO_FID_CACHE
{
	int cached;
	jclass clazz;
	jfieldID connectionType;
};

RAPI_DEVICEINFO_FID_CACHE RAPI_DEVICEINFOFc;
RAPI_CONNECTIONINFO_FID_CACHE RAPI_CONNECTIONINFOFc;

JNIEXPORT jint JNICALL RAPI_NATIVE(CreateSession)
  (JNIEnv *env, jobject that, jint arg0, jintArray arg1)
{
	jint rc = E_FAIL;
	jint *lparg1 = NULL;

	if (arg0 == 0) return rc;
	if (arg1) {
		lparg1 = env->GetIntArrayElements(arg1, NULL);
		if (lparg1 == NULL) goto fail;
	}
	IRAPIDevice *pDevice = (IRAPIDevice*) arg0;
	rc = pDevice->CreateSession((IRAPISession**)lparg1);
fail:
	if (arg1 && lparg1) env->ReleaseIntArrayElements(arg1, lparg1, 0);
	return rc;
}

void cacheRAPI_DEVICEINFOFields(JNIEnv *env, jobject lpObject)
{
	if (RAPI_DEVICEINFOFc.cached) return;
	RAPI_DEVICEINFOFc.clazz = env->GetObjectClass(lpObject);
	RAPI_DEVICEINFOFc.id = env->GetFieldID(RAPI_DEVICEINFOFc.clazz, "id", "Ljava/lang/String;");
	RAPI_DEVICEINFOFc.versionMajor = env->GetFieldID(RAPI_DEVICEINFOFc.clazz, "versionMajor", "I");
	RAPI_DEVICEINFOFc.versionMinor = env->GetFieldID(RAPI_DEVICEINFOFc.clazz, "versionMinor", "I");
	RAPI_DEVICEINFOFc.name = env->GetFieldID(RAPI_DEVICEINFOFc.clazz, "name", "Ljava/lang/String;");
	RAPI_DEVICEINFOFc.platform = env->GetFieldID(RAPI_DEVICEINFOFc.clazz, "platform", "Ljava/lang/String;");
	RAPI_DEVICEINFOFc.cached = 1;
}

void cacheRAPI_CONNECTIONINFOFields(JNIEnv *env, jobject lpObject)
{
	if (RAPI_CONNECTIONINFOFc.cached) return;
	RAPI_CONNECTIONINFOFc.clazz = env->GetObjectClass(lpObject);
	RAPI_CONNECTIONINFOFc.connectionType = env->GetFieldID(RAPI_CONNECTIONINFOFc.clazz, "connectionType", "I");
	RAPI_CONNECTIONINFOFc.cached = 1;
}

void setRAPI_DEVICEINFOFields(JNIEnv *env, jobject lpObject, RAPI_DEVICEINFO *pDeviceInfo)
{
	if (!RAPI_DEVICEINFOFc.cached) cacheRAPI_DEVICEINFOFields(env, lpObject);
	WCHAR *pwszGUID = NULL;
	if (StringFromCLSID(pDeviceInfo->DeviceId, &pwszGUID) == S_OK) {
		int idLen = lstrlen(pwszGUID);
		jstring id = env->NewString((const jchar*)pwszGUID, idLen);
		env->SetObjectField(lpObject, RAPI_DEVICEINFOFc.id, id);
		CoTaskMemFree(pwszGUID);
	}
	env->SetIntField(lpObject, RAPI_DEVICEINFOFc.versionMajor, (jint)pDeviceInfo->dwOsVersionMajor);
	env->SetIntField(lpObject, RAPI_DEVICEINFOFc.versionMinor, (jint)pDeviceInfo->dwOsVersionMinor);
	// create new String from the native BSTR
	UINT nameLen = SysStringLen(pDeviceInfo->bstrName);
	jstring name = env->NewString((const jchar*)pDeviceInfo->bstrName, nameLen);
	env->SetObjectField(lpObject, RAPI_DEVICEINFOFc.name, name);
	// create new String from the native BSTR
	UINT platformLen = SysStringLen(pDeviceInfo->bstrPlatform);
	jstring platform = env->NewString((const jchar*)pDeviceInfo->bstrPlatform, platformLen);
	env->SetObjectField(lpObject, RAPI_DEVICEINFOFc.platform, platform);
}

void setRAPI_CONNECTIONINFOFields(JNIEnv *env, jobject lpObject, RAPI_CONNECTIONINFO *pConnectionInfo)
{
	if (!RAPI_CONNECTIONINFOFc.cached) cacheRAPI_CONNECTIONINFOFields(env, lpObject);
	env->SetIntField(lpObject, RAPI_CONNECTIONINFOFc.connectionType, (jint)pConnectionInfo->connectionType);
}

JNIEXPORT jint JNICALL RAPI_NATIVE(GetDeviceInfo)
  (JNIEnv *env, jobject that, jint arg0, jobject arg1)
{
	jint rc = E_FAIL;
	RAPI_DEVICEINFO deviceInfo = {0};

	if (arg0 == 0) return rc;
	IRAPIDevice *pDevice = (IRAPIDevice*) arg0;
	rc = pDevice->GetDeviceInfo(&deviceInfo);
	if (rc != NOERROR) goto fail;
	if (arg1) setRAPI_DEVICEINFOFields(env, arg1, &deviceInfo);
fail:
	FreeDeviceInfoData(&deviceInfo);
	return rc;
}

JNIEXPORT jint JNICALL RAPI_NATIVE(GetConnectionInfo)
  (JNIEnv *env, jobject that, jint arg0, jobject arg1)
{
	jint rc = E_FAIL;
	RAPI_CONNECTIONINFO connectionInfo = {0};

	if (arg0 == 0) return rc;
	IRAPIDevice *pDevice = (IRAPIDevice*) arg0;
	rc = pDevice->GetConnectionInfo(&connectionInfo);
	if (rc != NOERROR) goto fail;
	if (arg1) setRAPI_CONNECTIONINFOFields(env, arg1, &connectionInfo);
fail:
	return rc;
}

JNIEXPORT jint JNICALL RAPI_NATIVE(GetConnectStat)
  (JNIEnv *env, jobject that, jint arg0, jintArray arg1)
{
	jint rc = E_FAIL;
	jint *lparg1 = NULL;
	RAPI_DEVICESTATUS devStatus;

	if (arg0 == 0) return rc;
	if (arg1) {
		lparg1 = env->GetIntArrayElements(arg1, NULL);
		if (lparg1 == NULL) goto fail;
	}
	IRAPIDevice *pDevice = (IRAPIDevice*) arg0;
	rc = pDevice->GetConnectStat(&devStatus);
	lparg1[0] = (int)devStatus;
fail:
	if (arg1 && lparg1) env->ReleaseIntArrayElements(arg1, lparg1, 0);
	return rc;
}

Back to the top