CoAP Driver — 기술 레퍼런스
PlantPulse Edge 의 CoAP 드라이버는 Eclipse Californium 3.13.0 라이브러리를 래핑한 자바 구현입니다. 저전력 IoT 디바이스 (RFC 7252) 의 리소스 URI 를 GET / PUT 으로 접근합니다.
소스: plantpulse.driver.protocol.coap.*
| 클래스 | 책임 |
|---|---|
CoAPDriver | ProtocolDriver 구현체 — connect/read(GET)/write(PUT)/close, resource 별 CoapClient 캐시 |
라이브러리:
lib/californium-core-3.13.0.jarlib/element-connector-3.13.0.jar
동작 흐름
[connect] CoapClient(baseUri) 생성 → setConnected(true)
↓
[read] resource path → resourceUri = baseUri + "/" + path
→ resourceClients.computeIfAbsent(uri, CoapClient::new)
→ CoapClient.get() (synchronous)
→ resp.isSuccess() ? resp.getResponseText() : ""
↓
[write] resourceClients.computeIfAbsent(uri, CoapClient::new)
→ CoapClient.put(value, MediaTypeRegistry.TEXT_PLAIN /* 0 */)
→ resp.isSuccess()
↓
[close] 모든 CoapClient.shutdown() + resourceClients.clear()
CoAP 는 UDP 기반이라 connectionless — 클라이언트 객체 생성만으로 "connected" 로 간주합니다. 실제 디바이스 가용성은 첫 read 시점에 확인됩니다.
옵션 (BasicDriverSource.options)
이 드라이버에는 옵션이 없습니다. 연결에 쓰이는 값은 host 와 port 뿐입니다.
dtls · psk-identity · psk-key 를 넣어도 아무 효과가 없습니다드라이버는 연결 URI 를 항상 coap:// 로 만듭니다. 옵션에 dtls=true 를 넣거나
port 를 DTLS 표준 포트인 5684 로 지정해도 평문 CoAP 로 나갑니다 — 오류도 나지
않으므로 암호화되고 있다고 오해하기 쉽습니다.
DTLS 가 필요한 디바이스는 디바이스 앞에 DTLS 종단 게이트웨이를 두고, 게이트웨이의 평문 쪽을 이 드라이버로 읽으세요.
요청 타임아웃도 지정할 수 없습니다. Californium 기본값이 그대로 적용됩니다.
응답이 늦는 디바이스는 타임아웃 대신 수집 주기(timecycle)를 늘려 대응하세요.
주소 (resource path) 형식
| 형식 | 변환 결과 |
|---|---|
/sensors/temperature | coap://host:5683/sensors/temperature |
sensors/temperature | coap://host:5683/sensors/temperature (leading / 자동 보정) |
/.well-known/core | 디바이스의 리소스 디스커버리 — 등록된 리소스 목록 (CoRE Link Format) |
resource 별로 CoapClient 가 캐싱되어 socket 재사용. close() 시 모두 shutdown.
응답 처리
| Californium 응답 | driver 처리 |
|---|---|
resp == null | read = "", write = false |
resp.isSuccess() (2.xx) | read = resp.getResponseText(), write = true |
resp.getCode() 에러 (4.xx / 5.xx) | warn log + read = "", write = false |
CoAP 응답 텍스트는 mediaType 무관하게 그대로 String 으로 반환됩니다 — 디바이스가 JSON 으로 응답하면
호출 쪽 (PlantPulse 의 JsonExtract) 에서 디코드. CBOR 응답은 텍스트로 표시되므로 별도 디코더 필요 (현재 미지원).
단위 테스트
test/java/plantpulse/driver/protocol/coap/CoAPDriverTest.java — 13 메서드:
- 클래스 로드 + 초기 상태
- 메타 (isWriteSupported / isExternal / getDriverSource)
- 미연결 read / write 안전 (NPE 없음)
- close() 안전
baseUri()— 명시 / default 포트resourceUri()— leading/보정getDebugBaseUrl()—coap://host:port
네트워크 호출 없는 빌드-격리 테스트 (./gradlew test 그린).