Main Page | Modules | Data Structures | File List | Data Fields | Globals | Examples

service.h

00001 /*
00002  * Copyright (c) 2005, Swedish Institute of Computer Science
00003  * All rights reserved. 
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions 
00007  * are met: 
00008  * 1. Redistributions of source code must retain the above copyright 
00009  *    notice, this list of conditions and the following disclaimer. 
00010  * 2. Redistributions in binary form must reproduce the above copyright 
00011  *    notice, this list of conditions and the following disclaimer in the 
00012  *    documentation and/or other materials provided with the distribution. 
00013  * 3. Neither the name of the Institute nor the names of its contributors 
00014  *    may be used to endorse or promote products derived from this software 
00015  *    without specific prior written permission. 
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00027  * SUCH DAMAGE. 
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * @(#)$Id: service.h,v 1.6 2005/07/04 14:57:21 adam Exp $
00032  */
00033 
00034 /**
00035  * \defgroup service The Contiki service mechanism
00036  *
00037  * The Contiki service mechanism enables cross-process functions. A
00038  * service that is registered by one process can be accessed by other
00039  * processes in the system. Services can be transparently replaced at
00040  * run-time.
00041  *
00042  * A service has an interface that callers use to access the service's
00043  * functions. This interface typically is defined in a header file
00044  * that is included by all users of the service. A service interface
00045  * is defined with the SERVICE_INTERFACE() macro.
00046  *
00047  * A service implementation is declared with the SERVICE() macro. The
00048  * SERVICE() statement specifies the actual functions that are used to
00049  * implement the service.
00050  *
00051  * Every service has a controlling process. The controlling process
00052  * registers the service with the system when it starts, and is also
00053  * notified if the service is removed or replaced. A process may
00054  * register any number of services.
00055  *
00056  * Service registration is done with a SERVICE_REGISTER()
00057  * statement. If a service with the same name is already registered,
00058  * this is removed before the new service is registered.
00059  *
00060  * The SERVICE_CALL() macro is used to call a service. If the service
00061  * to be called is not registered, the SERVICE_CALL() statement does
00062  * nothing. The SERVICE_FIND() function can be used to check if a
00063  * particular service exists before calling SERVICE_CALL().
00064  */
00065 
00066 #ifndef __SERVICE_H__
00067 #define __SERVICE_H__
00068 
00069 #include "contiki.h"
00070 
00071 struct service {
00072   struct service *next;
00073   struct process *p;
00074   const char *name;
00075   void *interface;    
00076 };
00077 
00078 /**
00079  * Define the name and interface of a service.
00080  *
00081  * This statement defines the name and interface of a service.
00082  *
00083  * \param name The name of the service.
00084  *
00085  * \param interface A list of function declarations that comprises the
00086  * service interface. This list must be enclosed by curly brackets and
00087  * consist of declarations of function pointers separated by
00088  * semicolons.
00089  *
00090  * \hideinitializer
00091  */
00092 #define SERVICE_INTERFACE(name, interface) struct name interface;
00093 
00094 #if ! CC_NO_VA_ARGS
00095 #define SERVICE(name, service_name, ...)                                \
00096   static struct service_name name##_interface = __VA_ARGS__ ;           \
00097   struct service name = { NULL, NULL, service_name##_name, & name##_interface }
00098 #endif
00099 
00100 /**
00101  * Call a function from a specified service, if it is registered.
00102  *
00103  * 
00104  * \param service_name The name of the service that is to be called.
00105  *
00106  * \param function The function that is to be called. This is a full
00107  * function call, including parameters.
00108  *
00109  * \hideinitializer
00110  */
00111 #define SERVICE_CALL(service_name, function)                    \
00112  {                                                              \
00113    struct service *service_s;                                   \
00114    service_s = service_find(service_name##_name);               \
00115    if(service_s != NULL) {                                      \
00116      ((struct service_name *)service_s->interface)->function;   \
00117    }                                                            \
00118  }
00119 
00120 #define SERVICE_EXISTS(service_name) (service_find(service_name##_name) != NULL)
00121 /**
00122  * Register a service.
00123  *
00124  * \hideinitializer
00125  */
00126 #define SERVICE_REGISTER(name) service_register(&name)
00127 
00128 /**
00129  * Remove a service.
00130  *
00131  * \hideinitializer
00132  */
00133 #define SERVICE_REMOVE(service_name) service_remove(&service_name)
00134 
00135 /**
00136  * Find service.
00137  *
00138  * \hideinitializer
00139  */
00140 #define SERVICE_FIND(service_name) service_find(service_name##_name)
00141 
00142 void service_register(struct service *s);
00143 void service_remove(struct service *s);
00144 struct service *service_find(const char *name);
00145 
00146 #endif /* __SERVICE_H__ */

Generated on Wed Jul 6 01:18:59 2005 for Contiki/ESB by doxygen 1.3.6