Revision 9e764ac9

View differences:

mainbox/Makefile
1
SRC=main.c tool.c
2
HDR=tool.h ../tooltron_mb.h
1
SRC=main.c tool.c query.c
2
HDR=tool.h query.h ../tooltron_mb.h
3 3

  
4
FLAGS=-O2 -g -Wall -I.. `pkg-config --cflags --libs libmodbus`
4
FLAGS=-O2 -g -Wall -I.. `pkg-config --cflags --libs libmodbus libcurl`
5 5

  
6 6
all: tooltron
7 7

  
mainbox/query.c
1
#include "query.h"
2
#include <stdio.h>
3
#include <curl/curl.h>
4

  
5
int query_init() {
6
  CURLcode error_code;
7

  
8
  error_code = curl_global_init(CURL_GLOBAL_NOTHING);
9
  if (error_code)
10
    fprintf(stderr, "curl_global_init: %s\n", curl_easy_strerror(error_code));
11

  
12
  return error_code;
13
}
14

  
15
void query_cleanup() {
16
  curl_global_cleanup();
17
}
18

  
19
static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
20
  int *resultp = userp;
21

  
22
  return fwrite(buffer, size, nmemb, stdout);
23
}
24

  
25
int query_user_permission(int tool_id, int user_id) {
26
  CURL* handle;
27
  CURLcode error_code;
28
  char url[1024];
29
  int result = 0;
30

  
31
  handle = curl_easy_init();
32
  if (handle == NULL)
33
    return 0;
34

  
35
  sprintf(url, "http://roboclub.org/tooltron/%08x/%d/", user_id, tool_id);
36
  /* TODO temporary */ sprintf(url, "http://www.google.com");
37
  error_code = curl_easy_setopt(handle, CURLOPT_URL, url);
38
  if (error_code) goto error;
39

  
40
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
41
  if (error_code) goto error;
42

  
43
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
44
  if (error_code) goto error;
45

  
46
  error_code = curl_easy_perform(handle);
47
  if (error_code) goto error;
48

  
49
  curl_easy_cleanup(handle);
50
  return result;
51

  
52
error:
53
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
54
  fprintf(stderr, "     when authenticating user %08x on tool %d\n",
55
      user_id, tool_id);
56
  curl_easy_cleanup(handle);
57
  return 0;
58
}
mainbox/query.h
1
#ifndef QUERY_H
2
#define QUERY_H
3

  
4
int query_init();
5
void query_cleanup();
6
int query_user_permission(int tool_id, int user_id);
7

  
8
#endif

Also available in: Unified diff