@@ -50,40 +50,28 @@ GPSController::~GPSController() {
50
50
bool GPSController::Handle_GPSConfig (wippersnapper_gps_GPSConfig *gps_config) {
51
51
// Attempt to decode the GPSConfig message
52
52
if (_driver_type == GPS_DRV_MTK) {
53
- // handle commands for mtk driver
54
53
WS_DEBUG_PRINTLN (" [gps] Handling GPSConfig for MediaTek driver..." );
55
54
if (gps_config == nullptr ) {
56
55
WS_DEBUG_PRINTLN (" [gps] ERROR: No GPSConfig message found!" );
57
56
return false ;
58
57
}
59
58
// Iterate through the command sentences and send them to the GPS module
59
+ // TODO: We may want to break this out into a generic function that supports
60
+ // MTK, Ublox, etc...
60
61
for (size_t i = 0 ; i < gps_config->commands_count ; i++) {
61
62
WS_DEBUG_PRINT (" [gps] Sending command to MediaTek GPS: " );
62
63
WS_DEBUG_PRINTLN (gps_config->commands [i]);
63
-
64
- // Build the ACK command
65
- // first, strip out the command number only from the command string
66
- int cmd_num = 0 ;
67
- if (sscanf (gps_config->commands [i], " $PMTK%d" , &cmd_num) == 1 ) {
68
- WS_DEBUG_PRINT (" [gps] Command number extracted: " );
69
- WS_DEBUG_PRINTLN (cmd_num);
70
- } else {
71
- WS_DEBUG_PRINTLN (" [gps] ERROR: Failed to extract command number!" );
72
- return false ;
73
- }
74
- char cmd_resp[255 ];
75
- snprintf (cmd_resp, sizeof (cmd_resp), " $PMTK001,%d,3" , cmd_num);
76
- _ada_gps->addChecksum (cmd_resp);
77
- WS_DEBUG_PRINT (" [gps] ACK command w/checksum: " );
78
- WS_DEBUG_PRINTLN (cmd_resp);
79
-
80
64
// Send the command to the GPS module
81
65
_hw_serial->flush (); // Flush the serial buffer before sending
82
66
_ada_gps->sendCommand (gps_config->commands [i]);
83
67
// and wait for the corresponding response from the GPS module
84
- if (!_ada_gps->waitForSentence (cmd_resp)) {
85
- WS_DEBUG_PRINT (
86
- " [gps] ERROR: Failed to get response from MediaTek for cmd:" );
68
+ char msg_resp[MAX_NEMA_SENTENCE_LEN];
69
+ if (!BuildPmtkAck (gps_config->commands [i], msg_resp)) {
70
+ WS_DEBUG_PRINTLN (" [gps] ERROR: Failed to build PMTK ACK response!" );
71
+ return false ;
72
+ }
73
+ if (!_ada_gps->waitForSentence (msg_resp)) {
74
+ WS_DEBUG_PRINT (" [gps] ERROR: Failed to get response | cmd:" );
87
75
WS_DEBUG_PRINTLN (gps_config->commands [i]);
88
76
return false ;
89
77
}
@@ -191,11 +179,8 @@ bool GPSController::DetectMediatek() {
191
179
delay (1 );
192
180
}
193
181
194
- if (timeout == 0 ) {
195
- // TODO: Remove in production
196
- WS_DEBUG_PRINTLN (" [gps] ERROR: Timeout from MediaTek GPS module!" );
182
+ if (timeout == 0 )
197
183
return false ;
198
- }
199
184
200
185
// We found a response, let's verify that it's the expected PMTK_DK_RELEASE
201
186
// command by reading out the NMEA sentence string into a buffer
@@ -231,3 +216,20 @@ bool GPSController::DetectMediatek() {
231
216
_driver_type = GPS_DRV_MTK;
232
217
return true ;
233
218
}
219
+
220
+ /* !
221
+ * @brief Builds a PMTK acknowledgment message for the provided command.
222
+ * @param msg_cmd
223
+ * Pointer to a command string.
224
+ * @param msg_resp
225
+ * Pointer to a response string.
226
+ * @returns True if the acknowledgment was built successfully, False otherwise.
227
+ */
228
+ bool GPSController::BuildPmtkAck (char *msg_cmd, char *msg_resp) {
229
+ int cmd_num = 0 ;
230
+ if (sscanf (msg_cmd, " $PMTK%d" , &cmd_num) != 1 )
231
+ return false ;
232
+ snprintf (msg_resp, MAX_NEMA_SENTENCE_LEN, " $PMTK001,%d,3" , cmd_num);
233
+ _ada_gps->addChecksum (msg_resp);
234
+ return true ;
235
+ }
0 commit comments