diff --git a/src/DAP_config.h b/src/DAP_config.h index 268f576..d6152ef 100644 --- a/src/DAP_config.h +++ b/src/DAP_config.h @@ -427,15 +427,16 @@ static __forceinline void PIN_SET ( uint8_t pin, uint32_t bit ) { } */ - +extern void late_close_socket(); /** Disable JTAG/SWD I/O Pins. Disables the DAP Hardware I/O pins which configures: - TCK/SWCLK, TMS/SWDIO, TDI, TDO, nTRST, nRESET to High-Z mode. */ static __inline void PORT_OFF (void) { //TODO HighZ? - ftdi_disable_bitbang(&__ftdic); - ftdi_deinit(&__ftdic); + /*ftdi_disable_bitbang(&__ftdic); + ftdi_deinit(&__ftdic); TODO */ + late_close_socket(); } @@ -529,7 +530,6 @@ static __forceinline void PIN_SWDIO_IN (uint8_t *ptr,uint8_t bit){ return; } if( ptr!=NULL ){ - DEBUG("setting read-to-pointer for pin reads!\n"); read_slot = ptr; if( bit_count8(bit)==1 ){ pin_read_mask=bit; @@ -570,7 +570,6 @@ called prior \ref PIN_SWDIO_OUT function calls. static __forceinline void PIN_SWDIO_OUT_ENABLE (void) { /* SWD is changing DIO line to output, submit current IO state and send it to FTDI*/ - DEBUG("SWDIO -> out\n"); dir_state |= TMS_SWDIO; int success = ftdi_set_bitmode(&__ftdic, dir_state, __ftdic.bitbang_mode ); if( success<0 ){ @@ -585,7 +584,6 @@ called prior \ref PIN_SWDIO_IN function calls. static __forceinline void PIN_SWDIO_OUT_DISABLE (void) { /* SWD is changing DIO line to input, submit current IO state and send it to FTDI */ - DEBUG("SWDIO -> in\n"); dir_state &= ~TMS_SWDIO; int success = ftdi_set_bitmode(&__ftdic, dir_state, __ftdic.bitbang_mode ); diff --git a/src/main.c b/src/main.c index d53c4aa..681ec03 100644 --- a/src/main.c +++ b/src/main.c @@ -26,30 +26,26 @@ #include - -extern void wait_for_client(void); extern void process_data(void); +extern void init_socket(void); + // Main program int main (void) { - - DAP_Setup(); // DAP Setup - - //DAP_PACKET_COUNT - LED_CONNECTED_OUT(1); // Turn on Debugger Connected LED - LED_RUNNING_OUT(1); // Turn on Target Running LED - Delayms(500); // Wait for 500ms - LED_RUNNING_OUT(0); // Turn off Target Running LED - LED_CONNECTED_OUT(0); // Turn off Debugger Connected LED - printf("Wait client... "); - fflush(stdout); - wait_for_client(); - printf("connected\n"); - - while (1) { // Endless Loop - //usbd_hid_process(); // Process USB HID Data - // TODO thread sleep - //printf("process_data\n"); - process_data(); - //Delayms(1000); - } + + DAP_Setup(); // DAP Setup + + //DAP_PACKET_COUNT + LED_CONNECTED_OUT(1); // Turn on Debugger Connected LED + LED_RUNNING_OUT(1); // Turn on Target Running LED + Delayms(500); // Wait for 500ms + LED_RUNNING_OUT(0); // Turn off Target Running LED + LED_CONNECTED_OUT(0); // Turn off Debugger Connected LED + fflush(stdout); + init_socket(); + + while (1) { // Endless Loop + //usbd_hid_process(); // Process USB HID Data + // TODO thread sleep + process_data(); + } } diff --git a/src/socket_ipc.c b/src/socket_ipc.c index 0a4d44a..f97c4e9 100644 --- a/src/socket_ipc.c +++ b/src/socket_ipc.c @@ -12,10 +12,12 @@ #include #include #include +#include #include #include #include #include +#include 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; }