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
9a0c0023
Commit
9a0c0023
authored
Mar 25, 2020
by
Vladislav Rykov
Browse files
connection works
parent
00cdcf83
Changes
4
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
9a0c0023
CC
=
gcc
INCLUDES
=
-Ilib
/gateway_protocol
LFLAGS
=
-Llib
LDIR
=
lib
LIBS
=
$(LDIR)
/gateway_protocol/gateway_protocol
LIBD
=
-pthread
SRC
=
src/gateway.c
OBJ
=
obj
MAIN
=
gateway
.PHONY
:
depend clean
$(LDIR)/gateway_protocol/gateway_protocol.o
:
$(CC)
-c
$(LIBS)
/gateway_protocol.c
-o
$(OBJ)
/gateway_protocol.o
-I
$(LIBS)
$(CC)
$(SRC)
$(OBJ)
/gateway_protocol.o
-o
$(MAIN)
-I
$(LIBS)
$(LIBD)
all
:
$(MAIN)
@
echo
Compiling gateway project
$(MAIN)
:
$(OBJ)/gateway_protocol.o
$(CC)
$(CFLAGS)
$(INCLUDES)
-o
$(MAIN)
$(OBJS)
$(LFLAGS)
$(LIBS)
.c.o
:
$(CC)
$(CFLAGS)
$(INCLUDES)
-c
$<
-o
$@
clean
:
$(RM)
*
.o
*
~
$(MAIN)
depend
:
$(SRCS)
makedepend
$(INCLUDES)
$^
# line needed by makedepend
gateway
0 → 100755
View file @
9a0c0023
File added
src/gateway.c
0 → 100644
View file @
9a0c0023
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>
#include<stdint.h>
#include<pthread.h>
#include<gateway_protocol.h>
#include<sys/time.h>
void
*
connection_handler
(
void
*
args
);
int
main
(
int
argc
,
char
**
argv
)
{
int
server_desc
,
client_desc
;
struct
sockaddr_in
server
,
client
;
socklen_t
client_socklen
;
uint8_t
buf
[
1024
];
uint8_t
buf_len
=
0
;
uint8_t
payload
[
128
];
uint8_t
payload_length
=
0
;
int
sock_len
;
if
((
server_desc
=
socket
(
AF_INET
,
SOCK_DGRAM
,
IPPROTO_UDP
))
<
0
)
{
perror
(
"socket creation error"
);
return
EXIT_FAILURE
;
}
server
.
sin_family
=
AF_INET
;
server
.
sin_port
=
htons
(
9043
);
server
.
sin_addr
.
s_addr
=
INADDR_ANY
;
if
(
bind
(
server_desc
,
(
struct
sockaddr
*
)
&
server
,
sizeof
(
server
))
<
0
)
{
perror
(
"binding error"
);
return
EXIT_FAILURE
;
}
while
(
1
)
{
printf
(
"listenninig...
\n
"
);
if
((
buf_len
=
recvfrom
(
server_desc
,
(
char
*
)
buf
,
1024
,
MSG_WAITALL
,
(
struct
sockaddr
*
)
&
client
,
&
sock_len
))
<
0
)
{
perror
(
"socket receive error"
);
}
printf
(
"packet received!
\n
"
);
for
(
uint8_t
i
=
0
;
i
<
buf_len
;
i
++
)
{
printf
(
"%02X :"
,
buf
[
i
]);
}
printf
(
"
\n
"
);
uint8_t
dev_id
=
0xFF
;
gateway_protocol_packet_type_t
packet_type
;
if
(
gateway_protocol_packet_decode
(
&
dev_id
,
&
packet_type
,
&
payload_length
,
payload
,
buf_len
,
buf
))
{
if
(
packet_type
==
GATEWAY_PROTOCOL_PACKET_TYPE_TIME_REQ
)
{
printf
(
"TIME REQ received
\n
"
);
struct
timeval
tv
;
buf_len
=
0
;
buf
[
0
]
=
dev_id
;
buf_len
++
;
buf
[
1
]
=
GATEWAY_PROTOCOL_PACKET_TYPE_TIME_SEND
;
buf_len
++
;
gettimeofday
(
&
tv
,
NULL
);
memcpy
(
&
buf
[
buf_len
],
&
tv
.
tv_sec
,
sizeof
(
uint32_t
));
buf_len
+=
sizeof
(
uint32_t
);
}
else
{
perror
(
"packet type error"
);
}
if
(
sendto
(
server_desc
,
(
char
*
)
buf
,
buf_len
,
0
,
(
struct
sockaddr
*
)
&
client
,
sock_len
)
<
0
)
{
perror
(
"sendto error"
);
}
}
else
{
perror
(
"packet decode error"
);
}
}
close
(
server_desc
);
return
EXIT_SUCCESS
;
}
void
*
connection_handler
(
void
*
args
)
{
int
client_desc
=
*
(
int
*
)
args
;
uint8_t
buf
[
128
]
=
""
;
uint8_t
buf_len
=
0
;
uint8_t
payload
[
128
];
uint8_t
payload_length
=
0
;
//strncpy(buf, "connection handler greetings!", sizeof(buf));
//write(client_desc, buf, strlen(buf));
if
((
buf_len
=
recv
(
client_desc
,
buf
,
sizeof
(
buf
),
0
))
>
0
)
{
uint8_t
dev_id
=
0xFF
;
gateway_protocol_packet_type_t
packet_type
;
if
(
gateway_protocol_packet_decode
(
&
dev_id
,
&
packet_type
,
&
payload_length
,
payload
,
buf_len
,
buf
))
{
if
(
packet_type
==
GATEWAY_PROTOCOL_PACKET_TYPE_TIME_REQ
)
{
printf
(
"TIME REQ received
\n
"
);
struct
timeval
tv
;
buf_len
=
0
;
buf
[
0
]
=
dev_id
;
buf_len
++
;
buf
[
1
]
=
GATEWAY_PROTOCOL_PACKET_TYPE_TIME_SEND
;
buf_len
++
;
gettimeofday
(
&
tv
,
NULL
);
memcpy
(
&
buf
[
buf_len
],
&
tv
.
tv_sec
,
sizeof
(
uint32_t
));
buf_len
+=
sizeof
(
uint32_t
);
write
(
client_desc
,
buf
,
buf_len
);
}
else
{
perror
(
"packet type error"
);
}
}
else
{
perror
(
"packet decode error"
);
}
}
}
gateway.c
→
src/
gateway
_tcp
.c
View file @
9a0c0023
...
...
@@ -8,7 +8,7 @@
#include<pthread.h>
#include<
lib/gateway_protocol/
gateway_protocol.h>
#include<gateway_protocol.h>
#include<sys/time.h>
...
...
@@ -36,10 +36,12 @@ int main (int argc, char **argv) {
listen
(
server_desc
,
3
);
printf
(
"liteninig...
\n
"
);
while
((
client_desc
=
accept
(
server_desc
,
(
struct
sockaddr
*
)
&
client
,
&
client_socklen
)))
{
//strncpy(buf, "Hello, client!", sizeof(buf));
//write(client_desc, buf, strlen(buf));
printf
(
"packet received!
\n
"
);
pthread_t
thr
;
if
(
pthread_create
(
&
thr
,
NULL
,
connection_handler
,
(
void
*
)
&
client_desc
)
<
0
)
{
perror
(
"thread creation failed"
);
...
...
@@ -75,6 +77,7 @@ void *connection_handler(void *args) {
buf_len
,
buf
))
{
if
(
packet_type
==
GATEWAY_PROTOCOL_PACKET_TYPE_TIME_REQ
)
{
printf
(
"TIME REQ received
\n
"
);
struct
timeval
tv
;
buf_len
=
0
;
...
...
@@ -89,7 +92,11 @@ void *connection_handler(void *args) {
buf_len
+=
sizeof
(
uint32_t
);
write
(
client_desc
,
buf
,
buf_len
);
}
else
{
perror
(
"packet type error"
);
}
}
else
{
perror
(
"packet decode error"
);
}
}
...
...
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