Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Gebhardt, Albrecht
frewe-rpi
Commits
f198e1db
Commit
f198e1db
authored
Nov 11, 2018
by
Gebhardt, Albrecht
Browse files
use libcurl for https upload
parent
c02512ac
Changes
2
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
f198e1db
all
:
frewe-client
frewe-client
:
frewe-client.c http_fetcher.c http_error_codes.c
gcc frewe-client.c http_fetcher.c http_error_codes.c
-o
frewe-client
-lusb
-lm
-lcrypto
gcc frewe-client.c http_fetcher.c http_error_codes.c
-o
frewe-client
-lusb
-lm
-lcrypto
-lcurl
install
:
frewe-client
cp
rc.frewe-client /etc/init.d/frewe-client
...
...
frewe-client.c
View file @
f198e1db
...
...
@@ -63,6 +63,7 @@
* 2015-04-24 Handle lasttime as UTC
* 2015-08-20 Don't write fhem.txt for older records
* 2016-01-16 Separate read interval for fhem.txt, run each 48 seconds for FHEM
* 2018-11-10 switch to libcurl
* TODO: Handle rain counter overflow
*/
...
...
@@ -81,6 +82,7 @@
#include <elf.h>
#include <openssl/md5.h>
#include "http_fetcher.h"
#include <curl/curl.h>
#define PROGRAM_VERSION "1.19"
...
...
@@ -250,6 +252,8 @@ int main(int argc, char **argv)
for
(
i
=
0
;
i
<
MAX_ADD_URLS
;
i
++
)
add_url
[
i
]
=
NULL
;
// Parse options
while
(
rv
==
0
&&
(
c
=
getopt
(
argc
,
argv
,
"hH?vxf:d:a:A:p:e:t:s:c:u:r:t:k:"
))
!=-
1
)
...
...
@@ -1547,6 +1551,7 @@ int ws_parse(uint8_t *buffer, uint8_t *buffer60, uint8_t *buffer0h, time_t curti
if
(
w
.
rainhour
<
0
)
{
logger
(
LOG_ERROR
,
"ws_parse"
,
"Rainhour is negative, will set it to 0, rain=%f, lastrain=%f"
,
w
.
rain
,
lastrain
);
w
.
rainhour
=
0
;
w
.
ok
=
0
;
}
if
(
w
.
rainhour
>
150
)
{
logger
(
LOG_ERROR
,
"ws_parse"
,
"Rainhour is out of range, rain=%f, lastrain=%f"
,
w
.
rain
,
lastrain
);
...
...
@@ -1637,6 +1642,31 @@ static size_t write_callback(char *buffer, size_t size,size_t nitems,void *outpu
return size*nitems;
}
*/
size_t
write_data
(
void
*
ptr
,
size_t
size
,
size_t
nmemb
,
char
**
filebuf
){
logger
(
LOG_DEBUG
,
"write_data"
,
" %i times %i bytes"
,
nmemb
,
size
);
char
*
pagebuf
=
(
char
*
)
malloc
(
nmemb
*
size
);
if
(
pagebuf
==
NULL
){
logger
(
LOG_ERROR
,
"write_data"
,
"malloc failed"
);
return
1
;
}
logger
(
LOG_DEBUG
,
"write_data"
,
"malloc success: %i"
,
nmemb
*
size
);
char
*
cptr
=
(
char
*
)
ptr
;
size_t
written
=
0
;
int
pos
=
0
;
while
(((
char
*
)
ptr
)[
pos
]
!=
'\0'
&&
pos
<
nmemb
*
size
){
logger
(
LOG_DEBUG
,
"write_data"
,
"transfer byte %i"
,
pos
);
//logger(LOG_DEBUG,"write_data","read byte: %s", cptr[pos]);
//pagebuf[pos]=cptr[pos];
written
++
;
logger
(
LOG_DEBUG
,
"write_data"
,
"read byte: %s"
,
pagebuf
[
pos
]);
pos
++
;
}
//size_t written = fwrite(ptr, size, nmemb, pagebuf);
logger
(
LOG_DEBUG
,
"write_data"
,
"content: %s"
,
pagebuf
);
*
filebuf
=
pagebuf
;
return
written
;
}
int
ws_submit
(
char
*
server_url
,
char
**
filebuf
)
{
...
...
@@ -1644,16 +1674,38 @@ int ws_submit(char *server_url, char** filebuf)
{
free
(
*
filebuf
);
*
filebuf
=
NULL
;
}
// init CURL
CURL
*
curl
;
CURLcode
res
;
curl_global_init
(
CURL_GLOBAL_DEFAULT
);
http_setTimeout
(
15
);
int
l
=
http_fetch
(
server_url
,
filebuf
);
if
(
l
>=
0
)
{
logger
(
LOG_DEBUG
,
"ws_submit"
,
"http_fetcher performed OK content: %s"
,
*
filebuf
);
/* int l=http_fetch(server_url, filebuf);*/
curl
=
curl_easy_init
();
if
(
!
curl
){
logger
(
LOG_ERROR
,
"ws_submit"
,
"curl init failed"
);
return
1
;
}
curl_easy_setopt
(
curl
,
CURLOPT_URL
,
server_url
);
curl_easy_setopt
(
curl
,
CURLOPT_SSL_VERIFYPEER
,
0L
);
curl_easy_setopt
(
curl
,
CURLOPT_SSL_VERIFYHOST
,
0L
);
curl_easy_setopt
(
curl
,
CURLOPT_WRITEFUNCTION
,
write_data
);
curl_easy_setopt
(
curl
,
CURLOPT_WRITEDATA
,
*
filebuf
);
int
l
=
curl_easy_perform
(
curl
);
curl_easy_cleanup
(
curl
);
curl_global_cleanup
();
/*if (l>=0)*/
if
(
l
==
CURLE_OK
)
/* { logger(LOG_DEBUG,"ws_submit","http_fetcher performed OK content: %s", *filebuf);*/
{
logger
(
LOG_DEBUG
,
"ws_submit"
,
"curl performed OK content: %s"
,
*
filebuf
);
return
0
;
}
else
{
logger
(
LOG_WARNING
,
"ws_submit"
,
"http_fetcher failed with message
\"
%s
\"
"
,
http_strerror
());
/*{ logger(LOG_WARNING,"ws_submit","http_fetcher failed with message \"%s\"", http_strerror());*/
{
logger
(
LOG_WARNING
,
"ws_submit"
,
"curl failed with message
\"
%s
\"
"
,
curl_easy_strerror
(
l
));
return
1
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment