You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TermApp/app/src/main/cpp/netcamera/httpclient.cpp

338 lines
10 KiB
C++

#include "httpclient.h"
#include "netcamera.h"
#include <LogThread.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;
if( NULL == data || NULL == buffer )
{
XYLOG(XYLOG_SEVERITY_ERROR,"OnWriteData callback -1");
return -1;
}
uint8_t* begin = (uint8_t *)buffer;
uint8_t* end = begin + size * nmemb;
data->insert(data->end(), begin, end);
return nmemb;
}
static int SockOptCallback(void *clientp, curl_socket_t curlfd, curlsocktype purpose)
{
net_handle_t netHandle = *((net_handle_t *)clientp);
int res = android_setsocknetwork(netHandle, curlfd);
if (res == -1)
{
int errcode = errno;
printf("android_setsocknetwork errno=%d", errcode);
XYLOG(XYLOG_SEVERITY_ERROR,"setsocknetwork -1, errcode=%d",errcode);
}
return res == 0 ? CURL_SOCKOPT_OK : CURL_SOCKOPT_ERROR;
}
int DoGetRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, std::vector<uint8_t>& data)
{
CURLcode nRet;
std::string auth;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_easy_setopt(curl, CURLOPT_URL, url);
if (authType != HTTP_AUTH_TYPE_NONE)
{
if (userName != NULL && password != NULL && strlen(userName) > 0)
{
auth = userName;
auth += ":";
auth += password;
curl_easy_setopt(curl, CURLOPT_USERPWD, auth.c_str());
// DIGEST Auth
if (authType == HTTP_AUTH_TYPE_BASIC)
{
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
else if (authType == HTTP_AUTH_TYPE_DIGEST)
{
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_FAILONERROR, 1);
curl_easy_setopt(curl, CURLOPT_USERAGENT , "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36");
//
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
// 设置回调函数的参数,获取反馈信息
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
// 接收数据时超时设置如果5秒内数据未接收完直接退出
#ifndef NDEBUG
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
#else
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
#endif
// 设置重定向次数,防止重定向次数太多
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 4);
// 连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
nRet = curl_easy_perform(curl);
long responseCode = 0;
if (CURLE_OK == nRet)
{
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
if (responseCode != 200)
{
// #ifdef _DEBUG
char * log = new char[data.size() + 16];
memset(&log[0], 0, data.size());
snprintf(log, data.size() + 16, "%d", (int)responseCode);
if (!data.empty())
{
strcat(log, " ");
memcpy(&log[strlen(log)], &data[0], data.size());
}
// printf("%s", log);
XYLOG(XYLOG_SEVERITY_ERROR, log);
delete[] log;
// #endif
}
}
else
{
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
XYLOG(XYLOG_SEVERITY_WARNING, "Net Photo failure, nRet=%d, code=%d", (int)nRet, (int)responseCode);
5 months ago
// printf("GET err=%d", nRet);
}
curl_easy_cleanup(curl);
return ((0 == nRet) && (responseCode == 200)) ? 0 : 1;
}
int DoPutRequest(const char* url, int authType, const char* userName, const char* password, net_handle_t netHandle, const char* contents, std::vector<uint8_t>& data)
{
std::string auth;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(curl, CURLOPT_URL, url);
if (authType != HTTP_AUTH_TYPE_NONE)
{
if (userName != NULL && password != NULL && strlen(userName) > 0)
{
auth = userName;
auth += ":";
auth += password;
curl_easy_setopt(curl, CURLOPT_USERPWD, auth.c_str());
// DIGEST Auth
if (authType == HTTP_AUTH_TYPE_BASIC)
{
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
else if (authType == HTTP_AUTH_TYPE_DIGEST)
{
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);
}
if(contents != NULL)
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, contents);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
// 设置回调函数的参数,获取反馈信息
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
// 接收数据时超时设置如果5秒内数据未接收完直接退出
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
// 设置重定向次数,防止重定向次数太多
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 4);
// 连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
CURLcode nRet = curl_easy_perform(curl);
if (CURLE_OK != nRet)
{
printf("GET err=%d", nRet);
}
curl_easy_cleanup(curl);
return (0 == nRet) ? 0 : 1;
}
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(), photoInfo.authType, 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);
fdatasync(fileno(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(), photoInfo.authType, userName, password, photoInfo.netHandle, img);
return (0 == nRet);
}
int UniviewResolutionSet(const NET_PHOTO_INFO& photoInfo, int channel, unsigned int cmd)
{
std::string path = "/LAPI/V1.0/Channels/" + std::to_string(channel) + "/Media/Capture";
Json::Value outdata; // 创建 Json::Value 对象来存储 JSON 数据
uniview_resolution_jsoncpp_file_info(outdata, cmd);
Json::StreamWriterBuilder writer;
std::string sendbuf = Json::writeString(writer, outdata);
std::vector<uint8_t> respContent;
DoPutRequest(path.c_str(), photoInfo.authType, photoInfo.userName, photoInfo.password, photoInfo.netHandle, sendbuf.c_str(), respContent);
// respContent.push_back(0);
// XYLOG(XYLOG_SEVERITY_DEBUG, "Sendlen= %zu, respContent=%s", sendbuf.size(), (const char*)&respContent[0]);
return 0;
}
int uniview_resolution_jsoncpp_file_info(Json::Value &outdata, unsigned int cmd)
{
PIC_RESOLUTION pic_resol[] = { {352,288},{640,360},{720,576},{1280,720},{1920,1080},{2688,1520},{3072,2048},{3840,2160},{2560,1440},{704,288} };
outdata["Enable"] = 1;
Json::Value Resolution;
outdata["Resolution"] = Resolution;
if ((cmd < 1) || (cmd > 10))
{
cmd = 5;
}
Resolution["Width"] = pic_resol[cmd-1].width;
Resolution["Height"] = pic_resol[cmd-1].height;
outdata["Picturesize"] = 900;
return 0;
}
namespace nc_hk
{
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(), photoInfo.authType, userName, password, photoInfo.netHandle, img);
#ifdef _DEBUG
if (0 == nRet)
{
FILE *fp = fopen("/sdcard/com.xypower.mpapp/tmp/netimg.jpg", "wb");
if (fp != NULL)
{
fwrite(&img[0], img.size(), 1, fp);
fclose(fp);
}
}
#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;
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(), photoInfo.authType, userName, password, photoInfo.netHandle, img);
#ifdef _DEBUG
if (0 == nRet)
{
FILE *fp = fopen("/sdcard/com.xypower.mpapp/tmp/netimg.jpg", "wb");
if (fp != NULL)
{
fwrite(&img[0], img.size(), 1, fp);
fclose(fp);
}
}
#endif
return (0 == nRet);
}
}