Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Vladislav Rykov
THSO.gateway
Commits
e4db38a9
Commit
e4db38a9
authored
Apr 01, 2020
by
Vladislav Rykov
Browse files
pend_send assured
parent
c88d7738
Changes
2
Hide whitespace changes
Inline
Side-by-side
gateway
View file @
e4db38a9
No preview for this file type
src/gateway.c
View file @
e4db38a9
...
@@ -14,6 +14,7 @@
...
@@ -14,6 +14,7 @@
#include<signal.h>
#include<signal.h>
#define TIMEDATE_LENGTH 32
#define TIMEDATE_LENGTH 32
#define PEND_SEND_RETRIES_MAX 5
typedef
struct
{
typedef
struct
{
uint32_t
utc
;
uint32_t
utc
;
...
@@ -92,15 +93,11 @@ void ctrc_handler (int sig);
...
@@ -92,15 +93,11 @@ void ctrc_handler (int sig);
static
volatile
uint8_t
working
=
1
;
static
volatile
uint8_t
working
=
1
;
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
//int server_desc, client_desc;
//struct sockaddr_in server, client;
//socklen_t client_socklen;
gcom_ch_t
gch
;
gcom_ch_t
gch
;
uint8_t
buf
[
1024
];
uint8_t
buf
[
1024
];
uint8_t
buf_len
=
0
;
uint8_t
buf_len
=
0
;
uint8_t
payload
[
256
];
uint8_t
payload
[
256
];
uint8_t
payload_length
=
0
;
uint8_t
payload_length
=
0
;
//int sock_len;
PGresult
*
res
;
PGresult
*
res
;
signal
(
SIGINT
,
ctrc_handler
);
signal
(
SIGINT
,
ctrc_handler
);
...
@@ -198,35 +195,41 @@ int main (int argc, char **argv) {
...
@@ -198,35 +195,41 @@ int main (int argc, char **argv) {
PQclear
(
res
);
PQclear
(
res
);
printf
(
"prepared to send %d bytes
\n
"
,
payload_length
);
printf
(
"prepared to send %d bytes
\n
"
,
payload_length
);
packet_encode
(
// send the msg until ack is received
gch
.
dev_id
,
uint8_t
received_ack
=
0
;
GATEWAY_PROTOCOL_PACKET_TYPE_PEND_SEND
,
uint8_t
pend_send_retries
=
PEND_SEND_RETRIES_MAX
;
payload_length
,
payload
,
do
{
&
buf_len
,
buf
);
packet_encode
(
gch
.
dev_id
,
GATEWAY_PROTOCOL_PACKET_TYPE_PEND_SEND
,
payload_length
,
payload
,
&
buf_len
,
buf
);
send_gcom_ch
(
&
gch
,
buf
,
buf_len
);
recv_gcom_ch
(
&
gch
,
buf
,
&
buf_len
,
1024
);
send_gcom_ch
(
&
gch
,
buf
,
buf_len
);
if
(
packet_decode
(
recv_gcom_ch
(
&
gch
,
buf
,
&
buf_len
,
1024
);
&
gch
.
dev_id
,
&
packet_type
,
if
(
packet_decode
(
&
buf_len
,
buf
,
&
gch
.
dev_id
,
buf_len
,
buf
))
&
packet_type
,
&
payload_length
,
payload
,
buf_len
,
buf
))
{
if
(
packet_type
==
GATEWAY_PROTOCOL_PACKET_TYPE_STAT
&&
payload_length
==
1
&&
payload
[
0
]
==
GATEWAY_PROTOCOL_STAT_ACK
)
{
{
sprintf
(
buf
,
"DELETE FROM pend_msgs WHERE dev_id = %d AND msg = '%s'"
,
gch
.
dev_id
,
msg_cont
);
if
(
packet_type
==
GATEWAY_PROTOCOL_PACKET_TYPE_STAT
&&
printf
(
"%s"
,
buf
);
buf_len
==
1
&&
res
=
PQexec
(
conn
,
buf
);
buf
[
0
]
==
GATEWAY_PROTOCOL_STAT_ACK
)
if
(
PQresultStatus
(
res
)
!=
PGRES_COMMAND_OK
)
{
{
fprintf
(
stderr
,
"error db deleting : %s"
,
PQerrorMessage
(
conn
));
sprintf
(
buf
,
"DELETE FROM pend_msgs WHERE dev_id = %d AND msg = '%s'"
,
gch
.
dev_id
,
msg_cont
);
printf
(
"%s"
,
buf
);
res
=
PQexec
(
conn
,
buf
);
if
(
PQresultStatus
(
res
)
!=
PGRES_COMMAND_OK
)
{
fprintf
(
stderr
,
"error db deleting : %s"
,
PQerrorMessage
(
conn
));
}
PQclear
(
res
);
received_ack
=
1
;
printf
(
"ACK received
\n
"
);
}
}
PQclear
(
res
);
printf
(
"ACK received
\n
"
);
}
}
}
}
while
(
!
received_ack
&&
pend_send_retries
--
);
}
}
}
else
{
}
else
{
fprintf
(
stderr
,
"packet type error : %02X
\n
"
,
packet_type
);
fprintf
(
stderr
,
"packet type error : %02X
\n
"
,
packet_type
);
...
...
Write
Preview
Supports
Markdown
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