~jfi/ubuntu/quantal/psensor/new-upstream

« back to all changes in this revision

Viewing changes to src/rsensor.c

  • Committer: Package Import Robot
  • Author(s): Micah Gersten
  • Date: 2012-01-21 22:41:34 UTC
  • mfrom: (3.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120121224134-mty23d3917ng5ujd
Tags: 0.6.2.16-1ubuntu1
* Merge from Debian testing.  Remaining changes:
  + Modifed build-dep to add NVidia and Application Indicator support
  + Only add an nvidia-settings build-dep on i386 and amd64

* Add a patch to drop the deprecated g_thread_init() call
  - add debian/patches/drop_g_thread_init.patch
  - add debian/patches/series

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
    Copyright (C) 2010-2011 wpitchoune@gmail.com
3
 
 
4
 
    This program is free software; you can redistribute it and/or modify
5
 
    it under the terms of the GNU General Public License as published by
6
 
    the Free Software Foundation; either version 2 of the License, or
7
 
    (at your option) any later version.
8
 
 
9
 
    This program is distributed in the hope that it will be useful,
10
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
    GNU General Public License for more details.
13
 
 
14
 
    You should have received a copy of the GNU General Public License
15
 
    along with this program; if not, write to the Free Software
16
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
 
    02110-1301 USA
18
 
*/
19
 
 
 
2
 * Copyright (C) 2010-2011 jeanfi@gmail.com
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; either version 2 of the
 
7
 * License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but
 
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
17
 * 02110-1301 USA
 
18
 */
20
19
#include <locale.h>
21
20
#include <libintl.h>
22
21
#define _(str) gettext(str)
23
22
 
24
 
#include "plib/url.h"
25
 
#include "server/server.h"
26
 
 
27
23
#include <stdio.h>
28
24
#include <stdlib.h>
29
25
#include <string.h>
30
26
 
31
27
#include <curl/curl.h>
32
 
#include <json/json.h>
33
28
 
 
29
#include "psensor_json.h"
34
30
#include "rsensor.h"
 
31
#include "server/server.h"
 
32
#include "url.h"
35
33
 
36
34
struct ucontent {
37
35
        char *data;
40
38
 
41
39
static CURL *curl;
42
40
 
43
 
size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
 
41
static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
44
42
{
45
 
        size_t realsize = size * nmemb;
46
 
        struct ucontent *mem = (struct ucontent *)userp;
 
43
        size_t realsize;
 
44
        struct ucontent *mem;
 
45
 
 
46
        realsize = size * nmemb;
 
47
        mem = (struct ucontent *)userp;
47
48
 
48
49
        mem->data = realloc(mem->data, mem->len + realsize + 1);
49
50
 
54
55
        return realsize;
55
56
}
56
57
 
57
 
char *create_api_1_0_sensors_url(const char *base_url)
 
58
static char *create_api_1_0_sensors_url(const char *base_url)
58
59
{
59
 
        char *nurl = url_normalize(base_url);
60
 
        int n = strlen(nurl) + strlen(URL_BASE_API_1_0_SENSORS) + 1;
61
 
        char *ret = malloc(n);
 
60
        char *nurl, *ret;
 
61
        int n;
 
62
 
 
63
        nurl = url_normalize(base_url);
 
64
        n = strlen(nurl) + strlen(URL_BASE_API_1_0_SENSORS) + 1;
 
65
        ret = malloc(n);
62
66
 
63
67
        strcpy(ret, nurl);
64
68
        strcat(ret, URL_BASE_API_1_0_SENSORS);
68
72
        return ret;
69
73
}
70
74
 
71
 
struct psensor *json_object_to_psensor(json_object * o,
72
 
                                       const char *sensors_url,
73
 
                                       int values_max_length)
74
 
{
75
 
        json_object *oid;
76
 
        json_object *oname;
77
 
        json_object *otype;
78
 
        struct psensor *s;
79
 
        char *eid;
80
 
        char *url;
81
 
 
82
 
        oid = json_object_object_get(o, "id");
83
 
        oname = json_object_object_get(o, "name");
84
 
        otype = json_object_object_get(o, "type");
85
 
 
86
 
        eid = url_encode(json_object_get_string(oid));
87
 
        url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1);
88
 
        sprintf(url, "%s/%s", sensors_url, eid);
89
 
 
90
 
        s = psensor_create(strdup(url),
91
 
                           strdup(json_object_get_string(oname)),
92
 
                           json_object_get_int(otype) | SENSOR_TYPE_REMOTE,
93
 
                           values_max_length);
94
 
        s->url = url;
95
 
 
96
 
        free(eid);
97
 
 
98
 
        return s;
99
 
}
100
 
 
101
75
void rsensor_init()
102
76
{
103
77
        curl = curl_easy_init();
104
78
}
105
79
 
106
 
void rsensor_end()
 
80
void rsensor_cleanup()
107
81
{
108
82
        curl_easy_cleanup(curl);
109
83
}
110
84
 
111
 
json_object *get_json_object(const char *url)
 
85
static json_object *get_json_object(const char *url)
112
86
{
113
 
 
114
87
        struct ucontent chunk;
115
 
        json_object *obj = NULL;
 
88
        json_object *obj;
 
89
 
 
90
        obj = NULL;
116
91
 
117
92
        if (!curl)
118
93
                return NULL;
125
100
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
126
101
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
127
102
 
 
103
        log_debug("HTTP request %s", url);
128
104
        if (curl_easy_perform(curl) == CURLE_OK)
129
105
                obj = json_tokener_parse(chunk.data);
130
106
        else
131
 
                fprintf(stderr, _("ERROR: Fail to connect to: %s\n"), url);
 
107
                log_printf(LOG_ERR, _("Fail to connect to: %s"), url);
132
108
 
133
109
        free(chunk.data);
134
110
 
138
114
struct psensor **get_remote_sensors(const char *server_url,
139
115
                                    int values_max_length)
140
116
{
141
 
        struct psensor **sensors = NULL;
 
117
        struct psensor **sensors, *s;
142
118
        char *url;
143
119
        json_object *obj;
 
120
        int i, n;
 
121
 
 
122
        sensors = NULL;
144
123
 
145
124
        url = create_api_1_0_sensors_url(server_url);
146
125
 
147
126
        obj = get_json_object(url);
148
127
 
149
128
        if (obj && !is_error(obj)) {
150
 
                int i;
151
 
                int n = json_object_array_length(obj);
 
129
                n = json_object_array_length(obj);
152
130
                sensors = malloc((n + 1) * sizeof(struct psensor *));
153
131
 
154
132
                for (i = 0; i < n; i++) {
155
 
                        struct psensor *s = json_object_to_psensor
156
 
                            (json_object_array_get_idx(obj, i),
157
 
                             url,
158
 
                             values_max_length);
 
133
                        s = psensor_new_from_json
 
134
                                (json_object_array_get_idx(obj, i),
 
135
                                 url,
 
136
                                 values_max_length);
159
137
                        sensors[i] = s;
160
138
                }
161
139
 
163
141
 
164
142
                json_object_put(obj);
165
143
        } else {
166
 
                fprintf(stderr, _("ERROR: Invalid content: %s\n"), url);
 
144
                log_printf(LOG_ERR, _("Invalid content: %s"), url);
167
145
        }
168
146
 
169
147
        free(url);
178
156
 
179
157
void remote_psensor_update(struct psensor *s)
180
158
{
181
 
        json_object *obj = get_json_object(s->url);
 
159
        json_object *obj;
 
160
 
 
161
        obj = get_json_object(s->url);
182
162
 
183
163
        if (obj && !is_error(obj)) {
184
164
                json_object *om;
196
176
                        tv.tv_usec = 0;
197
177
 
198
178
                        psensor_set_current_measure
199
 
                            (s, json_object_get_double(ov), tv);;
 
179
                            (s, json_object_get_double(ov), tv);
200
180
                }
201
181
 
202
182
                json_object_put(obj);
203
183
        } else {
204
 
                printf(_("ERROR: Invalid JSON: %s\n"), s->url);
 
184
                log_printf(LOG_ERR, _("Invalid JSON: %s"), s->url);
205
185
        }
206
186
 
207
187
}
208
188
 
209
189
void remote_psensor_list_update(struct psensor **sensors)
210
190
{
211
 
        struct psensor **cur = sensors;
 
191
        struct psensor **cur;
212
192
 
 
193
        cur = sensors;
213
194
        while (*cur) {
214
195
                struct psensor *s = *cur;
215
196