Move PM35 protocol into shared C core
This commit is contained in:
@@ -7,6 +7,182 @@
|
||||
#include "set_trends.h"
|
||||
#include "set_spectrum.h"
|
||||
#include "balsam_can.h"
|
||||
#include "periph28335.h"
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativePeriph28335Crc16(
|
||||
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 = periph28335_crc16_modbus((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_nativePeriph28335AppendCrc(
|
||||
JNIEnv *env, jobject self, jbyteArray input)
|
||||
{
|
||||
(void)self;
|
||||
if (input == NULL) return NULL;
|
||||
jsize size = (*env)->GetArrayLength(env, input);
|
||||
jbyte *data = (*env)->GetByteArrayElements(env, input, NULL);
|
||||
if ((data == NULL) && (size != 0)) return NULL;
|
||||
uint8_t *output = (uint8_t *)malloc((size_t)size + 2U);
|
||||
if (output == NULL) {
|
||||
if (data != NULL) (*env)->ReleaseByteArrayElements(env, input, data, JNI_ABORT);
|
||||
return NULL;
|
||||
}
|
||||
size_t written = periph28335_append_crc(
|
||||
(const uint8_t *)data, (size_t)size, output, (size_t)size + 2U);
|
||||
if (data != NULL) (*env)->ReleaseByteArrayElements(env, input, data, JNI_ABORT);
|
||||
if (written == 0U) { free(output); return NULL; }
|
||||
jbyteArray result = (*env)->NewByteArray(env, (jsize)written);
|
||||
if (result != NULL) (*env)->SetByteArrayRegion(
|
||||
env, result, 0, (jsize)written, (const jbyte *)output);
|
||||
free(output);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jbyteArray JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativePeriph28335BuildRead(
|
||||
JNIEnv *env, jobject self, jint controller, jint start, jint count)
|
||||
{
|
||||
(void)self;
|
||||
uint8_t output[PERIPH28335_REQUEST_SIZE];
|
||||
if (controller < 0 || controller > 255 || start < 0 || start > 65535 ||
|
||||
count < 0 || count > 65535) return NULL;
|
||||
size_t written = periph28335_build_read_registers(
|
||||
(uint8_t)controller, (uint16_t)start, (uint16_t)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 jbyteArray JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativePeriph28335BuildWrite(
|
||||
JNIEnv *env, jobject self, jint controller, jint address, jint value)
|
||||
{
|
||||
(void)self;
|
||||
uint8_t output[PERIPH28335_REQUEST_SIZE];
|
||||
if (controller < 0 || controller > 255 || address < 0 || address > 65535 ||
|
||||
value < 0 || value > 65535) return NULL;
|
||||
size_t written = periph28335_build_write_register(
|
||||
(uint8_t)controller, (uint16_t)address, (uint16_t)value,
|
||||
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 jbyteArray JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativePeriph28335BuildCommand(
|
||||
JNIEnv *env, jobject self, jint controller, jint command_index)
|
||||
{
|
||||
(void)self;
|
||||
uint8_t output[PERIPH28335_REQUEST_SIZE];
|
||||
if (controller < 0 || controller > 255 || command_index < 0 || command_index > 255) return NULL;
|
||||
size_t written = periph28335_build_command(
|
||||
(uint8_t)controller, (uint8_t)command_index, 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_nativePeriph28335ExpectedReadSize(
|
||||
JNIEnv *env, jobject self, jint count)
|
||||
{
|
||||
(void)env; (void)self;
|
||||
if (count < 0 || count > 65535) return 0;
|
||||
return (jint)periph28335_expected_read_response_size((uint16_t)count);
|
||||
}
|
||||
|
||||
JNIEXPORT jintArray JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativePeriph28335DecodeRead(
|
||||
JNIEnv *env, jobject self, jbyteArray input, jint controller, jint count)
|
||||
{
|
||||
(void)self;
|
||||
if (input == NULL || controller < 0 || controller > 255 ||
|
||||
count < 1 || count > (jint)PERIPH28335_REGISTER_COUNT) return NULL;
|
||||
jsize size = (*env)->GetArrayLength(env, input);
|
||||
jbyte *data = (*env)->GetByteArrayElements(env, input, NULL);
|
||||
if ((data == NULL) && (size != 0)) return NULL;
|
||||
uint16_t words[PERIPH28335_REGISTER_COUNT];
|
||||
int status = periph28335_decode_read_response(
|
||||
(const uint8_t *)data, (size_t)size, (uint8_t)controller,
|
||||
(uint16_t)count, words, PERIPH28335_REGISTER_COUNT);
|
||||
if (data != NULL) (*env)->ReleaseByteArrayElements(env, input, data, JNI_ABORT);
|
||||
if (status != PERIPH28335_OK) return NULL;
|
||||
jint values[PERIPH28335_REGISTER_COUNT];
|
||||
for (jint index = 0; index < count; ++index) values[index] = words[index];
|
||||
jintArray result = (*env)->NewIntArray(env, count);
|
||||
if (result != NULL) (*env)->SetIntArrayRegion(env, result, 0, count, values);
|
||||
return result;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativePeriph28335ValidateWrite(
|
||||
JNIEnv *env, jobject self, jbyteArray response, jbyteArray request)
|
||||
{
|
||||
(void)self;
|
||||
if (response == NULL || request == NULL) return PERIPH28335_ERROR_ARGUMENT;
|
||||
jsize response_size = (*env)->GetArrayLength(env, response);
|
||||
jsize request_size = (*env)->GetArrayLength(env, request);
|
||||
jbyte *response_data = (*env)->GetByteArrayElements(env, response, NULL);
|
||||
jbyte *request_data = (*env)->GetByteArrayElements(env, request, NULL);
|
||||
if (response_data == NULL || request_data == NULL) {
|
||||
if (response_data != NULL) (*env)->ReleaseByteArrayElements(env, response, response_data, JNI_ABORT);
|
||||
if (request_data != NULL) (*env)->ReleaseByteArrayElements(env, request, request_data, JNI_ABORT);
|
||||
return PERIPH28335_ERROR_ARGUMENT;
|
||||
}
|
||||
int status = periph28335_validate_write_response(
|
||||
(const uint8_t *)response_data, (size_t)response_size,
|
||||
(const uint8_t *)request_data, (size_t)request_size);
|
||||
(*env)->ReleaseByteArrayElements(env, response, response_data, JNI_ABORT);
|
||||
(*env)->ReleaseByteArrayElements(env, request, request_data, JNI_ABORT);
|
||||
return status;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativePeriph28335ProjectCount(
|
||||
JNIEnv *env, jobject self)
|
||||
{
|
||||
(void)env; (void)self;
|
||||
return (jint)periph28335_project_count();
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativePeriph28335ProjectName(
|
||||
JNIEnv *env, jobject self, jint project_index)
|
||||
{
|
||||
(void)self;
|
||||
const char *name = project_index >= 0
|
||||
? periph28335_project_name((size_t)project_index) : NULL;
|
||||
return name != NULL ? (*env)->NewStringUTF(env, name) : NULL;
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativePeriph28335CommandName(
|
||||
JNIEnv *env, jobject self, jint project_index, jint command_index)
|
||||
{
|
||||
(void)self;
|
||||
const char *name = project_index >= 0 && command_index >= 0
|
||||
? periph28335_command_name((size_t)project_index, (size_t)command_index)
|
||||
: NULL;
|
||||
return name != NULL ? (*env)->NewStringUTF(env, name) : NULL;
|
||||
}
|
||||
|
||||
JNIEXPORT jintArray JNICALL
|
||||
Java_ru_setcorp_setprotocol_NativeSetProtocol_nativeBalsamDecode(
|
||||
|
||||
Reference in New Issue
Block a user