1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  * Author: Michael Ring <mail@michael-ring.org>
4  * Copyright (c) 2014 Intel Corporation.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include "mraa_internal.h"
30 #include "arm/raspberry_pi.h"
31 #include "arm/beaglebone.h"
32 #include "arm/banana.h"
33 #include "arm/96boards.h"
34 
35 
36 mraa_platform_t
mraa_arm_platform()37 mraa_arm_platform()
38 {
39     mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
40     size_t len = 100;
41     char* line = malloc(len);
42     FILE* fh = fopen("/proc/cpuinfo", "r");
43 
44     if (fh != NULL) {
45         while (getline(&line, &len, fh) != -1) {
46             if (strncmp(line, "Hardware", 8) == 0) {
47                 if (strstr(line, "BCM2708")) {
48                     platform_type = MRAA_RASPBERRY_PI;
49                 }
50                 if (strstr(line, "BCM2709")) {
51                     platform_type = MRAA_RASPBERRY_PI;
52                 }
53                 if (strstr(line, "Generic AM33XX")) {
54                     platform_type = MRAA_BEAGLEBONE;
55                 }
56 		if (strstr(line, "HiKey Development Board")) {
57                     platform_type = MRAA_96BOARDS;
58                 }
59                 if (strstr(line, "sun7i")) {
60                     if (mraa_file_contains("/sys/firmware/devicetree/base/model", "Banana Pro")) {
61                         platform_type = MRAA_BANANA;
62                     }
63                     if (mraa_file_contains("/sys/firmware/devicetree/base/model", "Banana Pi")) {
64                         platform_type = MRAA_BANANA;
65                     }
66                     // For old kernels
67                     if (mraa_file_exist("/sys/class/leds/green:ph24:led1")) {
68                         platform_type = MRAA_BANANA;
69                     }
70                 }
71             }
72 
73         }
74         fclose(fh);
75     }
76     free(line);
77 
78     /* Get compatible string from Device tree for boards that dont have enough info in /proc/cpuinfo */
79     if (platform_type == MRAA_UNKNOWN_PLATFORM) {
80         if (mraa_file_contains("/sys/firmware/devicetree/base/compatible", "qcom,apq8016-sbc"))
81                    platform_type = MRAA_96BOARDS;
82      }
83 
84     switch (platform_type) {
85         case MRAA_RASPBERRY_PI:
86             plat = mraa_raspberry_pi();
87             break;
88         case MRAA_BEAGLEBONE:
89             plat = mraa_beaglebone();
90             break;
91         case MRAA_BANANA:
92             plat = mraa_banana();
93             break;
94         case MRAA_96BOARDS:
95             plat = mraa_96boards();
96             break;
97         default:
98             plat = NULL;
99             syslog(LOG_ERR, "Unknown Platform, currently not supported by MRAA");
100     }
101     return platform_type;
102 }
103