|
1 | 1 | #include "process.h" |
2 | 2 | #include <stdio.h> |
3 | 3 | #include <stdlib.h> |
4 | | -#include <stdbool.h> |
5 | 4 | #include <unistd.h> |
6 | 5 | #include <string.h> |
7 | 6 | #include <errno.h> |
|
10 | 9 | #include <euicc/es10b.h> |
11 | 10 | #include <euicc/es9p.h> |
12 | 11 |
|
13 | | -static const char *opt_string = "rh?"; |
14 | | - |
15 | | -static int applet_main(int argc, char **argv) |
| 12 | +static int _process_single(uint32_t seqNumber, uint8_t autoremove) |
16 | 13 | { |
17 | | - bool auto_remove = false; |
18 | | - int opt = getopt(argc, argv, opt_string); |
19 | | - while (opt != -1) |
| 14 | + int ret; |
| 15 | + char str_seqNumber[11]; |
| 16 | + struct es10b_pending_notification notification; |
| 17 | + |
| 18 | + snprintf(str_seqNumber, sizeof(str_seqNumber), "%u", seqNumber); |
| 19 | + |
| 20 | + jprint_progress("es10b_retrieve_notifications_list", str_seqNumber); |
| 21 | + if (es10b_retrieve_notifications_list(&euicc_ctx, ¬ification, seqNumber)) |
20 | 22 | { |
21 | | - switch (opt) |
22 | | - { |
23 | | - case 'r': |
24 | | - auto_remove = true; |
25 | | - break; |
26 | | - case 'h': |
27 | | - case '?': |
28 | | - printf("Usage: %s [seqNumber] etc [OPTIONS]\r\n", argv[0]); |
29 | | - printf("\t -r Automatically remove processed notifications\r\n"); |
30 | | - return -1; |
31 | | - default: |
32 | | - break; |
33 | | - } |
34 | | - opt = getopt(argc, argv, opt_string); |
| 23 | + jprint_error("es10b_retrieve_notifications_list", NULL); |
| 24 | + return -1; |
35 | 25 | } |
36 | 26 |
|
37 | | - int seqNumber; |
38 | | - char *str_seqNumber = NULL; |
39 | | - struct es10b_pending_notification notification; |
40 | | - int ret; |
| 27 | + euicc_ctx.http.server_address = notification.notificationAddress; |
| 28 | + |
| 29 | + jprint_progress("es9p_handle_notification", str_seqNumber); |
| 30 | + if (es9p_handle_notification(&euicc_ctx, notification.b64_PendingNotification)) |
| 31 | + { |
| 32 | + jprint_error("es9p_handle_notification", NULL); |
| 33 | + return -1; |
| 34 | + } |
| 35 | + |
| 36 | + es10b_pending_notification_free(¬ification); |
41 | 37 |
|
42 | | - for (int i = 1; i < argc; i++) |
| 38 | + if (!autoremove) |
43 | 39 | { |
44 | | - errno = 0; |
45 | | - seqNumber = (int) strtol(argv[i], NULL, 10); |
46 | | - if (errno != 0) |
| 40 | + return 0; |
| 41 | + } |
| 42 | + |
| 43 | + jprint_progress("es10b_remove_notification_from_list", str_seqNumber); |
| 44 | + if ((ret = es10b_remove_notification_from_list(&euicc_ctx, seqNumber))) |
| 45 | + { |
| 46 | + const char *reason; |
| 47 | + switch (ret) |
47 | 48 | { |
48 | | - continue; |
| 49 | + case 1: |
| 50 | + reason = "seqNumber not found"; |
| 51 | + break; |
| 52 | + default: |
| 53 | + reason = "unknown"; |
| 54 | + break; |
49 | 55 | } |
50 | | - str_seqNumber = argv[i]; |
| 56 | + jprint_error("es10b_remove_notification_from_list", reason); |
| 57 | + return -1; |
| 58 | + } |
| 59 | + |
| 60 | + return 0; |
| 61 | +} |
| 62 | + |
| 63 | +static int applet_main(int argc, char **argv) |
| 64 | +{ |
| 65 | + static const char *opt_string = "arh?"; |
51 | 66 |
|
52 | | - jprint_progress("es10b_retrieve_notifications_list", str_seqNumber); |
53 | | - if (es10b_retrieve_notifications_list(&euicc_ctx, ¬ification, seqNumber)) |
| 67 | + int fret = 0; |
| 68 | + int all = 0; |
| 69 | + int autoremove = 0; |
| 70 | + int argc_seq_offset = 1; |
| 71 | + |
| 72 | + int opt = getopt(argc, argv, opt_string); |
| 73 | + for (int i = 0; opt != -1; i++) |
| 74 | + { |
| 75 | + switch (opt) |
54 | 76 | { |
55 | | - jprint_error("es10b_retrieve_notifications_list", NULL); |
| 77 | + case 'a': |
| 78 | + all = 1; |
| 79 | + break; |
| 80 | + case 'r': |
| 81 | + autoremove = 1; |
| 82 | + break; |
| 83 | + case 'h': |
| 84 | + case '?': |
| 85 | + printf("Usage: %s [OPTIONS] [seqNumber_0] [seqNumber_1]...\r\n", argv[0]); |
| 86 | + printf("\t -a All notifications\r\n"); |
| 87 | + printf("\t -r Automatically remove processed notifications\r\n"); |
56 | 88 | return -1; |
| 89 | + default: |
| 90 | + goto run; |
| 91 | + break; |
57 | 92 | } |
| 93 | + argc_seq_offset++; |
| 94 | + opt = getopt(argc, argv, opt_string); |
| 95 | + } |
58 | 96 |
|
59 | | - euicc_ctx.http.server_address = notification.notificationAddress; |
| 97 | +run: |
| 98 | + if (all) |
| 99 | + { |
| 100 | + struct es10b_notification_metadata_list *notifications, *rptr; |
60 | 101 |
|
61 | | - jprint_progress("es9p_handle_notification", str_seqNumber); |
62 | | - if (es9p_handle_notification(&euicc_ctx, notification.b64_PendingNotification)) |
| 102 | + jprint_progress("es10b_list_notification", NULL); |
| 103 | + if (es10b_list_notification(&euicc_ctx, ¬ifications)) |
63 | 104 | { |
64 | | - jprint_error("es9p_handle_notification", NULL); |
| 105 | + jprint_error("es10b_list_notification", NULL); |
65 | 106 | return -1; |
66 | 107 | } |
67 | 108 |
|
68 | | - es10b_pending_notification_free(¬ification); |
69 | | - |
70 | | - if (!auto_remove) |
| 109 | + rptr = notifications; |
| 110 | + while (rptr) |
71 | 111 | { |
72 | | - continue; |
| 112 | + if (_process_single(rptr->seqNumber, autoremove)) |
| 113 | + { |
| 114 | + fret = -1; |
| 115 | + break; |
| 116 | + } |
| 117 | + rptr = rptr->next; |
73 | 118 | } |
74 | 119 |
|
75 | | - jprint_progress("es10b_remove_notification_from_list", str_seqNumber); |
76 | | - if ((ret = es10b_remove_notification_from_list(&euicc_ctx, seqNumber))) |
| 120 | + es10b_notification_metadata_list_free_all(notifications); |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + for (int i = argc_seq_offset; i < argc; i++) |
77 | 125 | { |
78 | | - const char *reason; |
79 | | - switch (ret) |
| 126 | + unsigned long seqNumber; |
| 127 | + |
| 128 | + errno = 0; |
| 129 | + seqNumber = strtoul(argv[i], NULL, 10); |
| 130 | + if (errno != 0) |
80 | 131 | { |
81 | | - case 1: |
82 | | - reason = "seqNumber not found"; |
83 | | - break; |
84 | | - default: |
85 | | - reason = "unknown"; |
86 | | - break; |
| 132 | + continue; |
| 133 | + } |
| 134 | + if (_process_single(seqNumber, autoremove)) |
| 135 | + { |
| 136 | + fret = -1; |
| 137 | + break; |
87 | 138 | } |
88 | | - jprint_error("es10b_remove_notification_from_list", reason); |
89 | | - return -1; |
90 | 139 | } |
91 | 140 | } |
92 | 141 |
|
93 | | - jprint_success(NULL); |
| 142 | + if (fret == 0) |
| 143 | + { |
| 144 | + jprint_success(NULL); |
| 145 | + } |
94 | 146 |
|
95 | | - return 0; |
| 147 | + return fret; |
96 | 148 | } |
97 | 149 |
|
98 | 150 | struct applet_entry applet_notification_process = { |
|
0 commit comments