public static void LinkActionCallback(Object stateInfo) { Message m = (Message)stateInfo; ConnectionHelper ch = Program.toMihf; ushort transactionID = m.MIHHeader.TransactionID; Payload.TLVIterator it = m.Payload.GetTLVIterator(); ID srcID = new ID(new OctetString(it.Next().Value)); ID dstID = new ID(new OctetString(it.Next().Value)); Link_Action_Request lar = new Link_Action_Request(MIHDeserializer.DeserializeLinkAction(it.Next()), MIHDeserializer.DeserializeTimeInterval(it.Next()), MIHDeserializer.DeserializePoA(it.Next())); Link_Action_Response laresp = new Link_Action_Response(); laresp.Status = STATUS.UNSPECIFIED_FAILURE; laresp.Result = Link_Ac_Result.INCAPABLE; NativeWifi.WlanClient.WlanInterface iface = null; try { iface = Information.GenericInfo.WlanInterfaceInstance; } catch (Exception e) { /*nothing*/ } switch (lar.LinkAction.LinkAcType) { case Link_Ac_Type.LINK_DISCONNECT: ActionsInterface.Action_Disconnect(ref laresp, ref iface); break; case Link_Ac_Type.NONE: Console.WriteLine("No action performed."); laresp.Status = STATUS.SUCCESS; laresp.Result = Link_Ac_Result.SUCCESS; break; case Link_Ac_Type.LINK_POWER_DOWN: ActionsInterface.Action_Power_Down(ref laresp, ref iface); break; case Link_Ac_Type.LINK_POWER_UP: ActionsInterface.Action_Power_Up(ref laresp, ref iface); break; default: throw new InvalidOperationException("Unsupported Operation"); } laresp.ScanResults = new List <Link_Scan_Rsp>(); if (lar.LinkAction.LinkAcAttr.Link_Scan) { ActionsInterface.Action_Scan(laresp, iface, m);//Message is sent inside this branch, after the scan is complete. } else { ch.Send(ResponseBuilders.Link_Action_Response_Builder(srcID, dstID, transactionID, laresp).ByteValue); } }
/// <summary> /// Executes a disconnect on the interface and changes the Status of the Link_Action_Response. /// </summary> /// <param name="laresp">The Link_Action_Response for this action.</param> /// <param name="iface">The interface do disconnect the link from.</param> public static void Action_Disconnect(ref Link_Action_Response laresp, ref NativeWifi.WlanClient.WlanInterface iface) { if (iface != null) { iface.Disconnect(); Console.WriteLine("Disconnected b/c a Disconnect Link Action was received."); laresp.Status = STATUS.SUCCESS; laresp.Result = Link_Ac_Result.SUCCESS; } else { Console.WriteLine("Cannot disconnect a disabled interface."); laresp.Status = STATUS.REJECTED; laresp.Result = Link_Ac_Result.REFUSED; } }
/// <summary> /// Issues a scan order on the interface and processes (and sends) the rest of the message when the scan is completed. /// </summary> /// <param name="laresp">The Link_Action_Response for this action.</param> /// <param name="iface">The interface to scan.</param> /// <param name="m">The message to be processed and sent when the scan is complete.</param> public static void Action_Scan(Link_Action_Response laresp, NativeWifi.WlanClient.WlanInterface iface, Message m) { if (iface != null) { if (!iface.ScanRequested) { iface.Scan(); } iface.ScanRequested = true; Console.Write("Waiting for scan results..."); Information.MiscData.PendingLinkActionResponses.Add(m, laresp); //Message sent at the WlanApi class, when a scan event occurs. } else { Console.WriteLine("Cannot scan a disabled interface."); laresp.Status = STATUS.REJECTED; } }
/// <summary> /// Executes a Power Up (enable interface) on the interface and changes the Status of the Link_Action_Response. /// </summary> /// <param name="laresp">The Link_Action_Response for this action.</param> /// <param name="iface">The interface do disconnect the link from.</param> public static void Action_Power_Up(ref Link_Action_Response laresp, ref NativeWifi.WlanClient.WlanInterface iface) { Console.WriteLine("Enabling interface."); try { using (ManagementObjectSearcher mos = new ManagementObjectSearcher(@"SELECT * FROM Win32_NetworkAdapter WHERE GUID = '" + iface.InterfaceGuid.ToString() + "'")) { ManagementObject objMO = mos.Get().Cast <ManagementObject>().SingleOrDefault(); objMO.InvokeMethod("Enable", null); } laresp.Status = STATUS.SUCCESS; laresp.Result = Link_Ac_Result.SUCCESS; } catch (Exception) { laresp.Status = STATUS.UNSPECIFIED_FAILURE; laresp.Result = Link_Ac_Result.FAILURE; } }