310 lines
7.0 KiB
C++
310 lines
7.0 KiB
C++
#include<iostream>
|
|
#include<stdlib.h>
|
|
#include<stdio.h>
|
|
#include <unistd.h>
|
|
#include<string>
|
|
#include<functional>
|
|
#include "common.h"
|
|
#include "HuaWei/HWsign.h"
|
|
#include "HuaWei/HWsec.h"
|
|
#include "data264.h"
|
|
// #include "data265.h"
|
|
// #include "H265TCP.h"
|
|
#include "putdata.h"
|
|
#include<netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include<sys/socket.h>
|
|
#include <thread>
|
|
extern void pushdata(RTPpackage::Ptr ptr);
|
|
using namespace std;
|
|
class UdpSocket{
|
|
private:
|
|
int _sockfd;
|
|
public:
|
|
UdpSocket():_sockfd(-1){}
|
|
~UdpSocket()
|
|
{
|
|
if(_sockfd != -1)
|
|
{
|
|
close(_sockfd);
|
|
}
|
|
}
|
|
bool Socket(){
|
|
_sockfd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
|
|
if(_sockfd < 0)
|
|
{
|
|
perror("socket error !");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool Bind(const string &ip,uint16_t port){
|
|
struct sockaddr_in addr;
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons(port);
|
|
addr.sin_addr.s_addr = inet_addr(ip.c_str());
|
|
socklen_t len = sizeof(struct sockaddr_in);
|
|
int ret;
|
|
ret = bind(_sockfd,(struct sockaddr*)&addr,len);
|
|
if(ret < 0)
|
|
{
|
|
perror("bind error !");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Send(char* data,const uint32_t &dlen, const string &ip,uint16_t port){
|
|
struct sockaddr_in addr;
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons(port);
|
|
addr.sin_addr.s_addr = inet_addr(ip.c_str());
|
|
socklen_t len = sizeof(struct sockaddr_in);
|
|
int ret;
|
|
ret = sendto(_sockfd,(void*)data,dlen,0,(struct sockaddr*) &addr,len);
|
|
if(ret < 0)
|
|
{
|
|
perror("sendto error");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool Recv(string *buf, string *ip = NULL,uint16_t *port = NULL)
|
|
{
|
|
struct sockaddr_in addr;
|
|
socklen_t len = sizeof(struct sockaddr_in);
|
|
char temp [4096] = {0};
|
|
int ret;
|
|
ret = recvfrom(_sockfd,temp,4096,0,(struct sockaddr *)&addr,&len);
|
|
if(ret < 0){
|
|
perror ("recvfrom error !");
|
|
return false;
|
|
}
|
|
if(ip != NULL)
|
|
{
|
|
*ip = inet_ntoa(addr.sin_addr);
|
|
}
|
|
if(port != NULL){
|
|
*port = ntohs(addr.sin_port);
|
|
}
|
|
*buf = temp;
|
|
return true;
|
|
}
|
|
|
|
bool Close()
|
|
{
|
|
if(_sockfd != -1)
|
|
{
|
|
close(_sockfd);
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
|
|
void print_data(const char * buf, uint32_t len){
|
|
printf("\n --%d-- \n",len);
|
|
for (int num = 0; num < 12; num++)
|
|
{
|
|
printf("%02X ", (uint8_t)buf[num]);
|
|
}
|
|
printf(", ");
|
|
for (int num = 12; num < 20; num++)
|
|
{
|
|
printf("%02X ", (uint8_t)buf[num]);
|
|
}
|
|
|
|
printf("\n ---- \n");
|
|
}
|
|
|
|
void print_rtp(const char * buf, uint32_t len){
|
|
uint8_t mask=0;
|
|
uint16_t seq=0;
|
|
uint32_t stamp=0;
|
|
mask=(uint8_t)*(buf+1)>>7;
|
|
memcpy(&seq, buf + 2, 2);
|
|
seq= ntohs(seq);
|
|
memcpy(&stamp, buf + 4, 4);
|
|
stamp= ntohl(stamp);
|
|
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ modify @@@@@@@@@ seq %d stamp %d mask %d",seq,stamp,mask);
|
|
print_data(buf,len);
|
|
}
|
|
|
|
// mask=*(pkt##x+offset+1)>>7; \
|
|
// memcpy(&seq, pkt##x+offset + 2, 2); \
|
|
// seq= ntohs(seq); \
|
|
// memcpy(&stamp, pkt##x+offset + 4, 4); \
|
|
// stamp= ntohl(stamp); \
|
|
// printf("raw seq %d stamp %d mask %d",seq,stamp,mask); \
|
|
// print_data((const char*)pkt##x+offset,sizeof(pkt##x)-offset); \
|
|
|
|
#define ARRAY(x,offset) { \
|
|
id=(int *)malloc(sizeof(int)); \
|
|
*id=x; \
|
|
auto ret =HWSign_rtp_input(sign_handle,(const char*)pkt##x+offset,sizeof(pkt##x)-offset,0,(void*)id); \
|
|
}
|
|
|
|
void tcp_test(){
|
|
void * hw_sec_ptr= HWSign_tcp_init();
|
|
|
|
char buf_out[2048];
|
|
int ret=1;
|
|
uint32_t * param;
|
|
uint32_t len=0;
|
|
|
|
#if 1
|
|
for (uint32_t rd=1;rd>0;rd--){
|
|
pushfile(hw_sec_ptr,0);
|
|
}
|
|
printout(hw_sec_ptr);
|
|
#else
|
|
for (uint32_t rd=5;rd>0;rd--){
|
|
pushdata(hw_sec_ptr);
|
|
printout(hw_sec_ptr);
|
|
}
|
|
#endif
|
|
|
|
|
|
|
|
|
|
HWSign_tcp_release(hw_sec_ptr);
|
|
}
|
|
|
|
|
|
void UDPtest(){
|
|
// EXPAND(DEFER(A)());
|
|
void * sign_handle=nullptr;
|
|
|
|
auto udpsocket=new UdpSocket();
|
|
udpsocket->Socket();
|
|
// udpsocket.Send()
|
|
// [udpsocket](const char * rtp_ptr, const uint32_t rtp_len){
|
|
// udpsocket->Send(rtp_ptr,rtp_len,string("192.168.123.1"),30000);
|
|
// // print_rtp(rtp_ptr,rtp_len);
|
|
// }
|
|
uint8_t mask;
|
|
uint16_t seq=0;
|
|
uint32_t stamp=0;
|
|
int* id;
|
|
int ret,count;
|
|
void * Verify_handle=nullptr;
|
|
// SDF_Device_open();
|
|
|
|
Verify_handle=HWVerify_init();
|
|
|
|
sign_handle=HWSign_init();
|
|
|
|
// char buf_test[2048];
|
|
// uint32_t len_test;
|
|
// int* id_test;
|
|
// HWSign_rtp_out(sign_handle,buf_test,&len_test,(void **)&id_test);
|
|
|
|
// ARRAY(2,42)
|
|
// ARRAY(3,42)
|
|
// ARRAY(4,42)
|
|
// ARRAY(5,42) //I
|
|
// ARRAY(6,42)
|
|
// ARRAY(7,42)
|
|
// ARRAY(8,42)
|
|
// ARRAY(9,42)
|
|
// ARRAY(10,42)
|
|
// ARRAY(11,42)
|
|
// ARRAY(12,42)
|
|
// ARRAY(13,42)
|
|
// ARRAY(14,42)
|
|
// ARRAY(15,42)
|
|
// ARRAY(16,42)
|
|
// ARRAY(17,42)
|
|
// ARRAY(18,42)
|
|
// ARRAY(19,42)
|
|
|
|
|
|
// for (int loop=0;loop<=1;loop++){
|
|
|
|
|
|
ARRAY(1,42)
|
|
ARRAY(2,42)
|
|
ARRAY(3,42)
|
|
ARRAY(4,42)
|
|
|
|
|
|
|
|
ARRAY(5,42) //I
|
|
ARRAY(6,42)
|
|
ARRAY(7,42)
|
|
ARRAY(8,42)
|
|
ARRAY(9,42)
|
|
ARRAY(10,42)
|
|
ARRAY(11,42)
|
|
ARRAY(12,42)
|
|
ARRAY(13,42)
|
|
ARRAY(14,42)
|
|
ARRAY(15,42)
|
|
ARRAY(16,42)
|
|
ARRAY(17,42)
|
|
ARRAY(18,42)
|
|
ARRAY(19,42)
|
|
ARRAY(20,42)
|
|
ARRAY(21,42)
|
|
|
|
///Verify init
|
|
int vef_ret=-2;
|
|
|
|
|
|
//////////////////////
|
|
ret=1;count=0;
|
|
while (ret==1)
|
|
{
|
|
char buf_h[2048];
|
|
uint32_t len;
|
|
int* id;
|
|
count++;
|
|
ret=HWSign_rtp_out(sign_handle,buf_h,&len,(void **)&id);
|
|
// if(len>1400){
|
|
// buf_h[1000]=0x01;
|
|
// }
|
|
vef_ret=HWVerify_rtp_input(Verify_handle,buf_h,len,0,nullptr);
|
|
printf("verify status: %d \n",vef_ret);
|
|
if (ret==-1) break;
|
|
|
|
udpsocket->Send((char*)buf_h,len,string("192.168.123.1"),30000);
|
|
|
|
// printf("%d %d %d\n",count,*id,len);
|
|
free(id);
|
|
// printf(" modify @@@@@@@@@ id %d seq %d stamp %d mask %d",id,seq,stamp,mask);
|
|
// print_data(buf_h,len);
|
|
// sleep(0.1);
|
|
/* code */
|
|
}
|
|
// }
|
|
|
|
// printf("raw");
|
|
// print_data((const char*)pkt2+42,sizeof(pkt2)-42);
|
|
// HWSign_rtp_input(sign_handle,(const char*)pkt2+42,sizeof(pkt2)-42,0);
|
|
|
|
// printf("raw");
|
|
// print_data((const char*)pkt3+42,sizeof(pkt3)-42);
|
|
// HWSign_rtp_input(sign_handle,(const char*)pkt3+42,sizeof(pkt3)-42,0);
|
|
|
|
|
|
HWSign_release(sign_handle);
|
|
HWVerify_release(Verify_handle);
|
|
// SDF_Device_close();
|
|
}
|
|
|
|
|
|
|
|
int main(){
|
|
// std:thread threads[2];
|
|
|
|
// for (size_t i = 0; i < 2; i++)
|
|
// {
|
|
// threads[i]=std::thread(tcp_test);
|
|
// }
|
|
|
|
// for (auto & th:threads)
|
|
// th.join();
|
|
//UDPtest();
|
|
tcp_test();
|
|
|
|
} |