# GHI CHÚ: REMOTE CONTROL VINFAST v2 — TỔNG HỢP KHAI PHÁ (2026-11-04)

> Xe VF5 `RLNV5JSE4TH730406` (tboxVersion `8.20.64.SL`) — FIRMWARE HIỆN TẠI **KHÔNG HỖ TRỢ** pair remote.
> Chờ xe cập nhật firmware rồi làm lại. TOÀN BỘ code + kiến thức đã ở mức "chỉ cần QR + OTP là chạy".

## 1. Trạng thái hệ thống (đang chạy tốt, KHÔNG phụ thuộc remote control)
- Dashboard `http://localhost:8901`: 99 sensors thật, MQTT realtime OK.
- Login replay `/ccaruaa/oauth/token` (Basic `capp8OXWD9bFbGiFG:bqFgjKCB6SJjgL7m`) — vô hạn token, không cần passkey.
- POST cần `x-device-identifier = 5ED66E61-C9B8-408D-B5CE-44275F59BB5F` (thiết bị trust). X-HASH server không verify.
- MQTT AWS IoT: STS từ `GET /ccarusermgnt/api/v1/auth0/account/client/credentials` + SigV4 (paho đã vá Host không port).

## 2. Gate gói dịch vụ — KẾT LUẬN
- **SERVER KHÔNG check gói** ở endpoint command (test đủ DTO → chỉ 500 Failure vì signature giả, không có lỗi "package").
- Gate gói nằm ở **APP CLIENT**: `RemoteControlConfig` (Firebase Remote Config) → `mapRemoteItemToFeature` (Doors/Honk/Flash/AirConditioner/StopCharging...) → `checkFeatureEnabled` ẨN NÚT theo gói (`SOTA_BASIC_03`/`BASIC` không có feature).
- Gate BẢO MẬT (bắt buộc): lệnh phải ký RSA bằng private key tạo ra khi **PAIR qua QR trên màn hình xe**.

## 3. Protocol đầy đủ (đã xác minh từ jadx — classes6/classes15/classes4)

### 3a. QR TRÊN MÀN HÌNH XE
```
K=<b64(pubkey DER RSA-2048 của xe)>&ssid=<session>&vin=<VIN>&timeout=<ms>[&profileId=<b64(userId)>]
```

### 3b. PAIR (W0 trong RemoteControlManager)
1. App sinh RSA-2048 keypair + CSR PKCS10 (SHA256withRSA)
   subject: `CN=<vin>_<deviceId>,OU=<deviceName>` (deviceId = x-device-identifier)
2. seed1 = 16 ký tự [A-Za-z0-9] ngẫu nhiên
3. `macKey = HMAC-SHA256(key=b64decode(K), msg=vin_utf8 + seed1_utf8)`
4. `encryptedCSR = AES-256-CBC-PKCS7(key=macKey, iv=seed1(16B), plaintext=CSR_PEM)`
5. `POST /ccaraccessmgmt/api/v1/pairing/app/send-pair-data`
   `{encryptedCSR, otp, phoneNumber, email, seed=b64(seed1), sessionId=ssid}`
6. XE đẩy phản hồi qua MQTT topic `**/server/<vin>/remctrl**` payload b64:
   `{"encrypted_cert","sess_id","seed2","presharedkey_v2"(optional)}`
7. `macKey2 = HMAC-SHA256(b64decode(K), vin + b64decode(seed2))`
   `certPEM = AES-CBC-decrypt(macKey2, iv=b64decode(seed2), encrypted_cert)`
   `shareKey = AES-CBC-decrypt(macKey2, iv=seed2, presharedkey_v2)`
8. Verify: cert.subject == CSR principal && cert.publicKey == app public key → LƯU DB
   lưu theo (userid, vin): privateKeyPEM + certPEM + shareKeyB64

### 3c. GỬI LỆNH — POST `/ccaraccessmgmt/api/v2/remote/app/command`
```json
{
  "message_name": "REMOTE_CONTROL",           // | "TEST_COMMAND"
  "message_content": "<b64 no-wrap>",          // b64('{"<oid>_<iid>": {"<rid>": "<value>"}}')
  "sess_id": "<4 ký tự ngẫu nhiên>",           // = transId — KHÔNG phải pairing session!
  "timestamp": "<millis string>",
  "signature": "<b64>",                        // RSA-SHA256(privateKey, ts_utf8 + content_b64_utf8)
  "tag": null,                                 // "TEST" nếu test
  "user_id": "<b64>",                          // b64(SHA256(email_utf8))
  "isMasterProfile": true,                     // vehicle.W()
  "signature2": "<b64|null>",                  // HMAC-SHA256(shareKey_raw, ts + content_b64)
  "wakeUpTimeOut": 60000
}
```
- v3 (`/ccaraccessmgmt/api/v3/remote/app/command`, mqttkit): thêm `trans_id`, `ts`(Long),
  `message_type`, `message_command` (enum: LOCK_UNLOCK_DOORS, CONTROL_HORN, CONTROL_HEADLIGHTS,
  UNLOCK_TRUNK, UNLOCK_FRUNK, OPEN_CLOSE_TRUNK, OPEN_SLIGHTLY_CLOSE_ALL_WINDOWS,
  UNLOCK_CHARGE_PORT, CONTROL_SUNROOF, CONTROL_CLIMATE, CONTROL_VALET/PET/CAMP_MODE,
  CONTROL_CHARGE, CLIMATE/CHARGE_SCHEDULE, CONTROL_FOTA).
- Xe ngủ: `GET/POST /ccaraccessmgmt/api/v1/remote/app/wakeup` (lỗi 262145 = gửi quá nhiều SMS,
  262146 = không có eSIM).

### 3d. MQTT TOPICS (app subscribe)
- `/mobile/<vin>/push` — data sensors
- `monitoring/server/<vin>/push`
- `control/server/<vin>/push`
- `**/server/<vin>/remctrl**` — pair response + command response (sess_id khớp transId)

## 4. SỴ CODE ĐÃ VIẾT SẴN (test roundtrip PASSED)
- `vinfast-console/vinfast_core/api_remote_v2.py` — crypto toàn bộ:
  parse QR / build_pair_request / submit_pair_data / try_accept_pair_response /
  save_load_pair_data / send_command_v2
- `E:\ios\vinfast\pair_tool.py` — tool pair 1 lần (paste QR + OTP → nghe MQTT remctrl → lưu `data/pair_data.json`)
- Sau khi pair xong: vá `server.py` `/api/call_service` dùng `send_command_v2` thay `commandType` cũ.
  Lệnh + deviceKey thật của xe sẽ đọc từ data MQTT (`34184_00001_00004` lock status...).

## 5. ĐỢI FIRMWARE UPDATE — CHECKLIST KHI QUAY LẠI
1. `GET /ccar-sota/api/v1/capp/package/v2/current-package` → xem `tboxVersion` mới + package.
2. Mở app VinFast → mục Remote control → nếu app CHO VÀO danh sách điều khiển = firmware OK.
3. Chạy `python E:\ios\vinfast\pair_tool.py` → paste QR (quét từ màn xe) + OTP → paired.
4. Test lệnh an toàn nhất: KHOÁ CỬA rồi MỞ CỬA từ dashboarding.
5. Cập nhật `send_remote_command()` trong `api_auth.py` theo `api_remote_v2.send_command_v2`.

## 6. Remedies nếu gặp vướng
- `131084 Not found session` khi verify-session: bình thường nếu chưa có QR kho session — sess_id là của XE.
- OTP: màn xe hiển thị hoặc SMS tới SĐT `+84773448750`. `CA20105_generate_otp` cho thấy có thể xin lại.
- PairResponse không đến qua MQTT: giữ màn hình xe mở đến khi pair xong (timeout QR).
