-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestio.c
123 lines (90 loc) · 2.79 KB
/
restio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "restio.h"
const static char* rest_button_messages[3] = {"presence","increase","decrease"};
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if(ptr == NULL) {
/* out of memory! */
perror("Not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
rest_handle *rest_init(const char *path,int deskid)
{
rest_handle* handle;
if(!(handle = malloc(sizeof(rest_handle)))) {
return 0;
}
handle->path = strdup(path);
handle->deskid = deskid;
return handle;
}
void rest_cleanup(rest_handle *handle)
{
free(handle->path);
free(handle);
}
int rest_press_button(rest_handle *handle,int which,rest_button_change direction,int *totals,short pcount)
{
struct MemoryStruct chunk;
struct curl_slist *headers = NULL;
CURL *curl;
CURLcode curlres;
char webbuffer[1024];
json_object * jobj;
char ord;
chunk.memory = malloc(1);
if(chunk.memory == 0) {
perror("Can't allocate curl return buffer.");
exit(1);
}
chunk.size = 0;
curl = curl_easy_init();
if(!curl)
{
perror("Can't open curl");
return -2;
}
if(direction != PRESENCE && direction != INCREASE && direction != DECREASE) {
return -1;
}
// Prepare to send data to server.
snprintf(webbuffer,1024,"{\"number\":%d,\"direction\":\"%s\",\"presence\":%d,\"deskid\":%d}",which+1,rest_button_messages[direction],pcount,handle->deskid);
//printf("Sending %s\n",webbuffer);
// Really long URL to include hashed auth creds.
//printf("Path = '%s'\n",handle->path);
curl_easy_setopt(curl,CURLOPT_URL,handle->path);
headers = curl_slist_append(headers,"Accept: application/json");
headers = curl_slist_append(headers,"Content-Type: application/json");
headers = curl_slist_append(headers,"charset: utf-8");
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,webbuffer);
curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);
//curl_easy_setopt(curl,CURLOPT_VERBOSE,1L);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,WriteMemoryCallback);
curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&chunk);
curlres = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(curlres != CURLE_OK) {
return -1;
}
jobj = json_tokener_parse(chunk.memory);
ord = 0;
json_object_object_foreach(jobj,key,val) {
if(key[0] >= '1' && key[0] <= '4' && key[1] == 0) {
totals[key[0] - '1'] = json_object_get_int(val);
ord++;
}
}
json_object_put(jobj);
if(ord != 0) {
return 0;
}
return -1;
}