1 /******************************************************************************
3 @file nwkinfo.js
5 @brief network information specific functions
7 Group: WCS LPC
8 $Target Devices: Linux: AM335x, Embedded Devices: CC1310, CC1350$
10 ******************************************************************************
11 $License: BSD3 2016 $
13 Copyright (c) 2015, Texas Instruments Incorporated
14 All rights reserved.
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
20 * Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
23 * Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
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.
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
33 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
37 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
38 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
41 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 ******************************************************************************
43 $Release Name: TI-15.4Stack Linux x64 SDK$
44 $Release Date: July 14, 2016 (2.00.00.30)$
45 *****************************************************************************/
47 var util = require('util');
48 var protobuf = require("protocol-buffers");
49 var fs = require("fs");
51 /* set-up to decode/encode proto messages */
52 var ntimac_pb = protobuf(fs.readFileSync('appClient/protofiles/appsrv.proto'));
54 /*!
55 * @brief Constructor for network Information object
56 *
57 * @param networkInfo - object with network information
58 *
59 * @return network information object
60 */
61 function NwkInfo(networkInfo) {
62 var nwkInfo = this;
63 return createNwkInfoObj(networkInfo);
64 function createNwkInfoObj(networkInfo) {
65 /* set network mode */
67 nwkInfo.fh = networkInfo.nwkinfo.nwkInfo.fh;
68 /* set network channel information */
69 nwkInfo.channel = networkInfo.nwkinfo.nwkInfo.channel;
70 /* set the PAN Coordinator device information */
71 nwkInfo.panCoord = {
72 panId: networkInfo.nwkinfo.nwkInfo.devInfo.panID,
73 shortAddress: networkInfo.nwkinfo.nwkInfo.devInfo.shortAddress,
74 extAddress: networkInfo.nwkinfo.nwkInfo.devInfo.extAddress
75 };
76 /* set the security information */
77 nwkInfo.securityEnabled = networkInfo.nwkinfo.securityEnabled;
78 /* set network mode */
79 if (networkInfo.nwkinfo.networkMode == ntimac_pb.nwkMode.BEACON_ENABLED) {
80 nwkInfo.networkMode = "Beacon Enabled"
81 }
82 else if (networkInfo.nwkinfo.networkMode == ntimac_pb.nwkMode.NON_BEACON) {
83 nwkInfo.networkMode = "Non Beacon";
84 }
85 else if (networkInfo.nwkinfo.networkMode == ntimac_pb.nwkMode.FREQUENCY_HOPPING) {
86 nwkInfo.networkMode = "Freq Hopping";
87 }
88 else {
89 nwkInfo.networkMode = "Unknown Mode"
90 }
91 /* set network state */
92 switch (networkInfo.nwkinfo.state) {
93 case ntimac_pb.Cllc_states.Cllc_states_initWaiting:
94 /* Application is waiting for user input
95 to start the application */
96 nwkInfo.state = "waiting";
97 break;
98 case ntimac_pb.Cllc_states.Cllc_states_startingCoordinator:
99 /* Application is working to start the network */
100 nwkInfo.state = "starting";
101 break;
102 case ntimac_pb.Cllc_states.Cllc_states_initRestoringCoordinator:
103 /* Application is working to restore the network
104 from previously stored informatoin */
105 nwkInfo.state = "restoring";
106 break;
107 case ntimac_pb.Cllc_states.Cllc_states_started:
108 case ntimac_pb.Cllc_states.Cllc_states_restored:
109 /* Network is started */
110 nwkInfo.state = "started";
111 break;
112 case ntimac_pb.Cllc_states.Cllc_states_joiningAllowed:
113 /* Network is open for new devices to join */
114 nwkInfo.state = "open";
115 break;
116 case ntimac_pb.Cllc_states.Cllc_states_joiningNotAllowed:
117 /* Network is closed for new devices to join */
118 nwkInfo.state = "close";
119 break;
120 default:
121 /* Should not get here */
122 console.log("ERROR: rcvd illegal coord state(NwkInfo)");
123 nwkInfo.state = "unknown";
124 break;
125 }
126 return nwkInfo;
127 }
128 }
130 /* update network information */
131 NwkInfo.prototype.updateNwkInfo = function (networkInfo) {
132 var self = this;
133 self.fh = networkInfo.nwkinfo.nwkInfo.fh;
134 /* set network channel information */
135 self.channel = networkInfo.nwkinfo.nwkInfo.channel;
136 /* set the PAN Coordinator device information */
137 self.panCoord = {
138 panId: networkInfo.nwkinfo.nwkInfo.devInfo.panID,
139 shortAddress: networkInfo.nwkinfo.nwkInfo.devInfo.shortAddress,
140 extAddress: networkInfo.nwkinfo.nwkInfo.devInfo.extAddress
141 };
142 self.securityEnabled = networkInfo.nwkinfo.securityEnabled;
143 self.networkMode = networkInfo.nwkinfo.networkMode;
144 /* set network state */
145 switch (networkInfo.nwkinfo.state) {
146 case ntimac_pb.Cllc_states.Cllc_states_initWaiting:
147 /* Application is waiting for user input
148 to start the application */
149 self.state = "waiting";
150 break;
151 case ntimac_pb.Cllc_states.Cllc_states_startingCoordinator:
152 /* Application is working to start the network */
153 self.state = "starting";
154 break;
155 case ntimac_pb.Cllc_states.Cllc_states_initRestoringCoordinator:
156 /* Application is working to restore the network
157 from previously stored informatoin */
158 self.state = "restoring";
159 break;
160 case ntimac_pb.Cllc_states.Cllc_states_started:
161 case ntimac_pb.Cllc_states.Cllc_states_restored:
162 /* Network is started */
163 self.state = "started";
164 break;
165 case ntimac_pb.Cllc_states.Cllc_states_joiningAllowed:
166 /* Network is open for new devices to join */
167 self.state = "open";
168 break;
169 case ntimac_pb.Cllc_states.Cllc_states_joiningNotAllowed:
170 /* Network is closed for new devices to join */
171 self.state = "close";
172 break;
173 default:
174 /* Should not get here */
175 console.log("ERROR: rcvd illegal coord state(networkInfo)");
176 self.state = "unknown";
177 break;
178 }
179 }
181 /* update network state */
182 NwkInfo.prototype.updateNwkState = function (nState) {
183 var self = this;
184 /* set network state */
185 switch (nState.state) {
186 case ntimac_pb.Cllc_states.Cllc_states_initWaiting:
187 /* Application is waiting for user input
188 to start the application */
189 self.state = "waiting";
190 break;
191 case ntimac_pb.Cllc_states.Cllc_states_startingCoordinator:
192 /* Application is working to start the network */
193 self.state = "starting";
194 break;
195 case ntimac_pb.Cllc_states.Cllc_states_initRestoringCoordinator:
196 /* Application is working to restore the network
197 from previously stored informatoin */
198 self.state = "restoring";
199 break;
200 case ntimac_pb.Cllc_states.Cllc_states_started:
201 case ntimac_pb.Cllc_states.Cllc_states_restored:
202 /* Network is started */
203 self.state = "started";
204 break;
205 case ntimac_pb.Cllc_states.Cllc_states_joiningAllowed:
206 /* Network is open for new devices to join */
207 self.state = "open";
208 break;
209 case ntimac_pb.Cllc_states.Cllc_states_joiningNotAllowed:
210 /* Network is closed for new devices to join */
211 self.state = "close";
212 break;
213 default:
214 /* Should not get here */
215 console.log("ERROR: rcvd illegal coord state(nState)");
216 self.state = "unknown";
217 break;
218 }
219 }
221 module.exports = NwkInfo;