// samples are written to from one thread, and read from another (this one) int fifoRead (int fifoBasePtr, int fifoPtrPtr, int fifoCheckBasePtr, int fifoCheckVal) { int *fifoBase; int *fifoPtr; int readData; int *fifoCheckBase; int readCheckData; fifoBase = (int *)(fifoBasePtr); fifoPtr = (int *)(fifoPtrPtr); fifoCheckBase = (int *)(fifoCheckBasePtr); // Check the sample number readCheckData = fifoCheckBase[*fifoPtr]; #if PRINT == PWM_FLOW_CHECK if (readCheckData != fifoCheckVal) { printstrln("PWM Fifo Checking failure"); printstr("Expected: "); printintln(fifoCheckVal); printstr("Actual: "); printintln(readCheckData); while(1); } #endif // now perform read readData = fifoBase[*fifoPtr]; (*fifoPtr)++; (*fifoPtr) = (*fifoPtr) & PWM_FIFO_MASK; return (readData); }
static int tftp_make_ack_pkt(unsigned char *tx_buf, unsigned short block_num) { tftp_ack_t *pkt = (tftp_ack_t*) &tx_buf[0]; pkt->opcode = hton16((unsigned short) TFTP_OPCODE_ACK); pkt->block_number = hton16(block_num); #if TFTP_DEBUG_PRINT printstr("TFTP: Gen ACK, block #"); printintln(block_num); #endif return TFTP_MIN_PKT_SIZE; }
static int tftp_make_error_pkt(unsigned char *tx_buf, unsigned short code, char *msg, int *error) { tftp_error_t *pkt = (tftp_error_t*) &tx_buf[0]; if (error != 0) { *error = 1; } // The length of the error message must always be less than TFTP_ERROR_MSG_MAX_LENGTH assert(strlen(msg) < TFTP_ERROR_MSG_MAX_LENGTH); pkt->opcode = hton16((unsigned short) TFTP_OPCODE_ERROR); pkt->error_code = hton16(code); strcpy(pkt->error_msg, msg); #if TFTP_DEBUG_PRINT printstr("TFTP: Gen ERROR, code "); printintln(code); #endif return (TFTP_MIN_PKT_SIZE + strlen(msg) + TFTP_NULL_BYTE); }
static void process_xscope_data(chanend c_xtcp, xscope_protocol xscope_data) { /* Custom protocol definition * Start with cmd_type, send end_token as 0 as last * Listen: 0-dc-inport-proto-dc * Connect: 1-dc-out_port-proto-host_ipconfig * Send: 2-1-local_port-dc-remote_addr * Close: 2-2-local_port-dc-remote_addr */ #define CONTROLLER_IP {169, 254, 196, 179} #if !defined(CONTROLLER_IP) #warning No define of form CONTROLLER_IP #error Rebuild the application with this define assigned to the host controller ip #endif xtcp_ipaddr_t ipaddr = CONTROLLER_IP; /*ipaddr[0] = atoi(xscope_data.ip_addr_1); ipaddr[1] = atoi(xscope_data.ip_addr_2); ipaddr[2] = atoi(xscope_data.ip_addr_3); ipaddr[3] = atoi(xscope_data.ip_addr_4);*/ switch (xscope_data.cmd_type) { case XSCOPE_CMD_LISTEN: //listen; for server type conn xtcp_listen(c_xtcp, xscope_data.port_no, xscope_data.protocol); printstr("Listening on port: "); printintln(xscope_data.port_no); break; case XSCOPE_CMD_CONNECT: //connect; for client type conn xtcp_connect(c_xtcp, xscope_data.port_no, ipaddr, xscope_data.protocol); printstr("Connected to host on port: "); printintln(xscope_data.port_no); break; case XSCOPE_CMD_SEND: { //Send data int conn_id; xtcp_connection_t conn; if (PROTO_TCP == xscope_data.protocol) conn_id = get_tcp_conn_id(ipaddr, xscope_data.port_no); else if (PROTO_UDP == xscope_data.protocol) conn_id = get_udp_conn_id(ipaddr, xscope_data.port_no); if (conn_id) { conn.id = conn_id; xtcp_init_send(c_xtcp, &conn); printstr("Sending data on the connection: "); printintln(conn_id); } } break; case XSCOPE_CMD_CLOSE: { //Close command int conn_id; xtcp_connection_t conn; if (PROTO_TCP == xscope_data.protocol) conn_id = get_tcp_conn_id(ipaddr, xscope_data.port_no); else if (PROTO_UDP == xscope_data.protocol) conn_id = get_udp_conn_id(ipaddr, xscope_data.port_no); if (conn_id) { conn.id = conn_id; xtcp_close(c_xtcp, &conn); printstr("Closing the connection: "); printintln(conn_id); } } break; case XSCOPE_CMD_CTRLR_SEND: //this is a controller send command; do nothing break; default: printstrln("unknown command received"); break; } }
int tftp_process_packet(unsigned char *tx_buf, unsigned char *rx_buf, int num_bytes, unsigned short *block_num_glob, int *error, int *complete) { tftp_packet_t *pkt = (tftp_packet_t*) &rx_buf[0]; u16_t opcode = ntoh16(pkt->opcode); switch (opcode) { case TFTP_OPCODE_RRQ: // Read Request { // We don't support read requests - reply with an error packet return tftp_make_error_pkt(tx_buf, TFTP_ERROR_NOT_DEFINED, "Read not supported", error); } case TFTP_OPCODE_WRQ: // Write Request { char *filename; char *mode; filename = (char *) pkt->payload; #if !TFTP_ACCEPT_ANY_FILENAME // Check that the requested filename matches what we expect if (strncmp(filename, TFTP_IMAGE_FILENAME, strlen(TFTP_IMAGE_FILENAME)) != 0) { return tftp_make_error_pkt(tx_buf, TFTP_ERROR_NOT_DEFINED, "Invalid filename", error); } #endif // Generate a pointer to the mode string mode = filename + strlen(filename) + TFTP_NULL_BYTE; // Must be a binary transfer if (strncmp(mode, "octet", 5) != 0) { return tftp_make_error_pkt(tx_buf, TFTP_ERROR_NOT_DEFINED, "Invalid transfer mode", error); } // ACK with data block number zero return tftp_make_ack_pkt(tx_buf, 0); } case TFTP_OPCODE_DATA: // Data Packet { unsigned short block_num; tftp_data_t *data_pkt = (tftp_data_t*) &rx_buf[0]; if (num_bytes < (TFTP_BLOCK_SIZE + TFTP_MIN_PKT_SIZE)) { // last block *complete = 1; } block_num = ntoh16(data_pkt->block_num); *block_num_glob = block_num; // Check that we've received the correct block of data and it's not a duplicate if (block_num == (prev_block_num + 1)) { prev_block_num = block_num; #if TFTP_DEBUG_PRINT printstr("TFTP: Rcvd data, block #"); printintln(block_num); #endif if ((block_num * TFTP_BLOCK_SIZE) >= TFTP_MAX_FILE_SIZE) { // We have received more data that the allowed maximum - send an error return tftp_make_error_pkt(tx_buf, TFTP_ERROR_DISK_FULL, "", error); } // Here the data is passed to the application for processing. It can signal an error to TFTP // by returning a non-zero value. if (tftp_app_process_data_block(data_pkt->data, num_bytes - TFTP_MIN_PKT_SIZE) != 0) { // We send an access violation error, but this could be modified to send a custom // error from the application layer */ return tftp_make_error_pkt(tx_buf, TFTP_ERROR_ACCESS_VIOLATION, "", error); } } else { #if TFTP_DEBUG_PRINT printstr("TFTP: Rvcd invalid data, block #"); printintln(block_num); #endif } // Make the ACK packet for the received data return tftp_make_ack_pkt(tx_buf, block_num); } case TFTP_OPCODE_ACK: // Acknowledgement { // We never expect to receive an ACK packet from the active connection return tftp_make_error_pkt(tx_buf, TFTP_ERROR_ILLEGAL_OPERATION, "", error); } case TFTP_OPCODE_ERROR: // Error packet { return -1; } default: { // Error: Not a valid TFTP opcode return tftp_make_error_pkt(tx_buf, TFTP_ERROR_ILLEGAL_OPERATION, "", error); } } }