# NcProgramManager

## What This Is

Console EXE (C# .NET 4.7.2) for upload/download of CNC programs to/from machine controllers. Supports Fanuc FOCAS, Heidenhain LSV2, Siemens Sinumerik FTP, and Mitsubishi FTP. Runs on Windows, distributed to licensed machine operators and integrators.

## Core Value

Machinists and operators transfer CNC programs to/from any controller without manual intervention or internet access.

## Current State

| Attribute | Value |
|-----------|-------|
| Type | Application |
| Version | 0.5.0 |
| Status | v0.5 complete |
| Last Updated | 2026-05-18 |

## Requirements

### Core Features

- Upload programs to CNC controller
- Download programs from CNC controller
- Local file management (organize/store programs on disk)
- Multi-CN brand support (Fanuc + others)

### Validated (Shipped)
- [x] Fanuc FOCAS upload/download — initial
- [x] AES encryption for program files
- [x] CLI argument parsing
- [x] Machine-bound license check (hardcoded)
- [x] Multi-CN support — Fanuc, Heidenhain, Siemens, Mitsubishi — Phase 1–4
- [x] 3-file management mode (Fanuc) via IFanucMachine — Phase 4
- [x] NC program validation before upload (all 4 manufacturers) — Phase 5
- [x] Project renamed FanucProgramManager → NcProgramManager — Phase 6

### Validated (Shipped) — continued
- [x] RSA offline license core: IMachineFingerprint, ILicenseValidator, LicenseValidator, LicenseFile — Phase 7
- [x] License wired into Program.cs — RSA check at startup, license.lic next to EXE, -chiave= removed — Phase 8
- [x] LicenseGenerator standalone tool (keygen, fingerprint, sign) — Phase 9
- [x] Real RSA 2048-bit key pair generated; PublicKeyXml embedded in LicenseValidator.cs — Phase 9
- [x] ArgParser extracted — CLI parsing isolated from Program.cs; AesCrypt.cs dead code removed — Phase 11
- [x] Connection leak fixed — try/finally in Scarica/Invia; CncResult.Errors surfaced on failure — Phase 11
- [x] README.md — all 16 CLI params documented; 12 error codes with causa probabile + soluzione — Phase 12

### Validated (Shipped) — v0.4 Robustness
- [x] Null guards on WMI reads in MachineFingerprint (ISS-001) — Phase 13
- [x] SHA256CryptoServiceProvider disposed after use (ISS-005) — Phase 13
- [x] LSV2 connect timeout aborts hung TCP socket (ISS-006) — Phase 13
- [x] Hardware test scaffolding — env-var-gated `[Category("Hardware")]` tests for all 3 machine types (ISS-007) — Phase 14
- [x] LicenseFile edge-case tests (empty, whitespace, valid, nonexistent) — Phase 14
- [x] Empty-fingerprint guard in LicenseValidator (ISS-008 partial) — Phase 14

### Validated (Shipped) — v0.5 Operational Quality
- [x] NLog 5.2.8 structured logging — rolling file + console targets; all 4 machine classes + Program.cs (ISS-004) — Phase 15
- [x] Event handler silent catches surfaced — SetState catch → `_log.Warn`; cleanup catches annotated (ISS-003) — Phase 15
- [x] All Console.Write/WriteLine replaced with `_log.Info`; console target `${message}` plain layout, `levels="Info"` only — Phase 15

### Planned (Next)
- [ ] ReadActiveProgramAsync / SelectMainProgram for Heidenhain and Siemens
- [ ] Full hardware test run on real Windows machine (ISS-008 full closure)
- [ ] Async refactor — replace `.GetAwaiter().GetResult()` calls (ISS-002)

### Out of Scope
- Cloud sync
- Web UI
- Internet-dependent license validation

## Constraints

### Technical Constraints
- .NET Framework 4.7.2 — max compatibility, no upgrade
- Fanuc FOCAS via `fwlib32` (32-bit DLL, Windows-only)
- Must run offline — no internet dependency at runtime
- License validation must work without network

### Business Constraints
- Software must be license-protected (offline, machine-bound)
- License approach: RSA-SHA256 asymmetric, machine fingerprint (WMI CPU+board), .lic file next to EXE
- Vendor issues licenses with LicenseGenerator.exe; private key never distributed

## Key Decisions

| Decision | Rationale | Date | Status |
|----------|-----------|------|--------|
| .NET 4.7.2 | Max compatibility with operator machines | 2026-05-13 | Active |
| Console EXE output | Simple deployment, scriptable | 2026-05-13 | Active |
| Offline license only | Operators may have no internet on shop floor | 2026-05-13 | Active |
| `#` = Warning in Fanuc/Mitsubishi | Macro variable indicator — valid in context | 2026-05-13 | Active |
| Constructor injection for validator | Internal ctor accepts mock — enables unit testing without Windows DLL | 2026-05-13 | Active |
| RSA asymmetric licensing | Private key vendor-only; public key embedded in binary — decompiler can't forge licenses | 2026-05-17 | Active |
| LicenseGenerator as standalone separate repo | Vendor tool, never shipped to customers; separate lifecycle from NcProgramManager | 2026-05-17 | Active |
| Heidenhain LSV2 port 19000 | Standard LSV2 port; needs real hardware verification | 2026-05-13 | Active |
| Siemens FTP dir `/_N_MPF_DIR/` | Sinumerik standard MPF directory; FTP option must be enabled | 2026-05-13 | Active |
| NLog console target levels="Info", layout="${message}" | All operator output through NLog; console shows plain text identical to before; Warn/Error file only | 2026-05-18 | Active |
| Cleanup catches: comment-only, no log | Dispose must not throw; logging infra can fail | 2026-05-18 | Active |

## Success Metrics

| Metric | Target | Current | Status |
|--------|--------|---------|--------|
| Program transfer correctness | 100% (byte-verified) | Unknown | Pending hardware test |
| License validation (offline) | Works without network | RSA + LicenseGenerator complete; needs Windows hardware test | Done (pending HW test) |
| Multi-CN support | ≥2 manufacturers | 4 (Fanuc/Heidenhain/Siemens/Mitsubishi) | Done |
| NC validation coverage | All manufacturers | 4 manufacturers, 30+ tests | Done |

## Tech Stack / Tools

| Layer | Technology | Notes |
|-------|------------|-------|
| Language | C# | .NET Framework 4.7.2 |
| UI | Console EXE | CLI args via InputArgs.cs |
| CN Protocol (Fanuc) | FOCAS fwlib32 | 32-bit DLL, Windows-only |
| CN Protocol (Heidenhain) | LSV2 over TCP | Port 19000, CRC-16/CCITT |
| CN Protocol (Siemens) | FTP (FtpWebRequest) | ASCII mode, passive, `/_N_MPF_DIR/` |
| CN Protocol (Mitsubishi) | FTP (FtpWebRequest) | Port 21, `/PRG/`, no extensions |
| Validation | NcProgramValidator | Universal + per-manufacturer rules |
| Encryption | AES | AesCrypt.cs removed (dead code) — encryption not active |
| Licensing | RSA-SHA256 asymmetric | IMachineFingerprint + ILicenseValidator + LicenseGenerator; real key pair embedded |
| Logging | NLog 5.2.8 | Rolling file (daily, 7-day archive) + console Info-only plain layout; all production classes |
| Testing | NUnit 3.13.3 + Moq 4.18.4 | .NET 4.7.2 test project |

---
*PROJECT.md — Updated when requirements or context change*
*Last updated: 2026-05-18 after Phase 15 plan 02 — v0.5 complete, all operator output through NLog*
