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 <pwroff_rpc.h>
20 
21 extern void *_gp;
22 
23 static poweroff_callback poweroff_cb = NULL;
24 static void *poweroff_data = NULL;
25 
26 static u8 poffThreadStack[512 * 16] __attribute__((aligned(16)));
27 
28 extern int _iop_reboot_count;
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  static int _init_count = -1;
69 
70  if (_init_count == _iop_reboot_count)
71  return 0;
72  _init_count = _iop_reboot_count;
73 
74  while (((res = sceSifBindRpc(&cd0, PWROFF_IRX, 0)) < 0) || (cd0.server == NULL))
75  nopdelay();
76 
77  // Terminate and delete any previously created threads
78  if (powerOffThreadId >= 0)
79  {
80  TerminateThread(powerOffThreadId);
81  DeleteThread(powerOffThreadId);
82  sceSifRemoveRpc(&cb_srv, &cb_queue);
83  sceSifRemoveRpcQueue(&cb_queue);
84  powerOffThreadId = -1;
85  }
86 
87  thread.initial_priority = POWEROFF_THREAD_PRIORITY;
88  thread.stack_size = sizeof(poffThreadStack);
89  thread.gp_reg = &_gp;
90  thread.func = PowerOffThread;
91  thread.stack = (void *)poffThreadStack;
92  thread.option = 0;
93  thread.attr = 0;
94  powerOffThreadId = CreateThread(&thread);
95  StartThread(powerOffThreadId, NULL);
96 
97  int autoShutdown[4] = {0};
98  sceSifCallRpc(&cd0, PWROFF_ENABLE_AUTO_SHUTOFF, 0, NULL, 0, autoShutdown, sizeof(autoShutdown), NULL, NULL);
99 
100  return res;
101 }
102 
104 {
105  poweroffInit();
106 
107  poweroff_cb = cb;
108  poweroff_data = arg;
109 }
110 
112 {
113  poweroffInit();
114 
115  sceSifCallRpc(&cd0, PWROFF_SHUTDOWN, 0, NULL, 0, NULL, 0, NULL, NULL);
116 }
117 
119 {
120  ChangeThreadPriority(powerOffThreadId, priority);
121 }
kernel.h
poweroffChangeThreadPriority
void poweroffChangeThreadPriority(int priority)
Change thread priority.
Definition: poweroff.c:118
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:103
poweroffShutdown
void poweroffShutdown(void)
Immidiate console shutdown. Do not call it without closing all stuff.
Definition: poweroff.c:111
thread
Definition: thcommon.h:143
_gp
void * _gp
__attribute__
typedef __attribute__
Definition: tlbfunc.c:60
poweroffInit
int poweroffInit(void)
Initializes the poweroff library.
Definition: poweroff.c:64
_iop_reboot_count
int _iop_reboot_count
tamtypes.h
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