Skip to content

Commit b612b8e

Browse files
committed
First commit
0 parents  commit b612b8e

13 files changed

Lines changed: 169 additions & 0 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Pierre Pélisset
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# php-ioctl
2+
3+
This package is a basic port of Ioctl Unix API for PHP using FFI.
4+
5+
## Installation
6+
php-ioctl require PHP8.0 and php-ffi enabled. To install this package, use composer to require package `ppelisset/ioctl`.
7+
8+
## Documentation
9+
`Ioctl\Ioctl::ioctl` - control device

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "ppelisset/ioctl",
3+
"description": "Port of Unix ioctl API with PHP FFI",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Pierre Pélisset",
8+
"email": "ppelisset@hotmail.fr"
9+
}
10+
],
11+
"require": {
12+
"php": ">=8.0",
13+
"ext-ffi": "*"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Ioctl\\": "src/"
18+
}
19+
}
20+
}

src/Ioctl.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php /** @noinspection PhpMultipleClassDeclarationsInspection */
2+
3+
namespace Ioctl;
4+
5+
use FFI\CData;
6+
use Ioctl\PlatformSpecific\PlatformSpecific;
7+
8+
final class Ioctl
9+
{
10+
public const TIOCMBIS = PlatformSpecific::TIOCMBIS;
11+
public const TIOCMBIC = PlatformSpecific::TIOCMBIC;
12+
public const TIOCM_DTR = PlatformSpecific::TIOCM_DTR;
13+
public const TIOCM_RTS = PlatformSpecific::TIOCM_RTS;
14+
15+
public static function has($attribute): bool
16+
{
17+
return $attribute !== 0;
18+
}
19+
20+
public static function attr(mixed $attribute, mixed $defaultValue): mixed
21+
{
22+
return self::has($attribute) ? $attribute : $defaultValue;
23+
}
24+
25+
public static function ioctl(int $fd, int $request, mixed ...$values): Ioctl
26+
{
27+
return IoctlFunctions::ioctl($fd, $request, ...$values);
28+
}
29+
}

src/IoctlFunctions.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Ioctl;
4+
5+
use FFI;
6+
7+
final class IoctlFunctions
8+
{
9+
private static FFI $ffi;
10+
11+
public static function ioctl(int $fd, int $request, mixed ...$values)
12+
{
13+
return self::getFFI()->ioctl($fd, $request, ...$values);
14+
}
15+
16+
private static function getFFI(): FFI
17+
{
18+
if (is_null(self::$ffi)) {
19+
self::createFFI();
20+
}
21+
return self::$ffi;
22+
}
23+
24+
private static function createFFI(): void
25+
{
26+
self::$ffi = FFI::load(__DIR__ . "/header.h");
27+
}
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Ioctl\PlatformSpecific;
4+
5+
abstract class BasePlatformSpecific
6+
{
7+
public const TIOCMBIS = 0;
8+
public const TIOCMBIC = 0;
9+
public const TIOCM_DTR = 0;
10+
public const TIOCM_RTS = 0;
11+
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Ioctl\PlatformSpecific;
4+
5+
final class PlatformSpecific extends BasePlatformSpecific {
6+
public const TIOCMBIS = 0x8004746c;
7+
public const TIOCMBIC = 0x8004746b;
8+
public const TIOCM_DTR = 0x00000002;
9+
public const TIOCM_RTS = 0x00000004;
10+
}
11+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Ioctl\PlatformSpecific;
4+
5+
final class PlatformSpecific extends BasePlatformSpecific
6+
{
7+
public const TIOCMBIS = 0x00005416;
8+
public const TIOCMBIC = 0x00005417;
9+
public const TIOCM_DTR = 0x00000002;
10+
public const TIOCM_RTS = 0x00000004;
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Ioctl\PlatformSpecific;
4+
5+
final class PlatformSpecific extends BasePlatformSpecific
6+
{
7+
8+
}

0 commit comments

Comments
 (0)