-
Notifications
You must be signed in to change notification settings - Fork 2
/
emulator.c
192 lines (154 loc) · 4.82 KB
/
emulator.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define BUFFER_SIZE 1024
// Function prototypes
void profileDownload();
void installProfile();
void disableProfile();
void exitEmulator();
void start_process(const char *program, int pipefd[2]);
void read_from_pipe(int pipefd[2], const char *process_name);
int main() {
int choice;
// Display the menu
printf("\nWelcome to eSIM Emulator:\n");
printf("1. Profile Download\n");
printf("2. Install Profile\n");
printf("3. Delete Profile\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
// Handle the user's choice
switch (choice) {
case 1:
profileDownload();
break;
case 2:
installProfile();
break;
case 3:
disableProfile();
break;
case 4:
exitEmulator();
break;
default:
printf("Invalid choice, please try again.\n");
}
return 0;
}
void profileDownload() {
// Message to user
printf("\n->Initiating profile download procedure\n");
printf("->Establishing Secure Connection between IPAd and eIM \n");
int server_pipe[2];
int client_pipe[2];
// Create pipes
if (pipe(server_pipe) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
if (pipe(client_pipe) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
// Start the server process
printf("\nStarting eIM as server...\n");
start_process("./architecture/eIM", server_pipe);
// Give the server some time to start up and listen
sleep(2);
// Start the client process
printf("\nStarting IPAd as client...\n");
start_process("./architecture/IoTDevice/IPAd", client_pipe);
// Read and print the outputs from both processes
read_from_pipe(server_pipe, "Server");
read_from_pipe(client_pipe, "Client");
// Message to user
printf("\n->Re-establishing Secure Connection between IPAd and SM-DPPlus\n");
// Recreate pipes for the next pair of processes
if (pipe(server_pipe) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
if (pipe(client_pipe) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
// Start the SM-DPPlus process
printf("\nStarting SM-DPPlus as server...\n");
start_process("./architecture/SM-DPPlus", server_pipe);
// Give the server some time to start up and listen
sleep(2);
// Start the client process again
printf("\nStarting IPAd as client...\n");
start_process("./architecture/IoTDevice/IPAd", client_pipe);
// Read and print the outputs from both processes
read_from_pipe(server_pipe, "Server");
read_from_pipe(client_pipe, "Client");
}
void installProfile() {
printf("Installing profile \n");
char input_data[] = "TS48 V3.0 eSIM_GTP_SAIP2.1_BERTLV.txt\n";
char command[256];
sprintf(command, "echo \"%s\" | python architecture/IoTDevice/eUICC/profileInstaller.py", input_data);
int result = system(command);
if (result == 0) {
printf("Installation executed successfully.\n");
} else {
printf("Error executing installation script.\n");
}
}
void disableProfile() {
printf("You have entered Mode 3.\n");
// Add more functionality here for Mode 3
}
void exitEmulator() {
printf("Exiting the emulator. Goodbye!\n");
exit(0); // Exit the program
}
void start_process(const char *program, int pipefd[2]) {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
// Close the read end of the pipe
close(pipefd[0]);
// Redirect stdout to the write end of the pipe
if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
// Close the write end of the pipe (it's duplicated now)
close(pipefd[1]);
// Execute the program
execl(program, program, (char *)NULL);
// If execl fails
perror("execl");
exit(EXIT_FAILURE);
}
}
void read_from_pipe(int pipefd[2], const char *process_name) {
// Close the write end of the pipe
close(pipefd[1]);
char buffer[BUFFER_SIZE];
ssize_t bytesRead;
// Read the output of the process from the pipe
while ((bytesRead = read(pipefd[0], buffer, sizeof(buffer))) > 0) {
fwrite(buffer, sizeof(char), bytesRead, stdout);
}
// Close the read end of the pipe
close(pipefd[0]);
// Wait for the process to finish
int status;
wait(&status);
if (WIFEXITED(status)) {
printf("%s exited with status %d\n", process_name, WEXITSTATUS(status));
} else {
printf("%s did not exit normally\n", process_name);
}
}