summaryrefslogtreecommitdiff
path: root/cesar/maximus/python/utest/stationtest/src/main_example.c
blob: a2023dc7619b778cdb77705252c5b56aae1b16cb (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    main_example.c
 * \brief   example of a main program of a station executable
 * \ingroup
 */

#include <cyg/infra/diag.h>
#include <cyg/kernel/kapi.h>
#include <errno.h>
#include "common/std.h"
#include "host/station/station.h"
#include "hal/phy/phy.h"
#include "hal/phy/maximus/inc/maximus_phy_ctx.h" // for 'phy_t'
#include "mac/common/tonemask.h"

extern station_ctx_t my_station;
phy_t * phy;

bool phy_rx_fc_cb (void *user, u32 rx_date, const u32 *fc_av)
{
    diag_write_string("=> phy_rx_fc_cb\n");

    phy_rx_prepare(phy, 1,
                   PHY_COMBO_PARAMS (PHY_MOD_MINI_ROBO, PHY_FEC_RATE_1_2,
                                     PHY_PB_SIZE_136), PHY_GIL_567, 0, 1);

    return true;
}

bool phy_access_cb (void *user)
{
    return true;
}

bool phy_access_conf_cb (void *user)
{
    return true;
}

bool phy_pbdma_cb (void *user, u32 status_word)
{
    return true;
}

bool phy_tx_false_alarm_cb (void *user)
{
    return true;
}

bool phy_zero_cross_cb (void *user, u32 zero_cross_date)
{
    return true;
}

void phy_deferred_cb (void *user)
{
    return;
}

bool phy_extra_timer_cb (void *user)
{
    return true;
}

typedef struct my_struct
{
    int i1;
    int i2;
    bool b;
} my_struct_t;

typedef long long my_type_t;

my_type_t my_param4;

unsigned int my_param6 = 123; // 0x0000007B
unsigned int my_param7 = 456; // 0x000001C8
bool my_param8 = false;
bool my_param9 = true;
unsigned short my_param10 = 0xFFEE;
unsigned long my_param11 = 0xFFEEDDCC;
unsigned long long my_param12 = 0xBBAA9988FFEEDDCCLL;


void my_return_function (void *data)
{
    static fcall_param_t return_param;
    static sci_msg_t return_msg;
    static unsigned char return_buffer[1024];
    unsigned short *return_msg_id = (unsigned short *)data;

    /* init structures */
    fcall_param_init(&return_param, "function_1_rsp", *return_msg_id);
    if (0 != sci_msg_init(&return_msg, return_buffer, 1024))
    {
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_SCI,
                    "%s: errno = %d", __FUNCTION__, errno);
    }

    /* do other tasks ... */
    diag_write_string("=> my_return_function\n");

    /* now make the return parameter list */
    fcall_param_reset(&return_param);
    fcall_param_add(&return_param, &return_msg, "result_2", sizeof(my_type_t), &my_param4);
    fcall_return(my_station.fcall, &return_param, &return_msg);

    return;
}


int my_function1 (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    static unsigned short asynchronous_msg_id = 0;
    my_struct_t my_param1;
    int my_param2[10];
    char my_param3[6];
    int i;

    // for netclock message
    netclock_id_t id;
    static netclock_callback_t my_return_function_cb;

    /* get parameters content */
    fcall_param_bind(*param, *msg, "param_1", sizeof(my_struct_t), &my_param1);
    fcall_param_bind(*param, *msg, "param_2", 10*sizeof(int), my_param2);
    fcall_param_bind(*param, *msg, "param_3", 6, (char*)my_param3);
    fcall_param_bind(*param, *msg, "param_4", sizeof(my_type_t), &my_param4);

    /* do other tasks ... */
    diag_write_string("=> my_function1\n");
    diag_printf("param 1 . i1 = %d\n", my_param1.i1);
    diag_printf("param 1 . i2 = %d\n", my_param1.i2);
    diag_printf("param 1 . b = %d\n", my_param1.b);
    diag_write_string("param 2 = ");
    for (i=0; i<10; i++)
    {
        diag_printf(" %d", my_param2[i]);
    }
    diag_write_string("\n");
    diag_printf("param 3 = %s\n", my_param3);
    diag_printf("param 4 = %lld\n", my_param4);

    /* prepare the future asynchronous return */
    fcall_param_set_async(*param, 1);
    asynchronous_msg_id = (*param)->msg_id;

    /* schedule the future my_return_function() call */
    // send netclock message to Maximus to call my_return_function()
    if (-1 == netclock_schedule(my_station.netclock,
                                &my_return_function_cb,
                                NETWORK_CLOCK_TYPE_STATION,
                                my_station.current_tick_tck+10,
                                &my_return_function,
                                (void*)&asynchronous_msg_id,
                                &id))
    {
        diag_printf("errno = %d\n", errno);
    }

    /* now return */
    return 0;
}


int my_function2 (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    bool my_param5;
    char my_result1[] = "this is result 1\0";

    /* get parameters content */
    if (-1 == fcall_param_bind(*param, *msg, "param_5", sizeof(bool), &my_param5))
    {
        diag_printf("errno = %d\n", errno);
        diag_printf("param nb = %d\n", (*param)->param_nb);
    }

    /* do other tasks ... */
    /* this line write in the TERMINAL => disable. */
    // diag_write_string("=> my_function2\n");

    /* now make the return parameter list */
    fcall_param_reset(*param);
    fcall_param_add(*param, *msg, "result_1", strlen(my_result1)+1, &my_result1);

    return 0;
}


void my_function_rsp (void *data)
{
    static fcall_param_t return_param;
    static sci_msg_t return_msg;
    static unsigned char return_buffer[1024];
    unsigned short *return_msg_id = (unsigned short *)data;

    /* init structures */
    fcall_param_init(&return_param, "function_rsp", *return_msg_id);
    if (0 != sci_msg_init(&return_msg, return_buffer, 1024))
    {
        station_log(&my_station, STATION_LOG_ERROR, STATION_LOGTYPE_SCI,
                    "%s: errno = %d", __FUNCTION__, errno);
    }

    /* do other tasks ... */
    diag_write_string("=> my_function_rsp\n");

    /* now make the return parameter list */
    fcall_param_reset(&return_param);
    fcall_return(my_station.fcall, &return_param, &return_msg);

    return;
}


int my_function3 (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    static unsigned short asynchronous_msg_id = 0;

    /* do other tasks ... */
    diag_write_string("=> my_function3\n");

    /* prepare the future asynchronous return */
    fcall_param_set_async(*param, 1);
    asynchronous_msg_id = (*param)->msg_id;

    /* call my_function_rsp() */
    my_function_rsp((void*)&asynchronous_msg_id);

    return 0;
}


int my_function4 (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    static unsigned short asynchronous_msg_id = 0;

    /* do other tasks ... */
    diag_write_string("=> my_function4\n");

    /* prepare the future asynchronous return */
    fcall_param_set_async(*param, 1);
    asynchronous_msg_id = (*param)->msg_id;

    /* call my_function_rsp() */
    my_function_rsp((void*)&asynchronous_msg_id);

    return 0;
}


int my_function5 (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    static unsigned short asynchronous_msg_id = 0;

    /* do other tasks ... */
    diag_write_string("=> my_function5\n");

    /* prepare the future asynchronous return */
    fcall_param_set_async(*param, 1);
    asynchronous_msg_id = (*param)->msg_id;

    /* call my_function_rsp() */
    my_function_rsp((void*)&asynchronous_msg_id);

    return 0;
}


int set_tonemask (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    uint carrier_nb = 0;
    u32 tonemask[PHY_TONEMASK_WORDS] = {0};

    diag_write_string("=> set_tonemask\n");

    // Initialize the HAL PHY
    phy = phy_init ((void*)&my_param4, &phy_rx_fc_cb, &phy_access_cb, &phy_access_conf_cb, &phy_pbdma_cb, &phy_tx_false_alarm_cb, &phy_deferred_cb);
    phy_zero_cross_init (phy, &phy_zero_cross_cb);

    // Enable assertions on warnings
    phy->warning_assert = true;

    // Set TONEMASK
    carrier_nb = tonemask_default (tonemask);
    phy_set_tonemask (phy, tonemask, carrier_nb);

    /* now make the return parameter list */
    fcall_param_reset(*param);

    return 0;
}


int prepare_rx (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    // Test with one PB of 128 octets
    static u8 first_pb_data[128];
    static phy_pb_t first_pb;
    static u32 nek[4];
    int i = 0;

    diag_write_string("=> prepare_rx\n");

    // Activate RX
    phy_rx_activate(phy, true, my_station.current_tick_tck, true);

    // Set RX parameters
    phy_rx_param(phy, PHY_FC_MODE_HYBRID_1);

    // Start PBDMA
    memset(first_pb_data, '1', 128);
    first_pb.pb_rx.blk.data = first_pb_data;
    first_pb.pb_rx.blk.next = NULL;
    for (i=0; i<4; i++)
    {
      nek[i] = i;
    }
    phy_pbdma_start(phy, true /* bypass_aes */, nek, 1 /* nb_total */,
                    1 /* nb_pb_it */, &first_pb, NULL, true);

    /* now make the return parameter list */
    fcall_param_reset(*param);

    return 0;
}


int uninit_phy (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    diag_write_string("=> uninit_phy\n");

    // Uninitialize the HAL PHY
    phy_uninit (phy);

    /* now make the return parameter list */
    fcall_param_reset(*param);

    return 0;
}


int function_to_call (fcall_ctx_t *fcall, fcall_param_t **param, sci_msg_t **msg, void *data)
{
    diag_write_string("=> function_to_call\n");

    /* now make the return parameter list */
    fcall_param_reset(*param);

    return 0;
}


int main (void)
{
    station_log_set_level(&my_station, STATION_LOG_NONE);
    station_log_set_mask(&my_station, STATION_LOGTYPE_ALL);

    fcall_register(my_station.fcall, "function_1", (void*)&my_function1, NULL);
    fcall_register(my_station.fcall, "function_2", (void*)&my_function2, NULL);
    fcall_register(my_station.fcall, "function_3", (void*)&my_function3, NULL);
    fcall_register(my_station.fcall, "function_4", (void*)&my_function4, NULL);
    fcall_register(my_station.fcall, "function_5", (void*)&my_function5, NULL);
    fcall_register(my_station.fcall, "set_tonemask", (void*)&set_tonemask, NULL);
    fcall_register(my_station.fcall, "prepare_rx", (void*)&prepare_rx, NULL);
    fcall_register(my_station.fcall, "uninit_phy", (void*)&uninit_phy, NULL);
    fcall_register(my_station.fcall, "function_to_call", (void*)&function_to_call, NULL);

    probe_register(my_station.probe, "param_6", sizeof(unsigned int), &my_param6);
    probe_register(my_station.probe, "param_7", sizeof(unsigned int), &my_param7);
    probe_register(my_station.probe, "param_8", sizeof(bool), &my_param8);
    probe_register(my_station.probe, "param_9", sizeof(bool), &my_param9);
    probe_register(my_station.probe, "param_10", sizeof(unsigned short), &my_param10);
    probe_register(my_station.probe, "param_11", sizeof(unsigned long), &my_param11);
    probe_register(my_station.probe, "param_12", sizeof(unsigned long long), &my_param12);

    return 0;
}