1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\MQTT\Protocol; |
6
|
|
|
|
7
|
|
|
use unreal4u\MQTT\Exceptions\Connect\BadUsernameOrPassword; |
8
|
|
|
use unreal4u\MQTT\Exceptions\Connect\GenericError; |
9
|
|
|
use unreal4u\MQTT\Exceptions\Connect\IdentifierRejected; |
10
|
|
|
use unreal4u\MQTT\Exceptions\Connect\NotAuthorized; |
11
|
|
|
use unreal4u\MQTT\Exceptions\Connect\ServerUnavailable; |
12
|
|
|
use unreal4u\MQTT\Exceptions\Connect\UnacceptableProtocolVersion; |
13
|
|
|
use unreal4u\MQTT\Internals\ClientInterface; |
14
|
|
|
use unreal4u\MQTT\Internals\ProtocolBase; |
15
|
|
|
use unreal4u\MQTT\Internals\ReadableContent; |
16
|
|
|
use unreal4u\MQTT\Internals\ReadableContentInterface; |
17
|
|
|
use unreal4u\MQTT\Internals\WritableContentInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The CONNACK Packet is the packet sent by the Server in response to a CONNECT Packet received from a Client. |
21
|
|
|
*/ |
22
|
|
|
final class ConnAck extends ProtocolBase implements ReadableContentInterface |
23
|
|
|
{ |
24
|
|
|
use ReadableContent; |
25
|
|
|
|
26
|
|
|
const CONTROL_PACKET_VALUE = 2; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The connect return code. If a server sends a CONNACK packet containing a non-zero return code it MUST then close |
30
|
|
|
* the Network Connection |
31
|
|
|
* |
32
|
|
|
* @see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_3.1_- |
33
|
|
|
* |
34
|
|
|
* 0 = Connection accepted |
35
|
|
|
* 1 = Connection Refused, unacceptable protocol version |
36
|
|
|
* 2 = Connection Refused, identifier rejected |
37
|
|
|
* 3 = Connection Refused, Server unavailable |
38
|
|
|
* 4 = Connection Refused, bad user name or password |
39
|
|
|
* 5 = Connection Refused, not authorized |
40
|
|
|
* 6-255 = Reserved for future use |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
public $connectReturnCode; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
* |
48
|
|
|
* @param string $rawMQTTHeaders |
49
|
|
|
* @param ClientInterface $client |
50
|
|
|
* @return ReadableContentInterface |
51
|
|
|
* @throws \unreal4u\MQTT\Exceptions\Connect\GenericError |
52
|
|
|
* @throws \unreal4u\MQTT\Exceptions\Connect\NotAuthorized |
53
|
|
|
* @throws \unreal4u\MQTT\Exceptions\Connect\BadUsernameOrPassword |
54
|
|
|
* @throws \unreal4u\MQTT\Exceptions\Connect\ServerUnavailable |
55
|
|
|
* @throws \unreal4u\MQTT\Exceptions\Connect\IdentifierRejected |
56
|
|
|
* @throws \unreal4u\MQTT\Exceptions\Connect\UnacceptableProtocolVersion |
57
|
|
|
*/ |
58
|
|
|
public function fillObject(string $rawMQTTHeaders, ClientInterface $client): ReadableContentInterface |
59
|
|
|
{ |
60
|
|
|
$this->connectReturnCode = \ord($rawMQTTHeaders{3}); |
61
|
|
|
switch ($this->connectReturnCode) { |
62
|
|
|
case 0: |
63
|
|
|
// Everything correct, do nothing |
64
|
|
|
break; |
65
|
|
|
case 1: |
66
|
|
|
throw new UnacceptableProtocolVersion( |
67
|
|
|
'The Server does not support the level of the MQTT protocol requested by the Client' |
68
|
|
|
); |
69
|
|
|
break; |
70
|
|
|
case 2: |
71
|
|
|
throw new IdentifierRejected('The Client identifier is correct UTF-8 but not allowed by the Server'); |
72
|
|
|
break; |
73
|
|
|
case 3: |
74
|
|
|
throw new ServerUnavailable('The Network Connection has been made but the MQTT service is unavailable'); |
75
|
|
|
break; |
76
|
|
|
case 4: |
77
|
|
|
throw new BadUsernameOrPassword('The data in the user name or password is malformed'); |
78
|
|
|
break; |
79
|
|
|
case 5: |
80
|
|
|
throw new NotAuthorized('The Client is not authorized to connect'); |
81
|
|
|
break; |
82
|
|
|
default: |
83
|
|
|
throw new GenericError(sprintf( |
84
|
|
|
'Reserved for future use or error not implemented yet (Error code: %d)', |
85
|
|
|
$this->connectReturnCode |
86
|
|
|
)); |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
|
|
public function performSpecialActions(ClientInterface $client, WritableContentInterface $originalRequest): bool |
97
|
|
|
{ |
98
|
|
|
$client |
99
|
|
|
->setConnected(true) |
100
|
|
|
->updateLastCommunication(); |
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
|
|
public function getOriginControlPacket(): int |
109
|
|
|
{ |
110
|
|
|
return Connect::getControlPacketValue(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|