forked from NordicSemiconductor/pc-nrfutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfu_transport.py
181 lines (148 loc) · 6.94 KB
/
dfu_transport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#
# Copyright (c) 2016 Nordic Semiconductor ASA
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# 3. Neither the name of Nordic Semiconductor ASA nor the names of other
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# 4. This software must only be used in or with a processor manufactured by Nordic
# Semiconductor ASA, or in or with a processor manufactured by a third party that
# is used in combination with a processor manufactured by Nordic Semiconductor.
#
# 5. Any software provided in binary or object form under this license must not be
# reverse engineered, decompiled, modified and/or disassembled.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Python specific imports
import abc
import logging
# Nordic Semiconductor imports
logger = logging.getLogger(__name__)
# Custom logging level for logging all transport events, including all bytes
# being transported over the wire or over the air.
# Note that this logging level is more verbose than logging.DEBUG.
TRANSPORT_LOGGING_LEVEL = 5
class DfuEvent:
PROGRESS_EVENT = 1
class DfuTransport(object):
"""
This class as an abstract base class inherited from when implementing transports.
The class is generic in nature, the underlying implementation may have missing semantic
than this class describes. But the intent is that the implementer shall follow the semantic as
best as she can.
"""
__metaclass__ = abc.ABCMeta
OP_CODE = {
'CreateObject' : 0x01,
'SetPRN' : 0x02,
'CalcChecSum' : 0x03,
'Execute' : 0x04,
'ReadObject' : 0x06,
'Response' : 0x60,
}
RES_CODE = {
'InvalidCode' : 0x00,
'Success' : 0x01,
'NotSupported' : 0x02,
'InvalidParameter' : 0x03,
'InsufficientResources' : 0x04,
'InvalidObject' : 0x05,
'InvalidSignature' : 0x06,
'UnsupportedType' : 0x07,
'OperationNotPermitted' : 0x08,
'OperationFailed' : 0x0A,
'ExtendedError' : 0x0B,
}
EXT_ERROR_CODE = [
"No extended error code has been set. This error indicates an implementation problem.",
"Invalid error code. This error code should never be used outside of development.",
"The format of the command was incorrect. This error code is not used in the current implementation, because @ref NRF_DFU_RES_CODE_OP_CODE_NOT_SUPPORTED and @ref NRF_DFU_RES_CODE_INVALID_PARAMETER cover all possible format errors.",
"The command was successfully parsed, but it is not supported or unknown.",
"The init command is invalid. The init packet either has an invalid update type or it is missing required fields for the update type (for example, the init packet for a SoftDevice update is missing the SoftDevice size field).",
"The firmware version is too low. For an application, the version must be greater than or equal to the current application. For a bootloader, it must be greater than the current version. This requirement prevents downgrade attacks.""",
"The hardware version of the device does not match the required hardware version for the update.",
"The array of supported SoftDevices for the update does not contain the FWID of the current SoftDevice.",
"The init packet does not contain a signature, but this bootloader requires all updates to have one.",
"The hash type that is specified by the init packet is not supported by the DFU bootloader.",
"The hash of the firmware image cannot be calculated.",
"The type of the signature is unknown or not supported by the DFU bootloader.",
"The hash of the received firmware image does not match the hash in the init packet.",
"The available space on the device is insufficient to hold the firmware.",
"The requested firmware to update was already present on the system.",
]
@abc.abstractmethod
def __init__(self):
self.callbacks = {}
@abc.abstractmethod
def open(self):
"""
Open a port if appropriate for the transport.
:return:
"""
pass
@abc.abstractmethod
def close(self):
"""
Close a port if appropriate for the transport.
:return:
"""
pass
@abc.abstractmethod
def send_init_packet(self, init_packet):
"""
Send init_packet to device.
This call will block until init_packet is sent and transfer of packet is complete.
:param init_packet: Init packet as a str.
:return:
"""
pass
@abc.abstractmethod
def send_firmware(self, firmware):
"""
Start sending firmware to device.
This call will block until transfer of firmware is complete.
:param firmware:
:return:
"""
pass
def register_events_callback(self, event_type, callback):
"""
Register a callback.
:param DfuEvent callback:
:return: None
"""
if event_type not in self.callbacks:
self.callbacks[event_type] = []
self.callbacks[event_type].append(callback)
def _send_event(self, event_type, **kwargs):
"""
Method for sending events to registered callbacks.
If callbacks throws exceptions event propagation will stop and this method be part of the track trace.
:param DfuEvent event_type:
:param kwargs: Arguments to callback function
:return:
"""
if event_type in self.callbacks.keys():
for callback in self.callbacks[event_type]:
callback(**kwargs)