cec: don't try to read or write in CUSBCECAdapterCommunication when the connection...
[deb_libcec.git] / src / lib / adapter / USBCECAdapterCommunication.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #include "USBCECAdapterCommunication.h"
34 #include "../platform/sockets/serialport.h"
35 #include "../platform/util/timeutils.h"
36 #include "../LibCEC.h"
37 #include "../CECProcessor.h"
38
39 using namespace std;
40 using namespace CEC;
41 using namespace PLATFORM;
42
43 CUSBCECAdapterCommunication::CUSBCECAdapterCommunication(CCECProcessor *processor, const char *strPort, uint16_t iBaudRate /* = 38400 */) :
44 m_port(NULL),
45 m_processor(processor),
46 m_iLineTimeout(0),
47 m_iFirmwareVersion(CEC_FW_VERSION_UNKNOWN),
48 m_lastInitiator(CECDEVICE_UNKNOWN),
49 m_bNextIsEscaped(false),
50 m_bGotStart(false)
51 {
52 m_port = new PLATFORM::CSerialPort(strPort, iBaudRate);
53 }
54
55 CUSBCECAdapterCommunication::~CUSBCECAdapterCommunication(void)
56 {
57 Close();
58 }
59
60 bool CUSBCECAdapterCommunication::CheckAdapter(uint32_t iTimeoutMs /* = 10000 */)
61 {
62 bool bReturn(false);
63 uint64_t iNow = GetTimeMs();
64 uint64_t iTarget = iTimeoutMs > 0 ? iNow + iTimeoutMs : iNow + CEC_DEFAULT_TRANSMIT_WAIT;
65
66 /* try to ping the adapter */
67 bool bPinged(false);
68 unsigned iPingTry(0);
69 while (iNow < iTarget && (bPinged = PingAdapter()) == false)
70 {
71 CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to a ping (try %d)", ++iPingTry);
72 Sleep(500);
73 iNow = GetTimeMs();
74 }
75
76 /* try to read the firmware version */
77 m_iFirmwareVersion = CEC_FW_VERSION_UNKNOWN;
78 unsigned iFwVersionTry(0);
79 while (bPinged && iNow < iTarget && (m_iFirmwareVersion = GetFirmwareVersion()) == CEC_FW_VERSION_UNKNOWN)
80 {
81 CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond with a correct firmware version (try %d)", ++iFwVersionTry);
82 Sleep(500);
83 iNow = GetTimeMs();
84 }
85
86 if (m_iFirmwareVersion >= 2)
87 {
88 /* try to set controlled mode */
89 unsigned iControlledTry(0);
90 bool bControlled(false);
91 while (iNow < iTarget && (bControlled = SetControlledMode(true)) == false)
92 {
93 CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter did not respond correctly to setting controlled mode (try %d)", ++iControlledTry);
94 Sleep(500);
95 iNow = GetTimeMs();
96 }
97 bReturn = bControlled;
98 }
99 else
100 bReturn = true;
101
102 return bReturn;
103 }
104
105 bool CUSBCECAdapterCommunication::Open(IAdapterCommunicationCallback *cb, uint32_t iTimeoutMs /* = 10000 */)
106 {
107 uint64_t iNow = GetTimeMs();
108 uint64_t iTimeout = iNow + iTimeoutMs;
109
110 {
111 CLockObject lock(m_mutex);
112
113 if (!m_port)
114 {
115 CLibCEC::AddLog(CEC_LOG_ERROR, "port is NULL");
116 return false;
117 }
118
119 if (IsOpen())
120 {
121 CLibCEC::AddLog(CEC_LOG_ERROR, "port is already open");
122 return true;
123 }
124
125 m_callback = cb;
126 CStdString strError;
127 bool bConnected(false);
128 while (!bConnected && iNow < iTimeout)
129 {
130 if ((bConnected = m_port->Open(iTimeout)) == false)
131 {
132 strError.Format("error opening serial port '%s': %s", m_port->GetName().c_str(), m_port->GetError().c_str());
133 Sleep(250);
134 iNow = GetTimeMs();
135 }
136 }
137
138 if (!bConnected)
139 {
140 CLibCEC::AddLog(CEC_LOG_ERROR, strError);
141 return false;
142 }
143
144 CLibCEC::AddLog(CEC_LOG_DEBUG, "connection opened, clearing any previous input and waiting for active transmissions to end before starting");
145
146 //clear any input bytes
147 uint8_t buff[1024];
148 while (m_port->Read(buff, 1024, 100) > 0)
149 {
150 CLibCEC::AddLog(CEC_LOG_DEBUG, "data received, clearing it");
151 Sleep(250);
152 }
153 }
154
155 if (CreateThread())
156 {
157 if (!CheckAdapter())
158 {
159 StopThread();
160 CLibCEC::AddLog(CEC_LOG_ERROR, "the adapter failed to pass basic checks");
161 }
162 else
163 {
164 CLibCEC::AddLog(CEC_LOG_DEBUG, "communication thread started");
165 return true;
166 }
167 }
168 CLibCEC::AddLog(CEC_LOG_ERROR, "could not create a communication thread");
169
170 return false;
171 }
172
173 void CUSBCECAdapterCommunication::Close(void)
174 {
175 CLockObject lock(m_mutex);
176 m_rcvCondition.Broadcast();
177 StopThread();
178 }
179
180 void *CUSBCECAdapterCommunication::Process(void)
181 {
182 cec_command command;
183 bool bCommandReceived(false);
184 while (!IsStopped())
185 {
186 {
187 CLockObject lock(m_mutex);
188 ReadFromDevice(50);
189 bCommandReceived = m_callback && Read(command, 0);
190 }
191
192 /* push the next command to the callback method if there is one */
193 if (!IsStopped() && bCommandReceived)
194 m_callback->OnCommandReceived(command);
195
196 if (!IsStopped())
197 {
198 Sleep(5);
199 WriteNextCommand();
200 }
201 }
202
203 CCECAdapterMessage *msg(NULL);
204 if (m_outBuffer.Pop(msg))
205 msg->condition.Broadcast();
206
207 if (m_port)
208 {
209 delete m_port;
210 m_port = NULL;
211 }
212
213 return NULL;
214 }
215
216 cec_adapter_message_state CUSBCECAdapterCommunication::Write(const cec_command &data, uint8_t iMaxTries, uint8_t iLineTimeout /* = 3 */, uint8_t iRetryLineTimeout /* = 3 */)
217 {
218 cec_adapter_message_state retVal(ADAPTER_MESSAGE_STATE_UNKNOWN);
219 if (!IsRunning())
220 return retVal;
221
222 CCECAdapterMessage *output = new CCECAdapterMessage(data);
223
224 /* set the number of retries */
225 if (data.opcode == CEC_OPCODE_NONE) //TODO
226 output->maxTries = 1;
227 else if (data.initiator != CECDEVICE_BROADCAST)
228 output->maxTries = iMaxTries;
229
230 output->lineTimeout = iLineTimeout;
231 output->retryTimeout = iRetryLineTimeout;
232 output->tries = 0;
233
234 bool bRetry(true);
235 while (bRetry && ++output->tries < output->maxTries)
236 {
237 bRetry = (!Write(output) || output->NeedsRetry()) && output->transmit_timeout > 0;
238 if (bRetry)
239 Sleep(CEC_DEFAULT_TRANSMIT_RETRY_WAIT);
240 }
241 retVal = output->state;
242
243 delete output;
244 return retVal;
245 }
246
247 bool CUSBCECAdapterCommunication::Write(CCECAdapterMessage *data)
248 {
249 CLockObject lock(data->mutex);
250 data->state = ADAPTER_MESSAGE_STATE_WAITING_TO_BE_SENT;
251 m_outBuffer.Push(data);
252 data->condition.Wait(data->mutex);
253
254 if ((data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT_ACKED) ||
255 (!data->expectControllerAck && data->state != ADAPTER_MESSAGE_STATE_SENT))
256 {
257 CLibCEC::AddLog(CEC_LOG_DEBUG, "command was not %s", data->state == ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED ? "acked" : "sent");
258 return false;
259 }
260
261 return true;
262 }
263
264 bool CUSBCECAdapterCommunication::Read(cec_command &command, uint32_t iTimeout)
265 {
266 if (!IsRunning())
267 return false;
268
269 CCECAdapterMessage msg;
270 if (Read(msg, iTimeout))
271 {
272 if (ParseMessage(msg))
273 {
274 command = m_currentframe;
275 m_currentframe.Clear();
276 return true;
277 }
278 }
279 return false;
280 }
281
282 bool CUSBCECAdapterCommunication::Read(CCECAdapterMessage &msg, uint32_t iTimeout)
283 {
284 CLockObject lock(m_mutex);
285
286 msg.Clear();
287 CCECAdapterMessage *buf(NULL);
288
289 if (!m_inBuffer.Pop(buf))
290 {
291 if (iTimeout == 0 || !m_rcvCondition.Wait(m_mutex, iTimeout))
292 return false;
293 m_inBuffer.Pop(buf);
294 }
295
296 if (buf)
297 {
298 msg.packet = buf->packet;
299 msg.state = ADAPTER_MESSAGE_STATE_INCOMING;
300 delete buf;
301 return true;
302 }
303 return false;
304 }
305
306 CStdString CUSBCECAdapterCommunication::GetError(void) const
307 {
308 CStdString strError;
309 strError = m_port->GetError();
310 return strError;
311 }
312
313 bool CUSBCECAdapterCommunication::StartBootloader(void)
314 {
315 bool bReturn(false);
316 if (!IsRunning())
317 return bReturn;
318
319 CLibCEC::AddLog(CEC_LOG_DEBUG, "starting the bootloader");
320 CCECAdapterMessage *output = new CCECAdapterMessage;
321
322 output->PushBack(MSGSTART);
323 output->PushEscaped(MSGCODE_START_BOOTLOADER);
324 output->PushBack(MSGEND);
325 output->isTransmission = false;
326 output->expectControllerAck = false;
327
328 if ((bReturn = Write(output)) == false)
329 CLibCEC::AddLog(CEC_LOG_ERROR, "could not start the bootloader");
330 delete output;
331
332 return bReturn;
333 }
334
335 bool CUSBCECAdapterCommunication::PingAdapter(void)
336 {
337 bool bReturn(false);
338 if (!IsRunning())
339 return bReturn;
340
341 CLibCEC::AddLog(CEC_LOG_DEBUG, "sending ping");
342 CCECAdapterMessage *output = new CCECAdapterMessage;
343
344 output->PushBack(MSGSTART);
345 output->PushEscaped(MSGCODE_PING);
346 output->PushBack(MSGEND);
347 output->isTransmission = false;
348
349 if ((bReturn = Write(output)) == false)
350 CLibCEC::AddLog(CEC_LOG_ERROR, "could not ping the adapter");
351 delete output;
352
353 return bReturn;
354 }
355
356 bool CUSBCECAdapterCommunication::ParseMessage(const CCECAdapterMessage &msg)
357 {
358 bool bEom(false);
359 bool bIsError(msg.IsError());
360
361 if (msg.IsEmpty())
362 return bEom;
363
364 switch(msg.Message())
365 {
366 case MSGCODE_FRAME_START:
367 {
368 m_currentframe.Clear();
369 if (msg.Size() >= 2)
370 {
371 m_currentframe.initiator = msg.Initiator();
372 m_currentframe.destination = msg.Destination();
373 m_currentframe.ack = msg.IsACK();
374 m_currentframe.eom = msg.IsEOM();
375 }
376 if (m_currentframe.ack == 0x1)
377 {
378 m_lastInitiator = m_currentframe.initiator;
379 m_processor->HandlePoll(m_currentframe.initiator, m_currentframe.destination);
380 }
381 }
382 break;
383 case MSGCODE_RECEIVE_FAILED:
384 {
385 m_currentframe.Clear();
386 if (m_lastInitiator != CECDEVICE_UNKNOWN)
387 bIsError = m_processor->HandleReceiveFailed(m_lastInitiator);
388 }
389 break;
390 case MSGCODE_FRAME_DATA:
391 {
392 if (msg.Size() >= 2)
393 {
394 m_currentframe.PushBack(msg[1]);
395 m_currentframe.eom = msg.IsEOM();
396 }
397 bEom = msg.IsEOM();
398 }
399 break;
400 default:
401 break;
402 }
403
404 CLibCEC::AddLog(bIsError ? CEC_LOG_WARNING : CEC_LOG_DEBUG, msg.ToString());
405 return bEom;
406 }
407
408 uint16_t CUSBCECAdapterCommunication::GetFirmwareVersion(void)
409 {
410 CLockObject lock(m_mutex);
411 uint16_t iReturn(m_iFirmwareVersion);
412 if (!IsRunning())
413 return iReturn;
414
415 if (iReturn == CEC_FW_VERSION_UNKNOWN)
416 {
417 CLibCEC::AddLog(CEC_LOG_DEBUG, "requesting the firmware version");
418 CCECAdapterMessage *output = new CCECAdapterMessage;
419
420 output->PushBack(MSGSTART);
421 output->PushEscaped(MSGCODE_FIRMWARE_VERSION);
422 output->PushBack(MSGEND);
423 output->isTransmission = false;
424 output->expectControllerAck = false;
425
426 SendMessageToAdapter(output);
427 bool bWriteOk = output->state == ADAPTER_MESSAGE_STATE_SENT;
428 delete output;
429 if (!bWriteOk)
430 {
431 CLibCEC::AddLog(CEC_LOG_ERROR, "could not request the firmware version");
432 return iReturn;
433 }
434
435 Sleep(250); // TODO ReadFromDevice() isn't waiting for the timeout to pass on win32
436 ReadFromDevice(CEC_DEFAULT_TRANSMIT_WAIT, 5 /* start + msgcode + 2 bytes for fw version + end */);
437 CCECAdapterMessage input;
438 if (Read(input, 0))
439 {
440 if (input.Message() != MSGCODE_FIRMWARE_VERSION || input.Size() != 3)
441 CLibCEC::AddLog(CEC_LOG_ERROR, "invalid firmware version (size = %d, message = %d)", input.Size(), input.Message());
442 else
443 {
444 m_iFirmwareVersion = (input[1] << 8 | input[2]);
445 iReturn = m_iFirmwareVersion;
446 }
447 }
448 else
449 {
450 CLibCEC::AddLog(CEC_LOG_ERROR, "no firmware version received");
451 }
452 }
453
454 return iReturn;
455 }
456
457 bool CUSBCECAdapterCommunication::SetLineTimeout(uint8_t iTimeout)
458 {
459 m_iLineTimeout = iTimeout;
460 return true;
461 //TODO
462 // bool bReturn(m_iLineTimeout != iTimeout);
463 //
464 // if (!bReturn)
465 // {
466 // CCECAdapterMessage *output = new CCECAdapterMessage;
467 //
468 // output->PushBack(MSGSTART);
469 // output->PushEscaped(MSGCODE_TRANSMIT_IDLETIME);
470 // output->PushEscaped(iTimeout);
471 // output->PushBack(MSGEND);
472 // output->isTransmission = false;
473 //
474 // if ((bReturn = Write(output)) == false)
475 // CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the idletime");
476 // delete output;
477 // }
478 //
479 // return bReturn;
480 }
481
482 bool CUSBCECAdapterCommunication::SetAckMask(uint16_t iMask)
483 {
484 bool bReturn(false);
485 CLibCEC::AddLog(CEC_LOG_DEBUG, "setting ackmask to %2x", iMask);
486
487 CCECAdapterMessage *output = new CCECAdapterMessage;
488
489 output->PushBack(MSGSTART);
490 output->PushEscaped(MSGCODE_SET_ACK_MASK);
491 output->PushEscaped(iMask >> 8);
492 output->PushEscaped((uint8_t)iMask);
493 output->PushBack(MSGEND);
494 output->isTransmission = false;
495
496 if ((bReturn = Write(output)) == false)
497 CLibCEC::AddLog(CEC_LOG_ERROR, "could not set the ackmask");
498 delete output;
499
500 return bReturn;
501 }
502
503
504 bool CUSBCECAdapterCommunication::SetControlledMode(bool controlled)
505 {
506 bool bReturn(false);
507 CLibCEC::AddLog(CEC_LOG_DEBUG, "turning controlled mode %s", controlled ? "on" : "off");
508
509 CCECAdapterMessage *output = new CCECAdapterMessage;
510
511 output->PushBack(MSGSTART);
512 output->PushEscaped(MSGCODE_SET_CONTROLLED);
513 output->PushEscaped(controlled);
514 output->PushBack(MSGEND);
515 output->isTransmission = false;
516
517 if ((bReturn = Write(output)) == false)
518 CLibCEC::AddLog(CEC_LOG_ERROR, "could not set controlled mode");
519 delete output;
520
521 return bReturn;
522 }
523
524 bool CUSBCECAdapterCommunication::IsOpen(void)
525 {
526 return !IsStopped() && m_port->IsOpen() && IsRunning();
527 }
528
529 bool CUSBCECAdapterCommunication::WaitForAck(CCECAdapterMessage &message)
530 {
531 bool bError(false);
532 bool bTransmitSucceeded(false);
533 uint8_t iPacketsLeft(message.Size() / 4);
534
535 int64_t iNow = GetTimeMs();
536 int64_t iTargetTime = iNow + (message.transmit_timeout <= 5 ? CEC_DEFAULT_TRANSMIT_WAIT : message.transmit_timeout);
537
538 while (!bTransmitSucceeded && !bError && iNow < iTargetTime)
539 {
540 ReadFromDevice(50);
541 CCECAdapterMessage msg;
542 if (!Read(msg, 0))
543 {
544 iNow = GetTimeMs();
545 continue;
546 }
547
548 if (msg.Message() == MSGCODE_FRAME_START && msg.IsACK())
549 {
550 m_processor->HandlePoll(msg.Initiator(), msg.Destination());
551 m_lastInitiator = msg.Initiator();
552 iNow = GetTimeMs();
553 continue;
554 }
555
556 if (msg.Message() == MSGCODE_RECEIVE_FAILED &&
557 m_lastInitiator != CECDEVICE_UNKNOWN &&
558 m_processor->HandleReceiveFailed(m_lastInitiator))
559 {
560 iNow = GetTimeMs();
561 continue;
562 }
563
564 bError = msg.IsError();
565 if (bError)
566 {
567 message.reply = msg.Message();
568 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
569 }
570 else
571 {
572 switch(msg.Message())
573 {
574 case MSGCODE_COMMAND_ACCEPTED:
575 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
576 if (iPacketsLeft > 0)
577 iPacketsLeft--;
578 if (!message.isTransmission && iPacketsLeft == 0)
579 bTransmitSucceeded = true;
580 break;
581 case MSGCODE_TRANSMIT_SUCCEEDED:
582 CLibCEC::AddLog(CEC_LOG_DEBUG, msg.ToString());
583 bTransmitSucceeded = (iPacketsLeft == 0);
584 bError = !bTransmitSucceeded;
585 message.reply = MSGCODE_TRANSMIT_SUCCEEDED;
586 break;
587 default:
588 // ignore other data while waiting
589 break;
590 }
591
592 iNow = GetTimeMs();
593 }
594 }
595
596 message.state = bTransmitSucceeded && !bError ?
597 ADAPTER_MESSAGE_STATE_SENT_ACKED :
598 ADAPTER_MESSAGE_STATE_SENT_NOT_ACKED;
599
600 return bTransmitSucceeded && !bError;
601 }
602
603 void CUSBCECAdapterCommunication::AddData(uint8_t *data, size_t iLen)
604 {
605 CLockObject lock(m_mutex);
606 for (size_t iPtr = 0; iPtr < iLen; iPtr++)
607 {
608 if (!m_bGotStart)
609 {
610 if (data[iPtr] == MSGSTART)
611 m_bGotStart = true;
612 }
613 else if (data[iPtr] == MSGSTART) //we found a msgstart before msgend, this is not right, remove
614 {
615 if (m_currentAdapterMessage.Size() > 0)
616 CLibCEC::AddLog(CEC_LOG_WARNING, "received MSGSTART before MSGEND, removing previous buffer contents");
617 m_currentAdapterMessage.Clear();
618 m_bGotStart = true;
619 }
620 else if (data[iPtr] == MSGEND)
621 {
622 CCECAdapterMessage *newMessage = new CCECAdapterMessage;
623 newMessage->packet = m_currentAdapterMessage.packet;
624 m_inBuffer.Push(newMessage);
625 m_currentAdapterMessage.Clear();
626 m_bGotStart = false;
627 m_bNextIsEscaped = false;
628 m_rcvCondition.Signal();
629 }
630 else if (m_bNextIsEscaped)
631 {
632 m_currentAdapterMessage.PushBack(data[iPtr] + (uint8_t)ESCOFFSET);
633 m_bNextIsEscaped = false;
634 }
635 else if (data[iPtr] == MSGESC)
636 {
637 m_bNextIsEscaped = true;
638 }
639 else
640 {
641 m_currentAdapterMessage.PushBack(data[iPtr]);
642 }
643 }
644 }
645
646 bool CUSBCECAdapterCommunication::ReadFromDevice(uint32_t iTimeout, size_t iSize /* = 256 */)
647 {
648 ssize_t iBytesRead;
649 uint8_t buff[256];
650 if (!m_port)
651 return false;
652 if (iSize > 256)
653 iSize = 256;
654
655 CLockObject lock(m_mutex);
656 iBytesRead = m_port->Read(buff, sizeof(uint8_t) * iSize, iTimeout);
657 if (iBytesRead < 0 || iBytesRead > 256)
658 {
659 CLibCEC::AddLog(CEC_LOG_ERROR, "error reading from serial port: %s", m_port->GetError().c_str());
660 return false;
661 }
662 else if (iBytesRead > 0)
663 {
664 AddData(buff, iBytesRead);
665 }
666
667 return iBytesRead > 0;
668 }
669
670 void CUSBCECAdapterCommunication::SendMessageToAdapter(CCECAdapterMessage *msg)
671 {
672 CLockObject adapterLock(m_mutex);
673 CLockObject lock(msg->mutex);
674 if (msg->tries == 1)
675 SetLineTimeout(msg->lineTimeout);
676 else
677 SetLineTimeout(msg->retryTimeout);
678
679 if (m_port->Write(msg->packet.data, msg->Size()) != (ssize_t) msg->Size())
680 {
681 CLibCEC::AddLog(CEC_LOG_ERROR, "error writing to serial port: %s", m_port->GetError().c_str());
682 msg->state = ADAPTER_MESSAGE_STATE_ERROR;
683 }
684 else
685 {
686 CLibCEC::AddLog(CEC_LOG_DEBUG, "command sent");
687 msg->state = ADAPTER_MESSAGE_STATE_SENT;
688
689 if (msg->expectControllerAck)
690 {
691 if (!WaitForAck(*msg))
692 CLibCEC::AddLog(CEC_LOG_DEBUG, "did not receive ack");
693 }
694 }
695 msg->condition.Signal();
696 }
697
698 void CUSBCECAdapterCommunication::WriteNextCommand(void)
699 {
700 CCECAdapterMessage *msg(NULL);
701 if (m_outBuffer.Pop(msg))
702 SendMessageToAdapter(msg);
703 }