WiFi_TX.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. if (!initialised) {
  37. initialised = true;
  38. init();
  39. }
  40. uint8_t buffer[1024] {};
  41. int length;
  42. if ((length = odid_wifi_build_nan_sync_beacon_frame((char *)WiFi_mac_addr,
  43. buffer,sizeof(buffer))) > 0) {
  44. if (esp_wifi_80211_tx(WIFI_IF_AP,buffer,length,true) != ESP_OK) {
  45. return false;
  46. }
  47. }
  48. if ((length = odid_wifi_build_message_pack_nan_action_frame(&UAS_data,(char *)WiFi_mac_addr,
  49. ++send_counter,
  50. buffer,sizeof(buffer))) > 0) {
  51. if (esp_wifi_80211_tx(WIFI_IF_AP,buffer,length,true) != ESP_OK) {
  52. return false;
  53. }
  54. }
  55. return true;
  56. }