Share PM67 upload protocol through C core
This commit is contained in:
@@ -8,6 +8,97 @@
|
||||
#include "set_spectrum.h"
|
||||
#include "balsam_can.h"
|
||||
#include "periph28335.h"
|
||||
#include "tms2812.h"
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativeTms2812Crc16(
|
||||
JNIEnv *env, jobject self, jbyteArray input)
|
||||
{
|
||||
(void)self;
|
||||
if (input == NULL) return 0;
|
||||
jsize size = (*env)->GetArrayLength(env, input);
|
||||
jbyte *data = (*env)->GetByteArrayElements(env, input, NULL);
|
||||
if ((data == NULL) && (size != 0)) return 0;
|
||||
uint16_t crc = tms2812_crc16((const uint8_t *)data, (size_t)size);
|
||||
if (data != NULL) (*env)->ReleaseByteArrayElements(env, input, data, JNI_ABORT);
|
||||
return (jint)crc;
|
||||
}
|
||||
|
||||
JNIEXPORT jbyteArray JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativeTms2812BuildUpload(
|
||||
JNIEnv *env, jobject self, jint controller, jlong word_address,
|
||||
jlong byte_count)
|
||||
{
|
||||
(void)self;
|
||||
uint8_t output[TMS2812_UPLOAD_REQUEST_SIZE];
|
||||
if (controller < 0 || controller > 255 || word_address < 0 ||
|
||||
(uint64_t)word_address > UINT32_MAX || byte_count < 1 ||
|
||||
(uint64_t)byte_count > UINT32_MAX) return NULL;
|
||||
size_t written = tms2812_build_upload_request(
|
||||
(uint8_t)controller, (uint32_t)word_address, (uint32_t)byte_count,
|
||||
output, sizeof output);
|
||||
if (written == 0U) return NULL;
|
||||
jbyteArray result = (*env)->NewByteArray(env, (jsize)written);
|
||||
if (result != NULL) (*env)->SetByteArrayRegion(
|
||||
env, result, 0, (jsize)written, (const jbyte *)output);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativeTms2812ExpectedUploadSize(
|
||||
JNIEnv *env, jobject self, jlong byte_count)
|
||||
{
|
||||
(void)env; (void)self;
|
||||
if (byte_count < 1 || (uint64_t)byte_count > UINT32_MAX) return 0;
|
||||
size_t size = tms2812_expected_upload_response_size((uint32_t)byte_count);
|
||||
return size <= INT32_MAX ? (jint)size : 0;
|
||||
}
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativeTms2812ValidateUpload(
|
||||
JNIEnv *env, jobject self, jbyteArray input, jint controller,
|
||||
jint byte_count)
|
||||
{
|
||||
(void)self;
|
||||
if (input == NULL || controller < 0 || controller > 255 || byte_count < 1)
|
||||
return JNI_FALSE;
|
||||
jsize size = (*env)->GetArrayLength(env, input);
|
||||
jbyte *data = (*env)->GetByteArrayElements(env, input, NULL);
|
||||
if (data == NULL) return JNI_FALSE;
|
||||
int status = tms2812_validate_upload_response(
|
||||
(const uint8_t *)data, (size_t)size, (uint8_t)controller,
|
||||
(uint32_t)byte_count);
|
||||
(*env)->ReleaseByteArrayElements(env, input, data, JNI_ABORT);
|
||||
return status == TMS2812_OK ? JNI_TRUE : JNI_FALSE;
|
||||
}
|
||||
|
||||
JNIEXPORT jbyteArray JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativeTms2812DecodeUpload(
|
||||
JNIEnv *env, jobject self, jbyteArray input, jint controller,
|
||||
jint byte_count)
|
||||
{
|
||||
(void)self;
|
||||
if (input == NULL || controller < 0 || controller > 255 || byte_count < 1)
|
||||
return NULL;
|
||||
jsize size = (*env)->GetArrayLength(env, input);
|
||||
jbyte *data = (*env)->GetByteArrayElements(env, input, NULL);
|
||||
if (data == NULL) return NULL;
|
||||
uint8_t *output = (uint8_t *)malloc((size_t)byte_count);
|
||||
if (output == NULL) {
|
||||
(*env)->ReleaseByteArrayElements(env, input, data, JNI_ABORT);
|
||||
return NULL;
|
||||
}
|
||||
int status = tms2812_decode_upload_response(
|
||||
(const uint8_t *)data, (size_t)size, (uint8_t)controller,
|
||||
(uint32_t)byte_count, output, (size_t)byte_count);
|
||||
(*env)->ReleaseByteArrayElements(env, input, data, JNI_ABORT);
|
||||
if (status != TMS2812_OK) { free(output); return NULL; }
|
||||
jbyteArray result = (*env)->NewByteArray(env, byte_count);
|
||||
if (result != NULL) (*env)->SetByteArrayRegion(
|
||||
env, result, 0, byte_count, (const jbyte *)output);
|
||||
free(output);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativePeriph28335Crc16(
|
||||
|
||||
Reference in New Issue
Block a user