/****************************************************************************** * (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 "bapp.h" #include "chan_mgr.h" BDBG_MODULE(bapp_util); /* Register software module with debug interface */ #define MSO_PHONE_LEN (MAX_PHONE_LEN - 1) #define CMD_LEN 3 const char *s_def_phone_str = "1-800-COMCAST (1-800-266-2278)"; const char *s_def_service_str = "Please check the connections and if the problem is not resolved please contact Comcast at %s to restore service. We're sorry for the inconvenience."; #ifdef PROFILE #include "bprofile.h" static bprofile_sys_iface s_sys_iface; static bprofile_entry *s_table = NULL; static int s_num_entrires = 10000; static const char *bapp_util_thread_name(bprofile_thread_id thread) { return (const char*)thread; } /* Summary: Profiling utility start profiling. */ void bapp_util_start_profiling(void) { if (s_table == NULL) { BKNI_Memset(&s_sys_iface,0,sizeof(s_sys_iface)); s_table = BKNI_Malloc(s_num_entrires *sizeof(bprofile_entry)); } BDBG_ASSERT(s_table); bprofile_sys_iface_init(&s_sys_iface); bprofile_start(s_table, s_num_entrires); } /* Summary: Profiling utility stop profiling. */ void bapp_util_stop_profiling(void) { int nentries = bprofile_stop(); BDBG_WRN(("profile nentries %d\n", nentries)); s_sys_iface.thread_from_stack = bos_task_from_stack; s_sys_iface.thread_name = bapp_util_thread_name; s_sys_iface.maxentries = s_num_entrires; s_sys_iface.show_entries = 15; s_sys_iface.split_threads = true; s_sys_iface.substract_overhead = true; s_sys_iface.call_count = true; s_sys_iface.preempt_time = true; s_sys_iface.comma_delimited = true; bprofile_report_flat(s_table, nentries, &s_sys_iface); } #endif /* PROFILE */ /* 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) { b_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 `b_timeval' values X and Y, * storing the result in RESULT. * Return 1 if the difference is negative, otherwise 0 ****************************************************************/ int timeval_subtract (b_timeval *result, b_timeval *x, b_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) { #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) { #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 ) { b_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: scan the mso string looking for a delimiter */ static int scan_mso_str(char *pMSO, char *pStr, int max_str_len, char *pCmd, int max_cmd_len) { int cnt = 0; int acm = 0; char delim; delim = '\0'; /* Search for | delmited message string */ while (pMSO[cnt]) { switch (pMSO[cnt]) { case '|': delim = pMSO[cnt]; break; default: pStr[acm++] = pMSO[cnt]; break; } cnt++; if (delim != '\0') break; if (acm >= max_str_len) return -1; } pStr[acm] = 0; /* Search for command */ acm = 0; delim = '\0'; while (pMSO[cnt] != 0) { switch (pMSO[cnt]) { case '|': /* Empty command */ delim = pMSO[cnt]; if (acm < 2) { pCmd[acm++] = '*'; cnt++; if (pMSO[cnt] != 0) { pCmd[acm++] = '*'; } } else { if (pMSO[cnt] == '|') cnt++; /* This is a field separator so get rid of it also */ } break; case '#': case '&': case '*': pCmd[acm++] = pMSO[cnt]; break; default: return -1; } cnt++; if (delim != '\0') break; if (acm >= max_cmd_len) return -1; } pCmd[acm] = 0; return cnt; } /** Summary: Process the MSO command string and construct the MSO message */ static void process_mso_cmd(char *pMsg, int max_len, char *pDefMsg, char *pCmd) { static char s_tmp_phone_str[MSO_PHONE_LEN+1]; switch (pCmd[0]) { case '#': snprintf(pMsg,max_len,pDefMsg,s_def_phone_str); break; case '&': break; case '\0': case '*': if (strlen(pMsg) > 0) strcpy(s_tmp_phone_str,pMsg); else strcpy(s_tmp_phone_str,s_def_phone_str); snprintf(pMsg,max_len,pDefMsg,s_tmp_phone_str); break; } } /** Summary: parse the mso string looking for a delimiter and return true if successful */ void parse_mso_str(char *pMSO, char *pServiceInterrupted, char *pActivationSupport, int max_len) { int offset,len,ret; char s_service_interrupted_cmd_str[CMD_LEN+1]; char s_activation_support_cmd_str[CMD_LEN+1]; len = strlen(pMSO); offset = 0; ret = scan_mso_str(&(pMSO[offset]),pServiceInterrupted,max_len,s_service_interrupted_cmd_str,CMD_LEN); if (ret < 0) goto ErrExit; else offset += ret; BDBG_MSG(("MSO SI INFO: %s - %s\n",pServiceInterrupted,s_service_interrupted_cmd_str)); process_mso_cmd(pServiceInterrupted, max_len, (char*)s_def_service_str, s_service_interrupted_cmd_str); ret = scan_mso_str(&(pMSO[offset]),pActivationSupport,max_len,s_activation_support_cmd_str, CMD_LEN); if (ret < 0) goto ErrExit; else offset += ret; BDBG_MSG(("MSO SI INFO: %s - %s\n",pActivationSupport,s_activation_support_cmd_str)); process_mso_cmd(pActivationSupport, max_len, (char*)s_def_service_str, s_activation_support_cmd_str); return; ErrExit: BDBG_WRN(("Invalid MSO String: %s\n",pMSO)); pServiceInterrupted[0] = pActivationSupport[0] = 0; process_mso_cmd(pServiceInterrupted, max_len, (char*)s_def_service_str, s_service_interrupted_cmd_str); process_mso_cmd(pActivationSupport, max_len, (char*)s_def_service_str, s_activation_support_cmd_str); return; } #define MAX_TP_ALLOWED 4 static timing_profile_t g_tp[MAX_TP_ALLOWED]; /** Summary: initialize timing structure to use true if OK false otherwise */ bool timing_profile_init(void) { memset(g_tp, 0, sizeof(g_tp)); return true; } /** Summary: uninitialize timing structure used true if OK false otherwise */ bool timing_profile_uninit(void) { memset(g_tp, 0, sizeof(g_tp)); return true; } /** Summary: get timing profile object return NULL if all timing profile resources are used pointer to a timing profile resource to use */ p_timing_profile_t timing_profile_get(char *name) { int i, j; for (i = 0; i < MAX_TP_ALLOWED; i++) { if (!g_tp[i].in_use) { j = strlen(name); if (j > sizeof(g_tp[i].name)) j = sizeof(g_tp[i].name) - 1; strncpy(g_tp[i].name, name, j); g_tp[i].min = 0xffffffff; g_tp[i].in_use = true; return &g_tp[i]; } } return NULL; } /** Summary: reset timing profile object true if OK false otherwise */ bool timing_profile_reset(p_timing_profile_t ptp) { if (!ptp || !ptp->in_use) return false; memset(ptp, 0, sizeof(*ptp)); ptp->in_use = true; return true; } /** Summary: start timing profile true if OK false otherwise */ bool timing_profile_start(p_timing_profile_t ptp) { if (!ptp || !ptp->in_use) return false; ptp->cur_ticks = bos_getticks(); return true; } /** Summary: stop timing profiling true if OK false otherwise */ bool timing_profile_stop(p_timing_profile_t ptp) { if (!ptp || !ptp->in_use) return false; ptp->cnt++; ptp->diff = bos_getticks() - ptp->cur_ticks; /* TODO wrap */ if (ptp->diff > ptp->max) ptp->max = ptp->diff; if (ptp->diff < ptp->min) ptp->min = ptp->diff; ptp->total += ptp->diff; timing_profile_print(ptp); return true; } /** Summary: print timing profile information true if OK false otherwise */ bool timing_profile_print(p_timing_profile_t ptp) { int cnt; if (!ptp || !ptp->in_use) return false; cnt = ptp->cnt; if (cnt < 1) cnt = 1; printf("Profiling %s: min=%u, ave=%u, max=%u, last time=%d\n", ptp->name, TICKS_TO_MS(ptp->min), TICKS_TO_MS(ptp->total/cnt), TICKS_TO_MS(ptp->max), TICKS_TO_MS(ptp->diff)); return true; }