diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index f548d3f3..fe856631 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -391,7 +391,7 @@ std::mutex CPhoneDevice::m_powerLocker; long CPhoneDevice::mCameraPowerCount = 0; long CPhoneDevice::mOtgCount = 0; -CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPath, unsigned int netId, unsigned int versionCode, const std::string& nativeLibDir) : mVersionCode(versionCode), m_nativeLibraryDir(nativeLibDir) +CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPath, unsigned int netId, unsigned int versionCode, const std::string& nativeLibDir) : mVersionCode(versionCode), m_nativeLibraryDir(nativeLibDir), m_network(NULL) { mCamera = NULL; m_listener = NULL; @@ -410,6 +410,8 @@ CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPa RegisterHandlerForSignal(SIGUSR2); + LoadNetworkInfo(); + m_vm = vm; JNIEnv* env = NULL; bool didAttachThread = false; @@ -439,6 +441,8 @@ CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPa mExecHdrplusMid = env->GetMethodID(classService, "execHdrplus", "(IILjava/lang/String;Ljava/lang/String;)I"); + mSetStaticIpMid = env->GetMethodID(classService, "setStaticNetwork", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"); + mCallSysCameraMid = env->GetMethodID(classService, "callSystemCamera", "(IJ)V"); env->DeleteLocalRef(classService); @@ -492,6 +496,12 @@ CPhoneDevice::~CPhoneDevice() } m_pRecognizationCfg = NULL; } + + if (m_network != NULL) + { + delete m_network; + m_network = NULL; + } } void CPhoneDevice::SetListener(IListener* listener) @@ -1507,6 +1517,10 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< std::thread t([localPhotoInfo, path, pThis, osds]() mutable { + pThis->SetStaticIp(); + + std::this_thread::sleep_for(std::chrono::milliseconds(128)); + NET_PHOTO_INFO netPhotoInfo = { 0 }; strcpy(netPhotoInfo.ip, "192.168.19.107"); strcpy(netPhotoInfo.url, "/cgi-bin/snapshot.cgi"); @@ -3243,6 +3257,33 @@ void CPhoneDevice::UpdateSimcard(const std::string& simcard) m_simcard = simcard; } +void CPhoneDevice::SetStaticIp(const std::string& iface, const std::string& ip, const std::string& netmask, const std::string& gateway) +{ + JNIEnv* env = NULL; + jboolean ret = JNI_FALSE; + bool didAttachThread = false; + bool res = GetJniEnv(m_vm, &env, didAttachThread); + if (!res) + { + ALOGE("Failed to get JNI Env"); + } + + jstring jiface = env->NewStringUTF(iface.c_str()); + jstring jip = env->NewStringUTF(ip.c_str()); + jstring jnetmask = env->NewStringUTF(netmask.c_str()); + jstring jgw = env->NewStringUTF(gateway.c_str()); + env->CallVoidMethod(m_javaService, mSetStaticIpMid, jiface, jip, jnetmask, jgw); + env->DeleteLocalRef(jgw); + env->DeleteLocalRef(jnetmask); + env->DeleteLocalRef(jip); + env->DeleteLocalRef(jiface); + + if (didAttachThread) + { + m_vm->DetachCurrentThread(); + } +} + int CPhoneDevice::GetIceData(IDevice::ICE_INFO *iceInfo, IDevice::ICE_TAIL *iceTail, SENSOR_PARAM *sensorParam) { Collect_sensor_data(); //15s @@ -3440,3 +3481,41 @@ bool CPhoneDevice::CloseSensors(int sensortype) return 0; } +bool CPhoneDevice::LoadNetworkInfo() +{ + std::vector content; + if (!readFile(m_appPath + (APP_PATH_NETWORK), content) && !content.empty()) + { + return false; + } + + Json::CharReaderBuilder builder; + std::unique_ptr reader(builder.newCharReader()); + + Json::Value jsonVal; + const char* doc = (const char*)&(content[0]); + std::string errMsg; + if (!reader->parse(doc, doc + content.size(), &jsonVal, &errMsg)) + { + return false; + } + + if (m_network == NULL) + { + m_network = new NETWORK(); + } + GetJSONValue(jsonVal, "iface", m_network->iface); + GetJSONValue(jsonVal, "ip", m_network->ip); + GetJSONValue(jsonVal, "netmask", m_network->netmask); + GetJSONValue(jsonVal, "gateway", m_network->gateway); + + return true; +} + +void CPhoneDevice::SetStaticIp() +{ + if (m_network != NULL) + { + SetStaticIp(m_network->iface, m_network->ip, m_network->netmask, m_network->gateway); + } +} diff --git a/app/src/main/cpp/PhoneDevice.h b/app/src/main/cpp/PhoneDevice.h index 7163d622..9b938b9e 100644 --- a/app/src/main/cpp/PhoneDevice.h +++ b/app/src/main/cpp/PhoneDevice.h @@ -153,6 +153,14 @@ class CPhoneDevice : public IDevice { public: + struct NETWORK + { + std::string iface; + std::string ip; + std::string netmask; + std::string gateway; + }; + class CPhoneCamera : public NdkCamera { public: @@ -223,6 +231,7 @@ public: virtual bool CloseSensors(int sensortype); // bool CapturePhoto(unsigned char channel, unsigned char type); + bool LoadNetworkInfo(); bool GetNextScheduleItem(uint32_t tsBasedZero, uint32_t scheduleTime, vector& items); void UpdatePosition(double lon, double lat, double radius, time_t ts); @@ -310,6 +319,9 @@ protected: int CallExecv(int rotation, int frontCamera, const std::string& outputPath, const std::vector& images); + void SetStaticIp(const std::string& iface, const std::string& ip, const std::string& netmask, const std::string& gateway); + void SetStaticIp(); + protected: std::mutex m_devLocker; @@ -320,6 +332,8 @@ protected: std::string m_tfCardPath; std::string m_nativeLibraryDir; + NETWORK* m_network; + jmethodID mRegisterHeartbeatMid; jmethodID mUpdateCaptureScheduleMid; jmethodID mUpdateTimeMid; @@ -335,6 +349,7 @@ protected: jmethodID mEnableGpsMid; jmethodID mRequestPositionMid; jmethodID mExecHdrplusMid; + jmethodID mSetStaticIpMid; jmethodID mCallSysCameraMid; diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 125397c6..d0c66e33 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -1438,6 +1438,30 @@ public class MicroPhotoService extends Service { return exitCode; } + public void setStaticNetwork(String iface, String ip, String netmask, String gateway) + { + Intent intent = new Intent(); + intent.putExtra("cmd", "setnet"); + intent.putExtra("staticip", true); + intent.putExtra("iface", iface); + intent.putExtra("ip", ip); + intent.putExtra("netmask", netmask); + if (!TextUtils.isEmpty(gateway)) { + intent.putExtra("gateway", gateway); + } + // nn.putExtra("dns1", "8.8.8.8"); + // nn.putExtra("dns2", "192.168.1.1"); + sendBroadcast(getApplicationContext(), intent); + } + + public static void sendBroadcast(Context context, Intent intent) + { + intent.setAction("com.xy.xsetting.action"); + intent.setPackage("com.android.systemui"); + intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); + context.sendBroadcast(intent); + } + /* TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); // for example value of first element