Skip to content

Commit

Permalink
glib style error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
monich committed Feb 19, 2014
1 parent b606e0c commit cb8b17a
Show file tree
Hide file tree
Showing 22 changed files with 221 additions and 42 deletions.
18 changes: 12 additions & 6 deletions mms-engine/mms_engine.c
Expand Up @@ -147,15 +147,17 @@ mms_engine_handle_receive_message(
if (imsi && bytes && len) {
char* id = g_strdup_printf("%d", database_id);
GBytes* push = g_bytes_new(bytes, len);
GError* error = NULL;
if (mms_dispatcher_receive_message(engine->dispatcher, id, imsi,
automatic, push)) {
automatic, push, &error)) {
if (mms_dispatcher_start(engine->dispatcher)) {
mms_engine_start_timeout_cancel(engine);
}
org_nemomobile_mms_engine_complete_receive_message(proxy, call);
} else {
g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
G_DBUS_ERROR_FAILED, "Unable to receive message");
G_DBUS_ERROR_FAILED, "%s", MMS_ERRMSG(error));
g_error_free(error);
}
g_bytes_unref(push);
g_free(id);
Expand All @@ -179,17 +181,19 @@ mms_engine_handle_send_read_report(
int read_status, /* 0: Read 1: Deleted without reading */
MMSEngine* engine)
{
GError* error = NULL;
MMS_DEBUG_("%s %s %s %s %d", id, imsi, message_id, to, read_status);
if (mms_dispatcher_send_read_report(engine->dispatcher, id, imsi,
message_id, to, (read_status == 1) ? MMS_READ_STATUS_DELETED :
MMS_READ_STATUS_READ)) {
MMS_READ_STATUS_READ, &error)) {
if (mms_dispatcher_start(engine->dispatcher)) {
mms_engine_start_timeout_cancel(engine);
}
org_nemomobile_mms_engine_complete_send_read_report(proxy, call);
} else {
g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
G_DBUS_ERROR_FAILED, "Error submitting read report");
G_DBUS_ERROR_FAILED, "%s", MMS_ERRMSG(error));
g_error_free(error);
}
return TRUE;
}
Expand Down Expand Up @@ -236,15 +240,17 @@ mms_engine_handle_push_notify(
g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
G_DBUS_ERROR_FAILED, "No data provided");
} else {
GError* err = NULL;
GBytes* msg = g_bytes_new(bytes, len);
if (mms_dispatcher_handle_push(engine->dispatcher, imsi, msg)) {
if (mms_dispatcher_handle_push(engine->dispatcher, imsi, msg, &err)) {
if (mms_dispatcher_start(engine->dispatcher)) {
mms_engine_start_timeout_cancel(engine);
}
org_nemomobile_mms_engine_complete_push(proxy, call);
} else {
g_dbus_method_invocation_return_error(call, G_DBUS_ERROR,
G_DBUS_ERROR_FAILED, "Unable to handle push message");
G_DBUS_ERROR_FAILED, "%s", MMS_ERRMSG(err));
g_error_free(err);
}
g_bytes_unref(msg);
}
Expand Down
2 changes: 1 addition & 1 deletion mms-lib/Makefile
Expand Up @@ -16,7 +16,7 @@ all: debug release
#

SRC = mms_codec.c mms_connection.c mms_connman.c mms_dispatcher.c \
mms_handler.c mms_lib_util.c mms_file_util.c mms_log.c \
mms_error.c mms_handler.c mms_lib_util.c mms_file_util.c mms_log.c \
mms_message.c mms_task.c mms_task_ack.c mms_task_decode.c \
mms_task_notification.c mms_task_notifyresp.c mms_task_publish.c \
mms_task_read.c mms_task_retrieve.c mms_task_upload.c mms_util.c
Expand Down
9 changes: 6 additions & 3 deletions mms-lib/include/mms_dispatcher.h
Expand Up @@ -57,15 +57,17 @@ gboolean
mms_dispatcher_handle_push(
MMSDispatcher* dispatcher,
const char* imsi,
GBytes* push);
GBytes* push,
GError** error);

gboolean
mms_dispatcher_receive_message(
MMSDispatcher* dispatcher,
const char* id,
const char* imsi,
gboolean automatic,
GBytes* push);
GBytes* push,
GError** error);

gboolean
mms_dispatcher_send_read_report(
Expand All @@ -74,7 +76,8 @@ mms_dispatcher_send_read_report(
const char* imsi,
const char* message_id,
const char* to,
MMSReadStatus status);
MMSReadStatus status,
GError** error);

void
mms_dispatcher_cancel(
Expand Down
10 changes: 10 additions & 0 deletions mms-lib/include/mms_lib_util.h
Expand Up @@ -17,6 +17,16 @@

#include "mms_lib_types.h"

/* Error reporting */
GQuark mms_lib_error_quark(void);
#define MMS_LIB_ERROR mms_lib_error_quark()
typedef enum {
MMS_LIB_ERROR_ENCODE,
MMS_LIB_ERROR_DECODE,
MMS_LIB_ERROR_FILE,
MMS_LIB_ERROR_EXPIRED
} MMSLibError;

/* One-time initialization */
void
mms_lib_init(void);
Expand Down
2 changes: 2 additions & 0 deletions mms-lib/mms-lib.pro
Expand Up @@ -17,6 +17,7 @@ SOURCES += \
src/mms_codec.c \
src/mms_connection.c \
src/mms_connman.c \
src/mms_error.c \
src/mms_dispatcher.c \
src/mms_file_util.c \
src/mms_handler.c \
Expand All @@ -36,6 +37,7 @@ SOURCES += \

HEADERS += \
src/mms_codec.h \
src/mms_error.h \
src/mms_file_util.h \
src/mms_task.h \
src/mms_util.h \
Expand Down
19 changes: 12 additions & 7 deletions mms-lib/src/mms_dispatcher.c
Expand Up @@ -26,6 +26,7 @@
/* Logging */
#define MMS_LOG_MODULE_NAME mms_dispatcher_log
#include "mms_lib_log.h"
#include "mms_error.h"
MMS_LOG_MODULE_DEFINE("mms-dispatcher");

struct mms_dispatcher {
Expand Down Expand Up @@ -431,10 +432,12 @@ gboolean
mms_dispatcher_handle_push(
MMSDispatcher* disp,
const char* imsi,
GBytes* push)
GBytes* push,
GError** error)
{
return mms_dispatcher_queue_and_unref_task(disp,
mms_task_notification_new(disp->config, disp->handler, imsi, push));
mms_task_notification_new(disp->config, disp->handler,
imsi, push, error));
}

/**
Expand All @@ -446,7 +449,8 @@ mms_dispatcher_receive_message(
const char* id,
const char* imsi,
gboolean automatic,
GBytes* bytes)
GBytes* bytes,
GError** error)
{
gboolean ok = FALSE;
MMSPdu* pdu = mms_decode_bytes(bytes);
Expand All @@ -455,11 +459,11 @@ mms_dispatcher_receive_message(
if (pdu->type == MMS_MESSAGE_TYPE_NOTIFICATION_IND) {
ok = mms_dispatcher_queue_and_unref_task(disp,
mms_task_retrieve_new(disp->config, disp->handler,
id, imsi, pdu));
id, imsi, pdu, error));
}
mms_message_free(pdu);
} else {
MMS_ERR("Unable to parse MMS push data");
MMS_ERROR(error, MMS_LIB_ERROR_DECODE, "Failed to decode MMS PDU");
}
return ok;
}
Expand All @@ -474,11 +478,12 @@ mms_dispatcher_send_read_report(
const char* imsi,
const char* message_id,
const char* to,
MMSReadStatus status)
MMSReadStatus status,
GError** error)
{
return mms_dispatcher_queue_and_unref_task(disp,
mms_task_read_new(disp->config, disp->handler,
id, imsi, message_id, to, status));
id, imsi, message_id, to, status, error));
}

/**
Expand Down
53 changes: 53 additions & 0 deletions mms-lib/src/mms_error.c
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2013-2014 Jolla Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/

#include "mms_log.h"
#include "mms_error.h"

void
mms_error_valist(
const MMSLogModule* module,
GError** error,
MMSLibError code,
const char* format,
va_list args)
{
if (error) {
g_propagate_error(error,
g_error_new_valist(MMS_LIB_ERROR, code, format, args));
}
mms_logv(module, MMS_LOGLEVEL_ERR, format, args);
}

void
mms_error(
const MMSLogModule* module,
GError** error,
MMSLibError code,
const char* format,
...)
{
va_list args;
va_start(args, format);
mms_error_valist(module, error, code, format, args);
va_end (args);
}

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
62 changes: 62 additions & 0 deletions mms-lib/src/mms_error.h
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2013-2014 Jolla Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/

#ifndef JOLLA_MMS_ERROR_H
#define JOLLA_MMS_ERROR_H

#ifndef JOLLA_MMS_LOG_H
# error mms_log.h must be included before mms_error.h
#endif

#include "mms_lib_util.h"

void
mms_error(
const MMSLogModule* module,
GError** error,
MMSLibError code,
const char* format,
...);

void
mms_error_valist(
const MMSLogModule* module,
GError** error,
MMSLibError code,
const char* format,
va_list va);

#ifdef MMS_LOG_VARARGS
# define MMS_ERROR(error,code,format,args...) \
mms_error(MMS_LOG_MODULE_CURRENT, error, code, format, ##args)
#else
# define MMS_ERROR mms_error_static
static inline void mms_error_static(GError** error, MMSLibError code, \
const char* format, ...) { \
va_list va; va_start(va,format); \
mms_error_valist(MMS_LOG_MODULE_CURRENT, error, code, format, va); \
va_end(va); \
}
#endif /* MMS_LOG_VARARGS */


#endif /* JOLLA_MMS_ERROR_H */

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
11 changes: 8 additions & 3 deletions mms-lib/src/mms_file_util.c
Expand Up @@ -14,6 +14,8 @@

#include "mms_file_util.h"
#include "mms_log.h"
#include "mms_error.h"

#include <fcntl.h>

#ifndef O_BINARY
Expand Down Expand Up @@ -45,22 +47,25 @@ int
mms_create_file(
const char* dir,
const char* file,
char** path)
char** path,
GError** error)
{
int fd = -1;
int err = g_mkdir_with_parents(dir, MMS_DIR_PERM);
if (!err || errno == EEXIST) {
char* fname = g_strconcat(dir, "/", file, NULL);
fd = open(fname, O_CREAT|O_RDWR|O_TRUNC|O_BINARY, MMS_FILE_PERM);
if (fd < 0) {
MMS_ERR("Failed to create %s: %s", fname, strerror(errno));
MMS_ERROR(error, MMS_LIB_ERROR_FILE,
"Failed to create file %s: %s", fname, strerror(errno));
} else if (path) {
*path = fname;
fname = NULL;
}
g_free(fname);
} else {
MMS_ERR("Failed to create directory %s: %s", dir, strerror(errno));
MMS_ERROR(error, MMS_LIB_ERROR_FILE,
"Failed to create directory %s: %s", dir, strerror(errno));
}
return fd;
}
Expand Down
3 changes: 2 additions & 1 deletion mms-lib/src/mms_file_util.h
Expand Up @@ -43,7 +43,8 @@ int
mms_create_file(
const char* dir,
const char* fname,
char** path);
char** path,
GError** error);

gboolean
mms_write_file(
Expand Down
9 changes: 9 additions & 0 deletions mms-lib/src/mms_lib_util.c
Expand Up @@ -23,6 +23,15 @@
#define MMS_DEFAULT_RETRY_SECS (15)
#define MMS_DEFAULT_IDLE_SECS (20)

/**
* MMS error domain
*/
GQuark
mms_lib_error_quark()
{
return g_quark_from_static_string("mms-lib-error-quark");
}

/**
* One-time initialization
*/
Expand Down

0 comments on commit cb8b17a

Please sign in to comment.