Skip to content

Commit

Permalink
Add MDNS wrapper
Browse files Browse the repository at this point in the history
-Add WebServerMDNS example
-Update keywords
  • Loading branch information
vjmuzik committed Jun 29, 2020
1 parent f6a702e commit 35148d4
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 1 deletion.
73 changes: 73 additions & 0 deletions examples/WebServerMDNS/WebServerMDNS.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "NativeEthernet.h"

uint8_t mac[6];
void teensyMAC(uint8_t *mac) {
for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF;
for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF;
Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

EthernetServer server(80);

void setup() {
// put your setup code here, to run once:
teensyMAC(mac);
Ethernet.begin(mac);
server.begin();
MDNS.begin("Teensy41");
MDNS.addService("_http._tcp", 80);
Serial.print("IP address:");
Serial.println(Ethernet.localIP());
}

void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
8 changes: 8 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Ethernet KEYWORD1 Ethernet
EthernetClient KEYWORD1 EthernetClient
EthernetServer KEYWORD1 EthernetServer
IPAddress KEYWORD1 EthernetIPAddress
EthernetMDNS KEYWORD1
MDNS KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
Expand Down Expand Up @@ -51,6 +53,12 @@ setDnsServerIP KEYWORD2
setRetransmissionTimeout KEYWORD2
setRetransmissionCount KEYWORD2
setConnectionTimeout KEYWORD2
addService KEYWORD2
removeService KEYWORD2
setServiceName KEYWORD2
setStackHeap KEYWORD2
setSocketSize KEYWORD2
setSocketNum KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=NativeEthernet
version=1.0.1
version=1.0.2
author=Various (see AUTHORS file for details)
maintainer=Tino Hernandez <vjmuzik1@gmail.com>
sentence=Enables network connection (local and Internet) using the Native Ethernet on Teensy 4.1.
Expand Down
19 changes: 19 additions & 0 deletions src/NativeEthernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,26 @@ class EthernetServer : public Server {
};


class EthernetMDNS{
public:
void begin(const char* host_name, uint8_t num_services = 1);
void addService(const char* service_type, uint16_t service_port, const fnet_mdns_txt_key_t* (*service_get_txt)(void) = EmptyTXTRecord);
void removeService(const char* service_type);
void setServiceName(const char* service_name);
protected:
private:
fnet_mdns_desc_t mdns_desc;
fnet_mdns_service_desc_t *service_desc;
uint8_t num_service_desc;
static const fnet_mdns_txt_key_t* EmptyTXTRecord(){
static const fnet_mdns_txt_key_t NullTXTRecord[] = {
{0,0}
};
return NullTXTRecord;
}
};

extern EthernetMDNS MDNS;



Expand Down
80 changes: 80 additions & 0 deletions src/NativeMdns.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* Copyright 2020 Tino Hernandez (vjmuzik)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <Arduino.h>
#include "NativeEthernet.h"
#include "NativeMdns.h"

void EthernetMDNS::begin(const char* host_name, uint8_t num_services){
fnet_mdns_params_t mdns_params = {
.netif_desc = fnet_netif_get_default(),
.addr_family = AF_INET,
.rr_ttl = FNET_CFG_MDNS_RR_TTL,
.name = host_name
};
mdns_desc = fnet_mdns_init(&mdns_params);
if(mdns_desc == 0){
Serial.println("Failed to begin MDNS");
return;
}
fnet_mdns_service_t mdns_service;
mdns_service.name = (char*)host_name;
fnet_mdns_service_update_name(mdns_desc, &mdns_service);
service_desc = new fnet_mdns_service_desc_t[num_services];
num_service_desc = num_services;
for(uint8_t i = 0; i < num_service_desc; i++){
service_desc[i] = 0;
}
}

void EthernetMDNS::addService(const char* service_type, uint16_t service_port, const fnet_mdns_txt_key_t* (*service_get_txt)(void)){
fnet_mdns_service_t mdns_service;
mdns_service.service_type = service_type;
mdns_service.service_port = FNET_HTONS(service_port);
mdns_service.service_get_txt = service_get_txt;
for(uint8_t i = 0; i < num_service_desc; i++){
if(service_desc[i] == 0){
service_desc[i] = fnet_mdns_service_register(mdns_desc, &mdns_service);
if(service_desc[i] == 0){
Serial.println("Failed to add MDNS Service");
}
return;
}
}
Serial.println("Unable to add more MDNS Services");
}

void EthernetMDNS::removeService(const char* service_type){
fnet_mdns_service_desc_t srv_desc = fnet_mdns_service_get_by_type(mdns_desc, service_type);
fnet_mdns_service_unregister(srv_desc);
for(uint8_t i = 0; i < num_service_desc; i++){
if(service_desc[i] == srv_desc){
service_desc[i] = 0;
}
}
}

void EthernetMDNS::setServiceName(const char* service_name){
fnet_mdns_service_t mdns_service;
mdns_service.name = (char*)service_name;
fnet_mdns_service_update_name(mdns_desc, &mdns_service);
}

EthernetMDNS MDNS;
21 changes: 21 additions & 0 deletions src/NativeMdns.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Copyright 2020 Tino Hernandez (vjmuzik)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "NativeEthernet.h"

0 comments on commit 35148d4

Please sign in to comment.