Skip to content

Commit 8f00263

Browse files
committed
add context section to protect index when receiving data
manage connection Rx packet Signed-off-by: Francois Ramu <[email protected]>
1 parent 05e5a0b commit 8f00263

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/utility/HCISharedMemTransport.cpp

+22-11
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@ PLACE_IN_SECTION("MB_MEM2") ALIGN(4) static uint8_t
5353
uint16_t _read_index;
5454
uint16_t _write_index;
5555

56-
5756
/** Bluetooth Device Address */
5857
static uint8_t bd_addr_udn[6];
58+
59+
/** different device steps */
5960
static bool phase_bd_addr = false;
6061
static bool phase_tx_power = false;
6162
static bool phase_reset = false;
63+
static bool phase_cnx = false;
6264

6365
HCISharedMemTransportClass::HCISharedMemTransportClass(BLEChip_t ble_chip) :
6466
_ble_chip(ble_chip)
@@ -82,16 +84,16 @@ static void evt_received(TL_EvtPacket_t *hcievt)
8284
case TL_BLEEVT_PKT_TYPE:
8385

8486
/* check the Rx event of complete the previous bd_addr opcode 0xfc0c */
85-
if ((hcievt->evtserial.evt.evtcode == 0x0e) &&
87+
if ((hcievt->evtserial.evt.evtcode == TL_BLEEVT_CC_OPCODE) &&
8688
(hcievt->evtserial.evt.payload[0] == 0x01) &&
8789
(hcievt->evtserial.evt.payload[1] == 0x0C) &&
8890
(hcievt->evtserial.evt.payload[2] == 0xFC)) {
8991
phase_bd_addr = true;
9092
/* rx data is no more useful */
9193
break;
9294
}
93-
/* check the Rx event of complete the previous tx power opcode 0xfc0f */
94-
if ((hcievt->evtserial.evt.evtcode == 0x0e) &&
95+
/* check the Rx event of complete the previous tx power opcode 0xFC0F */
96+
if ((hcievt->evtserial.evt.evtcode == TL_BLEEVT_CC_OPCODE) &&
9597
(hcievt->evtserial.evt.payload[0] == 0x01) &&
9698
(hcievt->evtserial.evt.payload[1] == 0x0F) &&
9799
(hcievt->evtserial.evt.payload[2] == 0xFC)) {
@@ -101,22 +103,31 @@ static void evt_received(TL_EvtPacket_t *hcievt)
101103
}
102104

103105
/* check if the reset phase is in progress (opcode is 0x0C03) */
104-
if ((hcievt->evtserial.evt.evtcode == 0x0e) &&
106+
if ((hcievt->evtserial.evt.evtcode == TL_BLEEVT_CC_OPCODE) &&
105107
(hcievt->evtserial.evt.payload[0] == 0x01) &&
106108
(hcievt->evtserial.evt.payload[1] == 0x03) &&
107109
(hcievt->evtserial.evt.payload[2] == 0x0C)) {
108-
/* this is the HCI Reset command being sent */
109110
phase_reset = true;
110111
}
111112

113+
/* check if the connection phase is up (event code is 0x3E) */
114+
if ((hcievt->evtserial.evt.evtcode == 0x3E)) {
115+
phase_cnx = true;
116+
}
117+
/* check if the connection phase is down (event code is 0x05) */
118+
if ((hcievt->evtserial.evt.evtcode == 0x05)) {
119+
phase_cnx = false;
120+
}
121+
122+
__disable_irq();
112123
/* store received data in the _rxbuff buffer */
113124
len = hcievt->evtserial.evt.plen + TL_EVT_HDR_SIZE;
114125
/* at the position of the _write_index */
115126
memcpy((uint8_t *)&_rxbuff[_write_index], (uint8_t *)&hcievt->evtserial, len);
116127
/* move index */
117128
_write_index += len;
118129
//TODO: control the _rxbuff cannot overflow
119-
130+
__enable_irq();
120131
break;
121132
case TL_ACL_DATA_PKT_TYPE: {
122133
TL_AclDataSerial_t *acl = &(((TL_AclDataPacket_t *)hcievt)->AclDataSerial);
@@ -353,19 +364,19 @@ int HCISharedMemTransportClass::available()
353364
int HCISharedMemTransportClass::peek()
354365
{
355366
int peek_val = -1;
356-
367+
__disable_irq();
357368
if(_read_index != _write_index)
358369
{
359370
peek_val = _rxbuff[_read_index];
360371
}
361-
372+
__enable_irq();
362373
return peek_val;
363374
}
364375

365376
int HCISharedMemTransportClass::read()
366377
{
367378
int read_val = -1;
368-
379+
__disable_irq();
369380
if(_read_index != _write_index)
370381
{
371382
read_val = _rxbuff[_read_index];
@@ -377,7 +388,7 @@ int HCISharedMemTransportClass::read()
377388
_write_index = 0;
378389
}
379390
}
380-
391+
__enable_irq();
381392
return read_val;
382393
}
383394

0 commit comments

Comments
 (0)