Skip to content

Commit

Permalink
Added practice 2 resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzafernan committed Aug 13, 2023
1 parent f30dc1b commit 61bf99f
Show file tree
Hide file tree
Showing 7 changed files with 515 additions and 211 deletions.
6 changes: 3 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 2
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
Expand All @@ -116,7 +116,7 @@ MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBlockIndentWidth: 4
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
Expand Down Expand Up @@ -179,7 +179,7 @@ StatementAttributeLikeMacros:
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
TabWidth: 4
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
build/
log/
tmp/

# practice 2
tp2/SerialService/serialService
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,19 @@ se modifican los archivos desde el sitio web).

Por último este servicio iniciará un servidor TCP para que el servicio InterfaceService se puede
conectar y comunicar.

### Protocolo serie entre el Emulador y SerialService y protocolo TCP entre SerialService e InterfaceService

#### Seteo encendido de salida (hacia el emulador)
">OUT:X,Y\r\n"

Siendo X el número de salida (0, 1 ó 2) e Y el estado (0 ó 1).

Esta trama es la misma que enviará InterfaceService a SerialService.

#### Evento pulsador (desde el emulador)
">SW:X,Y\r\n"

Siendo X el número de salida (0, 1 ó 2) e Y el nuevo estado a setear (0 ó 1).

Esta trama es la misma que enviará SerialService a InterfaceService.
184 changes: 92 additions & 92 deletions tp1/src/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,107 +24,107 @@ FILE *psign_log; /*!> Data logging file pointer */
* @param signo: Number of signal received
*/
void signal_handler(int signo) {
if (signo == SIGINT) {
// Close file pointers
fclose(pdata_log);
fclose(psign_log);
}
if (signo == SIGINT) {
// Close file pointers
fclose(pdata_log);
fclose(psign_log);
}
}

int main(void) {
char buffer[BUFFER_SIZE];
char *buffer_content = (buffer + MSG_PREFIX_LEN); // Pointer to message without prefix
int return_code, fd;
ssize_t bytes_read;
FILE *plog;

printf("Reader process initializaton. PID %d\n", getpid());

// Create tmp folder with permissions 6:110 rw-
return_code = mkdir(pipe_dir_path, 0777);
if (return_code == -1 && errno != EEXIST) {
perror("Error creating tmp directory");
return 1; // Exit with error
}

// Create log folder with permissions 6:110 rw-
return_code = mkdir(log_dir_path, 0777);
if (return_code == -1 && errno != EEXIST) {
perror("Error creating log directory");
return 1; // Exit with error
}

/* Logging files setup */
pdata_log = fopen(data_log_path, "a"); // Open data log file in append mode
psign_log = fopen(sign_log_path, "a"); // Open signals log file in append mode

if (pdata_log == NULL || psign_log == NULL) {
printf("Error open logging files.\n");
return 1;
}

// Named pipe permissions 6:110 rw-
return_code = mknod(pipe_name, S_IFIFO | 0666, 0);

if (return_code == -1) {
perror("Error creating named pipe");
} else if (return_code < -1) {
perror("Error creating named pipe");
return 1; // Exit with error
}

/* Signal handle */
struct sigaction hsigint;
hsigint.sa_handler = signal_handler;
hsigint.sa_flags = 0;
sigemptyset(&hsigint.sa_mask);
sigaction(SIGINT, &hsigint, NULL);

/* Open named pipe. Blocks until reader is found */
// TODO: A timeout can be implemented
printf("Waiting for writer...\n");
if ((fd = open(pipe_name, O_RDONLY)) < 0) {
perror("Error opening named pipe");
return 1;
}

/**
* Open syscalls returned without error,
* meaning other process is attached to named pipe in read only mode
*/
printf("Got a writer\n");

size_t write_chk; // Aux var to check bytes written

/* Reader loop */
do {
/* Read named pipe into local buffer */
if ((bytes_read = read(fd, buffer, BUFFER_SIZE)) < 0) {
perror("Error reading named pipe");
continue;
char buffer[BUFFER_SIZE];
char *buffer_content = (buffer + MSG_PREFIX_LEN); // Pointer to message without prefix
int return_code, fd;
ssize_t bytes_read;
FILE *plog;

printf("Reader process initializaton. PID %d\n", getpid());

// Create tmp folder with permissions 6:110 rw-
return_code = mkdir(pipe_dir_path, 0777);
if (return_code == -1 && errno != EEXIST) {
perror("Error creating tmp directory");
return 1; // Exit with error
}

// Create log folder with permissions 6:110 rw-
return_code = mkdir(log_dir_path, 0777);
if (return_code == -1 && errno != EEXIST) {
perror("Error creating log directory");
return 1; // Exit with error
}

/* Logging files setup */
pdata_log = fopen(data_log_path, "a"); // Open data log file in append mode
psign_log = fopen(sign_log_path, "a"); // Open signals log file in append mode

if (pdata_log == NULL || psign_log == NULL) {
printf("Error open logging files.\n");
return 1;
}

buffer[bytes_read] = '\0';
printf("Read %ld bytes: %s", bytes_read, buffer);
// Named pipe permissions 6:110 rw-
return_code = mknod(pipe_name, S_IFIFO | 0666, 0);

if (NULL != strstr(buffer, data_msg_prefix)) {
plog = pdata_log;
} else if (NULL != strstr(buffer, sign_msg_prefix)) {
plog = psign_log;
} else {
continue;
if (return_code == -1) {
perror("Error creating named pipe");
} else if (return_code < -1) {
perror("Error creating named pipe");
return 1; // Exit with error
}
write_chk = fwrite(buffer_content, sizeof(char), strlen(buffer_content), plog);
if (write_chk < strlen(buffer_content)) {
perror("Error encountered when writing log file");

/* Signal handle */
struct sigaction hsigint;
hsigint.sa_handler = signal_handler;
hsigint.sa_flags = 0;
sigemptyset(&hsigint.sa_mask);
sigaction(SIGINT, &hsigint, NULL);

/* Open named pipe. Blocks until reader is found */
// TODO: A timeout can be implemented
printf("Waiting for writer...\n");
if ((fd = open(pipe_name, O_RDONLY)) < 0) {
perror("Error opening named pipe");
return 1;
}

} while (bytes_read > 0);
/**
* Open syscalls returned without error,
* meaning other process is attached to named pipe in read only mode
*/
printf("Got a writer\n");

size_t write_chk; // Aux var to check bytes written

/* Reader loop */
do {
/* Read named pipe into local buffer */
if ((bytes_read = read(fd, buffer, BUFFER_SIZE)) < 0) {
perror("Error reading named pipe");
continue;
}

buffer[bytes_read] = '\0';
printf("Read %ld bytes: %s", bytes_read, buffer);

if (NULL != strstr(buffer, data_msg_prefix)) {
plog = pdata_log;
} else if (NULL != strstr(buffer, sign_msg_prefix)) {
plog = psign_log;
} else {
continue;
}
write_chk = fwrite(buffer_content, sizeof(char), strlen(buffer_content), plog);
if (write_chk < strlen(buffer_content)) {
perror("Error encountered when writing log file");
}

} while (bytes_read > 0);

// Close file pointers
if (fclose(pdata_log) != 0 || fclose(psign_log) != 0) {
perror("Error closing log file");
}
// Close file pointers
if (fclose(pdata_log) != 0 || fclose(psign_log) != 0) {
perror("Error closing log file");
}

return 0;
return 0;
}
Loading

0 comments on commit 61bf99f

Please sign in to comment.