+static void mesh_ssid_init(GSupplicantSSID *ssid, struct connman_mesh *mesh)
+{
+ const char *name;
+ const char *security;
+
+ if (ssid->ssid)
+ g_free(ssid->ssid);
+
+ memset(ssid, 0, sizeof(*ssid));
+ ssid->mode = G_SUPPLICANT_MODE_MESH;
+
+ security = connman_mesh_get_security(mesh);
+ ssid->security = mesh_network_security(security);
+
+ if (ssid->security == G_SUPPLICANT_SECURITY_SAE)
+ ssid->passphrase = connman_mesh_get_passphrase(mesh);
+
+ ssid->freq = connman_mesh_get_frequency(mesh);
+ name = connman_mesh_get_name(mesh);
+ if (name) {
+ ssid->ssid_len = strlen(name);
+ ssid->ssid = g_malloc0(ssid->ssid_len + 1);
+ memcpy(ssid->ssid, name, ssid->ssid_len);
+ ssid->scan_ssid = 1;
+ }
+}
+
+static int mesh_peer_connect(struct connman_mesh *mesh)
+{
+ GList *list;
+ struct wifi_data *wifi;
+ struct wifi_mesh_info *mesh_info;
+ bool mesh_if_found = false;
+ GSupplicantInterface *interface;
+ GSupplicantSSID *ssid;
+
+ for (list = iface_list; list; list = list->next) {
+ wifi = list->data;
+
+ if (wifi->mesh_interface) {
+ mesh_if_found = true;
+ break;
+ }
+ }
+
+ if (!mesh_if_found) {
+ DBG("Mesh interface is not created");
+ return -ENODEV;
+ }
+
+ mesh_info = wifi->mesh_info;
+
+ interface = mesh_info->interface;
+
+ ssid = g_try_malloc0(sizeof(GSupplicantSSID));
+ if (!ssid)
+ return -ENOMEM;
+
+ mesh_info->mesh = mesh;
+
+ mesh_ssid_init(ssid, mesh);
+ return g_supplicant_interface_connect(interface, ssid,
+ mesh_connect_callback, mesh);
+}
+
+static void mesh_peer_change_status_callback(int result,
+ GSupplicantInterface *interface,
+ void *user_data)
+{
+ struct mesh_change_peer_status_info *data = user_data;
+
+ DBG("result %d Peer Status %d", result, data->peer_status);
+
+ if (result == 0 && data->peer_status == CONNMAN_MESH_PEER_REMOVE) {
+ /* WLAN_REASON_MESH_PEERING_CANCELLED = 52 */
+ connman_mesh_remove_connected_peer(data->peer_address, 52);
+ }
+
+ if (data->callback)
+ data->callback(result, data->user_data);
+
+ g_free(data->peer_address);
+ g_free(data);
+ return;
+}
+
+static int mesh_change_peer_status(const char *peer_address,
+ enum connman_mesh_peer_status status,
+ mesh_change_peer_status_cb_t callback, void *user_data)
+{
+ GList *list;
+ struct wifi_data *wifi;
+ struct wifi_mesh_info *mesh_info;
+ bool mesh_if_found = false;
+ GSupplicantInterface *interface;
+ struct mesh_change_peer_status_info *data;
+ const char *method;
+
+ for (list = iface_list; list; list = list->next) {
+ wifi = list->data;
+
+ if (wifi->mesh_interface) {
+ mesh_if_found = true;
+ break;
+ }
+ }
+
+ if (!mesh_if_found) {
+ DBG("Mesh interface is not created");
+ return -ENODEV;
+ }
+
+ mesh_info = wifi->mesh_info;
+
+ interface = mesh_info->interface;
+
+ switch (status) {
+ case CONNMAN_MESH_PEER_ADD:
+ method = "MeshPeerAdd";
+ break;
+ case CONNMAN_MESH_PEER_REMOVE:
+ method = "MeshPeerRemove";
+ break;
+ default:
+ DBG("Invalid method");
+ return -EINVAL;
+ }
+
+ data = g_try_malloc0(sizeof(struct mesh_change_peer_status_info));
+ if (data == NULL) {
+ DBG("Memory allocation failed");
+ return -ENOMEM;
+ }
+
+ data->peer_address = g_strdup(peer_address);
+ data->peer_status = status;
+ data->callback = callback;
+ data->user_data = user_data;
+
+ return g_supplicant_interface_mesh_peer_change_status(interface,
+ mesh_peer_change_status_callback, peer_address, method,
+ data);
+}
+
+static struct connman_mesh_driver mesh_driver = {
+ .add_interface = add_mesh_interface,
+ .remove_interface = remove_mesh_interface,
+ .connect = mesh_peer_connect,
+ .disconnect = mesh_peer_disconnect,
+ .change_peer_status = mesh_change_peer_status,
+};
+
+static void mesh_support(GSupplicantInterface *interface)
+{
+ DBG("");
+
+ if (!g_supplicant_interface_has_mesh(interface))
+ return;
+
+ if (connman_technology_driver_register(&mesh_tech_driver) < 0) {
+ DBG("Could not register Mesh technology driver");
+ return;
+ }
+
+ connman_mesh_driver_register(&mesh_driver);
+}
+
+static void check_mesh_technology(void)
+{
+ bool mesh_exists = false;
+ GList *list;
+
+ for (list = iface_list; list; list = list->next) {
+ struct wifi_data *w = list->data;
+
+ if (w->interface &&
+ g_supplicant_interface_has_mesh(w->interface))
+ mesh_exists = true;
+ }
+
+ if (!mesh_exists) {
+ connman_technology_driver_unregister(&mesh_tech_driver);
+ connman_mesh_driver_unregister(&mesh_driver);
+ }
+}
+
+static void mesh_group_started(GSupplicantInterface *interface)
+{
+ struct wifi_data *wifi;
+ struct wifi_mesh_info *mesh_info;
+ struct connman_mesh *mesh;
+ const unsigned char *ssid;
+ unsigned int ssid_len;
+ char name[33];
+
+ ssid = g_supplicant_interface_get_mesh_group_ssid(interface, &ssid_len);
+ memcpy(name, ssid, ssid_len);
+ name[ssid_len] = '\0';
+ DBG("name %s", name);
+ wifi = g_supplicant_interface_get_data(interface);
+ DBG("wifi %p", wifi);
+
+ if (!wifi)
+ return;
+
+ mesh_info = wifi->mesh_info;
+ if (!mesh_info)
+ return;
+
+ mesh = mesh_info->mesh;
+ if (!mesh)
+ return;
+
+ connman_mesh_peer_set_state(mesh, CONNMAN_MESH_STATE_CONFIGURATION);
+}
+
+static void mesh_group_removed(GSupplicantInterface *interface)
+{
+ struct wifi_data *wifi;
+ struct wifi_mesh_info *mesh_info;
+ struct connman_mesh *mesh;
+ const unsigned char *ssid;
+ unsigned int ssid_len;
+ int disconnect_reason;
+ char name[33];
+
+ ssid = g_supplicant_interface_get_mesh_group_ssid(interface, &ssid_len);
+ memcpy(name, ssid, ssid_len);
+ name[ssid_len] = '\0';
+ DBG("name %s", name);
+
+ disconnect_reason = g_supplicant_mesh_get_disconnect_reason(interface);
+ DBG("Disconnect Reason %d", disconnect_reason);
+
+ wifi = g_supplicant_interface_get_data(interface);
+ DBG("wifi %p", wifi);
+
+ if (!wifi)
+ return;
+
+ mesh_info = wifi->mesh_info;
+ if (!mesh_info)
+ return;
+
+ mesh = connman_get_connected_mesh_from_name(name);
+ if (!mesh) {
+ DBG("%s is not connected", name);
+ mesh = connman_get_connecting_mesh_from_name(name);
+ if (!mesh) {
+ DBG("%s is not connecting", name);
+ return;
+ }
+ }
+
+ connman_mesh_peer_set_disconnect_reason(mesh, disconnect_reason);
+ connman_mesh_peer_set_state(mesh, CONNMAN_MESH_STATE_DISCONNECT);
+}
+
+static void mesh_peer_connected(GSupplicantMeshPeer *mesh_peer)
+{
+ const char *peer_address;
+
+ peer_address = g_supplicant_mesh_peer_get_address(mesh_peer);
+
+ if (!peer_address)
+ return;
+
+ DBG("Peer %s connected", peer_address);
+ connman_mesh_add_connected_peer(peer_address);
+}
+
+static void mesh_peer_disconnected(GSupplicantMeshPeer *mesh_peer)
+{
+ const char *peer_address;
+ int reason;
+
+ peer_address = g_supplicant_mesh_peer_get_address(mesh_peer);
+
+ if (!peer_address)
+ return;
+
+ reason = g_supplicant_mesh_peer_get_disconnect_reason(mesh_peer);
+
+ DBG("Peer %s disconnected with reason %d", peer_address, reason);
+ connman_mesh_remove_connected_peer(peer_address, reason);
+}
+#endif
+
+static struct wifi_data *get_pending_wifi_data(const char *ifname)
+{
+ GList *list;
+
+ for (list = pending_wifi_device; list; list = list->next) {
+ struct wifi_data *wifi;
+ const char *dev_name;
+
+ wifi = list->data;
+ if (!wifi || !wifi->device)
+ continue;
+
+ dev_name = connman_device_get_string(wifi->device, "Interface");
+ if (!g_strcmp0(ifname, dev_name)) {
+ pending_wifi_device = g_list_delete_link(
+ pending_wifi_device, list);
+ return wifi;
+ }
+ }
+
+ return NULL;
+}
+
+static void remove_pending_wifi_device(struct wifi_data *wifi)
+{
+ GList *link;
+
+ link = g_list_find(pending_wifi_device, wifi);
+
+ if (!link)
+ return;
+
+ pending_wifi_device = g_list_delete_link(pending_wifi_device, link);
+}
+
+static void peer_cancel_timeout(struct wifi_data *wifi)
+{
+ if (wifi->p2p_connection_timeout > 0)
+ g_source_remove(wifi->p2p_connection_timeout);
+
+ wifi->p2p_connection_timeout = 0;
+ wifi->p2p_connecting = false;
+
+ if (wifi->pending_peer) {
+ connman_peer_unref(wifi->pending_peer);
+ wifi->pending_peer = NULL;
+ }
+}
+
+static gboolean peer_connect_timeout(gpointer data)
+{
+ struct wifi_data *wifi = data;
+
+ DBG("");
+
+ if (wifi->p2p_connecting) {
+ enum connman_peer_state state = CONNMAN_PEER_STATE_FAILURE;
+ GSupplicantPeer *gs_peer =
+ g_supplicant_interface_peer_lookup(wifi->interface,
+ connman_peer_get_identifier(wifi->pending_peer));
+
+ if (g_supplicant_peer_has_requested_connection(gs_peer))
+ state = CONNMAN_PEER_STATE_IDLE;
+
+ connman_peer_set_state(wifi->pending_peer, state);
+ }
+
+ peer_cancel_timeout(wifi);
+
+ return FALSE;
+}
+
+static void peer_connect_callback(int result, GSupplicantInterface *interface,
+ void *user_data)
+{
+ struct wifi_data *wifi = user_data;
+ struct connman_peer *peer = wifi->pending_peer;
+
+ DBG("peer %p - %d", peer, result);
+
+ if (!peer)
+ return;
+
+ if (result < 0) {
+ peer_connect_timeout(wifi);
+ return;
+ }
+
+ connman_peer_set_state(peer, CONNMAN_PEER_STATE_ASSOCIATION);
+
+ wifi->p2p_connection_timeout = g_timeout_add_seconds(
+ P2P_CONNECTION_TIMEOUT,
+ peer_connect_timeout, wifi);
+}
+
+static int peer_connect(struct connman_peer *peer,
+ enum connman_peer_wps_method wps_method,
+ const char *wps_pin)
+{
+ struct connman_device *device = connman_peer_get_device(peer);
+ GSupplicantPeerParams *peer_params;
+ GSupplicantPeer *gs_peer;
+ struct wifi_data *wifi;
+ bool pbc, pin;
+ int ret;
+
+ DBG("peer %p", peer);
+
+ if (!device)
+ return -ENODEV;
+
+ wifi = connman_device_get_data(device);
+ if (!wifi || !wifi->interface)
+ return -ENODEV;
+
+ if (wifi->p2p_connecting)
+ return -EBUSY;
+
+ gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
+ connman_peer_get_identifier(peer));
+ if (!gs_peer)
+ return -EINVAL;
+
+ pbc = g_supplicant_peer_is_wps_pbc(gs_peer);
+ pin = g_supplicant_peer_is_wps_pin(gs_peer);
+
+ switch (wps_method) {
+ case CONNMAN_PEER_WPS_UNKNOWN:
+ if ((pbc && pin) || pin)
+ return -ENOKEY;
+ break;
+ case CONNMAN_PEER_WPS_PBC:
+ if (!pbc)
+ return -EINVAL;
+ wps_pin = NULL;
+ break;
+ case CONNMAN_PEER_WPS_PIN:
+ if (!pin || !wps_pin)
+ return -EINVAL;
+ break;
+ }
+
+ peer_params = g_try_malloc0(sizeof(GSupplicantPeerParams));
+ if (!peer_params)
+ return -ENOMEM;
+
+ peer_params->path = g_strdup(g_supplicant_peer_get_path(gs_peer));
+ if (wps_pin)
+ peer_params->wps_pin = g_strdup(wps_pin);
+
+ peer_params->master = connman_peer_service_is_master();
+
+ ret = g_supplicant_interface_p2p_connect(wifi->interface, peer_params,
+ peer_connect_callback, wifi);
+ if (ret == -EINPROGRESS) {
+ wifi->pending_peer = connman_peer_ref(peer);
+ wifi->p2p_connecting = true;
+ } else if (ret < 0) {
+ g_free(peer_params->path);
+ g_free(peer_params->wps_pin);
+ g_free(peer_params);
+ }
+
+ return ret;
+}
+
+static int peer_disconnect(struct connman_peer *peer)
+{
+ struct connman_device *device = connman_peer_get_device(peer);
+ GSupplicantPeerParams peer_params = {};
+ GSupplicantPeer *gs_peer;
+ struct wifi_data *wifi;
+ int ret;
+
+ DBG("peer %p", peer);
+
+ if (!device)
+ return -ENODEV;