-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.c
145 lines (120 loc) · 3.5 KB
/
memory.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
//
// Weather Station Poller
//
// Copyright (C) 2010 Joakim Söderberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <stdio.h>
#include "wsp.h"
#include "memory.h"
#include "utils.h"
//
// Sends a USB message to the device from a given buffer.
//
int send_usb_msgbuf(struct usb_dev_handle *h, char *msg, int msgsize)
{
int bytes_written = 0;
debug_printf(2, "--> ");
print_bytes(2, msg, msgsize);
bytes_written = usb_control_msg(h, USB_TYPE_CLASS + USB_RECIP_INTERFACE,
9, 0x200, 0, msg, msgsize, USB_TIMEOUT);
//assert(bytes_written == msgsize);
return bytes_written;
}
//
// All data from the weather station is read in 32 byte chunks.
//
int read_weather_msg(struct usb_dev_handle *h, char buf[32])
{
return usb_interrupt_read(h, ENDPOINT_INTERRUPT_ADDRESS, buf, 32, USB_TIMEOUT);
}
//
// Reads a weather message from a given address in history.
//
int read_weather_address(struct usb_dev_handle *h, unsigned short addr, char buf[32])
{
if (program_settings.from_file)
{
// Special case if we try to read the next to last history chunk.
int bytes_to_read = (addr == (HISTORY_END - HISTORY_CHUNK_SIZE)) ? 16 : 32;
FILE *f = program_settings.f;
// Only open the file once.
if (!f)
{
f = fopen(program_settings.infile, "r");
}
if (fseek(f, addr, SEEK_SET))
{
fprintf(stderr, "Failed to seek to position %d (0x%d) in file.\n", addr, addr);
return -1;
}
if (fread(buf, 1, bytes_to_read, f) != bytes_to_read)
{
if (feof(f))
{
fprintf(stderr, "Tried to read past the end of file.\n");
}
else
{
perror("Error reading from file. ");
}
return -1;
}
return 0;
}
else
{
char msg[8] = {0xa1, (addr >> 8), (addr & 0xff), 0x20, 0xa1, 0, 0, 0x20};
send_usb_msgbuf(h, msg, 8);
return (read_weather_msg(h, buf) != 32);
}
}
//
// Reads weather ack message when writing setting data.
//
int read_weather_ack(struct usb_dev_handle *h)
{
unsigned int i;
char buf[8];
read_weather_msg(h, buf);
// The ack should consist of just 0xa5.
for (i = 0; i < 8; i++)
{
debug_printf(2, "%x ", (buf[i] & 0xff));
if ((buf[i] & 0xff) != 0xa5)
return -1;
}
debug_printf(2, "\n");
return 0;
}
//
// Writes 1 byte of data to the weather station.
//
int write_weather_1(struct usb_dev_handle *h, unsigned short addr, char data)
{
char msg[8] = {0xa2, (addr >> 8), (addr & 0xff), 0x20, 0xa2, data, 0, 0x20};
send_usb_msgbuf(h, msg, 8);
return read_weather_ack(h);
}
//
// Writes 32 bytes of data to the weather station.
//
int write_weather_32(struct usb_dev_handle *h, unsigned short addr, char data[32])
{
char msg[8] = {0xa0, (addr >> 8), (addr & 0xff), 0x20, 0xa0, 0, 0, 0x20};
send_usb_msgbuf(h, msg, 8); // Send write command.
send_usb_msgbuf(h, data, 32); // Send data.
return read_weather_ack(h);
}