int parse_ini_file(void) { FILE *fp; char line[4 * BUF_LENGTH]; size_t size; uint32_t index = 0; fp = fopen(ini_file, "r"); if (fp == NULL) { return -1; } size = read_file_size(ini_file); /* * Allocate memory to hold read buffer */ ini_data = calloc(size, sizeof(char)); if (ini_data == NULL) { return -1; } while (fgets(line, sizeof(line), fp) != NULL) { copy_line(ini_data, line, &index); } fclose(fp); return 0; }
int app_send(char * port, const char * filepath) { printf("> [APP] Connecting...\n"); // Getting file attributes if( read_file_size(filepath) != OK) return ERROR; // Open the file for reading and sending the data packets if( open_file(filepath, "r") != OK) { printf("> [APP] Error opening input file\n"); return ERROR; } int fd = llopen(port, TRANSMITTER); if(fd < 0) { return ERROR; } printf("> [APP] Connection established...\n"); printf("\n> [APP] Sending start control packet...\n"); if( app_send_control(PACKET_START, PACKET_FILE_SIZE, fstatus.size) != OK) { return ERROR; } pack.sequence_nr = 0; int number_of_packets = (fstatus.size / MAX_PACKET_SIZE) + (fstatus.size % MAX_PACKET_SIZE > 0 ? 1 : 0); char buf[MAX_PACKET_SIZE]; int res = 0; int i; for(i = 0; i < number_of_packets; i++) { printf("\n> [APP] Sending data packet(%d)...\n", pack.sequence_nr); int read = fread(buf, sizeof(char), MAX_PACKET_SIZE, fstatus.f); res += read; if( app_send_data(pack.sequence_nr, read, buf) != OK) { printf("> [APP] Could not write packet(%d)\n", pack.sequence_nr); fclose(fstatus.f); llclose(); return ERROR; } pack.sequence_nr++; } printf("\n> [APP] Written bytes: [ %d / %d ]\n", res, fstatus.size); // Ending connection printf("\n> [APP] Sending end control packet...\n"); if( app_send_control(PACKET_END, PACKET_FILE_SIZE, fstatus.size) != OK) { return ERROR; } printf("\n> [APP] Closing connection...\n"); if(fclose(fstatus.f) < 0) { printf(" > [APP] Error closing the input file\n"); llclose(); return ERROR; } llclose(); return OK; }