ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
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 
25 static int CamInited = 0;
26 static SifRpcClientData_t cdata __attribute__((aligned(64)));
27 static char data[1024] __attribute__((aligned(64)));
28 static char campacket[896] __attribute__((aligned(64)));
29 
31 
32 int sem;
33 
34 
35 
36 
37 int 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 
69 printf("init done\n");
70 
71 
72  compSema.init_count = 1;
73  compSema.max_count = 1;
74  compSema.option = 0;
76 
77 printf("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 
118 int 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 
134 int 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 
151 int PS2CamGetDeviceStatus(int handle)
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 
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 
194 int 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 
216 int 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 
251 int 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 
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 
284 int PS2CamExtractFrame(int handle, char *buffer, int bufsize)
285 {
286  static EYETOY_FRAME_HEAD *head;
287  static int capturing;
288  static int pos;
289  static int ret;
290  static int pic_size;
291 
292  pos = 0;
293  capturing = 0;
294 
295 
296 
297  while(1)
298  {
299  ret = PS2CamReadPacket(handle);
300 
301  // printf("packet =%d\n",ret);
302  //if read has a error return it
303  if(ret < 0) return ret;
304 
305  head = (EYETOY_FRAME_HEAD *)&campacket[0];
306 
307  if(head->magic2==0xff && head->magic3==0xff && ret!=0)
308  {
309  if(head->type==0x50 && head->frame==0x00)
310  {
311  // start of frame
312  // printf("SOF(%d)\n",ret);
313  if(capturing == 1)
314  {
315  return 0;
316  }
317 
318  memcpy(&buffer[pos], &campacket[16], ret-16 );
319  pos += (ret-16);
320  capturing = 1;
321 
322  }
323  else if(head->type==0x50 && head->frame==0x01)
324  {
325  // frame head with no data
326  // so do nothing
327 
328  }
329  else if(head->type==0x51 && capturing==1)
330  {
331  //end of frame
332  // printf("EOF(%d)\n",ret);
333  pic_size = (int)(((head->Lo) + ((int)(head->Hi)<<8))<<3);
334 
335 
336  if(pos != pic_size)
337  {
338  return 0;
339  }
340  else
341  {
342  return pic_size;
343  }
344  }
345  else
346  {
347  //if it doesnt fit in those pos then it must be data
348  if(capturing==1)
349  {
350  memcpy(&buffer[pos], &campacket[0], ret );
351  pos += (ret);
352  }
353  }
354  }
355  else
356  {
357  // it must be data
358  if(capturing==1 && ret !=0)
359  {
360  memcpy(&buffer[pos], &campacket[0], ret );
361  pos += (ret);
362  }
363  }
364  }
365 
366 }
367 
368 
369 
370 
PS2CAM_DEVICE_CONFIG cfg
Definition: camera-test.c:18
jpgData * info
Definition: camera-test.c:14
s32 CreateSema(ee_sema_t *sema)
static int ee_kmode_exit()
Definition: kernel.h:198
s32 SignalSema(s32 sema_id)
static void nopdelay(void)
Definition: kernel.h:141
static int ee_kmode_enter()
Definition: kernel.h:181
s32 WaitSema(s32 sema_id)
#define DI
Definition: kernel.h:24
#define EI
Definition: kernel.h:25
struct @29::@30 pos
int PS2CamExtractFrame(int handle, char *buffer, int bufsize)
Definition: ps2cam_rpc.c:284
int PS2CamReadPacket(int handle)
Definition: ps2cam_rpc.c:216
int PS2CamGetDeviceCount(void)
Definition: ps2cam_rpc.c:102
int PS2CamSetLEDMode(int handle, int mode)
Definition: ps2cam_rpc.c:251
static char campacket[896]
Definition: ps2cam_rpc.c:28
int PS2CamInit(int mode)
Definition: ps2cam_rpc.c:37
int PS2CamCloseDevice(int handle)
Definition: ps2cam_rpc.c:134
int PS2CamGetDeviceInfo(int handle, PS2CAM_DEVICE_INFO *info)
Definition: ps2cam_rpc.c:170
static int CamInited
Definition: ps2cam_rpc.c:25
int PS2CamSetDeviceBandwidth(int handle, char bandwidth)
Definition: ps2cam_rpc.c:194
static SifRpcClientData_t cdata
Definition: ps2cam_rpc.c:26
int PS2CamGetIRXVersion(void)
Definition: ps2cam_rpc.c:86
int PS2CamSetDeviceConfig(int handle, PS2CAM_DEVICE_CONFIG *cfg)
Definition: ps2cam_rpc.c:267
int PS2CamOpenDevice(int device_index)
Definition: ps2cam_rpc.c:118
static char data[1024]
Definition: ps2cam_rpc.c:27
ee_sema_t compSema
Definition: ps2cam_rpc.c:30
int PS2CamGetDeviceStatus(int handle)
Definition: ps2cam_rpc.c:151
int sem
Definition: ps2cam_rpc.c:32
#define PS2_CAM_RPC_ID
Definition: ps2cam_rpc.c:21
#define PS2CAM_RPC_GETDEVSTATUS
Definition: ps2cam_rpc.h:24
#define PS2CAM_RPC_CLOSEDEVICE
Definition: ps2cam_rpc.h:22
#define PS2CAM_RPC_SETDEVBANDWIDTH
Definition: ps2cam_rpc.h:28
#define PS2CAM_RPC_OPENDEVICE
Definition: ps2cam_rpc.h:21
#define PS2CAM_RPC_GETIRXVERSION
Definition: ps2cam_rpc.h:16
#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_SETDEVCONFIG
Definition: ps2cam_rpc.h:34
#define PS2CAM_RPC_INITIALIZE
Definition: ps2cam_rpc.h:18
#define PS2CAM_RPC_READPACKET
Definition: ps2cam_rpc.h:30
s32 mode
Definition: rpc_client.c:15
u8 buffer[128]
Definition: rpc_client.c:19
void SifInitRpc(int mode)
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)
unsigned char magic3
Definition: ps2cam_rpc.h:84
unsigned char Hi
Definition: ps2cam_rpc.h:101
unsigned char type
Definition: ps2cam_rpc.h:86
unsigned char Lo
Definition: ps2cam_rpc.h:99
unsigned char frame
Definition: ps2cam_rpc.h:93
unsigned char magic2
Definition: ps2cam_rpc.h:82
unsigned int ssize
Definition: ps2cam_rpc.h:124
unsigned int ssize
Definition: ps2cam_rpc.h:107
struct t_SifRpcServerData * server
Definition: sifrpc.h:142
int init_count
Definition: kernel.h:218
int max_count
Definition: kernel.h:217
u32 option
Definition: kernel.h:221
#define NULL
Definition: tamtypes.h:91