Refactoring

This commit is contained in:
PoliEcho 2024-12-08 20:34:59 +01:00
parent 35d28d2a63
commit e4c86e2322
57 changed files with 338 additions and 0 deletions

View File

Before

Width:  |  Height:  |  Size: 214 KiB

After

Width:  |  Height:  |  Size: 214 KiB

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

Before

Width:  |  Height:  |  Size: 695 B

After

Width:  |  Height:  |  Size: 695 B

View File

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB

View File

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

Before

Width:  |  Height:  |  Size: 53 KiB

After

Width:  |  Height:  |  Size: 53 KiB

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View File

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

Before

Width:  |  Height:  |  Size: 84 KiB

After

Width:  |  Height:  |  Size: 84 KiB

View File

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

4
cpp-server/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
server
.vscode
build
src/secret.h

35
cpp-server/Makefile Normal file
View File

@ -0,0 +1,35 @@
# Compiler and flags
CPPC = g++
CPPC_FLAGS = -s -O3 -lasound -lcryptopp -Wall -Wextra
SRC_PATH := src
OBJ_PATH := build/obj
BIN_PATH := build/bin
SRC_FILES := $(shell find $(SRC_PATH) -name '*.cpp')
# Generate corresponding object file paths by replacing src/ with build/obj/
OBJ_FILES := $(patsubst $(SRC_PATH)/%.cpp,$(OBJ_PATH)/%.o,$(SRC_FILES))
all: make-build-dir $(BIN_PATH)/TabletPcControl-server
make-build-dir:
mkdir -p $(OBJ_PATH)
mkdir -p $(BIN_PATH)
$(BIN_PATH)/TabletPcControl-server: $(OBJ_FILES)
$(CPPC) $(CPPC_FLAGS) $^ -o $@
$(OBJ_PATH)/%.o: $(SRC_PATH)/%.cpp
$(CPPC) $(CPPC_FLAGS) -c $< -o $@
clean:
rm -fr build
.PHONY: all clean

4
cpp-server/build.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
read -p "enter secret password: " SECRET
echo "#define SECRET \"$SECRET\"" > src/secret.h
make -j$(nproc)

BIN
cpp-server/crypt_text Executable file

Binary file not shown.

115
cpp-server/src/alsa.cpp Normal file
View File

@ -0,0 +1,115 @@
#include "helper_funcs.h"
#include <alsa/asoundlib.h>
#include <arpa/inet.h>
#include <cerrno>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <unistd.h>
namespace alsa {
int get_volume(const char *selem_name) {
long min, max, volume;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
// Open mixer
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle,
"default"); // Use "default" for the default sound card
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
// Allocate and set mixer element ID
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_name(sid, selem_name);
// Find the mixer element
snd_mixer_elem_t *elem = snd_mixer_find_selem(handle, sid);
int volume_percent;
if (elem) {
// Get the playback volume range
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
// Get the current playback volume for front left channel
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT,
&volume);
// Calculate the volume percentage
volume_percent = (volume - min) * 100 / (max - min);
} else {
std::cerr << "Mixer element not found\n";
snd_mixer_close(handle);
safe_exit(ENODEV);
}
// Clean up
snd_mixer_close(handle);
return volume_percent;
}
void set_volume(const char *selem_name, long volume_percent) {
long min, max;
snd_mixer_t *handle;
snd_mixer_selem_id_t *sid;
// Open mixer
snd_mixer_open(&handle, 0);
snd_mixer_attach(handle, "default"); // Use "default" instead of hw:0
snd_mixer_selem_register(handle, NULL, NULL);
snd_mixer_load(handle);
// Allocate and set mixer element ID
snd_mixer_selem_id_alloca(&sid);
snd_mixer_selem_id_set_name(sid, selem_name);
// Find the mixer element
snd_mixer_elem_t *elem = snd_mixer_find_selem(handle, sid);
if (elem) {
// Get the playback volume range
snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
// Calculate the new volume value
long volume = min + (volume_percent * (max - min) / 100);
// Set the playback volume
snd_mixer_selem_set_playback_volume_all(elem, volume);
printf("Volume set to %ld%%\n", volume_percent);
} else {
fprintf(stderr, "Mixer element not found\n");
}
// Clean up
snd_mixer_close(handle);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <selem_name> <volume_percent>\n", argv[0]);
return 1;
}
const char *selem_name = argv[1];
long volume_percent = atol(argv[2]);
set_volume(selem_name, volume_percent);
return 0;
}
} // namespace alsa

4
cpp-server/src/alsa.h Normal file
View File

@ -0,0 +1,4 @@
namespace alsa {
int get_volume(const char *selem_name);
void set_volume(const char *selem_name, long volume_percent);
} // namespace alsa

38
cpp-server/src/crypt.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <cryptopp/aes.h>
#include <cryptopp/base64.h>
#include <cryptopp/filters.h>
#include <cryptopp/modes.h>
#include <iomanip>
#include <iostream>
#include <string>
int main() {
// Key and IV (Initialization Vector) for AES
CryptoPP::byte key[CryptoPP::AES::DEFAULT_KEYLENGTH] = {
'k', 'e', 'y', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '1', '0', '1', '1'};
CryptoPP::byte iv[CryptoPP::AES::BLOCKSIZE] = {'i', 'v', '1', '2', '3', '4',
'5', '6', '7', '8', '9', '1',
'0', '1', '1', '1'};
// Message to be encrypted
std::string plainText = "Hello, AES!";
std::string cipherText; // Declare cipherText variable
// Encrypt using AES in CBC mode
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption encryptor(key, sizeof(key), iv);
CryptoPP::StringSource(plainText, true,
new CryptoPP::StreamTransformationFilter(
encryptor, new CryptoPP::StringSink(cipherText)));
// Encode the encrypted message in Base64 format
std::string base64Encoded;
CryptoPP::StringSource(
cipherText, true,
new CryptoPP::Base64Encoder(new CryptoPP::StringSink(base64Encoded)));
// Display the Base64 encoded encrypted message
std::cout << "Encrypted Text (Base64): " << base64Encoded << std::endl;
return 0;
}

View File

@ -0,0 +1,12 @@
#include "main.h"
#include <iostream>
#include <unistd.h>
void safe_exit(int code) {
try {
std::clog << "Exiting\n";
close(serverSocket);
} catch (...) {
}
exit(code);
}

View File

@ -0,0 +1 @@
void safe_exit(int code);

124
cpp-server/src/main.cpp Normal file
View File

@ -0,0 +1,124 @@
#include "alsa.h"
#include "helper_funcs.h"
#include "secret.h"
#include <alsa/asoundlib.h>
#include <arpa/inet.h>
#include <cerrno>
#include <csignal>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <unistd.h>
#define PORT 54321
int serverSocket;
int main(int argc, char *argv[]) {
std::string SecretNumCode_s = "";
for (char ch : SECRET) {
uint num = ch - 'a' + 1; // Convert 'a' to 1, 'b' to 2, etc.
SecretNumCode_s.append(std::to_string(num));
}
const long SecretNumCode = std::stoi(SecretNumCode_s);
// signal handlers
signal(SIGTERM, safe_exit);
signal(SIGINT, safe_exit);
signal(SIGQUIT, safe_exit);
signal(SIGHUP, safe_exit);
// error signal handlers
signal(SIGSEGV, safe_exit);
// creating socket
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
// specifying the address
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT);
serverAddress.sin_addr.s_addr = INADDR_ANY;
// binding socket.
while (bind(serverSocket, (struct sockaddr *)&serverAddress,
sizeof(serverAddress)) != 0) {
std::cerr << "error binding socket retring\n";
sleep(2);
}
// listening to the assigned socket
listen(serverSocket, 5);
accept_new_connection:
// accepting connection request
int clientSocket = accept(serverSocket, nullptr, nullptr);
pid_t pid;
pid = fork();
if (pid == -1) {
std::cerr << "Fork failed";
safe_exit(-1);
} else if (pid == 0) {
// child process
signal(SIGUSR1, exit);
char bufferSend[1024];
int volume_percent = INTMAX_MIN;
int volume_percent_old;
while (1) {
volume_percent_old = volume_percent;
volume_percent = alsa::get_volume("Master");
if (volume_percent != volume_percent_old) {
}
usleep(100000);
sprintf(bufferSend, "Volume: %d%%\n", alsa::get_volume("Master"));
if (send(clientSocket, bufferSend, strlen(bufferSend), 0) == -1) {
std::cerr << "send() returned an error code\n";
safe_exit(-1);
}
sleep(1);
}
} else {
// parent process
char bufferRecv[1024];
// recieving data
while (1) {
memset(&bufferRecv, 0, sizeof(bufferRecv));
int RecvRetVal = recv(clientSocket, bufferRecv, sizeof(bufferRecv), 0);
if (RecvRetVal > sizeof(bufferRecv) || RecvRetVal < 0) {
std::cerr << "recv() returned an error code\n";
safe_exit(RecvRetVal);
} else if (RecvRetVal == 0) {
// detect if client disconected
kill(pid, SIGUSR1);
close(clientSocket);
goto accept_new_connection;
}
std::cout << "Message from client: " << bufferRecv << std::endl;
}
// closing the socket.
close(serverSocket);
}
return 0;
}

1
cpp-server/src/main.h Normal file
View File

@ -0,0 +1 @@
extern int serverSocket;