AWS IoT Device SDK C: MQTT
MQTT 3.1.1 client library
Return to main page ↑
aws_iot_mqtt_client_common_internal.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15 
16 // Based on Eclipse Paho.
17 /*******************************************************************************
18  * Copyright (c) 2014 IBM Corp.
19  *
20  * All rights reserved. This program and the accompanying materials
21  * are made available under the terms of the Eclipse Public License v1.0
22  * and Eclipse Distribution License v1.0 which accompany this distribution.
23  *
24  * The Eclipse Public License is available at
25  * http://www.eclipse.org/legal/epl-v10.html
26  * and the Eclipse Distribution License is available at
27  * http://www.eclipse.org/org/documents/edl-v10.php.
28  *
29  * Contributors:
30  * Ian Craggs - initial API and implementation and/or initial documentation
31  * Xiang Rong - 442039 Add makefile to Embedded C client
32  *******************************************************************************/
33 
39 #ifndef AWS_IOT_SDK_SRC_IOT_COMMON_INTERNAL_H
40 #define AWS_IOT_SDK_SRC_IOT_COMMON_INTERNAL_H
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #include <stdint.h>
47 #include <stddef.h>
48 #include <string.h>
49 
50 #include "aws_iot_log.h"
52 
54 typedef enum msgTypes {
55  UNKNOWN = -1,
56  CONNECT = 1,
57  CONNACK = 2,
58  PUBLISH = 3,
59  PUBACK = 4,
60  PUBREC = 5,
61  PUBREL = 6,
62  PUBCOMP = 7,
63  SUBSCRIBE = 8,
64  SUBACK = 9,
65  UNSUBSCRIBE = 10,
66  UNSUBACK = 11,
67  PINGREQ = 12,
68  PINGRESP = 13,
69  DISCONNECT = 14
70 } MessageTypes;
71 
72 /* Macros for parsing header fields from incoming MQTT frame. */
73 #define MQTT_HEADER_FIELD_TYPE(_byte) ((_byte >> 4) & 0x0F)
74 #define MQTT_HEADER_FIELD_DUP(_byte) ((_byte & (1 << 3)) >> 3)
75 #define MQTT_HEADER_FIELD_QOS(_byte) ((_byte & (3 << 1)) >> 1)
76 #define MQTT_HEADER_FIELD_RETAIN(_byte) ((_byte & (1 << 0)) >> 0)
81 typedef union {
82  unsigned char byte;
83 } MQTTHeader;
84 
85 IoT_Error_t aws_iot_mqtt_internal_init_header(MQTTHeader *pHeader, MessageTypes message_type,
86  QoS qos, uint8_t dup, uint8_t retained);
87 
88 IoT_Error_t aws_iot_mqtt_internal_serialize_ack(unsigned char *pTxBuf, size_t txBufLen,
89  MessageTypes msgType, uint8_t dup, uint16_t packetId,
90  uint32_t *pSerializedLen);
91 IoT_Error_t aws_iot_mqtt_internal_deserialize_ack(unsigned char *, unsigned char *,
92  uint16_t *, unsigned char *, size_t);
93 
95 
96 size_t aws_iot_mqtt_internal_write_len_to_buffer(unsigned char *buf, uint32_t length);
97 IoT_Error_t aws_iot_mqtt_internal_decode_remaining_length_from_buffer(unsigned char *buf, uint32_t *decodedLen,
98  uint32_t *readBytesLen);
99 
100 uint16_t aws_iot_mqtt_internal_read_uint16_t(unsigned char **pptr);
101 void aws_iot_mqtt_internal_write_uint_16(unsigned char **pptr, uint16_t anInt);
102 
103 unsigned char aws_iot_mqtt_internal_read_char(unsigned char **pptr);
104 void aws_iot_mqtt_internal_write_char(unsigned char **pptr, unsigned char c);
105 void aws_iot_mqtt_internal_write_utf8_string(unsigned char **pptr, const char *string, uint16_t stringLen);
106 
107 IoT_Error_t aws_iot_mqtt_internal_flushBuffers( AWS_IoT_Client *pClient );
108 IoT_Error_t aws_iot_mqtt_internal_send_packet(AWS_IoT_Client *pClient, size_t length, Timer *pTimer);
109 IoT_Error_t aws_iot_mqtt_internal_cycle_read(AWS_IoT_Client *pClient, Timer *pTimer, uint8_t *pPacketType);
110 IoT_Error_t aws_iot_mqtt_internal_wait_for_read(AWS_IoT_Client *pClient, uint8_t packetType, Timer *pTimer);
111 IoT_Error_t aws_iot_mqtt_internal_serialize_zero(unsigned char *pTxBuf, size_t txBufLen,
112  MessageTypes packetType, size_t *pSerializedLength);
113 IoT_Error_t aws_iot_mqtt_internal_deserialize_publish(uint8_t *dup, QoS *qos,
114  uint8_t *retained, uint16_t *pPacketId,
115  char **pTopicName, uint16_t *topicNameLen,
116  unsigned char **payload, size_t *payloadLen,
117  unsigned char *pRxBuf, size_t rxBufLen);
118 
119 IoT_Error_t aws_iot_mqtt_set_client_state(AWS_IoT_Client *pClient, ClientState expectedCurrentState,
120  ClientState newState);
121 
122 #ifdef _ENABLE_THREAD_SUPPORT_
123 
124 IoT_Error_t aws_iot_mqtt_client_lock_mutex(AWS_IoT_Client *pClient, IoT_Mutex_t *pMutex);
125 
126 IoT_Error_t aws_iot_mqtt_client_unlock_mutex(AWS_IoT_Client *pClient, IoT_Mutex_t *pMutex);
127 
128 #endif
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 #endif /* AWS_IOT_SDK_SRC_IOT_COMMON_INTERNAL_H */
unsigned char byte
Definition: aws_iot_mqtt_client_common_internal.h:82
uint16_t aws_iot_mqtt_internal_read_uint16_t(unsigned char **pptr)
Calculates uint16 packet id from two bytes read from the input buffer.
Definition: aws_iot_mqtt_client_common_internal.c:140
IoT_Error_t aws_iot_mqtt_set_client_state(AWS_IoT_Client *pClient, ClientState expectedCurrentState, ClientState newState)
Change the state in an MQTT client.
Definition: aws_iot_mqtt_client.c:132
MessageTypes
Definition: aws_iot_mqtt_client_common_internal.h:54
IoT_Error_t aws_iot_mqtt_internal_serialize_ack(unsigned char *pTxBuf, size_t txBufLen, MessageTypes msgType, uint8_t dup, uint16_t packetId, uint32_t *pSerializedLen)
Definition: aws_iot_mqtt_client_publish.c:146
size_t aws_iot_mqtt_internal_write_len_to_buffer(unsigned char *buf, uint32_t length)
Encodes the message length according to the MQTT algorithm.
Definition: aws_iot_mqtt_client_common_internal.c:57
IoT_Error_t aws_iot_mqtt_internal_wait_for_read(AWS_IoT_Client *pClient, uint8_t packetType, Timer *pTimer)
Wait until a packet is read from the network.
Definition: aws_iot_mqtt_client_common_internal.c:725
Interface definition for MQTT client.
uint32_t aws_iot_mqtt_internal_get_final_packet_length_from_remaining_length(uint32_t rem_len)
Calculates the length of the "remaining length" encoding.
Definition: aws_iot_mqtt_client_common_internal.c:117
IoT_Error_t aws_iot_mqtt_internal_deserialize_publish(uint8_t *dup, QoS *qos, uint8_t *retained, uint16_t *pPacketId, char **pTopicName, uint16_t *topicNameLen, unsigned char **payload, size_t *payloadLen, unsigned char *pRxBuf, size_t rxBufLen)
Definition: aws_iot_mqtt_client_publish.c:295
ClientState
MQTT Client State Type.
Definition: aws_iot_mqtt_client.h:203
void aws_iot_mqtt_internal_write_uint_16(unsigned char **pptr, uint16_t anInt)
Writes an integer as 2 bytes to an output buffer.
Definition: aws_iot_mqtt_client_common_internal.c:157
void aws_iot_mqtt_internal_write_char(unsigned char **pptr, unsigned char c)
Writes one character to an output buffer.
Definition: aws_iot_mqtt_client_common_internal.c:182
IoT_Error_t aws_iot_mqtt_internal_decode_remaining_length_from_buffer(unsigned char *buf, uint32_t *decodedLen, uint32_t *readBytesLen)
Decodes the message length according to the MQTT algorithm.
Definition: aws_iot_mqtt_client_common_internal.c:84
void aws_iot_mqtt_internal_write_utf8_string(unsigned char **pptr, const char *string, uint16_t stringLen)
Writes a UTF-8 string into an MQTT packet.
Definition: aws_iot_mqtt_client_common_internal.c:194
IoT_Error_t aws_iot_mqtt_internal_flushBuffers(AWS_IoT_Client *pClient)
Flush incoming data from the MQTT client.
Definition: aws_iot_mqtt_client_common_internal.c:709
IoT_Error_t aws_iot_mqtt_internal_deserialize_ack(unsigned char *, unsigned char *, uint16_t *, unsigned char *, size_t)
Definition: aws_iot_mqtt_client_publish.c:366
IoT_Error_t aws_iot_mqtt_internal_send_packet(AWS_IoT_Client *pClient, size_t length, Timer *pTimer)
Send an MQTT packet on the network.
Definition: aws_iot_mqtt_client_common_internal.c:308
IoT_Error_t aws_iot_mqtt_internal_serialize_zero(unsigned char *pTxBuf, size_t txBufLen, MessageTypes packetType, size_t *pSerializedLength)
Definition: aws_iot_mqtt_client_common_internal.c:757
IoT_Error_t aws_iot_mqtt_internal_cycle_read(AWS_IoT_Client *pClient, Timer *pTimer, uint8_t *pPacketType)
Read an MQTT packet from the network.
Definition: aws_iot_mqtt_client_common_internal.c:636
IoT_Error_t aws_iot_mqtt_client_lock_mutex(AWS_IoT_Client *pClient, IoT_Mutex_t *pMutex)
Lock a mutex in the MQTT client.
Definition: aws_iot_mqtt_client.c:84
Definition: aws_iot_mqtt_client_common_internal.h:81
IoT_Error_t aws_iot_mqtt_client_unlock_mutex(AWS_IoT_Client *pClient, IoT_Mutex_t *pMutex)
Unlock a mutex in the MQTT client.
Definition: aws_iot_mqtt_client.c:114
IoT_Error_t aws_iot_mqtt_internal_init_header(MQTTHeader *pHeader, MessageTypes message_type, QoS qos, uint8_t dup, uint8_t retained)
Initialize the MQTTHeader structure.
Definition: aws_iot_mqtt_client_common_internal.c:218
QoS
Quality of Service Type.
Definition: aws_iot_mqtt_client.h:76
unsigned char aws_iot_mqtt_internal_read_char(unsigned char **pptr)
Reads one character from the input buffer.
Definition: aws_iot_mqtt_client_common_internal.c:170