Skip to content

Commit

Permalink
Change OneWire API to use a logical port number in the range 0..MAX_P…
Browse files Browse the repository at this point in the history
…ORTNUM-1

OneWire constructor extended to accept a logical pin number in the range 0..MAX_PORTNUM-1
and check in range in subsequint calls to avoid reference outside array bounds.
  • Loading branch information
doingnz committed May 22, 2016
1 parent 4f3ef31 commit 3543b56
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
struct Library_spot_hardware_onewire_native_Microsoft_SPOT_Hardware_OneWire
{
static const int FIELD___pin = 1;

static const int FIELD___logicalPort = 2;
TINYCLR_NATIVE_DECLARE(TouchReset___I4);
TINYCLR_NATIVE_DECLARE(TouchBit___I4__I4);
TINYCLR_NATIVE_DECLARE(TouchByte___I4__I4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,80 +11,81 @@ using namespace Microsoft::SPOT::Hardware;
INT32 OneWire::TouchReset( CLR_RT_HeapBlock* pMngObj, HRESULT &hr )
{
INT32 retVal = 0;
UINT32 pin = Get__pin(pMngObj);
retVal = owTouchReset(pin);
UINT32 pin = Get__logicalPort(pMngObj);
retVal = owTouchReset(pin);
return retVal;
}

INT32 OneWire::TouchBit( CLR_RT_HeapBlock* pMngObj, INT32 param0, HRESULT &hr )
{
INT32 retVal = 0;
UINT32 pin = Get__pin(pMngObj);
retVal = owTouchBit(pin, param0);
UINT32 pin = Get__logicalPort(pMngObj);
retVal = owTouchBit(pin, param0);
return retVal;
}

INT32 OneWire::TouchByte( CLR_RT_HeapBlock* pMngObj, INT32 param0, HRESULT &hr )
{
INT32 retVal = 0;
UINT32 pin = Get__pin(pMngObj);
retVal = owTouchByte(pin, param0);
UINT32 pin = Get__logicalPort(pMngObj);
retVal = owTouchByte(pin, param0);
return retVal;
}

INT32 OneWire::WriteByte( CLR_RT_HeapBlock* pMngObj, INT32 param0, HRESULT &hr )
{
INT32 retVal = 0;
UINT32 pin = Get__pin(pMngObj);
retVal = owWriteByte(pin, param0);
UINT32 pin = Get__logicalPort(pMngObj);
retVal = owWriteByte(pin, param0);
return retVal;
}

INT32 OneWire::ReadByte( CLR_RT_HeapBlock* pMngObj, HRESULT &hr )
{
INT32 retVal = 0;
UINT32 pin = Get__pin(pMngObj);
retVal = owReadByte(pin);
UINT32 pin = Get__logicalPort(pMngObj);
retVal = owReadByte(pin);
return retVal;
}

INT32 OneWire::AcquireEx( CLR_RT_HeapBlock* pMngObj, HRESULT &hr )
{
INT32 retVal = 0;
UINT32 pin = Get__pin(pMngObj);
retVal = owAcquire(pin, "\\\\OneWire\\");
UINT32 pin = Get__pin(pMngObj);
UINT32 logicalPort = Get__logicalPort(pMngObj);
retVal = owAcquire(logicalPort, pin);
return retVal;
}

INT32 OneWire::Release( CLR_RT_HeapBlock* pMngObj, HRESULT &hr )
{
INT32 retVal = 0;
UINT32 pin = Get__pin(pMngObj);
owRelease(pin);
UINT32 pin = Get__logicalPort(pMngObj);
owRelease(pin);
return retVal;
}

INT32 OneWire::First( CLR_RT_HeapBlock* pMngObj, INT8 param0, INT8 param1, HRESULT &hr )
{
INT32 retVal = 0;
UINT32 pin = Get__pin(pMngObj);
retVal = owFirst(pin, param0, param1);
UINT32 pin = Get__logicalPort(pMngObj);
retVal = owFirst(pin, param0, param1);
return retVal;
}

INT32 OneWire::Next( CLR_RT_HeapBlock* pMngObj, INT8 param0, INT8 param1, HRESULT &hr )
{
INT32 retVal = 0;
UINT32 pin = Get__pin(pMngObj);
retVal = owNext(pin, param0, param1);
UINT32 pin = Get__logicalPort(pMngObj);
retVal = owNext(pin, param0, param1);
return retVal;
}

INT32 OneWire::SerialNum( CLR_RT_HeapBlock* pMngObj, CLR_RT_TypedArray_UINT8 param0, INT8 param1, HRESULT &hr )
{
INT32 retVal = 0;
UINT32 pin = Get__pin(pMngObj);
owSerialNum(pin, param0.GetBuffer(), param1);
UINT32 pin = Get__logicalPort(pMngObj);
owSerialNum(pin, param0.GetBuffer(), param1);
return retVal;
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace Microsoft
struct OneWire
{
// Helper Functions to access fields of managed object
static UINT32& Get__pin( CLR_RT_HeapBlock* pMngObj ) { return Interop_Marshal_GetField_UINT32( pMngObj, Library_spot_hardware_onewire_native_Microsoft_SPOT_Hardware_OneWire::FIELD___pin ); }
static UINT32& Get__pin( CLR_RT_HeapBlock* pMngObj ) { return Interop_Marshal_GetField_UINT32( pMngObj, Library_spot_hardware_onewire_native_Microsoft_SPOT_Hardware_OneWire::FIELD___pin ); }
static UINT32& Get__logicalPort( CLR_RT_HeapBlock* pMngObj ) { return Interop_Marshal_GetField_UINT32( pMngObj, Library_spot_hardware_onewire_native_Microsoft_SPOT_Hardware_OneWire::FIELD___logicalPort ); }

// Declaration of stubs. These functions are implemented by Interop code developers
static INT32 TouchReset( CLR_RT_HeapBlock* pMngObj, HRESULT &hr );
Expand Down
8 changes: 6 additions & 2 deletions DeviceCode/pal/OneWire/DallasSemi/crcutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static uchar dscrc_table[] = {
//
void setcrc16(int portnum, ushort reset)
{
utilcrc16[portnum&0x0FF] = reset;
if((portnum < MAX_PORTNUM) && (portnum >= 0) ) utilcrc16[portnum&0x0FF] = reset;
return;
}

Expand All @@ -72,7 +72,7 @@ void setcrc16(int portnum, ushort reset)
//
void setcrc8(int portnum, uchar reset)
{
utilcrc8[portnum&0x0FF] = reset;
if((portnum < MAX_PORTNUM) && (portnum >= 0) ) utilcrc8[portnum&0x0FF] = reset;
return;
}

Expand All @@ -88,6 +88,8 @@ void setcrc8(int portnum, uchar reset)
//
ushort docrc16(int portnum, ushort cdata)
{
if((portnum >= MAX_PORTNUM) || (portnum < 0)) return 0;

cdata = (cdata ^ (utilcrc16[portnum&0x0FF] & 0xff)) & 0xff;
utilcrc16[portnum&0x0FF] >>= 8;

Expand All @@ -114,6 +116,8 @@ ushort docrc16(int portnum, ushort cdata)
//
uchar docrc8(int portnum, uchar x)
{
if((portnum >= MAX_PORTNUM) || (portnum < 0)) return 0;

utilcrc8[portnum&0x0FF] = dscrc_table[utilcrc8[portnum&0x0FF] ^ x];
return utilcrc8[portnum&0x0FF];
}
26 changes: 19 additions & 7 deletions DeviceCode/pal/OneWire/DallasSemi/ownet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@
SMALLINT bitacc(SMALLINT,SMALLINT,SMALLINT,uchar *);

// global variables for this module to hold search state information
static SMALLINT LastDiscrepancy[MAX_PORTNUM];
static SMALLINT LastFamilyDiscrepancy[MAX_PORTNUM];
static SMALLINT LastDevice[MAX_PORTNUM];
uchar SerialNum[MAX_PORTNUM][8];
static SMALLINT LastDiscrepancy[MAX_PORTNUM] = { 0 };
static SMALLINT LastFamilyDiscrepancy[MAX_PORTNUM] = { 0 };
static SMALLINT LastDevice[MAX_PORTNUM] = { 0 };
SMALLINT owPortPin[MAX_PORTNUM] = { 0 };
uchar SerialNum[MAX_PORTNUM][8] = { 0 };

//--------------------------------------------------------------------------
// The 'owFirst' finds the first device on the 1-Wire Net This function
Expand Down Expand Up @@ -109,6 +110,8 @@ SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only)
uchar serial_byte_mask;
uchar lastcrc8;

if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;

// initialize for search
lastcrc8=0;
bit_number = 1;
Expand Down Expand Up @@ -262,7 +265,11 @@ SMALLINT owNext(int portnum, SMALLINT do_reset, SMALLINT alarm_only)
void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read)
{
uchar i;

if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0))
{
if (serialnum_buf != NULL) *serialnum_buf=0;
return;
}
// read the internal buffer and place in 'serialnum_buf'
if (do_read)
{
Expand All @@ -289,6 +296,8 @@ void owSerialNum(int portnum, uchar *serialnum_buf, SMALLINT do_read)
void owFamilySearchSetup(int portnum, SMALLINT search_family)
{
uchar i;
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return;


// set the search state to find SearchFamily type devices
SerialNum[portnum][0] = search_family;
Expand All @@ -306,6 +315,8 @@ void owFamilySearchSetup(int portnum, SMALLINT search_family)
//
void owSkipFamily(int portnum)
{
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return;

// set the Last discrepancy to last family discrepancy
LastDiscrepancy[portnum] = LastFamilyDiscrepancy[portnum];
LastFamilyDiscrepancy[portnum] = 0;
Expand Down Expand Up @@ -333,6 +344,7 @@ SMALLINT owAccess(int portnum)
{
uchar sendpacket[9];
uchar i;
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return FALSE;

// reset the 1-wire
if (owTouchReset(portnum))
Expand Down Expand Up @@ -396,7 +408,7 @@ SMALLINT owVerify(int portnum, SMALLINT alarm_only)
{
uchar i,sendlen=0,goodbits=0,cnt=0,s,tst;
uchar sendpacket[50];

if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return FALSE;
// construct the search
if (alarm_only)
sendpacket[sendlen++] = 0xEC; // issue the alarming search command
Expand Down Expand Up @@ -460,7 +472,7 @@ SMALLINT owOverdriveAccess(int portnum)
{
uchar sendpacket[8];
uchar i, bad_echo = FALSE;

if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return FALSE;
// make sure normal level
owLevel(portnum,MODE_NORMAL);

Expand Down
10 changes: 6 additions & 4 deletions DeviceCode/pal/OneWire/DallasSemi/ownet.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,10 @@ SMALLINT owVerify(int portnum, SMALLINT alarm_only);
SMALLINT owOverdriveAccess(int portnum);


// external One Wire functions defined in owsesu.c
SMALLINT owAcquire(int portnum, char *port_zstr);
int owAcquireEx(char *port_zstr);
void owRelease(int portnum);
// external One Wire functions defined in OneWireLinkLevelSession.cpp
SMALLINT owAcquire(int portnum, int pin);
//int owAcquireEx(char *port_zstr);
void owRelease(int portnum);

// external One Wire functions defined in findtype.c
// SMALLINT FindDevices(int,uchar FamilySN[][8],SMALLINT,int);
Expand Down Expand Up @@ -433,4 +433,6 @@ ushort docrc16(int portnum, ushort cdata);
void setcrc8(int portnum, uchar reset);
uchar docrc8(int portnum, uchar x);

extern SMALLINT owPortPin[MAX_PORTNUM];

#endif //OWNET_H
20 changes: 15 additions & 5 deletions DeviceCode/pal/OneWire/OneWireLinkLevelDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ static SMALLINT ULevel; // current 1-Wire Net level
SMALLINT owTouchReset(int portnum)
{
uchar result;
UINT32 pin = (UINT32)portnum;
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;

UINT32 pin = (UINT32)owPortPin[portnum];

// Code from appnote 126.
CPU_GPIO_EnableOutputPin( pin, false ); // impulse start OW_PORT = 0; // drive bus low.
Expand Down Expand Up @@ -94,7 +96,10 @@ SMALLINT owTouchReset(int portnum)
SMALLINT owTouchBit(int portnum, SMALLINT sendbit)
{
unsigned char result=0;
UINT32 pin = (UINT32)portnum;

if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0) ) return 0;

UINT32 pin = (UINT32)owPortPin[portnum];

//timing critical, so I'll disable interrupts here
GLOBAL_LOCK(irq); //EA = 0;
Expand Down Expand Up @@ -156,6 +161,8 @@ SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
uchar i;
uchar result = 0;

if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;

for (i = 0; i < 8; i++)
{
result |= (owTouchBit(portnum,sendbyte & 1) << i);
Expand All @@ -179,6 +186,8 @@ SMALLINT owTouchByte(int portnum, SMALLINT sendbyte)
//
SMALLINT owWriteByte(int portnum, SMALLINT sendbyte)
{
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;

return (owTouchByte(portnum,sendbyte) == sendbyte) ? TRUE : FALSE;
}

Expand All @@ -193,6 +202,7 @@ SMALLINT owWriteByte(int portnum, SMALLINT sendbyte)
//
SMALLINT owReadByte(int portnum)
{
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;
return owTouchByte(portnum,0xFF);
}

Expand All @@ -209,7 +219,7 @@ SMALLINT owReadByte(int portnum)
//
SMALLINT owSpeed(int portnum, SMALLINT new_speed)
{
portnum = 0;
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;

USpeed = new_speed;
// not supported yet
Expand All @@ -236,7 +246,7 @@ SMALLINT owSpeed(int portnum, SMALLINT new_speed)
// Note: Strong and Program not supported on 520 target.
SMALLINT owLevel(int portnum, SMALLINT new_level)
{
portnum = 0;
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] ==0)) return 0;

UINT32 pin = (UINT32)portnum;

Expand Down Expand Up @@ -268,7 +278,7 @@ SMALLINT owLevel(int portnum, SMALLINT new_level)
//
SMALLINT owProgramPulse(int portnum)
{
portnum = 0;
//portnum = 0;
// Not supported
return 0;
}
Expand Down
31 changes: 23 additions & 8 deletions DeviceCode/pal/OneWire/OneWireLinkLevelSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
//extern void usDelay(int);

// local function prototypes
SMALLINT owAcquire(int,char *);
void owRelease(int);
//SMALLINT owAcquire(int,int);
//void owRelease(int);

//---------------------------------------------------------------------------
// Attempt to acquire a 1-Wire net
Expand All @@ -50,12 +50,12 @@ void owRelease(int);
//
// Returns: TRUE - success, port opened
//
SMALLINT owAcquire(int portnum, char *port_zstr)
SMALLINT owAcquire(int portnum, int pin)
{
port_zstr = 0;
if((portnum >= MAX_PORTNUM) || (portnum < 0) || (owPortPin[portnum] != 0)) return FALSE; // already in use

//portnum = 0;
UINT32 pin = (UINT32)portnum;
//UINT32 pin = (UINT32)atoi(port_zstr);

// drive bus high.
CPU_GPIO_EnableOutputPin( pin, true );
Expand All @@ -65,9 +65,17 @@ SMALLINT owAcquire(int portnum, char *port_zstr)

// checks to make sure the line is idling high.
CPU_GPIO_EnableInputPin( pin, false, NULL, GPIO_INT_EDGE_HIGH, RESISTOR_PULLUP );
return (CPU_GPIO_GetPinState(pin) ==1 ? TRUE : FALSE);

if (CPU_GPIO_GetPinState(pin)==1)
{
owPortPin[portnum] = pin;
return TRUE;
}
else
{
return FALSE;
}
}

//---------------------------------------------------------------------------
// Release the previously acquired a 1-Wire net.
//
Expand All @@ -76,7 +84,14 @@ SMALLINT owAcquire(int portnum, char *port_zstr)
//
void owRelease(int portnum)
{
portnum = 0;
if((portnum < MAX_PORTNUM) && (portnum >= 0) )
{
UINT32 pin = (UINT32)owPortPin[portnum];

CPU_GPIO_EnableInputPin( pin, false, NULL, GPIO_INT_NONE, RESISTOR_PULLUP );

owPortPin[portnum] = 0;
}
}


Loading

0 comments on commit 3543b56

Please sign in to comment.