source: UsbWattMeter/trunk/src/usb_watt_meter/client.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 8 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 9.3 KB
RevLine 
[164]1/*
2 * TOPPERS ECHONET Lite Communication Middleware
3 *
4 * Copyright (C) 2016 Cores Co., Ltd. Japan
5 *
6 * ã‹L’˜ìŒ ŽÒ‚́CˆÈ‰º‚Ì(1)`(4)‚ÌðŒ‚ð–ž‚½‚·ê‡‚ÉŒÀ‚èC–{ƒ\ƒtƒgƒEƒF
7 * ƒAi–{ƒ\ƒtƒgƒEƒFƒA‚ð‰ü•Ï‚µ‚½‚à‚Ì‚ðŠÜ‚ށDˆÈ‰º“¯‚¶j‚ðŽg—pE•¡»E‰ü
8 * •ÏEÄ”z•ziˆÈ‰ºC—˜—p‚ƌĂԁj‚·‚邱‚Ƃ𖳏ž‚Å‹–‘ø‚·‚éD
9 * (1) –{ƒ\ƒtƒgƒEƒFƒA‚ðƒ\[ƒXƒR[ƒh‚ÌŒ`‚Å—˜—p‚·‚éê‡‚ɂ́Cã‹L‚Ì’˜ì
10 * Œ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’肪C‚»‚Ì‚Ü‚Ü‚ÌŒ`‚Ń\[
11 * ƒXƒR[ƒh’†‚ÉŠÜ‚Ü‚ê‚Ä‚¢‚邱‚ƁD
12 * (2) –{ƒ\ƒtƒgƒEƒFƒA‚ðCƒ‰ƒCƒuƒ‰ƒŠŒ`Ž®‚ȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
13 * —p‚Å‚«‚éŒ`‚ōĔz•z‚·‚éê‡‚ɂ́CÄ”z•z‚É”º‚¤ƒhƒLƒ…
14ƒƒ“ƒgi—˜—p
15 * ŽÒƒ}ƒjƒ…
16ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L
17 * ‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
18 * (3) –{ƒ\ƒtƒgƒEƒFƒA‚ðC‹@Ší‚É‘g‚ݍž‚ނȂǁC‘¼‚̃\ƒtƒgƒEƒFƒAŠJ”­‚ÉŽg
19 * —p‚Å‚«‚È‚¢Œ`‚ōĔz•z‚·‚éê‡‚ɂ́CŽŸ‚Ì‚¢‚¸‚ê‚©‚ÌðŒ‚ð–ž‚½‚·‚±
20 * ‚ƁD
21 * (a) Ä”z•z‚É”º‚¤ƒhƒLƒ…
22ƒƒ“ƒgi—˜—pŽÒƒ}ƒjƒ…
23ƒAƒ‹‚Ȃǁj‚ɁCã‹L‚Ì’˜
24 * ìŒ •\Ž¦C‚±‚Ì—˜—pðŒ‚¨‚æ‚щº‹L‚Ì–³•ÛØ‹K’è‚ðŒfÚ‚·‚邱‚ƁD
25 * (b) Ä”z•z‚ÌŒ`‘Ô‚ðC•Ê‚É’è‚ß‚é•û–@‚É‚æ‚Á‚āCTOPPERSƒvƒƒWƒFƒNƒg‚É
26 * •ñ‚·‚邱‚ƁD
27 * (4) –{ƒ\ƒtƒgƒEƒFƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚é‚¢‚©‚Ȃ鑹
28 * ŠQ‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð–Ɛӂ·‚邱‚ƁD
29 * ‚Ü‚½C–{ƒ\ƒtƒgƒEƒFƒA‚̃†[ƒU‚Ü‚½‚̓Gƒ“ƒhƒ†[ƒU‚©‚ç‚Ì‚¢‚©‚Ȃ闝
30 * —R‚ÉŠî‚­¿‹‚©‚ç‚àCã‹L’˜ìŒ ŽÒ‚¨‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚ð
31 * –Ɛӂ·‚邱‚ƁD
32 *
33 * –{ƒ\ƒtƒgƒEƒFƒA‚́C–³•ÛØ‚Å’ñ‹Ÿ‚³‚ê‚Ä‚¢‚é‚à‚Ì‚Å‚ ‚éDã‹L’˜ìŒ ŽÒ‚¨
34 * ‚æ‚ÑTOPPERSƒvƒƒWƒFƒNƒg‚́C–{ƒ\ƒtƒgƒEƒFƒA‚ÉŠÖ‚µ‚āC“Á’è‚ÌŽg—p–Ú“I
35 * ‚ɑ΂·‚é“K‡«‚àŠÜ‚߂āC‚¢‚©‚È‚é•ÛØ‚às‚í‚È‚¢D‚Ü‚½C–{ƒ\ƒtƒgƒEƒF
36 * ƒA‚Ì—˜—p‚É‚æ‚è’¼Ú“I‚Ü‚½‚͊ԐړI‚ɐ¶‚¶‚½‚¢‚©‚Ȃ鑹ŠQ‚ÉŠÖ‚µ‚Ä‚àC‚»
37 * ‚̐ӔC‚𕉂í‚È‚¢D
38 *
39 * @(#) $Id: client.c 164 2016-03-07 11:33:50Z coas-nagasima $
40 */
41#include <lib/curl_setup.h>
42#include <curl/curl.h>
43#include <wolfssl/wolfcrypt/wc_port.h>
44#include <wolfssl/ssl.h>
45#include <lwip/sockets.h>
46#include <string.h>
47#include "main.h"
48#include "kernel_cfg.h"
49#include "ff.h"
50
51#define SKIP_PEER_VERIFICATION
52//#define SKIP_HOSTNAME_VERIFICATION
53char response[80];
54
55void client_init(void)
56{
57 uITRON4_minit(ITRON_POOL_SIZE);
58
59 curl_global_init(CURL_GLOBAL_DEFAULT);
60}
61
62size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
63{
64 int rest = size * nmemb;
65 int len;
66
67 while (rest > 0) {
68 len = rest;
69 if (len > (sizeof(response) - 1)) {
70 len = sizeof(response) - 1;
71 }
72
73 memcpy(response, buffer, len);
74
75 response[len] = '\0';
76
77 syslog(LOG_NOTICE, response);
78
79 dly_tsk(100);
80
81 rest -= len;
82 (char *)buffer += len;
83 }
84
85 return size * nmemb;
86}
87
88size_t read_data(char *buffer, size_t size, size_t nitems, void *instream)
89{
90 FIL *file = (FIL *)instream;
91 UINT ret = 0;
92 FRESULT res;
93
94 res = f_read(file, buffer, size * nitems, &ret);
95 if (res != FR_OK)
96 return 0;
97
98 int rest = ret;
99 int len;
100
101 while (rest > 0) {
102 len = rest;
103 if (len > (sizeof(response) - 1)) {
104 len = sizeof(response) - 1;
105 }
106
107 memcpy(response, buffer, len);
108
109 response[len] = '\0';
110
111 syslog(LOG_NOTICE, response);
112
113 dly_tsk(100);
114
115 rest -= len;
116 (char *)buffer += len;
117 }
118
119 return ret;
120}
121
122char errbuf[CURL_ERROR_SIZE];
123
124static void get_logfname(char *fname)
125{
126 // fname = "0:/log/2016010100000000.log"
127 time_t t;
128 struct tm tm;
129 int tmp1, tmp2;
130 char *pos = &fname[7];
131
132 sys_time(&t);
133 gmtime_r(&t, &tm);
134
135 /* ”N */
136 tmp1 = 1900 + tm.tm_year;
137 tmp2 = tmp1 / 1000;
138 tmp1 -= tmp2 * 1000;
139 *pos++ = '0' + tmp2;
140 tmp2 = tmp1 / 100;
141 tmp1 -= tmp2 * 100;
142 *pos++ = '0' + tmp2;
143 tmp2 = tmp1 / 10;
144 tmp1 -= tmp2 * 10;
145 *pos++ = '0' + tmp2;
146 *pos++ = '0' + tmp1;
147 /* ŒŽ */
148 tmp1 = tm.tm_mon + 1;
149 tmp2 = tmp1 / 10;
150 tmp1 -= tmp2 * 10;
151 *pos++ = '0' + tmp2;
152 *pos++ = '0' + tmp1;
153 /* “ú */
154 tmp1 = tm.tm_mday;
155 tmp2 = tmp1 / 10;
156 tmp1 -= tmp2 * 10;
157 *pos++ = '0' + tmp2;
158 *pos++ = '0' + tmp1;
159 /* Žž */
160 tmp1 = tm.tm_hour;
161 tmp2 = tmp1 / 10;
162 tmp1 -= tmp2 * 10;
163 *pos++ = '0' + tmp2;
164 *pos++ = '0' + tmp1;
165 /* •ª */
166 tmp1 = tm.tm_min;
167 tmp2 = tmp1 / 10;
168 tmp1 -= tmp2 * 10;
169 *pos++ = '0' + tmp2;
170 *pos++ = '0' + tmp1;
171 /* •b */
172 tmp1 = tm.tm_sec;
173 tmp2 = tmp1 / 10;
174 tmp1 -= tmp2 * 10;
175 *pos++ = '0' + tmp2;
176 *pos++ = '0' + tmp1;
177}
178
179static FRESULT write_log(char *fname)
180{
181 FIL file;
182 FRESULT ret;
183
184 ret = f_open(&file, fname, FA_CREATE_ALWAYS | FA_WRITE);
185 if (ret != FR_OK) {
186 syslog(LOG_ERROR, "not open a upload file %d", ret);
187 return ret;
188 }
189
190 f_printf(&file, "{\"datetime\":\"");
191 for (int i = 7; i < 21; i++)
192 f_putc(fname[i], &file);
193 f_printf(&file, "\",");
194
195 for (int i = 0; i < 6; i++) {
196 struct watt_hour_meter_t *meter = &electric_energy_meter_data[i];
197 uint32_t *log;
198 int len;
199
200 f_printf(&file, "\"channel%d\":[", i + 1);
201
202 wai_sem(MAIN_SEMAPHORE);
203
204 len = 48 - meter->current_pos;
205 if (len > 0) {
206 log = &meter->integral_electric_energy_measurement_log[meter->current_pos];
207 for (int j = 1; j < len; j++) {
208 f_printf(&file, "%d,", *log);
209 }
210 f_printf(&file, "%d", *log);
211 }
212 len = 48 - len;
213 if (len > 0) {
214 f_putc(',', &file);
215
216 log = &meter->integral_electric_energy_measurement_log[0];
217 for (int j = 1; j < len; j++) {
218 f_printf(&file, "%d,", *log);
219 }
220 f_printf(&file, "%d", *log);
221 }
222
223 sig_sem(MAIN_SEMAPHORE);
224
225 f_putc(']', &file);
226 if (i != 5) {
227 f_putc(',', &file);
228 }
229 }
230
231 f_putc('}', &file);
232
233file_close:
234 f_close(&file);
235
236 return FR_OK;
237}
238
239void client_task(intptr_t exinf)
240{
241 CURL *curl;
242 CURLcode res;
243 int error = 0;
244 //const char *data = "{\"value\":\"data post\"}";
245 struct curl_slist *list = NULL;
246 FIL file;
247 FRESULT ret;
248 char fname[] = {"0:/log/20160101000000.log"};
249
250 get_logfname(fname);
251
252 ret = write_log(fname);
253 if (ret != FR_OK) {
254 syslog(LOG_ERROR, "log file write error %d", ret);
255 return;
256 }
257
258 ret = f_open(&file, fname, FA_READ);
259 if (ret != FR_OK) {
260 syslog(LOG_ERROR, "log file open error %d", ret);
261 return;
262 }
263
264 syslog(LOG_NOTICE, "cURL start");
265
266 curl = curl_easy_init();
267 if (curl == NULL) {
268 syslog(LOG_ERROR, "curl_easy_init() failed\n");
269 goto file_close;
270 }
271
272 /* ask libcurl to show us the verbose output */
273 curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
274
275 res = curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
276 if (res != CURLE_OK)
277 syslog(LOG_ERROR, "CURLOPT_URL failed: %s\n",
278 curl_easy_strerror(res));
279
280 /* set the error buffer as empty before performing a request */
281 errbuf[0] = 0;
282
283 /* provide a buffer to store errors in */
284 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
285
286#ifdef SKIP_PEER_VERIFICATION
287 /*
288 * If you want to connect to a site who isn't using a certificate that is
289 * signed by one of the certs in the CA bundle you have, you can skip the
290 * verification of the server's certificate. This makes the connection
291 * A LOT LESS SECURE.
292 *
293 * If you have a CA cert for the server stored someplace else than in the
294 * default bundle, then the CURLOPT_CAPATH option might come handy for
295 * you.
296 */
297 res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
298 if (res != CURLE_OK)
299 syslog(LOG_ERROR, "CURLOPT_SSL_VERIFYPEER failed: %s\n",
300 curl_easy_strerror(res));
301#else
302 res = curl_easy_setopt(curl, CURLOPT_CAINFO, "0:/certs/ca-cert.pem");
303 if (res != CURLE_OK)
304 syslog(LOG_ERROR, "CURLOPT_CAINFO failed: %s\n",
305 curl_easy_strerror(res));
306
307 res = curl_easy_setopt(curl, CURLOPT_SSLCERT, "0:/certs/client-cert.pem");
308 if (res != CURLE_OK)
309 syslog(LOG_ERROR, "CURLOPT_SSLCERT failed: %s\n",
310 curl_easy_strerror(res));
311
312 res = curl_easy_setopt(curl, CURLOPT_SSLKEY, "0:/certs/client-key.pem");
313 if (res != CURLE_OK)
314 syslog(LOG_ERROR, "CURLOPT_SSLKEY failed: %s\n",
315 curl_easy_strerror(res));
316#endif
317
318#ifdef SKIP_HOSTNAME_VERIFICATION
319 /*
320 * If the site you're connecting to uses a different host name that what
321 * they have mentioned in their server certificate's commonName (or
322 * subjectAltName) fields, libcurl will refuse to connect. You can skip
323 * this check, but this will make the connection less secure.
324 */
325 res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
326 if (res != CURLE_OK)
327 syslog(LOG_ERROR, "CURLOPT_SSL_VERIFYHOST failed: %s\n",
328 curl_easy_strerror(res));
329#endif
330
331 /*res = curl_easy_setopt(curl, CURLOPT_PROXY, "https://proxy.example.com:8080");
332 if (res != CURLE_OK)
333 syslog(LOG_ERROR, "CURLOPT_PROXY failed: %s\n",
334 curl_easy_strerror(res));*/
335
336 curl_easy_setopt(curl, CURLOPT_POST, 1);
337
338 /* size of the POST data */
339 //curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(data));
340 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, file.fsize);
341
342 /* pass in a pointer to the data - libcurl will not copy */
343 //curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
344
345 list = curl_slist_append(list, "Content-Type: application/json");
346
347 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
348
349 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&error);
350
351 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
352
353 /* now specify which file to upload */
354 curl_easy_setopt(curl, CURLOPT_READDATA, &file);
355
356 /* we want to use our own read function */
357 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_data);
358
359 syslog(LOG_NOTICE, "cURL perform the request");
360 tslp_tsk(100);
361
362 /* Perform the request, res will get the return code */
363 res = curl_easy_perform(curl);
364 /* Check for errors */
365 if (res != CURLE_OK) {
366 syslog(LOG_ERROR, "curl_easy_perform() failed: %s\n",
367 curl_easy_strerror(res));
368 syslog(LOG_ERROR, errbuf);
369 }
370
371 /* always cleanup */
372 curl_easy_cleanup(curl);
373
374 syslog(LOG_NOTICE, "cURL end");
375
376file_close:
377 f_close(&file);
378}
379
380void client_fin(void)
381{
382 curl_global_cleanup();
383}
Note: See TracBrowser for help on using the repository browser.