aboutsummaryrefslogtreecommitdiffstats
path: root/android/hw-control.c
blob: 681e85b80defd1f366ede3c1c31795a939c45548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/

/* this file implements the support of the new 'hardware control'
 * qemud communication channel, which is used by libs/hardware on
 * the system image to communicate with the emulator program for
 * emulating the following:
 *
 *   - power management
 *   - led(s) brightness
 *   - vibrator
 *   - flashlight
 */
#include "android/hw-control.h"
#include "cbuffer.h"
#include "android/qemud.h"
#include "android/utils/misc.h"
#include "android/utils/debug.h"
#include "qemu-char.h"
#include <stdio.h>
#include <string.h>

#define  D(...)  VERBOSE_PRINT(hw_control,__VA_ARGS__)

/* define T_ACTIVE to 1 to debug transport communications */
#define  T_ACTIVE  0

#if T_ACTIVE
#define  T(...)  VERBOSE_PRINT(hw_control,__VA_ARGS__)
#else
#define  T(...)   ((void)0)
#endif

static void*                  hw_control_client;
static AndroidHwControlFuncs  hw_control_funcs;

#define  BUFFER_SIZE  512

typedef struct {
    CharDriverState*  cs;
    int               overflow;
    int               wanted;
    CBuffer           input[1];
    char              input_0[ BUFFER_SIZE ];
    /* note: 1 more byte to zero-terminate the query */
    char              query[ BUFFER_SIZE+1 ];
} HwControl;

/* forward */
static void  hw_control_do_query( HwControl*  h,
                                  uint8_t*    query,
                                  int         querylen );

static void
hw_control_init( HwControl*  h, CharDriverState*  cs )
{
    h->cs       = cs;
    h->overflow = 0;
    h->wanted   = 0;
    cbuffer_reset( h->input,  h->input_0,  sizeof h->input_0 );
}

static int
hw_control_can_read( void*  _hw )
{
    HwControl*  h = _hw;
    return cbuffer_write_avail( h->input );
}

static void
hw_control_read( void*  _hw, const uint8_t*  data, int  len )
{
    HwControl*  h     = _hw;
    CBuffer*    input = h->input;

    T("%s: %4d '%.*s'", __FUNCTION__, len, len, data);

    cbuffer_write( input, data, len );

    while ( input->count > 0 )
    {
        /* skip over unwanted data, if any */
        while (h->overflow > 0) {
            uint8_t*  dummy;
            int       avail = cbuffer_read_peek( input, &dummy );

            if (avail == 0)
                return;

            if (avail > h->overflow)
                avail = h->overflow;

            cbuffer_read_step( input, avail );
            h->overflow -= avail;
        }

        /* all incoming messages are made of a 4-byte hexchar sequence giving */
        /* the length of the following payload                                */
        if (h->wanted == 0)
        {
            char  header[4];
            int   len;

            if (input->count < 4)
                return;

            cbuffer_read( input, header, 4 );
            len = hex2int( (uint8_t*)header, 4 );
            if (len >= 0) {
                /* if the message is too long, skip it */
                if (len > input->size) {
                    T("%s: skipping oversized message (%d > %d)",
                    __FUNCTION__, len, input->size);
                    h->overflow = len;
                } else {
                    T("%s: waiting for %d bytes", __FUNCTION__, len);
                    h->wanted = len;
                }
            }
        }
        else
        {
            if (input->count < h->wanted)
                break;

            cbuffer_read( input, h->query, h->wanted );
            h->query[h->wanted] = 0;
            hw_control_do_query( h, (uint8_t*)h->query, h->wanted );
            h->wanted = 0;
        }
    }
}


static uint8_t*
if_starts_with( uint8_t*  buf, int buflen, const char*  prefix )
{
    int  prefixlen = strlen(prefix);

    if (buflen < prefixlen || memcmp(buf, prefix, prefixlen))
        return NULL;

    return (uint8_t*)buf + prefixlen;
}


static void
hw_control_do_query( HwControl*  h,
                     uint8_t*    query,
                     int         querylen )
{
    uint8_t*   q;

    D("%s: query %4d '%.*s'", __FUNCTION__, querylen, querylen, query );

    q = if_starts_with( query, querylen, "power:light:brightness:" );
    if (q != NULL) {
        if (hw_control_funcs.light_brightness) {
            char*  qq = strchr((const char*)q, ':');
            int    value;
            if (qq == NULL) {
                D("%s: badly formatted", __FUNCTION__ );
                return;
            }
            *qq++ = 0;
            value = atoi(qq);
            hw_control_funcs.light_brightness( hw_control_client, (char*)q, value );
        }
        return;
    }
}


void
android_hw_control_init( void*  opaque, const AndroidHwControlFuncs*  funcs )
{
    static CharDriverState*   hw_control_cs;
    static HwControl          hwstate[1];

    if (hw_control_cs == NULL) {
        CharDriverState*  cs;
        if ( android_qemud_get_channel( ANDROID_QEMUD_CONTROL, &cs ) < 0 ) {
            derror( "could not create hardware control charpipe" );
            exit(1);
        }

        hw_control_cs = cs;
        hw_control_init( hwstate, cs );
        qemu_chr_add_handlers( cs, hw_control_can_read, hw_control_read, NULL, hwstate );

        D("%s: hw-control char pipe initialized", __FUNCTION__);
    }
    hw_control_client = opaque;
    hw_control_funcs  = funcs[0];
}