Cascoda SDK
Cascoda SDK for building software to run with CA-821x transceivers
ExternalFlasher.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, 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_EXTERNAL_FLASH_EXTERNALFLASHER_HPP_
30 #define POSIX_APP_CHILICTL_EXTERNAL_FLASH_EXTERNALFLASHER_HPP_
31 
32 #include <fstream>
33 #include <mutex>
34 
35 #include "common/DeviceInfo.hpp"
36 
37 namespace ca {
38 
40 {
41 public:
42  enum State
43  {
44  INIT,
49  FAIL,
51  };
52 
58  ExternalFlasher(const char *aAppFilePath, const DeviceInfo &aDeviceInfo);
59 
61 
68  ca_error Process();
69 
74  bool IsComplete();
75 
80  State GetState() { return mState; }
81 
82 private:
83  enum
84  {
85  kMsgSendTimeout = 5,
86  kWriteLen = 244
87  };
88 
89  std::mutex mMutex;
90  std::ifstream mFile;
91  size_t mFileSize;
92  size_t mMaxFileSize;
93  size_t mPageSize;
94  uint32_t mStartAddr;
95  uint32_t mMaxAddress;
96  uint32_t mMetadataStartAddr;
97  uint32_t mCurrentAppStartAddr;
98  uint32_t mBinarySizeMetadataPartitionAddr;
99  bool mBinarySizeSent;
100  uint32_t mBinaryHashMetadataPartitionAddr;
101  bool mBinaryHashSent;
102  ca821x_dev mDeviceRef;
103  DeviceInfo mDeviceInfo;
104  uint32_t mCounter;
105  State mState;
106 
107  void set_state(State aNextState);
108  ca_error init();
109  ca_error erase();
110  ca_error program();
111  ca_error verify();
112 
113  ca_error erase_done(ca_error status);
114  ca_error program_done(ca_error status);
115  ca_error verify_done(ca_error status);
116 
117  ca_error dfu_callback(EVBME_Message *params);
118  static ca_error dfu_callback(EVBME_Message *params, ca821x_dev *pDeviceRef);
119 
120  ca_error handle_evbme_message(EVBME_Message *params);
121  static ca_error handle_evbme_message(EVBME_Message *params, ca821x_dev *pDeviceRef);
122 
123  void configure_max_binsize();
124  size_t get_page_count(size_t len) { return (len + (mPageSize - 1)) / mPageSize; }
125 
126  static const char *state_string(State aState);
127 };
128 
129 } /* namespace ca */
130 
131 #endif /* POSIX_APP_CHILICTL_EXTERNAL_FLASH_EXTERNALFLASHER_HPP_ */
C++ wrapper for the C ca_device_info struct, which owns its own memory.
Definition: DeviceInfo.hpp:40
Definition: ExternalFlasher.hpp:40
State GetState()
Get the current state of the flasher.
Definition: ExternalFlasher.hpp:80
State
Definition: ExternalFlasher.hpp:43
@ ERASE
Erasing flash.
Definition: ExternalFlasher.hpp:45
@ INIT
Initial state.
Definition: ExternalFlasher.hpp:44
@ VERIFY
Definition: ExternalFlasher.hpp:47
@ COMPLETE
Flashing completed successfully.
Definition: ExternalFlasher.hpp:48
@ INVALID
Class not correctly instantiated.
Definition: ExternalFlasher.hpp:50
@ PROGRAM
Definition: ExternalFlasher.hpp:46
@ FAIL
Flashing failed.
Definition: ExternalFlasher.hpp:49
ca_error Process()
Process the internal state, returning an error if no more processing can be done.
Definition: ExternalFlasher.cpp:79
ExternalFlasher(const char *aAppFilePath, const DeviceInfo &aDeviceInfo)
Construct an ExternalFlasher instance.
Definition: ExternalFlasher.cpp:40
~ExternalFlasher()
Definition: ExternalFlasher.cpp:74
bool IsComplete()
Is the instance complete and successful?
Definition: ExternalFlasher.cpp:325
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