Skip to content

Commit

Permalink
Separate out functions from mosquitto.c to aid discoverability.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Apr 11, 2018
1 parent ff79432 commit 28dd14f
Show file tree
Hide file tree
Showing 8 changed files with 1,128 additions and 961 deletions.
5 changes: 5 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ include_directories(${mosquitto_SOURCE_DIR} ${mosquitto_SOURCE_DIR}/lib
link_directories(${mosquitto_SOURCE_DIR}/lib)

set(C_SRC
actions.c
callbacks.c
connect.c
handle_connack.c
handle_ping.c
handle_pubackcomp.c
Expand All @@ -42,12 +45,14 @@ set(C_SRC
handle_unsuback.c
helpers.c
logging_mosq.c logging_mosq.h
loop.c
memory_mosq.c memory_mosq.h
messages_mosq.c messages_mosq.h
mosquitto.c mosquitto.h
mosquitto_internal.h
mqtt3_protocol.h
net_mosq.c net_mosq.h
options.c
packet_mosq.c packet_mosq.h
read_handle.c read_handle.h
send_connect.c
Expand Down
22 changes: 21 additions & 1 deletion lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ include ../config.mk
.PHONY : really clean install

MOSQ_OBJS=mosquitto.o \
actions.o \
callbacks.o \
connect.o \
handle_connack.o \
handle_ping.o \
handle_pubackcomp.o \
Expand All @@ -13,9 +16,11 @@ MOSQ_OBJS=mosquitto.o \
handle_unsuback.o \
helpers.o \
logging_mosq.o \
loop.o \
memory_mosq.o \
messages_mosq.o \
net_mosq.o \
options.o \
packet_mosq.o \
read_handle.o \
send_connect.o \
Expand Down Expand Up @@ -72,7 +77,16 @@ libmosquitto.so.${SOVERSION} : ${MOSQ_OBJS}
libmosquitto.a : ${MOSQ_OBJS}
${CROSS_COMPILE}$(AR) cr $@ $^

mosquitto.o : mosquitto.c mosquitto.h
mosquitto.o : mosquitto.c mosquitto.h mosquitto_internal.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

actions.o : actions.c mosquitto.h mosquitto_internal.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

callbacks.o : callbacks.c mosquitto.h mosquitto_internal.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

connect.o : connect.c mosquitto.h mosquitto_internal.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

handle_connack.o : handle_connack.c read_handle.h
Expand Down Expand Up @@ -105,6 +119,9 @@ helpers.o : helpers.c
logging_mosq.o : logging_mosq.c logging_mosq.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

loop.o : loop.c mosquitto.h mosquitto_internal.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

messages_mosq.o : messages_mosq.c messages_mosq.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

Expand All @@ -114,6 +131,9 @@ memory_mosq.o : memory_mosq.c memory_mosq.h
net_mosq.o : net_mosq.c net_mosq.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

options.o : options.c mosquitto.h mosquitto_internal.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

packet_mosq.o : packet_mosq.c packet_mosq.h
${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@

Expand Down
120 changes: 120 additions & 0 deletions lib/actions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright (c) 2010-2018 Roger Light <[email protected]>
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:https://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http:https://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/

#include "config.h"

#include "mosquitto.h"
#include "mosquitto_internal.h"
#include "memory_mosq.h"
#include "messages_mosq.h"
#include "mqtt3_protocol.h"
#include "net_mosq.h"
#include "send_mosq.h"
#include "util_mosq.h"


int mosquitto_publish(struct mosquitto *mosq, int *mid, const char *topic, int payloadlen, const void *payload, int qos, bool retain)
{
struct mosquitto_message_all *message;
uint16_t local_mid;
int queue_status;

if(!mosq || !topic || qos<0 || qos>2) return MOSQ_ERR_INVAL;
if(STREMPTY(topic)) return MOSQ_ERR_INVAL;
if(mosquitto_validate_utf8(topic, strlen(topic))) return MOSQ_ERR_MALFORMED_UTF8;
if(payloadlen < 0 || payloadlen > MQTT_MAX_PAYLOAD) return MOSQ_ERR_PAYLOAD_SIZE;

if(mosquitto_pub_topic_check(topic) != MOSQ_ERR_SUCCESS){
return MOSQ_ERR_INVAL;
}

local_mid = mosquitto__mid_generate(mosq);
if(mid){
*mid = local_mid;
}

if(qos == 0){
return send__publish(mosq, local_mid, topic, payloadlen, payload, qos, retain, false);
}else{
message = mosquitto__calloc(1, sizeof(struct mosquitto_message_all));
if(!message) return MOSQ_ERR_NOMEM;

message->next = NULL;
message->timestamp = mosquitto_time();
message->msg.mid = local_mid;
message->msg.topic = mosquitto__strdup(topic);
if(!message->msg.topic){
message__cleanup(&message);
return MOSQ_ERR_NOMEM;
}
if(payloadlen){
message->msg.payloadlen = payloadlen;
message->msg.payload = mosquitto__malloc(payloadlen*sizeof(uint8_t));
if(!message->msg.payload){
message__cleanup(&message);
return MOSQ_ERR_NOMEM;
}
memcpy(message->msg.payload, payload, payloadlen*sizeof(uint8_t));
}else{
message->msg.payloadlen = 0;
message->msg.payload = NULL;
}
message->msg.qos = qos;
message->msg.retain = retain;
message->dup = false;

pthread_mutex_lock(&mosq->out_message_mutex);
queue_status = message__queue(mosq, message, mosq_md_out);
if(queue_status == 0){
if(qos == 1){
message->state = mosq_ms_wait_for_puback;
}else if(qos == 2){
message->state = mosq_ms_wait_for_pubrec;
}
pthread_mutex_unlock(&mosq->out_message_mutex);
return send__publish(mosq, message->msg.mid, message->msg.topic, message->msg.payloadlen, message->msg.payload, message->msg.qos, message->msg.retain, message->dup);
}else{
message->state = mosq_ms_invalid;
pthread_mutex_unlock(&mosq->out_message_mutex);
return MOSQ_ERR_SUCCESS;
}
}
}


int mosquitto_subscribe(struct mosquitto *mosq, int *mid, const char *sub, int qos)
{
if(!mosq) return MOSQ_ERR_INVAL;
if(mosq->sock == INVALID_SOCKET) return MOSQ_ERR_NO_CONN;

if(mosquitto_sub_topic_check(sub)) return MOSQ_ERR_INVAL;
if(mosquitto_validate_utf8(sub, strlen(sub))) return MOSQ_ERR_MALFORMED_UTF8;

return send__subscribe(mosq, mid, sub, qos);
}


int mosquitto_unsubscribe(struct mosquitto *mosq, int *mid, const char *sub)
{
if(!mosq) return MOSQ_ERR_INVAL;
if(mosq->sock == INVALID_SOCKET) return MOSQ_ERR_NO_CONN;

if(mosquitto_sub_topic_check(sub)) return MOSQ_ERR_INVAL;
if(mosquitto_validate_utf8(sub, strlen(sub))) return MOSQ_ERR_MALFORMED_UTF8;

return send__unsubscribe(mosq, mid, sub);
}

78 changes: 78 additions & 0 deletions lib/callbacks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright (c) 2010-2018 Roger Light <[email protected]>
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:https://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http:https://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/

#include "config.h"

#include "mosquitto.h"
#include "mosquitto_internal.h"


void mosquitto_connect_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int))
{
pthread_mutex_lock(&mosq->callback_mutex);
mosq->on_connect = on_connect;
pthread_mutex_unlock(&mosq->callback_mutex);
}

void mosquitto_connect_with_flags_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int, int))
{
pthread_mutex_lock(&mosq->callback_mutex);
mosq->on_connect_with_flags = on_connect;
pthread_mutex_unlock(&mosq->callback_mutex);
}

void mosquitto_disconnect_callback_set(struct mosquitto *mosq, void (*on_disconnect)(struct mosquitto *, void *, int))
{
pthread_mutex_lock(&mosq->callback_mutex);
mosq->on_disconnect = on_disconnect;
pthread_mutex_unlock(&mosq->callback_mutex);
}

void mosquitto_publish_callback_set(struct mosquitto *mosq, void (*on_publish)(struct mosquitto *, void *, int))
{
pthread_mutex_lock(&mosq->callback_mutex);
mosq->on_publish = on_publish;
pthread_mutex_unlock(&mosq->callback_mutex);
}

void mosquitto_message_callback_set(struct mosquitto *mosq, void (*on_message)(struct mosquitto *, void *, const struct mosquitto_message *))
{
pthread_mutex_lock(&mosq->callback_mutex);
mosq->on_message = on_message;
pthread_mutex_unlock(&mosq->callback_mutex);
}

void mosquitto_subscribe_callback_set(struct mosquitto *mosq, void (*on_subscribe)(struct mosquitto *, void *, int, int, const int *))
{
pthread_mutex_lock(&mosq->callback_mutex);
mosq->on_subscribe = on_subscribe;
pthread_mutex_unlock(&mosq->callback_mutex);
}

void mosquitto_unsubscribe_callback_set(struct mosquitto *mosq, void (*on_unsubscribe)(struct mosquitto *, void *, int))
{
pthread_mutex_lock(&mosq->callback_mutex);
mosq->on_unsubscribe = on_unsubscribe;
pthread_mutex_unlock(&mosq->callback_mutex);
}

void mosquitto_log_callback_set(struct mosquitto *mosq, void (*on_log)(struct mosquitto *, void *, int, const char *))
{
pthread_mutex_lock(&mosq->log_callback_mutex);
mosq->on_log = on_log;
pthread_mutex_unlock(&mosq->log_callback_mutex);
}

Loading

0 comments on commit 28dd14f

Please sign in to comment.