Skip to content

Commit 5ca87f0

Browse files
committed
Add NetBIOS lib and fix CMake includes
1 parent 28e45a9 commit 5ca87f0

File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ set(CORE_SRCS
3434

3535
set(LIBRARY_SRCS
3636
libraries/ArduinoOTA/src/ArduinoOTA.cpp
37+
libraries/AsyncUDP/src/AsyncUDP.cpp
3738
libraries/BluetoothSerial/src/BluetoothSerial.cpp
3839
libraries/DNSServer/src/DNSServer.cpp
3940
libraries/EEPROM/EEPROM.cpp
4041
libraries/ESPmDNS/src/ESPmDNS.cpp
4142
libraries/FS/src/FS.cpp
4243
libraries/FS/src/vfs_api.cpp
4344
libraries/HTTPClient/src/HTTPClient.cpp
45+
libraries/NetBIOS/src/NetBIOS.cpp
4446
libraries/Preferences/src/Preferences.cpp
4547
libraries/SD_MMC/src/SD_MMC.cpp
4648
libraries/SD/src/SD.cpp
@@ -162,6 +164,7 @@ set(COMPONENT_ADD_INCLUDEDIRS
162164
variants/esp32/
163165
cores/esp32/
164166
libraries/ArduinoOTA/src
167+
libraries/AsyncUDP/src
165168
libraries/AzureIoT/src
166169
libraries/BLE/src
167170
libraries/BluetoothSerial/src
@@ -170,6 +173,7 @@ set(COMPONENT_ADD_INCLUDEDIRS
170173
libraries/ESPmDNS/src
171174
libraries/FS/src
172175
libraries/HTTPClient/src
176+
libraries/NetBIOS/src
173177
libraries/Preferences/src
174178
libraries/SD_MMC/src
175179
libraries/SD/src
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <WiFi.h>
2+
#include <NetBIOS.h>
3+
4+
const char* ssid = "............";
5+
const char* password = "..............";
6+
7+
void setup() {
8+
Serial.begin(115200);
9+
10+
// Connect to WiFi network
11+
WiFi.mode(WIFI_STA);
12+
WiFi.begin(ssid, password);
13+
Serial.println("");
14+
15+
// Wait for connection
16+
while (WiFi.status() != WL_CONNECTED) {
17+
delay(500);
18+
Serial.print(".");
19+
}
20+
Serial.println("");
21+
Serial.print("Connected to ");
22+
Serial.println(ssid);
23+
Serial.print("IP address: ");
24+
Serial.println(WiFi.localIP());
25+
26+
NBNS.begin("ESP");
27+
}
28+
29+
void loop() {
30+
31+
}

libraries/NetBIOS/keywords.txt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#######################################
2+
# Syntax Coloring Map For ESPNBNS
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
NetBIOS KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
begin KEYWORD2
16+
17+
#######################################
18+
# Instances (KEYWORD2)
19+
#######################################
20+
21+
NBNS KEYWORD2
22+
23+
#######################################
24+
# Constants (LITERAL1)
25+
#######################################

libraries/NetBIOS/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=NetBIOS
2+
version=1.0
3+
4+
maintainer=Hristo Gochkov<[email protected]>
5+
sentence=Enables NBNS (NetBIOS) name resolution.
6+
paragraph=With this library you can connect to your ESP from Windows using a short name
7+
category=Communication
8+
url=http://www.xpablo.cz/?p=751#more-751
9+
architectures=esp32

libraries/NetBIOS/src/NetBIOS.cpp

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include "NetBIOS.h"
2+
3+
#include <functional>
4+
5+
#define NBNS_PORT 137
6+
#define NBNS_MAX_HOSTNAME_LEN 32
7+
8+
typedef struct {
9+
uint16_t id;
10+
uint8_t flags1;
11+
uint8_t flags2;
12+
uint16_t qcount;
13+
uint16_t acount;
14+
uint16_t nscount;
15+
uint16_t adcount;
16+
uint8_t name_len;
17+
char name[NBNS_MAX_HOSTNAME_LEN + 1];
18+
uint16_t type;
19+
uint16_t clas;
20+
} __attribute__((packed)) nbns_question_t;
21+
22+
typedef struct {
23+
uint16_t id;
24+
uint8_t flags1;
25+
uint8_t flags2;
26+
uint16_t qcount;
27+
uint16_t acount;
28+
uint16_t nscount;
29+
uint16_t adcount;
30+
uint8_t name_len;
31+
char name[NBNS_MAX_HOSTNAME_LEN + 1];
32+
uint16_t type;
33+
uint16_t clas;
34+
uint32_t ttl;
35+
uint16_t data_len;
36+
uint16_t flags;
37+
uint32_t addr;
38+
} __attribute__((packed)) nbns_answer_t;
39+
40+
static void _getnbname(const char *nbname, char *name, uint8_t maxlen){
41+
uint8_t b;
42+
uint8_t c = 0;
43+
44+
while ((*nbname) && (c < maxlen)) {
45+
b = (*nbname++ - 'A') << 4;
46+
c++;
47+
if (*nbname) {
48+
b |= *nbname++ - 'A';
49+
c++;
50+
}
51+
if(!b || b == ' '){
52+
break;
53+
}
54+
*name++ = b;
55+
}
56+
*name = 0;
57+
}
58+
59+
static void append_16(void * dst, uint16_t value){
60+
uint8_t * d = (uint8_t *)dst;
61+
*d++ = (value >> 8) & 0xFF;
62+
*d++ = value & 0xFF;
63+
}
64+
65+
static void append_32(void * dst, uint32_t value){
66+
uint8_t * d = (uint8_t *)dst;
67+
*d++ = (value >> 24) & 0xFF;
68+
*d++ = (value >> 16) & 0xFF;
69+
*d++ = (value >> 8) & 0xFF;
70+
*d++ = value & 0xFF;
71+
}
72+
73+
void NetBIOS::_onPacket(AsyncUDPPacket& packet){
74+
if (packet.length() >= sizeof(nbns_question_t)) {
75+
nbns_question_t * question = (nbns_question_t *)packet.data();
76+
if (0 == (question->flags1 & 0x80)) {
77+
char name[ NBNS_MAX_HOSTNAME_LEN + 1 ];
78+
_getnbname(&question->name[0], (char *)&name, question->name_len);
79+
if (_name.equals(name)) {
80+
nbns_answer_t nbnsa;
81+
nbnsa.id = question->id;
82+
nbnsa.flags1 = 0x85;
83+
nbnsa.flags2 = 0;
84+
append_16((void *)&nbnsa.qcount, 0);
85+
append_16((void *)&nbnsa.acount, 1);
86+
append_16((void *)&nbnsa.nscount, 0);
87+
append_16((void *)&nbnsa.adcount, 0);
88+
nbnsa.name_len = question->name_len;
89+
memcpy(&nbnsa.name[0], &question->name[0], question->name_len + 1);
90+
append_16((void *)&nbnsa.type, 0x20);
91+
append_16((void *)&nbnsa.clas, 1);
92+
append_32((void *)&nbnsa.ttl, 300000);
93+
append_16((void *)&nbnsa.data_len, 6);
94+
append_16((void *)&nbnsa.flags, 0);
95+
nbnsa.addr = WiFi.localIP();
96+
_udp.writeTo((uint8_t *)&nbnsa, sizeof(nbnsa), packet.remoteIP(), NBNS_PORT);
97+
}
98+
}
99+
}
100+
}
101+
102+
NetBIOS::NetBIOS(){
103+
104+
}
105+
NetBIOS::~NetBIOS(){
106+
end();
107+
}
108+
109+
bool NetBIOS::begin(const char *name){
110+
_name = name;
111+
_name.toUpperCase();
112+
113+
if(_udp.connected()){
114+
return true;
115+
}
116+
117+
_udp.onPacket([](void * arg, AsyncUDPPacket& packet){ ((NetBIOS*)(arg))->_onPacket(packet); }, this);
118+
return _udp.listen(NBNS_PORT);
119+
}
120+
121+
void NetBIOS::end(){
122+
if(_udp.connected()){
123+
_udp.close();
124+
}
125+
}
126+
127+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_NETBIOS)
128+
NetBIOS NBNS;
129+
#endif
130+
131+
// EOF

libraries/NetBIOS/src/NetBIOS.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
#ifndef __ESPNBNS_h__
3+
#define __ESPNBNS_h__
4+
5+
#include <WiFi.h>
6+
#include "AsyncUDP.h"
7+
8+
class NetBIOS
9+
{
10+
protected:
11+
AsyncUDP _udp;
12+
String _name;
13+
void _onPacket(AsyncUDPPacket& packet);
14+
15+
public:
16+
NetBIOS();
17+
~NetBIOS();
18+
bool begin(const char *name);
19+
void end();
20+
};
21+
22+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_NETBIOS)
23+
extern NetBIOS NBNS;
24+
#endif
25+
26+
#endif

0 commit comments

Comments
 (0)