1 /* -*- Mode: C; tab-width:4 -*- */
2 /* ex: set ts=4 shiftwidth=4 softtabstop=4 cindent: */
5 #include <sys_module.h>
6 #include <wiring_config.h>
12 #define SOURCE_MOD_PID DFLT_APP_ID0
16 // The pointers to function control blocks corresponding to
17 // the output(s) should appear first in the state. Other
18 // function CB's should follow after that.
20 func_cb_ptr output0
; //!< Function control block for output port 0
21 //func_cb_ptr put_token; //!< This function control block is used in SOS_CALL
23 //!< needs to put a token on any output port.
24 //!< The output port is identified by passing
25 //!< one of the function control blocks defined
26 //!< above (output0 or output1).
27 func_cb_ptr signal_error
; //!< Used with split-phase operations to indicate
28 //!< an error to the wiring engine after accepting
32 uint8_t sample_rate_in_sec
; //!< Exposed parameter. Controls timer period of application.
35 static int8_t element_module(void *state
, Message
*msg
);
37 // Function corresponding to the input port. Each port will
38 // have its own input function which will be published by the module.
39 // The prototypes of the input and output functions should match,
40 // else linking through the wiring engine will also not work.
41 // Since, all the input functions have a common prototype (for
42 // the indirect call through engine to work), the output
43 // functions should also have "cyC2" as their prototype.
44 // This is currently kept for simplicity, but can easily be
45 // used to enforce type checking at run-time too without any overhead.
46 // Split-phase operations should return -EBUSY after accepting
47 // an input token so that the wiring engine knows that the module
48 // will take long to complete that operation and it should queue
49 // all the tokens meant for that port till the module puts
50 // a result on any of its output ports.
51 //static int8_t input0 (func_cb_ptr p, void *data, uint16_t length);
53 // Each module that wants to provide access to its parameters for
54 // update should publish this function. The parameters will be
55 // bundled in the order as defined by its corresponding MoML file
57 static int8_t update_param (func_cb_ptr p
, void *data
, uint16_t length
);
59 static mod_header_t mod_header SOS_MODULE_HEADER
= {
60 .mod_id
= SOURCE_MOD_PID
,
61 .code_id
= ehtons(SOURCE_MOD_PID
),
62 .platform_type
= HW_TYPE
,
63 .processor_type
= MCU_TYPE
,
64 .state_size
= sizeof(element_state_t
),
68 .module_handler
= element_module
,
70 {error_8
, "cyC2", SOURCE_MOD_PID
, INVALID_GID
},
71 //{error_8, "cyC4", MULTICAST_SERV_PID, DISPATCH_FID},
72 {error_8
, "ccv1", MULTICAST_SERV_PID
, SIGNAL_ERR_FID
},
73 {update_param
, "cwS2", SOURCE_MOD_PID
, UPDATE_PARAM_FID
},
77 static int8_t element_module(void *state
, Message
*msg
) {
78 element_state_t
*s
= (element_state_t
*)state
;
85 // Default parameter value = 5 sec.
86 s
->sample_rate_in_sec
= 5;
88 sys_timer_start(TIMER_TID
, ((uint32_t)s
->sample_rate_in_sec
) * 1024L, TIMER_ONE_SHOT
);
91 case MSG_TIMER_TIMEOUT
:
93 LED_DBG(LED_RED_TOGGLE
);
95 DEBUG("Timer fired. Put token %d on output port. Function CB output = 0x%x.\n",
97 token_type_t
*my_token
= create_token(&s
->cnt
, sizeof(uint8_t), s
->pid
);
98 if (my_token
== NULL
) return -ENOMEM
;
99 //SOS_CALL(s->put_token, put_token_func_t, s->output0, my_token);
100 dispatch(s
->output0
, my_token
);
101 destroy_token(my_token
);
102 sys_timer_start(TIMER_TID
, ((uint32_t)s
->sample_rate_in_sec
) * 1024L, TIMER_ONE_SHOT
);
109 default: return -EINVAL
;
114 static int8_t update_param (func_cb_ptr p
, void *data
, uint16_t length
) {
115 element_state_t
*s
= (element_state_t
*)sys_get_state();
116 s
->sample_rate_in_sec
= *((uint8_t *)data
);
117 DEBUG("Sample rate updated to %d seconds.\n", s
->sample_rate_in_sec
);
122 mod_header_ptr
source_get_header()
124 return sos_get_header_address(mod_header
);