-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate out functions from mosquitto.c to aid discoverability.
- Loading branch information
Showing
8 changed files
with
1,128 additions
and
961 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
Oops, something went wrong.