1 // 2 // Copyright (c) 2010-2018 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using System; 9 using Antmicro.Renode.Peripherals.UART; 10 using Antmicro.Renode.Utilities; 11 using Antmicro.Renode.Core; 12 13 namespace Antmicro.Renode.Hooks 14 { 15 public static class UartHooksExtensions 16 { AddCharHook(this IUART uart, Func<byte, bool> predicate, Action<byte> hook)17 public static void AddCharHook(this IUART uart, Func<byte, bool> predicate, Action<byte> hook) 18 { 19 uart.CharReceived += x => 20 { 21 if(predicate(x)) 22 { 23 hook(x); 24 } 25 }; 26 } 27 AddLineHook(this IUART uart, Func<string, bool> predicate, Action<string> hook)28 public static void AddLineHook(this IUART uart, Func<string, bool> predicate, Action<string> hook) 29 { 30 var currentLine = string.Empty; 31 uart.CharReceived += x => 32 { 33 if((x == 10 || x == 13)) 34 { 35 if(predicate(currentLine)) 36 { 37 hook(currentLine); 38 } 39 currentLine = string.Empty; 40 return; 41 } 42 currentLine += (char)x; 43 }; 44 } 45 AddLineHook(this IUART uart, [AutoParameter] IMachine machine, string contains, string pythonScript)46 public static void AddLineHook(this IUART uart, [AutoParameter] IMachine machine, string contains, string pythonScript) 47 { 48 var engine = new UartPythonEngine(machine, uart, pythonScript); 49 uart.AddLineHook(x => x.Contains(contains), engine.Hook); 50 } 51 } 52 } 53 54