/****************************************************************************** * (c)2008-2009 Broadcom Corporation * * This program is the proprietary software of Broadcom Corporation and/or its licensors, * and may only be used, duplicated, modified or distributed pursuant to the terms and * conditions of a separate, written license agreement executed between you and Broadcom * (an "Authorized License"). Except as set forth in an Authorized License, Broadcom grants * no license (express or implied), right to use, or waiver of any kind with respect to the * Software, and Broadcom expressly reserves all rights in and to the Software and all * intellectual property rights therein. IF YOU HAVE NO AUTHORIZED LICENSE, THEN YOU * HAVE NO RIGHT TO USE THIS SOFTWARE IN ANY WAY, AND SHOULD IMMEDIATELY * NOTIFY BROADCOM AND DISCONTINUE ALL USE OF THE SOFTWARE. * * Except as expressly set forth in the Authorized License, * * 1. This program, including its structure, sequence and organization, constitutes the valuable trade * secrets of Broadcom, and you shall use all reasonable efforts to protect the confidentiality thereof, * and to use this information only in connection with your use of Broadcom integrated circuit products. * * 2. TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" * AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES, REPRESENTATIONS OR * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO * THE SOFTWARE. BROADCOM SPECIFICALLY DISCLAIMS ANY AND ALL IMPLIED WARRANTIES * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, * LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION * OR CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING OUT OF * USE OR PERFORMANCE OF THE SOFTWARE. * * 3. TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM OR ITS * LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL, INDIRECT, OR * EXEMPLARY DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY WAY RELATING TO YOUR * USE OF OR INABILITY TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF * THE POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF THE AMOUNT * ACTUALLY PAID FOR THE SOFTWARE ITSELF OR U.S. $1, WHICHEVER IS GREATER. THESE * LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF ESSENTIAL PURPOSE OF * ANY LIMITED REMEDY. * * $brcm_Workfile: $ * $brcm_Revision: $ * $brcm_Date: $ * * Module Description: * * Revision History: * * Created: 09/28/2009 by Jeff Fisher * * $brcm_Log: $ * * *****************************************************************************/ #include "bapp_util.h" #include #include BDBG_MODULE(bapp_util); /* Register software module with debug interface */ /* Summary: Utility IO function. Description: Utility IO function to be used for pseudo IO. */ int bin_read( void *buffer, int size, int count, void *fp ) { bin_read_t *p_br = (bin_read_t*)fp; size = size * count; if (p_br->cnt + size > p_br->size) { BDBG_MSG(("Requesting more bytes than available (cnt = %d, size = %d,total = %d)\n", p_br->cnt,size,p_br->size)); size = p_br->size - p_br->cnt; } memcpy(buffer,&p_br->data[p_br->cnt],size); p_br->cnt += size; return size; } /* Summary: Utility IO function. Description: Utility IO function to be used for pseudo IO. */ unsigned int bin_tell(void *fp ) { bin_read_t *p_br = (bin_read_t*)fp; return p_br->cnt; } /* Summary: Utility IO function. Description: Utility IO function to be used for pseudo IO. */ int bin_set(void *fp, int offset, int whence ) { bin_read_t *p_br = (bin_read_t*)fp; if ((whence != 0) || ((unsigned int)offset > p_br->size)) return -1; p_br->cnt = offset; return 0; } #if 0 /** Summary: Get ticks used to establish time deltas in the applicaiton. **/ unsigned int bapp_util_get_ticks(void) { struct timeval tv; GETTIMEOFDAY(&tv); return((tv.tv_sec * BAPP_TICKS_PER_SECOND) + (tv.tv_usec * BAPP_TICKS_PER_SECOND) / 1000000); } #endif #define isaf(c) ((c) >= 'a' && (c) <= 'f') #define isAF(c) ((c) >= 'A' && (c) <= 'Z') #define isdigit(c) ((c) >= '0' && (c) <= '9') /** Summary: Convert string to int value **/ int bapp_util_atoi(unsigned char *str_p, int bytes) { int i, value = 0; unsigned char c, *p; p = str_p; for (i = 0; i < bytes; i++) { value <<= 4; c = *p++; if (isdigit(c)) value |= (c - '0'); else if (isaf(c)) value |= (c - 'a') + 10; else if (isAF(c)) value |= (c - 'A') + 10; else return 0; } return value; } /**************************************************************** * timeval_subtract * * INPUTS: x,y - subtract the timevals * OUTPUTS: result - result of x,y timeval subtract * RETURNS: none * DESCRITPION: Subtract the `struct timeval' values X and Y, * storing the result in RESULT. * Return 1 if the difference is negative, otherwise 0 ****************************************************************/ int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) { /* Perform the carry for the later subtraction by updating y. */ if (x->tv_usec < y->tv_usec) { int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; y->tv_usec -= 1000000 * nsec; y->tv_sec += nsec; } if (x->tv_usec - y->tv_usec > 1000000) { int nsec = (x->tv_usec - y->tv_usec) / 1000000; y->tv_usec += 1000000 * nsec; y->tv_sec -= nsec; } /* Compute the time remaining to wait. tv_usec is certainly positive. */ result->tv_sec = x->tv_sec - y->tv_sec; result->tv_usec = x->tv_usec - y->tv_usec; /* Return 1 if result is negative. */ return x->tv_sec < y->tv_sec; } #define UNIT_ADDR_LEN 20 #define HW_ADDR_BYTES 5 static unsigned char s_unit_addr_str[UNIT_ADDR_LEN]; static unsigned char s_hw_addr_str[HW_ADDR_BYTES]; static const unsigned char crc8_table[] = { 0, 155, 173, 54, 193, 90, 108, 247, 25, 130, 180, 47, 216, 67, 117, 238, 50, 169, 159, 4, 243, 104, 94, 197, 43, 176, 134, 29, 234, 113, 71, 220, 100, 255, 201, 82, 165, 62, 8, 147, 125, 230, 208, 75, 188, 39, 17, 138, 86, 205, 251, 96, 151, 12, 58, 161, 79, 212, 226, 121, 142, 21, 35, 184, 200, 83, 101, 254, 9, 146, 164, 63, 209, 74, 124, 231, 16, 139, 189, 38, 250, 97, 87, 204, 59, 160, 150, 13, 227, 120, 78, 213, 34, 185, 143, 20, 172, 55, 1, 154, 109, 246, 192, 91, 181, 46, 24, 131, 116, 239, 217, 66, 158, 5, 51, 168, 95, 196, 242, 105, 135, 28, 42, 177, 70, 221, 235, 112, 11, 144, 166, 61, 202, 81, 103, 252, 18, 137, 191, 36, 211, 72, 126, 229, 57, 162, 148, 15, 248, 99, 85, 206, 32, 187, 141, 22, 225, 122, 76, 215, 111, 244, 194, 89, 174, 53, 3, 152, 118, 237, 219, 64, 183, 44, 26, 129, 93, 198, 240, 107, 156, 7, 49, 170, 68, 223, 233, 114, 133, 30, 40, 179, 195, 88, 110, 245, 2, 153, 175, 52, 218, 65, 119, 236, 27, 128, 182, 45, 241, 106, 92, 199, 48, 171, 157, 6, 232, 115, 69, 222, 41, 178, 132, 31, 167, 60, 10, 145, 102, 253, 203, 80, 190, 37, 19, 136, 127, 228, 210, 73, 149, 14, 56, 163, 84, 207, 249, 98, 140, 23, 33, 186, 77, 214, 224, 123, }; /* Summary: Return the mac address. Can return false if chip does not support HW address. An address to an array of at least 6 bytes must be provided. */ bool get_mac_address(unsigned char *hw_address) { unsigned int upper,lower; #if 1 /* Test value from UDTA CCAD spec with Radner stream captures */ hw_address[0] = 0x12; hw_address[1] = 0xBE; hw_address[2] = 0x20; hw_address[3] = 0x00; hw_address[4] = 0x1C; hw_address[5] = 0xC3; return true; #endif } /* Summary: Return the 40 bit hardware address. Can return false if chip does not support HW address. An address to an array of at least 5 bytes must be provided. */ #define SWAP_INT(x) ((((unsigned int)x & 0xFF) << 24) |(((unsigned int)x & 0xFF00) << 8) |(((unsigned int)x & 0xFF0000) >> 8) | (((unsigned int)x & 0xFF000000) >> 24)) bool get_hw_address(unsigned char *hw_address) { unsigned int upper,lower; #if 1 /* Test value from UDTA CCAD spec */ hw_address[0] = 0xc3; hw_address[1] = 0x1c; hw_address[2] = 0x00; hw_address[3] = 0x20; hw_address[4] = 0xBE; /* c3:1c:00:20:be */ #endif return true; } /* Summary: Get the current utc time in seconds. Return non-zero on failure. */ bool s_have_time = false; unsigned int s_system_time = 0; unsigned int s_system_offset = 0; unsigned int s_dst_entry = 0; unsigned int s_dst_exit = 0; unsigned int s_dst_delta = 0; bool get_utc_time(unsigned int *time ) { struct timeval tv; if (!s_have_time) return false; GETTIMEOFDAY(&tv); *time = s_system_time + (tv.tv_sec - s_system_offset); return true; } /* Summary: Get the current UTC time, local offset and dst flag. Returns non-zero when it is not possible to determine local time. Currently this function uses XDS local offset over one obtained from the TSIDs */ bool get_local_time(unsigned int *utc_secs ) /* Current UTC time in seconds adjusted to local time */ { if (!get_utc_time(utc_secs)) return false; if ((*utc_secs >= s_dst_entry) && (*utc_secs <= s_dst_exit)) { if (s_dst_delta == 0) *utc_secs += 3600; else *utc_secs += s_dst_delta * 60; } return true; } /* Summary: 8-bit CRC value based on x^^8 + x^^7 + x^^4 + x^^3 + x + 1 polynomial used to calculate CRC on 40-bit hardware address. Defined in Appendix F of CCAD UDTA specification. */ static unsigned char calc_crc8_ua40(unsigned char *hw_address) { unsigned char crc8; int i; crc8 = 0xFF; /* Start with all 1s */ for (i = 0; i < HW_ADDR_BYTES; i++) { crc8 = crc8_table[crc8 ^ *hw_address]; hw_address++; } return crc8; } /* Summary: Return a null terminated string form of the hw_address (40 bit chip ID) */ char *get_unit_address_str() { unsigned int lw,i; unsigned char crc8; unsigned char tmp_hw_addr_str[HW_ADDR_BYTES]; get_hw_address(s_hw_addr_str); 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]); /* memcpy(&lw,s_hw_addr_str,sizeof(lw)); */ bapp_util_memset(s_unit_addr_str,0,UNIT_ADDR_LEN); snprintf(s_unit_addr_str,UNIT_ADDR_LEN,"%03d-%010u",s_hw_addr_str[4],lw); /* Add - at dash in the middle of the 10 digit number */ s_unit_addr_str[15] = s_unit_addr_str[14]; s_unit_addr_str[14] = s_unit_addr_str[13]; s_unit_addr_str[13] = s_unit_addr_str[12]; s_unit_addr_str[12] = s_unit_addr_str[11]; s_unit_addr_str[11] = s_unit_addr_str[10]; s_unit_addr_str[10] = s_unit_addr_str[9]; s_unit_addr_str[9] = '-'; /* figure check digits and append */ crc8 = 0; 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)) crc8 = 0; else { /* Swap order */ for (i = 0; i < HW_ADDR_BYTES; i++) tmp_hw_addr_str[i] = s_hw_addr_str[4-i]; crc8 = calc_crc8_ua40(tmp_hw_addr_str); } snprintf(&s_unit_addr_str[15],5,"-%03d",crc8); return s_unit_addr_str; } /* Summary: Same as text box but with shadow. Description: Same as text box but with shadow. */ #define TEXT_SHADDOW int text_box_shadow(bgfx_surf_t *p_surf, /* bgfx surface */ bgfx_font_t *font, /* bgfx font */ uint16_t x, /* x location in pixels */ uint16_t y, /* y location in pixels */ uint16_t width, /* width in pixels */ uint16_t height, /* height in pixels */ unsigned int *msg, /* UNI string */ unsigned int msg_len, /* Number of characters in the msg */ bgfx_pixel c_f, /* palette index for forground text*/ bgfx_pixel c_b, /* palette index for background text */ int shadow_offset, /* offset from xy to place shadow (can be negative) */ int line_spacing /* number of vertical pixels between lines */ ) { #ifdef TEXT_SHADDOW text_box(p_surf,font,x + shadow_offset,y + shadow_offset,width,height,msg,msg_len,c_b,line_spacing); #endif text_box(p_surf,font,x,y,width,height,msg,msg_len,c_f,line_spacing); return 0; } /* Summary: Perform a "hard" chip reset if possible. */ void chip_reset(void) { } /* Summary: Calculate the UTC time offset from Jan 6th 1980. NOTE: This function only works for times after Jan 6th, 1980 */ #ifndef LINUX /* Jan 6, 1980 => Sunday */ #define START_DAY 0 #define START_YEAR 1980 #else /* Jan 1, 1970 => Thursday */ #define START_DAY 3 #define START_YEAR 1970 #endif /* Days/month for non-leap year */ const unsigned char s_days_per_mon[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, }; #define IS_LEAP_YEAR(y) ( !((y + START_YEAR) % 4) && ( ((y + START_YEAR) % 100) || !((y + START_YEAR) % 400) ) ) void utctime(unsigned int secs,struct utc_tm *p_tm) { unsigned int yday,mon,t_val; unsigned char *p_dpm = (unsigned char*)s_days_per_mon; memset(p_tm,0,sizeof(struct utc_tm)); /* seconds */ p_tm->tm_sec = secs % 60; t_val = secs / 60; /* minutes */ /* minutes */ p_tm->tm_min = t_val % 60; t_val /= 60; /* hours */ /* hours */ p_tm->tm_hour = t_val % 24; t_val /= 24; /* day of week */ p_tm->tm_wday = t_val % 7; p_tm->tm_wday = (t_val - START_DAY) % 7; /* offset value to start of the year */ #ifndef LINUX t_val += 5; #endif /* year */ p_tm->tm_yday = t_val; p_tm->tm_year = 0; /* day of current year */ while ((IS_LEAP_YEAR(p_tm->tm_year) && (p_tm->tm_yday > 365)) || (!IS_LEAP_YEAR(p_tm->tm_year) && (p_tm->tm_yday > 364))) { if (IS_LEAP_YEAR(p_tm->tm_year)) p_tm->tm_yday -= 366; else p_tm->tm_yday -= 365; p_tm->tm_year++; } if (IS_LEAP_YEAR(p_tm->tm_year)) p_dpm += 12; yday = p_tm->tm_yday + 1; mon = 0; while(yday > p_dpm[mon]) { yday -= p_dpm[mon]; mon++; } /* month */ p_tm->tm_mon = mon; /* day of month */ p_tm->tm_mday = yday; }