POK(kernelpart)
/home/jaouen/pok_official/pok/trunk/kernel/core/error.c
Go to the documentation of this file.
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Mon Jan 19 10:51:40 2009 
00015  */
00016 
00017 #ifdef POK_NEEDS_ERROR_HANDLING
00018 
00019 #include <types.h>
00020 #include <core/thread.h>
00021 #include <core/error.h>
00022 #include <core/debug.h>
00023 #include <core/partition.h>
00024 
00025 #include <libc.h>
00026 
00027 pok_ret_t pok_error_thread_create (uint32_t stack_size, void* entry)
00028 {
00029    uint32_t tid;
00030    pok_thread_attr_t attr;
00031    pok_ret_t         ret;
00032 
00033    (void) stack_size;
00034 
00035    attr.priority  = POK_THREAD_MAX_PRIORITY;
00036    attr.entry     = entry;
00037    attr.time_capacity = 10000;
00038 
00039    ret = pok_partition_thread_create (&tid, &attr, POK_SCHED_CURRENT_PARTITION);
00040 
00041    POK_CURRENT_PARTITION.thread_error = tid;
00042 
00043    pok_sched_activate_error_thread ();
00044 
00045    return (ret);
00046 }
00047 
00048 
00054 void pok_error_declare (const uint8_t error)
00055 {
00061    if (POK_CURRENT_PARTITION.thread_error != 0)
00062    {
00063       POK_CURRENT_PARTITION.error_status.error_kind    = error;
00064       POK_CURRENT_PARTITION.error_status.failed_thread = POK_SCHED_CURRENT_THREAD - POK_CURRENT_PARTITION.thread_index_low;
00065       POK_CURRENT_PARTITION.error_status.msg_size      = 0;
00066       /*
00067        * FIXME: Add failed address and so on.
00068        */
00069    }
00070    else
00071    {
00076       pok_partition_error (POK_SCHED_CURRENT_PARTITION, error);
00077    }
00078 }
00079 
00080 void pok_error_ignore ()
00081 {
00082    /* Do nothing at this time */
00083 }
00084 
00085 
00086 #ifndef POK_USE_GENERATED_KERNEL_ERROR_HANDLER
00087 void pok_kernel_error (uint32_t error)
00088 {
00089 #ifdef POK_NEEDS_DEBUG
00090    printf ("[KERNEL] [WARNING] Error %d was raised by the kernel but no error recovery was set\n", error);
00091 #else
00092    (void) error;
00093 #endif /* POK_NEEDS_DEBUG */
00094    return;
00095 }
00096 #endif
00097 
00098 #ifdef POK_NEEDS_PARTITIONS
00099 #ifndef POK_USE_GENERATED_PARTITION_ERROR_HANDLER
00100 void pok_partition_error (uint8_t partition, uint32_t error)
00101 {
00102 #ifdef POK_NEEDS_DEBUG
00103    printf ("[KERNEL] [WARNING] Error %d was raised by partition %d but no error recovery was set\n", error, partition);
00104 #else
00105    (void) partition;
00106    (void) error;
00107 #endif /* POK_NEEDS_DEBUG */
00108    return;
00109 }
00110 #endif /* POK_USE_GENERATED_PARTITION_ERROR_HANDLER */
00111 #endif /* POK_NEEDS_PARTITIONS */
00112 
00113 
00114 #ifndef POK_USE_GENERATED_KERNEL_ERROR_CALLBACK
00115 void pok_error_kernel_callback ()
00116 {
00117 #ifdef POK_NEEDS_DEBUG
00118    printf ("[KERNEL] [WARNING] Kernel calls callback function but nothing was defined by the user\n");
00119    printf ("[KERNEL] [WARNING] You MUST define the pok_error_partition_callback function\n");
00120 #endif /* POK_NEEDS_DEBUG */
00121    return;
00122 }
00123 #endif /* POK_USE_GENERATED_KERNEL_ERROR_CALLBACK */
00124 
00125 
00126 #ifdef POK_NEEDS_PARTITIONS
00127 #ifndef POK_USE_GENERATED_PARTITION_ERROR_CALLBACK
00128 void pok_error_partition_callback (uint32_t partition)
00129 {
00130 #ifdef POK_NEEDS_DEBUG
00131    printf ("[KERNEL] [WARNING] Partition %d calls callback function but nothing was defined by the user\n", partition);
00132    printf ("[KERNEL] [WARNING] You MUST define the pok_error_partition_callback function\n");
00133 #else
00134    (void) partition;
00135 #endif
00136    return;
00137 }
00138 #endif /* POK_USE_GENERATED_PARTITION_ERROR_CALLBACK */
00139 
00140 
00141 void pok_error_raise_application_error (char* msg, uint32_t msg_size)
00142 {
00143    if (msg_size > POK_ERROR_MAX_MSG_SIZE)
00144    {
00145       msg_size = POK_ERROR_MAX_MSG_SIZE;
00146    }
00147 
00148    pok_error_status_t* status;
00149    status                  = &pok_partitions[pok_current_partition].error_status;
00150    status->error_kind      = POK_ERROR_KIND_APPLICATION_ERROR;
00151    status->failed_thread   = POK_SCHED_CURRENT_THREAD - POK_CURRENT_PARTITION.thread_index_low;
00152    status->msg_size        = msg_size;
00153 
00154    memcpy (status->msg, msg, msg_size);
00155 
00156    pok_sched_activate_error_thread ();
00157 }
00158 
00159 pok_ret_t pok_error_get (pok_error_status_t* status)
00160 {
00161    if (POK_CURRENT_PARTITION.error_status.error_kind != POK_ERROR_KIND_INVALID)
00162    {
00163 
00164       status->error_kind       = POK_CURRENT_PARTITION.error_status.error_kind;
00165       status->failed_thread    = POK_CURRENT_PARTITION.error_status.failed_thread;
00166       status->failed_addr      = POK_CURRENT_PARTITION.error_status.failed_addr;
00167       status->msg_size         = POK_CURRENT_PARTITION.error_status.msg_size;
00168       memcpy (status->msg, POK_CURRENT_PARTITION.error_status.msg, POK_CURRENT_PARTITION.error_status.msg_size);
00169 
00170       return POK_ERRNO_OK;
00171    }
00172    else
00173    {
00174       return POK_ERRNO_UNAVAILABLE;
00175    }
00176 }
00177 
00178 #endif /* POK_NEEDS_PARTITIONS */
00179 
00180 #endif
00181