• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libiio - Library for interfacing industrial I/O (IIO) devices
3  *
4  * Copyright (C) 2014 Analog Devices, Inc.
5  * Author: Paul Cercueil <paul.cercueil@analog.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * */
18 
19 #include <getopt.h>
20 #include <iio.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #ifndef _WIN32
25 #define _strdup strdup
26 #endif
27 
28 #define MY_NAME "iio_genxml"
29 
30 enum backend {
31 	LOCAL,
32 	XML,
33 	NETWORK,
34 	AUTO,
35 };
36 
37 static const struct option options[] = {
38 	  {"help", no_argument, 0, 'h'},
39 	  {"xml", required_argument, 0, 'x'},
40 	  {"network", required_argument, 0, 'n'},
41 	  {"uri", required_argument, 0, 'u'},
42 	  {0, 0, 0, 0},
43 };
44 
45 static const char *options_descriptions[] = {
46 	"Show this help and quit.",
47 	"Use the XML backend with the provided XML file.",
48 	"Use the network backend with the provided hostname.",
49 	"Use the context with the provided URI.",
50 };
51 
usage(void)52 static void usage(void)
53 {
54 	unsigned int i;
55 
56 	printf("Usage:\n\t" MY_NAME " [-x <xml_file>]\n\t"
57 			MY_NAME " [-u <uri>]\n\t"
58 			MY_NAME " [-n <hostname>]\n\nOptions:\n");
59 	for (i = 0; options[i].name; i++)
60 		printf("\t-%c, --%s\n\t\t\t%s\n",
61 					options[i].val, options[i].name,
62 					options_descriptions[i]);
63 }
64 
main(int argc,char ** argv)65 int main(int argc, char **argv)
66 {
67 	char *xml;
68 	struct iio_context *ctx;
69 	int c, option_index = 0;
70 	const char *arg_uri = NULL;
71 	const char *arg_xml = NULL;
72 	const char *arg_ip = NULL;
73 	enum backend backend = LOCAL;
74 
75 	while ((c = getopt_long(argc, argv, "+hn:x:u:",
76 					options, &option_index)) != -1) {
77 		switch (c) {
78 		case 'h':
79 			usage();
80 			return EXIT_SUCCESS;
81 		case 'n':
82 			if (backend != LOCAL) {
83 				fprintf(stderr, "-x and -n are mutually exclusive\n");
84 				return EXIT_FAILURE;
85 			}
86 			backend = NETWORK;
87 			arg_ip = optarg;
88 			break;
89 		case 'x':
90 			if (backend != LOCAL) {
91 				fprintf(stderr, "-x and -n are mutually exclusive\n");
92 				return EXIT_FAILURE;
93 			}
94 			backend = XML;
95 			arg_xml = optarg;
96 			break;
97 		case 'u':
98 			arg_uri = optarg;
99 			backend = AUTO;
100 			break;
101 		case '?':
102 			return EXIT_FAILURE;
103 		}
104 	}
105 
106 	if (optind != argc) {
107 		fprintf(stderr, "Incorrect number of arguments.\n\n");
108 		usage();
109 		return EXIT_FAILURE;
110 	}
111 
112 	if (backend == AUTO)
113 		ctx = iio_create_context_from_uri(arg_uri);
114 	else if (backend == XML)
115 		ctx = iio_create_xml_context(arg_xml);
116 	else if (backend == NETWORK)
117 		ctx = iio_create_network_context(arg_ip);
118 	else
119 		ctx = iio_create_default_context();
120 
121 	if (!ctx) {
122 		fprintf(stderr, "Unable to create IIO context\n");
123 		return EXIT_FAILURE;
124 	}
125 
126 	xml = _strdup(iio_context_get_xml(ctx));
127 	if (!xml) {
128 		iio_context_destroy(ctx);
129 		return EXIT_FAILURE;
130 	}
131 
132 	printf("XML generated:\n\n%s\n\n", xml);
133 
134 	iio_context_destroy(ctx);
135 
136 	ctx = iio_create_xml_context_mem(xml, strlen(xml));
137 	if (!ctx) {
138 		fprintf(stderr, "Unable to re-generate context\n");
139 	} else {
140 		printf("Context re-creation from generated XML succeeded!\n");
141 		iio_context_destroy(ctx);
142 	}
143 	free(xml);
144 	return EXIT_SUCCESS;
145 }
146