Cascoda SDK
Cascoda SDK for building software to run with CA-821x transceivers
Flasher.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Cascoda Ltd.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. Neither the name of the copyright holder nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef POSIX_APP_CHILICTL_FLASH_FLASHER_HPP_
30 #define POSIX_APP_CHILICTL_FLASH_FLASHER_HPP_
31 
32 #include <fstream>
33 #include <mutex>
34 
35 #include "common/DeviceInfo.hpp"
36 
37 namespace ca {
38 
39 class Flasher
40 {
41 public:
45  enum FlashType
46  {
50  DFU = 3,
52  };
53 
79  enum State
80  {
81  INIT,
89  FAIL,
91  };
92 
101  Flasher(const char *aAppFilePath,
102  const char *aOtaBootFilePath,
103  const char *aManufacturerDataFilePath,
104  const DeviceInfo &aDeviceInfo,
105  FlashType aFlashType);
106 
107  ~Flasher();
108 
115  ca_error Process();
116 
121  bool IsComplete();
122 
127  State GetState() { return mState; }
128 
137  void SetIgnoreVersion(bool aIgnoreVersion) { mIgnoreVersion = aIgnoreVersion; }
138 
139  void SetEnumerateUartDevices(bool aEnumerateUartDevices) { mEnumerateUartDevices = aEnumerateUartDevices; }
140 
141 private:
142  enum
143  {
144  kMaxRebootDiscoverAttempts = 10,
145  kMsgSendTimeout = 5,
146  kWriteLen = 244,
147  };
148 
149  std::mutex mMutex;
150  std::ifstream mAppFile;
151  std::ifstream mOtaBootFile;
152  std::ifstream mManuDataFile;
153  size_t mAppFileSize;
154  size_t mOtaBootFileSize;
155  size_t mManuDataFileSize;
156  size_t mAppMaxFileSize;
157  size_t mOtaBootMaxFileSize;
158  size_t mManuDataMaxFileSize;
159  uint32_t mAppStartAddr;
160  uint32_t mOtaBootStartAddr;
161  uint32_t mManuDataStartAddr;
162  size_t mCombinedFileSize;
163  size_t mPageSize;
164  ca821x_dev mDeviceRef;
165  DeviceInfo mDeviceInfo;
166  uint32_t mCounter;
167  State mState;
168  FlashType mFlashType;
169  bool mIgnoreVersion;
170  bool mEnumerateUartDevices;
171  bool mOtaBootFilePresent;
172 
173  void set_state(State aNextState);
174  ca_error init();
175  ca_error ota_erase();
176  ca_error reboot();
177  ca_error erase();
178  ca_error flash();
179  ca_error verify();
180  ca_error validate();
181 
182  ca_error ota_erase_done(ca_error status);
183  ca_error reboot_done(ca_error status);
184  ca_error erase_done(ca_error status);
185  ca_error flash_done(ca_error status);
186  ca_error verify_done(ca_error status);
187  ca_error validate_done(ca_error status);
188 
189  ca_error dfu_callback(EVBME_Message *params);
190  static ca_error dfu_callback(EVBME_Message *params, ca821x_dev *pDeviceRef);
191 
192  ca_error handle_evbme_message(EVBME_Message *params);
193  static ca_error handle_evbme_message(EVBME_Message *params, ca821x_dev *pDeviceRef);
194 
195  ca_error send_reboot_request();
196  void configure_size_and_addresses();
197  size_t get_page_count_for_erase();
198  uint32_t get_start_address();
199 
200  static const char *state_string(State aState);
201 };
202 
203 } /* namespace ca */
204 
205 #endif /* POSIX_APP_CHILICTL_FLASH_FLASHER_HPP_ */
C++ wrapper for the C ca_device_info struct, which owns its own memory.
Definition: DeviceInfo.hpp:40
Definition: Flasher.hpp:40
void SetIgnoreVersion(bool aIgnoreVersion)
Enable/Disable the version check for the connected device.
Definition: Flasher.hpp:137
void SetEnumerateUartDevices(bool aEnumerateUartDevices)
Definition: Flasher.hpp:139
Flasher(const char *aAppFilePath, const char *aOtaBootFilePath, const char *aManufacturerDataFilePath, const DeviceInfo &aDeviceInfo, FlashType aFlashType)
Construct a flasher instance.
Definition: Flasher.cpp:50
~Flasher()
Definition: Flasher.cpp:129
State
State enumeration for flasher state machine.
Definition: Flasher.hpp:80
@ ERASE
Erasing flash.
Definition: Flasher.hpp:84
@ COMPLETE
Flashing completed successfully.
Definition: Flasher.hpp:88
@ INIT
Initial state.
Definition: Flasher.hpp:81
@ FLASH
Flashing program.
Definition: Flasher.hpp:85
@ INVALID
Class not correctly instantiated.
Definition: Flasher.hpp:90
@ OTA_ERASE
Erase the metadata region of the external flash (only happens if OTA upgrade is enabled)
Definition: Flasher.hpp:82
@ REBOOT
Rebooted into DFU mode.
Definition: Flasher.hpp:83
@ FAIL
Flashing failed.
Definition: Flasher.hpp:89
@ VERIFY
Verifying correct flashing.
Definition: Flasher.hpp:86
@ VALIDATE
Validating new program functionality.
Definition: Flasher.hpp:87
bool IsComplete()
Is the instance complete and successful?
Definition: Flasher.cpp:742
ca_error Process()
Process the internal state, returning an error if no more processing can be done.
Definition: Flasher.cpp:134
FlashType
Enumeration of different areas of flash to rewrite.
Definition: Flasher.hpp:46
@ APROM_CLEAR_AND_PROGRAM
Rewrite the APROM and delete persistent APROM data.
Definition: Flasher.hpp:49
@ APROM_PROGRAM
Rewrite the APROM while preserving persistent APROM data.
Definition: Flasher.hpp:48
@ DFU
Rewrite the DFU area (boot loader)
Definition: Flasher.hpp:50
@ APROM_CLEAR
Clear the APROM (application)
Definition: Flasher.hpp:47
@ MANUFACTURER
Rewrite the manufacturer reserved page.
Definition: Flasher.hpp:51
State GetState()
Get the current state of the flasher.
Definition: Flasher.hpp:127
ca_error
Cascoda error type.
Definition: ca821x_error.h:51
Definition: Args.cpp:34
EVBME Message command in Cascoda TLV format.
Definition: evbme_messages.h:258
CA-821x Device reference struct.
Definition: ca821x_api.h:123