forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell.nix
363 lines (318 loc) · 10.1 KB
/
shell.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
with import <nixpkgs> { };
let
nodejs = nodejs-12_x;
userName = "admin";
appName = "test-app2";
gtwName = "test-gtw";
mkDevName = class:
let base = "test-dev";
in if class == "a" then
"${base}-a"
else if class == "b" then
"${base}-b"
else if class == "c" then
"${base}-c"
else
throw "unknown class '${class}'";
frequencyPlan = "EU_863_870";
#frequencyPlan = "US_902_928_FSB_2";
stackPort = "1885";
udpPort = "1700";
pwd = toString ./.;
cmdDir = "${pwd}/cmd";
goRun = "${go}/bin/go run";
cliCmd = let conf = "${pwd}/ttn-lw-cli.yml";
in ''
$([[ -d '${cmdDir}/tti-lw-cli' ]] && printf "${goRun} -tags 'tti' ${cmdDir}/tti-lw-cli -c ${conf}" || printf "${goRun} ${cmdDir}/ttn-lw-cli -c ${conf}")'';
stackCmd = let conf = "${pwd}/ttn-lw-stack.yml";
in ''
$([[ -d '${cmdDir}/tti-lw-stack' ]] && printf "${goRun} -tags 'tti' ${cmdDir}/tti-lw-stack -c ${conf}" || printf "${goRun} ${cmdDir}/ttn-lw-stack -c ${conf}")'';
mageCmd = let mage = "${pwd}/tools/bin/mage";
in "MAGE=${mage} ${gnumake}/bin/make -C ${pwd} -s ${mage} && pushd ${pwd} && ${mage} init && ${mage}";
createAPIKeyCmd =
"${cliCmd} application api-keys create --application-id ${appName} --right-application-all | ${jq}/bin/jq -c -r '.key'";
makeWaitCmd = event: ''
read -n 1 -p "Ensure that ${event} and press any key to continue"
'';
joinWaitCmd = makeWaitCmd "the device has joined(OTAA) or booted(ABP)";
uplinkWaitCmd = makeWaitCmd "the device has sent an uplink";
linkApp = writeShellScriptBin "link-app" ''
set -xe
apiKey="$(${createAPIKeyCmd})"
${cliCmd} application link set ${appName} --api-key ''${apiKey} ''${@}
printf "export TTN_API_KEY=\"''${apiKey}\"" > ${pwd}/.envrc.api_key
${direnv}/bin/direnv reload
'';
bootstrap = writeShellScriptBin "bootstrap" ''
set -xe
${cliCmd} login
${cliCmd} application create ${appName} --user-id ${userName}
${cliCmd} gateway create ${gtwName} --defaults --user-id ${userName} --frequency_plan_id ${frequencyPlan}
printf "export BOOTSTRAP_DONE=1" > ${pwd}/.envrc.bootstrap
${linkApp}/bin/link-app
'';
scheduleClockExample = writeShellScriptBin "schedule-clock-example" ''
set -xe
cd ../../github.com/TheThingsNetwork/lorawan-stack-example-clock/controller
export TTN_APPLICATION_ID=''${TTN_APPLICATION_ID:-${appName}}
export TTN_DEVICE_ID=''${TTN_DEVICE_ID:-${mkDevName "c"}}
export TTN_API_KEY=''${TTN_API_KEY:-$(cat ${pwd}/.envrc.api_key)}
export TTN_SERVER=''${TTN_SERVER:-http://localhost:${stackPort}}
${nodejs}/bin/npm run start
'';
# TODO: Support class B/C args
makePushDownlinksCmd = devName:
lib.concatMapStringsSep "\n" (down: ''
${cliCmd} device downlink push ${appName} ${devName} \
--f-port=${toString down.port} \
--frm-payload=${down.payload}
sleep 1
'');
classADownlinks = [
{
port = 1;
payload = "1111";
}
{
port = 42;
payload = "22";
}
{
port = 120;
payload = "333333";
}
];
pushClassADownlinksCmd = devName:
makePushDownlinksCmd devName classADownlinks;
pushClassBDownlinksCmd = devName:
makePushDownlinksCmd devName (classADownlinks ++ [
# TODO: Add class B downlinks
]);
pushClassCDownlinksCmd = devName:
makePushDownlinksCmd devName (classADownlinks ++ [
# TODO: Add class C downlinks
]);
updateIfVersionAtLeast = v1: v2: base: attrs:
if lib.strings.versionAtLeast v1 v2 then base // attrs else base;
devices = let
mkGeneric = mac: phy: {
"lorawan-phy-version" = phy;
"lorawan-version" = mac;
};
mkGenericOTAA = mac: phy:
updateIfVersionAtLeast mac "1.1.0" (mkGeneric mac phy // {
"dev-eui" = "DEADBEEF01020304";
"join-eui" = "01020304DEADBEEF";
"resets-join-nonces" = true;
"root-keys.app-key.key" = "01020304DEADBEEF01020304DEADBEEF";
"supports-join" = true;
}) { "root-keys.nwk-key.key" = "DEADBEEF01020304DEADBEEF01020304"; };
mkGenericABP = mac: phy:
updateIfVersionAtLeast mac "1.1.0" (mkGeneric mac phy // {
"supports-join" = false;
"session.dev-addr" = "DEADBEEF";
"session.keys.f-nwk-s-int-key.key" = "01020304DEADBEEF01020304DEADBEEF";
"session.keys.app-s-key.key" = "DEADBEEF01020304DEADBEEF01020304";
}) {
"session.keys.s-nwk-s-int-key.key" = "01010203DEADBEEF01010203DEADBEEF";
"session.keys.nwk-s-enc-key.key" = "01010102DEADBEEF01010102DEADBEEF";
};
mkMbedOTAA = mkGenericOTAA;
mkLMNOTAA = mac: phy:
updateIfVersionAtLeast mac "1.1.0" (mkGenericOTAA mac phy // {
"dev-eui" = "DEADBEEF02030405";
"root-keys.app-key.key" = "02030405DEADBEEF02030405DEADBEEF";
}) { "root-keys.nwk-key.key" = "DEADBEEF02030405DEADBEEF02030405"; };
in lib.foldr (ver: acc:
acc // {
"generic-${ver.mac}-otaa" = mkGenericOTAA ver.mac ver.phy;
"generic-${ver.mac}-abp" = mkGenericABP ver.mac ver.phy;
}) { } [
{
mac = "1.0.0";
phy = "1.0.0";
}
{
mac = "1.0.2";
phy = "1.0.2-a";
}
{
mac = "1.0.2";
phy = "1.0.2-b";
}
{
mac = "1.0.3";
phy = "1.0.3-a";
}
{
mac = "1.1.0";
phy = "1.1.0-a";
}
{
mac = "1.1.0";
phy = "1.1.0-b";
}
] // {
"mbed-1.0.3-otaa" = mkMbedOTAA "1.0.3" "1.0.3-a";
"lmn-1.0.3-otaa" = mkLMNOTAA "1.0.3" "1.0.3-a";
"semtech-otaa" = {
"dev-eui" = "ffffffaa11111110";
"join-eui" = "0102030405060708";
"root-keys.app-key.key" = "\${SEMTECH_APPKEY}";
"lorawan-version" = "1.0.2";
"lorawan-phy-version" = "1.0.2-a";
"supports-join" = true;
};
"pba-abp" = {
"session.dev-addr" = "00000001";
"session.keys.app-s-key.key" = "01020304DEADBEEF01020304DEADBEEF";
"session.keys.f-nwk-s-int-key.key" = "01020304DEADBEEF01020304DEADBEEF";
"lorawan-phy-version" = "1.0.3-a";
"lorawan-version" = "1.0.3";
"supports-join" = false;
};
"uno-otaa" = {
"dev-eui" = "01020304DEADBEEF";
"join-eui" = "01020304DEADBEEF";
"root-keys.app-key.key" = "01020304DEADBEEF01020304DEADBEEF";
"lorawan-version" = "1.0.2";
"lorawan-phy-version" = "1.0.2-b";
"supports-join" = true;
"resets-join-nonces" = true;
};
};
toFlags = lib.generators.toKeyValue {
mkKeyValue = k: v: "--${k}=${lib.generators.mkValueStringDefault { } v} \\";
};
writeTestDeviceBin = name: devName: flags: script:
writeShellScriptBin name ''
[ "''${BOOTSTRAP_DONE}" == "1" ] || ${bootstrap}/bin/bootstrap
set -xe
${cliCmd} device delete ${appName} ${devName} || true
case "''${1}" in ${
lib.generators.toKeyValue {
mkKeyValue = k: v: ''
"${k}")
${cliCmd} device create ${appName} ${devName} \
--frequency_plan_id=${frequencyPlan} \
${toFlags v} ${toFlags flags} ''${@}
;;
'';
} devices
}
*)
echo "Unknown device ''${1} - specify one of ${
with builtins;
toString (attrNames devices)
}"
exit 1
esac
shift
${script}
'';
in mkShell {
CGO_ENABLED = "0";
GO111MODULE = "on";
GOROOT = "${go}/share/go";
TTN_LW_GS_UDP_LISTENERS = ":${udpPort}=${frequencyPlan}";
name = "ttn-env";
buildInputs = [
(writeShellScriptBin "mage" ''
${mageCmd} ''${@}
'')
(writeShellScriptBin "cli" ''
${cliCmd} ''${@}
'')
(writeShellScriptBin "ttn-lw-cli" ''
${cliCmd} ''${@}
'')
(writeShellScriptBin "stack" ''
${stackCmd} ''${@}
'')
(writeShellScriptBin "ttn-lw-stack" ''
${stackCmd} ''${@}
'')
(writeShellScriptBin "snapcraft" ''
${docker}/bin/docker run --rm -i \
--user $(id -u):$(id -g) \
--mount type=bind,src=$XDG_CONFIG_HOME/snapcraft,dst=$XDG_CONFIG_HOME/snapcraft \
--mount type=bind,src=$(pwd),dst=$(pwd) \
--entrypoint "/snap/bin/snapcraft" \
-e XDG_CONFIG_HOME \
-w $(pwd) \
snapcore/snapcraft:candidate ''${@}
'')
(writeShellScriptBin "start-clean-stack" ''
set -xe
${mageCmd} dev:dbStop
sudo ${coreutils}/bin/rm -rf .env/data
${docker-compose}/bin/docker-compose pull redis cockroach
${mageCmd} dev:dbStart
${mageCmd} dev:initStack
printf "export BOOTSTRAP_DONE=0" > ${pwd}/.envrc.bootstrap
${direnv}/bin/direnv reload
${stackCmd} start ''${@}
'')
(writeShellScriptBin "create-app" ''
set -xe
${cliCmd} application create ${appName} --user-id ${userName} ''${@}
'')
(writeShellScriptBin "delete-app" ''
set -xe
${cliCmd} application delete ${appName} ''${@}
'')
bootstrap
linkApp
scheduleClockExample
(let devName = mkDevName "a";
in writeTestDeviceBin "test-class-a-device" devName { } ''
${joinWaitCmd}
${makePushDownlinksCmd devName [{
port = 2;
payload = "abcd";
}]}
${uplinkWaitCmd}
${pushClassADownlinksCmd devName}
'')
(let devName = mkDevName "b";
in writeTestDeviceBin "test-class-b-device" devName {
supports-class-b = true;
#"mac-settings.desired-ping-slot-data-rate-index" = "DATA_RATE_0";
} ''
${joinWaitCmd}
${pushClassBDownlinksCmd devName}
'')
(let devName = mkDevName "c";
in writeTestDeviceBin "test-class-c-device" devName {
supports-class-c = true;
} ''
${joinWaitCmd}
source ${pwd}/.envrc.api_key
${scheduleClockExample}/bin/schedule-clock-example
${pushClassCDownlinksCmd devName}
'')
] ++ [
act
coreutils
entr
gnumake
go
go-tools
gopls
gotools
jq
minicom
mosquitto
neovim
nodejs
perl
postgresql_12
protobuf
rdbtools
redis
richgo
rpm
travis
xxd
];
}