aboutsummaryrefslogtreecommitdiff
path: root/backends/platform/ps2/iop/CoDyVDfs/iop/rpcfs.c
blob: c46d452d2c0e20fb020f59c672807babdfaa97a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

#include <sifman.h>
#include <sifrpc.h>
#include <thbase.h>
#include <stdio.h>
#include "codyvdfs.h"
#include "../common/codyvdirx.h"

struct t_SifRpcDataQueue qd;
struct t_SifRpcServerData sd0;
static uint8 rpcBuffer[64];

void *rpcServer(int func, void *data, int size);
void rpcThread(void *param);

int initRpc(void) {
	iop_thread_t thread;
	int tid;

	thread.attr = TH_C;
	thread.thread = rpcThread;
	thread.priority = 40;
	thread.stacksize = 0x1000;
	thread.attr = 0;

	tid = CreateThread(&thread);
	if (tid >= 0) 
		StartThread(tid, 0);
	else {
		printf("Unable to start RPC Thread!\n");
		return -1;
	}
	return 0;
}

void rpcThread(void *param) {
	SifInitRpc(0);
	SifSetRpcQueue(&qd, GetThreadId());
	SifRegisterRpc(&sd0, CDVDFS_IRX_ID, rpcServer, (void *)rpcBuffer, 0, 0, &qd);
	SifRpcLoop(&qd);
}

void *rpcReadClock(void *data) {
	CdReadClock((cd_clock_t *)data);
	return data;
}

void *driveStop(void *data) {
	if (CdStop() == 1) {
		if (CdSync(0) == 0) {
			*(int*)data = CdGetError();
		} else
			*(int*)data = -0x100;
	} else
		*(int*)data = -0x101;
	return data;
}

void *driveStandby(void *data) {
	int type;
	if (CdStandby() == 1) {
		if (CdSync(0) == 0) {
			*(int*)data = CdGetError();
		} else
			*(int*)data = -0x100;
	} else
		*(int*)data = -0x101;

	do {	// wait until drive detected disc type
		type = CdGetDiskType();
		if (DISC_NOT_READY(type))
			DelayThread(10 * 1000);
	} while (DISC_NOT_READY(type));
	printf("Standby: Disc type: %02X\n", type);

	return data;
}

void *rpcServer(int func, void *data, int size) {
	switch (func) {
		case READ_RTC:
			return rpcReadClock(data);
		case DRIVE_STOP:
			return driveStop(data);
		case DRIVE_STANDBY:
			return driveStandby(data);
		default:
			printf("Unknown RPC command %d\n", func);
			return NULL;
	}
	return NULL;
}