PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
58:5f58a2846a20
added fix for directrectangle()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 51:113b1d84c34f 1 /**************************************************************************/
Pokitto 51:113b1d84c34f 2 /*!
Pokitto 51:113b1d84c34f 3 @file PokittoCookie.cpp
Pokitto 51:113b1d84c34f 4 @author Jonne Valola
Pokitto 51:113b1d84c34f 5
Pokitto 51:113b1d84c34f 6 @section LICENSE
Pokitto 51:113b1d84c34f 7
Pokitto 51:113b1d84c34f 8 Software License Agreement (BSD License)
Pokitto 51:113b1d84c34f 9
Pokitto 51:113b1d84c34f 10 Copyright (c) 2018, Jonne Valola
Pokitto 51:113b1d84c34f 11 All rights reserved.
Pokitto 51:113b1d84c34f 12
Pokitto 51:113b1d84c34f 13 Redistribution and use in source and binary forms, with or without
Pokitto 51:113b1d84c34f 14 modification, are permitted provided that the following conditions are met:
Pokitto 51:113b1d84c34f 15 1. Redistributions of source code must retain the above copyright
Pokitto 51:113b1d84c34f 16 notice, this list of conditions and the following disclaimer.
Pokitto 51:113b1d84c34f 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 51:113b1d84c34f 18 notice, this list of conditions and the following disclaimer in the
Pokitto 51:113b1d84c34f 19 documentation and/or other materials provided with the distribution.
Pokitto 51:113b1d84c34f 20 3. Neither the name of the copyright holders nor the
Pokitto 51:113b1d84c34f 21 names of its contributors may be used to endorse or promote products
Pokitto 51:113b1d84c34f 22 derived from this software without specific prior written permission.
Pokitto 51:113b1d84c34f 23
Pokitto 51:113b1d84c34f 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 51:113b1d84c34f 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 51:113b1d84c34f 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 51:113b1d84c34f 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 51:113b1d84c34f 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 51:113b1d84c34f 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 51:113b1d84c34f 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 51:113b1d84c34f 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 51:113b1d84c34f 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 51:113b1d84c34f 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 51:113b1d84c34f 34 */
Pokitto 51:113b1d84c34f 35 /**************************************************************************/
Pokitto 51:113b1d84c34f 36
Pokitto 51:113b1d84c34f 37 #include "Pokitto_settings.h"
Pokitto 51:113b1d84c34f 38 #include "Pokitto.h"
Pokitto 51:113b1d84c34f 39 #include "PokittoCookie.h"
Pokitto 51:113b1d84c34f 40
Pokitto 51:113b1d84c34f 41 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 42 #else
Pokitto 51:113b1d84c34f 43 #include "PokittoSimulator.h"
Pokitto 51:113b1d84c34f 44 #endif
Pokitto 51:113b1d84c34f 45
Pokitto 51:113b1d84c34f 46 using namespace Pokitto;
Pokitto 51:113b1d84c34f 47
Pokitto 51:113b1d84c34f 48 //char Cookie::_key[SBKEYSIZE];
Pokitto 51:113b1d84c34f 49 //char Cookie::_keyorder;
Pokitto 51:113b1d84c34f 50 //bool Cookie::_status;
Pokitto 51:113b1d84c34f 51
Pokitto 51:113b1d84c34f 52 #define HARDCODEDOFFSET 25 //bypasses Cookie parent instance general data (that does not need to be saved in EEPROM)
Pokitto 51:113b1d84c34f 53
Pokitto 51:113b1d84c34f 54 Cookie::Cookie() {
Pokitto 51:113b1d84c34f 55 _status = false;
Pokitto 51:113b1d84c34f 56 _keyorder = SBINVALIDSLOT;
Pokitto 51:113b1d84c34f 57 }
Pokitto 51:113b1d84c34f 58
Pokitto 51:113b1d84c34f 59 int Cookie::initialize() {
Pokitto 51:113b1d84c34f 60 //initialize is called from begin() and can be called several times during program run
Pokitto 51:113b1d84c34f 61 int datasize = _datasize;
Pokitto 51:113b1d84c34f 62 // check if key already exists
Pokitto 51:113b1d84c34f 63 _keyorder = exists(_key);
Pokitto 51:113b1d84c34f 64 if (_keyorder < SBMAXKEYS) {
Pokitto 51:113b1d84c34f 65 // key already exists
Pokitto 51:113b1d84c34f 66 // check amount of existing storage reserved for cookie
Pokitto 51:113b1d84c34f 67 datasize -= getAssignedBlocks()*SBBLOCKSIZE;
Pokitto 51:113b1d84c34f 68 if (datasize<=0) {
Pokitto 51:113b1d84c34f 69 // the size of data matches the size requested
Pokitto 51:113b1d84c34f 70 // therefore retrieve data from storage
Pokitto 51:113b1d84c34f 71 _status = true; //were good to go
Pokitto 51:113b1d84c34f 72 loadCookie();
Pokitto 51:113b1d84c34f 73 } else {
Pokitto 51:113b1d84c34f 74 // if that does not cover the whole size (maybe a newer version of program, who knows)
Pokitto 51:113b1d84c34f 75 // then do not load but reserve more blocks and store a new version
Pokitto 51:113b1d84c34f 76 while (datasize>0) {
Pokitto 51:113b1d84c34f 77 if(reserveBlock()) datasize -= SBBLOCKSIZE;
Pokitto 51:113b1d84c34f 78 else return SBNOTENOUGHBLOCKSFREE; //no space to allocate
Pokitto 51:113b1d84c34f 79 }
Pokitto 51:113b1d84c34f 80 _status = true; //were good to go
Pokitto 51:113b1d84c34f 81 eraseKeytableEntry(_keyorder);
Pokitto 51:113b1d84c34f 82 writeKeyToKeytable(_key,_keyorder); // write the key in the key table in EEPROM
Pokitto 51:113b1d84c34f 83 saveCookie();
Pokitto 51:113b1d84c34f 84 }
Pokitto 51:113b1d84c34f 85 } else {
Pokitto 51:113b1d84c34f 86 // new key needed
Pokitto 51:113b1d84c34f 87 // check if we have free keyslots
Pokitto 51:113b1d84c34f 88 _keyorder = getFreeKeytableSlot();
Pokitto 51:113b1d84c34f 89 if (_keyorder>=SBMAXKEYS) return SBNOMOREKEYS; //no space for key
Pokitto 51:113b1d84c34f 90 // check if we have free storage blocks
Pokitto 51:113b1d84c34f 91 if (getFreeBlocks()*SBBLOCKSIZE<datasize) return SBNOTENOUGHBLOCKSFREE; //no space to allocate
Pokitto 51:113b1d84c34f 92 while (datasize>0) {
Pokitto 51:113b1d84c34f 93 //reserve enough blocks for the data until all data can fit
Pokitto 51:113b1d84c34f 94 if(reserveBlock()) datasize -= SBBLOCKSIZE;
Pokitto 51:113b1d84c34f 95 else return SBNOTENOUGHBLOCKSFREE; //no space to allocate
Pokitto 51:113b1d84c34f 96 }
Pokitto 51:113b1d84c34f 97 }
Pokitto 51:113b1d84c34f 98 _status = true; //were good to go
Pokitto 51:113b1d84c34f 99 eraseKeytableEntry(_keyorder);
Pokitto 51:113b1d84c34f 100 writeKeyToKeytable(_key,_keyorder); // write the key in the key table in EEPROM
Pokitto 51:113b1d84c34f 101 return 0;
Pokitto 51:113b1d84c34f 102 }
Pokitto 51:113b1d84c34f 103
Pokitto 51:113b1d84c34f 104 int Cookie::begin(const char* idkey, int datasize, char* ptr) {
Pokitto 51:113b1d84c34f 105 _status=false;
Pokitto 51:113b1d84c34f 106 _datasize=datasize-HARDCODEDOFFSET;// warning! hardcoded! sizeof(this); //do not include the data of the parent Cookie instance
Pokitto 51:113b1d84c34f 107 _pointer = ptr + HARDCODEDOFFSET;// warning! hardcoded! sizeof(this); //point to the beginning of the inherited instance
Pokitto 51:113b1d84c34f 108 char _idkey[8];
Pokitto 51:113b1d84c34f 109 // make _idkey exactly 8 readable characters long
Pokitto 51:113b1d84c34f 110 for (int t = 0 ; t < 8 ; t++) _idkey[t]=' ';
Pokitto 51:113b1d84c34f 111 for (int t = 0 ; t < 8 ; t++) {if (idkey[t]==0) break; _idkey[t]=idkey[t];}
Pokitto 51:113b1d84c34f 112 // clean Keytable of keys with no storage
Pokitto 51:113b1d84c34f 113 cleanKeytable();
Pokitto 51:113b1d84c34f 114 memcpy(_key, _idkey, SBKEYSIZE); //store name of key
Pokitto 51:113b1d84c34f 115 initialize();
Pokitto 51:113b1d84c34f 116 return 0; //success
Pokitto 51:113b1d84c34f 117 }
Pokitto 51:113b1d84c34f 118
Pokitto 51:113b1d84c34f 119 bool Cookie::saveCookie() {
Pokitto 51:113b1d84c34f 120 if (!_status || !_pointer) initialize(); //reinitialize if needed
Pokitto 51:113b1d84c34f 121 if (!_status || !_pointer) return false; //return if initialize still failed
Pokitto 51:113b1d84c34f 122 char* p = _pointer;
Pokitto 51:113b1d84c34f 123 _head=0;
Pokitto 51:113b1d84c34f 124 _block=0;
Pokitto 51:113b1d84c34f 125 _block=findMyNextBlock();
Pokitto 51:113b1d84c34f 126 for (int i=0; i<_datasize; i++) writeQueue(*p++);
Pokitto 58:5f58a2846a20 127 return true;
Pokitto 51:113b1d84c34f 128 }
Pokitto 51:113b1d84c34f 129
Pokitto 51:113b1d84c34f 130 bool Cookie::loadCookie() {
Pokitto 51:113b1d84c34f 131 if (!_status || !_pointer) return false;
Pokitto 51:113b1d84c34f 132 char* p = _pointer;
Pokitto 51:113b1d84c34f 133 _head=0;
Pokitto 51:113b1d84c34f 134 _block=0;
Pokitto 51:113b1d84c34f 135 _block=findMyNextBlock();
Pokitto 51:113b1d84c34f 136 for (int i=0; i<_datasize; i++) *p++ = readQueue();
Pokitto 58:5f58a2846a20 137 return true;
Pokitto 51:113b1d84c34f 138 }
Pokitto 51:113b1d84c34f 139
Pokitto 51:113b1d84c34f 140 void Cookie::deleteCookie() {
Pokitto 51:113b1d84c34f 141 if (!_status) return;
Pokitto 51:113b1d84c34f 142 // free all blocks held by Cookie
Pokitto 51:113b1d84c34f 143 for (int i=0; i<SBMAXBLOCKS; i++) {
Pokitto 51:113b1d84c34f 144 if (isMyBlock(i)) freeBlock(i);
Pokitto 51:113b1d84c34f 145 }
Pokitto 51:113b1d84c34f 146 // erase Cookie entry from keytable
Pokitto 51:113b1d84c34f 147 eraseKeytableEntry(_keyorder);
Pokitto 51:113b1d84c34f 148 // set status to deleted
Pokitto 51:113b1d84c34f 149 _status = false;
Pokitto 51:113b1d84c34f 150 }
Pokitto 51:113b1d84c34f 151
Pokitto 51:113b1d84c34f 152 int Cookie::exists(const char* idkey) {
Pokitto 51:113b1d84c34f 153 for (int i=0; i< SBMAXKEYS; i++) {
Pokitto 51:113b1d84c34f 154 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 155 if(eeprom_read_byte((uint16_t*)(i*SBKEYSIZE))==idkey[0]) {
Pokitto 51:113b1d84c34f 156 int total=0;
Pokitto 51:113b1d84c34f 157 for (int j=0; j<SBKEYSIZE;j++) {
Pokitto 51:113b1d84c34f 158 if(eeprom_read_byte((uint16_t*)(i*SBKEYSIZE+j))==idkey[j]) total++;
Pokitto 51:113b1d84c34f 159 }
Pokitto 51:113b1d84c34f 160 if (total==SBKEYSIZE) return i; // return the keyslot number where key exists
Pokitto 51:113b1d84c34f 161 }
Pokitto 51:113b1d84c34f 162 #endif
Pokitto 51:113b1d84c34f 163 }
Pokitto 51:113b1d84c34f 164 return SBINVALIDSLOT; //not found
Pokitto 51:113b1d84c34f 165 }
Pokitto 51:113b1d84c34f 166
Pokitto 51:113b1d84c34f 167 int Cookie::getFreeKeytableSlot() {
Pokitto 51:113b1d84c34f 168 int freeslot=SBINVALIDSLOT;
Pokitto 51:113b1d84c34f 169 for (int i=0; i<SBMAXKEYS; i++) {
Pokitto 51:113b1d84c34f 170 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 171 if (eeprom_read_byte((uint16_t*)(i*SBKEYSIZE))==0) {freeslot=i; break;}
Pokitto 51:113b1d84c34f 172 #endif
Pokitto 51:113b1d84c34f 173 }
Pokitto 51:113b1d84c34f 174 return freeslot;
Pokitto 51:113b1d84c34f 175 }
Pokitto 51:113b1d84c34f 176
Pokitto 51:113b1d84c34f 177 int Cookie::getAssignedBlocks() {
Pokitto 51:113b1d84c34f 178 int assignedblocks=0;
Pokitto 51:113b1d84c34f 179 for (int i=0;i<SBMAXBLOCKS;i++) {
Pokitto 51:113b1d84c34f 180 if (isMyBlock(i)) assignedblocks++;
Pokitto 51:113b1d84c34f 181 }
Pokitto 51:113b1d84c34f 182 return assignedblocks;
Pokitto 51:113b1d84c34f 183 }
Pokitto 51:113b1d84c34f 184
Pokitto 51:113b1d84c34f 185 int Cookie::getFreeBlocks() {
Pokitto 51:113b1d84c34f 186 int freeblocks=0;
Pokitto 51:113b1d84c34f 187 for (int i=0;i<SBMAXBLOCKS;i++) {
Pokitto 51:113b1d84c34f 188 if (isFreeBlock(i)) freeblocks++;
Pokitto 51:113b1d84c34f 189 }
Pokitto 51:113b1d84c34f 190 return freeblocks;
Pokitto 51:113b1d84c34f 191 }
Pokitto 51:113b1d84c34f 192
Pokitto 51:113b1d84c34f 193 bool Cookie::isFreeBlock(int n) {
Pokitto 51:113b1d84c34f 194 if (n>=SBMAXBLOCKS) return false;
Pokitto 51:113b1d84c34f 195 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 196 if (!(eeprom_read_byte((uint16_t*)(SBMAXKEYS*SBKEYSIZE+n))&0x80)) return true; //highest bit 0, its free
Pokitto 51:113b1d84c34f 197 #endif
Pokitto 51:113b1d84c34f 198 return false; //its not free
Pokitto 51:113b1d84c34f 199 }
Pokitto 51:113b1d84c34f 200
Pokitto 51:113b1d84c34f 201 bool Cookie::isMyBlock(int n) {
Pokitto 51:113b1d84c34f 202 if (n>=SBMAXBLOCKS) return false;
Pokitto 51:113b1d84c34f 203 if (isFreeBlock(n)) return false; //"free" blocks can not be "reserved" at the same time!
Pokitto 51:113b1d84c34f 204 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 205 char temp; int address;
Pokitto 51:113b1d84c34f 206 address = (SBMAXKEYS*SBKEYSIZE+n);
Pokitto 51:113b1d84c34f 207 temp = eeprom_read_byte((uint16_t*)address);
Pokitto 51:113b1d84c34f 208 if ((temp&0x7F) ==_keyorder) return true;
Pokitto 51:113b1d84c34f 209 #endif
Pokitto 51:113b1d84c34f 210 return false; //its not your block
Pokitto 51:113b1d84c34f 211 }
Pokitto 51:113b1d84c34f 212
Pokitto 51:113b1d84c34f 213 bool Cookie::blockIsOwnedBy(int n, int k) {
Pokitto 51:113b1d84c34f 214 if (n>=SBMAXBLOCKS) return false;
Pokitto 51:113b1d84c34f 215 if (k>=SBMAXKEYS) return false;
Pokitto 51:113b1d84c34f 216 if (isFreeBlock(n)) return false; //"free" blocks can not be "owned" by anyone
Pokitto 51:113b1d84c34f 217 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 218 char temp; int address;
Pokitto 51:113b1d84c34f 219 address = (SBMAXKEYS*SBKEYSIZE+n);
Pokitto 51:113b1d84c34f 220 temp = eeprom_read_byte((uint16_t*)address);
Pokitto 51:113b1d84c34f 221 if ((temp&0x7F) == k) return true;
Pokitto 51:113b1d84c34f 222 #endif
Pokitto 51:113b1d84c34f 223 return false; //its not your block
Pokitto 51:113b1d84c34f 224 }
Pokitto 51:113b1d84c34f 225
Pokitto 51:113b1d84c34f 226 void Cookie::writeKeyToKeytable(const char* key, int slot) {
Pokitto 51:113b1d84c34f 227 for (int i=0; i<SBKEYSIZE; i++) {
Pokitto 51:113b1d84c34f 228 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 229 if (key[i]) eeprom_write_byte((uint16_t*)(slot*SBKEYSIZE+i),key[i]);
Pokitto 51:113b1d84c34f 230 else eeprom_write_byte((uint16_t*)(slot*SBKEYSIZE+i),0);
Pokitto 51:113b1d84c34f 231 #endif
Pokitto 51:113b1d84c34f 232 }
Pokitto 51:113b1d84c34f 233 }
Pokitto 51:113b1d84c34f 234
Pokitto 51:113b1d84c34f 235 void Cookie::readKeytableEntry(int n, char* answer) {
Pokitto 51:113b1d84c34f 236 answer[8]=0;
Pokitto 51:113b1d84c34f 237 if (n >= SBMAXKEYS) n=SBMAXKEYS-1;
Pokitto 51:113b1d84c34f 238 for (int i=0; i<SBKEYSIZE; i++) {
Pokitto 51:113b1d84c34f 239 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 240 answer[i] = eeprom_read_byte((uint16_t*)(n*SBKEYSIZE+i));
Pokitto 51:113b1d84c34f 241 #endif
Pokitto 51:113b1d84c34f 242 }
Pokitto 51:113b1d84c34f 243 }
Pokitto 51:113b1d84c34f 244
Pokitto 51:113b1d84c34f 245 char Cookie::getBlockTableEntry(int n) {
Pokitto 51:113b1d84c34f 246 if (n>=SBMAXBLOCKS) return 0x80; // out of bounds will return a reserved block marker
Pokitto 51:113b1d84c34f 247 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 248 return eeprom_read_byte((uint16_t*)(SBKEYSIZE*SBMAXKEYS+n));
Pokitto 51:113b1d84c34f 249 #endif
Pokitto 58:5f58a2846a20 250 //return 0x80;
Pokitto 51:113b1d84c34f 251 }
Pokitto 51:113b1d84c34f 252
Pokitto 51:113b1d84c34f 253 void Cookie::readBlock(int n, char* data) {
Pokitto 51:113b1d84c34f 254 for (int i=0; i<SBBLOCKSIZE; i++) {
Pokitto 51:113b1d84c34f 255 data[i]=0;
Pokitto 51:113b1d84c34f 256 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 257 if (n < SBMAXBLOCKS) data[i] = eeprom_read_byte((uint16_t*)(SBKEYSIZE*SBMAXKEYS+SBMAXBLOCKS+n*SBBLOCKSIZE+i));
Pokitto 51:113b1d84c34f 258 #endif
Pokitto 51:113b1d84c34f 259 }
Pokitto 51:113b1d84c34f 260 }
Pokitto 51:113b1d84c34f 261
Pokitto 51:113b1d84c34f 262 void Cookie::formatKeytable() {
Pokitto 51:113b1d84c34f 263 for (int j=0; j<SBMAXKEYS; j++) {
Pokitto 51:113b1d84c34f 264 for (int i=0; i<SBKEYSIZE; i++) {
Pokitto 51:113b1d84c34f 265 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 266 eeprom_write_byte((uint16_t*)(j*SBKEYSIZE+i),0);
Pokitto 51:113b1d84c34f 267 #endif
Pokitto 51:113b1d84c34f 268 }
Pokitto 51:113b1d84c34f 269 }
Pokitto 51:113b1d84c34f 270 }
Pokitto 51:113b1d84c34f 271
Pokitto 51:113b1d84c34f 272 void Cookie::freeBlock(int n) {
Pokitto 51:113b1d84c34f 273 if (n >= SBMAXBLOCKS) return; //out of bounds
Pokitto 51:113b1d84c34f 274 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 275 // delete entry from blocktable
Pokitto 51:113b1d84c34f 276 eeprom_write_byte((uint16_t*)(SBKEYSIZE*SBMAXKEYS+n),0);
Pokitto 51:113b1d84c34f 277 #endif
Pokitto 51:113b1d84c34f 278 for (int i=0; i<SBBLOCKSIZE;i++) {
Pokitto 51:113b1d84c34f 279 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 280 // wipe data in the block
Pokitto 51:113b1d84c34f 281 eeprom_write_byte((uint16_t*)(SBKEYSIZE*SBMAXKEYS+SBMAXBLOCKS+n*SBBLOCKSIZE+i),0);
Pokitto 51:113b1d84c34f 282 #endif
Pokitto 51:113b1d84c34f 283 }
Pokitto 51:113b1d84c34f 284 }
Pokitto 51:113b1d84c34f 285
Pokitto 51:113b1d84c34f 286 bool Cookie::reserveBlock() {
Pokitto 51:113b1d84c34f 287 for (int i=0; i<SBMAXBLOCKS;i++) {
Pokitto 51:113b1d84c34f 288 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 289 // reserve block from blocktable
Pokitto 51:113b1d84c34f 290 if (isFreeBlock(i)) {
Pokitto 51:113b1d84c34f 291 //free block found, mark it for us in the blocktable
Pokitto 51:113b1d84c34f 292 eeprom_write_byte((uint16_t*)(SBKEYSIZE*SBMAXKEYS+i),_keyorder | 0x80);
Pokitto 51:113b1d84c34f 293 return true;
Pokitto 51:113b1d84c34f 294 }
Pokitto 51:113b1d84c34f 295 #endif
Pokitto 51:113b1d84c34f 296 }
Pokitto 51:113b1d84c34f 297 return false; // no free block found
Pokitto 51:113b1d84c34f 298 }
Pokitto 51:113b1d84c34f 299
Pokitto 51:113b1d84c34f 300 void Cookie::eraseKeytableEntry(int n) {
Pokitto 51:113b1d84c34f 301 if (n >= SBMAXKEYS) n=SBMAXKEYS-1;
Pokitto 51:113b1d84c34f 302 for (int i=0; i<SBKEYSIZE; i++) {
Pokitto 51:113b1d84c34f 303 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 304 eeprom_write_byte((uint16_t*)(n*SBKEYSIZE+i),0);
Pokitto 51:113b1d84c34f 305 #endif
Pokitto 51:113b1d84c34f 306 }
Pokitto 51:113b1d84c34f 307 }
Pokitto 51:113b1d84c34f 308
Pokitto 51:113b1d84c34f 309 void Cookie::cleanKeytable() {
Pokitto 51:113b1d84c34f 310 //Remove any keys without blocks
Pokitto 51:113b1d84c34f 311 for (int entry=0; entry<SBMAXKEYS; entry++) {
Pokitto 51:113b1d84c34f 312 if (eeprom_read_byte((uint16_t*)(entry*SBKEYSIZE))) {
Pokitto 51:113b1d84c34f 313 bool isEmpty=true;
Pokitto 51:113b1d84c34f 314 for (int block=0; block<SBMAXBLOCKS; block++) if (blockIsOwnedBy(block,entry)) {isEmpty=false;break;}
Pokitto 51:113b1d84c34f 315 //this entry has no blocks reserved, so lets clean it from the keytable
Pokitto 51:113b1d84c34f 316 if (isEmpty) eraseKeytableEntry(entry);
Pokitto 51:113b1d84c34f 317 }
Pokitto 51:113b1d84c34f 318 }
Pokitto 51:113b1d84c34f 319 for (int block=0;block<SBMAXBLOCKS;block++) {
Pokitto 51:113b1d84c34f 320 int blockentry = eeprom_read_byte((uint16_t*)(SBMAXKEYS*SBKEYSIZE+block));
Pokitto 51:113b1d84c34f 321 if (blockentry&0x80) {
Pokitto 51:113b1d84c34f 322 blockentry &= 0x7F;
Pokitto 51:113b1d84c34f 323 bool isEmpty=true;
Pokitto 51:113b1d84c34f 324 for (int key=0;key<SBMAXKEYS;key++) {
Pokitto 51:113b1d84c34f 325 if (eeprom_read_byte((uint16_t*)(key*SBKEYSIZE))) {isEmpty=false;break;}
Pokitto 51:113b1d84c34f 326 }
Pokitto 51:113b1d84c34f 327 if (isEmpty) eeprom_write_byte((uint16_t*)(SBMAXKEYS*SBKEYSIZE+block),0);
Pokitto 51:113b1d84c34f 328 }
Pokitto 51:113b1d84c34f 329 }
Pokitto 51:113b1d84c34f 330 }
Pokitto 51:113b1d84c34f 331
Pokitto 51:113b1d84c34f 332 char Cookie::readQueue() {
Pokitto 51:113b1d84c34f 333 char data=0;
Pokitto 51:113b1d84c34f 334 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 335 int address;
Pokitto 51:113b1d84c34f 336 address = SBMAXKEYS*SBKEYSIZE+SBMAXBLOCKS+SBBLOCKSIZE*_block+_head%SBBLOCKSIZE;
Pokitto 51:113b1d84c34f 337 data=eeprom_read_byte((uint16_t*)address);
Pokitto 51:113b1d84c34f 338 #endif
Pokitto 51:113b1d84c34f 339 _head++;
Pokitto 51:113b1d84c34f 340 if (_head%SBBLOCKSIZE==0 && _head) {
Pokitto 51:113b1d84c34f 341 _block++;
Pokitto 51:113b1d84c34f 342 _block=findMyNextBlock();
Pokitto 51:113b1d84c34f 343 }
Pokitto 51:113b1d84c34f 344 return data;
Pokitto 51:113b1d84c34f 345 }
Pokitto 51:113b1d84c34f 346
Pokitto 51:113b1d84c34f 347 void Cookie::writeQueue(char data) {
Pokitto 51:113b1d84c34f 348 #ifndef POK_SIM
Pokitto 51:113b1d84c34f 349 eeprom_write_byte((uint16_t*)(SBMAXKEYS*SBKEYSIZE+SBMAXBLOCKS+SBBLOCKSIZE*_block+_head%SBBLOCKSIZE),data);
Pokitto 51:113b1d84c34f 350 #endif
Pokitto 51:113b1d84c34f 351 _head++;
Pokitto 51:113b1d84c34f 352 if (_head%SBBLOCKSIZE==0 && _head) {
Pokitto 51:113b1d84c34f 353 _block++;
Pokitto 51:113b1d84c34f 354 _block=findMyNextBlock();
Pokitto 51:113b1d84c34f 355 }
Pokitto 51:113b1d84c34f 356 }
Pokitto 51:113b1d84c34f 357
Pokitto 51:113b1d84c34f 358 int Cookie::findMyNextBlock() {
Pokitto 51:113b1d84c34f 359 if (!_status) return SBINVALIDBLOCK;
Pokitto 51:113b1d84c34f 360 for (int i=_block; i<SBMAXBLOCKS;i++) if (isMyBlock(i)) return i;
Pokitto 58:5f58a2846a20 361 return SBINVALIDBLOCK;
Pokitto 51:113b1d84c34f 362 }
Pokitto 51:113b1d84c34f 363
Pokitto 51:113b1d84c34f 364
Pokitto 51:113b1d84c34f 365