-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
760 lines (515 loc) · 23.1 KB
/
main.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
/**
******************************************************************************
* @file main.c
* @author Aditya Mall,
* @brief Network API test app file
*
* Info
* Target Platform : EK-TM4C123GXL w/ ENC28J60
* Target uC : TM4C123GH6PM
* System Clock : 40 MHz
*
* Hardware configuration :-
* ENC28J60 Ethernet controller
* MOSI (SSI2Tx) on PB7
* MISO (SSI2Rx) on PB6
* SCLK (SSI2Clk) on PB4
* ~CS connected to PB1
*
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2019 Aditya Mall, MIT License </center></h2>
*
* Copyright (c) 2019 Aditya Mall
*
* Please Take prior permission from Dr. Jason Losh and Respective Owners of the,
* API Libraries if you are a student at The University of Texas at Arlington and,
* wish to use part of the code in your project.
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/*
******************************************************************************
*
* Repository Information :-
*
* Clone "clterm" Repo from : https://github.com/adimalla/clTerm
* Clone "MQTT-3.1-C" Repo from : https://github.com/adimalla/MQTT-3.1-C
*
******************************************************************************
*/
/*
* Standard header and API header files
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "tm4c123gh6pm.h"
#include "enc28j60.h"
#include "wait.h"
#include "ethernet.h"
#include "network_utilities.h"
#include "arp.h"
#include "ipv4.h"
#include "icmp.h"
#include "udp.h"
#include "dhcp.h"
#include "tcp.h"
#include "cl_term.h"
#include "mqtt_client.h"
/******************************************************************************/
/* */
/* Data Structures and Defines */
/* */
/******************************************************************************/
#define STATIC 1
#define ICMP_TEST 1
#define UDP_TEST 0
#define TCP_TEST 0
#define MQTT_TEST 1
#define RED_LED (*((volatile uint32_t *)(0x42000000 + (0x400253FC-0x40000000)*32 + 1*4)))
#define GREEN_LED (*((volatile uint32_t *)(0x42000000 + (0x400253FC-0x40000000)*32 + 3*4)))
#define BLUE_LED (*((volatile uint32_t *)(0x42000000 + (0x400253FC-0x40000000)*32 + 2*4)))
#define PUSH_BUTTON (*((volatile uint32_t *)(0x42000000 + (0x400253FC-0x40000000)*32 + 4*4)))
typedef enum _app_state
{
APP_INIT = 0,
APP_READ = 1,
APP_WRITE = 2,
USER_INPUT = 3,
}app_state_t;
/******************************************************************************/
/* */
/* Functions Implementations */
/* */
/******************************************************************************/
void initHw()
{
// Configure HW to work with 16 MHz XTAL, PLL enabled, system clock of 40 MHz
SYSCTL_RCC_R = SYSCTL_RCC_XTAL_16MHZ | SYSCTL_RCC_OSCSRC_MAIN | SYSCTL_RCC_USESYSDIV | (4 << SYSCTL_RCC_SYSDIV_S);
// Set GPIO ports to use APB (not needed since default configuration -- for clarity)
// Note UART on port A must use APB
SYSCTL_GPIOHBCTL_R = 0;
// Enable GPIO port B and E peripherals
SYSCTL_RCGC2_R = SYSCTL_RCGC2_GPIOB | SYSCTL_RCGC2_GPIOD | SYSCTL_RCGC2_GPIOF | SYSCTL_RCGC2_GPIOA | SYSCTL_RCGC2_GPIOE;
// Configure LED and pushbutton pins
GPIO_PORTF_DIR_R = 0x0E; // bits 1-3 are outputs, other pins are inputs
GPIO_PORTF_DR2R_R = 0x0E; // set drive strength to 2mA (not needed since default configuration -- for clarity)
GPIO_PORTF_DEN_R = 0x1E; // enable LEDs and pushbuttons
GPIO_PORTF_PUR_R = 0x1E; // enable internal pull-up for push button
#if IOT_COURSE_TEST /* IOT Networking Student Hardware check */
// Configure ~CS for ENC28J60
GPIO_PORTA_DIR_R = (1 << 3); // make bit 1 an output
GPIO_PORTA_DEN_R = (1 << 3); // enable bits 1 for digital
// GPIO_PORTD_DIR_R = (1 << 1); // make bit 1 an output
// GPIO_PORTD_DEN_R = (1 << 1); // enable bits 1 for digital
SYSCTL_RCGCSSI_R |= SYSCTL_RCGCSSI_R0;
GPIO_PORTA_DIR_R |= ( 1 << 2) | (1 << 3) | (1 << 5);
GPIO_PORTA_DR2R_R |= ( 1 << 2) | (1 << 4) | (1 << 5);
GPIO_PORTA_AFSEL_R |= ( 1 << 2) | (1 << 4) | (1 << 5);
GPIO_PORTA_PCTL_R = GPIO_PCTL_PA5_SSI0TX | GPIO_PCTL_PA4_SSI0RX | GPIO_PCTL_PA2_SSI0CLK;
GPIO_PORTA_DEN_R |= ( 1<< 2) | (1 << 4) | (1 << 5);
SSI0_CR1_R &= ~SSI_CR1_SSE;
SSI0_CR1_R = 0; // select master mode
SSI0_CC_R = 0; // select system clock as the clock source
SSI0_CPSR_R = 20; // set bit rate to 1 MHz (if SR=0 in CR0)
SSI0_CR0_R = SSI_CR0_FRF_MOTO | SSI_CR0_DSS_8; // set SR=0, mode 0 (SPH=0, SPO=0), 8-bit
SSI0_CR1_R |= SSI_CR1_SSE;
#else
// Configure ~CS for ENC28J60
GPIO_PORTB_DIR_R = (1 << 5); // make bit 1 an output
GPIO_PORTB_DEN_R = (1 << 5); // enable bits 1 for digital
// Configure SSI2 pins for SPI configuration
SYSCTL_RCGCSSI_R |= SYSCTL_RCGCSSI_R2; // turn-on SSI2 clocking
GPIO_PORTB_DIR_R |= 0x90; // make bits 4 and 7 outputs
GPIO_PORTB_DR2R_R |= 0x90; // set drive strength to 2mA
GPIO_PORTB_AFSEL_R |= 0xD0; // select alternative functions for MOSI, MISO, SCLK pins
GPIO_PORTB_PCTL_R = GPIO_PCTL_PB7_SSI2TX | \
GPIO_PCTL_PB6_SSI2RX | \
GPIO_PCTL_PB4_SSI2CLK; // map alt fns to SSI2
GPIO_PORTB_DEN_R |= 0xD0; // enable digital operation on TX, RX, CLK pins
// Configure the SSI2 as a SPI master, mode 3, 8bit operation, 1 MHz bit rate
SSI2_CR1_R &= ~SSI_CR1_SSE; // turn off SSI2 to allow re-configuration
SSI2_CR1_R = 0; // select master mode
SSI2_CC_R = 0; // select system clock as the clock source
SSI2_CPSR_R = 40; // set bit rate to 1 MHz (if SR=0 in CR0)
SSI2_CR0_R = SSI_CR0_FRF_MOTO | SSI_CR0_DSS_8; // set SR=0, mode 0 (SPH=0, SPO=0), 8-bit
SSI2_CR1_R |= SSI_CR1_SSE; // turn on SSI2
#endif
}
void init_uart0(void)
{
// Enable GPIO port F peripherals
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOA;
// Configure UART0 pins
SYSCTL_RCGCUART_R |= SYSCTL_RCGCUART_R0; // Turn-on UART0, leave other uarts in same status
GPIO_PORTA_DEN_R |= 3; // Turn on Digital Operations on PA0 and PA1
GPIO_PORTA_AFSEL_R |= 3; // Select Alternate Functionality on PA0 and PA1
GPIO_PORTA_PCTL_R |= GPIO_PCTL_PA1_U0TX | GPIO_PCTL_PA0_U0RX; // Select UART0 Module
// Configure UART0 to 115200 baud, 8N1 format (must be 3 clocks from clock enable and config writes)
UART0_CTL_R = 0; // turn-off UART0 to allow safe programming
UART0_CC_R |= UART_CC_CS_SYSCLK; // use system clock (40 MHz)
UART0_IBRD_R = 21; // r = 40 MHz / (Nx115.2kHz), set floor(r)=21, where N=16
UART0_FBRD_R = 45; // round(fract(r)*64)=45
UART0_LCRH_R |= UART_LCRH_WLEN_8 | UART_LCRH_FEN; // configure for 8N1 w/ 16-level FIFO
UART0_CTL_R |= UART_CTL_TXE | UART_CTL_RXE | UART_CTL_UARTEN; // enable TX, RX, and module
}
// Blocking function that writes a serial character when the UART buffer is not full
void putcUart0(const char c)
{
while(UART0_FR_R & UART_FR_TXFF);
UART0_DR_R = c;
}
// Blocking function that returns with serial data once the buffer is not empty
char getcUart0(void)
{
while (UART0_FR_R & UART_FR_RXFE);
return UART0_DR_R & 0xFF;
}
// Blocking function that writes a string when the UART buffer is not full
void putsUart0(const char* str)
{
uint8_t i;
for (i = 0; i < strlen(str); i++)
putcUart0(str[i]);
}
void init_adc(void)
{
// Enable GPIO port B and E peripherals
// Configure AN0 as an analog input
SYSCTL_RCGCADC_R |= SYSCTL_RCGCADC_R0; // turn on ADC module 0 clocking
GPIO_PORTE_AFSEL_R |= (1 << 3); // select alternative functions for AN0 (PE3)
GPIO_PORTE_DEN_R &= ~(1 << 3); // turn off digital operation on pin PE3
GPIO_PORTE_AMSEL_R |= (1 << 3); // turn on analog operation on pin PE3
ADC0_CC_R = ADC_CC_CS_SYSPLL; // select PLL as the time base (not needed, since default value)
ADC0_ACTSS_R &= ~ADC_ACTSS_ASEN3; // disable sample sequencer 3 (SS3) for programming
ADC0_EMUX_R = ADC_EMUX_EM3_PROCESSOR; // select SS3 bit in ADCPSSI as trigger
ADC0_SSMUX3_R = 0; // set first sample to AN0
ADC0_SSCTL3_R = ADC_SSCTL3_END0 | ADC_SSCTL3_TS0; // mark first sample as the end
ADC0_ACTSS_R |= ADC_ACTSS_ASEN3; // enable SS3 for operation
}
uint16_t readAdc0Ss3()
{
ADC0_PSSI_R |= ADC_PSSI_SS3; // set start bit
while (ADC0_ACTSS_R & ADC_ACTSS_BUSY); // wait until SS3 is not busy
return ADC0_SSFIFO3_R & 0x0F; // get single result from the FIFO
}
/* wrapper Functions */
uint8_t ether_open(uint8_t *mac_address)
{
etherInit(ETHER_UNICAST | ETHER_BROADCAST | ETHER_HALFDUPLEX, mac_address);
return 0;
}
uint8_t myUartOpen(uint32_t baud_rate)
{
init_uart0();
//return 1 for success, here
return 1;
}
uint8_t myPutChar(char data)
{
putcUart0(data);
return 0;
}
/* Link Console operation functions */
console_ops_t myUartOperations =
{
.open = myUartOpen,
.print_char = myPutChar,
.read_char = getcUart0,
};
/* Link Network operation functions */
ether_operations_t ether_ops =
{
.open = ether_open,
.network_interface_status = etherKbhit,
.ether_send_packet = etherPutPacket,
.ether_recv_packet = etherGetPacket,
.random_gen_seed = readAdc0Ss3,
};
/* Main */
int main(void)
{
enc28j60_frame_t *network_hardware;
ethernet_handle_t *ethernet;
uint8_t loop = 0;
int16_t retval = 0;
/* Network Data Buffer */
uint8_t data[ETHER_MTU_SIZE] = {0};
cl_term_t *my_console;
char serial_buffer[MAX_INPUT_SIZE] = {0};
/* Point Network data */
network_hardware = (void*)data;
/* init controller */
initHw();
init_adc();
/* Console Configurations */
my_console = console_open(&myUartOperations, 115200, serial_buffer, CONSOLE_STATIC);
console_print(my_console, CONSOLE_CLEAR_SCREEN);
/* Create Ethernet handle */
ethernet = create_ethernet_handle(&network_hardware->data, "02:03:04:50:60:48", "192.168.1.199", ðer_ops);
/* flash PHY LEDS */
etherWritePhy(PHLCON, 0x0880);
RED_LED = 1;
waitMicrosecond(500000);
etherWritePhy(PHLCON, 0x0990);
RED_LED = 0;
waitMicrosecond(500000);
#if STATIC
set_ip_address(ethernet->gateway_ip, "192.168.1.196");
#else
/* test DHCP */
ether_get_dhcp_ip(ethernet, (uint8_t*)network_hardware, ethernet->host_mac, DHCP_INIT_STATE);
#endif
#if ICMP_TEST
/* Test ICM, ARP packets */
uint8_t sequence_no = 1;
ether_send_arp_req(ethernet, ethernet->host_ip, ethernet->gateway_ip);
if(ether_is_arp(ethernet, (uint8_t*)network_hardware, 128))
{
ether_handle_arp_resp_req(ethernet);
GREEN_LED = 1;
waitMicrosecond(50000);
GREEN_LED = 0;
}
/* Test ICMP packets */
ether_send_icmp_req(ethernet, ICMP_ECHOREQUEST, ethernet->gateway_ip, &sequence_no, \
ethernet->arp_table[0].mac_address, ethernet->host_mac);
#endif
#if UDP_TEST
ether_send_udp(ethernet, ethernet->gateway_ip, 8080, "Hello", 5);
ether_read_udp(ethernet, (uint8_t*)network_hardware, udp_data, APP_BUFF_SIZE);
if(strncmp(udp_data, "Hello from server", 18) == 0)
ether_send_udp(ethernet, ethernet->gateway_ip, 8080, "Hello again", 11);
#endif
#if TCP_TEST
/* Test TCP application */
uint16_t tcp_src_port = 0;
uint16_t tcp_dest_port = 0;
int32_t tcp_retval = 0;
int16_t input_length = 0;
uint16_t count = 0;
char tcp_data[50] = {0};
uint8_t destination_ip[4] = {0};
set_ip_address(destination_ip, "192.168.1.13");
tcp_handle_t *test_client;
/* APP state machine */
app_state_t app_state = APP_INIT;
loop = 1 ;
while(loop)
{
switch(app_state)
{
case APP_INIT:
tcp_dest_port = 7788;
tcp_src_port = get_random_port(ethernet, 6534);
test_client = ether_tcp_create_client(ethernet, (uint8_t*)network_hardware, tcp_src_port, tcp_dest_port, destination_ip);
ether_tcp_connect(ethernet, (uint8_t*)network_hardware, test_client);
tcp_control(test_client, TCP_READ_NONBLOCK);
app_state = APP_WRITE;
break;
case APP_READ:
tcp_retval = 0;
console_print(my_console, "Read State \n");
ether_tcp_read_data(ethernet, (uint8_t*)network_hardware, test_client, tcp_data, 50);
app_state = APP_WRITE;
console_print(my_console,tcp_data);
console_print(my_console, "\n");
if(count > 100)
{
console_print(my_console,"Connection Closed");
ether_tcp_close(ethernet, (uint8_t*)network_hardware, test_client);
loop = 0;
count = 0;
}
if(test_client->client_flags.connect_established == 0)
{
loop = 0;
}
break;
case APP_WRITE:
console_print(my_console, "Write State \n");
#if 1
input_length = 0;
input_length = console_get_string(my_console, MAX_INPUT_SIZE);
if(input_length)
{
tcp_retval = ether_tcp_send_data(ethernet, (uint8_t*)network_hardware, test_client, serial_buffer, input_length);
}
#else
tcp_retval = ether_tcp_send_data(ethernet, (uint8_t*)network_hardware, test_client, "hey", 3);
#endif
if(tcp_retval > 0)
count++;
app_state = APP_READ;
break;
}
}
#endif
#if MQTT_TEST
/* Test MQTT application */
uint16_t tcp_src_port = 0;
uint16_t tcp_dest_port = 0;
int16_t input_length = 0;
uint8_t destination_ip[4] = {0};
tcp_handle_t *test_client;
mqtt_client_t publisher;
/* Network API related variable initializations */
char message[200] = {0};
char read_buffer[150] = {0};
/* Client State machine related variable initializations */
size_t message_length = 0;
int8_t message_status = 0;
uint8_t loop_state = 0;
uint8_t mqtt_message_state = 0;
/* MQTT message buffers */
char *my_client_name = "Sender|1990-adityamall";
char user_name[] = "device1.sensor";
char pass_word[] = "4321";
char publish_topic[] = "device1/temp";
char publish_message[20] = "hello ";
char copy_publish_message[20] = {0};
uint32_t count = 0;
char count_buff[4] = {0};
/* Configure/Connect to MQTT broker */
set_ip_address(destination_ip, "192.168.1.196");
tcp_dest_port = 1883;
tcp_src_port = get_random_port(ethernet, 6534);
/* Create TCP object/socket */
test_client = ether_tcp_create_client(ethernet, (uint8_t*)network_hardware, tcp_src_port, tcp_dest_port, destination_ip);
/*Connect to MQTT broker */
ether_tcp_connect(ethernet, (uint8_t*)network_hardware, test_client);
/* Configure TCP Network IO control */
tcp_control(test_client, TCP_READ_NONBLOCK);
/* MQTT State machine initializations */
loop_state = FSM_RUN;
/* Update state to connect to send connect message */
mqtt_message_state = mqtt_connect_state;
while(loop_state)
{
switch(mqtt_message_state)
{
case mqtt_idle_state:
break;
case mqtt_read_state:
console_print(my_console, "READ \n");
memset(read_buffer, 0, sizeof(read_buffer));
while(ether_tcp_read_data(ethernet, (uint8_t*)network_hardware, test_client, read_buffer, 150) < 0);
publisher.message = (void*)read_buffer;
/* get MQTT message type and update state */
mqtt_message_state = get_mqtt_message_type(&publisher);
if(!mqtt_message_state)
{
mqtt_message_state = mqtt_disconnect_state;
}
break;
case mqtt_connect_state:
console_print(my_console, "CONNECT \n");
/* Test connect message */
memset(message, '\0', sizeof(message));
publisher.connect_msg = (void*)message;
/* Setup User name password (optional) */
mqtt_client_username_passwd(&publisher, user_name, pass_word);
/* Set connect options */
mqtt_connect_options(&publisher, MQTT_CLEAN_SESSION, MQTT_MESSAGE_NO_RETAIN, MQTT_QOS_FIRE_FORGET);
/* Setup MQTT CONNECT Message */
message_length = mqtt_connect(&publisher, my_client_name, 6000);
/* Send connect message */
ether_tcp_send_data(ethernet, (uint8_t*)network_hardware, test_client, (char*)publisher.connect_msg, message_length);
/* Update state */
mqtt_message_state = mqtt_read_state;
break;
case mqtt_connack_state:
console_print(my_console, "CONNACK \n");
/* Check return code of CONNACK message */
publisher.connack_msg = (void *)read_buffer;
mqtt_message_state = get_connack_status(&publisher);
if(!mqtt_message_state)
mqtt_message_state = mqtt_publish_state;
break;
case mqtt_publish_state:
console_print(my_console, "PUBLISH \n");
/* Fill MQTT PUBLISH message structure */
memset(message, '\0', sizeof(message));
publisher.publish_msg = (void *)message;
/*Configure publish options */
mqtt_publish_options(&publisher, MQTT_MESSAGE_NO_RETAIN, MQTT_QOS_FIRE_FORGET);
#if 0
input_length = console_get_string(my_console, MAX_INPUT_SIZE);
if(strcmp(serial_buffer,"exit") == 0 )
{
mqtt_message_state = mqtt_disconnect_state;
break;
}
if(input_length)
{
/* Configure publish message */
message_length = mqtt_publish(&publisher, "device1/temp", serial_buffer, input_length);
/* Send publish message */
ether_tcp_send_data(ethernet, (uint8_t*)network_hardware, test_client, (char*)publisher.publish_msg, message_length);
}
#else
count++;
ltoa(count, count_buff);
console_print(my_console, count_buff);
console_print(my_console, "\n");
strncpy(copy_publish_message, publish_message, strlen(publish_message));
strncat(copy_publish_message, count_buff, strlen(count_buff));
input_length = strlen(copy_publish_message);
/* Configure publish message */
message_length = mqtt_publish(&publisher, publish_topic, copy_publish_message, input_length);
/* Send publish message */
ether_tcp_send_data(ethernet, (uint8_t*)network_hardware, test_client, (char*)publisher.publish_msg, message_length);
memset(copy_publish_message, NULL, strlen(copy_publish_message));
memset(count_buff, NULL, strlen(count_buff));
if(count >= 10)
{
mqtt_message_state = mqtt_disconnect_state;
break;
}
#endif
mqtt_message_state = mqtt_publish_state;
break;
case mqtt_disconnect_state:
console_print(my_console, "DISCONNECT \n");
/* Fill DISCONNECT structure */
memset(message,'\0',sizeof(message));
publisher.disconnect_msg = (void*)message;
message_length = mqtt_disconnect(&publisher);
/* Send Disconnect Message */
ether_tcp_send_data(ethernet, (uint8_t*)network_hardware, test_client, (char*)publisher.disconnect_msg, message_length);
/* Update State */
mqtt_message_state = mqtt_exit_state;
break;
case mqtt_exit_state:
/* Close the client */
ether_tcp_close(ethernet, (uint8_t*)network_hardware, test_client);
/* Suspend while loop */
loop_state = FSM_SUSPEND;
break;
default:
break;
}
}
/* Hold loop */
loop = 1;
while(loop);
#endif
return 0;
}