parent
ff1c09d9f8
commit
23883ef582
@ -1 +1 @@ |
||||
Subproject commit 7c43fd3e1a147d24cde05a7ecd3d155e293caa89 |
||||
Subproject commit 6df0fcacd8bc23d2af86df4d5c5c2c624938eeb4 |
@ -0,0 +1,72 @@ |
||||
#include <esp8266.h> |
||||
#include <httpd.h> |
||||
#include "page_home.h" |
||||
#include "uptime.h" |
||||
|
||||
|
||||
/** System Status page */ |
||||
int FLASH_FN tplHome(HttpdConnData *connData, char *token, void **arg) |
||||
{ |
||||
// arg is unused
|
||||
(void)arg; |
||||
|
||||
struct station_config stconf; |
||||
char buff[128]; |
||||
u8 mac[6]; |
||||
|
||||
// empty string if no token matches
|
||||
buff[0] = 0; |
||||
|
||||
if (token == NULL) return HTTPD_CGI_DONE; |
||||
|
||||
if (strcmp(token, "uptime") == 0) { |
||||
// Uptime
|
||||
uptime_str(buff); |
||||
|
||||
} else if (strcmp(token, "heap") == 0) { |
||||
// Free heap
|
||||
sprintf(buff, "%u", system_get_free_heap_size()); |
||||
|
||||
} else if (strcmp(token, "wifiMode") == 0) { |
||||
// WiFi mode
|
||||
switch (wifi_get_opmode()) { |
||||
case STATION_MODE: strcpy(buff, "Client"); break; |
||||
case SOFTAP_MODE: strcpy(buff, "SoftAP"); break; |
||||
case STATIONAP_MODE: strcpy(buff, "STA+AP"); break; |
||||
default: strcpy(buff, "Unknown"); |
||||
} |
||||
|
||||
} else if (strcmp(token, "staSSID") == 0) { |
||||
// Station SSID (if in station mode)
|
||||
int opmode = wifi_get_opmode(); |
||||
if (opmode != STATION_MODE && opmode != STATIONAP_MODE) { |
||||
strcpy(buff, "N/A"); // no SSID in AP-only mode
|
||||
} else { |
||||
wifi_station_get_config(&stconf); |
||||
strcpy(buff, (char*)stconf.ssid); |
||||
} |
||||
|
||||
} else if (strcmp(token, "staRSSI") == 0) { |
||||
// Signal strength if in Station mode
|
||||
int rssi = wifi_station_get_rssi(); |
||||
sprintf(buff, "%d", rssi); |
||||
|
||||
} else if (strcmp(token, "staMAC") == 0) { |
||||
// Station MAC addr
|
||||
wifi_get_macaddr(STATION_IF, mac); |
||||
sprintf(buff, MACSTR, MAC2STR(mac)); |
||||
|
||||
} else if (strcmp(token, "apMAC") == 0) { |
||||
// SoftAP MAC addr
|
||||
wifi_get_macaddr(SOFTAP_IF, mac); |
||||
sprintf(buff, MACSTR, MAC2STR(mac)); |
||||
|
||||
} else if (strcmp(token, "chipID") == 0) { |
||||
// Chip serial number
|
||||
sprintf(buff, "%08x", system_get_chip_id()); |
||||
} |
||||
|
||||
httpdSend(connData, buff, -1); |
||||
|
||||
return HTTPD_CGI_DONE; |
||||
} |
@ -0,0 +1,8 @@ |
||||
#ifndef PAGE_HOME_H |
||||
#define PAGE_HOME_H |
||||
|
||||
#include <httpd.h> |
||||
|
||||
int tplHome(HttpdConnData *connData, char *token, void **arg); |
||||
|
||||
#endif // PAGE_HOME_H
|
@ -0,0 +1,180 @@ |
||||
#include <esp8266.h> |
||||
#include <sbmp.h> |
||||
|
||||
#include "datalink.h" |
||||
#include "sampling.h" |
||||
|
||||
// The buffer is big enough for 256 data bytes - 4*64
|
||||
|
||||
// number of 32-bit vars in the chunk
|
||||
#define CHUNK_LEN_32 64 |
||||
|
||||
// chunk size for bulk transfer - 64 floats
|
||||
#define CHUNK_LEN (CHUNK_LEN_32*4) |
||||
|
||||
|
||||
static bool acquire_pending = false; |
||||
static uint16_t acquire_session; |
||||
|
||||
|
||||
static ETSTimer prSampleAbortTimer; |
||||
|
||||
static void FLASH_FN prSampleAbortTimerCb(void *arg) { |
||||
warn("Sampling aborted due to timeout."); |
||||
acquire_pending = false; |
||||
|
||||
// free the data obj if not NULL
|
||||
sbmp_ep_free_listener_obj(dlnk_ep, acquire_session); |
||||
// release the slot
|
||||
sbmp_ep_remove_listener(dlnk_ep, acquire_session); |
||||
} |
||||
|
||||
|
||||
typedef struct { |
||||
uint32_t pos; |
||||
uint32_t total; |
||||
} DataReadState; |
||||
|
||||
static void FLASH_FN request_data_sesn_listener(SBMP_Endpoint *ep, SBMP_Datagram *dg, void **obj) |
||||
{ |
||||
dbg("Received msg in session %d, dg type %d", dg->session, dg->type); |
||||
|
||||
DataReadState *state = *obj; |
||||
|
||||
// allocate the state struct
|
||||
if (state == NULL) { |
||||
state = malloc(sizeof(DataReadState)); |
||||
*obj = state; |
||||
} |
||||
|
||||
|
||||
PayloadParser pp; |
||||
switch (dg->type) { |
||||
case DG_BULK_OFFER:// Data ready notification
|
||||
info("--- Data offered for bulk transfer ---"); |
||||
|
||||
// data is ready to be read
|
||||
pp = pp_start(dg->payload, dg->length); |
||||
|
||||
state->pos = 0; |
||||
state->total = pp_u32(&pp); |
||||
|
||||
dbg("Total bytes: %d", state->total); |
||||
|
||||
sbmp_bulk_request(ep, state->pos, CHUNK_LEN, dg->session); // 64 floats in one message
|
||||
break; |
||||
|
||||
case DG_BULK_DATA: // data received
|
||||
info("--- Received a chunk ---"); |
||||
|
||||
// Process the received data
|
||||
pp = pp_start(dg->payload, dg->length); |
||||
for (int i = 0; i < dg->length/4; i++) { |
||||
float f = pp_float(&pp); |
||||
dbg("Rx %.2f", f); |
||||
} |
||||
|
||||
// and ask for more
|
||||
state->pos += dg->length; |
||||
|
||||
if (state->pos >= state->total) { |
||||
dbg("Transfer is complete."); |
||||
// transfer complete
|
||||
|
||||
// make sure the peer has freed the buffer
|
||||
// (may be waiting for us if we wanted to re-read something)
|
||||
sbmp_bulk_abort(ep, dg->session); |
||||
goto cleanup; |
||||
} else { |
||||
// read next part
|
||||
sbmp_bulk_request(ep, state->pos, CHUNK_LEN, dg->session); |
||||
} |
||||
break; |
||||
|
||||
case DG_BULK_ABORT: // Peer resets the readout
|
||||
// this is unlikely
|
||||
warn("Bulk transfer aborted by peer."); |
||||
goto cleanup; |
||||
} |
||||
|
||||
return; |
||||
|
||||
cleanup: |
||||
// free the obj
|
||||
free(state); |
||||
// remove the listener
|
||||
sbmp_ep_remove_listener(ep, dg->session); |
||||
} |
||||
|
||||
|
||||
static bool FLASH_FN meas_request_data(uint16_t count) |
||||
{ |
||||
info("Requesting data capture - %d samples.", count); |
||||
|
||||
if (acquire_pending) { |
||||
error("Acquire request already in progress."); |
||||
return false; |
||||
} |
||||
|
||||
if (sbmp_ep_handshake_status(dlnk_ep) != SBMP_HSK_SUCCESS) { |
||||
error("Hsk status not SUCCESS, can't request data."); |
||||
return false; |
||||
} |
||||
|
||||
// start the abort timer - timeout
|
||||
os_timer_disarm(&prSampleAbortTimer); |
||||
os_timer_setfn(&prSampleAbortTimer, prSampleAbortTimerCb, NULL); |
||||
os_timer_arm(&prSampleAbortTimer, 5000, 0); // 5 seconds, no repeat
|
||||
|
||||
|
||||
// start a message
|
||||
uint16_t sesn; |
||||
bool suc = sbmp_ep_start_message(dlnk_ep, DG_REQUEST_CAPTURE, sizeof(uint16_t), &sesn); |
||||
if (!suc) return false; |
||||
|
||||
// register the session listener
|
||||
sbmp_ep_add_listener(dlnk_ep, sesn, request_data_sesn_listener, NULL); |
||||
|
||||
// request N values
|
||||
sbmp_ep_send_u16(dlnk_ep, count); |
||||
|
||||
dbg("Request sent, session nr %d", sesn); |
||||
|
||||
acquire_session = sesn; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
|
||||
// ------ C G I ---------
|
||||
|
||||
|
||||
|
||||
int FLASH_FN cgiReadSamples(HttpdConnData *connData) |
||||
{ |
||||
char buff[128]; |
||||
|
||||
if (connData->conn == NULL) { |
||||
//Connection aborted. Clean up.
|
||||
return HTTPD_CGI_DONE; |
||||
} |
||||
|
||||
uint16_t count = 1; |
||||
int len = httpdFindArg(connData->getArgs, "n", buff, sizeof(buff)); |
||||
if (len != -1) { |
||||
count = (uint16_t)atoi(buff); |
||||
} |
||||
|
||||
dbg("User wants %d samples.", count); |
||||
|
||||
meas_request_data(count); |
||||
|
||||
httpdStartResponse(connData, 200); |
||||
httpdHeader(connData, "Content-Type", "text/plain"); |
||||
httpdEndHeaders(connData); |
||||
|
||||
// body
|
||||
httpdSend(connData, "OK.", -1); |
||||
|
||||
return HTTPD_CGI_DONE; |
||||
} |
@ -0,0 +1,10 @@ |
||||
#ifndef SAMPLING_H |
||||
#define SAMPLING_H |
||||
|
||||
#include <esp8266.h> |
||||
#include <httpd.h> |
||||
|
||||
// temporary func to nread samples
|
||||
int cgiReadSamples(HttpdConnData *connData); |
||||
|
||||
#endif // SAMPLING_H
|
Loading…
Reference in new issue