|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace DaveRandom\LibLifxLan; |
|
4
|
|
|
|
|
5
|
|
|
use DaveRandom\LibLifxLan\Header\Frame; |
|
6
|
|
|
use DaveRandom\LibLifxLan\Header\FrameAddress; |
|
7
|
|
|
use DaveRandom\LibLifxLan\Header\Header; |
|
8
|
|
|
use DaveRandom\LibLifxLan\Header\ProtocolHeader; |
|
9
|
|
|
use DaveRandom\LibLifxLan\Messages\Message; |
|
10
|
|
|
use DaveRandom\Network\MacAddress; |
|
11
|
|
|
|
|
12
|
|
|
final class Packet |
|
13
|
|
|
{ |
|
14
|
|
|
public const MESSAGE_ORIGIN = 0; |
|
15
|
|
|
public const PROTOCOL_NUMBER = 1024; |
|
16
|
|
|
|
|
17
|
|
|
private $header; |
|
18
|
|
|
private $message; |
|
19
|
|
|
|
|
20
|
10 |
|
private static function createFrameFromMessage(Message $message, int $sourceId, ?MacAddress $destination): Frame |
|
21
|
|
|
{ |
|
22
|
10 |
|
return new Frame( |
|
23
|
10 |
|
Header::WIRE_SIZE + $message->getWireSize(), |
|
24
|
10 |
|
self::MESSAGE_ORIGIN, |
|
25
|
10 |
|
/* tagged */ $destination === null, |
|
26
|
10 |
|
/* addressable */ true, |
|
27
|
10 |
|
self::PROTOCOL_NUMBER, |
|
28
|
10 |
|
$sourceId |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
10 |
|
private static function createFrameAddress(?MacAddress $destination, int $responsePattern, int $sequenceNo): FrameAddress |
|
33
|
|
|
{ |
|
34
|
10 |
|
return new FrameAddress( |
|
35
|
10 |
|
$destination ?? new MacAddress(0, 0, 0, 0, 0, 0), |
|
36
|
10 |
|
(bool)($responsePattern & ResponsePattern::REQUIRE_ACK), |
|
37
|
10 |
|
(bool)($responsePattern & ResponsePattern::REQUIRE_RESPONSE), |
|
38
|
10 |
|
$sequenceNo |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
10 |
|
public static function createFromMessage( |
|
43
|
|
|
Message $message, |
|
44
|
|
|
int $sourceId, |
|
45
|
|
|
?MacAddress $destination, |
|
46
|
|
|
int $sequenceNo, |
|
47
|
|
|
int $responsePattern |
|
48
|
|
|
): Packet |
|
49
|
|
|
{ |
|
50
|
10 |
|
$header = new Header( |
|
51
|
10 |
|
self::createFrameFromMessage($message, $sourceId, $destination), |
|
52
|
10 |
|
self::createFrameAddress($destination, $responsePattern, $sequenceNo), |
|
53
|
10 |
|
new ProtocolHeader($message->getTypeId()) |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
10 |
|
return new Packet($header, $message); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
17 |
|
public function __construct(Header $header, Message $message) |
|
60
|
|
|
{ |
|
61
|
17 |
|
$this->header = $header; |
|
62
|
17 |
|
$this->message = $message; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
16 |
|
public function getHeader(): Header |
|
66
|
|
|
{ |
|
67
|
16 |
|
return $this->header; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
6 |
|
public function getMessage(): Message |
|
71
|
|
|
{ |
|
72
|
6 |
|
return $this->message; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|