@ -24,12 +24,15 @@
# include <sys/system_properties.h>
# include <media/NdkImage.h>
# include <mat.h>
# include <string.h>
# ifdef USING_HDRPLUS
# include <hdrplus/hdrplus_pipeline.h>
# include <hdrplus2/include/HDRPlus.h>
# endif
# include "netcamera/netcamera.h"
# include <fcntl.h>
# include <filesystem>
# include <cstdio>
@ -192,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 )
{
}
@ -254,7 +288,6 @@ void CPhoneDevice::CPhoneCamera::onDisconnected(ACameraDevice* device)
}
}
CPhoneDevice : : CJpegCamera : : CJpegCamera ( CPhoneDevice * dev , int32_t width , int32_t height , const std : : string & path , const NdkCamera : : CAMERA_PARAMS & params ) : CPhoneDevice : : CPhoneCamera ( dev , width , height , params ) , m_path ( path )
{
}
@ -388,7 +421,8 @@ 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 ) , m_netHandle ( NETWORK_UNSPECIFIED )
{
mCamera = NULL ;
m_listener = NULL ;
@ -404,9 +438,15 @@ CPhoneDevice::CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPa
m_signalLevel = 0 ;
m_signalLevelUpdateTime = time ( NULL ) ;
mBuildTime = 0 ;
m_cameraStatus = false ;
m_sensorsStatus = false ;
m_lastTime = 0 ;
m_shouldStopWaiting = false ;
RegisterHandlerForSignal ( SIGUSR2 ) ;
LoadNetworkInfo ( ) ;
m_vm = vm ;
JNIEnv * env = NULL ;
bool didAttachThread = false ;
@ -436,6 +476,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 ) ;
@ -489,6 +531,12 @@ CPhoneDevice::~CPhoneDevice()
}
m_pRecognizationCfg = NULL ;
}
if ( m_network ! = NULL )
{
delete m_network ;
m_network = NULL ;
}
}
void CPhoneDevice : : SetListener ( IListener * listener )
@ -1167,6 +1215,9 @@ bool CPhoneDevice::RegisterHandlerForSignal(int sig)
void CPhoneDevice : : handleTimer ( union sigval v )
{
# ifdef _DEBUG
setThreadName ( " bztimer " ) ;
# endif
TIMER_CONTEXT * context = ( TIMER_CONTEXT * ) ( v . sival_ptr ) ;
context - > device - > handleTimerImpl ( context ) ;
}
@ -1402,6 +1453,25 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector<
mPath = path ;
mOsds = osds ;
bool res = false ;
if ( photoInfo . cameraType = = CAM_TYPE_USB | | photoInfo . cameraType = = CAM_TYPE_NET )
{
TurnOnOtg ( NULL ) ;
}
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 ) ;
res = true ;
if ( ( mPhotoInfo . mediaType = = 0 ) & & ( ( mPhotoInfo . cameraType = = CAM_TYPE_MIPI ) | | ( mPhotoInfo . cameraType = = CAM_TYPE_USB ) ) )
{
NdkCamera : : CAMERA_PARAMS params ;
memset ( & params , 0 , sizeof ( params ) ) ;
@ -1437,17 +1507,6 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector<
}
# endif
bool res = false ;
if ( photoInfo . usbCamera )
{
TurnOnOtg ( NULL ) ;
}
TurnOnCameraPower ( NULL ) ;
res = true ;
if ( mPhotoInfo . mediaType = = 0 & & mPhotoInfo . usingSysCamera = = 0 )
{
mCamera = new CPhoneCamera ( this , photoInfo . width , photoInfo . height , params ) ;
// mCamera = new CJpegCamera(this, photoInfo.width, photoInfo.height, mPath, params);
if ( mCamera - > open ( to_string ( mPhotoInfo . cameraId ) ) = = 0 )
@ -1475,6 +1534,176 @@ 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 < IDevice : : OSD_INFO > osds ;
osds . swap ( mOsds ) ;
IDevice : : PHOTO_INFO localPhotoInfo = mPhotoInfo ;
pThis - > SetStaticIp ( ) ;
std : : thread t ( [ localPhotoInfo , path , pThis , osds ] ( ) mutable
{
// AutoEnv autoEnv(pThis->m_vm);
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 10240 ) ) ;
net_handle_t netHandle = pThis - > GetNetHandle ( ) ;
if ( netHandle = = 0 )
{
// Wait about 10s
for ( int idx = 0 ; idx < 84 ; idx + + )
{
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 128 ) ) ;
netHandle = pThis - > GetNetHandle ( ) ;
if ( netHandle ! = 0 )
{
break ;
}
}
}
if ( netHandle = = 0 )
{
// 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 )
{
// Hai Kang
snprintf ( netPhotoInfo . url , sizeof ( netPhotoInfo . url ) , " /ISAPI/Streaming/channels/%u/picture " , ( uint32_t ) localPhotoInfo . channel ) ;
}
else if ( localPhotoInfo . vendor = = 2 )
{
// Hang Yu
strcpy ( netPhotoInfo . url , " /cgi-bin/snapshot.cgi " ) ;
}
else if ( localPhotoInfo . vendor = = 3 )
{
// Yu Shi
int streamSid = 0 ; // should put into config
snprintf ( netPhotoInfo . url , sizeof ( netPhotoInfo . url ) , " /LAPI/V1.0/Channels/%u/Media/Video/Streams/%d/Snapshot " , ( uint32_t ) localPhotoInfo . channel , streamSid ) ;
}
else
{
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 ;
}
struct in_addr addr ;
addr . s_addr = localPhotoInfo . ip ;
strcpy ( netPhotoInfo . ip , inet_ntoa ( addr ) ) ;
strcpy ( netPhotoInfo . outputPath , path . c_str ( ) ) ;
if ( ! localPhotoInfo . userName . empty ( ) )
{
size_t len = std : : min < size_t > ( sizeof ( netPhotoInfo . userName ) - 1 , localPhotoInfo . userName . size ( ) ) ;
strncpy ( netPhotoInfo . userName , localPhotoInfo . userName . c_str ( ) , len ) ;
}
if ( ! localPhotoInfo . password . empty ( ) )
{
size_t len = std : : min < size_t > ( sizeof ( netPhotoInfo . password ) - 1 , localPhotoInfo . password . size ( ) ) ;
strncpy ( netPhotoInfo . password , localPhotoInfo . password . c_str ( ) , len ) ;
}
// strcpy(netPhotoInfo.interface, "eth0");
std : : vector < uint8_t > img ;
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 )
{
if ( ( takingTime - localPhotoInfo . scheduleTime ) > 30 )
{
takingTime = localPhotoInfo . scheduleTime + localPhotoInfo . channel * 2 ;
}
}
localPhotoInfo . photoTime = takingTime ;
// Notify to take next photo
pThis - > TakePhotoCb ( 1 , localPhotoInfo , " " , takingTime ) ;
cv : : Mat rgb = cv : : imdecode ( cv : : Mat ( img ) , cv : : IMREAD_COLOR ) ;
# ifdef _DEBUG
// cv::imwrite("/sdcard/com.xypower.mpapp/tmp/netimg2.jpg", rgb);
# endif
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 ) ;
}
} ) ;
t . detach ( ) ;
}
else if ( mPhotoInfo . mediaType = = 0 & & ( mPhotoInfo . cameraType = = CAM_TYPE_SERIAL ) )
{
if ( photoInfo . preset ! = 0 & & photoInfo . preset ! = 0xFF )
{
CameraPhotoCmd ( time ( NULL ) , photoInfo . channel , MOVE_PRESETNO , 0 , photoInfo . preset ) ;
std : : this_thread : : sleep_for ( std : : chrono : : seconds ( 2 ) ) ;
}
time_t ts = time ( NULL ) ;
if ( ! GetPTZSensorsStatus ( ) & & ! GetCameraStatus ( ) )
{
OpenPTZSensors ( 120 ) ;
}
CameraPhotoCmd ( ts , photoInfo . channel , 0 , 6 , 0 ) ;
res = TakePTZPhotoCb ( 3 , photoInfo ) ;
}
else if ( mPhotoInfo . usingSysCamera = = 1 )
{
JNIEnv * env = NULL ;
@ -1556,6 +1785,84 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector<
return res ;
}
bool CPhoneDevice : : OpenPTZSensors ( int sec )
{
{
std : : lock_guard < std : : mutex > lock ( m_cameraLocker ) ;
// std::unique_lock<std::mutex> lock(m_cameraLocker);
if ( ! m_cameraStatus & & ! m_sensorsStatus )
{
m_sensorsStatus = true ;
OpenSensors ( MAIN_POWER_OPEN ) ;
OpenSensors ( CAMERA_SENSOR_OPEN ) ;
}
}
if ( m_sensorsStatus & & ! m_cameraStatus )
{
auto start = std : : chrono : : steady_clock : : now ( ) ;
while ( std : : chrono : : steady_clock : : now ( ) - start < std : : chrono : : seconds ( sec ) )
{
if ( m_shouldStopWaiting . load ( ) )
{
CloseSensors ( CAMERA_SENSOR_OPEN ) ;
CloseSensors ( MAIN_POWER_OPEN ) ;
m_cameraStatus = false ;
m_sensorsStatus = false ;
m_shouldStopWaiting . store ( false ) ;
return false ;
}
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 200 ) ) ;
}
}
// if(m_sensorsStatus && !m_cameraStatus)
// std::this_thread::sleep_for(std::chrono::seconds(sec));
{
std : : lock_guard < std : : mutex > lock ( m_cameraLocker ) ;
// std::unique_lock<std::mutex> lock(m_cameraLocker);
if ( ! m_cameraStatus & & m_sensorsStatus )
{
m_cameraStatus = true ;
}
}
return m_cameraStatus ;
}
bool CPhoneDevice : : ClosePTZSensors ( )
{
if ( m_sensorsStatus & & ! m_cameraStatus )
{
m_shouldStopWaiting . store ( true ) ;
}
else if ( m_sensorsStatus & & m_cameraStatus )
{
std : : lock_guard < std : : mutex > lock ( m_cameraLocker ) ;
// std::unique_lock<std::mutex> lock(m_cameraLocker);
CloseSensors ( CAMERA_SENSOR_OPEN ) ;
CloseSensors ( MAIN_POWER_OPEN ) ;
m_cameraStatus = false ;
m_sensorsStatus = false ;
}
return true ;
}
bool CPhoneDevice : : GetPTZSensorsStatus ( )
{
std : : lock_guard < std : : mutex > lock ( m_cameraLocker ) ;
return m_sensorsStatus ;
}
bool CPhoneDevice : : GetCameraStatus ( )
{
std : : lock_guard < std : : mutex > lock ( m_cameraLocker ) ;
return m_cameraStatus ;
}
bool CPhoneDevice : : CloseCamera ( )
{
if ( mCamera ! = NULL )
@ -1569,7 +1876,7 @@ bool CPhoneDevice::CloseCamera()
return true ;
}
void CPhoneDevice : : CloseCamera2 ( CPhoneDevice : : CPhoneCamera * camera , unsigned int photoId , bool turnOffOtg )
void CPhoneDevice : : CloseCamera2 ( CPhoneDevice : : CPhoneCamera * camera , unsigned int photoId , unsigned char cameraType )
{
XYLOG ( XYLOG_SEVERITY_DEBUG , " TP: Start CloseCamera PHOTOID=%u " , photoId ) ;
// std::this_thread::sleep_for(std::chrono::milliseconds(16));
@ -1580,11 +1887,16 @@ void CPhoneDevice::CloseCamera2(CPhoneDevice::CPhoneCamera* camera, unsigned int
}
XYLOG ( XYLOG_SEVERITY_DEBUG , " TP: Will Turn Off Power PHOTOID=%u " , photoId ) ;
if ( turnOffOtg )
if ( cameraType = = CAM_TYPE_NET )
{
TurnOffOtg( NULL ) ;
GpioControl: : set12VEnable ( false ) ;
}
TurnOffCameraPower ( NULL ) ;
if ( cameraType = = CAM_TYPE_USB | | cameraType = = CAM_TYPE_NET )
{
GpioControl : : setOtgState ( false ) ;
}
GpioControl : : setCam3V3Enable ( false ) ;
XYLOG ( XYLOG_SEVERITY_DEBUG , " TP: End Turn Off Power PHOTOID=%u " , photoId ) ;
XYLOG ( XYLOG_SEVERITY_DEBUG , " TP: CloseCamera PHOTOID=%u " , photoId ) ;
@ -1665,7 +1977,7 @@ bool CPhoneDevice::onOneCapture(std::shared_ptr<ACameraMetadata> characteristics
media_status_t mstatus ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , photoInfo . photoId , turnOffOtg ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , photoInfo . photoId , photoInfo. cameraType ) ;
m_threadClose . swap ( closeThread ) ;
if ( closeThread . joinable ( ) )
{
@ -1777,7 +2089,7 @@ bool CPhoneDevice::onBurstCapture(std::shared_ptr<ACameraMetadata> characteristi
CPhoneCamera * pCamera = mCamera ;
mCamera = NULL ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , photoInfo . photoId , turnOffOtg ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , photoInfo . photoId , photoInfo. cameraType ) ;
m_threadClose . swap ( closeThread ) ;
if ( closeThread . joinable ( ) )
{
@ -2118,7 +2430,7 @@ bool CPhoneDevice::onBurstCapture(std::shared_ptr<ACameraMetadata> characteristi
int32_t planeCount = 0 ;
mstatus = AImage_getNumberOfPlanes ( spImage . get ( ) , & planeCount ) ;
AASSERT ( status = = AMEDIA_OK & & planeCount = = 1 , " Error: getNumberOfPlanes() planeCount = %d " , planeCount ) ;
AASSERT ( m status = = AMEDIA_OK & & planeCount = = 1 , " Error: getNumberOfPlanes() planeCount = %d " , planeCount ) ;
uint8_t * planeData = NULL ;
int planeDataLen = 0 ;
@ -2220,7 +2532,7 @@ bool CPhoneDevice::onBurstCapture(std::shared_ptr<ACameraMetadata> characteristi
frames . clear ( ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , photoInfo . photoId , turnOffOtg ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , photoInfo . photoId , mPhotoInfo. cameraType ) ;
m_threadClose . swap ( closeThread ) ;
if ( closeThread . joinable ( ) )
{
@ -2963,7 +3275,7 @@ bool CPhoneDevice::OnCaptureReady(bool photoOrVideo, bool result, cv::Mat& mat,
mCamera = NULL ;
bool turnOffOtg = ( mPhotoInfo . usbCamera ! = 0 ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , mPhotoInfo . photoId , turnOffOtg ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , mPhotoInfo . photoId , mPhotoInfo. cameraType ) ;
m_threadClose . swap ( closeThread ) ;
if ( closeThread . joinable ( ) )
{
@ -2991,7 +3303,7 @@ bool CPhoneDevice::OnVideoReady(bool photoOrVideo, bool result, const char* path
TakePhotoCb ( result ? 3 : 0 , mPhotoInfo , fullPath , time ( NULL ) , objs ) ;
bool turnOffOtg = ( mPhotoInfo . usbCamera ! = 0 ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , mPhotoInfo . photoId , turnOffOtg ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , mPhotoInfo . photoId , mPhotoInfo. cameraType ) ;
m_threadClose . swap ( closeThread ) ;
}
else
@ -3008,7 +3320,7 @@ bool CPhoneDevice::OnVideoReady(bool photoOrVideo, bool result, const char* path
TakePhotoCb ( result ? 3 : 0 , mPhotoInfo , fullPath , time ( NULL ) , objs ) ;
bool turnOffOtg = ( mPhotoInfo . usbCamera ! = 0 ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , mPhotoInfo . photoId , turnOffOtg ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , mPhotoInfo . photoId , mPhotoInfo. cameraType ) ;
m_threadClose . swap ( closeThread ) ;
}
@ -3030,7 +3342,7 @@ void CPhoneDevice::onError(const std::string& msg)
TakePhotoCb ( 0 , mPhotoInfo , mPath , 0 ) ;
bool turnOffOtg = ( mPhotoInfo . usbCamera ! = 0 ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , mPhotoInfo . photoId , turnOffOtg ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , mPhotoInfo . photoId , mPhotoInfo. cameraType ) ;
// closeThread.detach();
m_threadClose . swap ( closeThread ) ;
}
@ -3049,7 +3361,7 @@ void CPhoneDevice::onDisconnected(ACameraDevice* device)
TakePhotoCb ( 0 , mPhotoInfo , mPath , 0 ) ;
bool turnOffOtg = ( mPhotoInfo . usbCamera ! = 0 ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , mPhotoInfo . photoId , turnOffOtg ) ;
std : : thread closeThread ( & CPhoneDevice : : CloseCamera2 , this , pCamera , mPhotoInfo . photoId , mPhotoInfo. cameraType ) ;
// closeThread.detach();
m_threadClose . swap ( closeThread ) ;
}
@ -3086,67 +3398,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 + + ;
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 ;
}
}
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 )
@ -3160,14 +3434,65 @@ void CPhoneDevice::UpdateSimcard(const std::string& simcard)
m_simcard = simcard ;
}
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
{
net_handle_t nethandle = NETWORK_UNSPECIFIED ;
m_devLocker . lock ( ) ;
nethandle = m_netHandle ;
m_devLocker . unlock ( ) ;
return nethandle ;
}
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 )
{
m_tempData . instantaneous_windspeed = 0xff ;
m_tempData . air_temperature = 0xff ;
m_tempData . instantaneous_winddirection = 0xff ;
m_tempData . humidity = 0xff ;
Collect_sensor_data ( ) ; //15s
Data_DEF airt ;
//++等值覆冰厚度, 综合悬挂载荷, 不均衡张力差 置0
iceInfo - > equal_icethickness = 0 ;
iceInfo - > tension = 0 ;
iceInfo - > tension_difference = 0 ;
iceInfo - > equal_icethickness = 0xff ;
iceInfo - > tension = 0xff ;
iceInfo - > tension_difference = 0xff ;
int pullno = 0 ;
int angleno = 0 ;
@ -3176,47 +3501,130 @@ int CPhoneDevice::GetIceData(IDevice::ICE_INFO *iceInfo, IDevice::ICE_TAIL *iceT
if ( sensorParam [ num ] . SensorsType = = RALLY_PROTOCOL )
{
GetPullValue ( num , & airt ) ;
if ( airt . AiState = = 2 )
iceInfo - > t_sensor_data [ pullno ] . original_tension = airt . EuValue ;
else
iceInfo - > t_sensor_data [ pullno ] . original_tension = 0xff ;
pullno + + ;
} else if ( sensorParam [ num ] . SensorsType = = SLANT_PROTOCOL )
{
GetAngleValue ( num , & airt , 0 ) ;
if ( airt . AiState = = 2 )
iceInfo - > t_sensor_data [ angleno ] . deflection_angle = airt . EuValue ;
else
iceInfo - > t_sensor_data [ angleno ] . deflection_angle = 0xff ;
GetAngleValue ( num , & airt , 1 ) ;
if ( airt . AiState = = 2 )
iceInfo - > t_sensor_data [ angleno ] . windage_yaw_angle = airt . EuValue ;
else
iceInfo - > t_sensor_data [ angleno ] . windage_yaw_angle = 0xff ;
angleno + + ;
}
}
{
std : : lock_guard < std : : mutex > lock ( m_dataLocker ) ;
GetWindSpeedData ( & airt ) ;
if ( airt . AiState = = 2 )
{
iceTail - > instantaneous_windspeed = airt . EuValue ;
m_tempData . instantaneous_windspeed = iceTail - > instantaneous_windspeed ;
} else
{
iceTail - > instantaneous_windspeed = m_tempData . instantaneous_windspeed ;
m_tempData . instantaneous_windspeed = 0xff ;
}
GetWindDirectionData ( & airt ) ;
iceTail - > instantaneous_winddirection = airt . EuValue ; //需求无符号整数给出浮点数
if ( airt . AiState = = 2 )
{
iceTail - > instantaneous_winddirection = airt . EuValue ;
m_tempData . instantaneous_winddirection = iceTail - > instantaneous_winddirection ;
} else
{
iceTail - > instantaneous_winddirection = m_tempData . instantaneous_winddirection ;
m_tempData . instantaneous_winddirection = 0xff ;
}
GetAirTempData ( & airt ) ;
if ( airt . AiState = = 2 ) {
iceTail - > air_temperature = airt . EuValue ;
m_tempData . air_temperature = iceTail - > air_temperature ;
} else
{
iceTail - > air_temperature = m_tempData . air_temperature ;
m_tempData . air_temperature = 0xff ;
}
GetHumidityData ( & airt ) ;
iceTail - > humidity = airt . EuValue ; //需求无符号整数给出浮点数
return true ;
if ( airt . AiState = = 2 )
{
iceTail - > humidity = airt . EuValue ;
m_tempData . humidity = iceTail - > humidity ;
} else
{
iceTail - > humidity = m_tempData . humidity ;
m_tempData . humidity = 0xff ;
}
}
return true ;
}
int CPhoneDevice : : GetWData ( IDevice : : WEATHER_INFO * weatherInfo )
{
m_tempData . instantaneous_windspeed = 0xff ;
m_tempData . air_temperature = 0xff ;
m_tempData . instantaneous_winddirection = 0xff ;
m_tempData . humidity = 0xff ;
Collect_sensor_data ( ) ; //15s
Data_DEF airt ;
GetWeatherData ( & airt , 0 ) ;
weatherInfo - > air_temperature = airt . EuValue ;
GetWeatherData ( & airt , 1 ) ;
weatherInfo - > humidity = airt . EuValue ;
{
std : : lock_guard < std : : mutex > lock ( m_dataLocker ) ;
GetWeatherData ( & airt , 2 ) ;
if ( airt . AiState = = 2 )
{
weatherInfo - > avg_windspeed_10min = airt . EuValue ;
weatherInfo - > extreme_windspeed = airt . EuValue ;
weatherInfo - > standard_windspeed = airt . EuValue ;
m_tempData . instantaneous_windspeed = weatherInfo - > avg_windspeed_10min ;
} else
{
weatherInfo - > avg_windspeed_10min = m_tempData . instantaneous_windspeed ;
weatherInfo - > extreme_windspeed = m_tempData . instantaneous_windspeed ;
weatherInfo - > standard_windspeed = m_tempData . instantaneous_windspeed ;
m_tempData . instantaneous_windspeed = 0xff ;
}
GetWeatherData ( & airt , 3 ) ;
if ( airt . AiState = = 2 )
{
weatherInfo - > avg_winddirection_10min = airt . EuValue ;
m_tempData . instantaneous_winddirection = weatherInfo - > avg_winddirection_10min ;
} else
{
weatherInfo - > avg_winddirection_10min = m_tempData . instantaneous_winddirection ;
m_tempData . instantaneous_winddirection = 0xff ;
}
GetWeatherData ( & airt , 0 ) ;
if ( airt . AiState = = 2 )
{
weatherInfo - > air_temperature = airt . EuValue ;
m_tempData . air_temperature = weatherInfo - > air_temperature ;
} else
{
weatherInfo - > air_temperature = m_tempData . air_temperature ;
m_tempData . air_temperature = 0xff ;
}
GetWeatherData ( & airt , 1 ) ;
if ( airt . AiState = = 2 )
{
weatherInfo - > humidity = airt . EuValue ;
m_tempData . humidity = weatherInfo - > humidity ;
} else
{
weatherInfo - > humidity = m_tempData . humidity ;
m_tempData . humidity = 0xff ;
}
GetWeatherData ( & airt , 4 ) ;
if ( airt . AiState = = 2 )
weatherInfo - > precipitation = airt . EuValue ;
@ -3226,6 +3634,8 @@ int CPhoneDevice::GetWData(IDevice::WEATHER_INFO *weatherInfo)
GetWeatherData ( & airt , 6 ) ;
if ( airt . AiState = = 2 )
weatherInfo - > radiation_intensity = airt . EuValue ;
}
return true ;
}
@ -3236,41 +3646,59 @@ bool CPhoneDevice::OpenSensors(int sensortype)
GpioControl : : set12VEnable ( true ) ;
GpioControl : : setCam3V3Enable ( true ) ;
GpioControl : : setRS485Enable ( true ) ;
GpioControl : : setInt ( CMD_SET_SPI_POWER , 1 ) ;
// GpioControl::setInt(CMD_SET_485_EN_STATE, 1); // 打开RS485电源
# ifndef USING_N938
# ifndef USING_PLZ
GpioControl : : setInt ( CMD_SET_485_EN_STATE , 1 ) ;
# else
GpioControl : : setInt ( CMD_SET_485_ENABLE , 1 ) ;
# endif
# else
GpioControl : : setInt ( CMD_SPI2SERIAL_POWER_EN , 1 ) ;
GpioControl : : setInt ( CMD_RS485_3V3_EN , 1 ) ;
# endif
GpioControl : : setInt ( CMD_SET_SPI_POWER , 1 ) ;
}
if ( sensortype = = CAMERA_SENSOR_OPEN )
{
# ifndef USING_N938
# ifndef USING_PLZ
# else
GpioControl : : setInt ( CMD_SET_PTZ_PWR_ENABLE , 1 ) ;
# endif
# else
GpioControl : : setInt ( CMD_SET_PIC1_POWER , 1 ) ;
GpioControl : : setInt ( CMD_SET_485_EN4 , 1 ) ;
# endif
// GpioControl::setInt(CMD_SET_CAM_3V3_EN_STATE, 1); // 打开3.3V电压
// GpioControl::setInt(CMD_SET_3V3_PWR_ENABLE, 1);
# ifndef USING_N938
GpioControl : : setInt ( CMD_SET_PTZ_PWR_ENABLE , 1 ) ;
# endif
}
if ( sensortype = = WEATHER_SENSOR_OPEN | | sensortype = = ICETHICK_SENSOR_OPEN )
if ( sensortype = = WEATHER_SENSOR_OPEN )
{
# ifndef USING_N938
# else
GpioControl : : setInt ( CMD_SET_WTH_POWER , 1 ) ;
GpioControl : : setInt ( CMD_SET_485_EN3 , 1 ) ;
# endif
}
if ( sensortype = = ICETHICK_SENSOR_OPEN )
{
# ifndef USING_N938
# else
GpioControl : : setInt ( CMD_SET_PULL_POWER , 1 ) ;
GpioControl : : setInt ( CMD_SET_ANGLE_POWER , 1 ) ;
GpioControl : : setInt ( CMD_SET_485_EN1 , 1 ) ;
GpioControl : : setInt ( CMD_SET_485_EN0 , 1 ) ;
# endif
}
if ( sensortype = = OTHER_SENSOR )
{
# ifndef USING_N938
# else
GpioControl : : setInt ( CMD_SET_OTHER_POWER , 1 ) ;
GpioControl : : setInt ( CMD_SET_485_EN2 , 1 ) ;
# endif
}
return 0 ;
}
@ -3285,7 +3713,11 @@ bool CPhoneDevice::CloseSensors(int sensortype)
GpioControl : : setRS485Enable ( false ) ;
// GpioControl::setInt(CMD_SET_485_EN_STATE, 0);
# ifndef USING_N938
# ifndef USING_PLZ
GpioControl : : setInt ( CMD_SET_485_EN_STATE , 0 ) ;
# else
GpioControl : : setInt ( CMD_SET_485_ENABLE , 0 ) ;
# endif
# else
GpioControl : : setInt ( CMD_SPI2SERIAL_POWER_EN , 0 ) ;
GpioControl : : setInt ( CMD_RS485_3V3_EN , 0 ) ;
@ -3294,30 +3726,84 @@ bool CPhoneDevice::CloseSensors(int sensortype)
}
if ( sensortype = = CAMERA_SENSOR_OPEN )
{
# ifdef USING_N938
GpioControl : : setInt ( CMD_SET_PIC1_POWER , 0 ) ;
GpioControl : : setInt ( CMD_SET_485_EN4 , 0 ) ;
// GpioControl::setInt(CMD_SET_CAM_3V3_EN_STATE, 0);
# endif
# ifndef USING_N938
GpioControl : : setInt ( CMD_SET_3V3_PWR_ENABLE , 0 ) ;
// GpioControl::setInt(CMD_SET_3V3_PWR_ENABLE, 0);
# ifndef USING_PLZ
# else
GpioControl : : setInt ( CMD_SET_PTZ_PWR_ENABLE , 0 ) ;
# endif
# endif
}
if ( sensortype = = WEATHER_SENSOR_OPEN | | sensortype = = ICETHICK_SENSOR_OPEN )
if ( sensortype = = WEATHER_SENSOR_OPEN )
{
# ifndef USING_N938
# else
GpioControl : : setInt ( CMD_SET_WTH_POWER , 0 ) ;
GpioControl : : setInt ( CMD_SET_485_EN3 , 0 ) ;
# endif
}
if ( sensortype = = ICETHICK_SENSOR_OPEN )
{
# ifndef USING_N938
# else
GpioControl : : setInt ( CMD_SET_PULL_POWER , 0 ) ;
GpioControl : : setInt ( CMD_SET_ANGLE_POWER , 0 ) ;
GpioControl : : setInt ( CMD_SET_485_EN1 , 0 ) ;
GpioControl : : setInt ( CMD_SET_485_EN0 , 0 ) ;
# endif
}
if ( sensortype = = OTHER_SENSOR )
{
# ifndef USING_N938
# else
GpioControl : : setInt ( CMD_SET_OTHER_POWER , 0 ) ;
GpioControl : : setInt ( CMD_SET_485_EN2 , 0 ) ;
# endif
}
return 0 ;
}
bool CPhoneDevice : : LoadNetworkInfo ( )
{
std : : vector < uint8_t > content ;
if ( ! readFile ( m_appPath + ( APP_PATH_NETWORK ) , content ) & & ! content . empty ( ) )
{
return false ;
}
Json : : CharReaderBuilder builder ;
std : : unique_ptr < Json : : CharReader > 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 ) ;
}
}