Skip to content

Commit

Permalink
Init code
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
Jianhui Zhao committed Jan 26, 2018
1 parent d30030c commit 0a6edf0
Show file tree
Hide file tree
Showing 15 changed files with 1,355 additions and 2 deletions.
23 changes: 23 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
language: c

os:
- linux

before_install:
- sudo apt-get -qq update
- sudo apt-get install -y libjson-c-dev
- sudo apt-get remove libssl-dev

install:
- git clone https://git.openwrt.org/project/libubox.git
- pushd libubox && cmake -DBUILD_LUA=OFF . && make && sudo make install && popd
- wget https://www.openssl.org/source/openssl-1.0.2n.tar.gz
- tar zxvf openssl-1.0.2n.tar.gz
- pushd openssl-1.0.2n && ./config --prefix=/usr/local --openssldir=/usr/local/openssl shared && make && sudo make install && popd
- git clone https://git.openwrt.org/project/ustream-ssl.git
- pushd ustream-ssl && cmake . && make && sudo make install && popd

script:
- mkdir build && cd build
- cmake ..
- make && sudo make install
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 2.8)

project(libumqtt C)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/")

#set(CMAKE_VERBOSE_MAKEFILE ON)

add_subdirectory(src)
add_subdirectory(example)
40 changes: 40 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Contributing
================================================================================

If you want to contribute to [umqtt](https://github.com/zhaojh329/umqtt), please follow these simple rules:

1. Press the fork button:

![fork](http://oi58.tinypic.com/jj2trm.jpg)

2. Clone the repository from your account with:

```
git clone https://github.com/your_github_username/umqtt.git
```
3. Create a new branch with:
```
git checkout -b "umqtt-1-fix"
```
You can name it however you want.
4. Make your changes
5. Commit and push your changes, then make a pull request from Github.
git commit --signoff
git push origin umqtt-1-fix
6. Awaiting review, if accepted, merged!
**IMPORTANT**
Please, don't forget to update your fork. While you made your changes,
the content of the `master` branch can change because other pull requests
were merged and it can create conflicts. This is why you have to rebase
on `master` every time before pushing your changes and check that your
branch doesn't have any conflicts with `master`.
Thank you.
41 changes: 39 additions & 2 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# libumqttc
Lightweight MQTT client C library based on libubox for Embedded Linux.
# libumqtt

[1]: https://img.shields.io/badge/license-GPLV3-brightgreen.svg?style=plastic
[2]: /LICENSE
[3]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=plastic
[4]: https://github.com/zhaojh329/libumqtt/pulls
[5]: https://img.shields.io/badge/Issues-welcome-brightgreen.svg?style=plastic
[6]: https://github.com/zhaojh329/libumqtt/issues/new
[7]: https://img.shields.io/badge/release-0.0.1-blue.svg?style=plastic
[8]: https://github.com/zhaojh329/libumqtt/releases
[9]: https://travis-ci.org/zhaojh329/libumqtt.svg?branch=master
[10]: https://travis-ci.org/zhaojh329/libumqtt

[![license][1]][2]
[![PRs Welcome][3]][4]
[![Issue Welcome][5]][6]
[![Release Version][7]][8]
[![Build Status][9]][10]

[libubox]: https://git.openwrt.org/?p=project/libubox.git
[ustream-ssl]: https://git.openwrt.org/?p=project/ustream-ssl.git
[openssl]: https://github.com/openssl/openssl
[mbedtls]: https://github.com/ARMmbed/mbedtls
[CyaSSl(wolfssl)]: https://github.com/wolfSSL/wolfssl

Lightweight MQTT client C library based on libubox for Embedded Linux. Use [libubox] as its event backend.

`Keep Watching for More Actions on This Space`

# Dependencies
* [libubox]
* [ustream-ssl] - If you need to support SSL
* [mbedtls] - If you choose mbedtls as your SSL backend
* [CyaSSl(wolfssl)] - If you choose wolfssl as your SSL backend
* [openssl] - If you choose openssl as your SSL backend

# Contributing
If you would like to help making [libumqtt](https://github.com/zhaojh329/libumqtt) better,
see the [CONTRIBUTING.md](/CONTRIBUTING.md) file.
17 changes: 17 additions & 0 deletions cmake/Modules/FindLibubox.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# - Try to find libubox
# Once done this will define
# LIBUBOX_FOUND - System has libubox
# LIBUBOX_INCLUDE_DIR - The libubox include directories
# LIBUBOX_LIBRARY - The libraries needed to use libubox

find_path(LIBUBOX_INCLUDE_DIR libubox)
find_library(LIBUBOX_LIBRARY ubox PATH_SUFFIXES lib64)

include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set LIBUBOX_FOUND to TRUE
# if all listed variables are TRUE and the requested version matches.
find_package_handle_standard_args(Libubox REQUIRED_VARS
LIBUBOX_LIBRARY LIBUBOX_INCLUDE_DIR
VERSION_VAR LIBUBOX_VERSION)

mark_as_advanced(LIBUBOX_INCLUDE_DIR LIBUBOX_LIBRARY)
29 changes: 29 additions & 0 deletions cmake/Modules/FindLibumqtt.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# - Try to find libumqtt
# Once done this will define
# LIBUMQTT_FOUND - System has libumqtt
# LIBUMQTT_INCLUDE_DIR - The libumqtt include directories
# LIBUMQTT_LIBRARY - The libraries needed to use libumqtt

find_path(LIBUMQTT_INCLUDE_DIR umqtt)
find_library(LIBUMQTT_LIBRARY umqtt PATH_SUFFIXES lib64)

if(LIBUMQTT_INCLUDE_DIR)
file(STRINGS "${LIBUMQTT_INCLUDE_DIR}/umqtt/config.h"
LIBUMQTT_VERSION_MAJOR REGEX "^#define[ \t]+UMQTT_VERSION_MAJOR[ \t]+[0-9]+")
file(STRINGS "${LIBUMQTT_INCLUDE_DIR}/umqtt/config.h"
LIBUMQTT_VERSION_MINOR REGEX "^#define[ \t]+UMQTT_VERSION_MINOR[ \t]+[0-9]+")
string(REGEX REPLACE "[^0-9]+" "" LIBUMQTT_VERSION_MAJOR "${LIBUMQTT_VERSION_MAJOR}")
string(REGEX REPLACE "[^0-9]+" "" LIBUMQTT_VERSION_MINOR "${LIBUMQTT_VERSION_MINOR}")
set(LIBUMQTT_VERSION "${LIBUMQTT_VERSION_MAJOR}.${LIBUMQTT_VERSION_MINOR}")
unset(LIBUMQTT_VERSION_MINOR)
unset(LIBUMQTT_VERSION_MAJOR)
endif()

include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set LIBUMQTT_FOUND to TRUE
# if all listed variables are TRUE and the requested version matches.
find_package_handle_standard_args(Libumqtt REQUIRED_VARS
LIBUMQTT_LIBRARY LIBUMQTT_INCLUDE_DIR
VERSION_VAR LIBUMQTT_VERSION)

mark_as_advanced(LIBUMQTT_INCLUDE_DIR LIBUMQTT_LIBRARY)
4 changes: 4 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include_directories(${CMAKE_SOURCE_DIR}/src ${CMAKE_BINARY_DIR}/src)

add_executable(helloworld helloworld.c)
target_link_libraries(helloworld umqtt ${LIBUBOX_LIBRARY})
178 changes: 178 additions & 0 deletions example/helloworld.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Copyright (C) 2017 Jianhui Zhao <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <umqtt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libubox/utils.h>
#include <libubox/ulog.h>

static void on_conack(struct umqtt_client *cl, uint8_t code)
{
struct umqtt_topic topics[] = {
{
.len = strlen("test1"),
.topic = "test1",
.qos = 0x00
},
{
.len = strlen("test2"),
.topic = "test2",
.qos = 0x01
},
{
.len = strlen("test3"),
.topic = "test3",
.qos = 0x02
}
};

ULOG_INFO("on_conack: %u\n", code);

cl->publish(cl, "test2", "hello world", 1);
cl->subscribe(cl, topics, ARRAY_SIZE(topics));
}

static void on_puback(struct umqtt_client *cl, uint16_t mid)
{
ULOG_INFO("on_puback msg id: %u\n", mid);
}

static void on_pubrel(struct umqtt_client *cl, uint16_t mid)
{
ULOG_INFO("on_pubrel msg id: %u\n", mid);
}

static void on_pubcomp(struct umqtt_client *cl, uint16_t mid)
{
ULOG_INFO("on_pubcomp msg id: %u\n", mid);
}

static void on_unsuback(struct umqtt_client *cl, uint16_t mid)
{
ULOG_INFO("on_unsuback msg id: %u\n", mid);
}

static void on_suback(struct umqtt_client *cl, uint16_t mid, uint8_t qos[], int num)
{
int i;
for (i = 0; i < num; i++)
ULOG_INFO("on_suback msg id: %u, qos: 0x%02X\n", mid, qos[i]);
}

static void on_publish(struct umqtt_client *cl, const char *topic, struct umqtt_payload *payload)
{
ULOG_INFO("on_publish: msd_id(%d) dup(%d) qos(%d) retain(%d) topic(%s) [%.*s]\n",
payload->mid, payload->dup, payload->qos, payload->retain, topic, payload->len, payload->data);
}

static void on_error(struct umqtt_client *cl)
{
ULOG_INFO("on_error: %u\n", cl->error);
}

static void on_close(struct umqtt_client *cl)
{
ULOG_INFO("on_close\n");
uloop_end();
}

static void usage(const char *prog)
{
fprintf(stderr, "Usage: %s [option]\n"
" -h host # Default is 'localhost'\n"
" -p port # Default is 1883\n"
" -c file # Load CA certificates from file\n"
" -n # don't validate the server's certificate\n"
" -s # Use ssl\n"
, prog);
exit(1);
}

int main(int argc, char **argv)
{
int opt;
struct umqtt_client *cl = NULL;
static bool verify = true;
const char *host = "localhost";
int port = 1883;
bool ssl = false;
const char *crt_file = NULL;
struct umqtt_options options = {
.keep_alive = 12,
.client_id = "LIBUMQTT"
};

while ((opt = getopt(argc, argv, "h:p:nc:s")) != -1) {
switch (opt)
{
case 'h':
host = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 's':
ssl = true;
break;
case 'n':
verify = false;
break;
case 'c':
crt_file = optarg;
break;
default: /* '?' */
usage(argv[0]);
}
}

ULOG_INFO("libumqttc version %s\n", UMQTT_VERSION_STRING);

uloop_init();

cl = umqtt_new_ssl(host, port, ssl, crt_file, verify);
if (!cl) {
uloop_done();
return -1;
}

cl->on_conack = on_conack;
cl->on_puback = on_puback;
cl->on_pubrel = on_pubrel;
cl->on_suback = on_suback;
cl->on_publish = on_publish;
cl->on_pubcomp = on_pubcomp;
cl->on_unsuback = on_unsuback;
cl->on_error = on_error;
cl->on_close = on_close;

if (cl->connect(cl, &options, NULL) < 0) {
ULOG_ERR("connect failed\n");
goto err;
}

uloop_run();

err:
cl->free(cl);

uloop_done();

return 0;
}
47 changes: 47 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

add_definitions(-O -Wall -Werror --std=gnu99 -D_GNU_SOURCE -Wno-misleading-indentation)

# The version number.
set(UMQTT_VERSION_MAJOR 0)
set(UMQTT_VERSION_MINOR 0)
set(UMQTT_VERSION_PATCH 1)

# Check the third party Libraries
find_package(Libubox REQUIRED)

include_directories(${CMAKE_CURRENT_BINARY_DIR} ${LIBUBOX_INCLUDE_DIR})

set(EXTRA_LIBS ${LIBUBOX_LIBRARY} dl)
set(SOURCE_FILES umqtt.c log.c)

set(UMQTT_SSL_SUPPORT_CONFIG 1)
option(UMQTT_SSL_SUPPORT "SSL support" ON)

if(NOT UMQTT_SSL_SUPPORT)
set(UMQTT_SSL_SUPPORT_CONFIG 0)
endif()

add_library(umqtt SHARED ${SOURCE_FILES})
set_target_properties(umqtt PROPERTIES VERSION ${UMQTT_VERSION_MAJOR}.${UMQTT_VERSION_MINOR}.${UMQTT_VERSION_PATCH})
target_link_libraries(umqtt ${EXTRA_LIBS})

# configure a header file to pass some of the CMake settings to the source code
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h)

install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/config.h
${CMAKE_CURRENT_SOURCE_DIR}/umqtt.h
DESTINATION
include/umqtt
)

install(
TARGETS umqtt LIBRARY
DESTINATION lib
)

message("")
message(STATUS "UMQTT_VERSION: ${UMQTT_VERSION_MAJOR}.${UMQTT_VERSION_MINOR}.${UMQTT_VERSION_PATCH}")
message(STATUS "UMQTT_SSL_SUPPORT: ${UMQTT_SSL_SUPPORT}")
message("")
Loading

0 comments on commit 0a6edf0

Please sign in to comment.