commit 2e59a58fa678be18fb8617af42634b113eb5247a
parent 5f8b8b727a20febd8572288abe3a6ea0abde1df9
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Thu, 28 Mar 2019 19:50:57 +0000
parent 5f8b8b727a20febd8572288abe3a6ea0abde1df9
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Thu, 28 Mar 2019 19:50:57 +0000
Updated system call with GPIO library
5 files changed, 174 insertions(+), 50 deletions(-)
M
|
85
+++++++++++++++++++++++++++++++++----------------------------------------------
diff --git a/Bluetooth.hpp b/Bluetooth.hpp @@ -0,0 +1,61 @@ +#ifndef _BLUETOOTH_H +#define _BLUETOOTH_H +#include <bluetooth/bluetooth.h> +#include <bluetooth/rfcomm.h> +#include <crosssocket.h> +#include <functional> +bdaddr_t inc = ((bdaddr_t){{0, 0, 0, 0, 0, 0}}); + +typedef std::function<void(xs_SOCKET)> OnDeviceAdded; + +class Bluetooth { +public: + Bluetooth() {} + OnDeviceAdded onDeviceAdded; + + void setOnDeviceConnected(OnDeviceAdded onDeviceAdded) { + this->onDeviceAdded = onDeviceAdded; + } + bool startSlave() { + + struct sockaddr_rc loc_addr = {0}, rem_addr = {0}; + xs_SOCKET s; + socklen_t opt = sizeof(rem_addr); + + // allocate socket + s = xs_socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); + if (s < 0) { + return false; + } + + // 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)1; + if (bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) == -1) { + return false; + } + + // put socket into listening mode + if (::listen(s, 1) == -1) { + return false; + } + + // accept one connection + xs_SOCKET c = accept(s, (struct sockaddr *)&rem_addr, &opt); + if (c < 0) { + return false; + } + if (onDeviceAdded != nullptr) { + try { + onDeviceAdded(c); + } catch (std::bad_function_call bfc) { + } + } + + close(s); + return true; + } +}; +#endif+ \ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -2,9 +2,18 @@ cmake_minimum_required(VERSION 3.0) project(right_initiator) find_package(Threads) +find_package(crosssocket) +find_package(fmt) add_executable(right_initiator right_initiator.cpp) +target_include_directories(right_initiator PRIVATE ./) +target_include_directories(right_initiator PRIVATE crosssocket) +target_include_directories(right_initiator PRIVATE fmt) + #TODO use find_package, instead of hard linking target_link_libraries(right_initiator -lbluetooth) target_link_libraries(right_initiator ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(right_initiator crosssocket) +target_link_libraries(right_initiator fmt) +
diff --git a/GPIO.hpp b/GPIO.hpp @@ -0,0 +1,49 @@ +#ifndef _GPIO_H +#define _GPIO_H +#include <GPIO.hpp> +#include <cstdio> +#include <fmt/format.h> +#include <fstream> +#include <iostream> +#include <unistd.h> +namespace gpio { +enum class Direction { IN, OUT }; +const char *GPIO_EXPORT_PATH = "/sys/class/gpio/export"; +const char *GPIO_DIRECTION_PATH = "/sys/class/gpio/gpio{}/direction"; +const char *GPIO_VALUE_PATH = "/sys/class/gpio/gpio{}/value"; +const int offset = 458; +class GPIO { + + template <typename T> void write(std::string path, T value) { + std::ofstream f(path); + f << value; + f.close(); + } + +public: + int pin = 0; + Direction direction = Direction::IN; + GPIO(int pin) { this->pin = offset + pin; } + + static GPIO *getGPIO(int pinNo) { + int pin = offset + pinNo; + return new GPIO(pinNo); + } + + void setDirection(Direction direction) { + std::string path = fmt::format(GPIO_DIRECTION_PATH, pin); + write(path, direction == Direction::IN ? "in" : "out"); + } + + void setHigh() { + std::string path = fmt::format(GPIO_VALUE_PATH, pin); + write(path, 1); + } + + void setLow() { + std::string path = fmt::format(GPIO_VALUE_PATH, pin); + write(path, 1); + } +}; +} // namespace gpio +#endif+ \ No newline at end of file
diff --git a/LED.hpp b/LED.hpp @@ -0,0 +1,17 @@ +#ifndef _LED_H +#define _LED_H +#include <GPIO.hpp> +#include <fmt/format.h> +#include <unistd.h> +using namespace gpio; +class LED +{ + public: + GPIO *g = nullptr; + LED(GPIO *g) : g(g) { g->setDirection(Direction::OUT); } + + void on() { g->setHigh(); } + + void off() { g->setLow(); } +}; +#endif+ \ No newline at end of file
diff --git a/right_initiator.cpp b/right_initiator.cpp @@ -1,69 +1,54 @@ +#include <Bluetooth.hpp> +#include <GPIO.hpp> +#include <LED.hpp> #include <iostream> -#include <unistd.h> -#include <sys/socket.h> -#include <bluetooth/bluetooth.h> -#include <bluetooth/rfcomm.h> #include <thread> +#include <unistd.h> #define BUF_SIZE 256 using namespace std; +using namespace gpio; -bdaddr_t inc = ((bdaddr_t){{0, 0, 0, 0, 0, 0}}); - -int main(int argc, char *argv[]) +void deviceConnected(xs_SOCKET s) { - - struct sockaddr_rc addr = {0}; - int s, status = 0; + int pin = 17; + GPIO *g = GPIO::getGPIO(pin); + if (g == nullptr) + { + fmt::print("Cannot open GPIO {}\n", pin); + return; + } + LED led(g); + int bytesRead = 0; char *msg = (char *)calloc(sizeof(char), BUF_SIZE); - char dest[18] = "18:5E:0F:75:DB:8F"; - // allocate a socket - s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); - cout << s << endl; - - // 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 - status = connect(s, (struct sockaddr *)&addr, sizeof(addr)); - - // send a message - if (status == 0) + while ((bytesRead = read(s, msg, BUF_SIZE - 1)) > 0) { - int bytesRead = 0; - new std::thread([&]() { - string str; - std::getline(std::cin, str); - write(s, str.c_str(), str.size()); - }); - while ((bytesRead = read(s, msg, BUF_SIZE - 1)) > 0) + msg[bytesRead] = '\0'; + if (strncmp("hello", msg, 5) == 0) + { + led.on(); + } + else { - msg[bytesRead] = '\0'; - if (strncmp("hello", msg, 5) == 0) - { - system("echo \"475\" > /sys/class/gpio/export"); - system("echo \"out\" > /sys/class/gpio/gpio475/direction"); - system(" echo \"1\" > /sys/class/gpio/gpio475/value"); - printf("light on\n"); - } - else - { - system(" echo \"0\" > /sys/class/gpio/gpio475/value"); - printf("light off\n"); - } - printf("%s", msg); + led.off(); } + printf("%s", msg); + } +} + +int main(int argc, char *argv[]) +{ + Bluetooth b; + b.setOnDeviceConnected(deviceConnected); + if (b.startSlave()) + { + fmt::print("Slave running\n"); } else { - int se = errno; - cout << "Connection failed" << status << se << endl; + fmt::print("Cannot start slave\n"); } - close(s); while (1) ; return 0;
: bharatvaj/forwarder-initiators