DVBCore  20.3.0
DVBCore Documentation
stbpvrmsg.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2007 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 /* Enable debug output */
26 /* #define STB_DEBUG*/
27 
28 /*---includes for this file---------------------------------------------------- */
29 /* compiler library header files */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 /* third party header files */
36 
37 /* DVBCore header files */
38 
39 #include <techtype.h>
40 #include <dbgfuncs.h>
41 
42 #include "stbhwos.h"
43 #include "stbheap.h"
44 #include "stbgc.h"
45 #include "stbuni.h"
46 
47 /*---constant definitions for this file---------------------------------------- */
48 #ifdef STB_DEBUG
49 #define STB_PVR_MSG_PRINT(x) STB_SPDebugWrite x
50 #else
51 #define STB_PVR_MSG_PRINT(x)
52 #endif
53 
54 /*---local typedef structs for this file--------------------------------------- */
55 
56 typedef struct s_pvr_message
57 {
58  struct s_pvr_message *prev;
59  struct s_pvr_message *next;
60  U16BIT handle;
61  U16BIT date;
62  U8BIT hour;
63  U8BIT min;
64  U8BIT message;
66 
67 /*---local (static) variable declarations for this file---------------------------------------------
68  (internal variables declared static to make them local)*/
69 static S_PVR_MESSAGE *msg_head;
70 static S_PVR_MESSAGE *msg_tail;
71 static U16BIT last_handle_value;
72 static BOOLEAN new_messages;
73 static void *msg_mutex;
74 
75 /*---local function prototypes for this file--------------------------------------------------------
76  (internal functions declared static to make them local) */
77 static S_PVR_MESSAGE* GetHandleStatus(U16BIT handle);
78 
79 
80 /*!**************************************************************************
81  * @brief Initialises file that provides messages for recording er
82  * @param None
83  * @return TRUE if successful, otherwise FALSE
84  ****************************************************************************/
86 {
87  FUNCTION_START(STB_PVRInitialiseMessages);
88 
89  STB_PVR_MSG_PRINT(("PVR MSG - InitialiseMessages"));
90 
91  if (msg_mutex == NULL)
92  {
93  msg_head = NULL;
94  msg_tail = NULL;
95  last_handle_value = 0;
96  new_messages = FALSE;
97 
98  msg_mutex = STB_OSCreateMutex();
99  }
100 
101  FUNCTION_FINISH(STB_PVRInitialiseMessages);
102 
103  return(TRUE);
104 }
105 
106 /*!**************************************************************************
107  * @brief Adds a message to the end of the list of existing messages
108  * @param message - The message to be written
109  * @return TRUE if successful, otherwise FALSE
110  ****************************************************************************/
111 BOOLEAN STB_PVRAddMessage(U8BIT *message)
112 {
113  BOOLEAN retval;
114  S_PVR_MESSAGE *msg_ptr;
115  U32BIT num_bytes_in_string;
116  U16BIT handle;
117  U16BIT cur_date;
118  U8BIT cur_hour;
119  U8BIT cur_min;
120  U8BIT cur_sec;
121 
122  FUNCTION_START(STB_PVRAddMessage);
123 
124  retval = FALSE;
125 
126  if (msg_mutex != NULL)
127  {
128  num_bytes_in_string = STB_GetNumBytesInString(message);
129  if (num_bytes_in_string > 0)
130  {
131  /* Lock access to the messages while the new message is added */
132  STB_OSMutexLock(msg_mutex);
133 
134  /* Find an unused handle*/
135  handle = last_handle_value;
136 
137  do
138  {
139  handle++;
140  msg_ptr = GetHandleStatus(handle);
141  }
142  while (msg_ptr != NULL);
143 
144  last_handle_value = handle;
145 
146  STB_GCGetGMTDateTime(&cur_date, &cur_hour, &cur_min, &cur_sec);
147 
148  /* Create a new entry for the message */
149  msg_ptr = (S_PVR_MESSAGE *)STB_GetMemory(sizeof(S_PVR_MESSAGE) + num_bytes_in_string);
150  if (msg_ptr != NULL)
151  {
152  memset(msg_ptr, 0, sizeof(S_PVR_MESSAGE));
153 
154  msg_ptr->handle = handle;
155  msg_ptr->date = cur_date;
156  msg_ptr->hour = cur_hour;
157  msg_ptr->min = cur_min;
158 
159  memcpy(&msg_ptr->message, message, num_bytes_in_string);
160 
161  /* Add the message to the list */
162  if (msg_head == NULL)
163  {
164  msg_head = msg_ptr;
165  msg_tail = msg_ptr;
166  }
167  else
168  {
169  msg_ptr->prev = msg_tail;
170  msg_tail->next = msg_ptr;
171  msg_tail = msg_ptr;
172  }
173 
174  new_messages = TRUE;
175  retval = TRUE;
176  }
177 
178  STB_OSMutexUnlock(msg_mutex);
179  }
180  }
181 
182  FUNCTION_FINISH(STB_PVRAddMessage);
183 
184  return(retval);
185 }
186 
187 /*!**************************************************************************
188  * @brief Check for new messages and clear the new message flag
189  * @return TRUE if there is a new message, otherwise FALSE
190  ****************************************************************************/
191 BOOLEAN STB_PVRCheckMessages(void)
192 {
193  BOOLEAN ret_val;
194 
195  FUNCTION_START(STB_PVRCheckMessages);
196 
197  ret_val = new_messages;
198  new_messages = FALSE;
199 
200  FUNCTION_FINISH(STB_PVRCheckMessages);
201 
202  return(ret_val);
203 }
204 
205 /*!**************************************************************************
206  * @brief Returns the number of messages
207  * @return number of messages
208  ****************************************************************************/
210 {
211  U16BIT num_messages;
212  S_PVR_MESSAGE *msg_ptr;
213 
214  FUNCTION_START(STB_PVRGetNumMessages);
215 
216  num_messages = 0;
217 
218  if (msg_mutex != NULL)
219  {
220  STB_OSMutexLock(msg_mutex);
221 
222  msg_ptr = msg_head;
223  while (msg_ptr != NULL)
224  {
225  num_messages++;
226  msg_ptr = msg_ptr->next;
227  }
228 
229  STB_OSMutexUnlock(msg_mutex);
230  }
231 
232  FUNCTION_FINISH(STB_PVRGetNumMessages);
233 
234  return(num_messages);
235 }
236 
237 /*!**************************************************************************
238  * @brief Deletes a message from the list
239  * @param handle - message handle
240  * @return TRUE if the message is found and deleted, FALSE otherwise
241  ****************************************************************************/
242 BOOLEAN STB_PVRDeleteMessage(U16BIT handle)
243 {
244  S_PVR_MESSAGE *msg_ptr;
245  BOOLEAN retval;
246 
247  FUNCTION_START(STB_PVRDeleteHandle);
248 
249  retval = FALSE;
250 
251  if (msg_mutex != NULL)
252  {
253  STB_OSMutexLock(msg_mutex);
254 
255  /* Find the message */
256  msg_ptr = GetHandleStatus(handle);
257  if (msg_ptr != NULL)
258  {
259  /* Remove it from the list */
260  if (msg_ptr->prev != NULL)
261  {
262  /* Message isn't at the head of the list */
263  msg_ptr->prev->next = msg_ptr->next;
264  }
265  else
266  {
267  /* This message is at the head of the list */
268  msg_head = msg_ptr->next;
269  if (msg_head != NULL)
270  {
271  msg_head->prev = NULL;
272  }
273  }
274 
275  if (msg_ptr->next != NULL)
276  {
277  /* Message isn't at the tail of the list */
278  msg_ptr->next->prev = msg_ptr->prev;
279  }
280  else
281  {
282  /* This message is at the tail of the list */
283  msg_tail = msg_tail->prev;
284  if (msg_tail != NULL)
285  {
286  msg_tail->next = NULL;
287  }
288  }
289 
290  /* Destroy the message */
291  STB_FreeMemory(msg_ptr);
292 
293  retval = TRUE;
294  }
295 
296  STB_OSMutexUnlock(msg_mutex);
297  }
298 
299  FUNCTION_FINISH(STB_PVRDeleteMessage);
300 
301  return(retval);
302 }
303 
304 /*!**************************************************************************
305  * @brief Returns an allocated array of the message handles and number of
306  * messages currently in the list
307  * @param handle_array - pointer to an array that's allocated by the function
308  * and is filled with the handles of all messages
309  * @return Number of messages
310  ****************************************************************************/
311 U16BIT STB_PVRGetMessages(U16BIT **handle_array)
312 {
313  U16BIT i, num_messages;
314  S_PVR_MESSAGE *msg_ptr;
315 
316  FUNCTION_START(STB_PVRGetMessages);
317 
318  if (msg_mutex != NULL)
319  {
320  STB_OSMutexLock(msg_mutex);
321 
322  num_messages = STB_PVRGetNumMessages();
323  if (num_messages > 0)
324  {
325  *handle_array = (U16BIT *)STB_GetMemory(num_messages * sizeof(U16BIT));
326  if (*handle_array != NULL)
327  {
328  msg_ptr = msg_head;
329  for (i = 0; (i < num_messages) && (msg_ptr != NULL); i++)
330  {
331  (*handle_array)[i] = msg_ptr->handle;
332  msg_ptr = msg_ptr->next;
333  }
334  }
335  else
336  {
337  num_messages = 0;
338  }
339  }
340 
341  STB_OSMutexUnlock(msg_mutex);
342  }
343  else
344  {
345  num_messages = 0;
346  }
347 
348  FUNCTION_FINISH(STB_PVRGetMessages);
349 
350  return(num_messages);
351 }
352 
353 /*!**************************************************************************
354  * @brief Gets date/time for the message with the given handle
355  * @param handle - message handle
356  * @param date - pointer to value in which to return the date of the message
357  * @param hour - pointer to value in which to return the hour of the message
358  * @param min - pointer to value in which to return the min of the message
359  * @return TRUE if the message is found, FALSE otherwise
360  ****************************************************************************/
361 BOOLEAN STB_PVRGetMessageInfo(U16BIT handle, U16BIT *date, U8BIT *hour, U8BIT *min)
362 {
363  BOOLEAN retval;
364  S_PVR_MESSAGE *msg_ptr;
365 
366  FUNCTION_START(STB_PVRGetMessageInfo);
367 
368  retval = FALSE;
369 
370  if (msg_mutex != NULL)
371  {
372  STB_OSMutexLock(msg_mutex);
373 
374  msg_ptr = GetHandleStatus(handle);
375  if (msg_ptr != NULL)
376  {
377  *date = msg_ptr->date;
378  *hour = msg_ptr->hour;
379  *min = msg_ptr->min;
380 
381  retval = TRUE;
382  }
383 
384  STB_OSMutexUnlock(msg_mutex);
385  }
386 
387  FUNCTION_FINISH(STB_PVRGetMessageInfo);
388 
389  return(retval);
390 }
391 
392 /*!**************************************************************************
393  * @brief Returns a pointer to the text for the message with the given handle
394  * @param handle - message handle
395  * @return pointer to message text (this is not a copy and shouldn't be deleted)
396  ****************************************************************************/
397 U8BIT* STB_PVRGetMessageText(U16BIT handle)
398 {
399  U8BIT *text_ptr;
400  S_PVR_MESSAGE *msg_ptr;
401 
402  FUNCTION_START(STB_PVRGetMessageText);
403 
404  text_ptr = NULL;
405 
406  if (msg_mutex != NULL)
407  {
408  STB_OSMutexLock(msg_mutex);
409 
410  msg_ptr = GetHandleStatus(handle);
411  if (msg_ptr != NULL)
412  {
413  text_ptr = &msg_ptr->message;
414  }
415 
416  STB_OSMutexUnlock(msg_mutex);
417  }
418 
419  FUNCTION_FINISH(STB_PVRGetMessageText);
420 
421  return(text_ptr);
422 }
423 
424 /*!**************************************************************************
425  * @brief Gets a handle
426  * @param handle - message handle
427  * @return pointer to message entry
428  ****************************************************************************/
429 static S_PVR_MESSAGE* GetHandleStatus(U16BIT handle)
430 {
431  S_PVR_MESSAGE *msg_ptr;
432 
433  FUNCTION_START(GetHandleStatus);
434 
435  msg_ptr = msg_head;
436  while (msg_ptr != NULL)
437  {
438  /* Go through each handle to find match */
439  if (msg_ptr->handle == handle)
440  {
441  break;
442  }
443 
444  msg_ptr = msg_ptr->next;
445  }
446 
447  FUNCTION_FINISH(GetHandleStatus);
448 
449  return(msg_ptr);
450 }
451 
void * STB_GetMemory(U32BIT bytes)
Attempts to allocate memory from the heap.
Definition: stbheap.c:221
BOOLEAN STB_PVRDeleteMessage(U16BIT handle)
Deletes a message from the list.
Definition: stbpvrmsg.c:242
BOOLEAN STB_PVRInitialiseMessages(void)
Initialises file that provides messages for recording er.
Definition: stbpvrmsg.c:85
U16BIT STB_PVRGetNumMessages(void)
Returns the number of messages.
Definition: stbpvrmsg.c:209
void STB_OSMutexUnlock(void *mutex)
Unlock a mutex (a.k.a. &#39;leave&#39;, &#39;signal&#39; or &#39;release&#39;)
U8BIT * STB_PVRGetMessageText(U16BIT handle)
Returns a pointer to the text for the message with the given handle.
Definition: stbpvrmsg.c:397
void STB_FreeMemory(void *addr)
Releases previously allocated heap memory.
Definition: stbheap.c:336
Debug functions header file.
void STB_OSMutexLock(void *mutex)
Lock a mutex (a.k.a. &#39;enter&#39;, &#39;wait&#39; or &#39;get&#39;).
Header file - macros and function prototypes for public use.
void STB_GCGetGMTDateTime(U16BIT *code, U8BIT *hour, U8BIT *min, U8BIT *secs)
Reads the current GMT date code and time.
Definition: stbgc.c:1372
Header for STB unicode string handling routines.
BOOLEAN STB_PVRAddMessage(U8BIT *message)
Adds a message to the end of the list of existing messages.
Definition: stbpvrmsg.c:111
U32BIT STB_GetNumBytesInString(U8BIT *string_ptr)
Determines the no of bytes of the given string.
Definition: stbuni.c:311
Header file - Function prototypes for operating system.
System Wide Global Technical Data Type Definitions.
BOOLEAN STB_PVRCheckMessages(void)
Check for new messages and clear the new message flag.
Definition: stbpvrmsg.c:191
Header file - Function prototypes for heap memory.
void * STB_OSCreateMutex(void)
Create a mutex.
U16BIT STB_PVRGetMessages(U16BIT **handle_array)
Returns an allocated array of the message handles and number of messages currently in the list...
Definition: stbpvrmsg.c:311
BOOLEAN STB_PVRGetMessageInfo(U16BIT handle, U16BIT *date, U8BIT *hour, U8BIT *min)
Gets date/time for the message with the given handle.
Definition: stbpvrmsg.c:361