|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
#include "httpclient.h"
|
|
|
|
|
#include "netcamera.h"
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
|
|
|
|
|
{
|
|
|
|
|
std::vector<uint8_t>* data = (std::vector<uint8_t>*)lpVoid;
|
|
|
|
@ -14,8 +16,20 @@ static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
|
|
|
|
|
return nmemb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int SockOptCallback(void *clientp, curl_socket_t curlfd, curlsocktype purpose)
|
|
|
|
|
{
|
|
|
|
|
net_handle_t netHandle = *((net_handle_t *)clientp);
|
|
|
|
|
|
|
|
|
|
int DoGetRequest(const char* url, const char* userName, const char* password, const char* interface, std::vector<uint8_t>& data)
|
|
|
|
|
int res = android_setsocknetwork(netHandle, curlfd);
|
|
|
|
|
if (res == -1)
|
|
|
|
|
{
|
|
|
|
|
int errcode = errno;
|
|
|
|
|
printf("android_setsocknetwork errno=%d", errcode);
|
|
|
|
|
}
|
|
|
|
|
return res == 0 ? CURL_SOCKOPT_OK : CURL_SOCKOPT_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DoGetRequest(const char* url, const char* userName, const char* password, net_handle_t netHandle, std::vector<uint8_t>& data)
|
|
|
|
|
{
|
|
|
|
|
CURLcode nRet;
|
|
|
|
|
std::string auth;
|
|
|
|
@ -33,7 +47,13 @@ int DoGetRequest(const char* url, const char* userName, const char* password, co
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置回调函数
|
|
|
|
|
if (netHandle != NETWORK_UNSPECIFIED)
|
|
|
|
|
{
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, SockOptCallback);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SOCKOPTDATA, &netHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
|
|
// 设置回调函数的参数,获取反馈信息
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
|
|
|
|
@ -48,10 +68,6 @@ int DoGetRequest(const char* url, const char* userName, const char* password, co
|
|
|
|
|
// 连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
|
|
|
|
|
|
|
|
|
|
if (interface != NULL && strlen(interface) > 0)
|
|
|
|
|
{
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_INTERFACE , interface);
|
|
|
|
|
}
|
|
|
|
|
nRet = curl_easy_perform(curl);
|
|
|
|
|
if (CURLE_OK != nRet)
|
|
|
|
|
{
|
|
|
|
@ -62,7 +78,7 @@ int DoGetRequest(const char* url, const char* userName, const char* password, co
|
|
|
|
|
return (0 == nRet) ? 0 : 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DoPutRequest(const char* url, const char* userName, const char* password, const char* interface, const char* contents, std::vector<uint8_t>& data)
|
|
|
|
|
int DoPutRequest(const char* url, const char* userName, const char* password, net_handle_t netHandle, const char* contents, std::vector<uint8_t>& data)
|
|
|
|
|
{
|
|
|
|
|
std::string auth;
|
|
|
|
|
|
|
|
|
@ -78,7 +94,13 @@ int DoPutRequest(const char* url, const char* userName, const char* password, co
|
|
|
|
|
// DIGEST Auth
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
|
|
|
|
|
}
|
|
|
|
|
// 设置回调函数
|
|
|
|
|
|
|
|
|
|
if (netHandle != NETWORK_UNSPECIFIED)
|
|
|
|
|
{
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, SockOptCallback);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SOCKOPTDATA, &netHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
|
|
|
|
|
// 设置回调函数的参数,获取反馈信息
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
|
|
|
|
@ -89,10 +111,6 @@ int DoPutRequest(const char* url, const char* userName, const char* password, co
|
|
|
|
|
// 连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
|
|
|
|
|
|
|
|
|
|
if (interface != NULL && strlen(interface) > 0)
|
|
|
|
|
{
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_INTERFACE , interface);
|
|
|
|
|
}
|
|
|
|
|
CURLcode nRet = curl_easy_perform(curl);
|
|
|
|
|
if (CURLE_OK != nRet)
|
|
|
|
|
{
|
|
|
|
@ -103,12 +121,64 @@ int DoPutRequest(const char* url, const char* userName, const char* password, co
|
|
|
|
|
return (0 == nRet) ? 0 : 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace nc_hy
|
|
|
|
|
bool requestCapture(uint8_t channel, uint8_t preset, const NET_PHOTO_INFO& photoInfo)
|
|
|
|
|
{
|
|
|
|
|
bool requestCapture(uint8_t channel, uint8_t preset, const NET_PHOTO_INFO& photoInfo)
|
|
|
|
|
bool res = false;
|
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
|
const char* userName = NULL;
|
|
|
|
|
const char* password = NULL;
|
|
|
|
|
if (photoInfo.authType != 0)
|
|
|
|
|
{
|
|
|
|
|
userName = photoInfo.userName;
|
|
|
|
|
password = photoInfo.password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string url = "http://";
|
|
|
|
|
url += photoInfo.ip;
|
|
|
|
|
url += photoInfo.url;
|
|
|
|
|
|
|
|
|
|
int nRet = DoGetRequest(url.c_str(), userName, password, photoInfo.netHandle, data);
|
|
|
|
|
if (0 == nRet)
|
|
|
|
|
{
|
|
|
|
|
if (!data.empty())
|
|
|
|
|
{
|
|
|
|
|
FILE *fp = fopen(photoInfo.outputPath, "wb");
|
|
|
|
|
if (fp != NULL)
|
|
|
|
|
{
|
|
|
|
|
fwrite(&data[0], data.size(), 1, fp);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
res = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool requestCapture(uint8_t channel, uint8_t preset, const NET_PHOTO_INFO& photoInfo, std::vector<uint8_t>& img)
|
|
|
|
|
{
|
|
|
|
|
bool res = false;
|
|
|
|
|
const char* userName = NULL;
|
|
|
|
|
const char* password = NULL;
|
|
|
|
|
if (photoInfo.authType != 0)
|
|
|
|
|
{
|
|
|
|
|
userName = photoInfo.userName;
|
|
|
|
|
password = photoInfo.password;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string url = "http://";
|
|
|
|
|
url += photoInfo.ip;
|
|
|
|
|
url += photoInfo.url;
|
|
|
|
|
|
|
|
|
|
int nRet = DoGetRequest(url.c_str(), userName, password, photoInfo.netHandle, img);
|
|
|
|
|
return (0 == nRet);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace nc_hk
|
|
|
|
|
{
|
|
|
|
|
bool requestCapture(uint8_t channel, uint8_t preset, const NET_PHOTO_INFO& photoInfo, std::vector<uint8_t>& img)
|
|
|
|
|
{
|
|
|
|
|
bool res = false;
|
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
|
const char* userName = NULL;
|
|
|
|
|
const char* password = NULL;
|
|
|
|
|
if (photoInfo.authType != 0)
|
|
|
|
@ -116,30 +186,30 @@ namespace nc_hy
|
|
|
|
|
userName = photoInfo.userName;
|
|
|
|
|
password = photoInfo.password;
|
|
|
|
|
}
|
|
|
|
|
const char* interface = photoInfo.interface;
|
|
|
|
|
|
|
|
|
|
std::string url = "http://";
|
|
|
|
|
url += photoInfo.ip;
|
|
|
|
|
url += photoInfo.url;
|
|
|
|
|
|
|
|
|
|
int nRet = DoGetRequest(url.c_str(), userName, password, interface, data);
|
|
|
|
|
int nRet = DoGetRequest(url.c_str(), userName, password, photoInfo.netHandle, img);
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
if (0 == nRet)
|
|
|
|
|
{
|
|
|
|
|
if (!data.empty())
|
|
|
|
|
FILE *fp = fopen("/sdcard/com.xypower.mpapp/tmp/netimg.jpg", "wb");
|
|
|
|
|
if (fp != NULL)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp = fopen(photoInfo.outputPath, "wb");
|
|
|
|
|
if (fp != NULL)
|
|
|
|
|
{
|
|
|
|
|
fwrite(&data[0], data.size(), 1, fp);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
res = true;
|
|
|
|
|
}
|
|
|
|
|
fwrite(&img[0], img.size(), 1, fp);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
#endif
|
|
|
|
|
return (0 == nRet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace nc_ys
|
|
|
|
|
{
|
|
|
|
|
bool requestCapture(uint8_t channel, uint8_t preset, const NET_PHOTO_INFO& photoInfo, std::vector<uint8_t>& img)
|
|
|
|
|
{
|
|
|
|
|
bool res = false;
|
|
|
|
@ -155,7 +225,7 @@ namespace nc_hy
|
|
|
|
|
url += photoInfo.ip;
|
|
|
|
|
url += photoInfo.url;
|
|
|
|
|
|
|
|
|
|
int nRet = DoGetRequest(url.c_str(), userName, password, photoInfo.interface, img);
|
|
|
|
|
int nRet = DoGetRequest(url.c_str(), userName, password, photoInfo.netHandle, img);
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
if (0 == nRet)
|
|
|
|
|
{
|
|
|
|
@ -169,4 +239,4 @@ namespace nc_hy
|
|
|
|
|
#endif
|
|
|
|
|
return (0 == nRet);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|