commit d2d6d477ee0a2ed0d489f178eacc30e15af4376d
parent 21392f0bcccfe211b0b9a0b28b898624640da6a1
Author: Bharatvaj H <bharatvaj@yahoo.com>
Date: Sat, 29 Jun 2019 02:37:31 +0530
parent 21392f0bcccfe211b0b9a0b28b898624640da6a1
Author: Bharatvaj H <bharatvaj@yahoo.com>
Date: Sat, 29 Jun 2019 02:37:31 +0530
Add Documentation
4 files changed, 518 insertions(+), 19 deletions(-)
A
|
143
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
|
156
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A
|
158
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/BLUETOOTH_IMPLEMENTATION.md b/BLUETOOTH_IMPLEMENTATION.md @@ -0,0 +1,143 @@ +### Bluetooth Implementation + +#### Network Module +```cpp +class Bluetooth : public Network { +private: + bdaddr_t inc = ((bdaddr_t){{0, 0, 0, 0, 0, 0}}); + +public: + Bluetooth() {} + + bool init() override { + int s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + close(s); + return !(s < 0); + } + + std::string getUserFriendlyName() override { return _BLUETOOTH_H; } + + int listen(std::string networkParams) { + struct sockaddr_rc loc_addr = {0}, rem_addr = {0}; + xs_SOCKET s; + socklen_t opt = sizeof(rem_addr); + + // allocate socket + s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + if (s < 0) { + throw s; + } + + // bind socket to port 1 of the first available + // local bluetooth adapter + loc_addr.rc_family = AF_BLUETOOTH; + loc_addr.rc_bdaddr = inc; + loc_addr.rc_channel = (uint8_t)6; + ::bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); + + // put socket into listening mode + ::listen(s, 1); + + // accept one connection + xs_SOCKET c = accept(s, (struct sockaddr *)&rem_addr, &opt); + Device *d = (Device *)new BluetoothDevice(c); + d->address = "B8:27:EB:A5:A1:70"; + devices.push_back(d); + close(s); + if (c == SOCKET_ERROR) { + return -1; + } + deviceConnected(*d); + return 0; + } + + Device *connect(std::string conParams) { + struct sockaddr_rc addr = {0}; + xs_SOCKET s; + const char *dest = conParams.c_str(); + // allocate a socket + s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + + // set the connection parameters (who to connect to) + addr.rc_family = AF_BLUETOOTH; + addr.rc_bdaddr = inc; + addr.rc_channel = (uint8_t)1; + str2ba(dest, &addr.rc_bdaddr); + + // connect to server + int status = ::connect(s, (struct sockaddr *)&addr, sizeof(addr)); + Device *d = (Device *)new BluetoothDevice(s); + perror("socket"); + if(status < 0){ + return nullptr; + } + return d; + } + + int bind(std::string bindParams) override { + return -1; + } + + int unbind(std::string bindParams) override { + return -1; + } + + // TODO + std::vector<DeviceInfo> discover() override { + std::vector<DeviceInfo> values; + inquiry_info *ii = NULL; + int max_rsp, num_rsp; + int dev_id, sock, len, flags; + int i; + char addr[19] = {0}; + char name[248] = {0}; + + dev_id = hci_get_route(NULL); + sock = hci_open_dev(dev_id); + if (dev_id < 0 || sock < 0) { + perror("opening socket"); + exit(1); + } + + len = 8; + max_rsp = 255; + flags = IREQ_CACHE_FLUSH; + ii = (inquiry_info *)malloc(max_rsp * sizeof(inquiry_info)); + + num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags); + if (num_rsp < 0) + perror("hci_inquiry"); + + for (i = 0; i < num_rsp; i++) { + ba2str(&(ii + i)->bdaddr, addr); + hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name), name, 0); + DeviceInfo ni; + ni.name = name; + values.push_back(ni); + } + + free(ii); + close(sock); + return values; + } +}; +``` + +#### Device Module +```cpp +class BluetoothDevice : Device { +private: + xs_SOCKET s; + +public: + BluetoothDevice(xs_SOCKET s) : s(s) {} + + int write(char *str, int bytesToSend) override { + return ::write(s, str, bytesToSend); + } + + int read(char *str, int bytesToRead) override { + return ::read(s, str, bytesToRead); + } +}; +```
diff --git a/IMPLEMENTATION.md b/IMPLEMENTATION.md @@ -0,0 +1,156 @@ +### Core Implementation +#### Network Module +```cpp +// initialize the module +virtual bool init(); +// returns a human readable user friendly name +virtual std::string getUserFriendlyName(); +// listen +virtual int listen(std::string); +// connects to the given address +virtual Device *connect(std::string); +// bind a route to system +virtual int bind(std::string); +// unbind a route from the table +virtual int unbind(std::string); +// discovers different networks +virtual std::vector<DeviceInfo> discover(); +// should be deprecated +std::vector<Device *> getDevices(); +``` + +#### Device Module +```cpp +// disconnects the device from the network +virtual void disconnect(); +// read from the connected device +virtual int read(char *message, int bytesToRead); +// write to the connected device +virtual int write(char *message, int bytesToWrite); +``` + +### Core Routing and Message Transmission + +#### Routing +`BindInfo` holds the binding Information +```cpp +class BindInfo { + std::string networkName; + std::string params; + Device *device; +}; +``` + +`NetworkController` is guided by the rules set in place by the `nBindMap`. +```cpp +std::vector<std::map<BindInfo *, BindInfo *>> nBindMap; +``` + +`bind` is called for updating the routing table +```cpp +void bind(std::string n1, std::string cp1, std::string n2, std::string cp2) { + Network *net1 = activeNetworks[n1]; + Network *net2 = activeNetworks[n2]; + if (net1 == nullptr && net2 == nullptr) { + // network not recognized + return; + } else if (net1 == nullptr || net2 == nullptr) { + // network not available + return; + } + if (net1->bind(cp1) < 0) { + // net1: param error + return; + } + if (net2->bind(cp2) < 0) { + // net2: param error + net1->unbind(cp1); + return; + } + BindInfo *bi1 = new BindInfo(); + bi1->networkName = n1; + bi1->params = cp1; + BindInfo *bi2 = new BindInfo(); + bi2->networkName = n2; + bi2->params = cp2; + + bind(bi1, bi2); + if (!start(bi1)) { + //server start error + unbind(bi1); + return; + } +} +``` + +`start` initializous the local hardware and is responsible for arranging message transmission. + +```cpp + + bool start(BindInfo *bi2) { + Network *n = activeNetworks[bi2->networkName]; + if (n == nullptr) { + return false; + } + std::string nParams = bi2->params; + n->setOnDeviceConnected([&](Device &d) { + BindInfo *bi = getPairOf(&d); + if (bi == nullptr) { + // invalid route + return; + } + Network *destNetwork = activeNetworks[bi->networkName]; + if (destNetwork == nullptr) { + // invalid route + return; + } + Device *d2 = destNetwork->connect(bi->params); + if (d2 == nullptr) { + // destination not found + d.disconnect(); + return; + } + Forwarder *forwarder = new Forwarder(&d, d2); + forwarder->start(); + forwarders.push_back(forwarder); + }); + return !n->listen(nParams); +} +``` + + Once connection is established, the connected device references are delegated to the `Forwarder` for message transmission. + +#### Message Transmission + +`DeviceInfo` is an container for storing information of a device which is later used by the Forwarder. + ```cpp + class DeviceInfo { + public: + std::string address; + std::string name; + Network *network; + }; + ``` + +`Forwarder` performs a simple bi-directional message transmission. + + ```cpp + t1 = new std::thread([&]() { + char *str = (char *)calloc(sizeof(char), BUF_SIZE); + int bytesRead = 0; + while ((bytesRead = lp->read(str, BUF_SIZE - 1)) > 0) { + if (rp->write(str, bytesRead) < 0) { + break; + }; + } + }); + t2 = new std::thread([&]() { + char *str = (char *)calloc(sizeof(char), BUF_SIZE); + int bytesRead = 0; + while ((bytesRead = rp->read(str, BUF_SIZE - 1)) > 0) { + if (lp->write(str, bytesRead) < 0) { + break; + } + } + }); + ```
diff --git a/IP_IMPLEMENTATON.md b/IP_IMPLEMENTATON.md @@ -0,0 +1,158 @@ +### IP +#### Network Module + +```cpp +class IP : public Network { + + std::string networkParams; + int configPort = 2500; + +public: + IP() {} + + bool init() override { + return true; + struct ifreq ifr; + int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + memset(&ifr, 0, sizeof(ifr)); + strcpy(ifr.ifr_name, networkParams.c_str()); + if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { + perror("SIOCGIFFLAGS"); + } + close(sock); + return !!(ifr.ifr_flags & IFF_UP); + } + + std::string getUserFriendlyName() override { return _IP_H; } + + xs_SOCKET start_server(bool isTcp, int port) { + static xs_SOCKET servfd; + + struct sockaddr_in server, client; + socklen_t cli_size; + + if (comm_check_port(port) < 0) { + return -1; + } + // Prepare the sockaddr_in structure + server.sin_family = AF_INET; + server.sin_addr.s_addr = INADDR_ANY; + server.sin_port = htons(port); + cli_size = sizeof(struct sockaddr_in); + + // Create socket + if (isTcp) { + servfd = socket(AF_INET, SOCK_STREAM, 0); + } else { + servfd = socket(AF_INET, SOCK_DGRAM, 0); + } + if (servfd == SOCKET_ERROR) { + clog_e(_COMM, "could not create socket"); + return -1; + } + // Bind + if (::bind(servfd, (struct sockaddr *)&server, sizeof(server)) < 0) { + clog_e(_COMM, "bind failed"); + return -1; + } + // Listen + ::listen(servfd, COMM_SERV_BACKLOG); + // Accept and incoming connection + clog_i(_COMM, "Waiting for incoming connections..."); + // accept connection from an incoming client + xs_SOCKET clifd = accept(servfd, (struct sockaddr *)&client, &cli_size); + if (clifd == SOCKET_ERROR) { + clog_i(_COMM, "Accept failed"); + return -1; + } + clog_i(_COMM, "Connection accepted"); + return clifd; + } + + int bind(std::string params) override { + int stat = system( + fmt::format("ip address add {} dev {}", params, getNetworkParams()) + .c_str()); + if (stat != 0) { + Log::e("Operation failed"); + return -1; + } + return 0; + } + + int unbind(std::string params) override { + return -1; + } + + int listen(std::string networkParams) override { + cout << "IP listening" << endl; + struct sockaddr_in peer; + socklen_t peerLen = sizeof(peer); + static int cont; + bool isTcp = true; + xs_SOCKET fd = start_server(isTcp, 2500); + + if (fd < 0) { + cout << "Socket error" << endl; + } + cout << fd << endl; + getsockname(fd, (struct sockaddr *)&peer, &peerLen); + Device *d = (Device *)new IPDevice(fd); + // FIXME NAMING ERROR + d->name = "IPDevice " + std::to_string(devices.size()); + d->address = inet_ntoa(peer.sin_addr); + devices.push_back(d); + + if (fd == SOCKET_ERROR) { + return -1; + } + deviceConnected(*d); + return 0; + } + + int addDevice(std::string ipaddress) { return -1; } + + void setNetworkParams(std::string networkParams) override { + Network::setNetworkParams(networkParams); + } + + Device *connect(std::string conParams) override { + xs_SOCKET fd = ::comm_connect_server(conParams.c_str(), 21); + if (fd == SOCKET_ERROR) { + return nullptr; + } + devices.push_back((Device *)new IPDevice(fd)); + return (Device *)new IPDevice(0); + } + + std::vector<DeviceInfo> discover() override { + return std::vector<DeviceInfo>(); + } +}; +``` + +#### Device Module +```cpp +class IPDevice : Device { +private: + xs_SOCKET fd; + bool isTcp; + +public: + IPDevice(xs_SOCKET fd) : fd(fd), isTcp(true) {} + + int read(char *str, int bytesToRead) override { + return ::read(fd, str, bytesToRead); + } + + int write(char *str, int bytesToSend) override { + return ::write(fd, str, bytesToSend); + } + + void disconnect(){ + ::close(fd); + } + + ~IPDevice() { comm_close_socket(fd); } +}; +```
diff --git a/README.md b/README.md @@ -1,19 +1,61 @@ -# Wireless Adapter - -## Installation -### Dependencies -wifi-scan - https://github.com/bmegli/wifi-scan -libcomm - https://github.com/bharatvaj/libcomm - -```c -$ git clone https://bitbucket.org/bharatvaj/forwarder -$ cd forwarder/ -$ sh depinstall.sh -$ mkdir build -$ cd build/ -$ cmake .. -$ make -``` - -## Note -Forwarder should run with root previlages for wifi interface +# Universal Network Gateway + +## Abstract +The system proposed provides a modular abstract model to which any existing network which supports routing and message transmission can be added as module. The devices of different networks are connected to the adapter in star topology. Network modules provide routing functionality. Device modules provide messaging functionality such as send and receive. The routing inside the system is guided by multiple routing tables and entries are added using a bind function. + +## Background +Transforming real world objects into virtual nodes that can be controlled by the user can be a real challenge considering the problem of interoperability. Internet of things aims to unify every devices under a single infrastructure. WiFi was the de-facto standard for wireless technology for more than a decade for the internet. With growing number of wireless specifications such as Bluetooth, ZigBee, Thread, Z-Wave, etc. There is hardly any effort for having any compatibility between them to achieve this unified behavior. Further, even though ZigBee and Z-Wave are mostly targeted towards IoT. It does not solve the compatibility problem. And also there will be no wireless technology which is perfect for every use case. Hypothetically speaking even if there is a perfect wireless technology, there is always “The Hipster Effect”(Jonathan Touboul, 2014).By introducing an adapter, we can solve this issue by connecting nodes operating in different wireless specifications. + +## Specification +### Novel Features +A bridge between devices of heterogeneous networks are created to pave the future for IoT. Traditionally the bridges are configured in the same system where a certain redirection of network is required. We are proposing a transparent bridge where the devices involved has no idea that it is contacting with devices of other networks. + +### Drawbacks of Existing +#### IP Standardization +Interoperability between devices have been achieved by using same networks for various physical devices. For example, IP network can be used for Ethernet, WiFi and Bluetooth. + +Drawbacks are, +* IP does not support ZigBee or ZWave and many other devices. +* Even support is brought in the future, new X/IP devices will not be able to work with old X/X devices, where X is a physical hardware such as ZigBee or ZWave. + +#### Blu-Fi +Product called Blu-Fi is already in market which connects Bluetooth and WiFi devices using a modem-like Blu-Fi adapter. + +The drawbacks are, +* Existing systems cannot be used or can be used only to a certain extent. +* There is very low coherence between the adapter and the devices. +* Applications are limited to tools offered by the company which offers Blu-Fi. +* Wildcard operations are not possible. An example would be connecting two different MQTT-SN devices under different networks. + +## Detailed Description +The `adapter` is collectively the software `modules` and hardware required to convert relay messages from one network to another. + +There are two phases in the working of adapter. + +### Phase One +--- +#### Detect +Forwarder supports various network technologies and also new network technologies can be added by adding the corresponding module. But due to cost and requirement, only some of the networks are actually needed. Detection of hardware devices are performed to load the corresponding software modules. For instance, in a adapter with a WiFi and a Bluetooth hardware loading extra modules such as ZigBee and ZWave will only slow the adapter. + +#### Select +Even if the hardware component is present in the adapter. The power to use that component resides in software modules. This is controlled by the user of the adapter. + +#### Bind +Once the networks which are needed are selected. The individual devices which is needed to register with other networks are specified. This step should be done for each devices and for each network. (i.e) If there are N networks and there are D devices in each network. D devices in each network should be configured to work with other N-1 networks or as need by the user. Since this process is systematic, it can be easily automated by scripts or can configured by the user as needed. Thus providing convenience and flexibility. + +### Phase two +--- +#### Listen +When a device is binded, the forwarder starts a listen instance. Whenever a bind is configured for a device with Network `A` to another device with Network `B`, Forwarder starts listening for `B` devices. When a device `C` tries to access `B`, connect instance of `A` is called. + +#### Connect +`A` is the device with true address, whereas the device `B` is pseudo. Connection to `A` is done with the forwarder. + +#### Forward +Adapter has the connection with `A` and `C`. This leaves us with stripping and repackaging +##### Strip +The `message` is the data which is to reach the end system. Suppose `C` is the first one to send message to `A`. `C` sends the message to adapter. Adapter strips the `C` related network headers and network related encryption, and stores the message. + + +##### Repackage +The message which is free of all the network headers and network related encryption, it is repackaged with `A` related network headers and encryption if any.
: bharatvaj/forwarder