-
Notifications
You must be signed in to change notification settings - Fork 124
Set pico_w unique dhcp_hostname or configured hostname #1563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
3799ea5
to
4f00554
Compare
// parameter is ignored and both interfaces have the same MAC address. | ||
int err = cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, mac); | ||
if (err) { | ||
return globalcontext_make_atom(global, ATOM_STR("\x10", "device_mac_error")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, shouldn't error be a tuple, such as {error, Reason}
?
Anyway in this specific case I'd let the caller handle the error.
@@ -211,10 +215,29 @@ static void send_sntp_sync(struct timeval *tv) | |||
END_WITH_STACK_HEAP(heap, driver_data->global); | |||
} | |||
|
|||
// param should be pointer to malloc'd destination to copy device hostname | |||
// return ok atom or error as atom | |||
static term get_default_device_name(char *name, GlobalContext *global) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might rework this function to be just a helper for writing to the hostname buffer.
Such as static bool write_default_device_name(char *out, size_t out_len);
The caller may decide how to handle the error.
if (err) { | ||
return globalcontext_make_atom(global, ATOM_STR("\x10", "device_mac_error")); | ||
} | ||
size_t buf_size = DEFAULT_HOSTNAME_SIZE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we take this as parameter, we might make this code more solid and less error prone.
cyw43_arch_disable_sta_mode(); | ||
return OUT_OF_MEMORY_ATOM; | ||
} | ||
memcpy(driver_data->hostname, buf, buf_size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strlen + malloc + memcpy = strdup ;)
In general: let's just use strdup!
In this specific case I didn't understand why we don't just keep the buffer return from interop_term_to_string
.
@@ -419,9 +481,50 @@ static term start_ap(term ap_config, GlobalContext *global) | |||
} | |||
} | |||
|
|||
if (term_is_invalid_term(hostname_term)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code feels like a copy & paste of another block, can we factor out this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. This has been refactored.
af39282
to
632bcde
Compare
char *buf = malloc(size); | ||
snprintf(buf, size, | ||
"atomvm-%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); | ||
*out = strndup(buf, size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I didn't explain well what I meant with my previous commit.
strdup is good when for any reason we need to duplicate a string, and it is equivalent of strlen + malloc + memcpy.
Here instead there is no reason for this: we already allocating some new memory, that we can use in any manner we like.
So just do: *out = buf;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the changes now out
has not been allocated yet, is it better to malloc and check for NULL before *out = buf
or just use strdup? We can't keep buf because there is no way to free it later if the interface or driver is stopped.
} | ||
char *buf = malloc(size); | ||
snprintf(buf, size, | ||
"atomvm-%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use DEFAULT_HOSTNAME_FMT
here snprintf(buf, size, DEFAULT_HOSTNAME_FMT
...
cyw43_arch_disable_sta_mode(); | ||
return DriverOOM; | ||
} | ||
*out = strdup(buf); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also here, there is no need to duplicate buf
, just *out = buf
.
return DriverOK; | ||
} | ||
|
||
static int set_interface_dhcp_name(char **out, term dhcp_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually suggest the convention to leave the out arg after all input parameters.
Also return type here should be likely: enum DriverErrorCodeType
instead of int
.
static enum DriverErrorCodeType set_interface_dhcp_name(term dhcp_name, char **out)
@@ -211,15 +244,61 @@ static void send_sntp_sync(struct timeval *tv) | |||
END_WITH_STACK_HEAP(heap, driver_data->global); | |||
} | |||
|
|||
static int write_default_device_name(char **out, size_t size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type here should be likely: enum DriverErrorCodeType
instead of int.
So: static enum DriverErrorCodeType write_default_device_name(char **out, size_t size)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I forgot to update that after adding the enum.
CHANGELOG.md
Outdated
@@ -67,6 +67,7 @@ memory error | |||
- Fixed nif_atomvm_posix_read GC bug | |||
- Fixed `erlang:is_number/1` function, now returns true also for floats | |||
- Fixed unlink protocol and add support for `link/1` on ports | |||
- Correctly set Pico-W unique hostname, use `dhcp_hostname` if present in config. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should at least add also an entry to "Added" in order to mention that now we allow to configure the hostname (using a certain option).
While under "Fixed" I would just focus on what has been fixed, also I don't find this entry really clear.
Can you clearly state what was not working and what we fixed?
632bcde
to
8341a72
Compare
Correctly set `dhcp_hostname` from `sta_config_options()` and/or `ap_config_options()`, or, if undefined, sets a unique hostname to the default device name of `atomvm-${DEVICE_MAC}`. Formerly all pico_w devices tried to register to an access point with the factory default `PicoW` hostname, which would make all subsequent pico devices to connect to an access point, after the first one, unreachable by hostname, and cause unpredictable problems for some routers. Fixes atomvm#1094 on release-0.6 branch IMPORTANT: DO NOT FORWARD this commit to main branch. Breaking changes for rp2 (formerly rp2040) platform necessitate a seaprate PR to fix this issue for release-0.6, a fix has already been submitted to main branch (PR atomvm#1173). Signed-off-by: Winford <[email protected]>
8341a72
to
e76ab09
Compare
Merge mostly chores from release-0.6 branch, but also PR for setting pico_w unique dhcp_hostname or configured hostname (#1563).
Correctly set
dhcp_hostname
fromsta_config_options()
and/orap_config_options()
, or, if undefined, sets a unique hostname to the default device name ofatomvm-${DEVICE_MAC}
. Formerly all pico_w devices tried to register to an access point with the factory defaultPicoW
hostname, which would make all subsequent pico devices to connect to an access point, after the first one, unreachable by hostname, and cause unpredictable problems for some routers.Fixes #1094 on release-0.6 branch
IMPORTANT: DO NOT FORWARD this commit to main branch. Breaking changes for rp2 (formerly rp2040) platform necessitate a seaprate PR to fix this issue for release-0.6, a fix has already been submitted to main branch (PR #1173).
These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).
SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later