/*---------------------------------------------------------------------------*/ PROCESS_THREAD(shell_ls_process, ev, data) { static struct cfs_dir dir; static cfs_offset_t totsize; struct cfs_dirent dirent; char buf[32]; PROCESS_BEGIN(); if(data != NULL) { if(cfs_opendir(&dir, data) != 0) { shell_output_str(&ls_command, "Cannot open directory", ""); } else { totsize = 0; while(cfs_readdir(&dir, &dirent) == 0) { totsize += dirent.size; sprintf(buf, "%lu ", (unsigned long)dirent.size); /* printf("'%s'\n", dirent.name);*/ shell_output_str(&ls_command, buf, dirent.name); } cfs_closedir(&dir); sprintf(buf, "%lu", (unsigned long)totsize); shell_output_str(&ls_command, "Total size: ", buf); } } PROCESS_END(); }
/** Directory listing callback. * Responds to a SBP_MSG_FILEIO_READ_DIR_REQUEST message. * * The offset parameter can be used to skip the first n elements of the file * list. * * Returns a SBP_MSG_FILEIO_READ_DIR_RESPONSE message containing the directory * listings as a NULL delimited list. The listing is chunked over multiple SBP * packets and the end of the list is identified by an entry containing just * the character 0xFF. */ static void read_dir_cb(u16 sender_id, u8 len, u8 msg[], void* context) { (void)context; if (sender_id != SBP_SENDER_ID) { log_error("Invalid sender!\n"); return; } if ((len < 5) || (msg[len-1] != '\0')) { log_error("Invalid fileio read dir message!\n"); return; } u32 offset = ((u32)msg[3] << 24) | ((u32)msg[2] << 16) | (msg[1] << 8) | msg[0]; struct cfs_dir dir; struct cfs_dirent dirent; u8 buf[256]; memcpy(buf, msg, len); cfs_opendir(&dir, (char*)&msg[4]); while (offset && (cfs_readdir(&dir, &dirent) == 0)) offset--; while ((cfs_readdir(&dir, &dirent) == 0) && (len < SBP_FRAMING_MAX_PAYLOAD_SIZE)) { strncpy((char*)buf + len, dirent.name, SBP_FRAMING_MAX_PAYLOAD_SIZE - len); len += strlen(dirent.name) + 1; } if (len < SBP_FRAMING_MAX_PAYLOAD_SIZE) buf[len++] = 0xff; cfs_closedir(&dir); sbp_send_msg(SBP_MSG_FILEIO_READ_DIR_RESPONSE, len, buf); }
/*---------------------------------------------------------------------------*/ static void start_loaddir(void) { memset(localfiles, 0, sizeof(localfiles)); localfileptr = 0; cfs_opendir(&dir, "."); process_post(&ftp_process, PROCESS_EVENT_CONTINUE, NULL); }
/*-----------------------------------------------------------------------------------*/ static void startloading(void) { if(cfs_opendir(&dir, "/") != 0) { show_statustext("Cannot open directory"); loading = 0; } else { loading = LOADING_DIR; process_post(&directory_process, PROCESS_EVENT_CONTINUE, NULL); numfiles = 0; } }
/*-----------------------------------------------------------------------------------*/ static void directory(char *str) { if(cfs_opendir(&dir, "/") != 0) { shell_output("Cannot open directory", ""); showingdir = 0; } else { shell_output("Disk directory:", ""); showingdir = 1; totsize = 0; ek_post(EK_PROC_ID(EK_CURRENT()), EK_EVENT_CONTINUE, NULL); } }
static int dir_test(void) { struct cfs_dir dir; struct cfs_dirent dirent; /* Coffee provides a root directory only. */ if(cfs_opendir(&dir, "/") != 0) { printf("failed to open the root directory\n"); return 0; } /* List all files and their file sizes. */ printf("Available files\n"); while(cfs_readdir(&dir, &dirent) == 0) { printf("%s (%lu bytes)\n", dirent.name, (unsigned long)dirent.size); } cfs_closedir(&dir); return 1; }
//TODO: help verify void handle_list_file(char* prefix) { log_info("handle_list_file(%s)\n", prefix); char buf[200] = ""; uint8_t buf_size = 0; struct cfs_dir dir; int ret = cfs_opendir(&dir, ""); if (ret == -1) { log_info("cfs_opendir() failed: %d\n", ret); return; } while (ret != -1) { struct cfs_dirent dirent; ret = cfs_readdir(&dir, &dirent); if (ret != -1) { log_info("file:%s, %d\n", dirent.name, dirent.size); char* short_name = filter_filename_by_prefix(prefix, dirent.name); uint8_t len = strlen(short_name) + 1; if (buf_size + len >= sizeof(buf) - 1) break; strcat(buf, short_name); strcat(buf, ";"); buf_size += len; } } cfs_closedir(&dir); send_file_list(buf); }
/*---------------------------------------------------------------------------*/ void ctk_filedialog_open(CC_REGISTER_ARG struct ctk_filedialog_state *s, const char *buttontext, process_event_t event) { ctk_dialog_new(&dialog, 20, 5 + FILES_HEIGHT); CTK_WIDGET_ADD(&dialog, &leftptrlabel); CTK_WIDGET_ADD(&dialog, &fileslabel); CTK_WIDGET_ADD(&dialog, &rightptrlabel); CTK_WIDGET_ADD(&dialog, &filenameentry); CTK_BUTTON_NEW(&button, 1, 4 + FILES_HEIGHT, strlen(buttontext), (char *)buttontext); CTK_WIDGET_ADD(&dialog, &button); ctk_dialog_open(&dialog); state = STATE_OPEN; memset(filename, 0, sizeof(filename)); memset(leftptr, ' ', sizeof(leftptr)); memset(rightptr, ' ', sizeof(rightptr)); memset(files, 0, sizeof(files)); fileptr = 0; dirfileptr = 0; showptr(); cfs_opendir(&dir, "."); process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, s); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(console_server, ev, data) { static uint8_t buf[257]; static uint8_t processingFile = 0; static uint32_t received = 0; static struct cfs_dirent dirent; static struct cfs_dir dir; static uint32_t fdFile; static char *filename; PROCESS_BEGIN(); elfloader_init(); printf("Console server started !\n"); while(1) { PROCESS_YIELD(); if (ev == serial_line_event_message) { if (!strcmp(data, "ls")) { if(cfs_opendir(&dir, ".") == 0) { while(cfs_readdir(&dir, &dirent) != -1) { printf("File: %s (%ld bytes)\n", dirent.name, (long)dirent.size); } cfs_closedir(&dir); } } else if (!strcmp(data, "format")) { /* format the flash */ printf("Formatting\n"); printf("It takes around 3 minutes\n"); printf("...\n"); fdFile = cfs_coffee_format(); printf("Formatted with result %ld\n", fdFile); } else if (strstr(data, "cat") == data) { int n, jj; char* tmp = strstr(data, " "); tmp++; fdFile = cfs_open(tmp, CFS_READ); if (fdFile < 0) printf("error opening the file %s\n", tmp); while ((n = cfs_read(fdFile, buf, 60)) > 0) { for (jj = 0 ; jj < n ; jj++) printf("%c", (char)buf[jj]); } printf("\n"); cfs_close(fdFile); if (n!=0) printf("Some error reading the file\n"); } else if (strstr(data, "loadelf") == data) { filename = strstr(data, " "); filename++; // Cleanup previous loads if (elfloader_autostart_processes != NULL) autostart_exit(elfloader_autostart_processes); elfloader_autostart_processes = NULL; // Load elf file fdFile = cfs_open(filename, CFS_READ | CFS_WRITE); received = elfloader_load(fdFile); cfs_close(fdFile); printf("Result of loading %lu\n", received); // As the file has been modified and can't be reloaded, remove it printf("Remove dirty firmware '%s'\n", filename); cfs_remove(filename); // execute the program if (ELFLOADER_OK == received) { if (elfloader_autostart_processes) { PRINT_PROCESSES(elfloader_autostart_processes); autostart_start(elfloader_autostart_processes); } } else if (ELFLOADER_SYMBOL_NOT_FOUND == received) { printf("Symbol not found: '%s'\n", elfloader_unknown); } } else if (strstr(data, "rm") == data) { int n, jj; char* tmp = strstr(data, " "); tmp++; cfs_remove(tmp); } else if (strstr(data, "upload") == data) { char* tmp = strstr(data, " "); tmp++; fdFile = cfs_open(tmp, CFS_READ | CFS_WRITE); printf("Uploading file %s\n", tmp); processingFile = 1; } else if (!strcmp(data, "endupload")) { cfs_close(fdFile); printf("File uploaded (%ld bytes)\n", received); received = 0; processingFile = 0; } else if (processingFile) { int n = strlen(data); int r = decode(data, n, buf); received += r; cfs_write(fdFile, buf, r); } else { printf("%s (%lu bytes received)\n", (char*)data, received); } } } PROCESS_END(); }