Added SHT11 driver for TMote.
[sos-2x.git] / modules / vire / wiring_test / source.c
blob4e7918939d99feef75f6e5b750f7526a0ad9127b
1 /* -*- Mode: C; tab-width:4 -*- */
2 /* ex: set ts=4 shiftwidth=4 softtabstop=4 cindent: */
4 #include <module.h>
5 #include <sys_module.h>
6 #include <wiring_config.h>
8 //#define LED_DEBUG
9 #include <led_dbg.h>
11 // Private
12 #define SOURCE_MOD_PID DFLT_APP_ID0
13 #define TIMER_TID 0
15 // Element state.
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.
19 typedef struct {
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
22 //!< when the element
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
29 //!< the input token.
30 uint8_t cnt;
31 sos_pid_t pid;
32 uint8_t sample_rate_in_sec; //!< Exposed parameter. Controls timer period of application.
33 } element_state_t;
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
56 // in Ptolemy.
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),
65 .num_out_port = 1,
66 .num_sub_func = 2,
67 .num_prov_func = 1,
68 .module_handler = element_module,
69 .funct = {
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},
74 },
77 static int8_t element_module(void *state, Message *msg) {
78 element_state_t *s = (element_state_t *)state;
80 switch (msg->type) {
81 case MSG_INIT:
83 LED_DBG(LED_RED_ON);
84 s->cnt = 0;
85 // Default parameter value = 5 sec.
86 s->sample_rate_in_sec = 5;
87 s->pid = msg->did;
88 sys_timer_start(TIMER_TID, ((uint32_t)s->sample_rate_in_sec) * 1024L, TIMER_ONE_SHOT);
89 break;
91 case MSG_TIMER_TIMEOUT:
93 LED_DBG(LED_RED_TOGGLE);
94 s->cnt++;
95 DEBUG("Timer fired. Put token %d on output port. Function CB output = 0x%x.\n",
96 s->cnt, s->output0);
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);
103 break;
105 case MSG_FINAL:
107 break;
109 default: return -EINVAL;
111 return SOS_OK;
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);
118 return SOS_OK;
121 #ifndef _MODULE_
122 mod_header_ptr source_get_header()
124 return sos_get_header_address(mod_header);
126 #endif