[136] | 1 | /*
|
---|
| 2 | Copyright (c) 2013 Arduino LLC. All right reserved.
|
---|
| 3 |
|
---|
| 4 | This library is free software; you can redistribute it and/or
|
---|
| 5 | modify it under the terms of the GNU Lesser General Public
|
---|
| 6 | License as published by the Free Software Foundation; either
|
---|
| 7 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 8 |
|
---|
| 9 | This library is distributed in the hope that it will be useful,
|
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 12 | Lesser General Public License for more details.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU Lesser General Public
|
---|
| 15 | License along with this library; if not, write to the Free Software
|
---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | #include <FileIO.h>
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | File::File(BridgeClass &b) : mode(255), bridge(b) {
|
---|
| 24 | // Empty
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | File::File(const char *_filename, uint8_t _mode, BridgeClass &b) : mode(_mode), bridge(b) {
|
---|
| 28 | filename = _filename;
|
---|
| 29 | char modes[] = {'r', 'w', 'a'};
|
---|
| 30 | uint8_t cmd[] = {'F', modes[mode]};
|
---|
| 31 | uint8_t res[2];
|
---|
| 32 | dirPosition = 1;
|
---|
| 33 | bridge.transfer(cmd, 2, (uint8_t*)filename.c_str(), filename.length(), res, 2);
|
---|
| 34 | if (res[0] != 0) { // res[0] contains error code
|
---|
| 35 | mode = 255; // In case of error keep the file closed
|
---|
| 36 | return;
|
---|
| 37 | }
|
---|
| 38 | handle = res[1];
|
---|
| 39 | buffered = 0;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | File::operator bool() {
|
---|
| 43 | return (mode != 255);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | File::~File() {
|
---|
| 47 | close();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | size_t File::write(uint8_t c) {
|
---|
| 51 | return write(&c, 1);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | size_t File::write(const uint8_t *buf, size_t size) {
|
---|
| 55 | if (mode == 255)
|
---|
| 56 | return -1;
|
---|
| 57 | uint8_t cmd[] = {'g', handle};
|
---|
| 58 | uint8_t res[1];
|
---|
| 59 | bridge.transfer(cmd, 2, buf, size, res, 1);
|
---|
| 60 | if (res[0] != 0) // res[0] contains error code
|
---|
| 61 | return -res[0];
|
---|
| 62 | return size;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | int File::read() {
|
---|
| 66 | doBuffer();
|
---|
| 67 | if (buffered == 0)
|
---|
| 68 | return -1; // no chars available
|
---|
| 69 | else {
|
---|
| 70 | buffered--;
|
---|
| 71 | return buffer[readPos++];
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | int File::peek() {
|
---|
| 76 | doBuffer();
|
---|
| 77 | if (buffered == 0)
|
---|
| 78 | return -1; // no chars available
|
---|
| 79 | else
|
---|
| 80 | return buffer[readPos];
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | boolean File::seek(uint32_t position) {
|
---|
| 84 | uint8_t cmd[] = {
|
---|
| 85 | 's',
|
---|
| 86 | handle,
|
---|
| 87 | (position >> 24) & 0xFF,
|
---|
| 88 | (position >> 16) & 0xFF,
|
---|
| 89 | (position >> 8) & 0xFF,
|
---|
| 90 | position & 0xFF
|
---|
| 91 | };
|
---|
| 92 | uint8_t res[1];
|
---|
| 93 | bridge.transfer(cmd, 6, res, 1);
|
---|
| 94 | if (res[0] == 0) {
|
---|
| 95 | // If seek succeed then flush buffers
|
---|
| 96 | buffered = 0;
|
---|
| 97 | return true;
|
---|
| 98 | }
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | uint32_t File::position() {
|
---|
| 103 | uint8_t cmd[] = {'S', handle};
|
---|
| 104 | uint8_t res[5];
|
---|
| 105 | bridge.transfer(cmd, 2, res, 5);
|
---|
| 106 | //err = res[0]; // res[0] contains error code
|
---|
| 107 | uint32_t pos = res[1] << 24;
|
---|
| 108 | pos += res[2] << 16;
|
---|
| 109 | pos += res[3] << 8;
|
---|
| 110 | pos += res[4];
|
---|
| 111 | return pos - buffered;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | void File::doBuffer() {
|
---|
| 115 | // If there are already char in buffer exit
|
---|
| 116 | if (buffered > 0)
|
---|
| 117 | return;
|
---|
| 118 |
|
---|
| 119 | // Try to buffer up to BUFFER_SIZE characters
|
---|
| 120 | readPos = 0;
|
---|
| 121 | uint8_t cmd[] = {'G', handle, BUFFER_SIZE - 1};
|
---|
| 122 | buffered = bridge.transfer(cmd, 3, buffer, BUFFER_SIZE);
|
---|
| 123 | //err = buff[0]; // First byte is error code
|
---|
| 124 | if (BridgeClass::TRANSFER_TIMEOUT == buffered || 0 == buffered) {
|
---|
| 125 | // transfer failed to retrieve any data
|
---|
| 126 | buffered = 0;
|
---|
| 127 | } else {
|
---|
| 128 | // transfer retrieved at least one byte of data so skip the error code character
|
---|
| 129 | readPos++;
|
---|
| 130 | buffered--;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | int File::available() {
|
---|
| 135 | // Look if there is new data available
|
---|
| 136 | doBuffer();
|
---|
| 137 | return buffered;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | void File::flush() {
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | int File::read(void *buff, uint16_t nbyte) {
|
---|
| 144 | uint16_t n = 0;
|
---|
| 145 | uint8_t *p = reinterpret_cast<uint8_t *>(buff);
|
---|
| 146 | while (n < nbyte) {
|
---|
| 147 | if (buffered == 0) {
|
---|
| 148 | doBuffer();
|
---|
| 149 | if (buffered == 0)
|
---|
| 150 | break;
|
---|
| 151 | }
|
---|
| 152 | *p++ = buffer[readPos++];
|
---|
| 153 | buffered--;
|
---|
| 154 | n++;
|
---|
| 155 | }
|
---|
| 156 | return n;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | uint32_t File::size() {
|
---|
| 160 | if (bridge.getBridgeVersion() < 101)
|
---|
| 161 | return 0;
|
---|
| 162 | uint8_t cmd[] = {'t', handle};
|
---|
| 163 | uint8_t buff[5];
|
---|
| 164 | bridge.transfer(cmd, 2, buff, 5);
|
---|
| 165 | //err = res[0]; // First byte is error code
|
---|
| 166 | uint32_t res;
|
---|
| 167 | res = ((uint32_t)buff[1]) << 24;
|
---|
| 168 | res |= ((uint32_t)buff[2]) << 16;
|
---|
| 169 | res |= ((uint32_t)buff[3]) << 8;
|
---|
| 170 | res |= ((uint32_t)buff[4]);
|
---|
| 171 | return res;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | void File::close() {
|
---|
| 175 | if (mode == 255)
|
---|
| 176 | return;
|
---|
| 177 | uint8_t cmd[] = {'f', handle};
|
---|
| 178 | bridge.transfer(cmd, 2);
|
---|
| 179 | mode = 255;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | const char *File::name() {
|
---|
| 183 | return filename.c_str();
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 | boolean File::isDirectory() {
|
---|
| 188 | uint8_t res[1];
|
---|
| 189 | uint8_t lenght;
|
---|
| 190 | uint8_t cmd[] = {'i'};
|
---|
| 191 | if (mode != 255)
|
---|
| 192 | return 0;
|
---|
| 193 |
|
---|
| 194 | bridge.transfer(cmd, 1, (uint8_t *)filename.c_str(), filename.length(), res, 1);
|
---|
| 195 | return res[0];
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | File File::openNextFile(uint8_t mode) {
|
---|
| 200 | Process awk;
|
---|
| 201 | char tmp;
|
---|
| 202 | String command;
|
---|
| 203 | String filepath;
|
---|
| 204 | if (dirPosition == 0xFFFF) return File();
|
---|
| 205 |
|
---|
| 206 | command = "ls ";
|
---|
| 207 | command += filename;
|
---|
| 208 | command += " | awk 'NR==";
|
---|
| 209 | command += dirPosition;
|
---|
| 210 | command += "'";
|
---|
| 211 |
|
---|
| 212 | awk.runShellCommand(command);
|
---|
| 213 |
|
---|
| 214 | while (awk.running());
|
---|
| 215 |
|
---|
| 216 | command = "";
|
---|
| 217 |
|
---|
| 218 | while (awk.available()) {
|
---|
| 219 | tmp = awk.read();
|
---|
| 220 | if (tmp != '\n') command += tmp;
|
---|
| 221 | }
|
---|
| 222 | if (command.length() == 0)
|
---|
| 223 | return File();
|
---|
| 224 | dirPosition++;
|
---|
| 225 | filepath = filename + "/" + command;
|
---|
| 226 | return File(filepath.c_str(), mode);
|
---|
| 227 |
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | void File::rewindDirectory(void) {
|
---|
| 231 | dirPosition = 1;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 |
|
---|
| 235 |
|
---|
| 236 |
|
---|
| 237 |
|
---|
| 238 |
|
---|
| 239 | boolean FileSystemClass::begin() {
|
---|
| 240 | return true;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | File FileSystemClass::open(const char *filename, uint8_t mode) {
|
---|
| 244 | return File(filename, mode);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | boolean FileSystemClass::exists(const char *filepath) {
|
---|
| 248 | Process ls;
|
---|
| 249 | ls.begin("ls");
|
---|
| 250 | ls.addParameter(filepath);
|
---|
| 251 | int res = ls.run();
|
---|
| 252 | return (res == 0);
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | boolean FileSystemClass::mkdir(const char *filepath) {
|
---|
| 256 | Process mk;
|
---|
| 257 | mk.begin("mkdir");
|
---|
| 258 | mk.addParameter("-p");
|
---|
| 259 | mk.addParameter(filepath);
|
---|
| 260 | int res = mk.run();
|
---|
| 261 | return (res == 0);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | boolean FileSystemClass::remove(const char *filepath) {
|
---|
| 265 | Process rm;
|
---|
| 266 | rm.begin("rm");
|
---|
| 267 | rm.addParameter(filepath);
|
---|
| 268 | int res = rm.run();
|
---|
| 269 | return (res == 0);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | boolean FileSystemClass::rmdir(const char *filepath) {
|
---|
| 273 | Process rm;
|
---|
| 274 | rm.begin("rmdir");
|
---|
| 275 | rm.addParameter(filepath);
|
---|
| 276 | int res = rm.run();
|
---|
| 277 | return (res == 0);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | FileSystemClass FileSystem;
|
---|