PS2SDK
PS2 Homebrew Libraries
poweroff.c
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
11 #include "libpwroff.h"
12 
13 #include <errno.h>
14 #include <stdio.h>
15 #include <tamtypes.h>
16 #include <kernel.h>
17 #include <string.h>
18 #include <sifrpc.h>
19 #include <iopcontrol.h>
20 #include <pwroff_rpc.h>
21 
22 extern void *_gp;
23 
24 static poweroff_callback poweroff_cb = NULL;
25 static void *poweroff_data = NULL;
26 
27 static u8 poffThreadStack[512 * 16] __attribute__((aligned(16)));
28 
29 static SifRpcClientData_t cd0;
30 static struct t_SifRpcDataQueue cb_queue;
31 static struct t_SifRpcServerData cb_srv;
32 static int powerOffThreadId = -1;
33 
34 static void *PowerOff_ee_rpc_handler(int fnum, void *buffer, int len)
35 {
36  (void)len;
37 
38  switch (fnum)
39  {
40  case POFF_RPC_BUTTON:
41  printf("EE: power button pressed\n");
42 
43  if (poweroff_cb)
44  {
45  poweroff_cb(poweroff_data);
46  }
47  break;
48  }
49 
50  return buffer;
51 }
52 
53 static void PowerOffThread(void *dat)
54 {
55  static unsigned char cb_rpc_buffer[64] __attribute__((__aligned__(64)));
56 
57  (void)dat;
58 
59  sceSifSetRpcQueue(&cb_queue, powerOffThreadId);
60  sceSifRegisterRpc(&cb_srv, PWROFF_IRX, &PowerOff_ee_rpc_handler, cb_rpc_buffer, NULL, NULL, &cb_queue);
61  sceSifRpcLoop(&cb_queue);
62 }
63 
64 int poweroffInit(void)
65 {
67  int res;
68 
70  memset(&cd0, 0, sizeof(cd0));
71 
72  if (cd0.server)
73  return 0;
74 
75  while (((res = sceSifBindRpc(&cd0, PWROFF_IRX, 0)) < 0) || (cd0.server == NULL))
76  nopdelay();
77 
78  // Terminate and delete any previously created threads
79  if (powerOffThreadId >= 0)
80  {
81  TerminateThread(powerOffThreadId);
82  DeleteThread(powerOffThreadId);
83  sceSifRemoveRpc(&cb_srv, &cb_queue);
84  sceSifRemoveRpcQueue(&cb_queue);
85  powerOffThreadId = -1;
86  }
87 
88  thread.initial_priority = POWEROFF_THREAD_PRIORITY;
89  thread.stack_size = sizeof(poffThreadStack);
90  thread.gp_reg = &_gp;
91  thread.func = PowerOffThread;
92  thread.stack = (void *)poffThreadStack;
93  thread.option = 0;
94  thread.attr = 0;
95  powerOffThreadId = CreateThread(&thread);
96  StartThread(powerOffThreadId, NULL);
97 
98  int autoShutdown[4] = {0};
99  sceSifCallRpc(&cd0, PWROFF_ENABLE_AUTO_SHUTOFF, 0, NULL, 0, autoShutdown, sizeof(autoShutdown), NULL, NULL);
100 
101  return res;
102 }
103 
105 {
106  poweroffInit();
107 
108  poweroff_cb = cb;
109  poweroff_data = arg;
110 }
111 
113 {
114  poweroffInit();
115 
116  sceSifCallRpc(&cd0, PWROFF_SHUTDOWN, 0, NULL, 0, NULL, 0, NULL, NULL);
117 }
118 
120 {
121  ChangeThreadPriority(powerOffThreadId, priority);
122 }
kernel.h
poweroffChangeThreadPriority
void poweroffChangeThreadPriority(int priority)
Change thread priority.
Definition: poweroff.c:119
poweroff_callback
void(* poweroff_callback)(void *arg)
User defined function for use in poweroffSetCallback()
Definition: libpwroff.h:49
pwroff_rpc.h
poweroffSetCallback
void poweroffSetCallback(poweroff_callback cb, void *arg)
Set callback function.
Definition: poweroff.c:104
poweroffShutdown
void poweroffShutdown(void)
Immidiate console shutdown. Do not call it without closing all stuff.
Definition: poweroff.c:112
thread
Definition: thcommon.h:143
iopcontrol.h
_gp
void * _gp
__attribute__
typedef __attribute__
Definition: tlbfunc.c:60
poweroffInit
int poweroffInit(void)
Initializes the poweroff library.
Definition: poweroff.c:64
tamtypes.h
HasIopRebootedSinceLastCall
static int HasIopRebootedSinceLastCall(void)
Definition: iopcontrol.h:47
stdio.h
t_SifRpcServerData
Definition: sifrpc-common.h:98
t_SifRpcDataQueue
Definition: sifrpc-common.h:153
t_SifRpcClientData
Definition: sifrpc-common.h:134
libpwroff.h
Power-off library.
__attribute__
Definition: gif_registers.h:38
t_ee_thread
Definition: kernel.h:203
errno.h