Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
server-node
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ThiefSil
server-node
Commits
ea289913
Commit
ea289913
authored
7 months ago
by
천 진강
Browse files
Options
Downloads
Patches
Plain Diff
fix(c): WSL 환경에서 컴파일 안되던 문제 수정
parent
9e418175
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
server-node-control/control.c
+69
-39
69 additions, 39 deletions
server-node-control/control.c
with
69 additions
and
39 deletions
server-node-control/control.c
+
69
−
39
View file @
ea289913
...
@@ -14,6 +14,10 @@
...
@@ -14,6 +14,10 @@
#define VALUE_MAX 256
#define VALUE_MAX 256
#define DIRECTION_MAX 256
#define DIRECTION_MAX 256
#ifndef PTHREAD_STACK_MIN
#define PTHREAD_STACK_MIN 16384
#endif
typedef
struct
{
typedef
struct
{
int
socket
;
int
socket
;
struct
sockaddr_in
address
;
struct
sockaddr_in
address
;
...
@@ -72,7 +76,7 @@ static int PWMEnable(int pwmnum) {
...
@@ -72,7 +76,7 @@ static int PWMEnable(int pwmnum) {
int
fd
;
int
fd
;
// TODO: Enter the enable path.
// TODO: Enter the enable path.
snprintf
(
path
,
DIRECTION_MAX
,
"/sys/class/pwm/pwmchip0/pwm0/enable"
,
pwmnum
);
snprintf
(
path
,
DIRECTION_MAX
,
"/sys/class/pwm/pwmchip0/pwm0/enable"
);
fd
=
open
(
path
,
O_WRONLY
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open in enable!
\n
"
);
fprintf
(
stderr
,
"Failed to open in enable!
\n
"
);
...
@@ -90,7 +94,7 @@ static int PWMWritePeriod(int pwmnum, int value) {
...
@@ -90,7 +94,7 @@ static int PWMWritePeriod(int pwmnum, int value) {
int
fd
,
byte
;
int
fd
,
byte
;
// TODO: Enter the period path.
// TODO: Enter the period path.
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm0/period"
,
pwmnum
);
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm0/period"
);
fd
=
open
(
path
,
O_WRONLY
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open in period!
\n
"
);
fprintf
(
stderr
,
"Failed to open in period!
\n
"
);
...
@@ -114,7 +118,7 @@ static int PWMWriteDutyCycle(int pwmnum, int value) {
...
@@ -114,7 +118,7 @@ static int PWMWriteDutyCycle(int pwmnum, int value) {
int
fd
,
byte
;
int
fd
,
byte
;
// TODO: Enter the duty_cycle path.
// TODO: Enter the duty_cycle path.
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm0/duty_cycle"
,
pwmnum
);
snprintf
(
path
,
VALUE_MAX
,
"/sys/class/pwm/pwmchip0/pwm0/duty_cycle"
);
fd
=
open
(
path
,
O_WRONLY
);
fd
=
open
(
path
,
O_WRONLY
);
if
(
-
1
==
fd
)
{
if
(
-
1
==
fd
)
{
fprintf
(
stderr
,
"Failed to open in duty cycle!
\n
"
);
fprintf
(
stderr
,
"Failed to open in duty cycle!
\n
"
);
...
@@ -132,7 +136,7 @@ static int PWMWriteDutyCycle(int pwmnum, int value) {
...
@@ -132,7 +136,7 @@ static int PWMWriteDutyCycle(int pwmnum, int value) {
return
(
0
);
return
(
0
);
}
}
// 수신 스레드 함수
//
클라이언트
수신 스레드 함수
void
*
ClientToServer
(
void
*
idx
)
{
void
*
ClientToServer
(
void
*
idx
)
{
int
i
=
*
((
int
*
)
idx
);
int
i
=
*
((
int
*
)
idx
);
char
buffer
[
BUFFER_SIZE
];
char
buffer
[
BUFFER_SIZE
];
...
@@ -141,17 +145,17 @@ void *ClientToServer(void *idx) {
...
@@ -141,17 +145,17 @@ void *ClientToServer(void *idx) {
while
((
len
=
recv
(
clients
[
i
].
socket
,
buffer
,
BUFFER_SIZE
,
0
))
>
0
)
{
while
((
len
=
recv
(
clients
[
i
].
socket
,
buffer
,
BUFFER_SIZE
,
0
))
>
0
)
{
buffer
[
len
]
=
'\0'
;
buffer
[
len
]
=
'\0'
;
printf
(
"Received from client %d: %s"
,
&
clients
[
i
].
address
.
sin_addr
,
buffer
);
inet_ntop
(
AF_INET
,
&
clients
[
i
].
address
.
sin_addr
,
ip
,
INET_ADDRSTRLEN
);
inet_ntop
(
AF_INET
,
&
clients
[
i
].
address
.
sin_addr
,
ip
,
INET_ADDRSTRLEN
);
printf
(
"Received from client %s: %s"
,
ip
,
buffer
);
if
(
send
(
pySocket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
if
(
send
(
pySocket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
perror
(
"
Error sending
message to server"
);
perror
(
"
Send
message to server
error
"
);
}
}
}
}
if
(
len
==
0
)
{
if
(
len
==
0
)
{
printf
(
"Client disconnected: %d
\n
"
,
clients
[
i
].
socket
);
printf
(
"Client disconnected: %d
\n
"
,
clients
[
i
].
socket
);
}
else
if
(
len
==
-
1
)
{
}
else
if
(
len
==
-
1
)
{
perror
(
"recv
failed
"
);
perror
(
"recv
error
"
);
}
}
clientCnt
--
;
clientCnt
--
;
close
(
clients
[
i
].
socket
);
close
(
clients
[
i
].
socket
);
...
@@ -162,8 +166,7 @@ void *ServerToClient() {
...
@@ -162,8 +166,7 @@ void *ServerToClient() {
char
buffer
[
BUFFER_SIZE
];
char
buffer
[
BUFFER_SIZE
];
int
len
;
int
len
;
while
(
1
)
{
while
(
1
)
{
len
=
read
(
pySocket
,
buffer
,
BUFFER_SIZE
-
1
);
if
(
len
=
recv
(
pySocket
,
buffer
,
BUFFER_SIZE
-
1
,
0
)
<=
0
)
{
if
(
len
<=
0
)
{
printf
(
"Server disconnected
\n
"
);
printf
(
"Server disconnected
\n
"
);
close
(
pySocket
);
close
(
pySocket
);
exit
(
0
);
exit
(
0
);
...
@@ -186,14 +189,20 @@ void *ServerToClient() {
...
@@ -186,14 +189,20 @@ void *ServerToClient() {
else
if
(
buffer
[
2
]
==
'P'
)
{
else
if
(
buffer
[
2
]
==
'P'
)
{
// 충전기 노드에 주차 사실 알림
// 충전기 노드에 주차 사실 알림
if
(
send
(
clients
[
0
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
if
(
send
(
clients
[
0
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
perror
(
"Error forwarding message to client"
);
perror
(
"Send message error"
);
}
if
(
send
(
clients
[
1
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
perror
(
"Send message error"
);
}
}
}
}
else
if
(
buffer
[
2
]
==
'E'
)
{
else
if
(
buffer
[
2
]
==
'E'
)
{
// 충전기 노드에 주차 사실 알림
if
(
send
(
clients
[
0
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
if
(
send
(
clients
[
0
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
perror
(
"Error forwarding message to client"
);
perror
(
"Send message error"
);
}
if
(
send
(
clients
[
1
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
perror
(
"Send message error"
);
}
}
if
(
PWMExport
(
PWM
))
printf
(
"export error
\n
"
);
if
(
PWMExport
(
PWM
))
printf
(
"export error
\n
"
);
if
(
PWMWritePeriod
(
PWM
,
20000000
))
printf
(
"period error
\n
"
);
if
(
PWMWritePeriod
(
PWM
,
20000000
))
printf
(
"period error
\n
"
);
...
@@ -208,11 +217,19 @@ void *ServerToClient() {
...
@@ -208,11 +217,19 @@ void *ServerToClient() {
// 충전기 노드에 화재 사실 알림
// 충전기 노드에 화재 사실 알림
// 화재 노드에 화재 사실 알림
// 화재 노드에 화재 사실 알림
if
(
send
(
clients
[
0
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
if
(
send
(
clients
[
0
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
perror
(
"Error forwarding message to client"
);
perror
(
"Send message error"
);
}
if
(
send
(
clients
[
2
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
perror
(
"Send message error"
);
}
}
}
if
(
send
(
clients
[
3
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
else
if
(
buffer
[
0
]
==
'C'
)
{
perror
(
"Error forwarding message to client"
);
for
(
int
x
=
0
;
x
<
3
;
x
++
)
{
if
(
send
(
clients
[
x
].
socket
,
buffer
,
strlen
(
buffer
),
0
)
<
0
)
{
perror
(
"Send message error"
);
}
}
}
}
}
}
}
...
@@ -223,20 +240,22 @@ int main() {
...
@@ -223,20 +240,22 @@ int main() {
pthread_attr_t
attr
,
attr2
,
at
[
3
];
pthread_attr_t
attr
,
attr2
,
at
[
3
];
struct
sched_param
param
,
param2
;
struct
sched_param
param
,
param2
;
socklen_t
clientAddrLen
=
sizeof
(
clientAddr
);
socklen_t
clientAddrLen
=
sizeof
(
clientAddr
);
param
.
sched_priority
=
3
;
param
.
sched_priority
=
sched_get_priority_max
(
SCHED_RR
)
;
param2
.
sched_priority
=
2
;
param2
.
sched_priority
=
sched_get_priority_max
(
SCHED_RR
)
-
1
;
pthread_attr_init
(
&
attr
);
pthread_attr_init
(
&
at
[
2
]);
pthread_attr_setschedpolicy
(
&
attr
,
SCHED_RR
);
pthread_attr_setschedpolicy
(
&
at
[
2
],
SCHED_RR
);
pthread_attr_setinheritsched
(
&
attr
,
PTHREAD_EXPLICIT_SCHED
);
pthread_attr_setinheritsched
(
&
at
[
2
],
PTHREAD_EXPLICIT_SCHED
);
pthread_attr_setschedparam
(
&
attr
,
&
param
);
pthread_attr_setschedparam
(
&
at
[
2
],
&
param
);
pthread_attr_setstacksize
(
&
at
[
2
],
PTHREAD_STACK_MIN
);
pthread_attr_init
(
&
attr2
);
for
(
int
i
=
0
;
i
<
2
;
i
++
)
{
pthread_attr_setschedpolicy
(
&
attr2
,
SCHED_RR
);
pthread_attr_init
(
&
at
[
i
]);
pthread_attr_setinheritsched
(
&
attr2
,
PTHREAD_EXPLICIT_SCHED
);
pthread_attr_setschedpolicy
(
&
at
[
i
],
SCHED_RR
);
pthread_attr_setschedparam
(
&
attr2
,
&
param2
);
pthread_attr_setinheritsched
(
&
at
[
i
],
PTHREAD_EXPLICIT_SCHED
);
at
[
0
]
=
at
[
1
]
=
attr2
;
pthread_attr_setschedparam
(
&
at
[
i
],
&
param2
);
at
[
2
]
=
attr
;
pthread_attr_setstacksize
(
&
at
[
i
],
PTHREAD_STACK_MIN
);
}
// 서버로 연결할 클라이언트 소켓 생성
// 서버로 연결할 클라이언트 소켓 생성
if
((
pySocket
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
<
0
)
{
if
((
pySocket
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
<
0
)
{
...
@@ -248,18 +267,18 @@ int main() {
...
@@ -248,18 +267,18 @@ int main() {
pyAddr
.
sin_family
=
AF_INET
;
pyAddr
.
sin_family
=
AF_INET
;
pyAddr
.
sin_port
=
htons
(
49152
);
pyAddr
.
sin_port
=
htons
(
49152
);
if
(
inet_pton
(
AF_INET
,
"127.0.0.1"
,
&
pyAddr
.
sin_addr
)
<=
0
)
{
if
(
inet_pton
(
AF_INET
,
"127.0.0.1"
,
&
pyAddr
.
sin_addr
)
<=
0
)
{
perror
(
"
Invalid address / Address not supported
"
);
perror
(
"
Address error
"
);
return
-
1
;
return
-
1
;
}
}
if
(
connect
(
pySocket
,
(
struct
sockaddr
*
)
&
pyAddr
,
sizeof
(
pyAddr
))
<
0
)
{
if
(
connect
(
pySocket
,
(
struct
sockaddr
*
)
&
pyAddr
,
sizeof
(
pyAddr
))
<
0
)
{
perror
(
"Connection
failed
"
);
perror
(
"Connection
error
"
);
return
-
1
;
return
-
1
;
}
}
printf
(
"
서버에 연결되었습니다.
\n
"
);
printf
(
"
Connected to server
\n
"
);
// 클라이언트를 받을 서버 소켓 설정
// 클라이언트를 받을 서버 소켓 설정
if
((
cSocket
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
==
0
)
{
if
((
cSocket
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
))
==
0
)
{
perror
(
"Socket
failed
"
);
perror
(
"Socket
error
"
);
return
-
1
;
return
-
1
;
}
}
cAddr
.
sin_family
=
AF_INET
;
cAddr
.
sin_family
=
AF_INET
;
...
@@ -267,13 +286,13 @@ int main() {
...
@@ -267,13 +286,13 @@ int main() {
cAddr
.
sin_port
=
htons
(
PORT
);
cAddr
.
sin_port
=
htons
(
PORT
);
if
(
bind
(
cSocket
,
(
struct
sockaddr
*
)
&
cAddr
,
sizeof
(
cAddr
))
<
0
)
{
if
(
bind
(
cSocket
,
(
struct
sockaddr
*
)
&
cAddr
,
sizeof
(
cAddr
))
<
0
)
{
perror
(
"Bind
failed
"
);
perror
(
"Bind
error
"
);
close
(
cSocket
);
close
(
cSocket
);
return
-
1
;
return
-
1
;
}
}
if
(
listen
(
cSocket
,
3
)
<
0
)
{
if
(
listen
(
cSocket
,
3
)
<
0
)
{
perror
(
"Listen
failed
"
);
perror
(
"Listen
error
"
);
close
(
cSocket
);
close
(
cSocket
);
return
-
1
;
return
-
1
;
}
}
...
@@ -285,7 +304,7 @@ int main() {
...
@@ -285,7 +304,7 @@ int main() {
int
x
;
int
x
;
char
ip
[
INET_ADDRSTRLEN
];
char
ip
[
INET_ADDRSTRLEN
];
if
((
x
=
accept
(
cSocket
,
(
struct
sockaddr
*
)
&
clientAddr
,
&
clientAddrLen
))
<
0
)
{
if
((
x
=
accept
(
cSocket
,
(
struct
sockaddr
*
)
&
clientAddr
,
&
clientAddrLen
))
<
0
)
{
perror
(
"Accept
failed
"
);
perror
(
"Accept
error
"
);
continue
;
continue
;
}
}
...
@@ -294,11 +313,22 @@ int main() {
...
@@ -294,11 +313,22 @@ int main() {
tmp
->
address
=
clientAddr
;
tmp
->
address
=
clientAddr
;
clients
[
clientCnt
]
=
*
tmp
;
clients
[
clientCnt
]
=
*
tmp
;
inet_ntop
(
AF_INET
,
&
tmp
->
address
.
sin_addr
,
ip
,
INET_ADDRSTRLEN
);
inet_ntop
(
AF_INET
,
&
tmp
->
address
.
sin_addr
,
ip
,
INET_ADDRSTRLEN
);
int
idx
=
ip
[
11
]
-
'7'
;
//int *idx = (int *)malloc(sizeof(int *));
int
*
idx
=
ip
[
11
]
-
'7'
;
if
(
ip
[
11
]
-
'7'
>
2
||
ip
[
11
]
-
'7'
<
0
)
*
idx
=
0
;
*
idx
=
clientCnt
;
if
(
pthread_create
(
&
clientThreads
[
*
idx
],
&
at
[
*
idx
],
ClientToServer
,
(
void
*
)
idx
)
<
0
)
{
perror
(
"Create thread error"
);
close
(
x
);
free
(
tmp
);
}
if
(
pthread_
create
(
&
clientThreads
[
idx
],
&
at
[
idx
],
ClientToServer
,
(
void
*
)
&
idx
)
<
0
)
{
if
(
pthread_
detach
(
clientThreads
[
*
idx
]
)
<
0
)
{
perror
(
"
Failed to create
thread"
);
perror
(
"
Detach
thread
error
"
);
close
(
x
);
close
(
x
);
free
(
tmp
);
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment