1 /******************************************************************************
3 @file nwkinfo.js
5 @brief network information specific functions
7 Group: WCS LPC
8 $Target Device: DEVICES $
10 ******************************************************************************
11 $License: BSD3 2016 $
12 ******************************************************************************
13 $Release Name: PACKAGE NAME $
14 $Release Date: PACKAGE RELEASE DATE $
15 *****************************************************************************/
17 var util = require('util');
19 const BEACON_ENABLED = 1;
20 const NON_BEACON = 2;
21 const FREQUENCY_HOPPING = 3;
23 /*!
24 Coordinator State Values
25 */
26 /*! Powered up, not started and waiting for user to start */
27 const Cllc_states_initWaiting = 1;
28 /*! Starting coordinator, scanning and selecting the best parameters */
29 const Cllc_states_startingCoordinator = 2;
30 /*!
31 Powered up, found network information, and restoring device in network
32 */
33 const Cllc_states_initRestoringCoordinator = 3;
34 /*! Device is operating as coordinator */
35 const Cllc_states_started = 4;
36 /*! Device is restored as coordinator in the network */
37 const Cllc_states_restored = 5;
38 /*! Joining allowed state has changed to allowed */
39 const Cllc_states_joiningAllowed = 6;
40 /*! Joining allowed state has changed to not allowed */
41 const Cllc_states_joiningNotAllowed = 7;
43 /*!
44 * @brief Constructor for network Information object
45 *
46 * @param networkInfo - object with network information
47 *
48 * @retun network information object
49 */
50 function NwkInfo(networkInfo) {
51 var nwkInfo = this;
52 return createNwkInfoObj(networkInfo);
53 function createNwkInfoObj(networkInfo) {
55 /* set network channel information */
56 nwkInfo.channel = networkInfo.channels;
57 /* set the PAN Coordinator device informatoin */
58 nwkInfo.panCoord = {
59 panId: networkInfo.pan_id,
60 shortAddress: networkInfo.short_addr,
61 extAddress: networkInfo.ext_addr
62 };
63 /* set the security information */
64 nwkInfo.securityEnabled = networkInfo.security_enabled;
65 /* set network mode */
66 if (networkInfo.mode.toLowerCase() === "beacon") {
67 nwkInfo.networkMode = "Beacon Enabled"
68 }
69 else if (networkInfo.mode.toLowerCase() === "non beacon") {
70 nwkInfo.networkMode = "Non Beacon";
71 }
72 else if (networkInfo.mode.toLowerCase() === "frequency hopping") {
73 nwkInfo.networkMode = "Freq Hopping";
74 }
75 else {
76 nwkInfo.networkMode = "Unknown Mode"
77 }
78 /* set network state */
80 if(networkInfo.state === "waiting" || networkInfo.state === "starting" || networkInfo.state === "restoring" || networkInfo.state === "started" || networkInfo.state === "open" || networkInfo.state === "close")
81 {
82 nwkInfo.state = networkInfo.state;
83 }
84 else{
85 /* Should not get here */
86 console.log("ERROR: rcvd illegal coord state(NwkInfo)");
87 nwkInfo.state = "unknown";
88 }
90 return nwkInfo;
91 }
92 }
94 /* update network information */
95 NwkInfo.prototype.updateNwkInfo = function (networkInfo) {
96 var self = this;
97 self.channel = networkInfo.channels;
98 /* set the PAN Coordinator device informatoin */
99 self.panCoord = {
100 panId: networkInfo.pan_id,
101 shortAddress: networkInfo.short_addr,
102 extAddress: networkInfo.ext_addr
103 };
104 self.securityEnabled = networkInfo.security_enabled;
105 /* set network mode */
106 if (networkInfo.mode.toLowerCase() === "beacon") {
107 self.networkMode = "Beacon Enabled"
108 }
109 else if (networkInfo.mode.toLowerCase() === "non beacon") {
110 self.networkMode = "Non Beacon";
111 }
112 else if (networkInfo.mode.toLowerCase() === "frequency hopping") {
113 self.networkMode = "Freq Hopping";
114 }
115 else {
116 self.networkMode = "Unknown Mode"
117 }
118 /* set network state */
119 if(networkInfo.state === "waiting" || networkInfo.state === "starting" || networkInfo.state === "restoring" || networkInfo.state === "started" || networkInfo.state === "open" || networkInfo.state === "close")
120 {
121 self.state = networkInfo.state;
122 }
123 else{
124 /* Should not get here */
125 console.log("ERROR: rcvd illegal coord state(NwkInfo)");
126 self.state = "unknown";
127 }
128 }
130 /* update network state */
131 NwkInfo.prototype.updateNwkState = function (nState) {
132 var self = this;
133 /* set network state */
134 switch (nState.state) {
135 case Cllc_states_initWaiting:
136 /* Application is waiting for user input
137 to start the application */
138 self.state = "waiting";
139 break;
140 case Cllc_states_startingCoordinator:
141 /* Application is working to start the network */
142 self.state = "starting";
143 break;
144 case Cllc_states_initRestoringCoordinator:
145 /* Application is working to restore the network
146 from previously stored informatoin */
147 self.state = "restoring";
148 break;
149 case Cllc_states_started:
150 case Cllc_states_restored:
151 /* Network is started */
152 self.state = "started";
153 break;
154 case Cllc_states_joiningAllowed:
155 /* Network is open for new devices to join */
156 self.state = "open";
157 break;
158 case Cllc_states_joiningNotAllowed:
159 /* Network is closed for new devices to join */
160 self.state = "close";
161 break;
162 default:
163 /* Should not get here */
164 console.log("ERROR: rcvd illegal coord state(nState)");
165 self.state = "unknown";
166 break;
167 }
168 }
170 module.exports = NwkInfo;