1 local getopt
= require('getopt')
2 local lib14a
= require('read14a')
3 local utils
= require('utils')
4 local ansicolors
= require('ansicolors')
6 copyright
= 'Copyright (c) 2017 IceSQL AB. All rights reserved.'
7 author
= "Christian Herrmann"
10 This script writes a empty template for 3D printing system onto a empty NTAG213 or MAGIC NTAG21*
12 Thanks to @jack for his invaluable input on some of the configuration.
15 -- This will generate GOLD, PLA, TH, EU, 200m, tagbased uid.
16 script run hf_ntag-3d -c 46 -m 50 -p 5448 -s 4555 -l 200
18 -- This will generate GOLD, PLA, TH, EU, 200m, userbased uid.
19 script run hf_ntag-3d -u 11223344556677 -c 46 -m 50 -p 5448 -s 4555 -l 200
21 -- This will generate GOLD, PLA, TH, EU, 200m, userbased uid. and configure a MAGIC NTAG.
22 script run hf_ntag-3d -u 11223344556677 -c 46 -m 50 -p 5448 -s 4555 -l 200 -1
25 script run hf_ntag-3d [-h] [-t] [-u <uid>] [-c <color>] [-m <material>] [-p <region>] [-s <region>] [-l <length>]
31 -c <COLOR> : color of filament
32 -m <MATERIAL> : material of filament
33 -p <REGION> : Manufacturer region
34 -s <REGION> : Sales region
35 -l <LENGTH> : Spool length. Use only 100,200,300. 300 has problems on OSX
39 local TIMEOUT
= 10000 -- 10 seconds
40 local bxor
= bit32
.bxor
41 local band
= bit32
.band
42 local rshift
= bit32
.rshift
60 local _manufacturers
= {
73 {'20', 'Other material'},
75 {'46', 'Flexible TPE Tree'},
83 {'56', 'Water Soluble PVA'},
90 {'34', 'Bottle Green'},
91 {'35', 'Neon Magenta'},
94 {'38', 'Pearl White'},
98 {'43', 'Neon Tangerine'},
103 {'48', 'Neon Green'},
104 {'49', 'Snow White'},
105 {'4A', 'Neon Yellow'},
108 {'4D', 'Grape Purple'},
110 {'4F', 'Clear Yellow'},
111 {'50', 'Clear Green'},
112 {'51', 'Clear Tangerine'},
114 {'53', 'Cyber Yellow'},
116 {'55', 'Clear Blue'},
117 {'56', 'Clear Purple'},
119 {'58', 'Clear Magenta'},
125 local function find( arr
, name
)
126 if not name
then return nil end
128 for k
, v
in pairs(arr
) do
129 if ( v
[2]:lower() == name
or v
[1]:lower() == name
) then
137 local function list( arr
, desc
)
138 print ('Value\t'..desc
)
139 print (string.rep('=', 20))
140 for k
, v
in pairs(arr
) do
141 print(("%s\t%s"):format(v
[1],v
[2]))
145 -- A debug printout-function
146 local function dbg(args
)
147 if not DEBUG
then return end
148 if type(args
) == 'table' then
159 -- This is only meant to be used when errors occur
160 local function oops(err
)
162 core
.clearCommandBuffer()
167 local function help()
172 print(ansicolors
.cyan
..'Usage'..ansicolors
.reset
)
174 print(ansicolors
.cyan
..'Arguments'..ansicolors
.reset
)
176 print(ansicolors
.cyan
..'Example usage'..ansicolors
.reset
)
181 local function ExitMsg(msg
)
182 print( string.rep('--',20) )
183 print( string.rep('--',20) )
189 local function write_tag(uid
, t
)
191 print('Writing to tag')
192 core
.console('hw dbg -0')
196 local pwd
, pack
= core
.keygen_algo_d(uid
)
199 cmd
= ('hf mfu wrbl --blk %02d -d %s -k %08X'):format(i
, t
[i
], pwd
)
204 core
.console(('hf mfu wrbl --blk 42 -d %s -k %08X'):format(t
[42], pwd
))
206 core
.console(('hf mfu wrbl --blk 41 -d %s -k %08X'):format(t
[41], pwd
))
208 core
.console(('hf mfu wrbl --blk 40 -d %s -k %08X'):format(t
[40], pwd
))
210 core
.console('hw dbg -1')
215 -- configures a magic NTAG for NTAG213, with UID and PWD,PACK.
216 local function configure_magic_ntag(uid
)
218 print('Configuring MAGIC NTAG')
219 -- Save the global args, those are *our* arguments
222 local pwd
, pack
= core
.keygen_algo_d(uid
)
224 -- Set the arguments for hf_mfu_magicwrite script v1.0.8
225 -- -t 12 == configure NTAG213F
229 args
=('-t 12 -u %s -p %08X -a %04X'):format(uid
, pwd
, pack
)
230 require('hf_mfu_magicwrite')
232 -- Set back args. Not that it's used, just for the karma...
238 -- generates random hex numbers between 31-39
239 local function random_num_hex(length
)
242 for i
= 1, length
, 1 do
243 str
= str
..math
.random(31, 39)
249 local function nwo( val
)
250 local b1
= band(val
, 0xFF)
251 local b2
= band( rshift(val
, 8), 0xFF)
252 local b3
= band( rshift(val
, 16), 0xFF)
253 local b4
= band( rshift(val
, 24), 0xFF)
254 return ('%02X%02X%02X%02X'):format(b1
, b2
, b3
, b4
)
258 local function template_NTAG213(uid
, material
, color
, length
, manufacture
, sales
)
259 local pwd
, pack
= core
.keygen_algo_d(uid
)
261 local m
= tonumber(length
, 10) * 1000
265 -- default empty file
269 -- t[4] = '0103A00C' --
270 -- t[5] = '340300FE' --
272 t
[8] = '5A'..material
..color
..'00' -- 5A, material, color, 00
273 t
[9] = '00'..random_num_hex(3) -- 00, three bytes serial number
274 t
[10] = m_str
-- total capacity
275 t
[11] = m_str
-- total capacity
276 t
[12] = 'D2002D00' -- fixed
277 t
[13] = manufacture
..sales
-- regioner,
278 t
[14] = random_num_hex(4) -- serial number
280 t
[17] = '34000000' -- fixed
282 -- remaining capacity of spool
284 t
[21] = nwo( bxor( m
, 0x54321248))
285 t
[22] = nwo( bxor( (m
- 3876923 ), 0x31275455))
286 t
[23] = nwo( bxor( (m
+ 6923923 ), 0x76235481))
288 t
[40] = '000000BD' --dynamic
289 t
[41] = '07000008' --cfg0
290 t
[42] = '80050000' --cfg1
291 t
[43] = ('%08X'):format(pwd
)
292 t
[44] = ('%04X0000'):format(pack
)
296 -- outputs the called arguments
297 local function print_conf(uid
, material
, color
, length
, producer
, sales
)
298 print('Create tag as following')
299 print( string.rep('--',16) )
301 print('Material ', material
[2])
302 print('Color ', color
[2])
303 print('Spool length ', length
)
305 print(' manufacturer', producer
[2])
306 print(' sales ', sales
[2])
307 print( string.rep('--',16) )
311 local function selftest()
312 list(_regions
, 'Regions')
313 list(_materials
, 'Materials')
314 list(_colors
, 'Colors')
318 -- The main entry point
319 local function main(args
)
321 math
.randomseed(os
.time());
324 print( string.rep('--',20) )
325 print( string.rep('--',20) )
328 local uid
= '04C5DF4A6D5180'
330 local useMAGIC
= false
331 local material
, color
, length
, producer
, sales
333 if #args
== 0 then return help() end
335 -- Read the parameters
336 for o
, a
in getopt
.getopt(args
, 'ht1u:l:m:c:p:s:') do
337 if o
== 'h' then return help() end
338 if o
== 't' then return selftest() end
339 if o
== 'u' then uid
= a
; useUID
= true end
340 if o
== 'm' then material
= a
end
341 if o
== 'c' then color
= a
end
342 if o
== 'l' then length
= tonumber(a
) end
343 if o
== 'p' then producer
= a
end
344 if o
== 's' then sales
= a
end
345 if o
== '1' then useMAGIC
= true end
348 color
= find(_colors
, color
)
349 if not color
then list(_colors
, 'Colors'); return oops('\n\nNot valid color') end
351 material
= find(_materials
, material
)
352 if not material
then list(_materials
, 'Materials'); return oops('\n\nNot valid material') end
354 producer
= find(_manufacturers
, producer
)
355 if not producer
then list(_manufacturers
, 'Regions Manufacturers'); return oops('\n\nNo valid manufacturer region') end
357 sales
= find(_sales
, sales
)
358 if not sales
then list(_sales
, 'Regions Sales'); return oops('\n\nNo valid sales region') end
361 return oops('\n\nNot valid spool length. Must be lesser than 300')
366 if uid
== nil then return oops('empty uid string') end
367 if #uid
== 0 then return oops('empty uid string') end
368 if #uid
~= 14 then return oops('uid wrong length. Should be 7 hex bytes') end
371 local tag, err
= lib14a
.read(false, true)
372 if not tag then return oops(err
) end
373 core
.clearCommandBuffer()
378 print_conf(uid
, material
, color
, length
, producer
, sales
)
381 local t
= template_NTAG213(uid
, material
[1], color
[1], length
, producer
[1], sales
[1])
385 configure_magic_ntag(uid
)
388 -- write template data to tag