1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.
//! TicKV can be used asynchronously. This module provides documentation and
//! tests for using it with an async `FlashController` interface.
//!
//! To do this first there are special error values to return from the
//! `FlashController` functions. These are the `ReadNotReady`, `WriteNotReady`
//! and `EraseNotReady` types.
//!
//! ```rust
//! // EXAMPLE ONLY: The `DefaultHasher` is subject to change
//! // and hence is not a good fit.
//! use std::collections::hash_map::DefaultHasher;
//! use core::hash::{Hash, Hasher};
//! use std::cell::{Cell, RefCell};
//! use tickv::{AsyncTicKV, MAIN_KEY};
//! use tickv::error_codes::ErrorCode;
//! use tickv::flash_controller::FlashController;
//!
//! fn get_hashed_key(unhashed_key: &[u8]) -> u64 {
//! let mut hash_function = DefaultHasher::new();
//! unhashed_key.hash(&mut hash_function);
//! hash_function.finish()
//! }
//!
//! struct FlashCtrl {
//! buf: RefCell<[[u8; 1024]; 64]>,
//! async_read_region: Cell<usize>,
//! async_erase_region: Cell<usize>,
//! }
//!
//! impl FlashCtrl {
//! fn new() -> Self {
//! Self {
//! buf: RefCell::new([[0xFF; 1024]; 64]),
//! async_read_region: Cell::new(10),
//! async_erase_region: Cell::new(10),
//! }
//! }
//! }
//!
//! impl FlashController<1024> for FlashCtrl {
//! fn read_region(
//! &self,
//! region_number: usize,
//! buf: &mut [u8; 1024],
//! ) -> Result<(), ErrorCode> {
//! // We aren't ready yet, launch the async operation
//! self.async_read_region.set(region_number);
//! return Err(ErrorCode::ReadNotReady(region_number));
//!
//! Ok(())
//! }
//!
//! fn write(&self, address: usize, buf: &[u8]) -> Result<(), ErrorCode> {
//! // Save the write operation to a queue, we don't need to re-call
//! for (i, d) in buf.iter().enumerate() {
//! self.buf.borrow_mut()[address / 1024][(address % 1024) + i] = *d;
//! }
//! Ok(())
//! }
//!
//! fn erase_region(&self, region_number: usize) -> Result<(), ErrorCode> {
//! if self.async_erase_region.get() != region_number {
//! // We aren't ready yet, launch the async operation
//! self.async_erase_region.set(region_number);
//! return Err(ErrorCode::EraseNotReady(region_number));
//! }
//!
//! Ok(())
//! }
//! }
//!
//! // Create the TicKV instance and loop until everything is done
//! // NOTE in an real implementation you will want to wait on
//! // callbacks/interrupts and make this async.
//!
//! let mut read_buf: [u8; 1024] = [0; 1024];
//! let mut hash_function = DefaultHasher::new();
//! MAIN_KEY.hash(&mut hash_function);
//! let tickv = AsyncTicKV::<FlashCtrl, 1024>::new(FlashCtrl::new(),
//! &mut read_buf, 0x1000);
//!
//! let mut ret = tickv.initialise(hash_function.finish());
//! while ret.is_err() {
//! // There is no actual delay here, in a real implementation wait on some event
//! ret = tickv.continue_operation().0;
//!
//! match ret {
//! Err(ErrorCode::ReadNotReady(reg)) => {
//! tickv.set_read_buffer(&tickv.tickv.controller.buf.borrow()[reg]);
//! }
//! Ok(_) => break,
//! Err(ErrorCode::WriteNotReady(reg)) => break,
//! Err(ErrorCode::EraseNotReady(reg)) => {}
//! _ => unreachable!(),
//! }
//! }
//!
//! // Then when calling the TicKV function check for the error. For example
//! // when appending a key:
//!
//! // Add a key
//! static mut VALUE: [u8; 32] = [0x23; 32];
//! let ret = unsafe { tickv.append_key(get_hashed_key(b"ONE"), &mut VALUE, 32) };
//!
//! match ret {
//! Err((_buf, ErrorCode::ReadNotReady(reg))) => {
//! // There is no actual delay in the test, just continue now
//! tickv.set_read_buffer(&tickv.tickv.controller.buf.borrow()[reg]);
//! tickv
//! .continue_operation().0
//! .unwrap();
//! }
//! Ok(_) => {}
//! _ => unreachable!(),
//! }
//!
//! ```
//!
//! This will call into the `FlashController` again where the
//! `FlashController` implementation must return the data that is requested.
//! If the data isn't ready (multiple reads might occur) then the `NotReady`
//! error types can still be used.
//!
use crate::error_codes::ErrorCode;
use crate::flash_controller::FlashController;
use crate::success_codes::SuccessCode;
use crate::tickv::{State, TicKV};
use core::cell::Cell;
/// The return type from the continue operation
type ContinueReturn = (
// Result
Result<SuccessCode, ErrorCode>,
// Buf Buffer
Option<&'static mut [u8]>,
// Length of valid data inside of the buffer.
usize,
);
/// The struct storing all of the TicKV information for the async implementation.
pub struct AsyncTicKV<'a, C: FlashController<S>, const S: usize> {
/// The main TicKV struct
pub tickv: TicKV<'a, C, S>,
key: Cell<Option<u64>>,
value: Cell<Option<&'static mut [u8]>>,
value_length: Cell<usize>,
}
impl<'a, C: FlashController<S>, const S: usize> AsyncTicKV<'a, C, S> {
/// Create a new struct
///
/// `C`: An implementation of the `FlashController` trait
///
/// `controller`: An new struct implementing `FlashController`
/// `flash_size`: The total size of the flash used for TicKV
pub fn new(controller: C, read_buffer: &'a mut [u8; S], flash_size: usize) -> Self {
Self {
tickv: TicKV::<C, S>::new(controller, read_buffer, flash_size),
key: Cell::new(None),
value: Cell::new(None),
value_length: Cell::new(0),
}
}
/// This function setups the flash region to be used as a key-value store.
/// If the region is already initialised this won't make any changes.
///
/// `hashed_main_key`: The u64 hash of the const string `MAIN_KEY`.
///
/// If the specified region has not already been setup for TicKV
/// the entire region will be erased.
///
/// On success a `SuccessCode` will be returned.
/// On error a `ErrorCode` will be returned.
pub fn initialise(&self, hashed_main_key: u64) -> Result<SuccessCode, ErrorCode> {
self.key.replace(Some(hashed_main_key));
self.tickv.initialise(hashed_main_key)
}
/// Appends the key/value pair to flash storage.
///
/// `hash`: A hashed key. This key will be used in future to retrieve
/// or remove the `value`.
/// `value`: A buffer containing the data to be stored to flash.
///
/// On success nothing will be returned.
/// On error a `ErrorCode` will be returned.
pub fn append_key(
&self,
hash: u64,
value: &'static mut [u8],
length: usize,
) -> Result<SuccessCode, (&'static mut [u8], ErrorCode)> {
match self.tickv.append_key(hash, &value[0..length]) {
Ok(_code) => {
// Ok is a problem, since that means no asynchronous operations
// were called, which means our client will never get a
// callback. We need to error.
Err((value, ErrorCode::WriteFail))
}
Err(e) => match e {
ErrorCode::ReadNotReady(_)
| ErrorCode::EraseNotReady(_)
| ErrorCode::WriteNotReady(_) => {
// This is what we expect, since it means we are going
// an asynchronous operation which this interface expects.
self.key.replace(Some(hash));
self.value.replace(Some(value));
self.value_length.set(length);
Ok(SuccessCode::Queued)
}
_ => {
// On any other error we report the error.
Err((value, e))
}
},
}
}
/// Retrieves the value from flash storage.
///
/// `hash`: A hashed key.
/// `buf`: A buffer to store the value to.
///
/// On success a `SuccessCode` will be returned.
/// On error a `ErrorCode` will be returned.
///
/// If a power loss occurs before success is returned the data is
/// assumed to be lost.
pub fn get_key(
&self,
hash: u64,
buf: &'static mut [u8],
) -> Result<SuccessCode, (&'static mut [u8], ErrorCode)> {
match self.tickv.get_key(hash, buf) {
Ok(_code) => {
// Ok is a problem, since that means no asynchronous operations
// were called, which means our client will never get a
// callback. We need to error.
Err((buf, ErrorCode::ReadFail))
}
Err(e) => match e {
ErrorCode::ReadNotReady(_)
| ErrorCode::EraseNotReady(_)
| ErrorCode::WriteNotReady(_) => {
self.key.replace(Some(hash));
self.value.replace(Some(buf));
Ok(SuccessCode::Queued)
}
_ => Err((buf, e)),
},
}
}
/// Invalidates the key in flash storage
///
/// `hash`: A hashed key.
/// `key`: A unhashed key. This will be hashed internally.
///
/// On success a `SuccessCode` will be returned.
/// On error a `ErrorCode` will be returned.
///
/// If a power loss occurs before success is returned the data is
/// assumed to be lost.
pub fn invalidate_key(&self, hash: u64) -> Result<SuccessCode, ErrorCode> {
match self.tickv.invalidate_key(hash) {
Ok(_code) => Err(ErrorCode::WriteFail),
Err(_e) => {
self.key.replace(Some(hash));
Ok(SuccessCode::Queued)
}
}
}
/// Zeroizes the key in flash storage
///
/// `hash`: A hashed key.
/// `key`: A unhashed key. This will be hashed internally.
///
/// On success a `SuccessCode` will be returned.
/// On error a `ErrorCode` will be returned.
///
/// If a power loss occurs before success is returned the data is
/// assumed to be lost.
pub fn zeroise_key(&self, hash: u64) -> Result<SuccessCode, ErrorCode> {
match self.tickv.zeroise_key(hash) {
Ok(_code) => Err(ErrorCode::WriteFail),
Err(_e) => {
self.key.replace(Some(hash));
Ok(SuccessCode::Queued)
}
}
}
/// Perform a garbage collection on TicKV
///
/// On success a `SuccessCode` will be returned.
/// On error a `ErrorCode` will be returned.
pub fn garbage_collect(&self) -> Result<SuccessCode, ErrorCode> {
match self.tickv.garbage_collect() {
Ok(_code) => Err(ErrorCode::EraseFail),
Err(_e) => Ok(SuccessCode::Queued),
}
}
/// Copy data from `read_buffer` argument to the internal read_buffer.
/// This should be used to copy the data that the implementation wanted
/// to read when calling `read_region` after the async operation has
/// completed.
pub fn set_read_buffer(&self, read_buffer: &[u8]) {
let buf = self.tickv.read_buffer.take().unwrap();
buf.copy_from_slice(read_buffer);
self.tickv.read_buffer.replace(Some(buf));
}
/// Continue the last operation after the async operation has completed.
/// This should be called from a read/erase complete callback.
/// NOTE: If called from a read callback, `set_read_buffer` should be
/// called first to update the data.
///
/// `hash_function`: Hash function with no previous state. This is
/// usually a newly created hash.
///
/// Returns a tuple of 3 values
/// Result:
/// On success a `SuccessCode` will be returned.
/// On error a `ErrorCode` will be returned.
/// Buf Buffer:
/// An option of the buf buffer used
/// Length usize:
/// The number of valid bytes in the buffer. 0 if Buf is None.
/// The buffers will only be returned on a non async error or on success.
pub fn continue_operation(&self) -> ContinueReturn {
let (ret, length) = match self.tickv.state.get() {
State::Init(_) => (self.tickv.initialise(self.key.get().unwrap()), 0),
State::AppendKey(_) => {
let value = self.value.take().unwrap();
let value_length = self.value_length.get();
let ret = self
.tickv
.append_key(self.key.get().unwrap(), &value[0..value_length]);
self.value.replace(Some(value));
(ret, value_length)
}
State::GetKey(_) => {
let buf = self.value.take().unwrap();
let ret = self.tickv.get_key(self.key.get().unwrap(), buf);
self.value.replace(Some(buf));
match ret {
Ok((s, len)) => (Ok(s), len),
Err(e) => (Err(e), 0),
}
}
State::InvalidateKey(_) => (self.tickv.invalidate_key(self.key.get().unwrap()), 0),
State::ZeroiseKey(_) => (self.tickv.zeroise_key(self.key.get().unwrap()), 0),
State::GarbageCollect(_) => match self.tickv.garbage_collect() {
Ok(bytes_freed) => (Ok(SuccessCode::Complete), bytes_freed),
Err(e) => (Err(e), 0),
},
_ => unreachable!(),
};
match ret {
Ok(_) => {
self.tickv.state.set(State::None);
(ret, self.value.take(), length)
}
Err(e) => match e {
ErrorCode::ReadNotReady(_) | ErrorCode::EraseNotReady(_) => (ret, None, 0),
ErrorCode::WriteNotReady(_) => {
self.tickv.state.set(State::None);
(ret, None, 0)
}
_ => {
self.tickv.state.set(State::None);
(ret, self.value.take(), length)
}
},
}
}
}
#[cfg(test)]
mod tests {
#![allow(unsafe_code)]
/// Tests using a flash controller that can store data
mod store_flast_ctrl {
use crate::async_ops::AsyncTicKV;
use crate::error_codes::ErrorCode;
use crate::flash_controller::FlashController;
use crate::success_codes::SuccessCode;
use crate::tickv::{HASH_OFFSET, LEN_OFFSET, MAIN_KEY, VERSION, VERSION_OFFSET};
use core::hash::{Hash, Hasher};
use core::ptr::addr_of_mut;
use std::cell::Cell;
use std::cell::RefCell;
use std::collections::hash_map::DefaultHasher;
fn check_region_main(buf: &[u8]) {
// Check the version
assert_eq!(buf[VERSION_OFFSET], VERSION);
// Check the length
assert_eq!(buf[LEN_OFFSET], 0x80);
assert_eq!(buf[LEN_OFFSET + 1], 15);
// Check the hash
assert_eq!(buf[HASH_OFFSET + 0], 0x7b);
assert_eq!(buf[HASH_OFFSET + 1], 0xc9);
assert_eq!(buf[HASH_OFFSET + 2], 0xf7);
assert_eq!(buf[HASH_OFFSET + 3], 0xff);
assert_eq!(buf[HASH_OFFSET + 4], 0x4f);
assert_eq!(buf[HASH_OFFSET + 5], 0x76);
assert_eq!(buf[HASH_OFFSET + 6], 0xf2);
assert_eq!(buf[HASH_OFFSET + 7], 0x44);
// Check the check hash
assert_eq!(buf[HASH_OFFSET + 8], 0xbb);
assert_eq!(buf[HASH_OFFSET + 9], 0x32);
assert_eq!(buf[HASH_OFFSET + 10], 0x74);
assert_eq!(buf[HASH_OFFSET + 11], 0x1d);
}
fn check_region_one(buf: &[u8]) {
// Check the version
assert_eq!(buf[VERSION_OFFSET], VERSION);
// Check the length
assert_eq!(buf[LEN_OFFSET], 0x80);
assert_eq!(buf[LEN_OFFSET + 1], 47);
// Check the hash
assert_eq!(buf[HASH_OFFSET + 0], 0x81);
assert_eq!(buf[HASH_OFFSET + 1], 0x13);
assert_eq!(buf[HASH_OFFSET + 2], 0x7e);
assert_eq!(buf[HASH_OFFSET + 3], 0x95);
assert_eq!(buf[HASH_OFFSET + 4], 0x9e);
assert_eq!(buf[HASH_OFFSET + 5], 0x93);
assert_eq!(buf[HASH_OFFSET + 6], 0xaa);
assert_eq!(buf[HASH_OFFSET + 7], 0x3d);
// Check the value
assert_eq!(buf[HASH_OFFSET + 8], 0x23);
assert_eq!(buf[28], 0x23);
assert_eq!(buf[42], 0x23);
// Check the check hash
assert_eq!(buf[43], 0xfd);
assert_eq!(buf[44], 0x24);
assert_eq!(buf[45], 0xf0);
assert_eq!(buf[46], 0x07);
}
fn check_region_two(buf: &[u8]) {
// Check the version
assert_eq!(buf[VERSION_OFFSET], VERSION);
// Check the length
assert_eq!(buf[LEN_OFFSET], 0x80);
assert_eq!(buf[LEN_OFFSET + 1], 47);
// Check the hash
assert_eq!(buf[HASH_OFFSET + 0], 0x9d);
assert_eq!(buf[HASH_OFFSET + 1], 0xd3);
assert_eq!(buf[HASH_OFFSET + 2], 0x71);
assert_eq!(buf[HASH_OFFSET + 3], 0x45);
assert_eq!(buf[HASH_OFFSET + 4], 0x05);
assert_eq!(buf[HASH_OFFSET + 5], 0xc2);
assert_eq!(buf[HASH_OFFSET + 6], 0xf8);
assert_eq!(buf[HASH_OFFSET + 7], 0x66);
// Check the value
assert_eq!(buf[HASH_OFFSET + 8], 0x23);
assert_eq!(buf[28], 0x23);
assert_eq!(buf[42], 0x23);
// Check the check hash
assert_eq!(buf[43], 0x1b);
assert_eq!(buf[44], 0x53);
assert_eq!(buf[45], 0xf9);
assert_eq!(buf[46], 0x54);
}
fn get_hashed_key(unhashed_key: &[u8]) -> u64 {
let mut hash_function = DefaultHasher::new();
unhashed_key.hash(&mut hash_function);
hash_function.finish()
}
#[derive(Clone, Copy)]
enum FlashCtrlAction {
Idle,
Read,
Write,
Erase,
}
// An example FlashCtrl implementation
struct FlashCtrl<const S: usize> {
buf: RefCell<[[u8; S]; 64]>,
run: Cell<u8>,
async_read_region: Cell<usize>,
async_erase_region: Cell<usize>,
waiting_on: Cell<FlashCtrlAction>,
check_write_contents: bool,
}
impl<const S: usize> FlashCtrl<S> {
fn new(check_write_contents: bool) -> Self {
Self {
buf: RefCell::new([[0xFF; S]; 64]),
run: Cell::new(0),
async_read_region: Cell::new(100),
async_erase_region: Cell::new(100),
waiting_on: Cell::new(FlashCtrlAction::Idle),
check_write_contents,
}
}
fn get_waiting_action(&self) -> FlashCtrlAction {
self.waiting_on.get()
}
}
impl<const S: usize> FlashController<S> for FlashCtrl<S> {
fn read_region(
&self,
region_number: usize,
_buf: &mut [u8; S],
) -> Result<(), ErrorCode> {
println!("Read from region: {}", region_number);
// Pretend that we aren't ready
self.async_read_region.set(region_number);
println!(" Not ready");
self.waiting_on.set(FlashCtrlAction::Read);
Err(ErrorCode::ReadNotReady(region_number))
}
fn write(&self, address: usize, buf: &[u8]) -> Result<(), ErrorCode> {
println!(
"Write to address: {:#x}, region: {}",
address % S,
address / S
);
for (i, d) in buf.iter().enumerate() {
self.buf.borrow_mut()[address / S][(address % S) + i] = *d;
}
// Check to see if we are adding a key
if buf.len() > 1 && self.check_write_contents {
if self.run.get() == 0 {
println!("Writing main key: {:#x?}", buf);
check_region_main(buf);
} else if self.run.get() == 1 {
println!("Writing key ONE: {:#x?}", buf);
check_region_one(buf);
} else if self.run.get() == 2 {
println!("Writing key TWO: {:#x?}", buf);
check_region_two(buf);
}
}
self.run.set(self.run.get() + 1);
self.waiting_on.set(FlashCtrlAction::Write);
Err(ErrorCode::WriteNotReady(address))
}
fn erase_region(&self, region_number: usize) -> Result<(), ErrorCode> {
println!("Erase region: {}", region_number);
let mut local_buf = self.buf.borrow_mut()[region_number];
for d in local_buf.iter_mut() {
*d = 0xFF;
}
// Pretend that we aren't ready
self.async_erase_region.set(region_number);
self.waiting_on.set(FlashCtrlAction::Erase);
Err(ErrorCode::EraseNotReady(region_number))
}
}
/// This function implements what would happen in the callback function
/// triggered by the underlying flash hardware. In effect, it is the
/// callback handler, but since we don't actually have an async flash
/// implementation, this is just called by each test after starting the
/// flash operation.
fn flash_ctrl_callback<const S: usize>(tickv: &AsyncTicKV<FlashCtrl<S>, S>) {
match tickv.tickv.controller.get_waiting_action() {
FlashCtrlAction::Read => {
// This mimics a read is complete, and we provide the buffer
// with the newly read data to the tickv layer.
tickv.set_read_buffer(
&tickv.tickv.controller.buf.borrow()
[tickv.tickv.controller.async_read_region.get()],
);
}
_ => {
// For write an erase all of the operation already occurred
// in the original operation, and nothing needs to be done
// in the simulated callback.
}
}
}
#[test]
fn test_simple_append() {
let mut read_buf: [u8; 1024] = [0; 1024];
let mut hash_function = DefaultHasher::new();
MAIN_KEY.hash(&mut hash_function);
let tickv = AsyncTicKV::<FlashCtrl<1024>, 1024>::new(
FlashCtrl::new(true),
&mut read_buf,
0x1000,
);
let mut ret = tickv.initialise(hash_function.finish());
while ret.is_err() {
flash_ctrl_callback(&tickv);
// There is no actual delay in the test, just continue now
let (r, _buf, _len) = tickv.continue_operation();
ret = r;
}
static mut VALUE: [u8; 32] = [0x23; 32];
println!("HASHED KEY {:?}", get_hashed_key(b"ONE"));
let ret =
unsafe { tickv.append_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
let ret =
unsafe { tickv.append_key(get_hashed_key(b"TWO"), &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
}
#[test]
fn test_double_append() {
let mut read_buf: [u8; 1024] = [0; 1024];
let mut hash_function = DefaultHasher::new();
MAIN_KEY.hash(&mut hash_function);
let tickv = AsyncTicKV::<FlashCtrl<1024>, 1024>::new(
FlashCtrl::new(true),
&mut read_buf,
0x10000,
);
let mut ret = tickv.initialise(hash_function.finish());
while ret.is_err() {
flash_ctrl_callback(&tickv);
// There is no actual delay in the test, just continue now
let (r, _buf, _len) = tickv.continue_operation();
ret = r;
}
static mut VALUE: [u8; 32] = [0x23; 32];
static mut BUF: [u8; 32] = [0; 32];
println!("Add key ONE");
let ret =
unsafe { tickv.append_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
println!("Get key ONE");
let ret = unsafe { tickv.get_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(BUF)) };
match ret {
Ok(SuccessCode::Queued) => {
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
println!("Get non-existent key TWO");
let ret = unsafe { tickv.get_key(get_hashed_key(b"TWO"), &mut *addr_of_mut!(BUF)) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
assert_eq!(tickv.continue_operation().0, Err(ErrorCode::KeyNotFound));
}
Err((_, ErrorCode::KeyNotFound)) => {}
_ => unreachable!(),
}
println!("Add key ONE again");
let ret =
unsafe { tickv.append_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
assert_eq!(
tickv.continue_operation().0,
Err(ErrorCode::KeyAlreadyExists)
);
}
Err((_buf, ErrorCode::KeyAlreadyExists)) => {}
_ => unreachable!(),
}
println!("Add key TWO");
let ret =
unsafe { tickv.append_key(get_hashed_key(b"TWO"), &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
println!("Get key ONE");
let ret = unsafe { tickv.get_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(BUF)) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
println!("Get key TWO");
let ret = unsafe { tickv.get_key(get_hashed_key(b"TWO"), &mut *addr_of_mut!(BUF)) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
println!("Get non-existent key THREE");
let ret = unsafe { tickv.get_key(get_hashed_key(b"THREE"), &mut *addr_of_mut!(BUF)) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
assert_eq!(tickv.continue_operation().0, Err(ErrorCode::KeyNotFound));
}
_ => unreachable!(),
}
let ret = unsafe { tickv.get_key(get_hashed_key(b"THREE"), &mut *addr_of_mut!(BUF)) };
match ret {
Ok(SuccessCode::Queued) => {
flash_ctrl_callback(&tickv);
assert_eq!(tickv.continue_operation().0, Err(ErrorCode::KeyNotFound));
}
Err(_) => {}
_ => unreachable!(),
}
}
#[test]
fn test_spread_pages() {
let mut read_buf: [u8; 64] = [0; 64];
let mut hash_function = DefaultHasher::new();
MAIN_KEY.hash(&mut hash_function);
let tickv =
AsyncTicKV::<FlashCtrl<64>, 64>::new(FlashCtrl::new(false), &mut read_buf, 64 * 64);
let mut ret = tickv.initialise(hash_function.finish());
while ret.is_err() {
tickv.set_read_buffer(
&tickv.tickv.controller.buf.borrow()
[tickv.tickv.controller.async_read_region.get()],
);
// There is no actual delay in the test, just continue now
let (r, _buf, _len) = tickv.continue_operation();
ret = r;
}
static mut VALUE: [u8; 32] = [0x23; 32];
static mut BUF: [u8; 32] = [0; 32];
println!("Add key 0x1000");
let ret = unsafe { tickv.append_key(0x1000, &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(e) => panic!("Unable to add key 0x100: {e:?}"),
_ => unreachable!(),
}
println!("Add key 0x2000");
let ret = unsafe { tickv.append_key(0x2000, &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
assert_eq!(
tickv.continue_operation().0,
Err(ErrorCode::ReadNotReady(1))
);
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(e) => panic!("Unable to add key 0x200: {e:?}"),
_ => unreachable!(),
}
println!("Add key 0x3000");
let ret = unsafe { tickv.append_key(0x3000, &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
}
Err(e) => panic!("Unable to add key 0x3000: {e:?}"),
_ => unreachable!(),
}
loop {
let ret = tickv.continue_operation().0;
match ret {
Err(ErrorCode::ReadNotReady(_reg)) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
}
Err(e) => panic!("Unable to add key 0x3000: {e:?}"),
Ok(_) => break,
}
}
println!("Get key 0x1000");
let ret = unsafe { tickv.get_key(0x1000, &mut *addr_of_mut!(BUF)) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
}
_ => unreachable!(),
}
loop {
let ret = tickv.continue_operation().0;
match ret {
Err(ErrorCode::ReadNotReady(_reg)) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
}
Err(e) => panic!("Unable to get key 0x1000: {e:?}"),
Ok(_) => break,
}
}
println!("Get key 0x3000");
let ret = unsafe { tickv.get_key(0x3000, &mut *addr_of_mut!(BUF)) };
match ret {
Ok(_) => flash_ctrl_callback(&tickv),
Err(_) => unreachable!(),
}
loop {
let ret = tickv.continue_operation().0;
match ret {
Err(ErrorCode::ReadNotReady(reg)) => {
// There is no actual delay in the test, just continue now
tickv.set_read_buffer(&tickv.tickv.controller.buf.borrow()[reg]);
}
Err(e) => panic!("Unable to get key 0x1000: {e:?}"),
Ok(_) => break,
}
}
}
#[test]
fn test_append_and_delete() {
let mut read_buf: [u8; 1024] = [0; 1024];
let mut hash_function = DefaultHasher::new();
MAIN_KEY.hash(&mut hash_function);
let tickv = AsyncTicKV::<FlashCtrl<1024>, 1024>::new(
FlashCtrl::new(true),
&mut read_buf,
0x10000,
);
let mut ret = tickv.initialise(hash_function.finish());
while ret.is_err() {
flash_ctrl_callback(&tickv);
// There is no actual delay in the test, just continue now
let (r, _buf, _len) = tickv.continue_operation();
ret = r;
}
static mut VALUE: [u8; 32] = [0x23; 32];
static mut BUF: [u8; 32] = [0; 32];
println!("Add key ONE");
let ret =
unsafe { tickv.append_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
println!("Get key ONE");
let ret = unsafe { tickv.get_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(BUF)) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
println!("Delete Key ONE");
let ret = tickv.invalidate_key(get_hashed_key(b"ONE"));
match ret {
Ok(SuccessCode::Queued) => {
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
println!("Get non-existent key ONE");
unsafe {
match tickv.get_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(BUF)) {
Ok(SuccessCode::Queued) => {
flash_ctrl_callback(&tickv);
assert_eq!(
tickv.continue_operation().0,
Err(ErrorCode::ReadNotReady(62))
);
flash_ctrl_callback(&tickv);
match tickv.continue_operation().0 {
Err(ErrorCode::ReadNotReady(reg)) => {
panic!("Searching too far for keys: {reg}");
}
Err(ErrorCode::KeyNotFound) => {}
e => {
panic!("Expected ErrorCode::KeyNotFound, got {e:?}");
}
}
}
_ => unreachable!(),
}
}
println!("Try to delete Key ONE Again");
match tickv.invalidate_key(get_hashed_key(b"ONE")) {
Ok(SuccessCode::Queued) => {
let reg = tickv.tickv.controller.async_read_region.get();
flash_ctrl_callback(&tickv);
assert_eq!(
tickv.continue_operation().0,
Err(ErrorCode::ReadNotReady(reg + 1))
);
// In normal operation we will read region `reg`, determine
// that it isn't full and stop looking for the key
//
// The following test is a hack to continue testing.
// We don't fill the read buffer with new data. So
// the read buffer will continue to provide the data from
// `reg`, which means TicKV will continue searching for
// an empty region.
//
// In normal operation this isn't correct, but for the test
// case it's a good check to test region searching
assert_eq!(
tickv.continue_operation().0,
Err(ErrorCode::ReadNotReady(reg - 1))
);
assert_eq!(
tickv.continue_operation().0,
Err(ErrorCode::ReadNotReady(reg + 2))
);
assert_eq!(
tickv.continue_operation().0,
Err(ErrorCode::ReadNotReady(reg - 2))
);
assert_eq!(
tickv.continue_operation().0,
Err(ErrorCode::ReadNotReady(reg - 3))
);
// Now set the read buffer and end the search
tickv.set_read_buffer(&tickv.tickv.controller.buf.borrow()[reg - 1]);
match tickv.continue_operation().0 {
Err(ErrorCode::ReadNotReady(reg)) => {
panic!("Searching too far for keys: {reg}");
}
Err(ErrorCode::KeyNotFound) => {}
e => {
panic!("Expected ErrorCode::KeyNotFound, got {e:?}");
}
}
}
e => {
panic!("Expected ErrorCode::KeyNotFound, got {e:?}");
}
}
}
#[test]
fn test_garbage_collect() {
let mut read_buf: [u8; 1024] = [0; 1024];
let mut hash_function = DefaultHasher::new();
MAIN_KEY.hash(&mut hash_function);
let tickv = AsyncTicKV::<FlashCtrl<1024>, 1024>::new(
FlashCtrl::new(true),
&mut read_buf,
0x10000,
);
let mut ret = tickv.initialise(hash_function.finish());
while ret.is_err() {
flash_ctrl_callback(&tickv);
// There is no actual delay in the test, just continue now
let (r, _buf, _len) = tickv.continue_operation();
ret = r;
}
static mut VALUE: [u8; 32] = [0x23; 32];
static mut BUF: [u8; 32] = [0; 32];
println!("Garbage collect empty flash");
let ret = tickv.garbage_collect();
match ret {
Ok(SuccessCode::Queued) => loop {
flash_ctrl_callback(&tickv);
let (res, _buf, len) = tickv.continue_operation();
if res.is_ok() {
assert_eq!(len, 0);
break;
}
},
Ok(_) => {}
_ => unreachable!(),
}
println!("Add key ONE");
let ret =
unsafe { tickv.append_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Ok(_) => {}
_ => unreachable!(),
}
println!("Garbage collect flash with valid key");
let ret = tickv.garbage_collect();
match ret {
Ok(SuccessCode::Queued) => loop {
flash_ctrl_callback(&tickv);
let (res, _buf, len) = tickv.continue_operation();
if res.is_ok() {
assert_eq!(len, 0);
break;
}
},
Ok(_) => {}
_ => unreachable!(),
}
println!("Delete Key ONE");
let ret = tickv.invalidate_key(get_hashed_key(b"ONE"));
match ret {
Ok(SuccessCode::Queued) => {
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
Err(_) => {}
_ => unreachable!(),
}
println!("Garbage collect flash with deleted key");
let ret = tickv.garbage_collect();
match ret {
Ok(SuccessCode::Queued) => loop {
flash_ctrl_callback(&tickv);
let (res, _buf, len) = tickv.continue_operation();
if res.is_ok() {
assert_eq!(len, 1024);
break;
}
},
Ok(_) => {}
_ => unreachable!(),
}
println!("Get non-existent key ONE");
match unsafe { tickv.get_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(BUF)) } {
Ok(SuccessCode::Queued) => {
flash_ctrl_callback(&tickv);
assert_eq!(
tickv.continue_operation().0,
Err(ErrorCode::ReadNotReady(62))
);
flash_ctrl_callback(&tickv);
assert_eq!(tickv.continue_operation().0, Err(ErrorCode::KeyNotFound));
}
_ => unreachable!(),
}
println!("Add Key ONE");
let ret =
unsafe { tickv.append_key(get_hashed_key(b"ONE"), &mut *addr_of_mut!(VALUE), 32) };
match ret {
Ok(SuccessCode::Queued) => {
// There is no actual delay in the test, just continue now
flash_ctrl_callback(&tickv);
tickv.continue_operation().0.unwrap();
}
_ => unreachable!("ret: {:?}", ret),
}
}
}
}