Skip to content

Commit

Permalink
network init done
Browse files Browse the repository at this point in the history
  • Loading branch information
jiejieTop committed Dec 15, 2019
1 parent fd9ac84 commit e64b82c
Show file tree
Hide file tree
Showing 41 changed files with 3,025 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
34 changes: 34 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 2.8)

project(mqtt-client)

set(TARGETS "mqtt-client")
set(SUBDIRS "mqtt" "test" "platform" "network" "common" "mqttclient")
set(INCDIRS "mqtt" "platform/inc" "common" "network")
set(OUTDIRS "build")
set(LIBNAMES "mqtt" "platform" "network" "common" "mqttclient")
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "clang++" )
set(CMAKE_BUILE_TYPE "RELEASE")
set(PROJECT_ROOT_PATH "${PROJECT_SOURCE_DIR}")
set(LIBRARY_OUTPUT_PATH "${PROJECT_ROOT_PATH}/${OUTDIRS}/lib/")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_ROOT_PATH}/${OUTDIRS}/bin/")

if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_CXX_FLAGS "-g")
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "-O1 -DNDEBUG ")
endif(CMAKE_COMPILER_IS_GNUCXX)

foreach(incdir ${INCDIRS})
include_directories(${incdir})
endforeach()

foreach(subdir ${SUBDIRS})
add_subdirectory(${PROJECT_ROOT_PATH}/${subdir})
endforeach()

link_directories(${LIBRARY_OUTPUT_PATH})

6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

mkdir -p build build/bin build/lib
cd build
cmake ..
make
17 changes: 17 additions & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
aux_source_directory(. DIR_SRCS)

string(REGEX REPLACE ".*/(.*)" "\\1" LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})

if (DIR_SRCS)
foreach(libname ${LIBNAMES})
if (${LIB_NAME} STREQUAL ${libname})
add_library(${libname} STATIC ${DIR_SRCS})
endif()
endforeach()

else()
message(WARNING "not find is src file!")
endif()



19 changes: 19 additions & 0 deletions common/error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* @Author: jiejie
* @Github: https://github.com/jiejieTop
* @Date: 2019-12-15 00:42:16
* @LastEditTime: 2019-12-15 15:43:17
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
*/
#ifndef _ERROR_H_
#define _ERROR_H_

typedef enum error {
NULL_VALUE_ERROR = -2,
FAIL_ERROR = -1,
SUCCESS_ERROR = 0
} error_t;

#define RETURN_ERROR(x) { return x; }

#endif
72 changes: 72 additions & 0 deletions common/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* @Author: jiejie
* @Github: https://github.com/jiejieTop
* @Date: 2019-12-11 22:46:33
* @LastEditTime: 2019-12-12 22:56:27
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
*/

# include "list.h"

static void _list_add(list_t *node, list_t *prev, list_t *next)
{
next->prev = node;
node->next = next;
node->prev = prev;
prev->next = node;
}

static void _list_del(list_t *prev, list_t *next)
{
next->prev = prev;
prev->next = next;
}

static void _list_del_entry(list_t *entry)
{
_list_del(entry->prev, entry->next);
}

void list_init(list_t *list)
{
list->next = list;
list->prev = list;
}

void list_add(list_t *node, list_t *list)
{
_list_add(node, list, list->next);
}

void list_add_tail(list_t *node, list_t *list)
{
_list_add(node, list->prev, list);
}

void list_del(list_t *entry)
{
_list_del(entry->prev, entry->next);
}

void list_del_init(list_t *entry)
{
_list_del_entry(entry);
list_init(entry);
}

void list_move(list_t *node, list_t *list)
{
_list_del_entry(node);
list_add(node, list);
}

void list_move_tail(list_t *node, list_t *list)
{
_list_del_entry(node);
list_add_tail(node, list);
}

int list_empty(list_t *list)
{
return list->next == list;
}
62 changes: 62 additions & 0 deletions common/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* @Author: jiejie
* @Github: https://github.com/jiejieTop
* @Date: 2019-12-11 22:47:55
* @LastEditTime: 2019-12-12 23:00:11
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
*/
#ifndef _LIST_H_
#define _LIST_H_

typedef struct list_node {
struct list_node *next;
struct list_node *prev;
} list_t;

#define OFFSET_OF_FIELD(type, field) \
((uint32_t)&(((type *)0)->field))

#define CONTAINER_OF_FIELD(ptr, type, field) \
((type *)((uint8_t *)(ptr) - OFFSET_OF_FIELD(type, field)))

#define LIST_NODE(node) \
{ &(node), &(node) }

#define LIST_DEFINE(list) \
list_t list = { &(list), &(list) }

#define LIST_ENTRY(list, type, field) \
CONTAINER_OF_FIELD(list, type, field)

#define LIST_FIRST_ENTRY(list, type, field) \
LIST_ENTRY((list)->next, type, field)

#define LIST_FIRST_ENTRY_OR_NULL(list, type, field) \
(list_empty(list) ? NULL : LIST_FIRST_ENTRY(list, type, field))

#define LIST_FOR_EACH(curr, list) \
for (curr = (list)->next; curr != (list); curr = curr->next)

#define LIST_FOR_EACH_PREV(curr, list) \
for (curr = (list)->prev; curr != (list); curr = curr->prev)

#define LIST_FOR_EACH_SAFE(curr, next, list) \
for (curr = (list)->next, next = curr->next; curr != (list); \
curr = next, next = curr->next)

#define LIST_FOR_EACH_PREV_SAFE(curr, next, list) \
for (curr = (list)->prev, next = curr->prev; \
curr != (list); \
curr = next, next = curr->prev)

void list_init(list_t *list);
void list_add(list_t *node, list_t *list);
void list_add_tail(list_t *node, list_t *list);
void list_del(list_t *entry);
void list_del_init(list_t *entry);
void list_move(list_t *node, list_t *list);
void list_move_tail(list_t *node, list_t *list);
int list_empty(list_t *list);

#endif /* _LIST_H_ */

17 changes: 17 additions & 0 deletions mqtt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
aux_source_directory(. DIR_SRCS)

string(REGEX REPLACE ".*/(.*)" "\\1" LIB_NAME ${CMAKE_CURRENT_SOURCE_DIR})

if (DIR_SRCS)
foreach(libname ${LIBNAMES})
if (${LIB_NAME} STREQUAL ${libname})
add_library(${libname} STATIC ${DIR_SRCS})
endif()
endforeach()

else()
message(WARNING "not find is src file!")
endif()



148 changes: 148 additions & 0 deletions mqtt/MQTTConnect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*******************************************************************************
* Copyright (c) 2014, 2017 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
* Ian Craggs - add connack return code definitions
* Xiang Rong - 442039 Add makefile to Embedded C client
* Ian Craggs - fix for issue #64, bit order in connack response
*******************************************************************************/

#ifndef MQTTCONNECT_H_
#define MQTTCONNECT_H_

enum connack_return_codes
{
MQTT_CONNECTION_ACCEPTED = 0,
MQTT_UNNACCEPTABLE_PROTOCOL = 1,
MQTT_CLIENTID_REJECTED = 2,
MQTT_SERVER_UNAVAILABLE = 3,
MQTT_BAD_USERNAME_OR_PASSWORD = 4,
MQTT_NOT_AUTHORIZED = 5,
};

#if !defined(DLLImport)
#define DLLImport
#endif
#if !defined(DLLExport)
#define DLLExport
#endif


typedef union
{
unsigned char all; /**< all connect flags */
#if defined(REVERSED)
struct
{
unsigned int username : 1; /**< 3.1 user name */
unsigned int password : 1; /**< 3.1 password */
unsigned int willRetain : 1; /**< will retain setting */
unsigned int willQoS : 2; /**< will QoS value */
unsigned int will : 1; /**< will flag */
unsigned int cleansession : 1; /**< clean session flag */
unsigned int : 1; /**< unused */
} bits;
#else
struct
{
unsigned int : 1; /**< unused */
unsigned int cleansession : 1; /**< cleansession flag */
unsigned int will : 1; /**< will flag */
unsigned int willQoS : 2; /**< will QoS value */
unsigned int willRetain : 1; /**< will retain setting */
unsigned int password : 1; /**< 3.1 password */
unsigned int username : 1; /**< 3.1 user name */
} bits;
#endif
} MQTTConnectFlags; /**< connect flags byte */



/**
* Defines the MQTT "Last Will and Testament" (LWT) settings for
* the connect packet.
*/
typedef struct
{
/** The eyecatcher for this structure. must be MQTW. */
char struct_id[4];
/** The version number of this structure. Must be 0 */
int struct_version;
/** The LWT topic to which the LWT message will be published. */
MQTTString topicName;
/** The LWT payload. */
MQTTString message;
/**
* The retained flag for the LWT message (see MQTTAsync_message.retained).
*/
unsigned char retained;
/**
* The quality of service setting for the LWT message (see
* MQTTAsync_message.qos and @ref qos).
*/
char qos;
} MQTTPacket_willOptions;


#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }


typedef struct
{
/** The eyecatcher for this structure. must be MQTC. */
char struct_id[4];
/** The version number of this structure. Must be 0 */
int struct_version;
/** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
*/
unsigned char MQTTVersion;
MQTTString clientID;
unsigned short keepAliveInterval;
unsigned char cleansession;
unsigned char willFlag;
MQTTPacket_willOptions will;
MQTTString username;
MQTTString password;
} MQTTPacket_connectData;

typedef union
{
unsigned char all; /**< all connack flags */
#if defined(REVERSED)
struct
{
unsigned int reserved : 7; /**< unused */
unsigned int sessionpresent : 1; /**< session present flag */
} bits;
#else
struct
{
unsigned int sessionpresent : 1; /**< session present flag */
unsigned int reserved: 7; /**< unused */
} bits;
#endif
} MQTTConnackFlags; /**< connack flags byte */

#define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \
MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }

DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options);
DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len);

DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent);
DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen);

DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen);
DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen);

#endif /* MQTTCONNECT_H_ */
Loading

0 comments on commit e64b82c

Please sign in to comment.