|
|
|
|
@ -12,10 +12,12 @@ |
|
|
|
|
#include <arpa/inet.h> |
|
|
|
|
#include <sys/types.h> |
|
|
|
|
#include <sys/socket.h> |
|
|
|
|
#include <netinet/tcp.h> |
|
|
|
|
#include <unistd.h> |
|
|
|
|
#include <netinet/in.h> |
|
|
|
|
#include <string.h> |
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <errno.h> |
|
|
|
|
|
|
|
|
|
static volatile uint8_t request_full; // Request Buffer Usage Flag
|
|
|
|
|
static volatile uint32_t request_head; // ReqBuff Buffer In Index
|
|
|
|
|
@ -42,13 +44,11 @@ typedef struct { |
|
|
|
|
typedef struct sockaddr_in sockaddr_in_t; |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
int sockfd, newsockfd, portno, clilen; |
|
|
|
|
int sockfd, csockfd, portno, clilen; |
|
|
|
|
sockaddr_in_t serv_addr; |
|
|
|
|
sockaddr_in_t cli_addr; |
|
|
|
|
|
|
|
|
|
// USB HID Callback: when system initializes
|
|
|
|
|
void wait_for_client(void) { |
|
|
|
|
|
|
|
|
|
void init_socket(void) { |
|
|
|
|
request_full = 0; |
|
|
|
|
request_head = 0; |
|
|
|
|
request_tail = 0; |
|
|
|
|
@ -56,10 +56,9 @@ void wait_for_client(void) { |
|
|
|
|
response_head = 0; |
|
|
|
|
response_tail = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sockfd = socket(AF_INET, SOCK_STREAM, 0); |
|
|
|
|
if (sockfd < 0) { |
|
|
|
|
DEBUG("ERROR opening socket"); |
|
|
|
|
DEBUG("ERROR opening socket: %s\n", strerror(errno)); |
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
bzero((char *) &serv_addr, sizeof(serv_addr)); |
|
|
|
|
@ -69,39 +68,60 @@ void wait_for_client(void) { |
|
|
|
|
serv_addr.sin_port = htons(portno); |
|
|
|
|
int yes = 1; |
|
|
|
|
if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0){ |
|
|
|
|
DEBUG("ERROR on binding"); |
|
|
|
|
DEBUG("ERROR on setting socket option: %s\n", strerror(errno)); |
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){ |
|
|
|
|
DEBUG("ERROR on binding"); |
|
|
|
|
DEBUG("ERROR on binding: %s\n", strerror(errno)); |
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
listen(sockfd,5); |
|
|
|
|
csockfd = -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void close_socket() { |
|
|
|
|
if( csockfd == -1){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
printf("Closed session!\n"); |
|
|
|
|
close(csockfd); |
|
|
|
|
csockfd = -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int should_close=0; |
|
|
|
|
void late_close_socket() { |
|
|
|
|
should_close = 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// USB HID Callback: when system initializes
|
|
|
|
|
void wait_for_client(void) { |
|
|
|
|
printf("Wait client... "); |
|
|
|
|
clilen = sizeof(cli_addr); |
|
|
|
|
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t*)&clilen); |
|
|
|
|
if (newsockfd < 0){ |
|
|
|
|
DEBUG("ERROR on accept"); |
|
|
|
|
csockfd = accept(sockfd, (struct sockaddr*)&cli_addr, (socklen_t*)&clilen); |
|
|
|
|
if (csockfd < 0){ |
|
|
|
|
DEBUG("ERROR on accept: %s\n", strerror(errno)); |
|
|
|
|
exit(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int flags; |
|
|
|
|
flags = fcntl(newsockfd, F_GETFL, 0); |
|
|
|
|
flags = fcntl(csockfd, F_GETFL, 0); |
|
|
|
|
if(flags < 0 ){ |
|
|
|
|
flags = 0; |
|
|
|
|
} |
|
|
|
|
if( fcntl(newsockfd, F_SETFL, flags | O_NONBLOCK) < 0 ){ |
|
|
|
|
DEBUG("ERROR seting non-blocking!\n"); |
|
|
|
|
if( fcntl(csockfd, F_SETFL, flags | O_NONBLOCK) < 0 ){ |
|
|
|
|
DEBUG("ERROR seting non-blocking: %s\n", strerror(errno)); |
|
|
|
|
} |
|
|
|
|
printf("connected\n"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// USB HID Callback: when data needs to be prepared for the host
|
|
|
|
|
int write_response() { |
|
|
|
|
if ((response_tail != response_head) || response_full) { |
|
|
|
|
int n = write(newsockfd,response_buff[response_tail],DAP_PACKET_SIZE); |
|
|
|
|
int n = write(csockfd,response_buff[response_tail],DAP_PACKET_SIZE); |
|
|
|
|
if (n != DAP_PACKET_SIZE){ |
|
|
|
|
DEBUG("ERROR writing to socket"); |
|
|
|
|
DEBUG("ERROR writing to socket: %s\n", strerror(errno)); |
|
|
|
|
close_socket(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// remove a response
|
|
|
|
|
@ -118,7 +138,7 @@ void buffer_request(uint8_t *buff, int len) { |
|
|
|
|
if (len == 0){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (buff[0] == ID_DAP_TransferAbort) { |
|
|
|
|
if (buff[0] == ID_DAP_TransferAbort) { |
|
|
|
|
DAP_TransferAbort = 1; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
@ -135,20 +155,36 @@ void buffer_request(uint8_t *buff, int len) { |
|
|
|
|
request_full = 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Process USB HID Data
|
|
|
|
|
void process_data(void) { |
|
|
|
|
static uint8_t buff[DAP_PACKET_SIZE]; |
|
|
|
|
static int buff_pos = 0; |
|
|
|
|
|
|
|
|
|
int n; |
|
|
|
|
n = read(newsockfd,buff+buff_pos,DAP_PACKET_SIZE); |
|
|
|
|
if( csockfd == -1 ){ |
|
|
|
|
wait_for_client(); |
|
|
|
|
} |
|
|
|
|
n = read(csockfd,buff+buff_pos,DAP_PACKET_SIZE); |
|
|
|
|
|
|
|
|
|
if( n>0 ) { |
|
|
|
|
if ( n==0 ) { |
|
|
|
|
|
|
|
|
|
}else if( n>0 ) { |
|
|
|
|
buff_pos += n; |
|
|
|
|
if( buff_pos==DAP_PACKET_SIZE ) { |
|
|
|
|
buffer_request(buff,DAP_PACKET_SIZE); |
|
|
|
|
buff_pos=0; |
|
|
|
|
} |
|
|
|
|
}else if( n<0 ){ |
|
|
|
|
switch( errno ) { |
|
|
|
|
case EAGAIN: break; |
|
|
|
|
default: |
|
|
|
|
DEBUG("A problem occured: %d %d %s\n", n, errno,strerror(errno) ); |
|
|
|
|
DEBUG("Closing connection!\n"); |
|
|
|
|
close_socket(csockfd); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Process pending requests
|
|
|
|
|
@ -164,12 +200,18 @@ void process_data(void) { |
|
|
|
|
request_tail = (request_tail + 1) % DAP_PACKET_COUNT; |
|
|
|
|
request_full = 0; |
|
|
|
|
|
|
|
|
|
// add a response
|
|
|
|
|
response_head = (response_head + 1) % DAP_PACKET_COUNT; |
|
|
|
|
if (response_head == response_tail) { |
|
|
|
|
response_full = 1; |
|
|
|
|
if( csockfd != -1 ) { |
|
|
|
|
// add a response
|
|
|
|
|
response_head = (response_head + 1) % DAP_PACKET_COUNT; |
|
|
|
|
if (response_head == response_tail) { |
|
|
|
|
response_full = 1; |
|
|
|
|
} |
|
|
|
|
write_response(); |
|
|
|
|
} |
|
|
|
|
write_response(); |
|
|
|
|
} |
|
|
|
|
if(should_close) { |
|
|
|
|
close_socket(); |
|
|
|
|
} |
|
|
|
|
should_close = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|