AWS IoT Embedded C Device SDK
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"
51 #include "aws_iot_mqtt_client_interface.h"
52 
53 /* Enum order should match the packet ids array defined in MQTTFormat.c */
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)
77 
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 
94 uint32_t aws_iot_mqtt_internal_get_final_packet_length_from_remaining_length(uint32_t rem_len);
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_send_packet(AWS_IoT_Client *pClient, size_t length, Timer *pTimer);
108 IoT_Error_t aws_iot_mqtt_internal_cycle_read(AWS_IoT_Client *pClient, Timer *pTimer, uint8_t *pPacketType);
109 IoT_Error_t aws_iot_mqtt_internal_wait_for_read(AWS_IoT_Client *pClient, uint8_t packetType, Timer *pTimer);
110 IoT_Error_t aws_iot_mqtt_internal_serialize_zero(unsigned char *pTxBuf, size_t txBufLen,
111  MessageTypes packetType, size_t *pSerializedLength);
113  uint8_t *retained, uint16_t *pPacketId,
114  char **pTopicName, uint16_t *topicNameLen,
115  unsigned char **payload, size_t *payloadLen,
116  unsigned char *pRxBuf, size_t rxBufLen);
117 
118 IoT_Error_t aws_iot_mqtt_set_client_state(AWS_IoT_Client *pClient, ClientState expectedCurrentState,
119  ClientState newState);
120 
121 #ifdef _ENABLE_THREAD_SUPPORT_
122 
123 IoT_Error_t aws_iot_mqtt_client_lock_mutex(AWS_IoT_Client *pClient, IoT_Mutex_t *pMutex);
124 
125 IoT_Error_t aws_iot_mqtt_client_unlock_mutex(AWS_IoT_Client *pClient, IoT_Mutex_t *pMutex);
126 
127 #endif
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 #endif /* AWS_IOT_SDK_SRC_IOT_COMMON_INTERNAL_H */
unsigned char byte
Definition: aws_iot_mqtt_client_common_internal.h:82
MQTT Client.
Definition: aws_iot_mqtt_client.h:297
uint16_t aws_iot_mqtt_internal_read_uint16_t(unsigned char **pptr)
Definition: aws_iot_mqtt_client_common_internal.c:127
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)
Definition: aws_iot_mqtt_client_common_internal.c:55
IoT_Error_t
IoT Error enum.
Definition: aws_iot_error.h:40
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:312
Definition: timer_platform.h:33
void aws_iot_mqtt_internal_write_uint_16(unsigned char **pptr, uint16_t anInt)
Definition: aws_iot_mqtt_client_common_internal.c:143
enum _ClientState ClientState
MQTT Client State Type.
void aws_iot_mqtt_internal_write_char(unsigned char **pptr, unsigned char c)
Definition: aws_iot_mqtt_client_common_internal.c:166
IoT_Error_t aws_iot_mqtt_internal_decode_remaining_length_from_buffer(unsigned char *buf, uint32_t *decodedLen, uint32_t *readBytesLen)
Definition: aws_iot_mqtt_client_common_internal.c:79
Logging macros for the SDK. This file defines common logging macros with log levels to be used within...
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:383
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:652
Definition: aws_iot_mqtt_client_common_internal.h:81
IoT_Error_t aws_iot_mqtt_internal_init_header(MQTTHeader *pHeader, MessageTypes message_type, QoS qos, uint8_t dup, uint8_t retained)
Definition: aws_iot_mqtt_client_common_internal.c:186
QoS
Quality of Service Type.
Definition: aws_iot_mqtt_client.h:75
unsigned char aws_iot_mqtt_internal_read_char(unsigned char **pptr)
Definition: aws_iot_mqtt_client_common_internal.c:155