UNPKG

signalk-server

Version:

An implementation of a [Signal K](http://signalk.org) server for boats.

166 lines (157 loc) 33 kB
<!DOCTYPE html><html class="default" lang="en" data-base="../../../../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>Go/TinyGo Plugins | Signal K</title><meta name="description" content="Documentation for Signal K"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../../../../assets/style.css?cache=1783009712609"/><link rel="stylesheet" href="../../../../assets/highlight.css?cache=1783009712609"/><script defer src="../../../../assets/main.js?cache=1783009712609"></script><script async src="../../../../assets/icons.js?cache=1783009712609" id="tsd-icons-script"></script><script async src="../../../../assets/search.js?cache=1783009712609" id="tsd-search-script"></script><script async src="../../../../assets/navigation.js?cache=1783009712609" id="tsd-nav-script"></script><script async src="../../../../assets/hierarchy.js?cache=1783009712609" id="tsd-hierarchy-script"></script><link rel="stylesheet" href="../../../../assets/theme.css"/><script>if(window.self!==window.top){document.documentElement.classList.add('embedded')}</script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../../../../index.html" class="title"><img class="title-logo" src="../../../../assets/logo.svg" alt="Signal K"/><svg class="title-home-icon" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-label="Documentation home"><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path><polyline points="9 22 9 12 15 12 15 22"></polyline></svg></a><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-search"></use></svg><span class="visible@s">Search</span></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><div id="tsd-toolbar-links"><a href="https://discord.gg/uuZrwz4dCS" target="_blank" rel="noopener" class="toolbar-icon visible@s" aria-label="Discord"><svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><path d="M524.5 69.8a1.5 1.5 0 0 0 -.8-.7A485.1 485.1 0 0 0 404.1 32a1.8 1.8 0 0 0 -1.9 .9 337.5 337.5 0 0 0 -14.9 30.6 447.8 447.8 0 0 0 -134.4 0 309.5 309.5 0 0 0 -15.1-30.6 1.9 1.9 0 0 0 -1.9-.9A483.7 483.7 0 0 0 116.1 69.1a1.7 1.7 0 0 0 -.8 .7C39.1 183.7 18.2 294.7 28.4 404.4a2 2 0 0 0 .8 1.4A487.7 487.7 0 0 0 176 479.9a1.9 1.9 0 0 0 2.1-.7A348.2 348.2 0 0 0 208.1 430.4a1.9 1.9 0 0 0 -1-2.6 321.2 321.2 0 0 1 -45.9-21.9 1.9 1.9 0 0 1 -.2-3.1c3.1-2.3 6.2-4.7 9.1-7.1a1.8 1.8 0 0 1 1.9-.3c96.2 43.9 200.4 43.9 295.5 0a1.8 1.8 0 0 1 1.9 .2c2.9 2.4 6 4.9 9.1 7.2a1.9 1.9 0 0 1 -.2 3.1 301.4 301.4 0 0 1 -45.9 21.8 1.9 1.9 0 0 0 -1 2.6 391.1 391.1 0 0 0 30 48.8 1.9 1.9 0 0 0 2.1 .7A486 486 0 0 0 610.7 405.7a1.9 1.9 0 0 0 .8-1.4C623.7 277.6 590.9 167.5 524.5 69.8zM222.5 337.6c-29 0-52.8-26.6-52.8-59.2S193.1 219.1 222.5 219.1c29.7 0 53.3 26.8 52.8 59.2C275.3 311 251.9 337.6 222.5 337.6zm195.4 0c-29 0-52.8-26.6-52.8-59.2S388.4 219.1 417.9 219.1c29.7 0 53.3 26.8 52.8 59.2C470.7 311 447.5 337.6 417.9 337.6z"></path></svg></a><a href="https://github.com/SignalK/signalk-server" target="_blank" rel="noopener" class="toolbar-icon visible@s" aria-label="Discord"><svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3 .3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5 .3-6.2 2.3zm44.2-1.7c-2.9 .7-4.9 2.6-4.6 4.9 .3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3 .7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3 .3 2.9 2.3 3.9 1.6 1 3.6 .7 4.3-.7 .7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3 .7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3 .7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg></a><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-menu"></use></svg></a></div></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="../../../../Developing.html">Developing</a></li><li><a href="../../../Plugins.html">Plugins</a></li><li><a href="../../WASM_Plugins.html">WASM Plugins</a></li><li><a href="" aria-current="page">Go/TinyGo Plugins</a></li></ul></div><div class="tsd-panel tsd-typography"><h1 id="creating-gotinygo-plugins" class="tsd-anchor-link">Creating Go/TinyGo Plugins<a href="#creating-gotinygo-plugins" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h1> <p>Go plugins use TinyGo, a Go compiler designed for small environments including WebAssembly.</p> <h2 id="step-1-install-tinygo" class="tsd-anchor-link">Step 1: Install TinyGo<a href="#step-1-install-tinygo" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <p>Download from <a href="https://tinygo.org/getting-started/install/">https://tinygo.org/getting-started/install/</a></p> <pre><code class="bash"><span class="hl-6"># Verify installation</span><br/><span class="hl-5">tinygo</span><span class="hl-0"> </span><span class="hl-4">version</span> </code><button type="button">Copy</button></pre> <h2 id="step-2-create-project-structure" class="tsd-anchor-link">Step 2: Create Project Structure<a href="#step-2-create-project-structure" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <pre><code><span class="hl-0">my</span><span class="hl-1">-</span><span class="hl-0">go</span><span class="hl-1">-</span><span class="hl-0">plugin</span><span class="hl-1">/</span><br/><span class="hl-0">├── main.go # Plugin code</span><br/><span class="hl-0">├── go.mod # Go </span><span class="hl-2">module</span><br/><span class="hl-0">├── package.json # npm </span><span class="hl-1">package</span><span class="hl-0"> manifest</span><br/><span class="hl-0">├── public</span><span class="hl-1">/</span><span class="hl-0"> # Static web </span><span class="hl-7">assets</span><span class="hl-0"> (optional)</span><br/><span class="hl-0">│ └── index.html</span><br/><span class="hl-0">└── </span><span class="hl-2">README</span><span class="hl-0">.md</span> </code><button>Copy</button></pre> <h2 id="step-3-create-gomod" class="tsd-anchor-link">Step 3: Create go.mod<a href="#step-3-create-gomod" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <pre><code class="go"><span class="hl-0">module my</span><span class="hl-1">-go-</span><span class="hl-0">plugin</span><br/><br/><span class="hl-1">go</span><span class="hl-0"> </span><span class="hl-2">1.21</span> </code><button type="button">Copy</button></pre> <h2 id="step-4-create-maingo" class="tsd-anchor-link">Step 4: Create main.go<a href="#step-4-create-maingo" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <pre><code class="go"><span class="hl-1">package</span><span class="hl-0"> </span><span class="hl-5">main</span><br/><br/><span class="hl-1">import</span><span class="hl-0"> (</span><br/><span class="hl-0"> </span><span class="hl-4">&quot;</span><span class="hl-5">encoding/json</span><span class="hl-4">&quot;</span><br/><span class="hl-0"> </span><span class="hl-4">&quot;</span><span class="hl-5">unsafe</span><span class="hl-4">&quot;</span><br/><span class="hl-0">)</span><br/><br/><span class="hl-6">// FFI Imports from Signal K host</span><br/><span class="hl-6">//go:wasmimport env sk_debug</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">sk_debug</span><span class="hl-0">(</span><span class="hl-5">ptr</span><span class="hl-0"> </span><span class="hl-1">*byte</span><span class="hl-0">, </span><span class="hl-5">len</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">)</span><br/><br/><span class="hl-6">//go:wasmimport env sk_set_status</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">sk_set_status</span><span class="hl-0">(</span><span class="hl-5">ptr</span><span class="hl-0"> </span><span class="hl-1">*byte</span><span class="hl-0">, </span><span class="hl-5">len</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">)</span><br/><br/><span class="hl-6">//go:wasmimport env sk_set_error</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">sk_set_error</span><span class="hl-0">(</span><span class="hl-5">ptr</span><span class="hl-0"> </span><span class="hl-1">*byte</span><span class="hl-0">, </span><span class="hl-5">len</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">)</span><br/><br/><span class="hl-6">//go:wasmimport env sk_handle_message</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">sk_handle_message</span><span class="hl-0">(</span><span class="hl-5">ptr</span><span class="hl-0"> </span><span class="hl-1">*byte</span><span class="hl-0">, </span><span class="hl-5">len</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">)</span><br/><br/><span class="hl-6">// Helper wrappers</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">debug</span><span class="hl-0">(</span><span class="hl-5">msg</span><span class="hl-0"> </span><span class="hl-1">string</span><span class="hl-0">) {</span><br/><span class="hl-0"> </span><span class="hl-1">if</span><span class="hl-0"> </span><span class="hl-7">len</span><span class="hl-0">(msg) </span><span class="hl-1">&gt;</span><span class="hl-0"> </span><span class="hl-2">0</span><span class="hl-0"> {</span><br/><span class="hl-0"> </span><span class="hl-7">sk_debug</span><span class="hl-0">(unsafe.</span><span class="hl-7">StringData</span><span class="hl-0">(msg), </span><span class="hl-1">uint32</span><span class="hl-0">(</span><span class="hl-7">len</span><span class="hl-0">(msg)))</span><br/><span class="hl-0"> }</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">setStatus</span><span class="hl-0">(</span><span class="hl-5">msg</span><span class="hl-0"> </span><span class="hl-1">string</span><span class="hl-0">) {</span><br/><span class="hl-0"> </span><span class="hl-1">if</span><span class="hl-0"> </span><span class="hl-7">len</span><span class="hl-0">(msg) </span><span class="hl-1">&gt;</span><span class="hl-0"> </span><span class="hl-2">0</span><span class="hl-0"> {</span><br/><span class="hl-0"> </span><span class="hl-7">sk_set_status</span><span class="hl-0">(unsafe.</span><span class="hl-7">StringData</span><span class="hl-0">(msg), </span><span class="hl-1">uint32</span><span class="hl-0">(</span><span class="hl-7">len</span><span class="hl-0">(msg)))</span><br/><span class="hl-0"> }</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">handleMessage</span><span class="hl-0">(</span><span class="hl-5">msg</span><span class="hl-0"> </span><span class="hl-1">string</span><span class="hl-0">) {</span><br/><span class="hl-0"> </span><span class="hl-1">if</span><span class="hl-0"> </span><span class="hl-7">len</span><span class="hl-0">(msg) </span><span class="hl-1">&gt;</span><span class="hl-0"> </span><span class="hl-2">0</span><span class="hl-0"> {</span><br/><span class="hl-0"> </span><span class="hl-7">sk_handle_message</span><span class="hl-0">(unsafe.</span><span class="hl-7">StringData</span><span class="hl-0">(msg), </span><span class="hl-1">uint32</span><span class="hl-0">(</span><span class="hl-7">len</span><span class="hl-0">(msg)))</span><br/><span class="hl-0"> }</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-6">// Memory allocation for string passing</span><br/><span class="hl-6">//export allocate</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">allocate</span><span class="hl-0">(</span><span class="hl-5">size</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">) </span><span class="hl-1">*byte</span><span class="hl-0"> {</span><br/><span class="hl-0"> buf </span><span class="hl-1">:=</span><span class="hl-0"> </span><span class="hl-7">make</span><span class="hl-0">([]</span><span class="hl-1">byte</span><span class="hl-0">, size)</span><br/><span class="hl-0"> </span><span class="hl-1">return</span><span class="hl-0"> </span><span class="hl-1">&amp;</span><span class="hl-0">buf[</span><span class="hl-2">0</span><span class="hl-0">]</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-6">//export deallocate</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">deallocate</span><span class="hl-0">(</span><span class="hl-5">ptr</span><span class="hl-0"> </span><span class="hl-1">*byte</span><span class="hl-0">, </span><span class="hl-5">size</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">) {</span><br/><span class="hl-0"> </span><span class="hl-6">// With leaking GC, memory is reclaimed when module unloads</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-6">// Plugin exports</span><br/><span class="hl-6">//export plugin_id</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">plugin_id</span><span class="hl-0">(</span><span class="hl-5">outPtr</span><span class="hl-0"> </span><span class="hl-1">*byte</span><span class="hl-0">, </span><span class="hl-5">maxLen</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">) </span><span class="hl-1">int32</span><span class="hl-0"> {</span><br/><span class="hl-0"> </span><span class="hl-1">return</span><span class="hl-0"> </span><span class="hl-7">writeString</span><span class="hl-0">(</span><span class="hl-4">&quot;my-go-plugin&quot;</span><span class="hl-0">, outPtr, maxLen)</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-6">//export plugin_name</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">plugin_name</span><span class="hl-0">(</span><span class="hl-5">outPtr</span><span class="hl-0"> </span><span class="hl-1">*byte</span><span class="hl-0">, </span><span class="hl-5">maxLen</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">) </span><span class="hl-1">int32</span><span class="hl-0"> {</span><br/><span class="hl-0"> </span><span class="hl-1">return</span><span class="hl-0"> </span><span class="hl-7">writeString</span><span class="hl-0">(</span><span class="hl-4">&quot;My Go Plugin&quot;</span><span class="hl-0">, outPtr, maxLen)</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-6">//export plugin_schema</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">plugin_schema</span><span class="hl-0">(</span><span class="hl-5">outPtr</span><span class="hl-0"> </span><span class="hl-1">*byte</span><span class="hl-0">, </span><span class="hl-5">maxLen</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">) </span><span class="hl-1">int32</span><span class="hl-0"> {</span><br/><span class="hl-0"> schema </span><span class="hl-1">:=</span><span class="hl-0"> </span><span class="hl-4">`{&quot;type&quot;:&quot;object&quot;,&quot;properties&quot;:{}}`</span><br/><span class="hl-0"> </span><span class="hl-1">return</span><span class="hl-0"> </span><span class="hl-7">writeString</span><span class="hl-0">(schema, outPtr, maxLen)</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-6">//export plugin_start</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">plugin_start</span><span class="hl-0">(</span><span class="hl-5">configPtr</span><span class="hl-0"> </span><span class="hl-1">*byte</span><span class="hl-0">, </span><span class="hl-5">configLen</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">) </span><span class="hl-1">int32</span><span class="hl-0"> {</span><br/><span class="hl-0"> </span><span class="hl-7">debug</span><span class="hl-0">(</span><span class="hl-4">&quot;Go plugin starting&quot;</span><span class="hl-0">)</span><br/><span class="hl-0"> </span><span class="hl-7">setStatus</span><span class="hl-0">(</span><span class="hl-4">&quot;Running&quot;</span><span class="hl-0">)</span><br/><br/><span class="hl-0"> </span><span class="hl-6">// Emit a test delta</span><br/><span class="hl-0"> delta </span><span class="hl-1">:=</span><span class="hl-0"> </span><span class="hl-4">`{&quot;updates&quot;:[{&quot;values&quot;:[{&quot;path&quot;:&quot;test.goPlugin&quot;,&quot;value&quot;:&quot;hello from Go&quot;}]}]}`</span><br/><span class="hl-0"> </span><span class="hl-7">handleMessage</span><span class="hl-0">(delta)</span><br/><br/><span class="hl-0"> </span><span class="hl-1">return</span><span class="hl-0"> </span><span class="hl-2">0</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-6">//export plugin_stop</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">plugin_stop</span><span class="hl-0">() </span><span class="hl-1">int32</span><span class="hl-0"> {</span><br/><span class="hl-0"> </span><span class="hl-7">debug</span><span class="hl-0">(</span><span class="hl-4">&quot;Go plugin stopped&quot;</span><span class="hl-0">)</span><br/><span class="hl-0"> </span><span class="hl-7">setStatus</span><span class="hl-0">(</span><span class="hl-4">&quot;Stopped&quot;</span><span class="hl-0">)</span><br/><span class="hl-0"> </span><span class="hl-1">return</span><span class="hl-0"> </span><span class="hl-2">0</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-6">// Helper: write string to output buffer</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">writeString</span><span class="hl-0">(</span><span class="hl-5">s</span><span class="hl-0"> </span><span class="hl-1">string</span><span class="hl-0">, </span><span class="hl-5">ptr</span><span class="hl-0"> </span><span class="hl-1">*byte</span><span class="hl-0">, </span><span class="hl-5">maxLen</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">) </span><span class="hl-1">int32</span><span class="hl-0"> {</span><br/><span class="hl-0"> bytes </span><span class="hl-1">:=</span><span class="hl-0"> []</span><span class="hl-1">byte</span><span class="hl-0">(s)</span><br/><span class="hl-0"> length </span><span class="hl-1">:=</span><span class="hl-0"> </span><span class="hl-7">len</span><span class="hl-0">(bytes)</span><br/><span class="hl-0"> </span><span class="hl-1">if</span><span class="hl-0"> </span><span class="hl-1">uint32</span><span class="hl-0">(length) </span><span class="hl-1">&gt;</span><span class="hl-0"> maxLen {</span><br/><span class="hl-0"> length </span><span class="hl-1">=</span><span class="hl-0"> </span><span class="hl-1">int</span><span class="hl-0">(maxLen)</span><br/><span class="hl-0"> }</span><br/><span class="hl-0"> dst </span><span class="hl-1">:=</span><span class="hl-0"> unsafe.</span><span class="hl-7">Slice</span><span class="hl-0">(ptr, length)</span><br/><span class="hl-0"> </span><span class="hl-7">copy</span><span class="hl-0">(dst, bytes[:length])</span><br/><span class="hl-0"> </span><span class="hl-1">return</span><span class="hl-0"> </span><span class="hl-1">int32</span><span class="hl-0">(length)</span><br/><span class="hl-0">}</span><br/><br/><span class="hl-6">// Required for TinyGo WASM</span><br/><span class="hl-1">func</span><span class="hl-0"> </span><span class="hl-7">main</span><span class="hl-0">() {}</span> </code><button type="button">Copy</button></pre> <h2 id="step-5-create-packagejson" class="tsd-anchor-link">Step 5: Create package.json<a href="#step-5-create-packagejson" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <pre><code class="json"><span class="hl-0">{</span><br/><span class="hl-0"> </span><span class="hl-3">&quot;name&quot;</span><span class="hl-0">: </span><span class="hl-4">&quot;my-go-wasm-plugin&quot;</span><span class="hl-0">,</span><br/><span class="hl-0"> </span><span class="hl-3">&quot;version&quot;</span><span class="hl-0">: </span><span class="hl-4">&quot;0.1.0&quot;</span><span class="hl-0">,</span><br/><span class="hl-0"> </span><span class="hl-3">&quot;description&quot;</span><span class="hl-0">: </span><span class="hl-4">&quot;My Go WASM plugin&quot;</span><span class="hl-0">,</span><br/><span class="hl-0"> </span><span class="hl-3">&quot;keywords&quot;</span><span class="hl-0">: [</span><span class="hl-4">&quot;signalk-wasm-plugin&quot;</span><span class="hl-0">],</span><br/><span class="hl-0"> </span><span class="hl-3">&quot;wasmManifest&quot;</span><span class="hl-0">: </span><span class="hl-4">&quot;plugin.wasm&quot;</span><span class="hl-0">,</span><br/><span class="hl-0"> </span><span class="hl-3">&quot;wasmCapabilities&quot;</span><span class="hl-0">: {</span><br/><span class="hl-0"> </span><span class="hl-3">&quot;dataRead&quot;</span><span class="hl-0">: </span><span class="hl-2">true</span><span class="hl-0">,</span><br/><span class="hl-0"> </span><span class="hl-3">&quot;dataWrite&quot;</span><span class="hl-0">: </span><span class="hl-2">true</span><span class="hl-0">,</span><br/><span class="hl-0"> </span><span class="hl-3">&quot;storage&quot;</span><span class="hl-0">: </span><span class="hl-4">&quot;vfs-only&quot;</span><br/><span class="hl-0"> }</span><br/><span class="hl-0">}</span> </code><button type="button">Copy</button></pre> <blockquote> <p><strong>Note</strong>: The package name can be anything - there's no requirement for <code>@signalk/</code> scope. The <code>wasmManifest</code> field is what identifies this as a WASM plugin.</p> </blockquote> <h2 id="step-6-build" class="tsd-anchor-link">Step 6: Build<a href="#step-6-build" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <pre><code class="bash"><span class="hl-6"># Release build (smaller, optimized)</span><br/><span class="hl-5">tinygo</span><span class="hl-0"> </span><span class="hl-4">build</span><span class="hl-0"> </span><span class="hl-2">-o</span><span class="hl-0"> </span><span class="hl-4">plugin.wasm</span><span class="hl-0"> </span><span class="hl-2">-target=wasip1</span><span class="hl-0"> </span><span class="hl-2">-gc=leaking</span><span class="hl-0"> </span><span class="hl-2">-no-debug</span><span class="hl-0"> </span><span class="hl-4">main.go</span><br/><br/><span class="hl-6"># Debug build (for development)</span><br/><span class="hl-5">tinygo</span><span class="hl-0"> </span><span class="hl-4">build</span><span class="hl-0"> </span><span class="hl-2">-o</span><span class="hl-0"> </span><span class="hl-4">plugin.wasm</span><span class="hl-0"> </span><span class="hl-2">-target=wasip1</span><span class="hl-0"> </span><span class="hl-4">main.go</span> </code><button type="button">Copy</button></pre> <h2 id="step-7-install" class="tsd-anchor-link">Step 7: Install<a href="#step-7-install" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <p><strong>Option 1: Symlink (Recommended for Development)</strong></p> <pre><code class="bash"><span class="hl-2">cd</span><span class="hl-0"> </span><span class="hl-4">~/.signalk/node_modules</span><br/><span class="hl-5">ln</span><span class="hl-0"> </span><span class="hl-2">-s</span><span class="hl-0"> </span><span class="hl-4">/path/to/your/my-go-wasm-plugin</span><span class="hl-0"> </span><span class="hl-4">my-go-wasm-plugin</span> </code><button type="button">Copy</button></pre> <p><strong>Option 2: Direct Copy</strong></p> <pre><code class="bash"><span class="hl-5">mkdir</span><span class="hl-0"> </span><span class="hl-2">-p</span><span class="hl-0"> </span><span class="hl-4">~/.signalk/node_modules/my-go-wasm-plugin</span><br/><span class="hl-5">cp</span><span class="hl-0"> </span><span class="hl-4">plugin.wasm</span><span class="hl-0"> </span><span class="hl-4">package.json</span><span class="hl-0"> </span><span class="hl-4">~/.signalk/node_modules/my-go-wasm-plugin/</span> </code><button type="button">Copy</button></pre> <h2 id="go-ffi-interface-reference" class="tsd-anchor-link">Go FFI Interface Reference<a href="#go-ffi-interface-reference" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <p>Signal K provides these FFI imports in the <code>env</code> module:</p> <table> <thead> <tr> <th>Function</th> <th>Parameters</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>sk_debug</code></td> <td><code>(ptr, len)</code></td> <td>Log debug message</td> </tr> <tr> <td><code>sk_set_status</code></td> <td><code>(ptr, len)</code></td> <td>Set plugin status</td> </tr> <tr> <td><code>sk_set_error</code></td> <td><code>(ptr, len)</code></td> <td>Set error message</td> </tr> <tr> <td><code>sk_handle_message</code></td> <td><code>(ptr, len)</code></td> <td>Emit delta message</td> </tr> <tr> <td><code>sk_register_resource_provider</code></td> <td><code>(ptr, len)</code></td> <td>Register as resource provider</td> </tr> </tbody> </table> <h2 id="required-plugin-exports" class="tsd-anchor-link">Required Plugin Exports<a href="#required-plugin-exports" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <p>Your plugin MUST export:</p> <table> <thead> <tr> <th>Export</th> <th>Signature</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>plugin_id</code></td> <td><code>(out_ptr, max_len) -&gt; len</code></td> <td>Return plugin ID</td> </tr> <tr> <td><code>plugin_name</code></td> <td><code>(out_ptr, max_len) -&gt; len</code></td> <td>Return plugin name</td> </tr> <tr> <td><code>plugin_schema</code></td> <td><code>(out_ptr, max_len) -&gt; len</code></td> <td>Return JSON schema</td> </tr> <tr> <td><code>plugin_start</code></td> <td><code>(config_ptr, config_len) -&gt; status</code></td> <td>Start plugin</td> </tr> <tr> <td><code>plugin_stop</code></td> <td><code>() -&gt; status</code></td> <td>Stop plugin</td> </tr> <tr> <td><code>allocate</code></td> <td><code>(size) -&gt; ptr</code></td> <td>Allocate memory</td> </tr> <tr> <td><code>deallocate</code></td> <td><code>(ptr, size)</code></td> <td>Free memory</td> </tr> </tbody> </table> <h2 id="optional-plugin-exports" class="tsd-anchor-link">Optional Plugin Exports<a href="#optional-plugin-exports" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <p>Your plugin MAY export:</p> <table> <thead> <tr> <th>Export</th> <th>Signature</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td><code>poll</code></td> <td><code>() -&gt; status</code></td> <td>Called every 1 second while plugin is running. Useful for polling hardware, sockets, or external systems. Return 0 for success, non-zero for errors.</td> </tr> <tr> <td><code>http_endpoints</code></td> <td><code>() -&gt; json</code></td> <td>Return JSON array of HTTP endpoint definitions</td> </tr> <tr> <td><code>delta_handler</code></td> <td><code>(delta_ptr, delta_len)</code></td> <td>Receives Signal K deltas as JSON strings. Called for every delta emitted by the server.</td> </tr> </tbody> </table> <h2 id="tinygo-limitations" class="tsd-anchor-link">TinyGo Limitations<a href="#tinygo-limitations" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <p>TinyGo is a subset of Go. Notable limitations:</p> <ul> <li>No reflection (limited <code>encoding/json</code> support)</li> <li>No goroutines with WASI Preview 1</li> <li>Garbage collector options: <code>leaking</code> (recommended), <code>conservative</code></li> <li>Some standard library packages unavailable</li> </ul> <p>See <a href="https://tinygo.org/docs/reference/lang-support/">https://tinygo.org/docs/reference/lang-support/</a> for details.</p> <h2 id="additional-resources" class="tsd-anchor-link">Additional Resources<a href="#additional-resources" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-anchor"></use></svg></a></h2> <p>See the example-routes-waypoints plugin in <code>examples/wasm-plugins/example-routes-waypoints/</code> for a complete resource provider plugin.</p> </div></div><div class="col-sidebar"><div class="page-menu"><details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../../../../assets/icons.svg#icon-chevronDown"></use></svg><h3>On This Page</h3></summary><div class="tsd-accordion-details"><a href="#step-1-install-tinygo"><span>Step 1: <wbr/>Install <wbr/>Tiny<wbr/>Go</span></a><a href="#step-2-create-project-structure"><span>Step 2: <wbr/>Create <wbr/>Project <wbr/>Structure</span></a><a href="#step-3-create-gomod"><span>Step 3: <wbr/>Create go.mod</span></a><a href="#step-4-create-maingo"><span>Step 4: <wbr/>Create main.go</span></a><a href="#step-5-create-packagejson"><span>Step 5: <wbr/>Create package.json</span></a><a href="#step-6-build"><span>Step 6: <wbr/>Build</span></a><a href="#step-7-install"><span>Step 7: <wbr/>Install</span></a><a href="#go-ffi-interface-reference"><span>Go <wbr/>FFI <wbr/>Interface <wbr/>Reference</span></a><a href="#required-plugin-exports"><span>Required <wbr/>Plugin <wbr/>Exports</span></a><a href="#optional-plugin-exports"><span>Optional <wbr/>Plugin <wbr/>Exports</span></a><a href="#tinygo-limitations"><span>Tiny<wbr/>Go <wbr/>Limitations</span></a><a href="#additional-resources"><span>Additional <wbr/>Resources</span></a></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="../../../../modules.html">Signal K</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>