WifiList.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. *
  3. * Sebastien L. 2023, sle118@hotmail.com
  4. * Philippe G. 2023, philippe_44@outlook.com
  5. *
  6. * This software is released under the MIT License.
  7. * https://opensource.org/licenses/MIT
  8. *
  9. * License Overview:
  10. * ----------------
  11. * The MIT License is a permissive open source license. As a user of this software, you are free to:
  12. * - Use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of this software.
  13. * - Use the software for private, commercial, or any other purposes.
  14. *
  15. * Conditions:
  16. * - You must include the above copyright notice and this permission notice in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * The MIT License offers a high degree of freedom and is well-suited for both open source and
  20. * commercial applications. It places minimal restrictions on how the software can be used,
  21. * modified, and redistributed. For more details on the MIT License, please refer to the link above.
  22. */
  23. #pragma once
  24. #include "Network.pb.h"
  25. #include "Status.pb.h"
  26. #include "esp_log.h"
  27. #include "esp_wifi.h"
  28. #include "PBW.h"
  29. #include "pb_decode.h"
  30. #include "pb_encode.h"
  31. #ifdef __cplusplus
  32. #include "Locking.h"
  33. #include <cstring>
  34. #include <list>
  35. #include <map>
  36. #include <string>
  37. class WifiList : public System::Locking {
  38. public:
  39. WifiList(std::string name) : System::Locking(name), name_(name) {}
  40. ~WifiList(){
  41. Clear();
  42. }
  43. static std::string toString(const uint8_t* data, size_t max_length) {
  44. // Find the actual length of the string up to max_length
  45. size_t length = strnlen(reinterpret_cast<const char*>(data), max_length);
  46. // Construct a std::string using the data and its length
  47. auto p = std::string(reinterpret_cast<const char*>(data), length);
  48. return p;
  49. }
  50. static void Release(sys_net_wifi_entry& entry){
  51. Release(&entry);
  52. }
  53. static void Release(sys_net_wifi_entry* entry){
  54. pb_release(&sys_net_wifi_entry_msg,entry);
  55. }
  56. static std::list<sys_net_radio_types> GetRadioTypes(const wifi_ap_record_t* sta);
  57. static wifi_auth_mode_t GetESPAuthMode(sys_net_auth_types auth_type);
  58. static sys_net_auth_types GetAuthType(const wifi_ap_record_t* ap);
  59. static sys_net_auth_types GetAuthType(const wifi_auth_mode_t mode);
  60. static bool areRadioTypesDifferent(const sys_net_radio_types* types1, pb_size_t count1, const sys_net_radio_types* types2, pb_size_t count2) {
  61. if (count1 != count2) {
  62. return true;
  63. }
  64. for (pb_size_t i = 0; i < count1; ++i) {
  65. if (types1[i] != types2[i]) {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. static std::string GetPassword(const wifi_sta_config_t* config) {
  72. auto p = toString(config->password, sizeof(config->password));
  73. return p;
  74. }
  75. static std::string GetSSID(const wifi_event_sta_connected_t* evt) { return toString(evt->ssid, sizeof(evt->ssid)); }
  76. static std::string GetSSID(const wifi_sta_config_t* config) { return toString(config->ssid, sizeof(config->ssid)); }
  77. static std::string GetSSID(const wifi_ap_record_t* ap) { return toString(ap->ssid, sizeof(ap->ssid)); }
  78. static void FormatBSSID(char* buffer, size_t len, const uint8_t* bssid) {
  79. memset(buffer,0x00,len);
  80. snprintf(buffer, len, "%02X:%02X:%02X:%02X:%02X:%02X", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
  81. }
  82. static void FormatBSSID(const wifi_ap_record_t* ap, sys_net_wifi_entry& entry) {
  83. memset(entry.bssid,0x00,sizeof(entry.bssid));
  84. if (!ap) return;
  85. FormatBSSID(entry.bssid, sizeof(entry.bssid), ap->bssid);
  86. }
  87. static std::string GetBSSID(const wifi_event_sta_connected_t* evt);
  88. static bool isEmpty(const char* str, size_t len);
  89. static void PrintString(const char* pData, size_t length, const char* format);
  90. static void PrintWifiSTAEntryTitle();
  91. sys_net_wifi_entry& GetStrongestSTA();
  92. bool RemoveCredential(const std::string& ssid);
  93. bool RemoveCredential(const wifi_sta_config_t* sta);
  94. void Clear();
  95. static sys_net_wifi_entry ToSTAEntry(const sys_net_wifi_entry* sta);
  96. static sys_net_wifi_entry ToSTAEntry(const wifi_sta_config_t* sta, const std::list<sys_net_radio_types>& radio_types,
  97. sys_net_auth_types auth_type = sys_net_auth_types_AUTH_UNKNOWN);
  98. static sys_net_wifi_entry ToSTAEntry(const wifi_sta_config_t* sta, sys_net_auth_types auth_type = sys_net_auth_types_AUTH_UNKNOWN);
  99. static sys_net_wifi_entry& ToSTAEntry(const wifi_ap_record_t* ap, sys_net_wifi_entry& item);
  100. static sys_net_wifi_entry ToSTAEntry(const wifi_ap_record_t* ap);
  101. static sys_net_wifi_entry& ToSTAEntry(const wifi_sta_config_t* sta, sys_net_wifi_entry& item, const std::list<sys_net_radio_types>& radio_types,
  102. sys_net_auth_types auth_type = sys_net_auth_types_AUTH_UNKNOWN);
  103. static void PrintTimeStamp(const google_protobuf_Timestamp* timestamp);
  104. static void PrintWifiSTAEntry(const sys_net_wifi_entry& entry);
  105. static std::string formatRadioTypes(const sys_net_radio_types* radioTypes, pb_size_t count);
  106. static bool OffsetTimeStamp(google_protobuf_Timestamp * ts);
  107. static bool UpdateTimeStamp(google_protobuf_Timestamp* ts, bool& has_flag_val);
  108. bool ResetRSSI();
  109. bool ResetConnected();
  110. const sys_net_wifi_entry* GetConnected();
  111. bool SetConnected(const wifi_event_sta_connected_t* evt, bool connected = true);
  112. size_t GetCount() const { return credentials_.size(); }
  113. sys_net_wifi_entry* GetIndex(size_t index);
  114. bool Exists(const std::string& ssid) { return Get(ssid) != nullptr; }
  115. bool Exists(const char* ssid) { return Get(ssid) != nullptr; }
  116. bool Exists(const wifi_ap_record_t* ap) { return ap != nullptr && Get(GetSSID(ap)) != nullptr; }
  117. bool Exists(const wifi_sta_config_t* sta) { return Exists(GetSSID(sta)); }
  118. bool Update(const wifi_sta_config_t* sta, bool connected = false);
  119. bool Update(const wifi_ap_record_t* ap, bool connected = false);
  120. static bool Update(sys_net_wifi_entry& existingEntry, sys_net_wifi_entry& compared);
  121. sys_net_wifi_entry* Get(const wifi_sta_config_t* sta) { return Get(GetSSID(sta)); }
  122. sys_net_wifi_entry* Get(const wifi_ap_record_t* ap) { return Get(GetSSID(ap)); }
  123. sys_net_wifi_entry* Get(const char* ssid) { return Get(std::string(ssid)); }
  124. sys_net_wifi_entry* Get(const std::string& ssid);
  125. bool UpdateFromClock();
  126. bool UpdateLastTry(const wifi_sta_config_t* sta);
  127. bool UpdateLastTry(const wifi_ap_record_t* ap);
  128. bool UpdateLastTry(const std::string ssid);
  129. bool UpdateLastTry(sys_net_wifi_entry* entry);
  130. bool UpdateLastSeen(const wifi_sta_config_t* sta);
  131. bool UpdateLastSeen(const wifi_ap_record_t* ap);
  132. bool UpdateLastSeen(const std::string ssid);
  133. bool UpdateLastSeen(sys_net_wifi_entry* entry);
  134. sys_net_wifi_entry& AddUpdate(const wifi_sta_config_t* sta, sys_net_auth_types auth_type = sys_net_auth_types_AUTH_UNKNOWN);
  135. sys_net_wifi_entry& AddUpdate(const wifi_sta_config_t* sta, const std::list<sys_net_radio_types>& radio_types,
  136. sys_net_auth_types auth_type = sys_net_auth_types_AUTH_UNKNOWN);
  137. sys_net_wifi_entry& AddUpdate(const wifi_ap_record_t* scan_rec);
  138. sys_net_wifi_entry& AddUpdate(const char* ssid = "", const char* password = "");
  139. sys_net_wifi_entry& AddUpdate(const sys_net_wifi_entry* existing, const char* password = "");
  140. // this one below is used by pb_decode
  141. void AddUpdate(const sys_net_wifi_entry& entry) {
  142. if (!Lock()) {
  143. throw std::runtime_error("Lock failed");
  144. }
  145. credentials_[entry.ssid] = entry;
  146. Unlock();
  147. }
  148. using Iterator = std::map<std::string, sys_net_wifi_entry>::iterator;
  149. using ConstIterator = std::map<std::string, sys_net_wifi_entry>::const_iterator;
  150. Iterator begin() { return credentials_.begin(); }
  151. ConstIterator begin() const { return credentials_.begin(); }
  152. Iterator end() { return credentials_.end(); }
  153. ConstIterator end() const { return credentials_.end(); }
  154. private:
  155. static std::string FormatTimestamp(const google_protobuf_Timestamp& timestamp) {
  156. // Format the timestamp as needed
  157. // This is a placeholder implementation.
  158. return std::to_string(timestamp.seconds) + "s";
  159. }
  160. std::map<std::string, sys_net_wifi_entry> credentials_;
  161. std::string name_; // Name of the WifiCredentialsManager
  162. };
  163. extern "C" {
  164. #endif
  165. typedef struct WifiList WifiList;
  166. #ifdef __cplusplus
  167. }
  168. #endif