| 222 | } |
| 223 | |
| 224 | bool wifi::commandhelper2::exp_set_wifi_frequency(const std::string &device, |
| 225 | uint32_t freq_mhz) { |
| 226 | openhd::log::get_default()->debug("exp_set_wifi_frequency {} {}", device, |
| 227 | freq_mhz); |
| 228 | int err = 1; |
| 229 | struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT); |
| 230 | int rc; |
| 231 | |
| 232 | // Create the socket and connect to it. |
| 233 | struct nl_sock *sckt = nl_socket_alloc(); |
| 234 | genl_connect(sckt); |
| 235 | |
| 236 | // Allocate a new message. |
| 237 | struct nl_msg *mesg = nlmsg_alloc(); |
| 238 | |
| 239 | // Check /usr/include/linux/nl80211.h for a list of commands and attributes. |
| 240 | enum nl80211_commands command = NL80211_CMD_SET_CHANNEL; |
| 241 | |
| 242 | // Create the message so it will send a command to the nl80211 interface. |
| 243 | genlmsg_put(mesg, 0, 0, genl_ctrl_resolve(sckt, "nl80211"), 0, 0, command, 0); |
| 244 | |
| 245 | // Add specific attributes to change the frequency of the device. |
| 246 | NLA_PUT_U32(mesg, NL80211_ATTR_IFINDEX, if_nametoindex(device.c_str())); |
| 247 | NLA_PUT_U32(mesg, NL80211_ATTR_WIPHY_FREQ, freq_mhz); |
| 248 | |
| 249 | // Finally send it and receive the amount of bytes sent. |
| 250 | nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); |
| 251 | rc = nl_send_auto(sckt, mesg); |
| 252 | nl_recvmsgs(sckt, cb); |
| 253 | nl_cb_put(cb); |
| 254 | nlmsg_free(mesg); |
| 255 | nl_socket_free(sckt); |
| 256 | return (err >= 0); |
| 257 | |
| 258 | nla_put_failure: |
| 259 | nlmsg_free(mesg); |
| 260 | nl_socket_free(sckt); |
| 261 | return false; |
| 262 | } |