fota lib for mdot
Embed:
(wiki syntax)
Show/hide line numbers
LoRaConfig.h
00001 #include "mbed.h" 00002 #if defined (TARGET_MTS_MDOT_F411RE) 00003 #include "spiffs.h" 00004 #include "SpiFlash25.h" 00005 #else 00006 #include "xdot_eeprom.h" 00007 #endif /* TARGET_MTS_MDOT_F411RE */ 00008 #include "mDot.h" 00009 #include "ChannelPlan.h" 00010 #include <stdint.h> 00011 #include <string> 00012 #include <vector> 00013 00014 #ifndef __LORA_CONFIG_H__ 00015 #define __LORA_CONFIG_H__ 00016 00017 #if defined (TARGET_MTS_MDOT_F411RE) 00018 // this value represents the number of files you can have open at the same time 00019 // adjust it according to your requirements 00020 #define MAX_CONCURRENT_FDS 5 00021 00022 #define PAGE_SIZE 256 00023 #define SECTOR_SIZE 64*1024 00024 #define MEM_SIZE 2*1024*1024 00025 #else 00026 #define SETTINGS_ADDR 0x0000 // configuration is 1024 bytes (0x000-0x3FF) 00027 #define PROTECTED_ADDR 0x0400 // protected configuration is 256 bytes (0x400-0x4FF) 00028 #define SESSION_ADDR 0x0500 // session is 256 bytes (0x500-0x5FF) 00029 #define USER_ADDR 0x0800 // user space is 6*1024 bytes (0x800 - 0x1FFF) 00030 #endif /* TARGET_MTS_MDOT_F411RE */ 00031 00032 #define MULTICAST_SESSIONS 3 00033 #define EUI_LENGTH 8 00034 #define KEY_LENGTH 16 00035 #define PASSPHRASE_LENGTH 128 00036 00037 // DON'T CHANGE THIS UNLESS YOU REALLY WANT TO 00038 #define PROTECTED_RFU_SIZE 223 00039 00040 // PROTECTED SETTINGS SHOULD ALWAYS HAVE SIZE OF 256 BYTES 00041 typedef struct { 00042 uint8_t FrequencyBand; 00043 uint8_t DeviceEUI[EUI_LENGTH]; 00044 uint8_t AppEUI[EUI_LENGTH]; 00045 uint8_t AppKey[KEY_LENGTH]; 00046 uint8_t RFU[PROTECTED_RFU_SIZE]; 00047 } ProtectedSettings_t; 00048 00049 // DON'T CHANGE THIS UNLESS YOU REALLY WANT TO 00050 #define CONFIG_RFU_SIZE 361 00051 00052 // SETTINGS SHOULD ALWAYS HAVE SIZE OF 1024 BYTES 00053 typedef struct { 00054 uint8_t ConfigVersion; 00055 00056 uint32_t SerialBaudRate; 00057 uint32_t DebugBaudRate; 00058 uint8_t StartUpMode; 00059 00060 /* Network Settings */ 00061 uint8_t NetworkEUI[EUI_LENGTH]; 00062 uint8_t NetworkEUIPassphrase[PASSPHRASE_LENGTH]; 00063 uint8_t NetworkKey[KEY_LENGTH]; 00064 uint8_t NetworkKeyPassphrase[PASSPHRASE_LENGTH]; 00065 00066 uint32_t NetworkAddress; 00067 uint8_t NetworkSessionKey[KEY_LENGTH]; 00068 uint8_t DataSessionKey[KEY_LENGTH]; 00069 00070 uint8_t JoinMode; 00071 uint8_t JoinRetries; 00072 00073 /* Radio Settings */ 00074 00075 uint8_t ForwardErrorCorrection; 00076 uint8_t ACKAttempts; 00077 bool EnableEncryption; 00078 bool EnableCRC; 00079 bool EnableADR; 00080 bool EnableEcho; 00081 bool EnableVerbose; 00082 00083 uint32_t TxFrequency; 00084 uint8_t TxDataRate; 00085 bool TxInverted; 00086 uint8_t TxPower; 00087 bool TxWait; 00088 uint8_t FrequencySubBand; 00089 00090 uint32_t RxFrequency; 00091 uint8_t RxDataRate; 00092 bool RxInverted; 00093 uint8_t RxOutput; 00094 00095 /* Serial Settings */ 00096 uint32_t WakeInterval; 00097 uint16_t WakeTimeout; 00098 uint32_t WakeDelay; 00099 00100 uint8_t PublicNetwork; 00101 uint8_t LinkCheckCount; 00102 uint8_t LinkCheckThreshold; 00103 00104 uint8_t LogLevel; 00105 uint8_t JoinByteOrder; 00106 00107 uint8_t WakePin; 00108 uint8_t WakeMode; 00109 00110 uint8_t PreserveSessionOverReset; 00111 00112 uint8_t JoinDelay; 00113 uint8_t RxDelay; 00114 uint8_t Port; 00115 00116 uint8_t Class; 00117 int8_t AntennaGain; 00118 00119 bool FlowControl; 00120 uint8_t Repeat; 00121 00122 bool SerialClearOnError; 00123 uint8_t Rx2Datarate; 00124 uint8_t JoinRx1DatarateOffset; //!< Offset for datarate for first window 00125 uint8_t JoinRx2DatarateIndex; //!< Datarate for second window 00126 00127 uint32_t Channels[16]; 00128 uint8_t ChannelRanges[16]; 00129 00130 uint32_t JoinRx2Frequency; //!< Frequency used in second window 00131 00132 uint8_t MaxEIRP; 00133 uint8_t UlDwellTime; 00134 uint8_t DlDwellTime; 00135 00136 uint8_t padding; 00137 00138 uint32_t DlChannels[16]; 00139 00140 uint8_t LastPlan; //!< Channel plan from last saved configuration 00141 00142 int8_t lbtThreshold; //!< Listen before talk threshold 00143 uint16_t lbtTimeUs; //!< Listen before talk time 00144 00145 // Multicast session address and keys 00146 uint32_t MulticastAddress[MULTICAST_SESSIONS]; 00147 uint8_t MulticastNetSessionKey[MULTICAST_SESSIONS][KEY_LENGTH]; 00148 uint8_t MulticastAppSessionKey[MULTICAST_SESSIONS][KEY_LENGTH]; 00149 00150 uint8_t AutoSleep; 00151 00152 uint8_t RFU[CONFIG_RFU_SIZE]; 00153 } LoRaSettings_t; 00154 00155 // DON'T CHANGE THIS UNLESS YOU REALLY WANT TO 00156 #define SESSION_RFU_SIZE 2 00157 00158 // SESSION SETTINGS SHOULD ALWAYS HAVE SIZE OF 256 BYTES 00159 typedef struct { 00160 bool Joined; 00161 uint8_t Rx1DatarateOffset; 00162 uint8_t Rx2Datarate; 00163 uint8_t ChannelMask500k; 00164 00165 uint32_t NetworkAddress; 00166 00167 uint8_t NetworkKey[KEY_LENGTH]; 00168 uint8_t DataKey[KEY_LENGTH]; 00169 00170 uint64_t ChannelMask; 00171 00172 uint32_t Channels[16]; 00173 uint8_t ChannelRanges[16]; 00174 00175 uint32_t UplinkCounter; 00176 00177 uint8_t Rx1Delay; 00178 uint8_t Datarate; 00179 uint8_t TxPower; 00180 uint8_t Repeat; 00181 00182 uint32_t Rx2Frequency; 00183 00184 uint32_t DownlinkCounter; 00185 00186 uint8_t MaxDutyCycle; 00187 uint8_t AdrAckCounter; 00188 uint8_t LinkFailCount; 00189 uint8_t FrequencySubBand; 00190 00191 uint32_t NetworkId; 00192 00193 bool ServerAckRequested; 00194 uint8_t DeviceClass; 00195 00196 uint8_t CommandBufferIndex; 00197 00198 uint8_t CommandBuffer[15]; 00199 00200 uint8_t UlDwellTime; 00201 uint8_t DlDwellTime; 00202 00203 uint32_t DlChannels[16]; 00204 00205 uint8_t MaxEIRP; 00206 00207 // Multicast session counter values 00208 uint32_t MulticastCounters[MULTICAST_SESSIONS]; 00209 00210 uint8_t RFU[SESSION_RFU_SIZE]; 00211 00212 uint8_t LastPlan; //!< Channel plan from last saved session 00213 00214 } NetworkSession_t; 00215 00216 class LoRaConfig { 00217 00218 public: 00219 00220 static const uint8_t EuiLength = EUI_LENGTH; 00221 static const uint8_t KeyLength = KEY_LENGTH; 00222 static const uint8_t PassPhraseLength = PASSPHRASE_LENGTH; 00223 00224 enum JoinMode { 00225 MANUAL, 00226 OTA, 00227 AUTO_OTA, 00228 PEER_TO_PEER 00229 }; 00230 00231 enum Mode { 00232 COMMAND_MODE, 00233 SERIAL_MODE 00234 }; 00235 00236 enum RX_Output { 00237 HEXADECIMAL, 00238 BINARY 00239 }; 00240 00241 enum DataRates { 00242 DR0, 00243 DR1, 00244 DR2, 00245 DR3, 00246 DR4, 00247 DR5, 00248 DR6, 00249 DR7, 00250 DR8, 00251 DR9, 00252 DR10, 00253 DR11, 00254 DR12, 00255 DR13, 00256 DR14, 00257 DR15 00258 }; 00259 00260 enum FrequencySubBands { 00261 FSB_ALL, 00262 FSB_1, 00263 FSB_2, 00264 FSB_3, 00265 FSB_4, 00266 FSB_5, 00267 FSB_6, 00268 FSB_7, 00269 FSB_8 00270 }; 00271 00272 enum JoinByteOrder { 00273 LSB, 00274 MSB 00275 }; 00276 00277 static const uint32_t BaudRates[]; 00278 static const std::string DRS[]; 00279 static const uint32_t DefaultBaudRate; 00280 00281 uint32_t ValidateBaudRate(uint32_t rate); 00282 00283 LoRaConfig(lora::ChannelPlan *plan); 00284 ~LoRaConfig(); 00285 00286 bool Save(); 00287 bool SaveSession(); 00288 bool SaveProtected(); 00289 00290 #if defined (TARGET_MTS_MDOT_F411RE) 00291 void EnablePVD(); 00292 bool PVDO(); 00293 #endif /* TARGET_MTS_MDOT_F411RE */ 00294 00295 void Mount(); 00296 void Load(); 00297 void Default(); 00298 void DefaultSession(); 00299 void DefaultProtected(); 00300 00301 uint8_t PublicNetwork(); 00302 void PublicNetwork(uint8_t val); 00303 00304 uint8_t* DeviceEUI(); 00305 void DeviceEUI(uint8_t* devEUI); 00306 00307 const uint8_t* AppEUI(); 00308 void AppEUI(const uint8_t* appEUI); 00309 00310 const uint8_t* AppKey(); 00311 void AppKey(const uint8_t* appKey); 00312 00313 uint8_t* NetworkEUI(); 00314 void NetworkEUI(const uint8_t* networkEUI); 00315 00316 uint8_t* NetworkEUIPassphrase(); 00317 void NetworkEUIPassphrase(uint8_t* networkEUIPassphrase, uint8_t length); 00318 00319 uint8_t* NetworkKey(); 00320 void NetworkKey(const uint8_t* networkKey); 00321 00322 uint8_t* NetworkKeyPassphrase(); 00323 void NetworkKeyPassphrase(uint8_t* networkKeyPassphrase, uint8_t length); 00324 00325 uint32_t NetworkAddress(); 00326 void NetworkAddress(uint32_t addr); 00327 00328 uint8_t* NetworkSessionKey(); 00329 void NetworkSessionKey(uint8_t* networkSessionKey); 00330 00331 uint8_t* DataSessionKey(); 00332 void DataSessionKey(uint8_t* dataSessionKey); 00333 00334 uint8_t JoinMode(); 00335 void JoinMode(uint8_t mode); 00336 00337 uint8_t JoinRetries(); 00338 void JoinRetries(uint8_t retries); 00339 00340 uint8_t LinkCheckCount(); 00341 void LinkCheckCount(uint8_t count); 00342 00343 uint8_t LinkCheckThreshold(); 00344 void LinkCheckThreshold(uint8_t count); 00345 00346 uint8_t ForwardErrorCorrection(); 00347 void ForwardErrorCorrection(uint8_t bytes); 00348 00349 uint8_t ACKAttempts(); 00350 void ACKAttempts(uint8_t attempts); 00351 00352 bool EnableEncryption(); 00353 void EnableEncryption(uint8_t enable); 00354 00355 bool EnableCRC(); 00356 void EnableCRC(uint8_t enable); 00357 00358 bool EnableADR(); 00359 void EnableADR(uint8_t enable); 00360 00361 bool EnableEcho(); 00362 void EnableEcho(uint8_t enable); 00363 00364 bool EnableVerbose(); 00365 void EnableVerbose(uint8_t enable); 00366 00367 uint8_t TxDataRate(); 00368 void TxDataRate(uint8_t rate); 00369 00370 uint8_t TxPower(); 00371 void TxPower(uint8_t power); 00372 00373 uint8_t FrequencyBand(); 00374 void FrequencyBand(uint8_t band); 00375 00376 uint8_t FrequencySubBand(); 00377 void FrequencySubBand(uint8_t band); 00378 00379 uint32_t TxFrequency(); 00380 void TxFrequency(uint32_t freq); 00381 00382 bool TxInverted(); 00383 void TxInverted(bool enable); 00384 00385 bool TxWait(); 00386 void TxWait(bool timeout); 00387 00388 uint32_t RxFrequency(); 00389 void RxFrequency(uint32_t freq); 00390 00391 bool RxInverted(); 00392 void RxInverted(bool enable); 00393 00394 uint8_t RxOutput(); 00395 void RxOutput(uint8_t type); 00396 00397 uint8_t RxDataRate(); 00398 void RxDataRate(uint8_t rate); 00399 00400 uint8_t Rx2DataRate(); 00401 void Rx2DataRate(uint8_t rate); 00402 00403 uint8_t StartUpMode(); 00404 void StartUpMode(uint8_t mode); 00405 00406 /* Serial Settings */ 00407 uint32_t SerialBaudRate(); 00408 void SerialBaudRate(uint32_t rate); 00409 00410 uint32_t WakeInterval(); 00411 void WakeInterval(uint32_t interval); 00412 00413 uint32_t WakeDelay(); 00414 void WakeDelay(uint32_t delay); 00415 00416 uint16_t WakeTimeout(); 00417 void WakeTimeout(uint16_t timeout); 00418 00419 uint8_t WakePin(); 00420 void WakePin(uint8_t pin); 00421 00422 uint8_t WakeMode(); 00423 void WakeMode(uint8_t mode); 00424 00425 uint32_t DebugBaudRate(); 00426 void DebugBaudRate(uint32_t baud); 00427 00428 bool Joined(); 00429 void Joined(bool val); 00430 00431 uint32_t* Channels(); 00432 00433 uint32_t* DownlinkChannels(); 00434 00435 uint8_t* ChannelRanges(); 00436 00437 uint8_t* SessionDataKey(); 00438 void SessionDataKey(uint8_t* key); 00439 00440 uint8_t* SessionNetworkKey(); 00441 void SessionNetworkKey(uint8_t* key); 00442 00443 uint32_t SessionNetworkAddress(); 00444 void SessionNetworkAddress(uint32_t addr); 00445 00446 uint8_t SessionDatarate(); 00447 void SessionDatarate(uint8_t datarate); 00448 00449 uint8_t SessionTxPower(); 00450 void SessionTxPower(uint8_t power); 00451 00452 uint8_t SessionRepeat(); 00453 void SessionRepeat(uint8_t repeat); 00454 00455 uint8_t SessionRxDelay(); 00456 void SessionRxDelay(uint8_t delay); 00457 00458 uint8_t SessionRx1DatarateOffset(); 00459 void SessionRx1DatarateOffset(uint8_t offset); 00460 00461 uint8_t SessionRx2Datarate(); 00462 void SessionRx2Datarate(uint8_t datarate); 00463 00464 uint32_t SessionRx2Frequency(); 00465 void SessionRx2Frequency(uint32_t frequency); 00466 00467 uint64_t SessionChannelMask(); 00468 void SessionChannelMask(uint64_t mask); 00469 00470 uint8_t Session500kMask(); 00471 void Session500kMask(uint8_t mask); 00472 00473 uint32_t* SessionChannels(); 00474 00475 uint32_t* SessionDownlinkChannels(); 00476 00477 uint8_t* SessionChannelRanges(); 00478 00479 uint32_t SessionUplinkCounter(); 00480 void SessionUplinkCounter(uint32_t count); 00481 00482 uint32_t SessionDownlinkCounter(); 00483 void SessionDownlinkCounter(uint32_t count); 00484 00485 uint8_t SessionAdrAckCounter(); 00486 void SessionAdrAckCounter(uint8_t count); 00487 00488 uint8_t SessionLinkFailCount(); 00489 void SessionLinkFailCount(uint8_t count); 00490 00491 uint8_t SessionFrequencySubBand(); 00492 void SessionFrequencySubBand(uint8_t band); 00493 00494 bool SessionServerAckRequested(); 00495 void SessionServerAckRequested(bool val); 00496 00497 uint32_t SessionNetworkId(); 00498 void SessionNetworkId(uint32_t id); 00499 00500 uint8_t SessionDeviceClass(); 00501 void SessionDeviceClass(uint8_t cls); 00502 00503 uint8_t* SessionCommandBuffer(); 00504 void SessionCommandBuffer(uint8_t* buffer); 00505 00506 uint8_t SessionCommandBufferIndex(); 00507 void SessionCommandBufferIndex(uint8_t index); 00508 00509 uint8_t SessionMaxDutyCycle(); 00510 void SessionMaxDutyCycle(uint8_t duty); 00511 00512 uint8_t SessionUplinkDwelltime(); 00513 void SessionUplinkDwelltime(uint8_t dwell); 00514 00515 uint8_t SessionDownlinkDwelltime(); 00516 void SessionDownlinkDwelltime(uint8_t dwell); 00517 00518 uint8_t SessionMaxEIRP(); 00519 void SessionMaxEIRP(uint8_t pow); 00520 00521 uint8_t LogLevel(); 00522 void LogLevel(uint8_t level); 00523 00524 uint8_t JoinByteOrder(); 00525 void JoinByteOrder(uint8_t order); 00526 00527 uint8_t PreserveSessionOverReset(); 00528 void PreserveSessionOverReset(uint8_t); 00529 00530 uint8_t JoinDelay(); 00531 void JoinDelay(uint8_t delay); 00532 00533 uint8_t JoinRx1DatarateOffset(); 00534 void JoinRx1DatarateOffset(uint8_t offset); 00535 00536 uint8_t JoinRx2DatarateIndex(); 00537 void JoinRx2DatarateIndex(uint8_t datarate); 00538 00539 uint32_t JoinRx2Frequency(); 00540 void JoinRx2Frequency(uint32_t freq); 00541 00542 uint8_t RxDelay(); 00543 void RxDelay(uint8_t delay); 00544 00545 uint8_t Port(); 00546 void Port(uint8_t port); 00547 00548 uint8_t Class(); 00549 void Class(uint8_t cls); 00550 00551 int8_t AntennaGain(); 00552 void AntennaGain(int8_t gain); 00553 00554 bool FlowControl(); 00555 void FlowControl(bool enable); 00556 00557 uint8_t Repeat(); 00558 void Repeat(uint8_t repeat); 00559 00560 bool SerialClearOnError(); 00561 void SerialClearOnError(bool val); 00562 00563 uint8_t LastPlan(); 00564 uint8_t SessionLastPlan(); 00565 00566 void LbtTimeUs(uint16_t us); 00567 uint16_t LbtTimeUs(); 00568 void LbtThreshold(int8_t rssi); 00569 int8_t LbtThreshold(); 00570 00571 uint32_t GetMulticastAddress(int i); 00572 uint8_t* GetMulticastNetSessionKey(int i); 00573 uint8_t* GetMulticastAppSessionKey(int i); 00574 uint32_t GetMulticastCounter(int i); 00575 00576 00577 void SetMulticastAddress(int i, uint32_t addr); 00578 void SetMulticastNetSessionKey(int i, const uint8_t* key); 00579 void SetMulticastAppSessionKey(int i, const uint8_t* key); 00580 void SetMulticastCounter(int i, uint32_t count); 00581 00582 void AutoSleep(uint8_t enable); 00583 uint8_t AutoSleep(); 00584 00585 /*! 00586 * AES encryption/decryption cipher network app ieee eui 00587 */ 00588 static const uint8_t defaultNetworkEUI[EUI_LENGTH]; 00589 /*! 00590 * AES encryption/decryption cipher application key 00591 */ 00592 static const uint8_t defaultNetworkKey[KEY_LENGTH]; 00593 00594 /*! 00595 * AES encryption/decryption cipher network session key 00596 */ 00597 static const uint8_t defaultNetworkSessionKey[KEY_LENGTH]; 00598 00599 /*! 00600 * AES encryption/decryption cipher application session key 00601 */ 00602 static const uint8_t defaultDataSessionKey[KEY_LENGTH]; 00603 00604 void Sleep(); 00605 void Wakeup(); 00606 00607 void SetChannelPlan(lora::ChannelPlan *plan); 00608 00609 #if defined (TARGET_MTS_MDOT_F411RE) 00610 mDot::mdot_file OpenUserFile(const char* file, int mode); 00611 bool SeekUserFile(mDot::mdot_file& file, size_t offset, int whence); 00612 int ReadUserFile(mDot::mdot_file& file, void* data, size_t length); 00613 int WriteUserFile(mDot::mdot_file& file, void* data, size_t length); 00614 bool CloseUserFile(mDot::mdot_file& file); 00615 bool MoveUserFile(mDot::mdot_file& file, const char* new_name); 00616 00617 bool MoveUserFile(const char* file, const char* new_name); 00618 bool SaveUserFile(const char* file, void* data, uint32_t size); 00619 bool AppendUserFile(const char* file, void* data, uint32_t size); 00620 bool ReadUserFile(const char* file, void* data, uint32_t size); 00621 bool DeleteUserFile(const char* file); 00622 bool DeleteFile(const char* file); 00623 00624 bool MoveUserFileToFirwareUpgrade(const char* file); 00625 00626 std::vector<mDot::mdot_file> ListUserFiles(); 00627 #endif /* TARGET_MTS_MDOT_F411RE */ 00628 00629 private: 00630 LoRaSettings_t _settings; 00631 ProtectedSettings_t _protected; 00632 NetworkSession_t _session; 00633 bool _dirty_settings; 00634 00635 uint32_t _frequencyMin; 00636 uint32_t _frequencyMax; 00637 00638 lora::ChannelPlan *_plan; 00639 00640 #if defined (TARGET_MTS_MDOT_F411RE) 00641 // SpiFlash25 flash(MOSI, MISO, SCK, CS, W, HOLD); 00642 static SpiFlash25 _flash; 00643 00644 mDot::mdot_file OpenFile(spiffs *fs, const char* file, int mode); 00645 bool SeekFile(spiffs *fs, mDot::mdot_file& file, size_t offset, int whence); 00646 00647 int ReadFile(spiffs *fs, mDot::mdot_file& file, void* data, size_t length); 00648 int WriteFile(spiffs *fs, mDot::mdot_file& file, void* data, size_t length); 00649 int MoveFile(spiffs* fs, mDot::mdot_file& file, const char* new_name); 00650 bool CloseFile(spiffs *fs, mDot::mdot_file& file); 00651 00652 bool AppendFile(spiffs *fs, const char* file, void* data, uint32_t size); 00653 bool SaveFile(spiffs *fs, const char* file, void* data, uint32_t size); 00654 bool ReadFile(spiffs *fs, const char* file, void* dest, uint32_t size); 00655 bool MoveFile(spiffs *fs, const char* file, const char* new_name); 00656 00657 // glue code between SPI driver and filesystem 00658 static int spi_read(unsigned int addr, unsigned int size, unsigned char* data); 00659 static int spi_write(unsigned int addr, unsigned int size, unsigned char* data); 00660 static int spi_erase(unsigned int addr, unsigned int size); 00661 00662 static u8_t spiffs_work_buf[PAGE_SIZE * 2]; 00663 static u8_t spiffs_fds[32 * MAX_CONCURRENT_FDS]; 00664 static u8_t spiffs_cache_buf[(PAGE_SIZE + 32) * 4]; 00665 00666 u8_t _openFds; 00667 00668 static spiffs _fs; 00669 00670 static char file[]; 00671 static char protected_file[]; 00672 static char session_file[]; 00673 static char user_dir[]; 00674 #endif /* TARGET_MTS_MDOT_F411RE */ 00675 00676 }; 00677 00678 #endif // __LORA_CONFIG_H__
Generated on Tue Jul 12 2022 12:07:34 by 1.7.2