1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/* libs/cutils/cpu_info.c
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project**
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project** Copyright 2007, The Android Open Source Project
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project**
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project** you may not use this file except in compliance with the License.
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project** You may obtain a copy of the License at
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project**
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project**     http://www.apache.org/licenses/LICENSE-2.0
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project**
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project** Unless required by applicable law or agreed to in writing, software
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project** distributed under the License is distributed on an "AS IS" BASIS,
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project** See the License for the specific language governing permissions and
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project** limitations under the License.
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project*/
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <cutils/cpu_info.h>
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project// we cache the serial number here.
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project// this is also used as a fgets() line buffer when we are reading /proc/cpuinfo
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic char serial_number[100] = { 0 };
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectextern const char* get_cpu_serial_number(void)
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (serial_number[0] == 0)
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    {
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        FILE* file;
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        char* chp, *end;
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        char* whitespace;
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        int length;
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        // read serial number from /proc/cpuinfo
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        file = fopen("proc/cpuinfo", "r");
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (! file)
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return NULL;
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        while ((chp = fgets(serial_number, sizeof(serial_number), file)) != NULL)
42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        {
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            // look for something like "Serial          : 999206122a03591c"
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (strncmp(chp, "Serial", 6) != 0)
46dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                continue;
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            chp = strchr(chp, ':');
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (!chp)
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                continue;
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project             // skip colon and whitespace
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            while ( *(++chp) == ' ') {}
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            // truncate trailing whitespace
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            end = chp;
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            while (*end && *end != ' ' && *end != '\t' && *end != '\n' && *end != '\r')
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                ++end;
59dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            *end = 0;
60dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
61dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            whitespace = strchr(chp, ' ');
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (whitespace)
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                *whitespace = 0;
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            whitespace = strchr(chp, '\t');
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (whitespace)
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                *whitespace = 0;
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            whitespace = strchr(chp, '\r');
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (whitespace)
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                *whitespace = 0;
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            whitespace = strchr(chp, '\n');
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (whitespace)
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                *whitespace = 0;
73dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
74dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            // shift serial number to beginning of the buffer
75dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            memmove(serial_number, chp, strlen(chp) + 1);
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fclose(file);
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return (serial_number[0] ? serial_number : NULL);
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
84