VirtualBox

source: vbox/trunk/src/VBox/Main/ParallelPortImpl.cpp@ 6076

Last change on this file since 6076 was 6076, checked in by vboxsync, 17 years ago

Merged dmik/s2 branch (r25959:26751) to the trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.0 KB
Line 
1/* $Id: ParallelPortImpl.cpp 6076 2007-12-14 19:23:03Z 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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will 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
29DEFINE_EMPTY_CTOR_DTOR (ParallelPort)
30
31HRESULT ParallelPort::FinalConstruct()
32{
33 return S_OK;
34}
35
36void 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 */
49HRESULT 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 */
83HRESULT 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 */
115HRESULT 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 */
144void 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 * Loads settings from the given port node.
164 * May be called once right after this object creation.
165 *
166 * @param aPortNode <Port> node.
167 *
168 * @note Locks this object for writing.
169 */
170HRESULT ParallelPort::loadSettings (const settings::Key &aPortNode)
171{
172 using namespace settings;
173
174 AssertReturn (!aPortNode.isNull(), E_FAIL);
175
176 AutoCaller autoCaller (this);
177 AssertComRCReturnRC (autoCaller.rc());
178
179 AutoLock alock (this);
180
181 /* Note: we assume that the default values for attributes of optional
182 * nodes are assigned in the Data::Data() constructor and don't do it
183 * here. It implies that this method may only be called after constructing
184 * a new BIOSSettings object while all its data fields are in the default
185 * values. Exceptions are fields whose creation time defaults don't match
186 * values that should be applied when these fields are not explicitly set
187 * in the settings file (for backwards compatibility reasons). This takes
188 * place when a setting of a newly created object must default to A while
189 * the same setting of an object loaded from the old settings file must
190 * default to B. */
191
192 /* enabled (required) */
193 mData->mEnabled = aPortNode.value <bool> ("enabled");
194 /* I/O base (required) */
195 mData->mIOBase = aPortNode.value <ULONG> ("IOBase");
196 /* IRQ (required) */
197 mData->mIRQ = aPortNode.value <ULONG> ("IRQ");
198 /* device path (may be null) */
199 /// @todo report an error if enabled is true and path is empty or null!
200 // The same applies to COMSETTER(Path).
201 mData->mPath = aPortNode.stringValue ("path");
202
203 return S_OK;
204}
205
206/**
207 * Saves settings to the given port node.
208 *
209 * Note that the given Port node is comletely empty on input.
210 *
211 * @param aPortNode <Port> node.
212 *
213 * @note Locks this object for reading.
214 */
215HRESULT ParallelPort::saveSettings (settings::Key &aPortNode)
216{
217 using namespace settings;
218
219 AssertReturn (!aPortNode.isNull(), E_FAIL);
220
221 AutoCaller autoCaller (this);
222 AssertComRCReturnRC (autoCaller.rc());
223
224 AutoReaderLock alock (this);
225
226 aPortNode.setValue <bool> ("enabled", !!mData->mEnabled);
227 aPortNode.setValue <ULONG> ("IOBase", mData->mIOBase, 16);
228 aPortNode.setValue <ULONG> ("IRQ", mData->mIRQ);
229
230 /* 'path' is optional in XML */
231 if (!mData->mPath.isEmpty())
232 aPortNode.setValue <Bstr> ("path", mData->mPath);
233
234 return S_OK;
235}
236
237/**
238 * @note Locks this object for writing.
239 */
240bool ParallelPort::rollback()
241{
242 /* sanity */
243 AutoCaller autoCaller (this);
244 AssertComRCReturn (autoCaller.rc(), false);
245
246 AutoLock alock (this);
247
248 bool changed = false;
249
250 if (mData.isBackedUp())
251 {
252 /* we need to check all data to see whether anything will be changed
253 * after rollback */
254 changed = mData.hasActualChanges();
255 mData.rollback();
256 }
257
258 return changed;
259}
260
261/**
262 * @note Locks this object for writing, together with the peer object (also
263 * for writing) if there is one.
264 */
265void ParallelPort::commit()
266{
267 /* sanity */
268 AutoCaller autoCaller (this);
269 AssertComRCReturnVoid (autoCaller.rc());
270
271 /* sanity too */
272 AutoCaller thatCaller (mPeer);
273 AssertComRCReturnVoid (thatCaller.rc());
274
275 /* lock both for writing since we modify both */
276 AutoMultiLock <2> alock (this->wlock(), AutoLock::maybeWlock (mPeer));
277
278 if (mData.isBackedUp())
279 {
280 mData.commit();
281 if (mPeer)
282 {
283 /* attach new data to the peer and reshare it */
284 mPeer->mData.attach (mData);
285 }
286 }
287}
288
289/**
290 * @note Locks this object for writing, together with the peer object
291 * represented by @a aThat (locked for reading).
292 */
293void ParallelPort::copyFrom (ParallelPort *aThat)
294{
295 AssertReturnVoid (aThat != NULL);
296
297 /* sanity */
298 AutoCaller autoCaller (this);
299 AssertComRCReturnVoid (autoCaller.rc());
300
301 /* sanity too */
302 AutoCaller thatCaller (mPeer);
303 AssertComRCReturnVoid (thatCaller.rc());
304
305 /* peer is not modified, lock it for reading */
306 AutoMultiLock <2> alock (this->wlock(), aThat->rlock());
307
308 /* this will back up current data */
309 mData.assignCopy (aThat->mData);
310}
311
312// IParallelPort properties
313/////////////////////////////////////////////////////////////////////////////
314
315STDMETHODIMP ParallelPort::COMGETTER(Enabled) (BOOL *aEnabled)
316{
317 if (!aEnabled)
318 return E_POINTER;
319
320 AutoCaller autoCaller (this);
321 CheckComRCReturnRC (autoCaller.rc());
322
323 AutoReaderLock alock (this);
324
325 *aEnabled = mData->mEnabled;
326
327 return S_OK;
328}
329
330STDMETHODIMP ParallelPort::COMSETTER(Enabled) (BOOL aEnabled)
331{
332 LogFlowThisFunc (("aEnabled=%RTbool\n", aEnabled));
333
334 AutoCaller autoCaller (this);
335 CheckComRCReturnRC (autoCaller.rc());
336
337 /* the machine needs to be mutable */
338 Machine::AutoMutableStateDependency adep (mParent);
339 CheckComRCReturnRC (adep.rc());
340
341 AutoLock alock (this);
342
343 if (mData->mEnabled != aEnabled)
344 {
345 mData.backup();
346 mData->mEnabled = aEnabled;
347
348 /* leave the lock before informing callbacks */
349 alock.unlock();
350
351 mParent->onParallelPortChange (this);
352 }
353
354 return S_OK;
355}
356
357STDMETHODIMP ParallelPort::COMGETTER(Slot) (ULONG *aSlot)
358{
359 if (!aSlot)
360 return E_POINTER;
361
362 AutoCaller autoCaller (this);
363 CheckComRCReturnRC (autoCaller.rc());
364
365 AutoReaderLock alock (this);
366
367 *aSlot = mData->mSlot;
368
369 return S_OK;
370}
371
372STDMETHODIMP ParallelPort::COMGETTER(IRQ) (ULONG *aIRQ)
373{
374 if (!aIRQ)
375 return E_POINTER;
376
377 AutoCaller autoCaller (this);
378 CheckComRCReturnRC (autoCaller.rc());
379
380 AutoReaderLock alock (this);
381
382 *aIRQ = mData->mIRQ;
383
384 return S_OK;
385}
386
387STDMETHODIMP ParallelPort::COMSETTER(IRQ)(ULONG aIRQ)
388{
389 /* check IRQ limits
390 * (when changing this, make sure it corresponds to XML schema */
391 if (aIRQ > 255)
392 return setError (E_INVALIDARG,
393 tr ("Invalid IRQ number of the parallel port %d: "
394 "%lu (must be in range [0, %lu])"),
395 mData->mSlot, aIRQ, 255);
396
397 AutoCaller autoCaller (this);
398 CheckComRCReturnRC (autoCaller.rc());
399
400 /* the machine needs to be mutable */
401 Machine::AutoMutableStateDependency adep (mParent);
402 CheckComRCReturnRC (adep.rc());
403
404 AutoLock alock (this);
405
406 HRESULT rc = S_OK;
407 bool emitChangeEvent = false;
408
409 if (mData->mIRQ != aIRQ)
410 {
411 mData.backup();
412 mData->mIRQ = aIRQ;
413 emitChangeEvent = true;
414 }
415
416 if (emitChangeEvent)
417 {
418 /* leave the lock before informing callbacks */
419 alock.unlock();
420
421 mParent->onParallelPortChange (this);
422 }
423
424 return rc;
425}
426
427STDMETHODIMP ParallelPort::COMGETTER(IOBase) (ULONG *aIOBase)
428{
429 if (!aIOBase)
430 return E_POINTER;
431
432 AutoCaller autoCaller (this);
433 CheckComRCReturnRC (autoCaller.rc());
434
435 AutoReaderLock alock (this);
436
437 *aIOBase = mData->mIOBase;
438
439 return S_OK;
440}
441
442STDMETHODIMP ParallelPort::COMSETTER(IOBase)(ULONG aIOBase)
443{
444 /* check IOBase limits
445 * (when changing this, make sure it corresponds to XML schema */
446 if (aIOBase > 0xFFFF)
447 return setError (E_INVALIDARG,
448 tr ("Invalid I/O port base address of the parallel port %d: "
449 "%lu (must be in range [0, 0x%X])"),
450 mData->mSlot, aIOBase, 0, 0xFFFF);
451
452 AutoCaller autoCaller (this);
453 CheckComRCReturnRC (autoCaller.rc());
454
455 /* the machine needs to be mutable */
456 Machine::AutoMutableStateDependency adep (mParent);
457 CheckComRCReturnRC (adep.rc());
458
459 AutoLock alock (this);
460
461 HRESULT rc = S_OK;
462 bool emitChangeEvent = false;
463
464 if (mData->mIOBase != aIOBase)
465 {
466 mData.backup();
467 mData->mIOBase = aIOBase;
468 emitChangeEvent = true;
469 }
470
471 if (emitChangeEvent)
472 {
473 /* leave the lock before informing callbacks */
474 alock.unlock();
475
476 mParent->onParallelPortChange (this);
477 }
478
479 return rc;
480}
481
482STDMETHODIMP ParallelPort::COMGETTER(Path) (BSTR *aPath)
483{
484 if (!aPath)
485 return E_POINTER;
486
487 AutoCaller autoCaller (this);
488 CheckComRCReturnRC (autoCaller.rc());
489
490 AutoReaderLock alock (this);
491
492 mData->mPath.cloneTo (aPath);
493
494 return S_OK;
495}
496
497STDMETHODIMP ParallelPort::COMSETTER(Path) (INPTR BSTR aPath)
498{
499 if (!aPath)
500 return E_POINTER;
501
502 if (!*aPath)
503 return setError (E_INVALIDARG,
504 tr ("Path of the parallel port %d may not be empty"),
505 mData->mSlot);
506
507 AutoCaller autoCaller (this);
508 CheckComRCReturnRC (autoCaller.rc());
509
510 /* the machine needs to be mutable */
511 Machine::AutoMutableStateDependency adep (mParent);
512 CheckComRCReturnRC (adep.rc());
513
514 AutoLock alock (this);
515
516 if (mData->mPath != aPath)
517 {
518 mData.backup();
519 mData->mPath = aPath;
520
521 /* leave the lock before informing callbacks */
522 alock.unlock();
523
524 return mParent->onParallelPortChange (this);
525 }
526
527 return S_OK;
528}
529
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette