core: improve error helpers

First, simplify the logic that attempts to find the correct error code
associated to an error text string. This logic is very flaky, as it
relies on knowing what the modems report as text string for the
different error codes, and obviously that is not always the same.
Instead of supporting all error codes in this logic, we now limit them
to a small subset with common error codes and hopefully common error
strings.

Second, in order to quickly get the error description string for a
given error code, we rework the array of supported errors so that
the index of the array is the error code itself. For mobile equipment
errors the logic is straightforward, as they're always in the [0,255]
range, but for the message errors we create an array where the index
of the array is equal to the code plus 300 as they're always in the
[300,500] range. By doing this, we avoid having an array with 300 NULL
items before the actual values we want to support.
This commit is contained in:
Aleksander Morgado
2021-05-22 22:58:37 +00:00
parent 9fdbbc6929
commit d1bd0a2971
2 changed files with 353 additions and 226 deletions
+85
View File
@@ -52,6 +52,85 @@ TEST_ERROR_HELPER (MESSAGE_ERROR, message_error, MessageError)
/*****************************************************************************/
static void
test_error_helpers_mobile_equipment_error_for_code (void)
{
GError *error = NULL;
/* first */
error = mm_mobile_equipment_error_for_code (MM_MOBILE_EQUIPMENT_ERROR_PHONE_FAILURE, NULL);
g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_PHONE_FAILURE);
g_clear_error (&error);
/* last */
error = mm_mobile_equipment_error_for_code (MM_MOBILE_EQUIPMENT_ERROR_GPRS_REQUEST_REJECTED_BCM_VIOLATION, NULL);
g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_GPRS_REQUEST_REJECTED_BCM_VIOLATION);
g_clear_error (&error);
/* other > 255 */
error = mm_mobile_equipment_error_for_code (256, NULL);
g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_UNKNOWN);
g_clear_error (&error);
}
static void
test_error_helpers_message_error_for_code (void)
{
GError *error = NULL;
/* first */
error = mm_message_error_for_code (MM_MESSAGE_ERROR_ME_FAILURE, NULL);
g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_ME_FAILURE);
g_clear_error (&error);
/* last */
error = mm_message_error_for_code (MM_MESSAGE_ERROR_UNKNOWN, NULL);
g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_UNKNOWN);
g_clear_error (&error);
/* other < 300 */
error = mm_message_error_for_code (299, NULL);
g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_UNKNOWN);
g_clear_error (&error);
/* other > 500 */
error = mm_message_error_for_code (501, NULL);
g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_UNKNOWN);
g_clear_error (&error);
}
/*****************************************************************************/
static void
test_error_helpers_mobile_equipment_error_for_string (void)
{
GError *error = NULL;
error = mm_mobile_equipment_error_for_string ("operationnotallowed", NULL);
g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_NOT_ALLOWED);
g_clear_error (&error);
error = mm_mobile_equipment_error_for_string ("arratsaldeon", NULL);
g_assert_error (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_UNKNOWN);
g_clear_error (&error);
}
static void
test_error_helpers_message_error_for_string (void)
{
GError *error = NULL;
error = mm_message_error_for_string ("operationnotallowed", NULL);
g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_NOT_ALLOWED);
g_clear_error (&error);
error = mm_message_error_for_string ("arratsaldeon", NULL);
g_assert_error (error, MM_MESSAGE_ERROR, MM_MESSAGE_ERROR_UNKNOWN);
g_clear_error (&error);
}
/*****************************************************************************/
int main (int argc, char **argv)
{
setlocale (LC_ALL, "");
@@ -62,5 +141,11 @@ int main (int argc, char **argv)
g_test_add_func ("/MM/error-helpers/mobile-equipment-error", test_error_helpers_mobile_equipment_error);
g_test_add_func ("/MM/error-helpers/message-error", test_error_helpers_message_error);
g_test_add_func ("/MM/error-helpers/mobile-equipment-error/for-code", test_error_helpers_mobile_equipment_error_for_code);
g_test_add_func ("/MM/error-helpers/message-error/for-code", test_error_helpers_message_error_for_code);
g_test_add_func ("/MM/error-helpers/mobile-equipment-error/for-string", test_error_helpers_mobile_equipment_error_for_string);
g_test_add_func ("/MM/error-helpers/message-error/for-string", test_error_helpers_message_error_for_string);
return g_test_run ();
}