Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
A
ademco_hb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
ademco_hb
Commits
6d70a831
Commit
6d70a831
authored
Nov 18, 2023
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
prettier, nothing new
parent
8770892b
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
24 changed files
with
3024 additions
and
2906 deletions
+3024
-2906
ademco.c
ademco.c
+97
-77
ademco.h
ademco.h
+334
-293
copy_from_hb.sh
examples/httprelay/copy_from_hb.sh
+11
-0
httprelay.vcxproj
examples/httprelay/httprelay.vcxproj
+181
-180
httprelay.vcxproj.filters
examples/httprelay/httprelay.vcxproj.filters
+110
-107
list.h
examples/httprelay/uvlib/list.h
+17
-18
mybuf.c
examples/httprelay/uvlib/mybuf.c
+105
-94
mybuf.h
examples/httprelay/uvlib/mybuf.h
+42
-40
uv_http.h
examples/httprelay/uvlib/uv_http.h
+6
-11
uv_httpc.c
examples/httprelay/uvlib/uv_httpc.c
+522
-522
uv_httpc.h
examples/httprelay/uvlib/uv_httpc.h
+62
-62
uv_httpd.c
examples/httprelay/uvlib/uv_httpd.c
+479
-497
uv_httpd.h
examples/httprelay/uvlib/uv_httpd.h
+75
-75
uv_log.c
examples/httprelay/uvlib/uv_log.c
+84
-84
uv_log.h
examples/httprelay/uvlib/uv_log.h
+68
-68
uv_tcp.c
examples/httprelay/uvlib/uv_tcp.c
+25
-0
uv_tcp.h
examples/httprelay/uvlib/uv_tcp.h
+24
-27
uv_tcpclient.c
examples/httprelay/uvlib/uv_tcpclient.c
+236
-235
uv_tcpclient.h
examples/httprelay/uvlib/uv_tcpclient.h
+51
-55
uv_tcpserver.c
examples/httprelay/uvlib/uv_tcpserver.c
+182
-185
uv_tcpserver.h
examples/httprelay/uvlib/uv_tcpserver.h
+55
-60
uvlib.h
examples/httprelay/uvlib/uvlib.h
+40
-0
hb_com.c
hb_com.c
+33
-39
hb_com.h
hb_com.h
+185
-177
No files found.
ademco.c
View file @
6d70a831
...
...
@@ -18,8 +18,9 @@
#endif
#define dline dprintf("%s %d\n", __FILE__, __LINE__);
#define dmsg dline; dprintf
#define dmsg \
dline; \
dprintf
static
uint8_t
hex2char
(
uint8_t
h
)
{
h
&=
0x0F
;
...
...
@@ -40,7 +41,6 @@ static uint8_t char2hex(uint8_t c) {
return
0xFF
;
}
void
ademcoPrint
(
const
ademco_char_t
*
p
,
size_t
len
)
{
while
(
len
--
)
{
if
(
isprint
(
*
(
const
uint8_t
*
)
p
))
...
...
@@ -74,35 +74,35 @@ int ademcoIsValidAccount(const char* acct) {
}
int
ademcoIsMachineStatusEvent
(
AdemcoEvent
ademcoEvent
)
{
return
ademcoEvent
==
EVENT_ARM
||
ademcoEvent
==
EVENT_HALFARM
||
ademcoEvent
==
EVENT_HALFARM_1456
||
ademcoEvent
==
EVENT_DISARM
;
return
ademcoEvent
==
EVENT_ARM
||
ademcoEvent
==
EVENT_HALFARM
||
ademcoEvent
==
EVENT_HALFARM_1456
||
ademcoEvent
==
EVENT_DISARM
;
}
int
ademcoIsMachineTypeEvent
(
AdemcoEvent
ademcoEvent
)
{
return
ademcoEvent
==
EVENT_I_AM_NET_MODULE
||
ademcoEvent
==
EVENT_I_AM_GPRS
||
ademcoEvent
==
EVENT_I_AM_LCD_MACHINE
||
ademcoEvent
==
EVENT_I_AM_WIRE_MACHINE
||
ademcoEvent
==
EVENT_I_AM_WIFI_MACHINE
||
ademcoEvent
==
EVENT_I_AM_3_SECTION_MACHINE
||
ademcoEvent
==
EVENT_I_AM_IOT_MACHINE
||
ademcoEvent
==
EVENT_I_AM_TRUE_COLOR
||
ademcoEvent
==
EVENT_I_AM_GPRS_IOT
||
ademcoEvent
==
EVENT_I_AM_GPRS_PHONE
||
ademcoEvent
==
EVENT_I_AM_NB_MACHINE
||
ademcoEvent
==
EVENT_I_AM_WIFI2_MACHINE
;
return
ademcoEvent
==
EVENT_I_AM_NET_MODULE
||
ademcoEvent
==
EVENT_I_AM_GPRS
||
ademcoEvent
==
EVENT_I_AM_LCD_MACHINE
||
ademcoEvent
==
EVENT_I_AM_WIRE_MACHINE
||
ademcoEvent
==
EVENT_I_AM_WIFI_MACHINE
||
ademcoEvent
==
EVENT_I_AM_3_SECTION_MACHINE
||
ademcoEvent
==
EVENT_I_AM_IOT_MACHINE
||
ademcoEvent
==
EVENT_I_AM_TRUE_COLOR
||
ademcoEvent
==
EVENT_I_AM_GPRS_IOT
||
ademcoEvent
==
EVENT_I_AM_GPRS_PHONE
||
ademcoEvent
==
EVENT_I_AM_NB_MACHINE
||
ademcoEvent
==
EVENT_I_AM_WIFI2_MACHINE
;
}
int
ademcoIsEventNeedControlSource
(
AdemcoEvent
ademcoEvent
)
{
return
ademcoEvent
==
EVENT_ARM
||
ademcoEvent
==
EVENT_HALFARM
||
ademcoEvent
==
EVENT_HALFARM_1456
||
ademcoEvent
==
EVENT_DISARM
||
ademcoEvent
==
EVENT_EMERGENCY
||
ademcoEvent
==
EVENT_DURESS
||
ademcoEvent
==
EVENT_PHONE_USER_CANCLE_ALARM
;
return
ademcoEvent
==
EVENT_ARM
||
ademcoEvent
==
EVENT_HALFARM
||
ademcoEvent
==
EVENT_HALFARM_1456
||
ademcoEvent
==
EVENT_DISARM
||
ademcoEvent
==
EVENT_EMERGENCY
||
ademcoEvent
==
EVENT_DURESS
||
ademcoEvent
==
EVENT_PHONE_USER_CANCLE_ALARM
;
}
AdemcoEvent
ademcoGetExceptionEventByResumeEvent
(
AdemcoEvent
resumeEvent
)
{
...
...
@@ -375,7 +375,8 @@ uint16_t ademcoCRC16(const ademco_char_t* buff, size_t len) {
return
crc
;
}
size_t
ademcoAppendDataSegment
(
ademco_char_t
*
buff
,
const
char
*
acct
,
AdemcoId
ademcoId
,
AdemcoEvent
ademcoEvent
,
AdemcoGG
gg
,
AdemcoZone
zone
)
{
size_t
ademcoAppendDataSegment
(
ademco_char_t
*
buff
,
const
char
*
acct
,
AdemcoId
ademcoId
,
AdemcoEvent
ademcoEvent
,
AdemcoGG
gg
,
AdemcoZone
zone
)
{
char
*
p
=
(
char
*
)
buff
;
if
(
ademcoEvent
==
EVENT_INVALID_EVENT
)
{
*
p
++
=
'['
;
...
...
@@ -425,21 +426,28 @@ size_t ademcoAppendDataSegment(ademco_char_t* buff, const char* acct, AdemcoId a
}
}
size_t
ademcoAppendDataSegment2
(
AdemcoDataSegment
*
dataSegment
,
const
char
*
acct
,
AdemcoId
ademcoId
,
AdemcoEvent
ademcoEvent
,
AdemcoGG
gg
,
AdemcoZone
zone
)
{
size_t
ademcoAppendDataSegment2
(
AdemcoDataSegment
*
dataSegment
,
const
char
*
acct
,
AdemcoId
ademcoId
,
AdemcoEvent
ademcoEvent
,
AdemcoGG
gg
,
AdemcoZone
zone
)
{
dataSegment
->
ademcoId
=
ademcoId
;
dataSegment
->
ademcoEvent
=
ademcoEvent
;
dataSegment
->
gg
=
gg
;
dataSegment
->
zone
=
zone
;
return
dataSegment
->
raw_len
=
ademcoAppendDataSegment
(
dataSegment
->
raw
,
acct
,
ademcoId
,
ademcoEvent
,
gg
,
zone
);
return
dataSegment
->
raw_len
=
ademcoAppendDataSegment
(
dataSegment
->
raw
,
acct
,
ademcoId
,
ademcoEvent
,
gg
,
zone
);
}
AdemcoParseResult
ademcoParseDataSegment
(
const
ademco_char_t
*
packet
,
size_t
packet_len
,
AdemcoDataSegment
*
dataSegment
)
{
if
(
packet_len
==
ADEMCO_PACKET_DATA_SEGMENT_EMPTY_LEN
&&
packet
[
0
]
==
'['
&&
packet
[
1
]
==
']'
)
{
AdemcoParseResult
ademcoParseDataSegment
(
const
ademco_char_t
*
packet
,
size_t
packet_len
,
AdemcoDataSegment
*
dataSegment
)
{
if
(
packet_len
==
ADEMCO_PACKET_DATA_SEGMENT_EMPTY_LEN
&&
packet
[
0
]
==
'['
&&
packet
[
1
]
==
']'
)
{
memset
(
dataSegment
,
0
,
sizeof
(
*
dataSegment
));
strcpy
((
char
*
)
dataSegment
->
raw
,
"[]"
);
dataSegment
->
raw_len
=
2
;
return
RESULT_OK
;
}
else
if
(
packet_len
>=
ADEMCO_PACKET_DATA_SEGMENT_FULL_LEN
&&
packet
[
0
]
==
'['
&&
packet
[
packet_len
-
1
]
==
']'
)
{
// [#000000|1400 00 000]
}
else
if
(
packet_len
>=
ADEMCO_PACKET_DATA_SEGMENT_FULL_LEN
&&
packet
[
0
]
==
'['
&&
packet
[
packet_len
-
1
]
==
']'
)
{
// [#000000|1400 00 000]
do
{
const
char
*
p
=
packet
+
2
;
size_t
acct_len
=
packet_len
-
15
;
...
...
@@ -507,7 +515,8 @@ size_t ademcoDataSegmentToCongwinFe100(ademco_char_t* fe100, const AdemcoDataSeg
*
p
++
=
' '
;
// E for open, R for close
if
(
dataSegment
->
ademcoEvent
/
1000
==
1
||
dataSegment
->
ademcoEvent
==
EVENT_HALFARM
)
if
(
dataSegment
->
ademcoEvent
/
1000
==
1
||
dataSegment
->
ademcoEvent
==
EVENT_HALFARM
)
*
p
++
=
'E'
;
else
*
p
++
=
'R'
;
...
...
@@ -605,7 +614,8 @@ size_t ademcoXDataCopy(AdemcoXDataSegment* dst, const AdemcoXDataSegment* src) {
return
0
;
}
int
ademcoMakeXData
(
AdemcoXDataSegment
*
xdata
,
AdemcoXDataLengthFormat
xlf
,
AdemcoXDataTransform
xtr
,
const
ademco_char_t
*
content
,
size_t
len
)
{
int
ademcoMakeXData
(
AdemcoXDataSegment
*
xdata
,
AdemcoXDataLengthFormat
xlf
,
AdemcoXDataTransform
xtr
,
const
ademco_char_t
*
content
,
size_t
len
)
{
char
transformed
[
ADEMCO_PACKET_XDATA_MAX_LEN
];
size_t
translen
;
len
&=
0xFFFF
;
...
...
@@ -677,7 +687,8 @@ const char* admecoPacketIdToString(AdemcoPacketId id) {
static
void
getNowTimestamp
(
char
*
buff
)
{
time_t
now
=
time
(
NULL
);
struct
tm
*
tm
=
localtime
(
&
now
);
snprintf
(
buff
,
ADEMCO_PACKET_TIMESTAMP_LEN
+
1
,
"_%02d:%02d:%02d,%02d-%02d-%04d"
,
snprintf
(
buff
,
ADEMCO_PACKET_TIMESTAMP_LEN
+
1
,
"_%02d:%02d:%02d,%02d-%02d-%04d"
,
tm
->
tm_hour
,
tm
->
tm_min
,
tm
->
tm_sec
,
tm
->
tm_mon
+
1
,
tm
->
tm_mday
,
tm
->
tm_year
+
1900
);
}
...
...
@@ -685,13 +696,15 @@ static void getNowTimestamp(char* buff) {
static
void
getGmtTimestamp
(
char
*
buff
)
{
time_t
now
=
time
(
NULL
);
struct
tm
*
tm
=
gmtime
(
&
now
);
snprintf
(
buff
,
ADEMCO_PACKET_TIMESTAMP_LEN
+
1
,
"_%02d:%02d:%02d,%02d-%02d-%04d"
,
snprintf
(
buff
,
ADEMCO_PACKET_TIMESTAMP_LEN
+
1
,
"_%02d:%02d:%02d,%02d-%02d-%04d"
,
tm
->
tm_hour
,
tm
->
tm_min
,
tm
->
tm_sec
,
tm
->
tm_mon
+
1
,
tm
->
tm_mday
,
tm
->
tm_year
+
1900
);
}
size_t
ademcoMakeEmptyDataPacket
(
ademco_char_t
*
dst_buff
,
size_t
len
,
const
char
*
id
,
uint16_t
seq
,
const
char
*
acct
,
AdemcoId
ademcoId
)
{
const
char
*
id
,
uint16_t
seq
,
const
char
*
acct
,
AdemcoId
ademcoId
)
{
char
buff
[
ADEMCO_PACKET_MAX_LEN
];
char
*
p
=
buff
;
char
*
pcrc
=
buff
+
1
;
...
...
@@ -741,7 +754,8 @@ size_t ademcoMakeEmptyDataPacket(ademco_char_t* dst_buff, size_t len,
size_t
ademcoMakeAdmEmptyDataPacket
(
ademco_char_t
*
dst_buff
,
size_t
len
,
const
char
*
id
,
uint16_t
seq
,
const
char
*
acct
,
AdemcoId
ademcoId
)
{
const
char
*
id
,
uint16_t
seq
,
const
char
*
acct
,
AdemcoId
ademcoId
)
{
char
buff
[
ADEMCO_PACKET_MAX_LEN
];
char
*
p
=
buff
;
char
*
pcrc
=
buff
+
1
;
...
...
@@ -979,7 +993,8 @@ size_t ademcoMakeAdmPacket2(AdemcoPacket* pkt, uint16_t seq, const char* acct, A
seq
,
acct
,
ademcoId
,
ademcoEvent
,
gg
,
zone
,
xdata
);
}
AdemcoParseResult
ademcoPacketParse
(
const
ademco_char_t
*
buff
,
size_t
len
,
AdemcoPacket
*
pkt
,
size_t
*
cb_commited
)
{
AdemcoParseResult
ademcoPacketParse
(
const
ademco_char_t
*
buff
,
size_t
len
,
AdemcoPacket
*
pkt
,
size_t
*
cb_commited
)
{
if
(
len
<
9
)
{
return
RESULT_NOT_ENOUGH
;
}
do
{
const
char
*
p
=
buff
;
...
...
@@ -1065,7 +1080,12 @@ AdemcoParseResult ademcoPacketParse(const ademco_char_t* buff, size_t len, Ademc
if
(
!
isxdigit
(
*
p
))
{
p
=
NULL
;
dline
;
break
;
}
p
++
;
}
if
(
p
==
NULL
||
p
>=
pcr
||
*
p
!=
'['
||
p
-
pacct
>=
ADEMCO_PACKET_ACCT_MAX_LEN
)
{
dline
;
break
;
}
if
(
p
==
NULL
||
p
>=
pcr
||
*
p
!=
'['
||
p
-
pacct
>=
ADEMCO_PACKET_ACCT_MAX_LEN
)
{
dline
;
break
;
}
strncpy
(
pkt
->
acct
,
(
const
char
*
)
pacct
,
p
-
pacct
);
pkt
->
acct
[
p
-
pacct
]
=
'\0'
;
...
...
ademco.h
View file @
6d70a831
This diff is collapsed.
Click to expand it.
examples/httprelay/copy_from_hb.sh
0 → 100644
View file @
6d70a831
#!/bin/bash
set
-e
CURRENT_DIR
=
$(
cd
"
$(
dirname
"
${
BASH_SOURCE
[0]
}
"
)
"
;
pwd
-P
)
cd
${
CURRENT_DIR
}
mkdir
-p
./uvlib
cp
-r
/g/dev_libs/ademco/uvlib/llhttp ./uvlib/
cp
/g/dev_libs/ademco/uvlib/
*
.h ./uvlib/
cp
/g/dev_libs/ademco/uvlib/
*
.c ./uvlib/
\ No newline at end of file
examples/httprelay/httprelay.vcxproj
View file @
6d70a831
...
...
@@ -158,6 +158,7 @@
<ClCompile
Include=
"uvlib\uv_httpc.c"
/>
<ClCompile
Include=
"uvlib\uv_httpd.c"
/>
<ClCompile
Include=
"uvlib\uv_log.c"
/>
<ClCompile
Include=
"uvlib\uv_tcp.c"
/>
<ClCompile
Include=
"uvlib\uv_tcpclient.c"
/>
<ClCompile
Include=
"uvlib\uv_tcpserver.c"
/>
</ItemGroup>
...
...
examples/httprelay/httprelay.vcxproj.filters
View file @
6d70a831
...
...
@@ -66,6 +66,9 @@
<ClCompile
Include=
"cJSON\cJSON.c"
>
<Filter>
cJSON
</Filter>
</ClCompile>
<ClCompile
Include=
"uvlib\uv_tcp.c"
>
<Filter>
uvlib
</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"..\..\ademco.h"
>
...
...
examples/httprelay/uvlib/list.h
View file @
6d70a831
...
...
@@ -4,7 +4,7 @@
#pragma once
#define list_append(type, l, node) \
do { \
do
{
\
type
*
tail_
=
(
l
);
\
while
(
tail_
&&
tail_
->
next
)
\
tail_
=
tail_
->
next
;
\
...
...
@@ -12,7 +12,6 @@ do { \
tail_
->
next
=
(
node
);
\
else
\
(
l
)
=
node
;
\
} while (0);
}
while
(
0
);
#endif
examples/httprelay/uvlib/mybuf.c
View file @
6d70a831
#include "mybuf.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "mybuf.h"
#include "uv_log.h"
void
mybuf_init
(
mybuf_t
*
buf
)
{
...
...
@@ -17,7 +19,7 @@ size_t mybuf_space(mybuf_t* buf) {
void
mybuf_reserve
(
mybuf_t
*
buf
,
size_t
size
)
{
if
(
mybuf_space
(
buf
)
<
size
)
{
//fprintf(stderr, "WARN: mybuf_t not enough, space=%zu, needed=%zu\n", mybuf_space(buf), size);
// fprintf(stderr, "WARN: mybuf_t not enough, space=%zu, needed=%zu\n", mybuf_space(buf), size);
while
(
mybuf_space
(
buf
)
<
size
)
{
buf
->
capacity
*=
2
;
}
...
...
@@ -38,7 +40,7 @@ void mybuf_append(mybuf_t* buf, const char* data, size_t len) {
memcpy
(
buf
->
buf
+
buf
->
size
,
data
,
len
);
buf
->
size
+=
len
;
}
else
{
//fprintf(stderr, "WARN: mybuf_t not enough, space=%zu, needed=%zu\n", mybuf_space(buf), len);
// fprintf(stderr, "WARN: mybuf_t not enough, space=%zu, needed=%zu\n", mybuf_space(buf), len);
while
(
mybuf_space
(
buf
)
<
len
)
{
buf
->
capacity
*=
2
;
}
...
...
@@ -92,3 +94,12 @@ void mybuf_clear(mybuf_t* buf) {
buf
->
size
=
0
;
buf
->
capacity
=
MYBUF_DEFAULT_SIZE
;
}
void
mybuf_clear_append
(
mybuf_t
*
buf
,
const
char
*
data
,
size_t
len
)
{
mybuf_t
tmp
;
mybuf_init
(
&
tmp
);
mybuf_append
(
&
tmp
,
data
,
len
);
mybuf_clear
(
buf
);
mybuf_append
(
buf
,
tmp
.
buf
,
tmp
.
size
);
mybuf_clear
(
&
tmp
);
}
examples/httprelay/uvlib/mybuf.h
View file @
6d70a831
...
...
@@ -17,7 +17,7 @@ typedef struct {
char
*
buf
;
char
mybuf
[
MYBUF_DEFAULT_SIZE
];
size_t
size
,
capacity
;
}
mybuf_t
;
}
mybuf_t
;
void
mybuf_init
(
mybuf_t
*
buf
);
size_t
mybuf_space
(
mybuf_t
*
buf
);
...
...
@@ -32,6 +32,8 @@ void mybuf_cat_printf(mybuf_t* buf, const char* fmt, ...);
#endif
void
mybuf_clear
(
mybuf_t
*
buf
);
// safe to call me when data in buf
void
mybuf_clear_append
(
mybuf_t
*
buf
,
const
char
*
data
,
size_t
len
);
#ifdef __cplusplus
}
...
...
examples/httprelay/uvlib/uv_http.h
View file @
6d70a831
...
...
@@ -3,9 +3,4 @@
#pragma once
#endif // !__UV_HTTP_H__
examples/httprelay/uvlib/uv_httpc.c
View file @
6d70a831
#include
<stdlib.h>
#include <string.h>
#include
"uv_httpc.h"
#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "llhttp/include/llhttp.h"
#include "uv_httpc.h"
#include "uv_log.h"
#include "list.h"
#define DEFAULT_BUFF_SIZE 1024
...
...
@@ -36,7 +38,7 @@ typedef struct {
uv_write_t
req
;
uv_buf_t
uvbuf
;
mybuf_t
buf
;
}
write_req_t
;
}
write_req_t
;
static
void
init_response
(
uv_httpc_response_t
*
response
)
{
response
->
code
=
uv_httpc_ok
;
...
...
@@ -159,7 +161,6 @@ static void setup_default_llhttp_settings(llhttp_settings_t* settings) {
settings
->
on_status_complete
=
on_status_complete
;
}
/*************************** uv callback functions ****************/
static
void
on_close_timer
(
uv_handle_t
*
handle
)
{
...
...
@@ -254,7 +255,7 @@ static void on_write(uv_write_t* req, int status) {
static
int
send_request
(
uv_httpc_t
*
httpc
)
{
int
r
;
write_req_t
*
req
=
malloc
(
sizeof
*
req
);
write_req_t
*
req
=
malloc
(
sizeof
*
req
);
fatal_if_null
(
req
);
mybuf_init
(
&
req
->
buf
);
mybuf_cat_printf
(
&
req
->
buf
,
"%s %s HTTP/1.1
\r\n
"
,
httpc
->
method
,
httpc
->
url
);
...
...
@@ -309,12 +310,11 @@ static void on_resolved(uv_getaddrinfo_t* resolver, int status, struct addrinfo*
fatal_on_uv_err
(
r
,
"uv_tcp_connect failed"
);
}
/*************************** public functions ****************/
int
uv_httpc_create
(
uv_httpc_t
**
httpc
,
uv_loop_t
*
loop
,
const
char
*
host
,
const
char
*
port
,
uv_httpc_on_response_t
on_response
)
{
uv_httpc_t
*
ptr
=
malloc
(
sizeof
*
ptr
);
uv_httpc_t
*
ptr
=
malloc
(
sizeof
*
ptr
);
if
(
!
ptr
)
return
UV_ENOMEM
;
int
r
=
uv_tcp_init
(
loop
,
&
ptr
->
tcp
);
if
(
r
)
{
...
...
@@ -389,7 +389,7 @@ void uv_httpc_free(uv_httpc_t* httpc) {
}
uv_httpc_headers_t
*
uv_httpc_headers_append_nodup
(
uv_httpc_headers_t
*
headers
,
char
*
data
)
{
uv_httpc_headers_t
*
header
=
malloc
(
sizeof
*
header
);
uv_httpc_headers_t
*
header
=
malloc
(
sizeof
*
header
);
if
(
!
header
)
return
NULL
;
header
->
next
=
NULL
;
header
->
data
=
data
;
...
...
@@ -400,9 +400,9 @@ uv_httpc_headers_t* uv_httpc_headers_append_nodup(uv_httpc_headers_t* headers, c
}
uv_httpc_headers_t
*
uv_httpc_headers_dup
(
uv_httpc_headers_t
*
headers
)
{
uv_httpc_headers_t
*
dup
=
NULL
,
*
tail
=
NULL
;
uv_httpc_headers_t
*
dup
=
NULL
,
*
tail
=
NULL
;
while
(
headers
)
{
uv_httpc_headers_t
*
header
=
malloc
(
sizeof
*
header
);
uv_httpc_headers_t
*
header
=
malloc
(
sizeof
*
header
);
fatal_if_null
(
header
);
header
->
data
=
strdup
(
headers
->
data
);
fatal_if_null
(
header
->
data
);
...
...
examples/httprelay/uvlib/uv_httpc.h
View file @
6d70a831
...
...
@@ -6,6 +6,7 @@
#pragma once
#include <uv.h>
#include "mybuf.h"
#ifdef __cplusplus
...
...
@@ -17,7 +18,7 @@ typedef struct uv_httpc_s uv_httpc_t;
typedef
struct
uv_httpc_headers_s
{
char
*
data
;
struct
uv_httpc_headers_s
*
next
;
}
uv_httpc_headers_t
;
}
uv_httpc_headers_t
;
typedef
enum
{
uv_httpc_ok
,
...
...
@@ -26,7 +27,7 @@ typedef enum {
uv_httpc_connection_reset
,
uv_httpc_connection_timeout
,
uv_httpc_parse_failed
,
}
uv_httpc_code
;
}
uv_httpc_code
;
typedef
struct
uv_httpc_response_s
{
uv_httpc_code
code
;
...
...
@@ -35,9 +36,9 @@ typedef struct uv_httpc_response_s {
int
http_status
;
char
*
body
;
size_t
len
;
}
uv_httpc_response_t
;
}
uv_httpc_response_t
;
typedef
void
(
*
uv_httpc_on_response_t
)(
uv_httpc_t
*
httpc
,
uv_httpc_response_t
*
res
);
typedef
void
(
*
uv_httpc_on_response_t
)(
uv_httpc_t
*
httpc
,
uv_httpc_response_t
*
res
);
int
uv_httpc_create
(
uv_httpc_t
**
httpc
,
uv_loop_t
*
loop
,
const
char
*
host
,
const
char
*
port
,
uv_httpc_on_response_t
on_response
);
...
...
@@ -54,7 +55,6 @@ void uv_httpc_headers_free_all(uv_httpc_headers_t* headers, int freedata);
int
uv_httpc_test_main
(
int
argc
,
char
**
argv
);
#endif
#ifdef __cplusplus
}
#endif
...
...
examples/httprelay/uvlib/uv_httpd.c
View file @
6d70a831
#include "uv_httpd.h"
#include <assert.h>
#include <
stdlib.h>
#include <
ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "uv_httpd.h"
#include "mybuf.h"
#include "uv_log.h"
#include "uv_tcp.h"
static
int
enable_print
=
0
;
#define dprintf if (enable_print) printf
#define dnprintf if (enable_print) nprintf
#define dprintf \
if
(
enable_print
)
printf
#define dnprintf \
if
(
enable_print
)
nprintf
#define print_func dprintf("%s\n", __FUNCTION__);
#define HEADERS_DEFAULT_LENGTH 16
#define DEFAULT_BUFF_SIZE 1024
struct
uv_httpd_client_s
{
uv_tcp_t
tcp
;
uv_httpd_server_t
*
server
;
...
...
@@ -36,7 +39,6 @@ struct write_req_t {
static
void
on_close
(
uv_handle_t
*
peer
);
/*************************** helper functions ****************/
static
void
reset_request
(
uv_httpd_client_t
*
client
)
{
...
...
@@ -71,15 +73,13 @@ static void headers_append_value(uv_httpd_client_t* client, size_t offset, size_
static
int
headers_contains
(
uv_httpd_client_t
*
client
,
const
char
*
key
,
const
char
*
value
)
{
for
(
size_t
i
=
0
;
i
<
client
->
req
.
headers
.
n
;
i
++
)
{
uv_httpd_header_t
header
=
client
->
req
.
headers
.
headers
[
i
];
if
(
0
==
string0_nicmp
(
key
,
client
->
req
.
base
+
header
.
key
.
offset
,
header
.
key
.
len
)
&&
0
==
string0_nicmp
(
value
,
client
->
req
.
base
+
header
.
value
.
offset
,
header
.
value
.
len
))
{
if
(
0
==
string0_nicmp
(
key
,
client
->
req
.
base
+
header
.
key
.
offset
,
header
.
key
.
len
)
&&
0
==
string0_nicmp
(
value
,
client
->
req
.
base
+
header
.
value
.
offset
,
header
.
value
.
len
))
{
return
1
;
}
}
return
0
;
}
/*************************** llhttp callback functions ****************/
static
int
on_message_begin
(
llhttp_t
*
llhttp
)
{
...
...
@@ -216,10 +216,10 @@ static int on_chunk_extension_name_complete(llhttp_t* llhttp) {
return
0
;
}
//
static int on_chunk_extension_value_complete(llhttp_t* llhttp) {
//
static int on_chunk_extension_value_complete(llhttp_t* llhttp) {
// print_func;
// return 0;
//
}
//
}
static
int
on_chunk_header
(
llhttp_t
*
llhttp
)
{
print_func
;
...
...
@@ -272,7 +272,8 @@ static void on_write(uv_write_t* req, int status) {
}
static
void
on_alloc
(
uv_handle_t
*
handle
,
size_t
suggested_size
,
uv_buf_t
*
buf
)
{
print_func
;
dprintf
(
"suggested_size=%zu
\n
"
,
suggested_size
);
print_func
;
dprintf
(
"suggested_size=%zu
\n
"
,
suggested_size
);
uv_httpd_client_t
*
client
=
handle
->
data
;
mybuf_reserve
(
&
client
->
buf
,
DEFAULT_BUFF_SIZE
);
buf
->
base
=
client
->
buf
.
buf
;
...
...
@@ -293,7 +294,8 @@ static void on_close(uv_handle_t* peer) {
}
static
void
on_read
(
uv_stream_t
*
stream
,
ssize_t
nread
,
const
uv_buf_t
*
buf
)
{
print_func
;
dprintf
(
"nread= %zd
\n
"
,
nread
);
print_func
;
dprintf
(
"nread= %zd
\n
"
,
nread
);
uv_httpd_client_t
*
client
=
stream
->
data
;
enum
llhttp_errno
parse_ret
;
...
...
@@ -318,28 +320,11 @@ static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
mybuf_clear
(
&
client
->
buf
);
}
static
int
getpeeraddr
(
uv_tcp_t
*
tcp
,
char
*
ip
,
size_t
len
)
{
struct
sockaddr_in
addr
;
int
addrlen
=
sizeof
(
addr
);
int
r
=
uv_tcp_getpeername
(
tcp
,
(
struct
sockaddr
*
)
&
addr
,
&
addrlen
);
if
(
r
)
{
warn_on_uv_err
(
r
,
"uv_tcp_getpeername"
);
return
r
;
}
r
=
uv_ip4_name
(
&
addr
,
ip
,
len
);
if
(
r
)
{
warn_on_uv_err
(
r
,
"uv_ip4_name"
);
}
if
(
len
)
ip
[
len
-
1
]
=
'\0'
;
return
r
;
}
static
void
on_connected
(
uv_stream_t
*
stream
,
int
status
)
{
print_func
;
assert
(
status
==
0
);
uv_httpd_server_t
*
server
=
stream
->
data
;
uv_httpd_client_t
*
client
=
malloc
(
sizeof
*
client
);
uv_httpd_client_t
*
client
=
malloc
(
sizeof
*
client
);
fatal_if_null
(
client
);
int
r
=
uv_tcp_init
(
stream
->
loop
,
&
client
->
tcp
);
fatal_on_uv_err
(
r
,
"uv_tcp_init failed"
);
...
...
@@ -355,11 +340,10 @@ static void on_connected(uv_stream_t* stream, int status) {
mybuf_init
(
&
client
->
pkt
);
client
->
req
.
headers
.
headers
=
client
->
headers
;
client
->
req
.
headers
.
n
=
0
;
getpeeraddr
(
&
client
->
tcp
,
client
->
req
.
ip
,
sizeof
(
client
->
req
.
ip
));
getpeeraddr
(
&
client
->
tcp
,
client
->
req
.
ip
,
sizeof
(
client
->
req
.
ip
),
NULL
);
uv_read_start
((
uv_stream_t
*
)
&
client
->
tcp
,
on_alloc
,
on_read
);
}
/*************************** public functions ****************/
void
nprintf
(
const
char
*
at
,
size_t
len
,
int
newline
)
{
...
...
@@ -460,8 +444,7 @@ void uv_httpd_free(uv_httpd_server_t* server) {
free
(
server
);
}
int
uv_httpd_listen
(
uv_httpd_server_t
*
server
,
const
char
*
ip
,
int
port
)
{
int
uv_httpd_listen
(
uv_httpd_server_t
*
server
,
const
char
*
ip
,
int
port
)
{
int
r
;
struct
sockaddr_in
addr
;
...
...
@@ -477,9 +460,8 @@ int uv_httpd_listen(uv_httpd_server_t* server, const char* ip, int port)
return
r
;
}
int
uv_httpd_write_response
(
uv_httpd_client_t
*
client
,
char
*
response
,
size_t
len
)
{
struct
write_req_t
*
req
=
malloc
(
sizeof
*
req
);
int
uv_httpd_write_response
(
uv_httpd_client_t
*
client
,
char
*
response
,
size_t
len
)
{
struct
write_req_t
*
req
=
malloc
(
sizeof
*
req
);
if
(
!
req
)
return
UV_ENOMEM
;
req
->
buf
.
base
=
malloc
(
len
);
if
(
!
req
->
buf
.
base
)
{
...
...
examples/httprelay/uvlib/uv_httpd.h
View file @
6d70a831
examples/httprelay/uvlib/uv_log.c
View file @
6d70a831
#include <stdio.h>
#include "uv_log.h"
#include <stdarg.h>
#include <
time.h>
#include <
stdio.h>
#include <string.h>
#include <time.h>
#include <uv.h>
#include "uv_log.h"
static
uv_log_level_t
g_level
=
uv_log_level_info
;
static
char
g_logfile
[
1024
]
=
{
0
};
static
char
g_logfile
[
1024
]
=
{
0
};
void
uv_log_set_level
(
uv_log_level_t
level
)
{
g_level
=
level
;
...
...
@@ -46,7 +47,7 @@ void uv_log_raw(uv_log_level_t level, const char* msg) {
static
void
uv_log_vprintf
(
uv_log_level_t
level
,
const
char
*
fmt
,
va_list
ap
)
{
va_list
cpy
;
char
msg
[
1024
],
*
p
=
msg
;
char
msg
[
1024
],
*
p
=
msg
;
size_t
len
=
sizeof
(
msg
);
int
l
=
0
;
...
...
@@ -80,5 +81,4 @@ void uv_log(uv_log_level_t level, const char* fmt, ...) {
va_start
(
ap
,
fmt
);
uv_log_vprintf
(
level
,
fmt
,
ap
);
va_end
(
ap
);
}
examples/httprelay/uvlib/uv_log.h
View file @
6d70a831
...
...
@@ -17,7 +17,7 @@ typedef enum {
uv_log_level_error
,
uv_log_level_fatal
,
uv_log_level_raw
=
1
<<
10
,
// modifier to log without timestamp
}
uv_log_level_t
;
}
uv_log_level_t
;
void
uv_log_set_level
(
uv_log_level_t
level
);
void
uv_log_set_log_file
(
const
char
*
file
);
...
...
@@ -34,32 +34,32 @@ void uv_log_raw(uv_log_level_t level, const char* msg);
#define uvlog_fatal(...) uv_log(uv_log_level_fatal, __VA_ARGS__)
#define warn_on_uv_err(r, msg) \
do { \
do
{
\
if
(
r
)
{
\
uvlog_warn
(
"WARN: %s:%d %s: %d %s %s"
,
\
__FILE__
,
__LINE__
,
\
(
msg
),
(
r
),
uv_err_name
((
r
)),
uv_strerror
((
r
)));
\
}
\
} while (0);
}
while
(
0
);
#define fatal_on_uv_err(r, msg) \
do { \
do
{
\
if
((
r
))
{
\
uvlog_fatal
(
"FATAL %s:%d %s: %d %s %s"
,
\
__FILE__
,
__LINE__
,
\
(
msg
),
r
,
uv_err_name
(
r
),
uv_strerror
((
r
)));
\
exit
(
r
);
\
}
\
} while (0);
}
while
(
0
);
#define fatal_if_null(p) \
do { \
do
{
\
if
((
p
)
==
NULL
)
{
\
uvlog_fatal
(
"FATAL %s:%d malloc failed"
,
\
__FILE__
,
__LINE__
);
\
abort
();
\
}
\
} while (0);
}
while
(
0
);
#ifdef __cplusplus
}
...
...
examples/httprelay/uvlib/uv_tcp.c
0 → 100644
View file @
6d70a831
#include "uv_tcp.h"
#include "uv_log.h"
int
getpeeraddr
(
uv_tcp_t
*
tcp
,
char
*
ip
,
size_t
len
,
uint16_t
*
port
)
{
struct
sockaddr_in
addr
;
int
addrlen
=
sizeof
(
addr
);
int
r
=
uv_tcp_getpeername
(
tcp
,
(
struct
sockaddr
*
)
&
addr
,
&
addrlen
);
if
(
r
)
{
warn_on_uv_err
(
r
,
"uv_tcp_getpeername"
);
return
r
;
}
r
=
uv_ip4_name
(
&
addr
,
ip
,
len
);
if
(
r
)
{
warn_on_uv_err
(
r
,
"uv_ip4_name"
);
}
if
(
len
)
ip
[
len
-
1
]
=
'\0'
;
if
(
port
)
{
*
port
=
ntohs
(
addr
.
sin_port
);
}
return
r
;
}
examples/httprelay/uvlib/uv_tcp.h
View file @
6d70a831
...
...
@@ -5,23 +5,20 @@
#include <uv.h>
#ifdef __cplusplus
extern
"C"
{
#endif
typedef
enum
{
uv_tcp_parse_ok
=
0
,
uv_tcp_parse_not_enough
,
uv_tcp_parse_error
,
}
uv_tcp_parse_result_t
;
}
uv_tcp_parse_result_t
;
int
getpeeraddr
(
uv_tcp_t
*
tcp
,
char
*
ip
,
size_t
len
,
uint16_t
*
port
);
#ifdef __cplusplus
}
#endif
#endif
examples/httprelay/uvlib/uv_tcpclient.c
View file @
6d70a831
#include "uv_tcpclient.h"
#include <assert.h>
#include <string.h>
#include "uv_tcpclient.h"
#include "uv_log.h"
#include "mybuf.h"
#include "uv_log.h"
#define DEFAULT_BUFF_SIZE 1024
typedef
struct
{
uv_write_t
req
;
uv_buf_t
buf
;
}
write_req_t
;
}
write_req_t
;
static
void
client_init
(
uv_tcpclient_t
*
cli
)
{
cli
->
data
=
NULL
;
...
...
@@ -166,7 +168,6 @@ static void on_resolved(uv_getaddrinfo_t* resolver, int status, struct addrinfo*
uv_freeaddrinfo
(
res
);
}
/********************************** uv_tcpclient_t functions ****************************/
int
uv_tcpclient_new
(
uv_tcpclient_t
**
client
,
uv_loop_t
*
loop
,
uv_tcpclient_settings_t
*
settings
)
{
...
...
@@ -207,7 +208,7 @@ int uv_tcpclient_start(uv_tcpclient_t* cli, const char* domain, const char* port
void
uv_tcpclient_send
(
uv_tcpclient_t
*
client
,
const
char
*
buf
,
size_t
len
)
{
if
(
!
client
->
connected
)
return
;
write_req_t
*
req
=
malloc
(
sizeof
*
req
);
write_req_t
*
req
=
malloc
(
sizeof
*
req
);
fatal_if_null
(
req
);
req
->
buf
.
base
=
malloc
(
len
);
fatal_if_null
(
req
->
buf
.
base
);
...
...
examples/httprelay/uvlib/uv_tcpclient.h
View file @
6d70a831
...
...
@@ -3,20 +3,19 @@
#pragma once
#include "uv_tcp.h"
#include "mybuf.h"
#include "uv_tcp.h"
#ifdef __cplusplus
extern
"C"
{
#endif
typedef
struct
uv_tcpclient_s
uv_tcpclient_t
;
typedef
struct
uv_tcpclient_settings_s
uv_tcpclient_settings_t
;
typedef
uv_tcp_parse_result_t
(
*
uv_tcpclient_on_parse_t
)(
uv_tcpclient_t
*
client
,
const
char
*
buf
,
size_t
len
,
size_t
*
ate
);
typedef
void
(
*
uv_tcpclient_on_connect_t
)(
uv_tcpclient_t
*
client
,
int
connected
);
typedef
void
(
*
uv_tcpclient_on_closed_t
)(
uv_tcpclient_t
*
client
);
typedef
uv_tcp_parse_result_t
(
*
uv_tcpclient_on_parse_t
)(
uv_tcpclient_t
*
client
,
const
char
*
buf
,
size_t
len
,
size_t
*
ate
);
typedef
void
(
*
uv_tcpclient_on_connect_t
)(
uv_tcpclient_t
*
client
,
int
connected
);
typedef
void
(
*
uv_tcpclient_on_closed_t
)(
uv_tcpclient_t
*
client
);
int
uv_tcpclient_new
(
uv_tcpclient_t
**
client
,
uv_loop_t
*
loop
,
uv_tcpclient_settings_t
*
settings
);
int
uv_tcpclient_start
(
uv_tcpclient_t
*
client
,
const
char
*
domain
,
const
char
*
port
);
...
...
@@ -24,7 +23,6 @@ void uv_tcpclient_send(uv_tcpclient_t* client, const char* buf, size_t len);
void
uv_tcpclient_stop
(
uv_tcpclient_t
*
client
);
void
uv_tcpclient_free
(
uv_tcpclient_t
*
client
);
struct
uv_tcpclient_settings_s
{
uv_tcpclient_on_connect_t
on_connect
;
uv_tcpclient_on_parse_t
on_parse
;
...
...
@@ -46,8 +44,6 @@ struct uv_tcpclient_s {
int
enable_reconn
;
};
#ifdef __cplusplus
}
#endif
...
...
examples/httprelay/uvlib/uv_tcpserver.c
View file @
6d70a831
#include "uv_tcpserver.h"
#include "uv_log.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "uv_log.h"
#define DEFAULT_BUFF_SIZE 1024
typedef
struct
{
uv_write_t
req
;
uv_buf_t
buf
;
}
write_req_t
;
}
write_req_t
;
static
void
on_close_server
(
uv_handle_t
*
handle
)
{
uv_tcpserver_t
*
server
=
handle
->
data
;
...
...
@@ -51,11 +52,7 @@ static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
mybuf_clear
(
&
cli
->
buf
);
}
else
if
(
p
!=
cli
->
buf
.
buf
)
{
if
(
total
>
0
)
{
mybuf_t
buf
;
mybuf_init
(
&
buf
);
mybuf_append
(
&
buf
,
p
,
total
);
mybuf_clear
(
&
cli
->
buf
);
mybuf_append
(
&
cli
->
buf
,
buf
.
buf
,
buf
.
size
);
mybuf_clear_append
(
&
cli
->
buf
,
p
,
total
);
}
else
{
mybuf_clear
(
&
cli
->
buf
);
}
...
...
@@ -86,7 +83,7 @@ static void on_connection(uv_stream_t* server, int status) {
return
;
}
client
=
malloc
(
sizeof
*
client
);
client
=
malloc
(
sizeof
*
client
);
fatal_if_null
(
client
);
do
{
...
...
@@ -112,7 +109,7 @@ static void on_connection(uv_stream_t* server, int status) {
int
uv_tcpserver_create
(
uv_tcpserver_t
**
server
,
uv_loop_t
*
loop
,
uv_tcpserver_settings_t
*
settings
)
{
int
r
;
uv_tcpserver_t
*
ptr
=
malloc
(
sizeof
*
ptr
);
uv_tcpserver_t
*
ptr
=
malloc
(
sizeof
*
ptr
);
if
(
!
ptr
)
return
UV_ENOMEM
;
r
=
uv_tcp_init
(
loop
,
&
ptr
->
tcp
);
if
(
r
)
{
...
...
@@ -163,7 +160,7 @@ static void after_write(uv_write_t* req, int status) {
int
uv_tcpserver_send_to_cli
(
uv_tcpserver_client_context_t
*
cli
,
const
char
*
buf
,
size_t
len
)
{
int
r
;
write_req_t
*
req
;
req
=
malloc
(
sizeof
*
req
);
req
=
malloc
(
sizeof
*
req
);
if
(
!
req
)
return
UV_ENOMEM
;
req
->
buf
.
base
=
malloc
(
len
);
if
(
!
req
->
buf
.
base
)
{
...
...
examples/httprelay/uvlib/uv_tcpserver.h
View file @
6d70a831
...
...
@@ -4,6 +4,7 @@
#pragma once
#include <uv.h>
#include "mybuf.h"
#include "uv_tcp.h"
...
...
@@ -14,9 +15,9 @@ extern "C" {
typedef
struct
uv_tcpserver_s
uv_tcpserver_t
;
typedef
struct
uv_tcpserver_client_context_s
uv_tcpserver_client_context_t
;
typedef
void
(
*
uv_tcpserver_on_connection_t
)(
uv_tcpserver_client_context_t
*
client
,
int
connected
);
typedef
uv_tcp_parse_result_t
(
*
uv_tcpserver_on_parse_t
)(
uv_tcpserver_client_context_t
*
client
,
const
char
*
buf
,
size_t
len
,
size_t
*
ate
);
typedef
void
(
*
uv_tcpserver_on_server_shutdown_t
)(
uv_tcpserver_t
*
server
);
typedef
void
(
*
uv_tcpserver_on_connection_t
)(
uv_tcpserver_client_context_t
*
client
,
int
connected
);
typedef
uv_tcp_parse_result_t
(
*
uv_tcpserver_on_parse_t
)(
uv_tcpserver_client_context_t
*
client
,
const
char
*
buf
,
size_t
len
,
size_t
*
ate
);
typedef
void
(
*
uv_tcpserver_on_server_shutdown_t
)(
uv_tcpserver_t
*
server
);
typedef
struct
uv_tcpserver_settings_s
uv_tcpserver_settings_t
;
int
uv_tcpserver_create
(
uv_tcpserver_t
**
server
,
uv_loop_t
*
loop
,
uv_tcpserver_settings_t
*
settings
);
...
...
@@ -28,12 +29,6 @@ void uv_tcpserver_close_cli(uv_tcpserver_client_context_t* cli);
void
uv_tcpserver_free_cli_ctx
(
uv_tcpserver_client_context_t
*
cli
);
int
uv_tcpserver_send_to_cli
(
uv_tcpserver_client_context_t
*
cli
,
const
char
*
buf
,
size_t
len
);
struct
uv_tcpserver_s
{
void
*
data
;
uv_tcp_t
tcp
;
...
...
examples/httprelay/uvlib/uvlib.h
0 → 100644
View file @
6d70a831
#ifndef __UVLIB_H__
#define __UVLIB_H__
#pragma once
#include "list.h"
#include "mybuf.h"
#include "uv_httpc.h"
#include "uv_httpd.h"
#include "uv_log.h"
#include "uv_tcpclient.h"
#include "uv_tcpserver.h"
#ifdef _WIN32
#define UVLIB_DIR "G:/dev_libs/ademco/"
#ifdef UVLIB_USE_MT
#define UVLIB_NAME "uvlib_mt.lib"
#else
#define UVLIB_NAME "uvlib.lib"
#endif
#ifdef _WIN64
#ifdef _DEBUG
#pragma comment(lib, UVLIB_DIR "x64/Debug/" UVLIB_NAME)
#else
#pragma comment(lib, UVLIB_DIR "x64/Release/" UVLIB_NAME)
#endif
#else // WIN32
#ifdef _DEBUG
#pragma comment(lib, UVLIB_DIR "Debug/" UVLIB_NAME)
#else
#pragma comment(lib, UVLIB_DIR "Release/" UVLIB_NAME)
#endif
#endif
#endif // _WIN32
#endif
hb_com.c
View file @
6d70a831
...
...
@@ -115,18 +115,17 @@ int hbIsValidZoneStrict(HbMachineType type, uint16_t zone) {
}
int
hbMachineIsSelling
(
HbMachineType
type
)
{
return
type
==
HMT_NETMOD
||
type
==
HMT_GPRS
||
type
==
HMT_GPRS_IOT
||
type
==
HMT_IOT
||
type
==
HMT_LCD
||
type
==
HMT_WIRED
||
type
==
HMT_TRUE_COLOR
||
type
==
HMT_3_SECTION
||
type
==
HMT_GPRS_PHONE
||
type
==
HMT_NB
||
type
==
HMT_WIFI2
;
return
type
==
HMT_GPRS_IOT
||
type
==
HMT_NETMOD
||
type
==
HMT_GPRS
||
type
==
HMT_IOT
||
type
==
HMT_LCD
||
type
==
HMT_WIRED
||
type
==
HMT_TRUE_COLOR
||
type
==
HMT_3_SECTION
||
type
==
HMT_GPRS_PHONE
||
type
==
HMT_NB
||
type
==
HMT_WIFI2
;
}
int
hbMachineCanArm
(
HbMachineType
type
)
{
...
...
@@ -142,37 +141,32 @@ int hbMachineCanEnterSettings(HbMachineType type) {
}
int
hbMachineCanHalfArm
(
HbMachineType
type
)
{
return
type
==
HMT_NETMOD
||
type
==
HMT_LCD
;
return
type
==
HMT_NETMOD
||
type
==
HMT_LCD
;
}
int
hbMachineCanReportSignalStrength
(
HbMachineType
type
)
{
return
type
==
HMT_GPRS
||
type
==
HMT_GPRS_IOT
||
type
==
HMT_IOT
||
type
==
HMT_LCD
||
type
==
HMT_TRUE_COLOR
||
type
==
HMT_3_SECTION
||
type
==
HMT_GPRS_PHONE
||
type
==
HMT_NB
;
return
type
==
HMT_GPRS
||
type
==
HMT_GPRS_IOT
||
type
==
HMT_IOT
||
type
==
HMT_LCD
||
type
==
HMT_TRUE_COLOR
||
type
==
HMT_3_SECTION
||
type
==
HMT_GPRS_PHONE
||
type
==
HMT_NB
;
}
int
hbMachineCanReportBySMS
(
HbMachineType
type
)
{
return
type
==
HMT_GPRS
||
type
==
HMT_LCD
||
type
==
HMT_TRUE_COLOR
||
type
==
HMT_3_SECTION
||
type
==
HMT_GPRS_PHONE
;
return
type
==
HMT_GPRS
||
type
==
HMT_LCD
||
type
==
HMT_TRUE_COLOR
||
type
==
HMT_3_SECTION
||
type
==
HMT_GPRS_PHONE
;
}
int
hbMachineHasWiredZone
(
HbMachineType
type
)
{
return
type
==
HMT_NETMOD
||
type
==
HMT_TRUE_COLOR
||
type
==
HMT_3_SECTION
;
return
type
==
HMT_NETMOD
||
type
==
HMT_TRUE_COLOR
||
type
==
HMT_3_SECTION
;
}
AdemcoZone
hbWiredZoneMin
(
HbMachineType
type
)
{
...
...
@@ -464,10 +458,10 @@ int hbCheckSum(const uint8_t* data, size_t len) {
}
#define copy_data_to_com \
if (cd) {
\
if (cd) {
\
cd->len = len & 0xFF; \
memcpy(cd->data, data, len & 0xFF); \
}
}
HbComRequestType
hbComParseRequest
(
const
uint8_t
*
data
,
size_t
len
,
HbComData
*
cd
)
{
do
{
...
...
hb_com.h
View file @
6d70a831
/*
* Hengbo COM library
* captainwong
* 2022-8-12 rewrited this C version
*/
* Hengbo COM library
* captainwong
* 2022-8-12 rewrited this C version
*/
#ifndef __HB_COM_H__
#define __HB_COM_H__
...
...
@@ -11,7 +11,6 @@
#include "ademco.h"
#ifdef __cplusplus
extern
"C"
{
#endif
...
...
@@ -33,7 +32,7 @@ typedef enum HbMachineStatus {
HMS_SETTING
,
// 设置
HMS_COUNT
,
HMS_INVALID
=
-
1
,
}
HbMachineStatus
;
}
HbMachineStatus
;
typedef
enum
HbMachineType
{
//! 0 WiFi主机 (已停用)
...
...
@@ -65,7 +64,7 @@ typedef enum HbMachineType {
HMT_COUNT
,
HMT_INVALID
=
-
1
,
}
HbMachineType
;
}
HbMachineType
;
typedef
enum
HbZoneProperty
{
//! 匪警全局
...
...
@@ -104,27 +103,27 @@ typedef enum HbZoneProperty {
HZP_COUNT
,
HZP_INVALID
=
-
1
,
}
HbZoneProperty
;
}
HbZoneProperty
;
typedef
struct
HbZoneAndProperty
{
AdemcoZone
zone
;
HbZoneProperty
prop
;
}
HbZoneAndProperty
;
}
HbZoneAndProperty
;
typedef
struct
HbMachineTimePoint
{
uint8_t
hour
;
uint8_t
minute
;
}
HbMachineTimePoint
;
}
HbMachineTimePoint
;
typedef
struct
HbTimer
{
HbMachineTimePoint
armAt
;
HbMachineTimePoint
disarmAt
;
}
HbTimer
;
}
HbTimer
;
typedef
union
HbMachineTimer
{
HbTimer
timer
[
2
];
// 2 set of timer
uint8_t
data
[
8
];
}
HbMachineTimer
;
}
HbMachineTimer
;
// 串口透传数据最大长度
#define HB_COM_DATA_MAX_LEN 64
...
...
@@ -133,7 +132,7 @@ typedef union HbMachineTimer {
typedef
struct
HbComData
{
uint8_t
data
[
HB_COM_DATA_MAX_LEN
];
uint8_t
len
;
}
HbComData
;
}
HbComData
;
// 发给主机
typedef
enum
HbComRequestType
{
...
...
@@ -152,7 +151,7 @@ typedef enum HbComRequestType {
HbComReq_RD_acct
,
// 读取主机账号
HbComReq_WR_acct
,
// 写入主机账号
HbComReq_Invalid
=
-
1
,
}
HbComRequestType
;
}
HbComRequestType
;
// 主机回应
typedef
enum
HbComResponseType
{
...
...
@@ -169,7 +168,7 @@ typedef enum HbComResponseType {
HbComResp_AF
,
// 三区段主机布撤防命令回应
HbComResp_B1
,
// 三区段主机状态回应
HbComResp_Invalid
=
-
1
,
}
HbComResponseType
;
}
HbComResponseType
;
//! 索要主机状态 EB AB 3F A0 75
#define HbComReq_A0_len 5
...
...
@@ -184,19 +183,22 @@ typedef enum HbComResponseType {
#define HbComReq_A1_data "\xEB\xAB\x3F\xA1\x76"
//! 回应主机防区 EB BA 3F PN P0 A2 [Z, P]xN P1 SUM
#define HbComResp_A2_len_min 8 // 无防区数据时长度最小为8
#define HbComResp_A2_max_zone 20 // 最多可以包含 20 个防区
#define HbComResp_A2_len_max (HbComResp_A2_len_min + HbComResp_A2_max_zone * 2) // 一包数据最多有8+20*2=48个字节
// 无防区数据时长度最小为8
#define HbComResp_A2_len_min 8
// 最多可以包含 20 个防区
#define HbComResp_A2_max_zone 20
// 一包数据最多有8+20*2=48个字节
#define HbComResp_A2_len_max (HbComResp_A2_len_min + HbComResp_A2_max_zone * 2)
typedef
enum
HbComResp_A2_p1
{
HbComResp_A2_p1_nomore
=
0xFF
,
// 无更多防区
HbComResp_A2_p1_more
=
0x00
,
// 还有更多防区
}
HbComResp_A2_p1
;
}
HbComResp_A2_p1
;
#define HbComResp_A2_head "\xEB\xBA\x3F\x08\xCC\xA2"
typedef
struct
HbComResp_A2_Iter
{
HbComData
com
;
uint8_t
i
,
total
;
HbComResp_A2_p1
p1
;
}
HbComResp_A2_Iter
;
}
HbComResp_A2_Iter
;
//! 索要更多主机防区 EB AB 3F A2 77
//! 仅应在收到ZoneResponse的param非0xFF时发送,以索要全部防区
...
...
@@ -212,10 +214,11 @@ typedef enum HbComReq_A3_p3 {
HbComReq_A3_p3_learn
=
0x01
,
//! 学码
HbComReq_A3_p3_stop
=
0x02
,
//! 停止学码
HbComReq_A3_p3_modify
=
0x03
,
//! 修改防区属性
}
HbComReq_A3_op
;
}
HbComReq_A3_op
;
//! 学码开始回应 EB BA 3F 07 P0 A3 5A
//! 因为学码时主机要等待外部无线信号(用户触发探测器),因此先回应A3表示已经开始学码,学码成功时回 ZoneOpResponse A4
//! 因为学码时主机要等待外部无线信号(用户触发探测器),
//! 因此先回应A3表示已经开始学码,学码成功时回 ZoneOpResponse A4
#define HbComResp_A3_len 7
#define HbComResp_A3_data "\xEB\xBA\x3F\x07\xCC\xA3\x5A"
...
...
@@ -227,7 +230,7 @@ typedef enum HbComResp_A4_p3 {
HbComResp_A4_p3_ok
=
0x01
,
//! 成功
HbComResp_A4_p3_dup
=
0x02
,
//! 失败--重码
HbComResp_A4_p3_ne
=
0x03
,
//! 失败--防区未对码 not exists
}
HbComResp_A4_p3
;
}
HbComResp_A4_p3
;
//! 获取主机定时器 EB AB 3F A5 7A
#define HbComReq_A5_len 5
...
...
@@ -262,7 +265,7 @@ typedef enum HbComResp_AB_p2 {
HbComResp_AB_p2_close
=
0
,
// 防区失联关
HbComResp_AB_p2_open
=
1
,
// 防区失联开
HbComResp_AB_p2_reject
=
2
,
// 拒绝操作
}
HbComResp_AB_p2
;
}
HbComResp_AB_p2
;
//! 索要防区探头遗失/失联 第一次索要 EB AB 3F AC 81
#define HbComReq_AC_len 5
...
...
@@ -273,30 +276,33 @@ typedef enum HbComResp_AB_p2 {
#define HbComReq_AD_data "\xEB\xAB\x3F\xAD\x82"
//! 索要防区探头遗失/失联回应 EB BA 3F PN P0 AD P1 DATA P2 SUM
#define HbComResp_AD_len_min 9 // 无防区失联数据时一包长度最小为9
#define HbComResp_AD_max_zone 20 // 一包最多包含20个防区
#define HbComResp_AD_len_max (HbComResp_AD_len_min + HbComResp_AD_max_zone * 2) // 一包最多有 9 + 20 *2 = 49 个字节
// 无防区失联数据时一包长度最小为9
#define HbComResp_AD_len_min 9
// 一包最多包含20个防区
#define HbComResp_AD_max_zone 20
// 一包最多有 9 + 20 *2 = 49 个字节
#define HbComResp_AD_len_max (HbComResp_AD_len_min + HbComResp_AD_max_zone * 2)
typedef
enum
HbComResp_AD_p1
{
HbComResp_AD_p1_single
=
0xF0
,
// 防区号以单字节表示
HbComResp_AD_p1_double
=
0xF1
,
// 防区号以双字节表示
}
HbComResp_AD_p1
;
}
HbComResp_AD_p1
;
typedef
enum
HbComResp_AD_p2
{
HbComResp_AD_p2_nomore
=
0xFF
,
// 无更多防区
HbComResp_AD_p2_more
=
0x00
,
// 还有更多防区
}
HbComResp_AD_p2
;
}
HbComResp_AD_p2
;
#define HbComResp_AD_head "\xEB\xBA\x3F\x09\xCC\xAD"
typedef
enum
HbCom_3sec_status
{
HbCom_3sec_arm
=
0x00
,
// 布防(成功)
HbCom_3sec_disarm
=
0x01
,
// 撤防(成功)
}
HbCom_3sec_status
;
}
HbCom_3sec_status
;
typedef
enum
HbCom_3sec_which
{
HbCom_3sec_machine
,
// 主机
HbCom_3sec_section1
,
// 区段1
HbCom_3sec_section2
,
// 区段2
HbCom_3sec_section3
,
// 区段3
}
HbCom_3sec_which
;
}
HbCom_3sec_which
;
//! 三区段主机布撤防命令 EB CB 3F 08 AE P1 P2 SUM
#define HbComReq_AE_len 8
...
...
@@ -318,7 +324,6 @@ typedef enum HbCom_3sec_which {
#define hbComResp_B1_status2(d) ((((d)[6] >> 2) & 0x03) == HbCom_3sec_arm ? HMS_ARM : HMS_DISARM)
#define hbComResp_B1_status3(d) ((((d)[6]) & 0x03) == HbCom_3sec_arm ? HMS_ARM : HMS_DISARM)
//! 读取主机账号
#define HbComReq_RD_acct_len 7
#define HbComReq_RD_acct_data "\xEB\xBA\x3F\x07\x00\x4C\x37"
...
...
@@ -337,8 +342,7 @@ typedef enum HbCom_3sec_which {
static
const
HbZoneProperty
hbZoneProperties
[
12
]
=
{
HZP_BUGLAR
,
HZP_EMERGENCY
,
HZP_FIRE
,
HZP_DURESS
,
HZP_GAS
,
HZP_WATER
,
HZP_SUB_MACHINE
,
HZP_REMOTE_CONTROL
,
HZP_BUGLAR_HALF
,
HZP_SHIELD
,
HZP_DOOR_RING
,
HZP_BYPASS
};
HZP_REMOTE_CONTROL
,
HZP_BUGLAR_HALF
,
HZP_SHIELD
,
HZP_DOOR_RING
,
HZP_BYPASS
};
// return available count
ADEMCO_EXPORT_SYMBOL
int
hbGetAvailableZoneProperties
(
HbMachineType
type
,
HbZoneProperty
props
[
12
]);
...
...
@@ -435,7 +439,8 @@ ADEMCO_EXPORT_SYMBOL HbComResp_A2_p1 hbComResp_A2_Iter_next(HbComResp_A2_Iter* i
ADEMCO_EXPORT_SYMBOL
void
hbComMakeRespA0_getMachineStatus
(
HbComData
*
data
,
HbMachineStatus
status
,
HbMachineType
type
);
// zones and props length is count
// if count is 0, p1 wil be set to HbComResp_A2_p1_nomore
ADEMCO_EXPORT_SYMBOL
void
hbComMakeRespA2_getMachineZones
(
HbComData
*
data
,
size_t
count
,
AdemcoZone
*
zones
,
HbZoneProperty
*
props
,
HbComResp_A2_p1
p1
);
ADEMCO_EXPORT_SYMBOL
void
hbComMakeRespA2_getMachineZones
(
HbComData
*
data
,
size_t
count
,
AdemcoZone
*
zones
,
HbZoneProperty
*
props
,
HbComResp_A2_p1
p1
);
// 学码开始,等待探测器无线信号中
ADEMCO_EXPORT_SYMBOL
void
hbComMakeRespA3_waitingSignal
(
HbComData
*
data
);
ADEMCO_EXPORT_SYMBOL
void
hbComMakeRespA4_modifyMachineZone
(
HbComData
*
data
,
AdemcoZone
zone
,
HbZoneProperty
prop
,
HbComResp_A4_p3
p3
);
...
...
@@ -449,8 +454,11 @@ ADEMCO_EXPORT_SYMBOL void hbComMakeRespAB_modifyMachineZoneLostConfig(HbComData*
ADEMCO_EXPORT_SYMBOL
void
hbComMakeRespAD_getMachineZoneLostConfig
(
HbComData
*
data
,
HbComResp_AD_p1
p1
,
size_t
count
,
AdemcoZone
*
zones
,
HbComResp_AD_p2
p2
);
ADEMCO_EXPORT_SYMBOL
void
hbComMakeRespAF_set3SectionMachineStatus
(
HbComData
*
data
,
HbCom_3sec_which
p1
,
HbCom_3sec_status
status
);
ADEMCO_EXPORT_SYMBOL
void
hbComMakeRespB1_get3SectionMachineStatus
(
HbComData
*
data
,
HbMachineStatus
statusMachine
,
HbMachineStatus
statusSec1
,
HbMachineStatus
statusSec2
,
HbMachineStatus
statusSec3
);
ADEMCO_EXPORT_SYMBOL
void
hbComDataToAdemcoXData
(
const
HbComData
*
const
data
,
AdemcoXDataSegment
*
xdata
,
AdemcoXDataLengthFormat
xlf
,
AdemcoXDataTransform
xtr
);
HbMachineStatus
statusSec1
,
HbMachineStatus
statusSec2
,
HbMachineStatus
statusSec3
);
ADEMCO_EXPORT_SYMBOL
void
hbComDataToAdemcoXData
(
const
HbComData
*
const
data
,
AdemcoXDataSegment
*
xdata
,
AdemcoXDataLengthFormat
xlf
,
AdemcoXDataTransform
xtr
);
#ifdef __cplusplus
}
...
...
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