Services
Interacting with system service modules via IPC
libnx
, the core C library used by nx.js to interact with the Switch, contains a
vast number of functions for interacting with various system services in a myriad
of different ways. Many of these functions are higher level wrappers on top of
the Switch's IPC communication mechanism, also known as "service modules".
It would not be reasonable for nx.js to attempt to expose all of these functions directly, so instead nx.js provides a low-level API for interacting with these system modules via IPC, which allows functionality that is not directly exposed by nx.js to be implemented in userland.
This is a low-level API which is not recommended to be used directly in most cases. You should look for an npm package which provides a higher-level API for the service you are trying to interact with.
Porting libnx functions to nx.js
Example with output value
Let's say you want your application to retrieve the current system region code. In libnx
,
this can be done using the setGetRegionCode()
function, which is exposed by the
set
service module.
However, nx.js does not directly expose this function, so your app can use the
Switch.Service
class to interact with the set
service module.
To do this correctly, you may need to reference the libnx source code to understand which command ID and parameters are required for the command you are attempting to port.
For example, the setGetRegionCode()
function is implemented as follows:
Based on this implementation, we can see that:
set
is the named service module- The command ID is
4
- There is no input data
- The output data is a region code, which is a
u32
value
Porting this function to nx.js would look like this:
Example with input value
Let's say you want to interact with a function which takes an input value, such as setsysSetRegionCode()
.
The setsysSetRegionCode()
function is implemented as follows:
set:sys
is the named service module- The command ID is
57
- The input data is a region code, which is a
u32
value - There is no output data
Porting this function to nx.js would look like this:
Example with buffers
Some commands require use of input and/or output buffers, such as setGetAvailableLanguageCodes()
. Let's take a look at how to port this function to nx.js.
The setGetAvailableLanguageCodes()
function is implemented as follows (some parts are omitted for brevity):
set
is the named service module- The command ID is
5
- There is no input data
- The output data is a
s32
number representing the number of entries that were returned - The output buffer is an array of
u64
values, which represent the available language codes
Additionally, looking at the Switchbrew wiki for LanguageCode
,
we can see that the language codes are represented as a u64
value, which is actually a NUL-terminated string.
Porting this function to nx.js would look like this:
Example with output service object
In some cases, the result of a command is itself a new Switch.Service
instance.
One such example is the clkrstOpenSession()
function:
In this case, you can create an "uninitialized" Switch.Service
instance (by
omitting the name
from the constructor), and then pass the instance to the
outObjects
option in the dispatch function: