format_debug_msg.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/python
  2. #
  3. # Copyright (c) 2019 Clementine Computing LLC.
  4. #
  5. # This file is part of PopuFare.
  6. #
  7. # PopuFare is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # PopuFare is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. # Messages can be inserted into the IPC hub via the `debug_client` program.
  21. # The `debug_client` requires messages to be of the format:
  22. #
  23. # MAILBOX: XX XX ... XX
  24. #
  25. # Where `MAILBOX` is the mailbox of the message and XX are the hex bytes
  26. # of the payload message in ASCII format.
  27. #
  28. # For example, to send a message to the `BILLING_LOG` mailbox with a message
  29. # of "#testing", the following would be used:
  30. #
  31. # echo 'BILLING_LOG: 23 74 65 73 74 69 6e 67' | /home/bus/bin/debug_client -s
  32. #
  33. # Where the '#' prefix sends the mesage to the `diagnostic_log` table on the server.
  34. #
  35. # See the `MAILBOX_*` defines in `commhub.h` for the full list of mailboxes.
  36. # As of this writing, the list of mailboxes is:
  37. #
  38. # _HELLO _SUBSCRIBE _UNSUBSCRIBE _BROADCAST _WIRETAP
  39. # _ERROR _PING _PONG _EXIT _HUP
  40. # STATUS_REQ PASS_STATUS BILL_STATUS DRIVER_STATUS GPS_STATUS
  41. # STOP_STATUS TOKEN_MAG TOKEN_RFID FLUSH_PASSES UPDATE_PASSES
  42. # DRIVER_NOTIFY BILLING_LOG PIU_MESSAGE SET_PADDLE PADDLE_ACK
  43. # NEXT_STOP PREV_STOP RULE_CALL VAULT_DROP PASSDB_CONSIST
  44. # PASSDB_PULSE
  45. #
  46. import sys
  47. import getopt
  48. VERSION = "0.1.0"
  49. VALID_MAILBOX = [
  50. "_HELLO", "_SUBSCRIBE", "_UNSUBSCRIBE",
  51. "_BROADCAST", "_WIRETAP", "_ERROR",
  52. "_PING", "_PONG", "_EXIT", "_HUP",
  53. "STATUS_REQ",
  54. "PASS_STATUS", "BILL_STATUS", "DRIVER_STATUS",
  55. "GPS_STATUS", "STOP_STATUS",
  56. "TOKEN_MAG", "TOKEN_RFID",
  57. "FLUSH_PASSES", "UPDATE_PASSES",
  58. "DRIVER_NOTIFY", "BILLING_LOG", "PIU_MESSAGE",
  59. "SET_PADDLE", "PADDLE_ACK",
  60. "NEXT_STOP", "PREV_STOP",
  61. "RULE_CALL", "VAULT_DROP",
  62. "PASSDB_CONSIST", "PASSDB_PULSE" ]
  63. def show_version(ofp):
  64. ofp.write("Version: " + VERSION + "\n");
  65. def show_help(ofp):
  66. ofp.write("\n");
  67. ofp.write("usage:");
  68. ofp.write("\n");
  69. ofp.write("\n");
  70. ofp.write(" format_debug_msg.py [-h] [-F] <MAILBOX> [--asciihex] [-i] <PAYLOAD>\n");
  71. ofp.write("\n");
  72. ofp.write(" <MAILBOX> destination mailbox\n")
  73. ofp.write(" <PAYLOAD> payload to convert\n")
  74. ofp.write(" [--show-mailbox] print out all valid mailboxes\n")
  75. ofp.write(" [-F|-force-mailbox] do not check to see if MAILBOX is in list of valid mailboxes\n")
  76. ofp.write(" [--asciihex] payload in ASCII hex format (with spaces) (example: ' 23 74 65 73 74')\n")
  77. ofp.write(" [-i] Read from file instead ('-' for stdin)\n")
  78. ofp.write(" [-h|-help] help (this screen)\n")
  79. ofp.write("\n");
  80. def show_mailboxes():
  81. for mb in VALID_MAILBOX:
  82. print mb
  83. mailbox = ""
  84. payload_text = ""
  85. payload_converted = ""
  86. asciihex_flag = False
  87. force_mailbox = False
  88. ifn = ''
  89. argv = sys.argv[1:]
  90. options, rem = getopt.gnu_getopt(sys.argv[1:], "hFi:", ["asciihex", "help", "version", "show-mailbox", "force-mailbox", "input="])
  91. for opt, arg in options:
  92. if opt in ('-v', '--version'):
  93. show_version(sys.stdout)
  94. sys.exit(0)
  95. if opt in ('-h', '--help'):
  96. show_help(sys.stdout)
  97. sys.exit(0)
  98. elif opt in ('--asciihex'):
  99. asciihex_flag = True
  100. elif opt in ('-F', '--force-mailbox'):
  101. force_mailbox = True
  102. elif opt in ('--show-mailbox'):
  103. show_mailboxes()
  104. sys.exit(0)
  105. elif opt in ('-i', '--input'):
  106. ifn = arg
  107. else:
  108. show_help(sys.sdterr)
  109. sys.exit(-1)
  110. if (len(rem) < 2) and ((len(rem) != 1) or (len(ifn)==0)):
  111. sys.stderr.write("must provide MAILBOX and PAYLOAD\n")
  112. show_help(sys.stderr)
  113. sys.exit(-1)
  114. mailbox = rem[0]
  115. if not force_mailbox:
  116. if not mailbox in VALID_MAILBOX:
  117. sys.stderr.write("ERROR: could not find '" + mailbox + "' in list of valid mailboxes.\n")
  118. sys.stderr.write("Use the --force-mailbox option to use this mailbox anyway.\n")
  119. sys.stderr.write("Use the --show-mailbox option to see a list of vlaid options\n\n")
  120. sys.exit(-1)
  121. if len(ifn)==0:
  122. payload_text = rem[1]
  123. else:
  124. with open(ifn) as fp:
  125. payload_text = fp.read()
  126. if asciihex_flag:
  127. payload_converted = payload_text
  128. else:
  129. for ch in payload_text:
  130. payload_converted += " " + format(ord(ch), '02x').zfill(2)
  131. print mailbox + ":" + payload_converted