2014-03-23 07:21:22 +05:30
// Host detection.
2014-03-03 00:51:23 +05:30
package embd
2014-02-27 04:24:53 +05:30
import (
"fmt"
2014-09-09 15:57:27 +01:00
"io/ioutil"
2014-02-27 04:24:53 +05:30
"os/exec"
"strconv"
"strings"
)
2014-03-23 07:21:22 +05:30
// The Host type represents all the supported host types.
2014-03-24 03:04:58 +05:30
type Host string
2014-02-27 04:24:53 +05:30
const (
2014-03-23 07:21:22 +05:30
// HostNull reprents a null host.
2014-03-24 03:04:58 +05:30
HostNull Host = ""
2014-03-23 07:21:22 +05:30
// HostRPi represents the RaspberryPi.
2014-03-24 03:04:58 +05:30
HostRPi = "Raspberry Pi"
2014-03-23 07:21:22 +05:30
// HostBBB represents the BeagleBone Black.
2014-03-24 03:04:58 +05:30
HostBBB = "BeagleBone Black"
// HostGalileo represents the Intel Galileo board.
HostGalileo = "Intel Galileo"
2014-03-23 07:21:22 +05:30
// HostCubieTruck represents the Cubie Truck.
2014-03-24 03:04:58 +05:30
HostCubieTruck = "CubieTruck"
2014-03-23 07:21:22 +05:30
2014-05-22 02:56:50 +05:30
// HostRadxa represents the Radxa board.
2014-03-24 03:04:58 +05:30
HostRadxa = "Radxa"
2014-02-27 04:24:53 +05:30
)
func execOutput ( name string , arg ... string ) ( output string , err error ) {
var out [ ] byte
if out , err = exec . Command ( name , arg ... ) . Output ( ) ; err != nil {
return
}
2014-03-02 19:39:04 +05:30
output = strings . TrimSpace ( string ( out ) )
2014-02-27 04:24:53 +05:30
return
}
func nodeName ( ) ( string , error ) {
return execOutput ( "uname" , "-n" )
}
2014-03-02 01:54:01 +05:30
func parseVersion ( str string ) ( major , minor , patch int , err error ) {
2014-05-15 23:48:53 -07:00
versionNumber := strings . Split ( str , "-" )
parts := strings . Split ( versionNumber [ 0 ] , "." )
2014-03-02 01:54:01 +05:30
len := len ( parts )
2014-02-27 04:24:53 +05:30
if major , err = strconv . Atoi ( parts [ 0 ] ) ; err != nil {
2014-03-02 01:54:01 +05:30
return 0 , 0 , 0 , err
2014-02-27 04:24:53 +05:30
}
if minor , err = strconv . Atoi ( parts [ 1 ] ) ; err != nil {
2014-03-02 01:54:01 +05:30
return 0 , 0 , 0 , err
2014-02-27 04:24:53 +05:30
}
2014-03-02 01:54:01 +05:30
if len > 2 {
part := parts [ 2 ]
part = strings . TrimSuffix ( part , "+" )
if patch , err = strconv . Atoi ( part ) ; err != nil {
return 0 , 0 , 0 , err
}
2014-02-27 04:24:53 +05:30
}
2014-03-02 01:54:01 +05:30
return major , minor , patch , err
}
func kernelVersion ( ) ( major , minor , patch int , err error ) {
output , err := execOutput ( "uname" , "-r" )
if err != nil {
return 0 , 0 , 0 , err
}
return parseVersion ( output )
2014-02-27 04:24:53 +05:30
}
2014-09-09 15:57:27 +01:00
func getPiRevision ( ) ( int , error ) {
//default return code of a rev2 board
cpuinfo , err := ioutil . ReadFile ( "/proc/cpuinfo" )
if err != nil {
return 4 , err
}
for _ , line := range strings . Split ( string ( cpuinfo ) , "\n" ) {
fields := strings . Fields ( line )
if len ( fields ) > 0 && fields [ 0 ] == "Revision" {
rev , err := strconv . ParseInt ( fields [ 2 ] , 16 , 8 )
return int ( rev ) , err
}
}
return 4 , nil
}
2014-03-23 07:21:22 +05:30
// DetectHost returns the detected host and its revision number.
2014-03-03 00:51:23 +05:30
func DetectHost ( ) ( Host , int , error ) {
2014-02-27 04:24:53 +05:30
major , minor , patch , err := kernelVersion ( )
if err != nil {
2014-03-03 00:51:23 +05:30
return HostNull , 0 , err
2014-02-27 04:24:53 +05:30
}
if major < 3 || ( major == 3 && minor < 8 ) {
2014-03-23 15:48:09 +05:30
return HostNull , 0 , fmt . Errorf ( "embd: linux kernel versions lower than 3.8 are not supported. you have %v.%v.%v" , major , minor , patch )
2014-02-27 04:24:53 +05:30
}
node , err := nodeName ( )
if err != nil {
2014-03-03 00:51:23 +05:30
return HostNull , 0 , err
2014-02-27 04:24:53 +05:30
}
2014-03-02 01:21:52 +05:30
var host Host
var rev int
2014-02-27 04:24:53 +05:30
switch node {
case "raspberrypi" :
2014-03-03 00:51:23 +05:30
host = HostRPi
2014-09-09 15:57:27 +01:00
rev , _ = getPiRevision ( )
2014-02-27 04:24:53 +05:30
case "beaglebone" :
2014-03-03 00:51:23 +05:30
host = HostBBB
2014-02-27 04:24:53 +05:30
default :
2014-03-23 15:48:09 +05:30
return HostNull , 0 , fmt . Errorf ( "embd: your host %q is not supported at this moment. please request support at https://github.com/kidoman/embd/issues" , node )
2014-02-27 04:24:53 +05:30
}
2014-03-02 01:21:52 +05:30
return host , rev , nil
2014-02-27 04:24:53 +05:30
}