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
200
201
|
/*
* Hot Beverage Companion – desktop application utilities
*
* Copyright (C) 2018 Gediminas Jakutis
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; version 2.1
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <gtk/gtk.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <endian.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "util.h"
#include "net.h"
#include "gtk.h"
struct child_data {
char **argv;
int fd;
char *buf;
};
static float digest_temp(const short int rawtemp);
static float digest_volt(const short int rawvolt);
static int find_settings_region(char *start, char *end, char *buf, size_t n);
static int flash(char *buf, size_t address, size_t size, char* port);
void reap_dead_children(int pid, int status, void *data);
/*
* raw data is in 0.1°K per 1. Subtract 2730 to get Celsius.
* Multiply by 0.1f (divide by 10) to get the correct scale.
*/
static float digest_temp(const short int rawtemp)
{
return (rawtemp - 2730) * 0.1f;
}
/*
* raw data is in 1/1024 parts of the max A0 input voltage
* on the D1 mini board, which is 3.2V
*/
static float digest_volt(const short int rawvolt)
{
return 3.2f / 1024.0f * rawvolt;
}
int refresh_data(int nd, struct tempmodule_state *state)
{
int ret;
char buf[dgsize];
ret = net_getlastdata(nd, buf);
if (ret == A_OK) {
memcpy(&state->sequence, buf + 2, sizeof(state->size));
state->temperature = digest_temp(state->temperature_raw);
state->voltage = digest_volt(state->voltage_raw);
}
return ret;
}
int write_settings(char *imagename, const struct configuration * const conf)
{
/* the largest flash size supported by ESP-8266 is 16MiB, so let's just use that. */
static const size_t flash_size = 16 * 1024 * 1024;
char pattern_a[4] = PADDING_START;
char pattern_b[4] = PADDING_END;
ssize_t buf_used;
ssize_t offset;
char *buf = NULL;
int fd;
int ret = ERROR;
fd = open(imagename, O_RDONLY);
if (fd < 0) {
goto fail;
}
buf = malloc(flash_size);
buf_used = read(fd, buf, flash_size);
close(fd);
if (buf_used < sizeof(struct configuration)) {
goto fail;
}
offset = find_settings_region(pattern_a, pattern_b, buf, buf_used);
if (offset == ERROR) {
goto fail;
};
memcpy(buf + offset, conf, sizeof(struct configuration));
/* make sure the block written is aligned on 4k boundry
* and is 4k of size.
*
* see:
* https://github.com/espressif/esptool/issues/306
*/
offset &= ~(0xfff);
ret = flash(buf + offset, offset, 4 * 1024, NULL);
fail:
free(buf);
return ret;
}
static int find_settings_region(char *start, char *end, char *buf, size_t n)
{
int ret = ERROR;
char *a;
if (!(a = memmem(buf, n, start, 4))) {
goto fail;
}
if ((a - buf + sizeof(struct configuration)) > n) {
goto fail;
}
if (memcmp(a + sizeof(struct configuration) - 4, end, 4)) {
goto fail;
}
ret = a - buf;
fail:
return ret;
}
static int flash(char *buf, size_t address, size_t size, char* port)
{
static const char esptool[] = "esptool.py";
static const char command[] = "write_flash";
struct child_data *cdata;
char tmpfn[] = "espXXXXXX"; /* temporary file name template */
char *fifn; /* flash image file name */
char textbuf[32];
char *argv[7] = {0};
int ret = ERROR;
pid_t pid;
int fd;
size_t i = 1;
fd = mkstemp(tmpfn);
write(fd, buf, size);
fifn = malloc(0x100);
memset(fifn, '\0', 0x100);
sprintf(textbuf, "/proc/self/fd/%d", fd);
if (readlink(textbuf, fifn, 0x100) == -1) {
exit(TMPFILE);
}
argv[0] = strdup(esptool);
if (port) {
argv[i++] = strdup("--port");
argv[i++] = strdup(port);
}
argv[i++] = strdup(command);
sprintf(textbuf, "%#.4lx", address);
argv[i++] = strdup(textbuf);
argv[i++] = strdup(fifn);
argv[i] = NULL;
cdata = malloc(sizeof(*cdata));
cdata->argv = argv;
cdata->fd = fd;
cdata->buf = fifn;
g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_LEAVE_DESCRIPTORS_OPEN | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, NULL, NULL, NULL);
g_child_watch_add(pid, reap_dead_children, cdata);
return ret;
}
void reap_dead_children(int pid, int status, void *data)
{
struct child_data *a;
a = data;
free(a->buf);
close(a->fd);
free(data);
g_spawn_close_pid(pid);
gtkui_dialog_done(status);
}
|