blob: ebede371ebd906c22809b5630c4a29064488020e [file] [log] [blame]
adamdunkelsa2f3c422004-09-12 20:24:53 +00001/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
adamdunkelsd393e302005-02-07 07:50:35 +000033 * $Id: ek-service.h,v 1.4 2005/02/07 07:50:35 adamdunkels Exp $
adamdunkelsa2f3c422004-09-12 20:24:53 +000034 */
adamdunkelsd393e302005-02-07 07:50:35 +000035
36/**
37 * \addtogroup ek
38 * @{
39 */
40
41/**
42 * \defgroup ekservice The service layer
43 * @{
44 */
45
46/**
47 * \file
48 * Service layer header file.
49 * \author Adam Dunkels <adam@sics.se>
50 */
51
adamdunkelsa8c2b492004-07-04 15:01:58 +000052#ifndef __EK_SERVICE_H__
53#define __EK_SERVICE_H__
54
55#include "ek.h"
56
57struct ek_service {
58 const char *name;
59 ek_id_t id;
60};
adamdunkelsd393e302005-02-07 07:50:35 +000061
62/**
63 * Declare a service state.
64 *
65 * This macro declares a service state. The service state is used to
66 * access a particular service. The declared service state serves as
67 * the input to all other service functions.
68 *
69 * \param service The name of the service state.
70 * \param name The name of the service - this is defined in the header file of the service.
71 *
72 * \hideinitializer
73 */
adamdunkelsa8c2b492004-07-04 15:01:58 +000074#define EK_SERVICE(service, name) \
75 static struct ek_service service = {name, EK_ID_NONE}
76
adamdunkelsd393e302005-02-07 07:50:35 +000077/**
78 * Start a service.
79 *
80 * This function is called from within a service in order to start the
81 * service process and to register the service with Contiki. If
82 * another instance of the same service is running, it is requested to
83 * shut itself down before the current service is started.
84 *
85 * This function is the equivalent of ek_start() but for services.
86 *
87 * \note Do not call the function ek_start() from a service, use
88 * ek_service_start() instead.
89 *
90 * \param name The name of the service. The first part of the name
91 * must be indentical for all services of the same kind.
92 *
93 * \param p The process structure for the service.
94 *
95 * \return The process ID of the service.
96 */
adamdunkelsa8c2b492004-07-04 15:01:58 +000097ek_id_t ek_service_start(const char *name, struct ek_proc *p);
98
adamdunkelsd393e302005-02-07 07:50:35 +000099/**
100 * Find a running service.
101 *
102 * This function finds a running service. The function is called from
103 * programs that utilize the service, not from within a service process.
104 *
105 * Example:
106 \code
107 #include "packet-service.h"
108
109 EK_SERVICE(packetservice, PACKET_SERVICE_NAME);
110
111 void stop_packetservice(void) {
112 if(ek_service_find(&packetservice) == EK_ERR_OK) {
113 ek_service_post(&packetservice, EK_EVENT_REQUEST_EXIT, NULL);
114 ek_service_reset(&packetservice);
115 }
116 }
117 \endcode
118 *
119 * \param s The service state. This state is filled in with
120 * information by the ek_service_find() function.
121 *
122 * \retval EK_ERR_OK The requested service was found.
123 * \retval EK_ERR_NOTFOUND The requested service was not found.
124 */
adamdunkelsa8c2b492004-07-04 15:01:58 +0000125ek_err_t ek_service_find(struct ek_service *s);
126
adamdunkelsd393e302005-02-07 07:50:35 +0000127/**
128 * Retrieve the service interface for a running service.
129 *
130 * This function retrieves the service interface (the API for the
131 * service) for a running service. The service state must first have
132 * been looked up using the ek_service_find() function.
133 *
134 * This function is used in the service stub.
135 *
136 * Example
137 \code
138 #include "packet-service.h"
139
140 EK_SERVICE(packetservice, PACKET_SERVICE_NAME);
141
142 static struct packet_service_interface *
143 find_interface(void)
144 {
145 struct packet_service_interface *interface;
146 interface = (struct packet_service_interface *)ek_service_state(&service);
147 if(interface != NULL &&
148 interface->version == PACKET_SERVICE_VERSION) {
149 return interface;
150 } else {
151 return NULL;
152 }
153 }
154 \endcode
155 *
156 * \param s The service state that previously has been looked up with
157 * ek_service_find().
158 *
159 * \return A pointer to the service interface if the requested service
160 * is running, or NULL if no such service was found.
161 */
adamdunkelsa8c2b492004-07-04 15:01:58 +0000162void *ek_service_state(struct ek_service *s);
163
adamdunkelsd393e302005-02-07 07:50:35 +0000164/**
165 * Reset a service.
166 *
167 * When a service is looked up by Contiki, the process ID of the
168 * service is cached. This function resets the cache so that a running
169 * service can be removed from the system.
170 *
171 * Example:
172 \code
173 #include "packet-service.h"
174
175 EK_SERVICE(packetservice, PACKET_SERVICE_NAME);
176
177 void stop_packetservice(void) {
178 if(ek_service_find(&packetservice) == EK_ERR_OK) {
179 ek_service_post(&packetservice, EK_EVENT_REQUEST_EXIT, NULL);
180 ek_service_reset(&packetservice);
181 }
182 }
183 \endcode
184 *
185 * \param s The service state.
186 */
adamdunkels4e852942004-09-12 07:21:45 +0000187void ek_service_reset(struct ek_service *s);
188
adamdunkelsd393e302005-02-07 07:50:35 +0000189/**
190 * Post an event to a running service.
191 *
192 * This macro posts an event to a running service. The service must
193 * first be looked up using ek_service_find().
194 *
195 * \sa ek_post().
196 *
197 * \param s The service state
198 * \param ev The event to be posted.
199 * \param data An opaque pointer to be posted with the event.
200 *
201 * \hideinitializer
202 */
203#define ek_service_post(s, ev, data) ek_post((s)->id, (ev), (data))
adamdunkelsa8c2b492004-07-04 15:01:58 +0000204
205/*ek_err_t ek_service_call(struct ek_service *s,
206 ek_event_t ev, ek_data_t data);*/
207
adamdunkelsd393e302005-02-07 07:50:35 +0000208/** @} */
209/** @} */
210
adamdunkelsa8c2b492004-07-04 15:01:58 +0000211#endif /* __EK_SERVICE_H__ */
adamdunkelsd393e302005-02-07 07:50:35 +0000212