ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
poweroff.c
Go to the documentation of this file.
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 
24 static void *poweroff_data = NULL;
25 
26 static u8 poffThreadStack[512 * 16] __attribute__ ((aligned(16)));
27 
28 extern int _iop_reboot_count;
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  switch(fnum){
37  case POFF_RPC_BUTTON:
38  printf("EE: power button pressed\n");
39 
40  if (poweroff_cb)
42  break;
43  }
44 
45  return buffer;
46 }
47 
48 static void PowerOffThread(void *dat)
49 {
50  static unsigned char cb_rpc_buffer[64] __attribute__((aligned(64)));
51 
55 }
56 
57 int poweroffInit(void)
58 {
59  ee_thread_t thread;
60  int res;
61  static int _init_count = -1;
62 
63  if(_init_count == _iop_reboot_count)
64  return 0;
65  _init_count = _iop_reboot_count;
66 
67  while(((res = SifBindRpc(&cd0, PWROFF_IRX, 0)) < 0) || (cd0.server == NULL))
68  nopdelay();
69 
70  // Terminate and delete any previously created threads
71  if (powerOffThreadId >= 0) {
76  powerOffThreadId = -1;
77  }
78 
80  thread.stack_size = sizeof(poffThreadStack);
81  thread.gp_reg = &_gp;
82  thread.func = PowerOffThread;
83  thread.stack = (void *)poffThreadStack;
84  thread.option = 0;
85  thread.attr = 0;
86  powerOffThreadId = CreateThread(&thread);
88 
89  int autoShutdown[4] = {0};
90  SifCallRpc(&cd0, PWROFF_ENABLE_AUTO_SHUTOFF, 0, NULL, 0, autoShutdown, sizeof(autoShutdown), NULL, NULL);
91 
92  return res;
93 }
94 
96 {
97  poweroffInit();
98 
99  poweroff_cb = cb;
100  poweroff_data = arg;
101 }
102 
104 {
105  poweroffInit();
106 
107  SifCallRpc(&cd0, PWROFF_SHUTDOWN, 0, NULL, 0, NULL, 0, NULL, NULL);
108 }
109 
111 {
113 }
static void nopdelay(void)
Definition: kernel.h:141
s32 CreateThread(ee_thread_t *thread)
s32 StartThread(s32 thread_id, void *args)
s32 TerminateThread(s32 thread_id)
s32 DeleteThread(s32 thread_id)
s32 ChangeThreadPriority(s32 thread_id, s32 priority)
#define POWEROFF_THREAD_PRIORITY
Definition: libpwroff.h:19
void(* poweroff_callback)(void *arg)
Definition: libpwroff.h:25
#define PWROFF_IRX
Definition: pwroff_rpc.h:19
#define PWROFF_ENABLE_AUTO_SHUTOFF
Definition: pwroff_rpc.h:21
#define PWROFF_SHUTDOWN
Definition: pwroff_rpc.h:20
#define POFF_RPC_BUTTON
Definition: pwroff_rpc.h:23
u8 buffer[128]
Definition: rpc_client.c:19
SifRpcServerData_t * SifRegisterRpc(SifRpcServerData_t *srv, int sid, SifRpcFunc_t func, void *buff, SifRpcFunc_t cfunc, void *cbuff, SifRpcDataQueue_t *qd)
SifRpcDataQueue_t * SifSetRpcQueue(SifRpcDataQueue_t *q, int thread_id)
SifRpcServerData_t * SifRemoveRpc(SifRpcServerData_t *sd, SifRpcDataQueue_t *queue)
void SifRpcLoop(SifRpcDataQueue_t *q)
SifRpcDataQueue_t * SifRemoveRpcQueue(SifRpcDataQueue_t *qd)
int SifBindRpc(SifRpcClientData_t *client, int rpc_number, int mode)
int SifCallRpc(SifRpcClientData_t *client, int rpc_number, int mode, void *send, int ssize, void *receive, int rsize, SifRpcEndFunc_t end_function, void *end_param)
static poweroff_callback poweroff_cb
Definition: poweroff.c:23
void poweroffSetCallback(poweroff_callback cb, void *arg)
Definition: poweroff.c:95
static SifRpcClientData_t cd0
Definition: poweroff.c:29
int poweroffInit(void)
Definition: poweroff.c:57
int _iop_reboot_count
static void * PowerOff_ee_rpc_handler(int fnum, void *buffer, int len)
Definition: poweroff.c:34
static int powerOffThreadId
Definition: poweroff.c:32
static u8 poffThreadStack[512 *16]
Definition: poweroff.c:26
static struct t_SifRpcServerData cb_srv
Definition: poweroff.c:31
void * _gp
static struct t_SifRpcDataQueue cb_queue
Definition: poweroff.c:30
static void PowerOffThread(void *dat)
Definition: poweroff.c:48
static void * poweroff_data
Definition: poweroff.c:24
void poweroffShutdown(void)
Definition: poweroff.c:103
void poweroffChangeThreadPriority(int priority)
Definition: poweroff.c:110
struct t_SifRpcServerData * server
Definition: sifrpc.h:142
void * gp_reg
Definition: kernel.h:230
u32 option
Definition: kernel.h:234
int stack_size
Definition: kernel.h:229
void * func
Definition: kernel.h:227
void * stack
Definition: kernel.h:228
u32 attr
Definition: kernel.h:233
int initial_priority
Definition: kernel.h:231
#define NULL
Definition: tamtypes.h:91
unsigned char u8
Definition: tamtypes.h:23