PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
ps2cam_rpc.c
Go to the documentation of this file.
1/*
2 _____ ___ ____
3 ____| | ____| PSX2 OpenSource Project
4 | ___| |____ (c) 2004-2005 Lion | (psxdever@hotmail.com)
5*/
6
12#include <tamtypes.h>
13#include <kernel.h>
14#include <sifrpc.h>
15#include <stdio.h>
16#include <string.h>
17#include "../include/ps2cam_rpc.h"
18
19
20
21#define PS2_CAM_RPC_ID 0x00FD000 +2
22
23
24
25static int CamInited = 0;
26static SifRpcClientData_t cdata __attribute__((aligned(64)));
27static char data[1024] __attribute__((aligned(64)));
28static char campacket[896] __attribute__((aligned(64)));
29
30ee_sema_t compSema;
31
32int sem;
33
34
35
36
37int PS2CamInit(int mode)
38{
39 int ret=0;
40 int *buf;
41 // unsigned int i;
42 // int timeout = 100000;
43
44 if(CamInited)return 0;
45
46 SifInitRpc(0);
47
48 while (((ret = SifBindRpc(&cdata, PS2_CAM_RPC_ID, 0)) >= 0) && (cdata.server == NULL))
49 nopdelay();
50
51 nopdelay();
52
53
54
55 if (ret < 0)return ret;
56
57
58
59 buf = (int *)&data[0];
60 buf[0] = mode;
61
62 printf("bind done\n");
63
64 SifCallRpc(&cdata, PS2CAM_RPC_INITIALIZE, 0, (void*)(&data[0]),4,(void*)(&data[0]),4,0,0);
65 nopdelay();
66
67 CamInited = 1;
68
69printf("init done\n");
70
71
72 compSema.init_count = 1;
73 compSema.max_count = 1;
74 compSema.option = 0;
75 sem = CreateSema(&compSema);
76
77printf("sema done\n");
78
79 return buf[0];
80}
81
82
83
84
85
87{
88 int *ret;
89
90 ret = (int *)&data[0];
91
92 SifCallRpc(&cdata, PS2CAM_RPC_GETIRXVERSION, 0, (void*)(&data[0]),4,(void*)(&data[0]),4,0,0);
93 nopdelay();
94
95 return ret[0];
96}
97
98
99
100
101
103{
104 int *ret;
105
106 ret = (int *)&data[0];
107
108 SifCallRpc(&cdata, PS2CAM_RPC_GETDEVCOUNT, 0, (void*)(&data[0]),4,(void*)(&data[0]),4,0,0);
109 nopdelay();
110
111 return ret[0];
112}
113
114
115
116
117
118int PS2CamOpenDevice(int device_index)
119{
120 int *ret;
121
122 ret = (int *)&data[0];
123
124 ret[0] = device_index;
125
126 SifCallRpc(&cdata, PS2CAM_RPC_OPENDEVICE, 0, (void*)(&data[0]),4,(void*)(&data[0]),4,0,0);
127
128 return ret[0];
129}
130
131
132
133
134int PS2CamCloseDevice(int handle)
135{
136 int *ret;
137
138 ret = (int *)&data[0];
139
140 ret[0] = handle;
141
142 SifCallRpc(&cdata, PS2CAM_RPC_CLOSEDEVICE, 0, (void*)(&data[0]),4,(void*)(&data[0]),4,0,0);
143
144 return ret[0];
145}
146
147
148
149
150
152{
153 int *ret;
154
155
156 ret = (int *)&data[0];
157
158 ret[0] = handle;
159
160
161 SifCallRpc(&cdata, PS2CAM_RPC_GETDEVSTATUS, 0, (void*)(&data[0]),4,(void*)(&data[0]),4,0,0);
162 nopdelay();
163
164 return ret[0];
165}
166
167
168
169
170int PS2CamGetDeviceInfo(int handle, PS2CAM_DEVICE_INFO *info)
171{
172 int *ret;
173 PS2CAM_DEVICE_INFO *iop_info;
174
175 ret = (int *)&data[0];
176 iop_info = (PS2CAM_DEVICE_INFO *)&ret[1];
177
178 ret[0] = handle;
179
180 memcpy(iop_info, info, info->ssize);
181
182 SifCallRpc(&cdata, PS2CAM_RPC_GETDEVINFO, 0, (void*)(&data[0]),info->ssize+4,(void*)(&data[0]),info->ssize+4,0,0);
183 nopdelay();
184
185 memcpy(info, iop_info, iop_info->ssize);
186
187 return ret[0];
188}
189
190
191
192
193
194int PS2CamSetDeviceBandwidth(int handle, char bandwidth)
195{
196 int *ret;
197
198 ret = (int *)&data[0];
199
200 ret[0] = handle;
201 ret[1] = bandwidth;
202
203
204
205 SifCallRpc(&cdata, PS2CAM_RPC_SETDEVBANDWIDTH, 0, (void*)(&data[0]),4,(void*)(&data[0]),4,0,0);
206 nopdelay();
207
208 return ret[0];
209}
210
211
212
213
214
215
216int PS2CamReadPacket(int handle)
217{
218 int *ret;
219 int *iop_addr;
220
221 WaitSema(sem);
222
223 ret = (int *)&data[0];
224
225 ret[0] = handle;
226
227 SifCallRpc(&cdata, PS2CAM_RPC_READPACKET, 0, (void*)(&data[0]),4,(void*)(&data[0]),4*2,0,0);
228
229
230 if(ret[0] < 0) return ret[0];
231
232
233 DI();
234 ee_kmode_enter();
235
236 iop_addr = (int *)(0xbc000000+ret[1]);
237
238 memcpy(&campacket[0],iop_addr, ret[0]);
239
240 ee_kmode_exit();
241 EI();
242
243 //if i add a printf here, the ps2 will exit to sony's explorer
244 SignalSema(sem);
245 return ret[0];
246}
247
248
249
250
251int PS2CamSetLEDMode(int handle, int mode)
252{
253 int *ret;
254
255 ret = (int *)&data[0];
256
257 ret[0] = handle;
258 ret[1] = mode;
259
260 SifCallRpc(&cdata, PS2CAM_RPC_SETLEDMODE, 0, (void*)(&data[0]),4,(void*)(&data[0]),4,0,0);
261
262 return ret[0];
263}
264
265
266
267int PS2CamSetDeviceConfig(int handle, PS2CAM_DEVICE_CONFIG *cfg)
268{
269 int *ret;
270
271 ret = (int *)&data[0];
272
273 ret[0] = handle;
274
275 memcpy(&ret[1], cfg, cfg->ssize);
276
277 SifCallRpc(&cdata, PS2CAM_RPC_SETDEVCONFIG, 0, (void*)(&data[0]), 4+cfg->ssize,(void*)(&data[0]),4+cfg->ssize,0,0);
278
279 return ret[0];
280}
281
282
283
284int PS2CamExtractFrame(int handle, char *buffer, int bufsize)
285{
286 static int capturing;
287 static int pos;
288 static int pic_size;
289
290 (void)bufsize;
291
292 pos = 0;
293 capturing = 0;
294
295
296
297 while(1)
298 {
299 static EYETOY_FRAME_HEAD *head;
300 static int ret;
301
302 ret = PS2CamReadPacket(handle);
303
304 // printf("packet =%d\n",ret);
305 //if read has a error return it
306 if(ret < 0) return ret;
307
308 head = (EYETOY_FRAME_HEAD *)&campacket[0];
309
310 if(head->magic2==0xff && head->magic3==0xff && ret!=0)
311 {
312 if(head->type==0x50 && head->frame==0x00)
313 {
314 // start of frame
315 // printf("SOF(%d)\n",ret);
316 if(capturing == 1)
317 {
318 return 0;
319 }
320
321 memcpy(&buffer[pos], &campacket[16], ret-16 );
322 pos += (ret-16);
323 capturing = 1;
324
325 }
326 else if(head->type==0x50 && head->frame==0x01)
327 {
328 // frame head with no data
329 // so do nothing
330
331 }
332 else if(head->type==0x51 && capturing==1)
333 {
334 //end of frame
335 // printf("EOF(%d)\n",ret);
336 pic_size = (int)(((head->Lo) + ((int)(head->Hi)<<8))<<3);
337
338
339 if(pos != pic_size)
340 {
341 return 0;
342 }
343 else
344 {
345 return pic_size;
346 }
347 }
348 else
349 {
350 //if it doesnt fit in those pos then it must be data
351 if(capturing==1)
352 {
353 memcpy(&buffer[pos], &campacket[0], ret );
354 pos += (ret);
355 }
356 }
357 }
358 else
359 {
360 // it must be data
361 if(capturing==1 && ret !=0)
362 {
363 memcpy(&buffer[pos], &campacket[0], ret );
364 pos += (ret);
365 }
366 }
367 }
368
369}
370
371
372
373
unsigned char magic3
Definition ps2cam_rpc.h:84
unsigned int ssize
Definition ps2cam_rpc.h:107
unsigned char Hi
Definition ps2cam_rpc.h:101
unsigned char type
Definition ps2cam_rpc.h:86
unsigned int ssize
Definition ps2cam_rpc.h:124
unsigned char Lo
Definition ps2cam_rpc.h:99
unsigned char frame
Definition ps2cam_rpc.h:93
unsigned char magic2
Definition ps2cam_rpc.h:82
#define PS2CAM_RPC_GETDEVSTATUS
Definition ps2cam_rpc.h:24
int PS2CamReadPacket(int handle)
Definition ps2cam_rpc.c:216
#define PS2CAM_RPC_SETDEVBANDWIDTH
Definition ps2cam_rpc.h:28
int PS2CamGetDeviceCount(void)
Definition ps2cam_rpc.c:102
int PS2CamSetLEDMode(int handle, int mode)
Definition ps2cam_rpc.c:251
int PS2CamInit(int mode)
Definition ps2cam_rpc.c:37
#define PS2CAM_RPC_GETIRXVERSION
Definition ps2cam_rpc.h:16
int PS2CamCloseDevice(int handle)
Definition ps2cam_rpc.c:134
int PS2CamSetDeviceBandwidth(int handle, char bandwidth)
Definition ps2cam_rpc.c:194
#define PS2CAM_RPC_GETDEVCOUNT
Definition ps2cam_rpc.h:20
#define PS2CAM_RPC_SETLEDMODE
Definition ps2cam_rpc.h:32
#define PS2CAM_RPC_GETDEVINFO
Definition ps2cam_rpc.h:26
#define PS2CAM_RPC_INITIALIZE
Definition ps2cam_rpc.h:18
int PS2CamGetIRXVersion(void)
Definition ps2cam_rpc.c:86
int PS2CamOpenDevice(int device_index)
Definition ps2cam_rpc.c:118
int PS2CamGetDeviceStatus(int handle)
Definition ps2cam_rpc.c:151
#define PS2CAM_RPC_READPACKET
Definition ps2cam_rpc.h:30