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.Linq;
9 using Antmicro.Renode.Core;
10 using System.IO;
11 using System.Collections.Generic;
12 using System;
13 
14 
15 namespace Antmicro.Renode.Core
16 {
17     public static class PlatformsProvider
18     {
19         private const string Name = "name";
20         private const string ScriptPath = "scriptpath";
21         private const string IconResource = "icon";
22         private const string HasFlash = "hasflash";
23         private const string HasPendrive = "haspendrive";
24         private static readonly Platform[] platforms;
25 
IsPlatformAvailable(string name)26         public static bool IsPlatformAvailable(string name)
27         {
28             return platforms.Any(p => p.Name == name);
29         }
30 
GetPlatformByName(string name)31         public static Platform GetPlatformByName(string name)
32         {
33             return platforms.SingleOrDefault(p => p.Name == name);
34         }
35 
GetAvailablePlatforms()36         public static Platform[] GetAvailablePlatforms()
37         {
38             return platforms;
39         }
40 
LoadPlatforms()41         private static Platform[] LoadPlatforms()
42         {
43             var platformList = new List<Platform>();
44             string scriptsPath = Directory.GetCurrentDirectory() + "/platforms/boards";
45 
46             foreach(var fileName in Directory.EnumerateFiles(scriptsPath))
47             {
48                 var tagParser = new PropertyTagParser(File.ReadAllLines(fileName));
49                 var platform = new Platform();
50                 Tuple<string, string> tag;
51 
52                 while((tag = tagParser.GetNextTag()) != null)
53                 {
54                     bool val;
55                     switch(tag.Item1.ToLower())
56                     {
57                     case Name:
58                         platform.Name = tag.Item2;
59                         break;
60                     case IconResource:
61                         platform.IconResource = tag.Item2;
62                         break;
63                     case HasPendrive:
64                         if(!bool.TryParse(tag.Item2, out val))
65                         {
66                             break;
67                         }
68                         platform.HasPendrive = val;
69                         break;
70                     case HasFlash:
71                         if(!bool.TryParse(tag.Item2, out val))
72                         {
73                             break;
74                         }
75                         platform.HasFlash = val;
76                         break;
77                     }
78                 }
79 
80                 if(string.IsNullOrEmpty(platform.Name))
81                 {
82                     continue;
83                 }
84 
85                 platform.ScriptPath = fileName;
86                 platformList.Add(platform);
87             }
88 
89             return platformList.ToArray<Platform>();
90         }
91 
PlatformsProvider()92         static PlatformsProvider()
93         {
94             platforms = LoadPlatforms();
95         }
96     }
97 }
98 
99