Garmin Fleet Management Controller  2.19.0
TcpIpPort.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * MODULE NAME:
4 * TcpIpPort.cpp
5 *
6 * Copyright 2016 by Garmin Ltd. or its subsidiaries.
7 *---------------------------------------------------------------------
8 * $NoKeywords$
9 *********************************************************************/
10 
11 #include "stdafx.h"
12 #include "TcpIpPort.h"
13 #include "LinkLayer.h"
14 #include "Logger.h"
15 #include "GarminLinkLayer.h"
16 
17 #include <afx.h>
18 #include <afxext.h>
19 #include <iostream>
20 
21 #define RX_BUFFER_SIZE ( 1024 )
22 
24 {
25  if( AfxSocketInit() == FALSE)
26  {
27  sInstance->recordErrorText( _T( "Error calling AfxSocketInit()" ) );
28  return false;
29  }
30 
31  if( sInstance )
32  {
33  sInstance->close();
34  }
35 
36  // Create a TCP/IP virtual serial port and connect it to the link layer
37  sInstance = new TcpIpPort();
39 
40  // Only clear the log if it isn't already open.
41  // Reinitializing the com port shouldn't clear the log.
42  if( !Logger::isLogOpen() )
44 
45  return ( (TcpIpPort *) sInstance )->init();
46 }
47 
49 {
50  socketIsOpen = false;
51 
52  clientSocket.Create();
53  CString localHost("127.0.0.1");
54  if(clientSocket.Connect(localHost, 6000))
55  {
56  socketIsOpen = true;
57  }
58  else
59  {
60  recordErrorText( _T( "Error calling clientSocket.Connect(localHost, 6000)" ) );
61  }
62  return socketIsOpen;
63 }
64 
65 //----------------------------------------------------------------------
67 //----------------------------------------------------------------------
69 {
70 }
71 
72 //----------------------------------------------------------------------
74 //----------------------------------------------------------------------
76 {
77  if( mLinkLayer )
78  {
80  }
81  close();
82 }
83 
84 //----------------------------------------------------------------------
86 //----------------------------------------------------------------------
88 {
89  uint8 readBuffer[ RX_BUFFER_SIZE ];
90  int readSize;
91 
92  if( socketIsOpen )
93  {
94  // Get the number of bytes that can be read without blocking
95  DWORD availableBytes = 0;
96  if( clientSocket.IOCtl( FIONREAD, &availableBytes) == 0 )
97  {
98  recordErrorText( _T( "Error polling socket for available data." ) );
99  close();
100  return;
101  }
102 
103  if( availableBytes > 0 )
104  {
105  if( availableBytes > RX_BUFFER_SIZE )
106  {
107  availableBytes = RX_BUFFER_SIZE;
108  }
109  readSize = clientSocket.Receive(readBuffer, availableBytes);
110  if (readSize > 0)
111  {
112  // Push the received bytes up to the link layer
113  if( mLinkLayer )
114  {
115  mLinkLayer->rx( readBuffer, readSize );
116  }
117  }
118  }
119  }
120 }
121 
122 //----------------------------------------------------------------------
124 //----------------------------------------------------------------------
126 {
127  if( isOpen() )
128  {
129  clientSocket.Close();
130  }
131  socketIsOpen = false;
132 }
133 
134 //----------------------------------------------------------------------
139 //----------------------------------------------------------------------
140 bool TcpIpPort::tx
141  (
142  uint8 * aData,
143  uint16 aSize
144  )
145 {
146  if( socketIsOpen )
147  {
148  if( SOCKET_ERROR != clientSocket.Send(aData, aSize) )
149  {
150  return true;
151  }
152  else
153  {
154  recordErrorText( _T( "Error sending data on socket" ) );
155  close();
156  }
157  }
158 
159  return false;
160 }
161 
162 //----------------------------------------------------------------------
166 //----------------------------------------------------------------------
167 const CString& TcpIpPort::getPortName() const
168 {
169  return mPortName;
170 }
171 
172 //----------------------------------------------------------------------
175 //----------------------------------------------------------------------
176 bool TcpIpPort::isOpen() const
177 {
178  return socketIsOpen;
179 }
180 
181 //----------------------------------------------------------------------
185 //----------------------------------------------------------------------
187  (
188  uint32 aBaudRate
189  )
190 {
191  mBaudRate = aBaudRate;
192  return true;
193 }
194 //----------------------------------------------------------------------
197 //----------------------------------------------------------------------
199  {
200  return mBaudRate;
201  }
Physical layer implementation for a serial port.
Definition: TcpIpPort.h:29
void recordErrorText(const CString &aOperation)
Store a textual description of the last error that occurred.
Definition: SerialPort.cpp:456
virtual void close()
Close the COM port if one is in use.
Definition: SerialPort.cpp:191
static void clearLog()
Empties the packet log.
Definition: Logger.cpp:83
TcpIpPort()
Constructor.
Definition: TcpIpPort.cpp:68
virtual bool isOpen() const
Indicate whether the port is open.
Definition: TcpIpPort.cpp:176
static SerialPort * sInstance
The one and only instance of this object.
Definition: SerialPort.h:95
static GarminLinkLayer * getInstance()
Get the one and only link layer object.
#define FALSE
Definition: garmin_types.h:46
virtual bool tx(uint8 *aData, uint16 aSize)
Transmit bytes on the serial port.
Definition: TcpIpPort.cpp:141
bool init()
Definition: TcpIpPort.cpp:48
uint32 mBaudRate
The last successfully applied baud rate.
Definition: TcpIpPort.h:67
virtual ~TcpIpPort()
Destructor. Close the serial port.
Definition: TcpIpPort.cpp:75
void resetPhysicalLayer(PhysicalLayer *aPort)
Reset the physical layer.
LinkLayer * mLinkLayer
The link layer that is one level up from this serial port.
Definition: PhysicalLayer.h:47
bool socketIsOpen
Definition: TcpIpPort.h:71
#define RX_BUFFER_SIZE
Definition: TcpIpPort.cpp:21
unsigned short int uint16
16-bit unsigned integer
Definition: garmin_types.h:64
static bool initTcpPort()
Definition: TcpIpPort.cpp:23
virtual void rx(uint8 const *const aData, uint32 const aSize)=0
Receive data from the physical layer.
virtual const CString & getPortName() const
Get the name of the serial port that is open.
Definition: TcpIpPort.cpp:167
unsigned char uint8
8-bit unsigned integer
Definition: garmin_types.h:62
void setPhysicalLayer(PhysicalLayer *aPort)
Set the physical layer.
Definition: LinkLayer.cpp:41
virtual void close()
Close the TCP virtual COM port if one is in use.
Definition: TcpIpPort.cpp:125
CString mPortName
Display name of the serial port being used for communication.
Definition: TcpIpPort.h:64
virtual bool setBaudRate(uint32 aBaudRate)
Set the baud rate.
Definition: TcpIpPort.cpp:187
static bool isLogOpen()
Returns true if the log file is open.
Definition: Logger.cpp:111
unsigned long int uint32
32-bit unsigned integer
Definition: garmin_types.h:66
CSocket clientSocket
Definition: TcpIpPort.h:69
virtual uint32 getBaudRate() const
Return the last successfully applied baud rate .
Definition: TcpIpPort.cpp:198
virtual void pumpRx()
Receive and process any data.
Definition: TcpIpPort.cpp:87