From 97c5aa834b041bf52b1b9f508d58d39b03f70712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Fri, 28 Apr 2023 21:17:02 +0200 Subject: inventar + upravljalnik --- prog/inventar/h.php | 1 + prog/inventar/index.php | 40 ++++++++------ prog/upravljalnik/naprava/.gitignore | 1 + prog/upravljalnik/naprava/platformio.ini | 22 ++++++++ prog/upravljalnik/naprava/src/main.cpp | 68 +++++++++++++++++++++++ prog/upravljalnik/pcap.c | 92 ++++++++++++++++++++++++++++++++ 6 files changed, 208 insertions(+), 16 deletions(-) create mode 100644 prog/upravljalnik/naprava/.gitignore create mode 100644 prog/upravljalnik/naprava/platformio.ini create mode 100644 prog/upravljalnik/naprava/src/main.cpp create mode 100644 prog/upravljalnik/pcap.c (limited to 'prog') diff --git a/prog/inventar/h.php b/prog/inventar/h.php index 2cba303..9b25d7a 100644 --- a/prog/inventar/h.php +++ b/prog/inventar/h.php @@ -1,4 +1,5 @@ "password" ]; header("Content-Security-Policy: script-src 'none'"); // disable js $db = new PDO("sqlite:db", null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); if (!$db) diff --git a/prog/inventar/index.php b/prog/inventar/index.php index 4dd38dd..4407939 100644 --- a/prog/inventar/index.php +++ b/prog/inventar/index.php @@ -1,5 +1,10 @@ "test"]; +// create table stvari (id integer primary key autoincrement, lastnik, ime, opis, vrednost, omejitev, kol, lokacija, slika, datum default CURRENT_TIMESTAMP); +require_once "h.php"; +if (!empty($_REQUEST["src"])) { + header("Content-Type: text/plain"); + die(file_get_contents($_SERVER["SCRIPT_FILENAME"])); +} if ((isset($_REQUEST["prijava"]) && (empty($_SERVER["PHP_AUTH_USER"]) || $auth[$_SERVER['PHP_AUTH_USER']] != $_SERVER['PHP_AUTH_PW'])) || (isset($_SERVER['PHP_AUTH_USER']) && $auth[$_SERVER['PHP_AUTH_USER']] != $_SERVER['PHP_AUTH_PW'])) { header("WWW-Authenticate: Basic realm=inventar"); header("HTTP/1.0 401 Neprijavljen"); @@ -10,9 +15,15 @@ if ((isset($_REQUEST["prijava"]) && (empty($_SERVER["PHP_AUTH_USER"]) || $auth[$ table, td, tr, th { border: 1px solid red; } +img { + width: 1cm; +} +img:hover { + width: 5cm; +}
-" /> +" />
@@ -21,7 +32,6 @@ if (empty($_SERVER['PHP_AUTH_USER'])) echo "prijava"; else echo ""; -require_once "h.php"; $passed = []; if (!empty($_POST["izbriši"])) $db->exec("delete from stvari where lastnik = '{$_SERVER["PHP_AUTH_USER"]}' and id = '{$_POST["id"]}'"); @@ -30,23 +40,23 @@ if (!empty($_POST["dodaj"]) && !empty($_SERVER["PHP_AUTH_USER"])) foreach ($_POST as $k => $v) if (is_numeric($k)) { foreach ($_POST as $k2 => $v2) { - $sp = strpos($k2, $k); - if ($sp) { + $sp = strpos($k2, "" . $k); + if ($sp) $passed[] = substr($k2, 0, $sp); - } } $par = []; foreach ($passed as $p) { - if (!str_contains(strtolower($p), "id")) + if (strpos(strtolower($p), "id") === false) $par[] = "'" . SQLite3::escapeString($p) . "' = '" . SQLite3::escapeString($_POST[$p . $k]) . "'"; } $s = "update stvari set " . implode(", ", $par) . " where lastnik='{$_SERVER["PHP_AUTH_USER"]}' and id={$k}"; + echo $s; $db->exec($s); } if (!empty($_REQUEST["q"])) - $ret = $ro->query("select * from stvari where " . $_REQUEST["q"]); + $ret = $ro->query("select * from stvari where " . $_REQUEST["q"] . " order by datum desc"); else - $ret = $ro->query("select * from stvari"); + $ret = $ro->query("select * from stvari order by datum desc"); $i = 0; foreach ($ret as $row) { if ($i == 0) @@ -60,21 +70,19 @@ foreach ($ret as $row) { echo ""; } - $last = true; - if ($row["lastnik"] == $_SERVER['PHP_AUTH_USER'] && $k != "lastnik") { - echo ""; - $last = false; + if ($row["lastnik"] == @$_SERVER['PHP_AUTH_USER'] && $k != "lastnik") { + echo ""; } else - if ($last && $k != "slika") + if ($row["lastnik"] != @$_SERVER['PHP_AUTH_USER'] && $k != "slika") echo "$v"; } echo ""; diff --git a/prog/upravljalnik/naprava/.gitignore b/prog/upravljalnik/naprava/.gitignore new file mode 100644 index 0000000..03f4a3c --- /dev/null +++ b/prog/upravljalnik/naprava/.gitignore @@ -0,0 +1 @@ +.pio diff --git a/prog/upravljalnik/naprava/platformio.ini b/prog/upravljalnik/naprava/platformio.ini new file mode 100644 index 0000000..878054d --- /dev/null +++ b/prog/upravljalnik/naprava/platformio.ini @@ -0,0 +1,22 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:myboard] +platform = espressif8266 +board = nodemcuv2 +monitor_speed = 460800 +upload_speed = 460800 +build_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI -DDEBUG_ESP_PORT=Serial -DIP_FORWARD=1 -DMONITOR_SPEED=460800 +framework = arduino +board_build.filesystem = littlefs +board_build.ldscript = eagle.flash.4m2m.ld +src_filter = -<*> + +lib_deps = + /root/projects/ADS1115_WE diff --git a/prog/upravljalnik/naprava/src/main.cpp b/prog/upravljalnik/naprava/src/main.cpp new file mode 100644 index 0000000..78dd46a --- /dev/null +++ b/prog/upravljalnik/naprava/src/main.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +extern "C" { + #include "user_interface.h" +} +void incoming (unsigned char * buf, short unsigned int type) { + Serial.println(type); +} +uint8_t curchan[] = { 0 }; // we have sizeof curchan modules connected (at most four) +bool ready[sizeof curchan] = { false }; +IRAM_ATTR void rdy0 () { + ready[0] = true; +} +ADS1115_WE adc[] = { ADS1115_WE(0x49) }; // VDD +void (*rdyfuncs[])() = { rdy0 }; +int rdypins[] = { D7 }; +uint16_t adcval[sizeof curchan][4]; +bool upload = false; +void setup () { + Wire.begin(); + Wire.setClock(400000L); + Serial.begin(MONITOR_SPEED); + Serial.println("setupstart"); + for (unsigned i = 0; i < sizeof curchan; i++) { + Serial.println("nastavljam ADC " + String(i)); + if (!adc[i].init()) + Serial.println("ADS1115 " + String(i) + " not connected!"); + pinMode(rdypins[i], INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(rdypins[i]), rdyfuncs[i], RISING); + adc[i].setVoltageRange_mV(ADS1115_RANGE_4096); + adc[i].setSingleChannel(curchan[i]); + adc[i].setConvRate(ADS1115_32_SPS); + adc[i].setMeasureMode(ADS1115_CONTINOUS); + adc[i].setAlertPinMode(ADS1115_ASSERT_AFTER_1); + adc[i].setAlertPinToConversionReady(); + adc[i].skip_delays = true; + } + Serial.println("nastavljam wifi"); + WiFi.setOutputPower(20.5); + wifi_set_opmode(STATION_MODE); + wifi_station_disconnect(); + wifi_promiscuous_enable(1); + wifi_set_promiscuous_rx_cb(incoming); + wifi_set_channel(13); + Serial.println("nastavil"); +} +uint8_t packet[] = { 0x08, 0x00, 0x00, 0x00, 'K', '@', '4', 'A', '.', 'S', 'I', ' ', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +void loop () { + for (unsigned i = 0; i < sizeof curchan; i++) { + if (!ready[i]) + continue; + uint16_t val = adc[i].getRawResult(); + if (val != adcval[i][curchan[i]]) { + adcval[i][curchan[i]] = val; + packet[12+i*8+curchan[i]*2] = val >> 8; + packet[12+i*8+curchan[i]*2+1] = val & 0xff; + upload = true; + } + adc[i].setSingleChannel((curchan[i] = (curchan[i]+1) % 4)); // XXX kaj če dobim ready signal še preden naprava dejansko zamenja naslov? za vsak slučaj ready=false nastavim tukaj spodaj, ampak vseeno zna biti kaj težav, še posebej glede na to, da izvorna knjižnica za ads1115 za chansel da nekaj delaya + ready[i] = false; + } + if (upload) { + upload = false; + wifi_send_pkt_freedom(packet, sizeof packet, 0); + } +} diff --git a/prog/upravljalnik/pcap.c b/prog/upravljalnik/pcap.c new file mode 100644 index 0000000..fe89856 --- /dev/null +++ b/prog/upravljalnik/pcap.c @@ -0,0 +1,92 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#define error_here(status, errnum, ...) do { error_at_line(0, errnum, __FILE__, __LINE__, __VA_ARGS__); r = status; if (r) goto r; } while (0) +#define S0(x) (x ? x : "") +#define PKTSIZ 1024 +int main (int argc, char ** argv) { + struct bpf_program fp = { 0 }; + char pcaperr[PCAP_ERRBUF_SIZE]; + int r = 0; + pcap_if_t * vmesniki = NULL; + if (pcap_findalldevs(&vmesniki, pcaperr)) + error_here(1, 0, "pcap_findalldevs: %s", pcaperr); + if (!vmesniki) + error_here(2, 0, "ni vmesnikov za prisluškovanje -- zaženi airmon-ng?"); + int izbira = -1; + if (argc > 1) + izbira = atoi(argv[1]); + pcap_if_t * izbran = NULL; + int zap = 0; + for (pcap_if_t * vmesnik = vmesniki; vmesnik; vmesnik = vmesnik->next) { + printf("%d\t%s\t%s%s\n", zap, vmesnik->name, S0(vmesnik->description), zap == izbira ? "\tIZBRAN" : ""); + if (zap == izbira) + izbran = vmesnik; + zap++; + } + if (!izbran) + error_here(3, 0, "v 1. argument napiši zaporedno številko vmesnika iz seznama"); + pcap_t * pcap = pcap_create(izbran->name, pcaperr); + if (!pcap) + error_here(4, 0, "pcap_create(NULL, pcaperr): %s", pcaperr); + pcap_set_snaplen(pcap, PKTSIZ); + pcap_set_promisc(pcap, 1); + int ret = pcap_can_set_rfmon(pcap); + if (ret < 0) + error_here(5, 0, "pcap_can_set_rfmon(pcap): %s", pcap_geterr(pcap)); + if (ret) + pcap_set_rfmon(pcap, 1); + else + error_here(0, 0, "!pcap_can_set_rfmon(pcap)"); + pcap_set_immediate_mode(pcap, 1); + if ((ret = pcap_activate(pcap))) + error_here(6, 0, "pcap_activate(pcap): %s", pcap_statustostr(ret)); + if (pcap_setdirection(pcap, PCAP_D_IN)) + error_here(7, 0, "pcap_setdirection(pcap, PCAP_D_IN): %s", pcap_geterr(pcap)); + if (pcap_compile(pcap, &fp, "wlan addr1 4b:40:34:41:2e:53", 1, PCAP_NETMASK_UNKNOWN)) + error_here(8, 0, "pcap_compile(pcap, fp, ...): %s", pcap_geterr(pcap)); + if (pcap_setfilter(pcap, &fp)) + error_here(9, 0, "pcap_setfilter(pcap, fp): %s", pcap_geterr(pcap)); + const unsigned char * packet = NULL; + struct pcap_pkthdr * pkt_header = NULL; + int fd = pcap_get_selectable_fd(pcap); + if (fd == -1) + error_here(10, 0, "pcap_get_selectable_fd(pcap) == -1"); + struct pollfd pollfd = { + .fd = fd, + .events = POLLIN + }; + while (poll(&pollfd, 1, -1) != -1) { + if ((ret = pcap_next_ex(pcap, &pkt_header, &packet)) == 0) + error_here(11, 0, "pcap_next_ex(pcap, &pkt_header, &packet) == 0"); + if (ret == PCAP_ERROR) + error_here(12, 0, "pcap_next_ex(pcap, &pkt_header, &packet): %s", pcap_geterr(pcap)); +#define MAGIC "K@4A.SI " + const unsigned char * c = memmem(packet, PKTSIZ, MAGIC, strlen(MAGIC)); + if (!c) { + error_here(0, 0, "malformed packet"); + continue; + } + c += strlen(MAGIC); + if (c + 1 /* koliko bomo brali */ >= packet+PKTSIZ) { + error_here(0, 0, "malformed packet"); + continue; + } + fprintf(stderr, "\r"); + for (int i = 0; i < 128; i++) + fprintf(stderr, i < c[0]/2 ? "@" : " "); + fflush(stderr); + } + error_here(11, errno, "poll(&pollfd, 1, -1)"); + r: + pcap_freecode(&fp); + if (vmesniki) + pcap_freealldevs(vmesniki); + if (pcap) + pcap_close(pcap); + return r; +} -- cgit v1.2.3
"; $ok = false; foreach (["input", "name"] as $w) - if (!str_contains(strtolower($v), $w)) + if (strpos(strtolower($v), $w) === FALSE) $ok = true; if ($k == "slika" && !empty($v)) echo ""; - if ($k == "lastnik" && $v == $_SERVER['PHP_AUTH_USER']) { + if ($k == "lastnik" && $v == @$_SERVER['PHP_AUTH_USER']) { echo ""; echo ""; echo "