74 ESP_LOGI(TAG, "/spiffs/units.conf written %d bytes", bytes); |
74 ESP_LOGI(TAG, "/spiffs/units.conf written %d bytes", bytes); |
75 } |
75 } |
76 } |
76 } |
77 |
77 |
78 |
78 |
79 |
|
80 int add_station(uint8_t *SSID, uint8_t *Password) |
|
81 { |
|
82 FILE *f; |
|
83 uint8_t *dst = (uint8_t *)&wifiStation; |
|
84 |
|
85 if (read_station(SSID) >= 0) { |
|
86 ESP_LOGE(TAG, "add_station %s already exists", SSID); |
|
87 return -1; |
|
88 } |
|
89 |
|
90 f = fopen("/spiffs/stations.conf", "a+"); |
|
91 if (f == NULL) { |
|
92 ESP_LOGE(TAG, "write /spiffs/stations.conf failed"); |
|
93 return -1; |
|
94 } |
|
95 memset(dst, 0, sizeof(wifiStation)); |
|
96 sprintf(wifiStation.SSID, "%s", (char *)SSID); |
|
97 sprintf(wifiStation.Password, "%s", (char *)Password); |
|
98 fwrite(dst, 1, sizeof(wifiStation), f); |
|
99 fclose(f); |
|
100 |
|
101 ESP_LOGI(TAG, "add_station %s record: %d", (char *)SSID, read_station(SSID)); |
|
102 /* Return the record number */ |
|
103 return read_station(SSID); |
|
104 } |
|
105 |
|
106 |
|
107 |
|
108 int read_station(uint8_t *SSID) |
|
109 { |
|
110 uint8_t *dst = (uint8_t *)&wifiStation; |
|
111 static int rc; |
|
112 FILE *f; |
|
113 size_t bytes; |
|
114 |
|
115 if ((SSID == NULL) || (strlen((char *)SSID) == 0)) { |
|
116 ESP_LOGI(TAG, "read_station(NULL)"); |
|
117 return -1; |
|
118 } |
|
119 |
|
120 memset(dst, 0, sizeof(wifiStation)); |
|
121 f = fopen("/spiffs/stations.conf", "r+"); |
|
122 if (f == NULL) { |
|
123 f = fopen("/spiffs/stations.conf", "w+"); |
|
124 fclose(f); |
|
125 ESP_LOGI(TAG, "/spiffs/stations.conf created, return -1"); |
|
126 return -1; |
|
127 } |
|
128 |
|
129 rc = 0; |
|
130 fseek(f, 0, SEEK_SET); |
|
131 |
|
132 while (1) { |
|
133 bytes = fread(dst, 1, sizeof(wifiStation), f); |
|
134 ESP_LOGI(TAG, " read_station bytes %d size %d", bytes, sizeof(wifiStation)); |
|
135 if (bytes < sizeof(wifiStation)) { |
|
136 fclose(f); |
|
137 memset(dst, 0, sizeof(wifiStation)); |
|
138 return -1; |
|
139 } |
|
140 if (strcmp(wifiStation.SSID, (char *)SSID) == 0) { |
|
141 // Fount it |
|
142 fclose(f); |
|
143 return rc; |
|
144 } |
|
145 rc++; |
|
146 } |
|
147 return -1; |
|
148 } |
|
149 |
|
150 |
|
151 |
|
152 void remove_station(uint8_t *SSID) |
|
153 { |
|
154 FILE *n, *o; |
|
155 uint8_t *dst; |
|
156 size_t bytes; |
|
157 |
|
158 n = fopen("/spiffs/stations.new", "a"); |
|
159 if (n == NULL) { |
|
160 ESP_LOGE(TAG, "cannot create /spiffs/stations.new"); |
|
161 return; |
|
162 } |
|
163 o = fopen("/spiffs/stations.conf", "r"); |
|
164 if (o == NULL) { |
|
165 ESP_LOGE(TAG, "cannot open spiffs/stations.conf for reading"); |
|
166 fclose(n); |
|
167 return; |
|
168 } |
|
169 |
|
170 dst = (uint8_t*)&wifiStation; |
|
171 while (true) { |
|
172 bytes = fread(dst, 1, sizeof(wifiStation), o); |
|
173 if (bytes == 0) |
|
174 break; |
|
175 |
|
176 if ((strcmp((char *)SSID, wifiStation.SSID) == 0) || (strlen(wifiStation.SSID) == 0)) { |
|
177 ESP_LOGI(TAG, "remove station %s", (char *)SSID); |
|
178 } else { |
|
179 fwrite(dst, 1, sizeof(wifiStation), n); |
|
180 } |
|
181 } |
|
182 fclose(o); |
|
183 fclose(n); |
|
184 |
|
185 rename("/spiffs/stations.conf", "/spiffs/stations.old"); |
|
186 rename("/spiffs/stations.new", "/spiffs/stations.conf"); |
|
187 unlink("/spiffs/stations.old"); |
|
188 } |
|
189 |
|