Skip to content

Commit

Permalink
Merge pull request #1 from ESE-Peasy/initial_transfer_of_files_from_P…
Browse files Browse the repository at this point in the history
…P_repo

Initial transfer of files from Posture Perfection Repo
  • Loading branch information
C-Begley authored Apr 12, 2021
2 parents cf88c45 + ca0f302 commit 09499b3
Show file tree
Hide file tree
Showing 11 changed files with 443 additions and 6 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#CMake files
CMakeCache.txt
cmake_install.cmake
CMakeFiles/*
Makefile
install_manifest.txt

#PosturePerfection
Examples/PosturePefection/CMakeCache.txt
Examples/PosturePerfection/cmake_install.cmake
Examples/PosturePerfection/CMakeFiles/*
Examples/PosturePerfection/Makefile

#Binaries
libRemoteNotify.a
libRemoteNotifyBroadcast.a
libRemoteNotifyReceive.a
PosturePerfectionReceiver
PosturePerfectionTestBroadcast
#Vim
.swp
#VSCODE
.vscode/
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
project(RemoteNotification)

include(GNUInstallDirs)
add_library (RemoteNotifyBroadcast STATIC RemoteNotifyBroadcast.cpp)
add_library (RemoteNotifyReceive STATIC RemoteNotifyReceive.cpp)
set_target_properties(RemoteNotifyBroadcast PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
set_target_properties(RemoteNotifyReceive PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

set_target_properties(RemoteNotifyBroadcast PROPERTIES
PUBLIC_HEADER RemoteNotifyBroadcast.h)
set_target_properties(RemoteNotifyReceive PROPERTIES
PUBLIC_HEADER RemoteNotifyReceive.h)

install(TARGETS RemoteNotifyBroadcast RemoteNotifyReceive
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

15 changes: 15 additions & 0 deletions Examples/PosturePerfection/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.10)

project(Posture{erfectionNotification)

# add the executable
add_executable(PosturePerfectionReceiver PosturePerfection_receiver.cpp)
add_executable(PosturePerfectionTestBroadcast PosturePerfection_test_broadcast.cpp)

target_link_libraries(PosturePerfectionReceiver RemoteNotifyReceive)
target_link_libraries(PosturePerfectionTestBroadcast RemoteNotifyBroadcast)

set(CMAKE_CXX_STANDARD 11)
install(TARGETS PosturePerfectionReceiver
RUNTIME
)
32 changes: 32 additions & 0 deletions Examples/PosturePerfection/PosturePerfection_receiver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @file PosturePerfection_receiver.cpp
* @brief Program for running a notification receiver
*
* @copyright Copyright (C) 2021 Conor Begley
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <RemoteNotifyReceive.h>
int main(int argc, char *argv[]) {
char current_d[1024];
getcwd(current_d, sizeof(current_d));
std::string cwd(current_d);
std::string image = cwd + "/posture-logo-no-text.png";
RemoteNotify::Receive receiver(121121, true, "Posture Perfection", image);
while (1) {
receiver.run();
}
return 1;
}
29 changes: 29 additions & 0 deletions Examples/PosturePerfection/PosturePerfection_test_broadcast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @file PosturePerfection_test_broadcast.cpp
* @brief Program for running a simple broadcaster example
*
*
* @copyright Copyright (C) 2021 Conor Begley
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <RemoteNotifyBroadcast.h>

int main(int argc, char *argv[]) {
RemoteNotify::Broadcast broadcaster = RemoteNotify::Broadcast();
std::string msg = "Hello World \nSample message";
broadcaster.sendMessage(msg);
return 1;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 23 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
#About the Library
UDP Broadcaster and Receiver to trigger Notify-Send messages
# About the Library

##Example Projects
https://github.com/ESE-Peasy/PosturePerfection
UDP Broadcaster and Receiver to trigger Notify-Send messages

##Future Work
- Add MAC broadcaster and client
## Requirements

- Currently the system only runs on Linux
- notify-send [See here for more details](http://vaskovsky.net/notify-send/linux.html)

You can test notify-send is working with a simple command like

`notify-send "Test"`

A pop up should appear on your screen, with the text "Test"

_Note: It's been found on the raspberry pi to make notify-send work, the following installs are also needed_
`sudo apt-get install mate-notification-daemon mate-notification-daemon-common`

## Example Projects

[Posture Perfection](https://github.com/ESE-Peasy/PosturePerfection)

## Future Work

- Add MacOS broadcaster and client
- Add Windows broadcaster and client
38 changes: 38 additions & 0 deletions RemoteNotifyBroadcast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @copyright Copyright (C) 2021 Conor Begley
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "RemoteNotifyBroadcast.h"

namespace RemoteNotify {
Broadcast::Broadcast(int port, char *ip) {
this->target_ip = ip;
this->broadcast_fd = socket(AF_INET, SOCK_DGRAM, 0);
this->address.sin_family = AF_INET;
this->address.sin_port = htons(port);
this->address.sin_addr.s_addr = inet_addr(this->target_ip);
int addrlen = sizeof(address);
int opt = 1;
int setup = setsockopt(this->broadcast_fd, SOL_SOCKET, SO_BROADCAST, &opt,
sizeof(opt));
}
Broadcast::~Broadcast() { int cls = close(this->broadcast_fd); }

void Broadcast::sendMessage(std::string msg) {
sendto(this->broadcast_fd, msg.c_str(), msg.length(), MSG_DONTWAIT,
(const struct sockaddr *)&this->address, sizeof(this->address));
}
} // namespace RemoteNotify
62 changes: 62 additions & 0 deletions RemoteNotifyBroadcast.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @file RemoteNotifyBroadcast.h
* @brief Broadcast for sending notifications to `RemoteNotify:Receive`
*
* @copyright Copyright (C) 2021 Conor Begley
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

#ifndef REMOTENOTIFYBROADCAST_H_
#define REMOTENOTIFYBROADCAST_H_
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>

#include <cstring>
#include <iostream>

namespace RemoteNotify {
/**
* @brief A Broadcast to send messages to `RemoteNotify::Receive`
*
*/
class Broadcast {
private:
int broadcast_fd; ///< Socket which the Broadcast is bound to
struct sockaddr_in address; ///< Broadcast address structure
char* target_ip; ///< Broadcast IP address

public:
/**
* @brief Constructor for `RemoteNotify::Broadcast`
*
* @param port Port number to listen on (default is 121121)
* @param ip Broadcast address (default is 255.255.255.255)
*/
explicit Broadcast(int port = 121121,
char* ip = const_cast<char*>("255.255.255.255"));
~Broadcast();

/**
* @brief Broadcasts message to ip
* @param msg The message to be sent
*/
void sendMessage(std::string msg);
};
} // namespace RemoteNotify
#endif // REMOTENOTIFYBROADCAST_H_
105 changes: 105 additions & 0 deletions RemoteNotifyReceive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* @copyright Copyright (C) 2021 Conor Begley
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "RemoteNotifyReceive.h"

#include <exception>

namespace RemoteNotify {
int err_msg(int num, std::string msg) {
if (num < 0) {
std::cerr << "Error with " << msg << "\n"
<< "Error Code:" << errno << "\n";
exit(1);
}
return 0;
}

std::string GetStringFromCommand(std::string cmd) {
std::string data;
FILE *stream;
const int MaxBuffer = 256;
char buffer[MaxBuffer];
cmd.append(" 2>&1");

stream = popen(cmd.c_str(), "r");

if (stream) {
while (!feof(stream))
if (fgets(buffer, MaxBuffer, stream) != NULL) data.append(buffer);
pclose(stream);
}
return data;
}

Receive::Receive(int port, bool check_env, std::string title,
std::string image) {
// Setup notify send command
if (check_env == true) {
char *env = getenv("XDG_CURRENT_DESKTOP");
if (env != NULL) {
if (std::strcmp(env, "ubuntu:GNOME") == 0) {
this->command = "notify-send -u critical ";
}
}
}
this->title = title;
this->command =
this->command + this->space + this->quote + this->title + this->quote;
this->image = image;

// Setup socket
this->receive_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (this->receive_fd == 0) {
err_msg(-1, "socket_creation");
}
this->address.sin_family = AF_INET;
this->address.sin_addr.s_addr = htons(INADDR_ANY);
this->address.sin_port = htons(port);
int opt = 1;
int setup =
setsockopt(this->receive_fd, SOL_SOCKET,
SO_REUSEADDR | SO_REUSEPORT | SO_BROADCAST, &opt, sizeof(opt));
if (setup) {
err_msg(-1, "socket_creation");
}
int binding = bind(this->receive_fd, (struct sockaddr *)&(this->address),
sizeof(this->address));
err_msg(binding, "socket_bind");
} // namespace RemoteNotify
Receive::~Receive() {
shutdown(this->receive_fd, SHUT_RDWR);
close(this->receive_fd);
}

void Receive::run() {
std::cout.flush();
std::memset(this->buffer, 0, sizeof(this->buffer));
std::memset(&this->Broadcast_address, 0, sizeof(Broadcast_address));
int read_value =
recvfrom(this->receive_fd, this->buffer, 1024, MSG_DONTWAIT,
(struct sockaddr *)&this->Broadcast_address,
reinterpret_cast<socklen_t *>(&this->Broadcast_len));
if (read_value > 0) {
this->buffer[read_value] = '\0';
std::string out = this->command + this->space + this->quote + this->buffer +
this->quote + this->space + this->middle + this->space +
this->image;
system(out.c_str());
}
}
}; // namespace RemoteNotify
Loading

0 comments on commit 09499b3

Please sign in to comment.