Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
組込み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
770
Matter開発環境をざっくり眺める
ciniml
0
2.4k
ATOMDisplay_DisplayModule_M5Stack_UG_2023_Tokyo.pdf
ciniml
1
2k
Rusty Stack-chanのすすめ
ciniml
0
820
Raspberry Pi Picoデバッガ使用のすすめ
ciniml
0
2.4k
ベアメタル向けRust stdクレートの実装調査
ciniml
2
870
ESP32でSORACOM Arcつないでみた
ciniml
2
1.5k
RustでCMSIS-DAP実装してみた。
ciniml
3
1.2k
技書博初出展してみた
ciniml
0
530
Other Decks in Technology
See All in Technology
VS Code × GitHub Copilot での Fabric 開発
ryomaru0825
1
210
Databricksメトリクスビューはじめてのもくもく会
taka_aki
0
150
Mastering Agentic Development: Harness Engineering for Effective Coding Agents
konippi
3
670
Claude Designがめちゃくちゃ便利なので使ってほしい
diggymo
0
190
認知負荷を吸収し、プロダクトをまたぐPR Preview基盤の設計事例
taiki45
2
570
AIは爆速なのに、私が詰まっていた話 ― 音声入力と鳴くマスコットでボトルネックを削る
yama3133
0
240
キャリアLT今日までそして明日から
kentapapa
1
140
AIに書かせて、プラットフォームで縛る ― EKSプラットフォームで実践した責任境界と権限設計
elmodev09
1
1.1k
1人アドミンな私はAWSアカウント申請をSlackで完結したい!
ysuzuki
0
110
DORA_Metrics.pdf
wagnerfusca
1
140
企業の現実世界をグラフで写し取る
sansantech
PRO
0
240
カンファレンスに参加した後の浮遊感とセルフケア
pauli
0
290
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
380
Git: the NoSQL Database
bkeepers
PRO
432
67k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
910
Java REST API Framework Comparison - PWX 2021
mraible
34
9.7k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
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
おわり