File: fds.c

package info (click to toggle)
firmware-microbit-micropython 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 25,448 kB
  • sloc: ansic: 83,496; cpp: 27,664; python: 2,475; asm: 274; makefile: 245; javascript: 41; sh: 25
file content (2090 lines) | stat: -rw-r--r-- 57,650 bytes parent folder | download | duplicates (3)
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
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
/*
 * Copyright (c) Nordic Semiconductor ASA
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
 *   contributors to this software may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "fds.h"
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "fds_config.h"
#include "fds_types_internal.h"
#include "fstorage.h"
#include "nrf_error.h"
#include "app_util.h"


/** Our fstorage configuration.
 *  The other fields will be assigned automatically during compilation. */
FS_SECTION_VARS_ADD(fs_config_t fs_config) = { .cb = fs_callback, .num_pages = FDS_MAX_PAGES };

static uint32_t const fds_page_tag_swap[]   = {FDS_PAGE_TAG_WORD_0_SWAP, FDS_PAGE_TAG_WORD_1,
                                               FDS_PAGE_TAG_WORD_2,      FDS_PAGE_TAG_WORD_3};

static uint32_t const fds_page_tag_valid[]  = {FDS_PAGE_TAG_WORD_0_VALID, FDS_PAGE_TAG_WORD_1,
                                               FDS_PAGE_TAG_WORD_2,       FDS_PAGE_TAG_WORD_3};

static uint32_t const fds_page_tag_gc       = FDS_PAGE_TAG_WORD_3_GC;

static fds_tl_t const m_fds_tl_invalid      = { .type = FDS_TYPE_ID_INVALID,
                                                .length_words = 0xFFFF };

/**@brief Internal status flags. */
static uint8_t           volatile m_flags;       

static uint8_t                    m_users;
static fds_cb_t                   m_cb_table[FDS_MAX_USERS];

/**@brief The last record ID. Setup page by page_scan() during pages_init(). */
static fds_record_id_t            m_last_rec_id;

/**@brief The internal queues. */
static fds_cmd_queue_t            m_cmd_queue;   
static fds_chunk_queue_t          m_chunk_queue;

/**@brief Holds the state of pages. Setup by fds_init(). */
static fds_page_t                 m_pages[FDS_MAX_PAGES];
static bool                       m_swap_page_avail = false;

static fds_gc_data_t              m_gc;
static uint16_t                   m_gc_runs;

static uint8_t          volatile  m_counter;


static void app_notify(ret_code_t       result,
                       fds_cmd_id_t     cmd,
                       fds_record_id_t  record_id,
                       fds_record_key_t record_key)
{
    for (uint8_t user = 0; user < FDS_MAX_USERS; user++)
    {
        if (m_cb_table[user] != NULL)
        {
            m_cb_table[user](result, cmd, record_id, record_key);
        }
    }
}


static void atomic_counter_inc()
{
    CRITICAL_SECTION_ENTER();
    m_counter++;
    CRITICAL_SECTION_EXIT();
}


static void atomic_counter_dec()
{
    CRITICAL_SECTION_ENTER();
    m_counter--;
    CRITICAL_SECTION_EXIT();
}


static bool atomic_counter_is_zero()
{
    bool ret;
    CRITICAL_SECTION_ENTER();
    ret = (m_counter == 0);
    CRITICAL_SECTION_EXIT();
    return ret;
}


static void flag_set(fds_flags_t flag)
{
    CRITICAL_SECTION_ENTER();
    m_flags |= flag;
    CRITICAL_SECTION_EXIT();
}


static void flag_clear(fds_flags_t flag)
{
    CRITICAL_SECTION_ENTER();
    m_flags &= ~(flag);
    CRITICAL_SECTION_EXIT();
}


static bool flag_is_set(fds_flags_t flag)
{
    bool ret;
    CRITICAL_SECTION_ENTER();
    ret = (m_flags & flag);
    CRITICAL_SECTION_EXIT();
    return ret;
}


/**@brief Function to check if a header has valid information. */
static __INLINE bool header_is_valid(fds_header_t const * const p_header)
{
    return ((p_header->tl.type     != FDS_TYPE_ID_INVALID) &&
            (p_header->ic.instance != FDS_INSTANCE_ID_INVALID));
}


static bool address_within_page_bounds(uint32_t const * const p_addr)
{
    return (p_addr >= fs_config.p_start_addr) &&
           (p_addr <= fs_config.p_end_addr) &&
           (is_word_aligned(p_addr));
}


/**@brief Internal function to identify the page type. */
static fds_page_type_t page_identify(uint16_t page_number)
{
    uint32_t const * const p_page_addr = m_pages[page_number].start_addr;

    uint32_t const word0 = *(p_page_addr);
    uint32_t const word1 = *(p_page_addr + 1);
    uint32_t const word2 = *(p_page_addr + 2);
    uint32_t const word3 = *(p_page_addr + 3);

    if (word1 != FDS_PAGE_TAG_WORD_1)
    {
        return FDS_PAGE_UNDEFINED;
    }

    if (word2 != FDS_PAGE_TAG_WORD_2)
    {
        return FDS_PAGE_UNDEFINED;
    }

    if (word3 == FDS_PAGE_TAG_WORD_3)
    {
        if (word0 == FDS_PAGE_TAG_WORD_0_SWAP)
        {
            return FDS_PAGE_SWAP;
        }

        if (word0 == FDS_PAGE_TAG_WORD_0_VALID)
        {
            return FDS_PAGE_VALID;
        }
    }
    else if (word3 == FDS_PAGE_TAG_WORD_3_GC)
    {
        if (word0 == FDS_PAGE_TAG_WORD_0_SWAP || word0 == FDS_PAGE_TAG_WORD_0_VALID)
        {
            return FDS_PAGE_GC;
        }
    }

    return FDS_PAGE_UNDEFINED;
}


static uint16_t page_by_addr(uint32_t const * const p_addr)
{
    if (p_addr == NULL)
    {
        return 0;
    }

    // Compute the BYTES offset from the beginning of the first page.
    uint32_t const byte_offset = (uint32_t)p_addr - (uint32_t)m_pages[0].start_addr;

// See nrf.h.
#if defined (NRF51)
    return byte_offset >> 10; // Divide by page size (1024).
#elif defined (NRF52)
    return byte_offset >> 12; // Divide by page size (4096).
#else
    #error "Device family must be defined. See nrf.h."
#endif
}


// NOTE: depends on m_pages.write_offset to function.
static bool page_has_space(uint16_t page, fds_length_t length_words)
{
    if (page >= FDS_MAX_PAGES)
    {
        return false;
    }

    CRITICAL_SECTION_ENTER();
    length_words +=  m_pages[page].write_offset;
    length_words +=  m_pages[page].words_reserved;
    CRITICAL_SECTION_EXIT();

    return (length_words < FS_PAGE_SIZE_WORDS);
}


/**@brief This function scans a page to determine how many words have
 *        been written to it. This information is used to set the page
 *        write offset during initialization (mount). Additionally, this
 *        function will update the last known record ID as it proceeds.
 */
static void page_scan(uint16_t page, uint16_t volatile * words_written)
{
    uint32_t const * p_addr = (m_pages[page].start_addr + FDS_PAGE_TAG_SIZE);

    *words_written = FDS_PAGE_TAG_SIZE;

    // A corrupt TL might cause problems.
    while ((p_addr < m_pages[page].start_addr + FS_PAGE_SIZE_WORDS) &&
           (*p_addr != FDS_ERASED_WORD))
    {
        fds_header_t const * const p_header = (fds_header_t*)p_addr;

        /** Note: DO NOT check for the validity of the header using
         *  header_is_valid() here. If an header has an invalid type (0x0000)
         *  or a missing instance (0xFFFF) then we WANT to skip it.
         */

         // Update the last known record id.
         if (p_header->id > m_last_rec_id)
         {
            m_last_rec_id = p_header->id;
         }

         // Jump to the next record.
         p_addr         += (FDS_HEADER_SIZE + p_header->tl.length_words);
         *words_written += (FDS_HEADER_SIZE + p_header->tl.length_words);
    }
}


static bool page_is_empty(uint16_t page)
{
    uint32_t const * const p_addr = m_pages[page].start_addr;

    for (uint16_t i = 0; i < FS_PAGE_SIZE_WORDS; i++)
    {
        if (*(p_addr + i) != FDS_ERASED_WORD)
        {
            return false;
        }
    }

    return true;
}


static ret_code_t page_id_from_virtual_id(uint16_t vpage_id, uint16_t * p_page_id)
{
    for (uint16_t i = 0; i < FDS_MAX_PAGES; i++)
    {
        if (m_pages[i].vpage_id == vpage_id)
        {
            *p_page_id = i;
            return NRF_SUCCESS;
        }
    }

    return NRF_ERROR_NOT_FOUND;
}


static ret_code_t page_from_virtual_id(uint16_t vpage_id, fds_page_t ** p_page)
{
    for (uint16_t i = 0; i < FDS_MAX_PAGES; i++)
    {
        if (m_pages[i].vpage_id == vpage_id)
        {
            *p_page = &m_pages[i];
            return NRF_SUCCESS;
        }
    }

    return NRF_ERROR_NOT_FOUND;
}


static uint32_t record_id_new()
{
    return ++m_last_rec_id;
}


/**@brief Tags a page as swap, i.e., reserved for GC. */
static ret_code_t page_tag_write_swap(uint16_t page)
{
    return fs_store(&fs_config,
                    m_pages[page].start_addr,
                    (uint32_t const *)&fds_page_tag_swap,
                    FDS_PAGE_TAG_SIZE);
}


/**@brief Tags a page as valid, i.e, ready for storage. */
static ret_code_t page_tag_write_valid(uint16_t page)
{
    return fs_store(&fs_config,
                    m_pages[page].start_addr,
                    (uint32_t const *)&fds_page_tag_valid,
                    FDS_PAGE_TAG_SIZE);
}


/**@brief Tags a valid page as being garbage collected. */
static ret_code_t page_tag_write_gc(uint16_t page)
{
    return fs_store(&fs_config,
                    m_pages[page].start_addr + 3,
                    (uint32_t const *)&fds_page_tag_gc,
                    1 /*Words*/);
}


/**@brief Given a page and a record, finds the next valid record. */
static ret_code_t scan_next_valid(uint16_t page, uint32_t const ** p_record)
{
    uint32_t const * p_next_rec = (*p_record);

    if (p_next_rec == NULL)
    {
        // This if the first invocation on this page, start from the beginning.
        p_next_rec = m_pages[page].start_addr + FDS_PAGE_TAG_SIZE;
    }
    else
    {
        // Jump to the next record.
        p_next_rec += (FDS_HEADER_SIZE + ((fds_header_t*)(*p_record))->tl.length_words);
    }

    // Scan until we find a valid record or until the end of the page.

    /** README: We might seek until the write_offset is reached, but it might not
     *  known at this point. */
    while ((p_next_rec < (m_pages[page].start_addr + FS_PAGE_SIZE_WORDS)) &&
           (*p_next_rec != FDS_ERASED_WORD)) // Did we jump to an erased word?
    {
        fds_header_t const * const p_header = (fds_header_t*)p_next_rec;

        if (header_is_valid(p_header))
        {
            // Bingo!
            *p_record = p_next_rec;
            return NRF_SUCCESS;
        }
        else
        {
            // The item is not valid, jump to the next.
            p_next_rec += (FDS_HEADER_SIZE + (p_header->tl.length_words));
        }
    }

    return NRF_ERROR_NOT_FOUND;
}


static ret_code_t seek_record(fds_record_desc_t * const p_desc)
{
    uint32_t const * p_record;
    uint16_t         page;
    bool             seek_all_pages = false;

    if ((p_desc->ptr_magic == FDS_MAGIC_HWORD) &&
        (p_desc->gc_magic  == m_gc_runs))
    {
        // No need to seek the file.
        return NRF_SUCCESS;
    }

    /** The pointer in the descriptor is not initialized, or GC
     *  has been run since the last time it was retrieved.
     *  We must seek the record again. */

    // Obtain the physical page ID.
    if (page_id_from_virtual_id(p_desc->vpage_id, &page) != NRF_SUCCESS)
    {
        page = 0;
        seek_all_pages = true;
    }

    do {
        // Let's find the address from where we should start seeking the record.
        p_record = m_pages[page].start_addr + FDS_PAGE_TAG_SIZE;

        /** Seek for a record with matching ID.
         *  We might get away with seeking to the page write offset, if it is known. */

        while ((p_record < (m_pages[page].start_addr + FS_PAGE_SIZE_WORDS)) &&
               (*p_record != FDS_ERASED_WORD))
        {
            fds_header_t const * const p_header = (fds_header_t*)p_record;

            if ((p_header->id != p_desc->record_id) ||
                (!header_is_valid(p_header)))
            {
                // ID doesnt't match or the record has been cleared. Jump to the next record.
                p_record += FDS_HEADER_SIZE + p_header->tl.length_words;
            }
            else
            {
                // Update the pointer in the descriptor.
                p_desc->p_rec     = p_record;
                p_desc->ptr_magic = FDS_MAGIC_HWORD;
                p_desc->gc_magic  = m_gc_runs;

                return NRF_SUCCESS;
            }
        }
    } while (seek_all_pages ? page++ < FDS_MAX_PAGES : 0);

    return NRF_ERROR_NOT_FOUND;
}


static ret_code_t find_record(fds_type_id_t     const * const p_type,
                              fds_instance_id_t const * const p_inst,
                              fds_record_desc_t       * const p_desc,
                              fds_find_token_t        * const p_token)
{
    if (!flag_is_set(FDS_FLAG_INITIALIZED))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    // Here we distinguish between the first invocation and the and the others.
    if ((p_token->magic != FDS_MAGIC_WORD) ||
        !address_within_page_bounds(p_token->p_addr)) // Is the address is really okay?
    {
        // Initialize the token.
        p_token->magic    = FDS_MAGIC_WORD;
        p_token->vpage_id = 0;
        p_token->p_addr   = NULL;
    }
    else
    {
        // Look past the last record address.
         p_token->p_addr += (FDS_HEADER_SIZE + ((fds_header_t*)p_token->p_addr)->tl.length_words);
    }

    // Begin (or resume) searching for a record.
    for (; p_token->vpage_id < FDS_MAX_PAGES; p_token->vpage_id++)
    {
        uint16_t page = 0;

        // Obtain the physical page ID.
        page_id_from_virtual_id(p_token->vpage_id, &page);

        if (m_pages[page].page_type != FDS_PAGE_VALID)
        {
            // Skip this page.
            continue;
        }

        if (p_token->p_addr == NULL)
        {
            // If it's the first time the function is run, initialize the pointer.
            p_token->p_addr = m_pages[page].start_addr + FDS_PAGE_TAG_SIZE;
        }

        // Seek a valid record on this page, starting from the address stored in the token.
        while ((p_token->p_addr < (m_pages[page].start_addr + FS_PAGE_SIZE_WORDS)) &&
               (*p_token->p_addr != FDS_ERASED_WORD)) // Did we jump to an erased word?
        {
            fds_header_t const * const p_header = (fds_header_t*)p_token->p_addr;

            if (header_is_valid(p_header))
            {
                // A valid record was found, check its header for a match.
                bool item_match = false;

                if (p_type != NULL)
                {
                    if (p_header->tl.type == *p_type)
                    {
                        item_match = true;
                    }
                }

                if (p_inst != NULL)
                {
                    if (p_header->ic.instance == *p_inst)
                    {
                        item_match = (p_type == NULL) ? true : item_match && true;
                    }
                    else
                    {
                        item_match = false;
                    }
                }

                if (item_match)
                {
                    // We found the record! Update the descriptor.
                    p_desc->vpage_id  = m_pages[page].vpage_id;
                    p_desc->record_id = p_header->id;

                    p_desc->p_rec     = p_token->p_addr;
                    p_desc->ptr_magic = FDS_MAGIC_HWORD;
                    p_desc->gc_magic  = m_gc_runs;

                    return NRF_SUCCESS;
                }
            }
            // Jump to the next record.
            p_token->p_addr += (FDS_HEADER_SIZE + (p_header->tl.length_words));
        }

        /** We have seeked an entire page. Set the address in the token to NULL
         *  so that it will be set again on the next iteration. */
        p_token->p_addr = NULL;
    }

    /** If we couldn't find the record, zero the token structure
     *  so that it can be reused. */
    p_token->magic = 0x00;

    return NRF_ERROR_NOT_FOUND;
}


static void gc_init()
{
    // Set which pages to GC.
    for (uint16_t i = 0; i < FDS_MAX_PAGES; i++)
    {
        m_gc.do_gc_page[i] = (m_pages[i].page_type == FDS_PAGE_VALID);
    }
}


static void gc_reset()
{
    m_gc.state       = BEGIN;
    m_gc.cur_page    = 0;
    m_gc.p_scan_addr = NULL;
}


static void gc_set_state(fds_gc_state_t new_state)
{
    m_gc.state = new_state;
}


static ret_code_t gc_get_next_page(uint16_t * const next_page)
{
    for (uint16_t i = 0; i < FDS_MAX_PAGES; i++)
    {        
        if (m_gc.do_gc_page[i])
        {
            uint16_t records_open;

            CRITICAL_SECTION_ENTER();
            records_open = m_pages[i].records_open;
            CRITICAL_SECTION_EXIT();

            // Do not attempt to GC this page anymore.
            m_gc.do_gc_page[i] = false;

            // Only GC pages with no open records.
            if (records_open == 0)
            {
                *next_page = i;
                return NRF_SUCCESS;
            }   
        }
    }

    return NRF_ERROR_NOT_FOUND;
}


static ret_code_t gc_page()
{
    ret_code_t ret;

    ret = gc_get_next_page(&m_gc.cur_page);

    // No pages left to GC. GC has terminated. Reset GC data.
    if (ret != NRF_SUCCESS)
    {    
        gc_reset();

        return COMMAND_COMPLETED;
    }

    // Prepare to GC the page.
    gc_set_state(GC_PAGE);

    // Flag the page as being garbage collected.
    ret = page_tag_write_gc(m_gc.cur_page);

    if (ret != NRF_SUCCESS)
    {
        return ret;
    }

    return COMMAND_EXECUTING;
}


static ret_code_t gc_copy_record()
{
    ret_code_t fs_ret;

    // We have found a record to copy.
    fds_record_t const * const p_record = (fds_record_t*)m_gc.p_scan_addr;

    gc_set_state(COPY_RECORD);

    // Copy the item to swap.
    fs_ret = fs_store(&fs_config,
                      m_pages[m_gc.swap_page].start_addr + m_pages[m_gc.swap_page].write_offset,
                      (uint32_t*)p_record,
                      FDS_HEADER_SIZE + p_record->header.tl.length_words);

    if (fs_ret != NRF_SUCCESS)
    {
        // Oops :(
        // This is an error. Can we recover?
    }

    // Remember to update the swap page write offset.
    m_pages[m_gc.swap_page].write_offset += (FDS_HEADER_SIZE + p_record->header.tl.length_words);

    return COMMAND_EXECUTING;
}


static ret_code_t gc_ready_swap_page()
{
    ret_code_t fs_ret;

    /** A page has been scanned through. All valid records found were copied to swap.
     *  The swap page can now be flagged as a valid page. */
    gc_set_state(READY_SWAP);

    fs_ret = page_tag_write_valid(m_gc.swap_page);
    if (fs_ret != NRF_SUCCESS)
    {
        return fs_ret;
    }

    /** Do not update the page type in the internal page structure (m_pages)
     *  right away. (why?) */
    return COMMAND_EXECUTING;
}


static ret_code_t gc_seek_record()
{
    // Let's find a valid record which has not been copied yet.
    if (scan_next_valid(m_gc.cur_page, &m_gc.p_scan_addr) == NRF_SUCCESS)
    {
        /** The record is guaranteed to fit in the destination page,
         *  so we don't need to check its size. */
        return gc_copy_record();
    }
    else
    {
        /** No more (uncopied) records left on this page.
         *  The swap page can now be marked as a valid page. */
        return gc_ready_swap_page();
    }
}


static ret_code_t gc_new_swap_page()
{
    ret_code_t fs_ret;
    uint16_t   vpage_id;

    gc_set_state(NEW_SWAP);

    // Save the swap page virtual page ID.
    vpage_id = m_pages[m_gc.swap_page].vpage_id;

    /** The swap page has been marked as valid in Flash. We copy the GC'ed page
     *  write_offset and virtual page ID. */
    m_pages[m_gc.swap_page].page_type      = FDS_PAGE_VALID;
    m_pages[m_gc.swap_page].vpage_id       = m_pages[m_gc.cur_page].vpage_id;
    m_pages[m_gc.swap_page].words_reserved = m_pages[m_gc.cur_page].words_reserved;

    // The new swap page is now the page we just GC.
    m_gc.swap_page = m_gc.cur_page;

    // Update the write_offset, words_reserved and vpage_id fields for the new swap page.
    m_pages[m_gc.swap_page].page_type      = FDS_PAGE_SWAP;
    m_pages[m_gc.swap_page].vpage_id       = vpage_id;
    m_pages[m_gc.swap_page].write_offset   = FDS_PAGE_TAG_SIZE;
    m_pages[m_gc.swap_page].words_reserved = 0;

    /** Finally, erase the new swap page. Remember we still have to flag this
     *  new page as swap, but we'll wait the callback for this operation to do so. */
    fs_ret = fs_erase(&fs_config,
                      (uint32_t*)m_pages[m_gc.swap_page].start_addr,
                      FS_PAGE_SIZE_WORDS);

    if (fs_ret != NRF_SUCCESS)
    {
        return fs_ret;
    }

    return COMMAND_EXECUTING;
}


static ret_code_t gc_new_swap_page_init()
{
    ret_code_t fs_ret;

    gc_set_state(INIT_SWAP);

    fs_ret = page_tag_write_swap(m_gc.swap_page);
    if (fs_ret != NRF_SUCCESS)
    {
        return fs_ret;
    }

    return COMMAND_EXECUTING;
}


static ret_code_t gc_execute(uint32_t result)
{
    // TODO: Handle resuming GC.

    ret_code_t ret;

    if (result != NRF_SUCCESS)
    {
        // An operation failed. Report to the application.
        return result;
    }

    switch (m_gc.state)
    {
        case BEGIN:
        {
            // Increment the number of times the GC has been run.
            m_gc_runs++;
            // Sets up a list of pages to GC.
            gc_init();
            // Go !
            ret = gc_page();
        } break;

        case GC_PAGE:
            /** A page has been successfully flagged as being GC.
             *  Look for valid records to copy. */
            ret = gc_seek_record();
            break;

        case COPY_RECORD:
            /** A record has been copied to swap.
             *  Look for more records to copy. */
            ret = gc_seek_record();
            break;

        case READY_SWAP:
            /** The swap page has been flagged as 'valid' (ready).
             *  Let's prepare a new swap page. */
            ret = gc_new_swap_page();
            break;

        case NEW_SWAP:
            // A new swap page has been prepared. Let's flag it as swap.
            ret = gc_new_swap_page_init();
            break;

        case INIT_SWAP:
            /** The swap was flagged as swap in flash. Let's compress another page.
             *  Be sure to update the address where to scan from. */
            m_gc.p_scan_addr = NULL;
            ret = gc_page();
            break;

        default:
            // Should really not happen.
            ret = NRF_ERROR_INTERNAL;
            break;
    }

    return ret;
}


/**@brief Function for initializing the command queue. */
static void queues_init(void)
{
    memset(&m_cmd_queue,   0, sizeof(fds_cmd_queue_t));
    memset(&m_chunk_queue, 0, sizeof(fds_chunk_queue_t));
}


void cmd_queue_next(fds_cmd_t ** pp_cmd)
{
    if (*pp_cmd != &m_cmd_queue.cmd[FDS_CMD_QUEUE_SIZE - 1])
    {
        (*pp_cmd)++;
        return;
    }

    *pp_cmd = &m_cmd_queue.cmd[0];
}


void chunk_queue_next(fds_record_chunk_t ** pp_chunk)
{
    if ((*pp_chunk) != &m_chunk_queue.chunk[FDS_CHUNK_QUEUE_SIZE - 1])
    {
        (*pp_chunk)++;
        return;
    }

    *pp_chunk = &m_chunk_queue.chunk[0];
}


/**@brief Advances one position in the command queue. Returns true if the queue is not empty. */
static bool cmd_queue_advance(void)
{
    // Reset the current element.
    memset(&m_cmd_queue.cmd[m_cmd_queue.rp], 0, sizeof(fds_cmd_t));

    CRITICAL_SECTION_ENTER();
    if (m_cmd_queue.count != 0)
    {
        // Advance in the queue, wrapping around if necessary.
        m_cmd_queue.rp = (m_cmd_queue.rp + 1) % FDS_CMD_QUEUE_SIZE;
        m_cmd_queue.count--;
    }
    CRITICAL_SECTION_EXIT();

    return m_cmd_queue.count != 0;
}


/**@brief Returns the current chunk, and advances to the next in the queue. */
static bool chunk_queue_get_and_advance(fds_record_chunk_t ** pp_chunk)
{
    bool chunk_popped = false;

    CRITICAL_SECTION_ENTER();
    if (m_chunk_queue.count != 0)
    {
        // Point to the current chunk and advance the queue.
        *pp_chunk = &m_chunk_queue.chunk[m_chunk_queue.rp];

        m_chunk_queue.rp = (m_chunk_queue.rp + 1) % FDS_CHUNK_QUEUE_SIZE;
        m_chunk_queue.count--;

        chunk_popped = true;
    }
    CRITICAL_SECTION_EXIT();

    return chunk_popped;
}


static bool chunk_queue_skip(uint8_t num_op)
{
    bool chunk_skipped = false;

    CRITICAL_SECTION_ENTER();
    if (num_op <= m_chunk_queue.count)
    {
        m_chunk_queue.count -= num_op;
        chunk_skipped = true;
    }
    CRITICAL_SECTION_EXIT();

    return chunk_skipped;
}


/**@brief Reserves resources on both queues. */
static ret_code_t queue_reserve(uint8_t               num_cmd,
                                uint8_t               num_chunks,
                                fds_cmd_t          ** pp_cmd,
                                fds_record_chunk_t ** pp_chunk)
{
    uint8_t cmd_index;
    uint8_t chunk_index;

    // This is really just being safe.
    if (pp_cmd == NULL || ((pp_chunk == NULL) && (num_chunks != 0)))
    {
        return NRF_ERROR_NULL;
    }

    if (num_cmd == 0)
    {
        return NRF_ERROR_INVALID_DATA;
    }

    CRITICAL_SECTION_ENTER();

    // Ensure there is enough space in the queues.
    if ((m_cmd_queue.count   > FDS_CMD_QUEUE_SIZE - num_cmd) ||
        (m_chunk_queue.count > FDS_CHUNK_QUEUE_SIZE - num_chunks))
    {
        CRITICAL_SECTION_EXIT();
        return NRF_ERROR_BUSY;
    }

    // Find the write position in the commands queue.
    cmd_index  = m_cmd_queue.count;
    cmd_index += m_cmd_queue.rp;
    cmd_index  = cmd_index % FDS_CMD_QUEUE_SIZE;

    *pp_cmd = &m_cmd_queue.cmd[cmd_index];
    m_cmd_queue.count += num_cmd;

    /* If no operations are associated with the command, such as is the case 
     * for initialization and compression, pp_chunk can be NULL. */
    if (num_chunks != 0)
    {
        chunk_index  = m_chunk_queue.count;
        chunk_index += m_chunk_queue.rp;
        chunk_index  = chunk_index % FDS_CHUNK_QUEUE_SIZE;

        *pp_chunk = &m_chunk_queue.chunk[chunk_index];
        m_chunk_queue.count += num_chunks;
    }

    CRITICAL_SECTION_EXIT();

    return NRF_SUCCESS;
}


/**@brief Cancel the reservation on resources on queues. */
static void queue_reserve_cancel(uint8_t num_cmd, uint8_t num_chunks)
{
    CRITICAL_SECTION_ENTER();
    m_cmd_queue.count   -= num_cmd;
    m_chunk_queue.count -= num_chunks;
    CRITICAL_SECTION_EXIT();
}


static void pages_init(uint16_t * const p_pages_avail,
                       bool     * const p_write_page_tag,
                       bool     * const p_resume_comp)
{
    *p_pages_avail    = 0;
    *p_write_page_tag = false;
    *p_resume_comp    = false;

    /** Scan pages and setup page data.
     *  This function does NOT perform write operations in flash. */
    for (uint16_t i = 0; i < FDS_MAX_PAGES; i++)
    {
        // Initialize page data. Note that start_addr must be set BEFORE invoking page_identify().
        m_pages[i].start_addr     = fs_config.p_start_addr + (i * FS_PAGE_SIZE_WORDS);
        m_pages[i].write_offset   = FDS_PAGE_TAG_SIZE;
        m_pages[i].vpage_id       = i;
        m_pages[i].records_open   = 0;
        m_pages[i].words_reserved = 0;

        m_pages[i].page_type      = page_identify(i);

        switch (m_pages[i].page_type)
        {
            case FDS_PAGE_UNDEFINED:
            {
                if (page_is_empty(i))
                {
                    /* We have found an erased page, which can be initialized.
                     * This will require a write in flash. */
                    m_pages[i].page_type = FDS_PAGE_ERASED;
                    *p_write_page_tag = true;
                }
            } break;

            case FDS_PAGE_VALID:
            {
                /** If a page is valid, we update its write offset.
                 *  Additionally, page_scan will update the last known record ID. */
                page_scan(i, &m_pages[i].write_offset);
                (*p_pages_avail)++;
            } break;

            case FDS_PAGE_SWAP:
            {
                m_gc.swap_page    = i;
                m_swap_page_avail = true;
            } break;

            case FDS_PAGE_GC:
            {
                /** There is an ongoing garbage collection.
                 *  We should resume the operation, which we don't yet. */
                m_gc.cur_page   = i;
                m_gc.state      = GC_PAGE;
                *p_resume_comp  = true;
            } break;

            default:
                break;
        }
    }
}


// NOTE: Adds FDS_HEADER_SIZE automatically.
static ret_code_t write_space_reserve(uint16_t length_words, uint16_t * vpage_id)
{
    bool     space_reserved  = false;
    uint16_t total_len_words = length_words + FDS_HEADER_SIZE;

    if (total_len_words >= FS_PAGE_SIZE_WORDS - FDS_PAGE_TAG_SIZE)
    {
        return NRF_ERROR_INVALID_LENGTH;
    }

    for (uint16_t page = 0; page < FDS_MAX_PAGES; page++)
    {
        if ((m_pages[page].page_type == FDS_PAGE_VALID) &&
            (page_has_space(page, total_len_words)))
        {
            space_reserved = true;
            *vpage_id      = m_pages[page].vpage_id;

            CRITICAL_SECTION_ENTER();
            m_pages[page].words_reserved += total_len_words;
            CRITICAL_SECTION_EXIT();
            
            break;
        }
    }

    return space_reserved ? NRF_SUCCESS : NRF_ERROR_NO_MEM;
}


static bool chunk_is_aligned(fds_record_chunk_t const * const p_chunk, uint8_t num_parts)
{
    for (uint8_t i = 0; i < num_parts; i++)
    {
        if (!is_word_aligned(p_chunk[i].p_data))
        {
            return false;
        }
    }

    return true;
}


static ret_code_t init_execute(uint32_t result, uint32_t const * p_page_addr)
{
    uint16_t   cur_page;
    bool       page_tag_written = false;

    if (result != NRF_SUCCESS)
    {
        // Oops. Error.
        return result;
    }

    // Here we just distinguish between the first invocation and the others.
    cur_page = p_page_addr == NULL ? 0 : page_by_addr(p_page_addr) + 1;

    if (cur_page == FDS_MAX_PAGES)
    {
        // We have finished. We'd need to set some flags.
        flag_set(FDS_FLAG_INITIALIZED);
        flag_clear(FDS_FLAG_INITIALIZING);

        return COMMAND_COMPLETED;
    }

    while (cur_page < FDS_MAX_PAGES && !page_tag_written)
    {
        if (m_pages[cur_page].page_type == FDS_PAGE_ERASED)
        {
            page_tag_written = true;

            if (m_swap_page_avail)
            {
                if (page_tag_write_valid(cur_page) != NRF_SUCCESS)
                {
                    // Oops. Error.
                }
                // Update the page type.
                m_pages[cur_page].page_type = FDS_PAGE_VALID;
            }
            else
            {
                if (page_tag_write_swap(cur_page) != NRF_SUCCESS)
                {
                    // Oops. Error.
                }
                // Update the page type.
                m_pages[cur_page].page_type = FDS_PAGE_SWAP;

                /** Update compression data. We set this information in init_pages
                 *  if it is available, otherwise, we should set it here. */
                m_swap_page_avail = true;
                m_gc.swap_page = cur_page;
            }
        }

        cur_page++;
    }

    if (!page_tag_written)
    {
        if (m_swap_page_avail)
        {
            return COMMAND_COMPLETED;
        }
        else
        {
            // There is no empty space to use as swap.
            // Notify user that no compression is available?
        }
    }

    return COMMAND_EXECUTING;
}


/**@brief Function to execute write and update commands.
 *
 */
static ret_code_t store_execute(uint32_t result, fds_cmd_t * const p_cmd)
{
    ret_code_t           fs_ret;
    fds_record_chunk_t * p_chunk = NULL;
    fds_page_t         * p_page  = NULL;
    uint32_t           * p_write_addr;

    // Using virtual page IDs allows other operations to be queued even if GC has been requested.
    page_from_virtual_id(p_cmd->vpage_id, &p_page);

    if (result != NRF_SUCCESS)
    {
        // The previous operation has failed, update the page data.
        p_page->write_offset   += (FDS_HEADER_SIZE + (p_cmd->chunk_offset - FDS_WRITE_OFFSET_DATA));
        p_page->words_reserved -= (FDS_HEADER_SIZE + (p_cmd->chunk_offset - FDS_WRITE_OFFSET_DATA));

        return result;
    }

    // Compute the write address (just syntatic sugar).
    p_write_addr = (uint32_t*)(p_page->start_addr + p_page->write_offset);

    // Execute the operation.
    switch (p_cmd->op_code)
    {
        case FDS_OP_WRITE_TL:
        {
            fs_ret = fs_store(&fs_config,
                              p_write_addr + FDS_WRITE_OFFSET_TL,
                              (uint32_t*)&p_cmd->record_header.tl,
                              FDS_HEADER_SIZE_TL /*Words*/);

            // Set the next operation to be executed.
            p_cmd->op_code = FDS_OP_WRITE_ID;

        } break;

        case FDS_OP_WRITE_ID:
        {
            fs_ret = fs_store(&fs_config,
                              p_write_addr + FDS_WRITE_OFFSET_ID,
                              (uint32_t*)&p_cmd->record_header.id,
                              FDS_HEADER_SIZE_ID /*Words*/);

            p_cmd->op_code = FDS_OP_WRITE_CHUNK;

        } break;

        case FDS_OP_WRITE_CHUNK:
        {
            // Decrement the number of chunks left to write.
            p_cmd->num_chunks--;

            // Retrieve the chunk to be written.
            chunk_queue_get_and_advance(&p_chunk);

            fs_ret = fs_store(&fs_config,
                              p_write_addr + p_cmd->chunk_offset,
                              p_chunk->p_data,
                              p_chunk->length_words);

            // Accumulate the offset.
            p_cmd->chunk_offset += p_chunk->length_words;

            if (p_cmd->num_chunks == 0)
            {
                /** We have written all the record chunks; we'll write
                 *  IC last as a mean to 'validate' the record. */
                p_cmd->op_code = FDS_OP_WRITE_IC;
            }

        } break;

        case FDS_OP_WRITE_IC:
        {
            fs_ret = fs_store(&fs_config,
                              p_write_addr + FDS_WRITE_OFFSET_IC,
                              (uint32_t*)&p_cmd->record_header.ic,
                              FDS_HEADER_SIZE_IC /*Words*/);

            // This is the final operation.
            p_cmd->op_code = FDS_OP_DONE;

        } break;

        case FDS_OP_DONE:
        {
            // We have successfully written down the IC. The command has completed successfully.
            p_page->write_offset   += (FDS_HEADER_SIZE + (p_cmd->chunk_offset - FDS_WRITE_OFFSET_DATA));
            p_page->words_reserved -= (FDS_HEADER_SIZE + (p_cmd->chunk_offset - FDS_WRITE_OFFSET_DATA));

            return COMMAND_COMPLETED;

        };

        default:
            fs_ret = NRF_ERROR_INTERNAL;
            break;
    }

    // If fs_store did not succeed, the command has failed.
    if (fs_ret != NRF_SUCCESS)
    {
        /** We're not going to receive a callback from fstorage
         *  so we update the page data right away. */
        p_page->write_offset   += (FDS_HEADER_SIZE + (p_cmd->chunk_offset - FDS_WRITE_OFFSET_DATA));
        p_page->words_reserved -= (FDS_HEADER_SIZE + (p_cmd->chunk_offset - FDS_WRITE_OFFSET_DATA));

        // We should propagate the error from fstorage.
        return fs_ret;
    }

    // An operation has successfully been executed. Wait for the callback.
    return COMMAND_EXECUTING;
}


static ret_code_t clear_execute(ret_code_t result, fds_cmd_t * const p_cmd)
{
    ret_code_t        ret;
    fds_record_desc_t desc;

    // This must persist across calls.
    static fds_find_token_t tok;

    if (result != NRF_SUCCESS)
    {
        // A previous operation has failed. Propagate the error.
        return result;
    }

    switch (p_cmd->op_code)
    {
        case FDS_OP_CLEAR_TL:
        {
            // We were provided a descriptor for the record.
            desc.vpage_id  = p_cmd->vpage_id;
            desc.record_id = p_cmd->record_header.id;

            /** Unfortunately, we always seek the record in this case,
             *  because we don't buffer an entire record descriptor in the
             *  fds_cmd_t structure. Keep in mind though, that we will
             *  seek one page at most. */
            if (seek_record(&desc) != NRF_SUCCESS)
            {
                // The record never existed, or it is already cleared.
                ret = NRF_ERROR_NOT_FOUND;
            }
            else
            {
                // Copy the record key, so that it may be returned in the callback.
                p_cmd->record_header.tl.type     = ((fds_header_t*)desc.p_rec)->tl.type;
                p_cmd->record_header.ic.instance = ((fds_header_t*)desc.p_rec)->ic.instance;

                ret = fs_store(&fs_config,
                               desc.p_rec,
                               (uint32_t*)&m_fds_tl_invalid,
                               FDS_HEADER_SIZE_TL);
            }

            p_cmd->op_code = FDS_OP_DONE;

        } break;

        case FDS_OP_CLEAR_INSTANCE:
        {
            if (find_record(NULL, &p_cmd->record_header.ic.instance,
                            &desc, &tok) != NRF_SUCCESS)
            {
                // No more records to be found.
                p_cmd->op_code = FDS_OP_DONE;

                // Zero the token, so that we may reuse it.
                memset(&tok, 0, sizeof(fds_find_token_t));

                /** We won't receive a callback, since no flash operation
                 *  was initiated. The command has finished. */
                ret = COMMAND_COMPLETED;
            }
            else
            {
                ret = fs_store(&fs_config,
                               desc.p_rec,
                               (uint32_t*)&m_fds_tl_invalid,
                               FDS_HEADER_SIZE_TL);
            }
        } break;

        case FDS_OP_DONE:
        {
            /** The last operation completed successfully.
             *  The command has finished. Return. */
            ret = COMMAND_COMPLETED;
        } break;

        default:
            ret = NRF_ERROR_INVALID_DATA;
            break;
    }

    // Await for the operation result.
    return ret;
}


static ret_code_t cmd_queue_process(void)
{
    ret_code_t        ret;
    fds_cmd_t * const p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp];

    switch (p_cmd->id)
    {
        case FDS_CMD_INIT:
            ret = init_execute(NRF_SUCCESS, NULL);
            break;

        case FDS_CMD_WRITE:
        case FDS_CMD_UPDATE:
            ret = store_execute(NRF_SUCCESS, p_cmd);
            break;

        case FDS_CMD_CLEAR:
        case FDS_CMD_CLEAR_INST:
            ret = clear_execute(NRF_SUCCESS, p_cmd);
            break;

        case FDS_CMD_GC:
            ret = gc_execute(NRF_SUCCESS);
            break;

        default:
            ret = NRF_ERROR_FORBIDDEN;
            break;
    }

    if ((ret == COMMAND_EXECUTING) || (ret == COMMAND_COMPLETED))
    {
        return NRF_SUCCESS;
    }

    // This is an error.
    return ret;
}


static ret_code_t cmd_queue_process_start(void)
{
    bool start_processing = false;

    if (!flag_is_set(FDS_FLAG_PROCESSING))
    {
        flag_set(FDS_FLAG_PROCESSING);
        start_processing = true;
    }

    if (!start_processing)
    {
        // We are awaiting a callback, so there is no need to manually start queue processing.
        return NRF_SUCCESS;
    }

    return cmd_queue_process();
}


static void fs_callback(uint8_t           op_code,
                        uint32_t          result,
                        uint32_t  const * p_data,
                        fs_length_t       length)
{
    ret_code_t         ret;
    fds_cmd_t        * p_cmd = &m_cmd_queue.cmd[m_cmd_queue.rp];
    fds_record_key_t   record_key;

    switch (p_cmd->id)
    {
        case FDS_CMD_INIT:
            ret = init_execute(result, p_data);
            break;

        case FDS_CMD_WRITE:
        case FDS_CMD_UPDATE:
            ret = store_execute(result, p_cmd);
            break;

        case FDS_CMD_CLEAR:
        case FDS_CMD_CLEAR_INST:
            ret = clear_execute(result, p_cmd);
            break;

        case FDS_CMD_GC:
            ret = gc_execute(result);
            break;

        default:
            // Should not happen.
            ret = NRF_ERROR_INTERNAL;
            break;
    }

    if (ret == COMMAND_EXECUTING /*=NRF_SUCCESS*/)
    {
        /** The current command is still being processed.
         *  The command queue does not need to advance. */
        return;
    }

    // Initialize the fds_record_key_t structure needed for the callback.
    record_key.type     = p_cmd->record_header.tl.type;
    record_key.instance = p_cmd->record_header.ic.instance;

    // The command has either completed or an operation (and thus the command) has failed.
    if (ret == COMMAND_COMPLETED)
    {
        // The command has completed successfully. Notify the application.
        app_notify(NRF_SUCCESS, p_cmd->id, p_cmd->record_header.id, record_key);
    }
    else
    {
        /** An operation has failed. This is fatal for the execution of a command.
         *  Skip other operations associated with the current command.
         *  Notify the user of the failure.  */
        chunk_queue_skip(p_cmd->num_chunks);
        app_notify(ret /*=result*/, p_cmd->id, p_cmd->record_header.id, record_key);
    }

    // Advance the command queue, and if there is still something in the queue, process it.
    if (cmd_queue_advance())
    {
        /** Only process the queue if there are no pending commands being queued, since they
         *  will begin to process the queue on their own. Be sure to clear
         *  the flag FDS_FLAG_PROCESSING though ! */
        if (atomic_counter_is_zero())
        {
            cmd_queue_process();
        }
        else
        {
            flag_clear(FDS_FLAG_PROCESSING);
        }
    }
    else
    {
        /** No more elements in the queue. Clear the FDS_FLAG_PROCESSING flag,
         *  so that new commands can start the queue processing. */
        flag_clear(FDS_FLAG_PROCESSING);
    }
}


ret_code_t fds_init()
{
    ret_code_t   fs_ret;
    fds_cmd_t  * p_cmd;
    uint16_t     pages_avail;
    bool         write_page_tag;
    bool         resume_compression;

    fds_record_key_t const dummy_key = {.type     = FDS_TYPE_ID_INVALID,
                                        .instance = FDS_INSTANCE_ID_INVALID};

    if (flag_is_set(FDS_FLAG_INITIALIZED))
    {
        // Notify immediately.
        app_notify(NRF_SUCCESS, FDS_CMD_INIT, 0 /*unused*/, dummy_key /*unused*/);
        return NRF_SUCCESS;
    }

    if (flag_is_set(FDS_FLAG_INITIALIZING))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    fs_ret = fs_init();
    if (fs_ret != NRF_SUCCESS)
    {
        // fs_init() failed, propagate the error.
        return fs_ret;
    }

    queues_init();

    /** Initialize the last known record to zero.
     *  Its value will be updated by page_scan() called in pages_init(). */
    m_last_rec_id = 0;

    // Initialize the page table containing all info on pages (address, type etc).
    pages_init(&pages_avail, &write_page_tag, &resume_compression);

    if (pages_avail == 0 && !write_page_tag)
    {
        return NRF_ERROR_NO_MEM;
    }

    /** This flag means fds_init() has been called. However,
     *  the module is NOT yet initialized. */
    flag_set(FDS_FLAG_INITIALIZING);

    if (resume_compression)
    {
        return NRF_SUCCESS;
    }

    if (write_page_tag)
    {
        if (queue_reserve(FDS_CMD_QUEUE_SIZE_INIT, 0, &p_cmd, NULL) != NRF_SUCCESS)
        {
            // Should never happen.
            return NRF_ERROR_BUSY;
        }

        // Initialize the command in the queue.
        p_cmd->id = FDS_CMD_INIT;

        return cmd_queue_process_start();
    }
    else
    {
        /* No flash operation is necessary for initialization.
         * We can notify the application immediately. */
        flag_set  (FDS_FLAG_INITIALIZED);
        flag_clear(FDS_FLAG_INITIALIZING);
        app_notify(NRF_SUCCESS, FDS_CMD_INIT, 0 /*unused*/, dummy_key /*unused*/);
    }

    return NRF_SUCCESS;
}


ret_code_t fds_open(fds_record_desc_t * const p_desc,
                    fds_record_t      * const p_record)
{
    uint16_t page;

    if (p_desc == NULL || p_record == NULL)
    {
        return NRF_ERROR_NULL;
    }

    if (page_id_from_virtual_id(p_desc->vpage_id, &page) != NRF_SUCCESS)
    {
        // Should not happen.
        return NRF_ERROR_INVALID_DATA;
    }

    // Seek the record if necessary.
    if (seek_record(p_desc) == NRF_SUCCESS)
    {
        if (header_is_valid((fds_header_t*)p_desc->p_rec))
        {
            CRITICAL_SECTION_ENTER();
            m_pages[page].records_open++;
            CRITICAL_SECTION_EXIT();

            p_record->header = *((fds_header_t*)p_desc->p_rec);
            p_record->p_data = (p_desc->p_rec + FDS_HEADER_SIZE);

            return NRF_SUCCESS;
        }
    }

    /** The record could not be found.
     *  It either never existed or it has been cleared. */
    return NRF_ERROR_NOT_FOUND;
}


ret_code_t fds_close(fds_record_desc_t const * const p_desc)
{
    uint16_t page;

    if (p_desc == NULL)
    {
        return NRF_ERROR_NULL;
    }

    if (page_id_from_virtual_id(p_desc->vpage_id, &page) != NRF_SUCCESS)
    {
        return NRF_ERROR_INVALID_DATA;
    }

    CRITICAL_SECTION_ENTER();
    m_pages[page].records_open--;
    CRITICAL_SECTION_EXIT();

    return NRF_SUCCESS;
}


static ret_code_t write_enqueue(fds_record_desc_t        * const p_desc,
                                fds_record_key_t                 key,
                                uint8_t                          num_chunks,
                                fds_record_chunk_t               chunks[],
                                fds_write_token_t  const * const p_tok,
                                bool                             do_update)
{
    ret_code_t           ret;
    fds_cmd_t          * p_cmd;
    fds_record_chunk_t * p_chunk = NULL;
    uint16_t             vpage_id;
    uint16_t             length_words = 0;
    uint8_t              cmd_queue_elems;

    if (!flag_is_set(FDS_FLAG_INITIALIZED))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    if ((key.type     == FDS_TYPE_ID_INVALID) ||
        (key.instance == FDS_INSTANCE_ID_INVALID))
    {
        return NRF_ERROR_INVALID_DATA;
    }

    if (!chunk_is_aligned(chunks, num_chunks))
    {
        return NRF_ERROR_INVALID_ADDR;
    }

    cmd_queue_elems = do_update ? FDS_CMD_QUEUE_SIZE_UPDATE : FDS_CMD_QUEUE_SIZE_WRITE;

    // Reserve space on both queues, and obtain pointers to the first elements reserved.
    ret = queue_reserve(cmd_queue_elems,
                        num_chunks,
                        &p_cmd,
                        &p_chunk);

    if (ret != NRF_SUCCESS)
    {
        return ret;
    }

    // No space was previously reserved for this operation.
    if (p_tok == NULL)
    {
        // Compute the total length of the record.
        for (uint8_t i = 0; i < num_chunks; i++)
        {
            length_words += chunks[i].length_words;
        }

        /** Find a page where we can write the data. Reserve the space necessary
         *  to write the metadata as well. */
        ret = write_space_reserve(length_words, &vpage_id);
        if (ret != NRF_SUCCESS)
        {
            // If there is no space available, cancel the queue reservation.
            queue_reserve_cancel(cmd_queue_elems, num_chunks);
            return ret;
        }
    }
    else
    {
        length_words = p_tok->length_words;
        vpage_id     = p_tok->vpage_id;
    }

    // Initialize the command.
    p_cmd->id           = do_update ? FDS_CMD_UPDATE : FDS_CMD_WRITE;
    p_cmd->op_code      = FDS_OP_WRITE_TL;
    p_cmd->num_chunks   = num_chunks;
    p_cmd->chunk_offset = FDS_WRITE_OFFSET_DATA;
    p_cmd->vpage_id     = vpage_id;

    // Fill in the header information.
    p_cmd->record_header.id              = record_id_new();
    p_cmd->record_header.tl.type         = key.type;
    p_cmd->record_header.tl.length_words = length_words;
    p_cmd->record_header.ic.instance     = key.instance;
    p_cmd->record_header.ic.checksum     = 0;

    // Buffer the record chunks in the queue.
    for (uint8_t i = 0; i < num_chunks; i++)
    {
        p_chunk->p_data       = chunks[i].p_data;
        p_chunk->length_words = chunks[i].length_words;
        chunk_queue_next(&p_chunk);
    }

    if (do_update)
    {
        // Clear
        cmd_queue_next(&p_cmd);
        p_cmd->id      = FDS_CMD_CLEAR;
        p_cmd->op_code = FDS_OP_CLEAR_TL;

        p_cmd->vpage_id         = p_desc->vpage_id;
        p_cmd->record_header.id = p_desc->record_id;
    }

    // Initialize the record descriptor, if provided.
    if (p_desc != NULL)
    {
        p_desc->vpage_id  = vpage_id;
        // Don't invoke record_id_new() again.
        p_desc->record_id = p_cmd->record_header.id;
    }

    return cmd_queue_process_start();
}


ret_code_t fds_reserve(fds_write_token_t * const p_tok, uint16_t length_words)
{
    uint16_t vpage_id;

    if (!flag_is_set(FDS_FLAG_INITIALIZED))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    if (p_tok == NULL)
    {
        return NRF_ERROR_NULL;
    }

    // Reserve space on the page. write_space_reserve() accounts for the header.
    if (write_space_reserve(length_words, &vpage_id) == NRF_SUCCESS)
    {
        p_tok->vpage_id     = vpage_id;
        p_tok->length_words = length_words;

        return NRF_SUCCESS;
    }

    return NRF_ERROR_NO_MEM;
}


ret_code_t fds_reserve_cancel(fds_write_token_t * const p_tok)
{
    fds_page_t * p_page;

    if (!flag_is_set(FDS_FLAG_INITIALIZED))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    if (p_tok == NULL)
    {
        return NRF_ERROR_NULL;
    }

    if (page_from_virtual_id(p_tok->vpage_id, &p_page) != NRF_SUCCESS)
    {
        // Could not find the virtual page. This shouldn't happen.
        return NRF_ERROR_INVALID_DATA;
    }

    if ((p_page->words_reserved - p_tok->length_words) < 0)
    {
        /** We are trying to cancel a reservation for more words than how many are
         *  currently reserved on the page. This is shouldn't happen. */
        return NRF_ERROR_INVALID_DATA;
    }

    // Free the space which had been reserved.
    p_page->words_reserved -= p_tok->length_words;

    // Clean the token.
    p_tok->vpage_id     = 0;
    p_tok->length_words = 0;

    return NRF_SUCCESS;
}


ret_code_t fds_write(fds_record_desc_t  * const p_desc,
                     fds_record_key_t           key,
                     uint8_t                    num_chunks,
                     fds_record_chunk_t         chunks[])
{
    ret_code_t ret;
    atomic_counter_inc();
    ret = write_enqueue(p_desc, key, num_chunks, chunks, NULL, false /*not an update*/);
    atomic_counter_dec();
    return ret;
}


ret_code_t fds_write_reserved(fds_write_token_t  const * const p_tok,
                              fds_record_desc_t        * const p_desc,
                              fds_record_key_t                 key,
                              uint8_t                          num_chunks,
                              fds_record_chunk_t               chunks[])
{
    ret_code_t ret;
    atomic_counter_inc();
    ret = write_enqueue(p_desc, key, num_chunks, chunks, p_tok, false /*not an update*/);
    atomic_counter_dec();
    return ret;
}


static ret_code_t clear_enqueue(fds_record_desc_t * const p_desc)
{
    ret_code_t   ret;
    fds_cmd_t  * p_cmd;

    if (!flag_is_set(FDS_FLAG_INITIALIZED))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    if (p_desc == NULL)
    {
        return NRF_ERROR_NULL;
    }

    ret = queue_reserve(FDS_CMD_QUEUE_SIZE_CLEAR, 0, &p_cmd, NULL);

    if (ret != NRF_SUCCESS)
    {
        return ret;
    }

    // Initialize the command.
    p_cmd->id        = FDS_CMD_CLEAR;
    p_cmd->op_code   = FDS_OP_CLEAR_TL;

    p_cmd->record_header.id = p_desc->record_id;
    p_cmd->vpage_id         = p_desc->vpage_id;

    return cmd_queue_process_start();
}


ret_code_t fds_clear(fds_record_desc_t * const p_desc)
{
    ret_code_t ret;
    atomic_counter_inc();
    ret = clear_enqueue(p_desc);
    atomic_counter_dec();
    return ret;
}


static ret_code_t clear_by_instance_enqueue(fds_instance_id_t instance)
{
    ret_code_t   ret;
    fds_cmd_t  * p_cmd;

    if (!flag_is_set(FDS_FLAG_INITIALIZED))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    ret  = queue_reserve(FDS_CMD_QUEUE_SIZE_CLEAR, 0, &p_cmd, NULL);

    if (ret != NRF_SUCCESS)
    {
        return ret;
    }

    p_cmd->id      = FDS_CMD_CLEAR_INST;
    p_cmd->op_code = FDS_OP_CLEAR_INSTANCE;

    p_cmd->record_header.ic.instance = instance;

    return cmd_queue_process_start();
}

ret_code_t fds_clear_by_instance(fds_instance_id_t instance)
{
    ret_code_t ret;
    atomic_counter_inc();
    ret = clear_by_instance_enqueue(instance);
    atomic_counter_dec();
    return ret;
}


ret_code_t fds_update(fds_record_desc_t  * const p_desc,
                      fds_record_key_t           key,
                      uint8_t                    num_chunks,
                      fds_record_chunk_t         chunks[])
{
    ret_code_t ret;
    atomic_counter_inc();
    ret = write_enqueue(p_desc, key, num_chunks, chunks, NULL, true /*update*/);
    atomic_counter_dec();
    return ret;
}


static ret_code_t gc_enqueue()
{
    ret_code_t  ret;
    fds_cmd_t * p_cmd;

    if (!flag_is_set(FDS_FLAG_INITIALIZED))
    {
        return NRF_ERROR_INVALID_STATE;
    }

    ret = queue_reserve(FDS_CMD_QUEUE_SIZE_GC, 0, &p_cmd, NULL);
    if (ret != NRF_SUCCESS)
    {
        return ret;
    }

    p_cmd->id = FDS_CMD_GC;

    // Set compression parameters.
    m_gc.state = BEGIN;

    return cmd_queue_process_start();
}


ret_code_t fds_gc()
{
    ret_code_t ret;
    atomic_counter_inc();
    ret = gc_enqueue();
    atomic_counter_dec();
    return ret;   
}


ret_code_t fds_find(fds_type_id_t             type,
                    fds_instance_id_t         instance,
                    fds_record_desc_t * const p_desc,
                    fds_find_token_t  * const p_token)
{
    if (p_desc == NULL || p_token == NULL)
    {
        return NRF_ERROR_NULL;
    }

    return find_record(&type, &instance, p_desc, p_token);
}


ret_code_t fds_find_by_type(fds_type_id_t             type,
                            fds_record_desc_t * const p_desc,
                            fds_find_token_t  * const p_token)
{
    if (p_desc == NULL || p_token == NULL)
    {
        return NRF_ERROR_NULL;
    }

    return find_record(&type, NULL, p_desc, p_token);
}


ret_code_t fds_find_by_instance(fds_instance_id_t         instance,
                                fds_record_desc_t * const p_desc,
                                fds_find_token_t  * const p_token)
{
    if (p_desc == NULL || p_token == NULL)
    {
        return NRF_ERROR_NULL;
    }

    return find_record(NULL, &instance, p_desc, p_token);
}


ret_code_t fds_register(fds_cb_t cb)
{
    if (m_users == FDS_MAX_USERS)
    {
        return NRF_ERROR_NO_MEM;
    }

    m_cb_table[m_users] = cb;
    m_users++;

    return NRF_SUCCESS;
}


bool fds_descriptor_match(fds_record_desc_t const * const p_desc1,
                          fds_record_desc_t const * const p_desc2)
{
    if ((p_desc1 == NULL) || (p_desc2 == NULL))
    {
        return false;
    }

    return (p_desc1->record_id == p_desc2->record_id);
}


ret_code_t fds_descriptor_from_rec_id(fds_record_desc_t * const p_desc,
                                      fds_record_id_t           record_id)
{
    if (p_desc == NULL)
    {
        return NRF_ERROR_NULL;
    }

    p_desc->record_id = record_id;
    p_desc->vpage_id  = FDS_VPAGE_ID_UNKNOWN;

    return NRF_SUCCESS;
}

ret_code_t fds_record_id_from_desc(fds_record_desc_t const * const p_desc,
                                   fds_record_id_t         * const p_record_id)
{
    if (p_desc == NULL || p_record_id == NULL)
    {
        return NRF_ERROR_NULL;
    }

    *p_record_id = p_desc->record_id;

    return NRF_SUCCESS;
}