|
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 |
|
public static function createFromMessage( |
|
21
|
|
|
Message $message, |
|
22
|
|
|
int $sourceId, |
|
23
|
|
|
?MacAddress $destination, |
|
24
|
|
|
int $sequenceNo, |
|
25
|
|
|
int $responsePattern |
|
26
|
|
|
): Packet |
|
27
|
|
|
{ |
|
28
|
10 |
|
$frame = new Frame( |
|
29
|
10 |
|
Header::WIRE_SIZE + $message->getWireSize(), |
|
30
|
10 |
|
self::MESSAGE_ORIGIN, |
|
31
|
10 |
|
/* tagged */ $destination === null, |
|
32
|
10 |
|
/* addressable */ true, |
|
33
|
10 |
|
self::PROTOCOL_NUMBER, |
|
34
|
10 |
|
$sourceId |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
10 |
|
$destination = $destination ?? new MacAddress(0, 0, 0, 0, 0, 0); |
|
38
|
10 |
|
$isAckRequired = (bool)($responsePattern & ResponsePattern::REQUIRE_ACK); |
|
39
|
10 |
|
$isResponseRequired = (bool)($responsePattern & ResponsePattern::REQUIRE_RESPONSE); |
|
40
|
|
|
|
|
41
|
10 |
|
$frameAddress = new FrameAddress($destination, $isAckRequired, $isResponseRequired, $sequenceNo); |
|
42
|
10 |
|
$protocolHeader = new ProtocolHeader($message->getTypeId()); |
|
43
|
|
|
|
|
44
|
10 |
|
return new Packet(new Header($frame, $frameAddress, $protocolHeader), $message); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
12 |
|
public function __construct(Header $header, Message $message) |
|
48
|
|
|
{ |
|
49
|
12 |
|
$this->header = $header; |
|
50
|
12 |
|
$this->message = $message; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
11 |
|
public function getHeader(): Header |
|
54
|
|
|
{ |
|
55
|
11 |
|
return $this->header; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function getMessage(): Message |
|
59
|
|
|
{ |
|
60
|
1 |
|
return $this->message; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|