Skip to content

Commit 947224c

Browse files
committed
Merge remote-tracking branch 'remotes/origin/develop'
2 parents dffbd1f + 35c4847 commit 947224c

File tree

14 files changed

+418
-40
lines changed

14 files changed

+418
-40
lines changed

PKG-INFO

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Metadata-Version: 1.0
22
Name: SmartMeshSDK
3-
Version: 1.0.6.139
3+
Version: 1.0.8.142
44
Summary: UNKNOWN
55
Home-page: UNKNOWN
66
Author: Linear Technology

app/OapClient/OapClient.py

Lines changed: 111 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
from SmartMeshSDK import sdk_version
3131

3232
#============================ defines =========================================
33-
BCAST_CNT = 2
34-
LOGFILE = "OapClient_log.txt"
33+
34+
NUM_BCAST_TO_SEND = 2
35+
LOGFILE = "OapClient_log.txt"
3536

3637
#============================ globals =========================================
3738

3839
#============================ helpers =========================================
3940

4041
def printExcAndQuit(err):
41-
4242
output = []
4343
output += ["="*30]
4444
output += ["error"]
@@ -80,19 +80,25 @@ def getOperationalMotes():
8080
AppData().get('oap_dispatch'),
8181
)
8282

83+
return len(operationalmotes)
84+
8385
def printOperationalMotes():
86+
8487
output = []
85-
output += ["\nOperational motes:"]
88+
output += ["{0} operational motes:".format(len(AppData().get('operationalmotes')))]
8689
for (i,m) in enumerate(AppData().get('operationalmotes')):
8790
output += ['{0}: {1}'.format(i,FormatUtils.formatMacString(m))]
8891
output = '\n'.join(output)
8992

9093
print output
9194

92-
def selectOperationalMotes(moteNum):
95+
def selectOperationalMote(moteNum):
9396

9497
if moteNum>len(AppData().get('operationalmotes')):
95-
print 'Cannot select mote {0}, out of range'.format(moteNum)
98+
print 'Cannot select mote {0}, there are only {1} motes'.format(
99+
moteNum,
100+
len(AppData().get('operationalmotes')),
101+
)
96102
return
97103

98104
AppData().set('currentmote',moteNum)
@@ -103,6 +109,7 @@ def selectOperationalMotes(moteNum):
103109
)
104110

105111
def togglePrintNotifs():
112+
106113
if AppData().get('printNotifs')==False:
107114
AppData().set('printNotifs',True)
108115
print "notifications are ON."
@@ -111,6 +118,7 @@ def togglePrintNotifs():
111118
print "notifications are OFF."
112119

113120
def toggleLogNotifs():
121+
114122
if AppData().get('logNotifs')==False:
115123
AppData().set('logNotifs',True)
116124
print "logging to logfile is ON."
@@ -143,6 +151,9 @@ def set(self,k,v):
143151
def get(self,k):
144152
with self.dataLock:
145153
return self.data[k]
154+
def delete(self,k):
155+
with self.dataLock:
156+
del self.data[k]
146157

147158
class Manager(object):
148159

@@ -165,9 +176,10 @@ def __init__(self):
165176

166177
# list operational motes
167178
AppData().set('oap_clients',{})
168-
getOperationalMotes()
169-
printOperationalMotes()
170-
selectOperationalMotes(0)
179+
numMotes = getOperationalMotes()
180+
if numMotes:
181+
printOperationalMotes()
182+
selectOperationalMote(0)
171183
AppData().set('printNotifs',False)
172184
togglePrintNotifs()
173185

@@ -188,6 +200,7 @@ def disconnect(self):
188200
#======================== private =========================================
189201

190202
def _cb_NOTIFDATA(self,notifName,notifParams):
203+
191204
AppData().get('oap_dispatch').dispatch_pkt(notifName, notifParams)
192205
if AppData().get('logNotifs'):
193206
if notifParams.data[0] == 0:
@@ -196,30 +209,33 @@ def _cb_NOTIFDATA(self,notifName,notifParams):
196209
def _handle_oap_notif(self,mac,notif):
197210

198211
receive_time = float(time.time()) - self.mapmgrtime.pctomgr_time_offset
199-
output = " Received-Time = {0} OAP notification from {1}: {2}".format(
200-
receive_time,
212+
output = "OAP notification from {0} (receive time {1}):\n{2}".format(
201213
FormatUtils.formatMacString(mac),
214+
receive_time,
202215
notif
203216
)
204-
217+
205218
if AppData().get('printNotifs'):
206219
print output
207220
if AppData().get('logNotifs'):
208221
self.log_file.write('{0}\n'.format(output))
209222

210223
class MgrTime(threading.Thread):
211-
# This class sends getTime API command to map network time to UTC time. The offset is then
212-
# used to calculate the pkt arrival time for the same time base as the mote
224+
'''
225+
This class periodically sends a getTime() API command to the manager to map
226+
network time to UTC time. The offset is used to calculate the pkt arrival
227+
time for the same time base as the mote.
228+
'''
213229

214230
def __init__(self, pctomgr_time_offset, sleepperiod):
215231
# init the parent
216232
threading.Thread.__init__(self)
217233
self.event = threading.Event()
218-
self.sleepperiod = sleepperiod
234+
self.sleepperiod = sleepperiod
219235
self.daemon = True
220236
self.pctomgr_time_offset = pctomgr_time_offset
221237
# give this thread a name
222-
self.name = 'MgrTimePin'
238+
self.name = 'MgrTime'
223239

224240
def run(self):
225241
while True:
@@ -229,7 +245,7 @@ def run(self):
229245
mgr_time = mgr_timepinres.utcSecs + mgr_timepinres.utcUsecs / 1000000.0
230246
mgr_asn = int(''.join(["%02x"%i for i in mgr_timepinres.asn]),16)
231247
self.pctomgr_time_offset = pc_time - mgr_time
232-
248+
233249
self.event.wait(self.sleepperiod)
234250

235251
#============================ CLI handlers ====================================
@@ -239,6 +255,14 @@ def connect_clicb(params):
239255
# filter params
240256
port = params[0]
241257

258+
try:
259+
AppData().get('connector')
260+
except KeyError:
261+
pass
262+
else:
263+
print 'already connected.'
264+
return
265+
242266
# create a connector
243267
AppData().set('connector',IpMgrConnectorSerial.IpMgrConnectorSerial())
244268

@@ -248,7 +272,12 @@ def connect_clicb(params):
248272
'port': port,
249273
})
250274
except ConnectionError as err:
251-
printExcAndQuit(err)
275+
print 'Could not connect to {0}: {1}'.format(
276+
port,
277+
err,
278+
)
279+
AppData().delete('connector')
280+
return
252281

253282
# start threads
254283
AppData().set('manager',Manager())
@@ -258,14 +287,17 @@ def list_clicb(params):
258287
printOperationalMotes()
259288

260289
def select_clicb(params):
261-
selectOperationalMotes(int(params[0]))
290+
selectOperationalMote(int(params[0]))
262291

263292
def notifs_clicb(params):
264293
togglePrintNotifs()
265294

266295
def writelogfile_clicb(params):
267296
toggleLogNotifs()
268297

298+
def _resppoipoi(mac, resp, trans):
299+
print (mac, resp, trans)
300+
269301
def led_clicb(params):
270302

271303
# filter params
@@ -276,6 +308,13 @@ def led_clicb(params):
276308
isBcast = True
277309
ledState = params[1]
278310

311+
if moteId>len(AppData().get('operationalmotes')):
312+
print 'moteId {0} impossible, there are only {1} motes'.format(
313+
moteId,
314+
len(AppData().get('operationalmotes')),
315+
)
316+
return
317+
279318
if ledState=="0":
280319
ledVal = 0
281320
else:
@@ -287,6 +326,7 @@ def led_clicb(params):
287326
cmd_type = OAPMessage.CmdType.PUT,
288327
addr = [3,2],
289328
data_tags = [OAPMessage.TLVByte(t=0,v=ledVal)],
329+
cb = _respPoipoi,
290330
)
291331
else:
292332
# build OAP message
@@ -300,8 +340,8 @@ def led_clicb(params):
300340
)
301341
oap_msg = [ord(b) for b in oap_msg]
302342

303-
# send OAP message broadcast BCAST_CNT times
304-
for i in range (BCAST_CNT):
343+
# send OAP message broadcast NUM_BCAST_TO_SEND times
344+
for i in range (NUM_BCAST_TO_SEND):
305345
AppData().get('connector').dn_sendData(
306346
macAddress = [0xff]*8,
307347
priority = 0,
@@ -322,6 +362,13 @@ def temp_clicb(params):
322362
tempOn = int(params[1])
323363
pktPeriod = int(params[2])
324364

365+
if moteId>len(AppData().get('operationalmotes')):
366+
print 'moteId {0} impossible, there are only {1} motes'.format(
367+
moteId,
368+
len(AppData().get('operationalmotes')),
369+
)
370+
return
371+
325372
# send OAP command ... single or all broadcast
326373
if not isBcast:
327374
AppData().get('oap_clients')[AppData().get('operationalmotes')[moteId]].send(
@@ -347,8 +394,8 @@ def temp_clicb(params):
347394
)
348395
oap_msg = [ord(b) for b in oap_msg]
349396

350-
# send OAP message broadcast BCAST_CNT times
351-
for i in range (BCAST_CNT):
397+
# send OAP message broadcast NUM_BCAST_TO_SEND times
398+
for i in range (NUM_BCAST_TO_SEND):
352399
AppData().get('connector').dn_sendData(
353400
macAddress = [0xff]*8,
354401
priority = 0,
@@ -371,6 +418,13 @@ def pkgen_clicb(params):
371418
pktSize = int(params[3])
372419
pktstartPID = 0
373420

421+
if moteId>len(AppData().get('operationalmotes')):
422+
print 'moteId {0} impossible, there are only {1} motes'.format(
423+
moteId,
424+
len(AppData().get('operationalmotes')),
425+
)
426+
return
427+
374428
# send OAP command ... single mote, or all unicast, or all broadcast
375429
if isBcast == False:
376430
AppData().get('oap_clients')[AppData().get('operationalmotes')[moteId]].send(
@@ -418,8 +472,8 @@ def pkgen_clicb(params):
418472
)
419473
oap_msg = [ord(b) for b in oap_msg]
420474

421-
# send OAP message broadcast BCAST_CNT times
422-
for i in range (BCAST_CNT):
475+
# send OAP message broadcast NUM_BCAST_TO_SEND times
476+
for i in range (NUM_BCAST_TO_SEND):
423477
AppData().get('connector').dn_sendData(
424478
macAddress = [0xff]*8,
425479
priority = 0,
@@ -431,6 +485,30 @@ def pkgen_clicb(params):
431485
else:
432486
print (' unknown paramater ... {0}'.format(params[0]))
433487

488+
def analog_clicb(params):
489+
490+
# filter params
491+
moteId = int(params[0])
492+
channel = int(params[1])
493+
enable = int(params[2])
494+
rate = int(params[3])
495+
496+
if moteId>len(AppData().get('operationalmotes')):
497+
print 'moteId {0} impossible, there are only {1} motes'.format(
498+
moteId,
499+
len(AppData().get('operationalmotes')),
500+
)
501+
return
502+
503+
AppData().get('oap_clients')[AppData().get('operationalmotes')[moteId]].send(
504+
cmd_type = OAPMessage.CmdType.PUT,
505+
addr = [4,channel],
506+
data_tags = [
507+
OAPMessage.TLVByte(t=0,v=enable), # enable
508+
OAPMessage.TLVLong(t=1,v=rate), # rate
509+
],
510+
)
511+
434512
def quit_clicb():
435513

436514
if AppData().get('connector'):
@@ -511,6 +589,14 @@ def main():
511589
callback = pkgen_clicb,
512590
dontCheckParamsLength = False,
513591
)
592+
cli.registerCommand(
593+
name = 'analog',
594+
alias = 'a',
595+
description = 'set the analog application on the mote',
596+
params = ['moteId','channel','enable','rate'],
597+
callback = analog_clicb,
598+
dontCheckParamsLength = False,
599+
)
514600

515601
# print SmartMesh SDK version
516602
print 'SmartMesh SDK {0}'.format('.'.join([str(i) for i in sdk_version.VERSION]))

0 commit comments

Comments
 (0)