summaryrefslogtreecommitdiff
path: root/host/src/netclock.c
blob: 6b8fa139109f85992c9adbb96ab016005691289b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
 
/**
 * \file    netclock.c
 * \brief   The 'netclock' management functions
 * \ingroup host
 *
 * This file provides 'netclock' context management functions
 *
 * \todo   
 */

#include "common/std.h"
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "host/netclock.h"
#ifndef UNIT_TEST
#include "lib/swap.h"
#include "host/syscall.h"
#else /* UNIT_TEST */
#include <unistd.h>
#include <arpa/inet.h>
#endif /* UNIT_TEST */

/**
 * netclock context, called during station context creation. It will manage the callback of
 * received events
 * \param station station which uses the sci context
 * \return the new sci context, NULL if station is NULL
 */
netclock_ctx_t *netclock_new(struct station_ctx *station);

/**
 * netclock context destruction with memory freeing.
 * \param netclock netclock context to destroy
 */
void netclock_free(netclock_ctx_t *netclock);

static bool _netclock_node_less(set_node_t *left, set_node_t *right)
{
    return (((netclock_callback_t *)PARENT_OF(netclock_callback_t, node, left))->id
        < ((netclock_callback_t *)PARENT_OF(netclock_callback_t, node, right))->id);
}

/**
 * initialize a static netclock context, called during station context creation.
 * \param netclock netclock context to initialize
 * \param sci sci which uses the netclock context
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if netclock or station is NULL
 */
int netclock_init(netclock_ctx_t *netclock, sci_ctx_t *sci)
{
    if((netclock == NULL)
        || (sci == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    memset(netclock, '\0', sizeof(netclock_ctx_t));
    netclock->sci = sci;
    set_init(&netclock->callback_set, _netclock_node_less);
    /* register netclock_recv to the sci layer */
    if(sci_register_callback(sci, SCI_MSG_TYPE_NETWORK_CLOCK, netclock_recv, netclock) < 0)
        return -1;
    return 0;
}

/**
 * schedule a netclock event inside maximus netclock manager
 * \param netclock current netclock context
 * \param callback callback context to insert into callback queue 
 * \param event type of event (phy, system...)
 * \param tick next tick to start the event
 * \param function pointer to the callback function to process the received message
 * \param data user data to be included into callback function as 'data' parameter
 * \param id pointer filled with id used to create the netclock msg
 * \return 0 if ok, -1 if failed with errno:
 * - EINVAL if netclock or msg is null, or if tick < current_tick
 * - ENOSPC if there is no more place to register a callback function
 */  
int netclock_schedule(
    netclock_ctx_t *netclock,
    netclock_callback_t *callback,
    netclock_type_t type,
    tick_t schedule_tick,
    void (*callback_function)(void *data), 
    void *callback_data,
    netclock_id_t *id
    )
{
    sci_msg_t msg;
    unsigned char buffer[256];
    
    if((netclock == NULL)
        || (callback == NULL)
        || (type >= NETWORK_CLOCK_TYPE_NB)
        || (schedule_tick < netclock->sci->station->current_tick_tck)
        || (callback_function == NULL)
        || (callback_data == NULL)
        || (id == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    sci_msg_init(&msg, buffer, 256);
    
    /* fill the callback structure */
    callback->id = netclock->current_id;
    callback->tick = schedule_tick;
    callback->function = callback_function;
    callback->data = callback_data;
    set_node_init(&callback->node);
    
    /* register the callback */
    set_insert(&netclock->callback_set, &callback->node);
    netclock->callback_nb++;
    
    /* fill the netclock message part */
    *id = netclock->current_id;
    netclock_fill_hdr(netclock, &msg, type, 0, schedule_tick);
    
    /* fill the sci message part */
    sci_fill_hdr(netclock->sci, &msg, SCI_MSG_TYPE_NETWORK_CLOCK, 0);
    
    /* send message */
    return(sci_send(netclock->sci, &msg));
}
    
/**
 * remove a scheduled netclock event
 * \param netclock current netclock context
 * \param id id of scheduled event
 * \return 0 if ok, -1 if failed with errno:
 * - EINVAL if netclock is null
 * - ENOENT if id is not registred
 */  
int netclock_unschedule(
    netclock_ctx_t *netclock,
    netclock_id_t id)
{
    set_node_t *found;
    netclock_callback_t reference;
    sci_msg_t msg;
    unsigned char buffer[256];
    
    if(netclock == NULL)
    {
        errno = EINVAL;
        return -1;
    }
    
    /* init the search reference callback */
    reference.id = id;
    set_node_init(&reference.node);
    /* find the corresponding id */
    if((found = set_find(&netclock->callback_set, &reference.node)) == NULL)
    {
        /* not found */
        errno =  ENOENT;
        return -1;
    }
    
    /* build a remove message */
    sci_msg_init(&msg, buffer, 256);
    netclock_fill_hdr(
        netclock, 
        &msg, 
        NETWORK_CLOCK_TYPE_REMOVE, 
        0, 
        ((netclock_callback_t *)PARENT_OF(netclock_callback_t, node, found))->tick
    );
    msg.hdr.netclock->id = htons(id);
    sci_fill_hdr(netclock->sci, &msg, SCI_MSG_TYPE_NETWORK_CLOCK, 0);
    /* send it */
    if(sci_send(netclock->sci, &msg) < 0)
        return -1;
        
    /* remove callback from network callback set */
    set_remove(&netclock->callback_set, found);
    
    /* decrement callback_nb */
    if(netclock->callback_nb > 0)
        netclock->callback_nb--;
    
    return 0;
}
 
/**
 * fill a blank netclock header with needed msg type and length
 * \param netclock netclock current context
 * \param msg current msg to fill 
 * \param event type of event (phy, system...)
 * \param type type of action for message: insert or remove
 * \return 0 if ok, -1 if failed with errno:
 * - EINVAL if sci or hdr are NULL, or if type or length are out of range 
 */
int netclock_fill_hdr(
    netclock_ctx_t *netclock, 
    sci_msg_t *msg, 
    netclock_type_t type,
    int flags,
    tick_t tick)
{

    if((netclock == NULL)
        || (msg == NULL)
        || (type >= NETWORK_CLOCK_TYPE_NB)
        || (tick < netclock->sci->station->current_tick_tck))
    {
        errno = EINVAL;
        return -1;
    }
    
    /* reserve space */
    sci_msg_push(msg, sizeof(netclock_msg_hdr_t));
    /* fill the reserved header */
    msg->hdr.netclock = (netclock_msg_hdr_t *)msg->data_begin;
    msg->hdr.netclock->version = NETWORK_CLOCK_VERSION;
    msg->hdr.netclock->type = type;
    msg->hdr.netclock->id = htons(netclock->current_id);
    msg->hdr.netclock->flags = htons(flags);
    msg->hdr.netclock->tick_high = htonl(tick >> 32);
    msg->hdr.netclock->tick_low = htonl(tick & 0x00000000ffffffff);
    
    /* increment network id reference */
    netclock->current_id = (netclock->current_id + 1) & NETWORK_CLOCK_ID_MASK;
    
    return 0;
}

/**
 * check if a waiting callback is still valid (if planned tick is < current tick)
 * \param current_tick system/station current tick value
 * \param callback pointer to checked callback
 * \return 0 if not more valid, 1 if still valid 
 */
//int netclock_is_callback_valid(tick_t current_tick, struct netclock_callback *callback);

/**
 * Process netclock message received by the sci layer.
 * This function must be registred to SCI layer with SCI_MSG_TYPE_NETWORK_CLOCK type.
 * It will execute the netclock registred callback function
 * \param msg message to process
 * \param netclock netclock current context
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if netclock or msg is NULL
 * - errno from sci_send() call
 * */
int netclock_recv(sci_msg_t *msg, void *nclock)
{
    netclock_callback_t reference, *found_callback;
    set_node_t *found_node;
    netclock_ctx_t *netclock;
    
    if((msg == NULL)
        || (nclock == NULL))
    {
        errno = EINVAL;
        return -1;
    }
    
    netclock = (netclock_ctx_t *)nclock;
    /* set header pointer in case of not already done */
    msg->hdr.netclock = (struct netclock_msg_hdr *)(msg->data_begin);

    /* check message type */
    if(msg->hdr.netclock->type >= NETWORK_CLOCK_TYPE_NB)
    {
        station_log(netclock->sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_NETCLOCK, "%s: bad prototype %d", __FUNCTION__, msg->hdr.netclock->type); 
        errno = EPROTOTYPE;
        return -1;
    }
    
    /* init the callback to find */
    reference.id = ntohs(msg->hdr.netclock->id);
    set_node_init(&reference.node);
    
    /* find it */
    if((found_node = set_find(&netclock->callback_set, &reference.node)) == NULL)
    {
        station_log(netclock->sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_NETCLOCK, "%s: callback %d not found", __FUNCTION__, reference.id); 
        errno = ENOENT;
        return -1;
    }
    
    /* remove it from the callback set */
    found_callback = (netclock_callback_t *)PARENT_OF(netclock_callback_t, node, found_node);
    set_remove(&netclock->callback_set, found_node);

    
    /* call the callback function */
    if(found_callback->function == NULL)
    {
        station_log(netclock->sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_NETCLOCK, "%s: callback %d is NULL", __FUNCTION__, reference.id); 
        errno = EINVAL;
        return -1;
    }
    
    station_log(netclock->sci->station, STATION_LOG_DEBUG, STATION_LOGTYPE_NETCLOCK, "%s: callback %d called", __FUNCTION__, reference.id); 
    found_callback->function(found_callback->data);
    
    return 0;
}

void netclock_hdr_dump(netclock_msg_hdr_t *hdr, int fd, char *buffer, int size)
{
    if(size <= 0)
        return;
    buffer[size - 1] = '\0';
    snprintf(buffer, size - 1, "netclick_hdr: version=%d, type=%d, id=%d, flags=%d, clock=%llu\n",
        hdr->version, hdr->type, ntohs(hdr->id), ntohs(hdr->flags),
        ((unsigned long long)ntohl(hdr->tick_high) << 32) | (unsigned long long)ntohl(hdr->tick_low));
    write(fd, buffer, strlen(buffer));
    return;
}