telink.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # Copyright (c) 2022 Project CHIP Authors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. import os
  16. import shlex
  17. from enum import Enum, auto
  18. from .builder import Builder
  19. class TelinkApp(Enum):
  20. ALL_CLUSTERS = auto()
  21. ALL_CLUSTERS_MINIMAL = auto()
  22. BRIDGE = auto()
  23. CONTACT_SENSOR = auto()
  24. LIGHT = auto()
  25. SWITCH = auto()
  26. LOCK = auto()
  27. OTA_REQUESTOR = auto()
  28. PUMP = auto()
  29. PUMP_CONTROLLER = auto()
  30. RESOURCE_MONITORING = auto()
  31. SHELL = auto()
  32. SMOKE_CO_ALARM = auto()
  33. TEMPERATURE_MEASUREMENT = auto()
  34. THERMOSTAT = auto()
  35. WINDOW_COVERING = auto()
  36. def ExampleName(self):
  37. if self == TelinkApp.ALL_CLUSTERS:
  38. return 'all-clusters-app'
  39. elif self == TelinkApp.ALL_CLUSTERS_MINIMAL:
  40. return 'all-clusters-minimal-app'
  41. elif self == TelinkApp.BRIDGE:
  42. return 'bridge-app'
  43. elif self == TelinkApp.CONTACT_SENSOR:
  44. return 'contact-sensor-app'
  45. elif self == TelinkApp.LIGHT:
  46. return 'lighting-app'
  47. elif self == TelinkApp.SWITCH:
  48. return 'light-switch-app'
  49. elif self == TelinkApp.LOCK:
  50. return 'lock-app'
  51. elif self == TelinkApp.OTA_REQUESTOR:
  52. return 'ota-requestor-app'
  53. elif self == TelinkApp.PUMP:
  54. return 'pump-app'
  55. elif self == TelinkApp.PUMP_CONTROLLER:
  56. return 'pump-controller-app'
  57. elif self == TelinkApp.RESOURCE_MONITORING:
  58. return 'resource-monitoring-app'
  59. elif self == TelinkApp.SHELL:
  60. return 'shell'
  61. elif self == TelinkApp.SMOKE_CO_ALARM:
  62. return 'smoke-co-alarm-app'
  63. elif self == TelinkApp.TEMPERATURE_MEASUREMENT:
  64. return 'temperature-measurement-app'
  65. elif self == TelinkApp.THERMOSTAT:
  66. return 'thermostat'
  67. elif self == TelinkApp.WINDOW_COVERING:
  68. return 'window-app'
  69. else:
  70. raise Exception('Unknown app type: %r' % self)
  71. def AppNamePrefix(self):
  72. if self == TelinkApp.ALL_CLUSTERS:
  73. return 'chip-telink-all-clusters-example'
  74. elif self == TelinkApp.ALL_CLUSTERS_MINIMAL:
  75. return 'chip-telink-all-clusters-minimal-example'
  76. elif self == TelinkApp.BRIDGE:
  77. return 'chip-telink-bridge-example'
  78. elif self == TelinkApp.CONTACT_SENSOR:
  79. return 'chip-telink-contact-sensor-example'
  80. elif self == TelinkApp.LIGHT:
  81. return 'chip-telink-lighting-example'
  82. elif self == TelinkApp.SWITCH:
  83. return 'chip-telink-light-switch-example'
  84. elif self == TelinkApp.LOCK:
  85. return 'chip-telink-lock-example'
  86. elif self == TelinkApp.OTA_REQUESTOR:
  87. return 'chip-telink-ota-requestor-example'
  88. elif self == TelinkApp.PUMP:
  89. return 'chip-telink-pump-example'
  90. elif self == TelinkApp.PUMP_CONTROLLER:
  91. return 'chip-telink-pump-controller-example'
  92. elif self == TelinkApp.RESOURCE_MONITORING:
  93. return 'chip-telink-resource-monitoring-example'
  94. elif self == TelinkApp.SHELL:
  95. return 'chip-telink-shell-example'
  96. elif self == TelinkApp.SMOKE_CO_ALARM:
  97. return 'chip-telink-smoke-co-alarm-example'
  98. elif self == TelinkApp.TEMPERATURE_MEASUREMENT:
  99. return 'chip-telink-temperature-measurement-example'
  100. elif self == TelinkApp.THERMOSTAT:
  101. return 'chip-telink-thermostat-example'
  102. elif self == TelinkApp.WINDOW_COVERING:
  103. return 'chip-telink-window-example'
  104. else:
  105. raise Exception('Unknown app type: %r' % self)
  106. class TelinkBoard(Enum):
  107. TLSR9518ADK80D = auto()
  108. TLSR9528A = auto()
  109. def GnArgName(self):
  110. if self == TelinkBoard.TLSR9518ADK80D:
  111. return 'tlsr9518adk80d'
  112. elif self == TelinkBoard.TLSR9528A:
  113. return 'tlsr9528a'
  114. else:
  115. raise Exception('Unknown board type: %r' % self)
  116. class TelinkBuilder(Builder):
  117. def __init__(self,
  118. root,
  119. runner,
  120. app: TelinkApp = TelinkApp,
  121. board: TelinkBoard = TelinkBoard,
  122. enable_shell: bool = False,
  123. enable_rpcs: bool = False,
  124. enable_factory_data: bool = False):
  125. super(TelinkBuilder, self).__init__(root, runner)
  126. self.app = app
  127. self.board = board
  128. self.enable_shell = enable_shell
  129. self.enable_rpcs = enable_rpcs
  130. self.enable_factory_data = enable_factory_data
  131. def get_cmd_prefixes(self):
  132. if not self._runner.dry_run:
  133. # Zephyr base
  134. if 'TELINK_ZEPHYR_BASE' not in os.environ:
  135. raise Exception("Telink builds require TELINK_ZEPHYR_BASE")
  136. cmd = 'export ZEPHYR_TOOLCHAIN_VARIANT=zephyr\n'
  137. cmd += 'export ZEPHYR_BASE="$TELINK_ZEPHYR_BASE"\n'
  138. if 'TELINK_ZEPHYR_SDK_DIR' in os.environ:
  139. cmd += 'export ZEPHYR_SDK_INSTALL_DIR="$TELINK_ZEPHYR_SDK_DIR"\n'
  140. return cmd
  141. def generate(self):
  142. if os.path.exists(self.output_dir):
  143. return
  144. flags = []
  145. if self.enable_shell:
  146. flags.append("-DOVERLAY_CONFIG=shell.overlay")
  147. if self.enable_rpcs:
  148. flags.append("-DOVERLAY_CONFIG=rpc.overlay")
  149. if self.enable_factory_data:
  150. flags.append("-DOVERLAY_CONFIG=factory_data.overlay")
  151. if self.options.pregen_dir:
  152. flags.append(f"-DCHIP_CODEGEN_PREGEN_DIR={shlex.quote(self.options.pregen_dir)}")
  153. build_flags = " -- " + " ".join(flags) if len(flags) > 0 else ""
  154. cmd = self.get_cmd_prefixes()
  155. cmd += '''
  156. source "$ZEPHYR_BASE/zephyr-env.sh";
  157. west build --cmake-only -d {outdir} -b {board} {sourcedir}{build_flags}
  158. '''.format(
  159. outdir=shlex.quote(self.output_dir),
  160. board=self.board.GnArgName(),
  161. sourcedir=shlex.quote(os.path.join(self.root, 'examples', self.app.ExampleName(), 'telink')),
  162. build_flags=build_flags).strip()
  163. self._Execute(['bash', '-c', cmd],
  164. title='Generating ' + self.identifier)
  165. def _build(self):
  166. logging.info('Compiling Telink at %s', self.output_dir)
  167. cmd = self.get_cmd_prefixes() + ("ninja -C %s" % self.output_dir)
  168. self._Execute(['bash', '-c', cmd], title='Building ' + self.identifier)
  169. def build_outputs(self):
  170. return {
  171. '%s.elf' %
  172. self.app.AppNamePrefix(): os.path.join(
  173. self.output_dir,
  174. 'zephyr',
  175. 'zephyr.elf'),
  176. '%s.map' %
  177. self.app.AppNamePrefix(): os.path.join(
  178. self.output_dir,
  179. 'zephyr',
  180. 'zephyr.map'),
  181. }