| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2012, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: $ |
|---|
| 11 | * $brcm_Revision: $ |
|---|
| 12 | * $brcm_Date: $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: $ |
|---|
| 19 | * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | |
|---|
| 22 | #include <stdlib.h> |
|---|
| 23 | #include <unistd.h> |
|---|
| 24 | #include "bstd.h" |
|---|
| 25 | #include "bkni.h" |
|---|
| 26 | #include "bapp_types.h" |
|---|
| 27 | #include "bos.h" |
|---|
| 28 | #include "bapp_util.h" |
|---|
| 29 | #include "genericlist.h" |
|---|
| 30 | |
|---|
| 31 | typedef struct bos_event_t |
|---|
| 32 | { |
|---|
| 33 | LINKS_T links; |
|---|
| 34 | b_event_t *event; |
|---|
| 35 | }bos_event_t; |
|---|
| 36 | |
|---|
| 37 | typedef struct bos_queue_t |
|---|
| 38 | { |
|---|
| 39 | LIST_T list; |
|---|
| 40 | pthread_mutex_t mutex; |
|---|
| 41 | }bos_queue_t; |
|---|
| 42 | |
|---|
| 43 | int g_ticks_per_ms = 1; |
|---|
| 44 | static pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER; |
|---|
| 45 | static int lflag = 0; |
|---|
| 46 | |
|---|
| 47 | extern int pthread_kill(pthread_t thread, int sig); |
|---|
| 48 | |
|---|
| 49 | void bos_init(void) |
|---|
| 50 | { |
|---|
| 51 | } |
|---|
| 52 | void bos_start(void) |
|---|
| 53 | { |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | bresult bos_start_task( b_task_t *handle, const b_task_params *params, b_task_func func, void *data ) |
|---|
| 57 | { |
|---|
| 58 | BSTD_UNUSED(params); |
|---|
| 59 | |
|---|
| 60 | if (pthread_create((pthread_t*)handle, NULL, |
|---|
| 61 | (void *(*)(void*))func, data) == 0) |
|---|
| 62 | { |
|---|
| 63 | return b_ok; |
|---|
| 64 | } |
|---|
| 65 | return berr_out_of_memory; |
|---|
| 66 | } |
|---|
| 67 | void bos_stop_task(b_task_t handle) |
|---|
| 68 | { |
|---|
| 69 | #ifndef __MINGW32__ |
|---|
| 70 | pthread_kill((pthread_t)handle,-9); |
|---|
| 71 | #endif |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | bresult bos_create_queue( b_queue_t *handle, b_event_t *events, int num_events ) |
|---|
| 75 | { |
|---|
| 76 | BSTD_UNUSED(events); |
|---|
| 77 | BSTD_UNUSED(num_events); |
|---|
| 78 | |
|---|
| 79 | bos_queue_t *p_bq = (bos_queue_t*)malloc(sizeof(bos_queue_t)); |
|---|
| 80 | if (!p_bq) |
|---|
| 81 | return berr_out_of_memory; |
|---|
| 82 | |
|---|
| 83 | if (pthread_mutex_init(&p_bq->mutex,NULL) != 0) |
|---|
| 84 | { |
|---|
| 85 | free(p_bq); |
|---|
| 86 | return berr_out_of_memory; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | initl(&p_bq->list); |
|---|
| 90 | *handle = (b_queue_t)p_bq; |
|---|
| 91 | return b_ok; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void bos_delete_queue( b_queue_t *handle ) |
|---|
| 95 | { |
|---|
| 96 | bos_queue_t *p_bq = (bos_queue_t*)handle; |
|---|
| 97 | bos_event_t *p_evt; |
|---|
| 98 | |
|---|
| 99 | for (p_evt = (bos_event_t*)remlh(&p_bq->list); p_evt != NULL; p_evt = (bos_event_t*)remlh(&p_bq->list)) |
|---|
| 100 | { |
|---|
| 101 | free(p_evt); |
|---|
| 102 | } |
|---|
| 103 | free(p_bq); |
|---|
| 104 | } |
|---|
| 105 | bresult bos_post_event( b_queue_t handle, b_event_t *event ) |
|---|
| 106 | { |
|---|
| 107 | bos_queue_t *p_bq = (bos_queue_t*)handle; |
|---|
| 108 | bos_event_t *p_evt = NULL; |
|---|
| 109 | |
|---|
| 110 | p_evt = (bos_event_t*)malloc(sizeof(bos_event_t)); |
|---|
| 111 | if (!p_bq) |
|---|
| 112 | return berr_out_of_memory; |
|---|
| 113 | |
|---|
| 114 | if (pthread_mutex_lock((pthread_mutex_t *)&p_bq->mutex) != 0) |
|---|
| 115 | goto error; |
|---|
| 116 | |
|---|
| 117 | p_evt->event = event; |
|---|
| 118 | inslt(&p_bq->list,p_evt); |
|---|
| 119 | pthread_mutex_unlock((pthread_mutex_t *)&p_bq->mutex); |
|---|
| 120 | |
|---|
| 121 | return b_ok; |
|---|
| 122 | |
|---|
| 123 | error: |
|---|
| 124 | if (p_evt) |
|---|
| 125 | free(p_evt); |
|---|
| 126 | return berr_timeout; |
|---|
| 127 | |
|---|
| 128 | } |
|---|
| 129 | b_event_t *bos_pend_event( b_queue_t handle, int timeout_ms ) |
|---|
| 130 | { |
|---|
| 131 | bos_queue_t *p_bq = (bos_queue_t*)handle; |
|---|
| 132 | bos_event_t *p_evt = NULL; |
|---|
| 133 | b_event_t *return_event = NULL; |
|---|
| 134 | |
|---|
| 135 | unsigned int timout_ticks = bos_getticks() + timeout_ms * g_ticks_per_ms;; |
|---|
| 136 | |
|---|
| 137 | if (pthread_mutex_lock((pthread_mutex_t *)&p_bq->mutex) != 0) |
|---|
| 138 | goto exit; |
|---|
| 139 | |
|---|
| 140 | while (LEMPTY(&p_bq->list)) |
|---|
| 141 | { |
|---|
| 142 | pthread_mutex_unlock((pthread_mutex_t *)&p_bq->mutex); |
|---|
| 143 | bos_sleep(1); |
|---|
| 144 | |
|---|
| 145 | if (timeout_ms >= 0) |
|---|
| 146 | { |
|---|
| 147 | if (timout_ticks < bos_getticks()) |
|---|
| 148 | goto exit; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | if (pthread_mutex_lock((pthread_mutex_t *)&p_bq->mutex) != 0) |
|---|
| 152 | goto exit; |
|---|
| 153 | } |
|---|
| 154 | p_evt = (bos_event_t*)LHEAD(&p_bq->list); |
|---|
| 155 | reml(&p_bq->list,p_evt); |
|---|
| 156 | return_event = p_evt->event; |
|---|
| 157 | free(p_evt); |
|---|
| 158 | pthread_mutex_unlock((pthread_mutex_t *)&p_bq->mutex); |
|---|
| 159 | |
|---|
| 160 | exit: |
|---|
| 161 | return return_event; |
|---|
| 162 | } |
|---|
| 163 | bresult bos_create_mutex( b_mutex_t *handle ) |
|---|
| 164 | { |
|---|
| 165 | handle->queue = (b_queue_t)malloc(sizeof(pthread_mutex_t)); |
|---|
| 166 | if (pthread_mutex_init((pthread_mutex_t *)(handle->queue),NULL) == 0) |
|---|
| 167 | return b_ok; |
|---|
| 168 | return berr_out_of_memory; |
|---|
| 169 | } |
|---|
| 170 | void bos_delete_mutex( b_mutex_t *handle) |
|---|
| 171 | { |
|---|
| 172 | pthread_mutex_destroy((pthread_mutex_t *)(handle->queue)); |
|---|
| 173 | free((void*)(handle->queue)); |
|---|
| 174 | } |
|---|
| 175 | bresult bos_acquire_mutex( b_mutex_t *handle, int timeout_ms ) |
|---|
| 176 | { |
|---|
| 177 | if (timeout_ms < 0) |
|---|
| 178 | { |
|---|
| 179 | if (pthread_mutex_lock((pthread_mutex_t *)(handle->queue)) != 0) |
|---|
| 180 | return berr_timeout; |
|---|
| 181 | } |
|---|
| 182 | else |
|---|
| 183 | { |
|---|
| 184 | unsigned int timout_ticks = bos_getticks() + timeout_ms * g_ticks_per_ms; |
|---|
| 185 | while (pthread_mutex_trylock((pthread_mutex_t *)(handle->queue)) != 0) |
|---|
| 186 | { |
|---|
| 187 | bos_sleep(1); |
|---|
| 188 | if (timout_ticks < bos_getticks()) |
|---|
| 189 | return berr_timeout; |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | return b_ok; |
|---|
| 193 | } |
|---|
| 194 | bresult bos_release_mutex( b_mutex_t *handle ) |
|---|
| 195 | { |
|---|
| 196 | pthread_mutex_unlock((pthread_mutex_t *)(handle->queue)); |
|---|
| 197 | return b_ok; |
|---|
| 198 | } |
|---|
| 199 | void bos_sleep( unsigned int sleep_ms ) |
|---|
| 200 | { |
|---|
| 201 | if (sleep_ms >=1000) |
|---|
| 202 | { |
|---|
| 203 | sleep(sleep_ms/1000); |
|---|
| 204 | usleep((sleep_ms%1000)*1000); |
|---|
| 205 | } |
|---|
| 206 | else |
|---|
| 207 | { |
|---|
| 208 | usleep(sleep_ms*1000); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | static struct timeval s_start_time = {0,0}; |
|---|
| 213 | unsigned int bos_getticks( void ) |
|---|
| 214 | { |
|---|
| 215 | struct timeval tv; |
|---|
| 216 | struct timeval cur_tv; |
|---|
| 217 | |
|---|
| 218 | if ((s_start_time.tv_sec == 0) && (s_start_time.tv_usec == 0)) |
|---|
| 219 | { |
|---|
| 220 | gettimeofday(&s_start_time,NULL); |
|---|
| 221 | } |
|---|
| 222 | gettimeofday(&cur_tv,NULL); |
|---|
| 223 | timeval_subtract(&tv,&cur_tv,&s_start_time); |
|---|
| 224 | return (tv.tv_sec * 1000 + tv.tv_usec/1000); |
|---|
| 225 | } |
|---|
| 226 | void bos_getclock(b_clock *clock) |
|---|
| 227 | { |
|---|
| 228 | BSTD_UNUSED(clock); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | unsigned int bos_enter_critical(void) |
|---|
| 232 | { |
|---|
| 233 | pthread_mutex_lock((pthread_mutex_t *)&s_mutex); |
|---|
| 234 | return lflag++; |
|---|
| 235 | } |
|---|
| 236 | void bos_exit_critical(unsigned int flags) |
|---|
| 237 | { |
|---|
| 238 | BSTD_UNUSED(flags); |
|---|
| 239 | pthread_mutex_unlock((pthread_mutex_t *)&s_mutex); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | #ifndef LINUX |
|---|
| 245 | /* Jan 6, 1980 => Sunday */ |
|---|
| 246 | #define START_DAY 0 |
|---|
| 247 | #define START_YEAR 1980 |
|---|
| 248 | #else |
|---|
| 249 | /* Jan 1, 1970 => Thursday */ |
|---|
| 250 | #define START_DAY 3 |
|---|
| 251 | #define START_YEAR 1970 |
|---|
| 252 | #endif |
|---|
| 253 | |
|---|
| 254 | /* Days/month for non-leap year */ |
|---|
| 255 | const unsigned char s_days_per_mon[] = |
|---|
| 256 | { |
|---|
| 257 | 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, |
|---|
| 258 | 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, |
|---|
| 259 | }; |
|---|
| 260 | |
|---|
| 261 | #define IS_LEAP_YEAR(y) ( !((y + START_YEAR) % 4) && ( ((y + START_YEAR) % 100) || !((y + START_YEAR) % 400) ) ) |
|---|
| 262 | void utctime(unsigned int secs,b_tm *p_tm) |
|---|
| 263 | { |
|---|
| 264 | unsigned int yday,mon,t_val; |
|---|
| 265 | unsigned char *p_dpm = (unsigned char*)s_days_per_mon; |
|---|
| 266 | |
|---|
| 267 | memset(p_tm,0,sizeof(b_tm)); |
|---|
| 268 | |
|---|
| 269 | /* seconds */ |
|---|
| 270 | p_tm->tm_sec = secs % 60; |
|---|
| 271 | t_val = secs / 60; /* minutes */ |
|---|
| 272 | |
|---|
| 273 | /* minutes */ |
|---|
| 274 | p_tm->tm_min = t_val % 60; |
|---|
| 275 | t_val /= 60; /* hours */ |
|---|
| 276 | |
|---|
| 277 | /* hours */ |
|---|
| 278 | p_tm->tm_hour = t_val % 24; |
|---|
| 279 | t_val /= 24; |
|---|
| 280 | |
|---|
| 281 | /* day of week */ |
|---|
| 282 | p_tm->tm_wday = t_val % 7; |
|---|
| 283 | p_tm->tm_wday = (t_val - START_DAY) % 7; |
|---|
| 284 | |
|---|
| 285 | #ifndef LINUX |
|---|
| 286 | t_val += 5; |
|---|
| 287 | #endif |
|---|
| 288 | /* year */ |
|---|
| 289 | p_tm->tm_yday = t_val; |
|---|
| 290 | p_tm->tm_year = 0; |
|---|
| 291 | |
|---|
| 292 | /* day of current year */ |
|---|
| 293 | while ((IS_LEAP_YEAR(p_tm->tm_year) && (p_tm->tm_yday > 365)) || |
|---|
| 294 | (!IS_LEAP_YEAR(p_tm->tm_year) && (p_tm->tm_yday > 364))) |
|---|
| 295 | { |
|---|
| 296 | if (IS_LEAP_YEAR(p_tm->tm_year)) |
|---|
| 297 | p_tm->tm_yday -= 366; |
|---|
| 298 | else |
|---|
| 299 | p_tm->tm_yday -= 365; |
|---|
| 300 | p_tm->tm_year++; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | if (IS_LEAP_YEAR(p_tm->tm_year)) |
|---|
| 304 | p_dpm += 12; |
|---|
| 305 | |
|---|
| 306 | yday = p_tm->tm_yday + 1; |
|---|
| 307 | mon = 0; |
|---|
| 308 | while(yday > p_dpm[mon]) |
|---|
| 309 | { |
|---|
| 310 | yday -= p_dpm[mon]; |
|---|
| 311 | mon++; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | /* month */ |
|---|
| 315 | p_tm->tm_mon = mon; |
|---|
| 316 | |
|---|
| 317 | /* day of month */ |
|---|
| 318 | p_tm->tm_mday = yday; |
|---|
| 319 | |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | #ifdef CONFIG_GP |
|---|
| 323 | void GP_putchar(char ch) |
|---|
| 324 | { |
|---|
| 325 | putchar(ch); |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | int GP_getchar(void) |
|---|
| 329 | { |
|---|
| 330 | return getchar(); |
|---|
| 331 | } |
|---|
| 332 | #endif |
|---|