Private
Public Access
2
0

gpio: use open in all function opposed to fopen

Signed-off-by: Thomas Ingleby <thomas.c.ingleby@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Thomas Ingleby
2014-05-19 15:40:43 +01:00
committed by Brendan Le Foll
parent b0707886af
commit e2f72540ef

View File

@@ -80,7 +80,6 @@ maa_gpio_init_raw(int pin)
if (pin < 0) if (pin < 0)
return NULL; return NULL;
FILE *export_f;
char bu[MAX_SIZE]; char bu[MAX_SIZE];
int length; int length;
@@ -90,14 +89,17 @@ maa_gpio_init_raw(int pin)
dev->isr_value_fp = -1; dev->isr_value_fp = -1;
dev->pin = pin; dev->pin = pin;
if ((export_f = fopen(SYSFS_CLASS_GPIO "/export", "w")) == NULL) { int export = open(SYSFS_CLASS_GPIO "/export", O_WRONLY);
if (export == -1) {
fprintf(stderr, "Failed to open export for writing!\n"); fprintf(stderr, "Failed to open export for writing!\n");
return NULL; return NULL;
} }
length = snprintf(bu, sizeof(bu), "%d", dev->pin); length = snprintf(bu, sizeof(bu), "%d", dev->pin);
fwrite(bu, sizeof(char), length, export_f); if (write(export, bu, length*sizeof(char)) == -1) {
fprintf(stderr, "Failed to write to export\n");
}
fclose(export_f); close(export);
return dev; return dev;
} }
@@ -178,8 +180,8 @@ maa_gpio_edge_mode(maa_gpio_context dev, gpio_edge_t mode)
char filepath[MAX_SIZE]; char filepath[MAX_SIZE];
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/edge", dev->pin); snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/edge", dev->pin);
FILE *edge; int edge = open(filepath, O_RDWR);
if ((edge= fopen(filepath, "w")) == NULL) { if (edge == -1) {
fprintf(stderr, "Failed to open edge for writing!\n"); fprintf(stderr, "Failed to open edge for writing!\n");
return MAA_ERROR_INVALID_RESOURCE; return MAA_ERROR_INVALID_RESOURCE;
} }
@@ -200,12 +202,16 @@ maa_gpio_edge_mode(maa_gpio_context dev, gpio_edge_t mode)
length = snprintf(bu, sizeof(bu), "falling"); length = snprintf(bu, sizeof(bu), "falling");
break; break;
default: default:
fclose(edge); close(edge);
return MAA_ERROR_FEATURE_NOT_IMPLEMENTED; return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
} }
fwrite(bu, sizeof(char), length, edge); if (write(edge, bu, length*sizeof(char)) == -1) {
fprintf(stderr, "Failed to write to edge\n");
close(edge);
return MAA_ERROR_INVALID_RESOURCE;
}
fclose(edge); close(edge);
return MAA_SUCCESS; return MAA_SUCCESS;
} }
@@ -272,8 +278,8 @@ maa_gpio_mode(maa_gpio_context dev, gpio_mode_t mode)
char filepath[MAX_SIZE]; char filepath[MAX_SIZE];
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/drive", dev->pin); snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/drive", dev->pin);
FILE *drive; int drive = open(filepath, O_WRONLY);
if ((drive = fopen(filepath, "w")) == NULL) { if (drive == -1) {
fprintf(stderr, "Failed to open drive for writing!\n"); fprintf(stderr, "Failed to open drive for writing!\n");
return MAA_ERROR_INVALID_RESOURCE; return MAA_ERROR_INVALID_RESOURCE;
} }
@@ -294,12 +300,17 @@ maa_gpio_mode(maa_gpio_context dev, gpio_mode_t mode)
length = snprintf(bu, sizeof(bu), "hiz"); length = snprintf(bu, sizeof(bu), "hiz");
break; break;
default: default:
fclose(drive); close(drive);
return MAA_ERROR_FEATURE_NOT_IMPLEMENTED; return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
} }
fwrite(bu, sizeof(char), length, drive); if (write(drive, bu, length*sizeof(char)) == -1) {
fprintf(stderr, "Failed to write to drive mode!\n");
close(drive);
return MAA_ERROR_INVALID_RESOURCE;
fclose(drive); }
close(drive);
return MAA_SUCCESS; return MAA_SUCCESS;
} }
@@ -316,8 +327,9 @@ maa_gpio_dir(maa_gpio_context dev, gpio_dir_t dir)
char filepath[MAX_SIZE]; char filepath[MAX_SIZE];
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin); snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
FILE *direction; int direction = open(filepath, O_RDWR);
if ((direction = fopen(filepath, "w")) == NULL) {
if (direction == -1) {
fprintf(stderr, "Failed to open direction for writing!\n"); fprintf(stderr, "Failed to open direction for writing!\n");
return MAA_ERROR_INVALID_RESOURCE; return MAA_ERROR_INVALID_RESOURCE;
} }
@@ -332,12 +344,17 @@ maa_gpio_dir(maa_gpio_context dev, gpio_dir_t dir)
length = snprintf(bu, sizeof(bu), "in"); length = snprintf(bu, sizeof(bu), "in");
break; break;
default: default:
fclose(direction); close(direction);
return MAA_ERROR_FEATURE_NOT_IMPLEMENTED; return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
} }
fwrite(bu, sizeof(char), length, direction);
fclose(direction); if (write(direction, bu, length*sizeof(char)) == -1) {
fprintf(stderr, "Failed to write to direction\n");
close(direction);
return MAA_ERROR_INVALID_RESOURCE;
}
close(direction);
return MAA_SUCCESS; return MAA_SUCCESS;
} }
@@ -385,20 +402,22 @@ maa_gpio_write(maa_gpio_context dev, int value)
maa_result_t maa_result_t
maa_gpio_unexport(maa_gpio_context dev) maa_gpio_unexport(maa_gpio_context dev)
{ {
FILE *unexport_f; int unexport = open(SYSFS_CLASS_GPIO "/unexport", O_WRONLY);
if (unexport == -1) {
if ((unexport_f = fopen(SYSFS_CLASS_GPIO "/unexport", "w")) == NULL) {
fprintf(stderr, "Failed to open unexport for writing!\n"); fprintf(stderr, "Failed to open unexport for writing!\n");
return MAA_ERROR_INVALID_RESOURCE; return MAA_ERROR_INVALID_RESOURCE;
} }
char bu[MAX_SIZE]; char bu[MAX_SIZE];
int length = snprintf(bu, sizeof(bu), "%d", dev->pin); int length = snprintf(bu, sizeof(bu), "%d", dev->pin);
fwrite(bu, sizeof(char), length, unexport_f); if (write(unexport, bu, length*sizeof(char)) == -1) {
fclose(unexport_f); fprintf(stderr, "Failed to write to unexport\n");
close(unexport);
return MAA_ERROR_INVALID_RESOURCE;
}
close(unexport);
maa_gpio_isr_exit(dev); maa_gpio_isr_exit(dev);
return MAA_SUCCESS; return MAA_SUCCESS;
} }