VirtualBox

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

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

Main: Fixed SerialPort::COMSETTER(Path) to not accept empty paths (forbidden by the XML Schema).

File size: 15.1 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 uint32_t uIOBase;
259 /* I/O base (required) */
260 CFGLDRQueryUInt32 (portNode, "IOBase", &uIOBase);
261 /* IRQ (required) */
262 uint32_t uIRQ;
263 CFGLDRQueryUInt32 (portNode, "IRQ", &uIRQ);
264 /* host mode (required) */
265 Bstr mode;
266 CFGLDRQueryBSTR (portNode, "hostMode", mode.asOutParam());
267 if (mode == L"HostPipe")
268 mData->mHostMode = PortMode_HostPipePort;
269 else if (mode == L"HostDevice")
270 mData->mHostMode = PortMode_HostDevicePort;
271 else
272 mData->mHostMode = PortMode_DisconnectedPort;
273 /* pipe/device path */
274 Bstr path;
275 CFGLDRQueryBSTR(portNode, "path", path.asOutParam());
276 /* server mode */
277 bool fServer = true;
278 CFGLDRQueryBool (portNode, "server", &fServer);
279
280 mData->mEnabled = fEnabled;
281 mData->mSlot = uSlot;
282 mData->mIOBase = uIOBase;
283 mData->mIRQ = uIRQ;
284 mData->mPath = path;
285 mData->mServer = fServer;
286
287 return S_OK;
288}
289
290HRESULT SerialPort::saveSettings (CFGNODE aNode)
291{
292 AssertReturn (aNode, E_FAIL);
293
294 AutoCaller autoCaller (this);
295 CheckComRCReturnRC (autoCaller.rc());
296
297 AutoReaderLock alock (this);
298
299 CFGNODE portNode = 0;
300 int vrc = CFGLDRAppendChildNode (aNode, "Port", &portNode);
301 ComAssertRCRet (vrc, E_FAIL);
302
303 const char *mode;
304 switch (mData->mHostMode)
305 {
306 default:
307 case PortMode_DisconnectedPort:
308 mode = "Disconnected";
309 break;
310 case PortMode_HostPipePort:
311 mode = "HostPipe";
312 break;
313 case PortMode_HostDevicePort:
314 mode = "HostDevice";
315 break;
316 }
317 CFGLDRSetUInt32 (portNode, "slot", mData->mSlot);
318 CFGLDRSetBool (portNode, "enabled", !!mData->mEnabled);
319 CFGLDRSetUInt32Ex (portNode, "IOBase", mData->mIOBase, 16);
320 CFGLDRSetUInt32 (portNode, "IRQ", mData->mIRQ);
321 CFGLDRSetString (portNode, "hostMode", mode);
322 if (mData->mHostMode != PortMode_DisconnectedPort)
323 {
324 CFGLDRSetBSTR (portNode, "path", mData->mPath);
325 if (mData->mHostMode == PortMode_HostPipePort)
326 CFGLDRSetBool (portNode, "server", !!mData->mServer);
327 }
328
329 return S_OK;
330}
331
332// ISerialPort properties
333/////////////////////////////////////////////////////////////////////////////
334
335STDMETHODIMP SerialPort::COMGETTER(Enabled) (BOOL *aEnabled)
336{
337 if (!aEnabled)
338 return E_POINTER;
339
340 AutoCaller autoCaller (this);
341 CheckComRCReturnRC (autoCaller.rc());
342
343 AutoReaderLock alock (this);
344
345 *aEnabled = mData->mEnabled;
346
347 return S_OK;
348}
349
350STDMETHODIMP SerialPort::COMSETTER(Enabled) (BOOL aEnabled)
351{
352 LogFlowThisFunc (("aEnabled=%RTbool\n", aEnabled));
353
354 AutoCaller autoCaller (this);
355 CheckComRCReturnRC (autoCaller.rc());
356
357 /* the machine needs to be mutable */
358 Machine::AutoMutableStateDependency adep (mParent);
359 CheckComRCReturnRC (adep.rc());
360
361 AutoLock alock (this);
362
363 if (mData->mEnabled != aEnabled)
364 {
365 mData.backup();
366 mData->mEnabled = aEnabled;
367
368 /* leave the lock before informing callbacks */
369 alock.unlock();
370
371 mParent->onSerialPortChange (this);
372 }
373
374 return S_OK;
375}
376
377STDMETHODIMP SerialPort::COMGETTER(HostMode) (PortMode_T *aHostMode)
378{
379 if (!aHostMode)
380 return E_POINTER;
381
382 AutoCaller autoCaller (this);
383 CheckComRCReturnRC (autoCaller.rc());
384
385 AutoReaderLock alock (this);
386
387 *aHostMode = mData->mHostMode;
388
389 return S_OK;
390}
391
392STDMETHODIMP SerialPort::COMSETTER(HostMode) (PortMode_T aHostMode)
393{
394 AutoCaller autoCaller (this);
395 CheckComRCReturnRC (autoCaller.rc());
396
397 /* the machine needs to be mutable */
398 Machine::AutoMutableStateDependency adep (mParent);
399 CheckComRCReturnRC (adep.rc());
400
401 AutoLock alock (this);
402
403 HRESULT rc = S_OK;
404 bool emitChangeEvent = false;
405
406 if (mData->mHostMode != aHostMode)
407 {
408 mData.backup();
409 mData->mHostMode = aHostMode;
410 if (aHostMode == PortMode_DisconnectedPort)
411 {
412 mData->mPath.setNull();
413 mData->mServer = false;
414 }
415 emitChangeEvent = true;
416 }
417
418 if (emitChangeEvent)
419 {
420 /* leave the lock before informing callbacks */
421 alock.unlock();
422
423 mParent->onSerialPortChange (this);
424 }
425
426 return rc;
427}
428
429STDMETHODIMP SerialPort::COMGETTER(Slot) (ULONG *aSlot)
430{
431 if (!aSlot)
432 return E_POINTER;
433
434 AutoCaller autoCaller (this);
435 CheckComRCReturnRC (autoCaller.rc());
436
437 AutoReaderLock alock (this);
438
439 *aSlot = mData->mSlot;
440
441 return S_OK;
442}
443
444STDMETHODIMP SerialPort::COMGETTER(IRQ) (ULONG *aIRQ)
445{
446 if (!aIRQ)
447 return E_POINTER;
448
449 AutoCaller autoCaller (this);
450 CheckComRCReturnRC (autoCaller.rc());
451
452 AutoReaderLock alock (this);
453
454 *aIRQ = mData->mIRQ;
455
456 return S_OK;
457}
458
459STDMETHODIMP SerialPort::COMSETTER(IRQ)(ULONG aIRQ)
460{
461 /* check IRQ limits
462 * (when changing this, make sure it corresponds to XML schema */
463 if (aIRQ > 255)
464 return setError (E_INVALIDARG,
465 tr ("Invalid IRQ number: %lu (must be in range [0, %lu])"),
466 aIRQ, 255);
467
468 AutoCaller autoCaller (this);
469 CheckComRCReturnRC (autoCaller.rc());
470
471 /* the machine needs to be mutable */
472 Machine::AutoMutableStateDependency adep (mParent);
473 CheckComRCReturnRC (adep.rc());
474
475 AutoLock alock (this);
476
477 HRESULT rc = S_OK;
478 bool emitChangeEvent = false;
479
480 if (mData->mIRQ != aIRQ)
481 {
482 mData.backup();
483 mData->mIRQ = aIRQ;
484 emitChangeEvent = true;
485 }
486
487 if (emitChangeEvent)
488 {
489 /* leave the lock before informing callbacks */
490 alock.unlock();
491
492 mParent->onSerialPortChange (this);
493 }
494
495 return rc;
496}
497
498STDMETHODIMP SerialPort::COMGETTER(IOBase) (ULONG *aIOBase)
499{
500 if (!aIOBase)
501 return E_POINTER;
502
503 AutoCaller autoCaller (this);
504 CheckComRCReturnRC (autoCaller.rc());
505
506 AutoReaderLock alock (this);
507
508 *aIOBase = mData->mIOBase;
509
510 return S_OK;
511}
512
513STDMETHODIMP SerialPort::COMSETTER(IOBase)(ULONG aIOBase)
514{
515 /* check IOBase limits
516 * (when changing this, make sure it corresponds to XML schema */
517 if (aIOBase > 0xFFFF)
518 return setError (E_INVALIDARG,
519 tr ("Invalid I/O port base address: %lu (must be in range [0, 0x%X])"),
520 aIOBase, 0, 0xFFFF);
521
522 AutoCaller autoCaller (this);
523 CheckComRCReturnRC (autoCaller.rc());
524
525 /* the machine needs to be mutable */
526 Machine::AutoMutableStateDependency adep (mParent);
527 CheckComRCReturnRC (adep.rc());
528
529 AutoLock alock (this);
530
531 HRESULT rc = S_OK;
532 bool emitChangeEvent = false;
533
534 if (mData->mIOBase != aIOBase)
535 {
536 mData.backup();
537 mData->mIOBase = aIOBase;
538 emitChangeEvent = true;
539 }
540
541 if (emitChangeEvent)
542 {
543 /* leave the lock before informing callbacks */
544 alock.unlock();
545
546 mParent->onSerialPortChange (this);
547 }
548
549 return rc;
550}
551
552STDMETHODIMP SerialPort::COMGETTER(Path) (BSTR *aPath)
553{
554 if (!aPath)
555 return E_POINTER;
556
557 AutoCaller autoCaller (this);
558 CheckComRCReturnRC (autoCaller.rc());
559
560 AutoReaderLock alock (this);
561
562 mData->mPath.cloneTo (aPath);
563
564 return S_OK;
565}
566
567STDMETHODIMP SerialPort::COMSETTER(Path) (INPTR BSTR aPath)
568{
569 if (!aPath)
570 return E_POINTER;
571
572 if (!*aPath)
573 return setError (E_INVALIDARG,
574 tr ("Serial port path cannot be empty"));
575
576
577 AutoCaller autoCaller (this);
578 CheckComRCReturnRC (autoCaller.rc());
579
580 /* the machine needs to be mutable */
581 Machine::AutoMutableStateDependency adep (mParent);
582 CheckComRCReturnRC (adep.rc());
583
584 AutoLock alock (this);
585
586 if (mData->mPath != aPath)
587 {
588 mData.backup();
589 mData->mPath = aPath;
590
591 /* leave the lock before informing callbacks */
592 alock.unlock();
593
594 return mParent->onSerialPortChange (this);
595 }
596
597 return S_OK;
598}
599
600STDMETHODIMP SerialPort::COMGETTER(Server) (BOOL *aServer)
601{
602 if (!aServer)
603 return E_POINTER;
604
605 AutoCaller autoCaller (this);
606 CheckComRCReturnRC (autoCaller.rc());
607
608 AutoReaderLock alock (this);
609
610 *aServer = mData->mServer;
611
612 return S_OK;
613}
614
615STDMETHODIMP SerialPort::COMSETTER(Server) (BOOL aServer)
616{
617 LogFlowThisFunc (("aServer=%RTbool\n", aServer));
618
619 AutoCaller autoCaller (this);
620 CheckComRCReturnRC (autoCaller.rc());
621
622 /* the machine needs to be mutable */
623 Machine::AutoMutableStateDependency adep (mParent);
624 CheckComRCReturnRC (adep.rc());
625
626 AutoLock alock (this);
627
628 if (mData->mServer != aServer)
629 {
630 mData.backup();
631 mData->mServer = aServer;
632
633 /* leave the lock before informing callbacks */
634 alock.unlock();
635
636 mParent->onSerialPortChange (this);
637 }
638
639 return S_OK;
640}
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