WiFi_TX.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. WiFi NAN driver
  3. with thanks to https://github.com/sxjack/uav_electronic_ids for WiFi calls
  4. */
  5. #include "WiFi_TX.h"
  6. #include <esp_wifi.h>
  7. #include <WiFi.h>
  8. #include <esp_system.h>
  9. bool WiFi_NAN::init(void)
  10. {
  11. //use a local MAC address to avoid tracking transponders based on their MAC address
  12. uint8_t mac_addr[6];
  13. generate_random_mac(mac_addr);
  14. mac_addr[0] |= 0x02; // set MAC local bit
  15. mac_addr[0] &= 0xFE; // unset MAC multicast bit
  16. //set MAC address
  17. esp_base_mac_addr_set(mac_addr);
  18. wifi_config_t wifi_config {};
  19. WiFi.softAP("");
  20. esp_wifi_get_config(WIFI_IF_AP, &wifi_config);
  21. wifi_config.ap.ssid_hidden = 1;
  22. wifi_config.ap.channel = wifi_channel;
  23. if (esp_wifi_set_config(WIFI_IF_AP, &wifi_config) != ESP_OK) {
  24. return false;
  25. }
  26. if (esp_wifi_set_bandwidth(WIFI_IF_AP, WIFI_BW_HT20) != ESP_OK) {
  27. return false;
  28. }
  29. if (esp_read_mac(WiFi_mac_addr, ESP_MAC_WIFI_STA) != ESP_OK) {
  30. return false;
  31. }
  32. return true;
  33. }
  34. bool WiFi_NAN::transmit(ODID_UAS_Data &UAS_data)
  35. {
  36. uint8_t buffer[1024] {};
  37. int length;
  38. if ((length = odid_wifi_build_nan_sync_beacon_frame((char *)WiFi_mac_addr,
  39. buffer,sizeof(buffer))) > 0) {
  40. if (esp_wifi_80211_tx(WIFI_IF_AP,buffer,length,true) != ESP_OK) {
  41. return false;
  42. }
  43. }
  44. if ((length = odid_wifi_build_message_pack_nan_action_frame(&UAS_data,(char *)WiFi_mac_addr,
  45. ++send_counter,
  46. buffer,sizeof(buffer))) > 0) {
  47. if (esp_wifi_80211_tx(WIFI_IF_AP,buffer,length,true) != ESP_OK) {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }