DVBCore  20.3.0
DVBCore Documentation
stbdbram.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2004 Ocean Blue Software Ltd
4  *
5  * This file is part of a DTVKit Software Component
6  * You are permitted to copy, modify or distribute this file subject to the terms
7  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
8  *
9  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
10  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
11  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * If you or your organisation is not a member of DTVKit then you have access
14  * to this source code outside of the terms of the licence agreement
15  * and you are expected to delete this and any associated files immediately.
16  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
17  *******************************************************************************/
25 //---includes for this file----------------------------------------------------
26 // compiler library header files
27 
28 #include <string.h>
29 
30 // third party header files
31 
32 // Ocean Blue Software header files
33 
34 #include <techtype.h>
35 #include <dbgfuncs.h>
36 
37 #include "dba.h"
38 #include "dba_nvm.h"
39 #include "stbdbram.h"
40 #include "stbdbnvm.h"
41 #include "stbllist.h"
42 #include "stbheap.h"
43 
44 //---constant definitions for this file----------------------------------------
45 
46 //---local typedef structs for this file---------------------------------------
47 
48 typedef struct
49 {
50  LINK_LIST_PTR_BLK list_ptrs;
51  U8BIT rec_id;
52  U16BIT nvm_block;
53  void *parent;
55 
56 //---local (static) variable declarations for this file------------------------
57 // (internal variables declared static to make them local)
58 
59 static LINK_LIST_HEADER *llist_hdr_array;
60 
61 //---local function prototypes for this file-----------------------------------
62 // (internal functions declared static to make them local)
63 
64 //---local function definitions------------------------------------------------
65 
66 //---global function definitions-----------------------------------------------
67 
80 {
81  U8BIT i;
82 
83  FUNCTION_START(STB_InitRAMAccess);
84 
85  // create and init linked list headers
86  llist_hdr_array = (LINK_LIST_HEADER *)STB_GetMemory((U32BIT)(sizeof(LINK_LIST_HEADER) * DBA_NUM_RECORDS));
87  if (llist_hdr_array != NULL)
88  {
89  for (i = 0; i < DBA_NUM_RECORDS; i++)
90  {
91  llist_hdr_array[i].null_next = NULL;
92  llist_hdr_array[i].first = &llist_hdr_array[i];
93  llist_hdr_array[i].last = &llist_hdr_array[i];
94  }
95  }
96 
97  FUNCTION_FINISH(STB_InitRAMAccess);
98 }
99 
112 {
113  U8BIT i;
114  void *rec_ptr;
115  void *next_ptr;
116 
117  FUNCTION_START(STB_PurgeRAMRecords);
118 
119  // for each record list
120  for (i = 0; i < DBA_NUM_RECORDS; i++)
121  {
122  // get first record in list
123  rec_ptr = (void *)STB_LLGetFirstBlock(&llist_hdr_array[i]);
124  while (rec_ptr != NULL)
125  {
126  // get next record in list
127  next_ptr = (void *)STB_LLGetNextBlock((LINK_LIST_PTR_BLK *)rec_ptr);
128 
129  // remove record from linked list
131 
132  // delete memory
133  STB_FreeMemory(rec_ptr);
134 
135  rec_ptr = next_ptr;
136  }
137  }
138 
139  FUNCTION_FINISH(STB_PurgeRAMRecords);
140 }
141 
156 void* STB_CreateRAMRecord(U8BIT rec_id, U16BIT size, U16BIT nvm_block, void *parent)
157 {
158  void *rec_ptr;
159 
160  FUNCTION_START(STB_CreateRAMRecord);
161 
162  ASSERT(rec_id < DBA_NUM_RECORDS);
163 
164  // add space for rec header
165  size += (U16BIT)sizeof(RAM_DB_HEADER);
166 
167  // get memory in RAM
168  rec_ptr = STB_GetMemory((U32BIT)size);
169  if (rec_ptr != NULL)
170  {
171  // setup contents of new memory block
172  memset(rec_ptr, 0, size);
173  ((RAM_DB_HEADER *)rec_ptr)->rec_id = rec_id;
174  ((RAM_DB_HEADER *)rec_ptr)->nvm_block = nvm_block;
175  ((RAM_DB_HEADER *)rec_ptr)->parent = parent;
176 
177  // add record to linked list
178  STB_LLAddBlockToEnd(&llist_hdr_array[rec_id], (LINK_LIST_PTR_BLK *)rec_ptr);
179  }
180 
181  FUNCTION_FINISH(STB_CreateRAMRecord);
182 
183  return(rec_ptr);
184 }
185 
197 void STB_DestroyRAMRecord(void *rec_ptr)
198 {
199  FUNCTION_START(STB_DestroyRAMRecord);
200 
201  ASSERT(rec_ptr != NULL);
202 
203  // remove record from linked list
205 
206  // delete memory
207  STB_FreeMemory(rec_ptr);
208 
209  FUNCTION_FINISH(STB_DestroyRAMRecord);
210 }
211 
223 U8BIT STB_GetRAMRecordId(void *rec_ptr)
224 {
225  U8BIT ret_val;
226 
227  FUNCTION_START(STB_GetRAMRecordId);
228 
229  ASSERT(rec_ptr != NULL);
230 
231  ret_val = ((RAM_DB_HEADER *)rec_ptr)->rec_id;
232 
233  FUNCTION_FINISH(STB_GetRAMRecordId);
234 
235  return(ret_val);
236 }
237 
249 U16BIT STB_GetRAMRecordNVMBlock(void *rec_ptr)
250 {
251  U16BIT ret_val;
252 
253  FUNCTION_START(STB_GetRAMRecordNVMBlock);
254 
255  ASSERT(rec_ptr != NULL);
256 
257  ret_val = ((RAM_DB_HEADER *)rec_ptr)->nvm_block;
258 
259  FUNCTION_FINISH(STB_GetRAMRecordNVMBlock);
260 
261  return(ret_val);
262 }
263 
275 U16BIT STB_GetRAMRecordPrevNVMBlock(void *rec_ptr)
276 {
277  U16BIT ret_val;
278 
279  FUNCTION_START(STB_GetRAMRecordPrevNVMBlock);
280 
281  ASSERT(rec_ptr != NULL);
282 
283  rec_ptr = (void *)STB_LLGetPrevBlock((LINK_LIST_PTR_BLK *)rec_ptr);
284  if (rec_ptr == NULL)
285  {
286  ret_val = NVM_INVALID_BLOCK_ID;
287  }
288  else
289  {
290  ret_val = ((RAM_DB_HEADER *)rec_ptr)->nvm_block;
291  }
292 
293  FUNCTION_FINISH(STB_GetRAMRecordPrevNVMBlock);
294 
295  return(ret_val);
296 }
297 
309 U16BIT STB_GetRAMRecordNextNVMBlock(void *rec_ptr)
310 {
311  U16BIT ret_val;
312 
313  FUNCTION_START(STB_GetRAMRecordNextNVMBlock);
314 
315  ASSERT(rec_ptr != NULL);
316 
317  rec_ptr = (void *)STB_LLGetNextBlock((LINK_LIST_PTR_BLK *)rec_ptr);
318  if (rec_ptr == NULL)
319  {
320  ret_val = NVM_INVALID_BLOCK_ID;
321  }
322  else
323  {
324  ret_val = ((RAM_DB_HEADER *)rec_ptr)->nvm_block;
325  }
326 
327  FUNCTION_FINISH(STB_GetRAMRecordNextNVMBlock);
328 
329  return(ret_val);
330 }
331 
343 void* STB_GetRAMRecordParent(void *rec_ptr)
344 {
345  void *ret_val;
346 
347  FUNCTION_START(STB_GetRAMRecordParent);
348 
349  ASSERT(rec_ptr != NULL);
350 
351  ret_val = ((RAM_DB_HEADER *)rec_ptr)->parent;
352 
353  FUNCTION_FINISH(STB_GetRAMRecordParent);
354 
355  return(ret_val);
356 }
357 
370 void STB_SetRAMRecordParent(void *rec_ptr, void *parent)
371 {
372  FUNCTION_START(STB_SetRAMRecordParent);
373 
374  ASSERT(rec_ptr != NULL);
375 
376  ((RAM_DB_HEADER *)rec_ptr)->parent = parent;
377 
378  FUNCTION_FINISH(STB_SetRAMRecordParent);
379 }
380 
393 void STB_MoveRAMRecordBefore(void *rec_ptr, void *dst_ptr)
394 {
395  FUNCTION_START(STB_MoveRAMRecordBefore);
396 
397  ASSERT(rec_ptr != NULL);
398  ASSERT(dst_ptr != NULL);
399 
402 
403  FUNCTION_FINISH(STB_MoveRAMRecordBefore);
404 }
405 
418 void STB_MoveRAMRecordAfter(void *rec_ptr, void *dst_ptr)
419 {
420  FUNCTION_START(STB_MoveRAMRecordAfter);
421 
422  ASSERT(rec_ptr != NULL);
423  ASSERT(dst_ptr != NULL);
424 
427 
428  FUNCTION_FINISH(STB_MoveRAMRecordAfter);
429 }
430 
445 void* STB_FindRAMRecordFromId(U8BIT rec_id, void *parent, void *last_rec)
446 {
447  void *rec_ptr;
448  void *tmp_ptr;
449  void *ret_val = NULL;
450 
451  FUNCTION_START(STB_FindRAMRecordFromId);
452 
453  ASSERT(rec_id < DBA_NUM_RECORDS);
454 
455  if (last_rec == NULL)
456  {
457  // if first time, start with first record in list
458  rec_ptr = (void *)STB_LLGetFirstBlock(&llist_hdr_array[rec_id]);
459  }
460  else
461  {
462  // else, start with next record in list
463  rec_ptr = (void *)STB_LLGetNextBlock((LINK_LIST_PTR_BLK *)last_rec);
464  }
465 
466  // from first record in list
467  while ((rec_ptr != NULL) && (ret_val == NULL))
468  {
469  if (((RAM_DB_HEADER *)rec_ptr)->rec_id == rec_id)
470  {
471  if (parent != NULL)
472  {
473  // search up tree for parent
474  tmp_ptr = rec_ptr;
475  while (tmp_ptr != NULL)
476  {
477  if (((RAM_DB_HEADER *)tmp_ptr)->parent == parent)
478  {
479  ret_val = rec_ptr;
480  break;
481  }
482  tmp_ptr = ((RAM_DB_HEADER *)tmp_ptr)->parent;
483  }
484  }
485  else
486  {
487  ret_val = rec_ptr;
488  }
489  }
490  // get next record in list
491  rec_ptr = (void *)STB_LLGetNextBlock((LINK_LIST_PTR_BLK *)rec_ptr);
492  }
493 
494  FUNCTION_FINISH(STB_FindRAMRecordFromId);
495 
496  return(ret_val);
497 }
498 
510 void* STB_FindRAMRecordFromNVMBlock(U16BIT nvm_block)
511 {
512  U8BIT i;
513  void *rec_ptr;
514  void *ret_val = NULL;
515 
516  FUNCTION_START(STB_FindRAMRecordFromNVMBlock);
517 
518  // for each record list
519  for (i = 0; i < DBA_NUM_RECORDS; i++)
520  {
521  // get first record in list
522  rec_ptr = (void *)STB_LLGetFirstBlock(&llist_hdr_array[i]);
523  while (rec_ptr != NULL)
524  {
525  if (((RAM_DB_HEADER *)rec_ptr)->nvm_block == nvm_block)
526  {
527  ret_val = rec_ptr;
528  break;
529  }
530  // get next record in list
531  rec_ptr = (void *)STB_LLGetNextBlock((LINK_LIST_PTR_BLK *)rec_ptr);
532  }
533  if (ret_val != NULL)
534  break;
535  }
536 
537  FUNCTION_FINISH(STB_FindRAMRecordFromNVMBlock);
538 
539  return(ret_val);
540 }
541 
552 void STB_SetRAMRecordString(void *rec_ptr, U16BIT offset, U16BIT size, U8BIT *string)
553 {
554  U16BIT i;
555  U8BIT *data_ptr;
556 
557  FUNCTION_START(STB_SetRAMRecordString);
558 
559  ASSERT(rec_ptr != NULL);
560 
561  // find start of field
562  data_ptr = (U8BIT *)rec_ptr;
563  data_ptr += sizeof(RAM_DB_HEADER);
564  data_ptr += offset;
565 
566  if (size != 0)
567  {
568  // for string type, copy data from pointer
569  for (i = 0; i != size; i++)
570  {
571  *data_ptr++ = string[i];
572  }
573  *(data_ptr - 1) = '\0'; // ensure string is null terminated
574  }
575  else
576  {
577  *data_ptr = '\0'; // ensure string is null terminated
578  }
579 
580  FUNCTION_FINISH(STB_SetRAMRecordString);
581 }
582 
593 void STB_SetRAMRecordNumber(void *rec_ptr, U16BIT offset, U16BIT size, U32BIT value )
594 {
595  U16BIT i;
596  U8BIT *data_ptr;
597 
598  FUNCTION_START(STB_SetRAMRecordNumber);
599 
600  ASSERT(rec_ptr != NULL);
601  ASSERT(size > 0);
602 
603  // find start of field
604  data_ptr = (U8BIT *)rec_ptr;
605  data_ptr += sizeof(RAM_DB_HEADER);
606  data_ptr += offset;
607 
608  // for number type, copy part/all of U32BIT into field
609  // forces data into big endian format
610  data_ptr += (size - 1);
611  for (i = 0; i != size; i++)
612  {
613  *data_ptr-- = (value & 0x000000ff);
614  value = (value >> 8);
615  }
616 
617  FUNCTION_FINISH(STB_SetRAMRecordNumber);
618 }
619 
630 void STB_SetRAMRecordPointer(void *rec_ptr, U16BIT offset, void* ptr)
631 {
632  U16BIT i;
633  U8BIT *data_ptr;
634  long value = (long)ptr;
635 
636  FUNCTION_START(STB_SetRAMRecordNumber);
637 
638  ASSERT(rec_ptr != NULL);
639 
640  // find start of field
641  data_ptr = (U8BIT *)rec_ptr;
642  data_ptr += sizeof(RAM_DB_HEADER);
643  data_ptr += offset;
644 
645  data_ptr += (sizeof(void*) - 1);
646  for (i = 0; i != sizeof(void*); i++)
647  {
648  *data_ptr-- = (value & 0x000000ff);
649  value = (value >> 8);
650  }
651 
652  FUNCTION_FINISH(STB_SetRAMRecordNumber);
653 }
654 
667 U8BIT* STB_GetRAMRecordString(void *rec_ptr, U16BIT offset, U16BIT size)
668 {
669  U8BIT *data_ptr;
670 
671  FUNCTION_START(STB_GetRAMRecordString);
672  USE_UNWANTED_PARAM(size);
673 
674  ASSERT(rec_ptr != NULL);
675 
676  // find start of field
677  data_ptr = (U8BIT *)rec_ptr;
678  data_ptr += sizeof(RAM_DB_HEADER);
679  data_ptr += offset;
680 
681  FUNCTION_FINISH(STB_GetRAMRecordString);
682 
683  return(data_ptr);
684 }
685 
697 U32BIT STB_GetRAMRecordNumber(void *rec_ptr, U16BIT offset, U16BIT size)
698 {
699  U16BIT i;
700  U8BIT *data_ptr;
701  U32BIT value = 0;
702 
703  FUNCTION_START(STB_GetRAMRecordNumber);
704 
705  ASSERT(rec_ptr != NULL);
706  ASSERT(size > 0);
707 
708  // find start of field
709  data_ptr = (U8BIT *)rec_ptr;
710  data_ptr += sizeof(RAM_DB_HEADER);
711  data_ptr += offset;
712 
713  // or for any other type, copy field into part/all of U32BIT return value
714  // expects data in big endian format
715  for (i = 0; i != size; i++)
716  {
717  value = (value << 8);
718  value |= *data_ptr++;
719  }
720 
721  FUNCTION_FINISH(STB_GetRAMRecordNumber);
722 
723  return(value);
724 }
725 
737 void *STB_GetRAMRecordPointer(void *rec_ptr, U16BIT offset)
738 {
739  U16BIT i;
740  U8BIT *data_ptr;
741  long value = 0;
742 
743  FUNCTION_START(STB_GetRAMRecordPointer);
744 
745  ASSERT(rec_ptr != NULL);
746 
747  // find start of field
748  data_ptr = (U8BIT *)rec_ptr;
749  data_ptr += sizeof(RAM_DB_HEADER);
750  data_ptr += offset;
751 
752  // or for any other type, copy field into part/all of U32BIT return value
753  // expects data in big endian format
754  for (i = 0; i != sizeof(void*); i++)
755  {
756  value = (value << 8);
757  value |= *data_ptr++;
758  }
759 
760  FUNCTION_FINISH(STB_GetRAMRecordPointer);
761 
762  return((void*)value);
763 }
764 
765 //*****************************************************************************
766 // End of file
767 //*****************************************************************************
768 
U16BIT STB_GetRAMRecordNVMBlock(void *rec_ptr)
Returns NVM block number for given record pointer.
Definition: stbdbram.c:249
void STB_LLAddBlockToEnd(LINK_LIST_HEADER *hdr, LINK_LIST_PTR_BLK *new_blk)
Adds the block identified by the new_blk pointer to the end of the linked list identified by the list...
Definition: stbllist.c:325
void * STB_GetMemory(U32BIT bytes)
Attempts to allocate memory from the heap.
Definition: stbheap.c:221
NVM database access defines, structures and functions.
Header file - Function prototypes for RAM database.
void * STB_GetRAMRecordParent(void *rec_ptr)
Returns parent pointer for given record pointer.
Definition: stbdbram.c:343
LINK_LIST_PTR_BLK * STB_LLGetPrevBlock(LINK_LIST_PTR_BLK *blk)
Returns a pointer to the previous block in the linked list, or NULL if at the start of the list...
Definition: stbllist.c:600
void * STB_GetRAMRecordPointer(void *rec_ptr, U16BIT offset)
Reads the specified value of a field from a RAM record.
Definition: stbdbram.c:737
void STB_SetRAMRecordString(void *rec_ptr, U16BIT offset, U16BIT size, U8BIT *string)
Writes the specified string into a field of a RAM record.
Definition: stbdbram.c:552
void * STB_CreateRAMRecord(U8BIT rec_id, U16BIT size, U16BIT nvm_block, void *parent)
Creates a record of the type given in RAM (mallocs block).
Definition: stbdbram.c:156
Header file - Function prototypes for NVM database.
void STB_SetRAMRecordParent(void *rec_ptr, void *parent)
Sets parent pointer for given record pointer.
Definition: stbdbram.c:370
U32BIT STB_GetRAMRecordNumber(void *rec_ptr, U16BIT offset, U16BIT size)
Reads the specified value of a field from a RAM record.
Definition: stbdbram.c:697
void STB_MoveRAMRecordBefore(void *rec_ptr, void *dst_ptr)
Moves RAM record before the specified record in the RAM linked list.
Definition: stbdbram.c:393
void STB_SetRAMRecordPointer(void *rec_ptr, U16BIT offset, void *ptr)
Writes the specified value into a field of a RAM record.
Definition: stbdbram.c:630
void STB_FreeMemory(void *addr)
Releases previously allocated heap memory.
Definition: stbheap.c:336
U8BIT * STB_GetRAMRecordString(void *rec_ptr, U16BIT offset, U16BIT size)
Reads the specified value of a field from a RAM record.
Definition: stbdbram.c:667
void STB_InitRAMAccess(void)
Initialises parameters needed for RAM record access.
Definition: stbdbram.c:79
U16BIT STB_GetRAMRecordNextNVMBlock(void *rec_ptr)
Returns NVM block number for next record of given pointer.
Definition: stbdbram.c:309
void STB_MoveRAMRecordAfter(void *rec_ptr, void *dst_ptr)
Moves RAM record after the specified record in the RAM linked list.
Definition: stbdbram.c:418
Debug functions header file.
void STB_LLRemoveBlock(LINK_LIST_PTR_BLK *blk)
Removes the block identified by the blk pointer from its linked list.
Definition: stbllist.c:488
Header file - Function prototypes for linked lists.
void * STB_FindRAMRecordFromId(U8BIT rec_id, void *parent, void *last_rec)
Returns pointer to RAM structure for the given record type. Finds the next record in the list after t...
Definition: stbdbram.c:445
LINK_LIST_PTR_BLK * STB_LLGetNextBlock(LINK_LIST_PTR_BLK *blk)
Returns a pointer to the next block in the linked list, or NULL if at the end of the list...
Definition: stbllist.c:566
Database access defines, structures and public functions.
void STB_PurgeRAMRecords(void)
Initialises RAM database by destroying all records and linked lists.
Definition: stbdbram.c:111
void STB_SetRAMRecordNumber(void *rec_ptr, U16BIT offset, U16BIT size, U32BIT value)
Writes the specified value into a field of a RAM record.
Definition: stbdbram.c:593
U8BIT STB_GetRAMRecordId(void *rec_ptr)
Returns record type id for given record pointer.
Definition: stbdbram.c:223
LINK_LIST_PTR_BLK * STB_LLGetFirstBlock(LINK_LIST_HEADER *hdr)
Returns a pointer to the first block in the linked list, identified by hdr.
Definition: stbllist.c:633
System Wide Global Technical Data Type Definitions.
void STB_LLAddBlockBefore(LINK_LIST_PTR_BLK *blk, LINK_LIST_PTR_BLK *new_blk)
Adds the block identified by the new_blk pointer to the linked list before the block identified by th...
Definition: stbllist.c:403
U16BIT STB_GetRAMRecordPrevNVMBlock(void *rec_ptr)
Returns NVM block number for previous record of given pointer.
Definition: stbdbram.c:275
Header file - Function prototypes for heap memory.
void STB_LLAddBlockAfter(LINK_LIST_PTR_BLK *blk, LINK_LIST_PTR_BLK *new_blk)
Adds the block identified by the new_blk pointer to the linked list after the block identified by the...
Definition: stbllist.c:447
void STB_DestroyRAMRecord(void *rec_ptr)
Destroys record given in RAM (frees block).
Definition: stbdbram.c:197
void * STB_FindRAMRecordFromNVMBlock(U16BIT nvm_block)
Returns pointer to RAM structure which relates to the given NVM record block no.
Definition: stbdbram.c:510