cake
/
libp7
Archived
1
0
Fork 1
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libp7/src/legacy/recv.c.draft

219 lines
6.0 KiB
Plaintext

/* *****************************************************************************
* legacy/recv.c -- receive a legacy packet.
* Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libp7.
* libp7 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; either version 3.0 of the License,
* or (at your option) any later version.
*
* libp7 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 libp7; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include <libp7/internals.h>
/* ************************************************************************** */
/* Raw packet structure */
/* ************************************************************************** */
/* Complete structure */ */
struct raw_packet {
uint8_t type[4];
uint8_t subtype[2];
uint8_t _zero2;
uint8_t in_use; /* 0 or 1 */
uint8_t _zero3;
uint8_t in_use2; /* duplicate of `in_use` */
uint8_t varname; /* variable name: 0x41 t 0x5A, 0xCD for r, 0xCE for è */
uint8_t _spacing[7];
uint8_t variable[8]; /* "Variable", ASCII-encoded */
uint8_t complex; /* 'C' if complex, 'R' if real */
uint8_t unknown; /* 0x0A */
uint8_t _spacing2[20];
uint8_t checksum; /* ~sum(all bytes not including start and checksum) + 1 */
};
/* Types */
static packet_type_t types[] = {
{"REQ", request},
{"VAL", value_description},
};
/* ************************************************************************** */
/* Packet completing utilities */
/* ************************************************************************** */
/**
* complete_packet:
* Complete the packet with N bytes.
*
* Uses stream buffering to achieve this.
*
* @arg handle the libp7 handle.
* @arg buf the buffer.
* @arg recv pointer to currently received bytes.
* @arg with complete with this number of byte.
* @return the error (0 if ok)
*/
static int complete_packet(p7_handle_t *handle,
unsigned char *buf, size_t *recv, size_t with)
{
int err = p7_read(handle->_stream, &buf[*recv], with, ACTIVE_TIMEOUT);
*recv += with;
return (err);
}
#define buffer handle->_recv_buffer
#define COMPLETE_PACKET(N) { \
int COMP_PACKET_err; \
if ((COMP_PACKET_err = complete_packet(handle, \
buffer, &received, (N)))) \
return (COMP_PACKET_err); \
}
/* ************************************************************************** */
/* Receive the packet */
/* ************************************************************************** */
/**
* p7_legacy_recv:
* Receive a [legacy] packet.
*
* This is the main receiving function. It receives, parses, and returns
* if everything has been successfully done or not.
* It reads progressively all of the packet.
*
* The checksum holds in one byte instead of two (Protocol 7).
* Because I don't want to make an other `p7_checksum` function,
* `p7_checksum(buffer, received + 1)` is a hack.
*
* Sources for these functions are Cafix and some reverse engineering
* report from Erik Grindheim (CASIO CFX-9950G communications protocol).
*
* @arg handle the libp7 handle
* @return the error (0 if ok)
*/
static int p7_legacy_recv(p7_handle_t *handle)
{
/* prepare reception */
size_t received = 0;
/* log packet */
log_info("receiving packet...");
/* get the first character */
COMPLETE_PACKET(1)
if (buffer[0] != ':') switch (buffer[0]) {
/* initial packets */
case 0x15: /* is interactive initial packet */
case 0x16: resp.type = p7_pt_ini; return (0);
/* initial packet response */
case 0x13: resp.type = p7_pt_ini_resp; return (0);
/* ack */
case 0x06: resp.type = p7_pt_ack; return (0);
/* unknown data type */
case 0x00: case 0x24: return (0);
/* checksum error */
case 0x2B: return (0);
/* abort XXX */
case 0x15: return (0);
/* we don't know this */
default: return (p7_error_unknown);
}
/* read the type */
COMPLETE_PACKET(4)
/* check if value packet */
if (!memcmp(&buffer[1], "\x00\x01\x00\x01", 4)) {
/* get the packet */
size_t packet_size = 16 - 10;
if (handle->_expecting_complex)
packet_size += 10;
COMPLETE_PACKET(packet_size)
/* check the sum */
if (checksum != p7_checksum(buffer, received + 1))
return (p7_error_checksum);
/* read the value */
/* TODO */
}
/* check legacy screendump packet */
if (buffer[3] == '@') {
COMPLETE_PACKET(35)
if (buffer[50] != p7_checksum(&buffer[1], 48))
return (p7_error_checksum);
/* TODO */
/* yay! */
return (0);
}
/* check legacy packet */
if (buffer[4] == 0) {
COMPLETE_PACKET(45)
if (buffer[50] != p7_checksum(&buffer[1], 48))
return (p7_error_checksum);
/* check the type */
const char *type = &buffer[1];
int datatype = get_data_type(&buffer[5]);
if (!memcmp(type, "REQ", 3)) {
/* request */
} else if (!memcmp(type, "VAL", 3)) {
/* variable */
} else if (!memcmp(type, "MEM", 3)) {
/* backup */
} else if (!memcmp(type, "FNC", 3)) {
/* function */
} else if (!memcmp(type, "IMG", 3)) {
/* picture */
} else if (!memcmp(type, "TXT", 3)) {
/* program */
} else if (!memcmp(type, "REQ", 3)) {
/* request? */
} else if (!memcmp(type, "END", 3)) {
/* end */
} else if (type[0] == 'P' && isdigit(type[1]) && type[2] == 0) {
/* program in P0, P1, ... */
} else {
/* unknown */
}
/* yay! */
return (0);
}
/* special packet? see cafix */
COMPLETE_PACKET(5)
if (!memcmp(&buffer[1], "BUTYPEA02", 9)) {
/* backup from CFX-9800G? */
COMPLETE_PACKET(30)
}
/* unknown packet! */
return (p7_error_unknown);
}