Matt Spinler | ea3d8a4 | 2018-08-07 13:29:34 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include "gpio_json.h" |
| 19 | |
| 20 | #define GPIO_FILE "/etc/default/obmc/gpio/gpio_defs.json" |
| 21 | |
| 22 | cJSON* load_json() |
| 23 | { |
| 24 | FILE* fd = fopen(GPIO_FILE, "r"); |
| 25 | if (!fd) |
| 26 | { |
| 27 | fprintf(stderr, "Unable to open GPIO JSON file %s\n", GPIO_FILE); |
| 28 | return NULL; |
| 29 | } |
| 30 | |
| 31 | fseek(fd, 0, SEEK_END); |
| 32 | long size = ftell(fd); |
| 33 | rewind(fd); |
| 34 | |
| 35 | char* data = malloc(size + 1); |
| 36 | |
| 37 | size_t rc = fread(data, 1, size, fd); |
| 38 | fclose(fd); |
| 39 | if (rc != size) |
| 40 | { |
| 41 | free(data); |
| 42 | fprintf(stderr, "Only read %d out of %ld bytes of GPIO file %s\n", |
| 43 | rc, size, GPIO_FILE); |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | data[size] = '\0'; |
| 48 | cJSON* json = cJSON_Parse(data); |
| 49 | free(data); |
| 50 | |
| 51 | if (json == NULL) |
| 52 | { |
| 53 | fprintf(stderr, "Failed parsing GPIO file %s\n", GPIO_FILE); |
| 54 | |
| 55 | const char* error_loc = cJSON_GetErrorPtr(); |
| 56 | if (error_loc != NULL) |
| 57 | { |
| 58 | fprintf(stderr, "JSON error at %s\n", error_loc); |
| 59 | } |
| 60 | } |
| 61 | return json; |
| 62 | } |