int32_t uart_putc(uint8_t uartNum, uint8_t ch) { if(uartNum == 0) { if(IS_BUFFER_FULL(u0tx)) { BUFFER_CLEAR(u0tx); return RET_NOK; } else { BUFFER_IN(u0tx) = ch; BUFFER_IN_MOVE(u0tx, 1); UART_ITConfig(UART0,(UART_IT_FLAG_TXI),ENABLE); } } else if(uartNum == 1) { if(IS_BUFFER_FULL(u1tx)) { BUFFER_CLEAR(u1tx); return RET_NOK; } else { UartPutc(UART1,ch); //BUFFER_IN(u1tx) = ch; //BUFFER_IN_MOVE(u1tx, 1); //UART_ITConfig(UART1,(UART_IT_FLAG_TXI),ENABLE); } } return RET_OK; }
/* This is the Thread which will read data from the Circular buffer * and print out to the Console */ void* consumer_thread(void *arg) { circ_buffer_t *rcv_buf; double mean, smoothed_mean; int sockfd; packet_info_t *pkt_info; double get_rand; bool data_present, was_buf_full, done = FALSE; unsigned int ack_seq; unsigned short new_win; double prob_loss; assert(arg); assert(rcv_buf = ((thread_arg *)arg)->rcv_buf); assert(((thread_arg *)arg)->config); mean = smoothed_mean = ((thread_arg *)arg)->config->mean; prob_loss = ((thread_arg *)arg)->config->prob_loss; sockfd = ((thread_arg *)arg)->sockfd; free(arg); Pthread_detach(pthread_self()); while (!done) { get_rand = (double)rand()/RAND_MAX; smoothed_mean = -1.0 * mean * log(get_rand); printf("[Consumer Thread] Wake Up in %lf ms\n", smoothed_mean); usleep((useconds_t)(smoothed_mean * 1000)); data_present = FALSE; was_buf_full = FALSE; Pthread_mutex_lock(&buf_mutex); if (IS_BUFFER_FULL(rcv_buf)) was_buf_full = TRUE; while(read_from_buffer(rcv_buf, &pkt_info) >= 0) { data_present = TRUE; fprintf(stdout, "[Consumer thread] [seq:%u]\n%.*s\n", pkt_info->seq, pkt_info->data_len, pkt_info->data); if(IS_EOF(pkt_info)) { done = TRUE; break; } free_pkt_info(pkt_info); pkt_info = NULL; } /* Save off these values as we are releasing the lock below */ new_win = window_size(rcv_buf); ack_seq = NEXT_ACK(rcv_buf); Pthread_mutex_unlock(&buf_mutex); if (!data_present) printf("[Consumer Thread] No data this time around\n"); if(was_buf_full) { /* Advertise New Opened Up window */ send_ack(sockfd, new_win, 0, ack_seq, 0, prob_loss); } } exit(0); }