DVBCore  20.3.0
DVBCore Documentation
stbgc.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 <stdio.h>
29 #include <time.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 // third party header files
34 
35 // Ocean Blue Software header files
36 
37 #include <techtype.h>
38 #include <dbgfuncs.h>
39 
40 #include "stbdpc.h"
41 #include "stbgc.h"
42 #include "stbhwos.h"
43 #include "stbhwfp.h"
44 #include "stbhwav.h"
45 #include "version.h"
46 
47 #ifdef COMMON_INTERFACE
48  #include "stbhwci.h" // CI (for pre-standby shutdown)
49 #endif
50 
51 //---constant definitions for this file----------------------------------------
52 
53 #define LCODE_STRING_MAX 4
54 #define TIME_STRING_MAX 9
55 #define DATE_STRING_MAX 11
56 #define CLOCK_STRING_MAX 11
57 #define SERIAL_NO_MAX 50
58 #define VERSION_NO_MAX 9
59 
60 
61 
62 //---local typedef structs for this file---------------------------------------
63 
64 typedef struct
65 {
66  U32BIT lang_code;
67  BOOLEAN audio_signal;
68  BOOLEAN time_set;
69  BOOLEAN use_broadcast_time;
70  U16BIT date_code;
71  U8BIT time_hour;
72  U8BIT time_min;
73  U8BIT time_secs;
74  U16BIT offset_change_date;
75  U8BIT offset_change_hour;
76  U8BIT offset_change_min;
77  U8BIT offset_change_secs;
78  S8BIT offset_old_hour;
79  S8BIT offset_old_min;
80  S8BIT offset_new_hour;
81  S8BIT offset_new_min;
82  S8BIT rtcoff_hour;
83  S8BIT rtcoff_min;
84 } GC_STATUS;
85 
86 //---local (static) variable declarations for this file------------------------
87 // (internal variables declared static to make them local)
88 
89 static GC_STATUS general_status;
90 static void *time_mutex;
91 static U8BIT lcode_string[LCODE_STRING_MAX];
92 static U8BIT time_string[TIME_STRING_MAX];
93 static U8BIT date_string[DATE_STRING_MAX];
94 static U8BIT clock_string[CLOCK_STRING_MAX];
95 static U8BIT serial_no[SERIAL_NO_MAX];
96 static U8BIT version_no[VERSION_NO_MAX];
97 
98 //---local function prototypes for this file-----------------------------------
99 // (internal functions declared static to make them local)
100 
101 static void ExtractUTCDate(U16BIT code, U8BIT *wday, U8BIT *day, U8BIT *month, U16BIT *year);
102 static void AddTime(U16BIT code1, U8BIT hour1, U8BIT min1, U8BIT secs1,
103  S16BIT ohour, S16BIT omin, S16BIT osecs,
104  U16BIT *code, U8BIT *hour, U8BIT *min, U8BIT *secs);
105 static BOOLEAN FutureTime(U16BIT code1, U8BIT hour1, U8BIT min1, U8BIT secs1,
106  U16BIT code2, U8BIT hour2, U8BIT min2, U8BIT secs2);
107 static void ConvertTimestamp(U32BIT timestamp, U16BIT *date, U8BIT *hour, U8BIT *mins, U8BIT *secs);
108 static U32BIT ConvertToTimestamp(U16BIT code, U8BIT hour, U8BIT min, U8BIT sec);
109 static void SetCTime( void );
110 static void GetCTime( void );
111 static void SetOffset(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs,
112  S8BIT ohour1, S8BIT omin1, S8BIT ohour2, S8BIT omin2);
113 static void GetOffset(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs,
114  S8BIT *ohour, S8BIT *omin);
115 
116 //---local function definitions------------------------------------------------
117 
133 static void ExtractUTCDate(U16BIT code, U8BIT *wday, U8BIT *day, U8BIT *month, U16BIT *year)
134 {
135  U32BIT t1, t2, t3, t4;
136 
137  FUNCTION_START(ExtractUTCDate);
138 
139  if (code != 0)
140  {
141  t1 = (U32BIT)code * 10L; // MJD with 1 d.p.
142 
143  t2 = t1 - 150782L; // subtract 15078.2
144  t2 *= 10L; // divide by 365.25
145  t2 /= 36525L; // t2 is now equivalent to y'
146  *year = (U16BIT)t2 + 1900;
147 
148  t3 = t1 - 149561L; // as before, MJD minus 14956.1
149 
150  t2 *= 36525L; // to be int( y' * 365.35)
151  t2 /= 100L;
152 
153  t3 -= t2 * 10L; // subract int( y' * 365.35) with 1 d.p.
154  t3 *= 1000L; // divide by 30.6001
155  t3 /= 306001L; // t3 is now equivalent to m'
156 
157  t4 = t3;
158  *month = (U8BIT)t3 - 1;
159 
160  t3 *= 306001L; // to be int( m' * 30.6001)
161  t3 /= 10000L;
162  *day = (U8BIT)((U32BIT)code - 14956UL - t2 - t3);
163 
164  if (t4 > 13)
165  {
166  *year = *year + 1;
167  *month = *month - 12;
168  }
169 
170  *wday = (U8BIT)((((U32BIT)code + 2L) % 7) + 1);
171  }
172  else
173  {
174  *wday = 0;
175  *day = 0;
176  *month = 0;
177  *year = 0;
178  }
179 
180  FUNCTION_FINISH(ExtractUTCDate);
181 }
182 
204 static void AddTime(U16BIT code1, U8BIT hour1, U8BIT min1, U8BIT secs1,
205  S16BIT ohour, S16BIT omin, S16BIT osecs,
206  U16BIT *code, U8BIT *hour, U8BIT *min, U8BIT *secs)
207 {
208  S32BIT rcode, rhour, rmin, rsecs;
209 
210  FUNCTION_START(AddTime);
211 
212  rcode = (S32BIT)code1;
213  rhour = ((S32BIT)hour1 + (S32BIT)ohour);
214  rmin = ((S32BIT)min1 + (S32BIT)omin);
215  rsecs = ((S32BIT)secs1 + (S32BIT)osecs);
216 
217  while (rsecs < 0)
218  {
219  rsecs += 60;
220  rmin -= 1;
221  }
222  while (rsecs > 59)
223  {
224  rsecs -= 60;
225  rmin += 1;
226  }
227 
228  while (rmin < 0)
229  {
230  rmin += 60;
231  rhour -= 1;
232  }
233  while (rmin > 59)
234  {
235  rmin -= 60;
236  rhour += 1;
237  }
238 
239  while (rhour < 0)
240  {
241  rhour += 24;
242  rcode -= 1;
243  }
244  while (rhour > 23)
245  {
246  rhour -= 24;
247  rcode += 1;
248  }
249 
250  if (code1 != 0)
251  *code = (U16BIT)rcode;
252  else
253  *code = 0;
254  *hour = (U8BIT)rhour;
255  *min = (U8BIT)rmin;
256  *secs = (U8BIT)rsecs;
257 
258  FUNCTION_FINISH(AddTime);
259 }
260 
279 static BOOLEAN FutureTime(U16BIT code1, U8BIT hour1, U8BIT min1, U8BIT secs1,
280  U16BIT code2, U8BIT hour2, U8BIT min2, U8BIT secs2)
281 {
282  BOOLEAN ret_val = FALSE;
283 
284  FUNCTION_START(FutureTime);
285 
286  if (code1 > code2)
287  {
288  ret_val = TRUE;
289  }
290  if (code1 == code2)
291  {
292  if (hour1 > hour2)
293  {
294  ret_val = TRUE;
295  }
296  if (hour1 == hour2)
297  {
298  if (min1 > min2)
299  {
300  ret_val = TRUE;
301  }
302  if (min1 == min2)
303  {
304  if (secs1 >= secs2)
305  {
306  ret_val = TRUE;
307  }
308  }
309  }
310  }
311 
312  FUNCTION_FINISH(FutureTime);
313 
314  return(ret_val);
315 }
316 
328 static void SetCTime( void )
329 {
330  U32BIT now;
331  U16BIT ecode;
332  U8BIT ehour, emin, esecs;
333 
334  FUNCTION_START(SetCTime);
335 
336  now = ConvertToTimestamp(general_status.date_code, general_status.time_hour,
337  general_status.time_min, general_status.time_secs);
338 
339  if (general_status.use_broadcast_time)
340  {
341  /* Set the GMT to the hardware */
342  STB_OSSetClockGMT(now);
343  }
344 
345  // calc local time
346  GetOffset(general_status.date_code, general_status.time_hour,
347  general_status.time_min, general_status.time_secs,
348  &general_status.rtcoff_hour, &general_status.rtcoff_min);
349 
350  AddTime(general_status.date_code, general_status.time_hour, general_status.time_min, general_status.time_secs,
351  (S16BIT)general_status.rtcoff_hour, (S16BIT)general_status.rtcoff_min, 0,
352  &ecode, &ehour, &emin, &esecs);
353 
354  now = ConvertToTimestamp(ecode, ehour, emin, esecs);
355 
356  // Notify the hardware layer of the current local time
357  STB_OSSetClockRTC(now);
358 
359  FUNCTION_FINISH(SetCTime);
360 }
361 
370 static void ConvertTimestamp(U32BIT timestamp, U16BIT *date, U8BIT *hour, U8BIT *mins, U8BIT *secs)
371 {
372  time_t t;
373  struct tm *tm_time;
374  U32BIT date_code;
375 
376  FUNCTION_START(ConvertTimestamp);
377 
378  t = timestamp;
379  tm_time = gmtime(&t);
380 
381  // Convert the year, month and date coefficients of 'C' compatible UTC format (days since 1970 00:00:00)
382  // back into SI-specified MJD (modified Julian dateline) (days since 1st Jan 1900).
383  // (this algorithm is derived from DVB (SI) specs - ETSI EN 300 468, Annex C)
384  date_code = 14956L + (U32BIT)tm_time->tm_mday;
385  // NB - algorithm is slightly different for January and Feburary, due to leap year synchronisation.
386  if (tm_time->tm_mon < 2)
387  {
388  date_code += (((U32BIT)tm_time->tm_year - 1L) * 36525L) / 100L;
389  date_code += (((U32BIT)tm_time->tm_mon + 14L) * 306001L) / 10000L;
390  }
391  else
392  {
393  date_code += ((U32BIT)tm_time->tm_year * 36525L) / 100L;
394  date_code += (((U32BIT)tm_time->tm_mon + 2L) * 306001L) / 10000L;
395  }
396 
397  *date = (U16BIT)date_code;
398  *hour = (U8BIT)tm_time->tm_hour;
399  *mins = (U8BIT)tm_time->tm_min;
400  *secs = (U8BIT)tm_time->tm_sec;
401 
402  FUNCTION_FINISH(ConvertTimestamp);
403 }
404 
405 
417 static U32BIT ConvertToTimestamp(U16BIT code, U8BIT hour, U8BIT min, U8BIT sec)
418 {
419  U32BIT timestamp;
420 
421  FUNCTION_START(ConvertToTimestamp);
422 
423  timestamp = (U32BIT)code;
424  if (timestamp >= 40587)
425  timestamp -= 40587;
426  else
427  timestamp = 0;
428 
429  // Convert date code (in days) and time into seconds
430  timestamp *= 86400L;
431  timestamp += (U32BIT)hour * 3600L;
432  timestamp += (U32BIT)min * 60L;
433  timestamp += (U32BIT)sec;
434 
435  FUNCTION_FINISH(ConvertToTimestamp);
436 
437  return timestamp;
438 }
439 
451 static void GetCTime( void )
452 {
453  U32BIT t;
454  U16BIT date;
455  U8BIT hour, mins, secs;
456 
457  FUNCTION_START(GetCTime);
458 
459  // Read the current time from the hardware
460  if (general_status.use_broadcast_time)
461  {
462  t = STB_OSGetClockRTC();
463  }
464  else
465  {
466  t = STB_OSGetClockGMT();
467  }
468 
469  // If clock has been set, convert RTC values to correct values
470  if (general_status.time_set == TRUE)
471  {
472  ConvertTimestamp(t, &date, &hour, &mins, &secs);
473 
474  #ifndef USE_SYSTEM_CLOCK
475  AddTime(date, hour, mins, secs,
476  (S16BIT)(general_status.rtcoff_hour * -1), (S16BIT)(general_status.rtcoff_min * -1), 0,
477  &general_status.date_code, &general_status.time_hour, &general_status.time_min, &general_status.time_secs);
478  #else
479  general_status.time_hour = hour;
480  general_status.time_min = mins;
481  general_status.time_secs = secs;
482  general_status.date_code = date;
483  #endif //USE_SYSTEM_CLOCK
484  }
485  else // Clock has not yet been set, so store as zero
486  {
487  general_status.time_hour = 0;
488  general_status.time_min = 0;
489  general_status.time_secs = 0;
490  general_status.date_code = 0;
491  }
492 
493  FUNCTION_FINISH(GetCTime);
494 }
495 
514 static void SetOffset(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs,
515  S8BIT ohour1, S8BIT omin1, S8BIT ohour2, S8BIT omin2)
516 {
517  FUNCTION_START(SetOffset);
518 
519  general_status.offset_change_date = code;
520  general_status.offset_change_hour = hour;
521  general_status.offset_change_min = min;
522  general_status.offset_change_secs = secs;
523 
524  general_status.offset_old_hour = ohour1;
525  general_status.offset_old_min = omin1;
526  general_status.offset_new_hour = ohour2;
527  general_status.offset_new_min = omin2;
528 
529  FUNCTION_FINISH(SetOffset);
530 }
531 
548 static void GetOffset(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs,
549  S8BIT *ohour, S8BIT *omin)
550 {
551  FUNCTION_START(GetOffset);
552 
553  if (FutureTime(code, hour, min, secs,
554  general_status.offset_change_date, general_status.offset_change_hour,
555  general_status.offset_change_min, general_status.offset_change_secs))
556  {
557  *ohour = general_status.offset_new_hour;
558  *omin = general_status.offset_new_min;
559  }
560  else
561  {
562  *ohour = general_status.offset_old_hour;
563  *omin = general_status.offset_old_min;
564  }
565 
566  FUNCTION_FINISH(GetOffset);
567 }
568 
569 //---global function definitions-----------------------------------------------
570 
583 {
584  FUNCTION_START(STB_GCInitialise);
585 
586  general_status.lang_code = 0;
587  general_status.audio_signal = FALSE;
588  general_status.time_set = FALSE;
589  general_status.use_broadcast_time = TRUE;
590  general_status.date_code = 0;
591  general_status.time_hour = 0;
592  general_status.time_min = 0;
593  general_status.time_secs = 0;
594  general_status.offset_change_date = 0;
595  general_status.offset_change_hour = 0;
596  general_status.offset_change_min = 0;
597  general_status.offset_change_secs = 0;
598  general_status.offset_old_hour = 0;
599  general_status.offset_old_min = 0;
600  general_status.offset_new_hour = 0;
601  general_status.offset_new_min = 0;
602  general_status.rtcoff_hour = 0;
603  general_status.rtcoff_min = 0;
604 
605  time_mutex = STB_OSCreateMutex();
606 
607  FUNCTION_FINISH(STB_GCInitialise);
608 }
609 
621 void STB_GCSetSearchLangCode(U32BIT lang)
622 {
623  FUNCTION_START(STB_GCSetSearchLangCode);
624 
625  general_status.lang_code = lang;
626 
627  FUNCTION_FINISH(STB_GCSetSearchLangCode);
628 }
629 
642 {
643  U32BIT ret_val;
644 
645  FUNCTION_START(STB_GCGetSearchLangCode);
646 
647  ret_val = general_status.lang_code;
648 
649  FUNCTION_FINISH(STB_GCGetSearchLangCode);
650 
651  return(ret_val);
652 }
653 
665 U8BIT* STB_GCGetLangCodeString(U32BIT lang)
666 {
667  FUNCTION_START(STB_GCGetLangCodeString);
668 
669  lcode_string[0] = (U8BIT)((lang >> 16) & 0x000000ff);
670  lcode_string[1] = (U8BIT)((lang >> 8) & 0x000000ff);
671  lcode_string[2] = (U8BIT)((lang) & 0x000000ff);
672  lcode_string[3] = '\0';
673 
674  FUNCTION_FINISH(STB_GCGetLangCodeString);
675 
676  return(lcode_string);
677 }
678 
690 void STB_GCSetCIStandby(BOOLEAN standby)
691 {
692  FUNCTION_START(STB_GCSetCIStandby);
693 
694 #ifdef COMMON_INTERFACE
695  if (standby == TRUE)
696  {
697  STB_CIStandbyOn();
698  }
699  else
700  {
701  STB_CIStandbyOff();
702  }
703 #else
704  // fool compiler into thinking these parameters are used - stops unnecessary warning
705  USE_UNWANTED_PARAM(standby);
706 #endif
707 
708  FUNCTION_FINISH(STB_GCSetCIStandby);
709 }
710 
724 {
725  FUNCTION_START(STB_GCGetFullSerialString);
726 
727  snprintf((char *)serial_no, SERIAL_NO_MAX, "%04x.%04x.%lu", STB_HWGetHwId(), STB_HWGetCustomerId(),
728  (unsigned long)STB_HWGetBoxSerialNumber());
729 
730  FUNCTION_FINISH(STB_GCGetFullSerialString);
731 
732  return(serial_no);
733 }
734 
747 {
748  FUNCTION_START(STB_GCGetVersionNumberString);
749 
750  snprintf((char *)version_no, VERSION_NO_MAX, "%02d.%02d.%02d", lib_ver_number[0], lib_ver_number[1], lib_ver_number[2]);
751 
752  FUNCTION_FINISH(STB_GCGetVersionNumberString);
753 
754  return(version_no);
755 }
756 
768 void STB_GCSetAudioSignal(BOOLEAN state)
769 {
770  FUNCTION_START(STB_GCSetAudioSignal);
771 
772  // jrg - add HI call in here
773  if (state == TRUE)
774  {
775  }
776  else
777  {
778  }
779 
780  general_status.audio_signal = state;
781 
782  FUNCTION_FINISH(STB_GCSetAudioSignal);
783 }
784 
796 BOOLEAN STB_GCGetAudioSignal(void)
797 {
798  BOOLEAN ret_val;
799 
800  FUNCTION_START(STB_GCGetAudioSignal);
801 
802  ret_val = general_status.audio_signal;
803 
804  FUNCTION_FINISH(STB_GCGetAudioSignal);
805 
806  return(ret_val);
807 }
808 
822 void STB_GCSetLocalTimeOffset(U8BIT ohour, U8BIT omin, BOOLEAN neg)
823 {
824  S8BIT oh, om;
825 
826  FUNCTION_START(STB_GCSetLocalTimeOffset);
827 
828  ASSERT(ohour <= 23);
829  ASSERT(omin <= 59);
830 
831  STB_OSMutexLock(time_mutex);
832 
833  // Update the local store from the hardware RTC
834  GetCTime();
835 
836  if (neg)
837  {
838  oh = ((S8BIT)ohour * -1);
839  om = ((S8BIT)omin * -1);
840  }
841  else
842  {
843  oh = (S8BIT)ohour;
844  om = (S8BIT)omin;
845  }
846  SetOffset(0, 0, 0, 0, oh, om, oh, om);
847 
848  // Ensure 'C' format RTC is updated
849  SetCTime();
850 
851  STB_OSMutexUnlock(time_mutex);
852 
853  FUNCTION_FINISH(STB_GCSetLocalTimeOffset);
854 }
855 
869 void STB_GCGetLocalTimeOffset(U8BIT *ohour, U8BIT *omin, BOOLEAN *neg)
870 {
871  S8BIT oh, om;
872 
873  FUNCTION_START(STB_GCGetLocalTimeOffset);
874 
875  ASSERT(ohour != NULL);
876  ASSERT(omin != NULL);
877  ASSERT(neg != NULL);
878 
879  STB_OSMutexLock(time_mutex);
880 
881  // Update the local store from the hardware RTC
882  GetCTime();
883 
884  // Get correct offset for now
885  GetOffset(general_status.date_code, general_status.time_hour,
886  general_status.time_min, general_status.time_secs,
887  &oh, &om);
888 
889  STB_OSMutexUnlock(time_mutex);
890 
891  // return offset
892  *ohour = (U8BIT)abs(oh);
893  *omin = (U8BIT)abs(om);
894  if ((oh < 0) || (om < 0))
895  {
896  *neg = TRUE;
897  }
898  else
899  {
900  *neg = FALSE;
901  }
902 
903  FUNCTION_FINISH(STB_GCGetLocalTimeOffset);
904 }
905 
925 void STB_GCSetLocalTimeChange(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs,
926  U8BIT ohour1, U8BIT omin1, U8BIT ohour2, U8BIT omin2, BOOLEAN neg)
927 {
928  S8BIT oh1, om1, oh2, om2;
929 
930  FUNCTION_START(STB_GCSetLocalTimeChange);
931 
932  ASSERT(hour <= 23);
933  ASSERT(min <= 59);
934  ASSERT(secs <= 59);
935  ASSERT(ohour1 <= 23);
936  ASSERT(omin1 <= 59);
937  ASSERT(ohour2 <= 23);
938  ASSERT(omin2 <= 59);
939 
940  STB_OSMutexLock(time_mutex);
941 
942  // Update the local store from the hardware RTC
943  GetCTime();
944 
945  if (neg)
946  {
947  oh1 = ((S8BIT)ohour1 * -1);
948  om1 = ((S8BIT)omin1 * -1);
949  oh2 = ((S8BIT)ohour2 * -1);
950  om2 = ((S8BIT)omin2 * -1);
951  }
952  else
953  {
954  oh1 = (S8BIT)ohour1;
955  om1 = (S8BIT)omin1;
956  oh2 = (S8BIT)ohour2;
957  om2 = (S8BIT)omin2;
958  }
959  SetOffset(code, hour, min, secs, oh1, om1, oh2, om2);
960 
961  // Ensure 'C' format RTC is updated
962  SetCTime();
963 
964  STB_OSMutexUnlock(time_mutex);
965 
966  FUNCTION_FINISH(STB_GCSetLocalTimeChange);
967 }
968 
986 void STB_GCGetLocalTimeChange(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs,
987  U8BIT *ohour, U8BIT *omin, BOOLEAN *neg)
988 {
989  S8BIT oh, om;
990 
991  FUNCTION_START(STB_GCGetLocalTimeChange);
992 
993  ASSERT(hour <= 23);
994  ASSERT(min <= 59);
995  ASSERT(secs <= 59);
996  ASSERT(ohour != NULL);
997  ASSERT(omin != NULL);
998  ASSERT(neg != NULL);
999 
1000  USE_UNWANTED_PARAM(code);
1001  USE_UNWANTED_PARAM(hour);
1002  USE_UNWANTED_PARAM(min);
1003  USE_UNWANTED_PARAM(secs);
1004 
1005  STB_OSMutexLock(time_mutex);
1006 
1007  // Get correct offset for now
1008  GetCTime();
1009  GetOffset(general_status.date_code, general_status.time_hour,
1010  general_status.time_min, general_status.time_secs, &oh, &om);
1011 
1012  STB_OSMutexUnlock(time_mutex);
1013 
1014  // return offset
1015  *ohour = (U8BIT)abs(oh);
1016  *omin = (U8BIT)abs(om);
1017  if ((oh < 0) || (om < 0))
1018  {
1019  *neg = TRUE;
1020  }
1021  else
1022  {
1023  *neg = FALSE;
1024  }
1025 
1026  FUNCTION_FINISH(STB_GCGetLocalTimeChange);
1027 }
1028 
1042 void STB_GCSetGMTTime(U8BIT hour, U8BIT min, U8BIT secs)
1043 {
1044  FUNCTION_START(STB_GCSetGMTTime);
1045 
1046  ASSERT(hour <= 23);
1047  ASSERT(min <= 59);
1048  ASSERT(secs <= 59);
1049 
1050  STB_OSMutexLock(time_mutex);
1051 
1052  // Update the local store from the hardware RTC
1053  GetCTime();
1054 
1055  general_status.time_hour = hour;
1056  general_status.time_min = min;
1057  general_status.time_secs = secs;
1058 
1059  // Ensure 'C' format RTC is updated
1060  SetCTime();
1061 
1062  // Record that the time has now been set
1063  general_status.time_set = TRUE;
1064 
1065  STB_OSMutexUnlock(time_mutex);
1066 
1067  FUNCTION_FINISH(STB_GCSetGMTTime);
1068 }
1069 
1081 U8BIT STB_GCGetGMTHour(void)
1082 {
1083  U8BIT ret_val;
1084 
1085  FUNCTION_START(STB_GCGetGMTHour);
1086 
1087  STB_OSMutexLock(time_mutex);
1088 
1089  // Update the local store from the hardware RTC
1090  GetCTime();
1091 
1092  ret_val = general_status.time_hour;
1093 
1094  STB_OSMutexUnlock(time_mutex);
1095 
1096  FUNCTION_FINISH(STB_GCGetGMTHour);
1097 
1098  return(ret_val);
1099 }
1100 
1112 U8BIT STB_GCGetGMTMin(void)
1113 {
1114  U8BIT ret_val;
1115 
1116  FUNCTION_START(STB_GCGetGMTMin);
1117 
1118  STB_OSMutexLock(time_mutex);
1119 
1120  // Update the local store from the hardware RTC
1121  GetCTime();
1122 
1123  ret_val = general_status.time_min;
1124 
1125  STB_OSMutexUnlock(time_mutex);
1126 
1127  FUNCTION_FINISH(STB_GCGetGMTMin);
1128 
1129  return(ret_val);
1130 }
1131 
1143 U8BIT STB_GCGetGMTSecs(void)
1144 {
1145  U8BIT ret_val;
1146 
1147  FUNCTION_START(STB_GCGetGMTSecs);
1148 
1149  STB_OSMutexLock(time_mutex);
1150 
1151  // Update the local store from the hardware RTC
1152  GetCTime();
1153 
1154  ret_val = general_status.time_secs;
1155 
1156  STB_OSMutexUnlock(time_mutex);
1157 
1158  FUNCTION_FINISH(STB_GCGetGMTSecs);
1159 
1160  return(ret_val);
1161 }
1162 
1174 void STB_GCSetGMTDate(U16BIT code)
1175 {
1176  FUNCTION_START(STB_GCSetGMTDate);
1177 
1178  STB_OSMutexLock(time_mutex);
1179 
1180  // Update the local store from the hardware RTC
1181  GetCTime();
1182 
1183  general_status.date_code = code;
1184 
1185  // Ensure 'C' format RTC is updated
1186  SetCTime();
1187 
1188  // Record that the time has now been set
1189  general_status.time_set = TRUE;
1190 
1191  STB_OSMutexUnlock(time_mutex);
1192 
1193  FUNCTION_FINISH(STB_GCSetGMTDate);
1194 }
1195 
1207 U16BIT STB_GCGetGMTDate(void)
1208 {
1209  U16BIT ret_val;
1210 
1211  FUNCTION_START(STB_GCGetGMTDate);
1212 
1213  STB_OSMutexLock(time_mutex);
1214 
1215  // Update the local store from the hardware RTC
1216  GetCTime();
1217 
1218  ret_val = general_status.date_code;
1219 
1220  STB_OSMutexUnlock(time_mutex);
1221 
1222  FUNCTION_FINISH(STB_GCGetGMTDate);
1223 
1224  return(ret_val);
1225 }
1226 
1238 E_STB_GC_WEEKDAY STB_GCGetGMTWeekDay(void)
1239 {
1240  U8BIT wday, day, month;
1241  U16BIT year;
1242  E_STB_GC_WEEKDAY ret_val;
1243 
1244  FUNCTION_START(STB_GCGetGMTWeekDay);
1245 
1246  STB_OSMutexLock(time_mutex);
1247 
1248  // Update the local store from the hardware RTC
1249  GetCTime();
1250 
1251  ExtractUTCDate(general_status.date_code, &wday, &day, &month, &year);
1252 
1253  STB_OSMutexUnlock(time_mutex);
1254 
1255  ret_val = (E_STB_GC_WEEKDAY)wday;
1256 
1257  FUNCTION_FINISH(STB_GCGetGMTWeekDay);
1258 
1259  return(ret_val);
1260 }
1261 
1273 U8BIT STB_GCGetGMTDay(void)
1274 {
1275  U8BIT wday, day, month;
1276  U16BIT year;
1277 
1278  FUNCTION_START(STB_GCGetGMTDay);
1279 
1280  STB_OSMutexLock(time_mutex);
1281 
1282  // Update the local store from the hardware RTC
1283  GetCTime();
1284 
1285  ExtractUTCDate(general_status.date_code, &wday, &day, &month, &year);
1286 
1287  STB_OSMutexUnlock(time_mutex);
1288 
1289  FUNCTION_FINISH(STB_GCGetGMTDay);
1290 
1291  return(day);
1292 }
1293 
1306 {
1307  U8BIT wday, day, month;
1308  U16BIT year;
1309 
1310  FUNCTION_START(STB_GCGetGMTMonth);
1311 
1312  STB_OSMutexLock(time_mutex);
1313 
1314  // Update the local store from the hardware RTC
1315  GetCTime();
1316 
1317  ExtractUTCDate(general_status.date_code, &wday, &day, &month, &year);
1318 
1319  STB_OSMutexUnlock(time_mutex);
1320 
1321  FUNCTION_FINISH(STB_GCGetGMTMonth);
1322 
1323  return(month);
1324 }
1325 
1337 U16BIT STB_GCGetGMTYear(void)
1338 {
1339  U8BIT wday, day, month;
1340  U16BIT year;
1341 
1342  FUNCTION_START(STB_GCGetGMTYear);
1343 
1344  STB_OSMutexLock(time_mutex);
1345 
1346  // Update the local store from the hardware RTC
1347  GetCTime();
1348 
1349  ExtractUTCDate(general_status.date_code, &wday, &day, &month, &year);
1350 
1351  STB_OSMutexUnlock(time_mutex);
1352 
1353  FUNCTION_FINISH(STB_GCGetGMTYear);
1354 
1355  return(year);
1356 }
1357 
1372 void STB_GCGetGMTDateTime(U16BIT *code, U8BIT *hour, U8BIT *min, U8BIT *secs)
1373 {
1374  FUNCTION_START(STB_GCGetGMTDateTime);
1375 
1376  ASSERT(code != NULL);
1377  ASSERT(hour != NULL);
1378  ASSERT(min != NULL);
1379  ASSERT(secs != NULL);
1380 
1381  STB_OSMutexLock(time_mutex);
1382 
1383  // Update the local store from the hardware RTC
1384  GetCTime();
1385 
1386  *code = general_status.date_code;
1387  *hour = general_status.time_hour;
1388  *min = general_status.time_min;
1389  *secs = general_status.time_secs;
1390 
1391  STB_OSMutexUnlock(time_mutex);
1392 
1393  FUNCTION_FINISH(STB_GCGetGMTDateTime);
1394 }
1395 
1411 BOOLEAN STB_GCIsFutureDateTime(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs)
1412 {
1413  BOOLEAN ret_val;
1414 
1415  FUNCTION_START(STB_GCIsFutureDateTime);
1416 
1417  ASSERT(hour <= 23);
1418  ASSERT(min <= 59);
1419  ASSERT(secs <= 59);
1420 
1421  STB_OSMutexLock(time_mutex);
1422 
1423  // Update the local store from the hardware RTC
1424  GetCTime();
1425 
1426  ret_val = !FutureTime(general_status.date_code, general_status.time_hour,
1427  general_status.time_min, general_status.time_secs,
1428  code, hour, min, secs);
1429 
1430  STB_OSMutexUnlock(time_mutex);
1431 
1432  FUNCTION_FINISH(STB_GCIsFutureDateTime);
1433 
1434  return(ret_val);
1435 }
1436 
1448 E_STB_GC_WEEKDAY STB_GCGetDateWeekDay(U16BIT code)
1449 {
1450  E_STB_GC_WEEKDAY ret_val;
1451 
1452  FUNCTION_START(STB_GCGetDateWeekDay);
1453 
1454  ret_val = (E_STB_GC_WEEKDAY)(((code + 2L) % 7) + 1);
1455 
1456  FUNCTION_FINISH(STB_GCGetDateWeekDay);
1457 
1458  return(ret_val);
1459 }
1460 
1472 BOOLEAN STB_GCIsDateDayWeek(U16BIT code)
1473 {
1474  E_STB_GC_WEEKDAY wday;
1475  BOOLEAN ret_val;
1476 
1477  FUNCTION_START(STB_GCIsDateDayWeek);
1478 
1479  wday = STB_GCGetDateWeekDay(code);
1480  if ((wday >= WEEKDAY_MONDAY) && (wday <= WEEKDAY_FRIDAY))
1481  ret_val = TRUE;
1482  else
1483  ret_val = FALSE;
1484 
1485  FUNCTION_FINISH(STB_GCIsDateDayWeek);
1486 
1487  return(ret_val);
1488 }
1489 
1501 BOOLEAN STB_GCIsDateDayWeekend(U16BIT code)
1502 {
1503  E_STB_GC_WEEKDAY wday;
1504  BOOLEAN ret_val;
1505 
1506  FUNCTION_START(STB_GCIsDateDayWeekend);
1507 
1508  wday = STB_GCGetDateWeekDay(code);
1509  if ((wday >= WEEKDAY_SATURDAY) && (wday <= WEEKDAY_SUNDAY))
1510  ret_val = TRUE;
1511  else
1512  ret_val = FALSE;
1513 
1514  FUNCTION_FINISH(STB_GCIsDateDayWeekend);
1515 
1516  return(ret_val);
1517 }
1518 
1540 void STB_GCCalculateDateTime(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs,
1541  U8BIT ohour, U8BIT omin, U8BIT osecs,
1542  U16BIT *rcode, U8BIT *rhour, U8BIT *rmin, U8BIT *rsecs,
1543  E_STB_GC_CALCTYPE calc)
1544 {
1545  FUNCTION_START(STB_GCCalculateDateTime);
1546 
1547  ASSERT(rcode != NULL);
1548  ASSERT(rhour != NULL);
1549  ASSERT(rmin != NULL);
1550  ASSERT(rsecs != NULL);
1551 
1552  switch (calc)
1553  {
1554  case CALC_ADD:
1555  AddTime(code, hour, min, secs,
1556  (S16BIT)ohour, (S16BIT)omin, (S16BIT)osecs,
1557  rcode, rhour, rmin, rsecs);
1558  break;
1559 
1560  case CALC_SUB:
1561  AddTime(code, hour, min, secs,
1562  ((S16BIT)ohour * -1), ((S16BIT)omin * -1), ((S16BIT)osecs * -1),
1563  rcode, rhour, rmin, rsecs);
1564  break;
1565 
1566  default:
1567  break;
1568  }
1569 
1570  FUNCTION_FINISH(STB_GCCalculateDateTime);
1571 }
1572 
1591 void STB_GCConvertDateTime(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs,
1592  U16BIT *rcode, U8BIT *rhour, U8BIT *rmin, U8BIT *rsecs,
1593  E_STB_GC_CONVTYPE conv)
1594 {
1595  S8BIT ohour, omin;
1596 
1597  FUNCTION_START(STB_GCConvertDateTime);
1598 
1599  ASSERT(rcode != NULL);
1600  ASSERT(rhour != NULL);
1601  ASSERT(rmin != NULL);
1602  ASSERT(rsecs != NULL);
1603 
1604  STB_OSMutexLock(time_mutex);
1605 
1606  GetCTime();
1607  GetOffset(code, hour, min, secs, &ohour, &omin);
1608 
1609  switch (conv)
1610  {
1611  case CONV_LOCAL:
1612  AddTime(code, hour, min, secs, (S16BIT)ohour, (S16BIT)omin, 0, rcode, rhour, rmin, rsecs);
1613  break;
1614 
1615  case CONV_GMT:
1616  AddTime(code, hour, min, secs, (S16BIT)(ohour * -1), (S16BIT)(omin * -1), 0,
1617  rcode, rhour, rmin, rsecs);
1618  break;
1619 
1620  default:
1621  break;
1622  }
1623 
1624  STB_OSMutexUnlock(time_mutex);
1625 
1626  FUNCTION_FINISH(STB_GCConvertDateTime);
1627 }
1628 
1647 BOOLEAN STB_GCCompareDateTime(U16BIT code1, U8BIT hour1, U8BIT min1, U8BIT secs1,
1648  U16BIT code2, U8BIT hour2, U8BIT min2, U8BIT secs2,
1649  E_STB_GC_COMPTYPE comp)
1650 {
1651  BOOLEAN ret_val = FALSE;
1652 
1653  FUNCTION_START(STB_GCCompareDateTime);
1654 
1655 
1656  /********************************************************/
1657  /* CQ99999:PRM:06/02/28 */
1658  /********************************************************/
1659  /* Changed COMP_EQUAL to COMP_MATCH multiple Definition */
1660  /********************************************************/
1661  switch (comp)
1662  {
1663  case COMP_MATCH:
1664  if ((code1 == code2) && (hour1 == hour2) && (min1 == min2) && (secs1 == secs2))
1665  {
1666  ret_val = TRUE;
1667  }
1668  break;
1669 
1670  case COMP_1GT2:
1671  case COMP_2LT1:
1672  ret_val = FutureTime(code2, hour2, min2, secs2, code1, hour1, min1, secs1);
1673  ret_val = !ret_val;
1674  break;
1675 
1676  case COMP_1GE2:
1677  case COMP_2LE1:
1678  ret_val = FutureTime(code1, hour1, min1, secs1, code2, hour2, min2, secs2);
1679  break;
1680 
1681  case COMP_1LT2:
1682  case COMP_2GT1:
1683  ret_val = FutureTime(code1, hour1, min1, secs1, code2, hour2, min2, secs2);
1684  ret_val = !ret_val;
1685  break;
1686 
1687  case COMP_1LE2:
1688  case COMP_2GE1:
1689  ret_val = FutureTime(code2, hour2, min2, secs2, code1, hour1, min1, secs1);
1690  break;
1691 
1692  default:
1693  break;
1694  }
1695 
1696  FUNCTION_FINISH(STB_GCCompareDateTime);
1697 
1698  return(ret_val);
1699 }
1700 
1701 /*!**************************************************************************
1702  * @brief Returns the difference in seconds between the two dates/times, as time1 - time2,
1703  * so the result will be negative if time1 is earlier than time2, and positive otherwise
1704  * @param code1 - MJD date code
1705  * @param hour1 - MJD hour
1706  * @param min1 - MJD minutes
1707  * @param secs1 - MJD seconds
1708  * @param code2 - MJD date code
1709  * @param hour2 - MJD hour
1710  * @param min2 - MJD minutes
1711  * @param secs2 - MJD seconds
1712  * @return number of seconds between the two dates/times
1713  ****************************************************************************/
1714 S32BIT STB_GCDateTimeDiff(U16BIT code1, U8BIT hour1, U8BIT min1, U8BIT secs1,
1715  U16BIT code2, U8BIT hour2, U8BIT min2, U8BIT secs2)
1716 {
1717  S32BIT time1, time2;
1718 
1719  FUNCTION_START(STB_GCDateTimeDiff);
1720 
1721  time1 = ((code1 * 24 + hour1) * 60 + min1) * 60 + secs1;
1722  time2 = ((code2 * 24 + hour2) * 60 + min2) * 60 + secs2;
1723 
1724  FUNCTION_FINISH(STB_GCDateTimeDiff);
1725 
1726  return(time1 - time2);
1727 }
1728 
1743 U8BIT* STB_GCGetTimeString(U16BIT code, U8BIT hour, U8BIT min, E_STB_GC_TIMETYPE format)
1744 {
1745  U16BIT lcode;
1746  U8BIT lhour, lmin, lsecs;
1747  S8BIT ohour, omin;
1748 
1749  FUNCTION_START(STB_GCGetTimeString);
1750 
1751  ASSERT(hour <= 23);
1752  ASSERT(min <= 59);
1753 
1754  STB_OSMutexLock(time_mutex);
1755 
1756  // calculate local time
1757  GetCTime();
1758  GetOffset(code, hour, min, 0, &ohour, &omin);
1759  AddTime(code, hour, min, 0, (S16BIT)ohour, (S16BIT)omin, 0,
1760  &lcode, &lhour, &lmin, &lsecs);
1761 
1762  STB_OSMutexUnlock(time_mutex);
1763 
1764  switch (format)
1765  {
1766  case TIME_12H:
1767  if (lhour > 11)
1768  {
1769  lhour -= 12;
1770  if (lhour == 0)
1771  lhour = 12;
1772  snprintf((char *)time_string, TIME_STRING_MAX, "%d:%02d pm", lhour, lmin);
1773  }
1774  else
1775  {
1776  if (lhour == 0)
1777  lhour = 12;
1778  snprintf((char *)time_string, TIME_STRING_MAX, "%d:%02d am", lhour, lmin);
1779  }
1780  break;
1781 
1782  case TIME_24H:
1783  snprintf((char *)time_string, TIME_STRING_MAX, "%02d:%02d", lhour, lmin);
1784  break;
1785 
1786  default:
1787  break;
1788  }
1789 
1790  FUNCTION_FINISH(STB_GCGetTimeString);
1791 
1792  return(time_string);
1793 }
1794 
1809 U8BIT* STB_GCGetDateString(U16BIT code, U8BIT hour, U8BIT min, E_STB_GC_DATETYPE format)
1810 {
1811  U16BIT lcode, year;
1812  U8BIT lhour, lmin, lsecs, wday, day, month;
1813  S8BIT ohour, omin;
1814 
1815  FUNCTION_START(STB_GCGetDateString);
1816 
1817  ASSERT(hour <= 23);
1818  ASSERT(min <= 59);
1819 
1820  STB_OSMutexLock(time_mutex);
1821 
1822  // calculate local time and date
1823  GetCTime();
1824  GetOffset(code, hour, min, 0, &ohour, &omin);
1825  AddTime(code, hour, min, 0, (S16BIT)ohour, (S16BIT)omin, 0,
1826  &lcode, &lhour, &lmin, &lsecs);
1827  ExtractUTCDate(lcode, &wday, &day, &month, &year);
1828 
1829  STB_OSMutexUnlock(time_mutex);
1830 
1831  switch (format)
1832  {
1833  case DATE_DMY:
1834  snprintf((char *)date_string, DATE_STRING_MAX, "%02d/%02d/%04d", day, month, year);
1835  break;
1836 
1837  case DATE_YMD:
1838  snprintf((char *)date_string, DATE_STRING_MAX, "%04d/%02d/%02d", year, month, day);
1839  break;
1840 
1841  default:
1842  break;
1843  }
1844 
1845  FUNCTION_FINISH(STB_GCGetDateString);
1846 
1847  return(date_string);
1848 }
1849 
1867 void STB_GCGetDateInfo(U16BIT code, U8BIT hour, U8BIT min, U8BIT *day, U8BIT *wday,
1868  U8BIT *month, U16BIT *year)
1869 {
1870  U16BIT lcode;
1871  U8BIT lhour, lmin, lsecs;
1872  S8BIT ohour, omin;
1873 
1874  FUNCTION_START(STB_GCGetDateInfo);
1875 
1876  ASSERT(hour <= 23);
1877  ASSERT(min <= 59);
1878 
1879  STB_OSMutexLock(time_mutex);
1880 
1881  // calculate local time and date
1882  GetCTime();
1883  GetOffset(code, hour, min, 0, &ohour, &omin);
1884  AddTime(code, hour, min, 0, (S16BIT)ohour, (S16BIT)omin, 0,
1885  &lcode, &lhour, &lmin, &lsecs);
1886 
1887  STB_OSMutexUnlock(time_mutex);
1888 
1889  ExtractUTCDate(lcode, wday, day, month, year);
1890 
1891  FUNCTION_FINISH(STB_GCGetDateInfo);
1892 }
1893 
1894 /*!**************************************************************************
1895  * @brief Returns the date info from the given MJD date code
1896  * @param code - date code as a Modified Julian Date value
1897  * @param day - returns with day of the month
1898  * @param wday - returns with day of the week
1899  * @param month - returns with month number
1900  * @param year - returns with the year
1901  ****************************************************************************/
1902 void STB_GCGetMJDDateInfo(U16BIT code, U8BIT *day, U8BIT *wday, U8BIT *month, U16BIT *year)
1903 {
1904  FUNCTION_START(STB_GCGetMJDDateInfo);
1905 
1906  ExtractUTCDate(code, wday, day, month, year);
1907 
1908  FUNCTION_FINISH(STB_GCGetMJDDateInfo);
1909 }
1910 
1925 U8BIT* STB_GCGetClockString(U8BIT hour, U8BIT min, U8BIT secs, E_STB_GC_CLOCKTYPE format)
1926 {
1927  FUNCTION_START(STB_GCGetClockString);
1928 
1929  switch (format)
1930  {
1931  case CLOCK_HMS:
1932  snprintf((char *)clock_string, CLOCK_STRING_MAX, "%02d:%02d:%02d", hour, min, secs);
1933  break;
1934 
1935  case CLOCK_SMH:
1936  snprintf((char *)clock_string, CLOCK_STRING_MAX, "%02d:%02d:%02d", secs, min, hour);
1937  break;
1938 
1939  case CLOCK_HM:
1940  snprintf((char *)clock_string, CLOCK_STRING_MAX, "%02d:%02d", hour, min);
1941  break;
1942 
1943  case CLOCK_MH:
1944  snprintf((char *)clock_string, CLOCK_STRING_MAX, "%02d:%02d", min, hour);
1945  break;
1946 
1947  default:
1948  break;
1949  }
1950 
1951  FUNCTION_FINISH(STB_GCGetClockString);
1952 
1953  return(clock_string);
1954 }
1955 
1969 void STB_GCGetLocalDateTime(U16BIT *year, U8BIT *month, U8BIT *day, U8BIT *hour, U8BIT *min)
1970 {
1971  U16BIT gcode, lcode;
1972  U8BIT ghour, gmin, secs, wday;
1973  S8BIT ohour, omin;
1974 
1975  FUNCTION_START(STB_GCGetLocalDateTime);
1976 
1977  ASSERT(year != NULL);
1978  ASSERT(month != NULL);
1979  ASSERT(day != NULL);
1980  ASSERT(hour != NULL);
1981  ASSERT(min != NULL);
1982 
1983  STB_OSMutexLock(time_mutex);
1984 
1985  GetCTime();
1986 
1987  gcode = general_status.date_code;
1988  ghour = general_status.time_hour;
1989  gmin = general_status.time_min;
1990 
1991  GetOffset(gcode, ghour, gmin, 0, &ohour, &omin);
1992  AddTime(gcode, ghour, gmin, 0, (S16BIT)ohour, (S16BIT)omin, 0,
1993  &lcode, hour, min, &secs);
1994 
1995  STB_OSMutexUnlock(time_mutex);
1996 
1997  ExtractUTCDate(lcode, &wday, day, month, year);
1998 
1999  FUNCTION_FINISH(STB_GCGetLocalDateTime);
2000 }
2001 
2002 /*!**************************************************************************
2003  * @brief Sets whether the date/time are taken from the broadcast or the system.
2004  * The default is to use the broadcast for date/time.
2005  * @param state - FALSE to use date/time from the system
2006  ****************************************************************************/
2007 void STB_GCUseBroadcastTime(BOOLEAN state)
2008 {
2009  FUNCTION_START(STB_GCUseBroadcastTime);
2010 
2011  if (state != general_status.use_broadcast_time)
2012  {
2013  STB_OSMutexLock(time_mutex);
2014 
2015  general_status.use_broadcast_time = state;
2016 
2017  if (!general_status.use_broadcast_time)
2018  {
2019  /* Time is assumed to be always available if it isn't being taken
2020  * from the broadcast */
2021  general_status.time_set = TRUE;
2022  }
2023 
2024  /* Force the time to be re-read with the change in state */
2025  GetCTime();
2026 
2027  STB_OSMutexUnlock(time_mutex);
2028  }
2029 
2030  FUNCTION_FINISH(STB_GCUseBroadcastTime);
2031 }
2032 
2049 U32DHMS STB_GCCreateDebugDHMS( U32BIT date, U32BIT hour, U32BIT mins, U32BIT secs )
2050 {
2051  U32DHMS dtm;
2052  FUNCTION_START(STB_GCCreateDebugDHMS);
2053  ASSERT( hour < 24 );
2054  ASSERT( mins < 60 );
2055  ASSERT( secs < 60 );
2056  dtm = (U32DHMS)date << D_DSHFT |
2057  (U32DHMS)hour << D_HSHFT |
2058  (U32DHMS)mins << D_MSHFT |
2059  (U32DHMS)secs;
2060  FUNCTION_FINISH(STB_GCCreateDebugDHMS);
2061  return dtm;
2062 }
2063 
2080 U32DHMS STB_GCCreateDHMS( U16BIT date, U8BIT hour, U8BIT mins, U8BIT secs )
2081 {
2082  FUNCTION_START(STB_GCCreateDHMS);
2083 
2084  while (secs > 59)
2085  {
2086  secs -= 60;
2087  mins++;
2088  }
2089  while (mins > 59)
2090  {
2091  mins -= 60;
2092  hour++;
2093  }
2094  while (hour > 23)
2095  {
2096  hour -= 24;
2097  date++;
2098  }
2099 
2100  FUNCTION_FINISH(STB_GCCreateDHMS);
2101 
2102  return DHMS_CREATE(date, hour, mins, secs);
2103 }
2104 
2110 U32DHMS STB_GCCreateDHMSFromSeconds(U32BIT num_seconds)
2111 {
2112  U32BIT hours, mins;
2113 
2114  FUNCTION_START(STB_GCCreateDHMSFromSeconds);
2115 
2116  hours = num_seconds / 3600;
2117  num_seconds %= 3600;
2118 
2119  mins = num_seconds / 60;
2120  num_seconds %= 60;
2121 
2122  FUNCTION_FINISH(STB_GCCreateDHMSFromSeconds);
2123 
2124  return(DHMS_CREATE(0, hours, mins, num_seconds));
2125 }
2126 
2136 U32DHMS STB_GCCalculateDHMS(U32DHMS dhms, U32DHMS period, E_STB_GC_CALCTYPE calc)
2137 {
2138  S32BIT rcode, rhour, rmins, rsecs;
2139 
2140  FUNCTION_START(STB_GCCalculateDHMS);
2141 
2142  if (calc == CALC_ADD)
2143  {
2144  // top bit of date is not needed here for 'rcode', as returned U32DHMS value will not have it anyway,
2145  // so just use DHMS_DAYS macro, rather than DHMS_DATE.
2146  rcode = DHMS_DAYS(dhms) + DHMS_DAYS(period);
2147  rhour = DHMS_HOUR32(dhms) + DHMS_HOUR32(period);
2148  rmins = DHMS_MINS32(dhms) + DHMS_MINS32(period);
2149  rsecs = DHMS_SECS32(dhms) + DHMS_SECS32(period);
2150 
2151  while (rsecs > 59)
2152  {
2153  rsecs -= 60;
2154  rmins++;
2155  }
2156  while (rmins > 59)
2157  {
2158  rmins -= 60;
2159  rhour++;
2160  }
2161  while (rhour > 23)
2162  {
2163  rhour -= 24;
2164  rcode++;
2165  }
2166  }
2167  else if (calc == CALC_SUB)
2168  {
2169  // top bit of date is not needed here for 'rcode', as returned U32DHMS value will not have it anyway,
2170  // so just use DHMS_DAYS macro, rather than DHMS_DATE.
2171  rcode = (S32BIT)DHMS_DAYS(dhms) - (S32BIT)DHMS_DAYS(period);
2172  rhour = (S32BIT)DHMS_HOUR32(dhms) - (S32BIT)DHMS_HOUR32(period);
2173  rmins = (S32BIT)DHMS_MINS32(dhms) - (S32BIT)DHMS_MINS32(period);
2174  rsecs = (S32BIT)DHMS_SECS32(dhms) - (S32BIT)DHMS_SECS32(period);
2175 
2176  while (rsecs < 0)
2177  {
2178  rsecs += 60;
2179  rmins--;
2180  }
2181  while (rmins < 0)
2182  {
2183  rmins += 60;
2184  rhour--;
2185  }
2186  while (rhour < 0)
2187  {
2188  rhour += 24;
2189  rcode--;
2190  }
2191  }
2192 
2193  FUNCTION_FINISH(STB_GCCalculateDHMS);
2194 
2195  return DHMS_CREATE(rcode, rhour, rmins, rsecs);
2196 }
2197 
2206 U32DHMS STB_GCConvertDHMS(U32DHMS dhms, E_STB_GC_CONVTYPE conv)
2207 {
2208  S8BIT ohour, omin;
2209 
2210  FUNCTION_START(STB_GCConvertDHMS);
2211 
2212  STB_OSMutexLock(time_mutex);
2213 
2214  GetCTime();
2215  GetOffset(DHMS_DATE(dhms), DHMS_HOUR(dhms), DHMS_MINS(dhms), 0, &ohour, &omin);
2216 
2217  STB_OSMutexUnlock(time_mutex);
2218 
2219  if (conv == CONV_GMT)
2220  {
2221  /* Convert local time to GMT */
2222  if (ohour < 0)
2223  {
2224  ohour *= -1;
2225  omin *= -1;
2226  dhms = STB_GCCalculateDHMS(dhms, DHMS_CREATE(0, ohour, omin, 0), CALC_ADD);
2227  }
2228  else if (ohour > 0)
2229  {
2230  dhms = STB_GCCalculateDHMS(dhms, DHMS_CREATE(0, ohour, omin, 0), CALC_SUB);
2231  }
2232  }
2233  else if (conv == CONV_LOCAL)
2234  {
2235  /* Convert GMT time to local */
2236  if (ohour > 0)
2237  {
2238  dhms = STB_GCCalculateDHMS(dhms, DHMS_CREATE(0, ohour, omin, 0), CALC_ADD);
2239  }
2240  else if (ohour < 0)
2241  {
2242  ohour *= -1;
2243  omin *= -1;
2244  dhms = STB_GCCalculateDHMS(dhms, DHMS_CREATE(0, ohour, omin, 0), CALC_SUB);
2245  }
2246  }
2247 
2248  FUNCTION_FINISH(STB_GCConvertDHMS);
2249 
2250  return dhms;
2251 }
2252 
2264 U32DHMS STB_GCNowDHMSGmt(void)
2265 {
2266  U32DHMS now;
2267 
2268  FUNCTION_START(STB_GCNowDHMSGmt);
2269 
2270  STB_OSMutexLock(time_mutex);
2271 
2272  GetCTime();
2273  now = DHMS_CREATE(general_status.date_code,
2274  general_status.time_hour,
2275  general_status.time_min,
2276  general_status.time_secs);
2277 
2278  STB_OSMutexUnlock(time_mutex);
2279 
2280  FUNCTION_FINISH(STB_GCNowDHMSGmt);
2281 
2282  return now;
2283 }
2284 
2296 U32DHMS STB_GCNowDHMSLocal(void)
2297 {
2298  U32DHMS local;
2299  S8BIT ohour, omin;
2300 
2301  FUNCTION_START(STB_GCNowDHMSLocal);
2302 
2303  STB_OSMutexLock(time_mutex);
2304 
2305  GetCTime();
2306  GetOffset( general_status.date_code,
2307  general_status.time_hour,
2308  general_status.time_min, 0, &ohour, &omin);
2309  if (ohour < 0)
2310  {
2311  ohour *= -1;
2312  omin *= -1;
2313  local = STB_GCCalculateDHMS(
2314  DHMS_CREATE(general_status.date_code,
2315  general_status.time_hour,
2316  general_status.time_min,
2317  general_status.time_secs),
2318  DHMS_CREATE(0, ohour, omin, 0), CALC_SUB);
2319  }
2320  else
2321  {
2322  local = STB_GCCalculateDHMS(
2323  DHMS_CREATE(general_status.date_code,
2324  general_status.time_hour,
2325  general_status.time_min,
2326  general_status.time_secs),
2327  DHMS_CREATE(0, ohour, omin, 0), CALC_ADD);
2328  }
2329 
2330  STB_OSMutexUnlock(time_mutex);
2331 
2332  FUNCTION_FINISH(STB_GCNowDHMSLocal);
2333 
2334  return local;
2335 }
2336 
2349 U8BIT* STB_GCGetDateStringDHMS(U32DHMS dhms, E_STB_GC_DATETYPE format)
2350 {
2351  U16BIT year;
2352  U8BIT wday, day, month;
2353 
2354  FUNCTION_START(STB_GCGetDateStringDHMS);
2355 
2356  dhms = STB_GCConvertDHMS(dhms, CONV_LOCAL);
2357 
2358  ExtractUTCDate(DHMS_DATE(dhms), &wday, &day, &month, &year);
2359  switch (format)
2360  {
2361  case DATE_DMY:
2362  snprintf((char *)date_string, DATE_STRING_MAX, "%02d/%02d/%04d", day, month, year);
2363  break;
2364 
2365  case DATE_YMD:
2366  snprintf((char *)date_string, DATE_STRING_MAX, "%04d/%02d/%02d", year, month, day);
2367  break;
2368 
2369  default:
2370  break;
2371  }
2372  FUNCTION_FINISH(STB_GCGetDateStringDHMS);
2373  return(date_string);
2374 }
2375 
2388 U8BIT* STB_GCGetTimeStringDHMS(U32DHMS dhms, E_STB_GC_TIMETYPE format)
2389 {
2390  U8BIT lhour, lmins;
2391 
2392  FUNCTION_START(STB_GCGetTimeStringDHMS);
2393 
2394  dhms = STB_GCConvertDHMS(dhms, CONV_LOCAL);
2395 
2396  lhour = DHMS_HOUR(dhms);
2397  lmins = DHMS_MINS(dhms);
2398  switch (format)
2399  {
2400  case TIME_12H:
2401  if (lhour > 11)
2402  {
2403  lhour -= 12;
2404  if (lhour == 0)
2405  lhour = 12;
2406  snprintf((char *)time_string, TIME_STRING_MAX, "%d:%02d pm", lhour, lmins);
2407  }
2408  else
2409  {
2410  if (lhour == 0)
2411  lhour = 12;
2412  snprintf((char *)time_string, TIME_STRING_MAX, "%d:%02d am", lhour, lmins);
2413  }
2414  break;
2415  case TIME_24H:
2416  snprintf((char *)time_string, TIME_STRING_MAX, "%02d:%02d", lhour, lmins);
2417  break;
2418  default:
2419  break;
2420  }
2421 
2422  FUNCTION_FINISH(STB_GCGetTimeStringDHMS);
2423 
2424  return(time_string);
2425 }
2426 
2432 U32DHMS STB_GCConvertTimestamp(U32BIT timestamp)
2433 {
2434  U16BIT date;
2435  U8BIT h, m, s;
2436  U32DHMS time;
2437 
2438  FUNCTION_START(STB_GCConvertTimestamp);
2439 
2440  ConvertTimestamp(timestamp, &date, &h, &m, &s);
2441  time = STB_GCCreateDHMS(date, h, m, s);
2442 
2443  FUNCTION_FINISH(STB_GCConvertTimestamp);
2444 
2445  return time;
2446 }
2447 
2453 U32BIT STB_GCConvertToTimestamp(U32DHMS time)
2454 {
2455  U32BIT timestamp;
2456  U16BIT code;
2457  U8BIT h, m, s;
2458 
2459  FUNCTION_START(STB_GCConvertToTimestamp);
2460 
2461  code = DHMS_DATE(time);
2462  h = DHMS_HOUR(time);
2463  m = DHMS_MINS(time);
2464  s = DHMS_SECS(time);
2465 
2466  timestamp = ConvertToTimestamp(code, h, m, s);
2467 
2468  FUNCTION_FINISH(STB_GCConvertToTimestamp);
2469 
2470  return timestamp;
2471 }
2472 
2473 //*****************************************************************************
2474 // End of file
2475 //*****************************************************************************
2476 
void STB_GCInitialise(void)
Initialises general control.
Definition: stbgc.c:582
void STB_GCSetAudioSignal(BOOLEAN state)
Enables/disables audio signal bleep and writes value into general control store.
Definition: stbgc.c:768
U32DHMS STB_GCCalculateDHMS(U32DHMS dhms, U32DHMS period, E_STB_GC_CALCTYPE calc)
Calculates the date/time when the period is added/subtracted to/from dhms.
Definition: stbgc.c:2136
U32BIT STB_OSGetClockGMT(void)
Returns the system time in seconds.
U32DHMS STB_GCCreateDHMSFromSeconds(U32BIT num_seconds)
Creates a DHMS value consisting of hours, minutes and seconds from a number of seconds.
Definition: stbgc.c:2110
U8BIT * STB_GCGetLangCodeString(U32BIT lang)
Converts the given 24bit ISO language code to a null-terminated string.
Definition: stbgc.c:665
BOOLEAN STB_GCIsFutureDateTime(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs)
Tests given date and time against current GMT date and time.
Definition: stbgc.c:1411
void STB_OSSetClockRTC(U32BIT num_seconds)
Set the local time in seconds since midnight 1-1-1970.
Header file - macros and function prototypes for public use.
U8BIT STB_GCGetGMTDay(void)
Reads the current GMT day number.
Definition: stbgc.c:1273
U32DHMS STB_GCCreateDHMS(U16BIT date, U8BIT hour, U8BIT mins, U8BIT secs)
Makes U32DHMS formated date/time from date code, hour, minutes, seconds.
Definition: stbgc.c:2080
U8BIT * STB_GCGetTimeStringDHMS(U32DHMS dhms, E_STB_GC_TIMETYPE format)
Supplies specified time as local time string, in format requested.
Definition: stbgc.c:2388
void STB_GCCalculateDateTime(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs, U8BIT ohour, U8BIT omin, U8BIT osecs, U16BIT *rcode, U8BIT *rhour, U8BIT *rmin, U8BIT *rsecs, E_STB_GC_CALCTYPE calc)
Adds or subtracts offset from a date/time.
Definition: stbgc.c:1540
U16BIT STB_HWGetCustomerId(void)
Returns the platform customer identifier code.
U16BIT STB_HWGetHwId(void)
Returns the platform hardware identifier code.
U32BIT STB_HWGetBoxSerialNumber(void)
Returns the serial number of the Set Top Box.
U8BIT * STB_GCGetVersionNumberString(void)
Retrieves and returns the library version number as a string.
Definition: stbgc.c:746
void STB_GCSetGMTTime(U8BIT hour, U8BIT min, U8BIT secs)
Sets current GMT time.
Definition: stbgc.c:1042
void STB_GCSetGMTDate(U16BIT code)
Sets current GMT date.
Definition: stbgc.c:1174
U8BIT * STB_GCGetTimeString(U16BIT code, U8BIT hour, U8BIT min, E_STB_GC_TIMETYPE format)
Supplies specified time as local time string, in format requested.
Definition: stbgc.c:1743
Header file - Function prototypes for A/V control.
void STB_GCGetMJDDateInfo(U16BIT code, U8BIT *day, U8BIT *wday, U8BIT *month, U16BIT *year)
Returns the date info from the given MJD date code.
Definition: stbgc.c:1902
E_STB_GC_WEEKDAY STB_GCGetGMTWeekDay(void)
Reads the current GMT weekday number.
Definition: stbgc.c:1238
void STB_GCUseBroadcastTime(BOOLEAN state)
Sets whether the date/time are taken from the broadcast or the system. The default is to use the broa...
Definition: stbgc.c:2007
void STB_OSMutexUnlock(void *mutex)
Unlock a mutex (a.k.a. &#39;leave&#39;, &#39;signal&#39; or &#39;release&#39;)
U32BIT STB_GCConvertToTimestamp(U32DHMS time)
Returns the number of seconds from midnight (UTC) 1 January 1970 to the specified U32DHMS time...
Definition: stbgc.c:2453
void STB_GCConvertDateTime(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs, U16BIT *rcode, U8BIT *rhour, U8BIT *rmin, U8BIT *rsecs, E_STB_GC_CONVTYPE conv)
Converts specified date / time to another.
Definition: stbgc.c:1591
BOOLEAN STB_GCCompareDateTime(U16BIT code1, U8BIT hour1, U8BIT min1, U8BIT secs1, U16BIT code2, U8BIT hour2, U8BIT min2, U8BIT secs2, E_STB_GC_COMPTYPE comp)
Compares one date/time to another.
Definition: stbgc.c:1647
BOOLEAN STB_GCGetAudioSignal(void)
Reads the current audio signal bleep state from general control store.
Definition: stbgc.c:796
U8BIT STB_GCGetGMTMonth(void)
Reads the current GMT month number.
Definition: stbgc.c:1305
void STB_GCGetGMTDateTime(U16BIT *code, U8BIT *hour, U8BIT *min, U8BIT *secs)
Reads the current GMT date code and time.
Definition: stbgc.c:1372
U32BIT STB_OSGetClockRTC(void)
Returns the current time in seconds. This is calculated by using the set UTC time and adding the diff...
S32BIT STB_GCDateTimeDiff(U16BIT code1, U8BIT hour1, U8BIT min1, U8BIT secs1, U16BIT code2, U8BIT hour2, U8BIT min2, U8BIT secs2)
Returns the difference in seconds between the two dates/times, as time1 - time2, so the result will b...
Definition: stbgc.c:1714
void STB_GCSetSearchLangCode(U32BIT lang)
Writes SI search language code into general control store.
Definition: stbgc.c:621
void STB_GCSetLocalTimeOffset(U8BIT ohour, U8BIT omin, BOOLEAN neg)
Sets local time offset from GMT.
Definition: stbgc.c:822
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.
U8BIT * STB_GCGetClockString(U8BIT hour, U8BIT min, U8BIT secs, E_STB_GC_CLOCKTYPE format)
Supplies specified time as clock string, in format requested.
Definition: stbgc.c:1925
U8BIT STB_GCGetGMTHour(void)
Reads the current GMT hour.
Definition: stbgc.c:1081
U8BIT STB_GCGetGMTMin(void)
Reads the current GMT minute.
Definition: stbgc.c:1112
U8BIT * STB_GCGetFullSerialString(void)
Retrieves and returns the fulls serial string, incorporating the hardware version box serial number a...
Definition: stbgc.c:723
U8BIT * STB_GCGetDateString(U16BIT code, U8BIT hour, U8BIT min, E_STB_GC_DATETYPE format)
Supplies specifed date as local date string, in format requested.
Definition: stbgc.c:1809
U32DHMS STB_GCNowDHMSGmt(void)
Reads the current GMT date code and time.
Definition: stbgc.c:2264
void STB_GCGetLocalTimeChange(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs, U8BIT *ohour, U8BIT *omin, BOOLEAN *neg)
Reads local time offset from GMT.
Definition: stbgc.c:986
Header file - library version number.
void STB_GCGetDateInfo(U16BIT code, U8BIT hour, U8BIT min, U8BIT *day, U8BIT *wday, U8BIT *month, U16BIT *year)
Get the day, weekday, month and the year from a date code and hour / minute offset.
Definition: stbgc.c:1867
U32DHMS STB_GCConvertDHMS(U32DHMS dhms, E_STB_GC_CONVTYPE conv)
Converts the given date/time to local or GMT.
Definition: stbgc.c:2206
Header file - Function prototypes for operating system.
System Wide Global Technical Data Type Definitions.
BOOLEAN STB_GCIsDateDayWeek(U16BIT code)
Tests whether the weekday of the specified date code is during the week.
Definition: stbgc.c:1472
void STB_GCGetLocalDateTime(U16BIT *year, U8BIT *month, U8BIT *day, U8BIT *hour, U8BIT *min)
Get the local year, month, day, hour and minute.
Definition: stbgc.c:1969
void STB_GCSetLocalTimeChange(U16BIT code, U8BIT hour, U8BIT min, U8BIT secs, U8BIT ohour1, U8BIT omin1, U8BIT ohour2, U8BIT omin2, BOOLEAN neg)
Sets new and old local time offset from GMT.
Definition: stbgc.c:925
U16BIT STB_GCGetGMTDate(void)
Reads the current GMT date code.
Definition: stbgc.c:1207
U32DHMS STB_GCNowDHMSLocal(void)
Reads the current Local date code and time.
Definition: stbgc.c:2296
void STB_GCSetCIStandby(BOOLEAN standby)
Enables / disables CI standby state.
Definition: stbgc.c:690
void STB_GCGetLocalTimeOffset(U8BIT *ohour, U8BIT *omin, BOOLEAN *neg)
Reads local time offset from GMT.
Definition: stbgc.c:869
U32DHMS STB_GCCreateDebugDHMS(U32BIT date, U32BIT hour, U32BIT mins, U32BIT secs)
Makes U32DHMS formated date/time from date code, hour, minutes, seconds.
Definition: stbgc.c:2049
void STB_OSSetClockGMT(U32BIT num_seconds)
Set the time in seconds since midnight 1-1-1970 in GMT.
U32BIT STB_GCGetSearchLangCode(void)
Reads the SI search language code from general control store.
Definition: stbgc.c:641
U8BIT STB_GCGetGMTSecs(void)
Reads the current GMT seconds.
Definition: stbgc.c:1143
void * STB_OSCreateMutex(void)
Create a mutex.
BOOLEAN STB_GCIsDateDayWeekend(U16BIT code)
Tests whether the weekday of the specified date code is during the weekend.
Definition: stbgc.c:1501
E_STB_GC_WEEKDAY STB_GCGetDateWeekDay(U16BIT code)
Returns the weekday number of the specified date code.
Definition: stbgc.c:1448
Header file - Function prototypes for front panel control.
U32DHMS STB_GCConvertTimestamp(U32BIT timestamp)
Converts a timestamp expressed in number of seconds since midnight (UTC) 1 January 1970...
Definition: stbgc.c:2432
U8BIT * STB_GCGetDateStringDHMS(U32DHMS dhms, E_STB_GC_DATETYPE format)
Supplies specifed date as local date string, in format requested.
Definition: stbgc.c:2349
U16BIT STB_GCGetGMTYear(void)
Reads the current GMT year number.
Definition: stbgc.c:1337