arm: added support for all models of Raspbery Pi A/B/A+/B+ & 2B
[contrib/mraa.git] / src / arm / arm.c
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
32 mraa_platform_t
33 mraa_arm_platform()
34 {
35     mraa_platform_t platform_type = MRAA_UNKNOWN_PLATFORM;
36     size_t len = 100;
37     char *line = malloc(len);
38     FILE *fh = fopen("/proc/cpuinfo", "r");
39     if (fh != NULL) {
40         while (getline(&line, &len, fh) != -1) {
41             if (strncmp(line, "Hardware", 8) == 0) {
42                 if (strstr(line, "BCM2708")) {
43                     platform_type = MRAA_RASPBERRY_PI;
44                 }
45                 if (strstr(line, "BCM2709")) {
46                     platform_type = MRAA_RASPBERRY_PI;
47                 }
48             }
49         }
50         fclose(fh);
51     }
52     free(line);
53
54     switch(platform_type) {
55         case MRAA_RASPBERRY_PI:
56             plat = mraa_raspberry_pi();
57             break;
58         default:
59             plat = mraa_raspberry_pi();
60             syslog(LOG_ERR, "Platform not supported, initialising as MRAA_RASPBERRY_PI");
61     }
62     return platform_type;
63 }