From 4ce407ab0519612e9b21ce9727d39a8b262121d0 Mon Sep 17 00:00:00 2001 From: SangYoun Kwak Date: Wed, 3 Jul 2024 15:38:34 +0900 Subject: Add exception handler to the create functions Each sensor creator may throw an exception if connection to the sensor is failed. To handle this exception, Try-catch statement is added to the sensor_create functions for each sensor. Change-Id: I2acccb1b0dc2939031c0ed7278851504af72507e Signed-off-by: SangYoun Kwak --- src/accelerometer/hal-backend-sensor-accel.cpp | 9 +++++++-- src/gyroscope/hal-backend-sensor-gyro.cpp | 9 +++++++-- src/magnetometer/hal-backend-sensor-magnet.cpp | 9 +++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/accelerometer/hal-backend-sensor-accel.cpp b/src/accelerometer/hal-backend-sensor-accel.cpp index 9d66893..a694ae6 100644 --- a/src/accelerometer/hal-backend-sensor-accel.cpp +++ b/src/accelerometer/hal-backend-sensor-accel.cpp @@ -7,8 +7,13 @@ static sensor_device_t accel_dev = NULL; static int sensor_create(sensor_device_t **devices) { - if (!accel_dev) - accel_dev = new accel_device; + if (!accel_dev) { + try { + accel_dev = new accel_device; + } catch (int error_ret) { + return 0; + } + } *devices = &accel_dev; return 1; diff --git a/src/gyroscope/hal-backend-sensor-gyro.cpp b/src/gyroscope/hal-backend-sensor-gyro.cpp index fc37fe7..61aaa9d 100644 --- a/src/gyroscope/hal-backend-sensor-gyro.cpp +++ b/src/gyroscope/hal-backend-sensor-gyro.cpp @@ -7,8 +7,13 @@ static sensor_device_t gyro_dev = NULL; static int sensor_create(sensor_device_t **devices) { - if (!gyro_dev) - gyro_dev = new gyro_device; + if (!gyro_dev) { + try { + gyro_dev = new gyro_device; + } catch (int error_ret) { + return 0; + } + } *devices = &gyro_dev; return 1; diff --git a/src/magnetometer/hal-backend-sensor-magnet.cpp b/src/magnetometer/hal-backend-sensor-magnet.cpp index 853c709..a7372d1 100644 --- a/src/magnetometer/hal-backend-sensor-magnet.cpp +++ b/src/magnetometer/hal-backend-sensor-magnet.cpp @@ -7,8 +7,13 @@ static sensor_device_t magnet_dev = NULL; static int sensor_create(sensor_device_t **devices) { - if (!magnet_dev) - magnet_dev = new magnet_device; + if (!magnet_dev) { + try { + magnet_dev = new magnet_device; + } catch (int error_ret) { + return 0; + } + } *devices = &magnet_dev; return 1; -- cgit v1.2.3