1 /****************************************
2 * File: netapi_sched.c
3 * Purpose: netapi scheduling module
4 * NOTE: This sample right now.
5 **************************************************************
6 * FILE: netapi_sched.c
7 *
8 * DESCRIPTION: netapi sample scheduler source file for user space transport
9 * library
10 *
11 * REVISION HISTORY: rev 0.0.1
12 *
13 * Copyright (c) Texas Instruments Incorporated 2010-2011
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 *
22 * Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the
25 * distribution.
26 *
27 * Neither the name of Texas Instruments Incorporated nor the names of
28 * its contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 ****************************************/
45 #include "netapi.h"
46 #include "netapi_sched.h"
49 /****************************************/
50 /************API************************/
51 /**************************************/
53 /* open a scheduling contex */
54 NETAPI_SCHED_HANDLE_T * netapi_schedOpen(NETAPI_T n, NETAPI_SCHED_CONFIG_T * p_config, int *p_err)
55 {
56 *p_err=0;
57 NETAPI_SCHED_HANDLE_T * ph = (NETAPI_SCHED_HANDLE_T *) netapi_get_scheduler(n);
58 if(!ph) {*p_err= NETAPI_ERR_NOMEM; return NULL;}
59 if(!p_config) {*p_err= NETAPI_ERR_BAD_INPUT; return NULL;}
60 memcpy(&ph->config,p_config,sizeof(NETAPI_SCHED_CONFIG_T));
61 ph->start = netapi_getTimestamp();
62 ph->back = (void *) n;
63 if (ph->config.valid_flags & NETAPI_SCHED_DURATION)
64 {
65 if (ph->config.duration == NETAPI_SCHED_FOREVER)
66 {
67 ph->shutdown_time=(uint64_t) -1;
68 }
69 else
70 {
71 ph->shutdown_time = ph->start + ph->config.duration;
72 }
73 }
74 else ph->shutdown_time = (uint64_t) -1;
75 ph->state =NETAPI_SCHED_STATE_ACTIVE;
76 return(ph);
78 }
80 /* re-configure a scheduling context */
81 int netapi_schedControl(NETAPI_SCHED_HANDLE_T *s, NETAPI_SCHED_CONFIG_T *p_config, int *p_err)
82 {
83 /* not implemented */
85 return 0;
86 }
89 /* main entry point. caller gives up control to scheduler */
90 int netapi_schedWaitForEvents(NETAPI_SCHED_HANDLE_T *s, int *p_err)
91 {
92 int err;
93 *p_err=0;
94 unsigned long long t = netapi_getTimestamp();
95 int next_house;
97 next_house = s->config.interval;
98 printf(">entering scheduling loop @%llx until %llx . interval=%x, (1st house=%lx)\n", t, s->shutdown_time,
99 s->config.interval, next_house);
100 /* loop for duration or until shutdown */
101 for(;t< s->shutdown_time;)
102 {
103 t = netapi_getTimestamp();
104 next_house -=1;
105 //poll all pktio channels we have open in RX mode
106 pktio_pollAll((NETAPI_T) s->back, NULL, &err);
108 //poll pktlib garbage collections for registered heaps..
109 netapi_pollHeapGarbage((NETAPI_T) s->back);
111 //todo timers (local and global)
112 netapi_TimerGroupPollAll((NETAPI_T) s->back, NETAPI_TIMER_FITLER_ALL, 100000);
114 //poll NETCP/PA control channels
115 netapi_netcpPoll((NETAPI_T) s->back);
117 //see if time to do a house keeping callback
118 if ((s->config.valid_flags & NETAPI_SCHED_CBV) && s->config.house_cb)
119 if (next_house<=0)
120 {
121 s->config.house_cb(s);
122 next_house = s->config.interval;
123 }
124 //see if we were closed and/or its time to close
125 if (s->state!= NETAPI_SCHED_STATE_ACTIVE)
126 { s->state=NETAPI_SCHED_STATE_SHUT; break;}
127 }
128 return 1;
129 }
131 /* shutdown scheduler context */
132 int netapi_schedShutdown(NETAPI_SCHED_HANDLE_T * s, NETAPI_SCHED_SHUTDOWN_T * p_close, int * p_err)
133 {
134 *p_err=0;
135 s->state=NETAPI_SCHED_STATE_SHUTTING; //say we are closing
137 return 1;
138 }