-
Notifications
You must be signed in to change notification settings - Fork 635
/
Copy pathjobs_demo_mosquitto.c
1320 lines (1099 loc) · 36.1 KB
/
jobs_demo_mosquitto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* AWS IoT Device SDK for Embedded C 202412.00
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* This demonstration downloads files from URLs present in job documents
* received from the AWS IoT Jobs service. It shows the use of the jobs
* library with the Mosquitto client MQTT library for communicating with the
* AWS IoT Jobs service. More details are available in the usage function
* in this file. Note: This demo focuses on use of the jobs library;
* a thorough explanation of libmosquitto is beyond the scope of the demo.
*/
/* C standard includes. */
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* POSIX includes. */
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <err.h>
#include <getopt.h>
#include <mosquitto.h>
#if ( LIBMOSQUITTO_VERSION_NUMBER < 1004010 )
#error Please use libmosquitto at version 1.4.10 or higher.
#endif
#include "demo_config.h"
#include "jobs.h"
#include "core_json.h"
/*-----------------------------------------------------------*/
/**
* @brief MQTT server port number.
*
* AWS IoT Core uses this port for MQTT over TLS.
*/
#define DEFAULT_MQTT_PORT ( 8883 )
/**
* @brief Certificate Authority Directory.
*
* Debian and Ubuntu use this directory for CA certificates.
*/
#define DEFAULT_CA_DIRECTORY "/etc/ssl/certs"
/**
* @brief ALPN (Application-Layer Protocol Negotiation) name for AWS IoT MQTT.
*/
#define ALPN_NAME "x-amzn-mqtt-ca"
/*-----------------------------------------------------------*/
/**
* @brief Describe program usage on stderr.
*
* @param[in] programName the value of argv[0]
*/
static void usage( const char * programName )
{
fprintf( stderr,
"\nThis demonstration downloads files from URLs received via AWS IoT Jobs.\n"
"(See https://docs.aws.amazon.com/iot/latest/developerguide/iot-jobs.html for an introduction.)\n"
"\nCreating a job may be done with the AWS console, or the aws cli, e.g.,\n"
"$ aws iot create-job --job-id t12 --targets arn:aws:iot:us-east-1:1234567890:thing/device1 \\\n"
" --document '{\"url\":\"https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.8.5.tar.xz\"}'\n"
"\nTo execute the job, on the target device run the demo program with the device's credentials, e.g.,\n"
"$ %s -n device1 -h abcdefg123.iot.us-east-1.amazonaws.com \\\n"
" --certfile bbaf123456-certificate.pem.crt --keyfile bbaf123456-private.pem.key\n"
"\nTo exit the program, type Control-C, or send a SIGTERM signal.\n",
programName );
fprintf( stderr,
"\nOutput should look like the following:\n"
"Connecting to abcdefg123.iot.us-east-1.amazonaws.com, port 8883.\n"
"Client device1 sending CONNECT\n"
"Client device1 received CONNACK\n"
"Client device1 sending SUBSCRIBE (Mid: 1, Topic: $aws/things/device1/jobs/start-next/accepted, QoS: 1)\n"
"Client device1 received SUBACK\n"
"[...]\n"
"starting job id: t12\n"
"sending first update\n" );
fprintf( stderr,
"\nIf the output does not show a successful connection, check in the AWS console\n"
"that the client certificate is associated with the target thing and is activated.\n"
"Also check that the Amazon Root CA certificates are in your system's trust store.\n"
"Note, you can provide a CA certificate file directly as a command-line argument.\n" );
fprintf( stderr,
"\nThis demonstration exits on most error conditions. One way to retry while avoiding\n"
"throttling due to excessive reconnects is to periodically relaunch from cron(8).\n"
"Given a shell script wrapper with the necessary arguments called download, the following\n"
"line in /etc/crontab would start the downloader unless it is already running.\n"
"This tries every 3 minutes, with an additional random delay up to 2 minutes.\n\n"
"*/3 * * * * root exec 9> /tmp/lock && flock -n 9 && sleep $((RANDOM %% 120)) && download\n"
);
fprintf( stderr,
"\nusage: %s "
"[-o] -n name -h host [-p port] {--cafile file | --capath dir} --certfile file --keyfile file [--pollinv seconds] [--updateinv seconds]\n"
"\n"
"-o : run once, exit after the first job is finished.\n"
"-n : thing name\n"
"-h : mqtt host to connect to.\n"
"-p : network port to connect to. Defaults to %d.\n",
programName, DEFAULT_MQTT_PORT );
fprintf( stderr,
"--cafile : path to a file containing trusted CA certificates to enable encrypted\n"
" certificate based communication.\n"
"--capath : path to a directory containing trusted CA certificates to enable encrypted\n"
" communication. Defaults to %s.\n"
"--certfile : client certificate for authentication in PEM format.\n"
"--keyfile : client private key for authentication in PEM format.\n",
DEFAULT_CA_DIRECTORY );
fprintf( stderr,
"--pollinv : after this many idle seconds, request a job.\n"
" Without this option and a positive value, no polling is done.\n"
"--updateinv : after this many seconds running a job, resend the current status to the jobs service.\n"
" Without this option and a positive value, status is not resent.\n\n"
);
}
/*-----------------------------------------------------------*/
/**
* @brief The several states of execution.
*/
typedef enum
{
None = 0, /* no current job */
Ready, /* job document received and parsed */
Running, /* download in progress */
Cancel, /* cancel due to failed update */
} runStatus_t;
/**
* @brief All runtime parameters and state.
*/
typedef struct
{
/* thing name */
char * name;
size_t nameLength;
/* connection parameters */
char * host;
uint16_t port;
char * cafile;
char * capath;
char * certfile;
char * keyfile;
uint32_t pollinv; /* 0 (default) disables polling for new jobs */
uint32_t updateinv; /* 0 (default) disables periodic resending of status */
/* flags */
bool runOnce;
/* callback-populated values */
int connectError;
int subscribeQOS;
/* mosquitto library handle */
struct mosquitto * m;
/* job parameters received via MQTT */
char * jobid;
size_t jobidLength;
char * url;
size_t urlLength;
/* internal state tracking */
runStatus_t runStatus;
char * report;
time_t lastPrompt;
time_t lastUpdate;
bool forcePrompt;
bool forceUpdate;
pid_t child;
} handle_t;
/*-----------------------------------------------------------*/
/**
* @brief Populate a handle with default values.
*
* @param[in] p runtime state handle
*/
void initHandle( handle_t * p );
/**
* @brief Validate the values within a handle.
*
* @param[in] h runtime state handle
*
* @return true if necessary arguments are present and valid;
* false otherwise
*/
static bool requiredArgs( handle_t * h );
/**
* @brief Populate a handle from command line arguments.
*
* @param[in] h runtime state handle
* @param[in] argc count of arguments
* @param[in] argv array of arguments
*
* @return false if there is an unrecognized switch;
* true otherwise
*/
static bool parseArgs( handle_t * h,
int argc,
char * argv[] );
/**
* @brief The libmosquitto callback for connection result.
*
* @param[in] m unused
* @param[in] p runtime state handle
* @param[in] rc connection result code
*/
static void on_connect( struct mosquitto * m,
void * p,
int rc );
/**
* @brief Connect to AWS IoT Core MQTT broker.
*
* @param[in] h runtime state handle
*
* @return true if a connection is established;
* false otherwise
*/
static bool connect( handle_t * h );
/**
* @brief Disconnect from AWS IoT Core MQTT broker.
*
* @param[in] h runtime state handle
*/
static void closeConnection( handle_t * h );
/**
* @brief The libmosquitto callback for subscription result.
*
* @param[in] m unused
* @param[in] p runtime state handle
* @param[in] mid unused
* @param[in] qos_count count of granted subscriptions
* @param[in] granted_qos the granted QOS subscription values
*/
static void on_subscribe( struct mosquitto * m,
void * p,
int mid,
int qos_count,
const int * granted_qos );
/**
* @brief Subscribe to a Jobs topic.
*
* @param[in] h runtime state handle
* @param[in] api the desired Jobs topic
*
* @return true if the broker granted the subscription;
* false otherwise
*/
static bool subscribe( handle_t * h,
JobsTopic_t api );
/**
* @brief Publish a status update for a job ID to the Jobs service.
*
* @param[in] h runtime state handle
* @param[in] jobid the job ID
* @param[in] jobidLength size of the job ID string
* @param[in] report body of the update
*
* @return true if libmosquitto accepted the publish message;
* false otherwise
*
* @note This does not call mosquitto_loop(); it expects main() to do so.
*/
static bool sendUpdate( handle_t * h,
char * jobid,
size_t jobidLength,
char * report );
/**
* @brief Read job ID and URL from a JSON job document.
*
* @param[in] h runtime state handle
* @param[in] message an MQTT publish message
*
* @return true if values were found and copied to the handle;
* false otherwise
*/
static bool parseJob( handle_t * h,
const struct mosquitto_message * message );
/**
* @brief The libmosquitto callback for a received publish message.
*
* @param[in] m unused
* @param[in] p runtime state handle
* @param[in] message an MQTT publish message
*
* This checks if a message corresponds to a Jobs API, and transitions
* runtime state based on the API and current state.
*/
void on_message( struct mosquitto * m,
void * p,
const struct mosquitto_message * message );
/**
* @brief Publish a request to the Jobs service to describe the next job.
*
* @param[in] h runtime state handle
*
* @return true if libmosquitto accepted the publish message;
* false otherwise
*
* @note This does not call mosquitto_loop(); it expects main() to do so.
*/
static bool sendDescribeNext( handle_t * h );
/**
* @brief Checks status of the download process.
*
* @param[in] h runtime state handle
*/
static void checkChild( handle_t * h );
/**
* @brief Launch a download process.
*
* @param[in] h runtime state handle
*
* @return true if fork() succeeded;
* false otherwise
*/
static bool download( handle_t * h );
/**
* @brief Kill a download process.
*
* @param[in] h runtime state handle
*/
static void cancelDownload( handle_t * h );
/**
* @brief The libmosquitto callback for log messages.
*
* @param[in] m unused
* @param[in] p unused
* @param[in] level unused
* @param[in] log the message to print
*/
static void on_log( struct mosquitto * m,
void * p,
int level,
const char * log );
/**
* @brief Generic signal handler.
*
* @param[in] signal the caught signal value
*/
static void catch( int signal );
/**
* @brief Setup signal handling and libmosquitto.
*
* @param[in] h runtime state handle
*
* @return false if a step failed;
* true otherwise
*/
static bool setup( handle_t * h );
/**
* @brief Disconnect and clean up.
*
* @param[in] x unused
* @param[in] p runtime state handle
*/
static void teardown( int x,
void * p );
/**
* @brief Log an informational message.
*/
#define info warnx
/**
* @brief Format a JSON status message.
*
* @param[in] x one of "IN_PROGRESS", "SUCCEEDED", or "FAILED"
*/
#define makeReport_( x ) "{\"status\":\"" x "\"}"
/*-----------------------------------------------------------*/
void initHandle( handle_t * p )
{
assert( p != NULL );
handle_t h = { 0 };
#ifdef AWS_IOT_ENDPOINT
h.host = AWS_IOT_ENDPOINT;
#endif
#ifdef CLIENT_CERT_PATH
h.certfile = CLIENT_CERT_PATH;
#endif
#ifdef CLIENT_PRIVATE_KEY_PATH
h.keyfile = CLIENT_PRIVATE_KEY_PATH;
#endif
#ifdef ROOT_CA_CERT_PATH
h.cafile = ROOT_CA_CERT_PATH;
#else
h.capath = DEFAULT_CA_DIRECTORY;
#endif
h.port = DEFAULT_MQTT_PORT;
h.runOnce = false;
/* initialize to -1, set by on_connect() to 0 or greater */
h.connectError = -1;
/* initialize to -1, set by on_subscribe() to 0 or greater */
h.subscribeQOS = -1;
*p = h;
}
/*-----------------------------------------------------------*/
static bool requiredArgs( handle_t * h )
{
bool ret = true;
struct stat s;
assert( h != NULL );
#define checkString( x ) \
if( ( h->x == NULL ) || ( h->x[ 0 ] == '\0' ) ) \
{ \
ret = false; \
warnx( "%s argument must not be empty", # x ); \
}
checkString( name );
checkString( host );
checkString( certfile );
checkString( keyfile );
if( h->nameLength > JOBS_THINGNAME_MAX_LENGTH )
{
ret = false;
warnx( "name length must not exceed %d", JOBS_THINGNAME_MAX_LENGTH );
}
#define checkPath( x ) \
if( ( h->x != NULL ) && ( h->x[ 0 ] != '\0' ) && ( stat( h->x, &s ) == -1 ) ) \
{ \
ret = false; \
warn( "cannot access '%s'", h->x ); \
h->x = NULL; \
}
checkPath( certfile );
checkPath( keyfile );
checkPath( cafile );
checkPath( capath );
/* use value in struct stat s from last check */
if( ( h->capath != NULL ) && ( h->capath[ 0 ] != '\0' ) && ( !S_ISDIR( s.st_mode ) ) )
{
ret = false;
warnx( "not a directory: %s", h->capath );
}
return ret;
}
/*-----------------------------------------------------------*/
static bool parseArgs( handle_t * h,
int argc,
char * argv[] )
{
bool ret = true;
assert( h != NULL );
if( argc == 1 )
{
ret = false;
usage( argv[ 0 ] );
}
while( ret == true )
{
int c, option_index = 0;
long x;
static struct option long_options[] =
{
{ "once", no_argument, NULL, 'o' },
{ "name", required_argument, NULL, 'n' },
{ "host", required_argument, NULL, 'h' },
{ "port", required_argument, NULL, 'p' },
{ "cafile", required_argument, NULL, 'f' },
{ "capath", required_argument, NULL, 'd' },
{ "certfile", required_argument, NULL, 'c' },
{ "keyfile", required_argument, NULL, 'k' },
{ "pollinv", required_argument, NULL, 'P' },
{ "updateinv", required_argument, NULL, 'u' },
{ "help", no_argument, NULL, '?' },
{ NULL, 0, NULL, 0 }
};
c = getopt_long( argc, argv, "on:h:p:P:u:f:d:c:k:?",
long_options, &option_index );
if( c == -1 )
{
break;
}
switch( c )
{
case 'o':
h->runOnce = true;
break;
case 'n':
h->name = optarg;
h->nameLength = strlen( optarg );
break;
case 'h':
h->host = optarg;
break;
#define optargToInt( element, min, max ) \
x = strtol( optarg, NULL, 0 ); \
\
if( ( x > min ) && ( x <= max ) ) \
{ \
h->element = x; \
} \
else \
{ \
ret = false; \
warnx( "bad %s value: %s", # element, optarg ); \
}
case 'p':
optargToInt( port, 0, 0xFFFF );
break;
case 'P':
optargToInt( pollinv, 0, INTERVAL_MAX );
break;
case 'u':
optargToInt( updateinv, 0, INTERVAL_MAX );
break;
case 'f':
h->cafile = optarg;
h->capath = NULL;
break;
case 'd':
h->capath = optarg;
break;
case 'c':
h->certfile = optarg;
break;
case 'k':
h->keyfile = optarg;
break;
case '?':
default:
ret = false;
usage( argv[ 0 ] );
}
}
if( optind < argc )
{
ret = false;
usage( argv[ 0 ] );
}
if( ret == true )
{
ret = requiredArgs( h );
}
return ret;
}
/*-----------------------------------------------------------*/
static void on_connect( struct mosquitto * m,
void * p,
int rc )
{
handle_t * h = p;
assert( h != NULL );
h->connectError = rc;
}
/*-----------------------------------------------------------*/
static bool connect( handle_t * h )
{
int ret = MOSQ_ERR_SUCCESS;
size_t i;
assert( h != NULL );
assert( h->m != NULL );
assert( h->connectError == -1 );
if( h->port == 443 )
{
#if ( LIBMOSQUITTO_VERSION_NUMBER >= 1006000 )
ret = mosquitto_string_option( h->m, MOSQ_OPT_TLS_ALPN, ALPN_NAME );
#else
warnx( "ALPN (port 443) is not supported by libmosquitto before version 1.6" );
ret = MOSQ_ERR_INVAL;
#endif
}
if( ret == MOSQ_ERR_SUCCESS )
{
ret = mosquitto_tls_set( h->m, h->cafile, h->capath, h->certfile, h->keyfile, NULL );
}
if( ret == MOSQ_ERR_SUCCESS )
{
info( "Connecting to %s, port %d.", h->host, h->port );
ret = mosquitto_connect( h->m, h->host, h->port, MQTT_KEEP_ALIVE );
}
/* expect the on_connect() callback to update h->connectError */
for( i = 0; ( i < MAX_LOOPS ) &&
( ret == MOSQ_ERR_SUCCESS ) &&
( h->connectError == -1 ); i++ )
{
ret = mosquitto_loop( h->m, MQTT_SHORT_WAIT_TIME, 1 );
}
if( h->connectError > 0 )
{
warnx( "%s", mosquitto_connack_string( h->connectError ) );
}
else if( ret != MOSQ_ERR_SUCCESS )
{
warnx( "connect: %s", mosquitto_strerror( ret ) );
}
return h->connectError == 0 ? true : false;
}
/*-----------------------------------------------------------*/
static void closeConnection( handle_t * h )
{
assert( h != NULL );
if( h->m != NULL )
{
int ret = mosquitto_disconnect( h->m );
if( ret != MOSQ_ERR_SUCCESS )
{
warnx( "closeConnection: %s", mosquitto_strerror( ret ) );
}
}
}
/*-----------------------------------------------------------*/
static void on_subscribe( struct mosquitto * m,
void * p,
int mid,
int qos_count,
const int * granted_qos )
{
handle_t * h = p;
assert( h != NULL );
assert( granted_qos != NULL );
assert( qos_count == 1 );
/* subscribe() is called with a single topic. */
h->subscribeQOS = granted_qos[ 0 ];
}
/*-----------------------------------------------------------*/
static bool subscribe( handle_t * h,
JobsTopic_t api )
{
int ret;
char topic[ JOBS_API_MAX_LENGTH( JOBS_THINGNAME_MAX_LENGTH ) ];
JobsStatus_t jobs_ret;
size_t i;
assert( h != NULL );
assert( MQTT_QOS <= 2 );
/* populate the topic buffer with the specified API for use
* in a subscription request */
jobs_ret = Jobs_GetTopic( topic,
sizeof( topic ),
h->name,
h->nameLength,
api,
NULL );
assert( jobs_ret == JobsSuccess );
( void ) jobs_ret;
/* set to default value */
h->subscribeQOS = -1;
ret = mosquitto_subscribe( h->m, NULL, topic, MQTT_QOS );
/* expect the on_subscribe() callback to update h->subscribeQOS */
for( i = 0; ( i < MAX_LOOPS ) &&
( ret == MOSQ_ERR_SUCCESS ) &&
( h->subscribeQOS == -1 ); i++ )
{
ret = mosquitto_loop( h->m, MQTT_SHORT_WAIT_TIME, 1 );
}
if( h->subscribeQOS == 0x80 )
{
warnx( "broker rejected subscription" );
}
else if( ret != MOSQ_ERR_SUCCESS )
{
warnx( "subscribe: %s", mosquitto_strerror( ret ) );
}
return ( ( h->subscribeQOS >= 0 ) && ( h->subscribeQOS <= MQTT_QOS ) ) ? true : false;
}
/*-----------------------------------------------------------*/
static bool sendUpdate( handle_t * h,
char * jobid,
size_t jobidLength,
char * report )
{
bool ret = true;
JobsStatus_t jobs_ret;
int m_ret;
char topic[ JOBS_API_MAX_LENGTH( JOBS_THINGNAME_MAX_LENGTH ) ];
assert( h != NULL );
assert( ( jobid != NULL ) && ( jobidLength > 0 ) );
assert( report != NULL );
/* populate the topic buffer for an UpdateJobExecution request */
jobs_ret = Jobs_Update( topic,
sizeof( topic ),
h->name,
h->nameLength,
jobid,
jobidLength,
NULL );
assert( jobs_ret == JobsSuccess );
( void ) jobs_ret;
m_ret = mosquitto_publish( h->m,
NULL,
topic,
strlen( report ),
report,
MQTT_QOS,
false );
if( m_ret != MOSQ_ERR_SUCCESS )
{
warnx( "sendUpdate: %s", mosquitto_strerror( ret ) );
ret = false;
}
return ret;
}
/*-----------------------------------------------------------*/
static bool parseJob( handle_t * h,
const struct mosquitto_message * message )
{
bool ret = false;
JSONStatus_t json_ret;
char * jobid = NULL, * url = NULL;
size_t jobidLength = 0, urlLength = 0;
assert( h != NULL );
assert( message != NULL );
assert( ( message->payload != NULL ) && ( message->payloadlen > 0 ) );
json_ret = JSON_Validate( message->payload, message->payloadlen );
if( json_ret != JSONSuccess )
{
warnx( "invalid job document" );
}
else
{
json_ret = JSON_Search( message->payload,
message->payloadlen,
"execution.jobId",
( sizeof( "execution.jobId" ) - 1 ),
&jobid,
&jobidLength );
}
if( json_ret == JSONSuccess )
{
json_ret = JSON_Search( message->payload,
message->payloadlen,
"execution.jobDocument.url",
( sizeof( "execution.jobDocument.url" ) - 1 ),
&url,
&urlLength );
}
if( json_ret == JSONSuccess )
{
jobid = strndup( jobid, jobidLength );
url = strndup( url, urlLength );
assert( ( jobid != NULL ) && ( url != NULL ) );
h->jobid = jobid;
h->jobidLength = jobidLength;
h->url = url;
h->urlLength = urlLength;
ret = true;
}
else
{
if( jobid != NULL )
{
jobid[ jobidLength ] = '\0';
warnx( "missing url; failing job id: %s", jobid );
( void ) sendUpdate( h, jobid, jobidLength, makeReport_( "FAILED" ) );
}
}
return ret;
}
/*-----------------------------------------------------------*/
void on_message( struct mosquitto * m,
void * p,
const struct mosquitto_message * message )
{
handle_t * h = p;
JobsStatus_t ret;
JobsTopic_t api;
char * jobid;
uint16_t jobidLength;
assert( h != NULL );
assert( message->topic != NULL );
/* identify a Jobs API and a job ID represented in a topic buffer */
ret = Jobs_MatchTopic( message->topic,
strlen( message->topic ),
h->name,
h->nameLength,
&api,
&jobid,
&jobidLength );
assert( ret != JobsBadParameter );
( void ) ret;
switch( api )
{
/* a job has been added or the current job was canceled */
case JobsNextJobChanged:
/* response to a request to describe the next job */
case JobsDescribeSuccess:
if( h->runStatus == Running )
{
h->forceUpdate = true;
h->forcePrompt = true;
}
else if( h->runStatus == None )
{
if( parseJob( h, message ) == true )
{
h->runStatus = Ready;
}
}
else
{
warnx( "unexpected message, topic: %s", message->topic );
}
break;
/* The last update was rejected. */
case JobsUpdateFailed:
if( ( h->runStatus == Running ) &&
( h->jobid != NULL ) &&
( h->jobidLength == jobidLength ) &&
( strncmp( h->jobid, jobid, jobidLength ) == 0 ) )
{
h->runStatus = Cancel;
}
else
{
warnx( "update failure for unknown job id: %.*s", jobidLength, jobid );
}
break;
/* We seem to receive these even without a subscription. */
case JobsUpdateSuccess:
info( "job update success" );
break;
case JobsInvalidTopic:
default:
warnx( "unknown topic: %s", message->topic );
break;
}
}
/*-----------------------------------------------------------*/
static bool sendDescribeNext( handle_t * h )
{
bool ret = true;
JobsStatus_t jobs_ret;
int m_ret;
char topic[ JOBS_API_MAX_LENGTH( JOBS_THINGNAME_MAX_LENGTH ) ];
assert( h != NULL );
/* populate the topic buffer for a DescribeJobExecution request */
jobs_ret = Jobs_Describe( topic,
sizeof( topic ),
h->name,
h->nameLength,
JOBS_API_JOBID_NEXT,
JOBS_API_JOBID_NEXT_LENGTH,
NULL );
assert( jobs_ret == JobsSuccess );
( void ) jobs_ret;
m_ret = mosquitto_publish( h->m, NULL, topic, 0, NULL, MQTT_QOS, false );
if( m_ret != MOSQ_ERR_SUCCESS )
{
warnx( "sendDescribeNext: %s", mosquitto_strerror( ret ) );
ret = false;
}