added more keys (@equipter)
[RRG-proxmark3.git] / client / android / jni_tools.c
blob5c75588530ade4c5c4a40e986c94793566085652
1 //
2 // Created by DXL on 2017/9/1.
3 //
5 //including header
6 #include <malloc.h>
7 #include <jni_tools.h>
8 #include "stdbool.h"
10 // native thread attach label
11 static bool g_IsAttach;
13 // get current env for jvm
14 JNIEnv *getJniEnv() {
15 JNIEnv *currentThreadEnv;
16 g_IsAttach = false;
17 if ((*g_JavaVM)->GetEnv(g_JavaVM, (void **) &currentThreadEnv, JNI_VERSION_1_4) != JNI_OK) {
18 LOGE("Get Env Fail!");
19 if ((*g_JavaVM)->AttachCurrentThread(g_JavaVM, &currentThreadEnv, NULL) != JNI_OK) {
20 LOGE("Attach the current thread Fail!");
21 g_IsAttach = false;
22 return NULL;
23 } else {
24 g_IsAttach = true;
25 LOGE("Attach the current thread Success!");
26 return currentThreadEnv;
28 } else {
29 g_IsAttach = false;
30 //LOGE("Get Env Success!");
31 return currentThreadEnv;
35 // detach native thread from jvm
36 void detachThread() {
37 if (g_IsAttach) {
38 (*g_JavaVM)->DetachCurrentThread(g_JavaVM);
42 // cmd arg parse
43 CMD *parse_command_line(const char *commandStr) {
44 CMD *cmd = (CMD *) malloc(sizeof(CMD));
45 if (!cmd) {
46 return NULL;
48 // copy the source to the heap
49 char *pTmp = strdup(commandStr);
50 // new memory size is default 20 for char **
51 int size = 20;
52 cmd->cmd = (char **) malloc(size * sizeof(char **));
53 if (!cmd->cmd) {
54 free(cmd);
55 return NULL;
57 // parse
58 char *pStr = strtok(pTmp, " ");
59 cmd->cmd[0] = pStr;
60 int count = 1;
61 for (; pStr != NULL; ++count) {
62 // Capacity expansion
63 if (count == (size - 1)) {
64 size += 20;
65 cmd->cmd = (char **) realloc(cmd->cmd, size * sizeof(char **));
67 pStr = strtok(NULL, " ");
68 if (pStr) {
69 cmd->cmd[count] = pStr;
72 cmd->len = (count - 1);
73 return cmd;
76 // cmd arg struct free
77 void free_command_line(CMD *cmd) {
78 free(cmd->cmd[0]);
79 free(cmd->cmd);
80 free(cmd);