You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

garmin_device_xml.cc 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Parse 'GarminDevice.xml' on a Garmin mass storage device (e.g. Zumo,
  3. Nuvi, Colorado, etc. and return key device info.
  4. Copyright (C) 2008 Robert Lipe, robertlipe+source@gpsbabel.org
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  16. */
  17. // References:
  18. // http://developer.garmin.com/web-device/garmin-mass-storage-mode-devices/
  19. // http://developer.garmin.com/schemas/device/v2/
  20. #include "defs.h"
  21. #include "xmlgeneric.h"
  22. #include "garmin_device_xml.h"
  23. #include <QtCore/QXmlStreamAttributes>
  24. #include <stdio.h>
  25. #define MYNAME "whatever"
  26. static gdx_info* my_gdx_info;
  27. static int type;
  28. static char* mountpoint, *base, *path, *ext;
  29. static xg_callback device_s, id_s, path_s, ext_s, base_s, dir_s;
  30. jmp_buf gdx_jmp_buf;
  31. void type_s(xg_string args, const QXmlStreamAttributes*)
  32. {
  33. type = args.compare("GPSData");
  34. }
  35. void device_s(xg_string args, const QXmlStreamAttributes*)
  36. {
  37. if (my_gdx_info) {
  38. fatal(MYNAME ": More than one device type found in file.\n");
  39. }
  40. my_gdx_info = (gdx_info*) xcalloc(sizeof *my_gdx_info, 1);
  41. my_gdx_info->device_desc = xstrdup(args);
  42. }
  43. void id_s(xg_string args, const QXmlStreamAttributes*)
  44. {
  45. my_gdx_info->device_id = xstrdup(args);
  46. }
  47. void path_s(xg_string args, const QXmlStreamAttributes*)
  48. {
  49. path = xstrdup(args);
  50. }
  51. void ext_s(xg_string args, const QXmlStreamAttributes*)
  52. {
  53. ext = xstrdup(args);
  54. }
  55. void base_s(xg_string args, const QXmlStreamAttributes*)
  56. {
  57. base = xstrdup(args);
  58. }
  59. void dir_s(xg_string args, const QXmlStreamAttributes*)
  60. {
  61. if (type) {
  62. return;
  63. }
  64. if ((args == "OutputFromUnit")) {
  65. xasprintf(&my_gdx_info->from_device.path, "%s%c%s",
  66. mountpoint, GB_PATHSEP, path);
  67. my_gdx_info->from_device.basename = xstrdup(base);
  68. my_gdx_info->from_device.extension = xstrdup(ext);
  69. xasprintf(&my_gdx_info->from_device.canon, "%s/%s.%s",
  70. my_gdx_info->from_device.path,
  71. my_gdx_info->from_device.basename,
  72. my_gdx_info->from_device.extension);
  73. } if ((args == "InputToUnit")) {
  74. xasprintf(&my_gdx_info->to_device.path, "%s%c%s",
  75. mountpoint, GB_PATHSEP, path);
  76. my_gdx_info->to_device.basename = xstrdup(base);
  77. my_gdx_info->to_device.extension = xstrdup(ext);
  78. } else {
  79. fatal(MYNAME ":Unknown direction '%s'\n", qPrintable(args));
  80. }
  81. if (base) {
  82. xfree(base) ;
  83. }
  84. base = NULL;
  85. if (ext) {
  86. xfree(ext) ;
  87. }
  88. ext = NULL;
  89. if (path) {
  90. xfree(path) ;
  91. }
  92. path = NULL;
  93. }
  94. static xg_tag_mapping gdx_map[] = {
  95. { device_s, cb_cdata, "/Device/Model/Description" },
  96. { id_s, cb_cdata, "/Device/Id" },
  97. { path_s, cb_cdata, "/Device/MassStorageMode/DataType/File/Location/Path" },
  98. { type_s, cb_cdata, "/Device/MassStorageMode/DataType/Name" },
  99. { ext_s, cb_cdata, "/Device/MassStorageMode/DataType/File/Location/FileExtension" },
  100. { base_s, cb_cdata, "/Device/MassStorageMode/DataType/File/Location/BaseName" },
  101. { dir_s, cb_cdata, "/Device/MassStorageMode/DataType/File/TransferDirection" },
  102. { 0, (xg_cb_type) 0, NULL }
  103. };
  104. const gdx_info*
  105. gdx_read(const char* fname)
  106. {
  107. // Test file open-able before gb_open gets a chance to fatal().
  108. FILE* fin = fopen(fname, "r");
  109. if (fin) {
  110. fclose(fin);
  111. xml_init(fname, gdx_map, NULL);
  112. xml_read();
  113. xml_deinit();
  114. }
  115. return my_gdx_info;
  116. }
  117. // Look for the Device in the incoming NULL-terminated list of directories
  118. const gdx_info*
  119. gdx_find_file(char** dirlist)
  120. {
  121. const gdx_info* gdx;
  122. while (dirlist && *dirlist) {
  123. char* tbuf;
  124. xasprintf(&tbuf, "%s/%s", *dirlist, "/Garmin/GarminDevice.xml");
  125. mountpoint = *dirlist;
  126. gdx = gdx_read(tbuf);
  127. xfree(tbuf);
  128. if (gdx) {
  129. longjmp(gdx_jmp_buf, 1);
  130. }
  131. dirlist++;
  132. }
  133. return NULL;
  134. }
  135. const gdx_info*
  136. gdx_get_info()
  137. {
  138. return my_gdx_info;
  139. }