实现加密相关的支持接口

serial
Matthew 1 year ago
parent 848defbcaa
commit 285bf6af05

@ -81,6 +81,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.MicroPhoto"
tools:targetApi="28">
<activity
android:name=".CertActivity"
android:exported="true" />
<activity
android:name=".video.VideoActivity"
android:exported="false"
@ -135,7 +138,6 @@
<action android:name="android.intent.action.USER_UNLOCKED" />
</intent-filter>
</receiver>
<receiver
android:name="com.xypower.common.UpdateReceiver"
android:enabled="true"

@ -696,27 +696,100 @@ Java_com_xypower_mpapp_MicroPhotoService_getSerialNumber(
}
extern "C" JNIEXPORT void JNICALL
Java_com_xypower_mpapp_MicroPhotoService_importPublicKey(
JNIEnv* env,
jclass cls, jstring path, jstring md5) {
extern "C" JNIEXPORT jboolean JNICALL
Java_com_xypower_mpapp_MicroPhotoService_importPublicKeyFile(
JNIEnv* env, jclass cls, jint index, jstring path, jstring md5) {
#ifdef USING_NRSEC
NrsecPort nrsec;
nrsec.Open("/dev/");
// NrsecSpiPort spi("/dev/mtkgpioctrl");
// NrsecSpiPort spi("/dev/spidevSE");
// const char *port = "/dev/mtkgpioctrl";
const char *port = "/dev/spidevSE";
if (!nrsec.Open(port))
{
return JNI_FALSE;
}
const char *pathStr = env->GetStringUTFChars(path, 0);
const char *md5Str = env->GetStringUTFChars(md5, 0);
bool res = false;
std::vector<unsigned char> data;
if (readFile(pathStr, data) && !data.empty())
{
nrsec.SM2InportPublicKey(1, &data[0]);
res = nrsec.SM2ImportPublicKey(1, &data[0]);
}
nrsec.Close();
env->ReleaseStringUTFChars(path, pathStr);
env->ReleaseStringUTFChars(md5, md5Str);
return res ? JNI_TRUE : JNI_FALSE;
#endif
}
extern "C" JNIEXPORT jboolean JNICALL
Java_com_xypower_mpapp_MicroPhotoService_importPublicKey(
JNIEnv* env, jclass cls, jint index, jbyteArray cert) {
#ifdef USING_NRSEC
NrsecPort nrsec;
// const char *port = "/dev/mtkgpioctrl";
const char *port = "/dev/spidevSE";
int byteCertLen = env->GetArrayLength(cert);
if (byteCertLen <= 0)
{
return JNI_FALSE;
}
if (!nrsec.Open(port))
{
return JNI_FALSE;
}
jbyte* byteCert = env->GetByteArrayElements(cert, 0);
bool res = nrsec.SM2ImportPublicKey(index, (const uint8_t*)byteCert);
nrsec.Close();
env->ReleaseByteArrayElements(cert, byteCert, JNI_ABORT);
return res ? JNI_TRUE : JNI_FALSE;
#endif
}
extern "C" JNIEXPORT jboolean JNICALL
Java_com_xypower_mpapp_MicroPhotoService_genKeys(
JNIEnv* env,
jclass cls, jint index) {
#ifdef USING_NRSEC
GpioControl::setRS485Enable(true);
// const char *port = "/dev/mtkgpioctrl";
const char *port = "/dev/spidevSE";
NrsecPort nrsec;
if (!nrsec.Open(port))
{
return JNI_FALSE;
}
// std::string version = nrsec.Version();
bool res = nrsec.SM2keypair(index) == 0;
nrsec.Close();
return res ? JNI_TRUE : JNI_FALSE;
#endif
}

@ -0,0 +1,65 @@
package com.xypower.mpapp;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Base64;
import java.io.File;
import java.nio.charset.StandardCharsets;
public class CertActivity extends AppCompatActivity {
private final static String ACTION_IMP_PUBKEY = "imp_pubkey";
private final static String ACTION_GEN_KEYS = "gen_keys";
private Handler mHandler = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cert);
mHandler = new Handler();
Intent intent = getIntent();
final String action = intent.getStringExtra("action");
if (!TextUtils.isEmpty(action)) {
if (TextUtils.equals(action, ACTION_IMP_PUBKEY)) {
String cert = intent.getStringExtra("cert");
String path = intent.getStringExtra("path");
int index = intent.getIntExtra("index", 1);
if (!TextUtils.isEmpty(cert)) {
// Import
// String cert = intent.getStringExtra("md5");
byte[] content = Base64.decode(cert, Base64.DEFAULT);
if (content != null) {
MicroPhotoService.importPublicKey(index, content);
}
} else if (TextUtils.isEmpty(path)) {
String md5 = intent.getStringExtra("md5");
File file = new File(path);
if (file.exists() && file.isFile()) {
MicroPhotoService.importPublicKeyFile(index, path, md5);
}
}
} else if (TextUtils.equals(action, ACTION_GEN_KEYS)) {
int index = intent.getIntExtra("index", 0);
MicroPhotoService.genKeys(index);
}
}
final Activity activity = this;
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
activity.finish();
}
}, 2000);
}
}

@ -146,6 +146,14 @@ public class MicroPhotoService extends Service {
public static boolean isRunning = false;
private Runnable delayedSleep = new Runnable() {
@Override
public void run() {
Log.i(TAG, "Device Sleep");
// SysApi.sleep(getApplicationContext());
}
};
public MicroPhotoService() {
}
@Override
@ -725,6 +733,12 @@ public class MicroPhotoService extends Service {
synchronized (mWakeLocks) {
wl2 = mWakeLocks.get(name);
mWakeLocks.put(name, wl);
}
try {
mHander.removeCallbacks(delayedSleep);
} catch (Exception ex) {
// ex.printStackTrace();
}
if (wl2 != null) {
Log.i(TAG, "Release same name wakelock:" + name);
@ -742,6 +756,10 @@ public class MicroPhotoService extends Service {
try {
wl = mWakeLocks.get(name);
mWakeLocks.remove(name);
if (mWakeLocks.isEmpty()) {
mHander.postDelayed(delayedSleep, 2000);
}
} catch (Exception ex) {
ex.printStackTrace();
}
@ -866,7 +884,7 @@ public class MicroPhotoService extends Service {
// Set Listener
}
try {
mLocationManager.requestLocationUpdates(mLocateType, 100, 1, mLocationListener);
mLocationManager.requestLocationUpdates(mLocateType, 30000, 1, mLocationListener);
} catch (Exception ex) {
ex.printStackTrace();
}
@ -1074,7 +1092,9 @@ cellSignalStrengthGsm.getDbm();
public static native void setOtgState(boolean enabled);
public static native void setCam3V3Enable(boolean enabled);
public static native String getSerialNumber();
public static native void importPublicKey(String path, String md5);
public static native boolean importPublicKeyFile(int index, String path, String md5);
public static native boolean importPublicKey(int index, byte cert[]);
public static native boolean genKeys(int index);
////////////////////////GPS////////////////////
// private static final String GPS_LOCATION_NAME = android.location.LocationManager.GPS_PROVIDER;

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CertActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save