c

command
v0.0.0-...-0dbc52a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

README

libmqtt C/C++ lib

A MQTT C/C++ client library exported from libmqtt

NOTE: This library is still under work, some bugs can happen

Contents

  1. Build
  2. Usage

Build

Prerequisite
  1. Go (with GOPATH configured)
  2. make (for build ease)
  3. gcc (for cgo builds)
Steps
  1. Go get this project
go get github.com/goiiot/libmqtt
  1. Make the library
cd $GOPATH/src/github.com/goiiot/libmqtt/c

make

Usage

  1. Include the generated header libmqtt.h
#include "libmqtt.h"
  1. Create and configure the client
client = Libmqtt_new_client();
if (client < 1) {
  printf("create client failed\n");
  exit(1);
}

Libmqtt_client_set_server(client, "localhost:8883");
Libmqtt_client_set_log(client, libmqtt_log_silent);
Libmqtt_client_set_clean_session(client, true);
Libmqtt_client_set_keepalive(client, 10, 1.2);
Libmqtt_client_set_identity(client, "foo", "bar");
Libmqtt_client_set_client_id(client, "foo");
Libmqtt_client_set_tls(client, "client-cert.pem", "client-key.pem", "ca-cert.pem", "MacBook-Air.local", true);

// other options provided by libmqtt are also avalieble here
  1. Setup the client and check if error happened
char *err = Libmqtt_setup(client);
if (err != NULL) {
  printf("error happened when create client: %s\n", err);
  return 1;
}
  1. Create and register the handlers as you like
// sub_handler for subscribe packet response, non-null err means topic sub failed
void sub_handler(char *topic, int qos, char *err) {}

// pub_handler for publish packet response, non-null err means `topic` publish failed
void pub_handler(char *topic, char *err) {}

// unsub_handler for unsubscribe response, non-null err means `topic` unsubscribe failed
void unsub_handler(char *topic, char *err) {}

// net_handler for net connection information, called only when error happens
void net_handler(char *server, char *err) {}

// topic_handler, just a example topic handler
void topic_handler(char *topic, int qos, char *msg, int size) {}

// client handlers (optional, but recommend to)
Libmqtt_set_pub_handler(client, &pub_handler);
Libmqtt_set_sub_handler(client, &sub_handler);
Libmqtt_set_net_handler(client, &net_handler);
Libmqtt_set_unsub_handler(client, &unsub_handler);

// the topic handler
Libmqtt_handle(client, "foo", &topic_handler);
// Libmqtt_handle(client, "bar", &topic_handler);
// ...
  1. Connect to server and wait for connection close
// conn_handler for connect packet response
void conn_handler(char *server, libmqtt_connack_t code, char *err) {}

// connect to server with
Libmqtt_conn(client, &conn_handler);

// wait until all connection closed
Libmqtt_wait(client);

You can refer to the example for a full usage example

LICENSE

Copyright GoIIoT (https://github.com/goiiot)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL