Revision 8f961e44
Added getopt for inputting serial device and CRM server
mainbox/main.c | ||
---|---|---|
1 | 1 |
#include "tool.h" |
2 |
#include "query.h" |
|
2 | 3 |
#include <unistd.h> |
3 | 4 |
#include <signal.h> |
4 | 5 |
#include <strings.h> |
... | ... | |
18 | 19 |
run = 0; |
19 | 20 |
} |
20 | 21 |
|
22 |
void print_usage(const char *name) { |
|
23 |
printf("Usage: %s [-h] [-d serial_device] [-s db_server[:port]]\n", name); |
|
24 |
printf(" -h prints this message\n"); |
|
25 |
printf(" -d specifies the serial port for Modbus\n"); |
|
26 |
printf(" defaults to /dev/ttyUSB0\n"); |
|
27 |
printf(" -s specifies the server where the CRM is running\n"); |
|
28 |
printf(" defaults to minecraft.roboclub.org:8000\n"); |
|
29 |
} |
|
30 |
|
|
21 | 31 |
int main(int argc, char **argv) { |
22 |
int i; |
|
32 |
int i, opt;
|
|
23 | 33 |
struct sigaction sigact; |
24 | 34 |
const char *device = "/dev/ttyUSB0"; |
35 |
const char *server = "minecraft.roboclub.org:8000"; |
|
36 |
|
|
37 |
while ((opt = getopt(argc, argv, "hd:s:")) != -1) { |
|
38 |
switch (opt) { |
|
39 |
case 'h': |
|
40 |
print_usage(argv[0]); |
|
41 |
return 0; |
|
42 |
case 'd': |
|
43 |
device = optarg; |
|
44 |
break; |
|
45 |
case 's': |
|
46 |
server = optarg; |
|
47 |
break; |
|
48 |
default: |
|
49 |
print_usage(argv[0]); |
|
50 |
return 1; |
|
51 |
} |
|
52 |
} |
|
25 | 53 |
|
26 |
// TODO getopts to get device name |
|
54 |
printf("Serial device: %s\n", device); |
|
55 |
printf("CRM server: http://%s/\n", server); |
|
27 | 56 |
|
28 | 57 |
bzero(&sigact, sizeof(sigact)); |
29 | 58 |
sigact.sa_handler = sigint; |
... | ... | |
31 | 60 |
sigemptyset(&sigact.sa_mask); |
32 | 61 |
sigaction(SIGINT, &sigact, NULL); |
33 | 62 |
|
63 |
if (query_init(server)) { |
|
64 |
return 1; |
|
65 |
} |
|
66 |
|
|
34 | 67 |
if (tool_init_mb(device)) { |
35 | 68 |
return 1; |
36 | 69 |
} |
mainbox/query.c | ||
---|---|---|
2 | 2 |
#include <stdio.h> |
3 | 3 |
#include <curl/curl.h> |
4 | 4 |
|
5 |
int query_init() { |
|
5 |
const char *server; |
|
6 |
|
|
7 |
int query_init(const char *server_name) { |
|
6 | 8 |
CURLcode error_code; |
7 | 9 |
|
10 |
server = server_name; |
|
11 |
|
|
8 | 12 |
error_code = curl_global_init(CURL_GLOBAL_NOTHING); |
9 | 13 |
if (error_code) |
10 | 14 |
fprintf(stderr, "curl_global_init: %s\n", curl_easy_strerror(error_code)); |
... | ... | |
18 | 22 |
|
19 | 23 |
static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) { |
20 | 24 |
int *resultp = userp; |
25 |
char *str = buffer; |
|
26 |
|
|
27 |
if (size*nmemb > 0 && str[0] == '1') |
|
28 |
*resultp = 1; |
|
29 |
else |
|
30 |
*resultp = 0; |
|
21 | 31 |
|
22 |
return fwrite(buffer, size, nmemb, stdout);
|
|
32 |
return nmemb;
|
|
23 | 33 |
} |
24 | 34 |
|
25 | 35 |
int query_user_permission(int tool_id, int user_id) { |
... | ... | |
32 | 42 |
if (handle == NULL) |
33 | 43 |
return 0; |
34 | 44 |
|
35 |
sprintf(url, "http://roboclub.org/tooltron/%08x/%d/", user_id, tool_id); |
|
36 |
/* TODO temporary */ sprintf(url, "http://www.google.com"); |
|
45 |
sprintf(url, "http://%s/roboauth/%08x/%d/", server, user_id, tool_id); |
|
37 | 46 |
error_code = curl_easy_setopt(handle, CURLOPT_URL, url); |
38 | 47 |
if (error_code) goto error; |
39 | 48 |
|
... | ... | |
51 | 60 |
|
52 | 61 |
error: |
53 | 62 |
fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code)); |
54 |
fprintf(stderr, " when authenticating user %08x on tool %d\n", |
|
63 |
fprintf(stderr, " when authenticating user %08x on tool %d\n",
|
|
55 | 64 |
user_id, tool_id); |
56 | 65 |
curl_easy_cleanup(handle); |
57 | 66 |
return 0; |
Also available in: Unified diff