proxmark3/client/android/pm3_main.c

168 lines
4.8 KiB
C
Raw Normal View History

2020-05-06 18:03:37 +08:00
//-----------------------------------------------------------------------------
// Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// Main binary
//-----------------------------------------------------------------------------
#include "proxmark3.h"
#include <stdlib.h>
#include <stdio.h> // for Mingw readline
#include <limits.h>
#include <unistd.h>
#include <ctype.h>
#include "usart_defs.h"
#include "util_posix.h"
#include "proxgui.h"
#include "cmdmain.h"
#include "ui.h"
#include "cmdhw.h"
#include "whereami.h"
#include "comms.h"
#include "fileutils.h"
#include "jni_tools.h"
2020-06-02 16:38:18 +08:00
//iceman, todo: proxify socker server name. Maybe set in preferences?
2020-07-03 16:46:40 +08:00
// DXL reply, todo:
// Is a good idea, we can move this def to preferences, but not now.
// Because libpm3rrg_rdv4.so cant load preferences.
// I will impl a function to load preferences at future.
2020-06-02 16:38:18 +08:00
#define PM3_LOCAL_SOCKET_SERVER "DXL.COM.ASL"
2020-05-06 18:03:37 +08:00
2020-06-02 16:38:18 +08:00
static char *g_android_executable_directory = NULL;
2020-07-03 16:46:40 +08:00
static char *g_android_user_directory = NULL;
2020-05-06 18:03:37 +08:00
2020-10-26 16:28:41 +08:00
char version_information[] = {""};
2020-05-06 18:03:37 +08:00
2020-07-03 16:46:40 +08:00
const char *get_my_executable_directory(void) {
2020-06-02 16:38:18 +08:00
if (g_android_executable_directory == NULL) {
char buf[FILE_PATH_SIZE] = {0};
getcwd(buf, sizeof(buf));
2020-06-08 19:19:01 +08:00
strncat(buf, PATHSEP, 1);
2020-06-02 16:38:18 +08:00
g_android_executable_directory = strdup(buf);
}
return g_android_executable_directory;
2020-05-06 18:03:37 +08:00
}
2020-07-03 16:46:40 +08:00
const char *get_my_user_directory(void) {
2020-06-02 16:38:18 +08:00
return g_android_user_directory;
2020-05-06 18:03:37 +08:00
}
2020-07-03 16:46:40 +08:00
void ShowGraphWindow(void) {}
void HideGraphWindow(void) {}
void RepaintGraphWindow(void) {}
int push_cmdscriptfile(char *path, bool stayafter) { return PM3_SUCCESS; }
2020-06-02 16:38:18 +08:00
static bool OpenPm3(void) {
2020-07-03 16:46:40 +08:00
if (conn.run) { return true; }
2020-06-02 16:38:18 +08:00
// Open with LocalSocket. Not a tcp connection!
bool ret = OpenProxmark(session.current_device, "socket:"PM3_LOCAL_SOCKET_SERVER, false, 1000, false, 115200);
2020-05-06 18:03:37 +08:00
return ret;
}
/*
* Transfers to the command buffer and waits for a new command to be executed
2020-05-06 18:03:37 +08:00
* */
2020-06-02 16:38:18 +08:00
jint Console(JNIEnv *env, jobject instance, jstring cmd_) {
2020-05-06 18:03:37 +08:00
if (!conn.run) {
2020-06-02 16:38:18 +08:00
if (OpenPm3() && TestProxmark() == PM3_SUCCESS) {
LOGD("Connected to device");
PrintAndLogEx(SUCCESS, "Connected to device");
2020-05-06 18:03:37 +08:00
} else {
2020-06-02 16:38:18 +08:00
LOGD("Failed to connect to device");
PrintAndLogEx(ERR, "Failed to connect to device");
2020-05-06 18:03:37 +08:00
CloseProxmark();
}
}
2020-06-02 16:38:18 +08:00
PrintAndLogEx(NORMAL, "");
2020-11-02 08:46:47 +08:00
char *cmd = (char *)((*env)->GetStringUTFChars(env, cmd_, 0));
2020-05-06 18:03:37 +08:00
int ret = CommandReceived(cmd);
if (ret == 99) {
// exit / quit
// TODO: implement this
PrintAndLogEx(NORMAL, "Asked to exit, can't really do that yet...");
}
2020-06-02 16:38:18 +08:00
2020-05-06 18:03:37 +08:00
(*env)->ReleaseStringUTFChars(env, cmd_, cmd);
return ret;
}
/*
* Is client running!
2020-05-06 18:03:37 +08:00
* */
2020-06-02 16:38:18 +08:00
jboolean IsClientRunning(JNIEnv *env, jobject instance) {
2020-11-02 08:46:47 +08:00
return (jboolean)((jboolean) conn.run);
2020-05-06 18:03:37 +08:00
}
/*
2020-07-03 16:46:40 +08:00
* test hw and fw and client.
2020-05-06 18:03:37 +08:00
* */
2020-06-02 16:38:18 +08:00
jboolean TestPm3(JNIEnv *env, jobject instance) {
if (open() == false) {
2020-05-06 18:03:37 +08:00
CloseProxmark();
return false;
}
2020-06-02 16:38:18 +08:00
bool ret = (TestProxmark() == PM3_SUCCESS);
2020-11-02 08:46:47 +08:00
return (jboolean)(ret);
2020-05-06 18:03:37 +08:00
}
/*
* stop pm3 client
* */
2020-06-02 16:38:18 +08:00
void ClosePm3(JNIEnv *env, jobject instance) {
2020-05-06 18:03:37 +08:00
CloseProxmark();
}
/*
* native function map to jvm
* */
2020-06-08 09:15:10 +08:00
//iceman: todo, pm3:ify java class root. Return codes, should match PM3_E* codes.
2020-05-06 18:03:37 +08:00
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *jniEnv = NULL;
if ((*vm)->GetEnv(vm, (void **) &jniEnv, JNI_VERSION_1_4) != JNI_OK) {
return -1;
}
(*jniEnv)->GetJavaVM(jniEnv, &g_JavaVM);
jclass clazz = (*jniEnv)->FindClass(jniEnv, "cn/rrg/natives/Proxmark3RRGRdv4Tools");
if (clazz == NULL) {
return -1;
}
jclass clz_test = (*jniEnv)->FindClass(jniEnv, "cn/rrg/devices/Proxmark3RRGRdv4");
JNINativeMethod methods[] = {
2020-11-02 08:46:47 +08:00
{"startExecute", "(Ljava/lang/String;)I", (void *) Console},
{"stopExecute", "()V", (void *) ClosePm3},
{"isExecuting", "()Z", (void *) IsClientRunning}
2020-05-06 18:03:37 +08:00
};
2020-06-02 16:38:18 +08:00
2020-05-06 18:03:37 +08:00
JNINativeMethod methods1[] = {
2020-11-02 08:46:47 +08:00
{"testPm3", "()Z", (void *) TestPm3},
{"closePm3", "()V", ClosePm3}
2020-05-06 18:03:37 +08:00
};
2020-06-02 16:38:18 +08:00
2020-07-03 16:46:40 +08:00
if ((*jniEnv)->RegisterNatives(jniEnv, clazz, methods, sizeof(methods) / sizeof(methods[0])) !=
2020-11-02 08:46:47 +08:00
JNI_OK) {
2020-05-06 18:03:37 +08:00
return -1;
}
2020-06-02 16:38:18 +08:00
2020-07-03 16:46:40 +08:00
if ((*jniEnv)->RegisterNatives(jniEnv, clz_test, methods1,
sizeof(methods1) / sizeof(methods1[0])) != JNI_OK) {
2020-05-06 18:03:37 +08:00
return -1;
}
2020-06-02 16:38:18 +08:00
2020-05-06 18:03:37 +08:00
(*jniEnv)->DeleteLocalRef(jniEnv, clazz);
(*jniEnv)->DeleteLocalRef(jniEnv, clz_test);
return JNI_VERSION_1_4;
2020-08-13 18:25:04 +08:00
}