📄 hpiom.c
字号:
/*****************************************************************************\ 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 attributesstatic 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 compensationstatic unsigned char gpPenAlignmentQuery[] = { 0x0F, 0x00, 0x00, 0x00 }; // 15 - return pen alignment valueint 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; } } else if (strcasecmp(key, "data:") == 0) { ma->data = (unsigned char *)tail; break; /* done parsing */ } else if (strcasecmp(key, "result-code") == 0) { ma->result = strtol(value, &tail2, 10); } else if (strcasecmp(key, "bytes-written") == 0) { ma->writelen = strtol(value, &tail2, 10); } else if (strcasecmp(key, "bytes-to-read") == 0) { ma->readlen = strtol(value, &tail2, 10); if (ma->readlen > BUFFER_SIZE) { bug("invalid read length:%d\n", ma->readlen); ret = R_INVALID_LENGTH; } } else if (strcasecmp(key, "status-code") == 0) { ma->status = strtol(value, &tail2, 10); } else if (strcasecmp(key, "status-name") == 0) { continue; /* ignor */ } else if (strcasecmp(key, "encoding") == 0) { continue; /* ignor */ } else { /* Unknown keys are ignored (R_AOK). */// bug("invalid key:%s\n", key); } } // end while (i < len) return ret;}int OpenHP(char *dev){ char message[512]; struct sockaddr_in pin; struct hostent *server_host_name; int len=0, fd=-1; MsgAttributes ma; ReadConfig(); server_host_name = gethostbyname("localhost"); bzero(&pin, sizeof(pin)); pin.sin_family = AF_INET; pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr; pin.sin_port = htons(hpiod_port_num); if ((hpiod_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { bug("unable to open socket %d: %m\n", hpiod_port_num); goto mordor; } if (connect(hpiod_socket, (void *)&pin, sizeof(pin)) == -1) { bug("unable to connect to socket %d: %m\n", hpiod_port_num); goto mordor; } len = sprintf(message, "msg=DeviceOpen\ndevice-uri=%s\n", dev); if (send(hpiod_socket, message, len, 0) == -1) { bug("unable to send DeviceOpen: %m\n"); goto mordor; } if ((len = recv(hpiod_socket, message, sizeof(message), 0)) == -1) { bug("unable to receive DeviceOpenResult: %m\n"); goto mordor; } message[len] = 0; ParseMsg(message, len, &ma); if (ma.result == R_AOK) fd = ma.descriptor;mordor: return fd;}int CloseHP(int hd){ char message[512]; int len=0; len = sprintf(message, "msg=DeviceClose\ndevice-id=%d\n", hd); if (send(hpiod_socket, message, len, 0) == -1) { bug("unable to send DeviceClose: %m\n"); goto mordor; } if ((len = recv(hpiod_socket, message, sizeof(message), 0)) == -1) { bug("unable to receive DeviceCloseResult: %m\n"); goto mordor; } message[len] = 0;mordor: close(hpiod_socket); hpiod_socket = -1; return 0;}/* Get device id string. Assume binary length value at begining of string has been removed. */int ReadHPDeviceID(int hd, char *buf, int bufSize){ char message[512]; int len=0; MsgAttributes ma; buf[0] = 0; len = sprintf(message, "msg=DeviceID\ndevice-id=%d\n", hd); if (send(hpiod_socket, message, len, 0) == -1) { bug("unable to send DeviceID: %m\n"); goto mordor; } if ((len = recv(hpiod_socket, buf, bufSize, 0)) == -1) { bug("unable to receive DeviceIDResult: %m\n"); goto mordor; } buf[len] = 0; ParseMsg(buf, len, &ma); if (ma.result == R_AOK) { len = ma.length; memcpy(buf, ma.data, len); } else len = 0; /* error */mordor: return len;} int ReadHPStatus(int hd, char *buf, int size){ char message[512]; int len=0; MsgAttributes ma; buf[0] = 0; len = sprintf(message, "msg=DeviceStatus\ndevice-id=%d\n", hd); if (send(hpiod_socket, message, len, 0) == -1) { bug("unable to send DeviceStatus: %m\n"); goto mordor; } if ((len = recv(hpiod_socket, message, sizeof(message), 0)) == -1) { bug("unable to receive DeviceStatusResult: %m\n"); goto mordor; } message[len] = 0; ParseMsg(message, len, &ma); if (ma.result == R_AOK) { len = 1; buf[0] = ma.status; } else len = 0; /* error */mordor: return len;} int WriteHP(int hd, int channel, char *buf, int size){ char message[BUFFER_SIZE+HEADER_SIZE]; int len=0, slen=0; MsgAttributes ma; len = sprintf(message, "msg=ChannelDataOut\ndevice-id=%d\nchannel-id=%d\nlength=%d\ndata:\n", hd, channel, size); if (size+len > sizeof(message)) { bug("unable to fill data buffer: size=%d\n", size); goto mordor; } memcpy(message+len, buf, size); if (send(hpiod_socket, message, size+len, 0) == -1) { bug("unable to send ChannelDataOut: %m\n"); goto mordor; } if ((len = recv(hpiod_socket, message, sizeof(message), 0)) == -1) { bug("unable to receive ChannelDataOutResult: %m\n"); goto mordor; } message[len] = 0; ParseMsg(message, len, &ma); slen = ma.writelen;mordor: return slen;}int ReadHP(int hd, int channel, char *buf, int size){ char message[BUFFER_SIZE+HEADER_SIZE]; int len=0, rlen=0; MsgAttributes ma; len = sprintf(message, "msg=ChannelDataIn\ndevice-id=%d\nchannel-id=%d\nbytes-to-read=%d\n", hd, channel, size); if (size+len > sizeof(message)) { fprintf(stderr, "Error data size=%d\n", size); goto mordor; } if (send(hpiod_socket, message, size+len, 0) == -1) { bug("unable to send ChannelDataIn: %m\n"); goto mordor; } if ((len = recv(hpiod_socket, message, sizeof(message), 0)) == -1) { bug("unable to receive ChannelDataInResult: %m\n"); goto mordor; } message[len] = 0; ParseMsg(message, len, &ma); if (ma.result == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -