Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
EmbeddedLinuxProject
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
Service Desk
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
Christoph-Anton Schwierz
EmbeddedLinuxProject
Commits
82deeeff
Commit
82deeeff
authored
5 months ago
by
Felix Moser
Browse files
Options
Downloads
Patches
Plain Diff
restructuring
parent
f2f3582b
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
main.c
+96
-74
96 additions, 74 deletions
main.c
with
96 additions
and
74 deletions
main.c
+
96
−
74
View file @
82deeeff
...
@@ -5,100 +5,122 @@
...
@@ -5,100 +5,122 @@
#include
<fcntl.h>
#include
<fcntl.h>
#include
<string.h>
#include
<string.h>
#include
<errno.h>
#include
<errno.h>
#include
<zlib.h>
static
const
char
*
PIPE_ONE
=
"/tmp/pipeOne"
;
//
static const char *PIPE_ONE = "/tmp/pipeOne";
static
const
char
*
PIPE_TWO
=
"/tmp/pipeTwo"
;
//
static const char *PIPE_TWO = "/tmp/pipeTwo";
int
main
(
void
)
static
const
char
*
CHAR_DEV
=
"/dev/packet_receiver"
;
{
// mkfifo() nur ausführen, wenn die Pipes noch nicht existieren,
// bzw. Fehler ignorieren, falls schon existiert:
if
(
mkfifo
(
PIPE_ONE
,
0666
)
<
0
&&
errno
!=
EEXIST
)
{
perror
(
"mkfifo pipeOne"
);
return
1
;
}
if
(
mkfifo
(
PIPE_TWO
,
0666
)
<
0
&&
errno
!=
EEXIST
)
{
perror
(
"mkfifo pipeTwo"
);
return
1
;
}
// Beide Pipes blockierend öffnen
static
const
char
*
SENDER_ID
=
"0x91"
;
int
fdOne
=
open
(
PIPE_ONE
,
O_RDONLY
);
if
(
fdOne
==
-
1
)
{
char
*
build_crc_checksum
(
const
char
*
payload
)
{
perror
(
"open pipeOne for reading"
);
uLong
crc
=
crc32
(
0L
,
Z_NULL
,
0
);
return
1
;
crc
=
crc32
(
crc
,
payload
,
strlen
(
payload
));
char
*
crc_checksum
=
(
"0x%08lX"
,
crc
);
return
crc_checksum
;
}
}
int
fdTwo
=
open
(
PIPE_TWO
,
O_RDONLY
);
char
*
build_package
(
char
const
*
payload
)
{
if
(
fdTwo
==
-
1
)
{
char
*
crc_checksum
=
build_crc_checksum
(
payload
);
perror
(
"open pipeTwo for reading"
);
close
(
fdOne
);
return
"SENDER=%s %s %s"
,
SENDER_ID
,
payload
,
crc_checksum
;
return
1
;
}
}
// Dauerhafte Lese-Schleife
void
send_package
(
const
char
*
package
,
const
int
fdCharDev
)
{
while
(
1
)
{
char
transmitBuf
[
256
]
=
{
0
};
char
bufOne
[
128
]
=
{
0
};
snprintf
(
transmitBuf
,
sizeof
(
transmitBuf
),
package
);
char
bufTwo
[
128
]
=
{
0
};
// =========== Lesevorgang PipeOne ===========
const
ssize_t
written
=
write
(
fdCharDev
,
transmitBuf
,
strlen
(
transmitBuf
));
if
(
written
<
0
)
{
perror
(
"write /dev/packet_receiver"
);
}
else
{
printf
(
"Wrote %ld bytes to %s
\n
"
,
written
,
CHAR_DEV
);
}
}
char
*
read_pipe
(
int
fd
,
char
const
*
pipeName
,
char
*
payload
)
{
char
buf
[
128
]
=
{
0
};
// =========== Lesevorgang Pipe ===========
// read() kann blockieren, wenn noch keine Daten da sind,
// read() kann blockieren, wenn noch keine Daten da sind,
// da wir blockierendes I/O verwenden.
// da wir blockierendes I/O verwenden.
// Wenn die Schreibseite offen bleibt, kommen periodisch neue Daten.
// Wenn die Schreibseite offen bleibt, kommen periodisch neue Daten.
ssize_t
bytesReadOne
=
read
(
fd
One
,
buf
One
,
sizeof
(
buf
One
));
const
ssize_t
bytesReadOne
=
read
(
fd
,
buf
,
sizeof
(
buf
));
if
(
bytesReadOne
>
0
)
{
if
(
bytesReadOne
>
0
)
{
// Gültige Daten -> ausgeben
// Gültige Daten -> ausgeben
printf
(
"
PipeOne CPU Temp
: %s
°C
\n
"
,
buf
One
);
printf
(
"
Value
: %s
\n
"
,
buf
);
}
else
if
(
bytesReadOne
==
0
)
{
}
else
if
(
bytesReadOne
==
0
)
{
// EOF -> Schreibseite hat geschlossen
// EOF -> Schreibseite hat geschlossen
// -> ggf. Pipe erneut öffnen
// -> ggf. Pipe erneut öffnen
printf
(
"Pipe
One
closed by writer. Reopening...
\n
"
);
printf
(
"Pipe closed by writer. Reopening...
\n
"
);
close
(
fd
One
);
close
(
fd
);
// Neu öffnen
// Neu öffnen
fdOne
=
open
(
PIPE_ONE
,
O_RDONLY
);
fd
=
open
(
pipeName
,
O_RDONLY
);
if
(
fdOne
==
-
1
)
{
if
(
fd
==
-
1
)
{
perror
(
"Reopen pipeOne"
);
perror
(
"Reopen pipe"
);
break
;
// oder return 1;
}
}
}
else
{
}
else
{
// bytesReadOne < 0 => Fehler
// bytesReadOne < 0 => Fehler
if
(
errno
==
EINTR
)
{
if
(
errno
==
EINTR
)
{
// Signal unterbrochen, einfach weiter
// Signal unterbrochen, einfach weiter
continue
;
return
;
}
perror
(
"read pipe"
);
}
}
perror
(
"read pipeOne"
);
break
;
// oder return 1;
}
}
// =========== Lesevorgang PipeTwo ===========
// argc = Anzahl Pipes *argv[] Pipe Pfade
ssize_t
bytesReadTwo
=
read
(
fdTwo
,
bufTwo
,
sizeof
(
bufTwo
));
int
main
(
const
int
argc
,
char
*
argv
[])
{
if
(
bytesReadTwo
>
0
)
{
int
fds
[
argc
];
printf
(
"PipeTwo CPU Freq: %s GHz
\n
"
,
bufTwo
);
}
else
if
(
bytesReadTwo
==
0
)
{
for
(
int
i
=
0
;
i
<
argc
;
i
++
)
{
printf
(
"PipeTwo closed by writer. Reopening...
\n
"
);
// mkfifo() nur ausführen, wenn die Pipes noch nicht existieren,
close
(
fdTwo
);
// bzw. Fehler ignorieren, falls schon existiert:
fdTwo
=
open
(
PIPE_TWO
,
O_RDONLY
);
if
(
mkfifo
(
argv
[
i
],
0666
)
<
0
&&
errno
!=
EEXIST
)
{
if
(
fdTwo
==
-
1
)
{
perror
(
"mkfifo pipe"
);
perror
(
"Reopen pipeTwo"
);
return
1
;
break
;
}
// Pipes blockierend öffnen
fds
[
i
]
=
open
(
argv
[
i
],
O_RDONLY
);
if
(
fds
[
i
]
==
-
1
)
{
perror
(
"open pipeOne for reading"
);
return
1
;
}
}
}
else
{
if
(
errno
==
EINTR
)
{
continue
;
}
}
perror
(
"read pipeTwo"
);
break
;
// Open the char device for writing
const
int
fdCharDev
=
open
(
CHAR_DEV
,
O_WRONLY
);
if
(
fdCharDev
==
-
1
)
{
perror
(
"open /dev/packet_receiver"
);
for
(
int
i
=
0
;
i
<
argc
;
i
++
)
{
close
(
fds
[
i
]);
}
return
1
;
}
printf
(
"Opened %s for writing.
\n
"
,
CHAR_DEV
);
// Dauerhafte Lese-Schleife
while
(
1
)
{
// =========== Lesevorgang Pipes ===========
for
(
int
i
=
0
;
i
<
argc
;
i
++
)
{
read_pipe
(
fds
[
i
],
argv
[
i
]);
}
}
// =========== Zum cdev schreiben ===========
send_package
(
package
,
fdCharDev
);
// Kurze Pause, damit CPU-Last nicht durch
// Kurze Pause, damit CPU-Last nicht durch
// Dauerschleife hochgetrieben wird
// Dauerschleife hochgetrieben wird
usleep
(
200000
);
// 200 ms
usleep
(
200000
);
// 200 ms
}
}
// Sollte man jemals aus der while(1)-Schleife ausbrechen:
// Sollte man jemals aus der while(1)-Schleife ausbrechen:
close
(
fdOne
);
for
(
int
i
=
0
;
i
<
argc
;
i
++
)
{
close
(
fdTwo
);
close
(
fds
[
i
]);
}
return
0
;
return
0
;
}
}
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