1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741 |
- #include "MS_brr_api.h"
- #include "MS_prov_api.h"
- #include "MS_access_api.h"
- #include "blebrr.h"
- #include "ll.h"
- #include "MS_trn_api.h"
- #include "pwrmgr.h"
- #include "ll_sleep.h"
- #include "led_light.h"
- #define BLEBRR_MAX_ADV_FILTER_LIST_COUNT 100
- #define BLEBRR_MAX_ADV_DATA_SIZE 31
- #define BLEBRR_BCON_ELEMENTS 2
- #define BLEBRR_ACTIVEADV_TIMEOUT 1
- #define BLEBRR_ADV_TIMEOUT 6
- #define BLEBRR_SCAN_TIMEOUT (EM_TIMEOUT_MILLISEC | 30)
- #define BLEBRR_NCON_ADVTYPE_OFFSET 2
- #define BLEBRR_ADVREPEAT_PRO_COUNT 2
- #define BLEBRR_ADVREPEAT_NET_COUNT 2
- #define BLEBRR_SCAN_ADJ_STEP 30
- #define BLEBRR_SCAN_ADJ_THD_MAX 45
- #define BLEBRR_SCAN_ADJ_THD_MIN 15
- #define BLEBRR_TURNOFF_RELAY_THD 32
- #define BLEBRR_SKIP_BEACON_QUEUE_DEPTH 0
- #define BLEBRR_BCON_READY_TIME 10
- #define BLEBRR_QTYPE_DATA 0x00
- #define BLEBRR_QTYPE_BEACON 0x01
- #define BLEBRR_NUM_QUEUES 0x02
- #define BLEBRR_UPROV_ADV_BCON 0x00
- #define BLEBRR_UPROV_ADV_URI 0x01
- #define BLEBRR_UPROV_GATT_BCON 0x02
- #define BLEBRR_UPROV_GATT_URI 0x03
- #define BLEBRR_SECNET_BCON 0x04
- #define BLEBRR_NUM_BCONS 0x05
- #define BLEBRR_GATT_ADV_SERV_DATA_OFFSET 11
- #define BLEBRR_GATT_ADV_SERV_DATALEN_OFFSET 7
- #define BLEBRR_GAP_ADVDATA_LEN 31
- #define BLEBRR_GAP_MAX_ADVDATA_SETS 2
- #ifdef BLEBRR_LP_SUPPORT
- UCHAR blebrr_lp_flag = MS_FALSE;
- #define BLEBRR_LP_UNPROVISION_TIMEOUT 10*60
- #define BLEBRR_LP_PROVISIONED_TIMEOUT 1200
- #define BLEBRR_LP_PROVISIONED_WKP_TIMEOUT 60
- #define BLEBRR_LP_PROVISIONED_SLP_TIMEOUT (BLEBRR_LP_PROVISIONED_TIMEOUT-BLEBRR_LP_PROVISIONED_WKP_TIMEOUT)
- #endif
- #define BLEBRR_MUTEX_INIT() MS_MUTEX_INIT(blebrr_mutex, BRR);
- #define BLEBRR_MUTEX_INIT_VOID() MS_MUTEX_INIT_VOID(blebrr_mutex, BRR);
- #define BLEBRR_LOCK() MS_MUTEX_LOCK(blebrr_mutex, BRR);
- #define BLEBRR_LOCK_VOID() MS_MUTEX_LOCK_VOID(blebrr_mutex, BRR);
- #define BLEBRR_UNLOCK() MS_MUTEX_UNLOCK(blebrr_mutex, BRR);
- #define BLEBRR_UNLOCK_VOID() MS_MUTEX_UNLOCK_VOID(blebrr_mutex, BRR);
- typedef struct _BLEBRR_Q_ELEMENT
- {
-
- UCHAR *pdata;
-
- UINT16 pdatalen;
-
- UCHAR type;
- } BLEBRR_Q_ELEMENT;
- typedef struct _BLEBRR_Q
- {
-
- BLEBRR_Q_ELEMENT element[BLEBRR_QUEUE_SIZE];
-
- UINT16 start;
-
- UINT16 end;
- } BLEBRR_Q;
- typedef struct _BLEBRR_GAP_ADV_DATA
- {
-
- UCHAR data[BLEBRR_GAP_ADVDATA_LEN];
-
- UCHAR datalen;
- } BLEBRR_GAP_ADV_DATA;
- #ifdef BLEBRR_LP_SUPPORT
- EM_timer_handle blebrr_lp_thandle;
- #endif
- #ifdef BLEBRR_FILTER_DUPLICATE_PACKETS
- DECL_STATIC UCHAR blebrr_adv_list[BLEBRR_MAX_ADV_FILTER_LIST_COUNT][BLEBRR_MAX_ADV_DATA_SIZE];
- DECL_STATIC UCHAR blebrr_adv_list_inser_index = 0;
- #endif
- BRR_BEARER_INFO blebrr_adv;
- DECL_STATIC BRR_HANDLE blebrr_advhandle;
- DECL_STATIC UCHAR blebrr_bconidx;
- #ifdef BLEBRR_LP_SUPPORT
- DECL_STATIC UCHAR blebrr_beacon;
- #endif
- DECL_STATIC UCHAR blebrr_update_advcount = BLEBRR_BCON_READY_TIME;
- DECL_STATIC BLEBRR_Q_ELEMENT blebrr_bcon[BRR_BCON_COUNT];
- DECL_STATIC BLEBRR_Q blebrr_queue;
- MS_DEFINE_MUTEX_TYPE(static, blebrr_mutex)
- DECL_STATIC EM_timer_handle blebrr_timer_handle;
- UCHAR blebrr_state;
- UCHAR blebrr_prov_started;
- UCHAR blebrr_adv_restart;
- UINT32 blebrr_scanTimeOut = 100;
- DECL_STATIC UCHAR blebrr_datacount;
- DECL_STATIC UCHAR blebrr_advrepeat_count;
- DECL_STATIC UCHAR blebrr_scan_interleave;
- BLEBRR_GAP_ADV_DATA blebrr_gap_adv_data[BLEBRR_GAP_MAX_ADVDATA_SETS] =
- {
-
- {
- {
-
- 0x02, 0x01, 0x06,
-
- 0x03, 0x03, 0x27, 0x18,
-
- 0x15, 0x16,
- 0x27, 0x18,
- 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
- 0x00, 0x00
- },
-
- 29
- },
-
- {
- {
-
- 0x02, 0x01, 0x06,
-
- 0x03, 0x03, 0x28, 0x18,
-
- 0x0C, 0x16,
- 0x28, 0x18,
- 0x00,
- 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88
- },
-
- 20
- }
- };
- DECL_STATIC UCHAR pl_advdata_offset;
- UCHAR blebrr_sleep;
- uint32 blebrr_advscan_timeout_count = 0;
- extern uint32_t osal_sys_tick;
- void blebrr_handle_evt_adv_complete (UINT8 enable);
- DECL_STATIC void blebrr_timer_start (UINT32 timeout);
- DECL_STATIC void blebrr_timer_stop (void);
- API_RESULT blebrr_queue_depth_check(void);
- void blebrr_scan_enable(void)
- {
- BLEBRR_LOCK_VOID();
- if ((BLEBRR_STATE_IDLE == BLEBRR_GET_STATE()) &&
- (MS_TRUE != blebrr_sleep))
- {
- blebrr_scan_pl(MS_TRUE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
- }
- BLEBRR_UNLOCK_VOID();
- }
- DECL_STATIC UCHAR blebrr_get_beacon_type (UCHAR type, UCHAR bcon)
- {
- return (BRR_BCON_PASSIVE == type)?
- ((BRR_BCON_TYPE_UNPROV_DEVICE == bcon)? BLEBRR_UPROV_ADV_BCON: BLEBRR_SECNET_BCON):
- ((BRR_BCON_TYPE_UNPROV_DEVICE == bcon)? BLEBRR_UPROV_GATT_BCON: BLEBRR_NUM_BCONS);
- }
- DECL_STATIC BLEBRR_Q_ELEMENT * blebrr_enqueue_alloc (void)
- {
- BLEBRR_Q_ELEMENT * elt;
- UINT16 ei;
-
- elt = blebrr_queue.element;
- ei = blebrr_queue.end;
-
- if (0 != (elt + ei)->pdatalen)
- {
-
- elt = NULL;
- }
- else
- {
-
- elt = (elt + ei);
-
-
-
- if(ei == BLEBRR_QUEUE_SIZE - 1)
- ei = 0;
- else
- ei++;
- blebrr_queue.end = ei;
- }
- return elt;
- }
- DECL_STATIC void blebrr_dequeue_manual (void)
- {
- UINT16 ei;
- ei = blebrr_queue.end;
-
-
-
- if(ei == 0)
- ei = BLEBRR_QUEUE_SIZE - 1;
- else
- ei--;
- blebrr_queue.end = ei;
- }
- DECL_STATIC BLEBRR_Q_ELEMENT * blebrr_dequeue (void)
- {
- BLEBRR_Q_ELEMENT * elt;
- UINT16 si;
-
- elt = blebrr_queue.element;
- si = blebrr_queue.start;
-
- if (0 == (elt + si)->pdatalen)
- {
-
- elt = NULL;
- }
- else
- {
-
- elt = (elt + si);
-
-
- if (BRR_BCON_COUNT == elt->type)
- {
-
- blebrr_datacount--;
- }
-
-
- if(si == BLEBRR_QUEUE_SIZE - 1)
- si = 0;
- else
- si++;
- blebrr_queue.start = si;
- }
- return elt;
- }
- DECL_STATIC void blebrr_clear_bcon (UCHAR bconidx)
- {
- BLEBRR_Q_ELEMENT * elt;
-
- elt = &blebrr_bcon[bconidx];
-
- if (NULL != elt->pdata)
- {
- EM_free_mem (elt->pdata);
- elt->pdata = NULL;
- elt->pdatalen = 0;
- elt->type = BRR_BCON_COUNT;
- if ((BRR_BCON_TYPE_UNPROV_DEVICE == bconidx) &&
- (NULL != (elt + 1)->pdata) &&
- (0 != (elt + 1)->pdatalen))
- {
- EM_free_mem((elt + 1)->pdata);
- (elt + 1)->pdata = NULL;
- (elt + 1)->pdatalen = 0;
- (elt + 1)->type = BRR_BCON_COUNT;
- }
- blebrr_datacount--;
- }
- }
- UCHAR blebrr_get_queue_depth(void)
- {
- UCHAR depth;
- if(blebrr_queue.end>=blebrr_queue.start)
- {
- depth = blebrr_queue.end-blebrr_queue.start;
- }
- else
- {
- depth = BLEBRR_QUEUE_SIZE-(blebrr_queue.start-blebrr_queue.end);
- }
- return depth;
- }
- API_RESULT blebrr_queue_depth_check(void)
- {
- API_RESULT retval =API_SUCCESS;
- uint8_t randData;
-
- UCHAR depth =blebrr_get_queue_depth();
-
- if(depth>BLEBRR_TURNOFF_RELAY_THD)
- {
- LL_Rand(&randData, 1);
- randData=randData>>1;
- if( depth > randData)
- {
- retval= API_FAILURE;
- }
- BLEBRR_LOG("[Queue DATA CNT] = %d %d %4X\n", depth,randData,retval);
- }
- return retval;
-
- }
- extern uint8 llState, llSecondaryState;
- DECL_STATIC API_RESULT blebrr_update_advdata(void)
- {
- BLEBRR_Q_ELEMENT * elt;
- UCHAR type;
- elt = NULL;
- UCHAR is_proxy_beacon;
- is_proxy_beacon = 1;
-
- if(blebrr_update_advcount < BLEBRR_BCON_READY_TIME)
- {
- is_proxy_beacon = 0;
- blebrr_update_advcount++;
- }
- else
- {
- blebrr_update_advcount = 0;
- }
- if(blebrr_get_queue_depth()>BLEBRR_SKIP_BEACON_QUEUE_DEPTH)
- {
- is_proxy_beacon = 0;
- }
- if (is_proxy_beacon)
- {
-
- UCHAR bconidx;
- bconidx = blebrr_bconidx;
- do
- {
- if (0 != blebrr_bcon[blebrr_bconidx].pdatalen)
- {
- elt = &blebrr_bcon[blebrr_bconidx];
- is_proxy_beacon = 0;
- }
- if (BRR_BCON_COUNT == ++blebrr_bconidx)
- {
- blebrr_bconidx = 0;
- }
- } while ((blebrr_bconidx != bconidx) && (NULL == elt));
- }
- if (!is_proxy_beacon && NULL == elt)
- {
- elt = blebrr_dequeue();
- is_proxy_beacon = 1;
- }
- if (NULL == elt)
- {
- return API_FAILURE;
- }
-
- type = (BRR_BCON_COUNT == elt->type) ? BRR_BCON_PASSIVE : elt->type;
-
- blebrr_advrepeat_count = 1;
- blebrr_advertise_data_pl(type, elt->pdata, elt->pdatalen);
-
- if (BRR_BCON_COUNT == elt->type)
- {
- #ifdef BLEBRR_LP_SUPPORT
- blebrr_beacon = 0;
- #endif
-
- EM_free_mem(elt->pdata);
- elt->pdatalen = 0;
- }
- #ifdef BLEBRR_LP_SUPPORT
- else
- {
- blebrr_beacon = 1;
- }
- #endif
- return API_SUCCESS;
- }
- #if 0
- DECL_STATIC void blebrr_timer_restart (UINT32 timeout);
- #endif
- DECL_STATIC API_RESULT blebrr_send
- (
- UCHAR type,
- void * pdata,
- UINT16 datalen,
- BLEBRR_Q_ELEMENT * elt
- )
- {
- API_RESULT retval;
- UCHAR * data;
- UINT16 packet_len;
- UCHAR offset;
- data = (UCHAR *)pdata;
-
-
- offset = (0 != type)? BLEBRR_NCON_ADVTYPE_OFFSET: 0;
-
- packet_len = datalen + offset;
-
- elt->pdata = EM_alloc_mem(packet_len);
- if (NULL == elt->pdata)
- {
- return API_FAILURE;
- }
- if (offset >= 1)
- {
-
- elt->pdata[0] = (UCHAR)(datalen + (offset - 1));
- if (offset - 1)
- {
- elt->pdata[1] = type;
- }
- }
-
- EM_mem_copy((elt->pdata + offset), data, datalen);
- elt->pdatalen = packet_len;
-
- if (EM_TIMER_HANDLE_INIT_VAL != blebrr_timer_handle)
- {
-
- if (BLEBRR_STATE_SCAN_ENABLED == BLEBRR_GET_STATE())
- {
- retval = EM_stop_timer(blebrr_timer_handle);
- blebrr_timer_handle = EM_TIMER_HANDLE_INIT_VAL;
-
- if(retval == EM_SUCCESS)
- blebrr_scan_pl (MS_FALSE);
- }
- }
- else
- {
-
- if (BLEBRR_STATE_SCAN_ENABLED == BLEBRR_GET_STATE())
- {
- blebrr_scan_pl (MS_FALSE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_DISABLE);
- }
- else if (BLEBRR_STATE_ADV_ENABLED == BLEBRR_GET_STATE())
- {
- blebrr_adv_restart = MS_TRUE;
- blebrr_timer_stop();
-
-
- blebrr_advertise_pl(MS_FALSE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_DISABLE);
- }
- else if (BLEBRR_STATE_IDLE == BLEBRR_GET_STATE())
- {
-
- retval = blebrr_update_advdata();
- if (API_SUCCESS != retval)
- {
-
- blebrr_scan_pl(MS_TRUE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
- }
- else
- {
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_ENABLE);
- }
- }
- }
- return API_SUCCESS;
- }
- DECL_STATIC API_RESULT blebrr_bcon_send(BRR_HANDLE * handle, void * pdata, UINT16 datalen)
- {
- BRR_BEACON_INFO * info;
- BLEBRR_Q_ELEMENT * elt;
- UCHAR op, action, type, bcon, bcontype;
- UCHAR bconidx;
-
- info = (BRR_BEACON_INFO *)pdata;
-
- op = (info->action & 0x0F);
- action = ((info->action & 0xF0) >> 4);
-
- type = (info->type & 0x0F);
- bcon = ((info->type & 0xF0) >> 4);
-
- BLEBRR_LOCK();
-
- switch (op)
- {
- case BRR_OBSERVE:
-
- break;
- case BRR_BROADCAST:
-
- bcontype = blebrr_get_beacon_type (type, bcon);
-
- bconidx = bcon;
- if (BRR_ENABLE == action)
- {
-
- if ((BRR_BCON_ACTIVE == type) && ((NULL != info->bcon_data)))
- {
-
- UCHAR abs_index;
- abs_index = blebrr_gatt_mode_get();
- if (BLEBRR_GATT_PROV_MODE == abs_index)
- {
-
-
- EM_mem_copy
- (
- blebrr_gap_adv_data[abs_index].data + BLEBRR_GATT_ADV_SERV_DATA_OFFSET,
- info->bcon_data + 1,
- 16 + 2
- );
-
-
- blebrr_scan_interleave = MS_FALSE;
- }
-
- else
- {
-
-
- abs_index = BLEBRR_GATT_PROXY_MODE;
-
- EM_mem_copy
- (
- blebrr_gap_adv_data[abs_index].data + BLEBRR_GATT_ADV_SERV_DATA_OFFSET,
- info->bcon_data,
- info->bcon_datalen
- );
-
- blebrr_gap_adv_data[abs_index].datalen = BLEBRR_GATT_ADV_SERV_DATA_OFFSET + info->bcon_datalen;
-
- blebrr_gap_adv_data[abs_index].data[BLEBRR_GATT_ADV_SERV_DATALEN_OFFSET] =
- info->bcon_datalen + 1 + 2;
- }
-
- info->bcon_data = blebrr_gap_adv_data[abs_index].data + pl_advdata_offset;
- info->bcon_datalen = blebrr_gap_adv_data[abs_index].datalen - pl_advdata_offset;
- }
- else
- {
-
- blebrr_scan_interleave = MS_TRUE;
- }
-
- if (0 != blebrr_bcon[bconidx].pdatalen)
- {
-
- BLEBRR_UNLOCK();
- BLEBRR_LOG("Beacon Not Free!\n");
- return API_FAILURE;
- }
- elt = &blebrr_bcon[bconidx];
- blebrr_datacount++;
-
- elt->type = type;
-
- blebrr_send
- (
- ((BRR_BCON_TYPE_UNPROV_DEVICE == bcon) &&
- (BRR_BCON_ACTIVE != type))? MESH_AD_TYPE_BCON : 0,
- info->bcon_data,
- info->bcon_datalen,
- elt
- );
-
- if ((BRR_BCON_TYPE_UNPROV_DEVICE == bconidx) &&
- (NULL != info->uri) &&
- (NULL != info->uri->payload) &&
- (0 != info->uri->length))
- {
- elt = &blebrr_bcon[bconidx + 1];
-
- elt->type = bcontype + 1;
-
- blebrr_send
- (
- 0,
- info->uri->payload,
- info->uri->length,
- elt
- );
- }
- }
- else
- {
-
- blebrr_clear_bcon (bconidx);
- }
- break;
- default:
- break;
- }
-
- BLEBRR_UNLOCK();
- return API_SUCCESS;
- }
- DECL_STATIC API_RESULT blebrr_adv_send(BRR_HANDLE * handle, UCHAR type, void * pdata, UINT16 datalen)
- {
- API_RESULT retval;
- BLEBRR_Q_ELEMENT * elt;
-
- if (*handle != blebrr_advhandle)
- {
- return API_FAILURE;
- }
- if ((NULL == pdata) ||
- (0 == datalen))
- {
- return API_FAILURE;
- }
-
- blebrr_scan_interleave = MS_TRUE;
- blebrr_update_advcount = BLEBRR_BCON_READY_TIME;
-
- if (MESH_AD_TYPE_BCON == type)
- {
- BRR_BEACON_INFO * info;
-
- info = (BRR_BEACON_INFO *)pdata;
- if (BRR_BCON_TYPE_SECURE_NET != (info->type >> 4))
- {
- return blebrr_bcon_send(handle, pdata, datalen);
- }
- else
- {
-
- pdata = info->bcon_data;
- datalen = info->bcon_datalen;
- }
- }
-
- BLEBRR_LOCK();
-
- elt = blebrr_enqueue_alloc();
-
- if (NULL == elt)
- {
-
- BLEBRR_UNLOCK();
- BLEBRR_LOG("Queue Full! blebrr_advscan_timeout_count = %d, ble state = %d\r\n", blebrr_advscan_timeout_count, BLEBRR_GET_STATE());
- blebrr_scan_pl(FALSE);
- return API_FAILURE;
- }
-
- elt->type = BRR_BCON_COUNT;
-
- retval = blebrr_send
- (
- type,
- pdata,
- datalen,
- elt
- );
- if(retval == API_FAILURE)
- blebrr_dequeue_manual();
- else
- blebrr_datacount++;
-
-
- BLEBRR_UNLOCK();
- return API_SUCCESS;
- }
- #ifdef BLEBRR_LP_SUPPORT
- DECL_STATIC void blebrr_adv_sleep(BRR_HANDLE * handle)
- {
- BLEBRR_LOCK_VOID();
-
- blebrr_sleep = MS_TRUE;
- MS_prov_stop_interleave_timer();
- MS_proxy_server_stop_timer();
- if (BLEBRR_STATE_SCAN_ENABLED == BLEBRR_GET_STATE() ||
- BLEBRR_STATE_IDLE == BLEBRR_GET_STATE())
- {
-
- blebrr_scan_pl(MS_FALSE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IDLE);
- }
- else if(BLEBRR_STATE_ADV_ENABLED == BLEBRR_GET_STATE())
- {
-
- blebrr_advertise_pl(MS_FALSE);
- }
-
- EM_enter_sleep_pl();
- BLEBRR_UNLOCK_VOID();
- }
- DECL_STATIC void blebrr_adv_wakeup(BRR_HANDLE * handle, UINT8 mode)
- {
- BLEBRR_LOCK_VOID();
-
- EM_exit_sleep_pl();
-
- blebrr_sleep = MS_FALSE;
- if (BRR_TX & mode)
- {
- if (BLEBRR_STATE_IDLE == BLEBRR_GET_STATE())
- {
- blebrr_update_advcount = BLEBRR_BCON_READY_TIME;
-
- blebrr_lp_flag = MS_TRUE;
- blebrr_update_advdata();
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_ENABLE);
- }
- }
- else if (BRR_RX & mode)
- {
- if (BLEBRR_STATE_IDLE == BLEBRR_GET_STATE())
- {
-
- blebrr_scan_pl(MS_TRUE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
- }
- }
- BLEBRR_UNLOCK_VOID();
- }
- API_RESULT blebrr_sleep_handler(void)
- {
- API_RESULT retval;
- retval=MS_brr_sleep();
- return retval;
- }
- API_RESULT blebrr_wakeup_handler(void)
- {
- API_RESULT retval;
- UCHAR state;
-
- MS_access_cm_get_features_field(&state, MS_FEATURE_PROXY);
- if(state == MS_TRUE)
- {
- retval = MS_brr_wakeup(BRR_TX|BRR_RX);
- }
- else
- {
- retval = MS_brr_wakeup(BRR_RX);
- }
-
- return retval;
- }
- static void enter_lp_sleep_mode (void)
- {
- light_timeout_handle();
- hal_pwrmgr_unlock(MOD_USR1);
- blebrr_sleep_handler();
- printf("sleep mode:%d\n", isSleepAllow());
- }
- void blebrr_lp_mode (void * args, UINT16 size)
- {
- UCHAR mode;
- UCHAR glp_mode;
- blebrr_lp_thandle = EM_TIMER_HANDLE_INIT_VAL;
- UCHAR state;
-
- MS_access_cm_get_features_field(&state, MS_FEATURE_PROXY);
- MS_IGNORE_UNUSED_PARAM(size);
- mode = (*((UCHAR *)args));
- if(mode == BLEBRR_LP_OFF)
- {
- pwroff_cfg_t pwr_wkp_cfg[]={{P14,NEGEDGE}};
- hal_pwrmgr_poweroff( pwr_wkp_cfg, sizeof(pwr_wkp_cfg)/sizeof(pwr_wkp_cfg[0]) );
- }
- else if(mode == BLEBRR_LP_SLEEP)
- {
- blebrr_wakeup_handler();
- if(state != MS_TRUE)
- {
- glp_mode = BLEBRR_LP_WKP;
- EM_start_timer
- (
- &blebrr_lp_thandle,
- EM_TIMEOUT_MILLISEC | BLEBRR_LP_PROVISIONED_WKP_TIMEOUT,
- blebrr_lp_mode,
- (void *)&glp_mode,
- sizeof(glp_mode)
- );
- }
-
- }
- else
- {
- glp_mode = BLEBRR_LP_SLEEP;
- EM_start_timer
- (
- &blebrr_lp_thandle,
- EM_TIMEOUT_MILLISEC | BLEBRR_LP_PROVISIONED_SLP_TIMEOUT,
- blebrr_lp_mode,
- (void *)&glp_mode,
- sizeof(glp_mode)
- );
- enter_lp_sleep_mode();
- }
- }
- API_RESULT blebrr_lp_start(UCHAR mode)
- {
- API_RESULT retval;
- UCHAR state;
- UINT32 timeout;
-
- MS_access_cm_get_features_field(&state, MS_FEATURE_PROXY);
- switch(mode)
- {
- case BLEBRR_LP_OFF:
- {
- timeout = BLEBRR_LP_UNPROVISION_TIMEOUT;
- }
- break;
- case BLEBRR_LP_SLEEP:
- {
- timeout = EM_TIMEOUT_MILLISEC | BLEBRR_LP_PROVISIONED_SLP_TIMEOUT;
- }
- break;
- case BLEBRR_LP_WKP:
- {
- timeout = EM_TIMEOUT_MILLISEC | BLEBRR_LP_PROVISIONED_WKP_TIMEOUT;
- }
- break;
- default:
- break;
-
- }
- retval = EM_start_timer
- (
- &blebrr_lp_thandle,
- timeout,
- blebrr_lp_mode,
- (void *)&mode,
- sizeof(mode)
- );
- if(mode == BLEBRR_LP_SLEEP)
- enter_lp_sleep_mode();
- return retval;
-
- }
- void blebrr_lp_stop(void)
- {
- EM_stop_timer (blebrr_lp_thandle);
- blebrr_lp_thandle = EM_TIMER_HANDLE_INIT_VAL;
- }
- #endif
- DECL_STATIC void blebrr_advscan_timeout_handler (void * args, UINT16 size)
- {
- MS_IGNORE_UNUSED_PARAM(args);
- MS_IGNORE_UNUSED_PARAM(size);
- BLEBRR_LOCK_VOID();
- blebrr_advscan_timeout_count ++;
-
- blebrr_timer_handle = EM_TIMER_HANDLE_INIT_VAL;
-
- switch (BLEBRR_GET_STATE())
- {
- case BLEBRR_STATE_ADV_ENABLED:
-
- if (!blebrr_advertise_pl (MS_FALSE))
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_DISABLE);
- break;
- case BLEBRR_STATE_SCAN_ENABLED:
-
- blebrr_scan_pl (MS_FALSE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_DISABLE);
- break;
- default:
-
-
- break;
- }
- BLEBRR_UNLOCK_VOID();
- }
- DECL_STATIC void blebrr_timer_start (UINT32 timeout)
- {
- EM_RESULT retval;
- blebrr_timer_handle = EM_TIMER_HANDLE_INIT_VAL;
- retval = EM_start_timer
- (
- &blebrr_timer_handle,
- timeout,
- blebrr_advscan_timeout_handler,
- NULL,
- 0
- );
- if (EM_SUCCESS != retval)
- {
-
- }
- }
- DECL_STATIC void blebrr_timer_stop (void)
- {
-
- EM_stop_timer(blebrr_timer_handle);
- blebrr_timer_handle = EM_TIMER_HANDLE_INIT_VAL;
- }
- #if 0
- DECL_STATIC void blebrr_timer_restart (UINT32 timeout)
- {
- EM_RESULT retval;
- retval = EM_restart_timer
- (
- blebrr_timer_handle,
- timeout
- );
- if (EM_SUCCESS != retval)
- {
-
- }
- }
- #endif
- void blebrr_pl_scan_setup (UCHAR enable)
- {
- API_RESULT retval;
- BLEBRR_LOCK_VOID();
- #ifdef BLEBRR_ENABLE_SCAN_TRACE
- BLEBRR_LOG ("Scan Setup - %d", enable);
- #endif
-
- if (MS_TRUE == enable)
- {
-
- BLEBRR_SET_STATE(BLEBRR_STATE_SCAN_ENABLED);
-
- if (0 != blebrr_datacount)
- {
- UCHAR depth=blebrr_get_queue_depth();
- if(depth>0)
- blebrr_scanTimeOut = BLEBRR_SCAN_ADJ_THD_MIN;
- else
- blebrr_scanTimeOut = BLEBRR_SCAN_ADJ_THD_MAX;
-
-
- blebrr_timer_start ((EM_TIMEOUT_MILLISEC | blebrr_scanTimeOut));
- }
- }
- else
- {
-
- retval = blebrr_update_advdata();
- if (API_SUCCESS != retval)
- {
- if (MS_TRUE != blebrr_sleep)
- {
-
- blebrr_scan_pl(MS_TRUE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
- }
- else
- {
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IDLE);
- }
- }
- else
- {
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_ENABLE);
- }
- }
- BLEBRR_UNLOCK_VOID();
- }
- void blebrr_pl_advertise_setup (UCHAR enable)
- {
- BLEBRR_LOCK_VOID();
- UCHAR adv_repeat_count;
- API_RESULT retval;
- #ifdef BLEBRR_ENABLE_ADV_TRACE
- BLEBRR_LOG ("Adv Setup - %d", enable);
- #endif
-
- if (MS_TRUE == enable)
- {
-
- BLEBRR_SET_STATE(BLEBRR_STATE_ADV_ENABLED);
-
- if (blebrr_scan_interleave == MS_TRUE)
- {
- blebrr_timer_start (EM_TIMEOUT_MILLISEC | BLEBRR_ADV_TIMEOUT);
- }
-
- }
- else
- {
- if (MS_TRUE == blebrr_sleep)
- {
-
- blebrr_adv_restart = MS_FALSE;
- BLEBRR_SET_STATE(BLEBRR_STATE_IDLE);
- return;
- }
-
- if(blebrr_adv_restart == MS_TRUE)
- {
- blebrr_adv_restart = MS_FALSE;
- blebrr_update_advcount = BLEBRR_BCON_READY_TIME;
- retval = blebrr_update_advdata();
- if (API_SUCCESS != retval)
- {
-
- blebrr_scan_pl(MS_TRUE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
- }
- else
- {
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_ENABLE);
- }
-
- }
- else
- {
- adv_repeat_count = (blebrr_prov_started == MS_TRUE)?BLEBRR_ADVREPEAT_PRO_COUNT:BLEBRR_ADVREPEAT_NET_COUNT;
- if ((adv_repeat_count > blebrr_advrepeat_count))
- {
- blebrr_advrepeat_count++;
- blebrr_advertise_pl(MS_TRUE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_ADV_ENABLE);
- }
- else
- {
-
- blebrr_scan_pl(MS_TRUE);
- #ifdef BLEBRR_LP_SUPPORT
-
- UCHAR glp_mode;
- if((blebrr_lp_flag == MS_TRUE) &&(blebrr_beacon == 1))
- {
- blebrr_beacon =0;
- blebrr_lp_flag = MS_FALSE;
- glp_mode = BLEBRR_LP_WKP;
- blebrr_lp_start(glp_mode);
- }
- #endif
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
- }
- }
-
- }
- BLEBRR_UNLOCK_VOID();
- }
- void blebrr_pl_advertise_end (void)
- {
- BLEBRR_LOCK_VOID();
- BLEBRR_SET_STATE(BLEBRR_STATE_IDLE);
- blebrr_advrepeat_count = 0;
- BLEBRR_UNLOCK_VOID();
- }
- #ifdef BLEBRR_FILTER_DUPLICATE_PACKETS
- DECL_STATIC API_RESULT blebrr_adv_duplicate_filter( UCHAR * p_adv_data_with_bd_addr)
- {
- UCHAR length, index;
-
- length = p_adv_data_with_bd_addr[1 + BT_BD_ADDR_SIZE];
- for (index = 0; index < BLEBRR_MAX_ADV_FILTER_LIST_COUNT; index++)
- {
-
- if (0 == EM_mem_cmp
- (
- &blebrr_adv_list[index][0],
- p_adv_data_with_bd_addr,
- 1 + BT_BD_ADDR_SIZE
- ))
- {
-
- if (blebrr_adv_list[index][1 + BT_BD_ADDR_SIZE] == p_adv_data_with_bd_addr[1 + BT_BD_ADDR_SIZE])
- {
- if (0 == EM_mem_cmp
- (
- &blebrr_adv_list[index][1 + BT_BD_ADDR_SIZE + 1],
- &p_adv_data_with_bd_addr[1 + BT_BD_ADDR_SIZE + 1],
- length
- ))
- {
- return API_SUCCESS;
- }
- }
-
- EM_mem_copy
- (
- &blebrr_adv_list[index][1 + BT_BD_ADDR_SIZE],
- &p_adv_data_with_bd_addr[1 + BT_BD_ADDR_SIZE],
- length + 1
- );
- return API_FAILURE;
- }
- }
-
-
- EM_mem_copy
- (
- &blebrr_adv_list[blebrr_adv_list_inser_index][0],
- p_adv_data_with_bd_addr,
- length + 1 + BT_BD_ADDR_SIZE + 1
- );
-
- blebrr_adv_list_inser_index++;
- if (BLEBRR_MAX_ADV_FILTER_LIST_COUNT <= blebrr_adv_list_inser_index)
- {
- blebrr_adv_list_inser_index = 0;
- }
- return API_FAILURE;
- }
- #endif
- extern uint8 osal_memory_audit(void *ptr);
- void blebrr_pl_recv_advpacket (UCHAR type, UCHAR * pdata, UINT16 pdatalen, UCHAR rssi)
- {
- MS_BUFFER info;
- #ifdef BLEBRR_FILTER_DUPLICATE_PACKETS
-
- retval = blebrr_adv_duplicate_filter(p_adv_data_with_bd_addr);
-
- if (API_SUCCESS == retval)
- {
- return API_FAILURE;
- }
- #endif
-
- if (BRR_BCON_PASSIVE != type)
- {
- return;
- }
-
- info.payload = &rssi;
- info.length = sizeof(UCHAR);
-
- if (NULL != blebrr_adv.bearer_recv)
- {
-
- blebrr_adv.bearer_recv(&blebrr_advhandle, pdata, pdatalen, &info);
- }
- else
- {
- BLEBRR_LOG("BEARER RECV Callback Currently NULL !!\n");
- }
- }
- void blebrr_register(void)
- {
-
- BLEBRR_MUTEX_INIT_VOID();
-
- blebrr_timer_handle = EM_TIMER_HANDLE_INIT_VAL;
-
- blebrr_init_pl();
-
- pl_advdata_offset = blebrr_get_advdata_offset_pl ();
-
- blebrr_sleep = MS_FALSE;
-
- blebrr_adv.bearer_send = blebrr_adv_send;
- #ifdef BLEBRR_LP_SUPPORT
- blebrr_adv.bearer_sleep = blebrr_adv_sleep;
- blebrr_adv.bearer_wakeup = blebrr_adv_wakeup;
- blebrr_lp_thandle = EM_TIMER_HANDLE_INIT_VAL;
- #endif
- MS_brr_add_bearer(BRR_TYPE_ADV, &blebrr_adv, &blebrr_advhandle);
-
- EM_enable_module_debug_flag
- (
- MS_MODULE_ID_APP |
- MS_MODULE_ID_ACCESS |
- MS_MODULE_ID_TRN |
- MS_MODULE_ID_LTRN |
- MS_MODULE_ID_NET |
- MS_MODULE_ID_COMMON |
- MS_MODULE_ID_BRR |
- MS_MODULE_ID_STBX |
- MS_MODULE_ID_PROV
- );
-
- EM_set_debug_level(EM_DEBUG_LEVEL_TRC);
-
- EM_sleep (1);
- #if 0
-
- BLEBRR_LOG ("Start Observing...\n");
- blebrr_scan_pl (MS_TRUE);
-
- BLEBRR_SET_STATE(BLEBRR_STATE_IN_SCAN_ENABLE);
- #else
- BLEBRR_SET_STATE(BLEBRR_STATE_IDLE);
- #endif
- }
|