gpssnmp.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* gpssnmp - poll local gpsd for SNMP variables
  2. *
  3. * To build this:
  4. * gcc -o gpssnmp gpssnmp.c -lgps
  5. *
  6. * Copyright 2016 David Taylor <gpsd@david.taylor.name>
  7. *
  8. * Copyright2018 by the GPSD project
  9. * SPDX-License-Identifier: BSD-2-clause
  10. *
  11. */
  12. #include <gps.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. static void usage() {
  16. printf("\n"
  17. "Usage:\n"
  18. "\n"
  19. "to get OID_VISIBLE\n"
  20. " $ gpssnmp -g .1.3.6.1.2.1.25.1.31\n"
  21. " .1.3.6.1.2.1.25.1.31\n"
  22. " gauge\n"
  23. " 13\n"
  24. "\n"
  25. "to get OID_USED\n"
  26. " $ gpssnmp -g .1.3.6.1.2.1.25.1.32\n"
  27. " .1.3.6.1.2.1.25.1.32\n"
  28. " gauge\n"
  29. " 4\n"
  30. "\n"
  31. "to get OID_SNR_AVG\n"
  32. " $ gpssnmp -g .1.3.6.1.2.1.25.1.33\n"
  33. " .1.3.6.1.2.1.25.1.33\n"
  34. " gauge\n"
  35. " 22.250000\n"
  36. "\n");
  37. }
  38. int main (int argc, char **argv) {
  39. struct gps_data_t gpsdata;
  40. #define OID_VISIBLE ".1.3.6.1.2.1.25.1.31"
  41. #define OID_USED ".1.3.6.1.2.1.25.1.32"
  42. #define OID_SNR_AVG ".1.3.6.1.2.1.25.1.33"
  43. if ((argc > 2) && (strcmp ("-g", argv[1]) == 0)) {
  44. int i;
  45. double snr_total=0;
  46. double snr_avg = 0.0;
  47. int status, used, visible;
  48. status = gps_open (GPSD_SHARED_MEMORY, DEFAULT_GPSD_PORT, &gpsdata);
  49. status = gps_read (&gpsdata, NULL, 0);
  50. used = gpsdata.satellites_used;
  51. visible = gpsdata.satellites_visible;
  52. for(i=0; i<=used; i++) {
  53. if (gpsdata.skyview[i].used > 0 && gpsdata.skyview[i].ss > 1) {
  54. // printf ("i: %d, P:%d, ss: %f\n", i, gpsdata.skyview[i].PRN,
  55. // gpsdata.skyview[i].ss);
  56. snr_total+=gpsdata.skyview[i].ss;
  57. }
  58. }
  59. gps_close (&gpsdata);
  60. if (used > 0) {
  61. snr_avg = snr_total / used;
  62. }
  63. if (strcmp (OID_VISIBLE, argv[2]) == 0) {
  64. printf (OID_VISIBLE);
  65. printf ("\n");
  66. printf ("gauge\n");
  67. printf ("%d\n", visible);
  68. }
  69. if (strcmp (OID_USED, argv[2]) == 0) {
  70. printf (OID_USED);
  71. printf ("\n");
  72. printf ("gauge\n");
  73. printf ("%d\n", used);
  74. }
  75. if (strcmp (OID_SNR_AVG, argv[2]) == 0) {
  76. printf (OID_SNR_AVG);
  77. printf ("\n");
  78. printf ("gauge\n");
  79. printf ("%lf\n", snr_avg);
  80. }
  81. } else {
  82. usage();
  83. }
  84. return 0;
  85. }