VirtualBox

source: vbox/trunk/src/VBox/Main/SerialPortImpl.cpp@ 4131

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

Biggest check-in ever. New source code headers for all (C) innotek files.

File size: 12.5 KB
Line 
1/** @file
2 *
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 "SerialPortImpl.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 (SerialPort)
30
31HRESULT SerialPort::FinalConstruct()
32{
33 return S_OK;
34}
35
36void SerialPort::FinalRelease()
37{
38 uninit();
39}
40
41// public initializer/uninitializer for internal purposes only
42/////////////////////////////////////////////////////////////////////////////
43
44/**
45 * Initializes the Serial Port object.
46 *
47 * @param aParent Handle of the parent object.
48 */
49HRESULT SerialPort::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 Serial 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 SerialPort::init (Machine *aParent, SerialPort *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 SerialPort::initCopy (Machine *aParent, SerialPort *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 SerialPort::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 */
165bool SerialPort::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 */
190void SerialPort::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 */
218void SerialPort::copyFrom (SerialPort *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
237HRESULT SerialPort::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 /* name of the pipe (required) */
265 Bstr pipe;
266 CFGLDRQueryBSTR (portNode, "pipe", pipe.asOutParam());
267 bool fServer = true;
268 CFGLDRQueryBool (portNode, "server", &fServer);
269
270 mData->mEnabled = fEnabled;
271 mData->mSlot = uSlot;
272 mData->mIOBase = uIOBase;
273 mData->mIRQ = uIRQ;
274 mData->mPipe = pipe;
275 mData->mServer = fServer;
276
277 return S_OK;
278}
279
280HRESULT SerialPort::saveSettings (CFGNODE aNode)
281{
282 AssertReturn (aNode, E_FAIL);
283
284 AutoCaller autoCaller (this);
285 CheckComRCReturnRC (autoCaller.rc());
286
287 AutoReaderLock alock (this);
288
289 CFGNODE portNode = 0;
290 int vrc = CFGLDRAppendChildNode (aNode, "Port", &portNode);
291 ComAssertRCRet (vrc, E_FAIL);
292
293 CFGLDRSetUInt32 (portNode, "slot", mData->mSlot);
294 CFGLDRSetBool (portNode, "enabled", !!mData->mEnabled);
295 CFGLDRSetUInt32 (portNode, "IOBase", mData->mIOBase);
296 CFGLDRSetUInt32 (portNode, "IRQ", mData->mIRQ);
297 CFGLDRSetBSTR (portNode, "pipe", mData->mPipe);
298 CFGLDRSetBool (portNode, "server", !!mData->mServer);
299
300 return S_OK;
301}
302
303// ISerialPort properties
304/////////////////////////////////////////////////////////////////////////////
305
306STDMETHODIMP SerialPort::COMGETTER(Enabled) (BOOL *aEnabled)
307{
308 if (!aEnabled)
309 return E_POINTER;
310
311 AutoCaller autoCaller (this);
312 CheckComRCReturnRC (autoCaller.rc());
313
314 AutoReaderLock alock (this);
315
316 *aEnabled = mData->mEnabled;
317
318 return S_OK;
319}
320
321STDMETHODIMP SerialPort::COMSETTER(Enabled) (BOOL aEnabled)
322{
323 LogFlowThisFunc (("aEnabled=%RTbool\n", aEnabled));
324
325 AutoCaller autoCaller (this);
326 CheckComRCReturnRC (autoCaller.rc());
327
328 /* the machine needs to be mutable */
329 Machine::AutoMutableStateDependency adep (mParent);
330 CheckComRCReturnRC (adep.rc());
331
332 AutoLock alock (this);
333
334 if (mData->mEnabled != aEnabled)
335 {
336 mData.backup();
337 mData->mEnabled = aEnabled;
338
339 /* leave the lock before informing callbacks */
340 alock.unlock();
341
342 mParent->onSerialPortChange (this);
343 }
344
345 return S_OK;
346}
347
348STDMETHODIMP SerialPort::COMGETTER(Slot) (ULONG *aSlot)
349{
350 if (!aSlot)
351 return E_POINTER;
352
353 AutoCaller autoCaller (this);
354 CheckComRCReturnRC (autoCaller.rc());
355
356 AutoReaderLock alock (this);
357
358 *aSlot = mData->mSlot;
359
360 return S_OK;
361}
362
363STDMETHODIMP SerialPort::COMGETTER(IRQ) (ULONG *aIRQ)
364{
365 if (!aIRQ)
366 return E_POINTER;
367
368 AutoCaller autoCaller (this);
369 CheckComRCReturnRC (autoCaller.rc());
370
371 AutoReaderLock alock (this);
372
373 *aIRQ = mData->mIRQ;
374
375 return S_OK;
376}
377
378STDMETHODIMP SerialPort::COMSETTER(IRQ)(ULONG aIRQ)
379{
380 AutoCaller autoCaller (this);
381 CheckComRCReturnRC (autoCaller.rc());
382
383 /* the machine needs to be mutable */
384 Machine::AutoMutableStateDependency adep (mParent);
385 CheckComRCReturnRC (adep.rc());
386
387 AutoLock alock (this);
388
389 HRESULT rc = S_OK;
390 bool emitChangeEvent = false;
391
392 if (mData->mIRQ != aIRQ)
393 {
394 mData.backup();
395 mData->mIRQ = aIRQ;
396 emitChangeEvent = true;
397 }
398
399 if (emitChangeEvent)
400 {
401 /* leave the lock before informing callbacks */
402 alock.unlock();
403
404 mParent->onSerialPortChange (this);
405 }
406
407 return rc;
408}
409
410STDMETHODIMP SerialPort::COMGETTER(IOBase) (ULONG *aIOBase)
411{
412 if (!aIOBase)
413 return E_POINTER;
414
415 AutoCaller autoCaller (this);
416 CheckComRCReturnRC (autoCaller.rc());
417
418 AutoReaderLock alock (this);
419
420 *aIOBase = mData->mIOBase;
421
422 return S_OK;
423}
424
425STDMETHODIMP SerialPort::COMSETTER(IOBase)(ULONG aIOBase)
426{
427 AutoCaller autoCaller (this);
428 CheckComRCReturnRC (autoCaller.rc());
429
430 /* the machine needs to be mutable */
431 Machine::AutoMutableStateDependency adep (mParent);
432 CheckComRCReturnRC (adep.rc());
433
434 AutoLock alock (this);
435
436 HRESULT rc = S_OK;
437 bool emitChangeEvent = false;
438
439 if (mData->mIOBase != aIOBase)
440 {
441 mData.backup();
442 mData->mIOBase = aIOBase;
443 emitChangeEvent = true;
444 }
445
446 if (emitChangeEvent)
447 {
448 /* leave the lock before informing callbacks */
449 alock.unlock();
450
451 mParent->onSerialPortChange (this);
452 }
453
454 return rc;
455}
456
457STDMETHODIMP SerialPort::COMGETTER(Pipe) (BSTR *aPipe)
458{
459 if (!aPipe)
460 return E_POINTER;
461
462 AutoCaller autoCaller (this);
463 CheckComRCReturnRC (autoCaller.rc());
464
465 AutoReaderLock alock (this);
466
467 mData->mPipe.cloneTo (aPipe);
468
469 return S_OK;
470}
471
472STDMETHODIMP SerialPort::COMSETTER(Pipe) (INPTR BSTR aPipe)
473{
474 if (!aPipe || *aPipe == 0)
475 return E_INVALIDARG;
476
477 AutoCaller autoCaller (this);
478 CheckComRCReturnRC (autoCaller.rc());
479
480 /* the machine needs to be mutable */
481 Machine::AutoMutableStateDependency adep (mParent);
482 CheckComRCReturnRC (adep.rc());
483
484 AutoLock alock (this);
485
486 if (mData->mPipe != aPipe)
487 {
488 mData.backup();
489 mData->mPipe = aPipe;
490
491 /* leave the lock before informing callbacks */
492 alock.unlock();
493
494 return mParent->onSerialPortChange (this);
495 }
496
497 return S_OK;
498}
499
500STDMETHODIMP SerialPort::COMGETTER(Server) (BOOL *aServer)
501{
502 if (!aServer)
503 return E_POINTER;
504
505 AutoCaller autoCaller (this);
506 CheckComRCReturnRC (autoCaller.rc());
507
508 AutoReaderLock alock (this);
509
510 *aServer = mData->mServer;
511
512 return S_OK;
513}
514
515STDMETHODIMP SerialPort::COMSETTER(Server) (BOOL aServer)
516{
517 LogFlowThisFunc (("aServer=%RTbool\n", aServer));
518
519 AutoCaller autoCaller (this);
520 CheckComRCReturnRC (autoCaller.rc());
521
522 /* the machine needs to be mutable */
523 Machine::AutoMutableStateDependency adep (mParent);
524 CheckComRCReturnRC (adep.rc());
525
526 AutoLock alock (this);
527
528 if (mData->mServer != aServer)
529 {
530 mData.backup();
531 mData->mServer = aServer;
532
533 /* leave the lock before informing callbacks */
534 alock.unlock();
535
536 mParent->onSerialPortChange (this);
537 }
538
539 return S_OK;
540}
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