1 | %============================================================================== |
---|
2 | % Function wl_initNodes() |
---|
3 | % |
---|
4 | % Input: |
---|
5 | % - Single number |
---|
6 | % - Array of struct (derived from wl_networkSetup()) |
---|
7 | % - Array of wl_nodes; Either optional array of numbers, or array of struct |
---|
8 | % |
---|
9 | % Output: |
---|
10 | % - Array of wl_nodes |
---|
11 | % |
---|
12 | %============================================================================== |
---|
13 | |
---|
14 | function nodes = wl_initNodes(varargin) |
---|
15 | |
---|
16 | if nargin == 0 |
---|
17 | error('Not enough arguments are provided'); |
---|
18 | elseif nargin == 1 |
---|
19 | switch( class(varargin{1}) ) |
---|
20 | case 'wl_node' |
---|
21 | nodes = varargin{1}; |
---|
22 | numNodes = length(nodes); |
---|
23 | nodeIDs = 0:(numNodes-1); % Node IDs default: 0 to (number of nodes in the array) |
---|
24 | case 'struct' |
---|
25 | numNodes = length(varargin{1}); |
---|
26 | nodes(numNodes) = wl_node; |
---|
27 | nodeIDs = varargin{1}; % Node IDs are specified in the input structure |
---|
28 | case 'double' |
---|
29 | numNodes = varargin{1}; |
---|
30 | nodes(numNodes) = wl_node; |
---|
31 | nodeIDs = 0:(numNodes-1); % Node IDs default: 0 to (number of nodes specified) |
---|
32 | otherwise |
---|
33 | error('Unknown argument. Argument is of type "%s", need "wl_node", "struct", or "double"', class(varargin{1})); |
---|
34 | end |
---|
35 | elseif nargin == 2 |
---|
36 | |
---|
37 | switch( class(varargin{1}) ) |
---|
38 | case 'wl_node' |
---|
39 | nodes = varargin{1}; |
---|
40 | numNodes = length(nodes); |
---|
41 | case 'double' |
---|
42 | numNodes = varargin{1}; |
---|
43 | nodes(numNodes) = wl_node; |
---|
44 | otherwise |
---|
45 | error('Unknown argument. Argument is of type "%s", need "wl_node" or "double"', class(varargin{1})); |
---|
46 | end |
---|
47 | |
---|
48 | switch( class(varargin{2}) ) |
---|
49 | case 'struct' |
---|
50 | nodeIDs = varargin{2}; % Node IDs are specified in the input structure |
---|
51 | case 'double' |
---|
52 | nodeIDs = varargin{2}; % Node IDs default: 0 to (number of nodes specified) |
---|
53 | otherwise |
---|
54 | error('Unknown argument. Argument is of type "%s", need "struct", or "double"', class(varargin{2})); |
---|
55 | end |
---|
56 | |
---|
57 | if(length(nodeIDs)~=numNodes) |
---|
58 | error('Number of nodes does not match ID vector'); |
---|
59 | end |
---|
60 | else |
---|
61 | error('Too many arguments provided'); |
---|
62 | end |
---|
63 | |
---|
64 | |
---|
65 | gen_error = []; |
---|
66 | gen_error_index = 0; |
---|
67 | |
---|
68 | for n = numNodes:-1:1 |
---|
69 | currNode = nodes(n); |
---|
70 | |
---|
71 | % If we are doing a network setup of the node based on the input structure then create the broadcast packet and send it |
---|
72 | % Note: |
---|
73 | % - Node must be configured with dip switches 0xF |
---|
74 | % - Node will initialize itself to IP address of 10.0.0.0 and wait for a broadcast message to configure itself |
---|
75 | % - Node will check against the serial number (only last 5 digits; "W3-a-" is not stored in EEPROM) |
---|
76 | if( strcmp( class( nodeIDs ), 'struct') ) |
---|
77 | |
---|
78 | configFile = which('wl_config.ini'); |
---|
79 | |
---|
80 | if(isempty(configFile)) |
---|
81 | error('cannot find wl_config.ini. please run wl_setup.m'); |
---|
82 | end |
---|
83 | |
---|
84 | readKeys = {'network','','transport',''}; |
---|
85 | transport = inifile(configFile,'read',readKeys); |
---|
86 | transport = transport{1}; |
---|
87 | |
---|
88 | switch(transport) |
---|
89 | case 'java' |
---|
90 | transport_bcast = wl_transport_eth_udp_java_bcast; |
---|
91 | transport_unicast = wl_transport_eth_udp_java; |
---|
92 | case 'wl_mex_udp' |
---|
93 | transport_bcast = wl_transport_eth_udp_mex_bcast; |
---|
94 | transport_unicast = wl_transport_eth_udp_mex; |
---|
95 | end |
---|
96 | |
---|
97 | % Open a broadcast transport to send the configure command to the node |
---|
98 | transport_bcast.open(); |
---|
99 | tempNode = wl_node; |
---|
100 | |
---|
101 | % Send serial number, node ID, and IP address |
---|
102 | myCmd = wl_cmd(tempNode.calcCmd(tempNode.GRP, tempNode.CMD_NODE_CONFIG_SETUP)); |
---|
103 | myCmd.addArgs(uint32(sscanf(nodeIDs(n).serialNumber, 'W3-a-%d'))); |
---|
104 | myCmd.addArgs(nodeIDs(n).IDUint32); |
---|
105 | myCmd.addArgs(nodeIDs(n).ipAddressUint32); |
---|
106 | transport_bcast.send('message',myCmd.serialize()); |
---|
107 | end |
---|
108 | |
---|
109 | % Error check to make sure that no node in the network has the same |
---|
110 | % ID as this host. |
---|
111 | configFile = which('wl_config.ini'); |
---|
112 | |
---|
113 | if(isempty(configFile)) |
---|
114 | error('cannot find wl_config.ini. please run wl_setup.m'); |
---|
115 | end |
---|
116 | |
---|
117 | readKeys = {'network','','host_ID',''}; |
---|
118 | hostID = inifile(configFile,'read',readKeys); |
---|
119 | hostID = hostID{1}; |
---|
120 | hostID = sscanf(hostID,'%d'); |
---|
121 | |
---|
122 | % Error check to make sure that no node in the network has the same ID as this host. |
---|
123 | % |
---|
124 | nodeID = 0; |
---|
125 | switch(class( nodeIDs(n) )) |
---|
126 | case 'double' |
---|
127 | nodeID = nodeIDs(n); |
---|
128 | if( hostID == nodeID ) |
---|
129 | error('Host ID is set to %d and must be unique. No node in the network can share this ID',hostID); |
---|
130 | end |
---|
131 | |
---|
132 | case 'struct' |
---|
133 | nodeID = nodeIDs(n).IDUint32; |
---|
134 | if( hostID == nodeID ) |
---|
135 | error('Host ID is set to %d and must be unique. No node in the network can share this ID',hostID); |
---|
136 | end |
---|
137 | end |
---|
138 | |
---|
139 | % Now that the node has a valid IP address, we can apply the configuration |
---|
140 | % |
---|
141 | try |
---|
142 | currNode.applyConfiguration(nodeIDs(n)); |
---|
143 | catch ME |
---|
144 | fprintf('\n'); |
---|
145 | fprintf('Error in node %d with ID = %d: ', n, nodeID ); |
---|
146 | ME |
---|
147 | fprintf('Error message follows:\n%s\n', ME.message); |
---|
148 | gen_error_index = gen_error_index + 1; |
---|
149 | gen_error( gen_error_index ) = nodeID; |
---|
150 | % error('Node with ID = %d is not responding. Please ensure that the node has been configured with the WLAN Exp bitstream.',nodeID) |
---|
151 | fprintf('\n'); |
---|
152 | end |
---|
153 | end |
---|
154 | |
---|
155 | % Output an error if there was one |
---|
156 | if( gen_error_index ~= 0 ) |
---|
157 | |
---|
158 | error_nodes = sprintf('['); |
---|
159 | for n = 1:gen_error_index |
---|
160 | error_nodes = sprintf('%s %d', error_nodes, gen_error(n)); |
---|
161 | end |
---|
162 | error_nodes = sprintf('%s ]', error_nodes); |
---|
163 | |
---|
164 | error('The following nodes with IDs = %s are not responding. Please ensure that the nodes have been configured with the WARPLab bitstream.', error_nodes) |
---|
165 | end |
---|
166 | |
---|
167 | %Send a test broadcast trigger command |
---|
168 | if(strcmp(class(nodes(1).trigger_manager),'wl_trigger_manager_proc')) |
---|
169 | |
---|
170 | configFile = which('wl_config.ini'); |
---|
171 | |
---|
172 | if(isempty(configFile)) |
---|
173 | error('cannot find wl_config.ini. please run wl_setup.m'); |
---|
174 | end |
---|
175 | |
---|
176 | readKeys = {'network','','transport',''}; |
---|
177 | transport = inifile(configFile,'read',readKeys); |
---|
178 | transport = transport{1}; |
---|
179 | |
---|
180 | switch(transport) |
---|
181 | case 'java' |
---|
182 | transport_bcast = wl_transport_eth_udp_java_bcast; |
---|
183 | case 'wl_mex_udp' |
---|
184 | transport_bcast = wl_transport_eth_udp_mex_bcast; |
---|
185 | end |
---|
186 | |
---|
187 | transport_bcast.open(); |
---|
188 | tempNode = wl_node; |
---|
189 | myCmd = wl_cmd(tempNode.calcCmd(wl_trigger_manager_proc.GRP,wl_trigger_manager_proc.CMD_TEST_TRIGGER)); |
---|
190 | bcastTestFlag = uint32(round(2^32 * rand)); |
---|
191 | myCmd.addArgs(bcastTestFlag); %Signals to the board that this is writing the received trigger test variable |
---|
192 | transport_bcast.send('message',myCmd.serialize()); |
---|
193 | |
---|
194 | resp = wl_triggerManagerCmd(nodes,'test_trigger'); |
---|
195 | |
---|
196 | I = find(resp~=bcastTestFlag); |
---|
197 | |
---|
198 | for nodeIndex = I |
---|
199 | warning('Node %d with Serial # %d failed trigger test',nodes(nodeIndex).ID,nodes(nodeIndex).serialNumber) |
---|
200 | end |
---|
201 | |
---|
202 | if(any(resp~=bcastTestFlag)) |
---|
203 | error('Broadcast triggers are not working. Please verify your ARP table has an entry for the broadcast address on your WARPLab subnet') |
---|
204 | end |
---|
205 | end |
---|
206 | |
---|
207 | nodes.wl_nodeCmd('initialize'); |
---|
208 | end |
---|