flinklib
flinklib: flink C library for Linux
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
read_write.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <getopt.h>
5 #include <ctype.h>
6 
7 #include <flinklib.h>
8 
9 #define DEFAULT_DEV "/dev/flink0"
10 #define SIZE 4
11 
12 int main(int argc, char* argv[]) {
13  flink_dev* dev;
14  flink_subdev* subdev;
15  char* dev_name = DEFAULT_DEV;
16  uint8_t subdevice_id = 0;
17  char c;
18  uint32_t offset;
19  uint32_t value;
20  uint8_t write;
21  int error = 0;
22 
23  /* Compute command line arguments */
24  while((c = getopt(argc, argv, "d:s:o:v:rw")) != -1) {
25  switch(c) {
26  case 'd': // device
27  dev_name = optarg;
28  break;
29  case 's':
30  subdevice_id = atoi(optarg);
31  break;
32  case 'o':
33  offset = atoi(optarg);
34  break;
35  case 'r':
36  write = 0;
37  break;
38  case 'w':
39  write = 1;
40  break;
41  case 'v':
42  if(!write) {
43  fprintf(stderr, "You can not set a value if you want to read from a device!");
44  }
45  else {
46  value = atoi(optarg);
47  }
48  break;
49  case '?':
50  if(optopt == 'd' || optopt == 's' || optopt == 'o' || optopt == 'v') fprintf(stderr, "Option -%c requires an argument.\n", optopt);
51  else if(isprint(optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt);
52  else fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
53  return -1;
54  default:
55  abort();
56  }
57  }
58 
59  printf("Opening device %s...\n", dev_name);
60  dev = flink_open(dev_name);
61  if(dev == NULL) {
62  printf("Failed to open device!\n");
63  return -1;
64  }
65 
66  subdev = flink_get_subdevice_by_id(dev, subdevice_id);
67 
68  if(write) {
69  printf("Writing 0x%x (%u) to offset 0x%x at subdevice %u on device %s...\n", value, value, offset, subdevice_id, dev_name);
70  error = flink_write(subdev, offset, SIZE, &value);
71  if(dev == NULL) {
72  printf("Failure while writing to device: %u!\n", error);
73  return -1;
74  }
75  }
76  else {
77  printf("Reading from offset 0x%x at subdevice %u on device %s...\n", offset, subdevice_id, dev_name);
78  error = flink_read(subdev, offset, SIZE, &value);
79  if(dev == NULL) {
80  printf("Failure while reading from device: %u!\n", error);
81  return -1;
82  }
83  else {
84  printf("--> Read: 0x%x (%u)\n", value, value);
85  }
86  }
87 
88  printf("Closing device %s...\n", dev_name);
89  flink_close(dev);
90 
91  return EXIT_SUCCESS;
92 }
flink_dev * flink_open(const char *file_name)
Opens a flink device file.
Definition: base.c:112
int flink_close(flink_dev *dev)
Close an open flink device.
Definition: base.c:145
ssize_t flink_read(flink_subdev *subdev, uint32_t offset, uint8_t size, void *rdata)
Read from a flink subdevice.
Definition: lowlevel.c:71
#define DEFAULT_DEV
Definition: read_write.c:9
#define EXIT_SUCCESS
Definition: flinklib.h:117
flink_subdev * flink_get_subdevice_by_id(flink_dev *dev, uint8_t subdev_id)
Find subdevice of a device with a given id.
Definition: base.c:225
int main(int argc, char *argv[])
Definition: read_write.c:12
#define SIZE
Definition: read_write.c:10
ssize_t flink_write(flink_subdev *subdev, uint32_t offset, uint8_t size, void *wdata)
Write to a flink subdevice.
Definition: lowlevel.c:111