00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 #include "fap.h"
00027
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <ctype.h>
00032
00033
00034
00035 #define FEND 0xc0
00036 #define FESC 0xdb
00037 #define TFEND 0xdc
00038 #define TFESC 0xdd
00039
00040
00041
00042 #define BUFFER_LEN 512
00043 char buffer[BUFFER_LEN];
00044
00045
00046
00047
00048 void print_packet(fap_packet_t* packet)
00049 {
00050 int i;
00051
00052 if ( packet->error_code ) printf("error_code: %d\n", *packet->error_code);
00053 if ( packet->error_message ) printf("error_message: %s\n", packet->error_message);
00054 if ( packet->type ) printf("type: %d\n", *packet->type);
00055
00056 if ( packet->orig_packet ) printf("orig_packet: %s\n", packet->orig_packet);
00057 printf("orig_packet_len: %d\n", packet->orig_packet_len);
00058
00059 if ( packet->header ) printf("header: %s\n", packet->header);
00060 if ( packet->body ) printf("body: %s\n", packet->body);
00061 printf("body_len: %d\n", packet->body_len);
00062 if ( packet->src_callsign ) printf("src_callsign: %s\n", packet->src_callsign);
00063 if ( packet->dst_callsign ) printf("dst_callsign: %s\n", packet->dst_callsign);
00064 if ( packet->path )
00065 {
00066 printf("path:");
00067 for ( i = 0; i < packet->path_len; ++i )
00068 {
00069 printf(" %s", packet->path[i]);
00070 }
00071 printf("\n");
00072 }
00073 printf("path_len: %d\n", packet->path_len);
00074
00075 if ( packet->latitude ) printf("latitude: %f\n", *packet->latitude);
00076 if ( packet->longitude ) printf("longitude: %f\n", *packet->longitude);
00077 if ( packet->pos_resolution ) printf("pos_resolution: %f\n", *packet->pos_resolution);
00078 if ( packet->pos_ambiguity ) printf("pos_ambiguity: %d\n", *packet->pos_ambiguity);
00079 if ( packet->dao_datum_byte ) printf("dao_datum_byte: %c\n", packet->dao_datum_byte);
00080
00081 if ( packet->altitude ) printf("altitude: %f\n", *packet->altitude);
00082 if ( packet->course ) printf("course: %d\n", *packet->course);
00083 if ( packet->speed ) printf("speed: %f\n", *packet->speed);
00084
00085 if ( packet->symbol_table ) printf("symbol_table: %c\n", packet->symbol_table);
00086 if ( packet->symbol_code ) printf("symbol_code: %c\n", packet->symbol_code);
00087
00088 if ( packet->messaging ) printf("messaging: %d\n", *packet->messaging);
00089 if ( packet->destination ) printf("destination: %s\n", packet->destination);
00090 if ( packet->message ) printf("message: %s\n", packet->message);
00091 if ( packet->message_ack ) printf("message_ack: %s\n", packet->message_ack);
00092 if ( packet->message_id ) printf("message_id: %s\n", packet->message_id);
00093 if ( packet->comment ) printf("comment: %s\n", packet->comment);
00094 printf("comment_len: %d\n", packet->comment_len);
00095
00096 if ( packet->object_or_item_name ) printf("object_or_item_name: %s\n", packet->object_or_item_name);
00097 if ( packet->alive ) printf("alive: %d\n", *packet->alive);
00098
00099 if ( packet->gps_fix_status ) printf("gps_fix_status: %d\n", *packet->gps_fix_status);
00100 if ( packet->radio_range ) printf("radio_range: %d\n", *packet->radio_range);
00101 if ( packet->phg ) printf("phg: %s\n", packet->phg);
00102 if ( packet->timestamp ) printf("timestamp: %d (%s)\n", (unsigned int)*packet->timestamp, ctime(packet->timestamp));
00103 if ( packet->nmea_checksum_ok ) printf("nmea_checksum_ok: %d\n", *packet->nmea_checksum_ok);
00104
00105 if ( packet->wx_report )
00106 {
00107 printf("wx_report:\n");
00108 if ( packet->wx_report->wind_gust) printf("\twind_gust: %f\n", *packet->wx_report->wind_gust);
00109 if ( packet->wx_report->wind_dir ) printf("\twind_dir: %d\n", *packet->wx_report->wind_dir);
00110 if ( packet->wx_report->wind_speed ) printf("\twind_speed: %f\n", *packet->wx_report->wind_speed);
00111 if ( packet->wx_report->temp ) printf("\ttemp: %f\n", *packet->wx_report->temp);
00112 if ( packet->wx_report->temp_in ) printf("\ttemp_in: %f\n", *packet->wx_report->temp_in);
00113 if ( packet->wx_report->rain_1h ) printf("\train_1h: %f\n", *packet->wx_report->rain_1h);
00114 if ( packet->wx_report->rain_24h ) printf("\train_24h: %f\n", *packet->wx_report->rain_24h);
00115 if ( packet->wx_report->rain_midnight ) printf("\train_midnight: %f\n", *packet->wx_report->rain_midnight);
00116 if ( packet->wx_report->humidity ) printf("\thumidity: %d\n", *packet->wx_report->humidity);
00117 if ( packet->wx_report->humidity_in ) printf("\thumidity_in: %d\n", *packet->wx_report->humidity_in);
00118 if ( packet->wx_report->pressure ) printf("\tpressure: %f\n", *packet->wx_report->pressure);
00119 if ( packet->wx_report->luminosity ) printf("\tluminosity: %d\n", *packet->wx_report->luminosity);
00120 if ( packet->wx_report->snow_24h ) printf("\tsnow_24h: %f\n", *packet->wx_report->snow_24h);
00121 if ( packet->wx_report->soft ) printf("\tsoft: %s\n", packet->wx_report->soft);
00122 }
00123
00124 if ( packet->telemetry )
00125 {
00126 printf("telemetry:\n");
00127 printf("\tseq: %d\n", packet->telemetry->seq);
00128 printf("\tval1: %f\n", packet->telemetry->val1);
00129 printf("\tval2: %f\n", packet->telemetry->val2);
00130 printf("\tval3: %f\n", packet->telemetry->val3);
00131 printf("\tval4: %f\n", packet->telemetry->val4);
00132 printf("\tval5: %f\n", packet->telemetry->val5);
00133 printf("\tbits: %s\n", packet->telemetry->bits);
00134 }
00135
00136 if ( packet->messagebits ) printf("messagebits: %s (%s)\n", packet->messagebits, fap_mice_mbits_to_message(packet->messagebits));
00137 if ( packet->status ) printf("status: %s\n", packet->status);
00138 printf("status_len: %d\n", packet->status_len);
00139
00140 if ( packet->capabilities )
00141 {
00142 printf("capabilities:");
00143 printf("\n");
00144 }
00145 printf("capabilites_len: %d\n", packet->capabilities_len);
00146 }
00147
00148
00149
00150
00151 int test_parser()
00152 {
00153 fap_packet_t* packet;
00154 int retval = 1;
00155
00156 memset(buffer, 0, BUFFER_LEN);
00157 sprintf(buffer, "OH2GVE-15>APRS,TCPIP*:!//H*{TPI.L{0Ctest");
00158 printf("########################################\n");
00159 printf("Testing parser with packet (%s) ... ", buffer);
00160 packet = fap_parseaprs(buffer, strlen(buffer), 0);
00161 if ( packet->error_code )
00162 {
00163 printf("failed: %s\n", fap_explain_error(*packet->error_code));
00164 retval = 0;
00165 }
00166 else printf("success\n");
00167 print_packet(packet);
00168 printf("########################################\n");
00169 fap_free(packet);
00170
00171 memset(buffer, 0, BUFFER_LEN);
00172 sprintf(buffer, "OH3RBE-1>APND0Z,WIDE1-1:)ACUTA!6130.32N/02348.81E+ 24h Emergency Ward ACUTA +358 3 5657 0023");
00173 printf("########################################\n");
00174 printf("Testing parser with packet (%s) ... ", buffer);
00175 packet = fap_parseaprs(buffer, strlen(buffer), 0);
00176 if ( packet->error_code )
00177 {
00178 printf("failed: %s\n", fap_explain_error(*packet->error_code));
00179 retval = 0;
00180 }
00181 else printf("success\n");
00182 print_packet(packet);
00183 printf("########################################\n");
00184 fap_free(packet);
00185
00186 memset(buffer, 0, BUFFER_LEN);
00187 sprintf(buffer, "OH3RDH>APNU19,OH3RBE-3*:!6059.33NU02530.82E#PHG5330 [Hollola KP20SX UIDIGI 1.9B3] 2");
00188 printf("########################################\n");
00189 printf("Testing parser with packet (%s) ... ", buffer);
00190 packet = fap_parseaprs(buffer, strlen(buffer), 0);
00191 if ( packet->error_code )
00192 {
00193 printf("failed: %s\n", fap_explain_error(*packet->error_code));
00194 retval = 0;
00195 }
00196 else printf("success\n");
00197 print_packet(packet);
00198 printf("########################################\n");
00199 fap_free(packet);
00200
00201 memset(buffer, 0, BUFFER_LEN);
00202 sprintf(buffer, "OH3RBE>APD225,OH3RBE-1*,WIDE1*:}OH3KV>APU25N,TCPIP*,OH3RBE*:=6055.91N/02335.96E#RX-igate KOIJARVI OH3KV {UIV32}");
00203 printf("########################################\n");
00204 printf("Testing parser with packet (%s) ... ", buffer);
00205 packet = fap_parseaprs(buffer, strlen(buffer), 0);
00206 if ( packet->error_code )
00207 {
00208 printf("failed: %s\n", fap_explain_error(*packet->error_code));
00209 retval = 0;
00210 }
00211 else printf("success\n");
00212 print_packet(packet);
00213 printf("########################################\n");
00214 fap_free(packet);
00215
00216 memset(buffer, 0, BUFFER_LEN);
00217 sprintf(buffer, "OH5LEP-9>V0SS62,OH2RDU*,OH3RBE-3*:`4V.l!e>/AZO-592");
00218 printf("########################################\n");
00219 printf("Testing parser with packet (%s) ... ", buffer);
00220 packet = fap_parseaprs(buffer, strlen(buffer), 0);
00221 if ( packet->error_code )
00222 {
00223 printf("failed: %s\n", fap_explain_error(*packet->error_code));
00224 retval = 0;
00225 }
00226 else printf("success\n");
00227 print_packet(packet);
00228 printf("########################################\n");
00229 fap_free(packet);
00230
00231 memset(buffer, 0, BUFFER_LEN);
00232 sprintf(buffer, "OH3RUX>BEACON,OH3RBE-1*,TRACE2-1:!6213.36N/02404.22E#PHG9500/R,W,T OH3RUX Virrat");
00233 printf("########################################\n");
00234 printf("Testing parser with packet (%s) ... ", buffer);
00235 packet = fap_parseaprs(buffer, strlen(buffer), 1);
00236 if ( packet->error_code )
00237 {
00238 printf("failed: %s\n", fap_explain_error(*packet->error_code));
00239 retval = 0;
00240 }
00241 else printf("success\n");
00242 print_packet(packet);
00243 printf("########################################\n");
00244 fap_free(packet);
00245
00246 memset(buffer, 0, BUFFER_LEN);
00247 sprintf(buffer, "OH7AA-1>APRS,WIDE1-1,WIDE2-2,qAo,OH7AA::OH7LZB :Testing, 1 2 3{42");
00248 printf("########################################\n");
00249 printf("Testing parser with packet (%s) ... ", buffer);
00250 packet = fap_parseaprs(buffer, strlen(buffer), 0);
00251 if ( packet->error_code )
00252 {
00253 printf("failed: %s\n", fap_explain_error(*packet->error_code));
00254 retval = 0;
00255 }
00256 else printf("success\n");
00257 print_packet(packet);
00258 printf("########################################\n");
00259 fap_free(packet);
00260
00261
00262
00263 return retval;
00264 }
00265
00266
00267
00268
00269 int test_converters(char const* filename)
00270 {
00271 FILE* infile;
00272
00273 int retval = 1, badness = 0, i = 0;
00274
00275 unsigned int out_len = BUFFER_LEN, tnc_id = 666;
00276 unsigned int kissframe_len = BUFFER_LEN;
00277 char outbuffer[out_len];
00278
00279 char orig_data[BUFFER_LEN];
00280 unsigned int orig_data_len;
00281
00282
00283 printf("Reading test data from %s:", filename);
00284 memset(buffer, 0, BUFFER_LEN);
00285 infile = fopen(filename, "r");
00286 while ( !feof(infile) )
00287 {
00288 buffer[i] = getc(infile);
00289 orig_data[i] = buffer[i];
00290 if ( !feof(infile) )
00291 {
00292 printf(" %2hhx", buffer[i]);
00293 ++i;
00294 }
00295 else
00296 {
00297 break;
00298 }
00299 }
00300 orig_data_len = i;
00301 fclose(infile);
00302 printf("\n");
00303
00304
00305 printf("########################################\n");
00306 printf("Testing kiss_to_tnc2() with a %d-byte input ... ", i);
00307 memset(outbuffer, 0, out_len);
00308 if ( fap_kiss_to_tnc2(buffer, i, (char*)&outbuffer, &out_len, &tnc_id) )
00309 {
00310 printf("success.\n");
00311 }
00312 else
00313 {
00314 printf("failure.\n");
00315 retval = 0;
00316 }
00317 printf("tnc2frame=(%s) len=(%d) tnc_id=(%d)\n", outbuffer, out_len, tnc_id);
00318 if ( !retval ) return 0;
00319 printf("########################################\n");
00320
00321
00322 printf("########################################\n");
00323 printf("Testing tnc2_to_kiss() by trying to convert output of previous test back to KISS frame ... ");
00324 memset(buffer, 0, BUFFER_LEN);
00325 if ( fap_tnc2_to_kiss(outbuffer, out_len, tnc_id, buffer, &kissframe_len) )
00326 {
00327 printf("success.\n");
00328 }
00329 else
00330 {
00331 printf("failure.\n");
00332 retval = 0;
00333 }
00334 printf("Comparing output to original test data:");
00335 for ( i = 0; i < orig_data_len; ++i )
00336 {
00337 if ( orig_data[i] != buffer[i] )
00338 {
00339 printf("!%2hhx%2hhx", orig_data[i], buffer[i]);
00340 badness = 1;
00341 }
00342 else
00343 {
00344 printf(" %2hhx%2hhx", orig_data[i], buffer[i]);
00345 }
00346 }
00347 printf("\n");
00348 if ( badness || kissframe_len != orig_data_len )
00349 {
00350 printf("Differences found, test failed!\n");
00351 retval = 0;
00352 }
00353 printf("Original test data had %d bytes, tnc2_to_kiss() returned %d bytes.\n", orig_data_len, kissframe_len);
00354 return retval;
00355 }
00356
00357
00358
00359 int main(int argc, char* argv[])
00360 {
00361 int retval;
00362 char const* kiss_datafile_name = "smoketest.data";
00363 FILE* tmp;
00364
00365 fap_init();
00366
00367 printf("Testing the parser.\n");
00368 retval = test_parser();
00369 if ( (tmp = fopen(kiss_datafile_name, "r")) != NULL )
00370 {
00371 fclose(tmp);
00372 printf("Found test data file, engaging converter tests.\n");
00373 retval = test_converters(kiss_datafile_name) && retval;
00374 }
00375
00376 fap_cleanup();
00377
00378 if ( !retval ) return EXIT_FAILURE;
00379 return EXIT_SUCCESS;
00380 }