FireBirdLib - Topfield TMS PVR TAP Programming Library
SDS.c
Go to the documentation of this file.
1#include <string.h>
2#include "FBLib_shutdown.h"
3#include "libFireBird.h"
4
5#define ROOTDIR "/ProgramFiles/Settings/SDS"
6#define LOGFILE "SDS.log"
7
8typedef enum
9{
18
19typedef struct
20{
21 byte Type;
22 byte unused[3];
23 dword EventID;
24 dword Queue;
26
27typedef struct
28{
30 byte State;
31 byte unused[2];
32 dword Handler;
34
35void (*__DevFront_PowerOffReply)(void) = NULL;
36void (*__DevFront_PowerOffCancel)(void) = NULL;
37
38bool ShutdownHooked = FALSE;
39int LastStatus = 0;
40char Log[512];
42
43void (*OrigHandler)(word, dword);
44
45void CreateRootDir(void)
46{
47 TRACEENTER();
48
49 //Check & Create Folders
51 HDD_ChangeDir("/ProgramFiles");
52 if(!TAP_Hdd_Exist("Settings")) TAP_Hdd_Create("Settings", ATTR_FOLDER);
53 HDD_ChangeDir("Settings");
54 if(!TAP_Hdd_Exist("SDS")) TAP_Hdd_Create("SDS", ATTR_FOLDER);
56
57 TRACEEXIT();
58}
59
60void WriteLog(char *s)
61{
63 TAP_Hdd_ChangeDir(ROOTDIR);
64 LogEntry(LOGFILE, "SDS", TRUE, TIMESTAMP_YMDHMS, s);
66}
67
68void Hooked_ApplEvent_CallHandler(unsigned int a1, unsigned int a2)
69{
70 TRACEENTER();
71
72 (void)a1;
73 (void)a2;
74
75 ShutdownHooked = TRUE;
76
77 TRACEEXIT();
78}
79
80bool SetHandler(dword EventID, void *Handler, void **OrigHandler)
81{
82 TRACEENTER();
83
84 static dword *__topEvent = NULL;
85 tEventQueue *EventQueue;
86 tEventQueueDetails *EventQueueDetails;
87 int i;
88
89 if(!Handler && !(dword*)Handler)
90 {
91 TRACEEXIT();
92 return FALSE;
93 }
94
95 if(!__topEvent)
96 {
97 __topEvent = (dword*)FIS_vTopEvent();
98 if(!__topEvent)
99 {
100 if(LastStatus != -10) WriteLog("Failed to resolve _topEvent");
101 LastStatus = -10;
102
103 TRACEEXIT();
104 return FALSE;
105 }
106 }
107
108 EventQueue = (tEventQueue*)*__topEvent;
109 for(i = 0; i < 512; i++)
110 {
111 if(EventQueue->Queue)
112 {
113 if(EventQueue->EventID == EventID)
114 {
115 EventQueueDetails = (tEventQueueDetails*)EventQueue->Queue;
116
117 if(((dword)Handler > 0x01400000) && (EventQueueDetails->Handler > 0x01400000))
118 {
119 if(LastStatus != -11)
120 {
121 TAP_SPrint(Log, "Found 0x%lx handler @ index %d. Already redirected to 0x%8.8lx", EventID, i, EventQueueDetails->Handler);
122 WriteLog(Log);
123 LastStatus = -11;
124 }
125
126 TRACEEXIT();
127 return FALSE;
128 }
129
130 if(LastStatus != -12)
131 {
132 TAP_SPrint(Log, "Found 0x%lx handler @ index %d. Redirecting from 0x%8.8lx to 0x%8.8lx", EventID, i, EventQueueDetails->Handler, (dword)Handler);
133 WriteLog(Log);
134 LastStatus = -12;
135 }
136 if(OrigHandler) *OrigHandler = (void*)EventQueueDetails->Handler;
137 EventQueueDetails->Handler = (dword)Handler;
138
139 TRACEEXIT();
140 return TRUE;
141 }
142 }
143 EventQueue++;
144 }
145
146 if(LastStatus != -13)
147 {
148 TAP_SPrint(Log, "EventID %4.4lx not yet declared", EventID);
149 WriteLog(Log);
150 LastStatus = -13;
151 }
152
153 TRACEEXIT();
154 return FALSE;
155}
156
157bool SDS(void)
158{
159 TRACEENTER();
160
161 static dword Timeout = 0;
162 static tHookHandlerState LastHHS = HHS_Exit;
163 static dword EF00FilterTimeout = 0;
164
165 if(LastHHS != HookHandlerState)
166 {
167 switch(HookHandlerState)
168 {
169 case HHS_Init: WriteLog("HHS_Init"); break;
170 case HHS_Idle: WriteLog("HHS_Idle"); break;
171 case HHS_EF00Received: WriteLog("HHS_EF00Received"); break;
172 case HHS_PowerOffCancel: WriteLog("HHS_PowerOffCancel"); break;
173 case HHS_ShutdownTimerDiags: WriteLog("HHS_ShutdownTimerDiags"); break;
174 case HHS_Shutdown: WriteLog("HHS_Shutdown"); break;
175 case HHS_Exit: WriteLog("HHS_Exit"); break;
176 }
177 LastHHS = HookHandlerState;
178 }
179
180 switch(HookHandlerState)
181 {
182 case HHS_Init:
183 {
184 char *TAPFileName;
185 tTAPInfo TAPInfo;
186
188
189 //Init some firmware routines
191 {
194 {
195 if(LastStatus != -1) WriteLog("Failed to resolve DevFront_PowerOffReply()");
196 LastStatus = -1;
198
199 TRACEEXIT();
200 return FALSE;
201 }
202 }
203
205 {
208 {
209 if(LastStatus != -2) WriteLog("Failed to resolve DevFront_PowerOffCancel()");
210 LastStatus = -2;
212
213 TRACEEXIT();
214 return FALSE;
215 }
216 }
217
218 //Modify the handler pointer of the ef00 event queue
220 {
221 TRACEEXIT();
222 return FALSE;
223 }
224
225 if(!LibInitialized && !InitTAPex())
226 {
227 TRACEEXIT();
228 return FALSE;
229 }
230
232 {
233 if(!HDD_TAP_GetInfo(TAPFileName, &TAPInfo)) strcpy(TAPInfo.TAPName, "???");
234 }
235 else
236 strcpy(TAPInfo.TAPName, "???");
237 TAP_SPrint(Log, "SDS has been activated by '%s'", TAPInfo.TAPName);
238 WriteLog(Log);
239
241 break;
242 }
243
244 case HHS_Idle:
245 {
246 if(EF00FilterTimeout && TAP_GetTick() > EF00FilterTimeout) EF00FilterTimeout = 0;
247
249 {
251 ShutdownHooked = FALSE;
252 }
253 break;
254 }
255
256 case HHS_EF00Received:
257 {
259
260 if(EF00FilterTimeout)
261 {
262 Timeout = TAP_GetTick() + 50;
264 }
265 else
266 {
267 dword ret = HDD_TAP_SendEvent(0, TRUE, EVT_STOP, 2, 0);
268 if(ret == 0)
269 {
270 if(LastStatus != -4) WriteLog("A TAP has intercepted the shutdown notfication");
271 LastStatus = -4;
272 Timeout = TAP_GetTick() + 50;
274 }
275 else
276 {
278 }
279 }
280
281 break;
282 }
283
285 {
286 if(TAP_GetTick() > Timeout)
287 {
288 if(LastStatus != -5) WriteLog("The shutdown has been canceled");
289 LastStatus = -5;
291 EF00FilterTimeout = TAP_GetTick() + 200;
293 }
294 break;
295 }
296
298 {
299 if(LastStatus != -6) WriteLog("Notifiying TimerDiags about the shutdown");
300 LastStatus = -6;
301 HDD_TAP_SendEvent(0x8E0A4224, FALSE, EVT_STOP, 0, 0);
302 Timeout = TAP_GetTick() + 200;
304 break;
305 }
306
307 case HHS_Shutdown:
308 {
309 if(TAP_GetTick() > Timeout)
310 {
311 if(OrigHandler)
312 {
313 SetHandler(0xef00, OrigHandler, NULL);
314 OrigHandler = NULL;
315 }
316 if(LastStatus != -7) WriteLog("Notifiying all TAPs about the shutdown");
317 LastStatus = -7;
318 HDD_TAP_SendEvent(0, FALSE, EVT_STOP, 0, 0);
319 //TAP_Exit();
320 if(LastStatus != -8) WriteLog("Enqueuing the shutdown event");
321 LastStatus = -8;
324 }
325 break;
326 }
327
328 case HHS_Exit:
329 {
330 if(OrigHandler)
331 {
332 SetHandler(0xef00, OrigHandler, NULL);
333 OrigHandler = NULL;
334 if(LastStatus != -9) WriteLog("SDS has been deactivated");
335 LastStatus = -9;
336 }
337
338 TRACEEXIT();
339 return FALSE;
340 }
341 }
342
343 TRACEEXIT();
344 return TRUE;
345}
346
347void SDSTerminate(void)
348{
349 TRACEENTER();
350
351 if(LastStatus != -3) WriteLog("SDS termination request received");
352 LastStatus = -3;
353
354 if(OrigHandler)
355 {
356 SetHandler(0xef00, OrigHandler, NULL);
357 OrigHandler = NULL;
358 if(LastStatus != -9) WriteLog("SDS has been deactivated");
359 LastStatus = -9;
360 }
362
363 TRACEEXIT();
364}
bool LibInitialized
Definition: InitTAPex.c:8
dword FIS_fwDevFront_PowerOffCancel(void)
dword FIS_fwDevFront_PowerOffReply(void)
dword FIS_vTopEvent(void)
Definition: FIS_vTopEvent.c:3
bool HDD_ChangeDir(const char *Dir)
Definition: HDD_ChangeDir.c:5
void LogEntry(char *FileName, char *ProgramName, bool Console, eTimeStampFormat TimeStampFormat, char *Text)
Definition: LogEntry.c:10
void(* __DevFront_PowerOffCancel)(void)
Definition: SDS.c:36
tHookHandlerState
Definition: SDS.c:9
@ HHS_Init
Definition: SDS.c:10
@ HHS_ShutdownTimerDiags
Definition: SDS.c:14
@ HHS_Shutdown
Definition: SDS.c:15
@ HHS_Idle
Definition: SDS.c:11
@ HHS_Exit
Definition: SDS.c:16
@ HHS_PowerOffCancel
Definition: SDS.c:13
@ HHS_EF00Received
Definition: SDS.c:12
void CreateRootDir(void)
Definition: SDS.c:45
tHookHandlerState HookHandlerState
Definition: SDS.c:41
void(* OrigHandler)(word, dword)
Definition: SDS.c:43
bool SetHandler(dword EventID, void *Handler, void **OrigHandler)
Definition: SDS.c:80
bool ShutdownHooked
Definition: SDS.c:38
#define ROOTDIR
Definition: SDS.c:5
void WriteLog(char *s)
Definition: SDS.c:60
bool SDS(void)
Definition: SDS.c:157
#define LOGFILE
Definition: SDS.c:6
void(* __DevFront_PowerOffReply)(void)
Definition: SDS.c:35
char Log[512]
Definition: SDS.c:40
int LastStatus
Definition: SDS.c:39
void SDSTerminate(void)
Definition: SDS.c:347
void Hooked_ApplEvent_CallHandler(unsigned int a1, unsigned int a2)
Definition: SDS.c:68
bool InitTAPex(void)
Definition: InitTAPex.c:10
dword HDD_TAP_SendEvent(dword TAPID, bool AllowParamInterception, word event, dword param1, dword param2)
bool HDD_TAP_PushDir(void)
bool HDD_TAP_GetFileNameByIndex(int Index, char **TAPFileName)
bool HDD_TAP_GetInfo(const char *FileName, tTAPInfo *pTAPInfo)
#define TRACEEXIT()
Definition: libFireBird.h:1244
@ TaskPower
Definition: libFireBird.h:2489
@ TIMESTAMP_YMDHMS
Definition: libFireBird.h:100
#define TRACEENTER()
Definition: libFireBird.h:1243
bool HDD_TAP_PopDir(void)
Definition: HDD_TAP_PopDir.c:3
dword TAP_TableIndex
Definition: InitTAPex.c:7
bool Shutdown(TaskEnum Task)
Definition: Shutdown.c:4
byte State
Definition: SDS.c:30
byte Priority
Definition: SDS.c:29
dword Handler
Definition: SDS.c:32
byte Type
Definition: SDS.c:21
dword Queue
Definition: SDS.c:24
dword EventID
Definition: SDS.c:23
char TAPName[MAX_PROGRAM_NAME]
Definition: libFireBird.h:2594