00001 #ifndef __XPORT_H__
00002 #define __XPORT_H__
00003
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <sys/param.h>
00028 #include <sys/socket.h>
00029 #include <netinet/in.h>
00030 #include <arpa/inet.h>
00031 #include <netdb.h>
00032
00033
00034
00035 #ifdef _QNX
00036 #include <iostream.h>
00037 #include <iomanip.h>
00038 #include <strstream.h>
00039 #include <unix.h>
00040 #else
00041 #include <iostream>
00042 #include <iomanip>
00043 #include <sstream>
00044 #include <ctime>
00045 #endif
00046 #include <errno.h>
00047 #include "Utils.h"
00048 #include "qnx_port.h"
00049 #include "BicamConsts.h"
00050 #ifndef _QNX
00051 using namespace std;
00052 #endif
00053
00055 #define SERVER_PORT 1500
00056
00058 #define MAX_MSG 100
00059
00061 #define GPIO_IP_PORT 0x77F0
00062
00064 #define SERIAL_IP_PORT 10001
00065
00066
00068 #define XPCMD_GET_FUNCTIONS 0x10
00069
00070 #define XPCMD_GET_DIRECTIONS 0x11
00071
00072 #define XPCMD_GET_ACTIVE_LEVELS 0x12
00073
00074 #define XPCMD_GET_CURRENT_STATES 0x13
00075
00076 #define XPCMD_SET_DIRECTIONS 0x19
00077
00078 #define XPCMD_SET_ACTIVE_LEVELS 0x1A
00079
00080 #define XPCMD_SET_CURRENT_STATES 0x1B
00081
00082
00083
00084
00086 #define XPMASK_GPIO_SEL_ALL 0x07
00087
00088 #define XPMASK_GPIO_SEL_NONE 0x00
00089
00090 #define XPMASK_GPIO_SET_ALL 0x07
00091
00092 #define XPMASK_GPIO_CLR_ALL 0x00
00093
00094 #define XP_GPIO_0 0x1
00095
00096 #define XP_GPIO_1 0x2
00097
00098 #define XP_GPIO_2 0x4
00099
00100 #define XP_GPIO_ALL 0x7
00101
00102 #define XP_GPIO_NONE 0x0
00103
00104 #define XP_GPIO_IN 0x0
00105
00106 #define XP_GPIO_OUT 0x7
00107
00108 #define XP_GPIO_LO 0x0
00109
00110 #define XP_GPIO_HI 0x7
00111
00112
00114 #define ERR_NULL -1
00115
00116 #define ERR_BADCMD -2
00117
00118 #define ERR_BADPAR1 -3
00119
00120 #define ERR_BADPAR2 -4
00121
00122 #define TEST_FAILED -5
00123
00124 #define TEST_OK 0
00125
00126 #define IS_VALID 0
00127
00134 #define XPCMD_SEND_MESSAGE 0x7F
00135
00141 #define XPORT_COMMAND_LEN 9
00142
00147 #define XPORT_RESPONSE_LEN 5
00148
00152 #define MAX_XPORT_COMMANDS 32
00153
00155 typedef short unsigned int t_TCP_PORT;
00156
00158 #define XP_CMD_LEN (sizeof(unsigned char))
00159
00160 #define XP_PARAM_LEN (sizeof(unsigned int))
00161
00163 enum ConnectionID{XPCON_SERIAL=0,XPCON_GPIO,XPCON_ALL};
00164
00173 class XPortCommand{
00174 friend ostream& operator<<(ostream& output, const XPortCommand& n);
00175 protected:
00176 private:
00178 unsigned char command;
00180 unsigned int param1;
00182 unsigned int param2;
00183
00184 public:
00185 XPortCommand();
00186 XPortCommand(unsigned char cmd,unsigned int p1,unsigned int p2);
00187 void set(unsigned char cmd,unsigned int param1,unsigned int param2);
00188 int validate();
00189 int parse(const char *command);
00190 void show();
00191 unsigned char getCommand();
00192 unsigned int getP1();
00193 unsigned int getP2();
00194 char *toBytes(char *buf);
00195 };
00196
00208 class XPortResponse{
00209 friend ostream& operator<<(ostream& output, const XPortResponse& n);
00210
00211 protected:
00212 private:
00214 unsigned char command;
00216 unsigned int param1;
00217 public:
00218 XPortResponse();
00219 XPortResponse(unsigned char cmd,unsigned int p1);
00220 void set(unsigned char cmd,unsigned int param1);
00221 int validate();
00222 int parse(char *response);
00223 void show();
00224 unsigned char getCommand();
00225 unsigned int getP1();
00226 char *toBytes(char *buf);
00227 };
00228
00238 class XPortConnection{
00239 friend ostream& operator<<(ostream& output, const XPortConnection& n);
00240 protected:
00241 private:
00243 char *host;
00245 int gpioPort;
00247 int serialPort;
00249 unsigned int dirMask;
00251 unsigned int levelMask;
00253 unsigned int dirValues;
00255 unsigned int levelValues;
00257 int sdGPIO;
00259 int sdSERIAL;
00260 public:
00261 XPortConnection();
00262 XPortConnection(char *host,
00263 unsigned int gPort,
00264 unsigned int sPort,
00265 unsigned int dMask,
00266 unsigned int dValues,
00267 unsigned int lMask,
00268 unsigned int lValues);
00269 int configAll( char *host,
00270 unsigned int serialIPPort,
00271 unsigned int gpioIPPort,
00272 unsigned int dirMask,
00273 unsigned int dirValues,
00274 unsigned int levelMask,
00275 unsigned int levelValues);
00276 int configHost( char *hostName);
00277 int configGPIO( int gpioIPPort,
00278 unsigned int dirMask,
00279 unsigned int dirValues,
00280 unsigned int levelMask,
00281 unsigned int levelValues);
00282 int configSerial( int serialIPPort);
00283 void setsdGPIO(int sd);
00284 void setsdSerial(int sd);
00285 char *getHost();
00286 unsigned int getpGPIO();
00287 unsigned int getpSerial();
00288 int getsdGPIO();
00289 int getsdSerial();
00290 unsigned int getDirMask();
00291 unsigned int getDirValues();
00292 unsigned int getLevelMask();
00293 unsigned int getLevelValues();
00294
00295 };
00330 class XPort{
00332 friend ostream& operator<<(ostream& output, const XPort& n);
00333 protected:
00334 private:
00336 XPortConnection xpConnection;
00337
00338 int connectSocket(char *host, int port);
00339 public:
00340 XPort();
00341 XPort(char *host,
00342 unsigned int gpioPort,
00343 unsigned int serialPort,
00344 unsigned int dirMask,
00345 unsigned int dirValues,
00346 unsigned int levelMask,
00347 unsigned int levelValues);
00348 XPortConnection *getConnection();
00349 int configure( char *host,
00350 unsigned int gpioIPPort,
00351 unsigned int serialIPPort,
00352 unsigned int dMask,
00353 unsigned int dValues,
00354 unsigned int lMask,
00355 unsigned int lValues);
00356 int connectxp(ConnectionID select,boolean doConfig);
00357 int connectxp(ConnectionID select);
00358 int disconnectxp(ConnectionID select);
00359 int writeBytes(int sd, char *buf, int len);
00360 int readBytes(int sd, char *buf, int len);
00361 int writeCommand(XPortCommand *xpCommand);
00362 int readResponse(XPortResponse *xpResponse);
00363 int writeRead(XPortCommand *xpCommand, XPortResponse *xpResponse);
00364 int writeSerial(const char *msg, unsigned int len, unsigned int offset);
00365 char *getHost();
00366 unsigned int getpGPIO();
00367 unsigned int getpSerial();
00368 int getsdGPIO();
00369 int getsdSerial();
00370
00371 };
00372
00373 #endif