1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <libdev/status_client.h>
6 #include <libmisc/misc_buf.h>
7 #include <libmisc/misc_init.h>
8
9 #include <devel/http/cgic.h>
10 #include <devel/http/ssi.h>
11 #include <devel/http/html.h>
12
13
14
15 /**
16 *
17 * Output a form and a command summary to allow the user to write
18 * commands to the device.
19 *
20 * required args:
21 * device: the device to cat
22 * optional args:
23 * cmd: a command to send to the device
24 */
25
26 static FILE* ostream = NULL;
27
28 static char tdev[128];
29
30 static char metrics[128] = "sympathy/metrics";
31 static char status[128] = "sympathy/status";
32 static char battery[128] = "sympathy/battery";
33 static char command[128] = "sympathy/cmd";
34
35
36 static
37 void form_init()
38 {
39 fprintf(ostream, "<br><br><form method=\"POST\" name=\"catform\" action=\"/cgi-bin/device.cgi\">\n");
40 fprintf(ostream, "<input type=\"hidden\" name=\"device\" value=\"%s\">\n", tdev);
41 }
42
43 static
44 void metrics_form()
45 {
46 /* Link to command summary */
47 fprintf(ostream, "<a href=\"#Command Summary\">View Command Summary</a>");
48
49 form_init();
50 fprintf(ostream, "<font size=3>Command:</font><input type=\"text\" name=\"arg\" size=\"4\" maxlength=\"10\">\n");
51 fprintf(ostream, "<br><br><input type=\"submit\" value=\"Send Command\">\n");
52 fprintf(ostream, "</form><br>\n");
53 }
54
55 static
56 void status_form()
57 {
58 form_init();
59 fprintf(ostream, "<font size=3><i>Use this form to change a node's minimum required percent of packets expected by the sink.</i></font><br>");
60 fprintf(ostream, "<font size=3>Node ID:</font><input type=\"text\" name=\"node\" size=\"4\" maxlength=\"20\"><br>\n");
61 fprintf(ostream, "<font size=3>Component Name:</font><input type=\"text\" name=\"comp_name\" size=\"6\" maxlength=\"20\"><br>\n");
62 fprintf(ostream, "<font size=3>Rx %%:</font><input type=\"text\" name=\"rx_percent\" size=\"4\" maxlength=\"10\">\n");
63 fprintf(ostream, "<br><br><input type=\"submit\" value=\"Submit\">\n");
64 fprintf(ostream, "</form><br>\n");
65 }
66
67 static
68 void battery_form()
69 {
70 form_init();
71 fprintf(ostream, "<font size=3><i>Use this form to update a node's battery.</i></font><br>");
72 fprintf(ostream, "<font size=3>Node ID:</font><input type=\"text\" name=\"node\" size=\"4\" maxlength=\"20\"><br>\n");
73 fprintf(ostream, "<font size=3>Voltage(mV):</font><input type=\"text\" name=\"voltage\" size=\"4\" maxlength=\"10\">\n");
74 fprintf(ostream, "<br><br><input type=\"submit\" value=\"Submit\">\n");
75 fprintf(ostream, "</form><br>\n");
76 }
77
78 static
79 void command_form()
80 {
81 form_init();
82 fprintf(ostream, "<font size=3><i>Use this form to ping a node to gather information about it.</i></font><br>");
83 fprintf(ostream, "<font size=3>ID of node to ping:</font><input type=\"text\" name=\"ping\" size=\"4\" maxlength=\"20\">\n");
84 fprintf(ostream, "<br><input type=\"submit\" value=\"Ping\">\n");
85 fprintf(ostream, "</form><br><br>\n");
86
87 form_init();
88 fprintf(ostream, "<font size=3><i>Use this form to change the period of automatic metric floods (pd=0 => stop metric floods).</i></font><br>");
89 fprintf(ostream, "<font size=3>Period:</font><input type=\"text\" name=\"auto_period\" size=\"4\" maxlength=\"10\">\n");
90 fprintf(ostream, "<br><br><input type=\"submit\" value=\"Submit\"><br><br><br>\n");
91 fprintf(ostream, "</form><br>\n");
92 }
93
94
95
96 /**
97 * This function is called by the library before the process_cgi
98 * function. You can use it to hijack the header being printed so you
99 * can do fancy redirects or play with the cookies or other
100 * things. You can use the cgic library calls, or do it yourself using
101 * the output_stream. This is NOT passed through the SSI parser.
102 *
103 * This function has a weak binding. You only need to define it if you
104 * change it!
105 */
106 void http_header(FILE* output_stream) {
107 cgiHeaderContentType("text/html");
108 fflush(output_stream);
109 }
110
111
112 int process_cgi(FILE* output_stream) {
113
114 buf_t *data = NULL;
115 int fd = 0;
116 char device[128];
117 char *final_device = NULL;
118 char arg[128], node[128], comp_name[128], rx_percent[128];
119 char voltage[128], ping[128], auto_period[128];
120 int hasarg = 0;
121 int length = 0;
122
123 ostream = output_stream;
124
125 /* always must have device */
126 if (cgiFormStringSpaceNeeded("device", &length) == cgiFormSuccess) {
127 if (length > 128)
128 goto out;
129 cgiFormString("device", tdev, length);
130 }
131 else {
132 fprintf(output_stream, "Please give full path to status device!\n");
133 goto out;
134 }
135
136 buf_t *title = buf_new();
137 buf_t *subtitle = buf_new();
138 bufprintf(title, "Device: %s", tdev);
139 bufprintf(subtitle, "Command Interface");
140 html_banner(output_stream, title->buf, subtitle->buf, NULL);
141
142
143 /* Link back to main device files page */
144 fprintf(output_stream, "<a href=\"/cgi-bin/sympathy.cgi\"><h3>Back to Sympathy Device Files page.</h3></a><br>\n");
145
146
147 if(!strcmp(tdev, metrics))
148 {
149 metrics_form();
150 /* check for arg to send to device */
151 if (cgiFormStringSpaceNeeded("arg", &length) == cgiFormSuccess) {
152 if (length > 128)
153 goto out;
154 cgiFormString("arg", arg, length);
155 hasarg = 1;
156 }
157 }
158
159 if(!strcmp(tdev, status))
160 {
161 status_form();
162 /* check for node argument to send to device */
163 if (cgiFormStringSpaceNeeded("node", &length) == cgiFormSuccess) {
164 if (length > 128)
165 goto out;
166 cgiFormString("node", node, length);
167
168 /* check for component name argument to send to device */
169 if (cgiFormStringSpaceNeeded("comp_name", &length) == cgiFormSuccess) {
170 if (length > 128)
171 goto out;
172 cgiFormString("comp_name", comp_name, length);
173
174 /* check for another argument to send to device */
175 if (cgiFormStringSpaceNeeded("rx_percent", &length) == cgiFormSuccess) {
176 if (length > 128)
177 goto out;
178
179 cgiFormString("rx_percent", rx_percent, length);
180 hasarg = 1; // must have all 3 arguments
181
182 /* concatenate all arguments */
183 strcat(strcpy(arg, "node="), node);
184 strcat(arg, ":");
185 strcat(strcat(arg, "comp-name="), comp_name);
186 strcat(arg, ":");
187 strcat(strcat(arg, "rx_percent="), rx_percent);
188 }
189 }
190 }
191 }
192
193 if(!strcmp(tdev, battery))
194 {
195 battery_form();
196 /* check for node argument to send to device */
197 if (cgiFormStringSpaceNeeded("node", &length) == cgiFormSuccess) {
198 if (length > 128)
199 goto out;
200 cgiFormString("node", node, length);
201
202 /* check for voltage argument to send to device */
203 if (cgiFormStringSpaceNeeded("voltage", &length) == cgiFormSuccess) {
204 if (length > 128)
205 goto out;
206
207 cgiFormString("voltage", voltage, length);
208 hasarg = 1; // must have both arguments
209
210 /* concatenate arguments */
211 strcat(strcpy(arg, "node="), node);
212 strcat(arg, ":");
213 strcat(strcat(arg, "voltage="), voltage);
214 }
215 }
216 }
217
218 if(!strcmp(tdev, command))
219 {
220 command_form();
221 /* check for ping argument to send to device */
222 if (cgiFormStringSpaceNeeded("ping", &length) == cgiFormSuccess) {
223 if (length > 128)
224 goto out;
225
226 cgiFormString("ping", ping, length);
227 hasarg = 1;
228
229 /* format argument */
230 strcat(strcpy(arg, "ping="), ping);
231 }
232
233 /* check for auto_period argument to send to device */
234 if (cgiFormStringSpaceNeeded("auto_period", &length) == cgiFormSuccess) {
235 if (length > 128)
236 goto out;
237
238 cgiFormString("auto_period", auto_period, length);
239 hasarg = 1;
240
241 /* format argument */
242 strcat(strcpy(arg, "auto_period="), auto_period);
243 }
244 }
245
246
247 /* add /dev */
248 sprintf(device, "%s%s", "/dev/", tdev);
249 final_device = sim_path(device);
250
251 fprintf(output_stream, "\n\n<!-- device.cgi reporting device %s -->\n", final_device);
252 fd = open(final_device, O_RDWR);
253
254 if (fd == -1) {
255 fprintf(output_stream, "\n\n<!-- device.cgi error on open: %m -->\n");
256 goto out;
257 }
258
259
260 g_status_client_set_output_mode(fd, STATUS_M_HTML);
261
262 int status;
263 if (hasarg) {
264 status = write(fd, arg, strlen(arg));
265 if (status < 0) {
266 fprintf(output_stream, "\n\n<!-- device.cgi error on write arg '%s': %m -->\n", arg);
267 fprintf(output_stream, "<br><br>ERROR sending command - please check your syntax.<br><br>'%s\n", arg);
268 }
269 }
270
271 data = g_status_client_read(fd, NULL);
272
273 fprintf(output_stream, "%s", buf_get(data));
274
275 close(fd);
276 fprintf(output_stream, "<!-- device.cgi done -->\n\n");
277 buf_free(data);
278 buf_free(title);
279 buf_free(subtitle);
280
281 out:
282 return 0;
283 }
284
285
286
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.