debug_protocol.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 
4 #include <jni.h>
5 #include "error.h"
6 #include <fstream>
7 #include "../../../include/librealsense2/h/rs_internal.h"
8 #include "../../../common/parser.hpp" //needed for auto completion in nGetCommands
9 
10 
11 extern "C"
12 JNIEXPORT jbyteArray JNICALL
14  jlong handle,
15  jbyteArray buffer_) {
16  jbyte *buffer = env->GetByteArrayElements(buffer_, NULL);
17  jsize length = env->GetArrayLength(buffer_);
18 
19  rs2_error* e = NULL;
20  std::shared_ptr<const rs2_raw_data_buffer> response_bytes(
21  rs2_send_and_receive_raw_data(reinterpret_cast<rs2_device*>(handle), (void*)buffer, (int)length, &e),
23 
24  int response_size = rs2_get_raw_data_size(response_bytes.get(), &e);
25  handle_error(env, e);
26 
27  const unsigned char* response_start = rs2_get_raw_data(response_bytes.get(), &e);
28  handle_error(env, e);
29 
30  env->ReleaseByteArrayElements(buffer_, buffer, 0);
31  jbyteArray rv = env->NewByteArray(response_size);
32  env->SetByteArrayRegion (rv, 0, response_size, reinterpret_cast<const jbyte *>(response_start));
33 
34  return rv;
35 }
36 
37 extern "C"
38 JNIEXPORT jbyteArray JNICALL
40  jlong handle,
41  jstring filePath,
42  jstring command) {
43  const char *file_path = env->GetStringUTFChars(filePath, 0);
44  const char *line = env->GetStringUTFChars(command, 0);
45 
46  std::ifstream f(file_path);
47  std::string xml_content((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
48 
49  rs2_error* e = NULL;
50 
51  //creating terminal parser
52  std::shared_ptr<rs2_terminal_parser> terminal_parser = std::shared_ptr<rs2_terminal_parser>(
53  rs2_create_terminal_parser(xml_content.c_str(), &e),
55  handle_error(env, e);
56 
57  //using parser to get bytes command from the string command
58  int line_length = strlen(line);
59  std::shared_ptr<const rs2_raw_data_buffer> command_buffer(
60  rs2_terminal_parse_command(terminal_parser.get(), line, line_length, &e),
62  handle_error(env, e);
63 
64  int command_size = rs2_get_raw_data_size(command_buffer.get(), &e);
65  handle_error(env, e);
66 
67  const unsigned char* command_start = rs2_get_raw_data(command_buffer.get(), &e);
68  handle_error(env, e);
69 
70  //calling send_receive to get the bytes response
71  std::shared_ptr<const rs2_raw_data_buffer> response_bytes(
72  rs2_send_and_receive_raw_data(reinterpret_cast<rs2_device*>(handle), (void*)command_start, command_size, &e),
74 
75  int response_size = rs2_get_raw_data_size(response_bytes.get(), &e);
76  handle_error(env, e);
77 
78  const unsigned char* response_start = rs2_get_raw_data(response_bytes.get(), &e);
79  handle_error(env, e);
80 
81  //using parser to get response string from response bytes and string command
82  std::shared_ptr<const rs2_raw_data_buffer> response_buffer(
83  rs2_terminal_parse_response(terminal_parser.get(), line, line_length,
84  (void*)response_start, response_size, &e),
86  handle_error(env, e);
87 
88  int response_buffer_size = rs2_get_raw_data_size(response_buffer.get(), &e);
89  handle_error(env, e);
90 
91  const unsigned char* response_buffer_start = rs2_get_raw_data(response_buffer.get(), &e);
92  handle_error(env, e);
93 
94  //releasing jni resources
95  env->ReleaseStringUTFChars(command, line);
96  env->ReleaseStringUTFChars(filePath, file_path);
97 
98  //assigning return value
99  jbyteArray rv = env->NewByteArray(response_buffer_size);
100  env->SetByteArrayRegion(rv, 0, response_buffer_size,
101  reinterpret_cast<const jbyte *>(response_buffer_start));
102 
103  return rv;
104 }
105 
106 extern "C"
107 JNIEXPORT jobjectArray JNICALL
109  jstring filePath) {
110  const char *file_path = env->GetStringUTFChars(filePath, 0);
111 
112  jobjectArray rv;
113  commands_xml cmd_xml;
114  bool sts = parse_xml_from_file(file_path, cmd_xml);
115  if(sts)
116  {
117  rv = (jobjectArray)env->NewObjectArray(cmd_xml.commands.size(), env->FindClass("java/lang/String"),env->NewStringUTF(""));
118  int i = 0;
119  for(auto&& c : cmd_xml.commands)
120  env->SetObjectArrayElement(rv, i++, env->NewStringUTF(c.first.c_str()));
121  }
122  env->ReleaseStringUTFChars(filePath, file_path);
123 
124  return(rv);
125 }
126 
bool parse_xml_from_file(const std::string &xml_full_file_path, commands_xml &cmd_xml)
Definition: parser.hpp:361
GLuint64 GLenum void * handle
Definition: glext.h:7785
const unsigned char * rs2_get_raw_data(const rs2_raw_data_buffer *buffer, rs2_error **error)
Definition: rs.cpp:567
void handle_error(JNIEnv *env, rs2_error *error)
Definition: error.cpp:6
GLsizei const GLchar *const * string
void rs2_delete_raw_data(const rs2_raw_data_buffer *buffer)
Definition: rs.cpp:581
e
Definition: rmse.py:177
GLenum GLfloat * buffer
int rs2_get_raw_data_size(const rs2_raw_data_buffer *buffer, rs2_error **error)
Definition: rs.cpp:574
GLdouble f
const GLubyte * c
Definition: glext.h:12690
rs2_raw_data_buffer * rs2_terminal_parse_response(rs2_terminal_parser *terminal_parser, const char *command, unsigned int size_of_command, const void *response, unsigned int size_of_response, rs2_error **error)
Parses terminal response via RealSense terminal parser.
Definition: rs.cpp:3389
std::map< std::string, command_from_xml > commands
Definition: parser.hpp:193
rs2_raw_data_buffer * rs2_terminal_parse_command(rs2_terminal_parser *terminal_parser, const char *command, unsigned int size_of_command, rs2_error **error)
Parses terminal command via RealSense terminal parser.
Definition: rs.cpp:3374
const rs2_raw_data_buffer * rs2_send_and_receive_raw_data(rs2_device *device, void *raw_data_to_send, unsigned size_of_raw_data_to_send, rs2_error **error)
Definition: rs.cpp:554
void rs2_delete_terminal_parser(rs2_terminal_parser *terminal_parser)
Deletes RealSense terminal parser.
Definition: rs.cpp:3367
GLenum type
JNIEXPORT jbyteArray JNICALL Java_com_intel_realsense_librealsense_DebugProtocol_nSendAndReceiveData(JNIEnv *env, jclass clazz, jlong handle, jstring filePath, jstring command)
#define NULL
Definition: tinycthread.c:47
int i
GLenum GLuint GLenum GLsizei length
JNIEXPORT jobjectArray JNICALL Java_com_intel_realsense_librealsense_DebugProtocol_nGetCommands(JNIEnv *env, jclass clazz, jstring filePath)
rs2_terminal_parser * rs2_create_terminal_parser(const char *xml_content, rs2_error **error)
Creates RealSense terminal parser.
Definition: rs.cpp:3360
JNIEXPORT jbyteArray JNICALL Java_com_intel_realsense_librealsense_DebugProtocol_nSendAndReceiveRawData(JNIEnv *env, jclass type, jlong handle, jbyteArray buffer_)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:12