fork from va009039/USBLocalFileSystem
Dependents: 11u35_usbLocalFilesystem
Fork of USBLocalFileSystem by
Revision 4:8f6857784854, committed 2014-05-08
- Comitter:
- va009039
- Date:
- Thu May 08 23:43:46 2014 +0900
- Parent:
- 3:09b562c87f89
- Child:
- 5:7e478063c6ee
- Commit message:
- remove disk access callback. add virtual com sendBreak callback.
Changed in this revision
--- a/USBDevice.lib Tue May 06 16:05:57 2014 +0900 +++ b/USBDevice.lib Thu May 08 23:43:46 2014 +0900 @@ -1,1 +1,1 @@ -http://mbed.org/users/va009039/code/USBDevice/#b5a68b899fd1 +http://mbed.org/users/va009039/code/USBDevice/#c4c8fd0b0f12
--- a/USBLocalFileSystem.cpp Tue May 06 16:05:57 2014 +0900 +++ b/USBLocalFileSystem.cpp Thu May 08 23:43:46 2014 +0900 @@ -1,6 +1,7 @@ #include "USBLocalFileSystem.h" #include "RamDisk.h" #include "SDStorage.h" +#include "USB_CDC.h" USBLocalFileSystem::USBLocalFileSystem(const char* name) { @@ -36,6 +37,15 @@ _local = new LocalStorage(_storage); } +int USBLocalFileSystem::lock(bool f) +{ + return _usb->block(f); +} + +bool USBLocalFileSystem::find(char* name, size_t size, const char* pat) +{ + return LocalStorage::find(name, size, _name, pat); +} int USBLocalFileSystem::readable() { @@ -52,9 +62,32 @@ return _usb->getc(); } -void USBLocalFileSystem::putc(int c) +int USBLocalFileSystem::putc(int c) { _usb->putc(c); + return c; +} + +int USBLocalFileSystem::puts(const char* str) +{ + while(*str) { + putc(*str++); + } + return 0; } +void USBLocalFileSystem::attachSendBreak(void (*fptr)(uint16_t duration)) +{ + getUsb()->getCDC()->attachSendBreak(fptr); +} +void USBLocalFileSystem::attachControlLineState(void (*fptr)(int dts, int dtr)) +{ + getUsb()->getCDC()->attachControlLineStateChanged(fptr); +} + +void USBLocalFileSystem::attachSettingChanged(void (*fptr)(int baud, int bits, int parity, int stop)) +{ + getUsb()->getCDC()->attach(fptr); +} +
--- a/USBLocalFileSystem.h Tue May 06 16:05:57 2014 +0900 +++ b/USBLocalFileSystem.h Thu May 08 23:43:46 2014 +0900 @@ -40,7 +40,10 @@ * @param name The name used to access the virtual filesystem */ USBLocalFileSystem(StorageInterface* storage, const char* name = "local"); - void remount(); + + void remount(); // remount local storage + int lock(bool f); + bool find(char* name, size_t size, const char* pat); /** Determine if there is a character available to read * @@ -58,8 +61,31 @@ */ int writeable(); + /** Read a char from the serial port + * + * @returns The char read from the serial port + */ int getc(); - void putc(int c); + + /** Write a char to the serial port + * + * @param c The char to write + * + * @returns The written char or -1 if an error occured + */ + int putc(int c); + + /** Write a string to the serial port + * + * @param str The string to write + * + * @returns 0 if the write succeeds, EOF for error + */ + int puts(const char* str); + + void attachSendBreak(void (*fptr)(uint16_t duration)); + void attachControlLineState(void (*fptr)(int dts, int dtr)); + void attachSettingChanged(void (*fptr)(int baud, int bits, int parity, int stop)); StorageInterface* getStoage() { return _storage; } LocalStorage* getLocal() { return _local; }
--- a/USBMSD2/USBMSD2.cpp Tue May 06 16:05:57 2014 +0900 +++ b/USBMSD2/USBMSD2.cpp Thu May 08 23:43:46 2014 +0900 @@ -75,6 +75,21 @@ return _hid->send(report); } +USB_MSD* USBMSD2::getMSD() +{ + return _msd; +} + +USB_CDC* USBMSD2::getCDC() +{ + return _cdc; +} + +USB_HID* USBMSD2::getHID() +{ + return _hid; +} + bool USBMSD2::connect() { if (_msd->connect()) {
--- a/USBMSD2/USBMSD2.h Tue May 06 16:05:57 2014 +0900 +++ b/USBMSD2/USBMSD2.h Thu May 08 23:43:46 2014 +0900 @@ -112,6 +112,10 @@ */ bool send(HID_REPORT* report); + USB_MSD* getMSD(); + USB_CDC* getCDC(); + USB_HID* getHID(); + protected: /* * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
--- a/USBMSD2/USB_CDC.cpp Tue May 06 16:05:57 2014 +0900 +++ b/USBMSD2/USB_CDC.cpp Thu May 08 23:43:46 2014 +0900 @@ -26,6 +26,8 @@ #define CDC_DBG_HEX(A,B) while(0) #endif +static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08}; + #define CDC_SET_LINE_CODING 0x20 #define CDC_GET_LINE_CODING 0x21 #define CDC_SET_CONTROL_LINE_STATE 0x22 @@ -34,6 +36,7 @@ #define MAX_CDC_REPORT_SIZE MAX_PACKET_SIZE_EPBULK USB_CDC::USB_CDC(USBDevice* device) : _device(device), _rx_buf(128) + ,settingsChangedCallback(NULL),controlLineStateChangedCallback(NULL),sendBreakCallback(NULL) { CDC_DBG("device=%p", device); @@ -68,21 +71,6 @@ return 1; } -void USB_CDC::baud_callback(int baudrate) -{ - CDC_DBG("baudrate=%d", baudrate); -} - -void USB_CDC::send_break_callback(uint16_t duration) -{ - CDC_DBG("duration=%04x", duration); -} - -void USB_CDC::control_line_callback(int rts, int dtr) -{ - CDC_DBG("rts=%d, dtr=%d", rts, dtr); -} - bool USB_CDC::send(uint8_t * buffer, uint32_t size) { return _device->write(CDC_EPBULK_IN, buffer, size, MAX_CDC_REPORT_SIZE); } @@ -105,8 +93,6 @@ bool USB_CDC::Request_callback(CONTROL_TRANSFER* transfer) { - static uint8_t cdc_line_coding[7]= {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08}; - if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { switch (transfer->setup.bRequest) { case CDC_SET_LINE_CODING: // 0x20 @@ -122,33 +108,34 @@ return true; case CDC_SET_CONTROL_LINE_STATE: // 0x22 - control_line_callback((transfer->setup.wValue>>1) & 1, (transfer->setup.wValue) & 1); + controlLineStateChanged((transfer->setup.wValue>>1) & 1, (transfer->setup.wValue) & 1); terminal_connected = false; return true; case CDC_SEND_BREAK: // 0x23 - send_break_callback(transfer->setup.wValue); + sendBreak(transfer->setup.wValue); return true; } } return false; } -static uint32_t LD32(uint8_t* buf) -{ - return buf[0]|(buf[1]<<8)|(buf[2]<<16)|(buf[3]<<24); -} - bool USB_CDC::RequestCompleted_callback(CONTROL_TRANSFER* transfer, uint8_t* buf, int length) { CDC_DBG("transer=%p", transfer); - int baudrate; if (transfer->setup.bmRequestType.Type == CLASS_TYPE) { - switch (transfer->setup.bRequest) { - case CDC_SET_LINE_CODING: // 0x20 - baudrate = LD32(buf); - baud_callback(baudrate); + if (transfer->setup.bRequest == CDC_SET_LINE_CODING) { + if (memcmp(cdc_line_coding, buf, 7)) { + memcpy(cdc_line_coding, buf, 7); + + int baud = buf[0] + (buf[1] << 8) + + (buf[2] << 16) + (buf[3] << 24); + int stop = (buf[4] == 0) ? 1 : 2; + int bits = buf[6]; + int parity = buf[5]; + lineCodingChanged(baud, bits, parity, stop); return true; + } } } CDC_DBG_HEX((uint8_t*)transfer, sizeof(CONTROL_TRANSFER)); @@ -170,3 +157,28 @@ _device->readStart(CDC_EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); return true; } + +void USB_CDC::lineCodingChanged(int baud, int bits, int parity, int stop) +{ + CDC_DBG("baud=%d,bits=%d,parity=%d,stop=%d", baud,bits, parity, stop); + if (settingsChangedCallback) { + settingsChangedCallback(baud, bits, parity, stop); + } +} + +void USB_CDC::controlLineStateChanged(int rts, int dtr) +{ + CDC_DBG("rts=%d,dtr=%d", rts, dtr); + if (controlLineStateChangedCallback) { + controlLineStateChangedCallback(rts, dtr); + } +} + +void USB_CDC::sendBreak(uint16_t duration) +{ + CDC_DBG("duration=%u", duration) + if (sendBreakCallback) { + sendBreakCallback(duration); + } +} +
--- a/USBMSD2/USB_CDC.h Tue May 06 16:05:57 2014 +0900 +++ b/USBMSD2/USB_CDC.h Thu May 08 23:43:46 2014 +0900 @@ -53,10 +53,6 @@ int writeable(); - void baud_callback(int baudrate); - void send_break_callback(uint16_t duration); - void control_line_callback(int rts, int dtr); - /* * Send a buffer * @@ -92,9 +88,35 @@ bool Request_callback(CONTROL_TRANSFER* transfer); bool RequestCompleted_callback(CONTROL_TRANSFER* transfer, uint8_t* buf, int length); bool EPBULK_OUT_callback(); + + /** + * Attach a callback to call when serial's settings are changed. + * + * @param fptr function pointer + */ + void attach(void (*fptr)(int baud, int bits, int parity, int stop)) { + settingsChangedCallback = fptr; + } + + void attachControlLineStateChanged(void (*fptr)(int rts, int dtr)) { + controlLineStateChangedCallback = fptr; + } + + void attachSendBreak(void (*fptr)(uint16_t duration)) { + sendBreakCallback = fptr; + } +protected: + void lineCodingChanged(int baud, int bits, int parity, int stop); + void controlLineStateChanged(int rts, int dtr); + void sendBreak(uint16_t duration); + private: USBDevice* _device; CircBuffer<uint8_t> _rx_buf; + void (*settingsChangedCallback)(int baud, int bits, int parity, int stop); + void (*controlLineStateChangedCallback)(int rts, int dtr); + void (*sendBreakCallback)(uint16_t duration); volatile bool terminal_connected; }; +
--- a/src/Storage.cpp Tue May 06 16:05:57 2014 +0900 +++ b/src/Storage.cpp Thu May 08 23:43:46 2014 +0900 @@ -1,7 +1,5 @@ #include "Storage.h" #include "FATFileSystem.h" -#include "mbed_debug.h" -#include "mystring.h" #include <ctype.h> #if (DEBUG2 > 3) @@ -10,18 +8,52 @@ #define STORAGE_DBG(...) #endif -#define LOCAL_DIR "local" - LocalStorage::LocalStorage(StorageInterface* storage, const char* name) : FATFileSystem(name),_storage(storage) { _name = name; } -extern FILINFO FATDirHandle_finfo; // fst/FATDirHandle.cpp -/* static */ bool LocalStorage::find_bin(mystring& filename) +static bool match1(const char* name, const char* pat) { + while(1) { + char c = *pat++; + char d = *name++; + if (c == '\0' && d == '\0') { + return true; + } else if (c == '\0' || d == '\0') { + return false; + } + switch(c) { + case '?': + break; + case '*': + name--; + while(*name) { + if (*name == '.') { + break; + } + name++; + } + break; + default: + if (toupper(d) != toupper(c)) { + return false; + } + break; + } + } +} + +extern FILINFO FATDirHandle_finfo; // fat/FATDirHandle.cpp +/* static */ bool LocalStorage::find(char* name, size_t size, const char* dirname, const char* pat) { - DIR *dir = ::opendir("/"LOCAL_DIR); + char dirpath[32]; + strcpy(dirpath, "/"); + if (strlen(dirname) >= sizeof(dirpath)-2) { + return false; + } + strcat(dirpath, dirname); + DIR *dir = ::opendir(dirpath); if (dir == NULL) { return false; } @@ -29,23 +61,22 @@ bool found = false; struct dirent *entry; while ((entry = readdir(dir)) != NULL) { - mystring name(entry->d_name); - int len = name.size(); - if (name[len-4] == '.' && - toupper(name[len-3]) == 'B' && - toupper(name[len-2]) == 'I' && - toupper(name[len-1]) == 'N') { + if (match1(entry->d_name, pat)) { FILINFO* fi = &FATDirHandle_finfo; uint32_t datetime = fi->ftime | (fi->fdate<<16); STORAGE_DBG("datetime=%08x [%s]", datetime, entry->d_name); if (datetime > fdatetime) { fdatetime = datetime; - filename = "/"LOCAL_DIR"/"; - filename += entry->d_name; - found = true; + if (strlen(dirpath) + 1 + strlen(entry->d_name) < size) { + strcpy(name, dirpath); + strcat(name, "/"); + strcat(name, entry->d_name); + found = true; + } } } } closedir(dir); return found; } +
--- a/src/Storage.h Tue May 06 16:05:57 2014 +0900 +++ b/src/Storage.h Thu May 08 23:43:46 2014 +0900 @@ -18,7 +18,8 @@ virtual uint64_t disk_sectors() { return _storage->storage_sectors(); } - static bool find_bin(mystring& filename); + static bool find(char* buf, size_t size, const char* dirname, const char* pat); + private: StorageInterface* _storage; const char* _name;