39 #define __FILE_ID__ "yjni" 55 jint throwYAPI_Exception( JNIEnv *env,
char *message )
58 char *className =
"com/yoctopuce/YoctoAPI/YAPI_Exception" ;
59 dbglog(
"Exception:%s\n", message);
60 exClass = (*env)->FindClass( env, className );
61 return (*env)->ThrowNew( env, exClass, message );
76 return (*env)->NewStringUTF(env, version);
92 throwYAPI_Exception(env, errmsg);
124 throwYAPI_Exception(env, errmsg);
127 if (res == fullsize) {
128 return (*env)->NewStringUTF(env, buffer);
132 memset(p, 0, fullsize+1);
136 throwYAPI_Exception(env, errmsg);
139 result = (*env)->NewStringUTF(env, buffer);
145 static jobject allocWPEntry(JNIEnv *env,
yDeviceSt *infos)
152 jstring serialNumber;
154 jmethodID constructor;
157 jclass cls = (*env)->FindClass(env,
"com/yoctopuce/YoctoAPI/WPEntry");
159 throwYAPI_Exception(env,
"Unable to find class WPEntry");
163 constructor = (*env)->GetMethodID(env, cls,
"<init>",
"(Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;ILjava/lang/String;)V");
164 if (constructor == 0) {
165 throwYAPI_Exception(env,
"Unable to find constructor for WPEntry");
169 logicalName = (*env)->NewStringUTF(env, infos->
logicalname);
170 productName = (*env)->NewStringUTF(env, infos->
productname);
171 networkUrl = (*env)->NewStringUTF(env,
"");
172 serialNumber = (*env)->NewStringUTF(env, infos->
serial);
176 res = (*env)->NewObject(env, cls, constructor, logicalName, productName, productId, networkUrl, beacon, serialNumber);
183 static jobject allocYPEntry(JNIEnv *env,
const char *classname,
const char *serial,
const char *funcId,
const char *logicalName,
const char *advertisedValue,
int baseType,
int funIdx)
188 jstring j_logicalName;
189 jstring j_advertisedValue;
190 jmethodID constructor;
192 jclass cls = (*env)->FindClass(env,
"com/yoctopuce/YoctoAPI/YPEntry");
194 throwYAPI_Exception(env,
"Unable to find class WPEntry");
197 constructor = (*env)->GetMethodID(env, cls,
"<init>",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;II)V");
198 if (constructor == 0) {
199 throwYAPI_Exception(env,
"Unable to find constructor for YPEntry");
202 j_classname = (*env)->NewStringUTF(env, classname);
203 j_serial = (*env)->NewStringUTF(env, serial);
204 j_funcId = (*env)->NewStringUTF(env, funcId);
205 j_logicalName = (*env)->NewStringUTF(env, logicalName);
206 j_advertisedValue = (*env)->NewStringUTF(env, advertisedValue);
207 return (*env)->NewObject(env, cls, constructor, j_classname, j_serial, j_funcId, j_logicalName, j_advertisedValue, baseType, funIdx);
222 int nbdev, buffsize, i;
225 jobject wpEntry, ypEntry;
226 jclass arrayList_class;
227 jmethodID arrayList_add;
230 throwYAPI_Exception(env, errmsg);
236 throwYAPI_Exception(env, errmsg);
242 arrayList_class = (*env)->FindClass(env,
"java/util/ArrayList");
243 if (arrayList_class == 0) {
244 throwYAPI_Exception(env,
"Unable to find class ArrayList");
248 arrayList_add = (*env)->GetMethodID(env, arrayList_class,
"add",
"(Ljava/lang/Object;)Z");
249 if (arrayList_add == 0) {
250 throwYAPI_Exception(env,
"Unable to find add method of ArrayList");
255 for (i = 0 ; i < nbdev; i++, dev_ptr++) {
258 throwYAPI_Exception(env, errmsg);
261 wpEntry = allocWPEntry(env, &dev_info);
262 if (wpEntry ==NULL) {
265 (*env)->CallBooleanMethod(env, wpArray, arrayList_add, wpEntry);
276 if (
YSTRCMP(categname,
"Module")==0){
282 yStrRef serial, funcId, funcName;
287 yidx =
ypGetAttributes(entry, &serial, &funcId, &funcName, &funcInfo, pubRaw);
291 pubDecoded, baseType, yidx);
292 if (ypEntry == NULL) {
295 (*env)->CallBooleanMethod(env, ypArray, arrayList_add, ypEntry);
314 const char *serial = NULL;
315 jbyte *request_bytes = NULL;
317 jbyteArray result = NULL;
320 serial = (*env)->GetStringUTFChars(env, serial_java, NULL);
321 if (NULL == serial) {
322 throwYAPI_Exception(env,
"Invalid String");
327 request_bytes = (*env)->GetByteArrayElements(env, request_java, NULL);
328 if (NULL == request_bytes) {
329 throwYAPI_Exception(env,
"Invalid Byte Array");
332 length = (*env)->GetArrayLength(env, request_java);
335 throwYAPI_Exception(env, errmsg);
338 if (replysize < 0 || reply == NULL) {
343 result = (*env)->NewByteArray(env, replysize);
344 if (NULL == result) {
345 throwYAPI_Exception(env,
"Unable to allocate bytes array");
350 (*env)->SetByteArrayRegion(env, result, 0 , replysize, (jbyte*)reply);
354 throwYAPI_Exception(env, errmsg);
358 if (serial != NULL) {
359 (*env)->ReleaseStringUTFChars(env, serial_java, serial);
362 if (request_bytes != NULL) {
363 (*env)->ReleaseByteArrayElements(env, request_java, request_bytes, 0);
378 const char *serial = NULL;
379 jbyte *request_bytes = NULL;
383 serial = (*env)->GetStringUTFChars(env, serial_java, NULL);
384 if (NULL == serial) {
385 throwYAPI_Exception(env,
"Invalid String");
390 request_bytes = (*env)->GetByteArrayElements(env, request_java, NULL);
391 if (NULL == request_bytes) {
392 throwYAPI_Exception(env,
"Invalid Byte Array");
395 length = (*env)->GetArrayLength(env, request_java);
399 throwYAPI_Exception(env, errmsg);
405 if (serial != NULL) {
406 (*env)->ReleaseStringUTFChars(env, serial_java, serial);
409 if (request_bytes != NULL) {
410 (*env)->ReleaseByteArrayElements(env, request_java, request_bytes, 0);
419 static JNIEnv* getThreadEnv()
423 int getEnvStat = (*jvm)->GetEnv(jvm, (
void**)&env, JNI_VERSION_1_6);
424 if (getEnvStat == JNI_EDETACHED) {
425 dbglog(
"GetEnv: not attached\n");
426 if ((*jvm)->AttachCurrentThread(jvm, (
void **) &env, NULL) != 0) {
427 dbglog(
"Failed to attach\n");
430 }
else if (getEnvStat == JNI_OK) {
432 }
else if (getEnvStat == JNI_EVERSION) {
433 dbglog(
"GetEnv: version not supported\n");
440 static void jFunctionUpdateCallbackFwd(
YAPI_FUNCTION fundesc,
const char *value)
447 jclass yUSBHub_class;
448 jmethodID yUSBHub_handleValueNotification;
455 env = getThreadEnv();
461 j_serial = (*env)->NewStringUTF(env, serial);
462 j_funcId = (*env)->NewStringUTF(env, funcId);
463 j_value = (*env)->NewStringUTF(env, value);
465 yUSBHub_class = (*env)->FindClass(env,
"com/yoctopuce/YoctoAPI/YUSBHub");
466 if (yUSBHub_class == 0) {
467 dbglog(
"Unable to find class YUSBHub\n");
472 yUSBHub_handleValueNotification = (*env)->GetMethodID(env, yUSBHub_class,
"handleValueNotification",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
473 if (yUSBHub_handleValueNotification == 0) {
474 dbglog(
"Unable to find add method of handleValueNotification\n");
478 (*env)->CallVoidMethod(env, jObj, yUSBHub_handleValueNotification, j_serial, j_funcId, j_value);
481 static void jFunctionTimedReportCallbackFwd(
YAPI_FUNCTION fundesc,
double timestamp,
const u8 *bytes, u32 len)
487 jbyteArray result = NULL;
488 jclass yUSBHub_class;
489 jmethodID YUSBHub_handleTimedNotification;
492 env = getThreadEnv();
498 j_serial = (*env)->NewStringUTF(env, serial);
499 j_funcId = (*env)->NewStringUTF(env, funcId);
501 yUSBHub_class = (*env)->FindClass(env,
"com/yoctopuce/YoctoAPI/YUSBHub");
502 if (yUSBHub_class == 0) {
503 dbglog(
"Unable to find class YUSBHub\n");
507 YUSBHub_handleTimedNotification = (*env)->GetMethodID(env, yUSBHub_class,
"handleTimedNotification",
"(Ljava/lang/String;Ljava/lang/String;D[B)V");
508 if (YUSBHub_handleTimedNotification == 0) {
509 dbglog(
"Unable to find add method of handleTimedNotification\n");
515 result = (*env)->NewByteArray(env, len);
516 if (NULL == result) {
517 dbglog(
"Unable to allocate bytes array");
521 (*env)->SetByteArrayRegion(env, result, 0 , len, (jbyte*) bytes);
523 (*env)->CallVoidMethod(env, jObj, YUSBHub_handleTimedNotification, j_serial, j_funcId, timestamp, result);
536 if ((*env)->GetJavaVM(env, &jvm) != 0) {
537 throwYAPI_Exception(env,
"GetJavaVM: Unable to get VM");
540 jObj = (*env)->NewGlobalRef(env, yUSBHubRef);
554 (*env)->DeleteGlobalRef(env, jObj);
570 throwYAPI_Exception(env, errmsg);
JNIEXPORT void JNICALL Java_com_yoctopuce_YoctoAPI_YJniWrapper_updateDeviceList(JNIEnv *, jclass, jobject, jobject)
YRETCODE YAPI_FUNCTION_EXPORT yapiGetBootloaders(char *buffer, int buffersize, int *fullsize, char *errmsg)
void YAPI_FUNCTION_EXPORT yapiRegisterTimedReportCallback(yapiTimedReportCallback timedReportCallback)
JNIEXPORT void JNICALL Java_com_yoctopuce_YoctoAPI_YJniWrapper_usbProcess(JNIEnv *, jclass, jobject)
JNIEXPORT jobject JNICALL Java_com_yoctopuce_YoctoAPI_YJniWrapper_getBootloaders(JNIEnv *, jclass)
void YAPI_FUNCTION_EXPORT yapiFreeAPI(void)
char * yHashGetStrPtr(yHash yhash)
int YAPI_FUNCTION_EXPORT yapiGetAllDevices(YAPI_DEVICE *buffer, int maxsize, int *neededsize, char *errmsg)
YRETCODE YAPI_FUNCTION_EXPORT yapiHTTPRequestSyncStartEx(YIOHDL *iohdl, const char *device, const char *request, int requestsize, char **reply, int *replysize, char *errmsg)
JNIEXPORT void JNICALL Java_com_yoctopuce_YoctoAPI_YJniWrapper_reserveUSBAccess(JNIEnv *, jclass)
void decodePubVal(Notification_funydx funInfo, const char *funcval, char *buffer)
#define YOCTO_PUBVAL_SIZE
JNIEXPORT jbyteArray JNICALL Java_com_yoctopuce_YoctoAPI_YJniWrapper_devRequestSync(JNIEnv *, jclass, jstring, jbyteArray)
JNIEXPORT void JNICALL Java_com_yoctopuce_YoctoAPI_YJniWrapper_releaseUSBAccess(JNIEnv *, jclass)
void ypGetCategory(yBlkHdl hdl, char *name, yBlkHdl *entries)
char logicalname[YOCTO_LOGICAL_LEN]
#define YOCTO_FUNCTION_LEN
YRETCODE YAPI_FUNCTION_EXPORT yapiHTTPRequestAsyncEx(const char *device, const char *request, int len, yapiRequestAsyncCallback callback, void *context, char *errmsg)
YRETCODE YAPI_FUNCTION_EXPORT yapiHTTPRequestSyncDone(YIOHDL *iohdl, char *errmsg)
u16 YAPI_FUNCTION_EXPORT yapiGetAPIVersion(const char **version, const char **apidate)
int ypGetFunctionInfo(YAPI_FUNCTION fundesc, char *serial, char *funcId, char *baseType, char *funcName, char *funcVal)
YRETCODE YAPI_FUNCTION_EXPORT yapiInitAPI(int detect_type, char *errmsg)
int ypGetAttributes(yBlkHdl hdl, yStrRef *serial, yStrRef *funcId, yStrRef *funcName, Notification_funydx *funcInfo, char *funcVal)
int ypGetType(yBlkHdl hdl)
JNIEXPORT jstring JNICALL Java_com_yoctopuce_YoctoAPI_YJniWrapper_getAPIVersion(JNIEnv *, jclass)
JNIEXPORT void JNICALL Java_com_yoctopuce_YoctoAPI_YJniWrapper_stopNotifications(JNIEnv *, jclass)
yBlkHdl yBlkListSeek(yBlkHdl hdl, u16 pos)
JNIEXPORT void JNICALL Java_com_yoctopuce_YoctoAPI_YJniWrapper_devRequestAsync(JNIEnv *, jclass, jstring, jbyteArray, jobject, jobject)
JNIEXPORT void JNICALL Java_com_yoctopuce_YoctoAPI_YJniWrapper_startNotifications(JNIEnv *, jclass, jobject)
void YAPI_FUNCTION_EXPORT yapiRegisterFunctionUpdateCallback(yapiFunctionUpdateCallback updateCallback)
char serial[YOCTO_SERIAL_LEN]
char productname[YOCTO_PRODUCTNAME_LEN]
YRETCODE YAPI_FUNCTION_EXPORT yapiGetDeviceInfo(YAPI_DEVICE devdesc, yDeviceSt *infos, char *errmsg)
YRETCODE YAPI_FUNCTION_EXPORT yapiUpdateDeviceList(u32 forceupdate, char *errmsg)
YRETCODE YAPI_FUNCTION_EXPORT yapiHandleEvents(char *errmsg)