Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b2225f2
initiating ftm implementation
cbicari Feb 5, 2026
a5e158e
added all FTM from past initiative and tested- compiles - should try …
cbicari Feb 5, 2026
843d730
made initateFTM() an API public method call to let the user decide if…
cbicari Feb 6, 2026
84b203d
details on c_str() of logging
cbicari Feb 6, 2026
c329476
stash pop after fetch pull might have some bizarre conflicts- will ha…
cbicari Feb 6, 2026
6528eab
Merge remote-tracking branch 'origin/main' into 98-implement-ftm
cbicari Feb 6, 2026
33f60a6
all compiles in verbose mode, FTM session initiated properly
cbicari Feb 6, 2026
348529c
ftm is giving back positive communication but we need to clarify cali…
cbicari Feb 6, 2026
21c5ae4
cleaning up and adding to API for better control in template. current…
cbicari Feb 9, 2026
c67c360
fixed memory leak by unregistering one of two WIFI_EVENT handlers. co…
cbicari Feb 9, 2026
2e08b78
refactored all FTM variables back into FTM instead of leaving them in…
cbicari Feb 10, 2026
ddd447b
cleaning up some dev debugging lines and adapting var names to signif…
cbicari Feb 10, 2026
78b5978
added offset control to responder in API for template calibration pur…
cbicari Feb 11, 2026
dbbbcfc
added last FTM function from ESP32 SDK as API - an end_ftm_request_se…
cbicari Feb 11, 2026
a745d64
added rssi api for ftm measure
cbicari Feb 16, 2026
24f48da
solved some var declarartion
cbicari Feb 16, 2026
e8c5495
ftm updates on api method
cbicari Feb 16, 2026
fe1cf9a
modified bandwidth to 20MHz by default
cbicari Feb 16, 2026
6c0894b
reordered some slight details for clarity, no new additions
cbicari Feb 18, 2026
78ed0e7
working on documenting FTM responders during the wifi scan routine at…
cbicari Feb 18, 2026
8a35d3e
review wifi_scan() as it seems broken
cbicari Feb 18, 2026
31f2d48
looking for reason wifi scan always returns 0 APs
cbicari Feb 18, 2026
7ddb24c
WIP: making modifications to make the ftm connectionless
cbicari Feb 18, 2026
1adae39
WIP : first beacon based FTM query succes :)
cbicari Feb 18, 2026
f9fe5c4
WIP : dynamic wifi_ftm_cfg_t allocation
cbicari Feb 18, 2026
0ff455c
modding API and FTM functions to get beacon-based implementation. cur…
cbicari Feb 20, 2026
20b2045
WIP : packaging all in one ftm_init while running tests with prior ap…
cbicari Feb 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/puara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ Edu Meneses (2022) - https://www.edumeneses.com
#include "puara_device.hpp"
#include "puara_logger.hpp"
#include "puara_filesystem.hpp"
#include "puara_ftm.hpp"
#include "puara_mdns.hpp"
#include "puara_serial.hpp"
#include "puara_web.hpp"
#include "puara_wifi.hpp"
#include <memory>

#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

Expand All @@ -39,8 +41,11 @@ struct PuaraGlobal
PuaraAPI::WiFi wifi{config};
PuaraAPI::Webserver webserver{config, device, fs, settings, wifi};
PuaraAPI::MDNSService mdns;
PuaraAPI::FTM ftm;

PuaraGlobal() { }
PuaraGlobal() {
wifi.ftm = &ftm;
}

void start(PuaraAPI::Monitors monitor, esp_log_level_t debug_level)
{
Expand Down Expand Up @@ -178,3 +183,32 @@ bool Puara::get_StaIsConnected()
{
return g_puara.wifi.get_StaIsConnected();
}

// FTM related functions
int Puara::get_num_responder_aps() {
return g_puara.ftm.scanned_responder_aps.size();
}
bool Puara::ftm_report_available()
{
return g_puara.ftm.ftm_report_available; // flag to indicate if FTM report data is available
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need that comment?

}
const std::map<std::string, wifi_ftm_initiator_cfg_t> Puara::get_map_of_responder_configs(
uint8_t frame_count, uint16_t burst_period, const std::vector<std::string>& returned_ssids)
{
return g_puara.ftm.get_map_of_responder_configs(frame_count, burst_period, returned_ssids);
}

const PuaraAPI::FTM::ftm_report_info& Puara::get_last_ftm_report()
{
return g_puara.ftm.get_last_ftm_report();
}

bool Puara::get_ftm_report(PuaraAPI::FTM::ftm_report_info &out)
{
return g_puara.ftm.get_ftm_report(out);
}

void Puara::set_ftm_report_as_consumed()
{
g_puara.ftm.ftm_report_available = false;
}
21 changes: 18 additions & 3 deletions src/puara.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#ifndef PUARA_H
#define PUARA_H


#include "puara_config.hpp"
#include "puara_ftm.hpp"

#include <string>
#include <string_view>
Expand Down Expand Up @@ -46,11 +48,24 @@ class Puara
void wifi_scan();
bool get_StaIsConnected();

/**
* returns current IP address on external wifi access point
*/
// returns current IP address on external wifi access point
std::string staIP();

// FTM related functions
int get_num_responder_aps();
bool ftm_report_available();
const std::map<std::string, wifi_ftm_initiator_cfg_t> get_map_of_responder_configs(
uint8_t frame_count, uint16_t burst_period, const std::vector<std::string>& returned_ssids);

// retrieve the latest FTM report all at once (fast, no per-field calls)
const PuaraAPI::FTM::ftm_report_info& get_last_ftm_report();

// retrieve a report by reference, returns true if available (flag not cleared)
bool get_ftm_report(PuaraAPI::FTM::ftm_report_info &out);

void set_ftm_report_as_consumed();


double getVarNumber(std::string varName);
std::string getVarText(std::string varName);
};
Expand Down
83 changes: 83 additions & 0 deletions src/puara_ftm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "puara_ftm.hpp"
#include "puara_logger.hpp"
#include "puara_wifi.hpp"

#include <unordered_set>

// #include <.h>
//#include <iostream>

namespace PuaraAPI
{

// Return a map of responders defined by user and their wifi_ftm_cfg_t struct info (MAC, channel)
const std::map<std::string, wifi_ftm_initiator_cfg_t> FTM::get_map_of_responder_configs(
uint8_t frame_count, uint16_t burst_period,
const std::vector<std::string>& returned_ssids)
{

std::map<std::string, wifi_ftm_initiator_cfg_t> responder_configs;
std::unordered_set<std::string> wanted_ssids(returned_ssids.begin(), returned_ssids.end());

for (const auto& ap : scanned_responder_aps) {
// Check if the AP is in the list of returned_ssids from the web interface (i.e. user selected it for FTM)
if (wanted_ssids.count(ap.ssid)) {
wifi_ftm_initiator_cfg_t ftm_config{};
std::copy(std::begin(ap.bssid), std::end(ap.bssid), std::begin(ftm_config.resp_mac));
ftm_config.channel = ap.primary_channel;
ftm_config.frm_count = frame_count;
ftm_config.burst_period = burst_period;
ftm_config.use_get_report_api = false; // use WIFI_EVENT_FTM_REPORT to get FTM report
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant comment

responder_configs[ap.ssid] = ftm_config;

ESP_LOGI(PUARA_TAG, "Added FTM responder config for AP: SSID: %s, BSSID: %02x:%02x:%02x:%02x:%02x:%02x, Channel: %d",
ap.ssid.c_str(),
ap.bssid[0], ap.bssid[1], ap.bssid[2], ap.bssid[3], ap.bssid[4], ap.bssid[5],
ap.primary_channel
);
}
}
return responder_configs;

}




}

/*
//make this take in a position from an array of wifi_ftm_cfg
void FTM::requestFTM(){
esp_err_t result = esp_wifi_ftm_initiate_session(&wifi_ftm_cfg);
if (result != ESP_OK) {
ESP_LOGD(PUARA_TAG, "FTM Session Failed : %s", esp_err_to_name(result));
}else if(result == ESP_OK) {
ESP_LOGD(PUARA_TAG, "FTM session initiated successfully");
}
}

bool FTM::set_offset_responder(int16_t offset_cm){
// Set offset in cm for FTM Responder :
// An equivalent offset is calculated in picoseconds and added in TOD of FTM
// Measurement frame (T1).
esp_err_t result = esp_wifi_ftm_resp_set_offset(offset_cm);
if(result == ESP_OK) {
ESP_LOGD(PUARA_TAG, "FTM responder offset set successfully");
return true;
} else {
ESP_LOGD(PUARA_TAG, "Failed to set FTM responder offset : %s", esp_err_to_name(result));
return false;
}
}

void FTM::end_ftm_request_session(){
//End the ongoing FTM Initiator session. This API works only on FTM Initiator
esp_err_t result = esp_wifi_ftm_end_session();
if(result == ESP_OK) {
ESP_LOGD(PUARA_TAG, "FTM session ended successfully");
} else {
ESP_LOGD(PUARA_TAG, "Failed to end FTM session : %s", esp_err_to_name(result));
}
}
*/
73 changes: 73 additions & 0 deletions src/puara_ftm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once

#include "esp_log.h"
#include <esp_wifi.h>

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <unordered_set>
#include <vector>

namespace PuaraAPI
{
//struct WiFi;

struct FTM
{
// flag to indicate if FTM report data is available
bool ftm_report_available = false; //API accessible

struct scanned_responder_ap_info
{
std::string ssid;
uint8_t bssid[6];
int8_t rssi;
uint8_t primary_channel;
};

// Vector to hold scanned found Responder-Ready APs, populated during WiFi scan
std::vector<scanned_responder_ap_info> scanned_responder_aps{}; //internal only - No API calls


struct ftm_report_info
{
uint32_t rtt_est; // in nanoseconds
uint32_t dist_est; // in centimeters
int8_t rssi; // RSSI of the FTM frame, as an indicator of signal strength of the FTM responder AP
};

ftm_report_info last_ftm_report{}; // API accessible

// fast accessor for the last report; returns const reference so caller can
// peek multiple fields without copying
const ftm_report_info& get_last_ftm_report() const { return last_ftm_report; }

// check if a report is available and optionally copy it into provided struct
// returns true when a report was present; does **not** consume the flag
bool get_ftm_report(ftm_report_info &out) const {
if (ftm_report_available) {
out = last_ftm_report;
return true;
}
return false;
}

// Map of user-defined FTM responder configurations, with SSID as key and
// wifi_ftm_initiator_cfg_t struct (holding responder MAC, channel, frame count,
// burst period) as value
std::map<std::string, wifi_ftm_initiator_cfg_t> map_of_responder_configs{}; // API accessible


// Return a map of responders defined by user and their wifi_ftm_cfg_t struct info
// (MAC, channel, frame count, burst period) for upcoming FTM session initiation.
// `returned_ssids` is passed by const-reference to avoid copying.
const std::map<std::string, wifi_ftm_initiator_cfg_t> get_map_of_responder_configs(
uint8_t frame_count, uint16_t burst_period, const std::vector<std::string>& returned_ssids);



};
}
Loading