base read function. The src offset is within the bytes allocated for the storage type of this StorageAccess object */
| 137 | for the storage type of this StorageAccess object |
| 138 | */ |
| 139 | bool StorageAccess::read_block(void *data, uint16_t addr, size_t n) const |
| 140 | { |
| 141 | uint8_t *b = (uint8_t *)data; |
| 142 | |
| 143 | #if AP_SDCARD_STORAGE_ENABLED |
| 144 | if (file != nullptr) { |
| 145 | // using microSD data |
| 146 | if (addr > file->bufsize) { |
| 147 | return false; |
| 148 | } |
| 149 | const size_t n2 = MIN(n, file->bufsize - addr); |
| 150 | memcpy(b, &file->buffer[addr], n2); |
| 151 | return n == n2; |
| 152 | } |
| 153 | #endif |
| 154 | |
| 155 | for (uint8_t i=0; i<STORAGE_NUM_AREAS; i++) { |
| 156 | const StorageManager::StorageArea &area = StorageManager::layout[i]; |
| 157 | uint16_t length = area.length; |
| 158 | uint16_t offset = area.offset; |
| 159 | if (area.type != type) { |
| 160 | continue; |
| 161 | } |
| 162 | if (addr >= length) { |
| 163 | // the data isn't in this area |
| 164 | addr -= length; |
| 165 | continue; |
| 166 | } |
| 167 | uint16_t count = n; |
| 168 | if (count+addr > length) { |
| 169 | // the data crosses a boundary between two areas |
| 170 | count = length - addr; |
| 171 | } |
| 172 | hal.storage->read_block(b, addr+offset, count); |
| 173 | n -= count; |
| 174 | |
| 175 | if (n == 0) { |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | // move pointer after written bytes |
| 180 | b += count; |
| 181 | // continue writing at the beginning of next valid area |
| 182 | addr = 0; |
| 183 | } |
| 184 | |
| 185 | return (n == 0); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /* |