src/bl602_wifi/ip/lmac/chan/chan.c
static void chan_tbtt_insert(struct chan_tbtt_tag *p_tbtt_entry) {
    struct co_list *list_tbtt = &chan_env.list_tbtt;
    struct chan_tbtt_tag *p_elem = (struct chan_tbtt_tag *)co_list_pick(list_tbtt);
    bool insert = true;
    // last item that is not conflict with p_tbtt_entry, and has smallest possible time
    struct chan_tbtt_tag *p_prev_elem  = NULL;
    struct chan_tbtt_tag *p_delay_elem = NULL;
    uint8_t nb_delayed = 0;
    for (; p_elem; p_elem = (struct chan_tbtt_tag *) co_list_next(&p_elem->list_hdr)) {
        ASSERT_ERR(p_elem != p_tbtt_entry);
        uint32_t time2 = p_tbtt_entry->time;
        uint32_t time1 = p_elem->time;
        
        if (p_elem->status != CHAN_TBTT_PRESENCE) {
            if (chan_tbtt_detect_conflict(time2, time1) == 0){
                if (time2 < time1) break;
                p_prev_elem = p_elem;
            } else {
                if (p_tbtt_entry->priority <= p_elem->priority) {
                    insert = false;
                    p_delay_elem = p_tbtt_entry;
                    nb_delayed = 1;
                    break;
                } else {
                    if (!p_delay_elem) {
                        p_delay_elem = p_elem;
                    }
                    nb_delayed ++;
                }
            }
        } else {
            if ((time2 < time1) || chan_tbtt_detect_conflict(time2, time1)) {
                insert = false;
                p_delay_elem = p_tbtt_entry;
                nb_delayed = 1;
            } else p_prev_elem = p_elem;
        }
    }

    // it seems that all events in list_tbtt are sorted descending so 
    // it can insert a range from the first p_delay_elem
    // to p_delay_elem + nb_delayed
    for (int i = 0; i < nb_delayed; i++) {
        ASSERT_ERR(p_delay_elem);
        if (p_delay_elem != p_tbtt_entry) {
            if (p_delay_elem->status == CHAN_TBTT_PROG) {
                mm_timer_clear(&chan_env.tmr_tbtt_switch);
                p_delay_elem->status = CHAN_TBTT_NOT_PROG;
            }
            co_list_extract(list_tbtt, &p_delay_elem->list_hdr);
        }
        // in case of event can't insert, p_tbtt_entry will be put into the delay list
        // otherwise the later insert will put it into list_tbtt
        co_list_push_back(&chan_env.list_tbtt_delay, &p_delay_elem->list_hdr); 
        p_delay_elem = (struct chan_tbtt_tag *)co_list_next(&p_delay_elem->list_hdr);
    }

    if (insert) {
        co_list_insert_after(&chan_env.list_tbtt, &p_prev_elem->list_hdr, &p_tbtt_entry->list_hdr);
    }
}
/////
/// Find a location in list_tbtt that p_tbtt_entry can be inserted
/// 1) p_tbtt_entry is sorted decending by time, need to find the 
/// largest possible i s.t. where p_tbtt_entry.time < list[i].time
/// 2) p_tbtt_entry should not conflict with higher priority elements
/// in the list.
/// 3) If p_tbtt_entry confilcts with elements with lower pri,
/// they should be moved in the delay list
///  they should be successive in the list
Find a location in list_tbtt that p_tbtt_entry can be inserted
1) p_tbtt_entry is sorted decending by time, need to find the
largest possible i s.t. where p_tbtt_entry.time < list[i].time
2) p_tbtt_entry should not conflict with higher priority elements
in the list.
3) If p_tbtt_entry confilcts with elements with lower pri,
they should be moved in the delay list
they should be successive in the list
Find a location in list_tbtt that p_tbtt_entry can be inserted 1 ) p_tbtt_entry is sorted decending by time need to find the largest possible i s . t . where p_tbtt_entry . time < list [ i ] . time 2 ) p_tbtt_entry should not conflict with higher priority elements in the list . 3 ) If p_tbtt_entry confilcts with elements with lower pri they should be moved in the delay list they should be successive in the list
static void chan_tbtt_insert(struct chan_tbtt_tag *p_tbtt_entry) {
    struct co_list *list_tbtt = &chan_env.list_tbtt;
    struct chan_tbtt_tag *p_elem = (struct chan_tbtt_tag *)co_list_pick(list_tbtt);
    bool insert = true;
    struct chan_tbtt_tag *p_prev_elem  = NULL;
    struct chan_tbtt_tag *p_delay_elem = NULL;
    uint8_t nb_delayed = 0;
    for (; p_elem; p_elem = (struct chan_tbtt_tag *) co_list_next(&p_elem->list_hdr)) {
        ASSERT_ERR(p_elem != p_tbtt_entry);
        uint32_t time2 = p_tbtt_entry->time;
        uint32_t time1 = p_elem->time;
        if (p_elem->status != CHAN_TBTT_PRESENCE) {
            if (chan_tbtt_detect_conflict(time2, time1) == 0){
                if (time2 < time1) break;
                p_prev_elem = p_elem;
            } else {
                if (p_tbtt_entry->priority <= p_elem->priority) {
                    insert = false;
                    p_delay_elem = p_tbtt_entry;
                    nb_delayed = 1;
                    break;
                } else {
                    if (!p_delay_elem) {
                        p_delay_elem = p_elem;
                    }
                    nb_delayed ++;
                }
            }
        } else {
            if ((time2 < time1) || chan_tbtt_detect_conflict(time2, time1)) {
                insert = false;
                p_delay_elem = p_tbtt_entry;
                nb_delayed = 1;
            } else p_prev_elem = p_elem;
        }
    }
    for (int i = 0; i < nb_delayed; i++) {
        ASSERT_ERR(p_delay_elem);
        if (p_delay_elem != p_tbtt_entry) {
            if (p_delay_elem->status == CHAN_TBTT_PROG) {
                mm_timer_clear(&chan_env.tmr_tbtt_switch);
                p_delay_elem->status = CHAN_TBTT_NOT_PROG;
            }
            co_list_extract(list_tbtt, &p_delay_elem->list_hdr);
        }
        co_list_push_back(&chan_env.list_tbtt_delay, &p_delay_elem->list_hdr); 
        p_delay_elem = (struct chan_tbtt_tag *)co_list_next(&p_delay_elem->list_hdr);
    }
    if (insert) {
        co_list_insert_after(&chan_env.list_tbtt, &p_prev_elem->list_hdr, &p_tbtt_entry->list_hdr);
    }
}
static void chan_tbtt_insert ( struct chan_tbtt_tag * p_tbtt_entry ) { struct co_list * list_tbtt = & chan_env . list_tbtt ; struct chan_tbtt_tag * p_elem = ( struct chan_tbtt_tag * ) co_list_pick ( list_tbtt ) ; bool insert = true ; struct chan_tbtt_tag * p_prev_elem = NULL ; struct chan_tbtt_tag * p_delay_elem = NULL ; uint8_t nb_delayed = 0 ; for ( ; p_elem ; p_elem = ( struct chan_tbtt_tag * ) co_list_next ( & p_elem -> list_hdr ) ) { ASSERT_ERR ( p_elem != p_tbtt_entry ) ; uint32_t time2 = p_tbtt_entry -> time ; uint32_t time1 = p_elem -> time ; if ( p_elem -> status != CHAN_TBTT_PRESENCE ) { if ( chan_tbtt_detect_conflict ( time2 , time1 ) == 0 ) { if ( time2 < time1 ) break ; p_prev_elem = p_elem ; } else { if ( p_tbtt_entry -> priority <= p_elem -> priority ) { insert = false ; p_delay_elem = p_tbtt_entry ; nb_delayed = 1 ; break ; } else { if ( ! p_delay_elem ) { p_delay_elem = p_elem ; } nb_delayed ++ ; } } } else { if ( ( time2 < time1 ) || chan_tbtt_detect_conflict ( time2 , time1 ) ) { insert = false ; p_delay_elem = p_tbtt_entry ; nb_delayed = 1 ; } else p_prev_elem = p_elem ; } } for ( int i = 0 ; i < nb_delayed ; i ++ ) { ASSERT_ERR ( p_delay_elem ) ; if ( p_delay_elem != p_tbtt_entry ) { if ( p_delay_elem -> status == CHAN_TBTT_PROG ) { mm_timer_clear ( & chan_env . tmr_tbtt_switch ) ; p_delay_elem -> status = CHAN_TBTT_NOT_PROG ; } co_list_extract ( list_tbtt , & p_delay_elem -> list_hdr ) ; } co_list_push_back ( & chan_env . list_tbtt_delay , & p_delay_elem -> list_hdr ) ; p_delay_elem = ( struct chan_tbtt_tag * ) co_list_next ( & p_delay_elem -> list_hdr ) ; } if ( insert ) { co_list_insert_after ( & chan_env . list_tbtt , & p_prev_elem -> list_hdr , & p_tbtt_entry -> list_hdr ) ; } }
Find a location in list_tbtt that p_tbtt_entry can be inserted
1) p_tbtt_entry is sorted decending by time, need to find the
largest possible i s.t.
Find a location in list_tbtt that p_tbtt_entry can be inserted 1 ) p_tbtt_entry is sorted decending by time need to find the largest possible i s . t .
// last item that is not conflict with p_tbtt_entry, and has smallest possible time // it seems that all events in list_tbtt are sorted descending so  // it can insert a range from the first p_delay_elem // to p_delay_elem + nb_delayed // in case of event can't insert, p_tbtt_entry will be put into the delay list // otherwise the later insert will put it into list_tbtt