diff --git a/app/src/main/cpp/GPIOControl.h b/app/src/main/cpp/GPIOControl.h index 95eca7f7..e862e2a8 100644 --- a/app/src/main/cpp/GPIOControl.h +++ b/app/src/main/cpp/GPIOControl.h @@ -108,6 +108,10 @@ #define CMD_SPI2SERIAL_POWER_EN 368 #define CMD_RS485_3V3_EN 369 +// Others +#define CMD_SET_485_EN_STATE 131 +#define CMD_SET_OTG_STATE 107 + #endif // USING_N938 diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index cee3c79b..a5351aa0 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -195,6 +195,37 @@ static inline uint32_t YUV2RGB(int nY, int nU, int nV) { return 0xff000000 | (nR << 16) | (nG << 8) | nB; } +class AutoEnv +{ +public: + AutoEnv(JavaVM* vm) + { + didAttachThread = false; + env = NULL; + m_vm = vm; + + jboolean ret = JNI_FALSE; + bool res = GetJniEnv(m_vm, &env, didAttachThread); + if (!res) + { + ALOGE("Failed to get JNI Env"); + } + } + + ~AutoEnv() + { + if (didAttachThread) + { + m_vm->DetachCurrentThread(); + } + } + +private: + JavaVM* m_vm; + JNIEnv* env; + bool didAttachThread; +}; + CPhoneDevice::CPhoneCamera::CPhoneCamera(CPhoneDevice* dev, int32_t width, int32_t height, const NdkCamera::CAMERA_PARAMS& params) : NdkCamera(width, height, params), m_dev(dev) { } @@ -1430,6 +1461,10 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< if (photoInfo.cameraType == CAM_TYPE_NET) { GpioControl::set12VEnable(true); +#ifdef USING_N938 + GpioControl::setInt(CMD_SET_NETWORK_POWER_EN, 1); + GpioControl::setInt(CMD_SET_485_EN_STATE, 1); +#endif } TurnOnCameraPower(NULL); @@ -1500,16 +1535,21 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< } else if ((mPhotoInfo.mediaType == 0) && (mPhotoInfo.cameraType == CAM_TYPE_NET)) { + XYLOG(XYLOG_SEVERITY_INFO, "Start TP on NET Camera CH=%u PR=%X PHOTOID=%u", (uint32_t)mPhotoInfo.channel, (uint32_t)mPhotoInfo.preset, mPhotoInfo.photoId); + // Start Thread CPhoneDevice* pThis = this; vector osds; osds.swap(mOsds); IDevice::PHOTO_INFO localPhotoInfo = mPhotoInfo; + pThis->SetStaticIp(); std::thread t([localPhotoInfo, path, pThis, osds]() mutable { - pThis->SetStaticIp(); + AutoEnv autoEnv(pThis->m_vm); + + std::this_thread::sleep_for(std::chrono::milliseconds(10240)); net_handle_t netHandle = pThis->GetNetHandle(); @@ -1533,9 +1573,19 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< // timeout XYLOG(XYLOG_SEVERITY_ERROR, "Ethernet not existing CH=%u PR=%X PHOTOID=%u", (uint32_t)localPhotoInfo.channel, (uint32_t)localPhotoInfo.preset, localPhotoInfo.photoId); pThis->TakePhotoCb(0, localPhotoInfo, "", 0); + + TurnOffOtg(NULL); + GpioControl::set12VEnable(false); +#ifdef USING_N938 + GpioControl::setInt(CMD_SET_NETWORK_POWER_EN, 0); + GpioControl::setInt(CMD_SET_485_EN_STATE, 0); +#endif return; } - + else + { + XYLOG(XYLOG_SEVERITY_INFO, "Ethernet is Available CH=%u PR=%X PHOTOID=%u", (uint32_t)localPhotoInfo.channel, (uint32_t)localPhotoInfo.preset, localPhotoInfo.photoId); + } NET_PHOTO_INFO netPhotoInfo = { netHandle, 0 }; if (localPhotoInfo.vendor == 1) { @@ -1557,6 +1607,13 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< { XYLOG(XYLOG_SEVERITY_ERROR, "Vendor(%u) not Supported CH=%u PR=%X PHOTOID=%u", (uint32_t)localPhotoInfo.vendor, (uint32_t)localPhotoInfo.channel, (unsigned int)localPhotoInfo.preset, localPhotoInfo.photoId); pThis->TakePhotoCb(0, localPhotoInfo, "", 0); + + TurnOffOtg(NULL); + GpioControl::set12VEnable(false); +#ifdef USING_N938 + GpioControl::setInt(CMD_SET_NETWORK_POWER_EN, 0); + GpioControl::setInt(CMD_SET_485_EN_STATE, 0); +#endif return; } @@ -1566,18 +1623,40 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< strcpy(netPhotoInfo.outputPath, path.c_str()); if (!localPhotoInfo.userName.empty()) { - strncpy(netPhotoInfo.userName, localPhotoInfo.userName.c_str(), std::min(sizeof(netPhotoInfo.userName) - 1, localPhotoInfo.userName.size())); + size_t len = std::min(sizeof(netPhotoInfo.userName) - 1, localPhotoInfo.userName.size()); + strncpy(netPhotoInfo.userName, localPhotoInfo.userName.c_str(), len); } if (!localPhotoInfo.password.empty()) { - strncpy(netPhotoInfo.password, localPhotoInfo.password.c_str(), std::min(sizeof(netPhotoInfo.password) - 1, localPhotoInfo.password.size())); + size_t len = std::min(sizeof(netPhotoInfo.password) - 1, localPhotoInfo.password.size()); + strncpy(netPhotoInfo.password, localPhotoInfo.password.c_str(), len); } // strcpy(netPhotoInfo.interface, "eth0"); std::vector img; - bool res = requestCapture(localPhotoInfo.channel, localPhotoInfo.preset, netPhotoInfo, img); - if (res) + bool netCaptureResult = false; + for (int idx = 0; idx < 3; idx++) + { + netHandle = pThis->GetNetHandle(); + netPhotoInfo.netHandle = netHandle; + + XYLOG(XYLOG_SEVERITY_INFO, "NetCapture %d NetHandle=%lld PHOTOID=%u", idx, netHandle, localPhotoInfo.photoId); + + netCaptureResult = requestCapture(localPhotoInfo.channel, localPhotoInfo.preset, netPhotoInfo, img); + if (netCaptureResult) + { + break; + } + } + + TurnOffOtg(NULL); + GpioControl::set12VEnable(false); +#ifdef USING_N938 + GpioControl::setInt(CMD_SET_NETWORK_POWER_EN, 0); + GpioControl::setInt(CMD_SET_485_EN_STATE, 0); +#endif + if (netCaptureResult) { time_t takingTime = time(NULL); if (localPhotoInfo.remedy != 0) @@ -1589,8 +1668,6 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< } localPhotoInfo.photoTime = takingTime; - // GpioControl::setOtgState(false); - GpioControl::set12VEnable(false); // Notify to take next photo pThis->TakePhotoCb(1, localPhotoInfo, "", takingTime); @@ -1599,11 +1676,12 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector< #ifdef _DEBUG // cv::imwrite("/sdcard/com.xypower.mpapp/tmp/netimg2.jpg", rgb); #endif - - res = pThis->PostProcessPhoto(localPhotoInfo, osds, path, "", rgb); + netCaptureResult = pThis->PostProcessPhoto(localPhotoInfo, osds, path, "", rgb); } else { + XYLOG(XYLOG_SEVERITY_ERROR, "Faile to TP on NET Camera CH=%u PR=%X PHOTOID=%u URL=http://%s%s", (uint32_t)localPhotoInfo.channel, (uint32_t)localPhotoInfo.preset, + localPhotoInfo.photoId, netPhotoInfo.ip, netPhotoInfo.url); pThis->TakePhotoCb(0, localPhotoInfo, "", 0); } }); @@ -3282,67 +3360,29 @@ void CPhoneDevice::UpdatePosition(double lon, double lat, double radius, time_t void CPhoneDevice::TurnOnCameraPower(JNIEnv* env) { m_powerLocker.lock(); - if (mCameraPowerCount == 0) - { - GpioControl::setCam3V3Enable(true); - } - mCameraPowerCount++; + GpioControl::setCam3V3Enable(true); m_powerLocker.unlock(); } void CPhoneDevice::TurnOffCameraPower(JNIEnv* env) { - bool turnedOff = false; m_powerLocker.lock(); - if (mCameraPowerCount > 0) - { - mCameraPowerCount--; - if (mCameraPowerCount == 0) - { - GpioControl::setCam3V3Enable(false); - turnedOff = true; - } - } + GpioControl::setCam3V3Enable(false); m_powerLocker.unlock(); - - if (turnedOff) - { - XYLOG(XYLOG_SEVERITY_INFO, "CAM PWR Turned Off"); - } } void CPhoneDevice::TurnOnOtg(JNIEnv* env) { m_powerLocker.lock(); - if (mOtgCount == 0) - { - ALOGD("setOtgState 1"); - GpioControl::setOtgState(true); - } - mOtgCount++; + GpioControl::setInt(CMD_SET_OTG_STATE, 1); m_powerLocker.unlock(); } void CPhoneDevice::TurnOffOtg(JNIEnv* env) { - bool turnedOff = false; m_powerLocker.lock(); - if (mOtgCount > 0) - { - mOtgCount--; - if (mOtgCount == 0) - { - // ALOGD("setOtgState 0"); - GpioControl::setOtgState(false); - turnedOff = true; - } - } + GpioControl::setInt(CMD_SET_OTG_STATE, 0); m_powerLocker.unlock(); - - if (turnedOff) - { - XYLOG(XYLOG_SEVERITY_INFO, "OTG PWR Turned Off"); - } } void CPhoneDevice::UpdateSignalLevel(int signalLevel) @@ -3361,6 +3401,8 @@ void CPhoneDevice::UpdateEthernet(net_handle_t nethandle, bool available) m_devLocker.lock(); m_netHandle = available ? nethandle : NETWORK_UNSPECIFIED; m_devLocker.unlock(); + + XYLOG(XYLOG_SEVERITY_WARNING, "NET Handle: %lld", available ? (uint64_t)nethandle : 0); } net_handle_t CPhoneDevice::GetNetHandle() const diff --git a/app/src/main/cpp/netcamera/httpclient.cpp b/app/src/main/cpp/netcamera/httpclient.cpp index 1d2deaef..ddb325d1 100644 --- a/app/src/main/cpp/netcamera/httpclient.cpp +++ b/app/src/main/cpp/netcamera/httpclient.cpp @@ -59,7 +59,7 @@ int DoGetRequest(const char* url, const char* userName, const char* password, ne curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data); // 接收数据时超时设置,如果5秒内数据未接收完,直接退出 #ifndef NDEBUG - curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60); #else curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60); #endif diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 386f3c68..fba0cc2e 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -25,6 +25,8 @@ import android.location.LocationListener; import android.location.LocationManager; import android.location.LocationProvider; import android.net.ConnectivityManager; +import android.net.LinkAddress; +import android.net.LinkProperties; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkInfo; @@ -1449,12 +1451,29 @@ public class MicroPhotoService extends Service { @Override public void onLost(Network network) { - Log.d(TAG, "Network Lost " + network.toString()); + infoLog("Network Lost " + network.toString()); updateEhernet(mNativeHandle, network.getNetworkHandle(), false); } @Override public void onAvailable(final Network network) { - Log.d(TAG, "Network Available " + network.toString()); + String ip = ""; + try { + NetworkInfo ni = mConnectivityManager.getNetworkInfo(network); + LinkProperties lp = mConnectivityManager.getLinkProperties(network); + if (lp != null) { + List addresses = lp.getLinkAddresses(); + if (addresses != null && addresses.size() > 0) { + InetAddress inetAddress = addresses.get(0).getAddress(); + if (inetAddress != null) { + ip = inetAddress.getHostAddress(); + } + } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + infoLog("Network Available " + network.toString() + " IP=" + ip); updateEhernet(mNativeHandle, network.getNetworkHandle(), true); } };