1 | /* $Id: ParallelPortImpl.cpp 5659 2007-11-09 19:26:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "ParallelPortImpl.h"
|
---|
19 | #include "MachineImpl.h"
|
---|
20 | #include "VirtualBoxImpl.h"
|
---|
21 | #include "Logging.h"
|
---|
22 |
|
---|
23 | #include <iprt/string.h>
|
---|
24 | #include <iprt/cpputils.h>
|
---|
25 |
|
---|
26 | // constructor / destructor
|
---|
27 | /////////////////////////////////////////////////////////////////////////////
|
---|
28 |
|
---|
29 | DEFINE_EMPTY_CTOR_DTOR (ParallelPort)
|
---|
30 |
|
---|
31 | HRESULT ParallelPort::FinalConstruct()
|
---|
32 | {
|
---|
33 | return S_OK;
|
---|
34 | }
|
---|
35 |
|
---|
36 | void ParallelPort::FinalRelease()
|
---|
37 | {
|
---|
38 | uninit();
|
---|
39 | }
|
---|
40 |
|
---|
41 | // public initializer/uninitializer for internal purposes only
|
---|
42 | /////////////////////////////////////////////////////////////////////////////
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Initializes the Parallel Port object.
|
---|
46 | *
|
---|
47 | * @param aParent Handle of the parent object.
|
---|
48 | */
|
---|
49 | HRESULT ParallelPort::init (Machine *aParent, ULONG aSlot)
|
---|
50 | {
|
---|
51 | LogFlowThisFunc (("aParent=%p, aSlot=%d\n", aParent, aSlot));
|
---|
52 |
|
---|
53 | ComAssertRet (aParent, E_INVALIDARG);
|
---|
54 |
|
---|
55 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
56 | AutoInitSpan autoInitSpan (this);
|
---|
57 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
58 |
|
---|
59 | unconst (mParent) = aParent;
|
---|
60 | /* mPeer is left null */
|
---|
61 |
|
---|
62 | mData.allocate();
|
---|
63 |
|
---|
64 | /* initialize data */
|
---|
65 | mData->mSlot = aSlot;
|
---|
66 |
|
---|
67 | /* Confirm a successful initialization */
|
---|
68 | autoInitSpan.setSucceeded();
|
---|
69 |
|
---|
70 | return S_OK;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Initializes the Parallel Port object given another serial port object
|
---|
75 | * (a kind of copy constructor). This object shares data with
|
---|
76 | * the object passed as an argument.
|
---|
77 | *
|
---|
78 | * @note This object must be destroyed before the original object
|
---|
79 | * it shares data with is destroyed.
|
---|
80 | *
|
---|
81 | * @note Locks @a aThat object for reading.
|
---|
82 | */
|
---|
83 | HRESULT ParallelPort::init (Machine *aParent, ParallelPort *aThat)
|
---|
84 | {
|
---|
85 | LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
|
---|
86 |
|
---|
87 | ComAssertRet (aParent && aThat, E_INVALIDARG);
|
---|
88 |
|
---|
89 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
90 | AutoInitSpan autoInitSpan (this);
|
---|
91 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
92 |
|
---|
93 | unconst (mParent) = aParent;
|
---|
94 | unconst (mPeer) = aThat;
|
---|
95 |
|
---|
96 | AutoCaller thatCaller (aThat);
|
---|
97 | AssertComRCReturnRC (thatCaller.rc());
|
---|
98 |
|
---|
99 | AutoReaderLock thatLock (aThat);
|
---|
100 | mData.share (aThat->mData);
|
---|
101 |
|
---|
102 | /* Confirm a successful initialization */
|
---|
103 | autoInitSpan.setSucceeded();
|
---|
104 |
|
---|
105 | return S_OK;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Initializes the guest object given another guest object
|
---|
110 | * (a kind of copy constructor). This object makes a private copy of data
|
---|
111 | * of the original object passed as an argument.
|
---|
112 | *
|
---|
113 | * @note Locks @a aThat object for reading.
|
---|
114 | */
|
---|
115 | HRESULT ParallelPort::initCopy (Machine *aParent, ParallelPort *aThat)
|
---|
116 | {
|
---|
117 | LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
|
---|
118 |
|
---|
119 | ComAssertRet (aParent && aThat, E_INVALIDARG);
|
---|
120 |
|
---|
121 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
122 | AutoInitSpan autoInitSpan (this);
|
---|
123 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
124 |
|
---|
125 | unconst (mParent) = aParent;
|
---|
126 | /* mPeer is left null */
|
---|
127 |
|
---|
128 | AutoCaller thatCaller (aThat);
|
---|
129 | AssertComRCReturnRC (thatCaller.rc());
|
---|
130 |
|
---|
131 | AutoReaderLock thatLock (aThat);
|
---|
132 | mData.attachCopy (aThat->mData);
|
---|
133 |
|
---|
134 | /* Confirm a successful initialization */
|
---|
135 | autoInitSpan.setSucceeded();
|
---|
136 |
|
---|
137 | return S_OK;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
142 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
143 | */
|
---|
144 | void ParallelPort::uninit()
|
---|
145 | {
|
---|
146 | LogFlowThisFunc (("\n"));
|
---|
147 |
|
---|
148 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
149 | AutoUninitSpan autoUninitSpan (this);
|
---|
150 | if (autoUninitSpan.uninitDone())
|
---|
151 | return;
|
---|
152 |
|
---|
153 | mData.free();
|
---|
154 |
|
---|
155 | unconst (mPeer).setNull();
|
---|
156 | unconst (mParent).setNull();
|
---|
157 | }
|
---|
158 |
|
---|
159 | // public methods only for internal purposes
|
---|
160 | ////////////////////////////////////////////////////////////////////////////////
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * @note Locks this object for writing.
|
---|
164 | */
|
---|
165 | bool ParallelPort::rollback()
|
---|
166 | {
|
---|
167 | /* sanity */
|
---|
168 | AutoCaller autoCaller (this);
|
---|
169 | AssertComRCReturn (autoCaller.rc(), false);
|
---|
170 |
|
---|
171 | AutoLock alock (this);
|
---|
172 |
|
---|
173 | bool changed = false;
|
---|
174 |
|
---|
175 | if (mData.isBackedUp())
|
---|
176 | {
|
---|
177 | /* we need to check all data to see whether anything will be changed
|
---|
178 | * after rollback */
|
---|
179 | changed = mData.hasActualChanges();
|
---|
180 | mData.rollback();
|
---|
181 | }
|
---|
182 |
|
---|
183 | return changed;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * @note Locks this object for writing, together with the peer object (also
|
---|
188 | * for writing) if there is one.
|
---|
189 | */
|
---|
190 | void ParallelPort::commit()
|
---|
191 | {
|
---|
192 | /* sanity */
|
---|
193 | AutoCaller autoCaller (this);
|
---|
194 | AssertComRCReturnVoid (autoCaller.rc());
|
---|
195 |
|
---|
196 | /* sanity too */
|
---|
197 | AutoCaller thatCaller (mPeer);
|
---|
198 | AssertComRCReturnVoid (thatCaller.rc());
|
---|
199 |
|
---|
200 | /* lock both for writing since we modify both */
|
---|
201 | AutoMultiLock <2> alock (this->wlock(), AutoLock::maybeWlock (mPeer));
|
---|
202 |
|
---|
203 | if (mData.isBackedUp())
|
---|
204 | {
|
---|
205 | mData.commit();
|
---|
206 | if (mPeer)
|
---|
207 | {
|
---|
208 | /* attach new data to the peer and reshare it */
|
---|
209 | mPeer->mData.attach (mData);
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * @note Locks this object for writing, together with the peer object
|
---|
216 | * represented by @a aThat (locked for reading).
|
---|
217 | */
|
---|
218 | void ParallelPort::copyFrom (ParallelPort *aThat)
|
---|
219 | {
|
---|
220 | AssertReturnVoid (aThat != NULL);
|
---|
221 |
|
---|
222 | /* sanity */
|
---|
223 | AutoCaller autoCaller (this);
|
---|
224 | AssertComRCReturnVoid (autoCaller.rc());
|
---|
225 |
|
---|
226 | /* sanity too */
|
---|
227 | AutoCaller thatCaller (mPeer);
|
---|
228 | AssertComRCReturnVoid (thatCaller.rc());
|
---|
229 |
|
---|
230 | /* peer is not modified, lock it for reading */
|
---|
231 | AutoMultiLock <2> alock (this->wlock(), aThat->rlock());
|
---|
232 |
|
---|
233 | /* this will back up current data */
|
---|
234 | mData.assignCopy (aThat->mData);
|
---|
235 | }
|
---|
236 |
|
---|
237 | HRESULT ParallelPort::loadSettings (CFGNODE aNode, ULONG aSlot)
|
---|
238 | {
|
---|
239 | LogFlowThisFunc (("aMachine=%p\n", aNode));
|
---|
240 |
|
---|
241 | AssertReturn (aNode, E_FAIL);
|
---|
242 |
|
---|
243 | AutoCaller autoCaller (this);
|
---|
244 | AssertComRCReturnRC (autoCaller.rc());
|
---|
245 |
|
---|
246 | AutoLock alock (this);
|
---|
247 |
|
---|
248 | CFGNODE portNode = NULL;
|
---|
249 | CFGLDRGetChildNode (aNode, "Port", aSlot, &portNode);
|
---|
250 |
|
---|
251 | /* slot number (required) */
|
---|
252 | /* slot unicity is guaranteed by XML Schema */
|
---|
253 | uint32_t uSlot = 0;
|
---|
254 | CFGLDRQueryUInt32 (portNode, "slot", &uSlot);
|
---|
255 | /* enabled (required) */
|
---|
256 | bool fEnabled = false;
|
---|
257 | CFGLDRQueryBool (portNode, "enabled", &fEnabled);
|
---|
258 | /* I/O base (required) */
|
---|
259 | uint32_t uIOBase;
|
---|
260 | CFGLDRQueryUInt32 (portNode, "IOBase", &uIOBase);
|
---|
261 | /* IRQ (required) */
|
---|
262 | uint32_t uIRQ;
|
---|
263 | CFGLDRQueryUInt32 (portNode, "IRQ", &uIRQ);
|
---|
264 | /* device path */
|
---|
265 | Bstr path;
|
---|
266 | CFGLDRQueryBSTR (portNode, "path", path.asOutParam());
|
---|
267 |
|
---|
268 | mData->mEnabled = fEnabled;
|
---|
269 | mData->mSlot = uSlot;
|
---|
270 | mData->mIOBase = uIOBase;
|
---|
271 | mData->mIRQ = uIRQ;
|
---|
272 | mData->mPath = path;
|
---|
273 |
|
---|
274 | return S_OK;
|
---|
275 | }
|
---|
276 |
|
---|
277 | HRESULT ParallelPort::saveSettings (CFGNODE aNode)
|
---|
278 | {
|
---|
279 | AssertReturn (aNode, E_FAIL);
|
---|
280 |
|
---|
281 | AutoCaller autoCaller (this);
|
---|
282 | CheckComRCReturnRC (autoCaller.rc());
|
---|
283 |
|
---|
284 | AutoReaderLock alock (this);
|
---|
285 |
|
---|
286 | CFGNODE portNode = 0;
|
---|
287 | int vrc = CFGLDRAppendChildNode (aNode, "Port", &portNode);
|
---|
288 | ComAssertRCRet (vrc, E_FAIL);
|
---|
289 |
|
---|
290 | CFGLDRSetUInt32 (portNode, "slot", mData->mSlot);
|
---|
291 | CFGLDRSetBool (portNode, "enabled", !!mData->mEnabled);
|
---|
292 | CFGLDRSetUInt32Ex (portNode, "IOBase", mData->mIOBase, 16);
|
---|
293 | CFGLDRSetUInt32 (portNode, "IRQ", mData->mIRQ);
|
---|
294 | CFGLDRSetBSTR (portNode, "path", mData->mPath);
|
---|
295 |
|
---|
296 | return S_OK;
|
---|
297 | }
|
---|
298 |
|
---|
299 | // IParallelPort properties
|
---|
300 | /////////////////////////////////////////////////////////////////////////////
|
---|
301 |
|
---|
302 | STDMETHODIMP ParallelPort::COMGETTER(Enabled) (BOOL *aEnabled)
|
---|
303 | {
|
---|
304 | if (!aEnabled)
|
---|
305 | return E_POINTER;
|
---|
306 |
|
---|
307 | AutoCaller autoCaller (this);
|
---|
308 | CheckComRCReturnRC (autoCaller.rc());
|
---|
309 |
|
---|
310 | AutoReaderLock alock (this);
|
---|
311 |
|
---|
312 | *aEnabled = mData->mEnabled;
|
---|
313 |
|
---|
314 | return S_OK;
|
---|
315 | }
|
---|
316 |
|
---|
317 | STDMETHODIMP ParallelPort::COMSETTER(Enabled) (BOOL aEnabled)
|
---|
318 | {
|
---|
319 | LogFlowThisFunc (("aEnabled=%RTbool\n", aEnabled));
|
---|
320 |
|
---|
321 | AutoCaller autoCaller (this);
|
---|
322 | CheckComRCReturnRC (autoCaller.rc());
|
---|
323 |
|
---|
324 | /* the machine needs to be mutable */
|
---|
325 | Machine::AutoMutableStateDependency adep (mParent);
|
---|
326 | CheckComRCReturnRC (adep.rc());
|
---|
327 |
|
---|
328 | AutoLock alock (this);
|
---|
329 |
|
---|
330 | if (mData->mEnabled != aEnabled)
|
---|
331 | {
|
---|
332 | mData.backup();
|
---|
333 | mData->mEnabled = aEnabled;
|
---|
334 |
|
---|
335 | /* leave the lock before informing callbacks */
|
---|
336 | alock.unlock();
|
---|
337 |
|
---|
338 | mParent->onParallelPortChange (this);
|
---|
339 | }
|
---|
340 |
|
---|
341 | return S_OK;
|
---|
342 | }
|
---|
343 |
|
---|
344 | STDMETHODIMP ParallelPort::COMGETTER(Slot) (ULONG *aSlot)
|
---|
345 | {
|
---|
346 | if (!aSlot)
|
---|
347 | return E_POINTER;
|
---|
348 |
|
---|
349 | AutoCaller autoCaller (this);
|
---|
350 | CheckComRCReturnRC (autoCaller.rc());
|
---|
351 |
|
---|
352 | AutoReaderLock alock (this);
|
---|
353 |
|
---|
354 | *aSlot = mData->mSlot;
|
---|
355 |
|
---|
356 | return S_OK;
|
---|
357 | }
|
---|
358 |
|
---|
359 | STDMETHODIMP ParallelPort::COMGETTER(IRQ) (ULONG *aIRQ)
|
---|
360 | {
|
---|
361 | if (!aIRQ)
|
---|
362 | return E_POINTER;
|
---|
363 |
|
---|
364 | AutoCaller autoCaller (this);
|
---|
365 | CheckComRCReturnRC (autoCaller.rc());
|
---|
366 |
|
---|
367 | AutoReaderLock alock (this);
|
---|
368 |
|
---|
369 | *aIRQ = mData->mIRQ;
|
---|
370 |
|
---|
371 | return S_OK;
|
---|
372 | }
|
---|
373 |
|
---|
374 | STDMETHODIMP ParallelPort::COMSETTER(IRQ)(ULONG aIRQ)
|
---|
375 | {
|
---|
376 | /* check IRQ limits
|
---|
377 | * (when changing this, make sure it corresponds to XML schema */
|
---|
378 | if (aIRQ > 255)
|
---|
379 | return setError (E_INVALIDARG,
|
---|
380 | tr ("Invalid IRQ number: %lu (must be in range [0, %lu])"),
|
---|
381 | aIRQ, 255);
|
---|
382 |
|
---|
383 | AutoCaller autoCaller (this);
|
---|
384 | CheckComRCReturnRC (autoCaller.rc());
|
---|
385 |
|
---|
386 | /* the machine needs to be mutable */
|
---|
387 | Machine::AutoMutableStateDependency adep (mParent);
|
---|
388 | CheckComRCReturnRC (adep.rc());
|
---|
389 |
|
---|
390 | AutoLock alock (this);
|
---|
391 |
|
---|
392 | HRESULT rc = S_OK;
|
---|
393 | bool emitChangeEvent = false;
|
---|
394 |
|
---|
395 | if (mData->mIRQ != aIRQ)
|
---|
396 | {
|
---|
397 | mData.backup();
|
---|
398 | mData->mIRQ = aIRQ;
|
---|
399 | emitChangeEvent = true;
|
---|
400 | }
|
---|
401 |
|
---|
402 | if (emitChangeEvent)
|
---|
403 | {
|
---|
404 | /* leave the lock before informing callbacks */
|
---|
405 | alock.unlock();
|
---|
406 |
|
---|
407 | mParent->onParallelPortChange (this);
|
---|
408 | }
|
---|
409 |
|
---|
410 | return rc;
|
---|
411 | }
|
---|
412 |
|
---|
413 | STDMETHODIMP ParallelPort::COMGETTER(IOBase) (ULONG *aIOBase)
|
---|
414 | {
|
---|
415 | if (!aIOBase)
|
---|
416 | return E_POINTER;
|
---|
417 |
|
---|
418 | AutoCaller autoCaller (this);
|
---|
419 | CheckComRCReturnRC (autoCaller.rc());
|
---|
420 |
|
---|
421 | AutoReaderLock alock (this);
|
---|
422 |
|
---|
423 | *aIOBase = mData->mIOBase;
|
---|
424 |
|
---|
425 | return S_OK;
|
---|
426 | }
|
---|
427 |
|
---|
428 | STDMETHODIMP ParallelPort::COMSETTER(IOBase)(ULONG aIOBase)
|
---|
429 | {
|
---|
430 | /* check IOBase limits
|
---|
431 | * (when changing this, make sure it corresponds to XML schema */
|
---|
432 | if (aIOBase > 0xFFFF)
|
---|
433 | return setError (E_INVALIDARG,
|
---|
434 | tr ("Invalid I/O port base address: %lu (must be in range [0, 0x%X])"),
|
---|
435 | aIOBase, 0, 0xFFFF);
|
---|
436 |
|
---|
437 | AutoCaller autoCaller (this);
|
---|
438 | CheckComRCReturnRC (autoCaller.rc());
|
---|
439 |
|
---|
440 | /* the machine needs to be mutable */
|
---|
441 | Machine::AutoMutableStateDependency adep (mParent);
|
---|
442 | CheckComRCReturnRC (adep.rc());
|
---|
443 |
|
---|
444 | AutoLock alock (this);
|
---|
445 |
|
---|
446 | HRESULT rc = S_OK;
|
---|
447 | bool emitChangeEvent = false;
|
---|
448 |
|
---|
449 | if (mData->mIOBase != aIOBase)
|
---|
450 | {
|
---|
451 | mData.backup();
|
---|
452 | mData->mIOBase = aIOBase;
|
---|
453 | emitChangeEvent = true;
|
---|
454 | }
|
---|
455 |
|
---|
456 | if (emitChangeEvent)
|
---|
457 | {
|
---|
458 | /* leave the lock before informing callbacks */
|
---|
459 | alock.unlock();
|
---|
460 |
|
---|
461 | mParent->onParallelPortChange (this);
|
---|
462 | }
|
---|
463 |
|
---|
464 | return rc;
|
---|
465 | }
|
---|
466 |
|
---|
467 | STDMETHODIMP ParallelPort::COMGETTER(Path) (BSTR *aPath)
|
---|
468 | {
|
---|
469 | if (!aPath)
|
---|
470 | return E_POINTER;
|
---|
471 |
|
---|
472 | AutoCaller autoCaller (this);
|
---|
473 | CheckComRCReturnRC (autoCaller.rc());
|
---|
474 |
|
---|
475 | AutoReaderLock alock (this);
|
---|
476 |
|
---|
477 | mData->mPath.cloneTo (aPath);
|
---|
478 |
|
---|
479 | return S_OK;
|
---|
480 | }
|
---|
481 |
|
---|
482 | STDMETHODIMP ParallelPort::COMSETTER(Path) (INPTR BSTR aPath)
|
---|
483 | {
|
---|
484 | if (!aPath)
|
---|
485 | return E_POINTER;
|
---|
486 |
|
---|
487 | if (!*aPath)
|
---|
488 | return setError (E_INVALIDARG,
|
---|
489 | tr ("Parallel port path cannot be empty"));
|
---|
490 |
|
---|
491 | AutoCaller autoCaller (this);
|
---|
492 | CheckComRCReturnRC (autoCaller.rc());
|
---|
493 |
|
---|
494 | /* the machine needs to be mutable */
|
---|
495 | Machine::AutoMutableStateDependency adep (mParent);
|
---|
496 | CheckComRCReturnRC (adep.rc());
|
---|
497 |
|
---|
498 | AutoLock alock (this);
|
---|
499 |
|
---|
500 | if (mData->mPath != aPath)
|
---|
501 | {
|
---|
502 | mData.backup();
|
---|
503 | mData->mPath = aPath;
|
---|
504 |
|
---|
505 | /* leave the lock before informing callbacks */
|
---|
506 | alock.unlock();
|
---|
507 |
|
---|
508 | return mParent->onParallelPortChange (this);
|
---|
509 | }
|
---|
510 |
|
---|
511 | return S_OK;
|
---|
512 | }
|
---|
513 |
|
---|