source: svn/newcon3bcm2_21bu/nexus/app/bapp_util.c

Last change on this file was 76, checked in by megakiss, 10 years ago

1W 대기전력을 만족시키기 위하여 POWEROFF시 튜너를 Standby 상태로 함

  • Property svn:executable set to *
File size: 14.1 KB
Line 
1/******************************************************************************
2 *    (c)2008-2009 Broadcom Corporation
3 *
4 * This program is the proprietary software of Broadcom Corporation and/or its licensors,
5 * and may only be used, duplicated, modified or distributed pursuant to the terms and
6 * conditions of a separate, written license agreement executed between you and Broadcom
7 * (an "Authorized License").  Except as set forth in an Authorized License, Broadcom grants
8 * no license (express or implied), right to use, or waiver of any kind with respect to the
9 * Software, and Broadcom expressly reserves all rights in and to the Software and all
10 * intellectual property rights therein.  IF YOU HAVE NO AUTHORIZED LICENSE, THEN YOU
11 * HAVE NO RIGHT TO USE THIS SOFTWARE IN ANY WAY, AND SHOULD IMMEDIATELY
12 * NOTIFY BROADCOM AND DISCONTINUE ALL USE OF THE SOFTWARE.
13 *
14 * Except as expressly set forth in the Authorized License,
15 *
16 * 1.     This program, including its structure, sequence and organization, constitutes the valuable trade
17 * secrets of Broadcom, and you shall use all reasonable efforts to protect the confidentiality thereof,
18 * and to use this information only in connection with your use of Broadcom integrated circuit products.
19 *
20 * 2.     TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
21 * AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES, REPRESENTATIONS OR
22 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
23 * THE SOFTWARE.  BROADCOM SPECIFICALLY DISCLAIMS ANY AND ALL IMPLIED WARRANTIES
24 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE,
25 * LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION
26 * OR CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING OUT OF
27 * USE OR PERFORMANCE OF THE SOFTWARE.
28 *
29 * 3.     TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM OR ITS
30 * LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL, INDIRECT, OR
31 * EXEMPLARY DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY WAY RELATING TO YOUR
32 * USE OF OR INABILITY TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF THE AMOUNT
34 * ACTUALLY PAID FOR THE SOFTWARE ITSELF OR U.S. $1, WHICHEVER IS GREATER. THESE
35 * LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF ESSENTIAL PURPOSE OF
36 * ANY LIMITED REMEDY.
37 *
38 * $brcm_Workfile:  $
39 * $brcm_Revision:  $
40 * $brcm_Date:  $
41 *
42 * Module Description:
43 *
44 * Revision History:
45 *
46 * Created: 09/28/2009 by Jeff Fisher
47 *
48 * $brcm_Log:  $
49 *
50 *
51 *****************************************************************************/
52#include "bapp_util.h"
53#include <string.h>
54#include <sys/time.h>
55BDBG_MODULE(bapp_util);         /* Register software module with debug interface */
56/*
57Summary:
58        Utility IO function.
59Description:
60        Utility IO function to be used for pseudo IO.
61*/
62
63int bin_read( void *buffer, int size, int count, void *fp )
64{
65        bin_read_t *p_br = (bin_read_t*)fp;
66        size = size * count;
67        if (p_br->cnt + size > p_br->size)
68        {
69                BDBG_MSG(("Requesting more bytes than available (cnt = %d, size = %d,total = %d)\n",
70                                  p_br->cnt,size,p_br->size));
71                size = p_br->size - p_br->cnt;
72        }
73
74        memcpy(buffer,&p_br->data[p_br->cnt],size);
75        p_br->cnt += size;
76        return size;
77}
78/*
79Summary:
80        Utility IO function.
81Description:
82        Utility IO function to be used for pseudo IO.
83*/
84
85unsigned int bin_tell(void *fp )
86{
87        bin_read_t *p_br = (bin_read_t*)fp;
88
89        return p_br->cnt;
90}
91/*
92Summary:
93        Utility IO function.
94Description:
95        Utility IO function to be used for pseudo IO.
96*/
97
98int bin_set(void *fp, int offset, int whence )
99{
100        bin_read_t *p_br = (bin_read_t*)fp;
101
102        if ((whence != 0) || ((unsigned int)offset > p_br->size))
103                return -1;
104
105        p_br->cnt = offset;
106        return 0;
107}
108#if 0
109/**
110Summary:
111        Get ticks used to establish time deltas in the applicaiton.
112**/
113
114unsigned int bapp_util_get_ticks(void)
115{
116        struct timeval tv;
117
118        GETTIMEOFDAY(&tv);
119
120        return((tv.tv_sec * BAPP_TICKS_PER_SECOND) + (tv.tv_usec * BAPP_TICKS_PER_SECOND) / 1000000);
121}
122#endif
123
124#define isaf(c)         ((c) >= 'a' && (c) <= 'f')
125#define isAF(c)         ((c) >= 'A' && (c) <= 'Z')
126#define isdigit(c)      ((c) >= '0' && (c) <= '9')
127
128/**
129Summary:
130        Convert string to int value
131**/
132int bapp_util_atoi(unsigned char *str_p, int bytes)
133{
134        int i, value = 0;
135        unsigned char c, *p;
136
137        p = str_p;
138
139        for (i = 0; i < bytes; i++)
140        {
141                value <<= 4;
142                c = *p++;
143                if (isdigit(c))
144                        value |= (c - '0');
145                else if (isaf(c))
146                        value |= (c - 'a') + 10;
147                else if (isAF(c))
148                        value |= (c - 'A') + 10;
149                else
150                        return 0;
151        }
152        return value;
153}
154
155/****************************************************************
156 * timeval_subtract
157 *
158 * INPUTS:      x,y - subtract the timevals
159 * OUTPUTS:     result - result of x,y timeval subtract
160 * RETURNS:     none
161 * DESCRITPION: Subtract the `struct timeval' values X and Y,
162 *              storing the result in RESULT.
163 *              Return 1 if the difference is negative, otherwise 0
164 ****************************************************************/
165
166int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
167{
168        /* Perform the carry for the later subtraction by updating y. */
169        if (x->tv_usec < y->tv_usec)
170        {
171                int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
172                y->tv_usec -= 1000000 * nsec;
173                y->tv_sec += nsec;
174        }
175        if (x->tv_usec - y->tv_usec > 1000000)
176        {
177                int nsec = (x->tv_usec - y->tv_usec) / 1000000;
178                y->tv_usec += 1000000 * nsec;
179                y->tv_sec -= nsec;
180        }
181
182        /* Compute the time remaining to wait.
183           tv_usec is certainly positive. */
184        result->tv_sec = x->tv_sec - y->tv_sec;
185        result->tv_usec = x->tv_usec - y->tv_usec;
186
187        /* Return 1 if result is negative. */
188        return x->tv_sec < y->tv_sec;
189}
190#define UNIT_ADDR_LEN 20
191#define HW_ADDR_BYTES 5
192static unsigned char s_unit_addr_str[UNIT_ADDR_LEN];
193static unsigned char s_hw_addr_str[HW_ADDR_BYTES];
194
195static const unsigned char crc8_table[] = 
196{
197          0, 155, 173,  54, 193,  90, 108, 247,  25, 130, 180,  47, 216,  67, 117, 238,
198         50, 169, 159,   4, 243, 104,  94, 197,  43, 176, 134,  29, 234, 113,  71, 220,
199        100, 255, 201,  82, 165,  62,   8, 147, 125, 230, 208,  75, 188,  39,  17, 138,
200         86, 205, 251,  96, 151,  12,  58, 161,  79, 212, 226, 121, 142,  21,  35, 184,
201        200,  83, 101, 254,   9, 146, 164,  63, 209,  74, 124, 231,  16, 139, 189,  38,
202        250,  97,  87, 204,  59, 160, 150,  13, 227, 120,  78, 213,  34, 185, 143,  20,
203        172,  55,   1, 154, 109, 246, 192,  91, 181,  46,  24, 131, 116, 239, 217,  66,
204        158,   5,  51, 168,  95, 196, 242, 105, 135,  28,  42, 177,  70, 221, 235, 112,
205         11, 144, 166,  61, 202,  81, 103, 252,  18, 137, 191,  36, 211,  72, 126, 229,
206         57, 162, 148,  15, 248,  99,  85, 206,  32, 187, 141,  22, 225, 122,  76, 215,
207        111, 244, 194,  89, 174,  53,   3, 152, 118, 237, 219,  64, 183,  44,  26, 129,
208         93, 198, 240, 107, 156,   7,  49, 170,  68, 223, 233, 114, 133,  30,  40, 179,
209        195,  88, 110, 245,   2, 153, 175,  52, 218,  65, 119, 236,  27, 128, 182,  45,
210        241, 106,  92, 199,  48, 171, 157,   6, 232, 115,  69, 222,  41, 178, 132,  31,
211        167,  60,  10, 145, 102, 253, 203,  80, 190,  37,  19, 136, 127, 228, 210,  73,
212        149,  14,  56, 163,  84, 207, 249,  98, 140,  23,  33, 186,  77, 214, 224, 123,
213};
214
215/*
216Summary:
217        Return the mac address. Can return false if chip does not support HW address.
218        An address to an array of at least 6 bytes must be provided.
219*/
220bool get_mac_address(unsigned char *hw_address)
221{
222        unsigned int upper,lower;
223#if 1 /* Test value from UDTA CCAD spec with Radner stream captures */
224        hw_address[0] = 0x12;
225        hw_address[1] = 0xBE;
226        hw_address[2] = 0x20;
227        hw_address[3] = 0x00;
228        hw_address[4] = 0x1C;
229        hw_address[5] = 0xC3;
230    return true;
231#endif
232}
233
234/*
235Summary:
236        Return the 40 bit hardware address. Can return false if chip does not support HW address.
237        An address to an array of at least 5 bytes must be provided.
238*/
239#define SWAP_INT(x)   ((((unsigned int)x & 0xFF) << 24) |(((unsigned int)x & 0xFF00) << 8) |(((unsigned int)x & 0xFF0000) >> 8) | (((unsigned int)x & 0xFF000000) >> 24))
240bool get_hw_address(unsigned char *hw_address)
241{
242        unsigned int upper,lower;
243#if 1 /* Test value from UDTA CCAD spec */
244        hw_address[0] = 0xc3;
245        hw_address[1] = 0x1c;
246        hw_address[2] = 0x00;
247        hw_address[3] = 0x20;
248        hw_address[4] = 0xBE;
249
250/* c3:1c:00:20:be */
251#endif
252        return true;
253}
254
255/*
256Summary:
257        Get the current utc time in seconds.
258        Return non-zero on failure.
259*/
260bool s_have_time = false;
261unsigned int s_system_time = 0;
262unsigned int s_system_offset = 0;
263unsigned int s_dst_entry = 0;
264unsigned int s_dst_exit = 0;
265unsigned int s_dst_delta = 0;
266bool get_utc_time(unsigned int *time )
267{
268        struct timeval tv;
269
270        if (!s_have_time)
271                return false;
272
273        GETTIMEOFDAY(&tv);
274
275        *time = s_system_time + (tv.tv_sec - s_system_offset);
276
277        return true;
278}
279
280/*
281Summary:
282        Get the current UTC time, local offset and dst flag.
283        Returns non-zero when it is not possible to determine local time.
284        Currently this function uses XDS local offset over one obtained from the TSIDs
285       
286*/
287bool get_local_time(unsigned int *utc_secs )    /* Current UTC time in seconds adjusted to local time */
288{
289        if (!get_utc_time(utc_secs))
290                return false;
291
292        if ((*utc_secs >= s_dst_entry) && (*utc_secs <= s_dst_exit))
293        {
294                if (s_dst_delta == 0)
295                        *utc_secs += 3600;
296                else
297                        *utc_secs += s_dst_delta * 60;
298        }
299        return true;
300}
301
302/*
303Summary:
304        8-bit CRC value based on  x^^8 + x^^7 + x^^4 + x^^3 + x + 1 polynomial used
305        to calculate CRC on 40-bit hardware address.  Defined in Appendix F of CCAD UDTA
306        specification.
307*/
308
309static unsigned char calc_crc8_ua40(unsigned char *hw_address)
310{
311    unsigned char crc8;
312        int i;
313
314    crc8 = 0xFF;        /* Start with all 1s */
315    for (i = 0; i < HW_ADDR_BYTES; i++) 
316        {
317        crc8 = crc8_table[crc8 ^ *hw_address];
318        hw_address++;
319    }
320    return crc8;
321}
322
323/*
324Summary:
325        Return a null terminated string form of the hw_address (40 bit chip ID)
326       
327*/
328
329char *get_unit_address_str()
330{
331        unsigned int lw,i;
332        unsigned char crc8;
333        unsigned char tmp_hw_addr_str[HW_ADDR_BYTES];
334       
335        get_hw_address(s_hw_addr_str);
336       
337        lw = ((uint32_t)s_hw_addr_str[3] << 24) | ((uint32_t)s_hw_addr_str[2] << 16) | ((uint32_t)s_hw_addr_str[1] << 8) | ((uint32_t)s_hw_addr_str[0]);
338        /* memcpy(&lw,s_hw_addr_str,sizeof(lw)); */
339   
340        bapp_util_memset(s_unit_addr_str,0,UNIT_ADDR_LEN);
341
342    snprintf(s_unit_addr_str,UNIT_ADDR_LEN,"%03d-%010u",s_hw_addr_str[4],lw);
343   
344        /* Add - at dash in the middle of the 10 digit number */
345        s_unit_addr_str[15] = s_unit_addr_str[14];
346        s_unit_addr_str[14] = s_unit_addr_str[13];
347        s_unit_addr_str[13] = s_unit_addr_str[12];
348        s_unit_addr_str[12] = s_unit_addr_str[11];
349        s_unit_addr_str[11] = s_unit_addr_str[10];
350        s_unit_addr_str[10] = s_unit_addr_str[9];
351    s_unit_addr_str[9] = '-';
352
353    /* figure check digits and append */
354    crc8 = 0;
355    if ((s_hw_addr_str[0] == 0) && (s_hw_addr_str[1] == 0) && (s_hw_addr_str[2] == 0) && (s_hw_addr_str[3] == 0) && (s_hw_addr_str[4] == 0)) 
356                crc8 = 0;
357        else
358        {
359                /* Swap order */
360                for (i = 0; i < HW_ADDR_BYTES; i++) 
361                        tmp_hw_addr_str[i] = s_hw_addr_str[4-i];
362        crc8 = calc_crc8_ua40(tmp_hw_addr_str);
363        }
364    snprintf(&s_unit_addr_str[15],5,"-%03d",crc8);
365
366    return s_unit_addr_str;
367}
368
369
370/*
371Summary:
372    Same as text box but with shadow.
373Description:
374    Same as text box but with shadow.
375*/
376#define TEXT_SHADDOW
377int text_box_shadow(bgfx_surf_t *p_surf,       /* bgfx surface */
378             bgfx_font_t *font,                 /* bgfx font */
379            uint16_t x,                         /* x location in pixels */
380            uint16_t y,                         /* y location in pixels */
381            uint16_t width,                     /* width in pixels */
382            uint16_t height,                    /* height in pixels */ 
383            unsigned int *msg,                  /* UNI string */
384            unsigned int msg_len,               /* Number of characters in the msg */
385            bgfx_pixel c_f,                     /* palette index for forground text*/
386            bgfx_pixel c_b,                     /* palette index for background text */
387            int shadow_offset,                 /* offset from xy to place shadow (can be negative) */
388            int line_spacing                    /* number of vertical pixels between lines */
389            )
390{
391#ifdef TEXT_SHADDOW
392    text_box(p_surf,font,x + shadow_offset,y + shadow_offset,width,height,msg,msg_len,c_b,line_spacing);
393#endif
394    text_box(p_surf,font,x,y,width,height,msg,msg_len,c_f,line_spacing);
395    return 0;
396}
397/*
398Summary:
399    Perform a "hard" chip reset if possible.
400*/
401void chip_reset(void)
402{
403}
404
405/*
406Summary:
407        Calculate the UTC time offset from Jan 6th 1980. 
408        NOTE:  This function only works for times after Jan 6th, 1980
409       
410*/
411#ifndef LINUX
412        /* Jan 6, 1980 => Sunday */
413        #define START_DAY       0
414        #define START_YEAR      1980
415#else
416        /* Jan 1, 1970 => Thursday */
417        #define START_DAY       3
418        #define START_YEAR      1970
419#endif
420
421/* Days/month for non-leap year */
422const unsigned char s_days_per_mon[] = 
423{
424        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
425        31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
426};
427
428#define IS_LEAP_YEAR(y) ( !((y + START_YEAR) % 4) && ( ((y + START_YEAR) % 100) || !((y + START_YEAR) % 400) ) )
429void utctime(unsigned int secs,struct utc_tm *p_tm)
430{
431        unsigned int yday,mon,t_val;
432        unsigned char *p_dpm = (unsigned char*)s_days_per_mon;
433
434        memset(p_tm,0,sizeof(struct utc_tm));
435
436        /* seconds */
437        p_tm->tm_sec = secs % 60;
438        t_val = secs / 60;  /* minutes */
439
440        /* minutes */
441        p_tm->tm_min = t_val % 60;
442        t_val /= 60; /* hours */
443
444        /* hours */
445        p_tm->tm_hour = t_val % 24;
446        t_val /= 24;
447
448        /* day of week */
449    p_tm->tm_wday = t_val % 7;
450    p_tm->tm_wday = (t_val - START_DAY) % 7;
451
452        /* offset value to start of the year */
453#ifndef LINUX
454        t_val += 5;
455#endif
456        /* year */
457    p_tm->tm_yday = t_val;
458    p_tm->tm_year = 0;
459
460    /* day of current year */
461    while ((IS_LEAP_YEAR(p_tm->tm_year) && (p_tm->tm_yday > 365)) ||
462            (!IS_LEAP_YEAR(p_tm->tm_year) && (p_tm->tm_yday > 364)))
463    {
464        if (IS_LEAP_YEAR(p_tm->tm_year))
465            p_tm->tm_yday -= 366;
466        else
467            p_tm->tm_yday -= 365;
468        p_tm->tm_year++;
469    }
470
471        if (IS_LEAP_YEAR(p_tm->tm_year))
472                p_dpm += 12;
473
474        yday = p_tm->tm_yday + 1;
475        mon = 0;
476    while(yday > p_dpm[mon])
477    {
478        yday -= p_dpm[mon];
479                mon++;
480        }
481
482        /* month */
483        p_tm->tm_mon = mon;
484
485        /* day of month */
486        p_tm->tm_mday = yday;
487
488}
489
490
Note: See TracBrowser for help on using the repository browser.