-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.c
199 lines (170 loc) · 6.34 KB
/
interface.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
// ========================================================================================================
// ========================================================================================================
// ********************************************** interface.c *********************************************
// ========================================================================================================
// ========================================================================================================
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "interface.h"
#include <poll.h>
extern int usleep (__useconds_t __useconds);
// =============================================================================================================
void show_account(int balance, int pending)
{
printf("Current balance %d\tPending %d\n", balance, pending);
fflush(stdout);
return;
}
// =============================================================================================================
int get_withdraw()
{
int amt;
printf("Enter withdraw amount as xxx in cents\n");
scanf("%d", &amt);
return amt;
}
// =============================================================================================================
void withdraw_fail()
{
printf("FAILED TO WITHDRAW\n");
printf("\tReturning to the main menu.\n\n");
}
// =============================================================================================================
void withdraw_success(unsigned int amount)
{
printf("WITHDRAW SUCCESS\n");
int cents = amount % 100;
int dollars = amount / 100;
printf("\tWithdrew $%d.%02d\n", dollars, cents);
printf("\tReturning to the main menu.\n\n");
}
// =============================================================================================================
struct transfer get_transfer()
{
struct transfer request;
int amt, id;
printf("TRANSFER\n");
printf("Enter request amount as xxx in cents\n");
scanf("%d", &amt);
if ( amt == -1 )
{
printf("Transfer canceled\n\n");
request.amount = -1;
return request;
}
printf("Enter Device ID\n");
scanf("%d", &id);
if ( id == -1 )
{
printf("Transfer canceled\n\n");
request.amount = -1;
return request;
}
request.amount = amt;
request.id_to = id;
request.timestamp = time(NULL);
request.type = TRANSFER_SEND;
return request;
}
// =============================================================================================================
void transfer_fail()
{
printf("FAILED TO TRANSFER\n");
printf("\tReturning to the main menu.\n\n");
}
// =============================================================================================================
void transfer_success(struct transfer request)
{
printf("TRANSFER SUCCESS\n");
int cents = request.amount % 100;
int dollars = request.amount / 100;
printf("\tSent $%d.%02d to ID %d\n", dollars, cents, request.id_to);
printf("\tReturning to the main menu.\n\n");
}
// =============================================================================================================
int receive_wait(int firstTime)
{
usleep(500000);
return 0;
}
// =============================================================================================================
void receive_fail()
{
printf("RECEIVE FAILED\n");
}
// =============================================================================================================
void receive_success(struct transfer receipt)
{
printf("RECEIVE SUCCESS\n");
int cents = receipt.amount % 100;
int dollars = receipt.amount / 100;
printf("\tGot $%d.%02d from ID %d\n", dollars, cents, receipt.id_to);
printf("\tReturning to the main menu.\n\n");
}
// =============================================================================================================
int get_deposit()
{
int amt;
printf("Enter deposit amount as xxx in cents, first drawn from pending and then from balance\n");
scanf("%d", &amt);
return amt;
}
// =============================================================================================================
void deposit_fail()
{
printf("FAILED TO DEPOSIT\n");
}
// =============================================================================================================
void deposit_success(unsigned int amount)
{
printf("DEPOSIT SUCCESS\n");
int cents = amount % 100;
int dollars = amount / 100;
printf("\tDeposited $%d.%02d\n", dollars, cents);
printf("\tReturning to the main menu.\n\n");
}
// =============================================================================================================
// =============================================================================================================
int main_menu(int main_menu_blocks, int iteration)
{
struct pollfd mypoll = {STDIN_FILENO, POLLIN|POLLPRI};
int menu_select;
// Using poll, https://stackoverflow.com/questions/21197977/how-can-i-prevent-scanf-to-wait-forever-for-an-input-character
printf("1) WITHDRAW, 2) TRANSFER, 3) RECEIVE, 4) DEPOSIT, 5) ACCOUNT, 6) SEND, 7) RECEIVE, 8) GET ATs (iteration %d)\n", iteration);
if ( poll(&mypoll, 1, 2000) == 1 || main_menu_blocks == 1 )
{
scanf("%d", &menu_select);
}
if ( menu_select == 1 )
return MENU_WITHDRAW;
else if ( menu_select == 2 )
return MENU_TRANSFER;
else if ( menu_select == 3 )
return MENU_RECEIVE;
else if ( menu_select == 4 )
return MENU_DEPOSIT;
else if ( menu_select == 5 )
return MENU_ACCOUNT;
else if ( menu_select == 6 )
return MENU_KEKSEND;
else if ( menu_select == 7 )
return MENU_KEKRECEIVE;
else if ( menu_select == 8 )
return MENU_GET_AT;
else
return MENU_NOOP;
}
// =============================================================================================================
// =============================================================================================================
void load_settings()
{
printf("Load Settings called\n");
return;
}
// =============================================================================================================
void lock_device(int sleep)
{
printf("Lock Device called\n");
return;
}