mrpd: comment cleanup
[profile/ivi/OpenAVB.git] / daemons / mrpd / mrp.h
1 /******************************************************************************
2
3   Copyright (c) 2012, Intel Corporation 
4   All rights reserved.
5   
6   Redistribution and use in source and binary forms, with or without 
7   modification, are permitted provided that the following conditions are met:
8   
9    1. Redistributions of source code must retain the above copyright notice, 
10       this list of conditions and the following disclaimer.
11   
12    2. Redistributions in binary form must reproduce the above copyright 
13       notice, this list of conditions and the following disclaimer in the 
14       documentation and/or other materials provided with the distribution.
15   
16    3. Neither the name of the Intel Corporation nor the names of its 
17       contributors may be used to endorse or promote products derived from 
18       this software without specific prior written permission.
19   
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31
32 ******************************************************************************/
33
34 #define MRP_ENCODE_YES          0       /* must send */
35 #define MRP_ENCODE_OPTIONAL     1       /* send if smaller */
36
37 typedef struct mrp_applicant_attribute {
38         int mrp_state;
39         int tx;                 /* tx=1 means transmit on next TX event */
40         int sndmsg;             /* sndmsg={NEW,IN,JOININ,JOINMT,MT, or LV} */
41         int encode;             /* when tx=1, NO, YES or OPTIONAL */
42 } mrp_applicant_attribute_t;
43
44 typedef struct mrp_registrar_attribute {
45         int mrp_state;
46         int notify;
47         short rsvd;
48         unsigned char macaddr[6];       /* mac address of last registration */
49 } mrp_registrar_attribute_t;
50
51 /* MRP Application Notifications */
52 #define MRP_NOTIFY_NONE         0
53 #define MRP_NOTIFY_NEW          1
54 #define MRP_NOTIFY_JOIN         2
55 #define MRP_NOTIFY_LV           3
56
57 /* Applicant counts number of NEW, JOIN_IN and JOIN_EMPTY states sent,
58  * as well as number of JOIN_IN messages received by peers.
59  * Upon receipt of LEAVE or LEAVEALL, applicant ensures at least 2 NEW, JOIN_IN
60  * or JOIN_EMPTY (or JOIN_IN from peers) have been sent since the last LEAVEALL.
61  */
62
63 /* Applicant FSM states */
64 #define MRP_VO_STATE    0       /* Very Anxious Observer */
65 #define MRP_VP_STATE    1       /* Very Anxious Passive */
66 #define MRP_VN_STATE    2       /* Very Anxious New */
67 #define MRP_AN_STATE    3       /* Anxious New */
68 #define MRP_AA_STATE    4       /* Anxious New */
69 #define MRP_QA_STATE    5       /* Quiet Active */
70 #define MRP_LA_STATE    6       /* Leaving Active */
71 #define MRP_AO_STATE    7       /* Anxious Observer State */
72 #define MRP_QO_STATE    8       /* Quiet Observer State */
73 #define MRP_AP_STATE    9       /* Anxious Passive State */
74 #define MRP_QP_STATE    10      /* Quiet Passive State */
75 #define MRP_LO_STATE    11      /* Leaving Observer State */
76
77 /* Registrar States */
78 #define MRP_IN_STATE    16      /* when Registrar state is IN */
79 #define MRP_LV_STATE    17      /* registrar state - leaving */
80 #define MRP_MT_STATE    18      /* whe Registrar state is MT or LV */
81
82 /* MRP Events */
83 #define MRP_EVENT_BEGIN 100     /*  Initialize state machine (10.7.5.1) */
84 #define MRP_EVENT_NEW   200     /*  A new declaration (10.7.5.4) */
85 #define MRP_EVENT_JOIN  300     /*  Declaration registration (10.7.5.5) */
86 #define MRP_EVENT_LV    400     /*  Withdraw a declaration (10.7.5.6) */
87 #define MRP_EVENT_TX    500     /*  Tx without LVA (10.7.5.7) */
88 #define MRP_EVENT_TXLA  600     /*  Tx with a LVA (10.7.5.8) */
89 #define MRP_EVENT_TXLAF 700     /*  Tx with a LVA, no room (Full) (10.7.5.9) */
90 #define MRP_EVENT_RNEW  800     /*  Rx New message (10.7.5.14) */
91 #define MRP_EVENT_RJOININ 900   /*  Rx JoinIn message (10.7.5.15),  */
92 #define MRP_EVENT_RIN   1000    /*  receive In message (10.7.5.18) */
93 #define MRP_EVENT_RJOINMT 1100  /*  receive JoinEmpty message (10.7.5.16) */
94 #define MRP_EVENT_RMT   1200    /*  receive Empty message (10.7.5.19) */
95 #define MRP_EVENT_RLV   1300    /*  receive Leave message (10.7.5.17) */
96 #define MRP_EVENT_RLA   1400    /*  receive a LeaveAll message (10.7.5.20) */
97 #define MRP_EVENT_FLUSH 1500    /*  Port role change (10.7.5.2) */
98 #define MRP_EVENT_REDECLARE 1600        /*  Port role changes (10.7.5.3) */
99 #define MRP_EVENT_PERIODIC 1700 /*  periodic timer expire */
100 #define MRP_EVENT_PERIODIC_ENABLE 1800  /*  periodic timer enable */
101 #define MRP_EVENT_PERIODIC_DISABLE 1900 /*  periodic timer disable */
102 #define MRP_EVENT_LVTIMER  2000 /*  leave timer expire */
103 #define MRP_EVENT_LVATIMER 2100 /*  leaveall timer expire */
104
105 #define MRP_SND_NEW     0       /* declare and register a new attribute from a new participant */
106 #define MRP_SND_JOIN    1       /* declare and register an attribute (generally) */
107 #define MRP_SND_IN      2
108 #define MRP_SND_LV      6
109 #define MRP_SND_LVA     7
110 #define MRP_SND_NULL    8       /* sent as 'ignore' to improve encoding */
111 #define MRP_SND_NONE    9
112
113 /* timer defaults from 802.1Q-2011, Table 10-7 */
114
115 #define MRP_JOINTIMER_VAL       200     /* join timeout in msec */
116 #define MRP_LVTIMER_VAL         1000    /* leave timeout in msec */
117 #define MRP_LVATIMER_VAL        10000   /* leaveall timeout in msec */
118 #define MRP_PERIODTIMER_VAL     1000    /* periodic timeout in msec */
119
120 typedef struct mrp_timer {
121         int state;
122         int tx;                 /* tx=1 means transmit on next TX event */
123         int sndmsg;             /* sndmsg={NEW,JOIN,or LV}  */
124 } mrp_timer_t;
125
126 #define MRP_TIMER_PASSIVE       0
127 #define MRP_TIMER_ACTIVE        1
128
129 #define MRP_REGISTRAR_CTL_NORMAL        0
130 #define MRP_REGISTRAR_CTL_FIXED         1
131 #define MRP_REGISTRAR_CTL_FORBIDDEN     2
132
133 #define MRP_APPLICANT_CTL_NORMAL        0
134 #define MRP_APPLICANT_CTL_SILENT        1
135
136 #define MRPDU_ENDMARK   0x0000
137 #define MRPDU_ENDMARK_SZ        2
138
139 #define MRPDU_NULL_LVA  0
140 #define MRPDU_LVA       1
141 #define MRPDU_NEW       0
142 #define MRPDU_JOININ    1
143 #define MRPDU_IN        2
144 #define MRPDU_JOINMT    3
145 #define MRPDU_MT        4
146 #define MRPDU_LV        5
147
148 #define MRPDU_3PACK_ENCODE(x, y, z)     (((((x) * 6) + (y)) * 6) + (z))
149 #define MRPDU_4PACK_ENCODE(w, x, y, z)  (((w) * 64) + ((x) * 16) + \
150                                                 ((y) * 4) + (z))
151
152 typedef struct mrpdu_vectorattrib {
153         uint16_t VectorHeader;  /* LVA << 13 | NumberOfValues */
154         uint8_t FirstValue_VectorEvents[];
155 } mrpdu_vectorattrib_t;
156
157 #define MRPDU_VECT_NUMVALUES(x) ((x) & ((1 << 13) - 1))
158 #define MRPDU_VECT_LVA(x)       ((x) & (1 << 13))
159
160 typedef struct client {
161         struct client *next;
162         struct sockaddr_in client;
163 } client_t;
164
165 struct mrp_database {
166         mrp_timer_t lva;
167         mrp_timer_t periodic;
168         HTIMER join_timer;
169         HTIMER lv_timer;
170         HTIMER lva_timer;
171         client_t *clients;
172         int registration;
173         int participant;
174 };
175
176 int mrp_client_add(client_t ** list, struct sockaddr_in *newclient);
177 int mrp_client_delete(client_t ** list, struct sockaddr_in *newclient);
178
179 int mrp_init(void);
180 int mrp_jointimer_stop(struct mrp_database *mrp_db);
181 int mrp_jointimer_start(struct mrp_database *mrp_db);
182 int mrp_lvtimer_start(struct mrp_database *mrp_db);
183 int mrp_lvtimer_stop(struct mrp_database *mrp_db);
184 int mrp_lvatimer_start(struct mrp_database *mrp_db);
185 int mrp_lvatimer_stop(struct mrp_database *mrp_db);
186 int mrp_lvatimer_fsm(struct mrp_database *mrp_db, int event);
187 int mrp_applicant_fsm(mrp_applicant_attribute_t * attrib, int event);
188 int mrp_registrar_fsm(mrp_registrar_attribute_t * attrib,
189                       struct mrp_database *mrp_db, int event);
190 int mrp_decode_state(mrp_registrar_attribute_t * rattrib,
191                      mrp_applicant_attribute_t * aattrib, char *str,
192                      int strlen);