00001 /***************************************************************************** 00002 * 00003 * $RCSfile: Serial_8c-source.html,v $ 00004 * 00005 * GPS4Palm Serial Manager API Wrapper Functions - 00006 * either the new or the old API is used 00007 * 00008 * Based on the example "GPS" from the book 00009 * "Palm Programming, the Developer's Guide, 2nd edition" 00010 * by O'Reilly. 00011 * (Copyright (c) 1998-2001, Neil Rhodes and Julie McKeehan neil@pobox.com) 00012 * 00013 * This program is Copyright (C) 03/2003 Matthias Prinke 00014 * <matthias.prinke@surfeu.de> and covered by GNU's GPL. 00015 * In particular, this program is free software and comes WITHOUT 00016 * ANY WARRANTY. 00017 * 00018 * $Author: mp $ 00019 * 00020 * $Date: 2007-10-08 20:40:34 $ 00021 * 00022 * $Revision: 1.7.2.1 $ 00023 * 00024 * $Log: Serial_8c-source.html,v $ 00024 * Revision 1.7.2.1 2007-10-08 20:40:34 mp 00024 * updated for gps4palm V0.9.5 beta 00024 * 00025 * Revision 1.5 2005-05-06 13:33:41 mp 00026 * added BluetoothOpen() 00027 * 00028 * Revision 1.4 2005/01/30 21:26:18 mp 00029 * added gCncManagerFtr, gProfiles, gNumProfiles, and FreeCncProfileList() 00030 * 00031 * Revision 1.3 2004/12/18 16:12:27 mp 00032 * fixed serial port re-opening for PDAs with New Serial Manager 00033 * 00034 * Revision 1.2 2004/04/30 15:54:59 mp 00035 * modified comments for doxygen 00036 * 00037 * Revision 1.1.1.1 2003/07/14 18:59:29 mp 00038 * Imported GPS4Palm to CVS revision control. 00039 * 00040 * 00041 ****************************************************************************/ 00042 #include <PalmOS.h> 00043 #include <SerialMgrOld.h> 00044 #include <BtCommVdrv.h> 00045 #include "Serial.h" 00046 00047 /** New Serial Manager available */ 00048 Boolean gNewSerialManager; 00049 00050 /** Connection Manager feature set available */ 00051 Boolean gCncManagerFtr; 00052 00053 /** Serial port ID */ 00054 UInt16 gPortID; 00055 00056 /** four-character port name or logical port number (New Serial Manager) */ 00057 UInt32 gSerialPort; 00058 00059 /** List of Connection Manager Profiles */ 00060 Char **gProfiles; 00061 00062 /** Number of Connection Manager Profiles */ 00063 UInt16 gNumProfiles; 00064 00065 /** 00066 * Serial port receive buffer. 00067 * 900 bytes should be more than enough for one second of 00068 * data: 10 lines @ 80 chars per line. 00069 */ 00070 char gSerialBuffer[900]; 00071 00072 00073 /**********************************************************************/ 00074 /** 00075 * Serial Manager/New Serial Manager wrapper function for data reception. 00076 * 00077 * \param portId port ID 00078 * \param rcvBufP ptr to receive buffer 00079 * \param count number of bytes to receive 00080 * \param timeout timeout in ticks 00081 * \param errP error code 00082 * 00083 * \return Number of bytes received. 00084 **********************************************************************/ 00085 UInt32 DoReceive(UInt16 portId, void *rcvBufP, UInt32 count, Int32 timeout, Err *errP) 00086 { 00087 if (gNewSerialManager) 00088 return SrmReceive(portId, rcvBufP, count, timeout, errP); 00089 else 00090 return SerReceive(portId, rcvBufP, count, timeout, errP); 00091 } 00092 00093 00094 /**********************************************************************/ 00095 /** 00096 * Serial Manager/New Serial Manager wrapper function to set receive 00097 * buffer. 00098 * 00099 * \param portId port ID 00100 * \param bufP ptr to receive buffer 00101 * \param bufSize size of buffer in bytes 00102 * 00103 * \return Returns 0 if successful. 00104 ********************************************************************/ 00105 Err DoSetReceiveBuffer(UInt16 portId, void *bufP, UInt16 bufSize) 00106 { 00107 if (gNewSerialManager) 00108 return SrmSetReceiveBuffer(portId, bufP, bufSize); 00109 else 00110 return SerSetReceiveBuffer(portId, bufP, bufSize); 00111 } 00112 00113 00114 /**********************************************************************/ 00115 /** 00116 * Serial Manager/New Serial Manager wrapper function to wait for 00117 * at least specified number of bytes in receive buffer. 00118 * 00119 * \param portId port ID 00120 * \param bytes number of bytes desired 00121 * \param timeout timeout in ticks 00122 * 00123 * \return Returns 00124 * - 0 if successful 00125 * - serErrTimeOut if timeout occurred 00126 * - serErrLineErr if line error ocurred 00127 ********************************************************************/ 00128 Err DoReceiveWait(UInt16 portId, UInt32 bytes, Int32 timeout) 00129 { 00130 if (gNewSerialManager) 00131 return SrmReceiveWait(portId, bytes, timeout); 00132 else 00133 return SerReceiveWait(portId, bytes, timeout); 00134 } 00135 00136 Err DoReceiveFlush(UInt16 portId, Int32 timeout) 00137 { 00138 if (gNewSerialManager) 00139 return SrmReceiveFlush(portId, timeout); 00140 else { 00141 SerReceiveFlush(portId, timeout); 00142 return 0; 00143 } 00144 } 00145 00146 00147 /**********************************************************************/ 00148 /** 00149 * Serial Manager/New Serial Manager wrapper function to return 00150 * the count of bytes presently in the receive queue. 00151 * 00152 * \param portId port ID 00153 * \param numBytesP number of bytes in receive queue 00154 * 00155 * \return 00156 * - 0 if no error 00157 * - serErrLineErr if line error pending 00158 **********************************************************************/ 00159 Err DoReceiveCheck(UInt16 portId, UInt32 *numBytesP) 00160 { 00161 if (gNewSerialManager) 00162 return SrmReceiveCheck(portId, numBytesP); 00163 else 00164 return SerReceiveCheck(portId, numBytesP); 00165 } 00166 00167 00168 /**********************************************************************/ 00169 /** 00170 * Serial Manager/New Serial Manager wrapper function to close port. 00171 * 00172 * \param portId port ID 00173 * 00174 * \return 00175 * - 0 if no error 00176 * - serErrNotOpen if port wasn't open 00177 * - serErrStillOpen if port still held open by 00178 * another process. 00179 **********************************************************************/ 00180 Err DoClose(UInt16 portId) 00181 { 00182 if (gNewSerialManager) 00183 return SrmClose(portId); 00184 else 00185 return SerClose(portId); 00186 } 00187 00188 00189 /****************************************************************************/ 00190 /** Deallocation of memory allocated by CncGetProfileList(). 00191 * 00192 ****************************************************************************/ 00193 void FreeCncProfileList(void) 00194 { 00195 00196 if (gProfiles) { 00197 UInt16 i; 00198 00199 for (i = 0; i < gNumProfiles; i++) 00200 if (gProfiles[i]) 00201 MemPtrFree(gProfiles[i]); 00202 MemPtrFree(gProfiles); 00203 gProfiles = NULL; 00204 } 00205 } 00206 00207 00208 /**************************************************************************/ 00209 /** 00210 * Open Bluetooth Virtual Serial Port and connect to known device (address) 00211 * 00212 * 00213 * \param port port name or logical port number 00214 * \param baud baud rate 00215 * \param bt_addr Bluetooth Device Address 00216 * \param PortID port ID returned by reference 00217 * 00218 * \return see SrmExtOpen() 00219 **************************************************************************/ 00220 Err BluetoothOpen(UInt32 port, UInt32 baud, UInt8 *bt_addr, UInt16 *PortID) 00221 { 00222 /* 00223 * Bluetooth Vitrual Serial Port -- 00224 * Connect to known device (address) 00225 */ 00226 SrmOpenConfigType config; /* port's properties */ 00227 BtVdOpenParams btParams; /* BT parameters */ 00228 int i; /* loop index */ 00229 Err err; /* error code */ 00230 00231 config.baud = baud; /* baud rate */ 00232 config.function = 0; /* default */ 00233 config.drvrDataP = (MemPtr)&btParams; 00234 config.drvrDataSize = sizeof(BtVdOpenParams); 00235 btParams.role = btVdClient; /* PDA is client */ 00236 /* UUID: Universal Unique Identifier */ 00237 btParams.u.client.method = btVdUseUuidList; /* no particular channel */ 00238 btParams.u.client.u.uuidList.len = 0; /* any service class */ 00239 btParams.authenticate = false; /* no authentification */ 00240 btParams.encrypt = false; /* no encryption */ 00241 00242 /* Copy device address from Application Preferences */ 00243 /* User will be queried if zero */ 00244 for (i=0; i<btLibDeviceAddressSize; i++) { 00245 btParams.u.client.remoteDevAddr.address[i] = bt_addr[i]; 00246 } 00247 00248 /* open BT serial port */ 00249 err = SrmExtOpen(port, &config, sizeof(config), PortID); 00250 00251 return err; 00252 }