size_t fio_printf(int fd, const char *format, ...){ int i,count=0; va_list(v1); va_start(v1, format); int tmpint; char *tmpcharp; for(i=0; format[i]; ++i){ if(format[i]=='%'){ switch(format[i+1]){ case '%': send_byte('%'); break; case 'd': case 'x': case 'X': tmpint = va_arg(v1, int); tmpcharp = itoa(format[i+1]=='x'?"0123456789abcdef":"0123456789ABCDEF", tmpint, format[i+1]=='d'?10: 16); fio_write(fd, tmpcharp, strlen(tmpcharp)); break; case 's': tmpcharp = va_arg(v1, char *); fio_write(fd, tmpcharp, strlen(tmpcharp)); break; } /* Skip the next character */ ++i; }else fio_write(fd, format+i, 1); }
void Print(char *msg) { if(!msg)return; char newLine[]="\n\r"; fio_write(1,msg,strlen(msg)); fio_write(1,newLine,strlen(newLine)); }
void help(){ const char help_desp[] = "\n\rOffer these funcs : "; int i; fio_write(1, &help_desp, sizeof(help_desp)); for (i = 1; i < CMD_COUNT; i++) { fio_write(1, "\n\r", 2); fio_write(1, cmd_data[i].cmd, strlen(cmd_data[i].cmd) + 1); fio_write(1, ": ", 3); fio_write(1, cmd_data[i].description, strlen(cmd_data[i].description) + 1); } }
void load_pak(struct paki_args* args) { result_t r; struct pak_file pak; char filename[DH_PATH_MAX]; path_norm(args->pakfile, args->pakfile); path_tounix(args->path, args->path); r = pak_open(&pak, mem_heap(), args->pakfile, 0); if (IS_FAIL(r)) { err_sendtolog(FALSE); return; } uint file_id = pak_findfile(&pak, args->path); if (file_id == INVALID_INDEX) { printf(TERM_BOLDRED "Extract failed: file '%s' not found in pak.\n" TERM_RESET, args->path); pak_close(&pak); return; } path_getfullfilename(filename, args->path); file_t f = fio_createdisk(filename); if (f == NULL) { printf(TERM_BOLDRED "Extract failed: could not create '%s' for writing.\n" TERM_RESET, filename); pak_close(&pak); err_sendtolog(FALSE); return; } file_t src_file = pak_getfile(&pak, mem_heap(), mem_heap(), file_id, 0); if (src_file == NULL) { pak_close(&pak); fio_close(f); err_sendtolog(FALSE); return; } size_t size; struct allocator* alloc; void* buffer = fio_detachmem(src_file, &size, &alloc); fio_write(f, buffer, size, 1); A_FREE(alloc, buffer); fio_close(f); pak_close(&pak); if (BIT_CHECK(args->usage, PAKI_USAGE_VERBOSE)) { printf(TERM_WHITE "%s -> %s\n" TERM_RESET, args->path, filename); } args->file_cnt ++; // report printf(TERM_BOLDWHITE "Finished: total %d file(s) - %d error(s), %d warning(s)\n" TERM_RESET, args->file_cnt, args->err_cnt, args->warn_cnt); }
/* Refer to school510587's implementation*/ void romfs_mount(void * mountpoint, struct fs_info *file_info) { uint8_t * p = (uint8_t *)mountpoint; if (!mountpoint) return NULL; p += 4; file_info->name = (const char *)p; fio_write(1, "\nhashing", 8); return get_unaligned(p) ? p : NULL; }
void check_keyword(char *msg, char *msg_size) { int i; for (i = 0; i < CMD_COUNT; i++) { if (!strcmp(msg, cmd_data[i].cmd)) { cmd_data[i].func(); break; } } if (i == CMD_COUNT) { fio_write(1, "\n\r: command not found\n\r", 23); } }
void read_romfs_task(void *pvParameters) { char buf[128]; size_t count; int fd = fs_open("/romfs/test.txt", 0, O_RDONLY); do { //Read from /romfs/test.txt to buffer count = fio_read(fd, buf, sizeof(buf)); //Write buffer to fd 1 (stdout, through uart) fio_write(1, buf, count); } while (count); while (1); }
int filedump(const char *filename){ char buf[128]; int fd=fs_open(filename, 0, O_RDONLY); if(fd==OPENFAIL) return 0; fio_printf(1, "\r\n"); int count; while((count=fio_read(fd, buf, sizeof(buf)))>0){ fio_write(1, buf, count); } fio_close(fd); return 1; }
void unit_test_task(void *pvParameters) { char msg1[] = "Start Unit Testing\n\r"; char msg2[128] = "Start String testing\n\r"; /* test file system */ fio_write(1, msg1, strlen(msg1)); /* test puts() */ puts(msg2); /* test strcmp */ if ( strcmp(msg1,msg2)) { puts("[Ok] msg1 ~= msg2\n\r"); } else { puts("strcpy result is not match\n\r"); } /* test printf */ printf("test htoi(255):%s\n\r", htoa(255)); printf("test atoi(100):%s\n\r", itoa(100)); printf("\n"); }
int printf ( const char * format, ... ) { char str[128]; va_list para; va_start(para,format); int curr_pos=0; char ch[]={'0','\0'}; char integer[11]; str[0]='\0'; while(format[curr_pos]!='\0') { if(format[curr_pos]!='%') { ch[0]=format[curr_pos]; strcat(str,ch); } else { switch(format[++curr_pos]) { case 's': strcat(str,va_arg(para,char*)); break; case 'c': ch[0]=(char)va_arg(para,int); strcat(str,ch); break; case 'i': case 'd': strcat(str,itoa(va_arg(para,int),integer)); break; case 'u': strcat(str,itoa(va_arg(para,unsigned),integer)); break; default: break; } } curr_pos++; } va_end(para); return fio_write(1,str,strlen(str)); }
void mmtest_cmd(char *str) { int i,j, size; char *p; unsigned int write_pointer = 0; unsigned int read_pointer = 0; for(j=0; j<MMTEST_NUM; j++) { do{ size = prng() & 0x7FF; }while(size<MIN_ALLOC_SIZE); printf("try to allocate %d bytes\r\n", size); p = (char *) pvPortMalloc(size); printf("malloc returned %d\r\n", p); if (p == NULL || (write_pointer+1)%CIRCBUFSIZE == read_pointer) { // can't do new allocations until we free some older ones while (circbuf_size(write_pointer,read_pointer) > 0) { // confirm that data didn't get trampled before freeing p = slots[read_pointer].pointer; lfsr = slots[read_pointer].lfsr; // reset the PRNG to its earlier state size = slots[read_pointer].size; read_pointer++; read_pointer %= CIRCBUFSIZE; printf("free a block, size %d\r\n", size); for (i = 0; i < size; i++) { unsigned char u = p[i]; unsigned char v = (unsigned char) prng(); if (u != v) { printf("OUCH: u=%02X, v=%02X\r\n", u, v); return; } } vPortFree(p); if ((prng() & 1) == 0) break; } send_byte('\r'); send_byte('\n'); } else { printf("allocate a block, size %d\r\n\r\n", size); if (circbuf_size(write_pointer, read_pointer) == CIRCBUFSIZE - 1) { fio_write(1,"circular buffer overflow\r\n",24); return; } slots[write_pointer].pointer=p; slots[write_pointer].size=size; slots[write_pointer].lfsr=lfsr; write_pointer++; write_pointer %= CIRCBUFSIZE; for (i = 0; i < size; i++) { p[i] = (unsigned char) prng(); } } } do{ p = slots[read_pointer].pointer; lfsr = slots[read_pointer].lfsr; // reset the PRNG to its earlier state size = slots[read_pointer].size; read_pointer++; read_pointer %= CIRCBUFSIZE; printf("free a block, size %d\r\n", size); for (i = 0; i < size; i++) { unsigned char u = p[i]; unsigned char v = (unsigned char) prng(); if (u != v) { printf("OUCH: u=%02X, v=%02X\r\n", u, v); return; } } vPortFree(p); }while(read_pointer!=write_pointer); }
void Print_nextLine() { char newLine[]="\n\r"; fio_write(1,newLine,strlen(newLine)); }
void shell_printf(char *buf, int count){ char msg[] = "\n\r"; fio_write(1, msg, sizeof(msg) ); fio_write(1, buf, count); }
void Puts(char *msg) { if(!msg)return; fio_write(1,msg,strlen(msg)); }
void hello(){ fio_write(1, "\n\r: 'Hello World'\n\r", 19) ; }
void print_to_console( const char *str ) { /* Send the string to the console. * fd = 1 for stdout. */ fio_write( 1, str, strlen( str ) ); }
void shellEnv() { char serial_buf[ MAX_SERIAL_LEN ], ch; char prompt[] = "LanKuDot@FreeRTOS~$ ", newLine[] = "\n\r"; int curr_pos, done; /* Infinite loop for running shell environmrnt. */ while (1) { /* Show prompt each time waiting for user input. */ print_to_console( prompt ); /* Initialize the relatived variable. */ curr_pos = 0; done = 0; do { /* Recieve a byte from the RS232 port ( this call will * block ). */ ch = recieve_byte(); /* If the byte is an end-of-line character, than finish * the string and indicate we are done. */ if ( curr_pos >= MAX_SERIAL_LEN-1 || (ch == '\n') || (ch == '\r') ) { serial_buf[ curr_pos ] = '\0'; done = -1; } /* Backspace key pressed */ else if ( ch == BACKSPACE ) { /* The char is not at the begin of the line. */ if ( curr_pos != 0 ) { --curr_pos; /* Cover the last character with space, and shift the * cursor left. */ print_to_console( "\b \b" ); } } /* Function/Arrow Key pressed */ else if ( ch == ESC ) { /* Arrow Key: ESC[A ~ ESC[D * Function Key: ESC[1~ ~ ESC[6~ */ ch = recieve_byte(); if ( ch == '[' ) { ch = recieve_byte(); if ( ch >= '1' && ch <= '6' ) { /* Discard '~' */ recieve_byte(); } continue; } } /* Otherwise, add the character to the response string */ else { serial_buf[ curr_pos++ ] = ch; /* Display the char that just typed */ // print_to_console( &ch ); // Bug: Press 1, print a lot... fio_write( 1, &ch, 1 ); } } while ( !done ); // end character recieving loop /* Direct to the new line */ print_to_console( newLine ); /* Deal with the command! */ getCommand( serial_buf ); } // end infinite while loop } // end of function shellEnv