forked from flipperdevices/flipperzero-firmware
-
-
Notifications
You must be signed in to change notification settings - Fork 514
/
namechangersrv.c
170 lines (133 loc) · 5.16 KB
/
namechangersrv.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
#include "namechangersrv.h"
#include "m-string.h"
#include <toolbox/path.h>
#include <flipper_format/flipper_format.h>
int32_t namechanger_on_system_start(void* p) {
UNUSED(p);
if(!furi_hal_is_normal_boot()) {
FURI_LOG_W(TAG, "NameChangerSRV load skipped. Device is in special startup mode.");
} else {
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = flipper_format_file_alloc(storage);
FuriString* NAMEHEADER;
NAMEHEADER = furi_string_alloc_set("Flipper Name File");
FuriString* filepath;
filepath = furi_string_alloc_set("/ext/dolphin/name.txt");
bool result = false;
FuriString* data;
data = furi_string_alloc();
do {
if(!flipper_format_file_open_existing(file, furi_string_get_cstr(filepath))) {
break;
}
// header
uint32_t version;
if(!flipper_format_read_header(file, data, &version)) {
break;
}
if(furi_string_cmp_str(data, furi_string_get_cstr(NAMEHEADER)) != 0) {
break;
}
if(version != 1) {
break;
}
// get Name
if(!flipper_format_read_string(file, "Name", data)) {
break;
}
result = true;
} while(false);
flipper_format_free(file);
if(!result) {
//file not good - write new one
FlipperFormat* file = flipper_format_file_alloc(storage);
bool res = false;
do {
// Open file for write
if(!flipper_format_file_open_always(file, furi_string_get_cstr(filepath))) {
break;
}
// Write header
if(!flipper_format_write_header_cstr(file, furi_string_get_cstr(NAMEHEADER), 1)) {
break;
}
// Write comments
if(!flipper_format_write_comment_cstr(
file,
"Changing the value below will change your FlipperZero device name.")) {
break;
}
if(!flipper_format_write_comment_cstr(
file,
"Note: This is limited to 8 characters using the following: a-z, A-Z, 0-9, and _")) {
break;
}
if(!flipper_format_write_comment_cstr(
file, "It can contain other characters but use at your own risk.")) {
break;
}
//Write name
if(!flipper_format_write_string_cstr(
file, "Name", furi_hal_version_get_name_ptr())) {
break;
}
res = true;
} while(false);
flipper_format_free(file);
if(!res) {
FURI_LOG_E(TAG, "Save failed.");
}
} else {
if(!furi_string_size(data)) {
//Empty file - get default name and write to file.
FlipperFormat* file = flipper_format_file_alloc(storage);
bool res = false;
do {
// Open file for write
if(!flipper_format_file_open_always(file, furi_string_get_cstr(filepath))) {
break;
}
// Write header
if(!flipper_format_write_header_cstr(
file, furi_string_get_cstr(NAMEHEADER), 1)) {
break;
}
// Write comments
if(!flipper_format_write_comment_cstr(
file,
"Changing the value below will change your FlipperZero device name.")) {
break;
}
if(!flipper_format_write_comment_cstr(
file,
"Note: This is limited to 8 characters using the following: a-z, A-Z, 0-9, and _")) {
break;
}
if(!flipper_format_write_comment_cstr(
file, "It cannot contain any other characters.")) {
break;
}
//Write name
if(!flipper_format_write_string_cstr(
file, "Name", furi_hal_version_get_name_ptr())) {
break;
}
res = true;
} while(false);
flipper_format_free(file);
if(!res) {
FURI_LOG_E(TAG, "Save failed.");
}
} else {
char newdata[9];
snprintf(newdata, 9, "%s", furi_string_get_cstr(data));
//set name from file
furi_hal_version_set_custom_name(newdata);
}
}
furi_string_free(data);
furi_string_free(filepath);
furi_record_close(RECORD_STORAGE);
}
return 0;
}