code_scanner/common/packet.h

260 lines
12 KiB
C
Raw Normal View History

2022-12-28 02:40:59 +00:00
#pragma once
// packet structures and command
//
// created on 2022-12-06
//
#include <bits/stdint-uintn.h>
#include <string.h>
2022-12-28 02:40:59 +00:00
///////////////////////////////////////////////////////////////////////////////
// definitions ...
#define CONFIG_NAME_MAX_LEN 32 // max bytes of configuration name
#define STRUCT_CONSTRUCTOR(st_name) \
st_name() \
{ \
memset(this, 0, sizeof(st_name));\
}
2022-12-28 02:40:59 +00:00
// NOTE: All text transmitted by pack cmd is in UTF-8 format !!!
enum packet_cmd
{
PACK_CMD_HEART_BEAT = 0, // notify peers you are still alive, receiver should reply the same pack
// attributes get/set, all content in PACK_BASE::payload should be in JSON style
PACK_CMD_ATTR_SYS_VER_GET = 10, // get system version on device, [in]: PACK_BASE, [out] PACK_BASE::payload - {"os":"linux", "ver":"4.4.194", ...}
PACK_CMD_ATTR_FIRMWARE_VER_GET, // get firmware version, [in]: PACK_BASE, [out] PACK_BASE::payload - {"firmware":"G2393A1234", "CIS":"CIS-123", ...}
PACK_CMD_ATTR_SERIAL_NUM_GET, // get device serial num, [in]: PACK_BASE, [out] PACK_BASE::payload - {"serial":"20221206001"}
PACK_CMD_ATTR_SERIAL_NUM_SET, // set device serial num, [in]: PACK_BASE::payload - {"serial":"20221206001"}, [out] PACK_BASE
PACK_CMD_ATTR_MAC_GET, // get mac address, [in]: PACK_BASE, [out] PACK_BASE::payload - {"mac":"12:34:56:78:9a:bc"}
PACK_CMD_ATTR_IP_GET, // get ip address, [in]: PACK_BASE, [out] PACK_BASE::payload - {"ipv4":"192.168.1.123", "ipv6":"::1"}
PACK_CMD_ATTR_HARDWARE_INFO_GET, // get hardwares information on device, [in]: PACK_BASE, [out] PACK_BASE::payload - {"CPU":"ARM x86", "mem":"16GB", ...}
PACK_CMD_ATTR_HISTORY_COUNT_GET, // get history count, [in]: PACK_BASE, [out] PACK_BASE::payload - {"history-count":12345, ...}
PACK_CMD_ATTR_ROLLER_COUNT_GET, // get roller count, [in]: PACK_BASE, [out] PACK_BASE::payload - {"roller-count":2345}
PACK_CMD_ATTR_ROLLER_COUNT_SET, // set roller count, [in]: PACK_BASE::payload - {"roller-count":2345}, [out] PACK_BASE
2022-12-28 02:40:59 +00:00
// configuration get/set
PACK_CMD_SETTING_GET = 50, // get all settings supported by the device, [in]: PACK_BASE, [out]: PACK_BASE::payload - configuration JSON, see SANE-configuration format
PACK_CMD_SETTING_GET_CUR, // get current value of given setting, [in]: PACK_BASE::payload - LPCFGVAL, [out]: PACK_BASE::payload - LPCFGVAL
PACK_CMD_SETTING_SET, // set value of given setting, [in]: PACK_BASE::payload - LPCFGVAL, [out]: PACK_BASE on OK or PACK_BASE::payload - LPCFGVAL
2022-12-28 02:40:59 +00:00
PACK_CMD_SETTING_RESTORE, // restore given settings, [in]: PACK_BASE, [out]: PACK_BASE
// status management
PACK_CMD_STATUS_GET = 60, // get current status of device, [in]: PACK_BASE, [out]: PACK_BASE
PACK_CMD_STATUS_RESTORE, // restore status to initializing, [in]: PACK_BASE, [out]: PACK_BASE
// scan command
PACK_CMD_SCAN_START = 100, // start scanning, [in]: PACK_BASE, [out]: PACK_BASE
PACK_CMD_SCAN_STOP, // stop scanning, [in]: PACK_BASE, [out]: PACK_BASE
PACK_CMD_SCAN_IMG_SIZE_GET, // get size of the first image, [in]: PACK_BASE, [out]: PACK_BASE::payload - (unsigned long long*)
PACK_CMD_SCAN_IMG_READ, // read first image, [in]: PACK_BASE, [out]: PACK_BASE::payload - LPPACKIMAGE
2022-12-28 02:40:59 +00:00
PACK_CMD_SCAN_IMG_POP, // discard first image, [in]: PACK_BASE, [out]: PACK_BASE
// file operation
PACK_CMD_FILE_QUERY = 150, // query file information, [in]: PACK_BASE::payload - (char*)file-path, [out] PACK_BASE::payload - LPFILEINFO
PACK_CMD_FILE_READ, // read file content, [in]: PACK_BASE::payload - (char*)file-path, [out] PACK_BASE::payload - file content
PACK_CMD_FILE_WRITE, // write a file, [in]: PACK_BASE::payload - LPFILEINFO, [out] PACK_BASE::payload - (char*)path-file. should receive whole data even if fails
PACK_CMD_FILE_MOVE, // move/rename a file, [in]: PACK_BASE::payload - LPFILEINFO, [out] same data as in
PACK_CMD_FILE_REMOVE, // delete a file, [in]: PACK_BASE::payload - (char*)file-path, [out] same data as in
2022-12-28 02:40:59 +00:00
// process operation
PACK_CMD_PROCESS_REBOOT = 200, // reboot system, [in]: PACK_BASE, [out]: PACK_BASE
PACK_CMD_PROCESS_START, // start a program [in]: PACK_BASE::payload - (char*)command string, [out]: PACK_BASE::payload - (char*)process-id
PACK_CMD_PROCESS_STOP, // kill a process [in]: PACK_BASE::payload - (char*)process-id, [out]: PACK_BASE
PACK_CMD_PROCESS_EXEC_RESULT, // get result of a command, [in]: PACK_BASE::payload - (char*)command string, [out]: PACK_BASE::payload - (char*)execute result. popen(), fgets ...
PACK_CMD_PROCESS_QUERY, // query process information [in]: PACK_BASE::payload - (char*)process-id(-1 for all), [out]: LPPROCINFO
2022-12-28 02:40:59 +00:00
PACK_CMD_TOKEN_GET = 272, // 0x110, Obtain the token of the required command, [in] PACK_BASE, [out] - PACK_BASE::payload - LPOPERTOKEN
PACK_CMD_INNER_WRITE, // inner command, for invoking IPC write, PACK_BASE::payload is the content to be sent
2022-12-28 02:40:59 +00:00
};
enum scanner_status
{
SCANNER_STATUS_READY = 0,
SCANNER_STATUS_START_SCANNING, // start ok, but scanning-thread not working
SCANNER_STATUS_WORKING, // start ok, and scanning-thread is working
SCANNER_STATUS_PAUSING,
2022-12-28 02:40:59 +00:00
SCANNER_STATUS_COVER_OPENNED,
SCANNER_STATUS_SLEEPING,
2022-12-28 02:40:59 +00:00
SCANNER_STATUS_COUNT_MODE,
SCANNER_STATUS_DOUBLE_FEEDED,
SCANNER_STATUS_PAPER_JAMMED,
SCANNER_STATUS_PAPER_ASKEW,
SCANNER_STATUS_FEED_FAILED,
SCANNER_STATUS_NO_PAPER,
SCANNER_STATUS_CFG_CHANGED, // PACK_BASE::payload - LPCFGVAL
2022-12-28 02:40:59 +00:00
};
enum img_format
{
IMG_FMT_UNKNOWN = 0, // unknown format
IMG_FMT_TIFF,
IMG_FMT_BMP,
IMG_FMT_JPEG,
IMG_FMT_PNG,
IMG_FMT_SVG,
IMG_FMT_WEBP,
IMG_FMT_GIF,
};
enum img_compression
{
IMG_COMPRESSION_NONE = 0,
IMG_COMPRESSION_GROUP4,
IMG_COMPRESSION_RLE4,
IMG_COMPRESSION_RLE8,
IMG_COMPRESSION_LZW,
IMG_COMPRESSION_ZIP,
};
enum img_status
{
IMG_STATUS_OK = 0,
IMG_STATUS_DOUBLE,
IMG_STATUS_JAM,
IMG_STATUS_STAPLE, // staples on the paper
IMG_STATUS_SIZE_ERR, // size check failed
IMG_STATUS_DOGEAR, // paper has dogear
};
enum data_type
{
DATA_TYPE_BOOL = 0, // (bool*)
DATA_TYPE_INT1, // (uint8_t*)
DATA_TYPE_INT2, // (uint16_t*)
DATA_TYPE_INT4, // (uint32_t*)
DATA_TYPE_INT8, // (uint64_t*)
DATA_TYPE_FLOAT, // (double*)
DATA_TYPE_STRING, // (char*) with max_len space
DATA_TYPE_CUSTOM,
};
enum paper_side
{
PAPER_SIDE_FRONT = 0, // single side, this is front
PAPER_SIDE_BACK, // single side, this is back
PAPER_SIDE_TOP, // VERT-compound sides, and front side is at top
PAPER_SIDE_BOTTOM, // VERT-compound sides, and front side is at bottom
PAPER_SIDE_LEFT, // HORZ-compound sides, and front side is at left
PAPER_SIDE_RIGHT, // HORZ-compound sides, and front side is at right
};
enum rot_angle
{
ROT_ANGLE_0 = 0,
ROT_ANGLE_90,
ROT_ANGLE_180,
ROT_ANGLE_270,
};
2022-12-28 02:40:59 +00:00
#pragma pack(push)
#pragma pack(1)
typedef struct _pack_base // A piece of data has only one header
{
uint64_t total_bytes : 63; // total bytes of payload
uint64_t thunk : 1; // when was 1, the payloads is stored as THUNKED in 'payload', and total_bytes is data bytes in 'payload' of this packet
uint32_t pack_id; // maintain by the initiator, the reply use the same id
uint32_t timestamp; // time stamp, in milliseconds, maintain by the initiator, used to timeout control
uint32_t cmd; // packet_cmd on initiator side
uint32_t result; // command result(error code) on roger side
char payload[0]; // payloads, according to 'cmd', LPTHUNKD if 'thunk' was 1
STRUCT_CONSTRUCTOR(_pack_base)
2022-12-28 02:40:59 +00:00
}PACK_BASE, * LPPACK_BASE;
typedef struct _thunk_data
{
uint16_t bytes; // bytes in 'data', 0 is end for whole data
char data[0]; // data buffer with length 'bytes'
}THUNKD, * LPTHUNKD;
2022-12-28 02:40:59 +00:00
typedef struct _cfg_val
{
uint16_t count; // count of array 'vals'
struct
{
char name[CONFIG_NAME_MAX_LEN]; // configuration name
uint16_t val_buf_len; // capacity of 'val'
char val[2]; // value
}vals[1];
}CFGVAL, * LPCFGVAL;
2022-12-28 02:40:59 +00:00
typedef struct _img_pos
{
uint64_t paper_ind : 32; // paper index in this turn/start, based ZERO. (image-collector set)
uint64_t new_img : 1; // 0 - partial data; 1 - new image data. (image-collector set)
uint64_t img_over : 1; // 0 - has data yet; 1 - END for the image. (image-collector set)
uint64_t paper_side : 3; // enum paper_side. front of paper(When scanning multiple sheets, the paper feeding side is the front side). (image-collector set)
uint64_t back_rot : 2; // back rotation angle, enum rot_angle. (image-collector set)
uint64_t channel_ind : 4; // index of color channel, based ZERO, 0x0f for all channels. (image-collector set)
uint64_t status : 4; // img_status. (image-collector set)
2022-12-28 02:40:59 +00:00
uint64_t split_ind : 7; // splitting order, from left to right and then top to bottom, based ZERO
uint64_t multiout_ind : 4; // index of multi-out
uint64_t reserved : 6; // reserved
STRUCT_CONSTRUCTOR(_img_pos)
}IMGPOS, * LPIMGPOS;
2022-12-28 02:40:59 +00:00
typedef struct _pack_img
{
IMGPOS pos; // image pos info ...
uint32_t width; // image width in pixel. (image-collector set)
uint32_t height; // image height in pixel. (image-collector set)
uint32_t resolution_x; // image horizontal reolution. (image-collector set)
uint32_t resolution_y; // image vertical reolution. (image-collector set)
uint32_t channels : 6; // image channels per pixel. (image-collector set)
uint32_t format : 6; // image format, see 'img_format'. (image-collector set)
uint32_t bpp : 6; // bits per pixel. (image-collector set)
uint32_t bppc : 6; // bits per pixel in this channel, equal to 'bpp' if pos.channel_ind == 0x0f. (image-collector set)
uint32_t compression : 6; // image data compression, see 'img_compression'. (image-collector set)
2022-12-28 02:40:59 +00:00
uint32_t reserve : 2; // unused now
uint32_t info_size; // image information size in bytes, information part is used for quality of JPEG, pallete of BMP .... (image-collector set)
uint64_t data_size; // image data size in bytes. (image-collector set)
2022-12-28 02:40:59 +00:00
char data[0]; // two parts: image info (info_size) + image data (data_size)
STRUCT_CONSTRUCTOR(_pack_img)
}PACKIMAGE, * LPPACKIMAGE;
2022-12-28 02:40:59 +00:00
typedef struct _oper_token
{
uint32_t type; // token type
char data[128]; // token data
}OPERTOKEN, * LPOPERTOKEN;
2022-12-28 02:40:59 +00:00
typedef struct _file_info
{
OPERTOKEN token; // operation token, returned by command PACK_CMD_TOKEN_GET
uint64_t size; // file size
uint16_t name_len; // bytes of file name string
uint16_t create_time_len; // bytes of create time string: '2022-12-07 12:34:56.789', or target file path in command PACK_CMD_FILE_MOVE
uint16_t modify_time_len;
uint16_t version_len; // bytes of version string
char data[0]; // 4 parts: path-file(name_len) + create-time(create_time_len) + modify-time(modify_time_len) + version(version_len)
// or 5 parts in command PACK_CMD_FILE_WRITE, add content at the last part of bytes 'size'
STRUCT_CONSTRUCTOR(_file_info)
}FILEINFO, * LPFILEINFO;
2022-12-28 02:40:59 +00:00
typedef struct _proc_info
{
OPERTOKEN token; // operation token, returned by command PACK_CMD_TOKEN_GET
uint32_t count; // number of elements in array proc
struct _info
{
uint16_t len; // bytes of this element, include this head
uint64_t pid; // process id
uint64_t ppid; // parent process id
uint64_t start; // started time in ns from 1970-01-01 00:00:00
uint64_t mem; // memory usage, in bytes
uint64_t cpu_clk; // cpu clock
char path_name[4];
}proc[1];
STRUCT_CONSTRUCTOR(_proc_info)
}PROCINFO, * LPPROCINFO;
2022-12-28 02:40:59 +00:00
#pragma pack(pop)
////////////////////////////////////////////////////////////////////////////////////////////////
// configurations ...
//
// 1 - App has whole set, group definitions
//
// 2 - device provides sub-set, or a customizing item
//