Discovery Service

From Nutwiki
Jump to: navigation, search

Test Environments

Hardware Comments Nut/OS
4.6.4
Nut/OS
4.7.4
Nut/OS
4.8.0
Ethernut 1.3 H OK
Binaries
OK
Binaries
OK
Binaries
Ethernut 2.1 B OK
Binaries
OK
Binaries
OK
Binaries
Ethernut 3.0 E Compiler
Error
OK
Binaries
OK
Binaries
EIR 1.0 C Set jumper JP1
to DEBUG mode.
Compiler
Error
OK
Binaries
OK
Binaries
Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0

Source Code

The code consists of two routines, the main routine and a discovery handler.

<source lang="c">

  1. include <dev/board.h>
  2. include <sys/timer.h>
  1. include <stdio.h>
  2. include <io.h>
  1. include <netinet/in.h>
  2. include <arpa/inet.h>
  1. include <pro/dhcp.h>
  2. include <pro/discover.h>

static NutDiscoveryCallback default_handler; static int MyHandler(uint32_t ip, uint16_t port, DISCOVERY_TELE * dist, int len);

int main(void) {

   u_long baud = 115200;

   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   puts("Discovery Service Sample");

   if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
       puts("Registering " DEV_ETHER_NAME " failed.");
   }
   else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
       uint8_t initial_mac[6] = { 0x00, 0x06, 0x98, 0x00, 0x00, 0x00 };
       if (NutDhcpIfConfig(DEV_ETHER_NAME, initial_mac, 0)) {
           u_long initial_ip_addr = inet_addr("192.0.0.192");
           u_long initial_ip_mask = inet_addr("255.255.255.0");
           NutNetIfConfig(DEV_ETHER_NAME, initial_mac, initial_ip_addr, initial_ip_mask);
       }
   }
   if (NutRegisterDiscovery(INADDR_BROADCAST, 0, DISF_INITAL_ANN)) {
       printf("Discover Service failed");
   } else {
       default_handler = NutRegisterDiscoveryCallback(MyHandler);
   }
   for (;;) {
       NutSleep(1000);
   }
   return 0;

} </source>

<source lang="c"> static int MyHandler(uint32_t ip, uint16_t port, DISCOVERY_TELE * dist, int len) {

   char * tt;
   switch (dist->dist_type) {
   case DIST_REQUEST:
       tt = "request";
       break;
   case DIST_ANNOUNCE:
       tt = "announce";
       break;
   case DIST_APPLY:
       tt = "apply";
       break;
   default:
       tt = "telegram";
       break;
   }
   printf("Discovery %s from %s\n", tt, inet_ntoa(ip));
   return (*default_handler)(ip, port, dist, len);

} </source>

Output

On serial output you will see something like:

Discovery Service Sample
Discovery request from 192.168.192.246

The Nut/OS Discoverer will display your (one or more) ethernut board in a table. By double clicking on the values, you can edit them and update your board.

See also