/*****************************************************************************\
hpiom.c - HP I/O message handler
(c) 2003-2004 Copyright Hewlett-Packard Development Company, LP
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Hewlett-Packard nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*****************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "hpijs.h"
#include "hpiom.h"
static int hpiod_port_num = 50000;
static int hpiod_socket=-1;
static const int gnMaxDataSize = 2048;
static const int gnMaxCmdOptionSize = 4;
static const char gcFrameMarker = '$';
static const int gnPadding = 255;
static const int gnRequiredSize = 11;
static const int gnMinCommandSize = 16;
static const int gnMinDecodeSize = 10; // needed six bytes to determine command number,
// command length and data length
static const int gUV8FrameOffset = 0;
static const int gUV16CommandLengthOffset = 1;
static const int gUV8UnitNumberOffset = 3;
static const int gE8PacketTypeOffset = 4;
static const int gUV8CommandNumberOffset = 5;
static const int gUV16ReferenceNumberOffset = 6;
static const int gUV16DataLengthOffset = 8;
static const int gUV8CommandOptionsOffset = 10;
static const int gUV8RespFrameOffset = 0;
static const int gUV16RespCommandLengthOffset = 1;
static const int gUV8RespUnitNumberOffset = 3;
static const int gE8RespPacketTypeOffset = 4;
static const int gUV8RespCommandNumberOffset = 5;
static const int gUV16RespReferenceNumberOffset = 6;
static const int gUV16RespDataLengthOffset = 8;
static const int gE8RespCompleteOffset = 10;
// reserved reference number
//
static const int gnMinRefNum = 0xF000;
static const int gnMaxRefNum = 0xFFFD;
unsigned short gwSynchRefNum = 0xFFEC;
unsigned short gwSynchCompleteRefNum = 0xFFEB;
unsigned short gwResetRefNum = 0xFFEA;
unsigned short gwPrinterVersionQueryRefNum = 0xFFD0;
unsigned short gwPrinterStatusQueryRefNum = 0xFFD1;
unsigned short gwPrinterAttributesQueryRefNum = 0xFFD2;
unsigned short gwAlignmentQueryRefNum = 0xFFD3;
unsigned short gwDeviceIdQueryRefNum = 0xFFDD;
unsigned short gwHueCompensationQueryRefNum = 0xFFDE;
// command options
//
// printer query command options
//
static const int gnPrinterQueryOptionsSize = 4;
static unsigned char gpPrinterVersionQuery[] = { 0x00, 0x00, 0x00, 0x00 }; // 0 - return UV32 version data
//static unsigned char gpPrinterStatusQuery[] = { 0x01, 0x00, 0x00, 0x00 }; // 1 - return status string
//static unsigned char gpPrinterAttributesQuery[] = { 0x02, 0x00, 0x00, 0x00 }; // 2 - return printer attributes
static unsigned char gpAlignmentQuery[] = { 0x03, 0x00, 0x00, 0x00 }; // 3 - return primitive alignment value
//static unsigned char gpDeviceIdQuery[] = { 0x0D, 0x00, 0x00, 0x00 }; // 13 - return device id
//static unsigned char gpHueCompensationQuery[] = { 0x0E, 0x00, 0x00, 0x00 }; // 14 - return hue compensation
static unsigned char gpPenAlignmentQuery[] = { 0x0F, 0x00, 0x00, 0x00 }; // 15 - return pen alignment value
int GetPair(char *buf, char *key, char *value, char **tail)
{
int i=0, j;
key[0] = 0;
value[0] = 0;
if (buf[i] == '#')
{
for (; buf[i] != '\n' && i < HEADER_SIZE; i++); /* eat comment line */
i++;
}
if (strncasecmp(&buf[i], "data:", 5) == 0)
{
strcpy(key, "data:"); /* "data:" key has no value */
i+=5;
}
else
{
j = 0;
while ((buf[i] != '=') && (i < HEADER_SIZE) && (j < LINE_SIZE))
key[j++] = buf[i++];
for (j--; key[j] == ' ' && j > 0; j--); /* eat white space before = */
key[++j] = 0;
for (i++; buf[i] == ' ' && i < HEADER_SIZE; i++); /* eat white space after = */
j = 0;
while ((buf[i] != '\n') && (i < HEADER_SIZE) && (j < LINE_SIZE))
value[j++] = buf[i++];
for (j--; value[j] == ' ' && j > 0; j--); /* eat white space before \n */
value[++j] = 0;
}
i++; /* bump past '\n' */
if (tail != NULL)
*tail = buf + i; /* tail points to next line */
return i;
}
int ReadConfig()
{
char rcbuf[255];
FILE *inFile;
char *tail;
if((inFile = fopen(HPIODFILE, "r")) == NULL)
{
bug("unable to open %s: %m\n", HPIODFILE);
return 1;
}
if (fgets(rcbuf, sizeof(rcbuf), inFile) != NULL)
hpiod_port_num = strtol(rcbuf, &tail, 10);
fclose(inFile);
return 0;
}
//System::ParseMsg
//! Parse and convert all key value pairs in message. Do sanity check on values.
/*!
******************************************************************************/
int ParseMsg(char *buf, int len, MsgAttributes *ma)
{
char key[LINE_SIZE];
char value[LINE_SIZE];
char *tail, *tail2;
int i, ret=R_AOK;
ma->cmd[0] = 0;
ma->descriptor = -1;
ma->length = 0;
ma->channel = -1;
ma->data = NULL;
ma->result = -1;
ma->writelen = 0;
ma->readlen = 0;
ma->status = 0;
i = GetPair(buf, key, value, &tail);
if (strcasecmp(key, "msg") != 0)
{
bug("invalid message:%s\n", key);
return R_INVALID_MESSAGE;
}
strncpy(ma->cmd, value, sizeof(ma->cmd));
while (i < len)
{
i += GetPair(tail, key, value, &tail);
if (strcasecmp(key, "device-id") == 0)
{
ma->descriptor = strtol(value, &tail2, 10);
if (ma->descriptor < 0)
{
bug("invalid device descriptor:%d\n", ma->descriptor);
ret = R_INVALID_DESCRIPTOR;
break;
}
}
else if (strcasecmp(key, "channel-id") == 0)
{
ma->channel = strtol(value, &tail2, 10);
if (ma->channel < 0)
{
bug("invalid channel descriptor:%d\n", ma->channel);
ret = R_INVALID_CHANNEL_ID;
break;
}
}
else if (strcasecmp(key, "length") == 0)
{
ma->length = strtol(value, &tail2, 10);
if (ma->length > BUFFER_SIZE)
{
bug("invalid data length:%d\n", ma->length);
ret = R_INVALID_LENGTH;
}
}