diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2023-04-28 21:17:02 +0200 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2023-04-28 21:17:02 +0200 |
commit | 97c5aa834b041bf52b1b9f508d58d39b03f70712 (patch) | |
tree | eb4278adb833fd86529eda097c471d299641ef22 /prog/upravljalnik | |
parent | razno (diff) | |
download | r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar.gz r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar.bz2 r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar.lz r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar.xz r-97c5aa834b041bf52b1b9f508d58d39b03f70712.tar.zst r-97c5aa834b041bf52b1b9f508d58d39b03f70712.zip |
Diffstat (limited to 'prog/upravljalnik')
-rw-r--r-- | prog/upravljalnik/naprava/.gitignore | 1 | ||||
-rw-r--r-- | prog/upravljalnik/naprava/platformio.ini | 22 | ||||
-rw-r--r-- | prog/upravljalnik/naprava/src/main.cpp | 68 | ||||
-rw-r--r-- | prog/upravljalnik/pcap.c | 92 |
4 files changed, 183 insertions, 0 deletions
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 = -<*> +<main.cpp> +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 <Arduino.h> +#include <ADS1115_WE.h> +#include <ESP8266WiFi.h> +#include <Wire.h> +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 <pcap/pcap.h> +#include <error.h> +#include <stdlib.h> +#include <poll.h> +#include <errno.h> +#include <string.h> +#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; +} |