83a31b8662f7e1a2b6f1119bc5acdf6edf29caa8
[contrib/mraa.git] / src / x86 / x86.c
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "mraa_internal.h"
29
30 #include "x86/intel_galileo_rev_d.h"
31 #include "x86/intel_galileo_rev_g.h"
32 #include "x86/intel_edison_fab_c.h"
33 #include "x86/intel_de3815.h"
34 #include "x86/intel_nuc5.h"
35 #include "x86/intel_minnow_byt_compatible.h"
36
37 mraa_platform_t
38 mraa_x86_platform()
39 {
40 #ifndef MRAA_PLATFORM_FORCE
41     mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
42
43     char* line = NULL;
44     // let getline allocate memory for *line
45     size_t len = 0;
46     FILE* fh = fopen("/sys/devices/virtual/dmi/id/board_name", "r");
47     if (fh != NULL) {
48         if (getline(&line, &len, fh) != -1) {
49             if (strncmp(line, "GalileoGen2", 11) == 0) {
50                 platform_type = MRAA_INTEL_GALILEO_GEN2;
51                 plat = mraa_intel_galileo_gen2();
52             } else if (strncmp(line, "BODEGA BAY", 10) == 0) {
53                 platform_type = MRAA_INTEL_EDISON_FAB_C;
54                 plat = mraa_intel_edison_fab_c();
55             } else if (strncmp(line, "SALT BAY", 8) == 0) {
56                 platform_type = MRAA_INTEL_EDISON_FAB_C;
57                 plat = mraa_intel_edison_fab_c();
58             } else if (strncmp(line, "DE3815", 6) == 0) {
59                 platform_type = MRAA_INTEL_DE3815;
60                 plat = mraa_intel_de3815();
61             } else if (strncmp(line, "NUC5i5MYBE", 10) == 0 || strncmp(line, "NUC5i3MYBE", 10) == 0) {
62                 platform_type = MRAA_INTEL_NUC5;
63                 plat = mraa_intel_nuc5();
64             } else if (strncmp(line, "NOTEBOOK", 8) == 0) {
65                 platform_type = MRAA_INTEL_MINNOWBOARD_MAX;
66                 plat = mraa_intel_minnowboard_byt_compatible();
67             } else if (strncasecmp(line, "MinnowBoard MAX", 15) == 0) {
68                 platform_type = MRAA_INTEL_MINNOWBOARD_MAX;
69                 plat = mraa_intel_minnowboard_byt_compatible();
70             } else if (strncasecmp(line, "Galileo", 7) == 0) {
71                 platform_type = MRAA_INTEL_GALILEO_GEN1;
72                 plat = mraa_intel_galileo_rev_d();
73             } else if (strncasecmp(line, "MinnowBoard Compatible", 22) == 0) {
74                 platform_type = MRAA_INTEL_MINNOWBOARD_MAX;
75                 plat = mraa_intel_minnowboard_byt_compatible();
76             } else {
77                 syslog(LOG_ERR, "Platform not supported, not initialising");
78                 platform_type = MRAA_UNKNOWN_PLATFORM;
79             }
80             free(line);
81         }
82         fclose(fh);
83     } else {
84         fh = fopen("/proc/cmdline", "r");
85         if (fh != NULL) {
86             if (getline(&line, &len, fh) != -1) {
87                 if (strstr(line, "sf3gr_mrd_version=P2.0")) {
88                     platform_type = MRAA_INTEL_SOFIA_3GR;
89                     plat = mraa_intel_sofia_3gr();
90                 }
91                 free(line);
92             }
93             fclose(fh);
94         }
95     }
96     return platform_type;
97 #else
98     #if defined(xMRAA_INTEL_GALILEO_GEN2)
99     plat = mraa_intel_galileo_gen2();
100     #elif defined(xMRAA_INTEL_EDISON_FAB_C)
101     plat = mraa_intel_edison_fab_c();
102     #elif defined(xMRAA_INTEL_DE3815)
103     plat = mraa_intel_de3815();
104     #elif defined(xMRAA_INTEL_MINNOWBOARD_MAX)
105     plat = mraa_intel_minnowboard_byt_compatible();
106     #elif defined(xMRAA_INTEL_GALILEO_GEN1)
107     plat = mraa_intel_galileo_rev_d();
108     #elif defined(xMRAA_INTEL_NUC5)
109     plat = mraa_intel_nuc5();
110     #elif defined(xMRAA_INTEL_SOFIA_3GR)
111     plat = mraa_intel_sofia_3gr();
112     #else
113         #error "Not using a valid platform value from mraa_platform_t - cannot compile"
114     #endif
115     return MRAA_PLATFORM_FORCE;
116 #endif
117 }