-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1668 lines (1494 loc) · 56.4 KB
/
main.cpp
File metadata and controls
1668 lines (1494 loc) · 56.4 KB
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
// Contributors:
// Aditya Bhatia - htxbhatia@ucdavis.edu
// Hanna Shui - hshui@ucdavis.edu
//*****************************************************************************
//
// Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// 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
// OWNER 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.
//
//*****************************************************************************
//*****************************************************************************
//
// Application Name - SSL Demo
// Application Overview - This is a sample application demonstrating the
// use of secure sockets on a CC3200 device.The
// application connects to an AP and
// tries to establish a secure connection to the
// Google server.
// Application Details -
// docs\examples\CC32xx_SSL_Demo_Application.pdf
// or
// http://processors.wiki.ti.com/index.php/CC32xx_SSL_Demo_Application
//
//*****************************************************************************
//*****************************************************************************
//
//! \addtogroup ssl
//! @{
//
//*****************************************************************************
// Contributors:
// Aditya Bhatia - htxbhatia@ucdavis.edu
// Hanna Shui - hshui@ucdavis.edu
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
// Simplelink includes
#include "simplelink.h"
// Driverlib includes
#include "hw_types.h"
#include "interrupt.h"
#include "hw_ints.h"
#include "hw_apps_rcm.h"
#include "hw_common_reg.h"
#include "prcm.h"
#include "rom.h"
#include "rom_map.h"
#include "hw_memmap.h"
#include "hw_nvic.h"
#include "timer.h"
#include "gpio.h"
#include "utils.h"
#include "uart.h"
#include "systick.h"
#include "spi.h"
//Common interface includes
#include "pinmux.h"
#include "gpio_if.h"
#include "common.h"
#include "uart_if.h"
#include "timer_if.h"
// Custom includes
#include "utils/network_utils.h"
#include "pitches.h"
// Includes Adafruit OLED and Fingerprint
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1351.h"
#include "glcdfont.h"
#include "oled_test.h"
#include "Adafruit_Fingerprint.h"
//NEED TO UPDATE THIS FOR IT TO WORK!
#define DATE 4 /* Current Date */
#define MONTH 6 /* Month 1-12 */
#define YEAR 2025 /* Current year */
#define HOUR 15 /* Time - hours */
#define MINUTE 36 /* Time - minutes */
#define SECOND 0 /* Time - seconds */
#define APPLICATION_NAME "SSL"
#define APPLICATION_VERSION "SQ25"
#define SERVER_NAME "a2oipe2vzqi2uq-ats.iot.us-east-1.amazonaws.com" // CHANGE ME
#define GOOGLE_DST_PORT 8443
#define POSTHEADER "POST /things/Aditya_CC3200_Board/shadow HTTP/1.1\r\n" // CHANGE ME
#define GETHEADER "GET /things/Aditya_CC3200_Board/shadow HTTP/1.1\r\n"
#define HOSTHEADER "Host: a2oipe2vzqi2uq-ats.iot.us-east-1.amazonaws.com\r\n" // CHANGE ME
#define CHEADER "Connection: Keep-Alive\r\n"
#define CTHEADER "Content-Type: application/json; charset=utf-8\r\n"
#define CLHEADER1 "Content-Length: "
#define CLHEADER2 "\r\n\r\n"
#define DATA1 "{" \
"\"state\": {\r\n" \
"\"desired\" : {\r\n" \
"\"Message\" : {\r\n" \
"\"default\" : \"" \
"default " \
"message from CC3200 via AWS IoT!" \
"\",\r\n" \
"\"email\" : \"" \
#define DATA2 "\"\r\n" \
"}" \
"}" \
"}" \
"}\r\n\r\n"
//****************************************************************************
// LOCAL FUNCTION PROTOTYPES
//****************************************************************************
static int set_time();
static void BoardInit(void);
static int http_post(int, int);
static int http_get(int);
//*****************************************************************************
//
// Application Master/Slave mode selector macro
//
// MASTER_MODE = 1 : Application in master mode
// MASTER_MODE = 0 : Application in slave mode
//
//*****************************************************************************
#define MASTER_MODE 1
#define SPI_IF_BIT_RATE 100000
#define TR_BUFF_SIZE 100
#define MASTER_MSG "This is CC3200 SPI Master Application\n\r"
#define SLAVE_MSG "This is CC3200 SPI Slave Application\n\r"
// some helpful macros for sysTick
#define SYSCLKFREQ 80000000ULL
#define TICKS_TO_US(ticks) \
((((ticks) / SYSCLKFREQ) * 1000000ULL) + \
((((ticks) % SYSCLKFREQ) * 1000000ULL) / SYSCLKFREQ))\
#define US_TO_TICKS(us) ((SYSCLKFREQ / 1000000ULL) * (us))
// sysTick reload value set to 40ms period
// (PERIOD_SEC) * (SYSCLKFREQ) = PERIOD_TICKS
#define SYSTICK_RELOAD_VAL 3200000UL
#if defined(ccs)
extern void (* const g_pfnVectors[])(void);
#endif
#if defined(ewarm)
extern uVectorEntry __vector_table;
#endif
volatile bool systick_expired = 0;
static inline void SysTickReset(void) {
HWREG(NVIC_ST_CURRENT) = 1;
systick_expired = 0;
}
// pin 03 info for reading IR receiver
#define IR_GPIO_PORT GPIOA1_BASE
#define IR_GPIO_PIN 0x10
// volatile variables for interrupts
volatile uint64_t ulsystick_delta_us = 0;
volatile uint32_t bitsequence = 0;
volatile bool start = 0;
volatile bool readyToPrint = 0;
volatile int bitCount = 0;
// state variable for alarm
// 0 -> Off, 1 -> On, 2 -> Alert, 3 -> Reset password
volatile int alarmState = 0;
// var if in input mode
// 0 -> no input, 1 -> enable or password
volatile int inputMode = 0;
// 0 -> no fingerprint, 1 -> enable or fingerprint
volatile int fpMode = 0;
// var to print new screen in main while loop
volatile bool print = false;
// var to check if motion detected
volatile bool motion = false;
/**
* Interrupt handler for GPIOA1 port
*
* Only used here for decoding IR transmissions
*/
static void GPIOA1IntHandler(void) {
static bool prev_state = 1;
// get and clear status
unsigned long ulStatus;
ulStatus = MAP_GPIOIntStatus(IR_GPIO_PORT, true);
MAP_GPIOIntClear(IR_GPIO_PORT, ulStatus);
// check in interrupt occurred on pin 03
if (ulStatus & IR_GPIO_PIN) {
if (prev_state) {
// previous state was high -> falling edge
if (!systick_expired) {
// if sysTick expired, the pulse was longer than 40ms
// don't measure it in that case
// calculate the pulse width
ulsystick_delta_us = TICKS_TO_US(SYSTICK_RELOAD_VAL - MAP_SysTickValueGet());
// if pulse between 4400 and 4500 us -> start bit -> start flag true, reset bit sequence
if (ulsystick_delta_us > 4400 && ulsystick_delta_us < 4500) { start = 1; bitsequence = 0;}
// if pulse b/w 500 and 600 us -> 0 bit
if (start && ulsystick_delta_us > 500 && ulsystick_delta_us < 600) {
// simply increment bit count
bitCount++;
}
// if pulse b/w 1500 and 1800 us -> 1 bit
else if (start && ulsystick_delta_us > 1500 && ulsystick_delta_us < 1800) {
// set bit at position 32 - bitCount to 1
bitsequence |= (0x80000000 >> bitCount);
bitCount++;
}
// if very long pulse -> sequence over
else if (start && ulsystick_delta_us > 39000 && ulsystick_delta_us < 40000) {
// start is false, bitsequence is ready to read, reset bit count
start = 0; readyToPrint = 1; bitCount = 0;
}
// reset time delta
ulsystick_delta_us = 0;
}
} else {
// previous state was low -> rising edge
// begin measuring a new pulse width, reset the delta and sysTick
ulsystick_delta_us = 0;
SysTickReset();
}
// store prev state, high or low
prev_state = MAP_GPIOPinRead(IR_GPIO_PORT, IR_GPIO_PIN) ? 1 : 0;
}
return;
}
/**
* SysTick Interrupt Handler
*
* Keep track of whether the sysTick counter expired
*/
static void SysTickHandler(void) {
systick_expired = 1;
}
// decoding information
uint32_t codes[] = {0x20df08f7, 0x20df8877, 0x20df48b7, 0x20dfc837, 0x20df28d7, 0x20dfa857, 0x20df6897, 0x20dfe817, 0x20df18e7, 0x20df9867, 0x20df906f, 0x20df58a7};
char buttons[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'm', 'l'};
// password for disarming alarm
// 7 characters long due to weird behavior on character 0
char password[8] = "0123456";
// set max length of message to 6
#define MAX_MSG_LEN 6
// resets bottom half of screen with password input prompt
void resetBottom() {
// Clear bottom half
fillRect(0, 65, 128, 63, BLACK);
char text[9] = "Password";
int i;
for (i = 0; i < 8; i++) {
drawChar(i * 6, 66, text[i], WHITE, BLACK, 1);
}
// print '>' (62 in ascii)
drawChar(48, 66, 62, WHITE, BLACK, 1);
// print '_' (95 in ascii) at current character position
drawChar(54, 66, 95, WHITE, BLACK, 1);
}
// initialises SPI for OLED
void SPIInit(){
// Reset SPI
MAP_SPIReset(GSPI_BASE);
//Enables the transmit and/or receive FIFOs.
//Base address is GSPI_BASE, SPI_TX_FIFO || SPI_RX_FIFO are the FIFOs to be enabled
MAP_SPIFIFOEnable(GSPI_BASE, SPI_TX_FIFO || SPI_RX_FIFO);
// Configure SPI interface
MAP_SPIConfigSetExpClk(GSPI_BASE,MAP_PRCMPeripheralClockGet(PRCM_GSPI),
SPI_IF_BIT_RATE,SPI_MODE_MASTER,SPI_SUB_MODE_0,
(SPI_SW_CTRL_CS |
SPI_4PIN_MODE |
SPI_TURBO_OFF |
SPI_CS_ACTIVELOW |
SPI_WL_8));
// Enable SPI for communication
MAP_SPIEnable(GSPI_BASE);
}
// initialisations for input messages
char inputBuffer[MAX_MSG_LEN] = {0};
int inputIndex = 0;
// starting pos for bottom half
int x_pos = 54; int y_pos = 66;
// return value for http post
long lRetVal = -1;
// password attempts
int passwordAttempts = 3;
// incorrect password flag
bool incorrect = false;
// success password flag
bool success = false;
// fingerprint status
// -1 -> error, 0 -> nothing, 1 -> success, 2 -> no match
int fpSuccess = 0;
// confirm password flag to allow for second password input
bool confirm = false;
// id stage flag (choosing between resetting password or adding new fingerprint)
bool idStage = true;
// password attempt
char attempt[8] = "0000000";
// fingerprint sensor communicating over UART1
Adafruit_Fingerprint finger = Adafruit_Fingerprint(UARTA1_BASE);
// id for fingerprint
uint8_t id;
// draws char being typed in bottom half of screen
void displayChar(char c) {
if (y_pos >= 115) return; // don't draw if end of screen
drawChar(x_pos, y_pos, c, WHITE, BLACK, 1);
x_pos += 6; // 6 pixels per character (5 pixels wide + 1 spacing)
if (x_pos >= 120) { // wrap to next line
x_pos = 0;
y_pos += 8; // 8 pixel height per line
if (y_pos >= 115) return; // stop drawing if end of screen
}
// print '_' (95 in ascii) at current character position
drawChar(x_pos, y_pos, 95, WHITE, BLACK, 1);
}
// draws char being typed in bottom half of screen
void addCharToInput(char c) {
// set curr index to char and following index to '\0'
if (inputIndex < MAX_MSG_LEN) {
inputBuffer[inputIndex++] = c;
inputBuffer[inputIndex] = '\0';
displayChar(c);
}
}
// deletes last char
void deleteLastChar(bool changeIndex) {
if (inputIndex > 0) {
// only decrement index when deleting, else it simply replaces the character at the current spot
if (changeIndex) { inputBuffer[--inputIndex] = '\0'; }
// draw a black '_' (95 in ascii) to clear '_'
drawChar(x_pos, y_pos, 95, BLACK, BLACK, 1);
// check if we're at the start of a row with no written characters
if (y_pos >= 74 && x_pos < 6) {
// go back to the last character of the previous row
y_pos -= 8; x_pos = 120;
} else {
x_pos -= 6;
}
// draw a black ' ' (32 in ascii) to clear a character
drawChar(x_pos, y_pos, 32, BLACK, BLACK, 1);
// redraw '_'
drawChar(x_pos, y_pos, 95, WHITE, BLACK, 1);
}
}
// print password incorrect message
void incorrectMessage(int attemptsLeft) {
char text[11] = "Incorrect!";
int i;
for (i = 0; i < 10; i++) {
drawChar(i * 6, 80, text[i], RED, BLACK, 1);
}
// if not on last attempt or on first attempt, print number of attempts left
if (attemptsLeft != 3) {
drawChar(0, 90, '0' + attemptsLeft, RED, BLACK, 1);
char text2[15] = " Attempts Left";
int j;
for (j = 0; j < 14; j++) {
drawChar(6 + j * 6, 90, text2[j], RED, BLACK, 1);
}
}
}
// print password/fingerprint success message
void successMessage() {
char text[9] = "Success!";
int i;
for (i = 0; i < 8; i++) {
drawChar(i * 6, 80, text[i], GREEN, BLACK, 1);
}
}
// custom string comparison function
// returns 0 if strings are the same, 1 if not
// string 2 needs a blank character at the start due to weird behavior on character 0
int strcomp(const char* input, const char* string2) {
int len = strlen(input);
if (len != 6) { return 1; }
int i;
for (i = 0; i < 6; i++) {
if (input[i] != string2[i + 1]) { return 1; }
}
return 0;
}
// prints 6 digits only message
void sixDigits() {
char text[14] = "6 Digits Only";
int i;
for (i = 0; i < 13; i++) {
drawChar(i * 6, 90, text[i], RED, BLACK, 1);
}
}
// prints 1 digit only message
void oneDigit() {
char text[19] = "1 Digit (1-9) Only";
int i;
for (i = 0; i < 18; i++) {
drawChar(i * 6, 90, text[i], RED, BLACK, 1);
}
}
// prints password/fingerprint mismatch message
void mismatchMessage() {
char text[10] = "Mismatch!";
int i;
for (i = 0; i < 9; i++) {
drawChar(i * 6, 90, text[i], RED, BLACK, 1);
}
char text2[13] = "SW3 to Reset";
int j;
for (j = 0; j < 12; j++) {
drawChar(j * 6, 100, text2[j], RED, BLACK, 1);
}
inputIndex = 0;
inputBuffer[0] = '\0';
x_pos = 54; y_pos = 66;
}
// prints fingerprint error message
void fingerprintError() {
char text[18] = "Fingerprint Error";
int i;
for (i = 0; i < 17; i++) {
drawChar(i * 6, 90, text[i], RED, BLACK, 1);
}
}
// processes button presses
void processButton(char btn) {
// if message has reached max length, forcefully trigger a submit
if (inputIndex == MAX_MSG_LEN) { btn = 'm'; }
switch (btn) {
// if button is 'Mute', handle alarm state changes or character input
case 'm':
// if alarm is off, handle input mode changes
if (alarmState == 0) {
// if not in input mode, set to input mode and print
if (inputMode == 0) { inputMode = 1; print = true; return;}
// if in input mode, set to alarm on and print
else if (inputMode == 1) { inputMode = 0; alarmState = 1; print = true; return;}
}
// if alarm is on, handle input mode changes
else if (alarmState == 1) {
// if not in input mode, set to input mode and print
if (inputMode == 0) { inputMode = 1; print = true; return;}
// if in input mode, check if password is correct or fingerprint is successful
else if (inputMode == 1) {
if ((strcomp(inputBuffer, password) == 0) || (fpSuccess == 1)) {
// if password is correct or fingerprint is successful, set alarm to off, input mode to off, print success message
// reset password attempts, set success flag, reset fingerprint success flag
alarmState = 0; inputMode = 0; print = true; incorrect = false; passwordAttempts = 3; success = true;
fpSuccess = 0;
}
// if fingerprint is not in error state, handle password/fingerprint attempts
else if (fpSuccess != -1) {
// if password/fingerprint is incorrect, set incorrect flag, decrement password attempts, print false, set fp mode to 1
incorrect = true; passwordAttempts--; print = false; fpMode = 1;
// if no attempts left, set alarm to triggered, reset incorrect flag, reset password attempts, print true, set fp mode to 0
if (passwordAttempts == 0) {
alarmState = 2; incorrect = false; passwordAttempts = 3; print = true; fpMode = 0;
}
}
// if fingerprint is in error state, print error message and set fp mode to 1
else {
fingerprintError(); fpMode = 1;
}
}
}
// if alarm is triggered, handle password/fingerprint attempts
else if (alarmState == 2) {
// if password/fingerprint is correct, set alarm to off, input mode to off, print success message
// reset password attempts, set success flag, reset fingerprint success flag
if ((strcomp(inputBuffer, password) == 0) || (fpSuccess == 1)) {
alarmState = 0; inputMode = 0; print = true; incorrect = false; passwordAttempts = 3; success = true;
fpSuccess = 0;
}
// if fingerprint is not in error state, handle password/fingerprint attempts
else if (fpSuccess != -1) {
// if password/fingerprint is incorrect, set incorrect flag, decrement password attempts, print false, set fp mode to 1
incorrect = true; print = false; fpMode = 1;
// if no attempts left, set alarm to triggered, reset incorrect flag, reset password attempts, print true, set fp mode to 0
if (passwordAttempts == 0) {
alarmState = 2; incorrect = false; passwordAttempts = 3; print = true; fpMode = 0;
}
}
// if fingerprint is in error state, print error message and set fp mode to 1
else {
fingerprintError(); fpMode = 1;
}
}
// if alarm is in reset password/new fingerprint state, handle id stage or password input
else if (alarmState == 3) {
// if id stage, handle id input
if (idStage) {
// if not a single digit, print error message
if (inputIndex != 1) {
oneDigit(); return;
}
// if a single digit, set id to the digit and set fp mode to 0 if 0, 2 if 1-9
else {
id = inputBuffer[0] - '0';
if (id == 0) { fpMode = 0; } // reset password
else { fpMode = 2; } // new fingerprint
idStage = false; print = true;
}
}
// if not id stage, handle new password input
else if (inputIndex != MAX_MSG_LEN) {
// if not 6 digits, print error message
sixDigits(); return;
}
// if 6 digits, handle password input
else {
// if not confirming password, set attempt to input buffer and print
if (!confirm) {
int i;
for (i = 0; i < 6; i++) {
attempt[i + 1] = inputBuffer[i];
}
print = true; confirm = true;
}
// if confirming password, check if password is correct
else {
// if password is correct, set success flag, set input mode to off, set alarm to off, print success message, reset confirm flag
// set password to input buffer, send http post with state 3
if (strcomp(inputBuffer, attempt) == 0) {
success = true; inputMode = 0; alarmState = 0; print = true; confirm = false;
int i;
for (i = 0; i < 6; i++) {
password[i + 1] = inputBuffer[i];
}
http_post(lRetVal, 3);
}
// if password is incorrect, print mismatch message, set input mode to off, set alarm to off, reset confirm flag
// this forces user to press SW3 to reset back to this state
else {
mismatchMessage(); inputMode = 0; alarmState = 0; confirm = false;
return;
}
}
}
}
// reset input index, input buffer
inputIndex = 0;
inputBuffer[0] = '\0';
// if incorrect, print incorrect message
if (incorrect) {
incorrectMessage(passwordAttempts);
}
// if not success or confirming password, reset bottom half of screen
if (!success && !confirm) { resetBottom(); }
// if success, print success message
if (success) {
success = false;
successMessage();
}
// reset x and y positions for next input
x_pos = 54; y_pos = 66;
break;
// if 'last' delete character
case 'l':
// if in input mode, delete last character
if (inputMode == 1) { deleteLastChar(true); }
break;
// if any number, add the number to input buffer
default:
// if in input mode, add character to input buffer
if (inputMode == 1) { addCharToInput(btn); }
break;
}
}
// sets the OLED to the alarm off state
// sets alarm state to off, input mode to off, turns off red LED
// fills screen black, prints "Alarm Off" in green, prints "To Enable, Press m" in white
void printAlarmOffScreen() {
alarmState = 0; inputMode = 0;
GPIO_IF_LedOff(MCU_RED_LED_GPIO);
fillScreen(BLACK);
char text[10] = "Alarm Off";
int i;
for (i = 0; i < 9; i++) {
drawChar(10 + i * 12, 20, text[i], GREEN, BLACK, 2);
}
char enable[19] = "To Enable, Press m";
int j;
for (j = 0; j < 18; j++) {
drawChar(j * 6, 45, enable[j], WHITE, BLACK, 1);
}
}
// prints alarm enable options when in off state
// clears bottom half of screen, prints "Enable?" in white, prints "Yes(m) No(SW3)" in white
void printEnableOptions() {
// Clear bottom half
fillRect(0, 65, 128, 63, BLACK);
char text[8] = "Enable?";
int i;
for (i = 0; i < 7; i++) {
drawChar(10 + i * 8, 65, text[i], WHITE, BLACK, 1);
}
char options[19] = "Yes(m) No(SW3)";
int j;
for (j = 0; j < 17; j++) {
drawChar(10 + j * 6, 75, options[j], WHITE, BLACK, 1);
}
}
// sets the OLED to the alarm on state
// sets alarm state to on, input mode to off, turns on red LED
// fills screen black, prints "Alarm On" in red, prints "To Disable, Press m" in white
void printAlarmOnScreen() {
alarmState = 1; inputMode = 0;
GPIO_IF_LedOn(MCU_RED_LED_GPIO);
fillScreen(BLACK);
char text[9] = "Alarm On";
int i;
for (i = 0; i < 8; i++) {
drawChar(16 + i * 12, 20, text[i], RED, BLACK, 2);
}
char enable[20] = "To Disable, Press m";
int j;
for (j = 0; j < 19; j++) {
drawChar(j * 6, 45, enable[j], WHITE, BLACK, 1);
}
}
// sets the OLED to the alarm triggered state
// sets alarm state to triggered, input mode to 1
// fills screen black, prints "Triggered!" in red
void printAlarmTriggeredScreen() {
alarmState = 2; inputMode = 1;
fillScreen(BLACK);
char text[11] = "Triggered!";
int i;
for (i = 0; i < 10; i++) {
drawChar(4 + i * 12, 20, text[i], RED, BLACK, 2);
}
}
// prints id stage
// prints "0:PSWD 1-9:NFP" in white, prints ">" in white, prints "_" in white
void drawIdStage() {
char text[15] = "0:PSWD 1-9:NFP";
int i;
for (i = 0; i < 14; i++) {
drawChar(i * 6, 66, text[i], WHITE, BLACK, 1);
}
// print '>' (62 in ascii)
drawChar(84, 66, 62, WHITE, BLACK, 1);
// print '_' (95 in ascii) at current character position
drawChar(90, 66, 95, WHITE, BLACK, 1);
x_pos = 90;
}
// prints confirm stage
// prints "Confirm New" in white
void drawConfirm() {
char text[12] = "Confirm New";
int i;
for (i = 0; i < 11; i++) {
drawChar(i * 6, 80, text[i], WHITE, BLACK, 1);
}
}
// prints new password stage
// prints "New Password" in white
void drawNewPass() {
char text[13] = "New Password";
int i;
for (i = 0; i < 12; i++) {
drawChar(i * 6, 80, text[i], WHITE, BLACK, 1);
}
}
// params for buzzer
#define TIMER_INTERVAL_RELOAD 40035 /* =(255*157) */
#define DUTYCYCLE_GRANULARITY 157
//*****************************************************************************
// GLOBAL VARIABLES -- Start
//*****************************************************************************
#if defined(ccs) || defined(gcc)
extern void (* const g_pfnVectors[])(void);
#endif
#if defined(ewarm)
extern uVectorEntry __vector_table;
#endif
//*****************************************************************************
// GLOBAL VARIABLES -- End: df
//*****************************************************************************
//****************************************************************************
//
//! Update the duty cycle of the PWM timer
//!
//! \param ulBase is the base address of the timer to be configured
//! \param ulTimer is the timer to be setup (TIMER_A or TIMER_B)
//! \param ucLevel translates to duty cycle settings (0:255)
//!
//! This function
//! 1. The specified timer is setup to operate as PWM
//!
//! \return None.
//
//****************************************************************************
void UpdateDutyCycle(unsigned long ulBase, unsigned long ulTimer,
unsigned char ucLevel)
{
//
// Match value is updated to reflect the new duty cycle settings
//
MAP_TimerMatchSet(ulBase,ulTimer,(ucLevel*DUTYCYCLE_GRANULARITY));
}
//****************************************************************************
//
//! Setup the timer in PWM mode
//!
//! \param ulBase is the base address of the timer to be configured
//! \param ulTimer is the timer to be setup (TIMER_A or TIMER_B)
//! \param ulConfig is the timer configuration setting
//! \param ucInvert is to select the inversion of the output
//!
//! This function
//! 1. The specified timer is setup to operate as PWM
//!
//! \return None.
//
//****************************************************************************
void SetupTimerPWMMode(unsigned long ulBase, unsigned long ulTimer,
unsigned long ulConfig, unsigned char ucInvert)
{
//
// Set GPT - Configured Timer in PWM mode.
//
MAP_TimerConfigure(ulBase,ulConfig);
MAP_TimerPrescaleSet(ulBase,ulTimer,0);
//
// Inverting the timer output if required
//
MAP_TimerControlLevel(ulBase,ulTimer,ucInvert);
//
// Load value set to ~0.5 ms time period
//
MAP_TimerLoadSet(ulBase,ulTimer,TIMER_INTERVAL_RELOAD);
//
// Match value set so as to output level 0
//
MAP_TimerMatchSet(ulBase,ulTimer,TIMER_INTERVAL_RELOAD);
}
//****************************************************************************
//
//! Sets up the identified timers as PWM to drive the peripherals
//!
//! \param none
//!
//! This function sets up the following
//! 1. TIMERA0 as Buzzer
//!
//! \return None.
//
//****************************************************************************
void InitPWMModules()
{
//
// Initialization of timers to generate PWM output
//
MAP_PRCMPeripheralClkEnable(PRCM_TIMERA0, PRCM_RUN_MODE_CLK);
//
// TIMERA0 as Buzzer
//
SetupTimerPWMMode(TIMERA0_BASE, TIMER_A, (TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM), 1);
MAP_TimerEnable(TIMERA0_BASE,TIMER_A);
}
//****************************************************************************
//
//! Disables the timer PWMs
//!
//! \param none
//!
//! This function disables the timers used
//!
//! \return None.
//
//****************************************************************************
void DeInitPWMModules()
{
//
// Disable the peripherals
//
MAP_TimerDisable(TIMERA0_BASE, TIMER_A);
MAP_PRCMPeripheralClkDisable(PRCM_TIMERA0, PRCM_RUN_MODE_CLK);
}
//*****************************************************************************
//
//! Board Initialization & Configuration
//!
//! \param None
//!
//! \return None
//
//*****************************************************************************
static void BoardInit(void) {
/* In case of TI-RTOS vector table is initialize by OS itself */
#ifndef USE_TIRTOS
//
// Set vector table base
//
#if defined(ccs)
MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
#endif
#if defined(ewarm)
MAP_IntVTableBaseSet((unsigned long)&__vector_table);
#endif
#endif
//
// Enable Processor
//
MAP_IntMasterEnable();
MAP_IntEnable(FAULT_SYSTICK);
PRCMCC3200MCUInit();
}
//*****************************************************************************
//
//! This function updates the date and time of CC3200.
//!
//! \param None
//!
//! \return
//! 0 for success, negative otherwise
//!
//*****************************************************************************
static int set_time() {
long retVal;
g_time.tm_day = DATE;
g_time.tm_mon = MONTH;
g_time.tm_year = YEAR;
g_time.tm_sec = SECOND;
g_time.tm_hour = HOUR;
g_time.tm_min = MINUTE;
retVal = sl_DevSet(SL_DEVICE_GENERAL_CONFIGURATION,
SL_DEVICE_GENERAL_CONFIGURATION_DATE_TIME,
sizeof(SlDateTime),(unsigned char *)(&g_time));
ASSERT_ON_ERROR(retVal);
return SUCCESS;
}
// initialises fingerprint sensor
// prints "Found fingerprint sensor!" if successful
// prints "Did not find fingerprint sensor :(" if unsuccessful
// sets fp mode to mode
void beginFingerprint(int mode) {
finger.begin();
if (finger.verifyPassword()) {
Report("Found fingerprint sensor!\n\r");
finger.LEDcontrol(true);
fpMode = mode;
}
else {
Report("Did not find fingerprint sensor :(\n\r");
while (1);
}
}
// enrolls fingerprint
// prints "Waiting for valid finger to enroll as #id"
// prints "Image taken" if successful
// prints "." if no finger detected
// prints "Imaging error" if imaging error
uint8_t getFingerprintEnroll() {
int p = -1;
Report("Waiting for valid finger to enroll as #%d\n\r", id);
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Report("Image taken\n\r");
break;
case FINGERPRINT_NOFINGER:
Report(".\n\r");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Report("Communication error\n\r");
break;
case FINGERPRINT_IMAGEFAIL: