![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
fork to force BLE id to have prefix that is assumed by the app
Dependencies: BLE_API Crypto RNG mbed nRF51822
Fork of Garagem by
History.h@10:80850cd6c29e, 2015-08-25 (annotated)
- Committer:
- dgomes
- Date:
- Tue Aug 25 23:26:43 2015 +0000
- Revision:
- 10:80850cd6c29e
- Parent:
- 9:329af8cdc923
Version 1; Moving to SecurityService
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dgomes | 9:329af8cdc923 | 1 | #ifndef HISTORY_H |
dgomes | 9:329af8cdc923 | 2 | #define HISTORY_H |
dgomes | 9:329af8cdc923 | 3 | |
dgomes | 9:329af8cdc923 | 4 | template<uint32_t BufferSize> |
dgomes | 9:329af8cdc923 | 5 | class History { |
dgomes | 9:329af8cdc923 | 6 | public: |
dgomes | 10:80850cd6c29e | 7 | History() : _head(0),_read(0) { |
dgomes | 10:80850cd6c29e | 8 | for(int i=0; i<BufferSize; i++) |
dgomes | 10:80850cd6c29e | 9 | _pool[i] = 0; |
dgomes | 9:329af8cdc923 | 10 | } |
dgomes | 9:329af8cdc923 | 11 | |
dgomes | 9:329af8cdc923 | 12 | ~History() { |
dgomes | 9:329af8cdc923 | 13 | } |
dgomes | 10:80850cd6c29e | 14 | |
dgomes | 10:80850cd6c29e | 15 | uint64_t getToken() { |
dgomes | 10:80850cd6c29e | 16 | uint64_t tok = _pool[_read++]; |
dgomes | 10:80850cd6c29e | 17 | _read %= BufferSize; |
dgomes | 10:80850cd6c29e | 18 | return tok; |
dgomes | 10:80850cd6c29e | 19 | } |
dgomes | 9:329af8cdc923 | 20 | |
dgomes | 9:329af8cdc923 | 21 | void save(const uint64_t& data) { |
dgomes | 9:329af8cdc923 | 22 | _pool[_head++] = data; |
dgomes | 9:329af8cdc923 | 23 | _head %= BufferSize; |
dgomes | 9:329af8cdc923 | 24 | } |
dgomes | 9:329af8cdc923 | 25 | |
dgomes | 9:329af8cdc923 | 26 | bool exist(const uint64_t& data) { |
dgomes | 9:329af8cdc923 | 27 | for(uint32_t i=0; i<BufferSize; i++) { |
dgomes | 9:329af8cdc923 | 28 | DBG("%llu == %llu\r\n", _pool[i], data); |
dgomes | 9:329af8cdc923 | 29 | if(_pool[i]==data) { |
dgomes | 9:329af8cdc923 | 30 | return true; |
dgomes | 9:329af8cdc923 | 31 | } |
dgomes | 9:329af8cdc923 | 32 | } |
dgomes | 9:329af8cdc923 | 33 | return false; |
dgomes | 9:329af8cdc923 | 34 | } |
dgomes | 9:329af8cdc923 | 35 | |
dgomes | 9:329af8cdc923 | 36 | uint64_t last() { |
dgomes | 9:329af8cdc923 | 37 | uint32_t prev = (_head-1+BufferSize)%BufferSize; |
dgomes | 9:329af8cdc923 | 38 | return _pool[prev]; |
dgomes | 9:329af8cdc923 | 39 | } |
dgomes | 9:329af8cdc923 | 40 | |
dgomes | 9:329af8cdc923 | 41 | uint32_t last_ts() { |
dgomes | 9:329af8cdc923 | 42 | return (uint32_t) last(); |
dgomes | 9:329af8cdc923 | 43 | } |
dgomes | 9:329af8cdc923 | 44 | |
dgomes | 9:329af8cdc923 | 45 | /** Reset the buffer |
dgomes | 9:329af8cdc923 | 46 | * |
dgomes | 9:329af8cdc923 | 47 | */ |
dgomes | 9:329af8cdc923 | 48 | void reset() { |
dgomes | 9:329af8cdc923 | 49 | _head = 0; |
dgomes | 10:80850cd6c29e | 50 | _read = 0; |
dgomes | 9:329af8cdc923 | 51 | } |
dgomes | 9:329af8cdc923 | 52 | |
dgomes | 9:329af8cdc923 | 53 | private: |
dgomes | 9:329af8cdc923 | 54 | uint64_t _pool[BufferSize]; |
dgomes | 10:80850cd6c29e | 55 | volatile uint32_t _head, _read; |
dgomes | 9:329af8cdc923 | 56 | }; |
dgomes | 9:329af8cdc923 | 57 | |
dgomes | 9:329af8cdc923 | 58 | |
dgomes | 9:329af8cdc923 | 59 | #endif |