Python code to configure B.A.T.M.A.N mesh node in Raspberry pi

 cat set_mesh_batman_node.py 
import fcntl, socket, struct
import os
import time

'''
GLOBAL VARIABLES
'''
# In case you are using any other dongle other than Edimax Wi-Fi dongle,
# replace this with the OUI of that dongle's company. For my project I 
# am using Edimax Wi-Fi Dongle.
EDIMAX_MAC_OUI    = "74:da:38"
MESH_NODE_IP_ADDR = "172.27.0.1/16
MESH_CELL_ADDRESS = "00:60:1D:01:23:45"

####################################################################
# Function Name: getHwAddr
#
# Description: Get Hardware/MAC address of the interfaces
####################################################################
def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    return ':'.join(['%02x' % ord(char) for char in info[18:24]])

#################################################################### 
# Step 1:   
#################################################################### 
# Fetch the mac address of the Wireless interface so that 
# 'batman' mesh can be programmed for the 'Edimax' Wi-Fi
# interface that supports "adhoc" mode.
wlan0_mac = getHwAddr('wlan0')
wlan1_mac = getHwAddr('wlan1')


#################################################################### 
# Step 2: Get the interface name of Edimax Wi-Fi USB dongle that 
# supports 'adhoc' mode.
#################################################################### 
# Fetch the mac address of the Wireless interface so that 
if EDIMAX_MAC_OUI in wlan0_mac:
    adhoc_wifi_inf = "wlan0"
    adhoc_wifi_inf_mac = wlan0_mac

if EDIMAX_MAC_OUI in wlan1_mac:
    adhoc_wifi_inf = "wlan1"
    adhoc_wifi_inf_mac = wlan1_mac

print ('Wi-Fi interface name on Raspberry'
       ' pi that supports adhoc mode: [%s] MAC: [%s]'
       %(adhoc_wifi_inf, adhoc_wifi_inf_mac))

# Activate batman-adv
os.system("sudo modprobe batman-adv")

# Disable and configure Wi-Fi interface to adhoc mode.
os.system("sudo ip link set %s down"         %(adhoc_wifi_inf))
os.system("sudo ifconfig %s mtu 1532"        %(adhoc_wifi_inf))
os.system("sudo iwconfig %s mode ad-hoc"     %(adhoc_wifi_inf))
os.system("sudo iwconfig %s essid neel_mesh" %(adhoc_wifi_inf))
os.system("sudo iwconfig %s ap any"          %(adhoc_wifi_inf))
os.system("sudo iwconfig %s channel 8"       %(adhoc_wifi_inf))

# All the nodes in the mesh network should have same CELL address.
# From the man page
# Force the card to register to the access point given by the address, 
#if it is possible.
os.system("sudo iwconfig %s ap %s"           %(adhoc_wifi_inf, 
                                               MESH_CELL_ADDRESS))

time.sleep(1)

os.system("sudo ip link set %s up" %(adhoc_wifi_inf))

time.sleep(1)

# add wlan interface to bat interface    
os.system("sudo batctl if add %s" %(adhoc_wifi_inf))
time.sleep(1)
os.system("sudo ifconfig bat0 up")
time.sleep(5)

# Assign ip address for bat interface which is the mesh interface    
os.system("sudo ifconfig bat0 %s" %(MESH_NODE_IP_ADDR))
x

Steps to build mesh network on Raspberry pi

working commands to enable adhoc in Raspberry pi with edimax USB Wi-Fi dongle:

https://wiki.debian.org/WiFi/AdHoc


Some other links:



@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@             Steps to build mesh network on Raspberry pi                    @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@



1.
ethernet ip address for Ethernet ports on all mesh nodes:

10.10.1.10/24

the ethernet ports for mesh nodes are used as console ports to configure
the mesh.


in Mesh, we have "routing capabilities at the MAC layer".
Path selection is used to refer to MAC-addressbased routing and
to differentiate it from conventional IP routing.

########################
pre-requisites:
########################
a. enable vnc server in raspberry pi using "sudo raspi-config" command.
b. in this file /etc/dhcpcd.conf, add the below. This is to ensure that the
ethernet port of raspberry pi is always configured with static IP. Since,
mesh network should have a minimum for 4 nodes, it would be easy to login
into raspberry pi via back to back pi to laptop ethernet port. The laptop's
IP should be also configured in the same network. do the same for all the
mesh node device.

IN short, one laptop can be used to log into every raspberry pi (mesh node) !!!!!

# Example static IP configuration:
interface eth0
static ip_address=10.10.1.10/24
static ip6_address=fd51:42f8:caae:d92e::ff/64
static routers=10.10.1.1
static domain_name_servers=10.10.1.1 8.8.8.8 fd51:42f8:caae:d92e::1


########################
Actual Steps:
########################




1. buy  Wi-fi adapter "Edimax EW-7811Un Wi-Fi USB Adapter"


2. comment out this line from raspberry pi blacklist for enabling mesh (80211s
) for the above mentioned Wi-Fi chipset.

pi@raspberrypi:~ $ cat /etc/modprobe.d/blacklist-rtl8192cu.conf
#blacklist rtl8192cu
This device also supports monitor mode.




@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
how to check whether the required drivers for edimax are installed or not.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
pi@raspberrypi:~ $ lsmod | grep 8192cu
rtl8192cu              90112  0
rtl_usb                20480  1 rtl8192cu             
rtl8192c_common        69632  1 rtl8192cu         <---------------------------------------------
rtlwifi               114688  3 rtl_usb,rtl8192c_common,rtl8192cu
mac80211              692224  3 rtl_usb,rtlwifi,rtl8192cu



This site has list of wifi adapters:
https://github.com/phillymesh/802.11s-adapters

3.

Now that the wifi driver (edimax EW-7811Un) supports mesh, we are good to program the
mesh ESSID on each of the raspberry pi.

su
ifconfig wlan0 down
iwconfig wlan0 channel 1 essid neel_mesh mode ad-hoc
ifconfig wlan0 192.168.13.100 netmask 255.255.255.0
ifconfig wlan0 up

su
ifconfig wlan0 down
iwconfig wlan0 channel 1 essid neel_mesh mode ad-hoc
ifconfig wlan0 192.168.13.101 netmask 255.255.255.0
ifconfig wlan0 up


su
ifconfig wlan0 down
iwconfig wlan0 channel 1 essid neel_mesh mode ad-hoc
ifconfig wlan0 192.168.13.102 netmask 255.255.255.0
ifconfig wlan0 up


su
ifconfig wlan0 down
iwconfig wlan0 channel 1 essid neel_mesh mode ad-hoc
ifconfig wlan0 192.168.13.103 netmask 255.255.255.0
ifconfig wlan0 up


4.
Check whether the SSID that the node is transmitting is visible in the scan

iwlist wlan0 scan | grep ESSID




Raspberry PI Mesh weblinks

USING YOUR NEW RASPBERRY PI 3 AS A WIFI ACCESS POINT WITH HOSTAPD


https://frillip.com/using-your-raspberry-pi-3-as-a-wifi-access-point-with-hostapd/

Ad-Hoc Network + Raspberry Pi

https://classes.engineering.wustl.edu/ese205/core/index.php?title=Ad-Hoc_Network_%2B_Raspberry_Pi


Wi-Fi Basics Weblinks

https://www.electronics-notes.com/articles/connectivity/wifi-ieee-802-11/what-is-wifi.php
http://www.revolutionwifi.net/revolutionwifi/2013/03/80211ac-channel-planning.html

Wi-Fi Raspberry Pi create Ad-hoc network

https://neelquestforlearning.blogspot.com/2019/06/how-to-create-adhoc-networks-using.html?_sm_au_=iQHf7w5TH77Ws73r

Mesh Raspberry Pi Git HUB

https://github.com/suiluj/pi-adhoc-mqtt-cluster/wiki
https://mjuenema.github.io/80211s_wireless_mesh/


How to find out whether the Wi-Fi Hardware on Pi supports 80211s or Mesh ?

https://github.com/phillymesh/802.11s-adapters   (List of adapters that support Mesh)

How to configure Raspberry pi  wireless Mesh network

https://classes.engineering.wustl.edu/ese205/core/index.php?title=Ad-Hoc_Network_%2B_Raspberry_Pi

https://raspberrypi.stackexchange.com/questions/49660/ad-hoc-setup-in-rpi-3

https://hackaday.com/2012/11/14/mesh-networking-with-multiple-raspberry-pi-boards/

http://scalabilly.com/2015/08/mesh-ad-hoc-network-on-multiple-raspberry-pis/      (good site)

https://www.reddit.com/r/raspberry_pi/comments/8sd7no/raspberry_pi_mesh_network_with_at_least_one_super/

https://tomesh.net/build-a-node/

http://www.techinterchange.com.au/howto-setup-raspberry-pi-3-with-mesh-wireless-networking-using-b-a-t-m-a-n/

How to check whether mesh is supported in the Raspberry Pi Hardware ? 

For mesh, we need to check the capability of hardware(wifi chip) and its wlan driver. Wifi driver has to support that mesh point support. I hope in Raspberry Pi, it has Broadcom wifi driver i.e brcmfmac(broadcom fullmac). In fullmac there is no mac80211 implementation. MLME(MAC sub Layer Management Entity) is implemented in software i.e mac80211. Even 802.11simplementation is in mac80211. since you are using the Raspberry Pi which has brcmfmac driver, can't support 802.11s.