Skip to content

Commit

Permalink
fixed ESP32C3 compile issue - fixed ssid and pass error when only num…
Browse files Browse the repository at this point in the history
…s are entered - re-enabled support for UDP in sta mode
  • Loading branch information
seeul8er committed Dec 27, 2023
1 parent b0dc1fc commit c17c8d7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
7 changes: 4 additions & 3 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
</script>
<!--Minified version of dronebridge.js-->
<script type="text/javascript">
const ROOT_URL=window.location.href;let conn_status=0;function change_ap_ip_visibility(){var t=document.getElementById("ap_ip_div"),e=document.getElementById("ap_channel_div");document.getElementById("esp32_mode").value<2?(t.style.display="block",e.style.display="block"):(t.style.display="none",e.style.display="none")}function change_msp_ltm_visibility(){var t=document.getElementById("msp_ltm_div"),e=document.getElementById("trans_pack_size_div");"1"===document.getElementById("telem_proto").value?(t.style.display="block",e.style.display="none"):(t.style.display="none",e.style.display="block")}function toJSONString(t){let e={},n=t.querySelectorAll("input, select");for(let s=0;s<n.length;++s){let o=n[s],i=o.name,a=o.value;isNaN(Number(a))?i&&(e[i]=a):i&&(e[i]=parseInt(a))}return JSON.stringify(e)}async function get_json(t){let e=new AbortController;setTimeout(()=>{e.abort()},1e3);let n=await fetch(ROOT_URL+t,{signal:e.signal});if(!n.ok){let s=`An error has occured: ${n.status}`;throw conn_status=0,Error(s)}return await n.json()}async function send_json(t,e){let n=await fetch(ROOT_URL+t,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:e});if(!n.ok){conn_status=0;let s=`An error has occured: ${n.status}`;throw Error(s)}return await n.json()}function get_system_info(){return get_json("api/system/info").then(t=>{console.log("Received settings: "+t),document.getElementById("about").innerHTML="DroneBridge for ESP32 - v"+t.major_version+"."+t.minor_version+" - esp-idf "+t.idf_version}).catch(t=>(conn_status=0,t.message,-1)),0}function update_conn_status(){conn_status?document.getElementById("web_conn_status").innerHTML='<span class="dot_green"></span> connected to ESP32':(document.getElementById("web_conn_status").innerHTML='<span class="dot_red"></span> disconnected from ESP32',document.getElementById("current_client_ip").innerHTML="")}function get_stats(){get_json("api/system/stats").then(t=>{conn_status=1;let e=parseInt(t.read_bytes);!isNaN(e)&&e>1e3?document.getElementById("read_bytes").innerHTML=e/1e3+" kb":isNaN(e)||(document.getElementById("read_bytes").innerHTML=e+" bytes");let n=parseInt(t.tcp_connected);!isNaN(n)&&n>1?document.getElementById("tcp_connected").innerHTML=n+" clients":isNaN(n)||(document.getElementById("tcp_connected").innerHTML=n+" client");let s=parseInt(t.udp_connected);!isNaN(s)&&s>1?document.getElementById("udp_connected").innerHTML=s+" clients":isNaN(s)||(document.getElementById("udp_connected").innerHTML=s+" client"),document.getElementById("current_client_ip").innerHTML="IP Address: "+t.current_client_ip}).catch(t=>{conn_status=0,t.message})}function get_settings(){return get_json("api/settings/request").then(t=>{for(let e in console.log("Received settings: "+t),conn_status=1,t)t.hasOwnProperty(e)&&(document.getElementById(e).value=t[e]+"")}).catch(t=>(conn_status=0,t.message,show_toast(t.message),-1)),change_ap_ip_visibility(),change_msp_ltm_visibility(),0}function show_toast(t){Toastify({text:t,duration:5e3,newWindow:!0,close:!0,gravity:"top",position:"center",backgroundColor:"linear-gradient(to right, #b6e026, #abdc28)",stopOnFocus:!0}).showToast()}function save_settings(){send_json("api/settings/change",toJSONString(document.getElementById("settings_form"))).then(t=>{console.log(t),conn_status=1,show_toast(t.msg),get_settings()}).catch(t=>{show_toast(t.message)})}function trigger_reboot(){get_json("api/system/reboot").then(t=>{show_toast(t.msg)}).catch(t=>{t.message,show_toast(t.message)})} </script>
const ROOT_URL=window.location.href;let conn_status=0;function change_ap_ip_visibility(){var e=document.getElementById("ap_ip_div"),t=document.getElementById("ap_channel_div");document.getElementById("esp32_mode").value<2?(e.style.display="block",t.style.display="block"):(e.style.display="none",t.style.display="none")}function change_msp_ltm_visibility(){var e=document.getElementById("msp_ltm_div"),t=document.getElementById("trans_pack_size_div");"1"===document.getElementById("telem_proto").value?(e.style.display="block",t.style.display="none"):(e.style.display="none",t.style.display="block")}function toJSONString(e){let t={},n=e.querySelectorAll("input, select");for(let s=0;s<n.length;++s){let o=n[s],i=o.name,a=o.value;isNaN(Number(a))||0===i.localeCompare("wifi_ssid")||0===i.localeCompare("wifi_pass")?i&&(t[i]=a):i&&(t[i]=parseInt(a))}return JSON.stringify(t)}async function get_json(e){let t=new AbortController;setTimeout(()=>{t.abort()},1e3);let n=await fetch(ROOT_URL+e,{signal:t.signal});if(!n.ok){let s=`An error has occured: ${n.status}`;throw conn_status=0,Error(s)}return await n.json()}async function send_json(e,t){let n=await fetch(ROOT_URL+e,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:t});if(!n.ok){conn_status=0;let s=`An error has occured: ${n.status}`;throw Error(s)}return await n.json()}function get_system_info(){return get_json("api/system/info").then(e=>{console.log("Received settings: "+e),document.getElementById("about").innerHTML="DroneBridge for ESP32 - v"+e.major_version+"."+e.minor_version+" - esp-idf "+e.idf_version}).catch(e=>(conn_status=0,e.message,-1)),0}function update_conn_status(){conn_status?document.getElementById("web_conn_status").innerHTML='<span class="dot_green"></span> connected to ESP32':(document.getElementById("web_conn_status").innerHTML='<span class="dot_red"></span> disconnected from ESP32',document.getElementById("current_client_ip").innerHTML="")}function get_stats(){get_json("api/system/stats").then(e=>{conn_status=1;let t=parseInt(e.read_bytes);!isNaN(t)&&t>1e3?document.getElementById("read_bytes").innerHTML=t/1e3+" kb":isNaN(t)||(document.getElementById("read_bytes").innerHTML=t+" bytes");let n=parseInt(e.tcp_connected);!isNaN(n)&&n>1?document.getElementById("tcp_connected").innerHTML=n+" clients":isNaN(n)||(document.getElementById("tcp_connected").innerHTML=n+" client");let s=parseInt(e.udp_connected);!isNaN(s)&&s>1?document.getElementById("udp_connected").innerHTML=s+" clients":isNaN(s)||(document.getElementById("udp_connected").innerHTML=s+" client"),document.getElementById("current_client_ip").innerHTML="IP Address: "+e.current_client_ip}).catch(e=>{conn_status=0,e.message})}function get_settings(){return get_json("api/settings/request").then(e=>{for(let t in console.log("Received settings: "+e),conn_status=1,e)e.hasOwnProperty(t)&&(document.getElementById(t).value=e[t]+"")}).catch(e=>(conn_status=0,e.message,show_toast(e.message),-1)),change_ap_ip_visibility(),change_msp_ltm_visibility(),0}function show_toast(e){Toastify({text:e,duration:5e3,newWindow:!0,close:!0,gravity:"top",position:"center",backgroundColor:"linear-gradient(to right, #b6e026, #abdc28)",stopOnFocus:!0}).showToast()}function save_settings(){send_json("api/settings/change",toJSONString(document.getElementById("settings_form"))).then(e=>{console.log(e),conn_status=1,show_toast(e.msg),get_settings()}).catch(e=>{show_toast(e.message)})}function trigger_reboot(){get_json("api/system/reboot").then(e=>{show_toast(e.msg)}).catch(e=>{e.message,show_toast(e.message)})}
</script>
</head>
<body>
<div class="container">
Expand Down Expand Up @@ -211,11 +212,11 @@ <h2>Settings</h2>
}
while (get_settings() < 0) {
}
change_msp_ltm_visibility()
change_ap_ip_visibility()
// get stats every half a second
setInterval(get_stats, 500)
setInterval(update_conn_status, 500)
change_msp_ltm_visibility()
change_ap_ip_visibility()
</script>
</body>
</html>
3 changes: 2 additions & 1 deletion frontend_src/dronebridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function toJSONString(form) {
let element = elements[i]
let name = element.name
let value = element.value;
if (!isNaN(Number(value))) {
// parse numbers as numbers except for the SSID and the password fields
if (!isNaN(Number(value)) && (name.localeCompare("wifi_ssid") !== 0) && (name.localeCompare("wifi_pass") !== 0)) {
if (name) {
obj[name] = parseInt(value)
}
Expand Down
34 changes: 20 additions & 14 deletions main/db_esp32_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,22 @@ void handle_tcp_master(const int tcp_master_socket, int tcp_clients[]) {
}

/**
* Add a new UDP client to the list of known UDP clients. Checks if client is already known. Added client will receive
* UDP packets with serial info and will be able to send UDP packets to the serial interface of the ESP32.
* PORT, MAC & IP must be set inside new_db_udp_client
* Add a new UDP client to the list of known UDP clients. Checks if client is already known based on IP and port.
* Added client will receive UDP packets with serial info and will be able to send UDP packets to the serial interface
* of the ESP32.
* PORT, MAC & IP should be set inside new_db_udp_client. If MAC is not set then the device cannot be removed later on.
*
* @param connections Structure containing all UDP connection information
* @param new_db_udp_client New client to add to the UDP list. PORT, MAC & IP must be
* @param new_db_udp_client New client to add to the UDP list. PORT, MAC & IP must be set. If MAC is not set then the
* device cannot be removed later on.
*/
void add_udp_to_known_clients(struct db_udp_connection_t *connections, struct db_udp_client_t new_db_udp_client) {
// Check if client is already listed based on PORT and MAC
// Check if client is already listed based on PORT and IP
for (int i = 0; i < MAX_UDP_CLIENTS; i++) {
if (connections->db_udp_clients[i].udp_client.sin_port == new_db_udp_client.udp_client.sin_port &&
memcmp(connections->db_udp_clients[i].mac, new_db_udp_client.mac, sizeof(connections->db_udp_clients[i].mac)) == 0) {
if ((connections->db_udp_clients[i].udp_client.sin_port == new_db_udp_client.udp_client.sin_port
&& new_db_udp_client.udp_client.sin_family == PF_INET &&
((struct sockaddr_in *) &connections->db_udp_clients[i])->sin_addr.s_addr ==
((struct sockaddr_in *) &new_db_udp_client)->sin_addr.s_addr)) {
return; // client existing - do not add
}
}
Expand All @@ -244,18 +248,18 @@ void add_udp_to_known_clients(struct db_udp_connection_t *connections, struct db
}

/**
* Remove a client from the sending list. Client will no longer receive UDP packets. Port & MAC address must be given.
* Remove a client from the sending list. Client will no longer receive UDP packets. MAC address must be given.
* Usually called in AP-Mode when a station disconnects. In any other case we will not know since UDP is a connection-less
* protocol
*
* @param connections The list of open UDP connections
* @param db_udp_client The UDP client to remove based on its PORT & MAC address
* @param db_udp_client The UDP client to remove based on its MAC address
*/
void remove_udp_from_known_clients(struct db_udp_connection_t *connections, struct db_udp_client_t db_udp_client) {
for (int i = 0; i < MAX_UDP_CLIENTS; i++) {
if (connections->db_udp_clients[i].udp_client.sin_port == db_udp_client.udp_client.sin_port &&
memcmp(connections->db_udp_clients[i].mac, db_udp_client.mac, sizeof(connections->db_udp_clients[i].mac)) == 0) {
if (memcmp(connections->db_udp_clients[i].mac, db_udp_client.mac, sizeof(connections->db_udp_clients[i].mac)) == 0) {
connections->db_udp_clients[i] = (const struct db_udp_client_t){ 0 };
num_connected_udp_clients -= 1;
return; // client existing
}
}
}
Expand Down Expand Up @@ -331,8 +335,10 @@ void control_module_udp_tcp() {
if (recv_length > 0) {
ESP_LOGD(TAG, "UDP: Received %i bytes", recv_length);
write_to_uart(udp_buffer, recv_length);
// Allows to register new app on different port
// TODO: Determine MAC address of UDP client otherwise it cannot be removed from the list later on
// Allows to register new app on different port. Used e.g. for UDP conn setup in sta-mode.
// Devices/Ports added this way cannot be removed in sta-mode since UDP is connectionless, and we cannot
// determine if the client is still existing. This will blow up the list connected devices.
// In AP-Mode the devices can be removed based on the IP/MAC address
add_udp_to_known_clients(&udp_conn, new_db_udp_client);

}
Expand Down
17 changes: 13 additions & 4 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ static EventGroupHandle_t s_wifi_event_group;
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1

/**
* Devices get added based on IP (check if IP & PORT are already listed) and removed from the UDP broadcast connection
* based on MAC address
*
* @param arg
* @param event_base
* @param event_id
* @param event_data
*/
static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
// Wifi access point mode events
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
Expand Down Expand Up @@ -215,8 +224,8 @@ void init_wifi_apmode() {
.max_connection = 10
},
};
xthal_memcpy(wifi_config.ap.ssid, DEFAULT_SSID, 32);
xthal_memcpy(wifi_config.ap.password, DEFAULT_PWD, 64);
strncpy((char *)wifi_config.ap.ssid, (char *)DEFAULT_SSID, 32);
strncpy((char *)wifi_config.ap.password, (char *)DEFAULT_PWD, 64);

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_11B));
Expand Down Expand Up @@ -267,8 +276,8 @@ void init_wifi_clientmode() {
.password = "dronebridge"
},
};
xthal_memcpy(wifi_config.sta.ssid, DEFAULT_SSID, 32);
xthal_memcpy(wifi_config.sta.password, DEFAULT_PWD, 64);
strncpy((char *)wifi_config.sta.ssid, (char *)DEFAULT_SSID, 32);
strncpy((char *)wifi_config.sta.password, (char *)DEFAULT_PWD, 64);

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
Expand Down

0 comments on commit c17c8d7

Please sign in to comment.