📄 blue.h
字号:
/* * blue.h * * Author : Lionetti Salvatore <salvatorelionetti@yahoo.it> * License: GPL * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#ifndef __BLUE_H_#define __BLUE_H_/* Redundant parameter.#undef LITTLE_ENDIAN#define BIG_ENDIAN*//* Module parameter.*/#define MAX_PENDING_CMD_CLASSA 1 #define MAX_PENDING_CMD_CLASSB 10#define MAX_PDU_LEN 2570 /* Bytes, either4 cmd,event. readRemoteName=255+2*//* 1) ALWAY CAN EXIST A SPURIOUS PARAMETER, SW MUST BE ROBUST. * 2) TEST WITH TWO CONNECT REQUEST IN QUEUE (4 A MAC role) */#define MAX_PENDING_CMD MAX_PENDING_CMD_CLASSA+MAX_PENDING_CMD_CLASSBstruct hciCmd { short int cmd; unsigned char len;};struct hciEvt { unsigned char evt; unsigned char len;};/* * This class support trasition from null->info->info ... * null mean no info. */class PossibleNullValue { bool stato;public: PossibleNullValue() {stato=false;} bool isNull() {return stato;} bool haveValue() {bool oldS=stato; stato=true; return oldS;}};/* HciReqRes: take care of build a request on a (char*, len) * and is be able to find his response/responses. * * Write, read command * hci<----> HciReqRes1 * | * |-> HciReqRes2 * | * |-> HciReqRes3 * * * Only 1 consume data, like a puzzle. * So we need inverse communication (i see if ok then send 'consumeThis'2hci * or use model (shared info, hci what i want) * * So instead of use count: 1 shared object, N user, * N User decide when destroy object. * we use atexit() f() : 1 stack/heap object, on destructor do all work * User can't do any work after destr. * 1 object decide when start/stop. */ struct HciReqRes { char *req; int reqL; int reqC; /* indicate current bytes written in req.*/ char *res; int resL; int resC; /* indicate current bytes written in req.*/ bool requestOn; enum ProtoT {HCI, L2CAP} proto; typedef void (*atExitT)(void*,HciReqRes&); atExitT atExitF; void* atExitP;public: HciReqRes(); virtual ~HciReqRes(); bool done(); enum ProtoT getProto(); void setProto(enum ProtoT pro); /* return: * <0 if we accept response but have error, (requestOn=0) * =0 if we !accept this response (requestOn=1) * >0 if we accept only some bytes of response. * * If buffer req[],res[] exist => default behavior is read/write * in such a buffer. * * Normal behavior: * writeReq() dump request to specified destination. * readRes() dump response from specified source.*/ virtual int readRes(char* des, int len); virtual int writeReq(char* des, int len); /* Help compiling request.*/ int compileReq(char* opt, int len); int scanRes(char* opt, int len); /* So we enter on this object only with method. * Usefule to change storage class.*/ int getReqC(); int getResC(); void setDone(bool idone); void atExit(void*,atExitT);};class HciBus { int fdCommand; int fdEvent; int fdDataAsync; int fdDataSync; char* lastPdu; /*[MAX_PDU_LEN]*/; int lastPduL; int lastPduC; HciReqRes* hcireqres[MAX_PENDING_CMD];public: HciBus(); ~HciBus(); /* User schedule build pdu. Ignore done(). * ret <0 if no space. * prio is used to ensure that some command * are ensured to read/write before other cmd (es sniffer).*/ int schedule(HciReqRes& hcirr, char classe='b'); /* Remove (one|all) command from queue so !response is needed.*/ int clear(HciReqRes& hcirr); int clear(); /* Basic handshaking.*/ int writeHci(char* data,int len); int writeHci(int fd); /* Sync read: wait to read a complete pdu. * ret>=0 if ok * <0 if nok */ int readMsg(HciReqRes::ProtoT proto); /* * 0 , ok model match * -EAGAIN, model !match, retry with another model, */ int readHci(HciReqRes::ProtoT proto); int hci_readEvt(); int l2cap_readEvt(); /* Wait 1 message.*/ int waitRes(int ms, HciReqRes::ProtoT proto); int hci_waitRes(int ms); int l2cap_waitRes(int ms); /* Command done with special device = host bluetooth firmware. * Now mac is sufficient to refer to device. List is in appl.*/ int readMac(char* mac); int searchDevice(char mac[][6],char opts[][8], int nMac,int nSec=5); int readRemoteName(char* mac, char* opt, char* nome, int nomeL); int Dump();};/* When message is detroyed.*/void onMessageDestroy(HciBus* hb, HciReqRes& hcirr);bool isEmpty(char* str,int len);enum ConnInitByT {HOST, DEVICE};class HciConn { char mac[6]; char pin[4]; char linkkey[16]; HciBus& hciBus; unsigned int hciScoHandle; unsigned int hciL2capHandleDst; unsigned int hciL2capHandleSrc; unsigned int hciHandle;public: HciConn(HciBus& hcib); ~HciConn(); /* Return hci handle.*/ int writeConf(); int connectHci(enum ConnInitByT whoStartConn); int connectSco(); int connectL2cap(); int disconnectL2cap(int dstHandle, int srcHandle); int disconnectHci(int handle); int disconnect(); /* Parameter specific for connectHci.*/ int getMac(char mac[6]); int getPin(char pin[4]); int getLinkKey(char lin[16]); int setMac(char mac[6]); int setPin(char pin[4]); int setLinkKey(char lin[16]);};#if 0class BlueDevice { char mac[6]; HciConn *conn;public: BlueDevice(char* mac=NULL, HciConn* conn); ~BlueDevice(); void setMac(char* mac); void setConn(HciConn conn); /* To have list of device.*/ static int add(BlueDevice* bd); static int del(BlueDevice* bd);};#endif#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -