MqttMsgConnack.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Copyright (c) 2013, 2014 Paolo Patierno
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License v1.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. http://www.eclipse.org/legal/epl-v10.html
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. Contributors:
  11. Paolo Patierno - initial API and implementation and/or initial documentation
  12. */
  13. using System;
  14. using uPLibrary.Networking.M2Mqtt.Exceptions;
  15. namespace uPLibrary.Networking.M2Mqtt.Messages
  16. {
  17. /// <summary>
  18. /// Class for CONNACK message from broker to client
  19. /// </summary>
  20. public class MqttMsgConnack : MqttMsgBase
  21. {
  22. #region Constants...
  23. // return codes for CONNACK message
  24. public const byte CONN_ACCEPTED = 0x00;
  25. public const byte CONN_REFUSED_PROT_VERS = 0x01;
  26. public const byte CONN_REFUSED_IDENT_REJECTED = 0x02;
  27. public const byte CONN_REFUSED_SERVER_UNAVAILABLE = 0x03;
  28. public const byte CONN_REFUSED_USERNAME_PASSWORD = 0x04;
  29. public const byte CONN_REFUSED_NOT_AUTHORIZED = 0x05;
  30. private const byte TOPIC_NAME_COMP_RESP_BYTE_OFFSET = 0;
  31. private const byte TOPIC_NAME_COMP_RESP_BYTE_SIZE = 1;
  32. // [v3.1.1] connect acknowledge flags replace "old" topic name compression respone (not used in 3.1)
  33. private const byte CONN_ACK_FLAGS_BYTE_OFFSET = 0;
  34. private const byte CONN_ACK_FLAGS_BYTE_SIZE = 1;
  35. // [v3.1.1] session present flag
  36. private const byte SESSION_PRESENT_FLAG_MASK = 0x01;
  37. private const byte SESSION_PRESENT_FLAG_OFFSET = 0x00;
  38. private const byte SESSION_PRESENT_FLAG_SIZE = 0x01;
  39. private const byte CONN_RETURN_CODE_BYTE_OFFSET = 1;
  40. private const byte CONN_RETURN_CODE_BYTE_SIZE = 1;
  41. #endregion
  42. #region Properties...
  43. // [v3.1.1] session present flag
  44. /// <summary>
  45. /// Session present flag
  46. /// </summary>
  47. public bool SessionPresent
  48. {
  49. get { return this.sessionPresent; }
  50. set { this.sessionPresent = value; }
  51. }
  52. /// <summary>
  53. /// Return Code
  54. /// </summary>
  55. public byte ReturnCode
  56. {
  57. get { return this.returnCode; }
  58. set { this.returnCode = value; }
  59. }
  60. #endregion
  61. // [v3.1.1] session present flag
  62. private bool sessionPresent;
  63. // return code for CONNACK message
  64. private byte returnCode;
  65. /// <summary>
  66. /// Constructor
  67. /// </summary>
  68. public MqttMsgConnack()
  69. {
  70. this.type = MQTT_MSG_CONNACK_TYPE;
  71. }
  72. /// <summary>
  73. /// Parse bytes for a CONNACK message
  74. /// </summary>
  75. /// <param name="fixedHeaderFirstByte">First fixed header byte</param>
  76. /// <param name="protocolVersion">Protocol Version</param>
  77. /// <param name="channel">Channel connected to the broker</param>
  78. /// <returns>CONNACK message instance</returns>
  79. public static MqttMsgConnack Parse(byte fixedHeaderFirstByte, byte protocolVersion, IMqttNetworkChannel channel)
  80. {
  81. byte[] buffer;
  82. MqttMsgConnack msg = new MqttMsgConnack();
  83. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  84. {
  85. // [v3.1.1] check flag bits
  86. if ((fixedHeaderFirstByte & MSG_FLAG_BITS_MASK) != MQTT_MSG_CONNACK_FLAG_BITS)
  87. throw new MqttClientException(MqttClientErrorCode.InvalidFlagBits);
  88. }
  89. // get remaining length and allocate buffer
  90. int remainingLength = MqttMsgBase.decodeRemainingLength(channel);
  91. buffer = new byte[remainingLength];
  92. // read bytes from socket...
  93. channel.Receive(buffer);
  94. if (protocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  95. {
  96. // [v3.1.1] ... set session present flag ...
  97. msg.sessionPresent = (buffer[CONN_ACK_FLAGS_BYTE_OFFSET] & SESSION_PRESENT_FLAG_MASK) != 0x00;
  98. }
  99. // ...and set return code from broker
  100. msg.returnCode = buffer[CONN_RETURN_CODE_BYTE_OFFSET];
  101. return msg;
  102. }
  103. public override byte[] GetBytes(byte ProtocolVersion)
  104. {
  105. int fixedHeaderSize = 0;
  106. int varHeaderSize = 0;
  107. int payloadSize = 0;
  108. int remainingLength = 0;
  109. byte[] buffer;
  110. int index = 0;
  111. if (ProtocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  112. // flags byte and connect return code
  113. varHeaderSize += (CONN_ACK_FLAGS_BYTE_SIZE + CONN_RETURN_CODE_BYTE_SIZE);
  114. else
  115. // topic name compression response and connect return code
  116. varHeaderSize += (TOPIC_NAME_COMP_RESP_BYTE_SIZE + CONN_RETURN_CODE_BYTE_SIZE);
  117. remainingLength += (varHeaderSize + payloadSize);
  118. // first byte of fixed header
  119. fixedHeaderSize = 1;
  120. int temp = remainingLength;
  121. // increase fixed header size based on remaining length
  122. // (each remaining length byte can encode until 128)
  123. do
  124. {
  125. fixedHeaderSize++;
  126. temp = temp / 128;
  127. } while (temp > 0);
  128. // allocate buffer for message
  129. buffer = new byte[fixedHeaderSize + varHeaderSize + payloadSize];
  130. // first fixed header byte
  131. if (ProtocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  132. buffer[index++] = (MQTT_MSG_CONNACK_TYPE << MSG_TYPE_OFFSET) | MQTT_MSG_CONNACK_FLAG_BITS; // [v.3.1.1]
  133. else
  134. buffer[index++] = (byte)(MQTT_MSG_CONNACK_TYPE << MSG_TYPE_OFFSET);
  135. // encode remaining length
  136. index = this.encodeRemainingLength(remainingLength, buffer, index);
  137. if (ProtocolVersion == MqttMsgConnect.PROTOCOL_VERSION_V3_1_1)
  138. // [v3.1.1] session present flag
  139. buffer[index++] = this.sessionPresent ? (byte)(1 << SESSION_PRESENT_FLAG_OFFSET) : (byte)0x00;
  140. else
  141. // topic name compression response (reserved values. not used);
  142. buffer[index++] = 0x00;
  143. // connect return code
  144. buffer[index++] = this.returnCode;
  145. return buffer;
  146. }
  147. public override string ToString()
  148. {
  149. #if TRACE
  150. return this.GetTraceString(
  151. "CONNACK",
  152. new object[] { "returnCode" },
  153. new object[] { this.returnCode });
  154. #else
  155. return base.ToString();
  156. #endif
  157. }
  158. }
  159. }