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.

135 lines
4.4 KiB

5 years ago
  1. # Copyright (c) 2014-2018 Adafruit Industries
  2. # Author: Tony DiCola, Limor Fried, Brennen Bearnes
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. # The above copyright notice and this permission notice shall be included in all
  10. # copies or substantial portions of the Software.
  11. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. # SOFTWARE.
  18. """
  19. Attempt to detect the current platform.
  20. """
  21. import re
  22. from adafruit_platformdetect.board import Board
  23. from adafruit_platformdetect.chip import Chip
  24. # Various methods here may retain state in future, so tell pylint not to worry
  25. # that they don't use self right now:
  26. # pylint: disable=no-self-use
  27. class Detector:
  28. """Wrap various platform detection functions."""
  29. def __init__(self):
  30. self.board = Board(self)
  31. self.chip = Chip(self)
  32. def get_cpuinfo_field(self, field):
  33. """
  34. Search /proc/cpuinfo for a field and return its value, if found,
  35. otherwise None.
  36. """
  37. # Match a line like 'Hardware : BCM2709':
  38. pattern = r"^" + field + r"\s+:\s+(.*)$"
  39. with open("/proc/cpuinfo", "r") as infile:
  40. cpuinfo = infile.read().split("\n")
  41. for line in cpuinfo:
  42. match = re.search(pattern, line, flags=re.IGNORECASE)
  43. if match:
  44. return match.group(1)
  45. return None
  46. def check_dt_compatible_value(self, value):
  47. """
  48. Search /proc/device-tree/compatible for a value and return True, if found,
  49. otherwise False.
  50. """
  51. # Match a value like 'qcom,apq8016-sbc':
  52. try:
  53. with open("/proc/device-tree/compatible") as compatible:
  54. if value in compatible.read():
  55. return True
  56. except FileNotFoundError:
  57. pass
  58. return False
  59. def get_armbian_release_field(self, field):
  60. """
  61. Search /etc/armbian-release, if it exists, for a field and return its
  62. value, if found, otherwise None.
  63. """
  64. field_value = None
  65. pattern = r"^" + field + r"=(.*)"
  66. try:
  67. with open("/etc/armbian-release", "r") as release_file:
  68. armbian = release_file.read().split("\n")
  69. for line in armbian:
  70. match = re.search(pattern, line)
  71. if match:
  72. field_value = match.group(1)
  73. except FileNotFoundError:
  74. pass
  75. return field_value
  76. def get_device_model(self):
  77. """
  78. Search /proc/device-tree/model for the device model and return its value, if found,
  79. otherwise None.
  80. """
  81. model = None
  82. try:
  83. with open("/proc/device-tree/model", "r") as model_file:
  84. model = model_file.read()
  85. except FileNotFoundError:
  86. pass
  87. return model
  88. def get_device_compatible(self):
  89. """
  90. Search /proc/device-tree/compatible for the compatible chip name.
  91. """
  92. model = None
  93. try:
  94. with open("/proc/device-tree/compatible", "r") as model_file:
  95. model = model_file.read()
  96. except FileNotFoundError:
  97. pass
  98. return model
  99. def check_board_asset_tag_value(self):
  100. """
  101. Search /proc/device-tree/model for the device model and return its value, if found,
  102. otherwise None.
  103. """
  104. tag = None
  105. try:
  106. with open("/sys/devices/virtual/dmi/id/board_asset_tag", "r") as tag_file:
  107. tag = tag_file.read().strip()
  108. except FileNotFoundError:
  109. pass
  110. return tag