NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
RPL.c
1/************************************************************************************
2* Copyright (C) 2023 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* Author: Shashi Kant Suman *
12* *
13* ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include "RPL.h"
16#include "RPL_enum.h"
17
18char* GetStringRPL_Subevent(NETSIM_ID);
19int fn_NetSim_RPL_Init_F();
20int fn_NetSim_RPL_Finish_F();
21int fn_NetSim_RPL_Configure_F(void** var);
22int fn_NetSim_RPL_FreePacket_F(NetSim_PACKET* packet);
23int fn_NetSim_RPL_CopyPacket_F(NetSim_PACKET* destPacket, NetSim_PACKET* srcPacket);
24
25/**
26RPL Init function initializes the RPL parameters.
27*/
28_declspec (dllexport) int fn_NetSim_RPL_Init(struct stru_NetSim_Network *NETWORK_Formal,
29 NetSim_EVENTDETAILS *pstruEventDetails_Formal,
30 char *pszAppPath_Formal,
31 char *pszWritePath_Formal,
32 int nVersion_Type,
33 void **fnPointer)
34{
35 return fn_NetSim_RPL_Init_F();
36}
37
38/**
39This function is called by NetworkStack.dll, whenever the event gets triggered
40inside the NetworkStack.dll for the RPL protocol
41*/
42_declspec (dllexport) int fn_NetSim_RPL_Run()
43{
44 switch (pstruEventDetails->nEventType)
45 {
46 case NETWORK_OUT_EVENT:
47 {
48 }
49 break;
50 case NETWORK_IN_EVENT:
51 {
52 rpl_add_to_neighbor_list();
53 if (is_rpl_control_packet(pstruEventDetails->pPacket))
54 {
55 rpl_process_ctrl_msg();
56 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
57 pstruEventDetails->pPacket = NULL;
58 }
59 }
60 break;
61 case TIMER_EVENT:
62 {
63 switch (pstruEventDetails->nSubEventType)
64 {
65 case RPL_TRICKLE_T_TIMEOUT:
66 rpl_trickle_handle_t_timeout();
67 break;
68 case RPL_TRICKLE_I_TIMEOUT:
69 rpl_trickle_handle_i_timeout();
70 break;
71 case RPL_SEND_DAO:
72 rpl_send_dao();
73 break;
74 case RPL_NEW_PREF_PARENT:
75 // Do nothing for now.
76 /* This event is written for customer
77 * who want to perform some action if
78 * new parent is selected.
79 */
80 break;
81 case RPL_DAO_ROUTE_TIMEOUT:
82 rpl_dao_route_timeout();
83 break;
84 case RPL_SEND_DIS:
85 rpl_dis_pdu_send();
86 break;
87 default:
88 fnNetSimError("Unknown subevent %d for RPL.", pstruEventDetails->nSubEventType);
89 break;
90 }
91 }
92 break;
93 default:
94 fnNetSimError("Unknow event %d for RPL protocol", pstruEventDetails->nEventType);
95 break;
96 }
97 return 0;
98}
99
100/**
101This function is called by NetworkStack.dll, once simulation end to free the
102allocated memory for the network.
103*/
104_declspec(dllexport) int fn_NetSim_RPL_Finish()
105{
106 return fn_NetSim_RPL_Finish_F();
107}
108
109/**
110This function is called by NetworkStack.dll, while writing the event trace
111to get the sub event as a string.
112*/
113_declspec (dllexport) char* fn_NetSim_RPL_Trace(NETSIM_ID nSubEvent)
114{
115 return GetStringRPL_Subevent(nSubEvent);
116}
117
118/**
119This function is called by NetworkStack.dll, while configuring the device
120for RPL protocol.
121*/
122_declspec(dllexport) int fn_NetSim_RPL_Configure(void** var)
123{
124 return fn_NetSim_RPL_Configure_F(var);
125}
126
127/**
128This function is called by NetworkStack.dll, to free the RPL protocol data.
129*/
130_declspec(dllexport) int fn_NetSim_RPL_FreePacket(NetSim_PACKET* pstruPacket)
131{
132 return fn_NetSim_RPL_FreePacket_F(pstruPacket);
133}
134
135/**
136This function is called by NetworkStack.dll, to copy the RPL protocol
137details from source packet to destination.
138*/
139_declspec(dllexport) int fn_NetSim_RPL_CopyPacket(NetSim_PACKET* pstruDestPacket, NetSim_PACKET* pstruSrcPacket)
140{
141 return fn_NetSim_RPL_CopyPacket_F(pstruDestPacket, pstruSrcPacket);
142}
143
144/**
145This function write the Metrics
146*/
147_declspec(dllexport) int fn_NetSim_RPL_Metrics(PMETRICSWRITER metricsWriter)
148{
149 return 0;
150}
151
152/**
153This function will return the string to write packet trace heading.
154*/
155_declspec(dllexport) char* fn_NetSim_RPL_ConfigPacketTrace()
156{
157 return "";
158}
159
160/**
161This function will return the string to write packet trace.
162*/
163_declspec(dllexport) char* fn_NetSim_RPL_WritePacketTrace(NetSim_PACKET* pstruPacket, char** ppszTrace)
164{
165 return "";
166}
167
168static PRPL_ROOT rpl_root_info_create()
169{
170 PRPL_ROOT root_info = calloc(1,sizeof* root_info);
171 memcpy(root_info, get_global_root_info(), sizeof* root_info);
172
173 root_info->dodag_id = NULL;
174 root_info->configured_dodag_id = NULL;
175 root_info->dodag_pref = RPL_DEFAULT_DAG_PREF;
176 root_info->grounded = FALSE;
177 root_info->min_hop_rank_inc = 1;
178 return root_info;
179}
180
181void rpl_node_init(NETSIM_ID d)
182{
183 PRPL_NODE r = GET_RPL_NODE(d);
184 r->root_info = rpl_root_info_create();
185
186 //Schedule the DIS message transmission
187 memset(pstruEventDetails, 0, sizeof* pstruEventDetails);
188 pstruEventDetails->dEventTime = r->DISInitDelay;
189 pstruEventDetails->nDeviceId = d;
190 pstruEventDetails->nDeviceType = DEVICE_TYPE(d);
191 pstruEventDetails->nEventType = TIMER_EVENT;
192 pstruEventDetails->nProtocolId = NW_PROTOCOL_RPL;
193 pstruEventDetails->nSubEventType = RPL_SEND_DIS;
194 fnpAddEvent(pstruEventDetails);
195}
196
197void start_as_root(NETSIM_ID d)
198{
199 PRPL_NODE rpl = GET_RPL_NODE(d);
200
201 /* forget about all previous DIO routes */
202 rpl_delete_all_route(d);
203
204 forget_neighbor_messages(rpl);
205
206 if (rpl->joined_dodag != NULL)
207 {
208 rpl_dodag_destroy(rpl->joined_dodag);
209 rpl->joined_dodag = NULL;
210 }
211
212 if (rpl->root_info->dodag_id == NULL)
213 {
214 if (rpl->root_info->configured_dodag_id == NULL)
215 {
216 rpl->root_info->dodag_id = IP_COPY(DEVICE_NWADDRESS(d, 1));
217 }
218 else
219 {
220 rpl->root_info->dodag_id = IP_COPY(rpl->root_info->configured_dodag_id);
221 }
222 }
223 rpl_trickle_reset(d);
224}