Skip to content

Commit

Permalink
Merge pull request #47 from cyberman54/development
Browse files Browse the repository at this point in the history
v1.3.2
  • Loading branch information
cyberman54 authored Apr 20, 2018
2 parents 9a035cd + 7f47829 commit 413b5f3
Show file tree
Hide file tree
Showing 12 changed files with 441 additions and 421 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,24 @@ Hardware dependent settings (pinout etc.) are stored in board files in /hal dire

These results where metered with software version 1.2.97 while continuously scanning wifi and ble, no LoRa TX’ing, OLED display (if present) on, 5V USB powered.

# Building
# Preparing

Use <A HREF="https://platformio.org/">PlatformIO</A> with your preferred IDE for development and building this code.
Before compiling the code,

Before compiling the code, **create file loraconf.h in your local /src directory** using the template [loraconf.sample.h](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/loraconf.sample.h) and populate it with your personal APPEUI und APPKEY for the LoRaWAN network. If you're using popular <A HREF="https://thethingsnetwork.org">TheThingsNetwork</A> you can copy&paste the keys from TTN console or output of ttnctl.
- **edit paxcounter.conf** and taylor settings in this file according to your needs and use case. Please take care of the duty cycle regulations of the LoRaWAN network you're going to use.

- **create file loraconf.h in your local /src directory** using the template [loraconf.sample.h](https://github.com/cyberman54/ESP32-Paxcounter/blob/master/src/loraconf.sample.h) and populate it with your personal APPEUI und APPKEY for the LoRaWAN network. If you're using popular <A HREF="https://thethingsnetwork.org">TheThingsNetwork</A> you can copy&paste the keys from TTN console or output of ttnctl.

To join the network only method OTAA is supported, not ABP. The DEVEUI for OTAA will be derived from the device's MAC adress during device startup and is shown as well on the device's display (if it has one) as on the serial console for copying it to your LoRaWAN network server settings.

If your device has a fixed DEVEUI enter this in your local loraconf.h file. During compile time this DEVEUI will be grabbed from loraconf.h and inserted in the code.

If your device has silicon **Unique ID** which is stored in serial EEPROM Microchip 24AA02E64 you don't need to change anything. The Unique ID will be read during startup and DEVEUI will be generated from it, overriding settings in loraconf.h.

# Building

Use <A HREF="https://platformio.org/">PlatformIO</A> with your preferred IDE for development and building this code.

# Uploading

To upload the code to your ESP32 board this needs to be switched from run to bootloader mode. Boards with USB bridge like Heltec and TTGO usually have an onboard logic which allows soft switching by the upload tool. In PlatformIO this happenes automatically.<p>
Expand Down
6 changes: 3 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ build_flags =
; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <---
; otherwise device may crash in dense environments due to serial buffer overflow
;
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
;
; override lora settings from LMiC library in lmic/config.h and use main.h instead
-D_lmic_config_h_
-include "src/main.h"
-include "src/paxcounter.conf"

[env:heltec_wifi_lora_32]
platform = espressif32
Expand Down
4 changes: 3 additions & 1 deletion src/blecsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,10 @@ esp_err_t register_ble_functionality(void)


// Main start code running in its own Xtask
void bt_loop(void *ignore)
void bt_loop(void * pvParameters)
{
configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check

esp_err_t status;

// Initialize BT controller to allocate task and other resource.
Expand Down
2 changes: 1 addition & 1 deletion src/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ esp_err_t err;

// populate cfg vars with factory settings
void defaultConfig() {
cfg.lorasf = LORASFDEFAULT; // 7-12, initial lora spreadfactor defined in main.h
cfg.lorasf = LORASFDEFAULT; // 7-12, initial lora spreadfactor defined in paxcounter.conf
cfg.txpower = 15; // 2-15, lora tx power
cfg.adrmode = 1; // 0=disabled, 1=enabled
cfg.screensaver = 0; // 0=disabled, 1=enabled
Expand Down
13 changes: 5 additions & 8 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

// std::set for unified array functions
#include <set>
#include <array>
#include <algorithm>
//#include <array>
//#include <algorithm>

#ifdef HAS_DISPLAY
// OLED Display
Expand All @@ -22,6 +22,7 @@

#include "rgb_led.h"
#include "macsniff.h"
#include "main.h"

// Struct holding devices's runtime configuration
typedef struct {
Expand All @@ -45,18 +46,14 @@ typedef struct {
extern configData_t cfg;
extern uint8_t mydata[];
extern uint64_t uptimecounter;
extern uint32_t currentMillis ;
extern osjob_t sendjob;
extern char display_lora[], display_lmic[];
extern int countermode, screensaver, adrmode, lorasf, txpower, rlim;
extern uint16_t macs_total, macs_wifi, macs_ble; // MAC counters
extern bool joinstate;
extern std::set<uint16_t> wifis;
extern std::set<uint16_t> macs;

#ifdef HAS_DISPLAY
extern HAS_DISPLAY u8x8;
#endif

#ifdef BLECOUNTER
extern int scanTime;
extern std::set<uint16_t> bles;
#endif
25 changes: 10 additions & 15 deletions src/lorawan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
// Local logging Tag
static const char *TAG = "lorawan";

// function defined in main.cpp
void set_onboard_led(int state);

// functions defined in rcommand.cpp
void rcommand(int cmd, int arg);
void switch_lora(int sf, int tx);
Expand Down Expand Up @@ -114,25 +111,23 @@ void do_send(osjob_t* j){
uint8_t mydata[4];
uint16_t data;
// Sum of unique WIFI MACs seen
data = (uint16_t) wifis.size();
mydata[0] = (data & 0xff00) >> 8;
mydata[1] = data & 0xff;
mydata[0] = (macs_wifi & 0xff00) >> 8;
mydata[1] = macs_wifi & 0xff;

#ifdef BLECOUNTER
// Sum of unique BLE MACs seen
data = (uint16_t) bles.size();
mydata[2] = (data & 0xff00) >> 8;
mydata[3] = data & 0xff;
// Sum of unique BLE MACs seen
mydata[2] = (macs_ble & 0xff00) >> 8;
mydata[3] = macs_ble & 0xff;
#else
mydata[2] = 0;
mydata[3] = 0;
mydata[2] = 0;
mydata[3] = 0;
#endif

// Total BLE+WIFI unique MACs seen
// TBD ?
//data = (uint16_t) macs.size();
//mydata[4] = (data & 0xff00) >> 8;
//mydata[5] = data & 0xff;
//mydata[4] = (macs_total & 0xff00) >> 8;
//mydata[5] = macs_total & 0xff;

// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Expand Down Expand Up @@ -167,7 +162,7 @@ void onEvent (ev_t ev) {

case EV_JOINED:
strcpy_P(buff, PSTR("JOINED"));
sprintf(display_lora, " "); // erase "Join Wait" message from display
sprintf(display_lora, ""); // erase "Join Wait" message from display
// Disable link check validation (automatically enabled
// during join, but not supported by TTN at this time).
LMIC_setLinkCheckMode(0);
Expand Down
53 changes: 23 additions & 30 deletions src/macsniff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,69 +13,62 @@ static const char *TAG = "macsniff";

static wifi_country_t wifi_country = {.cc=WIFI_MY_COUNTRY, .schan=WIFI_CHANNEL_MIN, .nchan=WIFI_CHANNEL_MAX, .policy=WIFI_COUNTRY_POLICY_MANUAL};

// globals
uint16_t salt;

uint16_t salt_reset(void) {
salt = random(65536); // get new 16bit random for salting hashes
uint16_t reset_salt(void) {
salt = random(65536); // get new 16bit random for salting hashes and set global salt var
return salt;
}

bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {

char buff[16]; // temporary buffer for printf
char buff[16]; // temporary buffer for printf
bool added = false;
uint32_t addr2int;
uint32_t vendor2int;
uint16_t hashedmac;
uint32_t addr2int, vendor2int; // temporary buffer for MAC and Vendor OUI
uint16_t hashedmac; // temporary buffer for generated hash value

// only last 3 MAC Address bytes are used for MAC Address Anonymization
// only last 3 MAC Address bytes are used for MAC address anonymization
// but since it's uint32 we take 4 bytes to avoid 1st value to be 0
addr2int = ( (uint32_t)paddr[2] ) | ( (uint32_t)paddr[3] << 8 ) | ( (uint32_t)paddr[4] << 16 ) | ( (uint32_t)paddr[5] << 24 );

#ifdef VENDORFILTER
vendor2int = ( (uint32_t)paddr[2] ) | ( (uint32_t)paddr[1] << 8 ) | ( (uint32_t)paddr[0] << 16 );
// use OUI vendor filter list only on Wifi, not on BLE
if ( (sniff_type==MAC_SNIFF_BLE) || std::find(vendors.begin(), vendors.end(), vendor2int) != vendors.end() ) {
if ( (sniff_type==MAC_SNIFF_BLE) || std::find(vendors.begin(), vendors.end(), vendor2int) != vendors.end() )
{
#endif

// salt and hash MAC, and if new unique one, store identifier in container and increment counter on display
// https://en.wikipedia.org/wiki/MAC_Address_Anonymization

addr2int += (uint32_t) salt; // add 16-bit salt to pseudo MAC
addr2int += (uint32_t)salt; // add 16-bit salt to pseudo MAC
snprintf(buff, sizeof(buff), "%08X", addr2int); // convert unsigned 32-bit salted MAC to 8 digit hex string
hashedmac = rokkit(&buff[3], 5); // hash MAC last string value, use 5 chars to fit hash in uint16_t container
auto newmac = macs.insert(hashedmac); // add hashed MAC to total container if new unique
added = newmac.second ? true:false; // true if hashed MAC is unique in container

// Insert only if it was not found on global count
// Count only if MAC was not yet seen
if (added) {

if (sniff_type == MAC_SNIFF_WIFI ) {
rgb_set_color(COLOR_GREEN);
wifis.insert(hashedmac); // add hashed MAC to wifi container
macs_wifi++; // increment Wifi MACs counter
if (joinstate)
blink_LED(COLOR_GREEN, 50, 0);
}
#ifdef BLECOUNTER
else if (sniff_type == MAC_SNIFF_BLE ) {
rgb_set_color(COLOR_MAGENTA);
bles.insert(hashedmac); // add hashed MAC to BLE container
macs_ble++; // increment BLE Macs counter
if (joinstate)
blink_LED(COLOR_MAGENTA, 50, 0);
}
#endif

// Not sure user will have time to see the LED
// TBD do light off further in the code
rgb_set_color(COLOR_NONE);
}

ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s",
sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH",
rssi, buff, hashedmac,
(int) wifis.size(),
#ifdef BLECOUNTER
(int) bles.size(),
#else
0,
#endif
added ? "new" : "known");

// Log scan result
ESP_LOGI(TAG, "%s RSSI %ddBi -> MAC %s -> Hash %04X -> WiFi:%d BLTH:%d %s",
sniff_type==MAC_SNIFF_WIFI ? "WiFi":"BLTH",
rssi, buff, hashedmac, macs_wifi, macs_ble,
added ? "new" : "known");

#ifdef VENDORFILTER
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/macsniff.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef struct {
uint8_t payload[0]; /* network data ended with 4 bytes csum (CRC32) */
} wifi_ieee80211_packet_t;

uint16_t salt_reset(void);
uint16_t reset_salt(void);
void wifi_sniffer_init(void);
void wifi_sniffer_set_channel(uint8_t channel);
void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type);
Expand Down
Loading

0 comments on commit 413b5f3

Please sign in to comment.