Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
組込みRustでも でかい?JSONを扱いたい!
Search
Kenta IDA
June 07, 2023
Technology
1.5k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
組込みRustでも でかい?JSONを扱いたい!
520kBしかメモリが無いESP32環境上の組込みRustで、100kB弱のJSONを扱うために、JSONストリームパーサーを作ったお話です。
Kenta IDA
June 07, 2023
More Decks by Kenta IDA
See All by Kenta IDA
日本の家電とMatterの世界
ciniml
0
650
Matter開発環境をざっくり眺める
ciniml
0
2.4k
ATOMDisplay_DisplayModule_M5Stack_UG_2023_Tokyo.pdf
ciniml
1
1.9k
Rusty Stack-chanのすすめ
ciniml
0
790
Raspberry Pi Picoデバッガ使用のすすめ
ciniml
0
2.3k
ベアメタル向けRust stdクレートの実装調査
ciniml
2
860
ESP32でSORACOM Arcつないでみた
ciniml
2
1.5k
RustでCMSIS-DAP実装してみた。
ciniml
3
1.2k
技書博初出展してみた
ciniml
0
500
Other Decks in Technology
See All in Technology
サイバー捜査員研修(前半)
nomizone
1
1.9k
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1.2k
AI ネイティブな組織に Gemini Enterprise Agent Platform がなぜ必要なのか
asei
1
190
モバイルアプリ開発概論2026
recruitengineers
PRO
3
490
ブラウザ研修 2026
recruitengineers
PRO
4
780
クラウドセキュリティ入門 ~安全なクラウド利用のための基礎知識~
lhazy
11
8.3k
Webアクセシビリティ入門 2026
recruitengineers
PRO
2
450
20260801_スクフェス大阪
kgnkhkr
2
1.2k
今こそ聞きたいソフトウェア設計 ドメイン駆動設計再入門
masuda220
PRO
17
6.7k
初めてのGitHub Actions / GitHub Actions at First
tooppoo
0
150
ガバメントクラウドでのランサムウェア対策
techniczna
2
980
侵入は突然に 〜 IoTマルウェアと悪用される家庭の機器 ~ / When Intrusion Strikes: IoT Malware and the Abuse of Home Devices
nttcom
0
1.5k
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
200
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Deep Space Network (abreviated)
tonyrice
0
250
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.9k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.2k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
450
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
650
We Are The Robots
honzajavorek
0
290
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
330
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Transcript
組込みRustでも でかい?JSONを扱いたい! 2023-06-07 人工衛星の開発現場でLT大会
自己紹介 • 井田 健太 • おしごと:ESP32のファームウェアを書く ◦ まえはXilinx FPGAさわってました • seccamp2022,
2023 RISC-V CPU自作ゼミ講師 • twitter: @ciniml
最近の組込みRust環境 • ESP32系がアツい! • ESP32 ◦ Espressifの無線機能 (Wi-Fi + Bluetooth)
付きマイコン ◦ 通常の開発環境はC/C++ ▪ ESP-IDFというTCP/IPスタックや RTOS搭載環境が提供されている • 搭載製品いろいろ ◦ 液晶付きモジュールが多い。M5StackとかM5Stackとか https://shop.m5stack.com/products/m5stack-core2-esp32-iot-development-kit
ESP32の組込みRust環境(1) • ESP32にはいくつかの系統がある • ESP32-C3 ◦ CPUコアがRISC-Vの低コスト製品 • ESP32, ESP32-S3
◦ CPUコアがXtensaの高性能製品 ◦ デュアルコア構成 • RISC-Vはいいけど、Xtensaって何?🤔 ◦ Cadence (に買収されたTensilica) のカスタム可能CPUコア ◦ プロセッサの構成情報からコンパイラ・デバッガ・シミュレータを 生成してくれるシステムで使われているベースプロセッサ
ESP32の組込みRust環境(2) • Xtensaに(公式)LLVMバックエンドが無かったので Espressifが自前でLLVMとrustcをビルドして提供 • 導入:インストール用のツール espup が用意されており簡単 ◦ https://github.com/esp-rs/espup
◦ espup installを実行するだけ。 • (余談:2~3年前はめっちゃめんどくさかった)
作ってみたもの:Remo monitor on M5Paper • M5Paperという電子ペーパー付きのESP32ユニットに Nature Remo / Remo
Eの各種センサー情報を表示 • センサデータはRemo Cloud API経由で取れる https://nature.global/nature-remo/nature-remo-3/ https://nature.global/nature-remo-e/ https://shop.m5stack.com/products/m5paper-esp32-development-kit-v1-1- 960x540-4-7-eink-display-235-ppi Nature Cloud (on AWS) 温湿度 電力使用量 送信 温湿度 電力使用量 取得
Nature Remo Cloud APIの仕様 • Webサイト上でREST APIの仕様を公開 ◦ https://developer.nature.global/ •
https://api.nature.global/1/appliances にアクセスすると 家電の情報を含むJSONが降ってくる ◦ スマートメーターの電力取得に使う • https://api.nature.global/1/devices にアクセスすると デバイス情報を含むJSONが降ってくる ◦ デバイスが持っている温湿度計の情報取得に使う
JSONのサイズ • 一般ユーザー:数kBくらい? ◦ まあ普通にparseできそうやね • Nature社員:100kB弱 ◦ いろんなデバッグ用機器が繋がっててデカいのが返ってくる ◦
PCとかで扱う分には全く問題ないが … • ESP32には520kBしかRAMない… • TLS接続でも結構メモリを使うので余裕はそんなにない • 普通にメモリに置いてparseすると死ぬ
JSONのストリーム型パーサー? • Rust実装探したけどぱっと見使いやすそうなの見つからず ◦ みんなserde大好きですね • 仕方ないので練習がてら実装してみた ◦ https://github.com/ciniml/fuga-json-seq-parser/ ◦
json-seq-parserだと汎用的な名前すぎるので、fugaってつけといた!
JSONのストリーム型パーサーの仕様 • no_std ◦ ヒープ使わない • 極力省メモリ ◦ 理屈上はJSONのキーを保持できるバッファがあればいけるはず •
コールバック呼び出し ◦ 配列開始・終了 ◦ マップ開始・終了 ◦ キーまたは値 ◦ 参照を渡す(コピーしない) pub enum JsonNode<'a> { StartMap, EndMap, StartArray, EndArray, Key(JsonScalarValue<'a>), Value(JsonScalarValue<'a>), } FnMut(JsonNode<'node>) -> Result<ParserCallbackAction, CallbackError>
JSONのストリーム型パーサーの使い方 // バッファ256、スタック10でパーサー作成 let mut parser: Parser<256, 10> = Parser::new();
let mut file = File::open("data/devices.json").unwrap(); let mut reader = embedded_io::adapters::FromStd::new(&mut file); let mut indent_level = 0; loop { let result = parser .parse(&mut reader, |node| { match node { JsonNode::EndMap => indent_level -= 1, JsonNode::EndArray => indent_level -= 1, _ => {} } for _ in 0..indent_level { print!(" "); } match node { JsonNode::StartMap => println!("{{"), JsonNode::StartArray => println!("["), JsonNode::Key(v) => print!("{}: ", v), JsonNode::Value(v) => println!("{},", v), JsonNode::EndMap => println!("}},"), JsonNode::EndArray => println!("],"), } match node { JsonNode::StartMap => indent_level += 1, JsonNode::StartArray => indent_level += 1, _ => {} } DefaultParserCallbackResult::Ok(ParserCallbackAction::N othing) });
実装したもの 室温 (Remo 3) 湿度 (Remo 3) 瞬時電力 (Remo E
Lite)
実装してみた感想 • タグ付き共用体とパターンマッチ便利 ◦ パーサーのステートマシン書くのがとても楽 ◦ タプルに対するマッチができるので、複数条件をフラットに書ける ▪ 条件を網羅しやすい •
nom便利 ◦ Rustのパーサーコンビネーターcrate ◦ 適当に関数組み合わせたらパースできる ◦ 主にJSONの値のパースに使っている
宣伝 • Interface 2023年5月号は組込みRust特集 • M5StampC3を使った記事あり ◦ なんかページ数おかしい… (65ページ) 中林さん書きすぎw
• 組込みRust本もよろしくね! https://interface.cqpub.c o.jp/magazine/202305/ https://www.c-r.com/boo k/detail/1403
おわり