Skip to content
Snippets Groups Projects
Commit b621bbab authored by lee chaemin's avatar lee chaemin
Browse files

Merge branch 'Minkyung-main-patch-09399' into 'main'

Update 권민경/thread_server.c

See merge request !1
parents 2db04942 ca477cf9
No related branches found
No related tags found
1 merge request!1Update 권민경/thread_server.c
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
#define VALUE_MAX 40
#define PIN 26
#define POUT 19
#define POUT_MOTOR_F 27
#define POUT_MOTOR_B 22
// #define PIN_BUTTON 20
// #define POUT_BUTTON 21
#define POUT_LED 18
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int temperature_client_socket = -1;
int motion_client_socket = -1;
int servo_client_socket = -1;
char on[2] = "1";
char off[2] = "0";
int temp = 0; // 0: off, 1: on
int motion = 0;
int motor = 0;
static int GPIOExport(int pin) {
#define BUFFER_MAX 3
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/export", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open export for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}
static int GPIOUnexport(int pin) {
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open unexport for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}
static int GPIODirection(int pin, int dir) {
static const char s_directions_str[] = "in\0out";
#define DIRECTION_MAX 35
char path[DIRECTION_MAX] = "/sys/class/gpio/gpio%d/direction";
int fd;
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio direction for writing!\n");
return(-1);
}
if (-1 == write(fd, &s_directions_str[IN==dir?0:3], IN==dir?2:3)) {
fprintf(stderr, "Failed to set direction!\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}
static int GPIOWrite(int pin, int value) {
static const char s_values_str[] = "01";
char path[VALUE_MAX];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio value for writing!\n");
return(-1);
}
if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) {
fprintf(stderr, "Failed to write value!\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}
static int GPIORead(int pin) {
char path[VALUE_MAX];
char value_str[3];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_RDONLY);
if ( -1 == fd) {
fprintf(stderr, "Failed to open gpio value for reading!\n");
return(-1);
}
if (-1 == read(fd, value_str, 3)) {
fprintf(stderr, "Failed to read value!\n");
close(fd);
return(-1);
}
close(fd);
return(atoi(value_str));
}
void error_handling(char *message){
fputs(message,stderr);
fputc('\n',stderr);
exit(1);
}
void* handle_temperature_client(void* arg) {
int client_socket = *((int*)arg);
char buffer[BUFFER_SIZE];
int read_size;
printf("Temperature client connected\n");
while ((read_size = read(client_socket, buffer, BUFFER_SIZE)) > 0) {
printf("Received from temperature client: %s", buffer);
if (strncmp(on, buffer, 1) == 0) {
// Write message to motion client
motor = 1;
}
else{
motor = 0;
}
memset(buffer, 0, sizeof(buffer));
}
close(client_socket);
temperature_client_socket = -1;
pthread_exit(NULL);
}
void* handle_motion_client(void* arg) {
int client_socket = *((int*)arg);
char buffer[BUFFER_SIZE];
int read_size;
int start;
char msg[2];
printf("Motion client connected\n");
if (motor == 1) {
// Write message to motion client
start = 1;
printf("Motion ON\n");
snprintf(msg, 2, "%d", start);
write(motion_client_socket, msg, sizeof(msg));
}
else{
start = 0;
//printf("Motion OFF\n");
//snprintf(msg, 2, "%d", start);
//write(motion_client_socket, msg, sizeof(msg));
}
while ((read_size = read(client_socket, buffer, BUFFER_SIZE)) > 0) {
printf("Received from motion client: %s", buffer);
// Perform actions based on received message from motion client
if (strncmp(on, buffer, 1) == 0) {
motor = 1;
break;
} else {
motor = 0;
break;
}
memset(buffer, 0, sizeof(buffer));
}
printf("Motion client disconnected\n");
close(client_socket);
motion_client_socket = -1;
pthread_exit(NULL);
}
void* handle_servo_client(void* arg) {
int client_socket = *((int*)arg);
char buffer[BUFFER_SIZE];
int read_size;
char msg[2];
printf("Servo client connected\n");
while ((read_size = read(client_socket, buffer, BUFFER_SIZE)) > 0) {
printf("Received from servo client: %s", buffer);
if (strcmp(buffer, "1") == 0) {
// Perform action for servo motor
// ...
}
memset(buffer, 0, sizeof(buffer));
}
printf("Servo client disconnected\n");
close(client_socket);
servo_client_socket = -1;
pthread_exit(NULL);
}
int main(int argc, char* argv[]) {
int state = 1;
int prev_state = 1;
int light = 0;
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
socklen_t clnt_addr_size;
pthread_t temperature_thread, motion_thread, servo_thread;
char msg[2];
int str_len;
//Enable GPIO pins
if (-1 == GPIOExport(POUT_MOTOR_F))
return (1);
if (-1 == GPIOExport(POUT_MOTOR_B))
return (1);
if (-1 == GPIOExport(PIN) || -1 == GPIOExport(POUT))
return (1);
if (-1 == GPIOExport(POUT_LED))
return (1);
//Set GPIO directions
if (-1 == GPIODirection(POUT_MOTOR_F, OUT))
return (2);
if (-1 == GPIODirection(POUT_MOTOR_B, OUT))
return (2);
if (-1 == GPIODirection(PIN, IN) || -1 == GPIODirection(POUT, OUT))
return(2);
if (-1 == GPIODirection(POUT_LED, OUT))
return(2);
if ( -1 == GPIOWrite(POUT_MOTOR_F, 1))
return(3);
if ( -1 == GPIOWrite(POUT_MOTOR_F, 1))
return(3);
if ( -1 == GPIOWrite(POUT_LED, 1))
return(3);
if ( -1 == GPIOWrite(POUT, 1))
return(3);
if (argc != 2) {
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
// Create a server socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1)
error_handling("socket() error");
// Set up the server address
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(atoi(argv[1]));
// Bind the server socket to the specified address and port
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == 1)
error_handling("bind() error");
// Listen for client connections
if (listen(server_socket, MAX_CLIENTS) == -1)
error_handling("listen() error");
socklen_t client_address_size = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr*)&client_address, &client_address_size);
if (client_socket < 0)
error_handling("accept() error");
while (1) {
// Handle temperature client
if (temperature_client_socket == -1) {
temperature_client_socket = client_socket;
pthread_create(&temperature_thread, NULL, handle_temperature_client, &temperature_client_socket);
}
// Handle motion client
else if (motion_client_socket == -1 && motor == 1) {
motion_client_socket = client_socket;
pthread_create(&motion_thread, NULL, handle_motion_client, &motion_client_socket);
}
if (motor == 1)
{
printf("Motor ON\n");
}
else if(motor == 0)
{
printf("Motor OFF\n");
}
GPIOWrite(POUT_LED, motor);
GPIOWrite(POUT_MOTOR_F, motor);
asleep(1000)
// Handle servo client
//else if (servo_client_socket == -1) {
// servo_client_socket = client_socket;
// pthread_create(&servo_thread, NULL, handle_servo_client, &servo_client_socket);
//}
// Reject the client connection
//else {
// printf("Max clients reached. Connection rejected.\n");
// close(client_socket);
//}
}
close(server_socket);
//Disable GPIO pins
if (-1 == GPIOUnexport(POUT_MOTOR_F))
return (4);
if (-1 == GPIOUnexport(POUT_MOTOR_B))
return (4);
if (-1 == GPIOUnexport(PIN) || -1 == GPIOUnexport(POUT))
return(4);
if (-1 == GPIOUnexport(POUT_LED))
return(4);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment