@@ -406,15 +406,16 @@ I2cController::~I2cController() {
406406*/
407407/* **********************************************************************/
408408bool I2cController::RemoveDriver (uint32_t address) {
409- for (drvBase* driver : _i2c_drivers) {
409+ for (drvBase * driver : _i2c_drivers) {
410410 if (driver == nullptr )
411411 continue ;
412412
413413 if (driver->GetAddress () != address)
414414 continue ;
415415
416416 delete driver;
417- _i2c_drivers.erase (std::find (_i2c_drivers.begin (), _i2c_drivers.end (), driver));
417+ _i2c_drivers.erase (
418+ std::find (_i2c_drivers.begin (), _i2c_drivers.end (), driver));
418419 return true ;
419420 }
420421 return false ;
@@ -504,29 +505,63 @@ bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
504505 return false ;
505506 }
506507
508+ bool did_remove = true ;
509+
507510 // Check for default bus
508511 if (strlen (msgRemove->i2c_device_description .i2c_bus_scl ) == 0 &&
509512 strlen (msgRemove->i2c_device_description .i2c_bus_sda ) == 0 ) {
510513 WS_DEBUG_PRINTLN (" [i2c] Removing device from default bus..." );
511514 if (!_i2c_bus_default->HasMux ()) {
512515 // TODO: Implement remove, straightforward
513516 if (!RemoveDriver (msgRemove->i2c_device_description .i2c_device_address )) {
514- WS_DEBUG_PRINTLN (" [i2c] ERROR: Failed to remove i2c device from default bus!" );
515- return false ;
517+ WS_DEBUG_PRINTLN (
518+ " [i2c] ERROR: Failed to remove i2c device from default bus!" );
519+ did_remove = false ;
516520 }
517521 } else {
518522 // Bus has a I2C MUX attached
519523 // Case 1: Is the I2C device connected to a MUX?
520- if (msgRemove->i2c_device_description .i2c_mux_address != 0xFFFF && msgRemove->i2c_device_description .i2c_mux_channel >= 0 ) {
524+ if (msgRemove->i2c_device_description .i2c_mux_address != 0xFFFF &&
525+ msgRemove->i2c_device_description .i2c_mux_channel >= 0 ) {
521526 // TODO: Remove the device from the mux's channel and delete the driver
527+ _i2c_bus_default->SelectMuxChannel (
528+ msgRemove->i2c_device_description .i2c_mux_channel );
529+ if (!RemoveDriver (
530+ msgRemove->i2c_device_description .i2c_device_address )) {
531+ WS_DEBUG_PRINTLN (
532+ " [i2c] ERROR: Failed to remove i2c device from default bus!" );
533+ did_remove = false ;
534+ }
522535 }
523536 // Case 2: Is the I2C device a MUX?
524- if (msgRemove->i2c_device_description .i2c_device_address == msgRemove->i2c_device_description .i2c_mux_address ) {
525- // TODO: Remove the MUX from the i2c bus
537+ if (msgRemove->i2c_device_description .i2c_device_address ==
538+ msgRemove->i2c_device_description .i2c_mux_address ) {
539+ // TODO: Scan the mux to see what drivers are attached?
540+ wippersnapper_i2c_I2cBusScanned scan_results;
541+ _i2c_bus_default->ScanMux (&scan_results);
542+ // DEBUG - TODO REMOVE - Print out the scan results
543+ for (int i = 0 ; i < scan_results.i2c_bus_found_devices_count ; i++) {
544+ WS_DEBUG_PRINT (" [i2c] Found device at address: " );
545+ WS_DEBUG_PRINT (
546+ scan_results.i2c_bus_found_devices [i].i2c_device_address , HEX);
547+ WS_DEBUG_PRINT (" on mux channel #: " );
548+ WS_DEBUG_PRINTLN (
549+ scan_results.i2c_bus_found_devices [i].i2c_mux_channel );
550+ // Select the channel and remove the device
551+ _i2c_bus_default->SelectMuxChannel (
552+ scan_results.i2c_bus_found_devices [i].i2c_mux_channel );
553+ RemoveDriver (
554+ scan_results.i2c_bus_found_devices [i].i2c_device_address );
555+ }
556+ _i2c_bus_default->RemoveMux ();
526557 }
527558 }
528559 }
529560
561+ // TODO: Check for Alt. I2C Bus
562+
563+ // Publush with did_remove to the response
564+
530565 return true ;
531566}
532567
0 commit comments