Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
paho.mqtt.c
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
eclipse
paho.mqtt.c
Commits
6f0edc21
Commit
6f0edc21
authored
Jul 20, 2018
by
Ian Craggs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update synchronous publish sample
parent
7702f614
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
203 additions
and
315 deletions
+203
-315
Makefile
Makefile
+9
-3
CMakeLists.txt
src/samples/CMakeLists.txt
+2
-2
paho_c_pub.c
src/samples/paho_c_pub.c
+7
-15
paho_c_sub.c
src/samples/paho_c_sub.c
+2
-6
paho_cs_pub.c
src/samples/paho_cs_pub.c
+158
-161
paho_cs_sub.c
src/samples/paho_cs_sub.c
+14
-127
pubsub_opts.c
src/samples/pubsub_opts.c
+11
-1
No files found.
Makefile
View file @
6f0edc21
...
@@ -84,9 +84,12 @@ HEADERS = $(srcdir)/*.h
...
@@ -84,9 +84,12 @@ HEADERS = $(srcdir)/*.h
HEADERS_C
=
$
(
filter-out
$(srcdir)
/MQTTAsync.h,
$(HEADERS)
)
HEADERS_C
=
$
(
filter-out
$(srcdir)
/MQTTAsync.h,
$(HEADERS)
)
HEADERS_A
=
$(HEADERS)
HEADERS_A
=
$(HEADERS)
SAMPLE_FILES_C
=
paho_cs_pub paho_cs_sub
MQTTClient_publish MQTTClient_publish_async MQTTClient_subscribe
SAMPLE_FILES_C
=
MQTTClient_publish MQTTClient_publish_async MQTTClient_subscribe
SYNC_SAMPLES
=
${
addprefix
${
blddir
}
/samples/,
${
SAMPLE_FILES_C
}}
SYNC_SAMPLES
=
${
addprefix
${
blddir
}
/samples/,
${
SAMPLE_FILES_C
}}
UTIL_FILES_CS
=
paho_cs_pub paho_cs_sub
SYNC_UTILS
=
${
addprefix
${
blddir
}
/samples/,
${
UTIL_FILES_CS
}}
SAMPLE_FILES_A
=
MQTTAsync_subscribe MQTTAsync_publish
SAMPLE_FILES_A
=
MQTTAsync_subscribe MQTTAsync_publish
ASYNC_SAMPLES
=
${
addprefix
${
blddir
}
/samples/,
${
SAMPLE_FILES_A
}}
ASYNC_SAMPLES
=
${
addprefix
${
blddir
}
/samples/,
${
SAMPLE_FILES_A
}}
...
@@ -181,7 +184,7 @@ endif
...
@@ -181,7 +184,7 @@ endif
all
:
build
all
:
build
build
:
| mkdir ${MQTTLIB_C_TARGET} ${MQTTLIB_CS_TARGET} ${MQTTLIB_A_TARGET} ${MQTTLIB_AS_TARGET} ${MQTTVERSION_TARGET} ${SYNC_SAMPLES} ${ASYNC_SAMPLES} ${ASYNC_UTILS} ${SYNC_TESTS} ${SYNC_SSL_TESTS} ${ASYNC_TESTS} ${ASYNC_SSL_TESTS}
build
:
| mkdir ${MQTTLIB_C_TARGET} ${MQTTLIB_CS_TARGET} ${MQTTLIB_A_TARGET} ${MQTTLIB_AS_TARGET} ${MQTTVERSION_TARGET} ${SYNC_SAMPLES} ${
SYNC_UTILS} ${
ASYNC_SAMPLES} ${ASYNC_UTILS} ${SYNC_TESTS} ${SYNC_SSL_TESTS} ${ASYNC_TESTS} ${ASYNC_SSL_TESTS}
clean
:
clean
:
rm
-rf
${
blddir
}
/
*
rm
-rf
${
blddir
}
/
*
...
@@ -207,6 +210,9 @@ ${ASYNC_SSL_TESTS}: ${blddir}/test/%: ${srcdir}/../test/%.c $(MQTTLIB_CS_TARGET)
...
@@ -207,6 +210,9 @@ ${ASYNC_SSL_TESTS}: ${blddir}/test/%: ${srcdir}/../test/%.c $(MQTTLIB_CS_TARGET)
${SYNC_SAMPLES}
:
${blddir}/samples/%: ${srcdir}/samples/%.c $(MQTTLIB_C_TARGET)
${SYNC_SAMPLES}
:
${blddir}/samples/%: ${srcdir}/samples/%.c $(MQTTLIB_C_TARGET)
${
CC
}
-o
$@
$<
-l
${
MQTTLIB_CS
}
${
FLAGS_EXES
}
${
CC
}
-o
$@
$<
-l
${
MQTTLIB_CS
}
${
FLAGS_EXES
}
${SYNC_UTILS}
:
${blddir}/samples/%: ${srcdir}/samples/%.c $(MQTTLIB_CS_TARGET)
${
CC
}
-o
$@
$<
-l
${
MQTTLIB_CS
}
${
FLAGS_EXES
}
${
srcdir
}
/samples/pubsub_opts.c
${ASYNC_SAMPLES}
:
${blddir}/samples/%: ${srcdir}/samples/%.c $(MQTTLIB_A_TARGET)
${ASYNC_SAMPLES}
:
${blddir}/samples/%: ${srcdir}/samples/%.c $(MQTTLIB_A_TARGET)
${
CC
}
-o
$@
$<
-l
${
MQTTLIB_AS
}
${
FLAGS_EXES
}
${
CC
}
-o
$@
$<
-l
${
MQTTLIB_AS
}
${
FLAGS_EXES
}
...
...
src/samples/CMakeLists.txt
View file @
6f0edc21
...
@@ -31,8 +31,8 @@ ENDIF()
...
@@ -31,8 +31,8 @@ ENDIF()
# sample files c
# sample files c
ADD_EXECUTABLE
(
paho_c_pub paho_c_pub.c pubsub_opts.c
)
ADD_EXECUTABLE
(
paho_c_pub paho_c_pub.c pubsub_opts.c
)
ADD_EXECUTABLE
(
paho_c_sub paho_c_sub.c pubsub_opts.c
)
ADD_EXECUTABLE
(
paho_c_sub paho_c_sub.c pubsub_opts.c
)
ADD_EXECUTABLE
(
paho_cs_pub paho_cs_pub.c
)
ADD_EXECUTABLE
(
paho_cs_pub paho_cs_pub.c
pubsub_opts.c
)
ADD_EXECUTABLE
(
paho_cs_sub paho_cs_sub.c
)
ADD_EXECUTABLE
(
paho_cs_sub paho_cs_sub.c
pubsub_opts.c
)
TARGET_LINK_LIBRARIES
(
paho_c_pub paho-mqtt3as
)
TARGET_LINK_LIBRARIES
(
paho_c_pub paho-mqtt3as
)
TARGET_LINK_LIBRARIES
(
paho_c_sub paho-mqtt3as
)
TARGET_LINK_LIBRARIES
(
paho_c_sub paho-mqtt3as
)
...
...
src/samples/paho_c_pub.c
View file @
6f0edc21
...
@@ -106,7 +106,7 @@ void onDisconnect(void* context, MQTTAsync_successData* response)
...
@@ -106,7 +106,7 @@ void onDisconnect(void* context, MQTTAsync_successData* response)
static
int
connected
=
0
;
static
int
connected
=
0
;
void
myconnect
(
MQTTAsync
*
client
);
void
myconnect
(
MQTTAsync
client
);
void
onConnectFailure5
(
void
*
context
,
MQTTAsync_failureData5
*
response
)
void
onConnectFailure5
(
void
*
context
,
MQTTAsync_failureData5
*
response
)
{
{
...
@@ -175,7 +175,7 @@ void onPublish(void* context, MQTTAsync_successData* response)
...
@@ -175,7 +175,7 @@ void onPublish(void* context, MQTTAsync_successData* response)
}
}
void
myconnect
(
MQTTAsync
*
client
)
void
myconnect
(
MQTTAsync
client
)
{
{
MQTTAsync_connectOptions
conn_opts
=
MQTTAsync_connectOptions_initializer
;
MQTTAsync_connectOptions
conn_opts
=
MQTTAsync_connectOptions_initializer
;
MQTTAsync_SSLOptions
ssl_opts
=
MQTTAsync_SSLOptions_initializer
;
MQTTAsync_SSLOptions
ssl_opts
=
MQTTAsync_SSLOptions_initializer
;
...
@@ -185,7 +185,6 @@ void myconnect(MQTTAsync* client)
...
@@ -185,7 +185,6 @@ void myconnect(MQTTAsync* client)
if
(
opts
.
verbose
)
if
(
opts
.
verbose
)
printf
(
"Connecting
\n
"
);
printf
(
"Connecting
\n
"
);
conn_opts
.
keepAliveInterval
=
opts
.
keepalive
;
conn_opts
.
keepAliveInterval
=
opts
.
keepalive
;
conn_opts
.
cleansession
=
1
;
conn_opts
.
username
=
opts
.
username
;
conn_opts
.
username
=
opts
.
username
;
conn_opts
.
password
=
opts
.
password
;
conn_opts
.
password
=
opts
.
password
;
conn_opts
.
MQTTVersion
=
opts
.
MQTTVersion
;
conn_opts
.
MQTTVersion
=
opts
.
MQTTVersion
;
...
@@ -195,11 +194,13 @@ void myconnect(MQTTAsync* client)
...
@@ -195,11 +194,13 @@ void myconnect(MQTTAsync* client)
conn_opts
=
conn_opts5
;
conn_opts
=
conn_opts5
;
conn_opts
.
onSuccess5
=
onConnect5
;
conn_opts
.
onSuccess5
=
onConnect5
;
conn_opts
.
onFailure5
=
onConnectFailure5
;
conn_opts
.
onFailure5
=
onConnectFailure5
;
conn_opts
.
cleanstart
=
1
;
}
}
else
else
{
{
conn_opts
.
onSuccess
=
onConnect
;
conn_opts
.
onSuccess
=
onConnect
;
conn_opts
.
onFailure
=
onConnectFailure
;
conn_opts
.
onFailure
=
onConnectFailure
;
conn_opts
.
cleansession
=
1
;
}
}
conn_opts
.
context
=
client
;
conn_opts
.
context
=
client
;
conn_opts
.
automaticReconnect
=
1
;
conn_opts
.
automaticReconnect
=
1
;
...
@@ -228,7 +229,7 @@ void myconnect(MQTTAsync* client)
...
@@ -228,7 +229,7 @@ void myconnect(MQTTAsync* client)
}
}
connected
=
0
;
connected
=
0
;
if
((
rc
=
MQTTAsync_connect
(
*
client
,
&
conn_opts
))
!=
MQTTASYNC_SUCCESS
)
if
((
rc
=
MQTTAsync_connect
(
client
,
&
conn_opts
))
!=
MQTTASYNC_SUCCESS
)
{
{
printf
(
"Failed to start connect, return code %d
\n
"
,
rc
);
printf
(
"Failed to start connect, return code %d
\n
"
,
rc
);
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
...
@@ -268,16 +269,6 @@ int main(int argc, char** argv)
...
@@ -268,16 +269,6 @@ int main(int argc, char** argv)
if
(
opts
.
verbose
)
if
(
opts
.
verbose
)
printf
(
"URL is %s
\n
"
,
url
);
printf
(
"URL is %s
\n
"
,
url
);
if
(
argv
[
1
][
0
]
!=
'-'
)
{
opts
.
topic
=
argv
[
1
];
if
(
opts
.
verbose
)
printf
(
"Topic is %s
\n
"
,
opts
.
topic
);
}
if
(
opts
.
topic
==
NULL
)
usage
();
if
(
opts
.
tracelevel
>
0
)
if
(
opts
.
tracelevel
>
0
)
{
{
MQTTAsync_setTraceCallback
(
trace_callback
);
MQTTAsync_setTraceCallback
(
trace_callback
);
...
@@ -290,9 +281,10 @@ int main(int argc, char** argv)
...
@@ -290,9 +281,10 @@ int main(int argc, char** argv)
signal
(
SIGINT
,
cfinish
);
signal
(
SIGINT
,
cfinish
);
signal
(
SIGTERM
,
cfinish
);
signal
(
SIGTERM
,
cfinish
);
rc
=
MQTTAsync_setCallbacks
(
client
,
client
,
NULL
,
messageArrived
,
NULL
);
rc
=
MQTTAsync_setCallbacks
(
client
,
client
,
NULL
,
messageArrived
,
NULL
);
myconnect
(
&
client
);
myconnect
(
client
);
buffer
=
malloc
(
opts
.
maxdatalen
);
buffer
=
malloc
(
opts
.
maxdatalen
);
...
...
src/samples/paho_c_sub.c
View file @
6f0edc21
...
@@ -247,15 +247,11 @@ int main(int argc, char** argv)
...
@@ -247,15 +247,11 @@ int main(int argc, char** argv)
if
(
argc
<
2
)
if
(
argc
<
2
)
usage
();
usage
();
topic
=
argv
[
1
];
if
(
getopts
(
argc
,
argv
,
&
opts
)
!=
0
)
usage
();
if
(
strchr
(
topic
,
'#'
)
||
strchr
(
topic
,
'+'
))
if
(
strchr
(
topic
,
'#'
)
||
strchr
(
topic
,
'+'
))
opts
.
verbose
=
1
;
opts
.
verbose
=
1
;
if
(
opts
.
verbose
)
printf
(
"topic is %s
\n
"
,
topic
);
if
(
getopts
(
argc
,
argv
,
&
opts
)
!=
0
)
usage
();
if
(
opts
.
connection
)
if
(
opts
.
connection
)
url
=
opts
.
connection
;
url
=
opts
.
connection
;
...
...
src/samples/paho_cs_pub.c
View file @
6f0edc21
This diff is collapsed.
Click to expand it.
src/samples/paho_cs_sub.c
View file @
6f0edc21
...
@@ -20,26 +20,10 @@
...
@@ -20,26 +20,10 @@
stdout subscriber
stdout subscriber
compulsory parameters:
--topic topic to subscribe to
defaulted parameters:
--host localhost
--port 1883
--qos 2
--delimiter \n
--clientid stdout-subscriber
--showtopics off
--keepalive 10
--userid none
--password none
*/
*/
#include "MQTTClient.h"
#include "MQTTClient.h"
#include "MQTTClientPersistence.h"
#include "MQTTClientPersistence.h"
#include "pubsub_opts.h"
#include <stdio.h>
#include <stdio.h>
#include <signal.h>
#include <signal.h>
...
@@ -57,21 +41,13 @@
...
@@ -57,21 +41,13 @@
volatile
int
toStop
=
0
;
volatile
int
toStop
=
0
;
struct
opts_struct
struct
pubsub_opts
opts
=
{
char
*
clientid
;
int
nodelimiter
;
char
*
delimiter
;
int
qos
;
char
*
username
;
char
*
password
;
char
*
host
;
char
*
port
;
int
showtopics
;
int
keepalive
;
}
opts
=
{
{
"stdout-subscriber"
,
0
,
"
\n
"
,
2
,
NULL
,
NULL
,
"localhost"
,
"1883"
,
0
,
10
MQTTVERSION_DEFAULT
,
0
,
NULL
,
"paho-cs-sub"
,
"
\n
"
,
100
,
0
,
0
,
NULL
,
NULL
,
"localhost"
,
"1883"
,
NULL
,
0
,
10
,
NULL
,
NULL
,
0
,
0
,
/* will options */
0
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
NULL
,
/* TLS options */
0
,
{
NULL
,
NULL
},
/* publish properties */
};
};
...
@@ -109,7 +85,6 @@ void cfinish(int sig)
...
@@ -109,7 +85,6 @@ void cfinish(int sig)
toStop
=
1
;
toStop
=
1
;
}
}
void
getopts
(
int
argc
,
char
**
argv
);
int
main
(
int
argc
,
char
**
argv
)
int
main
(
int
argc
,
char
**
argv
)
{
{
...
@@ -125,11 +100,13 @@ int main(int argc, char** argv)
...
@@ -125,11 +100,13 @@ int main(int argc, char** argv)
topic
=
argv
[
1
];
topic
=
argv
[
1
];
if
(
strchr
(
topic
,
'#'
)
||
strchr
(
topic
,
'+'
))
if
(
strchr
(
topic
,
'#'
)
||
strchr
(
topic
,
'+'
))
opts
.
showtopics
=
1
;
opts
.
verbose
=
1
;
if
(
opts
.
showtopics
)
if
(
opts
.
verbose
)
printf
(
"topic is %s
\n
"
,
topic
);
printf
(
"topic is %s
\n
"
,
topic
);
getopts
(
argc
,
argv
);
if
(
getopts
(
argc
,
argv
,
&
opts
)
!=
0
)
usage
();
sprintf
(
url
,
"%s:%s"
,
opts
.
host
,
opts
.
port
);
sprintf
(
url
,
"%s:%s"
,
opts
.
host
,
opts
.
port
);
rc
=
MQTTClient_create
(
&
client
,
url
,
opts
.
clientid
,
MQTTCLIENT_PERSISTENCE_NONE
,
NULL
);
rc
=
MQTTClient_create
(
&
client
,
url
,
opts
.
clientid
,
MQTTCLIENT_PERSISTENCE_NONE
,
NULL
);
...
@@ -156,9 +133,9 @@ int main(int argc, char** argv)
...
@@ -156,9 +133,9 @@ int main(int argc, char** argv)
rc
=
MQTTClient_receive
(
client
,
&
topicName
,
&
topicLen
,
&
message
,
1000
);
rc
=
MQTTClient_receive
(
client
,
&
topicName
,
&
topicLen
,
&
message
,
1000
);
if
(
message
)
if
(
message
)
{
{
if
(
opts
.
showtopics
)
if
(
opts
.
verbose
)
printf
(
"%s
\t
"
,
topicName
);
printf
(
"%s
\t
"
,
topicName
);
if
(
opts
.
nodelimiter
)
if
(
opts
.
delimiter
==
NULL
)
printf
(
"%.*s"
,
message
->
payloadlen
,
(
char
*
)
message
->
payload
);
printf
(
"%.*s"
,
message
->
payloadlen
,
(
char
*
)
message
->
payload
);
else
else
printf
(
"%.*s%s"
,
message
->
payloadlen
,
(
char
*
)
message
->
payload
,
opts
.
delimiter
);
printf
(
"%.*s%s"
,
message
->
payloadlen
,
(
char
*
)
message
->
payload
,
opts
.
delimiter
);
...
@@ -178,93 +155,3 @@ int main(int argc, char** argv)
...
@@ -178,93 +155,3 @@ int main(int argc, char** argv)
return
EXIT_SUCCESS
;
return
EXIT_SUCCESS
;
}
}
void
getopts
(
int
argc
,
char
**
argv
)
{
int
count
=
2
;
while
(
count
<
argc
)
{
if
(
strcmp
(
argv
[
count
],
"--qos"
)
==
0
)
{
if
(
++
count
<
argc
)
{
if
(
strcmp
(
argv
[
count
],
"0"
)
==
0
)
opts
.
qos
=
0
;
else
if
(
strcmp
(
argv
[
count
],
"1"
)
==
0
)
opts
.
qos
=
1
;
else
if
(
strcmp
(
argv
[
count
],
"2"
)
==
0
)
opts
.
qos
=
2
;
else
usage
();
}
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--host"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
host
=
argv
[
count
];
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--port"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
port
=
argv
[
count
];
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--clientid"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
clientid
=
argv
[
count
];
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--username"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
username
=
argv
[
count
];
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--password"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
password
=
argv
[
count
];
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--delimiter"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
delimiter
=
argv
[
count
];
else
opts
.
nodelimiter
=
1
;
}
else
if
(
strcmp
(
argv
[
count
],
"--showtopics"
)
==
0
)
{
if
(
++
count
<
argc
)
{
if
(
strcmp
(
argv
[
count
],
"on"
)
==
0
)
opts
.
showtopics
=
1
;
else
if
(
strcmp
(
argv
[
count
],
"off"
)
==
0
)
opts
.
showtopics
=
0
;
else
usage
();
}
else
usage
();
}
else
if
(
strcmp
(
argv
[
count
],
"--keepalive"
)
==
0
)
{
if
(
++
count
<
argc
)
opts
.
keepalive
=
atoi
(
argv
[
count
]);
else
usage
();
}
count
++
;
}
}
src/samples/pubsub_opts.c
View file @
6f0edc21
...
@@ -24,7 +24,13 @@
...
@@ -24,7 +24,13 @@
int
getopts
(
int
argc
,
char
**
argv
,
struct
pubsub_opts
*
opts
)
int
getopts
(
int
argc
,
char
**
argv
,
struct
pubsub_opts
*
opts
)
{
{
int
count
=
2
;
int
count
=
1
;
if
(
argv
[
1
][
0
]
!=
'-'
)
{
opts
->
topic
=
argv
[
1
];
count
=
2
;
}
while
(
count
<
argc
)
while
(
count
<
argc
)
{
{
...
@@ -254,5 +260,9 @@ int getopts(int argc, char** argv, struct pubsub_opts* opts)
...
@@ -254,5 +260,9 @@ int getopts(int argc, char** argv, struct pubsub_opts* opts)
count
++
;
count
++
;
}
}
if
(
opts
->
topic
==
NULL
)
return
1
;
return
0
;
return
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment