1 | /*
|
---|
2 | * TOPPERS ECHONET Lite Communication Middleware
|
---|
3 | *
|
---|
4 | * Copyright (C) 2016 Cores Co., Ltd. Japan
|
---|
5 | *
|
---|
6 | * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
|
---|
7 | * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
|
---|
8 | * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
|
---|
9 | * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
|
---|
10 | * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
|
---|
11 | * スコード中に含まれていること.
|
---|
12 | * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
|
---|
13 | * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
|
---|
14 | * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
|
---|
15 | * の無保証規定を掲載すること.
|
---|
16 | * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
|
---|
17 | * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
|
---|
18 | * と.
|
---|
19 | * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
|
---|
20 | * 作権表示,この利用条件および下記の無保証規定を掲載すること.
|
---|
21 | * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
|
---|
22 | * 報告すること.
|
---|
23 | * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
|
---|
24 | * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
|
---|
25 | * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
|
---|
26 | * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
|
---|
27 | * 免責すること.
|
---|
28 | *
|
---|
29 | * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
|
---|
30 | * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
|
---|
31 | * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
|
---|
32 | * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
|
---|
33 | * の責任を負わない.
|
---|
34 | *
|
---|
35 | * @(#) $Id$
|
---|
36 | */
|
---|
37 | #include <lib/curl_config.h>
|
---|
38 | #include <lib/curl_setup.h>
|
---|
39 | #include <curl/curl.h>
|
---|
40 | #include <wolfssl/wolfcrypt/wc_port.h>
|
---|
41 | //#include <wolfssl/ssl.h>
|
---|
42 | //#include <sys/socket.h>
|
---|
43 | #include <string.h>
|
---|
44 | #include <kernel.h>
|
---|
45 | #include <t_syslog.h>
|
---|
46 | #include "echonet_main.h"
|
---|
47 | #include "kernel_cfg.h"
|
---|
48 | #include "ff.h"
|
---|
49 | #include "util/ntstdio.h"
|
---|
50 |
|
---|
51 | #define SKIP_PEER_VERIFICATION
|
---|
52 | //#define SKIP_HOSTNAME_VERIFICATION
|
---|
53 | char response[80];
|
---|
54 | extern ntstdio_t ntstdio;
|
---|
55 |
|
---|
56 | void client_init(void)
|
---|
57 | {
|
---|
58 | //uITRON4_minit(ITRON_POOL_SIZE);
|
---|
59 |
|
---|
60 | curl_global_init(CURL_GLOBAL_DEFAULT);
|
---|
61 | }
|
---|
62 |
|
---|
63 | size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
|
---|
64 | {
|
---|
65 | int rest = size * nmemb;
|
---|
66 | int len;
|
---|
67 |
|
---|
68 | while (rest > 0) {
|
---|
69 | len = rest;
|
---|
70 | if (len > (sizeof(response) - 1)) {
|
---|
71 | len = sizeof(response) - 1;
|
---|
72 | }
|
---|
73 |
|
---|
74 | memcpy(response, buffer, len);
|
---|
75 |
|
---|
76 | response[len] = '\0';
|
---|
77 |
|
---|
78 | ntstdio_printf(&ntstdio, response);
|
---|
79 |
|
---|
80 | dly_tsk(100 * 1000);
|
---|
81 |
|
---|
82 | rest -= len;
|
---|
83 | buffer = (char *)buffer + len;
|
---|
84 | }
|
---|
85 |
|
---|
86 | return size * nmemb;
|
---|
87 | }
|
---|
88 |
|
---|
89 | size_t read_data(char *buffer, size_t size, size_t nitems, void *instream)
|
---|
90 | {
|
---|
91 | FIL *file = (FIL *)instream;
|
---|
92 | UINT ret = 0;
|
---|
93 | FRESULT res;
|
---|
94 |
|
---|
95 | res = f_read(file, buffer, size * nitems, &ret);
|
---|
96 | if (res != FR_OK)
|
---|
97 | return 0;
|
---|
98 |
|
---|
99 | int rest = ret;
|
---|
100 | int len;
|
---|
101 |
|
---|
102 | while (rest > 0) {
|
---|
103 | len = rest;
|
---|
104 | if (len > (sizeof(response) - 1)) {
|
---|
105 | len = sizeof(response) - 1;
|
---|
106 | }
|
---|
107 |
|
---|
108 | memcpy(response, buffer, len);
|
---|
109 |
|
---|
110 | response[len] = '\0';
|
---|
111 |
|
---|
112 | ntstdio_printf(&ntstdio, response);
|
---|
113 |
|
---|
114 | dly_tsk(100);
|
---|
115 |
|
---|
116 | rest -= len;
|
---|
117 | buffer = (char *)buffer + len;
|
---|
118 | }
|
---|
119 |
|
---|
120 | return ret;
|
---|
121 | }
|
---|
122 |
|
---|
123 | char errbuf[CURL_ERROR_SIZE];
|
---|
124 |
|
---|
125 | static void get_logfname(char *fname)
|
---|
126 | {
|
---|
127 | // fname = "0:/log/2016010100000000.log"
|
---|
128 | time_t t;
|
---|
129 | struct tm tm;
|
---|
130 | int tmp1, tmp2;
|
---|
131 | char *pos = &fname[7];
|
---|
132 |
|
---|
133 | time(&t);
|
---|
134 | gmtime_r(&t, &tm);
|
---|
135 |
|
---|
136 | /* 年 */
|
---|
137 | tmp1 = 1900 + tm.tm_year;
|
---|
138 | tmp2 = tmp1 / 1000;
|
---|
139 | tmp1 -= tmp2 * 1000;
|
---|
140 | *pos++ = '0' + tmp2;
|
---|
141 | tmp2 = tmp1 / 100;
|
---|
142 | tmp1 -= tmp2 * 100;
|
---|
143 | *pos++ = '0' + tmp2;
|
---|
144 | tmp2 = tmp1 / 10;
|
---|
145 | tmp1 -= tmp2 * 10;
|
---|
146 | *pos++ = '0' + tmp2;
|
---|
147 | *pos++ = '0' + tmp1;
|
---|
148 | /* 月 */
|
---|
149 | tmp1 = tm.tm_mon + 1;
|
---|
150 | tmp2 = tmp1 / 10;
|
---|
151 | tmp1 -= tmp2 * 10;
|
---|
152 | *pos++ = '0' + tmp2;
|
---|
153 | *pos++ = '0' + tmp1;
|
---|
154 | /* 日 */
|
---|
155 | tmp1 = tm.tm_mday;
|
---|
156 | tmp2 = tmp1 / 10;
|
---|
157 | tmp1 -= tmp2 * 10;
|
---|
158 | *pos++ = '0' + tmp2;
|
---|
159 | *pos++ = '0' + tmp1;
|
---|
160 | /* 時 */
|
---|
161 | tmp1 = tm.tm_hour;
|
---|
162 | tmp2 = tmp1 / 10;
|
---|
163 | tmp1 -= tmp2 * 10;
|
---|
164 | *pos++ = '0' + tmp2;
|
---|
165 | *pos++ = '0' + tmp1;
|
---|
166 | /* 分 */
|
---|
167 | tmp1 = tm.tm_min;
|
---|
168 | tmp2 = tmp1 / 10;
|
---|
169 | tmp1 -= tmp2 * 10;
|
---|
170 | *pos++ = '0' + tmp2;
|
---|
171 | *pos++ = '0' + tmp1;
|
---|
172 | /* 秒 */
|
---|
173 | tmp1 = tm.tm_sec;
|
---|
174 | tmp2 = tmp1 / 10;
|
---|
175 | tmp1 -= tmp2 * 10;
|
---|
176 | *pos++ = '0' + tmp2;
|
---|
177 | *pos++ = '0' + tmp1;
|
---|
178 | }
|
---|
179 |
|
---|
180 | static FRESULT write_log(char *fname)
|
---|
181 | {
|
---|
182 | FIL file;
|
---|
183 | FRESULT ret;
|
---|
184 |
|
---|
185 | ret = f_open(&file, fname, FA_CREATE_ALWAYS | FA_WRITE);
|
---|
186 | if (ret != FR_OK) {
|
---|
187 | ntstdio_printf(&ntstdio, "not open a upload file %d\n", ret);
|
---|
188 | return ret;
|
---|
189 | }
|
---|
190 |
|
---|
191 | f_printf(&file, "{\"datetime\":\"");
|
---|
192 | for (int i = 7; i < 21; i++)
|
---|
193 | f_putc(fname[i], &file);
|
---|
194 | f_printf(&file, "\",");
|
---|
195 |
|
---|
196 | for (int i = 0; i < 6; i++) {
|
---|
197 | struct watt_hour_meter_t *meter = &electric_energy_meter_data[i];
|
---|
198 | uint32_t *log;
|
---|
199 | int len;
|
---|
200 |
|
---|
201 | f_printf(&file, "\"channel%d\":[", i + 1);
|
---|
202 |
|
---|
203 | wai_sem(MAIN_SEMAPHORE);
|
---|
204 |
|
---|
205 | len = 48 - meter->current_pos;
|
---|
206 | if (len > 0) {
|
---|
207 | log = &meter->integral_electric_energy_measurement_log[meter->current_pos];
|
---|
208 | for (int j = 1; j < len; j++) {
|
---|
209 | f_printf(&file, "%d,", *log);
|
---|
210 | }
|
---|
211 | f_printf(&file, "%d", *log);
|
---|
212 | }
|
---|
213 | len = 48 - len;
|
---|
214 | if (len > 0) {
|
---|
215 | f_putc(',', &file);
|
---|
216 |
|
---|
217 | log = &meter->integral_electric_energy_measurement_log[0];
|
---|
218 | for (int j = 1; j < len; j++) {
|
---|
219 | f_printf(&file, "%d,", *log);
|
---|
220 | }
|
---|
221 | f_printf(&file, "%d", *log);
|
---|
222 | }
|
---|
223 |
|
---|
224 | sig_sem(MAIN_SEMAPHORE);
|
---|
225 |
|
---|
226 | f_putc(']', &file);
|
---|
227 | if (i != 5) {
|
---|
228 | f_putc(',', &file);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | f_putc('}', &file);
|
---|
233 |
|
---|
234 | file_close:
|
---|
235 | f_close(&file);
|
---|
236 |
|
---|
237 | return FR_OK;
|
---|
238 | }
|
---|
239 |
|
---|
240 | void client_task(intptr_t exinf)
|
---|
241 | {
|
---|
242 | CURL *curl;
|
---|
243 | CURLcode res;
|
---|
244 | int error = 0;
|
---|
245 | //const char *data = "{\"value\":\"data post\"}";
|
---|
246 | struct curl_slist *list = NULL;
|
---|
247 | FIL file;
|
---|
248 | FRESULT ret;
|
---|
249 | char fname[] = {"1:/log/20160101000000.log"};
|
---|
250 |
|
---|
251 | get_logfname(fname);
|
---|
252 |
|
---|
253 | ret = write_log(fname);
|
---|
254 | if (ret != FR_OK) {
|
---|
255 | ntstdio_printf(&ntstdio, "log file write error %d\n", ret);
|
---|
256 | return;
|
---|
257 | }
|
---|
258 |
|
---|
259 | ret = f_open(&file, fname, FA_READ);
|
---|
260 | if (ret != FR_OK) {
|
---|
261 | ntstdio_printf(&ntstdio, "log file open error %d\n", ret);
|
---|
262 | return;
|
---|
263 | }
|
---|
264 |
|
---|
265 | ntstdio_printf(&ntstdio, "cURL start\n");
|
---|
266 |
|
---|
267 | curl = curl_easy_init();
|
---|
268 | if (curl == NULL) {
|
---|
269 | ntstdio_printf(&ntstdio, "curl_easy_init() failed\n");
|
---|
270 | goto file_close;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /* ask libcurl to show us the verbose output */
|
---|
274 | curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
|
---|
275 |
|
---|
276 | res = curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/");
|
---|
277 | if (res != CURLE_OK)
|
---|
278 | ntstdio_printf(&ntstdio, "CURLOPT_URL failed: %s\n",
|
---|
279 | curl_easy_strerror(res));
|
---|
280 |
|
---|
281 | /* set the error buffer as empty before performing a request */
|
---|
282 | errbuf[0] = 0;
|
---|
283 |
|
---|
284 | /* provide a buffer to store errors in */
|
---|
285 | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
|
---|
286 |
|
---|
287 | #ifdef SKIP_PEER_VERIFICATION
|
---|
288 | /*
|
---|
289 | * If you want to connect to a site who isn't using a certificate that is
|
---|
290 | * signed by one of the certs in the CA bundle you have, you can skip the
|
---|
291 | * verification of the server's certificate. This makes the connection
|
---|
292 | * A LOT LESS SECURE.
|
---|
293 | *
|
---|
294 | * If you have a CA cert for the server stored someplace else than in the
|
---|
295 | * default bundle, then the CURLOPT_CAPATH option might come handy for
|
---|
296 | * you.
|
---|
297 | */
|
---|
298 | res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
---|
299 | if (res != CURLE_OK)
|
---|
300 | ntstdio_printf(&ntstdio, "CURLOPT_SSL_VERIFYPEER failed: %s\n",
|
---|
301 | curl_easy_strerror(res));
|
---|
302 | #else
|
---|
303 | res = curl_easy_setopt(curl, CURLOPT_CAINFO, "0:/certs/ca-cert.pem");
|
---|
304 | if (res != CURLE_OK)
|
---|
305 | ntstdio_printf(&ntstdio, "CURLOPT_CAINFO failed: %s\n",
|
---|
306 | curl_easy_strerror(res));
|
---|
307 |
|
---|
308 | res = curl_easy_setopt(curl, CURLOPT_SSLCERT, "0:/certs/client-cert.pem");
|
---|
309 | if (res != CURLE_OK)
|
---|
310 | ntstdio_printf(&ntstdio, "CURLOPT_SSLCERT failed: %s\n",
|
---|
311 | curl_easy_strerror(res));
|
---|
312 |
|
---|
313 | res = curl_easy_setopt(curl, CURLOPT_SSLKEY, "0:/certs/client-key.pem");
|
---|
314 | if (res != CURLE_OK)
|
---|
315 | ntstdio_printf(&ntstdio, "CURLOPT_SSLKEY failed: %s\n",
|
---|
316 | curl_easy_strerror(res));
|
---|
317 | #endif
|
---|
318 |
|
---|
319 | #ifdef SKIP_HOSTNAME_VERIFICATION
|
---|
320 | /*
|
---|
321 | * If the site you're connecting to uses a different host name that what
|
---|
322 | * they have mentioned in their server certificate's commonName (or
|
---|
323 | * subjectAltName) fields, libcurl will refuse to connect. You can skip
|
---|
324 | * this check, but this will make the connection less secure.
|
---|
325 | */
|
---|
326 | res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
---|
327 | if (res != CURLE_OK)
|
---|
328 | ntstdio_printf(&ntstdio, "CURLOPT_SSL_VERIFYHOST failed: %s\n",
|
---|
329 | curl_easy_strerror(res));
|
---|
330 | #endif
|
---|
331 |
|
---|
332 | /*res = curl_easy_setopt(curl, CURLOPT_PROXY, "https://proxy.example.com:8080");
|
---|
333 | if (res != CURLE_OK)
|
---|
334 | ntstdio_printf(&ntstdio, "CURLOPT_PROXY failed: %s\n",
|
---|
335 | curl_easy_strerror(res));*/
|
---|
336 |
|
---|
337 | curl_easy_setopt(curl, CURLOPT_POST, 1);
|
---|
338 |
|
---|
339 | /* size of the POST data */
|
---|
340 | //curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(data));
|
---|
341 | curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, file.fsize);
|
---|
342 |
|
---|
343 | /* pass in a pointer to the data - libcurl will not copy */
|
---|
344 | //curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
|
---|
345 |
|
---|
346 | list = curl_slist_append(list, "Content-Type: application/json");
|
---|
347 |
|
---|
348 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
|
---|
349 |
|
---|
350 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&error);
|
---|
351 |
|
---|
352 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
|
---|
353 |
|
---|
354 | /* now specify which file to upload */
|
---|
355 | curl_easy_setopt(curl, CURLOPT_READDATA, &file);
|
---|
356 |
|
---|
357 | /* we want to use our own read function */
|
---|
358 | curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_data);
|
---|
359 |
|
---|
360 | ntstdio_printf(&ntstdio, "cURL perform the request\n");
|
---|
361 | tslp_tsk(100 * 1000);
|
---|
362 |
|
---|
363 | /* Perform the request, res will get the return code */
|
---|
364 | res = curl_easy_perform(curl);
|
---|
365 | /* Check for errors */
|
---|
366 | if (res != CURLE_OK) {
|
---|
367 | ntstdio_printf(&ntstdio, "curl_easy_perform() failed: %s\n",
|
---|
368 | curl_easy_strerror(res));
|
---|
369 | ntstdio_printf(&ntstdio, errbuf);
|
---|
370 | }
|
---|
371 |
|
---|
372 | /* always cleanup */
|
---|
373 | curl_easy_cleanup(curl);
|
---|
374 |
|
---|
375 | ntstdio_printf(&ntstdio, "cURL end\n");
|
---|
376 |
|
---|
377 | file_close:
|
---|
378 | f_close(&file);
|
---|
379 | }
|
---|
380 |
|
---|
381 | void client_fin(void)
|
---|
382 | {
|
---|
383 | curl_global_cleanup();
|
---|
384 | }
|
---|