Server now listens for another client to connect after connection is closed

master
Stefan Krulj 12 years ago
parent 9578abf6d9
commit f5ac7af946
  1. 10
      src/DAP_config.h
  2. 10
      src/main.c
  3. 78
      src/socket_ipc.c

@ -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. /** Disable JTAG/SWD I/O Pins.
Disables the DAP Hardware I/O pins which configures: Disables the DAP Hardware I/O pins which configures:
- TCK/SWCLK, TMS/SWDIO, TDI, TDO, nTRST, nRESET to High-Z mode. - TCK/SWCLK, TMS/SWDIO, TDI, TDO, nTRST, nRESET to High-Z mode.
*/ */
static __inline void PORT_OFF (void) { static __inline void PORT_OFF (void) {
//TODO HighZ? //TODO HighZ?
ftdi_disable_bitbang(&__ftdic); /*ftdi_disable_bitbang(&__ftdic);
ftdi_deinit(&__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; return;
} }
if( ptr!=NULL ){ if( ptr!=NULL ){
DEBUG("setting read-to-pointer for pin reads!\n");
read_slot = ptr; read_slot = ptr;
if( bit_count8(bit)==1 ){ if( bit_count8(bit)==1 ){
pin_read_mask=bit; pin_read_mask=bit;
@ -570,7 +570,6 @@ called prior \ref PIN_SWDIO_OUT function calls.
static __forceinline void PIN_SWDIO_OUT_ENABLE (void) { static __forceinline void PIN_SWDIO_OUT_ENABLE (void) {
/* SWD is changing DIO line to output, submit current IO state /* SWD is changing DIO line to output, submit current IO state
and send it to FTDI*/ and send it to FTDI*/
DEBUG("SWDIO -> out\n");
dir_state |= TMS_SWDIO; dir_state |= TMS_SWDIO;
int success = ftdi_set_bitmode(&__ftdic, dir_state, __ftdic.bitbang_mode ); int success = ftdi_set_bitmode(&__ftdic, dir_state, __ftdic.bitbang_mode );
if( success<0 ){ if( success<0 ){
@ -585,7 +584,6 @@ called prior \ref PIN_SWDIO_IN function calls.
static __forceinline void PIN_SWDIO_OUT_DISABLE (void) { static __forceinline void PIN_SWDIO_OUT_DISABLE (void) {
/* SWD is changing DIO line to input, submit current IO state /* SWD is changing DIO line to input, submit current IO state
and send it to FTDI */ and send it to FTDI */
DEBUG("SWDIO -> in\n");
dir_state &= ~TMS_SWDIO; dir_state &= ~TMS_SWDIO;
int success = ftdi_set_bitmode(&__ftdic, dir_state, __ftdic.bitbang_mode ); int success = ftdi_set_bitmode(&__ftdic, dir_state, __ftdic.bitbang_mode );

@ -26,9 +26,9 @@
#include <libftdi1/ftdi.h> #include <libftdi1/ftdi.h>
extern void wait_for_client(void);
extern void process_data(void); extern void process_data(void);
extern void init_socket(void);
// Main program // Main program
int main (void) { int main (void) {
@ -40,16 +40,12 @@ int main (void) {
Delayms(500); // Wait for 500ms Delayms(500); // Wait for 500ms
LED_RUNNING_OUT(0); // Turn off Target Running LED LED_RUNNING_OUT(0); // Turn off Target Running LED
LED_CONNECTED_OUT(0); // Turn off Debugger Connected LED LED_CONNECTED_OUT(0); // Turn off Debugger Connected LED
printf("Wait client... ");
fflush(stdout); fflush(stdout);
wait_for_client(); init_socket();
printf("connected\n");
while (1) { // Endless Loop while (1) { // Endless Loop
//usbd_hid_process(); // Process USB HID Data //usbd_hid_process(); // Process USB HID Data
// TODO thread sleep // TODO thread sleep
//printf("process_data\n");
process_data(); process_data();
//Delayms(1000);
} }
} }

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

Loading…
Cancel
Save