dfu_transport.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # Copyright (c) 2015, Nordic Semiconductor
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. #
  7. # * Redistributions of source code must retain the above copyright notice, this
  8. # list of conditions and the following disclaimer.
  9. #
  10. # * Redistributions in binary form must reproduce the above copyright notice,
  11. # this list of conditions and the following disclaimer in the documentation
  12. # and/or other materials provided with the distribution.
  13. #
  14. # * Neither the name of Nordic Semiconductor ASA nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. # Python specific imports
  29. import abc
  30. import logging
  31. # Nordic Semiconductor imports
  32. from nordicsemi.dfu.util import int32_to_bytes
  33. logger = logging.getLogger(__name__)
  34. class DfuEvent:
  35. PROGRESS_EVENT = 1
  36. TIMEOUT_EVENT = 2
  37. ERROR_EVENT = 3
  38. class DfuTransport(object):
  39. """
  40. This class as an abstract base class inherited from when implementing transports.
  41. The class is generic in nature, the underlying implementation may have missing semantic
  42. than this class describes. But the intent is that the implementer shall follow the semantic as
  43. best as she can.
  44. """
  45. __metaclass__ = abc.ABCMeta
  46. @staticmethod
  47. def create_image_size_packet(softdevice_size=0, bootloader_size=0, app_size=0):
  48. """
  49. Creates an image size packet necessary for sending start dfu.
  50. @param softdevice_size: Size of SoftDevice firmware
  51. @type softdevice_size: int
  52. @param bootloader_size: Size of bootloader firmware
  53. @type softdevice_size: int
  54. @param app_size: Size of application firmware
  55. :return: The image size packet
  56. :rtype: str
  57. """
  58. softdevice_size_packet = int32_to_bytes(softdevice_size)
  59. bootloader_size_packet = int32_to_bytes(bootloader_size)
  60. app_size_packet = int32_to_bytes(app_size)
  61. image_size_packet = softdevice_size_packet + bootloader_size_packet + app_size_packet
  62. return image_size_packet
  63. @abc.abstractmethod
  64. def __init__(self):
  65. self.callbacks = {}
  66. @abc.abstractmethod
  67. def open(self):
  68. """
  69. Open a port if appropriate for the transport.
  70. :return:
  71. """
  72. pass
  73. @abc.abstractmethod
  74. def close(self):
  75. """
  76. Close a port if appropriate for the transport.
  77. :return:
  78. """
  79. pass
  80. @abc.abstractmethod
  81. def is_open(self):
  82. """
  83. Returns if transport is open.
  84. :return bool: True if transport is open, False if not
  85. """
  86. pass
  87. @abc.abstractmethod
  88. def send_start_dfu(self, program_mode, softdevice_size=0, bootloader_size=0, app_size=0):
  89. """
  90. Send packet to initiate DFU communication. Returns when packet is sent or timeout occurs.
  91. This call will block until packet is sent.
  92. If timeout or errors occurs exception is thrown.
  93. :param nordicsemi.dfu.model.HexType program_mode: Type of firmware to upgrade
  94. :param int softdevice_size: Size of softdevice firmware
  95. :param int bootloader_size: Size of bootloader firmware
  96. :param int app_size: Size of application firmware
  97. :return:
  98. """
  99. pass
  100. @abc.abstractmethod
  101. def send_init_packet(self, init_packet):
  102. """
  103. Send init_packet to device.
  104. This call will block until init_packet is sent and transfer of packet is complete.
  105. If timeout or errors occurs exception is thrown.
  106. :param str init_packet: Init packet as a str.
  107. :return:
  108. """
  109. pass
  110. @abc.abstractmethod
  111. def send_firmware(self, firmware):
  112. """
  113. Start sending firmware to device.
  114. This call will block until transfer of firmware is complete.
  115. If timeout or errors occurs exception is thrown.
  116. :param str firmware:
  117. :return:
  118. """
  119. pass
  120. @abc.abstractmethod
  121. def send_validate_firmware(self):
  122. """
  123. Send request to device to verify that firmware has been correctly transferred.
  124. This call will block until validation is sent and validation is complete.
  125. If timeout or errors occurs exception is thrown.
  126. :return bool: True if firmware validated successfully.
  127. """
  128. pass
  129. @abc.abstractmethod
  130. def send_activate_firmware(self):
  131. """
  132. Send command to device to activate new firmware and restart the device.
  133. The device will start up with the new firmware.
  134. Raises an nRFException if anything fails.
  135. :return:
  136. """
  137. pass
  138. def register_events_callback(self, event_type, callback):
  139. """
  140. Register a callback.
  141. :param DfuEvent callback:
  142. :return: None
  143. """
  144. if event_type not in self.callbacks:
  145. self.callbacks[event_type] = []
  146. self.callbacks[event_type].append(callback)
  147. def unregister_events_callback(self, callback):
  148. """
  149. Unregister a callback.
  150. :param callback: # TODO: add documentation for callback
  151. :return: None
  152. """
  153. for event_type in self.callbacks.keys():
  154. if callback in self.callbacks[event_type]:
  155. self.callbacks[event_type].remove(callback)
  156. def _send_event(self, event_type, **kwargs):
  157. """
  158. Method for sending events to registered callbacks.
  159. If callbacks throws exceptions event propagation will stop and this method be part of the track trace.
  160. :param DfuEvent event_type:
  161. :param args: Arguments to callback function
  162. :return:
  163. """
  164. if event_type in self.callbacks.keys():
  165. for callback in self.callbacks[event_type]:
  166. callback(**kwargs)