sdk-core v2026.11.05
PS2 SDK - core tier (build-critical EE/IOP modules)
Loading...
Searching...
No Matches
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
22extern void *_gp;
23
24static poweroff_callback poweroff_cb = NULL;
25static void *poweroff_data = NULL;
26
27static u8 poffThreadStack[512 * 16] __attribute__((aligned(16)));
28
29static SifRpcClientData_t cd0;
30static struct t_SifRpcDataQueue cb_queue;
31static struct t_SifRpcServerData cb_srv;
32static int powerOffThreadId;
33
34static 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
53static 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
64int poweroffInit(void)
65{
66 ee_thread_t thread;
67 int res;
68
69 if (HasIopRebootedSinceLastCall())
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 = 0;
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}
void * _gp
Power-off library.
void poweroffSetCallback(poweroff_callback cb, void *arg)
Set callback function.
Definition poweroff.c:104
int poweroffInit(void)
Initializes the poweroff library.
Definition poweroff.c:64
void(*) poweroff_callback(void *arg)
User defined function for use in poweroffSetCallback().
Definition libpwroff.h:50
void poweroffShutdown(void)
Immidiate console shutdown. Do not call it without closing all stuff.
Definition poweroff.c:112
void poweroffChangeThreadPriority(int priority)
Change thread priority.
Definition poweroff.c:119