24#include "garmin_wrapper.h"
27#include "garmin_gps.h"
37extern char last_error[];
41wxString GetLastGarminError(
void) {
42 return wxString(GetDeviceLastError(), wxConvUTF8);
52int Garmin_GPS_Init(
const wxString &port_name) {
57 GPS_Enable_Diagnose();
63 ret = GPS_Init(port_name.mb_str());
69int Garmin_GPS_Open(wxString &port_name) {
70 return GPS_Init(port_name.mb_str());
73int Garmin_GPS_PVT_On(wxString &port_name) {
74 return Garmin_Serial_GPS_PVT_On(port_name.mb_str());
77int Garmin_GPS_PVT_Off(wxString &port_name) {
78 return Garmin_Serial_GPS_PVT_Off(port_name.mb_str());
81int Garmin_GPS_GetPVT(
void *pvt) {
82 return GPS_Serial_Command_Pvt_Get((GPS_PPvt_Data *)pvt);
85void Garmin_GPS_ClosePortVerify(
void) { VerifyPortClosed(); }
87wxString Garmin_GPS_GetSaveString() {
88 return wxString(gps_save_string, wxConvUTF8);
91void Garmin_GPS_PrepareWptData(GPS_PWay pway,
RoutePoint *prp) {
92 pway->lat = prp->m_lat;
93 pway->lon = prp->m_lon;
94 pway->alt_is_unknown = 1;
96 strncpy(pway->ident, (prp->GetName().Truncate(6)).mb_str(), 6);
99int Garmin_GPS_SendWaypoints(
const wxString &port_name,
100 RoutePointList *wplist) {
103 int nPoints = wplist->GetCount();
107 GPS_SWay **ppway = (GPS_SWay **)malloc(nPoints *
sizeof(GPS_PWay));
110 for (
int i = 0; i < nPoints; i++) ppway[i] = GPS_Way_New();
113 for (
int i = 0; i < nPoints; i++) {
114 GPS_PWay pway = ppway[i];
115 wxRoutePointListNode *node = wplist->Item(i);
118 Garmin_GPS_PrepareWptData(pway, prp);
122 int xfer_result = GPS_Command_Send_Waypoint(
123 port_name.mb_str(), ppway, nPoints, 0 );
124 ret_val = xfer_result;
127 for (
int i = 0; i < nPoints; i++) GPS_Way_Del(&ppway[i]);
147GPS_SWay **Garmin_GPS_Create_A200_Route(
Route *pr,
int route_number,
149 RoutePointList *wplist = pr->pRoutePointList;
150 int nPoints = wplist->GetCount();
157 GPS_SWay **ppway = (GPS_SWay **)malloc((*size) *
sizeof(GPS_PWay));
160 for (
int i = 0; i < nPoints + 1; i++) ppway[i] = GPS_Way_New();
166 GPS_PWay pway = ppway[0];
168 pway->rte_num = route_number;
169 strncpy(pway->rte_ident, (pr->m_RouteNameString.Truncate(255)).mb_str(), 255);
170 strncpy(pway->rte_cmnt, (pr->m_RouteNameString.Truncate(19)).mb_str(), 19);
173 for (
int i = 1; i < *size; i++) {
174 GPS_PWay pway = ppway[i];
175 wxRoutePointListNode *node = wplist->Item(i - 1);
178 Garmin_GPS_PrepareWptData(pway, prp);
199GPS_SWay **Garmin_GPS_Create_A201_Route(
Route *pr,
int route_number,
201 RoutePointList *wplist = pr->pRoutePointList;
202 int nPoints = wplist->GetCount();
208 *size = 1 + nPoints + (nPoints - 1);
210 GPS_SWay **ppway = (GPS_SWay **)malloc((*size) *
sizeof(GPS_PWay));
213 for (
int i = 0; i < *size; i++) ppway[i] = GPS_Way_New();
219 GPS_PWay pway = ppway[0];
221 pway->rte_num = route_number;
222 strncpy(pway->rte_ident, (pr->m_RouteNameString.Truncate(255)).mb_str(), 255);
223 strncpy(pway->rte_cmnt, (pr->m_RouteNameString.Truncate(19)).mb_str(), 19);
227 for (
int i = 1; i < *size; i++) {
230 GPS_PWay pway = ppway[i];
231 wxRoutePointListNode *node = wplist->Item((i - 1) / 2);
234 Garmin_GPS_PrepareWptData(pway, prp);
238 GPS_PWay pway = ppway[i];
240 pway->rte_link_class = 0;
241 memset(pway->rte_link_subclass, 0,
sizeof(pway->rte_link_subclass));
242 memset(pway->rte_link_ident, 0,
sizeof(pway->rte_link_ident));
249int Garmin_GPS_SendRoute(
const wxString &port_name,
Route *pr,
250 wxGauge *pProgress) {
253 int route_number = 1;
257 if ((gps_rte_hdr_type == pD200) || (gps_rte_hdr_type == pD201)) {
259 GPS_Diag(
"Garmin: trying to get free route number");
260 GPS_PWay *pprouteway;
261 int32 npacks = GPS_A200_Get(port_name.mb_str(), &pprouteway);
262 if (npacks < 0)
return npacks;
265 pProgress->SetValue(60);
266 pProgress->Refresh();
275 for (
int i = 0; i < 10; i++) brn[i] =
false;
277 for (
int ip = 0; ip < npacks; ip++) {
278 GPS_PWay pway = pprouteway[ip];
280 if ((pway->rte_num < 10)) brn[pway->rte_num] =
true;
285 bool bfound_empty =
false;
286 for (
int i = 1; i < 10; i++) {
287 if (brn[i] ==
false) {
293 GPS_Diag(
"Using route number: %d", route_number);
298 int rv = OCPNMessageBox(
299 NULL, _(
"Overwrite Garmin device route number 1?"),
300 _(
"OpenCPN Message"), wxOK | wxCANCEL | wxICON_QUESTION);
301 if (rv != wxID_OK)
return 0;
309 if (gps_route_transfer == pA201)
310 ppway = Garmin_GPS_Create_A201_Route(pr, route_number, &elements);
312 ppway = Garmin_GPS_Create_A200_Route(pr, route_number, &elements);
315 int xfer_result = GPS_Command_Send_Route(port_name.mb_str(), ppway, elements);
316 ret_val = xfer_result;
319 for (
int i = 0; i < elements; i++) GPS_Way_Del(&ppway[i]);
324 pProgress->SetValue(80);
325 pProgress->Refresh();