summaryrefslogtreecommitdiffstats
path: root/ucs2-lib/src/ucs_net.c
blob: 4e4b27494978ef4e59e75161469cf9b8159a1267 (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
/*------------------------------------------------------------------------------------------------*/
/* UNICENS V2.1.0-3491                                                                            */
/* Copyright (c) 2017 Microchip Technology Germany II GmbH & Co. KG.                              */
/*                                                                                                */
/* This program is free software: you can redistribute it and/or modify                           */
/* it under the terms of the GNU General Public License as published by                           */
/* the Free Software Foundation, either version 2 of the License, or                              */
/* (at your option) any later version.                                                            */
/*                                                                                                */
/* This program is distributed in the hope that it will be useful,                                */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of                                 */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                  */
/* GNU General Public License for more details.                                                   */
/*                                                                                                */
/* You should have received a copy of the GNU General Public License                              */
/* along with this program.  If not, see <http://www.gnu.org/licenses/>.                          */
/*                                                                                                */
/* You may also obtain this software under a propriety license from Microchip.                    */
/* Please contact Microchip for further information.                                              */
/*------------------------------------------------------------------------------------------------*/

/*!
 * \file
 * \brief Implementation of the Network Management.
 *
 * \cond UCS_INTERNAL_DOC
 * \addtogroup G_NET
 * @{
 */

/*------------------------------------------------------------------------------------------------*/
/* Includes                                                                                       */
/*------------------------------------------------------------------------------------------------*/
#include "ucs_net.h"
#include "ucs_misc.h"
#include "ucs_trace.h"

/*------------------------------------------------------------------------------------------------*/
/* Service parameters                                                                             */
/*------------------------------------------------------------------------------------------------*/
/*! Priority of the NET service used by scheduler */
static const uint8_t NET_SRV_PRIO                   = 251U;   /* parasoft-suppress  MISRA2004-8_7 "Value shall be part of the module, not part of a function." */
/*! \brief Event to trigger notification of MOST Network Status */
static const Srv_Event_t NET_EVENT_NOTIFY_NW_STATUS = 1U;
/*! \brief Event to trigger notification of MOST Network Configuration */
static const Srv_Event_t NET_EVENT_NOTIFY_NW_CONFIG = 2U;

/*------------------------------------------------------------------------------------------------*/
/* Internal constants                                                                             */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Default value used for INIC sender handles */
static const uint16_t NET_DEFAULT_SENDER_HANDLE = 0x0001U;  /* parasoft-suppress  MISRA2004-8_7 "Value shall be part of the module, not part of a function." */
/*! \brief Initialization timeout in milliseconds (t = 7s) */
static const uint16_t NET_PBW_TIMEOUT           = 7000U;    /* parasoft-suppress  MISRA2004-8_7 "Value shall be part of the module, not part of a function." */

/*------------------------------------------------------------------------------------------------*/
/* Internal prototypes                                                                            */
/*------------------------------------------------------------------------------------------------*/
static void Net_Service(void *self);
static void Net_UpdateNetworkStatus(void *self, void *data_ptr);
static void Net_UpdateNetworkConfiguration(void *self, void *data_ptr);

/*------------------------------------------------------------------------------------------------*/
/* Implementation of class CNetworkManagement                                                     */
/*------------------------------------------------------------------------------------------------*/
/*! \brief Constructor of the Network Management class.
 *  \param self        Instance pointer
 *  \param init_ptr    Reference to the initialization data
 */
void Net_Ctor(CNetworkManagement *self, Net_InitData_t *init_ptr)
{
    MISC_MEM_SET(self, 0, sizeof(*self));
    self->base_ptr = init_ptr->base_ptr;
    self->inic_ptr = init_ptr->inic_ptr;
    Obs_Ctor(&self->network_status.observer, self, &Net_UpdateNetworkStatus);
    Inic_AddObsrvNwStatus(self->inic_ptr, &self->network_status.observer);
    self->network_status.param.change_mask = 0xFFFFU;               /* Used for initial notification! */
    Sub_Ctor(&self->network_status.pre_subject, self->base_ptr->ucs_user_ptr);
    Sub_Ctor(&self->network_status.subject, self->base_ptr->ucs_user_ptr);

    Obs_Ctor(&self->network_configuration.observer, self, &Net_UpdateNetworkConfiguration);
    Inic_AddObsvrNwConfig(self->inic_ptr, &self->network_configuration.observer);
    self->network_configuration.param.change_mask = 0xFFFFU;        /* Used for initial notification! */
    Sub_Ctor(&self->network_configuration.pre_subject, self->base_ptr->ucs_user_ptr);
    Sub_Ctor(&self->network_configuration.subject, self->base_ptr->ucs_user_ptr);

    Srv_Ctor(&self->net_srv, NET_SRV_PRIO, self, &Net_Service);     /* Initialize Network Management service */
    (void)Scd_AddService(&self->base_ptr->scd, &self->net_srv);     /* Add NET service to scheduler */
}

/*! \brief Service function of the network management.
 *  \param self    Instance pointer
 */
static void Net_Service(void *self)
{
    CNetworkManagement *self_ = (CNetworkManagement *)self;
    Srv_Event_t event_mask;
    Srv_GetEvent(&self_->net_srv, &event_mask);
    /* Notification of MOST Network Status triggered? */
    if((event_mask & NET_EVENT_NOTIFY_NW_STATUS) == NET_EVENT_NOTIFY_NW_STATUS)
    {
        Srv_ClearEvent(&self_->net_srv, NET_EVENT_NOTIFY_NW_STATUS);
        self_->network_status.param.change_mask = 0xFFFFU;
        Sub_Notify(&self_->network_status.pre_subject, &self_->network_status.param);
        self_->network_status.param.change_mask = 0U;
        (void)Sub_SwitchObservers(&self_->network_status.subject,
                                  &self_->network_status.pre_subject);
    }
    /* Notification of MOST Network Configuration triggered? */
    if((event_mask & NET_EVENT_NOTIFY_NW_CONFIG) == NET_EVENT_NOTIFY_NW_CONFIG)
    {
        Srv_ClearEvent(&self_->net_srv, NET_EVENT_NOTIFY_NW_CONFIG);
        self_->network_configuration.param.change_mask = 0xFFFFU;
        Sub_Notify(&self_->network_configuration.pre_subject, &self_->network_configuration.param);
        self_->network_configuration.param.change_mask = 0U;
        (void)Sub_SwitchObservers(&self_->network_configuration.subject,
                                  &self_->network_configuration.pre_subject);
    }
}

/*! \brief Adds an observer which is called if the network status has been changed.
 *  \param self                Instance pointer
 *  \param obs_ptr             Reference to an observer
 */
void Net_AddObserverNetworkStatus(CNetworkManagement *self, CMaskedObserver *obs_ptr)
{
    (void)Msub_AddObserver(&self->network_status.pre_subject, obs_ptr);
    Srv_SetEvent(&self->net_srv, NET_EVENT_NOTIFY_NW_STATUS);
}

/*! \brief Removes an observer registered by Net_AddObserverNetworkStatus().
 *  \param self        Instance pointer
 *  \param obs_ptr     Reference to observer to be removed

 */
void Net_DelObserverNetworkStatus(CNetworkManagement *self, CMaskedObserver *obs_ptr)
{
    (void)Msub_RemoveObserver(&self->network_status.pre_subject, obs_ptr);
    (void)Msub_RemoveObserver(&self->network_status.subject, obs_ptr);
}

/*! \brief Adds an observer which is called if the network configuration has been changed.
 *  \param self                Instance pointer
 *  \param obs_ptr             Reference to an observer
 */
void Net_AddObserverNetworkConfig(CNetworkManagement *self, CMaskedObserver *obs_ptr)
{
    (void)Msub_AddObserver(&self->network_configuration.pre_subject, obs_ptr);
    Srv_SetEvent(&self->net_srv, NET_EVENT_NOTIFY_NW_CONFIG);
}

/*! \brief Removes an observer registered by Net_AddObserverNetworkConfig().
 *  \param self        Instance pointer
 *  \param obs_ptr     Reference to observer to be removed
 */
void Net_DelObserverNetworkConfig(CNetworkManagement *self, CMaskedObserver *obs_ptr)
{
    (void)Msub_RemoveObserver(&self->network_configuration.pre_subject, obs_ptr);
    (void)Msub_RemoveObserver(&self->network_configuration.subject, obs_ptr);
}

/*! \brief Observer callback used for the MOST Network Status
 *  \param self        Instance pointer
 *  \param data_ptr    Reference to the data structure
 */
static void Net_UpdateNetworkStatus(void *self, void *data_ptr)
{
    Inic_StdResult_t *data_ptr_ = (Inic_StdResult_t *)data_ptr;

    if(data_ptr_->result.code == UCS_RES_SUCCESS)
    {
        CNetworkManagement *self_ = (CNetworkManagement *)self;
        Inic_NetworkStatus_t result = *((Inic_NetworkStatus_t *)data_ptr_->data_info);

        /* Check for changes */
        if(result.events != 0U)     /* Notify only if at least one event flag is set */
        {
            self_->network_status.param.change_mask |= 0x0001U;
        }
        if(self_->network_status.param.availability != result.availability)
        {
            self_->network_status.param.change_mask |= 0x0002U;
        }
        if(self_->network_status.param.avail_info != result.avail_info)
        {
            self_->network_status.param.change_mask |= 0x0004U;
        }
        if(self_->network_status.param.avail_trans_cause != result.avail_trans_cause)
        {
            self_->network_status.param.change_mask |= 0x0008U;
        }
        if(self_->network_status.param.node_address != result.node_address)
        {
            self_->network_status.param.change_mask |= 0x0010U;
        }
        if(self_->network_status.param.node_position != result.node_position)
        {
            self_->network_status.param.change_mask |= 0x0020U;
        }
        if(self_->network_status.param.max_position != result.max_position)
        {
            self_->network_status.param.change_mask |= 0x0040U;
        }
        if(self_->network_status.param.packet_bw != result.packet_bw)
        {
            self_->network_status.param.change_mask |= 0x0080U;
        }

        /* Update MOST Network Status parameters */
        self_->network_status.param.events            = result.events;
        self_->network_status.param.availability      = result.availability;
        self_->network_status.param.avail_info        = result.avail_info;
        self_->network_status.param.avail_trans_cause = result.avail_trans_cause;
        self_->network_status.param.node_address      = result.node_address;
        self_->network_status.param.node_position     = result.node_position;
        self_->network_status.param.max_position      = result.max_position;
        self_->network_status.param.packet_bw         = result.packet_bw;

        /* Notify observer? */
        Msub_Notify(&self_->network_status.subject,
                    &self_->network_status.param,
                    (uint32_t)self_->network_status.param.change_mask);

        /* Clear change-mask */
        self_->network_status.param.change_mask = 0U;
    }
}

/*! \brief Observer callback used for the MOST Network Configuration
 *  \param self        Instance pointer
 *  \param data_ptr    Reference to the data structure
 */
static void Net_UpdateNetworkConfiguration(void *self, void *data_ptr)
{
    Inic_StdResult_t *data_ptr_ = (Inic_StdResult_t *)data_ptr;

    if(data_ptr_->result.code == UCS_RES_SUCCESS)
    {
        CNetworkManagement *self_ = (CNetworkManagement *)self;
        Inic_NetworkConfig_t result = *((Inic_NetworkConfig_t *)data_ptr_->data_info);

        /* Check for changes */
        if(self_->network_configuration.param.node_address != result.node_address)
        {
            self_->network_configuration.param.change_mask |= 0x0001U;
        }
        if(self_->network_configuration.param.group_address != result.group_address)
        {
            self_->network_configuration.param.change_mask |= 0x0002U;
        }
        if(self_->network_configuration.param.llrbc != result.llrbc)
        {
            self_->network_configuration.param.change_mask |= 0x0004U;
        }

        /* Update MOST Network Configuration parameters */
        self_->network_configuration.param.node_address  = result.node_address;
        self_->network_configuration.param.group_address = result.group_address;
        self_->network_configuration.param.llrbc         = result.llrbc;

        /* Notify observer? */
        Msub_Notify(&self_->network_configuration.subject,
                    &self_->network_configuration.param,
                    (uint32_t)self_->network_configuration.param.change_mask);

        /* Clear change-mask */
        self_->network_configuration.param.change_mask = 0U;
    }
}

/*! \brief Checks if the given address matches with the own node address, node position or group address.
 *  \param self     Instance pointer
 *  \param address  Address to be checked
 *  \return Possible return values are shown in the table below.
 *          Value                 | Description 
 *          --------------------- | -------------------------------------------------------------
 *          NET_IS_OWN_ADDR_NODE  | Is own node position address or own logical node address
 *          NET_IS_OWN_ADDR_GROUP | Is own group address
 *          NET_IS_OWN_ADDR_NONE  | Is foreign address
 */
Net_IsOwnAddrResult_t Net_IsOwnAddress(CNetworkManagement *self, uint16_t address)
{
    Net_IsOwnAddrResult_t ret_val;

    if((self->network_status.param.node_address == address) ||
       (((uint16_t)self->network_status.param.node_position + (uint16_t)0x400) == address))
    {
        ret_val = NET_IS_OWN_ADDR_NODE;
    }
    else if(self->network_configuration.param.group_address == address)
    {
        ret_val = NET_IS_OWN_ADDR_GROUP;
    }
    else
    {
        ret_val = NET_IS_OWN_ADDR_NONE; 
    }

    return ret_val;
}

/*!
 * @}
 * \endcond
 */

/*------------------------------------------------------------------------------------------------*/
/* End of file                                                                                    */
/*------------------------------------------------------------------------------------------------*/