root/trunk/freebob/src/avdescriptor.cpp

Revision 22, 6.0 kB (checked in by pieterpalmers, 19 years ago)

- Added the following info block handlers:

Basic read functionallity only.

- Changed Makefile.am to reflect these new files
- Removed a bug from avdescriptor->readBuffer
- Added some basic tests of the new handlers (remark: the new tests could be device specific)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /* avdescriptor.cpp
2  * Copyright (C) 2004 by Pieter Palmers
3  *
4  * This file is part of FreeBob.
5  *
6  * FreeBob is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * FreeBob is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with FreeBob; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA.
19  */
20
21 #include "avdescriptor.h"
22 #include <string.h>
23 #include <errno.h>
24 #include <libavc1394/avc1394.h>
25 #include <libavc1394/avc1394_vcr.h>
26 #include "debugmodule.h"
27
28 /* should probably be attached to an AVC device,
29    a descriptor is attached to an AVC device anyway.
30    
31    a descriptor also always has a type
32 */
33
34 AvDescriptor::AvDescriptor(AvDevice *parent, quadlet_t target, unsigned int type)
35 {
36         cParent=parent;
37         iType=type;
38         qTarget=target;
39         aContents=NULL;
40         bLoaded=false;
41         iLength=0;
42         bOpen=false;
43         iAccessType=0;
44 }
45
46 AvDescriptor::~AvDescriptor()
47 {
48         if (bOpen) {
49                 Close();
50         }
51         if (aContents) {
52                 delete aContents;
53         }
54
55 }
56
57 void AvDescriptor::OpenReadOnly() {
58         quadlet_t *response;
59         quadlet_t request[3];   
60        
61         if (!cParent) {
62                 return;
63         }
64        
65         if(isOpen()) {
66                 Close();
67         }
68        
69         request[0] = AVC1394_CTYPE_CONTROL | qTarget
70                                         | AVC1394_COMMAND_OPEN_DESCRIPTOR | (iType & 0xFF);
71         request[1] = 0x01FFFFFF;
72         //fprintf(stderr, "Opening descriptor\n");
73        
74         response =  cParent->avcExecuteTransaction(request, 2, 2);
75        
76         if ((response[0]&0xFF000000)==AVC1394_RESPONSE_ACCEPTED) {
77                 bOpen=true;
78                 iAccessType=0x01;
79         } else {
80                 Close();
81                 bOpen=false;
82         }
83 }
84
85 void AvDescriptor::OpenReadWrite() {
86         quadlet_t *response;
87         quadlet_t request[3];   
88        
89         if (!cParent) {
90                 return;
91         }
92        
93         if(isOpen()) {
94                 Close();
95         }
96                
97         request[0] = AVC1394_CTYPE_CONTROL | qTarget
98                                         | AVC1394_COMMAND_OPEN_DESCRIPTOR | (iType & 0xFF);
99         request[1] = 0x03FFFFFF;
100         //fprintf(stderr, "Opening descriptor\n");
101        
102         response =  cParent->avcExecuteTransaction(request, 2, 2);
103        
104         if ((response[0]&0xFF000000)==AVC1394_RESPONSE_ACCEPTED) {
105                 bOpen=true;
106                 iAccessType=0x03;
107         } else {
108                 Close();
109                 bOpen=false;
110         }
111 }
112
113 bool AvDescriptor::canWrite() {
114         if(bOpen && (iAccessType==0x03) && cParent) {
115                 return true;
116         } else {
117                 return false;
118         }
119 }
120
121 void AvDescriptor::Close() {
122         quadlet_t *response;
123         quadlet_t request[3];   
124         if (!cParent) {
125                 return;
126         }       
127         request[0] = AVC1394_CTYPE_CONTROL | qTarget
128                                         | AVC1394_COMMAND_OPEN_DESCRIPTOR | (iType & 0xFF);
129         request[1] = 0x00FFFFFF;
130         //fprintf(stderr, "Opening descriptor\n");
131        
132         response =  cParent->avcExecuteTransaction(request, 2, 2);
133        
134         if ((response[0]&0xFF000000)==AVC1394_RESPONSE_ACCEPTED) { // should always be accepted according to spec
135                 bOpen=false;
136         }
137 }
138
139 /* Load the descriptor
140  * no error checking yet
141  */
142
143 void AvDescriptor::Load() {
144         quadlet_t *response;
145         quadlet_t request[3];   
146         unsigned int i=0;
147         unsigned int bytes_read=0;
148         unsigned char *databuffer;
149         unsigned char read_result_status;
150         unsigned int data_length_read;
151        
152         if (!cParent) {
153                 return;
154         }
155        
156         /* First determine the descriptor length */
157         request[0] = AVC1394_CTYPE_CONTROL | qTarget
158                                         | AVC1394_COMMAND_READ_DESCRIPTOR | (iType & 0xFF);
159         request[1] = 0xFFFF0000 | (0x02);
160         request[2] = (((0)&0xFFFF) << 16) |0x0000FFFF;
161         response =  cParent->avcExecuteTransaction(request, 3, 3);
162
163         iLength=response[2] & 0xFFFF;
164        
165         fprintf(stderr,"Descriptor length=0x%04X %d\n",iLength,iLength);       
166
167         // now get the rest of the descriptor
168         aContents=new unsigned char[iLength];
169        
170         while(bytes_read<iLength) {
171                 fprintf(stderr,".");   
172                 // apparently the lib modifies the request, so redefine it completely
173                 request[0] = AVC1394_CTYPE_CONTROL | qTarget
174                                                 | AVC1394_COMMAND_READ_DESCRIPTOR | (iType & 0xFF);
175                 request[1] = 0xFFFF0000 | (iLength-bytes_read)&0xFFFF;
176                 request[2] = (((bytes_read)&0xFFFF) << 16) |0x0000FFFF;
177                 response =  cParent->avcExecuteTransaction(request, 3, 3);
178                 data_length_read=(response[1]&0xFFFF);
179                 read_result_status=((response[1]>>24)&0xFF);
180                 databuffer=(unsigned char *)(response+3);
181                
182                 for (i=0;(i<data_length_read) && (bytes_read < iLength);i++) {
183                         *(aContents+bytes_read)=*(databuffer+i);
184                         bytes_read++;
185                 }
186         }
187         fprintf(stderr,"\n");   
188        
189        
190         bLoaded=true;
191 }
192
193 bool AvDescriptor::isPresent() {
194         quadlet_t *response;
195         quadlet_t request[2];   
196        
197         if (!cParent) {
198                 return false;
199         }
200        
201         request[0] = AVC1394_CTYPE_STATUS | qTarget | AVC1394_COMMAND_OPEN_DESCRIPTOR | (iType & 0xFF);
202         request[1] = 0xFFFFFFFF;
203         response =  cParent->avcExecuteTransaction(request, 2, 2);
204
205         if (((response[0] & 0xFF000000)==AVC1394_RESPONSE_NOT_IMPLEMENTED) || ((response[1] & 0xFF000000)==0x04)) {
206                 fprintf(stderr,"Descriptor not present.\n");
207                 return false;
208         }
209         return true;
210 }
211
212
213 bool AvDescriptor::isOpen() {
214         // maybe we should check this on the device instead of mirroring locally...
215         // after all it can time out
216         return bOpen;
217 }
218
219 bool AvDescriptor::isLoaded() {
220         return bLoaded;
221 }
222
223 unsigned int AvDescriptor::getLength() {
224         return iLength;
225 }
226
227 unsigned char AvDescriptor::readByte(unsigned int address) {
228         if(cParent && bLoaded && aContents) {
229                 return *(aContents+address);
230         } else {
231                 return 0; // what to do with this?
232         }
233 }
234
235 unsigned int AvDescriptor::readWord(unsigned int address) {
236         unsigned int word;
237        
238         if(cParent && bLoaded && aContents) {
239                 word=(*(aContents+address)<<8)+*(aContents+address+1);
240                 return word;
241         } else {
242                 return 0; // what to do with this?
243         }
244 }
245
246 unsigned int AvDescriptor::readBuffer(unsigned int address, unsigned int length, unsigned char *buffer) {
247         if(cParent && bLoaded && aContents && address < iLength) {
248                 if(address+length>iLength) {
249                         length=iLength-address;
250                 }
251                 memcpy((void*)buffer, (void*)(aContents+address), length);
252                 return length;
253                
254         } else {
255                 return 0;
256         }
257 }
258    
Note: See TracBrowser for help on using the browser.