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.

134 lines
4.3 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. if value in open("/proc/device-tree/compatible").read():
  54. return True
  55. except FileNotFoundError:
  56. pass
  57. return False
  58. def get_armbian_release_field(self, field):
  59. """
  60. Search /etc/armbian-release, if it exists, for a field and return its
  61. value, if found, otherwise None.
  62. """
  63. field_value = None
  64. pattern = r"^" + field + r"=(.*)"
  65. try:
  66. with open("/etc/armbian-release", "r") as release_file:
  67. armbian = release_file.read().split("\n")
  68. for line in armbian:
  69. match = re.search(pattern, line)
  70. if match:
  71. field_value = match.group(1)
  72. except FileNotFoundError:
  73. pass
  74. return field_value
  75. def get_device_model(self):
  76. """
  77. Search /proc/device-tree/model for the device model and return its value, if found,
  78. otherwise None.
  79. """
  80. model = None
  81. try:
  82. with open("/proc/device-tree/model", "r") as model_file:
  83. model = model_file.read()
  84. except FileNotFoundError:
  85. pass
  86. return model
  87. def get_device_compatible(self):
  88. """
  89. Search /proc/device-tree/compatible for the compatible chip name.
  90. """
  91. model = None
  92. try:
  93. with open("/proc/device-tree/compatible", "r") as model_file:
  94. model = model_file.read()
  95. except FileNotFoundError:
  96. pass
  97. return model
  98. def check_board_asset_tag_value(self):
  99. """
  100. Search /proc/device-tree/model for the device model and return its value, if found,
  101. otherwise None.
  102. """
  103. tag = None
  104. try:
  105. with open("/sys/devices/virtual/dmi/id/board_asset_tag", "r") as tag_file:
  106. tag = tag_file.read().strip()
  107. except FileNotFoundError:
  108. pass
  109. return tag