Garmin Fleet Management Controller  2.19.0
CStopNewDlg.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * MODULE NAME:
4 * CStopNewDlg.cpp
5 *
6 * Copyright 2008-2009 by Garmin Ltd. or its subsidiaries.
7 *---------------------------------------------------------------------
8 * $NoKeywords$
9 *********************************************************************/
10 #include "stdafx.h"
11 #include "CFmiApplication.h"
12 #include "CStopNewDlg.h"
13 #include "util.h"
14 
15 #if( FMI_SUPPORT_A618 )
16 #define LONG_STOP_TEMP_FILE _T( "long_stop_temp.bin" )
17 #endif
18 
19 IMPLEMENT_DYNAMIC( CStopNewDlg, CDialog )
20 
21 BEGIN_MESSAGE_MAP( CStopNewDlg, CDialog )
22  ON_BN_CLICKED( IDOK, OnBnClickedOk )
23  ON_EN_CHANGE( IDC_STOPNEW_EDIT_LAT, OnFormChanged )
24  ON_EN_CHANGE( IDC_STOPNEW_EDIT_LON, OnFormChanged )
25  ON_EN_CHANGE( IDC_STOPNEW_EDIT_NAME, OnFormChanged )
26  ON_BN_CLICKED( IDC_STOPNEW_RDO_LEGACY, OnFormChanged )
27 #if( FMI_SUPPORT_A602 )
28  ON_BN_CLICKED( IDC_STOPNEW_RDO_A602, OnFormChanged )
29 #endif
30 #if( FMI_SUPPORT_A603 )
31  ON_EN_CHANGE( IDC_STOPNEW_EDIT_STOPID, OnFormChanged )
32  ON_BN_CLICKED( IDC_STOPNEW_RDO_A603, OnFormChanged )
33 #endif
34 #if( FMI_SUPPORT_A618 )
35  ON_EN_CHANGE( IDC_STOPNEW_EDIT_STOPID, OnFormChanged )
36  ON_BN_CLICKED( IDC_STOPNEW_RDO_A618, OnFormChanged )
37 #endif
38 END_MESSAGE_MAP()
39 
40 //----------------------------------------------------------------------
44 //----------------------------------------------------------------------
45 CStopNewDlg::CStopNewDlg
46  (
47  CWnd * aParent,
48  FmiApplicationLayer & aCom
49  )
50  : CDialog( IDD_STOP_NEW, aParent )
51  , mCom( aCom )
52  , mLatitudeStr( _T("") )
53  , mLongitudeStr( _T("") )
54  , mMessageStr( _T("") )
55  , mCompressLongStop( TRUE )
56 {
57 #if( FMI_SUPPORT_A618 )
58  mStopProtocol = STOP_PROTOCOL_A618;
59 #elif( FMI_SUPPORT_A603 )
60  mStopProtocol = STOP_PROTOCOL_A603;
61 #elif( FMI_SUPPORT_A602 )
62  mStopProtocol = STOP_PROTOCOL_A602;
63 #else
64  mStopProtocol = STOP_PROTOCOL_LEGACY;
65 #endif
66 }
67 
68 //----------------------------------------------------------------------
70 //----------------------------------------------------------------------
72 {
73 }
74 
75 //----------------------------------------------------------------------
78 //----------------------------------------------------------------------
80  (
81  CDataExchange * aDataExchange
82  )
83 {
84  CDialog::DoDataExchange( aDataExchange );
85 
86  DDX_Text( aDataExchange, IDC_STOPNEW_EDIT_LAT, mLatitudeStr );
87  DDX_Text( aDataExchange, IDC_STOPNEW_EDIT_LON, mLongitudeStr );
88  DDX_Text( aDataExchange, IDC_STOPNEW_EDIT_NAME, mMessageStr );
89  DDX_Radio( aDataExchange, IDC_STOPNEW_RDO_A618, mStopProtocol );
90 #if( FMI_SUPPORT_A603 )
91  DDX_Text( aDataExchange, IDC_STOPNEW_EDIT_STOPID, mStopId );
92 #endif
93  DDX_Check( aDataExchange, IDC_COMPRESS_LONG_STOP, mCompressLongStop );
94 }
95 
96 //----------------------------------------------------------------------
102 //----------------------------------------------------------------------
104 {
105  CDialog::OnInitDialog();
106 #if( !FMI_SUPPORT_A618 )
107  CButton * rdo618 = (CButton *)GetDlgItem( IDC_STOPNEW_RDO_A618 );
108  rdo618->SetCheck( BST_UNCHECKED );
109  rdo618->EnableWindow( FALSE );
110  GetDlgItem( IDC_COMPRESS_LONG_STOP )->EnableWindow( FALSE );
111 #endif
112 
113 #if( FMI_SUPPORT_A603 )
114  mStopId.Format( _T("%u"), mCom.getNextStopId() );
115 #endif
116 #if( !FMI_SUPPORT_A603 )
117  CButton * rdo603 = (CButton *)GetDlgItem( IDC_STOPNEW_RDO_A603 );
118  rdo603->SetCheck( BST_UNCHECKED );
119  rdo603->EnableWindow( FALSE );
120  GetDlgItem( IDC_STOPNEW_EDIT_STOPID )->EnableWindow( FALSE );
121 #endif
122 #if( !FMI_SUPPORT_A602 )
123  CButton * rdo602 = (CButton *)GetDlgItem( IDC_STOPNEW_RDO_A602 );
124  rdo602->SetCheck( BST_UNCHECKED );
125  rdo602->EnableWindow( FALSE );
126 #endif
127 #if( !FMI_SUPPORT_LEGACY )
128  CButton * rdoLegacy = (CButton *)GetDlgItem( IDC_STOPNEW_RDO_LEGACY );
129  rdoLegacy->SetCheck( BST_UNCHECKED );
130  rdoLegacy->EnableWindow( FALSE );
131 #endif
132  UpdateData( FALSE );
133  return TRUE;
134 } /* OnInitDialog() */
135 
136 //----------------------------------------------------------------------
141 //----------------------------------------------------------------------
143 {
144  bool formIsValid = true;
145 
146 #if !SKIP_VALIDATION
147  char stopIdString[11];
148  memset( stopIdString, 0, sizeof( stopIdString ) );
149 
150  //convert LF and CR+LF to CR since the serialization framework delimits records by LF
151  mMessageStr.Replace( _T( "\r\n" ), _T( "\r" ) );
152  mMessageStr.Replace( _T( "\n" ), _T( "\r" ) );
153 
154  UpdateData( TRUE );
155  if( mLatitudeStr == "" || _tstof( mLatitudeStr ) < -90 || _tstof( mLatitudeStr ) >= 90 )
156  formIsValid = false;
157  else if( mLongitudeStr == "" || _tstof( mLongitudeStr ) < -180 || _tstof( mLongitudeStr ) >= 180 )
158  formIsValid = false;
159  else if( mMessageStr.GetLength() == 0 )
160  formIsValid = false;
161  else
162  {
163  switch ( mStopProtocol )
164  {
165  case STOP_PROTOCOL_A602:
166  formIsValid = ( mMessageStr.GetLength() < 51 );
167  break;
168 #if( FMI_SUPPORT_A603 )
169  case STOP_PROTOCOL_A603:
170  formIsValid = ( mMessageStr.GetLength() < 200 );
171  break;
172 #endif
173 #if( FMI_SUPPORT_A618 )
174  case STOP_PROTOCOL_A618:
175  formIsValid = ( mMessageStr.GetLength() < 2000 );
176  break;
177 #endif
178  }
179  }
180 
181  BOOL stopIdEnabled = FALSE;
182  GetDlgItem( IDC_COMPRESS_LONG_STOP )->EnableWindow( FALSE );
183 #if( FMI_SUPPORT_A618 )
185  {
186  GetDlgItem( IDC_COMPRESS_LONG_STOP )->EnableWindow( TRUE );
187  stopIdEnabled = TRUE;
188  }
189 #endif
190 
191 #if( FMI_SUPPORT_A603 )
193  {
194  stopIdEnabled = TRUE;
195  }
196  if( 0 != WideCharToMultiByte( CP_ACP, 0, mStopId.GetBuffer(), mStopId.GetLength(), stopIdString, sizeof( stopIdString ), NULL, NULL ) )
197  {
198  if( !UTIL_data_is_uint32( stopIdString ) )
199  {
200  formIsValid = false;
201  }
202 
203  }
204  else
205  {
206  formIsValid = false;
207  }
208  #endif
209 #endif
210 
211  GetDlgItem( IDC_STOPNEW_EDIT_STOPID )->EnableWindow( stopIdEnabled );
212  GetDlgItem( IDC_STOPNEW_LBL_STOPID )->EnableWindow( stopIdEnabled );
213 
214  GetDlgItem( IDOK )->EnableWindow( formIsValid );
215 
216 } /* OnFormChanged */
217 
218 //----------------------------------------------------------------------
222 //----------------------------------------------------------------------
224 {
225  double lat;
226  double lon;
227  char message[2000];
228 #if( FMI_SUPPORT_A603 )
229  uint32 stopId;
230 #endif
231 
232  UpdateData( TRUE );
233  //convert LF and CR+LF to CR since the serialization framework delimits records by LF
234  mMessageStr.Replace( _T( "\r\n" ), _T( "\r" ) );
235  mMessageStr.Replace( _T( "\n" ), _T( "\r" ) );
236  WideCharToMultiByte( mCom.mClientCodepage, 0, mMessageStr.GetBuffer(), -1, message, 2000, NULL, NULL );
237  message[1999] = '\0';
238  lat = _tstof( mLatitudeStr.GetBuffer() );
239  lon = _tstof( mLongitudeStr.GetBuffer() );
240  sc_position_type pos;
242  pos.lon = UTIL_convert_degrees_to_semicircles( lon );
243  switch( mStopProtocol )
244  {
245 #if( FMI_SUPPORT_A618 )
246  case STOP_PROTOCOL_A618: {// A618
247  if( mStopId == "" )
248  stopId = mCom.getNextStopId();
249  else
250  stopId = _tstoi( mStopId.GetBuffer() );
251 
252  CFile tmpFile;
253  if ( !tmpFile.Open( LONG_STOP_TEMP_FILE, CFile::modeCreate | CFile::modeWrite ) )
254  {
255  MessageBox( _T( "Unable to open temp file for building long stop payload" ), _T( "Send Stop" ), MB_ICONWARNING | MB_OK );
256  return;
257  }
258 
259  short ver = 0;
260  time_type origination_time = UTIL_get_current_garmin_time();
261 
262  tmpFile.Write( ( const void* ) "STOP", 4 );
263  tmpFile.Write( ( const void* ) &ver, sizeof( ver ) );
264  tmpFile.Write( ( const void* ) &origination_time, sizeof( origination_time ) );
265  tmpFile.Write( ( const void* ) &stopId, sizeof( stopId ) );
266  tmpFile.Write( ( const void* ) message, ( UINT ) strlen( message ) + 1 );
267  tmpFile.Write( ( const void* ) &pos.lat, sizeof( pos.lat ) );
268  tmpFile.Write( ( const void* ) &pos.lon, sizeof( pos.lon ) );
269  tmpFile.Flush();
270  tmpFile.Close();
271 
272  char file[MAX_PATH+1];
273  WideCharToMultiByte( CP_ACP, 0, LONG_STOP_TEMP_FILE, -1, file, MAX_PATH+1, NULL, NULL );
274  file[MAX_PATH] = '\0';
275 
276  char versionString[35];
277  uint8 version[16];
278  uint8 versionLength;
279 
280  memset( version, 0, sizeof( version ) );
281  WideCharToMultiByte( mCom.mClientCodepage, 0, _T( "1" ), -1, versionString, 34, NULL, NULL );
282  versionString[34] = '\0';
283  versionLength = (uint8)minval( 16, strlen( versionString ) );
284  memmove( version, versionString, versionLength );
285 
286  mCom.sendFile( file, versionLength, version, (uint8) FMI_FILE_TYPE_LONG_STOP, mCompressLongStop );
287 
288  StopListItem stopListItem;
289  stopListItem.setId( stopId );
290  stopListItem.setCurrentName( mMessageStr );
291  stopListItem.setStopStatus( STOP_STATUS_UNREAD );
292  stopListItem.setValid();
293  mCom.mA603Stops.put( stopListItem );
294  break;
295  }
296 #endif
297 #if( FMI_SUPPORT_A603 )
298  case STOP_PROTOCOL_A603: // A603
299  if( mStopId == "" )
300  stopId = mCom.getNextStopId();
301  else
302  stopId = _tstoi( mStopId.GetBuffer() );
303  mCom.sendA603Stop( pos.lat, pos.lon, message, stopId );
304  break;
305 #endif
306 #if( FMI_SUPPORT_A602 )
307  case STOP_PROTOCOL_A602: // A602
308  mCom.sendA602Stop( lat, lon, message );
309  break;
310 #endif
311 #if( FMI_SUPPORT_LEGACY )
312  case STOP_PROTOCOL_LEGACY: // Legacy
313  mCom.sendLegacyStop( lat, lon, message );
314  break;
315 #endif
316  default:
317  break;
318  }
319 
320  OnOK();
321 } /* OnBnClickedOk() */
#define IDC_STOPNEW_RDO_A603
Definition: resource.h:318
sint32 UTIL_convert_degrees_to_semicircles(double aDegrees)
Converts a latitude/longitude from degrees to semicircles.
Definition: util.cpp:204
afx_msg void OnFormChanged()
Change handler for all radio buttons and edit boxes on this dialog.
#define IDC_STOPNEW_RDO_LEGACY
Definition: resource.h:319
void setId(const key_type &aId)
Set item&#39;s ID.
#define IDC_STOPNEW_EDIT_NAME
Definition: resource.h:309
BOOL mCompressLongStop
Flag to determine if A618 stop "file" should be compressed.
Definition: CStopNewDlg.h:71
BOOL OnInitDialog()
Initialize the dialog.
afx_msg void OnBnClickedOk()
Click handler for the OK button.
#define IDC_STOPNEW_EDIT_STOPID
Definition: resource.h:310
void put(T &aValue)
Add (or replace) an item in the map.
CString mMessageStr
Text in the Destination Name/Message edit box.
Definition: CStopNewDlg.h:61
#define LONG_STOP_TEMP_FILE
Definition: CStopNewDlg.cpp:16
int mStopProtocol
Index of the selected mStopProtocol.
Definition: CStopNewDlg.h:68
bool UTIL_data_is_uint32(const char *aData)
Determine whether an array of characters consists only of numeric, and that the value when converted ...
Definition: util.cpp:495
CString mLatitudeStr
Text in the Latitude edit box.
Definition: CStopNewDlg.h:55
virtual void DoDataExchange(CDataExchange *aDataExchange)
Perform dialog data exchange and validation.
Definition: CStopNewDlg.cpp:80
#define FALSE
Definition: garmin_types.h:46
#define IDC_STOPNEW_EDIT_LAT
Definition: resource.h:307
FileBackedMap< StopListItem > mA603Stops
Map of unique ID to stop details for all A603 stops on the client.
void setValid(BOOL aValid=TRUE)
Set this item as valid.
Data structure to hold details of an A603 stop that the server needs to keep.
Definition: StopListItem.h:21
#define IDC_STOPNEW_LBL_STOPID
Definition: resource.h:316
void sendA602Stop(double aLatitude, double aLongitude, char *aStopName)
Initiates the A602 stop protocol.
#define TRUE
Definition: garmin_types.h:45
Modal dialog allowing the user to send a new stop to the client.
Definition: CStopNewDlg.h:19
sint32 lat
latitude in semicircles
Definition: garmin_types.h:126
#define IDC_STOPNEW_RDO_A618
Definition: resource.h:320
CString mStopId
Text in the Stop ID edit box.
Definition: CStopNewDlg.h:64
void setCurrentName(CString aName)
Set the current name of the stop.
#define minval(_x, _y)
The smaller of _x and _y.
Definition: util_macros.h:95
codepage_type mClientCodepage
Code page used for encoding of text fields when communicating with the client.
#define IDC_COMPRESS_LONG_STOP
Definition: resource.h:476
Serial communication controller for Garmin and FMI packets.
uint32 getNextStopId()
Find an available stop ID.
#define IDC_STOPNEW_EDIT_LON
Definition: resource.h:308
virtual ~CStopNewDlg()
Destructor.
Definition: CStopNewDlg.cpp:71
#define IDC_STOPNEW_RDO_A602
Definition: resource.h:317
void sendA603Stop(double aLatitude, double aLongitude, char *aStopName, uint32 aStopId)
Initiates the A603 stop protocol.
FmiApplicationLayer & mCom
Reference to the FMI communication controller.
Definition: CStopNewDlg.h:52
Indicates a latitude and longitude in semicircles.
Definition: garmin_types.h:124
void setStopStatus(stop_status_status_type aStatus)
Set the stop status.
unsigned char uint8
8-bit unsigned integer
Definition: garmin_types.h:62
void sendFile(char *aFilename, uint8 aVersionLength, uint8 *aVersion, uint8 aFileType, BOOL gzipIt=FALSE)
Start a file transfer protocol.
time_type UTIL_get_current_garmin_time()
Get the current server time in Garmin format.
Definition: util.cpp:603
CString mLongitudeStr
Text in the Longitude edit box.
Definition: CStopNewDlg.h:58
unsigned long int uint32
32-bit unsigned integer
Definition: garmin_types.h:66
uint32 time_type
Absolute time (number of seconds since 12/31/1989 12:00 am UTC)
Definition: garmin_types.h:97
#define IDD_STOP_NEW
Definition: resource.h:37