191 lines
5.3 KiB
C
191 lines
5.3 KiB
C
#include "lab.h"
|
|
|
|
void client_init(struct client *client, struct backend *backend)
|
|
{
|
|
memset(client, 0, sizeof(*client));
|
|
client->backend = backend;
|
|
|
|
}
|
|
|
|
static int invoke(struct client *client, int operation, int a, int b,
|
|
unsigned char *bytes, size_t length)
|
|
{
|
|
int result;
|
|
do {
|
|
result = device_call(client->backend, operation, a, b, bytes, length);
|
|
} while (result == -EINTR);
|
|
return result;
|
|
}
|
|
static void status_encode(unsigned value, unsigned char *bytes, size_t width)
|
|
{
|
|
size_t index;
|
|
for (index = 0; index < width; index++) {
|
|
bytes[index] = (unsigned char)(value & 255u);
|
|
value >>= 8;
|
|
}
|
|
}
|
|
|
|
static int status_query(struct client *client, const struct command *command,
|
|
struct reply *reply)
|
|
{
|
|
const int *state = client->value[client->selected];
|
|
unsigned char bytes[32];
|
|
size_t index;
|
|
int start;
|
|
int count;
|
|
int width;
|
|
if (command->operation != 'Q' && command->operation != 'P') {
|
|
return 0;
|
|
}
|
|
if (!command_has(command, 2)) {
|
|
reply->result = -EINVAL;
|
|
return 1;
|
|
}
|
|
start = command->argument[0];
|
|
if (command->operation == 'Q') {
|
|
count = 1;
|
|
width = command->argument[1];
|
|
if (width != 1 && width != 2 && width != 4) {
|
|
reply->result = -EINVAL;
|
|
return 1;
|
|
}
|
|
} else {
|
|
count = command->argument[1];
|
|
width = 4;
|
|
}
|
|
if (start < 0 || count < 0 || start > 8 || count > 8 - start) {
|
|
reply->result = -EINVAL;
|
|
return 1;
|
|
}
|
|
memset(bytes, 0, sizeof(bytes));
|
|
for (index = 0; index < (size_t)count; index++) {
|
|
unsigned value = (unsigned)state[start + (int)index];
|
|
status_encode(value, bytes + index * (size_t)width, (size_t)width);
|
|
}
|
|
reply_copy(reply, bytes, (size_t)(count * width));
|
|
return 1;
|
|
}
|
|
|
|
|
|
static int configure(struct client *client, int mode, int speed)
|
|
{
|
|
int *state = client->value[client->selected];
|
|
int result;
|
|
if (mode < 0 || mode > 3 || speed < 1 || speed > 1000000) {
|
|
return -EINVAL;
|
|
}
|
|
result = invoke(client, 5, mode, speed, NULL, 0);
|
|
if (result) {
|
|
return result;
|
|
}
|
|
state[0] = 1;
|
|
state[1] = mode;
|
|
state[2] = speed;
|
|
return 0;
|
|
}
|
|
|
|
static int program(struct client *client, const struct command *command)
|
|
{
|
|
int *state = client->value[client->selected];
|
|
unsigned char bytes[BYTE_LIMIT];
|
|
size_t offset = 0;
|
|
int address;
|
|
int count;
|
|
int result;
|
|
if (!state[0]) {
|
|
return -ENODEV;
|
|
}
|
|
if (command->count < 2) {
|
|
return -EINVAL;
|
|
}
|
|
address = command->argument[0];
|
|
count = command->argument[1];
|
|
if (address < 0 || address > 255 || count < 1 || count > 32 || count > 256 - address ||
|
|
!bytes_valid(command, 2, (size_t)count)) {
|
|
return -EINVAL;
|
|
}
|
|
bytes_from_command(bytes, command, 2, (size_t)count);
|
|
result = invoke(client, 1, 0, 0, NULL, 0);
|
|
if (result) {
|
|
return result;
|
|
}
|
|
while (offset < (size_t)count) {
|
|
size_t chunk = (size_t)count - offset;
|
|
if (chunk > 16) {
|
|
chunk = 16;
|
|
}
|
|
result = invoke(client, 2, address + (int)offset, 0, bytes + offset, chunk);
|
|
if (result) {
|
|
return result;
|
|
}
|
|
state[4]++;
|
|
state[6] += (int)chunk;
|
|
offset += chunk;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int read_flash(struct client *client, int address, int count,
|
|
struct reply *reply)
|
|
{
|
|
int *state = client->value[client->selected];
|
|
unsigned char bytes[BYTE_LIMIT];
|
|
int result;
|
|
if (!state[0]) {
|
|
return -ENODEV;
|
|
}
|
|
if (address < 0 || address > 255 || count < 1 || count > 32 || count > 256 - address) {
|
|
return -EINVAL;
|
|
}
|
|
memset(bytes, 0, sizeof(bytes));
|
|
result = invoke(client, 3, address, 0, bytes, (size_t)count);
|
|
if (result) {
|
|
return result;
|
|
}
|
|
/* RX_OFFSET */
|
|
reply_copy(reply, bytes + 4, (size_t)count);
|
|
state[3]++;
|
|
state[7] += count;
|
|
return 0;
|
|
}
|
|
|
|
static int erase_flash(struct client *client, int address)
|
|
{
|
|
int *state = client->value[client->selected];
|
|
int result;
|
|
if (!state[0]) {
|
|
return -ENODEV;
|
|
}
|
|
if (address < 0 || address > 224 || address % 32) {
|
|
return -EINVAL;
|
|
}
|
|
result = invoke(client, 1, 0, 0, NULL, 0);
|
|
if (result) {
|
|
return result;
|
|
}
|
|
result = invoke(client, 4, address, 0, NULL, 0);
|
|
if (!result) {
|
|
state[5]++;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void client_apply(struct client *client, const struct command *command,
|
|
struct reply *reply)
|
|
{
|
|
if (status_query(client, command, reply)) { return; }
|
|
if (command->operation == 'O' && command_has(command, 2)) {
|
|
reply->result = configure(client, command->argument[0], command->argument[1]);
|
|
} else if (command->operation == 'W') {
|
|
reply->result = program(client, command);
|
|
} else if (command->operation == 'R' && command_has(command, 2)) {
|
|
reply->result = read_flash(client, command->argument[0], command->argument[1], reply);
|
|
} else if (command->operation == 'E' && command_has(command, 1)) {
|
|
reply->result = erase_flash(client, command->argument[0]);
|
|
} else if (command->operation == 'X' && command_has(command, 0)) {
|
|
client->value[client->selected][0] = 0;
|
|
} else {
|
|
reply->result = -EINVAL;
|
|
}
|
|
}
|