{"id":13536718,"url":"https://github.com/JuulLabs/koap","last_synced_at":"2025-04-02T03:31:02.322Z","repository":{"id":36954099,"uuid":"269745680","full_name":"JuulLabs/koap","owner":"JuulLabs","description":"Kotlin CoAP encoder/decoder.","archived":false,"fork":false,"pushed_at":"2025-03-31T18:05:53.000Z","size":917,"stargazers_count":24,"open_issues_count":8,"forks_count":2,"subscribers_count":20,"default_branch":"main","last_synced_at":"2025-03-31T19:22:56.855Z","etag":null,"topics":["coap","rfc-7252","rfc-8323"],"latest_commit_sha":null,"homepage":"https://juullabs.github.io/koap","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JuulLabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-05T19:01:36.000Z","updated_at":"2025-03-31T18:05:57.000Z","dependencies_parsed_at":"2023-02-12T00:55:15.372Z","dependency_job_id":"6c72a65b-c1af-4ad9-ab48-50157a0257ff","html_url":"https://github.com/JuulLabs/koap","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuulLabs%2Fkoap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuulLabs%2Fkoap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuulLabs%2Fkoap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuulLabs%2Fkoap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JuulLabs","download_url":"https://codeload.github.com/JuulLabs/koap/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246750998,"owners_count":20827816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coap","rfc-7252","rfc-8323"],"created_at":"2024-08-01T09:00:47.869Z","updated_at":"2025-04-02T03:31:01.711Z","avatar_url":"https://github.com/JuulLabs.png","language":"Kotlin","funding_links":[],"categories":["Libraries"],"sub_categories":["Serializer"],"readme":"![badge][badge-js]\n![badge][badge-jvm]\n[![codecov](https://codecov.io/gh/JuulLabs/koap/branch/main/graph/badge.svg?token=EM9VA765J7)](https://codecov.io/gh/JuulLabs/koap)\n[![Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)\n\n# KoAP\n\nKotlin CoAP encoder/decoder that provides basic support for:\n- CoAP UDP ([RFC 7252])\n- CoAP TCP ([RFC 8323])\n- CoAP Observe ([RFC 7641])\n\n## Usage\n\n### Encoding\n\n`ByteArray`s of encoded CoAP messages can be created from `Message` objects (either `Message.Udp` or\n`Message.Tcp` depending on the desired encoding).\n\nTo encode a `Message` as a CoAP UDP `ByteArray`:\n\n```kotlin\nimport com.juul.koap.Message.Udp\nimport com.juul.koap.encode\n\nval message = Message.Udp(/* ... */)\nval encoded = message.encode()\n```\n\nSimilarly, to encode a `Message` as a CoAP TCP `ByteArray`:\n\n```kotlin\nimport com.juul.koap.Message.Tcp\nimport com.juul.koap.encode\n\nval message = Message.Tcp(/* ... */)\nval encoded = message.encode()\n```\n\n#### Javascript Encoding Example\n\nEncode a `Message` as a CoAP TCP `ByteArray`:\n\n```js\nimport kotlin from 'kotlin'\nimport koapModule from '@juullabs/koap'\n\nconst koap = koapModule.com.juul.koap\n\nlet method = koap.Message.Code.Method.GET\nlet token = new kotlin.Long(66)\nlet emptyPayload = new Uint8Array()\nlet optionsArray = [\n    new koap.Message.Option.UriPath('info'),\n    new koap.Message.Option.UriQuery('batt')\n]\nlet optionsList = new kotlin.kotlin.collections.ArrayList(optionsArray)\n\nlet message = new koap.Message.Tcp(\n    method,\n    token,\n    optionsList,\n    emptyPayload\n)\nlet encoded = koap.encode(message)\n```\n\n### Decoding\n\nEncoded CoAP messages (in the form of `ByteArray`s) can be decoded to `Message` objects (either\n`Message.Udp` or `Message.Tcp`, depending on the encoding).\n\nTo decode a `ByteArray` containing a message encoded as CoAP UDP:\n\n```kotlin\nimport com.juul.koap.Message.Udp\nimport com.juul.koap.decode\n\nval encoded: ByteArray // Encoded message that adheres to RFC 7252.\nval message = encoded.decode\u003cUdp\u003e()\n```\n\nSimilarly, to decode a `ByteArray` encoded as a CoAP TCP message:\n\n```kotlin\nimport com.juul.koap.Message.Tcp\nimport com.juul.koap.decode\n\nval encoded: ByteArray // Encoded message that adheres to RFC 8323.\nval message = encoded.decode\u003cTcp\u003e()\n```\n\n#### Header\n\nIf it's desirable to examine the header prior to decoding the entire encoded CoAP message, then\n`ByteArray.decodeUdpHeader` or `ByteArray.decodeTcpHeader` extension functions are available. The\nremaining encoded CoAP message can then be decoded by passing the header to the `ByteArray.decode`\nextension function, for example:\n\n```kotlin\nval encoded: ByteArray // Encoded message that adheres to RFC 7252.\nval header = encoded.decodeUdpHeader()\n\n// Examine header and determine that message should be decoded:\nval message = encoded.decode(header)\n```\n\n#### Javascript Decoding Example\n\nDecode a `ByteArray` as a CoAP TCP `Message`:\n\n```js\nimport kotlin from 'kotlin'\nimport koapModule from '@juullabs/koap'\n\nconst koap = koapModule.com.juul.koap\n\nlet encoded = new Uint8Array(/* ...message data... */)\nlet message = koap.decodeTcp(encoded)\n```\n\n## Examples\n\n### Encoding\n\n```kotlin\nval message = Message.Udp(\n    type = Confirmable,\n    code = GET,\n    id = 0xFEED,\n    token = 0xCAFE,\n    options = listOf(\n        UriPath(\"example\")\n    ),\n    payload = byteArrayOf()\n)\n```\n\nEncoding the above `Message` will produce the following encoded data:\n\n```\n42 01 FE ED CA FE B7 65 78 61 6D 70 6C 65\n```\n\nEncoded messages adhere to the format described in [Figure 7: Message Format] of [RFC 7252]:\n\n```\n 0                   1                   2                   3\n 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n|Ver| T |  TKL  |      Code     |          Message ID           |\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n|   Token (if any, TKL bytes) ...\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n|   Options (if any) ...\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n|1 1 1 1 1 1 1 1|    Payload (if any) ...\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n```\n\n| Field(s)            | Hex                    | Description                                                       |\n|---------------------|------------------------|-------------------------------------------------------------------|\n| Ver, T, TKL         | `42`                   | `4` = Version: 1, Type: 0 (Confirmable)\u003cbr/\u003e`2` = Token Length: 2 |\n| Code                | `01`                   | Code: 0.01 (GET)                                                  |\n| Message ID          | `FE ED`                |                                                                   |\n| Token               | `CA FE`                |                                                                   |\n| Option delta/length | `B7`                   | `B` = Delta option: 11 (Uri-Path)\u003cbr/\u003e`7` = Delta length: 7       |\n| Option value        | `65 78 61 6D 70 6C 65` | `\"example\"`                                                       |\n\n### Decoding\n\nAssuming an encoded message consists of the following CoAP UDP encoded data:\n\n```\n42 01 FE ED CA FE B5 2F 74 65 73 74 FF 48 65 6C 6C 6F 20 55 44 50 21\n```\n\n| Field(s)            | Hex                             | Description                                                       |\n|---------------------|---------------------------------|-------------------------------------------------------------------|\n| Ver, T, TKL         | `42`                            | `4` = Version: 1, Type: 0 (Confirmable)\u003cbr/\u003e`2` = Token Length: 2 |\n| Code                | `01`                            | Code: 0.01 (GET)                                                  |\n| Message ID          | `FE ED`                         |                                                                   |\n| Token               | `CA FE`                         |                                                                   |\n| Option delta/length | `B5`                            | `B` = Delta option: 11 (Uri-Path)\u003cbr/\u003e`5` = Delta length: 5       |\n| Option value        | `2F 74 65 73 74`                | `\"/test\"`                                                         |\n| Payload marker      | `FF`                            | Signifies the end of `Option`s and beginning of payload.          |\n| Payload             | `48 65 6C 6C 6F 20 55 44 50 21` | `\"Hello UDP!\"`                                                    |\n\nThe above `encoded` UDP message can be decoded to a `Message.Udp` via `ByteArray.decode` extension\nfunction:\n\n```kotlin\nval message = encoded.decode\u003cUdp\u003e()\n```\n\nThe resulting `Message` will be equivalent to:\n\n```kotlin\nMessage.Udp(\n    type = Confirmable,\n    code = GET,\n    id = 0xFEED,\n    token = 0xCAFE,\n    options = listOf(\n        UriPath(\"/test\")\n    ),\n    payload = \"Hello UDP!\".toByteArray()\n)\n```\n\n# Setup\n\n## Gradle\n\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.juul.koap/koap/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.juul.koap/koap)\n\n```kotlin\nrepositories {\n    mavenCentral()\n}\n\ndependencies {\n    implementation(\"com.juul.koap:koap-core:$version\")\n}\n```\n\n# License\n\n```\nCopyright 2020 JUUL Labs, Inc.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n   http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n\n\n[badge-android]: http://img.shields.io/badge/platform-android-6EDB8D.svg?style=flat\n[badge-ios]: http://img.shields.io/badge/platform-ios-CDCDCD.svg?style=flat\n[badge-js]: http://img.shields.io/badge/platform-js-F8DB5D.svg?style=flat\n[badge-jvm]: http://img.shields.io/badge/platform-jvm-DB413D.svg?style=flat\n[badge-linux]: http://img.shields.io/badge/platform-linux-2D3F6C.svg?style=flat\n[badge-windows]: http://img.shields.io/badge/platform-windows-4D76CD.svg?style=flat\n[badge-mac]: http://img.shields.io/badge/platform-macos-111111.svg?style=flat\n[badge-watchos]: http://img.shields.io/badge/platform-watchos-C0C0C0.svg?style=flat\n[badge-tvos]: http://img.shields.io/badge/platform-tvos-808080.svg?style=flat\n[badge-wasm]: https://img.shields.io/badge/platform-wasm-624FE8.svg?style=flat\n\n[RFC 7252]: https://tools.ietf.org/html/rfc7252\n[RFC 8323]: https://tools.ietf.org/html/rfc8323\n[RFC 7641]: https://tools.ietf.org/html/rfc7641\n[Figure 7: Message Format]: https://tools.ietf.org/html/rfc7252#section-3\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJuulLabs%2Fkoap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJuulLabs%2Fkoap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJuulLabs%2Fkoap/lists"}