static void user_fn(void) { int_32 c; uint_32 flags=0; MQX_FILE_PTR serial_fd = fopen(BSP_DEFAULT_IO_CHANNEL, 0); ioctl(serial_fd, IO_IOCTL_SERIAL_SET_FLAGS, &flags); printf("\nEthernet <-> Serial Bridge\n"); fprintf(serial_fd,"\r\nSerial <-> Ethernet Bridge\r\n"); while (TRUE) { if (fstatus( serial_fd )) { c = fgetc( serial_fd ); if (c==IO_ERROR) break; putchar((char)c); } if (status()) { c = getchar(); if (c==IO_ERROR) break; fputc((char)c,serial_fd); } } fclose(serial_fd); }
CBaseDec::RetCode CBaseDec::DecoderBase(CAudiofile* const in, const int OutputFd, State* const state, time_t* const t, unsigned int* const secondsToSkip) { RetCode Status = OK; FILE* fp = fopen( in->Filename.c_str(), "r" ); if ( fp == NULL ) { fprintf( stderr, "Error opening file %s for decoding.\n", in->Filename.c_str() ); Status = INTERNAL_ERR; } /* jump to first audio frame; audio_start_pos is only set for FILE_MP3 */ else if ( in->MetaData.audio_start_pos && fseek( fp, in->MetaData.audio_start_pos, SEEK_SET ) == -1 ) { fprintf( stderr, "fseek() failed.\n" ); Status = INTERNAL_ERR; } if ( Status == OK ) { CFile::FileType ft = in->FileType; if( in->FileType == CFile::STREAM_AUDIO ) { if ( fstatus( fp, ShoutcastCallback ) < 0 ) fprintf( stderr, "Error adding shoutcast callback: %s", err_txt ); if (ftype(fp, "ogg")) ft = CFile::FILE_OGG; else if (ftype(fp, "mpeg")) ft = CFile::FILE_MP3; else ft = CFile::FILE_UNKNOWN; } else { struct stat st; if (!fstat(fileno(fp), &st)) in->MetaData.filesize = st.st_size; } in->MetaData.type = ft; Status = CFfmpegDec::getInstance()->Decoder(fp, OutputFd, state, &in->MetaData, t, secondsToSkip ); if ( fclose( fp ) == EOF ) { fprintf( stderr, "Could not close file %s.\n", in->Filename.c_str() ); } } return Status; }
/* * Task for reading from socket and sending data to output file descriptor. */ void telnetcln_in_task(void * v_context, void * creator) { TELNETCLN_CONTEXT *context; context = (TELNETCLN_CONTEXT *) v_context; context->rx_tid = _task_get_id(); RTCS_task_resume_creator(creator, RTCS_OK); _task_block(); while(context->valid == TELNETCLN_CONTEXT_VALID) { bool b_result = TRUE; #if MQX_USE_IO_OLD /* old IO (fio.h) fstatus() */ b_result = fstatus(context->telnetfd); #endif if(b_result && context->tx_tid) { int32_t c; c = (int32_t) fgetc(context->telnetfd); if(c == IO_EOF) { if (context->tx_tid == 0) { context->params.callbacks.on_disconnected(context->params.callbacks.param); telnetcln_cleanup(context); _mem_free(context); return; } break; } fputc(c & 0x7F, context->params.fd_out); } else { /* * this executes only for old IO. * for NIO we expect fgetc() is blocking */ if (context->tx_tid == 0) { context->params.callbacks.on_disconnected(context->params.callbacks.param); telnetcln_cleanup(context); _mem_free(context); return; } RTCS_time_delay(10); } } context->rx_tid = 0; #if !MQX_USE_IO_OLD ioctl(fileno(context->params.fd_in), IOCTL_ABORT, NULL); #endif }
void _io_pcb_mqxa_read_task ( /* [IN] the device info */ uint_32 parameter ) { /* Body */ IO_PCB_MQXA_INFO_STRUCT_PTR info_ptr; IO_PCB_STRUCT_PTR pcb_ptr; uchar_ptr input_ptr; uchar_ptr input_init_ptr; boolean got_length = 0; _mem_size input_length = 0; _mem_size max_length = 0; _mem_size count = 0; _mqx_uint state = 0; _mqx_uint next_state = 0; uchar crc0 = 0; uchar crc1 = 0; uchar packet_crc0 = 0; uchar packet_crc1 = 0; uchar tmp; uchar c; info_ptr = (IO_PCB_MQXA_INFO_STRUCT_PTR)parameter; /* Get a PCB */ pcb_ptr = _io_pcb_alloc(info_ptr->READ_PCB_POOL, FALSE); #if MQX_CHECK_ERRORS if (pcb_ptr == NULL) { _task_block(); } /* Endif */ #endif max_length = info_ptr->INIT.INPUT_MAX_LENGTH; input_init_ptr = pcb_ptr->FRAGMENTS[0].FRAGMENT; state = AP_STATE_SYNC; /* Waiting for sync */ next_state = AP_STATE_SYNC; /* Waiting for sync */ while (TRUE) { if (info_ptr->INIT.IS_POLLED) { while (!fstatus(info_ptr->FD)) { _time_delay_ticks(1); } /* Endwhile */ } /* Endif */ c = (uchar)fgetc(info_ptr->FD); switch (state) { case AP_STATE_SYNC: if (c == AP_SYNC) { /* Sync detected. Start packet reception. */ state = AP_STATE_READING; next_state = AP_STATE_SYNC; count = 0; input_ptr = input_init_ptr; crc0 = 0x7e; crc1 = 0x7e; got_length = FALSE; } /* Endif */ break; case AP_STATE_SYNC_SKIP: if (c != AP_SYNC) { /* Single sync detected. Restart message reception. */ count = 0; input_ptr = input_init_ptr; crc0 = 0x7e; crc1 = 0x7e; got_length = FALSE; *input_ptr++ = c; ++count; AP_CHECKSUM(c, crc0, crc1); state = AP_STATE_READING; } else { state = next_state; } /* Endif */ break; case AP_STATE_READING: *input_ptr++ = c; ++count; AP_CHECKSUM(c, crc0, crc1); if (got_length ) { if (count >= input_length){ state = AP_STATE_CS0; } /* Endif */ } else { if ( count > MQXA_MSG_CONTROL_OFFSET) { /* The complete packet header has been read in */ input_length = GET_LENGTH(input_init_ptr); if (input_length > max_length) { next_state = AP_STATE_SYNC; ++info_ptr->RX_PACKETS_TOO_LONG; } else { got_length = TRUE; if (count >= input_length) { state = AP_STATE_CS0; } /* Endif */ } /* Endif */ } /* Endif */ } /* Endif */ if (c == AP_SYNC) { next_state = state; state = AP_STATE_SYNC_SKIP; } /* Endif */ break; case AP_STATE_CS0: packet_crc0 = c; state = AP_STATE_CS1; if (c == AP_SYNC) { next_state = state; state = AP_STATE_SYNC_SKIP; } /* Endif */ break; case AP_STATE_CS1: packet_crc1 = c; state = AP_STATE_DONE; if (c == AP_SYNC) { next_state = state; state = AP_STATE_SYNC_SKIP; } /* Endif */ break; default: state = AP_STATE_SYNC; break; } /* Endswitch */ if ( state == AP_STATE_DONE ) { /* Calculate the CRCs */ crc1 = (crc1 + 2 * crc0) & 0xFF; tmp = crc0 - crc1; crc1 = (crc1 - (crc0 * 2)) & 0xFF; crc0 = tmp & 0xFF; if ((crc0 == packet_crc0) && (crc1 == packet_crc1)) { ++info_ptr->RX_PACKETS; pcb_ptr->FRAGMENTS[0].LENGTH = input_length; if (info_ptr->READ_CALLBACK_FUNCTION) { /* Start CR 398 */ (*info_ptr->READ_CALLBACK_FUNCTION)(info_ptr->CALLBACK_FD, pcb_ptr); /* End CR */ } else { _queue_enqueue((QUEUE_STRUCT_PTR)&info_ptr->READ_QUEUE, (QUEUE_ELEMENT_STRUCT_PTR)&pcb_ptr->QUEUE); _lwsem_post(&info_ptr->READ_LWSEM); }/* Endif */ pcb_ptr = _io_pcb_alloc(info_ptr->READ_PCB_POOL, TRUE); /* Start CR 385 */ if (pcb_ptr == NULL) { /* Start CR 399 */ while (pcb_ptr == NULL) { _time_delay_ticks(2); pcb_ptr = _io_pcb_alloc(info_ptr->READ_PCB_POOL, TRUE); } /* Endwhile */ /* End CR 399 */ } /* Endif */ /* End CR 385 */ input_init_ptr = pcb_ptr->FRAGMENTS[0].FRAGMENT; } else { ++info_ptr->RX_PACKETS_BAD_CRC; } /* Endif */ state = AP_STATE_SYNC; } /* Endif */ } /* Endwhile */ } /* Endbody */
/* * Task for reading from input and sending data through socket. */ void telnetcln_out_task(void * v_context, void * creator) { TELNETCLN_CONTEXT *context; context = (TELNETCLN_CONTEXT *) v_context; context->tx_tid = _task_get_id(); RTCS_task_resume_creator(creator, RTCS_OK); while(context->valid == TELNETCLN_CONTEXT_VALID) { bool b_result = TRUE; #if MQX_USE_IO_OLD /* * old IO (fio.h) input might be polled UART driver * thus only for this case we check if a character is available. * is there is no character, sleep for a tick */ b_result = fstatus(context->params.fd_in); #endif if (b_result && context->rx_tid) { int32_t c; c = (int32_t) fgetc(context->params.fd_in); #if !MQX_USE_IO_OLD if (EOF == c) { clearerr(context->params.fd_in); if (context->rx_tid == 0) { context->params.callbacks.on_disconnected(context->params.callbacks.param); telnetcln_cleanup(context); _mem_free(context); return; } break; } if (c == '\n') { c = '\r' ; } #endif if (fputc(c & 0x7F, context->telnetfd) == IO_EOF) { break; } } else { /* * this executes only for old IO uart driver. * for NIO tty, fgetc() above is blocking function. */ if (context->rx_tid == 0) { context->params.callbacks.on_disconnected(context->params.callbacks.param); telnetcln_cleanup(context); _mem_free(context); return; } RTCS_time_delay(10); } } context->tx_tid = 0; }
CBaseDec::RetCode CBaseDec::DecoderBase(CAudiofile* const in, const int OutputFd, State* const state, time_t* const t, unsigned int* const secondsToSkip) { RetCode Status = OK; FILE* fp=fopen(in->Filename.c_str(), "r"); if ( fp == NULL ) { fprintf( stderr, "Error opening file %s for decoding.\n", in->Filename.c_str() ); Status = INTERNAL_ERR; } else { /* jump to first audio frame; audio_start_pos is only set for FILE_MP3 */ if ( in->MetaData.audio_start_pos && fseek( fp, in->MetaData.audio_start_pos, SEEK_SET ) == -1 ) { fprintf( stderr, "fseek() failed.\n" ); Status = INTERNAL_ERR; } } if ( Status == OK ) { if( in->FileType == CFile::STREAM_AUDIO ) { if ( fstatus( fp, ShoutcastCallback ) < 0 ) { fprintf( stderr, "Error adding shoutcast callback: %s", err_txt ); } if(ftype(fp, "ogg")) { Status = COggDec::getInstance()->Decoder( fp, OutputFd, state, &in->MetaData, t, secondsToSkip ); } else { Status = CMP3Dec::getInstance()->Decoder( fp, OutputFd, state, &in->MetaData, t, secondsToSkip ); } } else if( in->FileType == CFile::FILE_MP3) { Status = CMP3Dec::getInstance()->Decoder( fp, OutputFd, state, &in->MetaData, t, secondsToSkip ); } else if( in->FileType == CFile::FILE_OGG ) { Status = COggDec::getInstance()->Decoder( fp, OutputFd, state, &in->MetaData, t, secondsToSkip ); } else if( in->FileType == CFile::FILE_WAV ) { Status = CWavDec::getInstance()->Decoder( fp, OutputFd, state, &in->MetaData, t, secondsToSkip ); } else if( in->FileType == CFile::FILE_CDR ) { Status = CCdrDec::getInstance()->Decoder( fp, OutputFd, state, &in->MetaData, t, secondsToSkip ); } #ifdef ENABLE_FLAC else if( in->FileType == CFile::FILE_FLAC ) { Status = CFlacDec::getInstance()->Decoder( fp, OutputFd, state, &in->MetaData, t, secondsToSkip ); } #endif else { fprintf( stderr, "DecoderBase: Supplied filetype is not " ); fprintf( stderr, "supported by Audioplayer.\n" ); Status = INTERNAL_ERR; } if ( fclose( fp ) == EOF ) { fprintf( stderr, "Could not close file %s.\n", in->Filename.c_str() ); } } return Status; }
uint_32 TELNET_connect ( _ip_address ipaddress ) { /* Body */ MQX_FILE_PTR sockfd, telnetfd; sockaddr_in addr; uint_32 sock; uint_32 error; boolean work; int_32 c; /* ** Install device driver for socket and telnet */ _io_socket_install("socket:"); _io_telnet_install("telnet:"); sock = socket(PF_INET, SOCK_STREAM, 0); if (sock == RTCS_SOCKET_ERROR) { return RTCSERR_OUT_OF_SOCKETS; } /* Endif */ addr.sin_family = AF_INET; addr.sin_port = 0; addr.sin_addr.s_addr = INADDR_ANY; error = bind(sock,(const sockaddr *)&addr, sizeof(addr)); if (error != RTCS_OK) { return error; } /* Endif */ addr.sin_port = IPPORT_TELNET; addr.sin_addr.s_addr = ipaddress; error = connect(sock, (const sockaddr *)(&addr), sizeof(addr)); if (error != RTCS_OK) { shutdown(sock, FLAG_ABORT_CONNECTION); return error; } /* Endif */ sockfd = fopen("socket:", (char_ptr)sock); if (sockfd == NULL) { shutdown(sock, FLAG_ABORT_CONNECTION); return RTCSERR_FOPEN_FAILED; } /* Endif */ telnetfd = fopen("telnet:", (char_ptr)sockfd); if (telnetfd == NULL) { fclose(sockfd); shutdown(sock, FLAG_ABORT_CONNECTION); return RTCSERR_FOPEN_FAILED; } /* Endif */ /* Set the console stream to the client */ ioctl(telnetfd, IO_IOCTL_SET_STREAM, (uint_32_ptr)((void _PTR_)stdin)); while (TRUE) { work = FALSE; if (fstatus(stdin)) { work = TRUE; c = (int_32)fgetc(stdin); if (fputc(c & 0x7F, telnetfd) == IO_EOF) { break; } } /* Endif */ if (fstatus(telnetfd)) { work = TRUE; c = (int_32)fgetc(telnetfd); if (c == IO_EOF) { break; }/* Endif */ fputc(c & 0x7F, stdout); } /* Endif */ /* Let another task run if there is no I/O */ if (!work) { RTCS_time_delay(1); } /* Endif */ } /* Endwhile */ fclose(telnetfd); fclose(sockfd); shutdown(sock, FLAG_CLOSE_TX); return RTCS_OK; } /* Endbody */